xref: /openbmc/linux/drivers/ata/libata-core.c (revision 61176eed)
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,
555c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
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 /**
577c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
578bd056d7eSTejun Heo  *	@tf: command to examine and configure
579bd056d7eSTejun Heo  *	@dev: device tf belongs to
580c6fd2807SJeff Garzik  *
581c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
582c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
583c6fd2807SJeff Garzik  *
584c6fd2807SJeff Garzik  *	LOCKING:
585c6fd2807SJeff Garzik  *	caller.
586c6fd2807SJeff Garzik  */
587bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
588c6fd2807SJeff Garzik {
589c6fd2807SJeff Garzik 	u8 cmd;
590c6fd2807SJeff Garzik 
591c6fd2807SJeff Garzik 	int index, fua, lba48, write;
592c6fd2807SJeff Garzik 
593c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
594c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
595c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
596c6fd2807SJeff Garzik 
597c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
598c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
599c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6009af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
601c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
602c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
603c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
604c6fd2807SJeff Garzik 	} else {
605c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
606c6fd2807SJeff Garzik 		index = 16;
607c6fd2807SJeff Garzik 	}
608c6fd2807SJeff Garzik 
609c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
610c6fd2807SJeff Garzik 	if (cmd) {
611c6fd2807SJeff Garzik 		tf->command = cmd;
612c6fd2807SJeff Garzik 		return 0;
613c6fd2807SJeff Garzik 	}
614c6fd2807SJeff Garzik 	return -1;
615c6fd2807SJeff Garzik }
616c6fd2807SJeff Garzik 
617c6fd2807SJeff Garzik /**
61835b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
61935b649feSTejun Heo  *	@tf: ATA taskfile of interest
62035b649feSTejun Heo  *	@dev: ATA device @tf belongs to
62135b649feSTejun Heo  *
62235b649feSTejun Heo  *	LOCKING:
62335b649feSTejun Heo  *	None.
62435b649feSTejun Heo  *
62535b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
62635b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
62735b649feSTejun Heo  *	flags select the address format to use.
62835b649feSTejun Heo  *
62935b649feSTejun Heo  *	RETURNS:
63035b649feSTejun Heo  *	Block address read from @tf.
63135b649feSTejun Heo  */
632cffd1ee9SHannes Reinecke u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
63335b649feSTejun Heo {
63435b649feSTejun Heo 	u64 block = 0;
63535b649feSTejun Heo 
636fe16d4f2STejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
63735b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
63835b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
63935b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
64044901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
64135b649feSTejun Heo 		} else
64235b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
64335b649feSTejun Heo 
64435b649feSTejun Heo 		block |= tf->lbah << 16;
64535b649feSTejun Heo 		block |= tf->lbam << 8;
64635b649feSTejun Heo 		block |= tf->lbal;
64735b649feSTejun Heo 	} else {
64835b649feSTejun Heo 		u32 cyl, head, sect;
64935b649feSTejun Heo 
65035b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
65135b649feSTejun Heo 		head = tf->device & 0xf;
65235b649feSTejun Heo 		sect = tf->lbal;
65335b649feSTejun Heo 
654ac8672eaSTejun Heo 		if (!sect) {
655a9a79dfeSJoe Perches 			ata_dev_warn(dev,
656a9a79dfeSJoe Perches 				     "device reported invalid CHS sector 0\n");
657cffd1ee9SHannes Reinecke 			return U64_MAX;
658ac8672eaSTejun Heo 		}
659ac8672eaSTejun Heo 
660ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
66135b649feSTejun Heo 	}
66235b649feSTejun Heo 
66335b649feSTejun Heo 	return block;
66435b649feSTejun Heo }
66535b649feSTejun Heo 
66635b649feSTejun Heo /**
667bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
668bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
669bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
670bd056d7eSTejun Heo  *	@block: Block address
671bd056d7eSTejun Heo  *	@n_block: Number of blocks
672bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
673bd056d7eSTejun Heo  *	@tag: tag
6748e061784SAdam Manzanares  *	@class: IO priority class
675bd056d7eSTejun Heo  *
676bd056d7eSTejun Heo  *	LOCKING:
677bd056d7eSTejun Heo  *	None.
678bd056d7eSTejun Heo  *
679bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
680bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
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  */
687bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
688bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
6898e061784SAdam Manzanares 		    unsigned int tag, int class)
690bd056d7eSTejun Heo {
691bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
692bd056d7eSTejun Heo 	tf->flags |= tf_flags;
693bd056d7eSTejun Heo 
6942e2cc676SJens Axboe 	if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
695bd056d7eSTejun Heo 		/* yay, NCQ */
696bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
697bd056d7eSTejun Heo 			return -ERANGE;
698bd056d7eSTejun Heo 
699bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
700bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
701bd056d7eSTejun Heo 
702bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
703bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
704bd056d7eSTejun Heo 		else
705bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
706bd056d7eSTejun Heo 
707bd056d7eSTejun Heo 		tf->nsect = tag << 3;
708bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
709bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
710bd056d7eSTejun Heo 
711bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
712bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
713bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
714bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
715bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
716bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
717bd056d7eSTejun Heo 
7189ca7cfa4SSergei Shtylyov 		tf->device = ATA_LBA;
719bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
720bd056d7eSTejun Heo 			tf->device |= 1 << 7;
7218e061784SAdam Manzanares 
7222360fa18SDamien Le Moal 		if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE &&
7232360fa18SDamien Le Moal 		    class == IOPRIO_CLASS_RT)
7242360fa18SDamien Le Moal 			tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO;
725bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
726bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
727bd056d7eSTejun Heo 
728bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
729bd056d7eSTejun Heo 			/* use LBA28 */
730bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
731bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
732bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
733bd056d7eSTejun Heo 				return -ERANGE;
734bd056d7eSTejun Heo 
735bd056d7eSTejun Heo 			/* use LBA48 */
736bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
737bd056d7eSTejun Heo 
738bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
739bd056d7eSTejun Heo 
740bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
741bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
742bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
743bd056d7eSTejun Heo 		} else
744bd056d7eSTejun Heo 			/* request too large even for LBA48 */
745bd056d7eSTejun Heo 			return -ERANGE;
746bd056d7eSTejun Heo 
747bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
748bd056d7eSTejun Heo 			return -EINVAL;
749bd056d7eSTejun Heo 
750bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
751bd056d7eSTejun Heo 
752bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
753bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
754bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
755bd056d7eSTejun Heo 
756bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
757bd056d7eSTejun Heo 	} else {
758bd056d7eSTejun Heo 		/* CHS */
759bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
760bd056d7eSTejun Heo 
761bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
762bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
763bd056d7eSTejun Heo 			return -ERANGE;
764bd056d7eSTejun Heo 
765bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
766bd056d7eSTejun Heo 			return -EINVAL;
767bd056d7eSTejun Heo 
768bd056d7eSTejun Heo 		/* Convert LBA to CHS */
769bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
770bd056d7eSTejun Heo 		cyl   = track / dev->heads;
771bd056d7eSTejun Heo 		head  = track % dev->heads;
772bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
773bd056d7eSTejun Heo 
774bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
775bd056d7eSTejun Heo 		   Cylinder: 0-65535
776bd056d7eSTejun Heo 		   Head: 0-15
777bd056d7eSTejun Heo 		   Sector: 1-255*/
778bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
779bd056d7eSTejun Heo 			return -ERANGE;
780bd056d7eSTejun Heo 
781bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
782bd056d7eSTejun Heo 		tf->lbal = sect;
783bd056d7eSTejun Heo 		tf->lbam = cyl;
784bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
785bd056d7eSTejun Heo 		tf->device |= head;
786bd056d7eSTejun Heo 	}
787bd056d7eSTejun Heo 
788bd056d7eSTejun Heo 	return 0;
789bd056d7eSTejun Heo }
790bd056d7eSTejun Heo 
791bd056d7eSTejun Heo /**
792c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
793c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
794c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
795c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
796c6fd2807SJeff Garzik  *
797c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
798c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
799c6fd2807SJeff Garzik  *
800c6fd2807SJeff Garzik  *	LOCKING:
801c6fd2807SJeff Garzik  *	None.
802c6fd2807SJeff Garzik  *
803c6fd2807SJeff Garzik  *	RETURNS:
804c6fd2807SJeff Garzik  *	Packed xfer_mask.
805c6fd2807SJeff Garzik  */
806f0a6d77bSSergey Shtylyov unsigned int ata_pack_xfermask(unsigned int pio_mask,
807f0a6d77bSSergey Shtylyov 			       unsigned int mwdma_mask,
808f0a6d77bSSergey Shtylyov 			       unsigned int udma_mask)
809c6fd2807SJeff Garzik {
810c6fd2807SJeff Garzik 	return	((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
811c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
812c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
813c6fd2807SJeff Garzik }
814a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pack_xfermask);
815c6fd2807SJeff Garzik 
816c6fd2807SJeff Garzik /**
817c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
818c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
819c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
820c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
821c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
822c6fd2807SJeff Garzik  *
823c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
824c9b5560aSMasanari Iida  *	Any NULL destination masks will be ignored.
825c6fd2807SJeff Garzik  */
826f0a6d77bSSergey Shtylyov void ata_unpack_xfermask(unsigned int xfer_mask, unsigned int *pio_mask,
827f0a6d77bSSergey Shtylyov 			 unsigned int *mwdma_mask, unsigned int *udma_mask)
828c6fd2807SJeff Garzik {
829c6fd2807SJeff Garzik 	if (pio_mask)
830c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
831c6fd2807SJeff Garzik 	if (mwdma_mask)
832c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
833c6fd2807SJeff Garzik 	if (udma_mask)
834c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
835c6fd2807SJeff Garzik }
836c6fd2807SJeff Garzik 
837c6fd2807SJeff Garzik static const struct ata_xfer_ent {
838c6fd2807SJeff Garzik 	int shift, bits;
839c6fd2807SJeff Garzik 	u8 base;
840c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
84170cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
84270cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
84370cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
844c6fd2807SJeff Garzik 	{ -1, },
845c6fd2807SJeff Garzik };
846c6fd2807SJeff Garzik 
847c6fd2807SJeff Garzik /**
848c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
849c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
850c6fd2807SJeff Garzik  *
851c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
852c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
853c6fd2807SJeff Garzik  *
854c6fd2807SJeff Garzik  *	LOCKING:
855c6fd2807SJeff Garzik  *	None.
856c6fd2807SJeff Garzik  *
857c6fd2807SJeff Garzik  *	RETURNS:
85870cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
859c6fd2807SJeff Garzik  */
860f0a6d77bSSergey Shtylyov u8 ata_xfer_mask2mode(unsigned int xfer_mask)
861c6fd2807SJeff Garzik {
862c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
863c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
864c6fd2807SJeff Garzik 
865c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
866c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
867c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
86870cd071eSTejun Heo 	return 0xff;
869c6fd2807SJeff Garzik }
870a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
871c6fd2807SJeff Garzik 
872c6fd2807SJeff Garzik /**
873c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
874c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
875c6fd2807SJeff Garzik  *
876c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
877c6fd2807SJeff Garzik  *
878c6fd2807SJeff Garzik  *	LOCKING:
879c6fd2807SJeff Garzik  *	None.
880c6fd2807SJeff Garzik  *
881c6fd2807SJeff Garzik  *	RETURNS:
882c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
883c6fd2807SJeff Garzik  */
884f0a6d77bSSergey Shtylyov unsigned int ata_xfer_mode2mask(u8 xfer_mode)
885c6fd2807SJeff Garzik {
886c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
887c6fd2807SJeff Garzik 
888c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
889c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
89070cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
89170cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
892c6fd2807SJeff Garzik 	return 0;
893c6fd2807SJeff Garzik }
894a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
895c6fd2807SJeff Garzik 
896c6fd2807SJeff Garzik /**
897c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
898c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
899c6fd2807SJeff Garzik  *
900c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
901c6fd2807SJeff Garzik  *
902c6fd2807SJeff Garzik  *	LOCKING:
903c6fd2807SJeff Garzik  *	None.
904c6fd2807SJeff Garzik  *
905c6fd2807SJeff Garzik  *	RETURNS:
906c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
907c6fd2807SJeff Garzik  */
908a28c1ab3SSergey Shtylyov int ata_xfer_mode2shift(u8 xfer_mode)
909c6fd2807SJeff Garzik {
910c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
911c6fd2807SJeff Garzik 
912c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
913c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
914c6fd2807SJeff Garzik 			return ent->shift;
915c6fd2807SJeff Garzik 	return -1;
916c6fd2807SJeff Garzik }
917a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
918c6fd2807SJeff Garzik 
919c6fd2807SJeff Garzik /**
920c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
921c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
922c6fd2807SJeff Garzik  *
923c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
924c6fd2807SJeff Garzik  *	(highest bit in @modemask).
925c6fd2807SJeff Garzik  *
926c6fd2807SJeff Garzik  *	LOCKING:
927c6fd2807SJeff Garzik  *	None.
928c6fd2807SJeff Garzik  *
929c6fd2807SJeff Garzik  *	RETURNS:
930c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
931c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
932c6fd2807SJeff Garzik  */
933f0a6d77bSSergey Shtylyov const char *ata_mode_string(unsigned int xfer_mask)
934c6fd2807SJeff Garzik {
935c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
936c6fd2807SJeff Garzik 		"PIO0",
937c6fd2807SJeff Garzik 		"PIO1",
938c6fd2807SJeff Garzik 		"PIO2",
939c6fd2807SJeff Garzik 		"PIO3",
940c6fd2807SJeff Garzik 		"PIO4",
941b352e57dSAlan Cox 		"PIO5",
942b352e57dSAlan Cox 		"PIO6",
943c6fd2807SJeff Garzik 		"MWDMA0",
944c6fd2807SJeff Garzik 		"MWDMA1",
945c6fd2807SJeff Garzik 		"MWDMA2",
946b352e57dSAlan Cox 		"MWDMA3",
947b352e57dSAlan Cox 		"MWDMA4",
948c6fd2807SJeff Garzik 		"UDMA/16",
949c6fd2807SJeff Garzik 		"UDMA/25",
950c6fd2807SJeff Garzik 		"UDMA/33",
951c6fd2807SJeff Garzik 		"UDMA/44",
952c6fd2807SJeff Garzik 		"UDMA/66",
953c6fd2807SJeff Garzik 		"UDMA/100",
954c6fd2807SJeff Garzik 		"UDMA/133",
955c6fd2807SJeff Garzik 		"UDMA7",
956c6fd2807SJeff Garzik 	};
957c6fd2807SJeff Garzik 	int highbit;
958c6fd2807SJeff Garzik 
959c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
960c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
961c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
962c6fd2807SJeff Garzik 	return "<n/a>";
963c6fd2807SJeff Garzik }
964a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_mode_string);
965c6fd2807SJeff Garzik 
966d9027470SGwendal Grignou const char *sata_spd_string(unsigned int spd)
967c6fd2807SJeff Garzik {
968c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
969c6fd2807SJeff Garzik 		"1.5 Gbps",
970c6fd2807SJeff Garzik 		"3.0 Gbps",
9718522ee25SShane Huang 		"6.0 Gbps",
972c6fd2807SJeff Garzik 	};
973c6fd2807SJeff Garzik 
974c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
975c6fd2807SJeff Garzik 		return "<unknown>";
976c6fd2807SJeff Garzik 	return spd_str[spd - 1];
977c6fd2807SJeff Garzik }
978c6fd2807SJeff Garzik 
979c6fd2807SJeff Garzik /**
980c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
981c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
982c6fd2807SJeff Garzik  *
983c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
984c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
985c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
986c6fd2807SJeff Garzik  *
987c6fd2807SJeff Garzik  *	LOCKING:
988c6fd2807SJeff Garzik  *	None.
989c6fd2807SJeff Garzik  *
990c6fd2807SJeff Garzik  *	RETURNS:
9919162c657SHannes Reinecke  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
9929162c657SHannes Reinecke  *	%ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
993c6fd2807SJeff Garzik  */
994c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
995c6fd2807SJeff Garzik {
996c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
997c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
998c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
999633273a3STejun Heo 	 *
1000633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1001633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1002633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1003633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1004633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1005633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1006633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1007633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1008633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1009633273a3STejun Heo 	 * SerialATA.
1010633273a3STejun Heo 	 *
1011633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1012633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
101379b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
101479b42babSTejun Heo 	 * SEMB signature.  This is worked around in
101579b42babSTejun Heo 	 * ata_dev_read_id().
1016c6fd2807SJeff Garzik 	 */
10176c952a0dSHannes Reinecke 	if (tf->lbam == 0 && tf->lbah == 0)
1018c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1019c6fd2807SJeff Garzik 
10206c952a0dSHannes Reinecke 	if (tf->lbam == 0x14 && tf->lbah == 0xeb)
1021c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1022c6fd2807SJeff Garzik 
10236c952a0dSHannes Reinecke 	if (tf->lbam == 0x69 && tf->lbah == 0x96)
1024633273a3STejun Heo 		return ATA_DEV_PMP;
1025633273a3STejun Heo 
10266c952a0dSHannes Reinecke 	if (tf->lbam == 0x3c && tf->lbah == 0xc3)
102779b42babSTejun Heo 		return ATA_DEV_SEMB;
1028633273a3STejun Heo 
10296c952a0dSHannes Reinecke 	if (tf->lbam == 0xcd && tf->lbah == 0xab)
10309162c657SHannes Reinecke 		return ATA_DEV_ZAC;
10319162c657SHannes Reinecke 
1032c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1033c6fd2807SJeff Garzik }
1034a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_classify);
1035c6fd2807SJeff Garzik 
1036c6fd2807SJeff Garzik /**
1037c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1038c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1039c6fd2807SJeff Garzik  *	@s: string into which data is output
1040c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1041c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1042c6fd2807SJeff Garzik  *
1043c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1044c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1045c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1046c6fd2807SJeff Garzik  *
1047c6fd2807SJeff Garzik  *	LOCKING:
1048c6fd2807SJeff Garzik  *	caller.
1049c6fd2807SJeff Garzik  */
1050c6fd2807SJeff Garzik 
1051c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1052c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1053c6fd2807SJeff Garzik {
1054c6fd2807SJeff Garzik 	unsigned int c;
1055c6fd2807SJeff Garzik 
1056963e4975SAlan Cox 	BUG_ON(len & 1);
1057963e4975SAlan Cox 
1058c6fd2807SJeff Garzik 	while (len > 0) {
1059c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1060c6fd2807SJeff Garzik 		*s = c;
1061c6fd2807SJeff Garzik 		s++;
1062c6fd2807SJeff Garzik 
1063c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1064c6fd2807SJeff Garzik 		*s = c;
1065c6fd2807SJeff Garzik 		s++;
1066c6fd2807SJeff Garzik 
1067c6fd2807SJeff Garzik 		ofs++;
1068c6fd2807SJeff Garzik 		len -= 2;
1069c6fd2807SJeff Garzik 	}
1070c6fd2807SJeff Garzik }
1071a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_string);
1072c6fd2807SJeff Garzik 
1073c6fd2807SJeff Garzik /**
1074c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1075c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1076c6fd2807SJeff Garzik  *	@s: string into which data is output
1077c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1078c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1079c6fd2807SJeff Garzik  *
1080c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1081c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1082c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1083c6fd2807SJeff Garzik  *
1084c6fd2807SJeff Garzik  *	LOCKING:
1085c6fd2807SJeff Garzik  *	caller.
1086c6fd2807SJeff Garzik  */
1087c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1088c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1089c6fd2807SJeff Garzik {
1090c6fd2807SJeff Garzik 	unsigned char *p;
1091c6fd2807SJeff Garzik 
1092c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1093c6fd2807SJeff Garzik 
1094c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1095c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1096c6fd2807SJeff Garzik 		p--;
1097c6fd2807SJeff Garzik 	*p = '\0';
1098c6fd2807SJeff Garzik }
1099a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_c_string);
1100c6fd2807SJeff Garzik 
1101db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1102db6f8759STejun Heo {
1103db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1104db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1105968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
11065eb8deb4SSergey Shtylyov 
1107968e594aSRobert Hancock 		return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1108db6f8759STejun Heo 	}
11095eb8deb4SSergey Shtylyov 
11105eb8deb4SSergey Shtylyov 	if (ata_id_current_chs_valid(id))
11115eb8deb4SSergey Shtylyov 		return (u32)id[ATA_ID_CUR_CYLS] * (u32)id[ATA_ID_CUR_HEADS] *
11125eb8deb4SSergey Shtylyov 		       (u32)id[ATA_ID_CUR_SECTORS];
11135eb8deb4SSergey Shtylyov 
11145eb8deb4SSergey Shtylyov 	return (u32)id[ATA_ID_CYLS] * (u32)id[ATA_ID_HEADS] *
11155eb8deb4SSergey Shtylyov 	       (u32)id[ATA_ID_SECTORS];
1116db6f8759STejun Heo }
1117db6f8759STejun Heo 
1118a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
11191e999736SAlan Cox {
11201e999736SAlan Cox 	u64 sectors = 0;
11211e999736SAlan Cox 
11221e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
11231e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1124ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
11251e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11261e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11271e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11281e999736SAlan Cox 
1129a5987e0aSTejun Heo 	return sectors;
11301e999736SAlan Cox }
11311e999736SAlan Cox 
1132a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
11331e999736SAlan Cox {
11341e999736SAlan Cox 	u64 sectors = 0;
11351e999736SAlan Cox 
11361e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
11371e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11381e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11391e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11401e999736SAlan Cox 
1141a5987e0aSTejun Heo 	return sectors;
11421e999736SAlan Cox }
11431e999736SAlan Cox 
11441e999736SAlan Cox /**
1145c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1146c728a914STejun Heo  *	@dev: target device
1147c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
11481e999736SAlan Cox  *
1149c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1150c728a914STejun Heo  *	question.
1151c728a914STejun Heo  *
1152c728a914STejun Heo  *	RETURNS:
1153c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1154c728a914STejun Heo  *	-EIO on other errors.
11551e999736SAlan Cox  */
1156c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
11571e999736SAlan Cox {
1158c728a914STejun Heo 	unsigned int err_mask;
11591e999736SAlan Cox 	struct ata_taskfile tf;
1160c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
11611e999736SAlan Cox 
11621e999736SAlan Cox 	ata_tf_init(dev, &tf);
11631e999736SAlan Cox 
1164c728a914STejun Heo 	/* always clear all address registers */
11651e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1166c728a914STejun Heo 
1167c728a914STejun Heo 	if (lba48) {
1168c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1169c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1170c728a914STejun Heo 	} else
1171c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1172c728a914STejun Heo 
1173bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1174c728a914STejun Heo 	tf.device |= ATA_LBA;
11751e999736SAlan Cox 
11762b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1177c728a914STejun Heo 	if (err_mask) {
1178a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1179a9a79dfeSJoe Perches 			     "failed to read native max address (err_mask=0x%x)\n",
1180a9a79dfeSJoe Perches 			     err_mask);
1181efcef265SSergey Shtylyov 		if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
1182c728a914STejun Heo 			return -EACCES;
1183c728a914STejun Heo 		return -EIO;
1184c728a914STejun Heo 	}
1185c728a914STejun Heo 
1186c728a914STejun Heo 	if (lba48)
1187a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1188c728a914STejun Heo 	else
1189a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
119093328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
119193328e11SAlan Cox 		(*max_sectors)--;
11921e999736SAlan Cox 	return 0;
11931e999736SAlan Cox }
11941e999736SAlan Cox 
11951e999736SAlan Cox /**
1196c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1197c728a914STejun Heo  *	@dev: target device
11986b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
11991e999736SAlan Cox  *
1200c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1201c728a914STejun Heo  *
1202c728a914STejun Heo  *	RETURNS:
1203c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1204c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1205c728a914STejun Heo  *	errors.
12061e999736SAlan Cox  */
120705027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
12081e999736SAlan Cox {
1209c728a914STejun Heo 	unsigned int err_mask;
12101e999736SAlan Cox 	struct ata_taskfile tf;
1211c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12121e999736SAlan Cox 
12131e999736SAlan Cox 	new_sectors--;
12141e999736SAlan Cox 
12151e999736SAlan Cox 	ata_tf_init(dev, &tf);
12161e999736SAlan Cox 
1217c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
12181e999736SAlan Cox 
1219c728a914STejun Heo 	if (lba48) {
1220c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1221c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
12221e999736SAlan Cox 
12231e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
12241e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
12251e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
12261e582ba4STejun Heo 	} else {
12271e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1228c728a914STejun Heo 
12291e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
12301e582ba4STejun Heo 	}
12311e582ba4STejun Heo 
1232bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1233c728a914STejun Heo 	tf.device |= ATA_LBA;
12341e999736SAlan Cox 
12351e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
12361e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
12371e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
12381e999736SAlan Cox 
12392b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1240c728a914STejun Heo 	if (err_mask) {
1241a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1242a9a79dfeSJoe Perches 			     "failed to set max address (err_mask=0x%x)\n",
1243a9a79dfeSJoe Perches 			     err_mask);
1244c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1245efcef265SSergey Shtylyov 		    (tf.error & (ATA_ABORTED | ATA_IDNF)))
1246c728a914STejun Heo 			return -EACCES;
1247c728a914STejun Heo 		return -EIO;
1248c728a914STejun Heo 	}
1249c728a914STejun Heo 
12501e999736SAlan Cox 	return 0;
12511e999736SAlan Cox }
12521e999736SAlan Cox 
12531e999736SAlan Cox /**
12541e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
12551e999736SAlan Cox  *	@dev: Device to resize
12561e999736SAlan Cox  *
12571e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
12581e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
12591e999736SAlan Cox  *	the drive has the HPA feature set enabled.
126005027adcSTejun Heo  *
126105027adcSTejun Heo  *	RETURNS:
126205027adcSTejun Heo  *	0 on success, -errno on failure.
12631e999736SAlan Cox  */
126405027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
12651e999736SAlan Cox {
1266891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
1267445d211bSTejun Heo 	bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
126805027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
126905027adcSTejun Heo 	u64 native_sectors;
1270c728a914STejun Heo 	int rc;
12711e999736SAlan Cox 
127205027adcSTejun Heo 	/* do we need to do it? */
12739162c657SHannes Reinecke 	if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
127405027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
127505027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1276c728a914STejun Heo 		return 0;
12771e999736SAlan Cox 
127805027adcSTejun Heo 	/* read native max address */
127905027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
128005027adcSTejun Heo 	if (rc) {
1281dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1282dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
128305027adcSTejun Heo 		 */
1284445d211bSTejun Heo 		if (rc == -EACCES || !unlock_hpa) {
1285a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1286a9a79dfeSJoe Perches 				     "HPA support seems broken, skipping HPA handling\n");
128705027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
128805027adcSTejun Heo 
128905027adcSTejun Heo 			/* we can continue if device aborted the command */
129005027adcSTejun Heo 			if (rc == -EACCES)
129105027adcSTejun Heo 				rc = 0;
129205027adcSTejun Heo 		}
129305027adcSTejun Heo 
129405027adcSTejun Heo 		return rc;
129505027adcSTejun Heo 	}
12965920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
129705027adcSTejun Heo 
129805027adcSTejun Heo 	/* nothing to do? */
1299445d211bSTejun Heo 	if (native_sectors <= sectors || !unlock_hpa) {
130005027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
130105027adcSTejun Heo 			return 0;
130205027adcSTejun Heo 
130305027adcSTejun Heo 		if (native_sectors > sectors)
1304a9a79dfeSJoe Perches 			ata_dev_info(dev,
130505027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
130605027adcSTejun Heo 				(unsigned long long)sectors,
130705027adcSTejun Heo 				(unsigned long long)native_sectors);
130805027adcSTejun Heo 		else if (native_sectors < sectors)
1309a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1310a9a79dfeSJoe Perches 				"native sectors (%llu) is smaller than sectors (%llu)\n",
131105027adcSTejun Heo 				(unsigned long long)native_sectors,
131205027adcSTejun Heo 				(unsigned long long)sectors);
131305027adcSTejun Heo 		return 0;
13141e999736SAlan Cox 	}
131537301a55STejun Heo 
131605027adcSTejun Heo 	/* let's unlock HPA */
131705027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
131805027adcSTejun Heo 	if (rc == -EACCES) {
131905027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
1320a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1321a9a79dfeSJoe Perches 			     "device aborted resize (%llu -> %llu), skipping HPA handling\n",
132205027adcSTejun Heo 			     (unsigned long long)sectors,
132305027adcSTejun Heo 			     (unsigned long long)native_sectors);
132405027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
132505027adcSTejun Heo 		return 0;
132605027adcSTejun Heo 	} else if (rc)
132705027adcSTejun Heo 		return rc;
132805027adcSTejun Heo 
132905027adcSTejun Heo 	/* re-read IDENTIFY data */
133005027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
133105027adcSTejun Heo 	if (rc) {
1332a9a79dfeSJoe Perches 		ata_dev_err(dev,
1333a9a79dfeSJoe Perches 			    "failed to re-read IDENTIFY data after HPA resizing\n");
133405027adcSTejun Heo 		return rc;
133505027adcSTejun Heo 	}
133605027adcSTejun Heo 
133705027adcSTejun Heo 	if (print_info) {
133805027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
1339a9a79dfeSJoe Perches 		ata_dev_info(dev,
134005027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
134105027adcSTejun Heo 			(unsigned long long)sectors,
134205027adcSTejun Heo 			(unsigned long long)new_sectors,
134305027adcSTejun Heo 			(unsigned long long)native_sectors);
134405027adcSTejun Heo 	}
134505027adcSTejun Heo 
134605027adcSTejun Heo 	return 0;
13471e999736SAlan Cox }
13481e999736SAlan Cox 
1349c6fd2807SJeff Garzik /**
1350c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
13516044f3c4SHannes Reinecke  *	@dev: device from which the information is fetched
1352c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1353c6fd2807SJeff Garzik  *
1354c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1355c6fd2807SJeff Garzik  *	page.
1356c6fd2807SJeff Garzik  *
1357c6fd2807SJeff Garzik  *	LOCKING:
1358c6fd2807SJeff Garzik  *	caller.
1359c6fd2807SJeff Garzik  */
1360c6fd2807SJeff Garzik 
13616044f3c4SHannes Reinecke static inline void ata_dump_id(struct ata_device *dev, const u16 *id)
1362c6fd2807SJeff Garzik {
13636044f3c4SHannes Reinecke 	ata_dev_dbg(dev,
13646044f3c4SHannes Reinecke 		"49==0x%04x  53==0x%04x  63==0x%04x  64==0x%04x  75==0x%04x\n"
13656044f3c4SHannes Reinecke 		"80==0x%04x  81==0x%04x  82==0x%04x  83==0x%04x  84==0x%04x\n"
13666044f3c4SHannes Reinecke 		"88==0x%04x  93==0x%04x\n",
13676044f3c4SHannes Reinecke 		id[49], id[53], id[63], id[64], id[75], id[80],
13686044f3c4SHannes Reinecke 		id[81], id[82], id[83], id[84], id[88], id[93]);
1369c6fd2807SJeff Garzik }
1370c6fd2807SJeff Garzik 
1371c6fd2807SJeff Garzik /**
1372c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1373c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1374c6fd2807SJeff Garzik  *
1375c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1376c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1377c6fd2807SJeff Garzik  *
1378c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1379c6fd2807SJeff Garzik  *
1380c6fd2807SJeff Garzik  *	LOCKING:
1381c6fd2807SJeff Garzik  *	None.
1382c6fd2807SJeff Garzik  *
1383c6fd2807SJeff Garzik  *	RETURNS:
1384c6fd2807SJeff Garzik  *	Computed xfermask
1385c6fd2807SJeff Garzik  */
1386f0a6d77bSSergey Shtylyov unsigned int ata_id_xfermask(const u16 *id)
1387c6fd2807SJeff Garzik {
1388f0a6d77bSSergey Shtylyov 	unsigned int pio_mask, mwdma_mask, udma_mask;
1389c6fd2807SJeff Garzik 
1390c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1391c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1392c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1393c6fd2807SJeff Garzik 		pio_mask <<= 3;
1394c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1395c6fd2807SJeff Garzik 	} else {
1396c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1397c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1398c6fd2807SJeff Garzik 		 * a mask.
1399c6fd2807SJeff Garzik 		 */
14007a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
140146767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
140246767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
140346767aebSAlan Cox 		else
140446767aebSAlan Cox 			pio_mask = 1;
1405c6fd2807SJeff Garzik 
1406c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1407c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1408e0af10acSDiego Viola 		 * process. However it is the speeds not the modes that
1409c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1410c6fd2807SJeff Garzik 		 * will get this right anyway
1411c6fd2807SJeff Garzik 		 */
1412c6fd2807SJeff Garzik 	}
1413c6fd2807SJeff Garzik 
1414c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1415c6fd2807SJeff Garzik 
1416b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1417b352e57dSAlan Cox 		/*
1418b352e57dSAlan Cox 		 *	Process compact flash extended modes
1419b352e57dSAlan Cox 		 */
142062afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
142162afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1422b352e57dSAlan Cox 
1423b352e57dSAlan Cox 		if (pio)
1424b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1425b352e57dSAlan Cox 		if (pio > 1)
1426b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1427b352e57dSAlan Cox 		if (dma)
1428b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1429b352e57dSAlan Cox 		if (dma > 1)
1430b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1431b352e57dSAlan Cox 	}
1432b352e57dSAlan Cox 
1433c6fd2807SJeff Garzik 	udma_mask = 0;
1434c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1435c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1436c6fd2807SJeff Garzik 
1437c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1438c6fd2807SJeff Garzik }
1439a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_xfermask);
1440c6fd2807SJeff Garzik 
14417102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1442c6fd2807SJeff Garzik {
1443c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1444c6fd2807SJeff Garzik 
1445c6fd2807SJeff Garzik 	complete(waiting);
1446c6fd2807SJeff Garzik }
1447c6fd2807SJeff Garzik 
1448c6fd2807SJeff Garzik /**
14492432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1450c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1451c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1452c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1453e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
14545c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
14552432697bSTejun Heo  *	@n_elem: Number of sg entries
14562b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1457c6fd2807SJeff Garzik  *
1458c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1459c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1460c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1461c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1462c6fd2807SJeff Garzik  *	clean up after timeout.
1463c6fd2807SJeff Garzik  *
1464c6fd2807SJeff Garzik  *	LOCKING:
1465c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1466c6fd2807SJeff Garzik  *
1467c6fd2807SJeff Garzik  *	RETURNS:
1468c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1469c6fd2807SJeff Garzik  */
14704d6119f0SSergey Shtylyov static unsigned ata_exec_internal_sg(struct ata_device *dev,
1471c6fd2807SJeff Garzik 				     struct ata_taskfile *tf, const u8 *cdb,
147287260216SJens Axboe 				     int dma_dir, struct scatterlist *sgl,
1473*61176eedSSergey Shtylyov 				     unsigned int n_elem, unsigned int timeout)
1474c6fd2807SJeff Garzik {
14759af5c9c9STejun Heo 	struct ata_link *link = dev->link;
14769af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1477c6fd2807SJeff Garzik 	u8 command = tf->command;
147887fbc5a0STejun Heo 	int auto_timeout = 0;
1479c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
148028361c40SJens Axboe 	unsigned int preempted_tag;
1481e3ed8939SJens Axboe 	u32 preempted_sactive;
1482e3ed8939SJens Axboe 	u64 preempted_qc_active;
1483da917d69STejun Heo 	int preempted_nr_active_links;
1484c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1485c6fd2807SJeff Garzik 	unsigned long flags;
1486c6fd2807SJeff Garzik 	unsigned int err_mask;
1487c6fd2807SJeff Garzik 	int rc;
1488c6fd2807SJeff Garzik 
1489c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1490c6fd2807SJeff Garzik 
1491c6fd2807SJeff Garzik 	/* no internal command while frozen */
1492c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1493c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1494c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1495c6fd2807SJeff Garzik 	}
1496c6fd2807SJeff Garzik 
1497c6fd2807SJeff Garzik 	/* initialize internal qc */
149828361c40SJens Axboe 	qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
1499c6fd2807SJeff Garzik 
150028361c40SJens Axboe 	qc->tag = ATA_TAG_INTERNAL;
150128361c40SJens Axboe 	qc->hw_tag = 0;
1502c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1503c6fd2807SJeff Garzik 	qc->ap = ap;
1504c6fd2807SJeff Garzik 	qc->dev = dev;
1505c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1506c6fd2807SJeff Garzik 
15079af5c9c9STejun Heo 	preempted_tag = link->active_tag;
15089af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1509c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1510da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
15119af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
15129af5c9c9STejun Heo 	link->sactive = 0;
1513c6fd2807SJeff Garzik 	ap->qc_active = 0;
1514da917d69STejun Heo 	ap->nr_active_links = 0;
1515c6fd2807SJeff Garzik 
1516c6fd2807SJeff Garzik 	/* prepare & issue qc */
1517c6fd2807SJeff Garzik 	qc->tf = *tf;
1518c6fd2807SJeff Garzik 	if (cdb)
1519c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1520e771451cSVincent Pelletier 
1521e771451cSVincent Pelletier 	/* some SATA bridges need us to indicate data xfer direction */
1522e771451cSVincent Pelletier 	if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1523e771451cSVincent Pelletier 	    dma_dir == DMA_FROM_DEVICE)
1524e771451cSVincent Pelletier 		qc->tf.feature |= ATAPI_DMADIR;
1525e771451cSVincent Pelletier 
1526c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1527c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1528c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
15292432697bSTejun Heo 		unsigned int i, buflen = 0;
153087260216SJens Axboe 		struct scatterlist *sg;
15312432697bSTejun Heo 
153287260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
153387260216SJens Axboe 			buflen += sg->length;
15342432697bSTejun Heo 
153587260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
153649c80429SBrian King 		qc->nbytes = buflen;
1537c6fd2807SJeff Garzik 	}
1538c6fd2807SJeff Garzik 
1539c6fd2807SJeff Garzik 	qc->private_data = &wait;
1540c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1541c6fd2807SJeff Garzik 
1542c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1543c6fd2807SJeff Garzik 
1544c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1545c6fd2807SJeff Garzik 
154687fbc5a0STejun Heo 	if (!timeout) {
154787fbc5a0STejun Heo 		if (ata_probe_timeout)
1548341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
154987fbc5a0STejun Heo 		else {
155087fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
155187fbc5a0STejun Heo 			auto_timeout = 1;
155287fbc5a0STejun Heo 		}
155387fbc5a0STejun Heo 	}
15542b789108STejun Heo 
1555c0c362b6STejun Heo 	if (ap->ops->error_handler)
1556c0c362b6STejun Heo 		ata_eh_release(ap);
1557c0c362b6STejun Heo 
15582b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1559c6fd2807SJeff Garzik 
1560c0c362b6STejun Heo 	if (ap->ops->error_handler)
1561c0c362b6STejun Heo 		ata_eh_acquire(ap);
1562c0c362b6STejun Heo 
1563c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1564c6fd2807SJeff Garzik 
1565c6fd2807SJeff Garzik 	if (!rc) {
1566c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1567c6fd2807SJeff Garzik 
1568c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1569c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1570c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1571c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1572c6fd2807SJeff Garzik 		 */
1573c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1574c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1575c6fd2807SJeff Garzik 
1576c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1577c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1578c6fd2807SJeff Garzik 			else
1579c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1580c6fd2807SJeff Garzik 
1581a9a79dfeSJoe Perches 			ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1582a9a79dfeSJoe Perches 				     command);
1583c6fd2807SJeff Garzik 		}
1584c6fd2807SJeff Garzik 
1585c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1586c6fd2807SJeff Garzik 	}
1587c6fd2807SJeff Garzik 
1588c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1589c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1590c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1591c6fd2807SJeff Garzik 
1592a51d644aSTejun Heo 	/* perform minimal error analysis */
1593a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1594efcef265SSergey Shtylyov 		if (qc->result_tf.status & (ATA_ERR | ATA_DF))
1595a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1596a51d644aSTejun Heo 
1597a51d644aSTejun Heo 		if (!qc->err_mask)
1598c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1599a51d644aSTejun Heo 
1600a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1601a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
16022dae9955SDamien Le Moal 	} else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1603efcef265SSergey Shtylyov 		qc->result_tf.status |= ATA_SENSE;
1604c6fd2807SJeff Garzik 	}
1605c6fd2807SJeff Garzik 
1606c6fd2807SJeff Garzik 	/* finish up */
1607c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1608c6fd2807SJeff Garzik 
1609c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1610c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1611c6fd2807SJeff Garzik 
1612c6fd2807SJeff Garzik 	ata_qc_free(qc);
16139af5c9c9STejun Heo 	link->active_tag = preempted_tag;
16149af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1615c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1616da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1617c6fd2807SJeff Garzik 
1618c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1619c6fd2807SJeff Garzik 
162087fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
162187fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
162287fbc5a0STejun Heo 
1623c6fd2807SJeff Garzik 	return err_mask;
1624c6fd2807SJeff Garzik }
1625c6fd2807SJeff Garzik 
1626c6fd2807SJeff Garzik /**
162733480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
16282432697bSTejun Heo  *	@dev: Device to which the command is sent
16292432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
16302432697bSTejun Heo  *	@cdb: CDB for packet command
1631e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
16322432697bSTejun Heo  *	@buf: Data buffer of the command
16332432697bSTejun Heo  *	@buflen: Length of data buffer
16342b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
16352432697bSTejun Heo  *
16362432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
16372432697bSTejun Heo  *	buffer instead of sg list.
16382432697bSTejun Heo  *
16392432697bSTejun Heo  *	LOCKING:
16402432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
16412432697bSTejun Heo  *
16422432697bSTejun Heo  *	RETURNS:
16432432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
16442432697bSTejun Heo  */
16452432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
16462432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
16472b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
1648*61176eedSSergey Shtylyov 			   unsigned int timeout)
16492432697bSTejun Heo {
165033480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
165133480a0eSTejun Heo 	unsigned int n_elem = 0;
16522432697bSTejun Heo 
165333480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
165433480a0eSTejun Heo 		WARN_ON(!buf);
16552432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
165633480a0eSTejun Heo 		psg = &sg;
165733480a0eSTejun Heo 		n_elem++;
165833480a0eSTejun Heo 	}
16592432697bSTejun Heo 
16602b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
16612b789108STejun Heo 				    timeout);
16622432697bSTejun Heo }
16632432697bSTejun Heo 
16642432697bSTejun Heo /**
1665c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1666c6fd2807SJeff Garzik  *	@adev: ATA device
1667c6fd2807SJeff Garzik  *
1668c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1669c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1670c6fd2807SJeff Garzik  */
1671c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1672c6fd2807SJeff Garzik {
16730d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
16740d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
16750d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
16760d9e6659STejun Heo 	 */
16770d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
16780d9e6659STejun Heo 		return 0;
16790d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
16800d9e6659STejun Heo 	 * check as the caller should know this.
16810d9e6659STejun Heo 	 */
16829af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1683c6fd2807SJeff Garzik 		return 0;
16845c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
16855c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
16865c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
16875c18c4d2SDavid Daney 		return 0;
1688432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1689432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1690c6fd2807SJeff Garzik 		return 1;
1691432729f0SAlan Cox 	/* We turn it on when possible */
1692432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1693432729f0SAlan Cox 		return 1;
1694432729f0SAlan Cox 	return 0;
1695432729f0SAlan Cox }
1696a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
1697c6fd2807SJeff Garzik 
1698432729f0SAlan Cox /**
1699432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1700432729f0SAlan Cox  *	@adev: ATA device
1701432729f0SAlan Cox  *
1702432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1703432729f0SAlan Cox  *	-1 if no iordy mode is available.
1704432729f0SAlan Cox  */
1705432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1706432729f0SAlan Cox {
1707c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1708c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1709432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1710c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1711c6fd2807SJeff Garzik 		if (pio) {
1712c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1713c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1714432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1715432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1716c6fd2807SJeff Garzik 		}
1717c6fd2807SJeff Garzik 	}
1718432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1719c6fd2807SJeff Garzik }
1720c6fd2807SJeff Garzik 
1721c6fd2807SJeff Garzik /**
1722963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1723963e4975SAlan Cox  *	@dev: device
1724963e4975SAlan Cox  *	@tf: proposed taskfile
1725963e4975SAlan Cox  *	@id: data buffer
1726963e4975SAlan Cox  *
1727963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1728963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1729963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1730963e4975SAlan Cox  */
1731963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
17320561e514SDamien Le Moal 				struct ata_taskfile *tf, __le16 *id)
1733963e4975SAlan Cox {
1734963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1735963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1736963e4975SAlan Cox }
1737a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
1738963e4975SAlan Cox 
1739963e4975SAlan Cox /**
1740c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1741c6fd2807SJeff Garzik  *	@dev: target device
1742c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1743bff04647STejun Heo  *	@flags: ATA_READID_* flags
1744c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1745c6fd2807SJeff Garzik  *
1746c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1747c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1748c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1749c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1750c6fd2807SJeff Garzik  *
175150a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
175250a99018SAlan Cox  *	now we abort if we hit that case.
175350a99018SAlan Cox  *
1754c6fd2807SJeff Garzik  *	LOCKING:
1755c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1756c6fd2807SJeff Garzik  *
1757c6fd2807SJeff Garzik  *	RETURNS:
1758c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1759c6fd2807SJeff Garzik  */
1760c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1761bff04647STejun Heo 		    unsigned int flags, u16 *id)
1762c6fd2807SJeff Garzik {
17639af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1764c6fd2807SJeff Garzik 	unsigned int class = *p_class;
1765c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1766c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
1767c6fd2807SJeff Garzik 	const char *reason;
176879b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
176954936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
1770c6fd2807SJeff Garzik 	int rc;
1771c6fd2807SJeff Garzik 
1772c6fd2807SJeff Garzik retry:
1773c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1774c6fd2807SJeff Garzik 
1775c6fd2807SJeff Garzik 	switch (class) {
177679b42babSTejun Heo 	case ATA_DEV_SEMB:
177779b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
1778df561f66SGustavo A. R. Silva 		fallthrough;
1779c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
17809162c657SHannes Reinecke 	case ATA_DEV_ZAC:
1781c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1782c6fd2807SJeff Garzik 		break;
1783c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1784c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1785c6fd2807SJeff Garzik 		break;
1786c6fd2807SJeff Garzik 	default:
1787c6fd2807SJeff Garzik 		rc = -ENODEV;
1788c6fd2807SJeff Garzik 		reason = "unsupported class";
1789c6fd2807SJeff Garzik 		goto err_out;
1790c6fd2807SJeff Garzik 	}
1791c6fd2807SJeff Garzik 
1792c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
179381afe893STejun Heo 
179481afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
179581afe893STejun Heo 	 * sure those are properly initialized.
179681afe893STejun Heo 	 */
179781afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
179881afe893STejun Heo 
179981afe893STejun Heo 	/* Device presence detection is unreliable on some
180081afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
180181afe893STejun Heo 	 */
180281afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1803c6fd2807SJeff Garzik 
1804963e4975SAlan Cox 	if (ap->ops->read_id)
18050561e514SDamien Le Moal 		err_mask = ap->ops->read_id(dev, &tf, (__le16 *)id);
1806963e4975SAlan Cox 	else
18070561e514SDamien Le Moal 		err_mask = ata_do_dev_read_id(dev, &tf, (__le16 *)id);
1808963e4975SAlan Cox 
1809c6fd2807SJeff Garzik 	if (err_mask) {
1810800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1811a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
181255a8e2c8STejun Heo 			return -ENOENT;
181355a8e2c8STejun Heo 		}
181455a8e2c8STejun Heo 
181579b42babSTejun Heo 		if (is_semb) {
1816a9a79dfeSJoe Perches 			ata_dev_info(dev,
1817a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
181879b42babSTejun Heo 			/* SEMB is not supported yet */
181979b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
182079b42babSTejun Heo 			return 0;
182179b42babSTejun Heo 		}
182279b42babSTejun Heo 
1823efcef265SSergey Shtylyov 		if ((err_mask == AC_ERR_DEV) && (tf.error & ATA_ABORTED)) {
18241ffc151fSTejun Heo 			/* Device or controller might have reported
18251ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
18261ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
18271ffc151fSTejun Heo 			 * aborted by the device.
182854936f8bSTejun Heo 			 */
18291ffc151fSTejun Heo 			if (may_fallback) {
183054936f8bSTejun Heo 				may_fallback = 0;
183154936f8bSTejun Heo 
183254936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
183354936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
183454936f8bSTejun Heo 				else
183554936f8bSTejun Heo 					class = ATA_DEV_ATA;
183654936f8bSTejun Heo 				goto retry;
183754936f8bSTejun Heo 			}
183854936f8bSTejun Heo 
18391ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
18401ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
18411ffc151fSTejun Heo 			 * sometimes with phantom devices.
18421ffc151fSTejun Heo 			 */
1843a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
18441ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
18451ffc151fSTejun Heo 			return -ENOENT;
18461ffc151fSTejun Heo 		}
18471ffc151fSTejun Heo 
1848c6fd2807SJeff Garzik 		rc = -EIO;
1849c6fd2807SJeff Garzik 		reason = "I/O error";
1850c6fd2807SJeff Garzik 		goto err_out;
1851c6fd2807SJeff Garzik 	}
1852c6fd2807SJeff Garzik 
185343c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
18544baa5745SHannes Reinecke 		ata_dev_info(dev, "dumping IDENTIFY data, "
185543c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
185643c9c591STejun Heo 			    class, may_fallback, tried_spinup);
18574baa5745SHannes Reinecke 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
185843c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
185943c9c591STejun Heo 	}
186043c9c591STejun Heo 
186154936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
186254936f8bSTejun Heo 	 * successfully at least once.
186354936f8bSTejun Heo 	 */
186454936f8bSTejun Heo 	may_fallback = 0;
186554936f8bSTejun Heo 
1866c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1867c6fd2807SJeff Garzik 
1868c6fd2807SJeff Garzik 	/* sanity check */
1869c6fd2807SJeff Garzik 	rc = -EINVAL;
18706070068bSAlan Cox 	reason = "device reports invalid type";
18714a3381feSJeff Garzik 
18729162c657SHannes Reinecke 	if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
18734a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
18744a3381feSJeff Garzik 			goto err_out;
1875db63a4c8SAndy Whitcroft 		if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1876db63a4c8SAndy Whitcroft 							ata_id_is_ata(id)) {
1877db63a4c8SAndy Whitcroft 			ata_dev_dbg(dev,
1878db63a4c8SAndy Whitcroft 				"host indicates ignore ATA devices, ignored\n");
1879db63a4c8SAndy Whitcroft 			return -ENOENT;
1880db63a4c8SAndy Whitcroft 		}
18814a3381feSJeff Garzik 	} else {
18824a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1883c6fd2807SJeff Garzik 			goto err_out;
1884c6fd2807SJeff Garzik 	}
1885c6fd2807SJeff Garzik 
1886169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1887169439c2SMark Lord 		tried_spinup = 1;
1888169439c2SMark Lord 		/*
1889169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
1890169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
1891169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
1892169439c2SMark Lord 		 */
1893218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1894fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
1895169439c2SMark Lord 			rc = -EIO;
1896169439c2SMark Lord 			reason = "SPINUP failed";
1897169439c2SMark Lord 			goto err_out;
1898169439c2SMark Lord 		}
1899169439c2SMark Lord 		/*
1900169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
1901169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
1902169439c2SMark Lord 		 */
1903169439c2SMark Lord 		if (id[2] == 0x37c8)
1904169439c2SMark Lord 			goto retry;
1905169439c2SMark Lord 	}
1906169439c2SMark Lord 
19079162c657SHannes Reinecke 	if ((flags & ATA_READID_POSTRESET) &&
19089162c657SHannes Reinecke 	    (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
1909c6fd2807SJeff Garzik 		/*
1910c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
1911c6fd2807SJeff Garzik 		 * SRST RESET
191250a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
191350a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
1914c6fd2807SJeff Garzik 		 * anything else..
1915c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
191650a99018SAlan Cox 		 *
191750a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
1918c9404c9cSAdam Buchbinder 		 * should never trigger.
1919c6fd2807SJeff Garzik 		 */
1920c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1921c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
1922c6fd2807SJeff Garzik 			if (err_mask) {
1923c6fd2807SJeff Garzik 				rc = -EIO;
1924c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
1925c6fd2807SJeff Garzik 				goto err_out;
1926c6fd2807SJeff Garzik 			}
1927c6fd2807SJeff Garzik 
1928c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
1929c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
1930c6fd2807SJeff Garzik 			 */
1931bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
1932c6fd2807SJeff Garzik 			goto retry;
1933c6fd2807SJeff Garzik 		}
1934c6fd2807SJeff Garzik 	}
1935c6fd2807SJeff Garzik 
1936c6fd2807SJeff Garzik 	*p_class = class;
1937c6fd2807SJeff Garzik 
1938c6fd2807SJeff Garzik 	return 0;
1939c6fd2807SJeff Garzik 
1940c6fd2807SJeff Garzik  err_out:
1941a9a79dfeSJoe Perches 	ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1942a9a79dfeSJoe Perches 		     reason, err_mask);
1943c6fd2807SJeff Garzik 	return rc;
1944c6fd2807SJeff Garzik }
1945c6fd2807SJeff Garzik 
1946f01f62c2SChristoph Hellwig /**
1947f01f62c2SChristoph Hellwig  *	ata_read_log_page - read a specific log page
1948f01f62c2SChristoph Hellwig  *	@dev: target device
1949f01f62c2SChristoph Hellwig  *	@log: log to read
1950f01f62c2SChristoph Hellwig  *	@page: page to read
1951f01f62c2SChristoph Hellwig  *	@buf: buffer to store read page
1952f01f62c2SChristoph Hellwig  *	@sectors: number of sectors to read
1953f01f62c2SChristoph Hellwig  *
1954f01f62c2SChristoph Hellwig  *	Read log page using READ_LOG_EXT command.
1955f01f62c2SChristoph Hellwig  *
1956f01f62c2SChristoph Hellwig  *	LOCKING:
1957f01f62c2SChristoph Hellwig  *	Kernel thread context (may sleep).
1958f01f62c2SChristoph Hellwig  *
1959f01f62c2SChristoph Hellwig  *	RETURNS:
1960f01f62c2SChristoph Hellwig  *	0 on success, AC_ERR_* mask otherwise.
1961f01f62c2SChristoph Hellwig  */
1962f01f62c2SChristoph Hellwig unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1963f01f62c2SChristoph Hellwig 			       u8 page, void *buf, unsigned int sectors)
1964f01f62c2SChristoph Hellwig {
1965f01f62c2SChristoph Hellwig 	unsigned long ap_flags = dev->link->ap->flags;
1966f01f62c2SChristoph Hellwig 	struct ata_taskfile tf;
1967f01f62c2SChristoph Hellwig 	unsigned int err_mask;
1968f01f62c2SChristoph Hellwig 	bool dma = false;
1969f01f62c2SChristoph Hellwig 
19704633778bSHannes Reinecke 	ata_dev_dbg(dev, "read log page - log 0x%x, page 0x%x\n", log, page);
1971f01f62c2SChristoph Hellwig 
1972f01f62c2SChristoph Hellwig 	/*
1973f01f62c2SChristoph Hellwig 	 * Return error without actually issuing the command on controllers
1974f01f62c2SChristoph Hellwig 	 * which e.g. lockup on a read log page.
1975f01f62c2SChristoph Hellwig 	 */
1976f01f62c2SChristoph Hellwig 	if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
1977f01f62c2SChristoph Hellwig 		return AC_ERR_DEV;
1978f01f62c2SChristoph Hellwig 
1979f01f62c2SChristoph Hellwig retry:
1980f01f62c2SChristoph Hellwig 	ata_tf_init(dev, &tf);
1981f971a854SReimar Döffinger 	if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) &&
19827cfdfdc8SDamien Le Moal 	    !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
1983f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_DMA_EXT;
1984f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_DMA;
1985f01f62c2SChristoph Hellwig 		dma = true;
1986f01f62c2SChristoph Hellwig 	} else {
1987f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_EXT;
1988f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_PIO;
1989f01f62c2SChristoph Hellwig 		dma = false;
1990f01f62c2SChristoph Hellwig 	}
1991f01f62c2SChristoph Hellwig 	tf.lbal = log;
1992f01f62c2SChristoph Hellwig 	tf.lbam = page;
1993f01f62c2SChristoph Hellwig 	tf.nsect = sectors;
1994f01f62c2SChristoph Hellwig 	tf.hob_nsect = sectors >> 8;
1995f01f62c2SChristoph Hellwig 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1996f01f62c2SChristoph Hellwig 
1997f01f62c2SChristoph Hellwig 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1998f01f62c2SChristoph Hellwig 				     buf, sectors * ATA_SECT_SIZE, 0);
1999f01f62c2SChristoph Hellwig 
2000fc5c8aa7SDamien Le Moal 	if (err_mask) {
2001fc5c8aa7SDamien Le Moal 		if (dma) {
20027cfdfdc8SDamien Le Moal 			dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2003f01f62c2SChristoph Hellwig 			goto retry;
2004f01f62c2SChristoph Hellwig 		}
200523ef63d5SDamien Le Moal 		ata_dev_err(dev,
200623ef63d5SDamien Le Moal 			    "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n",
200723ef63d5SDamien Le Moal 			    (unsigned int)log, (unsigned int)page, err_mask);
2008fc5c8aa7SDamien Le Moal 	}
2009f01f62c2SChristoph Hellwig 
2010f01f62c2SChristoph Hellwig 	return err_mask;
2011f01f62c2SChristoph Hellwig }
2012f01f62c2SChristoph Hellwig 
2013c745dfc5STyler Erickson static int ata_log_supported(struct ata_device *dev, u8 log)
2014efe205a3SChristoph Hellwig {
2015efe205a3SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2016efe205a3SChristoph Hellwig 
2017ac9f0c81SAnton Lundin 	if (dev->horkage & ATA_HORKAGE_NO_LOG_DIR)
2018c745dfc5STyler Erickson 		return 0;
2019ac9f0c81SAnton Lundin 
2020efe205a3SChristoph Hellwig 	if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2021c745dfc5STyler Erickson 		return 0;
2022c745dfc5STyler Erickson 	return get_unaligned_le16(&ap->sector_buf[log * 2]);
2023efe205a3SChristoph Hellwig }
2024efe205a3SChristoph Hellwig 
2025a0fd2454SChristoph Hellwig static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2026a0fd2454SChristoph Hellwig {
2027a0fd2454SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2028a0fd2454SChristoph Hellwig 	unsigned int err, i;
2029a0fd2454SChristoph Hellwig 
2030636f6e2aSDamien Le Moal 	if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG)
2031636f6e2aSDamien Le Moal 		return false;
2032636f6e2aSDamien Le Moal 
2033a0fd2454SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2034636f6e2aSDamien Le Moal 		/*
2035636f6e2aSDamien Le Moal 		 * IDENTIFY DEVICE data log is defined as mandatory starting
2036636f6e2aSDamien Le Moal 		 * with ACS-3 (ATA version 10). Warn about the missing log
2037636f6e2aSDamien Le Moal 		 * for drives which implement this ATA level or above.
2038636f6e2aSDamien Le Moal 		 */
2039636f6e2aSDamien Le Moal 		if (ata_id_major_version(dev->id) >= 10)
2040636f6e2aSDamien Le Moal 			ata_dev_warn(dev,
2041636f6e2aSDamien Le Moal 				"ATA Identify Device Log not supported\n");
2042636f6e2aSDamien Le Moal 		dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG;
2043a0fd2454SChristoph Hellwig 		return false;
2044a0fd2454SChristoph Hellwig 	}
2045a0fd2454SChristoph Hellwig 
2046a0fd2454SChristoph Hellwig 	/*
2047a0fd2454SChristoph Hellwig 	 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2048a0fd2454SChristoph Hellwig 	 * supported.
2049a0fd2454SChristoph Hellwig 	 */
2050a0fd2454SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2051a0fd2454SChristoph Hellwig 				1);
2052fc5c8aa7SDamien Le Moal 	if (err)
2053a0fd2454SChristoph Hellwig 		return false;
2054a0fd2454SChristoph Hellwig 
2055a0fd2454SChristoph Hellwig 	for (i = 0; i < ap->sector_buf[8]; i++) {
2056a0fd2454SChristoph Hellwig 		if (ap->sector_buf[9 + i] == page)
2057a0fd2454SChristoph Hellwig 			return true;
2058a0fd2454SChristoph Hellwig 	}
2059a0fd2454SChristoph Hellwig 
2060a0fd2454SChristoph Hellwig 	return false;
2061a0fd2454SChristoph Hellwig }
2062a0fd2454SChristoph Hellwig 
20639062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
20649062712fSTejun Heo {
20659062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
20669062712fSTejun Heo 	u32 target, target_limit;
20679062712fSTejun Heo 
20689062712fSTejun Heo 	if (!sata_scr_valid(plink))
20699062712fSTejun Heo 		return 0;
20709062712fSTejun Heo 
20719062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
20729062712fSTejun Heo 		target = 1;
20739062712fSTejun Heo 	else
20749062712fSTejun Heo 		return 0;
20759062712fSTejun Heo 
20769062712fSTejun Heo 	target_limit = (1 << target) - 1;
20779062712fSTejun Heo 
20789062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
20799062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
20809062712fSTejun Heo 		return 0;
20819062712fSTejun Heo 
20829062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
20839062712fSTejun Heo 
20849062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
20859062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
20869062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
20879062712fSTejun Heo 	 */
20889062712fSTejun Heo 	if (plink->sata_spd > target) {
2089a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
20909062712fSTejun Heo 			     sata_spd_string(target));
20919062712fSTejun Heo 		return -EAGAIN;
20929062712fSTejun Heo 	}
20939062712fSTejun Heo 	return 0;
20949062712fSTejun Heo }
20959062712fSTejun Heo 
2096c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2097c6fd2807SJeff Garzik {
20989af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
20999ce8e307SJens Axboe 
21009ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
21019ce8e307SJens Axboe 		return 0;
21029ce8e307SJens Axboe 
21039af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2104c6fd2807SJeff Garzik }
2105c6fd2807SJeff Garzik 
21065a233551SHannes Reinecke static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
21075a233551SHannes Reinecke {
21085a233551SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
21095a233551SHannes Reinecke 	unsigned int err_mask;
21105a233551SHannes Reinecke 
2111efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2112efe205a3SChristoph Hellwig 		ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
2113fe5af0ccSHannes Reinecke 		return;
2114fe5af0ccSHannes Reinecke 	}
21155a233551SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
21165a233551SHannes Reinecke 				     0, ap->sector_buf, 1);
2117fc5c8aa7SDamien Le Moal 	if (!err_mask) {
21185a233551SHannes Reinecke 		u8 *cmds = dev->ncq_send_recv_cmds;
21195a233551SHannes Reinecke 
21205a233551SHannes Reinecke 		dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
21215a233551SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
21225a233551SHannes Reinecke 
21235a233551SHannes Reinecke 		if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
21245a233551SHannes Reinecke 			ata_dev_dbg(dev, "disabling queued TRIM support\n");
21255a233551SHannes Reinecke 			cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
21265a233551SHannes Reinecke 				~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
21275a233551SHannes Reinecke 		}
21285a233551SHannes Reinecke 	}
21295a233551SHannes Reinecke }
21305a233551SHannes Reinecke 
2131284b3b77SHannes Reinecke static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2132284b3b77SHannes Reinecke {
2133284b3b77SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
2134284b3b77SHannes Reinecke 	unsigned int err_mask;
2135284b3b77SHannes Reinecke 
2136efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
2137284b3b77SHannes Reinecke 		ata_dev_warn(dev,
2138284b3b77SHannes Reinecke 			     "NCQ Send/Recv Log not supported\n");
2139284b3b77SHannes Reinecke 		return;
2140284b3b77SHannes Reinecke 	}
2141284b3b77SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2142284b3b77SHannes Reinecke 				     0, ap->sector_buf, 1);
2143fc5c8aa7SDamien Le Moal 	if (!err_mask) {
2144284b3b77SHannes Reinecke 		u8 *cmds = dev->ncq_non_data_cmds;
2145284b3b77SHannes Reinecke 
2146284b3b77SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2147284b3b77SHannes Reinecke 	}
2148284b3b77SHannes Reinecke }
2149284b3b77SHannes Reinecke 
21508e061784SAdam Manzanares static void ata_dev_config_ncq_prio(struct ata_device *dev)
21518e061784SAdam Manzanares {
21528e061784SAdam Manzanares 	struct ata_port *ap = dev->link->ap;
21538e061784SAdam Manzanares 	unsigned int err_mask;
21548e061784SAdam Manzanares 
215506f6c4c6SDamien Le Moal 	if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
215606f6c4c6SDamien Le Moal 		return;
215706f6c4c6SDamien Le Moal 
21588e061784SAdam Manzanares 	err_mask = ata_read_log_page(dev,
21591d51d5f3SChristoph Hellwig 				     ATA_LOG_IDENTIFY_DEVICE,
21608e061784SAdam Manzanares 				     ATA_LOG_SATA_SETTINGS,
21618e061784SAdam Manzanares 				     ap->sector_buf,
21628e061784SAdam Manzanares 				     1);
2163fc5c8aa7SDamien Le Moal 	if (err_mask)
21642360fa18SDamien Le Moal 		goto not_supported;
21658e061784SAdam Manzanares 
21662360fa18SDamien Le Moal 	if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
21672360fa18SDamien Le Moal 		goto not_supported;
21682360fa18SDamien Le Moal 
21698e061784SAdam Manzanares 	dev->flags |= ATA_DFLAG_NCQ_PRIO;
21708e061784SAdam Manzanares 
21712360fa18SDamien Le Moal 	return;
21722360fa18SDamien Le Moal 
21732360fa18SDamien Le Moal not_supported:
21742360fa18SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
21752360fa18SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
21768e061784SAdam Manzanares }
21778e061784SAdam Manzanares 
21787a8526a5SKate Hsuan static bool ata_dev_check_adapter(struct ata_device *dev,
21797a8526a5SKate Hsuan 				  unsigned short vendor_id)
21807a8526a5SKate Hsuan {
21817a8526a5SKate Hsuan 	struct pci_dev *pcidev = NULL;
21827a8526a5SKate Hsuan 	struct device *parent_dev = NULL;
21837a8526a5SKate Hsuan 
21847a8526a5SKate Hsuan 	for (parent_dev = dev->tdev.parent; parent_dev != NULL;
21857a8526a5SKate Hsuan 	     parent_dev = parent_dev->parent) {
21867a8526a5SKate Hsuan 		if (dev_is_pci(parent_dev)) {
21877a8526a5SKate Hsuan 			pcidev = to_pci_dev(parent_dev);
21887a8526a5SKate Hsuan 			if (pcidev->vendor == vendor_id)
21897a8526a5SKate Hsuan 				return true;
21907a8526a5SKate Hsuan 			break;
21917a8526a5SKate Hsuan 		}
21927a8526a5SKate Hsuan 	}
21937a8526a5SKate Hsuan 
21947a8526a5SKate Hsuan 	return false;
21957a8526a5SKate Hsuan }
21967a8526a5SKate Hsuan 
2197388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2198c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2199c6fd2807SJeff Garzik {
22009af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2201c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2202388539f3SShaohua Li 	unsigned int err_mask;
2203388539f3SShaohua Li 	char *aa_desc = "";
2204c6fd2807SJeff Garzik 
2205c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2206c6fd2807SJeff Garzik 		desc[0] = '\0';
2207388539f3SShaohua Li 		return 0;
2208c6fd2807SJeff Garzik 	}
2209cba97ea1SBartlomiej Zolnierkiewicz 	if (!IS_ENABLED(CONFIG_SATA_HOST))
2210cba97ea1SBartlomiej Zolnierkiewicz 		return 0;
221175683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
22126919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2213388539f3SShaohua Li 		return 0;
22146919a0a6SAlan Cox 	}
22157a8526a5SKate Hsuan 
22167a8526a5SKate Hsuan 	if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI &&
22177a8526a5SKate Hsuan 	    ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) {
22187a8526a5SKate Hsuan 		snprintf(desc, desc_sz, "NCQ (not used)");
22197a8526a5SKate Hsuan 		return 0;
22207a8526a5SKate Hsuan 	}
22217a8526a5SKate Hsuan 
2222c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
222369278f79SJens Axboe 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
2224c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2225c6fd2807SJeff Garzik 	}
2226c6fd2807SJeff Garzik 
2227388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2228388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2229388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2230388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2231388539f3SShaohua Li 			SATA_FPDMA_AA);
2232388539f3SShaohua Li 		if (err_mask) {
2233a9a79dfeSJoe Perches 			ata_dev_err(dev,
2234a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2235a9a79dfeSJoe Perches 				    err_mask);
2236388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2237388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2238388539f3SShaohua Li 				return -EIO;
2239388539f3SShaohua Li 			}
2240388539f3SShaohua Li 		} else
2241388539f3SShaohua Li 			aa_desc = ", AA";
2242388539f3SShaohua Li 	}
2243388539f3SShaohua Li 
2244c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2245388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2246c6fd2807SJeff Garzik 	else
2247388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2248388539f3SShaohua Li 			ddepth, aa_desc);
2249ed36911cSMarc Carino 
2250284b3b77SHannes Reinecke 	if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2251284b3b77SHannes Reinecke 		if (ata_id_has_ncq_send_and_recv(dev->id))
22525a233551SHannes Reinecke 			ata_dev_config_ncq_send_recv(dev);
2253284b3b77SHannes Reinecke 		if (ata_id_has_ncq_non_data(dev->id))
2254284b3b77SHannes Reinecke 			ata_dev_config_ncq_non_data(dev);
22558e061784SAdam Manzanares 		if (ata_id_has_ncq_prio(dev->id))
22568e061784SAdam Manzanares 			ata_dev_config_ncq_prio(dev);
2257ed36911cSMarc Carino 	}
2258ed36911cSMarc Carino 
2259388539f3SShaohua Li 	return 0;
2260c6fd2807SJeff Garzik }
2261c6fd2807SJeff Garzik 
2262e87fd28cSHannes Reinecke static void ata_dev_config_sense_reporting(struct ata_device *dev)
2263e87fd28cSHannes Reinecke {
2264e87fd28cSHannes Reinecke 	unsigned int err_mask;
2265e87fd28cSHannes Reinecke 
2266e87fd28cSHannes Reinecke 	if (!ata_id_has_sense_reporting(dev->id))
2267e87fd28cSHannes Reinecke 		return;
2268e87fd28cSHannes Reinecke 
2269e87fd28cSHannes Reinecke 	if (ata_id_sense_reporting_enabled(dev->id))
2270e87fd28cSHannes Reinecke 		return;
2271e87fd28cSHannes Reinecke 
2272e87fd28cSHannes Reinecke 	err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2273e87fd28cSHannes Reinecke 	if (err_mask) {
2274e87fd28cSHannes Reinecke 		ata_dev_dbg(dev,
2275e87fd28cSHannes Reinecke 			    "failed to enable Sense Data Reporting, Emask 0x%x\n",
2276e87fd28cSHannes Reinecke 			    err_mask);
2277e87fd28cSHannes Reinecke 	}
2278e87fd28cSHannes Reinecke }
2279e87fd28cSHannes Reinecke 
22806d1003aeSHannes Reinecke static void ata_dev_config_zac(struct ata_device *dev)
22816d1003aeSHannes Reinecke {
22826d1003aeSHannes Reinecke 	struct ata_port *ap = dev->link->ap;
22836d1003aeSHannes Reinecke 	unsigned int err_mask;
22846d1003aeSHannes Reinecke 	u8 *identify_buf = ap->sector_buf;
22856d1003aeSHannes Reinecke 
22866d1003aeSHannes Reinecke 	dev->zac_zones_optimal_open = U32_MAX;
22876d1003aeSHannes Reinecke 	dev->zac_zones_optimal_nonseq = U32_MAX;
22886d1003aeSHannes Reinecke 	dev->zac_zones_max_open = U32_MAX;
22896d1003aeSHannes Reinecke 
22906d1003aeSHannes Reinecke 	/*
22916d1003aeSHannes Reinecke 	 * Always set the 'ZAC' flag for Host-managed devices.
22926d1003aeSHannes Reinecke 	 */
22936d1003aeSHannes Reinecke 	if (dev->class == ATA_DEV_ZAC)
22946d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
22956d1003aeSHannes Reinecke 	else if (ata_id_zoned_cap(dev->id) == 0x01)
22966d1003aeSHannes Reinecke 		/*
22976d1003aeSHannes Reinecke 		 * Check for host-aware devices.
22986d1003aeSHannes Reinecke 		 */
22996d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
23006d1003aeSHannes Reinecke 
23016d1003aeSHannes Reinecke 	if (!(dev->flags & ATA_DFLAG_ZAC))
23026d1003aeSHannes Reinecke 		return;
23036d1003aeSHannes Reinecke 
2304a0fd2454SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
23056d1003aeSHannes Reinecke 		ata_dev_warn(dev,
23066d1003aeSHannes Reinecke 			     "ATA Zoned Information Log not supported\n");
23076d1003aeSHannes Reinecke 		return;
23086d1003aeSHannes Reinecke 	}
23096d1003aeSHannes Reinecke 
23106d1003aeSHannes Reinecke 	/*
23116d1003aeSHannes Reinecke 	 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
23126d1003aeSHannes Reinecke 	 */
23131d51d5f3SChristoph Hellwig 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
23146d1003aeSHannes Reinecke 				     ATA_LOG_ZONED_INFORMATION,
23156d1003aeSHannes Reinecke 				     identify_buf, 1);
23166d1003aeSHannes Reinecke 	if (!err_mask) {
23176d1003aeSHannes Reinecke 		u64 zoned_cap, opt_open, opt_nonseq, max_open;
23186d1003aeSHannes Reinecke 
23196d1003aeSHannes Reinecke 		zoned_cap = get_unaligned_le64(&identify_buf[8]);
23206d1003aeSHannes Reinecke 		if ((zoned_cap >> 63))
23216d1003aeSHannes Reinecke 			dev->zac_zoned_cap = (zoned_cap & 1);
23226d1003aeSHannes Reinecke 		opt_open = get_unaligned_le64(&identify_buf[24]);
23236d1003aeSHannes Reinecke 		if ((opt_open >> 63))
23246d1003aeSHannes Reinecke 			dev->zac_zones_optimal_open = (u32)opt_open;
23256d1003aeSHannes Reinecke 		opt_nonseq = get_unaligned_le64(&identify_buf[32]);
23266d1003aeSHannes Reinecke 		if ((opt_nonseq >> 63))
23276d1003aeSHannes Reinecke 			dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
23286d1003aeSHannes Reinecke 		max_open = get_unaligned_le64(&identify_buf[40]);
23296d1003aeSHannes Reinecke 		if ((max_open >> 63))
23306d1003aeSHannes Reinecke 			dev->zac_zones_max_open = (u32)max_open;
23316d1003aeSHannes Reinecke 	}
23326d1003aeSHannes Reinecke }
23336d1003aeSHannes Reinecke 
2334818831c8SChristoph Hellwig static void ata_dev_config_trusted(struct ata_device *dev)
2335818831c8SChristoph Hellwig {
2336818831c8SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2337818831c8SChristoph Hellwig 	u64 trusted_cap;
2338818831c8SChristoph Hellwig 	unsigned int err;
2339818831c8SChristoph Hellwig 
2340e8f11db9SChristoph Hellwig 	if (!ata_id_has_trusted(dev->id))
2341e8f11db9SChristoph Hellwig 		return;
2342e8f11db9SChristoph Hellwig 
2343818831c8SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2344818831c8SChristoph Hellwig 		ata_dev_warn(dev,
2345818831c8SChristoph Hellwig 			     "Security Log not supported\n");
2346818831c8SChristoph Hellwig 		return;
2347818831c8SChristoph Hellwig 	}
2348818831c8SChristoph Hellwig 
2349818831c8SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2350818831c8SChristoph Hellwig 			ap->sector_buf, 1);
2351fc5c8aa7SDamien Le Moal 	if (err)
2352818831c8SChristoph Hellwig 		return;
2353818831c8SChristoph Hellwig 
2354818831c8SChristoph Hellwig 	trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2355818831c8SChristoph Hellwig 	if (!(trusted_cap & (1ULL << 63))) {
2356818831c8SChristoph Hellwig 		ata_dev_dbg(dev,
2357818831c8SChristoph Hellwig 			    "Trusted Computing capability qword not valid!\n");
2358818831c8SChristoph Hellwig 		return;
2359818831c8SChristoph Hellwig 	}
2360818831c8SChristoph Hellwig 
2361818831c8SChristoph Hellwig 	if (trusted_cap & (1 << 0))
2362818831c8SChristoph Hellwig 		dev->flags |= ATA_DFLAG_TRUSTED;
2363818831c8SChristoph Hellwig }
2364818831c8SChristoph Hellwig 
2365891fd7c6SDamien Le Moal static int ata_dev_config_lba(struct ata_device *dev)
2366891fd7c6SDamien Le Moal {
2367891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2368891fd7c6SDamien Le Moal 	const char *lba_desc;
2369891fd7c6SDamien Le Moal 	char ncq_desc[24];
2370891fd7c6SDamien Le Moal 	int ret;
2371891fd7c6SDamien Le Moal 
2372891fd7c6SDamien Le Moal 	dev->flags |= ATA_DFLAG_LBA;
2373891fd7c6SDamien Le Moal 
2374891fd7c6SDamien Le Moal 	if (ata_id_has_lba48(id)) {
2375891fd7c6SDamien Le Moal 		lba_desc = "LBA48";
2376891fd7c6SDamien Le Moal 		dev->flags |= ATA_DFLAG_LBA48;
2377891fd7c6SDamien Le Moal 		if (dev->n_sectors >= (1UL << 28) &&
2378891fd7c6SDamien Le Moal 		    ata_id_has_flush_ext(id))
2379891fd7c6SDamien Le Moal 			dev->flags |= ATA_DFLAG_FLUSH_EXT;
2380891fd7c6SDamien Le Moal 	} else {
2381891fd7c6SDamien Le Moal 		lba_desc = "LBA";
2382891fd7c6SDamien Le Moal 	}
2383891fd7c6SDamien Le Moal 
2384891fd7c6SDamien Le Moal 	/* config NCQ */
2385891fd7c6SDamien Le Moal 	ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2386891fd7c6SDamien Le Moal 
2387891fd7c6SDamien Le Moal 	/* print device info to dmesg */
23881c95a27cSHannes Reinecke 	if (ata_dev_print_info(dev))
2389891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2390891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u: %s %s\n",
2391891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2392891fd7c6SDamien Le Moal 			     dev->multi_count, lba_desc, ncq_desc);
2393891fd7c6SDamien Le Moal 
2394891fd7c6SDamien Le Moal 	return ret;
2395891fd7c6SDamien Le Moal }
2396891fd7c6SDamien Le Moal 
2397891fd7c6SDamien Le Moal static void ata_dev_config_chs(struct ata_device *dev)
2398891fd7c6SDamien Le Moal {
2399891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2400891fd7c6SDamien Le Moal 
2401891fd7c6SDamien Le Moal 	if (ata_id_current_chs_valid(id)) {
2402891fd7c6SDamien Le Moal 		/* Current CHS translation is valid. */
2403891fd7c6SDamien Le Moal 		dev->cylinders = id[54];
2404891fd7c6SDamien Le Moal 		dev->heads     = id[55];
2405891fd7c6SDamien Le Moal 		dev->sectors   = id[56];
2406891fd7c6SDamien Le Moal 	} else {
2407891fd7c6SDamien Le Moal 		/* Default translation */
2408891fd7c6SDamien Le Moal 		dev->cylinders	= id[1];
2409891fd7c6SDamien Le Moal 		dev->heads	= id[3];
2410891fd7c6SDamien Le Moal 		dev->sectors	= id[6];
2411891fd7c6SDamien Le Moal 	}
2412891fd7c6SDamien Le Moal 
2413891fd7c6SDamien Le Moal 	/* print device info to dmesg */
24141c95a27cSHannes Reinecke 	if (ata_dev_print_info(dev))
2415891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2416891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u, CHS %u/%u/%u\n",
2417891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2418891fd7c6SDamien Le Moal 			     dev->multi_count, dev->cylinders,
2419891fd7c6SDamien Le Moal 			     dev->heads, dev->sectors);
2420891fd7c6SDamien Le Moal }
2421891fd7c6SDamien Le Moal 
2422d8d8778cSDamien Le Moal static void ata_dev_config_devslp(struct ata_device *dev)
2423d8d8778cSDamien Le Moal {
2424d8d8778cSDamien Le Moal 	u8 *sata_setting = dev->link->ap->sector_buf;
2425d8d8778cSDamien Le Moal 	unsigned int err_mask;
2426d8d8778cSDamien Le Moal 	int i, j;
2427d8d8778cSDamien Le Moal 
2428d8d8778cSDamien Le Moal 	/*
2429d8d8778cSDamien Le Moal 	 * Check device sleep capability. Get DevSlp timing variables
2430d8d8778cSDamien Le Moal 	 * from SATA Settings page of Identify Device Data Log.
2431d8d8778cSDamien Le Moal 	 */
243206f6c4c6SDamien Le Moal 	if (!ata_id_has_devslp(dev->id) ||
243306f6c4c6SDamien Le Moal 	    !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
2434d8d8778cSDamien Le Moal 		return;
2435d8d8778cSDamien Le Moal 
2436d8d8778cSDamien Le Moal 	err_mask = ata_read_log_page(dev,
2437d8d8778cSDamien Le Moal 				     ATA_LOG_IDENTIFY_DEVICE,
2438d8d8778cSDamien Le Moal 				     ATA_LOG_SATA_SETTINGS,
2439d8d8778cSDamien Le Moal 				     sata_setting, 1);
2440fc5c8aa7SDamien Le Moal 	if (err_mask)
2441d8d8778cSDamien Le Moal 		return;
2442d8d8778cSDamien Le Moal 
2443d8d8778cSDamien Le Moal 	dev->flags |= ATA_DFLAG_DEVSLP;
2444d8d8778cSDamien Le Moal 	for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2445d8d8778cSDamien Le Moal 		j = ATA_LOG_DEVSLP_OFFSET + i;
2446d8d8778cSDamien Le Moal 		dev->devslp_timing[i] = sata_setting[j];
2447d8d8778cSDamien Le Moal 	}
2448d8d8778cSDamien Le Moal }
2449d8d8778cSDamien Le Moal 
2450fe22e1c2SDamien Le Moal static void ata_dev_config_cpr(struct ata_device *dev)
2451fe22e1c2SDamien Le Moal {
2452fe22e1c2SDamien Le Moal 	unsigned int err_mask;
2453fe22e1c2SDamien Le Moal 	size_t buf_len;
2454fe22e1c2SDamien Le Moal 	int i, nr_cpr = 0;
2455fe22e1c2SDamien Le Moal 	struct ata_cpr_log *cpr_log = NULL;
2456fe22e1c2SDamien Le Moal 	u8 *desc, *buf = NULL;
2457fe22e1c2SDamien Le Moal 
2458c745dfc5STyler Erickson 	if (ata_id_major_version(dev->id) < 11)
2459c745dfc5STyler Erickson 		goto out;
2460c745dfc5STyler Erickson 
2461c745dfc5STyler Erickson 	buf_len = ata_log_supported(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES);
2462c745dfc5STyler Erickson 	if (buf_len == 0)
2463fe22e1c2SDamien Le Moal 		goto out;
2464fe22e1c2SDamien Le Moal 
2465fe22e1c2SDamien Le Moal 	/*
2466fda17afcSDamien Le Moal 	 * Read the concurrent positioning ranges log (0x47). We can have at
2467c745dfc5STyler Erickson 	 * most 255 32B range descriptors plus a 64B header. This log varies in
2468c745dfc5STyler Erickson 	 * size, so use the size reported in the GPL directory. Reading beyond
2469c745dfc5STyler Erickson 	 * the supported length will result in an error.
2470fe22e1c2SDamien Le Moal 	 */
2471c745dfc5STyler Erickson 	buf_len <<= 9;
2472fe22e1c2SDamien Le Moal 	buf = kzalloc(buf_len, GFP_KERNEL);
2473fe22e1c2SDamien Le Moal 	if (!buf)
2474fe22e1c2SDamien Le Moal 		goto out;
2475fe22e1c2SDamien Le Moal 
2476fda17afcSDamien Le Moal 	err_mask = ata_read_log_page(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES,
2477fda17afcSDamien Le Moal 				     0, buf, buf_len >> 9);
2478fe22e1c2SDamien Le Moal 	if (err_mask)
2479fe22e1c2SDamien Le Moal 		goto out;
2480fe22e1c2SDamien Le Moal 
2481fe22e1c2SDamien Le Moal 	nr_cpr = buf[0];
2482fe22e1c2SDamien Le Moal 	if (!nr_cpr)
2483fe22e1c2SDamien Le Moal 		goto out;
2484fe22e1c2SDamien Le Moal 
2485fe22e1c2SDamien Le Moal 	cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL);
2486fe22e1c2SDamien Le Moal 	if (!cpr_log)
2487fe22e1c2SDamien Le Moal 		goto out;
2488fe22e1c2SDamien Le Moal 
2489fe22e1c2SDamien Le Moal 	cpr_log->nr_cpr = nr_cpr;
2490fe22e1c2SDamien Le Moal 	desc = &buf[64];
2491fe22e1c2SDamien Le Moal 	for (i = 0; i < nr_cpr; i++, desc += 32) {
2492fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num = desc[0];
2493fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num_storage_elements = desc[1];
2494fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]);
2495fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]);
2496fe22e1c2SDamien Le Moal 	}
2497fe22e1c2SDamien Le Moal 
2498fe22e1c2SDamien Le Moal out:
2499fe22e1c2SDamien Le Moal 	swap(dev->cpr_log, cpr_log);
2500fe22e1c2SDamien Le Moal 	kfree(cpr_log);
2501fe22e1c2SDamien Le Moal 	kfree(buf);
2502fe22e1c2SDamien Le Moal }
2503fe22e1c2SDamien Le Moal 
2504d633b8a7SDamien Le Moal static void ata_dev_print_features(struct ata_device *dev)
2505d633b8a7SDamien Le Moal {
2506d633b8a7SDamien Le Moal 	if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
2507d633b8a7SDamien Le Moal 		return;
2508d633b8a7SDamien Le Moal 
2509d633b8a7SDamien Le Moal 	ata_dev_info(dev,
2510fe22e1c2SDamien Le Moal 		     "Features:%s%s%s%s%s%s\n",
2511d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
2512d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
2513d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
2514d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
2515fe22e1c2SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "",
2516fe22e1c2SDamien Le Moal 		     dev->cpr_log ? " CPR" : "");
2517d633b8a7SDamien Le Moal }
2518d633b8a7SDamien Le Moal 
2519c6fd2807SJeff Garzik /**
2520c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2521c6fd2807SJeff Garzik  *	@dev: Target device to configure
2522c6fd2807SJeff Garzik  *
2523c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2524c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2525c6fd2807SJeff Garzik  *
2526c6fd2807SJeff Garzik  *	LOCKING:
2527c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2528c6fd2807SJeff Garzik  *
2529c6fd2807SJeff Garzik  *	RETURNS:
2530c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2531c6fd2807SJeff Garzik  */
2532efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2533c6fd2807SJeff Garzik {
25349af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2535891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
2536c6fd2807SJeff Garzik 	const u16 *id = dev->id;
2537f0a6d77bSSergey Shtylyov 	unsigned int xfer_mask;
253865fe1f0fSShane Huang 	unsigned int err_mask;
2539b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
25403f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
25413f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2542c6fd2807SJeff Garzik 	int rc;
2543c6fd2807SJeff Garzik 
254496c810f2SHannes Reinecke 	if (!ata_dev_enabled(dev)) {
254596c810f2SHannes Reinecke 		ata_dev_dbg(dev, "no device\n");
2546c6fd2807SJeff Garzik 		return 0;
2547c6fd2807SJeff Garzik 	}
2548c6fd2807SJeff Garzik 
254975683fe7STejun Heo 	/* set horkage */
255075683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
255133267325STejun Heo 	ata_force_horkage(dev);
255275683fe7STejun Heo 
255350af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2554a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
255550af2fa1STejun Heo 		ata_dev_disable(dev);
255650af2fa1STejun Heo 		return 0;
255750af2fa1STejun Heo 	}
255850af2fa1STejun Heo 
25592486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
25602486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2561a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
25622486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
25632486fa56STejun Heo 			     : "disabled");
25642486fa56STejun Heo 		ata_dev_disable(dev);
25652486fa56STejun Heo 		return 0;
25662486fa56STejun Heo 	}
25672486fa56STejun Heo 
25689062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
25699062712fSTejun Heo 	if (rc)
25709062712fSTejun Heo 		return rc;
25719062712fSTejun Heo 
2572ecd75ad5STejun Heo 	/* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2573ecd75ad5STejun Heo 	if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2574ecd75ad5STejun Heo 	    (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2575ecd75ad5STejun Heo 		dev->horkage |= ATA_HORKAGE_NOLPM;
2576ecd75ad5STejun Heo 
2577240630e6SHans de Goede 	if (ap->flags & ATA_FLAG_NO_LPM)
2578240630e6SHans de Goede 		dev->horkage |= ATA_HORKAGE_NOLPM;
2579240630e6SHans de Goede 
2580ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2581ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2582ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2583ecd75ad5STejun Heo 	}
2584ecd75ad5STejun Heo 
25856746544cSTejun Heo 	/* let ACPI work its magic */
25866746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
25876746544cSTejun Heo 	if (rc)
25886746544cSTejun Heo 		return rc;
258908573a86SKristen Carlson Accardi 
259005027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
259105027adcSTejun Heo 	rc = ata_hpa_resize(dev);
259205027adcSTejun Heo 	if (rc)
259305027adcSTejun Heo 		return rc;
259405027adcSTejun Heo 
2595c6fd2807SJeff Garzik 	/* print device capabilities */
2596a9a79dfeSJoe Perches 	ata_dev_dbg(dev,
2597c6fd2807SJeff Garzik 		    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2598c6fd2807SJeff Garzik 		    "85:%04x 86:%04x 87:%04x 88:%04x\n",
25997f5e4e8dSHarvey Harrison 		    __func__,
2600c6fd2807SJeff Garzik 		    id[49], id[82], id[83], id[84],
2601c6fd2807SJeff Garzik 		    id[85], id[86], id[87], id[88]);
2602c6fd2807SJeff Garzik 
2603c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2604c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2605c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2606c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2607c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2608c6fd2807SJeff Garzik 	dev->cylinders = 0;
2609c6fd2807SJeff Garzik 	dev->heads = 0;
2610c6fd2807SJeff Garzik 	dev->sectors = 0;
2611e18086d6SMark Lord 	dev->multi_count = 0;
2612c6fd2807SJeff Garzik 
2613c6fd2807SJeff Garzik 	/*
2614c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2615c6fd2807SJeff Garzik 	 */
2616c6fd2807SJeff Garzik 
2617c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2618c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2619c6fd2807SJeff Garzik 
26206044f3c4SHannes Reinecke 	ata_dump_id(dev, id);
2621c6fd2807SJeff Garzik 
2622ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2623ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2624ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2625ef143d57SAlbert Lee 
2626ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2627ef143d57SAlbert Lee 			sizeof(modelbuf));
2628ef143d57SAlbert Lee 
2629c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
26309162c657SHannes Reinecke 	if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2631b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
263262afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
263362afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2634a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2635a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2636b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2637ae8d4ee7SAlan Cox 		} else {
2638b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2639ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2640ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2641a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2642a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2643ae8d4ee7SAlan Cox 		}
2644b352e57dSAlan Cox 
2645c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2646c6fd2807SJeff Garzik 
2647e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2648e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2649e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2650e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2651e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2652e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2653e18086d6SMark Lord 				if (cnt <= max)
2654e18086d6SMark Lord 					dev->multi_count = cnt;
2655e18086d6SMark Lord 		}
26563f64f565SEric D. Mudama 
2657891fd7c6SDamien Le Moal 		/* print device info to dmesg */
26581c95a27cSHannes Reinecke 		if (print_info)
2659891fd7c6SDamien Le Moal 			ata_dev_info(dev, "%s: %s, %s, max %s\n",
2660891fd7c6SDamien Le Moal 				     revbuf, modelbuf, fwrevbuf,
2661891fd7c6SDamien Le Moal 				     ata_mode_string(xfer_mask));
2662891fd7c6SDamien Le Moal 
2663c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2664891fd7c6SDamien Le Moal 			rc = ata_dev_config_lba(dev);
2665388539f3SShaohua Li 			if (rc)
2666388539f3SShaohua Li 				return rc;
2667c6fd2807SJeff Garzik 		} else {
2668891fd7c6SDamien Le Moal 			ata_dev_config_chs(dev);
2669c6fd2807SJeff Garzik 		}
2670c6fd2807SJeff Garzik 
2671d8d8778cSDamien Le Moal 		ata_dev_config_devslp(dev);
2672e87fd28cSHannes Reinecke 		ata_dev_config_sense_reporting(dev);
26736d1003aeSHannes Reinecke 		ata_dev_config_zac(dev);
2674818831c8SChristoph Hellwig 		ata_dev_config_trusted(dev);
2675fe22e1c2SDamien Le Moal 		ata_dev_config_cpr(dev);
2676b1ffbf85SMinwoo Im 		dev->cdb_len = 32;
2677d633b8a7SDamien Le Moal 
26781c95a27cSHannes Reinecke 		if (print_info)
2679d633b8a7SDamien Le Moal 			ata_dev_print_features(dev);
2680c6fd2807SJeff Garzik 	}
2681c6fd2807SJeff Garzik 
2682c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2683c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2684854c73a2STejun Heo 		const char *cdb_intr_string = "";
2685854c73a2STejun Heo 		const char *atapi_an_string = "";
268691163006STejun Heo 		const char *dma_dir_string = "";
26877d77b247STejun Heo 		u32 sntf;
2688c6fd2807SJeff Garzik 
2689c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2690c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
269116d42467SHannes Reinecke 			ata_dev_warn(dev, "unsupported CDB len %d\n", rc);
2692c6fd2807SJeff Garzik 			rc = -EINVAL;
2693c6fd2807SJeff Garzik 			goto err_out_nosup;
2694c6fd2807SJeff Garzik 		}
2695c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2696c6fd2807SJeff Garzik 
26977d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
26987d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
26997d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
27007d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
27019f45cbd3SKristen Carlson Accardi 		 */
2702e7ecd435STejun Heo 		if (atapi_an &&
2703e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2704071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
27057d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
27069f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2707218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2708218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2709854c73a2STejun Heo 			if (err_mask)
2710a9a79dfeSJoe Perches 				ata_dev_err(dev,
2711a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2712a9a79dfeSJoe Perches 					    err_mask);
2713854c73a2STejun Heo 			else {
27149f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2715854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2716854c73a2STejun Heo 			}
27179f45cbd3SKristen Carlson Accardi 		}
27189f45cbd3SKristen Carlson Accardi 
2719c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2720c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2721c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2722c6fd2807SJeff Garzik 		}
2723c6fd2807SJeff Garzik 
2724966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
272591163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
272691163006STejun Heo 			dma_dir_string = ", DMADIR";
272791163006STejun Heo 		}
272891163006STejun Heo 
2729afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
2730b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2731afe75951SAaron Lu 			zpodd_init(dev);
2732afe75951SAaron Lu 		}
2733b1354cbbSLin Ming 
2734c6fd2807SJeff Garzik 		/* print device info to dmesg */
27351c95a27cSHannes Reinecke 		if (print_info)
2736a9a79dfeSJoe Perches 			ata_dev_info(dev,
273791163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2738ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2739c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
274091163006STejun Heo 				     cdb_intr_string, atapi_an_string,
274191163006STejun Heo 				     dma_dir_string);
2742c6fd2807SJeff Garzik 	}
2743c6fd2807SJeff Garzik 
2744914ed354STejun Heo 	/* determine max_sectors */
2745914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2746914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2747914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2748914ed354STejun Heo 
2749c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2750c5038fc0SAlan Cox 	   200 sectors */
2751c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
27521c95a27cSHannes Reinecke 		if (print_info)
2753a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2754c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2755c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2756c6fd2807SJeff Garzik 	}
2757c6fd2807SJeff Garzik 
2758f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2759f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2760f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2761f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2762f442cd86SAlbert Lee 	}
2763f8d8e579STony Battersby 
276475683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
276503ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
276603ec52deSTejun Heo 					 dev->max_sectors);
276718d6e9d5SAlbert Lee 
2768af34d637SDavid Milburn 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2769af34d637SDavid Milburn 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2770af34d637SDavid Milburn 					 dev->max_sectors);
2771af34d637SDavid Milburn 
2772a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2773a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2774a32450e1SShan Hai 
2775c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2776cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2777c6fd2807SJeff Garzik 
2778c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2779c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2780c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2781c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2782c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2783c5038fc0SAlan Cox 		   bugs */
2784c5038fc0SAlan Cox 
2785c5038fc0SAlan Cox 		if (print_info) {
2786a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2787c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2788a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2789c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2790c5038fc0SAlan Cox 		}
2791c5038fc0SAlan Cox 	}
2792c5038fc0SAlan Cox 
2793ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2794a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2795a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2796ac70a964STejun Heo 	}
2797ac70a964STejun Heo 
2798c6fd2807SJeff Garzik 	return 0;
2799c6fd2807SJeff Garzik 
2800c6fd2807SJeff Garzik err_out_nosup:
2801c6fd2807SJeff Garzik 	return rc;
2802c6fd2807SJeff Garzik }
2803c6fd2807SJeff Garzik 
2804c6fd2807SJeff Garzik /**
28052e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2806be0d18dfSAlan Cox  *	@ap: port
2807be0d18dfSAlan Cox  *
28082e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2809be0d18dfSAlan Cox  *	detection.
2810be0d18dfSAlan Cox  */
2811be0d18dfSAlan Cox 
2812be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2813be0d18dfSAlan Cox {
2814be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2815be0d18dfSAlan Cox }
2816a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_40wire);
2817be0d18dfSAlan Cox 
2818be0d18dfSAlan Cox /**
28192e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2820be0d18dfSAlan Cox  *	@ap: port
2821be0d18dfSAlan Cox  *
28222e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2823be0d18dfSAlan Cox  *	detection.
2824be0d18dfSAlan Cox  */
2825be0d18dfSAlan Cox 
2826be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2827be0d18dfSAlan Cox {
2828be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2829be0d18dfSAlan Cox }
2830a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_80wire);
2831be0d18dfSAlan Cox 
2832be0d18dfSAlan Cox /**
2833be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2834be0d18dfSAlan Cox  *	@ap: port
2835be0d18dfSAlan Cox  *
2836be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2837be0d18dfSAlan Cox  */
2838be0d18dfSAlan Cox 
2839be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2840be0d18dfSAlan Cox {
2841be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2842be0d18dfSAlan Cox }
2843a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_unknown);
2844be0d18dfSAlan Cox 
2845be0d18dfSAlan Cox /**
2846c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2847c88f90c3STejun Heo  *	@ap: port
2848c88f90c3STejun Heo  *
2849c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2850c88f90c3STejun Heo  *	transfer mode.
2851c88f90c3STejun Heo  */
2852c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2853c88f90c3STejun Heo {
2854c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2855c88f90c3STejun Heo }
2856a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_ignore);
2857c88f90c3STejun Heo 
2858c88f90c3STejun Heo /**
2859be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2860be0d18dfSAlan Cox  *	@ap: port
2861be0d18dfSAlan Cox  *
2862be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2863be0d18dfSAlan Cox  */
2864be0d18dfSAlan Cox 
2865be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2866be0d18dfSAlan Cox {
2867be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2868be0d18dfSAlan Cox }
2869a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_sata);
2870be0d18dfSAlan Cox 
2871be0d18dfSAlan Cox /**
2872c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2873c6fd2807SJeff Garzik  *	@ap: Bus to probe
2874c6fd2807SJeff Garzik  *
2875c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2876c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2877c6fd2807SJeff Garzik  *	the bus.
2878c6fd2807SJeff Garzik  *
2879c6fd2807SJeff Garzik  *	LOCKING:
2880c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2881c6fd2807SJeff Garzik  *
2882c6fd2807SJeff Garzik  *	RETURNS:
2883c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2884c6fd2807SJeff Garzik  */
2885c6fd2807SJeff Garzik 
2886c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2887c6fd2807SJeff Garzik {
2888c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2889c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2890f58229f8STejun Heo 	int rc;
2891c6fd2807SJeff Garzik 	struct ata_device *dev;
2892c6fd2807SJeff Garzik 
28931eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2894f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2895c6fd2807SJeff Garzik 
2896c6fd2807SJeff Garzik  retry:
28971eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2898cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2899cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2900cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2901cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2902cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2903cdeab114STejun Heo 		 * bus as we may be talking too fast.
2904cdeab114STejun Heo 		 */
2905cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
29065416912aSAaron Lu 		dev->dma_mode = 0xff;
2907cdeab114STejun Heo 
2908cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2909cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2910cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2911cdeab114STejun Heo 		 * configuring devices.
2912cdeab114STejun Heo 		 */
2913cdeab114STejun Heo 		if (ap->ops->set_piomode)
2914cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2915cdeab114STejun Heo 	}
2916cdeab114STejun Heo 
2917c6fd2807SJeff Garzik 	/* reset and determine device classes */
2918c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2919c6fd2807SJeff Garzik 
29201eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
29213e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2922c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2923c6fd2807SJeff Garzik 		else
2924c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2925c6fd2807SJeff Garzik 
2926c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2927c6fd2807SJeff Garzik 	}
2928c6fd2807SJeff Garzik 
2929f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2930f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2931f31f0cc2SJeff Garzik 	   the slave device */
2932f31f0cc2SJeff Garzik 
29331eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2934f58229f8STejun Heo 		if (tries[dev->devno])
2935f58229f8STejun Heo 			dev->class = classes[dev->devno];
2936c6fd2807SJeff Garzik 
2937c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2938c6fd2807SJeff Garzik 			continue;
2939c6fd2807SJeff Garzik 
2940bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2941bff04647STejun Heo 				     dev->id);
2942c6fd2807SJeff Garzik 		if (rc)
2943c6fd2807SJeff Garzik 			goto fail;
2944f31f0cc2SJeff Garzik 	}
2945f31f0cc2SJeff Garzik 
2946be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2947be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2948be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2949be0d18dfSAlan Cox 
29501eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
29511eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
29521eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
29531eca4365STejun Heo 	 * of the link the bridge is which is a problem.
29541eca4365STejun Heo 	 */
29551eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2956614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2957614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2958614fe29bSAlan Cox 
2959f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2960f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2961f31f0cc2SJeff Garzik 
29621eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
29639af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2964efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
29659af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2966c6fd2807SJeff Garzik 		if (rc)
2967c6fd2807SJeff Garzik 			goto fail;
2968c6fd2807SJeff Garzik 	}
2969c6fd2807SJeff Garzik 
2970c6fd2807SJeff Garzik 	/* configure transfer mode */
29710260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
29724ae72a1eSTejun Heo 	if (rc)
2973c6fd2807SJeff Garzik 		goto fail;
2974c6fd2807SJeff Garzik 
29751eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2976c6fd2807SJeff Garzik 		return 0;
2977c6fd2807SJeff Garzik 
2978c6fd2807SJeff Garzik 	return -ENODEV;
2979c6fd2807SJeff Garzik 
2980c6fd2807SJeff Garzik  fail:
29814ae72a1eSTejun Heo 	tries[dev->devno]--;
29824ae72a1eSTejun Heo 
2983c6fd2807SJeff Garzik 	switch (rc) {
2984c6fd2807SJeff Garzik 	case -EINVAL:
29854ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2986c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2987c6fd2807SJeff Garzik 		break;
29884ae72a1eSTejun Heo 
29894ae72a1eSTejun Heo 	case -ENODEV:
29904ae72a1eSTejun Heo 		/* give it just one more chance */
29914ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2992df561f66SGustavo A. R. Silva 		fallthrough;
2993c6fd2807SJeff Garzik 	case -EIO:
29944ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
29954ae72a1eSTejun Heo 			/* This is the last chance, better to slow
29964ae72a1eSTejun Heo 			 * down than lose it.
29974ae72a1eSTejun Heo 			 */
2998a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
29994ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
30004ae72a1eSTejun Heo 		}
3001c6fd2807SJeff Garzik 	}
3002c6fd2807SJeff Garzik 
30034ae72a1eSTejun Heo 	if (!tries[dev->devno])
3004c6fd2807SJeff Garzik 		ata_dev_disable(dev);
3005c6fd2807SJeff Garzik 
3006c6fd2807SJeff Garzik 	goto retry;
3007c6fd2807SJeff Garzik }
3008c6fd2807SJeff Garzik 
3009c6fd2807SJeff Garzik /**
3010c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
3011936fd732STejun Heo  *	@link: SATA link to printk link status about
3012c6fd2807SJeff Garzik  *
3013c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
3014c6fd2807SJeff Garzik  *
3015c6fd2807SJeff Garzik  *	LOCKING:
3016c6fd2807SJeff Garzik  *	None.
3017c6fd2807SJeff Garzik  */
30186bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
3019c6fd2807SJeff Garzik {
3020c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
3021c6fd2807SJeff Garzik 
3022936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
3023c6fd2807SJeff Garzik 		return;
3024936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
3025c6fd2807SJeff Garzik 
3026b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
3027c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
3028a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3029c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
3030c6fd2807SJeff Garzik 	} else {
3031a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3032c6fd2807SJeff Garzik 			      sstatus, scontrol);
3033c6fd2807SJeff Garzik 	}
3034c6fd2807SJeff Garzik }
3035c6fd2807SJeff Garzik 
3036c6fd2807SJeff Garzik /**
3037c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
3038c6fd2807SJeff Garzik  *	@adev: device
3039c6fd2807SJeff Garzik  *
3040c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
3041c6fd2807SJeff Garzik  *	present NULL is returned
3042c6fd2807SJeff Garzik  */
3043c6fd2807SJeff Garzik 
3044c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
3045c6fd2807SJeff Garzik {
30469af5c9c9STejun Heo 	struct ata_link *link = adev->link;
30479af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
3048c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
3049c6fd2807SJeff Garzik 		return NULL;
3050c6fd2807SJeff Garzik 	return pair;
3051c6fd2807SJeff Garzik }
3052a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_pair);
3053c6fd2807SJeff Garzik 
3054c6fd2807SJeff Garzik /**
3055c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
3056936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
3057a07d499bSTejun Heo  *	@spd_limit: Additional limit
3058c6fd2807SJeff Garzik  *
3059936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
3060c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
3061c6fd2807SJeff Garzik  *	using sata_set_spd().
3062c6fd2807SJeff Garzik  *
3063a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
3064a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
3065a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
3066a07d499bSTejun Heo  *	supported speed is allowed.
3067a07d499bSTejun Heo  *
3068c6fd2807SJeff Garzik  *	LOCKING:
3069c6fd2807SJeff Garzik  *	Inherited from caller.
3070c6fd2807SJeff Garzik  *
3071c6fd2807SJeff Garzik  *	RETURNS:
3072c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3073c6fd2807SJeff Garzik  */
3074a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3075c6fd2807SJeff Garzik {
3076c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
3077a07d499bSTejun Heo 	int rc, bit;
3078c6fd2807SJeff Garzik 
3079936fd732STejun Heo 	if (!sata_scr_valid(link))
3080008a7896STejun Heo 		return -EOPNOTSUPP;
3081008a7896STejun Heo 
3082008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
3083936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
3084008a7896STejun Heo 	 */
3085936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
30869913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
3087008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
3088008a7896STejun Heo 	else
3089936fd732STejun Heo 		spd = link->sata_spd;
3090c6fd2807SJeff Garzik 
3091936fd732STejun Heo 	mask = link->sata_spd_limit;
3092c6fd2807SJeff Garzik 	if (mask <= 1)
3093c6fd2807SJeff Garzik 		return -EINVAL;
3094008a7896STejun Heo 
3095008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3096a07d499bSTejun Heo 	bit = fls(mask) - 1;
3097a07d499bSTejun Heo 	mask &= ~(1 << bit);
3098c6fd2807SJeff Garzik 
30992dc0b46bSDavid Milburn 	/*
31002dc0b46bSDavid Milburn 	 * Mask off all speeds higher than or equal to the current one.  At
31012dc0b46bSDavid Milburn 	 * this point, if current SPD is not available and we previously
31022dc0b46bSDavid Milburn 	 * recorded the link speed from SStatus, the driver has already
31032dc0b46bSDavid Milburn 	 * masked off the highest bit so mask should already be 1 or 0.
31042dc0b46bSDavid Milburn 	 * Otherwise, we should not force 1.5Gbps on a link where we have
31052dc0b46bSDavid Milburn 	 * not previously recorded speed from SStatus.  Just return in this
31062dc0b46bSDavid Milburn 	 * case.
3107008a7896STejun Heo 	 */
3108008a7896STejun Heo 	if (spd > 1)
3109008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
3110008a7896STejun Heo 	else
31112dc0b46bSDavid Milburn 		return -EINVAL;
3112008a7896STejun Heo 
3113008a7896STejun Heo 	/* were we already at the bottom? */
3114c6fd2807SJeff Garzik 	if (!mask)
3115c6fd2807SJeff Garzik 		return -EINVAL;
3116c6fd2807SJeff Garzik 
3117a07d499bSTejun Heo 	if (spd_limit) {
3118a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3119a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3120a07d499bSTejun Heo 		else {
3121a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3122a07d499bSTejun Heo 			mask = 1 << bit;
3123a07d499bSTejun Heo 		}
3124a07d499bSTejun Heo 	}
3125a07d499bSTejun Heo 
3126936fd732STejun Heo 	link->sata_spd_limit = mask;
3127c6fd2807SJeff Garzik 
3128a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
3129c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
3130c6fd2807SJeff Garzik 
3131c6fd2807SJeff Garzik 	return 0;
3132c6fd2807SJeff Garzik }
3133c6fd2807SJeff Garzik 
3134a9b2c120SBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_ACPI
3135c6fd2807SJeff Garzik /**
3136a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3137a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3138a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3139a0f79b92STejun Heo  *
3140a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3141a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3142a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3143a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3144a0f79b92STejun Heo  *
3145a0f79b92STejun Heo  *	LOCKING:
3146a0f79b92STejun Heo  *	None.
3147a0f79b92STejun Heo  *
3148a0f79b92STejun Heo  *	RETURNS:
3149a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3150a0f79b92STejun Heo  */
3151a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3152a0f79b92STejun Heo {
3153a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3154a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3155a0f79b92STejun Heo 	const struct ata_timing *t;
3156a0f79b92STejun Heo 
3157a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3158a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3159a0f79b92STejun Heo 			base_mode = ent->base;
3160a0f79b92STejun Heo 
3161a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3162a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3163a0f79b92STejun Heo 		unsigned short this_cycle;
3164a0f79b92STejun Heo 
3165a0f79b92STejun Heo 		switch (xfer_shift) {
3166a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3167a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3168a0f79b92STejun Heo 			this_cycle = t->cycle;
3169a0f79b92STejun Heo 			break;
3170a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3171a0f79b92STejun Heo 			this_cycle = t->udma;
3172a0f79b92STejun Heo 			break;
3173a0f79b92STejun Heo 		default:
3174a0f79b92STejun Heo 			return 0xff;
3175a0f79b92STejun Heo 		}
3176a0f79b92STejun Heo 
3177a0f79b92STejun Heo 		if (cycle > this_cycle)
3178a0f79b92STejun Heo 			break;
3179a0f79b92STejun Heo 
3180a0f79b92STejun Heo 		last_mode = t->mode;
3181a0f79b92STejun Heo 	}
3182a0f79b92STejun Heo 
3183a0f79b92STejun Heo 	return last_mode;
3184a0f79b92STejun Heo }
3185a9b2c120SBartlomiej Zolnierkiewicz #endif
3186a0f79b92STejun Heo 
3187a0f79b92STejun Heo /**
3188c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3189c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3190458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3191c6fd2807SJeff Garzik  *
3192c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3193c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3194c6fd2807SJeff Garzik  *	will apply the limit.
3195c6fd2807SJeff Garzik  *
3196c6fd2807SJeff Garzik  *	LOCKING:
3197c6fd2807SJeff Garzik  *	Inherited from caller.
3198c6fd2807SJeff Garzik  *
3199c6fd2807SJeff Garzik  *	RETURNS:
3200c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3201c6fd2807SJeff Garzik  */
3202458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3203c6fd2807SJeff Garzik {
3204458337dbSTejun Heo 	char buf[32];
3205f0a6d77bSSergey Shtylyov 	unsigned int orig_mask, xfer_mask;
3206f0a6d77bSSergey Shtylyov 	unsigned int pio_mask, mwdma_mask, udma_mask;
3207458337dbSTejun Heo 	int quiet, highbit;
3208c6fd2807SJeff Garzik 
3209458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3210458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3211458337dbSTejun Heo 
3212458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3213458337dbSTejun Heo 						  dev->mwdma_mask,
3214c6fd2807SJeff Garzik 						  dev->udma_mask);
3215458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3216c6fd2807SJeff Garzik 
3217458337dbSTejun Heo 	switch (sel) {
3218458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3219458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3220458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3221458337dbSTejun Heo 		break;
3222458337dbSTejun Heo 
3223458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3224458337dbSTejun Heo 		if (udma_mask) {
3225458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3226458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3227458337dbSTejun Heo 			if (!udma_mask)
3228458337dbSTejun Heo 				return -ENOENT;
3229458337dbSTejun Heo 		} else if (mwdma_mask) {
3230458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3231458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3232458337dbSTejun Heo 			if (!mwdma_mask)
3233458337dbSTejun Heo 				return -ENOENT;
3234458337dbSTejun Heo 		}
3235458337dbSTejun Heo 		break;
3236458337dbSTejun Heo 
3237458337dbSTejun Heo 	case ATA_DNXFER_40C:
3238458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3239458337dbSTejun Heo 		break;
3240458337dbSTejun Heo 
3241458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3242458337dbSTejun Heo 		pio_mask &= 1;
3243df561f66SGustavo A. R. Silva 		fallthrough;
3244458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3245458337dbSTejun Heo 		mwdma_mask = 0;
3246458337dbSTejun Heo 		udma_mask = 0;
3247458337dbSTejun Heo 		break;
3248458337dbSTejun Heo 
3249458337dbSTejun Heo 	default:
3250458337dbSTejun Heo 		BUG();
3251458337dbSTejun Heo 	}
3252458337dbSTejun Heo 
3253458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3254458337dbSTejun Heo 
3255458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3256458337dbSTejun Heo 		return -ENOENT;
3257458337dbSTejun Heo 
3258458337dbSTejun Heo 	if (!quiet) {
3259458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3260458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3261458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3262458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3263458337dbSTejun Heo 		else
3264458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3265458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3266458337dbSTejun Heo 
3267a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3268458337dbSTejun Heo 	}
3269c6fd2807SJeff Garzik 
3270c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3271c6fd2807SJeff Garzik 			    &dev->udma_mask);
3272c6fd2807SJeff Garzik 
3273c6fd2807SJeff Garzik 	return 0;
3274c6fd2807SJeff Garzik }
3275c6fd2807SJeff Garzik 
3276c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3277c6fd2807SJeff Garzik {
3278d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
32799af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3280d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
32814055dee7STejun Heo 	const char *dev_err_whine = "";
32824055dee7STejun Heo 	int ign_dev_err = 0;
3283d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3284c6fd2807SJeff Garzik 	int rc;
3285c6fd2807SJeff Garzik 
3286c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3287c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3288c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3289c6fd2807SJeff Garzik 
3290d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3291d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3292d0cb43b3STejun Heo 	else {
3293d0cb43b3STejun Heo 		if (nosetxfer)
3294a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3295d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3296d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3297c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3298d0cb43b3STejun Heo 	}
32992dcb407eSJeff Garzik 
33004055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
33014055dee7STejun Heo 		goto fail;
33022dcb407eSJeff Garzik 
33034055dee7STejun Heo 	/* revalidate */
3304baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3305422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3306baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3307c6fd2807SJeff Garzik 	if (rc)
3308c6fd2807SJeff Garzik 		return rc;
3309c6fd2807SJeff Garzik 
3310b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
33114055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3312b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
33134055dee7STejun Heo 			ign_dev_err = 1;
3314b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3315b93fda12SAlan Cox 		   ATA devices */
3316b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
33174055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
33184055dee7STejun Heo 			ign_dev_err = 1;
3319b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3320b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3321b93fda12SAlan Cox 		   timings and no IORDY */
3322b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3323b93fda12SAlan Cox 			ign_dev_err = 1;
3324b93fda12SAlan Cox 	}
33254055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
33264055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
33274055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
33284055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
33294055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
33304055dee7STejun Heo 		ign_dev_err = 1;
33314055dee7STejun Heo 
33324055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
33334055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
33344055dee7STejun Heo 		ign_dev_err = 1;
33354055dee7STejun Heo 
33364055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
33374055dee7STejun Heo 		if (!ign_dev_err)
33384055dee7STejun Heo 			goto fail;
33394055dee7STejun Heo 		else
33404055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
33414055dee7STejun Heo 	}
33424055dee7STejun Heo 
33434633778bSHannes Reinecke 	ata_dev_dbg(dev, "xfer_shift=%u, xfer_mode=0x%x\n",
3344c6fd2807SJeff Garzik 		    dev->xfer_shift, (int)dev->xfer_mode);
3345c6fd2807SJeff Garzik 
334607b9b6d6SDamien Le Moal 	if (!(ehc->i.flags & ATA_EHI_QUIET) ||
334707b9b6d6SDamien Le Moal 	    ehc->i.flags & ATA_EHI_DID_HARDRESET)
3348a9a79dfeSJoe Perches 		ata_dev_info(dev, "configured for %s%s\n",
33494055dee7STejun Heo 			     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
33504055dee7STejun Heo 			     dev_err_whine);
33514055dee7STejun Heo 
3352c6fd2807SJeff Garzik 	return 0;
33534055dee7STejun Heo 
33544055dee7STejun Heo  fail:
3355a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
33564055dee7STejun Heo 	return -EIO;
3357c6fd2807SJeff Garzik }
3358c6fd2807SJeff Garzik 
3359c6fd2807SJeff Garzik /**
336004351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
33610260731fSTejun Heo  *	@link: link on which timings will be programmed
33621967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3363c6fd2807SJeff Garzik  *
336404351821SAlan  *	Standard implementation of the function used to tune and set
336504351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
336604351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3367c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3368c6fd2807SJeff Garzik  *
3369c6fd2807SJeff Garzik  *	LOCKING:
3370c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3371c6fd2807SJeff Garzik  *
3372c6fd2807SJeff Garzik  *	RETURNS:
3373c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3374c6fd2807SJeff Garzik  */
337504351821SAlan 
33760260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3377c6fd2807SJeff Garzik {
33780260731fSTejun Heo 	struct ata_port *ap = link->ap;
3379c6fd2807SJeff Garzik 	struct ata_device *dev;
3380f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3381c6fd2807SJeff Garzik 
3382c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
33831eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3384f0a6d77bSSergey Shtylyov 		unsigned int pio_mask, dma_mask;
3385b3a70601SAlan Cox 		unsigned int mode_mask;
3386c6fd2807SJeff Garzik 
3387b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3388b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3389b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3390b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3391b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3392b3a70601SAlan Cox 
3393c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
339433267325STejun Heo 		ata_force_xfermask(dev);
3395c6fd2807SJeff Garzik 
3396c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3397b3a70601SAlan Cox 
3398b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
339980a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
340080a9c430SSergei Shtylyov 						     dev->udma_mask);
3401b3a70601SAlan Cox 		else
3402b3a70601SAlan Cox 			dma_mask = 0;
3403b3a70601SAlan Cox 
3404c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3405c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3406c6fd2807SJeff Garzik 
3407c6fd2807SJeff Garzik 		found = 1;
3408b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3409c6fd2807SJeff Garzik 			used_dma = 1;
3410c6fd2807SJeff Garzik 	}
3411c6fd2807SJeff Garzik 	if (!found)
3412c6fd2807SJeff Garzik 		goto out;
3413c6fd2807SJeff Garzik 
3414c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
34151eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
341670cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3417a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3418c6fd2807SJeff Garzik 			rc = -EINVAL;
3419c6fd2807SJeff Garzik 			goto out;
3420c6fd2807SJeff Garzik 		}
3421c6fd2807SJeff Garzik 
3422c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3423c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3424c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3425c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3426c6fd2807SJeff Garzik 	}
3427c6fd2807SJeff Garzik 
3428c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
34291eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
34301eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3431c6fd2807SJeff Garzik 			continue;
3432c6fd2807SJeff Garzik 
3433c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3434c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3435c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3436c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3437c6fd2807SJeff Garzik 	}
3438c6fd2807SJeff Garzik 
3439c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
34401eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3441c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3442c6fd2807SJeff Garzik 		if (rc)
3443c6fd2807SJeff Garzik 			goto out;
3444c6fd2807SJeff Garzik 	}
3445c6fd2807SJeff Garzik 
3446c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3447c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3448c6fd2807SJeff Garzik 	 */
3449cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3450032af1ceSAlan 		ap->host->simplex_claimed = ap;
3451c6fd2807SJeff Garzik 
3452c6fd2807SJeff Garzik  out:
3453c6fd2807SJeff Garzik 	if (rc)
3454c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3455c6fd2807SJeff Garzik 	return rc;
3456c6fd2807SJeff Garzik }
3457a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_set_mode);
3458c6fd2807SJeff Garzik 
3459c6fd2807SJeff Garzik /**
3460aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3461aa2731adSTejun Heo  *	@link: link to be waited on
3462aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3463aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3464aa2731adSTejun Heo  *
3465aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3466aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3467aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3468aa2731adSTejun Heo  *	conditions.
3469aa2731adSTejun Heo  *
3470aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3471aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3472aa2731adSTejun Heo  *
3473aa2731adSTejun Heo  *	LOCKING:
3474aa2731adSTejun Heo  *	EH context.
3475aa2731adSTejun Heo  *
3476aa2731adSTejun Heo  *	RETURNS:
3477c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3478aa2731adSTejun Heo  */
3479aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3480aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3481aa2731adSTejun Heo {
3482aa2731adSTejun Heo 	unsigned long start = jiffies;
3483b48d58f5STejun Heo 	unsigned long nodev_deadline;
3484aa2731adSTejun Heo 	int warned = 0;
3485aa2731adSTejun Heo 
3486b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3487b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3488b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3489b48d58f5STejun Heo 	else
3490b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3491b48d58f5STejun Heo 
3492b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3493b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3494b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3495b1c72916STejun Heo 	 */
3496b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3497b1c72916STejun Heo 
3498aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3499aa2731adSTejun Heo 		nodev_deadline = deadline;
3500aa2731adSTejun Heo 
3501aa2731adSTejun Heo 	while (1) {
3502aa2731adSTejun Heo 		unsigned long now = jiffies;
3503aa2731adSTejun Heo 		int ready, tmp;
3504aa2731adSTejun Heo 
3505aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3506aa2731adSTejun Heo 		if (ready > 0)
3507aa2731adSTejun Heo 			return 0;
3508aa2731adSTejun Heo 
3509b48d58f5STejun Heo 		/*
3510b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3511aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3512b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3513b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3514b48d58f5STejun Heo 		 * offline.
3515aa2731adSTejun Heo 		 *
3516aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3517aa2731adSTejun Heo 		 * if status register is read more than once when
3518aa2731adSTejun Heo 		 * there's no device attached.
3519aa2731adSTejun Heo 		 */
3520aa2731adSTejun Heo 		if (ready == -ENODEV) {
3521aa2731adSTejun Heo 			if (ata_link_online(link))
3522aa2731adSTejun Heo 				ready = 0;
3523aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3524aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3525aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3526aa2731adSTejun Heo 				ready = 0;
3527aa2731adSTejun Heo 		}
3528aa2731adSTejun Heo 
3529aa2731adSTejun Heo 		if (ready)
3530aa2731adSTejun Heo 			return ready;
3531aa2731adSTejun Heo 		if (time_after(now, deadline))
3532aa2731adSTejun Heo 			return -EBUSY;
3533aa2731adSTejun Heo 
3534aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3535aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3536a9a79dfeSJoe Perches 			ata_link_warn(link,
3537aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3538aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3539aa2731adSTejun Heo 			warned = 1;
3540aa2731adSTejun Heo 		}
3541aa2731adSTejun Heo 
354297750cebSTejun Heo 		ata_msleep(link->ap, 50);
3543aa2731adSTejun Heo 	}
3544aa2731adSTejun Heo }
3545aa2731adSTejun Heo 
3546aa2731adSTejun Heo /**
3547aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3548aa2731adSTejun Heo  *	@link: link to be waited on
3549aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3550aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3551aa2731adSTejun Heo  *
3552aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3553aa2731adSTejun Heo  *
3554aa2731adSTejun Heo  *	LOCKING:
3555aa2731adSTejun Heo  *	EH context.
3556aa2731adSTejun Heo  *
3557aa2731adSTejun Heo  *	RETURNS:
3558c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3559aa2731adSTejun Heo  */
35602b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3561aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3562aa2731adSTejun Heo {
356397750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3564aa2731adSTejun Heo 
3565aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3566aa2731adSTejun Heo }
3567a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_after_reset);
3568aa2731adSTejun Heo 
3569aa2731adSTejun Heo /**
35700aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3571cc0680a5STejun Heo  *	@link: ATA link to be reset
3572d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3573c6fd2807SJeff Garzik  *
3574cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3575b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3576b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3577b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3578b8cffc6aSTejun Heo  *	should just whine, not fail.
3579c6fd2807SJeff Garzik  *
3580c6fd2807SJeff Garzik  *	LOCKING:
3581c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3582c6fd2807SJeff Garzik  *
3583c6fd2807SJeff Garzik  *	RETURNS:
3584ac1eb665SSergey Shtylyov  *	Always 0.
3585c6fd2807SJeff Garzik  */
35860aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3587c6fd2807SJeff Garzik {
3588cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3589936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3590c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3591c6fd2807SJeff Garzik 	int rc;
3592c6fd2807SJeff Garzik 
3593c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3594c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3595c6fd2807SJeff Garzik 		return 0;
3596c6fd2807SJeff Garzik 
3597936fd732STejun Heo 	/* if SATA, resume link */
3598a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3599936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3600b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3601b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3602a9a79dfeSJoe Perches 			ata_link_warn(link,
3603a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3604a9a79dfeSJoe Perches 				      rc);
3605c6fd2807SJeff Garzik 	}
3606c6fd2807SJeff Garzik 
360745db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3608b1c72916STejun Heo 	if (ata_phys_link_offline(link))
360945db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
361045db2f6cSTejun Heo 
3611c6fd2807SJeff Garzik 	return 0;
3612c6fd2807SJeff Garzik }
3613a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_prereset);
3614c6fd2807SJeff Garzik 
3615c6fd2807SJeff Garzik /**
361657c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
361757c9efdfSTejun Heo  *	@link: link to reset
361857c9efdfSTejun Heo  *	@class: resulting class of attached device
361957c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
362057c9efdfSTejun Heo  *
362157c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
362257c9efdfSTejun Heo  *
362357c9efdfSTejun Heo  *	LOCKING:
362457c9efdfSTejun Heo  *	Kernel thread context (may sleep)
362557c9efdfSTejun Heo  *
362657c9efdfSTejun Heo  *	RETURNS:
362757c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
362857c9efdfSTejun Heo  */
362957c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
363057c9efdfSTejun Heo 		       unsigned long deadline)
363157c9efdfSTejun Heo {
363257c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
363357c9efdfSTejun Heo 	bool online;
363457c9efdfSTejun Heo 	int rc;
363557c9efdfSTejun Heo 
363657c9efdfSTejun Heo 	/* do hardreset */
363757c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
363857c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
363957c9efdfSTejun Heo }
3640a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(sata_std_hardreset);
364157c9efdfSTejun Heo 
364257c9efdfSTejun Heo /**
3643203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3644cc0680a5STejun Heo  *	@link: the target ata_link
3645c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3646c6fd2807SJeff Garzik  *
3647c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3648c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3649c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3650c6fd2807SJeff Garzik  *
3651c6fd2807SJeff Garzik  *	LOCKING:
3652c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3653c6fd2807SJeff Garzik  */
3654203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3655c6fd2807SJeff Garzik {
3656f046519fSTejun Heo 	u32 serror;
3657f046519fSTejun Heo 
3658f046519fSTejun Heo 	/* reset complete, clear SError */
3659f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3660f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3661f046519fSTejun Heo 
3662c6fd2807SJeff Garzik 	/* print link status */
3663936fd732STejun Heo 	sata_print_link_status(link);
3664c6fd2807SJeff Garzik }
3665a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_postreset);
3666c6fd2807SJeff Garzik 
3667c6fd2807SJeff Garzik /**
3668c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3669c6fd2807SJeff Garzik  *	@dev: device to compare against
3670c6fd2807SJeff Garzik  *	@new_class: class of the new device
3671c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3672c6fd2807SJeff Garzik  *
3673c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3674c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3675c6fd2807SJeff Garzik  *	@new_id.
3676c6fd2807SJeff Garzik  *
3677c6fd2807SJeff Garzik  *	LOCKING:
3678c6fd2807SJeff Garzik  *	None.
3679c6fd2807SJeff Garzik  *
3680c6fd2807SJeff Garzik  *	RETURNS:
3681c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3682c6fd2807SJeff Garzik  */
3683c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3684c6fd2807SJeff Garzik 			       const u16 *new_id)
3685c6fd2807SJeff Garzik {
3686c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3687a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3688a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3689c6fd2807SJeff Garzik 
3690c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3691a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
3692c6fd2807SJeff Garzik 			     dev->class, new_class);
3693c6fd2807SJeff Garzik 		return 0;
3694c6fd2807SJeff Garzik 	}
3695c6fd2807SJeff Garzik 
3696a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3697a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3698a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3699a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3700c6fd2807SJeff Garzik 
3701c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3702a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3703a9a79dfeSJoe Perches 			     model[0], model[1]);
3704c6fd2807SJeff Garzik 		return 0;
3705c6fd2807SJeff Garzik 	}
3706c6fd2807SJeff Garzik 
3707c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3708a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3709a9a79dfeSJoe Perches 			     serial[0], serial[1]);
3710c6fd2807SJeff Garzik 		return 0;
3711c6fd2807SJeff Garzik 	}
3712c6fd2807SJeff Garzik 
3713c6fd2807SJeff Garzik 	return 1;
3714c6fd2807SJeff Garzik }
3715c6fd2807SJeff Garzik 
3716c6fd2807SJeff Garzik /**
3717fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
37183fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3719bff04647STejun Heo  *	@readid_flags: read ID flags
3720c6fd2807SJeff Garzik  *
3721c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3722c6fd2807SJeff Garzik  *	the port.
3723c6fd2807SJeff Garzik  *
3724c6fd2807SJeff Garzik  *	LOCKING:
3725c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3726c6fd2807SJeff Garzik  *
3727c6fd2807SJeff Garzik  *	RETURNS:
3728c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3729c6fd2807SJeff Garzik  */
3730fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3731c6fd2807SJeff Garzik {
3732c6fd2807SJeff Garzik 	unsigned int class = dev->class;
37339af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3734c6fd2807SJeff Garzik 	int rc;
3735c6fd2807SJeff Garzik 
3736c6fd2807SJeff Garzik 	/* read ID data */
3737bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3738c6fd2807SJeff Garzik 	if (rc)
3739fe30911bSTejun Heo 		return rc;
3740c6fd2807SJeff Garzik 
3741c6fd2807SJeff Garzik 	/* is the device still there? */
3742fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3743fe30911bSTejun Heo 		return -ENODEV;
3744c6fd2807SJeff Garzik 
3745c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3746fe30911bSTejun Heo 	return 0;
3747fe30911bSTejun Heo }
3748fe30911bSTejun Heo 
3749fe30911bSTejun Heo /**
3750fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3751fe30911bSTejun Heo  *	@dev: device to revalidate
3752422c9daaSTejun Heo  *	@new_class: new class code
3753fe30911bSTejun Heo  *	@readid_flags: read ID flags
3754fe30911bSTejun Heo  *
3755fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3756fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3757fe30911bSTejun Heo  *
3758fe30911bSTejun Heo  *	LOCKING:
3759fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3760fe30911bSTejun Heo  *
3761fe30911bSTejun Heo  *	RETURNS:
3762fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3763fe30911bSTejun Heo  */
3764422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3765422c9daaSTejun Heo 		       unsigned int readid_flags)
3766fe30911bSTejun Heo {
37676ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
37685920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
3769fe30911bSTejun Heo 	int rc;
3770fe30911bSTejun Heo 
3771fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3772fe30911bSTejun Heo 		return -ENODEV;
3773fe30911bSTejun Heo 
3774422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3775422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
3776f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
3777f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
37789162c657SHannes Reinecke 	    new_class != ATA_DEV_ZAC &&
3779f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
3780a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
3781422c9daaSTejun Heo 			     dev->class, new_class);
3782422c9daaSTejun Heo 		rc = -ENODEV;
3783422c9daaSTejun Heo 		goto fail;
3784422c9daaSTejun Heo 	}
3785422c9daaSTejun Heo 
3786fe30911bSTejun Heo 	/* re-read ID */
3787fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3788fe30911bSTejun Heo 	if (rc)
3789fe30911bSTejun Heo 		goto fail;
3790c6fd2807SJeff Garzik 
3791c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3792efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
37936ddcd3b0STejun Heo 	if (rc)
37946ddcd3b0STejun Heo 		goto fail;
37956ddcd3b0STejun Heo 
37966ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
3797445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
3798445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
3799445d211bSTejun Heo 		return 0;
3800445d211bSTejun Heo 
3801445d211bSTejun Heo 	/* n_sectors has changed */
3802a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
38036ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
38046ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
3805445d211bSTejun Heo 
38065920dadfSTejun Heo 	/*
38075920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
3808445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
3809445d211bSTejun Heo 	 * new size matches it, keep the device.
38105920dadfSTejun Heo 	 */
38115920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3812445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
3813a9a79dfeSJoe Perches 		ata_dev_warn(dev,
38145920dadfSTejun Heo 			     "new n_sectors matches native, probably "
381568939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
381668939ce5STejun Heo 		/* use the larger n_sectors */
3817445d211bSTejun Heo 		return 0;
3818445d211bSTejun Heo 	}
3819445d211bSTejun Heo 
3820445d211bSTejun Heo 	/*
3821445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
3822445d211bSTejun Heo 	 * unlocking HPA in those cases.
3823445d211bSTejun Heo 	 *
3824445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3825445d211bSTejun Heo 	 */
3826445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3827445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3828445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
3829a9a79dfeSJoe Perches 		ata_dev_warn(dev,
3830445d211bSTejun Heo 			     "old n_sectors matches native, probably "
3831445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
3832445d211bSTejun Heo 		/* try unlocking HPA */
3833445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3834445d211bSTejun Heo 		rc = -EIO;
3835445d211bSTejun Heo 	} else
3836445d211bSTejun Heo 		rc = -ENODEV;
3837445d211bSTejun Heo 
3838445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
38395920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
38405920dadfSTejun Heo 	dev->n_sectors = n_sectors;
3841c6fd2807SJeff Garzik  fail:
3842a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
3843c6fd2807SJeff Garzik 	return rc;
3844c6fd2807SJeff Garzik }
3845c6fd2807SJeff Garzik 
38466919a0a6SAlan Cox struct ata_blacklist_entry {
38476919a0a6SAlan Cox 	const char *model_num;
38486919a0a6SAlan Cox 	const char *model_rev;
38496919a0a6SAlan Cox 	unsigned long horkage;
38506919a0a6SAlan Cox };
38516919a0a6SAlan Cox 
38526919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
38536919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
38546919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
38556919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
38566919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
38576919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
38586919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
38596919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
38606919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
38616919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
38626919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
38637da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
38646919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
38656919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
38666919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
38676919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
38686919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
38697da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
38706919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
38716919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
38726919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
38736919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
38746919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
38756919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
38766919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
38776919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
38786919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
38796919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
38806919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
388139f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
3882d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
3883b00622fcSShikha Jain 	{ "VRFDFC22048UCHC-TE*", NULL,		ATA_HORKAGE_NODMA },
38843af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
388550af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
3886a66307d4SHannes Reinecke 	/* Similar story with ASMedia 1092 */
3887a66307d4SHannes Reinecke 	{ "ASMT109x- Config",	NULL,		ATA_HORKAGE_DISABLE },
38886919a0a6SAlan Cox 
388918d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
389040a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
38916a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
3892a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
38930523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
389418d6e9d5SAlbert Lee 
3895af34d637SDavid Milburn 	/*
3896af34d637SDavid Milburn 	 * Causes silent data corruption with higher max sects.
3897af34d637SDavid Milburn 	 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3898af34d637SDavid Milburn 	 */
3899af34d637SDavid Milburn 	{ "ST380013AS",		"3.20",		ATA_HORKAGE_MAX_SEC_1024 },
3900af34d637SDavid Milburn 
39011488a1e3STejun Heo 	/*
3902e0edc8c5STejun Heo 	 * These devices time out with higher max sects.
39031488a1e3STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
39041488a1e3STejun Heo 	 */
3905e0edc8c5STejun Heo 	{ "LITEON CX1-JB*-HP",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
3906db5ff909SXinyu Lin 	{ "LITEON EP1-*",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
39071488a1e3STejun Heo 
39086919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
39096919a0a6SAlan Cox 
39106919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
39116919a0a6SAlan Cox 	/* NCQ is slow */
39126919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
3913ef1429c0SDamien Le Moal 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ },
391409125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
391509125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
39167acfaf30SPaul Rolland 	/* NCQ is broken */
3917539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
39180e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
3919da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
3920e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
39215ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
3922539cc7c7SJeff Garzik 
3923ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
39244d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3925ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3926d10d491fSTejun Heo 
39274d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3928d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3929d10d491fSTejun Heo 
39304d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3931d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3932d10d491fSTejun Heo 
39334d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3934ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3935ac70a964STejun Heo 
393631f6264eSHans de Goede 	/* drives which fail FPDMA_AA activation (some may freeze afterwards)
393731f6264eSHans de Goede 	   the ST disks also have LPM issues */
39388756a25bSHans de Goede 	{ "ST1000LM024 HN-M101MBB", NULL,	ATA_HORKAGE_BROKEN_FPDMA_AA |
3939ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
394008c85d2aSAleksei Mamlin 	{ "VB0250EAVER",	"HPG7",		ATA_HORKAGE_BROKEN_FPDMA_AA },
394187809942SMichele Baldessari 
394236e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
394336e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
3944ef1429c0SDamien Le Moal 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ },
3945ef1429c0SDamien Le Moal 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ },
3946ef1429c0SDamien Le Moal 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ },
39476919a0a6SAlan Cox 
394868b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
3949ef1429c0SDamien Le Moal 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ },
395068b0ddb2STejun Heo 
39513b545563STejun Heo 	/* Sandisk SD7/8/9s lock up hard on large trims */
3952ef1429c0SDamien Le Moal 	{ "SanDisk SD[789]*",	NULL,		ATA_HORKAGE_MAX_TRIM_128M },
3953322579dcSTejun Heo 
395416c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
3955ef1429c0SDamien Le Moal 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA },
395616c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
395716c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
395816c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
39596919a0a6SAlan Cox 
39607831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
39617831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
39627831387bSTejun Heo 
396393328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
3964ef1429c0SDamien Le Moal 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE },
3965ef1429c0SDamien Le Moal 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE },
3966ef1429c0SDamien Le Moal 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE },
396793328e11SAlan Cox 
39686bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
3969ef1429c0SDamien Le Moal 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB },
3970a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
3971ef1429c0SDamien Le Moal 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB },
39726bbfd53dSAlan Cox 
39739ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
3974ef1429c0SDamien Le Moal 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK },
3975ef1429c0SDamien Le Moal 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK },
39769ce8e307SJens Axboe 
39779062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
3978ef1429c0SDamien Le Moal 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS },
3979ef1429c0SDamien Le Moal 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS },
39809062712fSTejun Heo 
3981d0cb43b3STejun Heo 	/*
3982d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
3983d0cb43b3STejun Heo 	 * device and controller are SATA.
3984d0cb43b3STejun Heo 	 */
3985cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
39863a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
39873a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
3988cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
3989cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
3990d0cb43b3STejun Heo 
3991b17e5729SKai-Heng Feng 	/* Crucial BX100 SSD 500GB has broken LPM support */
39923bf7b5d6SHans de Goede 	{ "CT500BX100SSD1",		NULL,	ATA_HORKAGE_NOLPM },
3993b17e5729SKai-Heng Feng 
3994d418ff56SHans de Goede 	/* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
3995d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
39969c7be59fSHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
3997ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
3998d418ff56SHans de Goede 	/* 512GB MX100 with newer firmware has only LPM issues */
3999d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM |
4000ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
40019c7be59fSHans de Goede 
400262ac3f73SHans de Goede 	/* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
400362ac3f73SHans de Goede 	{ "Crucial_CT480M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
400462ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4005ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
400662ac3f73SHans de Goede 	{ "Crucial_CT960M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
400762ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4008ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
400962ac3f73SHans de Goede 
401076936e9aSFrançois Cami 	/* These specific Samsung models/firmware-revs do not handle LPM well */
4011ef1429c0SDamien Le Moal 	{ "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM },
4012ef1429c0SDamien Le Moal 	{ "SAMSUNG SSD PM830 mSATA *",  "CXM13D1Q", ATA_HORKAGE_NOLPM },
4013ef1429c0SDamien Le Moal 	{ "SAMSUNG MZ7TD256HAFV-000L9", NULL,       ATA_HORKAGE_NOLPM },
4014ef1429c0SDamien Le Moal 	{ "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM },
4015b5b4d3a5SHans de Goede 
4016f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
4017136d769eSSudip Mukherjee 	{ "Micron_M500IT_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4018ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4019243918beSMartin K. Petersen 	{ "Micron_M500_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4020ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4021ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4022ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
40239051bd39SMartin K. Petersen 	{ "Micron_M5[15]0_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4024ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4025ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M550*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4026ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4027ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*MX100*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4028ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
402953997522SChristian Lamparter 	{ "Samsung SSD 840 EVO*",	NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
403053997522SChristian Lamparter 						ATA_HORKAGE_NO_DMA_LOG |
4031ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4032ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 840*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4033ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4034ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 850*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4035ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
40368a6430abSHans de Goede 	{ "Samsung SSD 860*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
40377a8526a5SKate Hsuan 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4038ef1429c0SDamien Le Moal 						ATA_HORKAGE_NO_NCQ_ON_ATI },
40398a6430abSHans de Goede 	{ "Samsung SSD 870*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
40407a8526a5SKate Hsuan 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4041ef1429c0SDamien Le Moal 						ATA_HORKAGE_NO_NCQ_ON_ATI },
40427a7184b0SGuillermo A. Amaral 	{ "FCCT*M500*",			NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4043ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4044e61f7d1cSMartin K. Petersen 
4045cda57b1bSArne Fitzenreiter 	/* devices that don't properly handle TRIM commands */
4046ef1429c0SDamien Le Moal 	{ "SuperSSpeed S238*",		NULL,	ATA_HORKAGE_NOTRIM },
4047ef1429c0SDamien Le Moal 	{ "M88V29*",			NULL,	ATA_HORKAGE_NOTRIM },
4048cda57b1bSArne Fitzenreiter 
4049e61f7d1cSMartin K. Petersen 	/*
4050e61f7d1cSMartin K. Petersen 	 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4051e61f7d1cSMartin K. Petersen 	 * (Return Zero After Trim) flags in the ATA Command Set are
4052e61f7d1cSMartin K. Petersen 	 * unreliable in the sense that they only define what happens if
4053e61f7d1cSMartin K. Petersen 	 * the device successfully executed the DSM TRIM command. TRIM
4054e61f7d1cSMartin K. Petersen 	 * is only advisory, however, and the device is free to silently
4055e61f7d1cSMartin K. Petersen 	 * ignore all or parts of the request.
4056e61f7d1cSMartin K. Petersen 	 *
4057e61f7d1cSMartin K. Petersen 	 * Whitelist drives that are known to reliably return zeroes
4058e61f7d1cSMartin K. Petersen 	 * after TRIM.
4059e61f7d1cSMartin K. Petersen 	 */
4060e61f7d1cSMartin K. Petersen 
4061e61f7d1cSMartin K. Petersen 	/*
4062e61f7d1cSMartin K. Petersen 	 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4063e61f7d1cSMartin K. Petersen 	 * that model before whitelisting all other intel SSDs.
4064e61f7d1cSMartin K. Petersen 	 */
4065ef1429c0SDamien Le Moal 	{ "INTEL*SSDSC2MH*",		NULL,	0 },
4066e61f7d1cSMartin K. Petersen 
4067ef1429c0SDamien Le Moal 	{ "Micron*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4068ef1429c0SDamien Le Moal 	{ "Crucial*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4069ef1429c0SDamien Le Moal 	{ "INTEL*SSD*", 		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4070ef1429c0SDamien Le Moal 	{ "SSD*INTEL*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4071ef1429c0SDamien Le Moal 	{ "Samsung*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4072ef1429c0SDamien Le Moal 	{ "SAMSUNG*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4073ef1429c0SDamien Le Moal 	{ "SAMSUNG*MZ7KM*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4074ef1429c0SDamien Le Moal 	{ "ST[1248][0248]0[FH]*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4075f78dea06SMarc Carino 
4076ecd75ad5STejun Heo 	/*
4077ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4078ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4079ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4080ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4081ecd75ad5STejun Heo 	 * lost too.
4082ecd75ad5STejun Heo 	 *
4083ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4084ecd75ad5STejun Heo 	 */
4085ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4086ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4087ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4088ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4089ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4090ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4091ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4092ecd75ad5STejun Heo 
4093ac9f0c81SAnton Lundin 	/*
4094ac9f0c81SAnton Lundin 	 * This sata dom device goes on a walkabout when the ATA_LOG_DIRECTORY
4095ac9f0c81SAnton Lundin 	 * log page is accessed. Ensure we never ask for this log page with
4096ac9f0c81SAnton Lundin 	 * these devices.
4097ac9f0c81SAnton Lundin 	 */
4098ac9f0c81SAnton Lundin 	{ "SATADOM-ML 3ME",		NULL,	ATA_HORKAGE_NO_LOG_DIR },
4099ac9f0c81SAnton Lundin 
41006919a0a6SAlan Cox 	/* End Marker */
41016919a0a6SAlan Cox 	{ }
4102c6fd2807SJeff Garzik };
4103c6fd2807SJeff Garzik 
410475683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4105c6fd2807SJeff Garzik {
41068bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
41078bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
41086919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4109c6fd2807SJeff Garzik 
41108bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
41118bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4112c6fd2807SJeff Garzik 
41136919a0a6SAlan Cox 	while (ad->model_num) {
41141c402799SGeorge Spelvin 		if (glob_match(ad->model_num, model_num)) {
41156919a0a6SAlan Cox 			if (ad->model_rev == NULL)
41166919a0a6SAlan Cox 				return ad->horkage;
41171c402799SGeorge Spelvin 			if (glob_match(ad->model_rev, model_rev))
41186919a0a6SAlan Cox 				return ad->horkage;
4119c6fd2807SJeff Garzik 		}
41206919a0a6SAlan Cox 		ad++;
4121c6fd2807SJeff Garzik 	}
4122c6fd2807SJeff Garzik 	return 0;
4123c6fd2807SJeff Garzik }
4124c6fd2807SJeff Garzik 
41256919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
41266919a0a6SAlan Cox {
41276919a0a6SAlan Cox 	/* We don't support polling DMA.
41286919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
41296919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
41306919a0a6SAlan Cox 	 */
41319af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
41326919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
41336919a0a6SAlan Cox 		return 1;
413475683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
41356919a0a6SAlan Cox }
41366919a0a6SAlan Cox 
4137c6fd2807SJeff Garzik /**
41386bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
41396bbfd53dSAlan Cox  *	@dev: device
41406bbfd53dSAlan Cox  *
41416bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
41426bbfd53dSAlan Cox  *	who can't follow the documentation.
41436bbfd53dSAlan Cox  */
41446bbfd53dSAlan Cox 
41456bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
41466bbfd53dSAlan Cox {
41476bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
41486bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
41496bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
41506bbfd53dSAlan Cox }
41516bbfd53dSAlan Cox 
41526bbfd53dSAlan Cox /**
415315a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
415415a5551cSAlan Cox  *	@ap: port to consider
415515a5551cSAlan Cox  *
415615a5551cSAlan Cox  *	This function encapsulates the policy for speed management
415715a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
415815a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
415915a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
416015a5551cSAlan Cox  *	impacts hotplug at all).
416115a5551cSAlan Cox  *
416215a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
416315a5551cSAlan Cox  */
416415a5551cSAlan Cox 
416515a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
416615a5551cSAlan Cox {
416715a5551cSAlan Cox 	struct ata_link *link;
416815a5551cSAlan Cox 	struct ata_device *dev;
416915a5551cSAlan Cox 
41704a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
417115a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
417215a5551cSAlan Cox 		return 1;
41734a9c7b33STejun Heo 
41744a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
417515a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
417615a5551cSAlan Cox 		return 0;
41774a9c7b33STejun Heo 
41784a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
41794a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
41804a9c7b33STejun Heo 	 * isn't sure.
41814a9c7b33STejun Heo 	 */
4182f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4183f792068eSAlan Cox 		return 0;
418415a5551cSAlan Cox 
41854a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
41864a9c7b33STejun Heo 	 *
41874a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
41884a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
41894a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
41904a9c7b33STejun Heo 	 *   give a valid detect
41914a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
41924a9c7b33STejun Heo 	 *   to colour the choice
419315a5551cSAlan Cox 	 */
41941eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
41951eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
41961eca4365STejun Heo 			if (!ata_is_40wire(dev))
419715a5551cSAlan Cox 				return 0;
419815a5551cSAlan Cox 		}
419915a5551cSAlan Cox 	}
420015a5551cSAlan Cox 	return 1;
420115a5551cSAlan Cox }
420215a5551cSAlan Cox 
420315a5551cSAlan Cox /**
4204c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4205c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4206c6fd2807SJeff Garzik  *
4207c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4208c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4209c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4210c6fd2807SJeff Garzik  *	blacklist, etc...
4211c6fd2807SJeff Garzik  *
4212c6fd2807SJeff Garzik  *	LOCKING:
4213c6fd2807SJeff Garzik  *	None.
4214c6fd2807SJeff Garzik  */
4215c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4216c6fd2807SJeff Garzik {
42179af5c9c9STejun Heo 	struct ata_link *link = dev->link;
42189af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4219cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4220f0a6d77bSSergey Shtylyov 	unsigned int xfer_mask;
4221c6fd2807SJeff Garzik 
4222c6fd2807SJeff Garzik 	/* controller modes available */
4223c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4224c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4225c6fd2807SJeff Garzik 
42268343f889SRobert Hancock 	/* drive modes available */
4227c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4228c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4229c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4230c6fd2807SJeff Garzik 
4231b352e57dSAlan Cox 	/*
4232b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4233b352e57dSAlan Cox 	 *	cable
4234b352e57dSAlan Cox 	 */
4235b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4236b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4237b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4238b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4239b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4240b352e57dSAlan Cox 	}
4241b352e57dSAlan Cox 
4242c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4243c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4244a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4245c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4246c6fd2807SJeff Garzik 	}
4247c6fd2807SJeff Garzik 
424814d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
424914d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4250c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4251a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4252a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4253c6fd2807SJeff Garzik 	}
4254c6fd2807SJeff Garzik 
4255e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4256e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4257e424675fSJeff Garzik 
4258c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4259a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4260c6fd2807SJeff Garzik 
42618343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
42628343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
42638343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
42648343f889SRobert Hancock 	 * solely limited by the cable.
42658343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
42668343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
42678343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
42688343f889SRobert Hancock 	 */
42698343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
42708343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
427115a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4272a9a79dfeSJoe Perches 			ata_dev_warn(dev,
42738343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
42748343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
42758343f889SRobert Hancock 		}
42768343f889SRobert Hancock 
4277c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4278c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4279c6fd2807SJeff Garzik }
4280c6fd2807SJeff Garzik 
4281c6fd2807SJeff Garzik /**
4282c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4283c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4284c6fd2807SJeff Garzik  *
4285c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4286c6fd2807SJeff Garzik  *	on port @ap.
4287c6fd2807SJeff Garzik  *
4288c6fd2807SJeff Garzik  *	LOCKING:
4289c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4290c6fd2807SJeff Garzik  *
4291c6fd2807SJeff Garzik  *	RETURNS:
4292c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4293c6fd2807SJeff Garzik  */
4294c6fd2807SJeff Garzik 
4295c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4296c6fd2807SJeff Garzik {
4297c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4298c6fd2807SJeff Garzik 	unsigned int err_mask;
4299c6fd2807SJeff Garzik 
4300c6fd2807SJeff Garzik 	/* set up set-features taskfile */
43014633778bSHannes Reinecke 	ata_dev_dbg(dev, "set features - xfer mode\n");
4302c6fd2807SJeff Garzik 
4303464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4304464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4305464cf177STejun Heo 	 */
4306c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4307c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4308c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4309464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4310c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4311b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4312b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4313c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4314b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4315b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4316b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4317b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4318b9f8ab2dSAlan Cox 		return 0;
4319c6fd2807SJeff Garzik 
4320d531be2cSMikulas Patocka 	/* On some disks, this command causes spin-up, so we need longer timeout */
4321d531be2cSMikulas Patocka 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4322c6fd2807SJeff Garzik 
4323c6fd2807SJeff Garzik 	return err_mask;
4324c6fd2807SJeff Garzik }
43251152b261STejun Heo 
4326c6fd2807SJeff Garzik /**
4327218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
43289f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
43299f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4330218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
43319f45cbd3SKristen Carlson Accardi  *
43329f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4333218f3d30SJeff Garzik  *	on port @ap with sector count
43349f45cbd3SKristen Carlson Accardi  *
43359f45cbd3SKristen Carlson Accardi  *	LOCKING:
43369f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
43379f45cbd3SKristen Carlson Accardi  *
43389f45cbd3SKristen Carlson Accardi  *	RETURNS:
43399f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
43409f45cbd3SKristen Carlson Accardi  */
43411152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
43429f45cbd3SKristen Carlson Accardi {
43439f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
43449f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
4345*61176eedSSergey Shtylyov 	unsigned int timeout = 0;
43469f45cbd3SKristen Carlson Accardi 
43479f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
43484633778bSHannes Reinecke 	ata_dev_dbg(dev, "set features - SATA features\n");
43499f45cbd3SKristen Carlson Accardi 
43509f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
43519f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
43529f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
43539f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
43549f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4355218f3d30SJeff Garzik 	tf.nsect = feature;
43569f45cbd3SKristen Carlson Accardi 
4357974e0a45SDamien Le Moal 	if (enable == SETFEATURES_SPINUP)
4358974e0a45SDamien Le Moal 		timeout = ata_probe_timeout ?
4359974e0a45SDamien Le Moal 			  ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4360974e0a45SDamien Le Moal 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
43619f45cbd3SKristen Carlson Accardi 
43629f45cbd3SKristen Carlson Accardi 	return err_mask;
43639f45cbd3SKristen Carlson Accardi }
4364633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
43659f45cbd3SKristen Carlson Accardi 
43669f45cbd3SKristen Carlson Accardi /**
4367c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4368c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4369c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4370c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4371c6fd2807SJeff Garzik  *
4372c6fd2807SJeff Garzik  *	LOCKING:
4373c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4374c6fd2807SJeff Garzik  *
4375c6fd2807SJeff Garzik  *	RETURNS:
4376c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4377c6fd2807SJeff Garzik  */
4378c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4379c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4380c6fd2807SJeff Garzik {
4381c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4382c6fd2807SJeff Garzik 	unsigned int err_mask;
4383c6fd2807SJeff Garzik 
4384c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4385c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4386c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4387c6fd2807SJeff Garzik 
4388c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
43894633778bSHannes Reinecke 	ata_dev_dbg(dev, "init dev params \n");
4390c6fd2807SJeff Garzik 
4391c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4392c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4393c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4394c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4395c6fd2807SJeff Garzik 	tf.nsect = sectors;
4396c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4397c6fd2807SJeff Garzik 
43982b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
439918b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
440018b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
440118b2466cSAlan Cox 	   drive reported working geometry */
4402efcef265SSergey Shtylyov 	if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
440318b2466cSAlan Cox 		err_mask = 0;
4404c6fd2807SJeff Garzik 
4405c6fd2807SJeff Garzik 	return err_mask;
4406c6fd2807SJeff Garzik }
4407c6fd2807SJeff Garzik 
4408c6fd2807SJeff Garzik /**
44095895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4410c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4411c6fd2807SJeff Garzik  *
4412c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4413c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4414c6fd2807SJeff Garzik  *	supplied PACKET command.
4415c6fd2807SJeff Garzik  *
4416c6fd2807SJeff Garzik  *	LOCKING:
4417cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4418c6fd2807SJeff Garzik  *
4419c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4420c6fd2807SJeff Garzik  *               nonzero otherwise
4421c6fd2807SJeff Garzik  */
44225895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4423c6fd2807SJeff Garzik {
4424c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4425c6fd2807SJeff Garzik 
4426b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4427b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4428b9a4197eSTejun Heo 	 */
44296a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
44306a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
44316f23a31dSAlbert Lee 		return 1;
44326f23a31dSAlbert Lee 
4433c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4434b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4435c6fd2807SJeff Garzik 
4436b9a4197eSTejun Heo 	return 0;
4437c6fd2807SJeff Garzik }
4438b9a4197eSTejun Heo 
4439c6fd2807SJeff Garzik /**
444031cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
444131cc23b3STejun Heo  *	@qc: ATA command in question
444231cc23b3STejun Heo  *
444331cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
444431cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
444531cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
444631cc23b3STejun Heo  *	whether a new command @qc can be issued.
444731cc23b3STejun Heo  *
444831cc23b3STejun Heo  *	LOCKING:
444931cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
445031cc23b3STejun Heo  *
445131cc23b3STejun Heo  *	RETURNS:
445231cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
445331cc23b3STejun Heo  */
445431cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
445531cc23b3STejun Heo {
445631cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
445731cc23b3STejun Heo 
4458179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
445931cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
446031cc23b3STejun Heo 			return 0;
446131cc23b3STejun Heo 	} else {
446231cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
446331cc23b3STejun Heo 			return 0;
446431cc23b3STejun Heo 	}
446531cc23b3STejun Heo 
446631cc23b3STejun Heo 	return ATA_DEFER_LINK;
446731cc23b3STejun Heo }
4468a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_qc_defer);
446931cc23b3STejun Heo 
447095364f36SJiri Slaby enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
447195364f36SJiri Slaby {
447295364f36SJiri Slaby 	return AC_ERR_OK;
447395364f36SJiri Slaby }
4474a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
4475c6fd2807SJeff Garzik 
4476c6fd2807SJeff Garzik /**
4477c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4478c6fd2807SJeff Garzik  *	@qc: Command to be associated
4479c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4480c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4481c6fd2807SJeff Garzik  *
4482c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4483c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4484c6fd2807SJeff Garzik  *	elements.
4485c6fd2807SJeff Garzik  *
4486c6fd2807SJeff Garzik  *	LOCKING:
4487cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4488c6fd2807SJeff Garzik  */
4489c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4490c6fd2807SJeff Garzik 		 unsigned int n_elem)
4491c6fd2807SJeff Garzik {
4492ff2aeb1eSTejun Heo 	qc->sg = sg;
4493c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4494ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4495ff2aeb1eSTejun Heo }
4496ff2aeb1eSTejun Heo 
44972874d5eeSGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
44982874d5eeSGeert Uytterhoeven 
44992874d5eeSGeert Uytterhoeven /**
45002874d5eeSGeert Uytterhoeven  *	ata_sg_clean - Unmap DMA memory associated with command
45012874d5eeSGeert Uytterhoeven  *	@qc: Command containing DMA memory to be released
45022874d5eeSGeert Uytterhoeven  *
45032874d5eeSGeert Uytterhoeven  *	Unmap all mapped DMA memory associated with this command.
45042874d5eeSGeert Uytterhoeven  *
45052874d5eeSGeert Uytterhoeven  *	LOCKING:
45062874d5eeSGeert Uytterhoeven  *	spin_lock_irqsave(host lock)
45072874d5eeSGeert Uytterhoeven  */
4508af27e01cSJason Yan static void ata_sg_clean(struct ata_queued_cmd *qc)
45092874d5eeSGeert Uytterhoeven {
45102874d5eeSGeert Uytterhoeven 	struct ata_port *ap = qc->ap;
45112874d5eeSGeert Uytterhoeven 	struct scatterlist *sg = qc->sg;
45122874d5eeSGeert Uytterhoeven 	int dir = qc->dma_dir;
45132874d5eeSGeert Uytterhoeven 
45142874d5eeSGeert Uytterhoeven 	WARN_ON_ONCE(sg == NULL);
45152874d5eeSGeert Uytterhoeven 
45162874d5eeSGeert Uytterhoeven 	if (qc->n_elem)
45172874d5eeSGeert Uytterhoeven 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
45182874d5eeSGeert Uytterhoeven 
45192874d5eeSGeert Uytterhoeven 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
45202874d5eeSGeert Uytterhoeven 	qc->sg = NULL;
45212874d5eeSGeert Uytterhoeven }
45222874d5eeSGeert Uytterhoeven 
4523c6fd2807SJeff Garzik /**
4524c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4525c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4526c6fd2807SJeff Garzik  *
4527c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4528c6fd2807SJeff Garzik  *
4529c6fd2807SJeff Garzik  *	LOCKING:
4530cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4531c6fd2807SJeff Garzik  *
4532c6fd2807SJeff Garzik  *	RETURNS:
4533c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4534c6fd2807SJeff Garzik  *
4535c6fd2807SJeff Garzik  */
4536c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4537c6fd2807SJeff Garzik {
4538c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4539dde20207SJames Bottomley 	unsigned int n_elem;
4540c6fd2807SJeff Garzik 
4541dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4542dde20207SJames Bottomley 	if (n_elem < 1)
4543c6fd2807SJeff Garzik 		return -1;
4544c6fd2807SJeff Garzik 
45455825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4546dde20207SJames Bottomley 	qc->n_elem = n_elem;
4547f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4548c6fd2807SJeff Garzik 
4549c6fd2807SJeff Garzik 	return 0;
4550c6fd2807SJeff Garzik }
4551c6fd2807SJeff Garzik 
45522874d5eeSGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
45532874d5eeSGeert Uytterhoeven 
45542874d5eeSGeert Uytterhoeven static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
45552874d5eeSGeert Uytterhoeven static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
45562874d5eeSGeert Uytterhoeven 
45572874d5eeSGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
45582874d5eeSGeert Uytterhoeven 
4559c6fd2807SJeff Garzik /**
4560c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4561c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4562c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4563c6fd2807SJeff Garzik  *
4564c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4565c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4566c6fd2807SJeff Garzik  *	vice-versa.
4567c6fd2807SJeff Garzik  *
4568c6fd2807SJeff Garzik  *	LOCKING:
4569c6fd2807SJeff Garzik  *	Inherited from caller.
4570c6fd2807SJeff Garzik  */
4571c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4572c6fd2807SJeff Garzik {
4573c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4574c6fd2807SJeff Garzik 	unsigned int i;
4575c6fd2807SJeff Garzik 
4576c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4577c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4578c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4579c6fd2807SJeff Garzik }
4580c6fd2807SJeff Garzik 
4581c6fd2807SJeff Garzik /**
45828a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
45838a8bc223STejun Heo  *	@qc: Command to complete
45848a8bc223STejun Heo  *
45858a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
45868a8bc223STejun Heo  *	in case something prevents using it.
45878a8bc223STejun Heo  *
45888a8bc223STejun Heo  *	LOCKING:
45898a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
45908a8bc223STejun Heo  */
45918a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
45928a8bc223STejun Heo {
45938a8bc223STejun Heo 	qc->flags = 0;
45944f1a22eeSJohn Garry 	if (ata_tag_valid(qc->tag))
45958a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
45968a8bc223STejun Heo }
45978a8bc223STejun Heo 
4598c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4599c6fd2807SJeff Garzik {
4600a1104016SJulia Lawall 	struct ata_port *ap;
4601a1104016SJulia Lawall 	struct ata_link *link;
4602c6fd2807SJeff Garzik 
4603efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4604efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4605a1104016SJulia Lawall 	ap = qc->ap;
4606a1104016SJulia Lawall 	link = qc->dev->link;
4607c6fd2807SJeff Garzik 
4608c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4609c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4610c6fd2807SJeff Garzik 
4611c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4612179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
46134e5b6260SJens Axboe 		link->sactive &= ~(1 << qc->hw_tag);
4614da917d69STejun Heo 		if (!link->sactive)
4615da917d69STejun Heo 			ap->nr_active_links--;
4616da917d69STejun Heo 	} else {
46179af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4618da917d69STejun Heo 		ap->nr_active_links--;
4619da917d69STejun Heo 	}
4620da917d69STejun Heo 
4621da917d69STejun Heo 	/* clear exclusive status */
4622da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4623da917d69STejun Heo 		     ap->excl_link == link))
4624da917d69STejun Heo 		ap->excl_link = NULL;
4625c6fd2807SJeff Garzik 
4626c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4627c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4628c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4629c6fd2807SJeff Garzik 	 */
4630c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4631e3ed8939SJens Axboe 	ap->qc_active &= ~(1ULL << qc->tag);
4632c6fd2807SJeff Garzik 
4633c6fd2807SJeff Garzik 	/* call completion callback */
4634c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4635c6fd2807SJeff Garzik }
4636c6fd2807SJeff Garzik 
463739599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
463839599a53STejun Heo {
463939599a53STejun Heo 	struct ata_port *ap = qc->ap;
464039599a53STejun Heo 
464139599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
464222183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
464339599a53STejun Heo }
464439599a53STejun Heo 
464500115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
464600115e0fSTejun Heo {
464700115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
464800115e0fSTejun Heo 
4649eb0effdfSChristoph Hellwig 	if (!ata_is_data(qc->tf.protocol))
465000115e0fSTejun Heo 		return;
465100115e0fSTejun Heo 
465200115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
465300115e0fSTejun Heo 		return;
465400115e0fSTejun Heo 
465500115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
465600115e0fSTejun Heo }
465700115e0fSTejun Heo 
4658c6fd2807SJeff Garzik /**
4659c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4660c6fd2807SJeff Garzik  *	@qc: Command to complete
4661c6fd2807SJeff Garzik  *
46621aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
46631aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
46641aadf5c3STejun Heo  *
46651aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
46661aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
46671aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
46681aadf5c3STejun Heo  *	properly update IRQ expect state.
4669c6fd2807SJeff Garzik  *
4670c6fd2807SJeff Garzik  *	LOCKING:
4671cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4672c6fd2807SJeff Garzik  */
4673c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4674c6fd2807SJeff Garzik {
4675c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4676c6fd2807SJeff Garzik 
4677eb25cb99SStephan Linz 	/* Trigger the LED (if available) */
4678d1ed7c55SLinus Walleij 	ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
4679eb25cb99SStephan Linz 
4680c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4681c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4682c6fd2807SJeff Garzik 	 *
4683c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4684c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4685c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4686c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4687c6fd2807SJeff Garzik 	 *
4688c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4689c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4690c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4691c6fd2807SJeff Garzik 	 * taken care of.
4692c6fd2807SJeff Garzik 	 */
4693c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
46944dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
46954dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
46964dbfa39bSTejun Heo 
4697c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4698c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4699c6fd2807SJeff Garzik 
4700f08dc1acSTejun Heo 		/*
4701f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
4702f08dc1acSTejun Heo 		 * and always with the result TF filled.
4703f08dc1acSTejun Heo 		 */
4704f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
470539599a53STejun Heo 			fill_result_tf(qc);
4706255c03d1SHannes Reinecke 			trace_ata_qc_complete_internal(qc);
4707f4b31db9STejun Heo 			__ata_qc_complete(qc);
4708c6fd2807SJeff Garzik 			return;
4709c6fd2807SJeff Garzik 		}
4710c6fd2807SJeff Garzik 
4711f08dc1acSTejun Heo 		/*
4712f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
4713f08dc1acSTejun Heo 		 * summon EH.
4714f08dc1acSTejun Heo 		 */
4715f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4716f08dc1acSTejun Heo 			fill_result_tf(qc);
4717255c03d1SHannes Reinecke 			trace_ata_qc_complete_failed(qc);
4718f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
4719f08dc1acSTejun Heo 			return;
4720f08dc1acSTejun Heo 		}
4721f08dc1acSTejun Heo 
47224dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
47234dc738edSTejun Heo 
4724c6fd2807SJeff Garzik 		/* read result TF if requested */
4725c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
472639599a53STejun Heo 			fill_result_tf(qc);
4727c6fd2807SJeff Garzik 
4728255c03d1SHannes Reinecke 		trace_ata_qc_complete_done(qc);
47294dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
47304dbfa39bSTejun Heo 		 * completion.
47314dbfa39bSTejun Heo 		 */
47324dbfa39bSTejun Heo 		switch (qc->tf.command) {
47334dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
47344dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
47350c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_WC_OFF &&
47360c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_ON &&
47370c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_OFF)
47384dbfa39bSTejun Heo 				break;
4739df561f66SGustavo A. R. Silva 			fallthrough;
47404dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
47414dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
47424dbfa39bSTejun Heo 			/* revalidate device */
47434dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
47444dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
47454dbfa39bSTejun Heo 			break;
4746054a5fbaSTejun Heo 
4747054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4748054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4749054a5fbaSTejun Heo 			break;
47504dbfa39bSTejun Heo 		}
47514dbfa39bSTejun Heo 
475200115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
475300115e0fSTejun Heo 			ata_verify_xfer(qc);
475400115e0fSTejun Heo 
4755c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4756c6fd2807SJeff Garzik 	} else {
4757c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4758c6fd2807SJeff Garzik 			return;
4759c6fd2807SJeff Garzik 
4760c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4761c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
476239599a53STejun Heo 			fill_result_tf(qc);
4763c6fd2807SJeff Garzik 
4764c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4765c6fd2807SJeff Garzik 	}
4766c6fd2807SJeff Garzik }
4767a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_qc_complete);
4768c6fd2807SJeff Garzik 
4769c6fd2807SJeff Garzik /**
47708385d756SSascha Hauer  *	ata_qc_get_active - get bitmask of active qcs
47718385d756SSascha Hauer  *	@ap: port in question
47728385d756SSascha Hauer  *
47738385d756SSascha Hauer  *	LOCKING:
47748385d756SSascha Hauer  *	spin_lock_irqsave(host lock)
47758385d756SSascha Hauer  *
47768385d756SSascha Hauer  *	RETURNS:
47778385d756SSascha Hauer  *	Bitmask of active qcs
47788385d756SSascha Hauer  */
47798385d756SSascha Hauer u64 ata_qc_get_active(struct ata_port *ap)
47808385d756SSascha Hauer {
47818385d756SSascha Hauer 	u64 qc_active = ap->qc_active;
47828385d756SSascha Hauer 
47838385d756SSascha Hauer 	/* ATA_TAG_INTERNAL is sent to hw as tag 0 */
47848385d756SSascha Hauer 	if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
47858385d756SSascha Hauer 		qc_active |= (1 << 0);
47868385d756SSascha Hauer 		qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
47878385d756SSascha Hauer 	}
47888385d756SSascha Hauer 
47898385d756SSascha Hauer 	return qc_active;
47908385d756SSascha Hauer }
47918385d756SSascha Hauer EXPORT_SYMBOL_GPL(ata_qc_get_active);
47928385d756SSascha Hauer 
47938385d756SSascha Hauer /**
4794c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4795c6fd2807SJeff Garzik  *	@qc: command to issue to device
4796c6fd2807SJeff Garzik  *
4797c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4798c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4799c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
4800c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
4801c6fd2807SJeff Garzik  *
4802c6fd2807SJeff Garzik  *	LOCKING:
4803cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4804c6fd2807SJeff Garzik  */
4805c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
4806c6fd2807SJeff Garzik {
4807c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
48089af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4809405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
4810c6fd2807SJeff Garzik 
4811c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
4812c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
4813c6fd2807SJeff Garzik 	 * request ATAPI sense.
4814c6fd2807SJeff Garzik 	 */
4815efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
4816c6fd2807SJeff Garzik 
48171973a023STejun Heo 	if (ata_is_ncq(prot)) {
48184e5b6260SJens Axboe 		WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
4819da917d69STejun Heo 
4820da917d69STejun Heo 		if (!link->sactive)
4821da917d69STejun Heo 			ap->nr_active_links++;
48224e5b6260SJens Axboe 		link->sactive |= 1 << qc->hw_tag;
4823c6fd2807SJeff Garzik 	} else {
4824efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
4825da917d69STejun Heo 
4826da917d69STejun Heo 		ap->nr_active_links++;
48279af5c9c9STejun Heo 		link->active_tag = qc->tag;
4828c6fd2807SJeff Garzik 	}
4829c6fd2807SJeff Garzik 
4830c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
4831e3ed8939SJens Axboe 	ap->qc_active |= 1ULL << qc->tag;
4832c6fd2807SJeff Garzik 
483360f5d6efSTejun Heo 	/*
483460f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
4835f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
4836f92a2636STejun Heo 	 */
48379173e5e8SEric Biggers 	if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
483860f5d6efSTejun Heo 		goto sys_err;
4839f92a2636STejun Heo 
4840405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
4841f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
4842c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
484360f5d6efSTejun Heo 			goto sys_err;
4844c6fd2807SJeff Garzik 
4845cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
4846054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
4847cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
4848054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4849054a5fbaSTejun Heo 		ata_link_abort(link);
4850054a5fbaSTejun Heo 		return;
4851054a5fbaSTejun Heo 	}
4852054a5fbaSTejun Heo 
4853fc914faaSHannes Reinecke 	trace_ata_qc_prep(qc);
485495364f36SJiri Slaby 	qc->err_mask |= ap->ops->qc_prep(qc);
485595364f36SJiri Slaby 	if (unlikely(qc->err_mask))
485695364f36SJiri Slaby 		goto err;
4857255c03d1SHannes Reinecke 	trace_ata_qc_issue(qc);
4858c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
4859c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
4860c6fd2807SJeff Garzik 		goto err;
4861c6fd2807SJeff Garzik 	return;
4862c6fd2807SJeff Garzik 
486360f5d6efSTejun Heo sys_err:
4864c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
4865c6fd2807SJeff Garzik err:
4866c6fd2807SJeff Garzik 	ata_qc_complete(qc);
4867c6fd2807SJeff Garzik }
4868c6fd2807SJeff Garzik 
4869c6fd2807SJeff Garzik /**
4870b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
4871936fd732STejun Heo  *	@link: ATA link to test
4872c6fd2807SJeff Garzik  *
4873936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
4874936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
4875936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4876c6fd2807SJeff Garzik  *
4877c6fd2807SJeff Garzik  *	LOCKING:
4878c6fd2807SJeff Garzik  *	None.
4879c6fd2807SJeff Garzik  *
4880c6fd2807SJeff Garzik  *	RETURNS:
4881b5b3fa38STejun Heo  *	True if the port online status is available and online.
4882c6fd2807SJeff Garzik  */
4883b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
4884c6fd2807SJeff Garzik {
4885c6fd2807SJeff Garzik 	u32 sstatus;
4886c6fd2807SJeff Garzik 
4887936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
48889913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
4889b5b3fa38STejun Heo 		return true;
4890b5b3fa38STejun Heo 	return false;
4891c6fd2807SJeff Garzik }
4892c6fd2807SJeff Garzik 
4893c6fd2807SJeff Garzik /**
4894b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
4895936fd732STejun Heo  *	@link: ATA link to test
4896c6fd2807SJeff Garzik  *
4897936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
4898936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
4899936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4900c6fd2807SJeff Garzik  *
4901c6fd2807SJeff Garzik  *	LOCKING:
4902c6fd2807SJeff Garzik  *	None.
4903c6fd2807SJeff Garzik  *
4904c6fd2807SJeff Garzik  *	RETURNS:
4905b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
4906c6fd2807SJeff Garzik  */
4907b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
4908c6fd2807SJeff Garzik {
4909c6fd2807SJeff Garzik 	u32 sstatus;
4910c6fd2807SJeff Garzik 
4911936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
49129913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
4913b5b3fa38STejun Heo 		return true;
4914b5b3fa38STejun Heo 	return false;
4915c6fd2807SJeff Garzik }
4916c6fd2807SJeff Garzik 
4917b1c72916STejun Heo /**
4918b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
4919b1c72916STejun Heo  *	@link: ATA link to test
4920b1c72916STejun Heo  *
4921b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
4922b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
4923b1c72916STejun Heo  *	there's a slave link, this function should only be called on
4924b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
4925b1c72916STejun Heo  *	online.
4926b1c72916STejun Heo  *
4927b1c72916STejun Heo  *	LOCKING:
4928b1c72916STejun Heo  *	None.
4929b1c72916STejun Heo  *
4930b1c72916STejun Heo  *	RETURNS:
4931b1c72916STejun Heo  *	True if the port online status is available and online.
4932b1c72916STejun Heo  */
4933b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
4934b1c72916STejun Heo {
4935b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
4936b1c72916STejun Heo 
4937b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
4938b1c72916STejun Heo 
4939b1c72916STejun Heo 	return ata_phys_link_online(link) ||
4940b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
4941b1c72916STejun Heo }
4942a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_online);
4943b1c72916STejun Heo 
4944b1c72916STejun Heo /**
4945b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
4946b1c72916STejun Heo  *	@link: ATA link to test
4947b1c72916STejun Heo  *
4948b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
4949b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
4950b1c72916STejun Heo  *	there's a slave link, this function should only be called on
4951b1c72916STejun Heo  *	the master link and will return true if both M/S links are
4952b1c72916STejun Heo  *	offline.
4953b1c72916STejun Heo  *
4954b1c72916STejun Heo  *	LOCKING:
4955b1c72916STejun Heo  *	None.
4956b1c72916STejun Heo  *
4957b1c72916STejun Heo  *	RETURNS:
4958b1c72916STejun Heo  *	True if the port offline status is available and offline.
4959b1c72916STejun Heo  */
4960b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
4961b1c72916STejun Heo {
4962b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
4963b1c72916STejun Heo 
4964b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
4965b1c72916STejun Heo 
4966b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
4967b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
4968b1c72916STejun Heo }
4969a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_offline);
4970b1c72916STejun Heo 
49716ffa01d8STejun Heo #ifdef CONFIG_PM
4972bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
4973cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
4974bc6e7c4bSDan Williams 				bool async)
4975c6fd2807SJeff Garzik {
4976e3667ebfSTejun Heo 	struct ata_link *link;
49775ef41082SLin Ming 	unsigned long flags;
4978c6fd2807SJeff Garzik 
4979c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
4980c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
4981c6fd2807SJeff Garzik 	 */
4982c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
4983c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
4984c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4985c6fd2807SJeff Garzik 	}
4986c6fd2807SJeff Garzik 
4987c6fd2807SJeff Garzik 	/* request PM ops to EH */
4988c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
4989c6fd2807SJeff Garzik 
4990c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
4991c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
49921eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
4993e3667ebfSTejun Heo 		link->eh_info.action |= action;
4994e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
4995e3667ebfSTejun Heo 	}
4996c6fd2807SJeff Garzik 
4997c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
4998c6fd2807SJeff Garzik 
4999c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5000c6fd2807SJeff Garzik 
50012fcbdcb4SDan Williams 	if (!async) {
5002c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5003c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5004c6fd2807SJeff Garzik 	}
5005c6fd2807SJeff Garzik }
5006c6fd2807SJeff Garzik 
500733574d68SLin Ming /*
5008bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
5009bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
5010bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
501133574d68SLin Ming  *
501233574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
501333574d68SLin Ming  */
5014bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5015bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
5016bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
5017bc6e7c4bSDan Williams 
5018bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5019bc6e7c4bSDan Williams {
5020bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
50215ef41082SLin Ming }
50225ef41082SLin Ming 
5023bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5024bc6e7c4bSDan Williams {
5025bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5026bc6e7c4bSDan Williams }
5027bc6e7c4bSDan Williams 
5028bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
50292fcbdcb4SDan Williams {
50302fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
50312fcbdcb4SDan Williams 
50325ef41082SLin Ming 	if (pm_runtime_suspended(dev))
50335ef41082SLin Ming 		return 0;
50345ef41082SLin Ming 
5035bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5036f5e6d0d0SAaron Lu 	return 0;
503733574d68SLin Ming }
503833574d68SLin Ming 
5039bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
50402fcbdcb4SDan Williams {
50412fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
50422fcbdcb4SDan Williams 
5043bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5044bc6e7c4bSDan Williams 		return 0;
5045bc6e7c4bSDan Williams 
5046bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5047bc6e7c4bSDan Williams 	return 0;
50482fcbdcb4SDan Williams }
50492fcbdcb4SDan Williams 
5050bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5051e90b1e5aSLin Ming {
5052bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5053bc6e7c4bSDan Williams 	return 0;
5054bc6e7c4bSDan Williams }
5055e90b1e5aSLin Ming 
5056bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5057bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5058bc6e7c4bSDan Williams 
5059bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5060bc6e7c4bSDan Williams {
5061bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5062bc6e7c4bSDan Williams }
5063bc6e7c4bSDan Williams 
5064bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5065bc6e7c4bSDan Williams {
5066bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5067bc6e7c4bSDan Williams }
5068bc6e7c4bSDan Williams 
5069bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5070bc6e7c4bSDan Williams {
5071200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5072e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5073e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5074e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5075bc6e7c4bSDan Williams 	return 0;
5076e90b1e5aSLin Ming }
5077e90b1e5aSLin Ming 
50787e15e9beSAaron Lu /*
50797e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
50807e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
50817e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
50827e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
50837e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
50847e15e9beSAaron Lu  * ODD is attached to the port.
50857e15e9beSAaron Lu  */
50869ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
50879ee4f393SLin Ming {
50887e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
50897e15e9beSAaron Lu 	struct ata_link *link;
50907e15e9beSAaron Lu 	struct ata_device *adev;
50917e15e9beSAaron Lu 
50927e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
50937e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
50947e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
50957e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
50967e15e9beSAaron Lu 				return -EBUSY;
50977e15e9beSAaron Lu 	}
50987e15e9beSAaron Lu 
509945f0a85cSRafael J. Wysocki 	return 0;
51009ee4f393SLin Ming }
51019ee4f393SLin Ming 
5102a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5103a7ff60dbSAaron Lu {
5104bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5105bc6e7c4bSDan Williams 	return 0;
5106a7ff60dbSAaron Lu }
5107a7ff60dbSAaron Lu 
5108a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5109a7ff60dbSAaron Lu {
5110bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5111bc6e7c4bSDan Williams 	return 0;
5112a7ff60dbSAaron Lu }
5113a7ff60dbSAaron Lu 
51145ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5115bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5116bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5117bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5118bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5119bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5120bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
51219ee4f393SLin Ming 
5122a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5123a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
51249ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
51255ef41082SLin Ming };
51265ef41082SLin Ming 
51272fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
51282fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
51292fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
51302fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
51312fcbdcb4SDan Williams  */
5132bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
51332fcbdcb4SDan Williams {
5134bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
51352fcbdcb4SDan Williams }
5136bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
51372fcbdcb4SDan Williams 
5138bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
51392fcbdcb4SDan Williams {
5140bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
51412fcbdcb4SDan Williams }
5142bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
51432fcbdcb4SDan Williams 
5144c6fd2807SJeff Garzik /**
5145cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5146cca3974eSJeff Garzik  *	@host: host to suspend
5147c6fd2807SJeff Garzik  *	@mesg: PM message
5148c6fd2807SJeff Garzik  *
51495ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5150c6fd2807SJeff Garzik  */
5151ec87cf37SSergey Shtylyov void ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5152c6fd2807SJeff Garzik {
515372ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
5154c6fd2807SJeff Garzik }
5155a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_suspend);
5156c6fd2807SJeff Garzik 
5157c6fd2807SJeff Garzik /**
5158cca3974eSJeff Garzik  *	ata_host_resume - resume host
5159cca3974eSJeff Garzik  *	@host: host to resume
5160c6fd2807SJeff Garzik  *
51615ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5162c6fd2807SJeff Garzik  */
5163cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5164c6fd2807SJeff Garzik {
516572ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5166c6fd2807SJeff Garzik }
5167a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_resume);
51686ffa01d8STejun Heo #endif
5169c6fd2807SJeff Garzik 
51708df82c13SBhumika Goyal const struct device_type ata_port_type = {
51715ef41082SLin Ming 	.name = "ata_port",
51725ef41082SLin Ming #ifdef CONFIG_PM
51735ef41082SLin Ming 	.pm = &ata_port_pm_ops,
51745ef41082SLin Ming #endif
51755ef41082SLin Ming };
51765ef41082SLin Ming 
5177c6fd2807SJeff Garzik /**
5178c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5179c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5180c6fd2807SJeff Garzik  *
5181c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5182c6fd2807SJeff Garzik  *
5183c6fd2807SJeff Garzik  *	LOCKING:
5184c6fd2807SJeff Garzik  *	Inherited from caller.
5185c6fd2807SJeff Garzik  */
5186c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5187c6fd2807SJeff Garzik {
5188b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
51899af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5190c6fd2807SJeff Garzik 	unsigned long flags;
5191c6fd2807SJeff Garzik 
5192b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
51939af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
51949af5c9c9STejun Heo 	link->sata_spd = 0;
5195c6fd2807SJeff Garzik 
5196c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5197c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5198cca3974eSJeff Garzik 	 * host lock.
5199c6fd2807SJeff Garzik 	 */
5200c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5201c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
52023dcc323fSTejun Heo 	dev->horkage = 0;
5203c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5204c6fd2807SJeff Garzik 
520599cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
520699cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5207c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5208c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5209c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5210c6fd2807SJeff Garzik }
5211c6fd2807SJeff Garzik 
5212c6fd2807SJeff Garzik /**
52134fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
52144fb37a25STejun Heo  *	@ap: ATA port link is attached to
52154fb37a25STejun Heo  *	@link: Link structure to initialize
52168989805dSTejun Heo  *	@pmp: Port multiplier port number
52174fb37a25STejun Heo  *
52184fb37a25STejun Heo  *	Initialize @link.
52194fb37a25STejun Heo  *
52204fb37a25STejun Heo  *	LOCKING:
52214fb37a25STejun Heo  *	Kernel thread context (may sleep)
52224fb37a25STejun Heo  */
5223fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
52244fb37a25STejun Heo {
52254fb37a25STejun Heo 	int i;
52264fb37a25STejun Heo 
52274fb37a25STejun Heo 	/* clear everything except for devices */
5228d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5229d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
52304fb37a25STejun Heo 
52314fb37a25STejun Heo 	link->ap = ap;
52328989805dSTejun Heo 	link->pmp = pmp;
52334fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
52344fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
52354fb37a25STejun Heo 
52364fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
52374fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
52384fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
52394fb37a25STejun Heo 
52404fb37a25STejun Heo 		dev->link = link;
52414fb37a25STejun Heo 		dev->devno = dev - link->device;
5242110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5243110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5244110f66d2STejun Heo #endif
52454fb37a25STejun Heo 		ata_dev_init(dev);
52464fb37a25STejun Heo 	}
52474fb37a25STejun Heo }
52484fb37a25STejun Heo 
52494fb37a25STejun Heo /**
52504fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
52514fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
52524fb37a25STejun Heo  *
5253a31a6997SMauro Carvalho Chehab  *	Initialize ``link->[hw_]sata_spd_limit`` to the currently
52544fb37a25STejun Heo  *	configured value.
52554fb37a25STejun Heo  *
52564fb37a25STejun Heo  *	LOCKING:
52574fb37a25STejun Heo  *	Kernel thread context (may sleep).
52584fb37a25STejun Heo  *
52594fb37a25STejun Heo  *	RETURNS:
52604fb37a25STejun Heo  *	0 on success, -errno on failure.
52614fb37a25STejun Heo  */
5262fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
52634fb37a25STejun Heo {
526433267325STejun Heo 	u8 spd;
52654fb37a25STejun Heo 	int rc;
52664fb37a25STejun Heo 
5267d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
52684fb37a25STejun Heo 	if (rc)
52694fb37a25STejun Heo 		return rc;
52704fb37a25STejun Heo 
5271d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
52724fb37a25STejun Heo 	if (spd)
52734fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
52744fb37a25STejun Heo 
527505944bdfSTejun Heo 	ata_force_link_limits(link);
527633267325STejun Heo 
52774fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
52784fb37a25STejun Heo 
52794fb37a25STejun Heo 	return 0;
52804fb37a25STejun Heo }
52814fb37a25STejun Heo 
52824fb37a25STejun Heo /**
5283f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5284f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5285c6fd2807SJeff Garzik  *
5286f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5287f3187195STejun Heo  *
5288f3187195STejun Heo  *	RETURNS:
5289f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5290c6fd2807SJeff Garzik  *
5291c6fd2807SJeff Garzik  *	LOCKING:
5292f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5293c6fd2807SJeff Garzik  */
5294f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5295c6fd2807SJeff Garzik {
5296f3187195STejun Heo 	struct ata_port *ap;
5297c6fd2807SJeff Garzik 
5298f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5299f3187195STejun Heo 	if (!ap)
5300f3187195STejun Heo 		return NULL;
5301f3187195STejun Heo 
53027b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5303cca3974eSJeff Garzik 	ap->lock = &host->lock;
5304f3187195STejun Heo 	ap->print_id = -1;
5305e628dc99SDavid Milburn 	ap->local_port_no = -1;
5306cca3974eSJeff Garzik 	ap->host = host;
5307f3187195STejun Heo 	ap->dev = host->dev;
5308c6fd2807SJeff Garzik 
5309ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
531065f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
531165f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5312c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5313c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
531445fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
5315b93ab338SKees Cook 	timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5316b93ab338SKees Cook 		    TIMER_DEFERRABLE);
5317c6fd2807SJeff Garzik 
5318c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5319c6fd2807SJeff Garzik 
53208989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5321c6fd2807SJeff Garzik 
5322c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5323c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5324c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5325c6fd2807SJeff Garzik #endif
5326270390e1STejun Heo 	ata_sff_port_init(ap);
5327270390e1STejun Heo 
5328c6fd2807SJeff Garzik 	return ap;
5329c6fd2807SJeff Garzik }
5330c6fd2807SJeff Garzik 
53312623c7a5STaras Kondratiuk static void ata_devres_release(struct device *gendev, void *res)
5332f0d36efdSTejun Heo {
5333f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5334f0d36efdSTejun Heo 	int i;
5335f0d36efdSTejun Heo 
5336f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5337f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5338f0d36efdSTejun Heo 
5339ecef7253STejun Heo 		if (!ap)
5340ecef7253STejun Heo 			continue;
5341ecef7253STejun Heo 
53424911487aSTejun Heo 		if (ap->scsi_host)
53431aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
53441aa506e4STejun Heo 
53452623c7a5STaras Kondratiuk 	}
53462623c7a5STaras Kondratiuk 
53472623c7a5STaras Kondratiuk 	dev_set_drvdata(gendev, NULL);
53482623c7a5STaras Kondratiuk 	ata_host_put(host);
53492623c7a5STaras Kondratiuk }
53502623c7a5STaras Kondratiuk 
53512623c7a5STaras Kondratiuk static void ata_host_release(struct kref *kref)
53522623c7a5STaras Kondratiuk {
53532623c7a5STaras Kondratiuk 	struct ata_host *host = container_of(kref, struct ata_host, kref);
53542623c7a5STaras Kondratiuk 	int i;
53552623c7a5STaras Kondratiuk 
53562623c7a5STaras Kondratiuk 	for (i = 0; i < host->n_ports; i++) {
53572623c7a5STaras Kondratiuk 		struct ata_port *ap = host->ports[i];
53582623c7a5STaras Kondratiuk 
5359633273a3STejun Heo 		kfree(ap->pmp_link);
5360b1c72916STejun Heo 		kfree(ap->slave_link);
53614911487aSTejun Heo 		kfree(ap);
53621aa506e4STejun Heo 		host->ports[i] = NULL;
53631aa506e4STejun Heo 	}
53642623c7a5STaras Kondratiuk 	kfree(host);
53652623c7a5STaras Kondratiuk }
53661aa506e4STejun Heo 
53672623c7a5STaras Kondratiuk void ata_host_get(struct ata_host *host)
53682623c7a5STaras Kondratiuk {
53692623c7a5STaras Kondratiuk 	kref_get(&host->kref);
53702623c7a5STaras Kondratiuk }
53712623c7a5STaras Kondratiuk 
53722623c7a5STaras Kondratiuk void ata_host_put(struct ata_host *host)
53732623c7a5STaras Kondratiuk {
53742623c7a5STaras Kondratiuk 	kref_put(&host->kref, ata_host_release);
5375f0d36efdSTejun Heo }
5376a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_put);
5377f0d36efdSTejun Heo 
5378c6fd2807SJeff Garzik /**
5379f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5380f3187195STejun Heo  *	@dev: generic device this host is associated with
5381f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5382f3187195STejun Heo  *
5383f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5384f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5385f3187195STejun Heo  *	attaches it using ata_host_register().
5386f3187195STejun Heo  *
5387f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5388f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5389f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5390f3187195STejun Heo  *	ports will be automatically freed on registration.
5391f3187195STejun Heo  *
5392f3187195STejun Heo  *	RETURNS:
5393f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5394f3187195STejun Heo  *
5395f3187195STejun Heo  *	LOCKING:
5396f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5397f3187195STejun Heo  */
5398f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5399f3187195STejun Heo {
5400f3187195STejun Heo 	struct ata_host *host;
5401f3187195STejun Heo 	size_t sz;
5402f3187195STejun Heo 	int i;
54032623c7a5STaras Kondratiuk 	void *dr;
5404f3187195STejun Heo 
5405f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5406f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
54072623c7a5STaras Kondratiuk 	host = kzalloc(sz, GFP_KERNEL);
5408f3187195STejun Heo 	if (!host)
54092623c7a5STaras Kondratiuk 		return NULL;
54102623c7a5STaras Kondratiuk 
5411f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5412dafd6c49SColin Ian King 		goto err_free;
5413f3187195STejun Heo 
54142623c7a5STaras Kondratiuk 	dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
54152623c7a5STaras Kondratiuk 	if (!dr)
5416f3187195STejun Heo 		goto err_out;
5417f3187195STejun Heo 
54182623c7a5STaras Kondratiuk 	devres_add(dev, dr);
5419f3187195STejun Heo 	dev_set_drvdata(dev, host);
5420f3187195STejun Heo 
5421f3187195STejun Heo 	spin_lock_init(&host->lock);
5422c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5423f3187195STejun Heo 	host->dev = dev;
5424f3187195STejun Heo 	host->n_ports = max_ports;
54252623c7a5STaras Kondratiuk 	kref_init(&host->kref);
5426f3187195STejun Heo 
5427f3187195STejun Heo 	/* allocate ports bound to this host */
5428f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5429f3187195STejun Heo 		struct ata_port *ap;
5430f3187195STejun Heo 
5431f3187195STejun Heo 		ap = ata_port_alloc(host);
5432f3187195STejun Heo 		if (!ap)
5433f3187195STejun Heo 			goto err_out;
5434f3187195STejun Heo 
5435f3187195STejun Heo 		ap->port_no = i;
5436f3187195STejun Heo 		host->ports[i] = ap;
5437f3187195STejun Heo 	}
5438f3187195STejun Heo 
5439f3187195STejun Heo 	devres_remove_group(dev, NULL);
5440f3187195STejun Heo 	return host;
5441f3187195STejun Heo 
5442f3187195STejun Heo  err_out:
5443f3187195STejun Heo 	devres_release_group(dev, NULL);
5444dafd6c49SColin Ian King  err_free:
5445dafd6c49SColin Ian King 	kfree(host);
5446f3187195STejun Heo 	return NULL;
5447f3187195STejun Heo }
5448a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc);
5449f3187195STejun Heo 
5450f3187195STejun Heo /**
5451f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5452f5cda257STejun Heo  *	@dev: generic device this host is associated with
5453f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5454f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5455f5cda257STejun Heo  *
5456f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5457f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5458f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5459f5cda257STejun Heo  *
5460f5cda257STejun Heo  *	RETURNS:
5461f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5462f5cda257STejun Heo  *
5463f5cda257STejun Heo  *	LOCKING:
5464f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5465f5cda257STejun Heo  */
5466f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5467f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5468f5cda257STejun Heo 				      int n_ports)
5469f5cda257STejun Heo {
5470bf476fe2SSergey Shtylyov 	const struct ata_port_info *pi = &ata_dummy_port_info;
5471f5cda257STejun Heo 	struct ata_host *host;
5472f5cda257STejun Heo 	int i, j;
5473f5cda257STejun Heo 
5474f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5475f5cda257STejun Heo 	if (!host)
5476f5cda257STejun Heo 		return NULL;
5477f5cda257STejun Heo 
5478bf476fe2SSergey Shtylyov 	for (i = 0, j = 0; i < host->n_ports; i++) {
5479f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5480f5cda257STejun Heo 
5481f5cda257STejun Heo 		if (ppi[j])
5482f5cda257STejun Heo 			pi = ppi[j++];
5483f5cda257STejun Heo 
5484f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5485f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5486f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5487f5cda257STejun Heo 		ap->flags |= pi->flags;
54880c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5489f5cda257STejun Heo 		ap->ops = pi->port_ops;
5490f5cda257STejun Heo 
5491f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5492f5cda257STejun Heo 			host->ops = pi->port_ops;
5493f5cda257STejun Heo 	}
5494f5cda257STejun Heo 
5495f5cda257STejun Heo 	return host;
5496f5cda257STejun Heo }
5497a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
5498f5cda257STejun Heo 
549932ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
550032ebbc0cSTejun Heo {
550132ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
550232ebbc0cSTejun Heo 	int i;
550332ebbc0cSTejun Heo 
550432ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
550532ebbc0cSTejun Heo 
550632ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
550732ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
550832ebbc0cSTejun Heo 
550932ebbc0cSTejun Heo 		if (ap->ops->port_stop)
551032ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
551132ebbc0cSTejun Heo 	}
551232ebbc0cSTejun Heo 
551332ebbc0cSTejun Heo 	if (host->ops->host_stop)
551432ebbc0cSTejun Heo 		host->ops->host_stop(host);
551532ebbc0cSTejun Heo }
551632ebbc0cSTejun Heo 
5517f5cda257STejun Heo /**
5518029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5519029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5520029cfd6bSTejun Heo  *
5521029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5522029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5523029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5524029cfd6bSTejun Heo  *	inheritance chain.
5525029cfd6bSTejun Heo  *
5526029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5527029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5528029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5529029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5530029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5531029cfd6bSTejun Heo  *
5532029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5533029cfd6bSTejun Heo  *
5534029cfd6bSTejun Heo  *	LOCKING:
5535029cfd6bSTejun Heo  *	None.
5536029cfd6bSTejun Heo  */
5537029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5538029cfd6bSTejun Heo {
55392da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5540029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5541029cfd6bSTejun Heo 	void **begin = (void **)ops;
5542029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5543029cfd6bSTejun Heo 	void **pp;
5544029cfd6bSTejun Heo 
5545029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5546029cfd6bSTejun Heo 		return;
5547029cfd6bSTejun Heo 
5548029cfd6bSTejun Heo 	spin_lock(&lock);
5549029cfd6bSTejun Heo 
5550029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5551029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5552029cfd6bSTejun Heo 
5553029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5554029cfd6bSTejun Heo 			if (!*pp)
5555029cfd6bSTejun Heo 				*pp = *inherit;
5556029cfd6bSTejun Heo 	}
5557029cfd6bSTejun Heo 
5558029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5559029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5560029cfd6bSTejun Heo 			*pp = NULL;
5561029cfd6bSTejun Heo 
5562029cfd6bSTejun Heo 	ops->inherits = NULL;
5563029cfd6bSTejun Heo 
5564029cfd6bSTejun Heo 	spin_unlock(&lock);
5565029cfd6bSTejun Heo }
5566029cfd6bSTejun Heo 
5567029cfd6bSTejun Heo /**
5568ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5569ecef7253STejun Heo  *	@host: ATA host to start ports for
5570ecef7253STejun Heo  *
5571ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5572ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5573ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5574e0af10acSDiego Viola  *	once.  If host->ops is not initialized yet, it is set to the
5575f3187195STejun Heo  *	first non-dummy port ops.
5576ecef7253STejun Heo  *
5577ecef7253STejun Heo  *	LOCKING:
5578ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5579ecef7253STejun Heo  *
5580ecef7253STejun Heo  *	RETURNS:
5581ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5582ecef7253STejun Heo  */
5583ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5584ecef7253STejun Heo {
558532ebbc0cSTejun Heo 	int have_stop = 0;
558632ebbc0cSTejun Heo 	void *start_dr = NULL;
5587ecef7253STejun Heo 	int i, rc;
5588ecef7253STejun Heo 
5589ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5590ecef7253STejun Heo 		return 0;
5591ecef7253STejun Heo 
5592029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5593029cfd6bSTejun Heo 
5594ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5595ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5596ecef7253STejun Heo 
5597029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5598029cfd6bSTejun Heo 
5599f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5600f3187195STejun Heo 			host->ops = ap->ops;
5601f3187195STejun Heo 
560232ebbc0cSTejun Heo 		if (ap->ops->port_stop)
560332ebbc0cSTejun Heo 			have_stop = 1;
560432ebbc0cSTejun Heo 	}
560532ebbc0cSTejun Heo 
5606355a8031SDamien Le Moal 	if (host->ops && host->ops->host_stop)
560732ebbc0cSTejun Heo 		have_stop = 1;
560832ebbc0cSTejun Heo 
560932ebbc0cSTejun Heo 	if (have_stop) {
561032ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
561132ebbc0cSTejun Heo 		if (!start_dr)
561232ebbc0cSTejun Heo 			return -ENOMEM;
561332ebbc0cSTejun Heo 	}
561432ebbc0cSTejun Heo 
561532ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
561632ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
561732ebbc0cSTejun Heo 
5618ecef7253STejun Heo 		if (ap->ops->port_start) {
5619ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5620ecef7253STejun Heo 			if (rc) {
56210f9fe9b7SAlan Cox 				if (rc != -ENODEV)
5622a44fec1fSJoe Perches 					dev_err(host->dev,
5623a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
5624a44fec1fSJoe Perches 						i, rc);
5625ecef7253STejun Heo 				goto err_out;
5626ecef7253STejun Heo 			}
5627ecef7253STejun Heo 		}
5628ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5629ecef7253STejun Heo 	}
5630ecef7253STejun Heo 
563132ebbc0cSTejun Heo 	if (start_dr)
563232ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5633ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5634ecef7253STejun Heo 	return 0;
5635ecef7253STejun Heo 
5636ecef7253STejun Heo  err_out:
5637ecef7253STejun Heo 	while (--i >= 0) {
5638ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5639ecef7253STejun Heo 
5640ecef7253STejun Heo 		if (ap->ops->port_stop)
5641ecef7253STejun Heo 			ap->ops->port_stop(ap);
5642ecef7253STejun Heo 	}
564332ebbc0cSTejun Heo 	devres_free(start_dr);
5644ecef7253STejun Heo 	return rc;
5645ecef7253STejun Heo }
5646a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_start);
5647ecef7253STejun Heo 
5648ecef7253STejun Heo /**
564994bd5719SMauro Carvalho Chehab  *	ata_host_init - Initialize a host struct for sas (ipr, libsas)
5650cca3974eSJeff Garzik  *	@host:	host to initialize
5651cca3974eSJeff Garzik  *	@dev:	device host is attached to
5652c6fd2807SJeff Garzik  *	@ops:	port_ops
5653c6fd2807SJeff Garzik  *
5654c6fd2807SJeff Garzik  */
5655cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
56568d8e7d13SDan Williams 		   struct ata_port_operations *ops)
5657c6fd2807SJeff Garzik {
5658cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5659c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
566069278f79SJens Axboe 	host->n_tags = ATA_MAX_QUEUE;
5661cca3974eSJeff Garzik 	host->dev = dev;
5662cca3974eSJeff Garzik 	host->ops = ops;
56632fa4a326SJason Yan 	kref_init(&host->kref);
5664c6fd2807SJeff Garzik }
5665a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_init);
5666c6fd2807SJeff Garzik 
56679508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
566879318057SArjan van de Ven {
566979318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
567079318057SArjan van de Ven 	unsigned long flags;
567179318057SArjan van de Ven 
567279318057SArjan van de Ven 	/* kick EH for boot probing */
567379318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
567479318057SArjan van de Ven 
567579318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
56766b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
567779318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
567879318057SArjan van de Ven 
567979318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
568079318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
568179318057SArjan van de Ven 	ata_port_schedule_eh(ap);
568279318057SArjan van de Ven 
568379318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
56849508a66fSDan Williams }
568579318057SArjan van de Ven 
56869508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
56879508a66fSDan Williams {
56889508a66fSDan Williams 	int rc = 0;
56899508a66fSDan Williams 
56909508a66fSDan Williams 	if (ap->ops->error_handler) {
56919508a66fSDan Williams 		__ata_port_probe(ap);
569279318057SArjan van de Ven 		ata_port_wait_eh(ap);
569379318057SArjan van de Ven 	} else {
569479318057SArjan van de Ven 		rc = ata_bus_probe(ap);
5695238c9cf9SJames Bottomley 	}
5696238c9cf9SJames Bottomley 	return rc;
5697238c9cf9SJames Bottomley }
569879318057SArjan van de Ven 
5699238c9cf9SJames Bottomley 
5700238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
5701238c9cf9SJames Bottomley {
5702238c9cf9SJames Bottomley 	struct ata_port *ap = data;
5703238c9cf9SJames Bottomley 
5704238c9cf9SJames Bottomley 	/*
5705238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
5706238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
5707238c9cf9SJames Bottomley 	 * before going further.
5708238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
5709238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
571079318057SArjan van de Ven 	 */
5711238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5712238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
5713238c9cf9SJames Bottomley 
5714238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
5715f29d3b23SArjan van de Ven 
5716f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
5717f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
5718f29d3b23SArjan van de Ven 
5719f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
572079318057SArjan van de Ven }
5721238c9cf9SJames Bottomley 
5722c6fd2807SJeff Garzik /**
5723f3187195STejun Heo  *	ata_host_register - register initialized ATA host
5724f3187195STejun Heo  *	@host: ATA host to register
5725f3187195STejun Heo  *	@sht: template for SCSI host
5726c6fd2807SJeff Garzik  *
5727f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
5728f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
5729f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
5730f3187195STejun Heo  *	probe registered devices.
5731c6fd2807SJeff Garzik  *
5732c6fd2807SJeff Garzik  *	LOCKING:
5733f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5734c6fd2807SJeff Garzik  *
5735c6fd2807SJeff Garzik  *	RETURNS:
5736f3187195STejun Heo  *	0 on success, -errno otherwise.
5737c6fd2807SJeff Garzik  */
5738f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
5739c6fd2807SJeff Garzik {
5740f3187195STejun Heo 	int i, rc;
5741c6fd2807SJeff Garzik 
574269278f79SJens Axboe 	host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
57431871ee13SKevin Hao 
5744f3187195STejun Heo 	/* host must have been started */
5745f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
5746a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
5747f3187195STejun Heo 		WARN_ON(1);
5748f3187195STejun Heo 		return -EINVAL;
574902f076aaSAlan Cox 	}
5750f0d36efdSTejun Heo 
5751f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
5752f3187195STejun Heo 	 * determine the exact number of ports to allocate at
5753f3187195STejun Heo 	 * allocation time.
5754f3187195STejun Heo 	 */
5755f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
5756f3187195STejun Heo 		kfree(host->ports[i]);
5757f0d36efdSTejun Heo 
5758f3187195STejun Heo 	/* give ports names and add SCSI hosts */
5759e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
576085d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
5761e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
5762e628dc99SDavid Milburn 	}
5763d9027470SGwendal Grignou 
5764d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
5765d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
5766d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
5767d9027470SGwendal Grignou 		if (rc) {
5768d9027470SGwendal Grignou 			goto err_tadd;
5769d9027470SGwendal Grignou 		}
5770d9027470SGwendal Grignou 	}
5771d9027470SGwendal Grignou 
5772f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
5773ecef7253STejun Heo 	if (rc)
5774d9027470SGwendal Grignou 		goto err_tadd;
5775ecef7253STejun Heo 
5776f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
5777cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5778cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5779f0a6d77bSSergey Shtylyov 		unsigned int xfer_mask;
5780f3187195STejun Heo 
5781f3187195STejun Heo 		/* set SATA cable type if still unset */
5782f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5783f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
5784c6fd2807SJeff Garzik 
5785c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
57864fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
5787b1c72916STejun Heo 		if (ap->slave_link)
5788b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
5789c6fd2807SJeff Garzik 
5790cbcdd875STejun Heo 		/* print per-port info to dmesg */
5791f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5792f3187195STejun Heo 					      ap->udma_mask);
5793f3187195STejun Heo 
5794abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
5795a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
5796a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5797f3187195STejun Heo 				      ata_mode_string(xfer_mask),
5798cbcdd875STejun Heo 				      ap->link.eh_info.desc);
5799abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
5800abf6e8edSTejun Heo 		} else
5801a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
5802c6fd2807SJeff Garzik 	}
5803c6fd2807SJeff Garzik 
5804f6005354SVegard Nossum 	/* perform each probe asynchronously */
5805f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5806f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
5807b5292111SKai-Heng Feng 		ap->cookie = async_schedule(async_port_probe, ap);
5808c6fd2807SJeff Garzik 	}
5809c6fd2807SJeff Garzik 
5810f3187195STejun Heo 	return 0;
5811d9027470SGwendal Grignou 
5812d9027470SGwendal Grignou  err_tadd:
5813d9027470SGwendal Grignou 	while (--i >= 0) {
5814d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
5815d9027470SGwendal Grignou 	}
5816d9027470SGwendal Grignou 	return rc;
5817d9027470SGwendal Grignou 
5818f3187195STejun Heo }
5819a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_register);
5820f3187195STejun Heo 
5821f3187195STejun Heo /**
5822f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
5823f5cda257STejun Heo  *	@host: target ATA host
5824f5cda257STejun Heo  *	@irq: IRQ to request
5825f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
5826f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
5827f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
5828f5cda257STejun Heo  *
5829f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
5830f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
5831c9b5560aSMasanari Iida  *	request IRQ and register it.  This helper takes necessary
5832f5cda257STejun Heo  *	arguments and performs the three steps in one go.
5833f5cda257STejun Heo  *
58343d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
58353d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
58363d46b2e2SPaul Mundt  *	should be NULL.
58373d46b2e2SPaul Mundt  *
5838f5cda257STejun Heo  *	LOCKING:
5839f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5840f5cda257STejun Heo  *
5841f5cda257STejun Heo  *	RETURNS:
5842f5cda257STejun Heo  *	0 on success, -errno otherwise.
5843f5cda257STejun Heo  */
5844f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
5845f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
5846f5cda257STejun Heo 		      struct scsi_host_template *sht)
5847f5cda257STejun Heo {
5848cbcdd875STejun Heo 	int i, rc;
58497e22c002SHeiner Kallweit 	char *irq_desc;
5850f5cda257STejun Heo 
5851f5cda257STejun Heo 	rc = ata_host_start(host);
5852f5cda257STejun Heo 	if (rc)
5853f5cda257STejun Heo 		return rc;
5854f5cda257STejun Heo 
58553d46b2e2SPaul Mundt 	/* Special case for polling mode */
58563d46b2e2SPaul Mundt 	if (!irq) {
58573d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
58583d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
58593d46b2e2SPaul Mundt 	}
58603d46b2e2SPaul Mundt 
58617e22c002SHeiner Kallweit 	irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
58627e22c002SHeiner Kallweit 				  dev_driver_string(host->dev),
58637e22c002SHeiner Kallweit 				  dev_name(host->dev));
58647e22c002SHeiner Kallweit 	if (!irq_desc)
58657e22c002SHeiner Kallweit 		return -ENOMEM;
58667e22c002SHeiner Kallweit 
5867f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
58687e22c002SHeiner Kallweit 			      irq_desc, host);
5869f5cda257STejun Heo 	if (rc)
5870f5cda257STejun Heo 		return rc;
5871f5cda257STejun Heo 
5872cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
5873cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
58744031826bSTejun Heo 
5875f5cda257STejun Heo 	rc = ata_host_register(host, sht);
5876f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
5877f5cda257STejun Heo 	if (rc)
5878f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
5879f5cda257STejun Heo 
5880f5cda257STejun Heo 	return rc;
5881f5cda257STejun Heo }
5882a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_activate);
5883f5cda257STejun Heo 
5884f5cda257STejun Heo /**
5885c9b5560aSMasanari Iida  *	ata_port_detach - Detach ATA port in preparation of device removal
5886c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
5887c6fd2807SJeff Garzik  *
5888c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
5889c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
5890c6fd2807SJeff Garzik  *	be quiescent on return from this function.
5891c6fd2807SJeff Garzik  *
5892c6fd2807SJeff Garzik  *	LOCKING:
5893c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5894c6fd2807SJeff Garzik  */
5895741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
5896c6fd2807SJeff Garzik {
5897c6fd2807SJeff Garzik 	unsigned long flags;
5898a6f9bf4dSLevente Kurusa 	struct ata_link *link;
5899a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
5900c6fd2807SJeff Garzik 
5901c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
5902c6fd2807SJeff Garzik 		goto skip_eh;
5903c6fd2807SJeff Garzik 
5904c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
5905c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5906c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
5907ece180d1STejun Heo 	ata_port_schedule_eh(ap);
5908c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5909c6fd2807SJeff Garzik 
5910ece180d1STejun Heo 	/* wait till EH commits suicide */
5911c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
5912c6fd2807SJeff Garzik 
5913ece180d1STejun Heo 	/* it better be dead now */
5914ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
5915c6fd2807SJeff Garzik 
5916afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
5917c6fd2807SJeff Garzik 
5918c6fd2807SJeff Garzik  skip_eh:
5919a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
5920a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
5921a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
5922a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
5923a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
5924a6f9bf4dSLevente Kurusa 		}
5925a6f9bf4dSLevente Kurusa 	}
5926d9027470SGwendal Grignou 	if (ap->pmp_link) {
5927d9027470SGwendal Grignou 		int i;
5928d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
5929d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
5930d9027470SGwendal Grignou 	}
5931c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
5932cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
5933c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
5934c6fd2807SJeff Garzik }
5935c6fd2807SJeff Garzik 
5936c6fd2807SJeff Garzik /**
59370529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
59380529c159STejun Heo  *	@host: Host to detach
59390529c159STejun Heo  *
59400529c159STejun Heo  *	Detach all ports of @host.
59410529c159STejun Heo  *
59420529c159STejun Heo  *	LOCKING:
59430529c159STejun Heo  *	Kernel thread context (may sleep).
59440529c159STejun Heo  */
59450529c159STejun Heo void ata_host_detach(struct ata_host *host)
59460529c159STejun Heo {
59470529c159STejun Heo 	int i;
59480529c159STejun Heo 
5949b5292111SKai-Heng Feng 	for (i = 0; i < host->n_ports; i++) {
5950130f4cafSJohn Garry 		/* Ensure ata_port probe has completed */
5951b5292111SKai-Heng Feng 		async_synchronize_cookie(host->ports[i]->cookie + 1);
59520529c159STejun Heo 		ata_port_detach(host->ports[i]);
5953b5292111SKai-Heng Feng 	}
5954562f0c2dSTejun Heo 
5955562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
5956562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
59570529c159STejun Heo }
5958a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_detach);
59590529c159STejun Heo 
5960c6fd2807SJeff Garzik #ifdef CONFIG_PCI
5961c6fd2807SJeff Garzik 
5962c6fd2807SJeff Garzik /**
5963c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
5964c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
5965c6fd2807SJeff Garzik  *
5966b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
5967b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
5968b878ca5dSTejun Heo  *	release is handled via devres.
5969c6fd2807SJeff Garzik  *
5970c6fd2807SJeff Garzik  *	LOCKING:
5971c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
5972c6fd2807SJeff Garzik  */
5973c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
5974c6fd2807SJeff Garzik {
597504a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
5976c6fd2807SJeff Garzik 
5977f0d36efdSTejun Heo 	ata_host_detach(host);
5978c6fd2807SJeff Garzik }
5979a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5980c6fd2807SJeff Garzik 
598110a663a1SPrabhakar Kushwaha void ata_pci_shutdown_one(struct pci_dev *pdev)
598210a663a1SPrabhakar Kushwaha {
598310a663a1SPrabhakar Kushwaha 	struct ata_host *host = pci_get_drvdata(pdev);
598410a663a1SPrabhakar Kushwaha 	int i;
598510a663a1SPrabhakar Kushwaha 
598610a663a1SPrabhakar Kushwaha 	for (i = 0; i < host->n_ports; i++) {
598710a663a1SPrabhakar Kushwaha 		struct ata_port *ap = host->ports[i];
598810a663a1SPrabhakar Kushwaha 
598910a663a1SPrabhakar Kushwaha 		ap->pflags |= ATA_PFLAG_FROZEN;
599010a663a1SPrabhakar Kushwaha 
599110a663a1SPrabhakar Kushwaha 		/* Disable port interrupts */
599210a663a1SPrabhakar Kushwaha 		if (ap->ops->freeze)
599310a663a1SPrabhakar Kushwaha 			ap->ops->freeze(ap);
599410a663a1SPrabhakar Kushwaha 
599510a663a1SPrabhakar Kushwaha 		/* Stop the port DMA engines */
599610a663a1SPrabhakar Kushwaha 		if (ap->ops->port_stop)
599710a663a1SPrabhakar Kushwaha 			ap->ops->port_stop(ap);
599810a663a1SPrabhakar Kushwaha 	}
599910a663a1SPrabhakar Kushwaha }
6000a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
600110a663a1SPrabhakar Kushwaha 
6002c6fd2807SJeff Garzik /* move to PCI subsystem */
6003c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6004c6fd2807SJeff Garzik {
6005c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6006c6fd2807SJeff Garzik 
6007c6fd2807SJeff Garzik 	switch (bits->width) {
6008c6fd2807SJeff Garzik 	case 1: {
6009c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6010c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6011c6fd2807SJeff Garzik 		tmp = tmp8;
6012c6fd2807SJeff Garzik 		break;
6013c6fd2807SJeff Garzik 	}
6014c6fd2807SJeff Garzik 	case 2: {
6015c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6016c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6017c6fd2807SJeff Garzik 		tmp = tmp16;
6018c6fd2807SJeff Garzik 		break;
6019c6fd2807SJeff Garzik 	}
6020c6fd2807SJeff Garzik 	case 4: {
6021c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6022c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6023c6fd2807SJeff Garzik 		tmp = tmp32;
6024c6fd2807SJeff Garzik 		break;
6025c6fd2807SJeff Garzik 	}
6026c6fd2807SJeff Garzik 
6027c6fd2807SJeff Garzik 	default:
6028c6fd2807SJeff Garzik 		return -EINVAL;
6029c6fd2807SJeff Garzik 	}
6030c6fd2807SJeff Garzik 
6031c6fd2807SJeff Garzik 	tmp &= bits->mask;
6032c6fd2807SJeff Garzik 
6033c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6034c6fd2807SJeff Garzik }
6035a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(pci_test_config_bits);
6036c6fd2807SJeff Garzik 
60376ffa01d8STejun Heo #ifdef CONFIG_PM
6038c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6039c6fd2807SJeff Garzik {
6040c6fd2807SJeff Garzik 	pci_save_state(pdev);
6041c6fd2807SJeff Garzik 	pci_disable_device(pdev);
60424c90d971STejun Heo 
60433a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6044c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6045c6fd2807SJeff Garzik }
6046a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6047c6fd2807SJeff Garzik 
6048553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6049c6fd2807SJeff Garzik {
6050553c4aa6STejun Heo 	int rc;
6051553c4aa6STejun Heo 
6052c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6053c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6054553c4aa6STejun Heo 
6055f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6056553c4aa6STejun Heo 	if (rc) {
6057a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6058553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6059553c4aa6STejun Heo 		return rc;
6060553c4aa6STejun Heo 	}
6061553c4aa6STejun Heo 
6062c6fd2807SJeff Garzik 	pci_set_master(pdev);
6063553c4aa6STejun Heo 	return 0;
6064c6fd2807SJeff Garzik }
6065a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6066c6fd2807SJeff Garzik 
6067c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6068c6fd2807SJeff Garzik {
606904a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6070c6fd2807SJeff Garzik 
6071ec87cf37SSergey Shtylyov 	ata_host_suspend(host, mesg);
6072c6fd2807SJeff Garzik 
6073c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6074c6fd2807SJeff Garzik 
6075c6fd2807SJeff Garzik 	return 0;
6076c6fd2807SJeff Garzik }
6077a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6078c6fd2807SJeff Garzik 
6079c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6080c6fd2807SJeff Garzik {
608104a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6082553c4aa6STejun Heo 	int rc;
6083c6fd2807SJeff Garzik 
6084553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6085553c4aa6STejun Heo 	if (rc == 0)
6086cca3974eSJeff Garzik 		ata_host_resume(host);
6087553c4aa6STejun Heo 	return rc;
6088c6fd2807SJeff Garzik }
6089a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_resume);
60906ffa01d8STejun Heo #endif /* CONFIG_PM */
6091c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6092c6fd2807SJeff Garzik 
6093b7db04d9SBrian Norris /**
6094b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6095b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6096b7db04d9SBrian Norris  *
6097b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6098b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6099b7db04d9SBrian Norris  *	release is handled via devres.
6100b7db04d9SBrian Norris  *
6101b7db04d9SBrian Norris  *	LOCKING:
6102b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6103b7db04d9SBrian Norris  */
6104b7db04d9SBrian Norris int ata_platform_remove_one(struct platform_device *pdev)
6105b7db04d9SBrian Norris {
6106b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6107b7db04d9SBrian Norris 
6108b7db04d9SBrian Norris 	ata_host_detach(host);
6109b7db04d9SBrian Norris 
6110b7db04d9SBrian Norris 	return 0;
6111b7db04d9SBrian Norris }
6112a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_platform_remove_one);
6113b7db04d9SBrian Norris 
6114bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
6115168af4afSDamien Le Moal 
6116168af4afSDamien Le Moal #define force_cbl(name, flag)				\
6117168af4afSDamien Le Moal 	{ #name,	.cbl		= (flag) }
6118168af4afSDamien Le Moal 
6119168af4afSDamien Le Moal #define force_spd_limit(spd, val)			\
6120168af4afSDamien Le Moal 	{ #spd,	.spd_limit		= (val) }
6121168af4afSDamien Le Moal 
6122168af4afSDamien Le Moal #define force_xfer(mode, shift)				\
6123168af4afSDamien Le Moal 	{ #mode,	.xfer_mask	= (1UL << (shift)) }
6124168af4afSDamien Le Moal 
61253af9ca4dSDamien Le Moal #define force_lflag_on(name, flags)			\
61263af9ca4dSDamien Le Moal 	{ #name,	.lflags_on	= (flags) }
61273af9ca4dSDamien Le Moal 
61283af9ca4dSDamien Le Moal #define force_lflag_onoff(name, flags)			\
61293af9ca4dSDamien Le Moal 	{ "no" #name,	.lflags_on	= (flags) },	\
61303af9ca4dSDamien Le Moal 	{ #name,	.lflags_off	= (flags) }
6131168af4afSDamien Le Moal 
6132168af4afSDamien Le Moal #define force_horkage_on(name, flag)			\
6133168af4afSDamien Le Moal 	{ #name,	.horkage_on	= (flag) }
6134168af4afSDamien Le Moal 
6135168af4afSDamien Le Moal #define force_horkage_onoff(name, flag)			\
6136168af4afSDamien Le Moal 	{ "no" #name,	.horkage_on	= (flag) },	\
6137168af4afSDamien Le Moal 	{ #name,	.horkage_off	= (flag) }
6138168af4afSDamien Le Moal 
6139168af4afSDamien Le Moal static const struct ata_force_param force_tbl[] __initconst = {
6140168af4afSDamien Le Moal 	force_cbl(40c,			ATA_CBL_PATA40),
6141168af4afSDamien Le Moal 	force_cbl(80c,			ATA_CBL_PATA80),
6142168af4afSDamien Le Moal 	force_cbl(short40c,		ATA_CBL_PATA40_SHORT),
6143168af4afSDamien Le Moal 	force_cbl(unk,			ATA_CBL_PATA_UNK),
6144168af4afSDamien Le Moal 	force_cbl(ign,			ATA_CBL_PATA_IGN),
6145168af4afSDamien Le Moal 	force_cbl(sata,			ATA_CBL_SATA),
6146168af4afSDamien Le Moal 
6147168af4afSDamien Le Moal 	force_spd_limit(1.5Gbps,	1),
6148168af4afSDamien Le Moal 	force_spd_limit(3.0Gbps,	2),
6149168af4afSDamien Le Moal 
6150168af4afSDamien Le Moal 	force_xfer(pio0,		ATA_SHIFT_PIO + 0),
6151168af4afSDamien Le Moal 	force_xfer(pio1,		ATA_SHIFT_PIO + 1),
6152168af4afSDamien Le Moal 	force_xfer(pio2,		ATA_SHIFT_PIO + 2),
6153168af4afSDamien Le Moal 	force_xfer(pio3,		ATA_SHIFT_PIO + 3),
6154168af4afSDamien Le Moal 	force_xfer(pio4,		ATA_SHIFT_PIO + 4),
6155168af4afSDamien Le Moal 	force_xfer(pio5,		ATA_SHIFT_PIO + 5),
6156168af4afSDamien Le Moal 	force_xfer(pio6,		ATA_SHIFT_PIO + 6),
6157168af4afSDamien Le Moal 	force_xfer(mwdma0,		ATA_SHIFT_MWDMA + 0),
6158168af4afSDamien Le Moal 	force_xfer(mwdma1,		ATA_SHIFT_MWDMA + 1),
6159168af4afSDamien Le Moal 	force_xfer(mwdma2,		ATA_SHIFT_MWDMA + 2),
6160168af4afSDamien Le Moal 	force_xfer(mwdma3,		ATA_SHIFT_MWDMA + 3),
6161168af4afSDamien Le Moal 	force_xfer(mwdma4,		ATA_SHIFT_MWDMA + 4),
6162168af4afSDamien Le Moal 	force_xfer(udma0,		ATA_SHIFT_UDMA + 0),
6163168af4afSDamien Le Moal 	force_xfer(udma16,		ATA_SHIFT_UDMA + 0),
6164168af4afSDamien Le Moal 	force_xfer(udma/16,		ATA_SHIFT_UDMA + 0),
6165168af4afSDamien Le Moal 	force_xfer(udma1,		ATA_SHIFT_UDMA + 1),
6166168af4afSDamien Le Moal 	force_xfer(udma25,		ATA_SHIFT_UDMA + 1),
6167168af4afSDamien Le Moal 	force_xfer(udma/25,		ATA_SHIFT_UDMA + 1),
6168168af4afSDamien Le Moal 	force_xfer(udma2,		ATA_SHIFT_UDMA + 2),
6169168af4afSDamien Le Moal 	force_xfer(udma33,		ATA_SHIFT_UDMA + 2),
6170168af4afSDamien Le Moal 	force_xfer(udma/33,		ATA_SHIFT_UDMA + 2),
6171168af4afSDamien Le Moal 	force_xfer(udma3,		ATA_SHIFT_UDMA + 3),
6172168af4afSDamien Le Moal 	force_xfer(udma44,		ATA_SHIFT_UDMA + 3),
6173168af4afSDamien Le Moal 	force_xfer(udma/44,		ATA_SHIFT_UDMA + 3),
6174168af4afSDamien Le Moal 	force_xfer(udma4,		ATA_SHIFT_UDMA + 4),
6175168af4afSDamien Le Moal 	force_xfer(udma66,		ATA_SHIFT_UDMA + 4),
6176168af4afSDamien Le Moal 	force_xfer(udma/66,		ATA_SHIFT_UDMA + 4),
6177168af4afSDamien Le Moal 	force_xfer(udma5,		ATA_SHIFT_UDMA + 5),
6178168af4afSDamien Le Moal 	force_xfer(udma100,		ATA_SHIFT_UDMA + 5),
6179168af4afSDamien Le Moal 	force_xfer(udma/100,		ATA_SHIFT_UDMA + 5),
6180168af4afSDamien Le Moal 	force_xfer(udma6,		ATA_SHIFT_UDMA + 6),
6181168af4afSDamien Le Moal 	force_xfer(udma133,		ATA_SHIFT_UDMA + 6),
6182168af4afSDamien Le Moal 	force_xfer(udma/133,		ATA_SHIFT_UDMA + 6),
6183168af4afSDamien Le Moal 	force_xfer(udma7,		ATA_SHIFT_UDMA + 7),
6184168af4afSDamien Le Moal 
61853af9ca4dSDamien Le Moal 	force_lflag_on(nohrst,		ATA_LFLAG_NO_HRST),
61863af9ca4dSDamien Le Moal 	force_lflag_on(nosrst,		ATA_LFLAG_NO_SRST),
61873af9ca4dSDamien Le Moal 	force_lflag_on(norst,		ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST),
61883af9ca4dSDamien Le Moal 	force_lflag_on(rstonce,		ATA_LFLAG_RST_ONCE),
61893af9ca4dSDamien Le Moal 	force_lflag_onoff(dbdelay,	ATA_LFLAG_NO_DEBOUNCE_DELAY),
6190168af4afSDamien Le Moal 
6191168af4afSDamien Le Moal 	force_horkage_onoff(ncq,	ATA_HORKAGE_NONCQ),
6192168af4afSDamien Le Moal 	force_horkage_onoff(ncqtrim,	ATA_HORKAGE_NO_NCQ_TRIM),
6193168af4afSDamien Le Moal 	force_horkage_onoff(ncqati,	ATA_HORKAGE_NO_NCQ_ON_ATI),
6194168af4afSDamien Le Moal 
61952c33bbdaSDamien Le Moal 	force_horkage_onoff(trim,	ATA_HORKAGE_NOTRIM),
61962c33bbdaSDamien Le Moal 	force_horkage_on(trim_zero,	ATA_HORKAGE_ZERO_AFTER_TRIM),
61972c33bbdaSDamien Le Moal 	force_horkage_on(max_trim_128m, ATA_HORKAGE_MAX_TRIM_128M),
61982c33bbdaSDamien Le Moal 
61992c33bbdaSDamien Le Moal 	force_horkage_onoff(dma,	ATA_HORKAGE_NODMA),
6200168af4afSDamien Le Moal 	force_horkage_on(atapi_dmadir,	ATA_HORKAGE_ATAPI_DMADIR),
62012c33bbdaSDamien Le Moal 	force_horkage_on(atapi_mod16_dma, ATA_HORKAGE_ATAPI_MOD16_DMA),
62022c33bbdaSDamien Le Moal 
62032c33bbdaSDamien Le Moal 	force_horkage_onoff(dmalog,	ATA_HORKAGE_NO_DMA_LOG),
62042c33bbdaSDamien Le Moal 	force_horkage_onoff(iddevlog,	ATA_HORKAGE_NO_ID_DEV_LOG),
62052c33bbdaSDamien Le Moal 	force_horkage_onoff(logdir,	ATA_HORKAGE_NO_LOG_DIR),
62062c33bbdaSDamien Le Moal 
62072c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_128,	ATA_HORKAGE_MAX_SEC_128),
62082c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_1024,	ATA_HORKAGE_MAX_SEC_1024),
62092c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_lba48,	ATA_HORKAGE_MAX_SEC_LBA48),
62102c33bbdaSDamien Le Moal 
62112c33bbdaSDamien Le Moal 	force_horkage_onoff(lpm,	ATA_HORKAGE_NOLPM),
62122c33bbdaSDamien Le Moal 	force_horkage_onoff(setxfer,	ATA_HORKAGE_NOSETXFER),
62132c33bbdaSDamien Le Moal 	force_horkage_on(dump_id,	ATA_HORKAGE_DUMP_ID),
62142c33bbdaSDamien Le Moal 
62152c33bbdaSDamien Le Moal 	force_horkage_on(disable,	ATA_HORKAGE_DISABLE),
6216168af4afSDamien Le Moal };
6217168af4afSDamien Le Moal 
621833267325STejun Heo static int __init ata_parse_force_one(char **cur,
621933267325STejun Heo 				      struct ata_force_ent *force_ent,
622033267325STejun Heo 				      const char **reason)
622133267325STejun Heo {
622233267325STejun Heo 	char *start = *cur, *p = *cur;
622333267325STejun Heo 	char *id, *val, *endp;
622433267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
622533267325STejun Heo 	int nr_matches = 0, i;
622633267325STejun Heo 
622733267325STejun Heo 	/* find where this param ends and update *cur */
622833267325STejun Heo 	while (*p != '\0' && *p != ',')
622933267325STejun Heo 		p++;
623033267325STejun Heo 
623133267325STejun Heo 	if (*p == '\0')
623233267325STejun Heo 		*cur = p;
623333267325STejun Heo 	else
623433267325STejun Heo 		*cur = p + 1;
623533267325STejun Heo 
623633267325STejun Heo 	*p = '\0';
623733267325STejun Heo 
623833267325STejun Heo 	/* parse */
623933267325STejun Heo 	p = strchr(start, ':');
624033267325STejun Heo 	if (!p) {
624133267325STejun Heo 		val = strstrip(start);
624233267325STejun Heo 		goto parse_val;
624333267325STejun Heo 	}
624433267325STejun Heo 	*p = '\0';
624533267325STejun Heo 
624633267325STejun Heo 	id = strstrip(start);
624733267325STejun Heo 	val = strstrip(p + 1);
624833267325STejun Heo 
624933267325STejun Heo 	/* parse id */
625033267325STejun Heo 	p = strchr(id, '.');
625133267325STejun Heo 	if (p) {
625233267325STejun Heo 		*p++ = '\0';
625333267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
625433267325STejun Heo 		if (p == endp || *endp != '\0') {
625533267325STejun Heo 			*reason = "invalid device";
625633267325STejun Heo 			return -EINVAL;
625733267325STejun Heo 		}
625833267325STejun Heo 	}
625933267325STejun Heo 
626033267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
6261f7cf69aeSTejun Heo 	if (id == endp || *endp != '\0') {
626233267325STejun Heo 		*reason = "invalid port/link";
626333267325STejun Heo 		return -EINVAL;
626433267325STejun Heo 	}
626533267325STejun Heo 
626633267325STejun Heo  parse_val:
626733267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
626833267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
626933267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
627033267325STejun Heo 
627133267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
627233267325STejun Heo 			continue;
627333267325STejun Heo 
627433267325STejun Heo 		nr_matches++;
627533267325STejun Heo 		match_fp = fp;
627633267325STejun Heo 
627733267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
627833267325STejun Heo 			nr_matches = 1;
627933267325STejun Heo 			break;
628033267325STejun Heo 		}
628133267325STejun Heo 	}
628233267325STejun Heo 
628333267325STejun Heo 	if (!nr_matches) {
628433267325STejun Heo 		*reason = "unknown value";
628533267325STejun Heo 		return -EINVAL;
628633267325STejun Heo 	}
628733267325STejun Heo 	if (nr_matches > 1) {
62889de55351SArvind Yadav 		*reason = "ambiguous value";
628933267325STejun Heo 		return -EINVAL;
629033267325STejun Heo 	}
629133267325STejun Heo 
629233267325STejun Heo 	force_ent->param = *match_fp;
629333267325STejun Heo 
629433267325STejun Heo 	return 0;
629533267325STejun Heo }
629633267325STejun Heo 
629733267325STejun Heo static void __init ata_parse_force_param(void)
629833267325STejun Heo {
629933267325STejun Heo 	int idx = 0, size = 1;
630033267325STejun Heo 	int last_port = -1, last_device = -1;
630133267325STejun Heo 	char *p, *cur, *next;
630233267325STejun Heo 
6303168af4afSDamien Le Moal 	/* Calculate maximum number of params and allocate ata_force_tbl */
630433267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
630533267325STejun Heo 		if (*p == ',')
630633267325STejun Heo 			size++;
630733267325STejun Heo 
63086396bb22SKees Cook 	ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
630933267325STejun Heo 	if (!ata_force_tbl) {
631033267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
631133267325STejun Heo 		       "libata.force ignored\n");
631233267325STejun Heo 		return;
631333267325STejun Heo 	}
631433267325STejun Heo 
631533267325STejun Heo 	/* parse and populate the table */
631633267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
631733267325STejun Heo 		const char *reason = "";
631833267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
631933267325STejun Heo 
632033267325STejun Heo 		next = cur;
632133267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
632233267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
632333267325STejun Heo 			       "parameter \"%s\" (%s)\n",
632433267325STejun Heo 			       cur, reason);
632533267325STejun Heo 			continue;
632633267325STejun Heo 		}
632733267325STejun Heo 
632833267325STejun Heo 		if (te.port == -1) {
632933267325STejun Heo 			te.port = last_port;
633033267325STejun Heo 			te.device = last_device;
633133267325STejun Heo 		}
633233267325STejun Heo 
633333267325STejun Heo 		ata_force_tbl[idx++] = te;
633433267325STejun Heo 
633533267325STejun Heo 		last_port = te.port;
633633267325STejun Heo 		last_device = te.device;
633733267325STejun Heo 	}
633833267325STejun Heo 
633933267325STejun Heo 	ata_force_tbl_size = idx;
634033267325STejun Heo }
6341c6fd2807SJeff Garzik 
6342bf89b0bfSBartlomiej Zolnierkiewicz static void ata_free_force_param(void)
6343bf89b0bfSBartlomiej Zolnierkiewicz {
6344bf89b0bfSBartlomiej Zolnierkiewicz 	kfree(ata_force_tbl);
6345bf89b0bfSBartlomiej Zolnierkiewicz }
6346bf89b0bfSBartlomiej Zolnierkiewicz #else
6347bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_parse_force_param(void) { }
6348bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_free_force_param(void) { }
6349bf89b0bfSBartlomiej Zolnierkiewicz #endif
6350bf89b0bfSBartlomiej Zolnierkiewicz 
6351c6fd2807SJeff Garzik static int __init ata_init(void)
6352c6fd2807SJeff Garzik {
6353d9027470SGwendal Grignou 	int rc;
6354270390e1STejun Heo 
635533267325STejun Heo 	ata_parse_force_param();
635633267325STejun Heo 
6357270390e1STejun Heo 	rc = ata_sff_init();
6358ad72cf98STejun Heo 	if (rc) {
6359bf89b0bfSBartlomiej Zolnierkiewicz 		ata_free_force_param();
6360ad72cf98STejun Heo 		return rc;
6361ad72cf98STejun Heo 	}
6362c6fd2807SJeff Garzik 
6363d9027470SGwendal Grignou 	libata_transport_init();
6364d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6365d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6366d9027470SGwendal Grignou 		ata_sff_exit();
6367d9027470SGwendal Grignou 		rc = -ENOMEM;
6368d9027470SGwendal Grignou 		goto err_out;
6369d9027470SGwendal Grignou 	}
6370d9027470SGwendal Grignou 
6371c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6372c6fd2807SJeff Garzik 	return 0;
6373d9027470SGwendal Grignou 
6374d9027470SGwendal Grignou err_out:
6375d9027470SGwendal Grignou 	return rc;
6376c6fd2807SJeff Garzik }
6377c6fd2807SJeff Garzik 
6378c6fd2807SJeff Garzik static void __exit ata_exit(void)
6379c6fd2807SJeff Garzik {
6380d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6381d9027470SGwendal Grignou 	libata_transport_exit();
6382270390e1STejun Heo 	ata_sff_exit();
6383bf89b0bfSBartlomiej Zolnierkiewicz 	ata_free_force_param();
6384c6fd2807SJeff Garzik }
6385c6fd2807SJeff Garzik 
6386a4625085SBrian King subsys_initcall(ata_init);
6387c6fd2807SJeff Garzik module_exit(ata_exit);
6388c6fd2807SJeff Garzik 
63899990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6390c6fd2807SJeff Garzik 
6391c6fd2807SJeff Garzik int ata_ratelimit(void)
6392c6fd2807SJeff Garzik {
63939990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6394c6fd2807SJeff Garzik }
6395a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ratelimit);
6396c6fd2807SJeff Garzik 
6397c0c362b6STejun Heo /**
6398c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6399c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6400c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6401c0c362b6STejun Heo  *
6402c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6403c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6404c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6405c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6406c0c362b6STejun Heo  *	sleeping.
6407c0c362b6STejun Heo  *
6408c0c362b6STejun Heo  *	LOCKING:
6409c0c362b6STejun Heo  *	Might sleep.
6410c0c362b6STejun Heo  */
641197750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
641297750cebSTejun Heo {
6413c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6414c0c362b6STejun Heo 
6415c0c362b6STejun Heo 	if (owns_eh)
6416c0c362b6STejun Heo 		ata_eh_release(ap);
6417c0c362b6STejun Heo 
6418848c3920SAnil Veliyankara Madam 	if (msecs < 20) {
6419848c3920SAnil Veliyankara Madam 		unsigned long usecs = msecs * USEC_PER_MSEC;
6420848c3920SAnil Veliyankara Madam 		usleep_range(usecs, usecs + 50);
6421848c3920SAnil Veliyankara Madam 	} else {
642297750cebSTejun Heo 		msleep(msecs);
6423848c3920SAnil Veliyankara Madam 	}
6424c0c362b6STejun Heo 
6425c0c362b6STejun Heo 	if (owns_eh)
6426c0c362b6STejun Heo 		ata_eh_acquire(ap);
642797750cebSTejun Heo }
6428a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_msleep);
642997750cebSTejun Heo 
6430c6fd2807SJeff Garzik /**
6431c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
643297750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6433c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6434c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6435c6fd2807SJeff Garzik  *	@val: Wait condition
6436341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6437341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6438c6fd2807SJeff Garzik  *
6439c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6440c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6441c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6442c6fd2807SJeff Garzik  *
6443c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6444c6fd2807SJeff Garzik  *
6445c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6446c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6447c6fd2807SJeff Garzik  *
6448c6fd2807SJeff Garzik  *	LOCKING:
6449c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6450c6fd2807SJeff Garzik  *
6451c6fd2807SJeff Garzik  *	RETURNS:
6452c6fd2807SJeff Garzik  *	The final register value.
6453c6fd2807SJeff Garzik  */
645497750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6455341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6456c6fd2807SJeff Garzik {
6457341c2c95STejun Heo 	unsigned long deadline;
6458c6fd2807SJeff Garzik 	u32 tmp;
6459c6fd2807SJeff Garzik 
6460c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6461c6fd2807SJeff Garzik 
6462c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6463c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6464c6fd2807SJeff Garzik 	 * eat away the timeout.
6465c6fd2807SJeff Garzik 	 */
6466341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6467c6fd2807SJeff Garzik 
6468341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
646997750cebSTejun Heo 		ata_msleep(ap, interval);
6470c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6471c6fd2807SJeff Garzik 	}
6472c6fd2807SJeff Garzik 
6473c6fd2807SJeff Garzik 	return tmp;
6474c6fd2807SJeff Garzik }
6475a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_register);
6476c6fd2807SJeff Garzik 
6477c6fd2807SJeff Garzik /*
6478c6fd2807SJeff Garzik  * Dummy port_ops
6479c6fd2807SJeff Garzik  */
6480c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6481c6fd2807SJeff Garzik {
6482c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6483c6fd2807SJeff Garzik }
6484c6fd2807SJeff Garzik 
6485182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6486182d7bbaSTejun Heo {
6487182d7bbaSTejun Heo 	/* truly dummy */
6488182d7bbaSTejun Heo }
6489182d7bbaSTejun Heo 
6490029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6491c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6492c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6493182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6494e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
6495e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
6496c6fd2807SJeff Garzik };
6497a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
6498c6fd2807SJeff Garzik 
649921b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
650021b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
650121b0ad4fSTejun Heo };
6502a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_info);
650321b0ad4fSTejun Heo 
650406296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
650506296a1eSJoe Perches {
650606296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
650706296a1eSJoe Perches }
650806296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
6509c206a389SHannes Reinecke 
6510c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_tf_load);
6511c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_exec_command);
6512c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_setup);
6513c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_start);
6514c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_status);
6515