xref: /openbmc/linux/drivers/ata/libata-core.c (revision 1c95a27c)
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;
9633267325STejun Heo 	unsigned long	xfer_mask;
9733267325STejun Heo 	unsigned int	horkage_on;
9833267325STejun Heo 	unsigned int	horkage_off;
998ba5a45cSBartlomiej Zolnierkiewicz 	u16		lflags;
10033267325STejun Heo };
10133267325STejun Heo 
10233267325STejun Heo struct ata_force_ent {
10333267325STejun Heo 	int			port;
10433267325STejun Heo 	int			device;
10533267325STejun Heo 	struct ata_force_param	param;
10633267325STejun Heo };
10733267325STejun Heo 
10833267325STejun Heo static struct ata_force_ent *ata_force_tbl;
10933267325STejun Heo static int ata_force_tbl_size;
11033267325STejun Heo 
111bbf5a097SBartlomiej Zolnierkiewicz static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
1127afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1137afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
1148c27ceffSMauro 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)");
115bf89b0bfSBartlomiej Zolnierkiewicz #endif
11633267325STejun Heo 
1172486fa56STejun Heo static int atapi_enabled = 1;
118c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
119ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
120c6fd2807SJeff Garzik 
121c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
122c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
123ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
124c6fd2807SJeff Garzik 
125baf4fdfaSMark Lord int atapi_passthru16 = 1;
126baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
127ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
128baf4fdfaSMark Lord 
129c6fd2807SJeff Garzik int libata_fua = 0;
130c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
131ad5d8eacSEvgeni Golov MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
132c6fd2807SJeff Garzik 
1332dcb407eSJeff Garzik static int ata_ignore_hpa;
1341e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1351e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1361e999736SAlan Cox 
137b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
138b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
139b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
140b3a70601SAlan Cox 
14187fbc5a0STejun Heo static int ata_probe_timeout;
142c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
143c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
144c6fd2807SJeff Garzik 
1456ebe9d86SJeff Garzik int libata_noacpi = 0;
146d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
147ad5d8eacSEvgeni Golov MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
14811ef697bSKristen Carlson Accardi 
149ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
150ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
151ad5d8eacSEvgeni Golov MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
152ae8d4ee7SAlan Cox 
153e7ecd435STejun Heo static int atapi_an;
154e7ecd435STejun Heo module_param(atapi_an, int, 0444);
155e7ecd435STejun Heo MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
156e7ecd435STejun Heo 
157c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
158c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
159c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
160c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
161c6fd2807SJeff Garzik 
162891fd7c6SDamien Le Moal static inline bool ata_dev_print_info(struct ata_device *dev)
163891fd7c6SDamien Le Moal {
164891fd7c6SDamien Le Moal 	struct ata_eh_context *ehc = &dev->link->eh_context;
165891fd7c6SDamien Le Moal 
166891fd7c6SDamien Le Moal 	return ehc->i.flags & ATA_EHI_PRINTINFO;
167891fd7c6SDamien Le Moal }
168c6fd2807SJeff Garzik 
1699913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1709913ff8aSTejun Heo {
1719913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1729913ff8aSTejun Heo }
1739913ff8aSTejun Heo 
1741eca4365STejun Heo /**
1751eca4365STejun Heo  *	ata_link_next - link iteration helper
1761eca4365STejun Heo  *	@link: the previous link, NULL to start
1771eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1781eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
179aadffb68STejun Heo  *
180aadffb68STejun Heo  *	LOCKING:
181aadffb68STejun Heo  *	Host lock or EH context.
1821eca4365STejun Heo  *
1831eca4365STejun Heo  *	RETURNS:
1841eca4365STejun Heo  *	Pointer to the next link.
185aadffb68STejun Heo  */
1861eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1871eca4365STejun Heo 			       enum ata_link_iter_mode mode)
188aadffb68STejun Heo {
1891eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
1901eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
1911eca4365STejun Heo 
192aadffb68STejun Heo 	/* NULL link indicates start of iteration */
1931eca4365STejun Heo 	if (!link)
1941eca4365STejun Heo 		switch (mode) {
1951eca4365STejun Heo 		case ATA_LITER_EDGE:
1961eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
1971eca4365STejun Heo 			if (sata_pmp_attached(ap))
198aadffb68STejun Heo 				return ap->pmp_link;
199df561f66SGustavo A. R. Silva 			fallthrough;
2001eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
201aadffb68STejun Heo 			return &ap->link;
202aadffb68STejun Heo 		}
203aadffb68STejun Heo 
2041eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2051eca4365STejun Heo 	if (link == &ap->link)
2061eca4365STejun Heo 		switch (mode) {
2071eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2081eca4365STejun Heo 			if (sata_pmp_attached(ap))
209aadffb68STejun Heo 				return ap->pmp_link;
210df561f66SGustavo A. R. Silva 			fallthrough;
2111eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2121eca4365STejun Heo 			if (unlikely(ap->slave_link))
2131eca4365STejun Heo 				return ap->slave_link;
214df561f66SGustavo A. R. Silva 			fallthrough;
2151eca4365STejun Heo 		case ATA_LITER_EDGE:
2161eca4365STejun Heo 			return NULL;
217aadffb68STejun Heo 		}
218aadffb68STejun Heo 
219b1c72916STejun Heo 	/* slave_link excludes PMP */
220b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
221b1c72916STejun Heo 		return NULL;
222b1c72916STejun Heo 
2231eca4365STejun Heo 	/* we were over a PMP link */
224aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
225aadffb68STejun Heo 		return link;
2261eca4365STejun Heo 
2271eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2281eca4365STejun Heo 		return &ap->link;
2291eca4365STejun Heo 
230aadffb68STejun Heo 	return NULL;
231aadffb68STejun Heo }
232a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_next);
233aadffb68STejun Heo 
234c6fd2807SJeff Garzik /**
2351eca4365STejun Heo  *	ata_dev_next - device iteration helper
2361eca4365STejun Heo  *	@dev: the previous device, NULL to start
2371eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2381eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2391eca4365STejun Heo  *
2401eca4365STejun Heo  *	LOCKING:
2411eca4365STejun Heo  *	Host lock or EH context.
2421eca4365STejun Heo  *
2431eca4365STejun Heo  *	RETURNS:
2441eca4365STejun Heo  *	Pointer to the next device.
2451eca4365STejun Heo  */
2461eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2471eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2481eca4365STejun Heo {
2491eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2501eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2511eca4365STejun Heo 
2521eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2531eca4365STejun Heo 	if (!dev)
2541eca4365STejun Heo 		switch (mode) {
2551eca4365STejun Heo 		case ATA_DITER_ENABLED:
2561eca4365STejun Heo 		case ATA_DITER_ALL:
2571eca4365STejun Heo 			dev = link->device;
2581eca4365STejun Heo 			goto check;
2591eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2601eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2611eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2621eca4365STejun Heo 			goto check;
2631eca4365STejun Heo 		}
2641eca4365STejun Heo 
2651eca4365STejun Heo  next:
2661eca4365STejun Heo 	/* move to the next one */
2671eca4365STejun Heo 	switch (mode) {
2681eca4365STejun Heo 	case ATA_DITER_ENABLED:
2691eca4365STejun Heo 	case ATA_DITER_ALL:
2701eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2711eca4365STejun Heo 			goto check;
2721eca4365STejun Heo 		return NULL;
2731eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2741eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2751eca4365STejun Heo 		if (--dev >= link->device)
2761eca4365STejun Heo 			goto check;
2771eca4365STejun Heo 		return NULL;
2781eca4365STejun Heo 	}
2791eca4365STejun Heo 
2801eca4365STejun Heo  check:
2811eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2821eca4365STejun Heo 	    !ata_dev_enabled(dev))
2831eca4365STejun Heo 		goto next;
2841eca4365STejun Heo 	return dev;
2851eca4365STejun Heo }
286a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_next);
2871eca4365STejun Heo 
2881eca4365STejun Heo /**
289b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
290b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
291b1c72916STejun Heo  *
292b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
293b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
294b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
295b1c72916STejun Heo  *
296b1c72916STejun Heo  *	LOCKING:
297b1c72916STejun Heo  *	Don't care.
298b1c72916STejun Heo  *
299b1c72916STejun Heo  *	RETURNS:
300b1c72916STejun Heo  *	Pointer to the found physical link.
301b1c72916STejun Heo  */
302b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
303b1c72916STejun Heo {
304b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
305b1c72916STejun Heo 
306b1c72916STejun Heo 	if (!ap->slave_link)
307b1c72916STejun Heo 		return dev->link;
308b1c72916STejun Heo 	if (!dev->devno)
309b1c72916STejun Heo 		return &ap->link;
310b1c72916STejun Heo 	return ap->slave_link;
311b1c72916STejun Heo }
312b1c72916STejun Heo 
313bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
314b1c72916STejun Heo /**
31533267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3164cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
31733267325STejun Heo  *
31833267325STejun Heo  *	Force cable type according to libata.force and whine about it.
31933267325STejun Heo  *	The last entry which has matching port number is used, so it
32033267325STejun Heo  *	can be specified as part of device force parameters.  For
32133267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
32233267325STejun Heo  *	same effect.
32333267325STejun Heo  *
32433267325STejun Heo  *	LOCKING:
32533267325STejun Heo  *	EH context.
32633267325STejun Heo  */
32733267325STejun Heo void ata_force_cbl(struct ata_port *ap)
32833267325STejun Heo {
32933267325STejun Heo 	int i;
33033267325STejun Heo 
33133267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
33233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
33333267325STejun Heo 
33433267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
33533267325STejun Heo 			continue;
33633267325STejun Heo 
33733267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
33833267325STejun Heo 			continue;
33933267325STejun Heo 
34033267325STejun Heo 		ap->cbl = fe->param.cbl;
341a9a79dfeSJoe Perches 		ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
34233267325STejun Heo 		return;
34333267325STejun Heo 	}
34433267325STejun Heo }
34533267325STejun Heo 
34633267325STejun Heo /**
34705944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
34833267325STejun Heo  *	@link: ATA link of interest
34933267325STejun Heo  *
35005944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
35105944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
35205944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
35305944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
35405944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
35505944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
356b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
357b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
35833267325STejun Heo  *
35933267325STejun Heo  *	LOCKING:
36033267325STejun Heo  *	EH context.
36133267325STejun Heo  */
36205944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
36333267325STejun Heo {
36405944bdfSTejun Heo 	bool did_spd = false;
365b1c72916STejun Heo 	int linkno = link->pmp;
366b1c72916STejun Heo 	int i;
36733267325STejun Heo 
36833267325STejun Heo 	if (ata_is_host_link(link))
369b1c72916STejun Heo 		linkno += 15;
37033267325STejun Heo 
37133267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
37233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
37333267325STejun Heo 
37433267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
37533267325STejun Heo 			continue;
37633267325STejun Heo 
37733267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
37833267325STejun Heo 			continue;
37933267325STejun Heo 
38005944bdfSTejun Heo 		/* only honor the first spd limit */
38105944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
38233267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
383a9a79dfeSJoe Perches 			ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
38405944bdfSTejun Heo 					fe->param.name);
38505944bdfSTejun Heo 			did_spd = true;
38605944bdfSTejun Heo 		}
38705944bdfSTejun Heo 
38805944bdfSTejun Heo 		/* let lflags stack */
38905944bdfSTejun Heo 		if (fe->param.lflags) {
39005944bdfSTejun Heo 			link->flags |= fe->param.lflags;
391a9a79dfeSJoe Perches 			ata_link_notice(link,
39205944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
39305944bdfSTejun Heo 					fe->param.lflags, link->flags);
39405944bdfSTejun Heo 		}
39533267325STejun Heo 	}
39633267325STejun Heo }
39733267325STejun Heo 
39833267325STejun Heo /**
39933267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
40033267325STejun Heo  *	@dev: ATA device of interest
40133267325STejun Heo  *
40233267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
40333267325STejun Heo  *	For consistency with link selection, device number 15 selects
40433267325STejun Heo  *	the first device connected to the host link.
40533267325STejun Heo  *
40633267325STejun Heo  *	LOCKING:
40733267325STejun Heo  *	EH context.
40833267325STejun Heo  */
40933267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
41033267325STejun Heo {
41133267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
41233267325STejun Heo 	int alt_devno = devno;
41333267325STejun Heo 	int i;
41433267325STejun Heo 
415b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
416b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
417b1c72916STejun Heo 		alt_devno += 15;
41833267325STejun Heo 
41933267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
42033267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
42133267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
42233267325STejun Heo 
42333267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
42433267325STejun Heo 			continue;
42533267325STejun Heo 
42633267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
42733267325STejun Heo 		    fe->device != alt_devno)
42833267325STejun Heo 			continue;
42933267325STejun Heo 
43033267325STejun Heo 		if (!fe->param.xfer_mask)
43133267325STejun Heo 			continue;
43233267325STejun Heo 
43333267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
43433267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
43533267325STejun Heo 		if (udma_mask)
43633267325STejun Heo 			dev->udma_mask = udma_mask;
43733267325STejun Heo 		else if (mwdma_mask) {
43833267325STejun Heo 			dev->udma_mask = 0;
43933267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
44033267325STejun Heo 		} else {
44133267325STejun Heo 			dev->udma_mask = 0;
44233267325STejun Heo 			dev->mwdma_mask = 0;
44333267325STejun Heo 			dev->pio_mask = pio_mask;
44433267325STejun Heo 		}
44533267325STejun Heo 
446a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
447a9a79dfeSJoe Perches 			       fe->param.name);
44833267325STejun Heo 		return;
44933267325STejun Heo 	}
45033267325STejun Heo }
45133267325STejun Heo 
45233267325STejun Heo /**
45333267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
45433267325STejun Heo  *	@dev: ATA device of interest
45533267325STejun Heo  *
45633267325STejun Heo  *	Force horkage according to libata.force and whine about it.
45733267325STejun Heo  *	For consistency with link selection, device number 15 selects
45833267325STejun Heo  *	the first device connected to the host link.
45933267325STejun Heo  *
46033267325STejun Heo  *	LOCKING:
46133267325STejun Heo  *	EH context.
46233267325STejun Heo  */
46333267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
46433267325STejun Heo {
46533267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
46633267325STejun Heo 	int alt_devno = devno;
46733267325STejun Heo 	int i;
46833267325STejun Heo 
469b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
470b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
471b1c72916STejun Heo 		alt_devno += 15;
47233267325STejun Heo 
47333267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
47433267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
47533267325STejun Heo 
47633267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
47733267325STejun Heo 			continue;
47833267325STejun Heo 
47933267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
48033267325STejun Heo 		    fe->device != alt_devno)
48133267325STejun Heo 			continue;
48233267325STejun Heo 
48333267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
48433267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
48533267325STejun Heo 			continue;
48633267325STejun Heo 
48733267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
48833267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
48933267325STejun Heo 
490a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
491a9a79dfeSJoe Perches 			       fe->param.name);
49233267325STejun Heo 	}
49333267325STejun Heo }
494bf89b0bfSBartlomiej Zolnierkiewicz #else
495bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_link_limits(struct ata_link *link) { }
496bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_xfermask(struct ata_device *dev) { }
497bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_horkage(struct ata_device *dev) { }
498bf89b0bfSBartlomiej Zolnierkiewicz #endif
49933267325STejun Heo 
50033267325STejun Heo /**
501436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
502436d34b3STejun Heo  *	@opcode: SCSI opcode
503436d34b3STejun Heo  *
504436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
505436d34b3STejun Heo  *
506436d34b3STejun Heo  *	LOCKING:
507436d34b3STejun Heo  *	None.
508436d34b3STejun Heo  *
509436d34b3STejun Heo  *	RETURNS:
510436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
511436d34b3STejun Heo  */
512436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
513436d34b3STejun Heo {
514436d34b3STejun Heo 	switch (opcode) {
515436d34b3STejun Heo 	case GPCMD_READ_10:
516436d34b3STejun Heo 	case GPCMD_READ_12:
517436d34b3STejun Heo 		return ATAPI_READ;
518436d34b3STejun Heo 
519436d34b3STejun Heo 	case GPCMD_WRITE_10:
520436d34b3STejun Heo 	case GPCMD_WRITE_12:
521436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
522436d34b3STejun Heo 		return ATAPI_WRITE;
523436d34b3STejun Heo 
524436d34b3STejun Heo 	case GPCMD_READ_CD:
525436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
526436d34b3STejun Heo 		return ATAPI_READ_CD;
527436d34b3STejun Heo 
528e52dcc48STejun Heo 	case ATA_16:
529e52dcc48STejun Heo 	case ATA_12:
530e52dcc48STejun Heo 		if (atapi_passthru16)
531e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
532df561f66SGustavo A. R. Silva 		fallthrough;
533436d34b3STejun Heo 	default:
534436d34b3STejun Heo 		return ATAPI_MISC;
535436d34b3STejun Heo 	}
536436d34b3STejun Heo }
537a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(atapi_cmd_type);
538436d34b3STejun Heo 
539c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
540c6fd2807SJeff Garzik 	/* pio multi */
541c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
542c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
543c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
544c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
545c6fd2807SJeff Garzik 	0,
546c6fd2807SJeff Garzik 	0,
547c6fd2807SJeff Garzik 	0,
548c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
549c6fd2807SJeff Garzik 	/* pio */
550c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
551c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
552c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
553c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
554c6fd2807SJeff Garzik 	0,
555c6fd2807SJeff Garzik 	0,
556c6fd2807SJeff Garzik 	0,
557c6fd2807SJeff Garzik 	0,
558c6fd2807SJeff Garzik 	/* dma */
559c6fd2807SJeff Garzik 	ATA_CMD_READ,
560c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
561c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
562c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
563c6fd2807SJeff Garzik 	0,
564c6fd2807SJeff Garzik 	0,
565c6fd2807SJeff Garzik 	0,
566c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
567c6fd2807SJeff Garzik };
568c6fd2807SJeff Garzik 
569c6fd2807SJeff Garzik /**
570c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
571bd056d7eSTejun Heo  *	@tf: command to examine and configure
572bd056d7eSTejun Heo  *	@dev: device tf belongs to
573c6fd2807SJeff Garzik  *
574c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
575c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
576c6fd2807SJeff Garzik  *
577c6fd2807SJeff Garzik  *	LOCKING:
578c6fd2807SJeff Garzik  *	caller.
579c6fd2807SJeff Garzik  */
580bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
581c6fd2807SJeff Garzik {
582c6fd2807SJeff Garzik 	u8 cmd;
583c6fd2807SJeff Garzik 
584c6fd2807SJeff Garzik 	int index, fua, lba48, write;
585c6fd2807SJeff Garzik 
586c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
587c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
588c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
589c6fd2807SJeff Garzik 
590c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
591c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
592c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
5939af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
594c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
595c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
596c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
597c6fd2807SJeff Garzik 	} else {
598c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
599c6fd2807SJeff Garzik 		index = 16;
600c6fd2807SJeff Garzik 	}
601c6fd2807SJeff Garzik 
602c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
603c6fd2807SJeff Garzik 	if (cmd) {
604c6fd2807SJeff Garzik 		tf->command = cmd;
605c6fd2807SJeff Garzik 		return 0;
606c6fd2807SJeff Garzik 	}
607c6fd2807SJeff Garzik 	return -1;
608c6fd2807SJeff Garzik }
609c6fd2807SJeff Garzik 
610c6fd2807SJeff Garzik /**
61135b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
61235b649feSTejun Heo  *	@tf: ATA taskfile of interest
61335b649feSTejun Heo  *	@dev: ATA device @tf belongs to
61435b649feSTejun Heo  *
61535b649feSTejun Heo  *	LOCKING:
61635b649feSTejun Heo  *	None.
61735b649feSTejun Heo  *
61835b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
61935b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
62035b649feSTejun Heo  *	flags select the address format to use.
62135b649feSTejun Heo  *
62235b649feSTejun Heo  *	RETURNS:
62335b649feSTejun Heo  *	Block address read from @tf.
62435b649feSTejun Heo  */
625cffd1ee9SHannes Reinecke u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
62635b649feSTejun Heo {
62735b649feSTejun Heo 	u64 block = 0;
62835b649feSTejun Heo 
629fe16d4f2STejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
63035b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
63135b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
63235b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
63344901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
63435b649feSTejun Heo 		} else
63535b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
63635b649feSTejun Heo 
63735b649feSTejun Heo 		block |= tf->lbah << 16;
63835b649feSTejun Heo 		block |= tf->lbam << 8;
63935b649feSTejun Heo 		block |= tf->lbal;
64035b649feSTejun Heo 	} else {
64135b649feSTejun Heo 		u32 cyl, head, sect;
64235b649feSTejun Heo 
64335b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
64435b649feSTejun Heo 		head = tf->device & 0xf;
64535b649feSTejun Heo 		sect = tf->lbal;
64635b649feSTejun Heo 
647ac8672eaSTejun Heo 		if (!sect) {
648a9a79dfeSJoe Perches 			ata_dev_warn(dev,
649a9a79dfeSJoe Perches 				     "device reported invalid CHS sector 0\n");
650cffd1ee9SHannes Reinecke 			return U64_MAX;
651ac8672eaSTejun Heo 		}
652ac8672eaSTejun Heo 
653ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
65435b649feSTejun Heo 	}
65535b649feSTejun Heo 
65635b649feSTejun Heo 	return block;
65735b649feSTejun Heo }
65835b649feSTejun Heo 
65935b649feSTejun Heo /**
660bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
661bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
662bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
663bd056d7eSTejun Heo  *	@block: Block address
664bd056d7eSTejun Heo  *	@n_block: Number of blocks
665bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
666bd056d7eSTejun Heo  *	@tag: tag
6678e061784SAdam Manzanares  *	@class: IO priority class
668bd056d7eSTejun Heo  *
669bd056d7eSTejun Heo  *	LOCKING:
670bd056d7eSTejun Heo  *	None.
671bd056d7eSTejun Heo  *
672bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
673bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
674bd056d7eSTejun Heo  *
675bd056d7eSTejun Heo  *	RETURNS:
676bd056d7eSTejun Heo  *
677bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
678bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
679bd056d7eSTejun Heo  */
680bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
681bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
6828e061784SAdam Manzanares 		    unsigned int tag, int class)
683bd056d7eSTejun Heo {
684bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
685bd056d7eSTejun Heo 	tf->flags |= tf_flags;
686bd056d7eSTejun Heo 
6872e2cc676SJens Axboe 	if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
688bd056d7eSTejun Heo 		/* yay, NCQ */
689bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
690bd056d7eSTejun Heo 			return -ERANGE;
691bd056d7eSTejun Heo 
692bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
693bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
694bd056d7eSTejun Heo 
695bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
696bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
697bd056d7eSTejun Heo 		else
698bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
699bd056d7eSTejun Heo 
700bd056d7eSTejun Heo 		tf->nsect = tag << 3;
701bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
702bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
703bd056d7eSTejun Heo 
704bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
705bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
706bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
707bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
708bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
709bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
710bd056d7eSTejun Heo 
7119ca7cfa4SSergei Shtylyov 		tf->device = ATA_LBA;
712bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
713bd056d7eSTejun Heo 			tf->device |= 1 << 7;
7148e061784SAdam Manzanares 
7152360fa18SDamien Le Moal 		if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE &&
7162360fa18SDamien Le Moal 		    class == IOPRIO_CLASS_RT)
7172360fa18SDamien Le Moal 			tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO;
718bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
719bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
720bd056d7eSTejun Heo 
721bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
722bd056d7eSTejun Heo 			/* use LBA28 */
723bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
724bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
725bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
726bd056d7eSTejun Heo 				return -ERANGE;
727bd056d7eSTejun Heo 
728bd056d7eSTejun Heo 			/* use LBA48 */
729bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
730bd056d7eSTejun Heo 
731bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
732bd056d7eSTejun Heo 
733bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
734bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
735bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
736bd056d7eSTejun Heo 		} else
737bd056d7eSTejun Heo 			/* request too large even for LBA48 */
738bd056d7eSTejun Heo 			return -ERANGE;
739bd056d7eSTejun Heo 
740bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
741bd056d7eSTejun Heo 			return -EINVAL;
742bd056d7eSTejun Heo 
743bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
744bd056d7eSTejun Heo 
745bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
746bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
747bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
748bd056d7eSTejun Heo 
749bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
750bd056d7eSTejun Heo 	} else {
751bd056d7eSTejun Heo 		/* CHS */
752bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
753bd056d7eSTejun Heo 
754bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
755bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
756bd056d7eSTejun Heo 			return -ERANGE;
757bd056d7eSTejun Heo 
758bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
759bd056d7eSTejun Heo 			return -EINVAL;
760bd056d7eSTejun Heo 
761bd056d7eSTejun Heo 		/* Convert LBA to CHS */
762bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
763bd056d7eSTejun Heo 		cyl   = track / dev->heads;
764bd056d7eSTejun Heo 		head  = track % dev->heads;
765bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
766bd056d7eSTejun Heo 
767bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
768bd056d7eSTejun Heo 		   Cylinder: 0-65535
769bd056d7eSTejun Heo 		   Head: 0-15
770bd056d7eSTejun Heo 		   Sector: 1-255*/
771bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
772bd056d7eSTejun Heo 			return -ERANGE;
773bd056d7eSTejun Heo 
774bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
775bd056d7eSTejun Heo 		tf->lbal = sect;
776bd056d7eSTejun Heo 		tf->lbam = cyl;
777bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
778bd056d7eSTejun Heo 		tf->device |= head;
779bd056d7eSTejun Heo 	}
780bd056d7eSTejun Heo 
781bd056d7eSTejun Heo 	return 0;
782bd056d7eSTejun Heo }
783bd056d7eSTejun Heo 
784bd056d7eSTejun Heo /**
785c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
786c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
787c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
788c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
789c6fd2807SJeff Garzik  *
790c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
791c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
792c6fd2807SJeff Garzik  *
793c6fd2807SJeff Garzik  *	LOCKING:
794c6fd2807SJeff Garzik  *	None.
795c6fd2807SJeff Garzik  *
796c6fd2807SJeff Garzik  *	RETURNS:
797c6fd2807SJeff Garzik  *	Packed xfer_mask.
798c6fd2807SJeff Garzik  */
7997dc951aeSTejun Heo unsigned long ata_pack_xfermask(unsigned long pio_mask,
8007dc951aeSTejun Heo 				unsigned long mwdma_mask,
8017dc951aeSTejun Heo 				unsigned long udma_mask)
802c6fd2807SJeff Garzik {
803c6fd2807SJeff Garzik 	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
804c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
805c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
806c6fd2807SJeff Garzik }
807a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pack_xfermask);
808c6fd2807SJeff Garzik 
809c6fd2807SJeff Garzik /**
810c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
811c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
812c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
813c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
814c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
815c6fd2807SJeff Garzik  *
816c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
817c9b5560aSMasanari Iida  *	Any NULL destination masks will be ignored.
818c6fd2807SJeff Garzik  */
8197dc951aeSTejun Heo void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
8207dc951aeSTejun Heo 			 unsigned long *mwdma_mask, unsigned long *udma_mask)
821c6fd2807SJeff Garzik {
822c6fd2807SJeff Garzik 	if (pio_mask)
823c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
824c6fd2807SJeff Garzik 	if (mwdma_mask)
825c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
826c6fd2807SJeff Garzik 	if (udma_mask)
827c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
828c6fd2807SJeff Garzik }
829c6fd2807SJeff Garzik 
830c6fd2807SJeff Garzik static const struct ata_xfer_ent {
831c6fd2807SJeff Garzik 	int shift, bits;
832c6fd2807SJeff Garzik 	u8 base;
833c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
83470cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
83570cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
83670cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
837c6fd2807SJeff Garzik 	{ -1, },
838c6fd2807SJeff Garzik };
839c6fd2807SJeff Garzik 
840c6fd2807SJeff Garzik /**
841c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
842c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
843c6fd2807SJeff Garzik  *
844c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
845c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
846c6fd2807SJeff Garzik  *
847c6fd2807SJeff Garzik  *	LOCKING:
848c6fd2807SJeff Garzik  *	None.
849c6fd2807SJeff Garzik  *
850c6fd2807SJeff Garzik  *	RETURNS:
85170cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
852c6fd2807SJeff Garzik  */
8537dc951aeSTejun Heo u8 ata_xfer_mask2mode(unsigned long xfer_mask)
854c6fd2807SJeff Garzik {
855c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
856c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
857c6fd2807SJeff Garzik 
858c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
859c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
860c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
86170cd071eSTejun Heo 	return 0xff;
862c6fd2807SJeff Garzik }
863a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
864c6fd2807SJeff Garzik 
865c6fd2807SJeff Garzik /**
866c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
867c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
868c6fd2807SJeff Garzik  *
869c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
870c6fd2807SJeff Garzik  *
871c6fd2807SJeff Garzik  *	LOCKING:
872c6fd2807SJeff Garzik  *	None.
873c6fd2807SJeff Garzik  *
874c6fd2807SJeff Garzik  *	RETURNS:
875c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
876c6fd2807SJeff Garzik  */
8777dc951aeSTejun Heo unsigned long ata_xfer_mode2mask(u8 xfer_mode)
878c6fd2807SJeff Garzik {
879c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
880c6fd2807SJeff Garzik 
881c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
882c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
88370cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
88470cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
885c6fd2807SJeff Garzik 	return 0;
886c6fd2807SJeff Garzik }
887a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
888c6fd2807SJeff Garzik 
889c6fd2807SJeff Garzik /**
890c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
891c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
892c6fd2807SJeff Garzik  *
893c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
894c6fd2807SJeff Garzik  *
895c6fd2807SJeff Garzik  *	LOCKING:
896c6fd2807SJeff Garzik  *	None.
897c6fd2807SJeff Garzik  *
898c6fd2807SJeff Garzik  *	RETURNS:
899c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
900c6fd2807SJeff Garzik  */
9017dc951aeSTejun Heo int ata_xfer_mode2shift(unsigned long xfer_mode)
902c6fd2807SJeff Garzik {
903c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
904c6fd2807SJeff Garzik 
905c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
906c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
907c6fd2807SJeff Garzik 			return ent->shift;
908c6fd2807SJeff Garzik 	return -1;
909c6fd2807SJeff Garzik }
910a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
911c6fd2807SJeff Garzik 
912c6fd2807SJeff Garzik /**
913c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
914c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
915c6fd2807SJeff Garzik  *
916c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
917c6fd2807SJeff Garzik  *	(highest bit in @modemask).
918c6fd2807SJeff Garzik  *
919c6fd2807SJeff Garzik  *	LOCKING:
920c6fd2807SJeff Garzik  *	None.
921c6fd2807SJeff Garzik  *
922c6fd2807SJeff Garzik  *	RETURNS:
923c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
924c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
925c6fd2807SJeff Garzik  */
9267dc951aeSTejun Heo const char *ata_mode_string(unsigned long xfer_mask)
927c6fd2807SJeff Garzik {
928c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
929c6fd2807SJeff Garzik 		"PIO0",
930c6fd2807SJeff Garzik 		"PIO1",
931c6fd2807SJeff Garzik 		"PIO2",
932c6fd2807SJeff Garzik 		"PIO3",
933c6fd2807SJeff Garzik 		"PIO4",
934b352e57dSAlan Cox 		"PIO5",
935b352e57dSAlan Cox 		"PIO6",
936c6fd2807SJeff Garzik 		"MWDMA0",
937c6fd2807SJeff Garzik 		"MWDMA1",
938c6fd2807SJeff Garzik 		"MWDMA2",
939b352e57dSAlan Cox 		"MWDMA3",
940b352e57dSAlan Cox 		"MWDMA4",
941c6fd2807SJeff Garzik 		"UDMA/16",
942c6fd2807SJeff Garzik 		"UDMA/25",
943c6fd2807SJeff Garzik 		"UDMA/33",
944c6fd2807SJeff Garzik 		"UDMA/44",
945c6fd2807SJeff Garzik 		"UDMA/66",
946c6fd2807SJeff Garzik 		"UDMA/100",
947c6fd2807SJeff Garzik 		"UDMA/133",
948c6fd2807SJeff Garzik 		"UDMA7",
949c6fd2807SJeff Garzik 	};
950c6fd2807SJeff Garzik 	int highbit;
951c6fd2807SJeff Garzik 
952c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
953c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
954c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
955c6fd2807SJeff Garzik 	return "<n/a>";
956c6fd2807SJeff Garzik }
957a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_mode_string);
958c6fd2807SJeff Garzik 
959d9027470SGwendal Grignou const char *sata_spd_string(unsigned int spd)
960c6fd2807SJeff Garzik {
961c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
962c6fd2807SJeff Garzik 		"1.5 Gbps",
963c6fd2807SJeff Garzik 		"3.0 Gbps",
9648522ee25SShane Huang 		"6.0 Gbps",
965c6fd2807SJeff Garzik 	};
966c6fd2807SJeff Garzik 
967c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
968c6fd2807SJeff Garzik 		return "<unknown>";
969c6fd2807SJeff Garzik 	return spd_str[spd - 1];
970c6fd2807SJeff Garzik }
971c6fd2807SJeff Garzik 
972c6fd2807SJeff Garzik /**
973c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
974c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
975c6fd2807SJeff Garzik  *
976c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
977c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
978c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
979c6fd2807SJeff Garzik  *
980c6fd2807SJeff Garzik  *	LOCKING:
981c6fd2807SJeff Garzik  *	None.
982c6fd2807SJeff Garzik  *
983c6fd2807SJeff Garzik  *	RETURNS:
9849162c657SHannes Reinecke  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
9859162c657SHannes Reinecke  *	%ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
986c6fd2807SJeff Garzik  */
987c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
988c6fd2807SJeff Garzik {
989c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
990c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
991c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
992633273a3STejun Heo 	 *
993633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
994633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
995633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
996633273a3STejun Heo 	 * spec has never mentioned about using different signatures
997633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
998633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
999633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1000633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1001633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1002633273a3STejun Heo 	 * SerialATA.
1003633273a3STejun Heo 	 *
1004633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1005633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
100679b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
100779b42babSTejun Heo 	 * SEMB signature.  This is worked around in
100879b42babSTejun Heo 	 * ata_dev_read_id().
1009c6fd2807SJeff Garzik 	 */
10106c952a0dSHannes Reinecke 	if (tf->lbam == 0 && tf->lbah == 0)
1011c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1012c6fd2807SJeff Garzik 
10136c952a0dSHannes Reinecke 	if (tf->lbam == 0x14 && tf->lbah == 0xeb)
1014c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1015c6fd2807SJeff Garzik 
10166c952a0dSHannes Reinecke 	if (tf->lbam == 0x69 && tf->lbah == 0x96)
1017633273a3STejun Heo 		return ATA_DEV_PMP;
1018633273a3STejun Heo 
10196c952a0dSHannes Reinecke 	if (tf->lbam == 0x3c && tf->lbah == 0xc3)
102079b42babSTejun Heo 		return ATA_DEV_SEMB;
1021633273a3STejun Heo 
10226c952a0dSHannes Reinecke 	if (tf->lbam == 0xcd && tf->lbah == 0xab)
10239162c657SHannes Reinecke 		return ATA_DEV_ZAC;
10249162c657SHannes Reinecke 
1025c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1026c6fd2807SJeff Garzik }
1027a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_classify);
1028c6fd2807SJeff Garzik 
1029c6fd2807SJeff Garzik /**
1030c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1031c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1032c6fd2807SJeff Garzik  *	@s: string into which data is output
1033c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1034c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1035c6fd2807SJeff Garzik  *
1036c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1037c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1038c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1039c6fd2807SJeff Garzik  *
1040c6fd2807SJeff Garzik  *	LOCKING:
1041c6fd2807SJeff Garzik  *	caller.
1042c6fd2807SJeff Garzik  */
1043c6fd2807SJeff Garzik 
1044c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1045c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1046c6fd2807SJeff Garzik {
1047c6fd2807SJeff Garzik 	unsigned int c;
1048c6fd2807SJeff Garzik 
1049963e4975SAlan Cox 	BUG_ON(len & 1);
1050963e4975SAlan Cox 
1051c6fd2807SJeff Garzik 	while (len > 0) {
1052c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1053c6fd2807SJeff Garzik 		*s = c;
1054c6fd2807SJeff Garzik 		s++;
1055c6fd2807SJeff Garzik 
1056c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1057c6fd2807SJeff Garzik 		*s = c;
1058c6fd2807SJeff Garzik 		s++;
1059c6fd2807SJeff Garzik 
1060c6fd2807SJeff Garzik 		ofs++;
1061c6fd2807SJeff Garzik 		len -= 2;
1062c6fd2807SJeff Garzik 	}
1063c6fd2807SJeff Garzik }
1064a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_string);
1065c6fd2807SJeff Garzik 
1066c6fd2807SJeff Garzik /**
1067c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1068c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1069c6fd2807SJeff Garzik  *	@s: string into which data is output
1070c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1071c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1072c6fd2807SJeff Garzik  *
1073c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1074c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1075c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1076c6fd2807SJeff Garzik  *
1077c6fd2807SJeff Garzik  *	LOCKING:
1078c6fd2807SJeff Garzik  *	caller.
1079c6fd2807SJeff Garzik  */
1080c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1081c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1082c6fd2807SJeff Garzik {
1083c6fd2807SJeff Garzik 	unsigned char *p;
1084c6fd2807SJeff Garzik 
1085c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1086c6fd2807SJeff Garzik 
1087c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1088c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1089c6fd2807SJeff Garzik 		p--;
1090c6fd2807SJeff Garzik 	*p = '\0';
1091c6fd2807SJeff Garzik }
1092a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_c_string);
1093c6fd2807SJeff Garzik 
1094db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1095db6f8759STejun Heo {
1096db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1097db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1098968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1099db6f8759STejun Heo 		else
1100968e594aSRobert Hancock 			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1101db6f8759STejun Heo 	} else {
1102db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1103968e594aSRobert Hancock 			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1104968e594aSRobert Hancock 			       id[ATA_ID_CUR_SECTORS];
1105db6f8759STejun Heo 		else
1106968e594aSRobert Hancock 			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1107968e594aSRobert Hancock 			       id[ATA_ID_SECTORS];
1108db6f8759STejun Heo 	}
1109db6f8759STejun Heo }
1110db6f8759STejun Heo 
1111a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
11121e999736SAlan Cox {
11131e999736SAlan Cox 	u64 sectors = 0;
11141e999736SAlan Cox 
11151e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
11161e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1117ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
11181e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11191e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11201e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11211e999736SAlan Cox 
1122a5987e0aSTejun Heo 	return sectors;
11231e999736SAlan Cox }
11241e999736SAlan Cox 
1125a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
11261e999736SAlan Cox {
11271e999736SAlan Cox 	u64 sectors = 0;
11281e999736SAlan Cox 
11291e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
11301e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11311e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11321e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11331e999736SAlan Cox 
1134a5987e0aSTejun Heo 	return sectors;
11351e999736SAlan Cox }
11361e999736SAlan Cox 
11371e999736SAlan Cox /**
1138c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1139c728a914STejun Heo  *	@dev: target device
1140c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
11411e999736SAlan Cox  *
1142c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1143c728a914STejun Heo  *	question.
1144c728a914STejun Heo  *
1145c728a914STejun Heo  *	RETURNS:
1146c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1147c728a914STejun Heo  *	-EIO on other errors.
11481e999736SAlan Cox  */
1149c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
11501e999736SAlan Cox {
1151c728a914STejun Heo 	unsigned int err_mask;
11521e999736SAlan Cox 	struct ata_taskfile tf;
1153c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
11541e999736SAlan Cox 
11551e999736SAlan Cox 	ata_tf_init(dev, &tf);
11561e999736SAlan Cox 
1157c728a914STejun Heo 	/* always clear all address registers */
11581e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1159c728a914STejun Heo 
1160c728a914STejun Heo 	if (lba48) {
1161c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1162c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1163c728a914STejun Heo 	} else
1164c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1165c728a914STejun Heo 
1166bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1167c728a914STejun Heo 	tf.device |= ATA_LBA;
11681e999736SAlan Cox 
11692b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1170c728a914STejun Heo 	if (err_mask) {
1171a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1172a9a79dfeSJoe Perches 			     "failed to read native max address (err_mask=0x%x)\n",
1173a9a79dfeSJoe Perches 			     err_mask);
1174c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1175c728a914STejun Heo 			return -EACCES;
1176c728a914STejun Heo 		return -EIO;
1177c728a914STejun Heo 	}
1178c728a914STejun Heo 
1179c728a914STejun Heo 	if (lba48)
1180a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1181c728a914STejun Heo 	else
1182a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
118393328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
118493328e11SAlan Cox 		(*max_sectors)--;
11851e999736SAlan Cox 	return 0;
11861e999736SAlan Cox }
11871e999736SAlan Cox 
11881e999736SAlan Cox /**
1189c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1190c728a914STejun Heo  *	@dev: target device
11916b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
11921e999736SAlan Cox  *
1193c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1194c728a914STejun Heo  *
1195c728a914STejun Heo  *	RETURNS:
1196c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1197c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1198c728a914STejun Heo  *	errors.
11991e999736SAlan Cox  */
120005027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
12011e999736SAlan Cox {
1202c728a914STejun Heo 	unsigned int err_mask;
12031e999736SAlan Cox 	struct ata_taskfile tf;
1204c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12051e999736SAlan Cox 
12061e999736SAlan Cox 	new_sectors--;
12071e999736SAlan Cox 
12081e999736SAlan Cox 	ata_tf_init(dev, &tf);
12091e999736SAlan Cox 
1210c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
12111e999736SAlan Cox 
1212c728a914STejun Heo 	if (lba48) {
1213c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1214c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
12151e999736SAlan Cox 
12161e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
12171e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
12181e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
12191e582ba4STejun Heo 	} else {
12201e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1221c728a914STejun Heo 
12221e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
12231e582ba4STejun Heo 	}
12241e582ba4STejun Heo 
1225bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1226c728a914STejun Heo 	tf.device |= ATA_LBA;
12271e999736SAlan Cox 
12281e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
12291e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
12301e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
12311e999736SAlan Cox 
12322b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1233c728a914STejun Heo 	if (err_mask) {
1234a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1235a9a79dfeSJoe Perches 			     "failed to set max address (err_mask=0x%x)\n",
1236a9a79dfeSJoe Perches 			     err_mask);
1237c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1238c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1239c728a914STejun Heo 			return -EACCES;
1240c728a914STejun Heo 		return -EIO;
1241c728a914STejun Heo 	}
1242c728a914STejun Heo 
12431e999736SAlan Cox 	return 0;
12441e999736SAlan Cox }
12451e999736SAlan Cox 
12461e999736SAlan Cox /**
12471e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
12481e999736SAlan Cox  *	@dev: Device to resize
12491e999736SAlan Cox  *
12501e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
12511e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
12521e999736SAlan Cox  *	the drive has the HPA feature set enabled.
125305027adcSTejun Heo  *
125405027adcSTejun Heo  *	RETURNS:
125505027adcSTejun Heo  *	0 on success, -errno on failure.
12561e999736SAlan Cox  */
125705027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
12581e999736SAlan Cox {
1259891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
1260445d211bSTejun Heo 	bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
126105027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
126205027adcSTejun Heo 	u64 native_sectors;
1263c728a914STejun Heo 	int rc;
12641e999736SAlan Cox 
126505027adcSTejun Heo 	/* do we need to do it? */
12669162c657SHannes Reinecke 	if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
126705027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
126805027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1269c728a914STejun Heo 		return 0;
12701e999736SAlan Cox 
127105027adcSTejun Heo 	/* read native max address */
127205027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
127305027adcSTejun Heo 	if (rc) {
1274dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1275dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
127605027adcSTejun Heo 		 */
1277445d211bSTejun Heo 		if (rc == -EACCES || !unlock_hpa) {
1278a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1279a9a79dfeSJoe Perches 				     "HPA support seems broken, skipping HPA handling\n");
128005027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
128105027adcSTejun Heo 
128205027adcSTejun Heo 			/* we can continue if device aborted the command */
128305027adcSTejun Heo 			if (rc == -EACCES)
128405027adcSTejun Heo 				rc = 0;
128505027adcSTejun Heo 		}
128605027adcSTejun Heo 
128705027adcSTejun Heo 		return rc;
128805027adcSTejun Heo 	}
12895920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
129005027adcSTejun Heo 
129105027adcSTejun Heo 	/* nothing to do? */
1292445d211bSTejun Heo 	if (native_sectors <= sectors || !unlock_hpa) {
129305027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
129405027adcSTejun Heo 			return 0;
129505027adcSTejun Heo 
129605027adcSTejun Heo 		if (native_sectors > sectors)
1297a9a79dfeSJoe Perches 			ata_dev_info(dev,
129805027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
129905027adcSTejun Heo 				(unsigned long long)sectors,
130005027adcSTejun Heo 				(unsigned long long)native_sectors);
130105027adcSTejun Heo 		else if (native_sectors < sectors)
1302a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1303a9a79dfeSJoe Perches 				"native sectors (%llu) is smaller than sectors (%llu)\n",
130405027adcSTejun Heo 				(unsigned long long)native_sectors,
130505027adcSTejun Heo 				(unsigned long long)sectors);
130605027adcSTejun Heo 		return 0;
13071e999736SAlan Cox 	}
130837301a55STejun Heo 
130905027adcSTejun Heo 	/* let's unlock HPA */
131005027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
131105027adcSTejun Heo 	if (rc == -EACCES) {
131205027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
1313a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1314a9a79dfeSJoe Perches 			     "device aborted resize (%llu -> %llu), skipping HPA handling\n",
131505027adcSTejun Heo 			     (unsigned long long)sectors,
131605027adcSTejun Heo 			     (unsigned long long)native_sectors);
131705027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
131805027adcSTejun Heo 		return 0;
131905027adcSTejun Heo 	} else if (rc)
132005027adcSTejun Heo 		return rc;
132105027adcSTejun Heo 
132205027adcSTejun Heo 	/* re-read IDENTIFY data */
132305027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
132405027adcSTejun Heo 	if (rc) {
1325a9a79dfeSJoe Perches 		ata_dev_err(dev,
1326a9a79dfeSJoe Perches 			    "failed to re-read IDENTIFY data after HPA resizing\n");
132705027adcSTejun Heo 		return rc;
132805027adcSTejun Heo 	}
132905027adcSTejun Heo 
133005027adcSTejun Heo 	if (print_info) {
133105027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
1332a9a79dfeSJoe Perches 		ata_dev_info(dev,
133305027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
133405027adcSTejun Heo 			(unsigned long long)sectors,
133505027adcSTejun Heo 			(unsigned long long)new_sectors,
133605027adcSTejun Heo 			(unsigned long long)native_sectors);
133705027adcSTejun Heo 	}
133805027adcSTejun Heo 
133905027adcSTejun Heo 	return 0;
13401e999736SAlan Cox }
13411e999736SAlan Cox 
1342c6fd2807SJeff Garzik /**
1343c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
13446044f3c4SHannes Reinecke  *	@dev: device from which the information is fetched
1345c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1346c6fd2807SJeff Garzik  *
1347c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1348c6fd2807SJeff Garzik  *	page.
1349c6fd2807SJeff Garzik  *
1350c6fd2807SJeff Garzik  *	LOCKING:
1351c6fd2807SJeff Garzik  *	caller.
1352c6fd2807SJeff Garzik  */
1353c6fd2807SJeff Garzik 
13546044f3c4SHannes Reinecke static inline void ata_dump_id(struct ata_device *dev, const u16 *id)
1355c6fd2807SJeff Garzik {
13566044f3c4SHannes Reinecke 	ata_dev_dbg(dev,
13576044f3c4SHannes Reinecke 		"49==0x%04x  53==0x%04x  63==0x%04x  64==0x%04x  75==0x%04x\n"
13586044f3c4SHannes Reinecke 		"80==0x%04x  81==0x%04x  82==0x%04x  83==0x%04x  84==0x%04x\n"
13596044f3c4SHannes Reinecke 		"88==0x%04x  93==0x%04x\n",
13606044f3c4SHannes Reinecke 		id[49], id[53], id[63], id[64], id[75], id[80],
13616044f3c4SHannes Reinecke 		id[81], id[82], id[83], id[84], id[88], id[93]);
1362c6fd2807SJeff Garzik }
1363c6fd2807SJeff Garzik 
1364c6fd2807SJeff Garzik /**
1365c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1366c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1367c6fd2807SJeff Garzik  *
1368c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1369c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1370c6fd2807SJeff Garzik  *
1371c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1372c6fd2807SJeff Garzik  *
1373c6fd2807SJeff Garzik  *	LOCKING:
1374c6fd2807SJeff Garzik  *	None.
1375c6fd2807SJeff Garzik  *
1376c6fd2807SJeff Garzik  *	RETURNS:
1377c6fd2807SJeff Garzik  *	Computed xfermask
1378c6fd2807SJeff Garzik  */
13797dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1380c6fd2807SJeff Garzik {
13817dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1382c6fd2807SJeff Garzik 
1383c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1384c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1385c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1386c6fd2807SJeff Garzik 		pio_mask <<= 3;
1387c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1388c6fd2807SJeff Garzik 	} else {
1389c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1390c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1391c6fd2807SJeff Garzik 		 * a mask.
1392c6fd2807SJeff Garzik 		 */
13937a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
139446767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
139546767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
139646767aebSAlan Cox 		else
139746767aebSAlan Cox 			pio_mask = 1;
1398c6fd2807SJeff Garzik 
1399c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1400c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1401c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1402c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1403c6fd2807SJeff Garzik 		 * will get this right anyway
1404c6fd2807SJeff Garzik 		 */
1405c6fd2807SJeff Garzik 	}
1406c6fd2807SJeff Garzik 
1407c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1408c6fd2807SJeff Garzik 
1409b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1410b352e57dSAlan Cox 		/*
1411b352e57dSAlan Cox 		 *	Process compact flash extended modes
1412b352e57dSAlan Cox 		 */
141362afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
141462afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1415b352e57dSAlan Cox 
1416b352e57dSAlan Cox 		if (pio)
1417b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1418b352e57dSAlan Cox 		if (pio > 1)
1419b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1420b352e57dSAlan Cox 		if (dma)
1421b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1422b352e57dSAlan Cox 		if (dma > 1)
1423b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1424b352e57dSAlan Cox 	}
1425b352e57dSAlan Cox 
1426c6fd2807SJeff Garzik 	udma_mask = 0;
1427c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1428c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1429c6fd2807SJeff Garzik 
1430c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1431c6fd2807SJeff Garzik }
1432a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_xfermask);
1433c6fd2807SJeff Garzik 
14347102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1435c6fd2807SJeff Garzik {
1436c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1437c6fd2807SJeff Garzik 
1438c6fd2807SJeff Garzik 	complete(waiting);
1439c6fd2807SJeff Garzik }
1440c6fd2807SJeff Garzik 
1441c6fd2807SJeff Garzik /**
14422432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1443c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1444c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1445c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1446e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
14475c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
14482432697bSTejun Heo  *	@n_elem: Number of sg entries
14492b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1450c6fd2807SJeff Garzik  *
1451c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1452c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1453c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1454c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1455c6fd2807SJeff Garzik  *	clean up after timeout.
1456c6fd2807SJeff Garzik  *
1457c6fd2807SJeff Garzik  *	LOCKING:
1458c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1459c6fd2807SJeff Garzik  *
1460c6fd2807SJeff Garzik  *	RETURNS:
1461c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1462c6fd2807SJeff Garzik  */
14632432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1464c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
146587260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
14662b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1467c6fd2807SJeff Garzik {
14689af5c9c9STejun Heo 	struct ata_link *link = dev->link;
14699af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1470c6fd2807SJeff Garzik 	u8 command = tf->command;
147187fbc5a0STejun Heo 	int auto_timeout = 0;
1472c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
147328361c40SJens Axboe 	unsigned int preempted_tag;
1474e3ed8939SJens Axboe 	u32 preempted_sactive;
1475e3ed8939SJens Axboe 	u64 preempted_qc_active;
1476da917d69STejun Heo 	int preempted_nr_active_links;
1477c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1478c6fd2807SJeff Garzik 	unsigned long flags;
1479c6fd2807SJeff Garzik 	unsigned int err_mask;
1480c6fd2807SJeff Garzik 	int rc;
1481c6fd2807SJeff Garzik 
1482c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1483c6fd2807SJeff Garzik 
1484c6fd2807SJeff Garzik 	/* no internal command while frozen */
1485c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1486c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1487c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1488c6fd2807SJeff Garzik 	}
1489c6fd2807SJeff Garzik 
1490c6fd2807SJeff Garzik 	/* initialize internal qc */
149128361c40SJens Axboe 	qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
1492c6fd2807SJeff Garzik 
149328361c40SJens Axboe 	qc->tag = ATA_TAG_INTERNAL;
149428361c40SJens Axboe 	qc->hw_tag = 0;
1495c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1496c6fd2807SJeff Garzik 	qc->ap = ap;
1497c6fd2807SJeff Garzik 	qc->dev = dev;
1498c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1499c6fd2807SJeff Garzik 
15009af5c9c9STejun Heo 	preempted_tag = link->active_tag;
15019af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1502c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1503da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
15049af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
15059af5c9c9STejun Heo 	link->sactive = 0;
1506c6fd2807SJeff Garzik 	ap->qc_active = 0;
1507da917d69STejun Heo 	ap->nr_active_links = 0;
1508c6fd2807SJeff Garzik 
1509c6fd2807SJeff Garzik 	/* prepare & issue qc */
1510c6fd2807SJeff Garzik 	qc->tf = *tf;
1511c6fd2807SJeff Garzik 	if (cdb)
1512c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1513e771451cSVincent Pelletier 
1514e771451cSVincent Pelletier 	/* some SATA bridges need us to indicate data xfer direction */
1515e771451cSVincent Pelletier 	if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1516e771451cSVincent Pelletier 	    dma_dir == DMA_FROM_DEVICE)
1517e771451cSVincent Pelletier 		qc->tf.feature |= ATAPI_DMADIR;
1518e771451cSVincent Pelletier 
1519c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1520c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1521c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
15222432697bSTejun Heo 		unsigned int i, buflen = 0;
152387260216SJens Axboe 		struct scatterlist *sg;
15242432697bSTejun Heo 
152587260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
152687260216SJens Axboe 			buflen += sg->length;
15272432697bSTejun Heo 
152887260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
152949c80429SBrian King 		qc->nbytes = buflen;
1530c6fd2807SJeff Garzik 	}
1531c6fd2807SJeff Garzik 
1532c6fd2807SJeff Garzik 	qc->private_data = &wait;
1533c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1534c6fd2807SJeff Garzik 
1535c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1536c6fd2807SJeff Garzik 
1537c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1538c6fd2807SJeff Garzik 
153987fbc5a0STejun Heo 	if (!timeout) {
154087fbc5a0STejun Heo 		if (ata_probe_timeout)
1541341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
154287fbc5a0STejun Heo 		else {
154387fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
154487fbc5a0STejun Heo 			auto_timeout = 1;
154587fbc5a0STejun Heo 		}
154687fbc5a0STejun Heo 	}
15472b789108STejun Heo 
1548c0c362b6STejun Heo 	if (ap->ops->error_handler)
1549c0c362b6STejun Heo 		ata_eh_release(ap);
1550c0c362b6STejun Heo 
15512b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1552c6fd2807SJeff Garzik 
1553c0c362b6STejun Heo 	if (ap->ops->error_handler)
1554c0c362b6STejun Heo 		ata_eh_acquire(ap);
1555c0c362b6STejun Heo 
1556c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1557c6fd2807SJeff Garzik 
1558c6fd2807SJeff Garzik 	if (!rc) {
1559c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1560c6fd2807SJeff Garzik 
1561c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1562c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1563c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1564c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1565c6fd2807SJeff Garzik 		 */
1566c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1567c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1568c6fd2807SJeff Garzik 
1569c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1570c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1571c6fd2807SJeff Garzik 			else
1572c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1573c6fd2807SJeff Garzik 
1574a9a79dfeSJoe Perches 			ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1575a9a79dfeSJoe Perches 				     command);
1576c6fd2807SJeff Garzik 		}
1577c6fd2807SJeff Garzik 
1578c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1579c6fd2807SJeff Garzik 	}
1580c6fd2807SJeff Garzik 
1581c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1582c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1583c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1584c6fd2807SJeff Garzik 
1585a51d644aSTejun Heo 	/* perform minimal error analysis */
1586a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1587a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1588a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1589a51d644aSTejun Heo 
1590a51d644aSTejun Heo 		if (!qc->err_mask)
1591c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1592a51d644aSTejun Heo 
1593a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1594a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
15952dae9955SDamien Le Moal 	} else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
15962dae9955SDamien Le Moal 		qc->result_tf.command |= ATA_SENSE;
1597c6fd2807SJeff Garzik 	}
1598c6fd2807SJeff Garzik 
1599c6fd2807SJeff Garzik 	/* finish up */
1600c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1601c6fd2807SJeff Garzik 
1602c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1603c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1604c6fd2807SJeff Garzik 
1605c6fd2807SJeff Garzik 	ata_qc_free(qc);
16069af5c9c9STejun Heo 	link->active_tag = preempted_tag;
16079af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1608c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1609da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1610c6fd2807SJeff Garzik 
1611c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1612c6fd2807SJeff Garzik 
161387fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
161487fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
161587fbc5a0STejun Heo 
1616c6fd2807SJeff Garzik 	return err_mask;
1617c6fd2807SJeff Garzik }
1618c6fd2807SJeff Garzik 
1619c6fd2807SJeff Garzik /**
162033480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
16212432697bSTejun Heo  *	@dev: Device to which the command is sent
16222432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
16232432697bSTejun Heo  *	@cdb: CDB for packet command
1624e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
16252432697bSTejun Heo  *	@buf: Data buffer of the command
16262432697bSTejun Heo  *	@buflen: Length of data buffer
16272b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
16282432697bSTejun Heo  *
16292432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
16302432697bSTejun Heo  *	buffer instead of sg list.
16312432697bSTejun Heo  *
16322432697bSTejun Heo  *	LOCKING:
16332432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
16342432697bSTejun Heo  *
16352432697bSTejun Heo  *	RETURNS:
16362432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
16372432697bSTejun Heo  */
16382432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
16392432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
16402b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
16412b789108STejun Heo 			   unsigned long timeout)
16422432697bSTejun Heo {
164333480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
164433480a0eSTejun Heo 	unsigned int n_elem = 0;
16452432697bSTejun Heo 
164633480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
164733480a0eSTejun Heo 		WARN_ON(!buf);
16482432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
164933480a0eSTejun Heo 		psg = &sg;
165033480a0eSTejun Heo 		n_elem++;
165133480a0eSTejun Heo 	}
16522432697bSTejun Heo 
16532b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
16542b789108STejun Heo 				    timeout);
16552432697bSTejun Heo }
16562432697bSTejun Heo 
16572432697bSTejun Heo /**
1658c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1659c6fd2807SJeff Garzik  *	@adev: ATA device
1660c6fd2807SJeff Garzik  *
1661c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1662c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1663c6fd2807SJeff Garzik  */
1664c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1665c6fd2807SJeff Garzik {
16660d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
16670d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
16680d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
16690d9e6659STejun Heo 	 */
16700d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
16710d9e6659STejun Heo 		return 0;
16720d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
16730d9e6659STejun Heo 	 * check as the caller should know this.
16740d9e6659STejun Heo 	 */
16759af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1676c6fd2807SJeff Garzik 		return 0;
16775c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
16785c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
16795c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
16805c18c4d2SDavid Daney 		return 0;
1681432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1682432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1683c6fd2807SJeff Garzik 		return 1;
1684432729f0SAlan Cox 	/* We turn it on when possible */
1685432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1686432729f0SAlan Cox 		return 1;
1687432729f0SAlan Cox 	return 0;
1688432729f0SAlan Cox }
1689a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
1690c6fd2807SJeff Garzik 
1691432729f0SAlan Cox /**
1692432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1693432729f0SAlan Cox  *	@adev: ATA device
1694432729f0SAlan Cox  *
1695432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1696432729f0SAlan Cox  *	-1 if no iordy mode is available.
1697432729f0SAlan Cox  */
1698432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1699432729f0SAlan Cox {
1700c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1701c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1702432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1703c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1704c6fd2807SJeff Garzik 		if (pio) {
1705c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1706c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1707432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1708432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1709c6fd2807SJeff Garzik 		}
1710c6fd2807SJeff Garzik 	}
1711432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1712c6fd2807SJeff Garzik }
1713c6fd2807SJeff Garzik 
1714c6fd2807SJeff Garzik /**
1715963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1716963e4975SAlan Cox  *	@dev: device
1717963e4975SAlan Cox  *	@tf: proposed taskfile
1718963e4975SAlan Cox  *	@id: data buffer
1719963e4975SAlan Cox  *
1720963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1721963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1722963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1723963e4975SAlan Cox  */
1724963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
1725963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
1726963e4975SAlan Cox {
1727963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1728963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1729963e4975SAlan Cox }
1730a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
1731963e4975SAlan Cox 
1732963e4975SAlan Cox /**
1733c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1734c6fd2807SJeff Garzik  *	@dev: target device
1735c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1736bff04647STejun Heo  *	@flags: ATA_READID_* flags
1737c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1738c6fd2807SJeff Garzik  *
1739c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1740c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1741c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1742c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1743c6fd2807SJeff Garzik  *
174450a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
174550a99018SAlan Cox  *	now we abort if we hit that case.
174650a99018SAlan Cox  *
1747c6fd2807SJeff Garzik  *	LOCKING:
1748c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1749c6fd2807SJeff Garzik  *
1750c6fd2807SJeff Garzik  *	RETURNS:
1751c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1752c6fd2807SJeff Garzik  */
1753c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1754bff04647STejun Heo 		    unsigned int flags, u16 *id)
1755c6fd2807SJeff Garzik {
17569af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1757c6fd2807SJeff Garzik 	unsigned int class = *p_class;
1758c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1759c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
1760c6fd2807SJeff Garzik 	const char *reason;
176179b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
176254936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
1763c6fd2807SJeff Garzik 	int rc;
1764c6fd2807SJeff Garzik 
1765c6fd2807SJeff Garzik retry:
1766c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1767c6fd2807SJeff Garzik 
1768c6fd2807SJeff Garzik 	switch (class) {
176979b42babSTejun Heo 	case ATA_DEV_SEMB:
177079b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
1771df561f66SGustavo A. R. Silva 		fallthrough;
1772c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
17739162c657SHannes Reinecke 	case ATA_DEV_ZAC:
1774c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1775c6fd2807SJeff Garzik 		break;
1776c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1777c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1778c6fd2807SJeff Garzik 		break;
1779c6fd2807SJeff Garzik 	default:
1780c6fd2807SJeff Garzik 		rc = -ENODEV;
1781c6fd2807SJeff Garzik 		reason = "unsupported class";
1782c6fd2807SJeff Garzik 		goto err_out;
1783c6fd2807SJeff Garzik 	}
1784c6fd2807SJeff Garzik 
1785c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
178681afe893STejun Heo 
178781afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
178881afe893STejun Heo 	 * sure those are properly initialized.
178981afe893STejun Heo 	 */
179081afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
179181afe893STejun Heo 
179281afe893STejun Heo 	/* Device presence detection is unreliable on some
179381afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
179481afe893STejun Heo 	 */
179581afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1796c6fd2807SJeff Garzik 
1797963e4975SAlan Cox 	if (ap->ops->read_id)
1798963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
1799963e4975SAlan Cox 	else
1800963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
1801963e4975SAlan Cox 
1802c6fd2807SJeff Garzik 	if (err_mask) {
1803800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1804a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
180555a8e2c8STejun Heo 			return -ENOENT;
180655a8e2c8STejun Heo 		}
180755a8e2c8STejun Heo 
180879b42babSTejun Heo 		if (is_semb) {
1809a9a79dfeSJoe Perches 			ata_dev_info(dev,
1810a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
181179b42babSTejun Heo 			/* SEMB is not supported yet */
181279b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
181379b42babSTejun Heo 			return 0;
181479b42babSTejun Heo 		}
181579b42babSTejun Heo 
18161ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
18171ffc151fSTejun Heo 			/* Device or controller might have reported
18181ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
18191ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
18201ffc151fSTejun Heo 			 * aborted by the device.
182154936f8bSTejun Heo 			 */
18221ffc151fSTejun Heo 			if (may_fallback) {
182354936f8bSTejun Heo 				may_fallback = 0;
182454936f8bSTejun Heo 
182554936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
182654936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
182754936f8bSTejun Heo 				else
182854936f8bSTejun Heo 					class = ATA_DEV_ATA;
182954936f8bSTejun Heo 				goto retry;
183054936f8bSTejun Heo 			}
183154936f8bSTejun Heo 
18321ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
18331ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
18341ffc151fSTejun Heo 			 * sometimes with phantom devices.
18351ffc151fSTejun Heo 			 */
1836a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
18371ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
18381ffc151fSTejun Heo 			return -ENOENT;
18391ffc151fSTejun Heo 		}
18401ffc151fSTejun Heo 
1841c6fd2807SJeff Garzik 		rc = -EIO;
1842c6fd2807SJeff Garzik 		reason = "I/O error";
1843c6fd2807SJeff Garzik 		goto err_out;
1844c6fd2807SJeff Garzik 	}
1845c6fd2807SJeff Garzik 
184643c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
18474baa5745SHannes Reinecke 		ata_dev_info(dev, "dumping IDENTIFY data, "
184843c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
184943c9c591STejun Heo 			    class, may_fallback, tried_spinup);
18504baa5745SHannes Reinecke 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
185143c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
185243c9c591STejun Heo 	}
185343c9c591STejun Heo 
185454936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
185554936f8bSTejun Heo 	 * successfully at least once.
185654936f8bSTejun Heo 	 */
185754936f8bSTejun Heo 	may_fallback = 0;
185854936f8bSTejun Heo 
1859c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1860c6fd2807SJeff Garzik 
1861c6fd2807SJeff Garzik 	/* sanity check */
1862c6fd2807SJeff Garzik 	rc = -EINVAL;
18636070068bSAlan Cox 	reason = "device reports invalid type";
18644a3381feSJeff Garzik 
18659162c657SHannes Reinecke 	if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
18664a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
18674a3381feSJeff Garzik 			goto err_out;
1868db63a4c8SAndy Whitcroft 		if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1869db63a4c8SAndy Whitcroft 							ata_id_is_ata(id)) {
1870db63a4c8SAndy Whitcroft 			ata_dev_dbg(dev,
1871db63a4c8SAndy Whitcroft 				"host indicates ignore ATA devices, ignored\n");
1872db63a4c8SAndy Whitcroft 			return -ENOENT;
1873db63a4c8SAndy Whitcroft 		}
18744a3381feSJeff Garzik 	} else {
18754a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1876c6fd2807SJeff Garzik 			goto err_out;
1877c6fd2807SJeff Garzik 	}
1878c6fd2807SJeff Garzik 
1879169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1880169439c2SMark Lord 		tried_spinup = 1;
1881169439c2SMark Lord 		/*
1882169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
1883169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
1884169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
1885169439c2SMark Lord 		 */
1886218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1887fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
1888169439c2SMark Lord 			rc = -EIO;
1889169439c2SMark Lord 			reason = "SPINUP failed";
1890169439c2SMark Lord 			goto err_out;
1891169439c2SMark Lord 		}
1892169439c2SMark Lord 		/*
1893169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
1894169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
1895169439c2SMark Lord 		 */
1896169439c2SMark Lord 		if (id[2] == 0x37c8)
1897169439c2SMark Lord 			goto retry;
1898169439c2SMark Lord 	}
1899169439c2SMark Lord 
19009162c657SHannes Reinecke 	if ((flags & ATA_READID_POSTRESET) &&
19019162c657SHannes Reinecke 	    (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
1902c6fd2807SJeff Garzik 		/*
1903c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
1904c6fd2807SJeff Garzik 		 * SRST RESET
190550a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
190650a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
1907c6fd2807SJeff Garzik 		 * anything else..
1908c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
190950a99018SAlan Cox 		 *
191050a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
1911c9404c9cSAdam Buchbinder 		 * should never trigger.
1912c6fd2807SJeff Garzik 		 */
1913c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1914c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
1915c6fd2807SJeff Garzik 			if (err_mask) {
1916c6fd2807SJeff Garzik 				rc = -EIO;
1917c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
1918c6fd2807SJeff Garzik 				goto err_out;
1919c6fd2807SJeff Garzik 			}
1920c6fd2807SJeff Garzik 
1921c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
1922c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
1923c6fd2807SJeff Garzik 			 */
1924bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
1925c6fd2807SJeff Garzik 			goto retry;
1926c6fd2807SJeff Garzik 		}
1927c6fd2807SJeff Garzik 	}
1928c6fd2807SJeff Garzik 
1929c6fd2807SJeff Garzik 	*p_class = class;
1930c6fd2807SJeff Garzik 
1931c6fd2807SJeff Garzik 	return 0;
1932c6fd2807SJeff Garzik 
1933c6fd2807SJeff Garzik  err_out:
1934a9a79dfeSJoe Perches 	ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1935a9a79dfeSJoe Perches 		     reason, err_mask);
1936c6fd2807SJeff Garzik 	return rc;
1937c6fd2807SJeff Garzik }
1938c6fd2807SJeff Garzik 
1939f01f62c2SChristoph Hellwig /**
1940f01f62c2SChristoph Hellwig  *	ata_read_log_page - read a specific log page
1941f01f62c2SChristoph Hellwig  *	@dev: target device
1942f01f62c2SChristoph Hellwig  *	@log: log to read
1943f01f62c2SChristoph Hellwig  *	@page: page to read
1944f01f62c2SChristoph Hellwig  *	@buf: buffer to store read page
1945f01f62c2SChristoph Hellwig  *	@sectors: number of sectors to read
1946f01f62c2SChristoph Hellwig  *
1947f01f62c2SChristoph Hellwig  *	Read log page using READ_LOG_EXT command.
1948f01f62c2SChristoph Hellwig  *
1949f01f62c2SChristoph Hellwig  *	LOCKING:
1950f01f62c2SChristoph Hellwig  *	Kernel thread context (may sleep).
1951f01f62c2SChristoph Hellwig  *
1952f01f62c2SChristoph Hellwig  *	RETURNS:
1953f01f62c2SChristoph Hellwig  *	0 on success, AC_ERR_* mask otherwise.
1954f01f62c2SChristoph Hellwig  */
1955f01f62c2SChristoph Hellwig unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1956f01f62c2SChristoph Hellwig 			       u8 page, void *buf, unsigned int sectors)
1957f01f62c2SChristoph Hellwig {
1958f01f62c2SChristoph Hellwig 	unsigned long ap_flags = dev->link->ap->flags;
1959f01f62c2SChristoph Hellwig 	struct ata_taskfile tf;
1960f01f62c2SChristoph Hellwig 	unsigned int err_mask;
1961f01f62c2SChristoph Hellwig 	bool dma = false;
1962f01f62c2SChristoph Hellwig 
19634633778bSHannes Reinecke 	ata_dev_dbg(dev, "read log page - log 0x%x, page 0x%x\n", log, page);
1964f01f62c2SChristoph Hellwig 
1965f01f62c2SChristoph Hellwig 	/*
1966f01f62c2SChristoph Hellwig 	 * Return error without actually issuing the command on controllers
1967f01f62c2SChristoph Hellwig 	 * which e.g. lockup on a read log page.
1968f01f62c2SChristoph Hellwig 	 */
1969f01f62c2SChristoph Hellwig 	if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
1970f01f62c2SChristoph Hellwig 		return AC_ERR_DEV;
1971f01f62c2SChristoph Hellwig 
1972f01f62c2SChristoph Hellwig retry:
1973f01f62c2SChristoph Hellwig 	ata_tf_init(dev, &tf);
1974f971a854SReimar Döffinger 	if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) &&
19757cfdfdc8SDamien Le Moal 	    !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
1976f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_DMA_EXT;
1977f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_DMA;
1978f01f62c2SChristoph Hellwig 		dma = true;
1979f01f62c2SChristoph Hellwig 	} else {
1980f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_EXT;
1981f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_PIO;
1982f01f62c2SChristoph Hellwig 		dma = false;
1983f01f62c2SChristoph Hellwig 	}
1984f01f62c2SChristoph Hellwig 	tf.lbal = log;
1985f01f62c2SChristoph Hellwig 	tf.lbam = page;
1986f01f62c2SChristoph Hellwig 	tf.nsect = sectors;
1987f01f62c2SChristoph Hellwig 	tf.hob_nsect = sectors >> 8;
1988f01f62c2SChristoph Hellwig 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1989f01f62c2SChristoph Hellwig 
1990f01f62c2SChristoph Hellwig 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1991f01f62c2SChristoph Hellwig 				     buf, sectors * ATA_SECT_SIZE, 0);
1992f01f62c2SChristoph Hellwig 
1993fc5c8aa7SDamien Le Moal 	if (err_mask) {
1994fc5c8aa7SDamien Le Moal 		if (dma) {
19957cfdfdc8SDamien Le Moal 			dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
1996f01f62c2SChristoph Hellwig 			goto retry;
1997f01f62c2SChristoph Hellwig 		}
199823ef63d5SDamien Le Moal 		ata_dev_err(dev,
199923ef63d5SDamien Le Moal 			    "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n",
200023ef63d5SDamien Le Moal 			    (unsigned int)log, (unsigned int)page, err_mask);
2001fc5c8aa7SDamien Le Moal 	}
2002f01f62c2SChristoph Hellwig 
2003f01f62c2SChristoph Hellwig 	return err_mask;
2004f01f62c2SChristoph Hellwig }
2005f01f62c2SChristoph Hellwig 
2006efe205a3SChristoph Hellwig static bool ata_log_supported(struct ata_device *dev, u8 log)
2007efe205a3SChristoph Hellwig {
2008efe205a3SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2009efe205a3SChristoph Hellwig 
2010efe205a3SChristoph Hellwig 	if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2011efe205a3SChristoph Hellwig 		return false;
2012efe205a3SChristoph Hellwig 	return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2013efe205a3SChristoph Hellwig }
2014efe205a3SChristoph Hellwig 
2015a0fd2454SChristoph Hellwig static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2016a0fd2454SChristoph Hellwig {
2017a0fd2454SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2018a0fd2454SChristoph Hellwig 	unsigned int err, i;
2019a0fd2454SChristoph Hellwig 
2020636f6e2aSDamien Le Moal 	if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG)
2021636f6e2aSDamien Le Moal 		return false;
2022636f6e2aSDamien Le Moal 
2023a0fd2454SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2024636f6e2aSDamien Le Moal 		/*
2025636f6e2aSDamien Le Moal 		 * IDENTIFY DEVICE data log is defined as mandatory starting
2026636f6e2aSDamien Le Moal 		 * with ACS-3 (ATA version 10). Warn about the missing log
2027636f6e2aSDamien Le Moal 		 * for drives which implement this ATA level or above.
2028636f6e2aSDamien Le Moal 		 */
2029636f6e2aSDamien Le Moal 		if (ata_id_major_version(dev->id) >= 10)
2030636f6e2aSDamien Le Moal 			ata_dev_warn(dev,
2031636f6e2aSDamien Le Moal 				"ATA Identify Device Log not supported\n");
2032636f6e2aSDamien Le Moal 		dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG;
2033a0fd2454SChristoph Hellwig 		return false;
2034a0fd2454SChristoph Hellwig 	}
2035a0fd2454SChristoph Hellwig 
2036a0fd2454SChristoph Hellwig 	/*
2037a0fd2454SChristoph Hellwig 	 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2038a0fd2454SChristoph Hellwig 	 * supported.
2039a0fd2454SChristoph Hellwig 	 */
2040a0fd2454SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2041a0fd2454SChristoph Hellwig 				1);
2042fc5c8aa7SDamien Le Moal 	if (err)
2043a0fd2454SChristoph Hellwig 		return false;
2044a0fd2454SChristoph Hellwig 
2045a0fd2454SChristoph Hellwig 	for (i = 0; i < ap->sector_buf[8]; i++) {
2046a0fd2454SChristoph Hellwig 		if (ap->sector_buf[9 + i] == page)
2047a0fd2454SChristoph Hellwig 			return true;
2048a0fd2454SChristoph Hellwig 	}
2049a0fd2454SChristoph Hellwig 
2050a0fd2454SChristoph Hellwig 	return false;
2051a0fd2454SChristoph Hellwig }
2052a0fd2454SChristoph Hellwig 
20539062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
20549062712fSTejun Heo {
20559062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
20569062712fSTejun Heo 	u32 target, target_limit;
20579062712fSTejun Heo 
20589062712fSTejun Heo 	if (!sata_scr_valid(plink))
20599062712fSTejun Heo 		return 0;
20609062712fSTejun Heo 
20619062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
20629062712fSTejun Heo 		target = 1;
20639062712fSTejun Heo 	else
20649062712fSTejun Heo 		return 0;
20659062712fSTejun Heo 
20669062712fSTejun Heo 	target_limit = (1 << target) - 1;
20679062712fSTejun Heo 
20689062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
20699062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
20709062712fSTejun Heo 		return 0;
20719062712fSTejun Heo 
20729062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
20739062712fSTejun Heo 
20749062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
20759062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
20769062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
20779062712fSTejun Heo 	 */
20789062712fSTejun Heo 	if (plink->sata_spd > target) {
2079a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
20809062712fSTejun Heo 			     sata_spd_string(target));
20819062712fSTejun Heo 		return -EAGAIN;
20829062712fSTejun Heo 	}
20839062712fSTejun Heo 	return 0;
20849062712fSTejun Heo }
20859062712fSTejun Heo 
2086c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2087c6fd2807SJeff Garzik {
20889af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
20899ce8e307SJens Axboe 
20909ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
20919ce8e307SJens Axboe 		return 0;
20929ce8e307SJens Axboe 
20939af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2094c6fd2807SJeff Garzik }
2095c6fd2807SJeff Garzik 
20965a233551SHannes Reinecke static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
20975a233551SHannes Reinecke {
20985a233551SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
20995a233551SHannes Reinecke 	unsigned int err_mask;
21005a233551SHannes Reinecke 
2101efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2102efe205a3SChristoph Hellwig 		ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
2103fe5af0ccSHannes Reinecke 		return;
2104fe5af0ccSHannes Reinecke 	}
21055a233551SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
21065a233551SHannes Reinecke 				     0, ap->sector_buf, 1);
2107fc5c8aa7SDamien Le Moal 	if (!err_mask) {
21085a233551SHannes Reinecke 		u8 *cmds = dev->ncq_send_recv_cmds;
21095a233551SHannes Reinecke 
21105a233551SHannes Reinecke 		dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
21115a233551SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
21125a233551SHannes Reinecke 
21135a233551SHannes Reinecke 		if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
21145a233551SHannes Reinecke 			ata_dev_dbg(dev, "disabling queued TRIM support\n");
21155a233551SHannes Reinecke 			cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
21165a233551SHannes Reinecke 				~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
21175a233551SHannes Reinecke 		}
21185a233551SHannes Reinecke 	}
21195a233551SHannes Reinecke }
21205a233551SHannes Reinecke 
2121284b3b77SHannes Reinecke static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2122284b3b77SHannes Reinecke {
2123284b3b77SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
2124284b3b77SHannes Reinecke 	unsigned int err_mask;
2125284b3b77SHannes Reinecke 
2126efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
2127284b3b77SHannes Reinecke 		ata_dev_warn(dev,
2128284b3b77SHannes Reinecke 			     "NCQ Send/Recv Log not supported\n");
2129284b3b77SHannes Reinecke 		return;
2130284b3b77SHannes Reinecke 	}
2131284b3b77SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2132284b3b77SHannes Reinecke 				     0, ap->sector_buf, 1);
2133fc5c8aa7SDamien Le Moal 	if (!err_mask) {
2134284b3b77SHannes Reinecke 		u8 *cmds = dev->ncq_non_data_cmds;
2135284b3b77SHannes Reinecke 
2136284b3b77SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2137284b3b77SHannes Reinecke 	}
2138284b3b77SHannes Reinecke }
2139284b3b77SHannes Reinecke 
21408e061784SAdam Manzanares static void ata_dev_config_ncq_prio(struct ata_device *dev)
21418e061784SAdam Manzanares {
21428e061784SAdam Manzanares 	struct ata_port *ap = dev->link->ap;
21438e061784SAdam Manzanares 	unsigned int err_mask;
21448e061784SAdam Manzanares 
214506f6c4c6SDamien Le Moal 	if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
214606f6c4c6SDamien Le Moal 		return;
214706f6c4c6SDamien Le Moal 
21488e061784SAdam Manzanares 	err_mask = ata_read_log_page(dev,
21491d51d5f3SChristoph Hellwig 				     ATA_LOG_IDENTIFY_DEVICE,
21508e061784SAdam Manzanares 				     ATA_LOG_SATA_SETTINGS,
21518e061784SAdam Manzanares 				     ap->sector_buf,
21528e061784SAdam Manzanares 				     1);
2153fc5c8aa7SDamien Le Moal 	if (err_mask)
21542360fa18SDamien Le Moal 		goto not_supported;
21558e061784SAdam Manzanares 
21562360fa18SDamien Le Moal 	if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
21572360fa18SDamien Le Moal 		goto not_supported;
21582360fa18SDamien Le Moal 
21598e061784SAdam Manzanares 	dev->flags |= ATA_DFLAG_NCQ_PRIO;
21608e061784SAdam Manzanares 
21612360fa18SDamien Le Moal 	return;
21622360fa18SDamien Le Moal 
21632360fa18SDamien Le Moal not_supported:
21642360fa18SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
21652360fa18SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
21668e061784SAdam Manzanares }
21678e061784SAdam Manzanares 
21687a8526a5SKate Hsuan static bool ata_dev_check_adapter(struct ata_device *dev,
21697a8526a5SKate Hsuan 				  unsigned short vendor_id)
21707a8526a5SKate Hsuan {
21717a8526a5SKate Hsuan 	struct pci_dev *pcidev = NULL;
21727a8526a5SKate Hsuan 	struct device *parent_dev = NULL;
21737a8526a5SKate Hsuan 
21747a8526a5SKate Hsuan 	for (parent_dev = dev->tdev.parent; parent_dev != NULL;
21757a8526a5SKate Hsuan 	     parent_dev = parent_dev->parent) {
21767a8526a5SKate Hsuan 		if (dev_is_pci(parent_dev)) {
21777a8526a5SKate Hsuan 			pcidev = to_pci_dev(parent_dev);
21787a8526a5SKate Hsuan 			if (pcidev->vendor == vendor_id)
21797a8526a5SKate Hsuan 				return true;
21807a8526a5SKate Hsuan 			break;
21817a8526a5SKate Hsuan 		}
21827a8526a5SKate Hsuan 	}
21837a8526a5SKate Hsuan 
21847a8526a5SKate Hsuan 	return false;
21857a8526a5SKate Hsuan }
21867a8526a5SKate Hsuan 
2187388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2188c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2189c6fd2807SJeff Garzik {
21909af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2191c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2192388539f3SShaohua Li 	unsigned int err_mask;
2193388539f3SShaohua Li 	char *aa_desc = "";
2194c6fd2807SJeff Garzik 
2195c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2196c6fd2807SJeff Garzik 		desc[0] = '\0';
2197388539f3SShaohua Li 		return 0;
2198c6fd2807SJeff Garzik 	}
2199cba97ea1SBartlomiej Zolnierkiewicz 	if (!IS_ENABLED(CONFIG_SATA_HOST))
2200cba97ea1SBartlomiej Zolnierkiewicz 		return 0;
220175683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
22026919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2203388539f3SShaohua Li 		return 0;
22046919a0a6SAlan Cox 	}
22057a8526a5SKate Hsuan 
22067a8526a5SKate Hsuan 	if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI &&
22077a8526a5SKate Hsuan 	    ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) {
22087a8526a5SKate Hsuan 		snprintf(desc, desc_sz, "NCQ (not used)");
22097a8526a5SKate Hsuan 		return 0;
22107a8526a5SKate Hsuan 	}
22117a8526a5SKate Hsuan 
2212c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
221369278f79SJens Axboe 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
2214c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2215c6fd2807SJeff Garzik 	}
2216c6fd2807SJeff Garzik 
2217388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2218388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2219388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2220388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2221388539f3SShaohua Li 			SATA_FPDMA_AA);
2222388539f3SShaohua Li 		if (err_mask) {
2223a9a79dfeSJoe Perches 			ata_dev_err(dev,
2224a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2225a9a79dfeSJoe Perches 				    err_mask);
2226388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2227388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2228388539f3SShaohua Li 				return -EIO;
2229388539f3SShaohua Li 			}
2230388539f3SShaohua Li 		} else
2231388539f3SShaohua Li 			aa_desc = ", AA";
2232388539f3SShaohua Li 	}
2233388539f3SShaohua Li 
2234c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2235388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2236c6fd2807SJeff Garzik 	else
2237388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2238388539f3SShaohua Li 			ddepth, aa_desc);
2239ed36911cSMarc Carino 
2240284b3b77SHannes Reinecke 	if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2241284b3b77SHannes Reinecke 		if (ata_id_has_ncq_send_and_recv(dev->id))
22425a233551SHannes Reinecke 			ata_dev_config_ncq_send_recv(dev);
2243284b3b77SHannes Reinecke 		if (ata_id_has_ncq_non_data(dev->id))
2244284b3b77SHannes Reinecke 			ata_dev_config_ncq_non_data(dev);
22458e061784SAdam Manzanares 		if (ata_id_has_ncq_prio(dev->id))
22468e061784SAdam Manzanares 			ata_dev_config_ncq_prio(dev);
2247ed36911cSMarc Carino 	}
2248ed36911cSMarc Carino 
2249388539f3SShaohua Li 	return 0;
2250c6fd2807SJeff Garzik }
2251c6fd2807SJeff Garzik 
2252e87fd28cSHannes Reinecke static void ata_dev_config_sense_reporting(struct ata_device *dev)
2253e87fd28cSHannes Reinecke {
2254e87fd28cSHannes Reinecke 	unsigned int err_mask;
2255e87fd28cSHannes Reinecke 
2256e87fd28cSHannes Reinecke 	if (!ata_id_has_sense_reporting(dev->id))
2257e87fd28cSHannes Reinecke 		return;
2258e87fd28cSHannes Reinecke 
2259e87fd28cSHannes Reinecke 	if (ata_id_sense_reporting_enabled(dev->id))
2260e87fd28cSHannes Reinecke 		return;
2261e87fd28cSHannes Reinecke 
2262e87fd28cSHannes Reinecke 	err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2263e87fd28cSHannes Reinecke 	if (err_mask) {
2264e87fd28cSHannes Reinecke 		ata_dev_dbg(dev,
2265e87fd28cSHannes Reinecke 			    "failed to enable Sense Data Reporting, Emask 0x%x\n",
2266e87fd28cSHannes Reinecke 			    err_mask);
2267e87fd28cSHannes Reinecke 	}
2268e87fd28cSHannes Reinecke }
2269e87fd28cSHannes Reinecke 
22706d1003aeSHannes Reinecke static void ata_dev_config_zac(struct ata_device *dev)
22716d1003aeSHannes Reinecke {
22726d1003aeSHannes Reinecke 	struct ata_port *ap = dev->link->ap;
22736d1003aeSHannes Reinecke 	unsigned int err_mask;
22746d1003aeSHannes Reinecke 	u8 *identify_buf = ap->sector_buf;
22756d1003aeSHannes Reinecke 
22766d1003aeSHannes Reinecke 	dev->zac_zones_optimal_open = U32_MAX;
22776d1003aeSHannes Reinecke 	dev->zac_zones_optimal_nonseq = U32_MAX;
22786d1003aeSHannes Reinecke 	dev->zac_zones_max_open = U32_MAX;
22796d1003aeSHannes Reinecke 
22806d1003aeSHannes Reinecke 	/*
22816d1003aeSHannes Reinecke 	 * Always set the 'ZAC' flag for Host-managed devices.
22826d1003aeSHannes Reinecke 	 */
22836d1003aeSHannes Reinecke 	if (dev->class == ATA_DEV_ZAC)
22846d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
22856d1003aeSHannes Reinecke 	else if (ata_id_zoned_cap(dev->id) == 0x01)
22866d1003aeSHannes Reinecke 		/*
22876d1003aeSHannes Reinecke 		 * Check for host-aware devices.
22886d1003aeSHannes Reinecke 		 */
22896d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
22906d1003aeSHannes Reinecke 
22916d1003aeSHannes Reinecke 	if (!(dev->flags & ATA_DFLAG_ZAC))
22926d1003aeSHannes Reinecke 		return;
22936d1003aeSHannes Reinecke 
2294a0fd2454SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
22956d1003aeSHannes Reinecke 		ata_dev_warn(dev,
22966d1003aeSHannes Reinecke 			     "ATA Zoned Information Log not supported\n");
22976d1003aeSHannes Reinecke 		return;
22986d1003aeSHannes Reinecke 	}
22996d1003aeSHannes Reinecke 
23006d1003aeSHannes Reinecke 	/*
23016d1003aeSHannes Reinecke 	 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
23026d1003aeSHannes Reinecke 	 */
23031d51d5f3SChristoph Hellwig 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
23046d1003aeSHannes Reinecke 				     ATA_LOG_ZONED_INFORMATION,
23056d1003aeSHannes Reinecke 				     identify_buf, 1);
23066d1003aeSHannes Reinecke 	if (!err_mask) {
23076d1003aeSHannes Reinecke 		u64 zoned_cap, opt_open, opt_nonseq, max_open;
23086d1003aeSHannes Reinecke 
23096d1003aeSHannes Reinecke 		zoned_cap = get_unaligned_le64(&identify_buf[8]);
23106d1003aeSHannes Reinecke 		if ((zoned_cap >> 63))
23116d1003aeSHannes Reinecke 			dev->zac_zoned_cap = (zoned_cap & 1);
23126d1003aeSHannes Reinecke 		opt_open = get_unaligned_le64(&identify_buf[24]);
23136d1003aeSHannes Reinecke 		if ((opt_open >> 63))
23146d1003aeSHannes Reinecke 			dev->zac_zones_optimal_open = (u32)opt_open;
23156d1003aeSHannes Reinecke 		opt_nonseq = get_unaligned_le64(&identify_buf[32]);
23166d1003aeSHannes Reinecke 		if ((opt_nonseq >> 63))
23176d1003aeSHannes Reinecke 			dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
23186d1003aeSHannes Reinecke 		max_open = get_unaligned_le64(&identify_buf[40]);
23196d1003aeSHannes Reinecke 		if ((max_open >> 63))
23206d1003aeSHannes Reinecke 			dev->zac_zones_max_open = (u32)max_open;
23216d1003aeSHannes Reinecke 	}
23226d1003aeSHannes Reinecke }
23236d1003aeSHannes Reinecke 
2324818831c8SChristoph Hellwig static void ata_dev_config_trusted(struct ata_device *dev)
2325818831c8SChristoph Hellwig {
2326818831c8SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2327818831c8SChristoph Hellwig 	u64 trusted_cap;
2328818831c8SChristoph Hellwig 	unsigned int err;
2329818831c8SChristoph Hellwig 
2330e8f11db9SChristoph Hellwig 	if (!ata_id_has_trusted(dev->id))
2331e8f11db9SChristoph Hellwig 		return;
2332e8f11db9SChristoph Hellwig 
2333818831c8SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2334818831c8SChristoph Hellwig 		ata_dev_warn(dev,
2335818831c8SChristoph Hellwig 			     "Security Log not supported\n");
2336818831c8SChristoph Hellwig 		return;
2337818831c8SChristoph Hellwig 	}
2338818831c8SChristoph Hellwig 
2339818831c8SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2340818831c8SChristoph Hellwig 			ap->sector_buf, 1);
2341fc5c8aa7SDamien Le Moal 	if (err)
2342818831c8SChristoph Hellwig 		return;
2343818831c8SChristoph Hellwig 
2344818831c8SChristoph Hellwig 	trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2345818831c8SChristoph Hellwig 	if (!(trusted_cap & (1ULL << 63))) {
2346818831c8SChristoph Hellwig 		ata_dev_dbg(dev,
2347818831c8SChristoph Hellwig 			    "Trusted Computing capability qword not valid!\n");
2348818831c8SChristoph Hellwig 		return;
2349818831c8SChristoph Hellwig 	}
2350818831c8SChristoph Hellwig 
2351818831c8SChristoph Hellwig 	if (trusted_cap & (1 << 0))
2352818831c8SChristoph Hellwig 		dev->flags |= ATA_DFLAG_TRUSTED;
2353818831c8SChristoph Hellwig }
2354818831c8SChristoph Hellwig 
2355891fd7c6SDamien Le Moal static int ata_dev_config_lba(struct ata_device *dev)
2356891fd7c6SDamien Le Moal {
2357891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2358891fd7c6SDamien Le Moal 	const char *lba_desc;
2359891fd7c6SDamien Le Moal 	char ncq_desc[24];
2360891fd7c6SDamien Le Moal 	int ret;
2361891fd7c6SDamien Le Moal 
2362891fd7c6SDamien Le Moal 	dev->flags |= ATA_DFLAG_LBA;
2363891fd7c6SDamien Le Moal 
2364891fd7c6SDamien Le Moal 	if (ata_id_has_lba48(id)) {
2365891fd7c6SDamien Le Moal 		lba_desc = "LBA48";
2366891fd7c6SDamien Le Moal 		dev->flags |= ATA_DFLAG_LBA48;
2367891fd7c6SDamien Le Moal 		if (dev->n_sectors >= (1UL << 28) &&
2368891fd7c6SDamien Le Moal 		    ata_id_has_flush_ext(id))
2369891fd7c6SDamien Le Moal 			dev->flags |= ATA_DFLAG_FLUSH_EXT;
2370891fd7c6SDamien Le Moal 	} else {
2371891fd7c6SDamien Le Moal 		lba_desc = "LBA";
2372891fd7c6SDamien Le Moal 	}
2373891fd7c6SDamien Le Moal 
2374891fd7c6SDamien Le Moal 	/* config NCQ */
2375891fd7c6SDamien Le Moal 	ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2376891fd7c6SDamien Le Moal 
2377891fd7c6SDamien Le Moal 	/* print device info to dmesg */
2378*1c95a27cSHannes Reinecke 	if (ata_dev_print_info(dev))
2379891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2380891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u: %s %s\n",
2381891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2382891fd7c6SDamien Le Moal 			     dev->multi_count, lba_desc, ncq_desc);
2383891fd7c6SDamien Le Moal 
2384891fd7c6SDamien Le Moal 	return ret;
2385891fd7c6SDamien Le Moal }
2386891fd7c6SDamien Le Moal 
2387891fd7c6SDamien Le Moal static void ata_dev_config_chs(struct ata_device *dev)
2388891fd7c6SDamien Le Moal {
2389891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2390891fd7c6SDamien Le Moal 
2391891fd7c6SDamien Le Moal 	if (ata_id_current_chs_valid(id)) {
2392891fd7c6SDamien Le Moal 		/* Current CHS translation is valid. */
2393891fd7c6SDamien Le Moal 		dev->cylinders = id[54];
2394891fd7c6SDamien Le Moal 		dev->heads     = id[55];
2395891fd7c6SDamien Le Moal 		dev->sectors   = id[56];
2396891fd7c6SDamien Le Moal 	} else {
2397891fd7c6SDamien Le Moal 		/* Default translation */
2398891fd7c6SDamien Le Moal 		dev->cylinders	= id[1];
2399891fd7c6SDamien Le Moal 		dev->heads	= id[3];
2400891fd7c6SDamien Le Moal 		dev->sectors	= id[6];
2401891fd7c6SDamien Le Moal 	}
2402891fd7c6SDamien Le Moal 
2403891fd7c6SDamien Le Moal 	/* print device info to dmesg */
2404*1c95a27cSHannes Reinecke 	if (ata_dev_print_info(dev))
2405891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2406891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u, CHS %u/%u/%u\n",
2407891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2408891fd7c6SDamien Le Moal 			     dev->multi_count, dev->cylinders,
2409891fd7c6SDamien Le Moal 			     dev->heads, dev->sectors);
2410891fd7c6SDamien Le Moal }
2411891fd7c6SDamien Le Moal 
2412d8d8778cSDamien Le Moal static void ata_dev_config_devslp(struct ata_device *dev)
2413d8d8778cSDamien Le Moal {
2414d8d8778cSDamien Le Moal 	u8 *sata_setting = dev->link->ap->sector_buf;
2415d8d8778cSDamien Le Moal 	unsigned int err_mask;
2416d8d8778cSDamien Le Moal 	int i, j;
2417d8d8778cSDamien Le Moal 
2418d8d8778cSDamien Le Moal 	/*
2419d8d8778cSDamien Le Moal 	 * Check device sleep capability. Get DevSlp timing variables
2420d8d8778cSDamien Le Moal 	 * from SATA Settings page of Identify Device Data Log.
2421d8d8778cSDamien Le Moal 	 */
242206f6c4c6SDamien Le Moal 	if (!ata_id_has_devslp(dev->id) ||
242306f6c4c6SDamien Le Moal 	    !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
2424d8d8778cSDamien Le Moal 		return;
2425d8d8778cSDamien Le Moal 
2426d8d8778cSDamien Le Moal 	err_mask = ata_read_log_page(dev,
2427d8d8778cSDamien Le Moal 				     ATA_LOG_IDENTIFY_DEVICE,
2428d8d8778cSDamien Le Moal 				     ATA_LOG_SATA_SETTINGS,
2429d8d8778cSDamien Le Moal 				     sata_setting, 1);
2430fc5c8aa7SDamien Le Moal 	if (err_mask)
2431d8d8778cSDamien Le Moal 		return;
2432d8d8778cSDamien Le Moal 
2433d8d8778cSDamien Le Moal 	dev->flags |= ATA_DFLAG_DEVSLP;
2434d8d8778cSDamien Le Moal 	for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2435d8d8778cSDamien Le Moal 		j = ATA_LOG_DEVSLP_OFFSET + i;
2436d8d8778cSDamien Le Moal 		dev->devslp_timing[i] = sata_setting[j];
2437d8d8778cSDamien Le Moal 	}
2438d8d8778cSDamien Le Moal }
2439d8d8778cSDamien Le Moal 
2440fe22e1c2SDamien Le Moal static void ata_dev_config_cpr(struct ata_device *dev)
2441fe22e1c2SDamien Le Moal {
2442fe22e1c2SDamien Le Moal 	unsigned int err_mask;
2443fe22e1c2SDamien Le Moal 	size_t buf_len;
2444fe22e1c2SDamien Le Moal 	int i, nr_cpr = 0;
2445fe22e1c2SDamien Le Moal 	struct ata_cpr_log *cpr_log = NULL;
2446fe22e1c2SDamien Le Moal 	u8 *desc, *buf = NULL;
2447fe22e1c2SDamien Le Moal 
2448fe22e1c2SDamien Le Moal 	if (!ata_identify_page_supported(dev,
2449fe22e1c2SDamien Le Moal 				 ATA_LOG_CONCURRENT_POSITIONING_RANGES))
2450fe22e1c2SDamien Le Moal 		goto out;
2451fe22e1c2SDamien Le Moal 
2452fe22e1c2SDamien Le Moal 	/*
2453fe22e1c2SDamien Le Moal 	 * Read IDENTIFY DEVICE data log, page 0x47
2454fe22e1c2SDamien Le Moal 	 * (concurrent positioning ranges). We can have at most 255 32B range
2455fe22e1c2SDamien Le Moal 	 * descriptors plus a 64B header.
2456fe22e1c2SDamien Le Moal 	 */
2457fe22e1c2SDamien Le Moal 	buf_len = (64 + 255 * 32 + 511) & ~511;
2458fe22e1c2SDamien Le Moal 	buf = kzalloc(buf_len, GFP_KERNEL);
2459fe22e1c2SDamien Le Moal 	if (!buf)
2460fe22e1c2SDamien Le Moal 		goto out;
2461fe22e1c2SDamien Le Moal 
2462fe22e1c2SDamien Le Moal 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
2463fe22e1c2SDamien Le Moal 				     ATA_LOG_CONCURRENT_POSITIONING_RANGES,
2464fe22e1c2SDamien Le Moal 				     buf, buf_len >> 9);
2465fe22e1c2SDamien Le Moal 	if (err_mask)
2466fe22e1c2SDamien Le Moal 		goto out;
2467fe22e1c2SDamien Le Moal 
2468fe22e1c2SDamien Le Moal 	nr_cpr = buf[0];
2469fe22e1c2SDamien Le Moal 	if (!nr_cpr)
2470fe22e1c2SDamien Le Moal 		goto out;
2471fe22e1c2SDamien Le Moal 
2472fe22e1c2SDamien Le Moal 	cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL);
2473fe22e1c2SDamien Le Moal 	if (!cpr_log)
2474fe22e1c2SDamien Le Moal 		goto out;
2475fe22e1c2SDamien Le Moal 
2476fe22e1c2SDamien Le Moal 	cpr_log->nr_cpr = nr_cpr;
2477fe22e1c2SDamien Le Moal 	desc = &buf[64];
2478fe22e1c2SDamien Le Moal 	for (i = 0; i < nr_cpr; i++, desc += 32) {
2479fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num = desc[0];
2480fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num_storage_elements = desc[1];
2481fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]);
2482fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]);
2483fe22e1c2SDamien Le Moal 	}
2484fe22e1c2SDamien Le Moal 
2485fe22e1c2SDamien Le Moal out:
2486fe22e1c2SDamien Le Moal 	swap(dev->cpr_log, cpr_log);
2487fe22e1c2SDamien Le Moal 	kfree(cpr_log);
2488fe22e1c2SDamien Le Moal 	kfree(buf);
2489fe22e1c2SDamien Le Moal }
2490fe22e1c2SDamien Le Moal 
2491d633b8a7SDamien Le Moal static void ata_dev_print_features(struct ata_device *dev)
2492d633b8a7SDamien Le Moal {
2493d633b8a7SDamien Le Moal 	if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
2494d633b8a7SDamien Le Moal 		return;
2495d633b8a7SDamien Le Moal 
2496d633b8a7SDamien Le Moal 	ata_dev_info(dev,
2497fe22e1c2SDamien Le Moal 		     "Features:%s%s%s%s%s%s\n",
2498d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
2499d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
2500d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
2501d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
2502fe22e1c2SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "",
2503fe22e1c2SDamien Le Moal 		     dev->cpr_log ? " CPR" : "");
2504d633b8a7SDamien Le Moal }
2505d633b8a7SDamien Le Moal 
2506c6fd2807SJeff Garzik /**
2507c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2508c6fd2807SJeff Garzik  *	@dev: Target device to configure
2509c6fd2807SJeff Garzik  *
2510c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2511c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2512c6fd2807SJeff Garzik  *
2513c6fd2807SJeff Garzik  *	LOCKING:
2514c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2515c6fd2807SJeff Garzik  *
2516c6fd2807SJeff Garzik  *	RETURNS:
2517c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2518c6fd2807SJeff Garzik  */
2519efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2520c6fd2807SJeff Garzik {
25219af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2522891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
2523c6fd2807SJeff Garzik 	const u16 *id = dev->id;
25247dc951aeSTejun Heo 	unsigned long xfer_mask;
252565fe1f0fSShane Huang 	unsigned int err_mask;
2526b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
25273f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
25283f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2529c6fd2807SJeff Garzik 	int rc;
2530c6fd2807SJeff Garzik 
253196c810f2SHannes Reinecke 	if (!ata_dev_enabled(dev)) {
253296c810f2SHannes Reinecke 		ata_dev_dbg(dev, "no device\n");
2533c6fd2807SJeff Garzik 		return 0;
2534c6fd2807SJeff Garzik 	}
2535c6fd2807SJeff Garzik 
253675683fe7STejun Heo 	/* set horkage */
253775683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
253833267325STejun Heo 	ata_force_horkage(dev);
253975683fe7STejun Heo 
254050af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2541a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
254250af2fa1STejun Heo 		ata_dev_disable(dev);
254350af2fa1STejun Heo 		return 0;
254450af2fa1STejun Heo 	}
254550af2fa1STejun Heo 
25462486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
25472486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2548a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
25492486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
25502486fa56STejun Heo 			     : "disabled");
25512486fa56STejun Heo 		ata_dev_disable(dev);
25522486fa56STejun Heo 		return 0;
25532486fa56STejun Heo 	}
25542486fa56STejun Heo 
25559062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
25569062712fSTejun Heo 	if (rc)
25579062712fSTejun Heo 		return rc;
25589062712fSTejun Heo 
2559ecd75ad5STejun Heo 	/* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2560ecd75ad5STejun Heo 	if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2561ecd75ad5STejun Heo 	    (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2562ecd75ad5STejun Heo 		dev->horkage |= ATA_HORKAGE_NOLPM;
2563ecd75ad5STejun Heo 
2564240630e6SHans de Goede 	if (ap->flags & ATA_FLAG_NO_LPM)
2565240630e6SHans de Goede 		dev->horkage |= ATA_HORKAGE_NOLPM;
2566240630e6SHans de Goede 
2567ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2568ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2569ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2570ecd75ad5STejun Heo 	}
2571ecd75ad5STejun Heo 
25726746544cSTejun Heo 	/* let ACPI work its magic */
25736746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
25746746544cSTejun Heo 	if (rc)
25756746544cSTejun Heo 		return rc;
257608573a86SKristen Carlson Accardi 
257705027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
257805027adcSTejun Heo 	rc = ata_hpa_resize(dev);
257905027adcSTejun Heo 	if (rc)
258005027adcSTejun Heo 		return rc;
258105027adcSTejun Heo 
2582c6fd2807SJeff Garzik 	/* print device capabilities */
2583a9a79dfeSJoe Perches 	ata_dev_dbg(dev,
2584c6fd2807SJeff Garzik 		    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2585c6fd2807SJeff Garzik 		    "85:%04x 86:%04x 87:%04x 88:%04x\n",
25867f5e4e8dSHarvey Harrison 		    __func__,
2587c6fd2807SJeff Garzik 		    id[49], id[82], id[83], id[84],
2588c6fd2807SJeff Garzik 		    id[85], id[86], id[87], id[88]);
2589c6fd2807SJeff Garzik 
2590c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2591c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2592c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2593c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2594c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2595c6fd2807SJeff Garzik 	dev->cylinders = 0;
2596c6fd2807SJeff Garzik 	dev->heads = 0;
2597c6fd2807SJeff Garzik 	dev->sectors = 0;
2598e18086d6SMark Lord 	dev->multi_count = 0;
2599c6fd2807SJeff Garzik 
2600c6fd2807SJeff Garzik 	/*
2601c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2602c6fd2807SJeff Garzik 	 */
2603c6fd2807SJeff Garzik 
2604c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2605c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2606c6fd2807SJeff Garzik 
26076044f3c4SHannes Reinecke 	ata_dump_id(dev, id);
2608c6fd2807SJeff Garzik 
2609ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2610ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2611ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2612ef143d57SAlbert Lee 
2613ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2614ef143d57SAlbert Lee 			sizeof(modelbuf));
2615ef143d57SAlbert Lee 
2616c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
26179162c657SHannes Reinecke 	if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2618b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
261962afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
262062afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2621a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2622a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2623b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2624ae8d4ee7SAlan Cox 		} else {
2625b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2626ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2627ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2628a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2629a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2630ae8d4ee7SAlan Cox 		}
2631b352e57dSAlan Cox 
2632c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2633c6fd2807SJeff Garzik 
2634e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2635e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2636e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2637e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2638e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2639e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2640e18086d6SMark Lord 				if (cnt <= max)
2641e18086d6SMark Lord 					dev->multi_count = cnt;
2642e18086d6SMark Lord 		}
26433f64f565SEric D. Mudama 
2644891fd7c6SDamien Le Moal 		/* print device info to dmesg */
2645*1c95a27cSHannes Reinecke 		if (print_info)
2646891fd7c6SDamien Le Moal 			ata_dev_info(dev, "%s: %s, %s, max %s\n",
2647891fd7c6SDamien Le Moal 				     revbuf, modelbuf, fwrevbuf,
2648891fd7c6SDamien Le Moal 				     ata_mode_string(xfer_mask));
2649891fd7c6SDamien Le Moal 
2650c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2651891fd7c6SDamien Le Moal 			rc = ata_dev_config_lba(dev);
2652388539f3SShaohua Li 			if (rc)
2653388539f3SShaohua Li 				return rc;
2654c6fd2807SJeff Garzik 		} else {
2655891fd7c6SDamien Le Moal 			ata_dev_config_chs(dev);
2656c6fd2807SJeff Garzik 		}
2657c6fd2807SJeff Garzik 
2658d8d8778cSDamien Le Moal 		ata_dev_config_devslp(dev);
2659e87fd28cSHannes Reinecke 		ata_dev_config_sense_reporting(dev);
26606d1003aeSHannes Reinecke 		ata_dev_config_zac(dev);
2661818831c8SChristoph Hellwig 		ata_dev_config_trusted(dev);
2662fe22e1c2SDamien Le Moal 		ata_dev_config_cpr(dev);
2663b1ffbf85SMinwoo Im 		dev->cdb_len = 32;
2664d633b8a7SDamien Le Moal 
2665*1c95a27cSHannes Reinecke 		if (print_info)
2666d633b8a7SDamien Le Moal 			ata_dev_print_features(dev);
2667c6fd2807SJeff Garzik 	}
2668c6fd2807SJeff Garzik 
2669c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2670c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2671854c73a2STejun Heo 		const char *cdb_intr_string = "";
2672854c73a2STejun Heo 		const char *atapi_an_string = "";
267391163006STejun Heo 		const char *dma_dir_string = "";
26747d77b247STejun Heo 		u32 sntf;
2675c6fd2807SJeff Garzik 
2676c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2677c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
267816d42467SHannes Reinecke 			ata_dev_warn(dev, "unsupported CDB len %d\n", rc);
2679c6fd2807SJeff Garzik 			rc = -EINVAL;
2680c6fd2807SJeff Garzik 			goto err_out_nosup;
2681c6fd2807SJeff Garzik 		}
2682c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2683c6fd2807SJeff Garzik 
26847d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
26857d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
26867d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
26877d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
26889f45cbd3SKristen Carlson Accardi 		 */
2689e7ecd435STejun Heo 		if (atapi_an &&
2690e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2691071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
26927d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
26939f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2694218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2695218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2696854c73a2STejun Heo 			if (err_mask)
2697a9a79dfeSJoe Perches 				ata_dev_err(dev,
2698a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2699a9a79dfeSJoe Perches 					    err_mask);
2700854c73a2STejun Heo 			else {
27019f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2702854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2703854c73a2STejun Heo 			}
27049f45cbd3SKristen Carlson Accardi 		}
27059f45cbd3SKristen Carlson Accardi 
2706c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2707c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2708c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2709c6fd2807SJeff Garzik 		}
2710c6fd2807SJeff Garzik 
2711966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
271291163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
271391163006STejun Heo 			dma_dir_string = ", DMADIR";
271491163006STejun Heo 		}
271591163006STejun Heo 
2716afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
2717b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2718afe75951SAaron Lu 			zpodd_init(dev);
2719afe75951SAaron Lu 		}
2720b1354cbbSLin Ming 
2721c6fd2807SJeff Garzik 		/* print device info to dmesg */
2722*1c95a27cSHannes Reinecke 		if (print_info)
2723a9a79dfeSJoe Perches 			ata_dev_info(dev,
272491163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2725ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2726c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
272791163006STejun Heo 				     cdb_intr_string, atapi_an_string,
272891163006STejun Heo 				     dma_dir_string);
2729c6fd2807SJeff Garzik 	}
2730c6fd2807SJeff Garzik 
2731914ed354STejun Heo 	/* determine max_sectors */
2732914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2733914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2734914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2735914ed354STejun Heo 
2736c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2737c5038fc0SAlan Cox 	   200 sectors */
2738c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2739*1c95a27cSHannes Reinecke 		if (print_info)
2740a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2741c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2742c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2743c6fd2807SJeff Garzik 	}
2744c6fd2807SJeff Garzik 
2745f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2746f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2747f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2748f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2749f442cd86SAlbert Lee 	}
2750f8d8e579STony Battersby 
275175683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
275203ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
275303ec52deSTejun Heo 					 dev->max_sectors);
275418d6e9d5SAlbert Lee 
2755af34d637SDavid Milburn 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2756af34d637SDavid Milburn 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2757af34d637SDavid Milburn 					 dev->max_sectors);
2758af34d637SDavid Milburn 
2759a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2760a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2761a32450e1SShan Hai 
2762c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2763cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2764c6fd2807SJeff Garzik 
2765c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2766c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2767c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2768c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2769c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2770c5038fc0SAlan Cox 		   bugs */
2771c5038fc0SAlan Cox 
2772c5038fc0SAlan Cox 		if (print_info) {
2773a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2774c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2775a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2776c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2777c5038fc0SAlan Cox 		}
2778c5038fc0SAlan Cox 	}
2779c5038fc0SAlan Cox 
2780ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2781a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2782a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2783ac70a964STejun Heo 	}
2784ac70a964STejun Heo 
2785c6fd2807SJeff Garzik 	return 0;
2786c6fd2807SJeff Garzik 
2787c6fd2807SJeff Garzik err_out_nosup:
2788c6fd2807SJeff Garzik 	return rc;
2789c6fd2807SJeff Garzik }
2790c6fd2807SJeff Garzik 
2791c6fd2807SJeff Garzik /**
27922e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2793be0d18dfSAlan Cox  *	@ap: port
2794be0d18dfSAlan Cox  *
27952e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2796be0d18dfSAlan Cox  *	detection.
2797be0d18dfSAlan Cox  */
2798be0d18dfSAlan Cox 
2799be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2800be0d18dfSAlan Cox {
2801be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2802be0d18dfSAlan Cox }
2803a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_40wire);
2804be0d18dfSAlan Cox 
2805be0d18dfSAlan Cox /**
28062e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2807be0d18dfSAlan Cox  *	@ap: port
2808be0d18dfSAlan Cox  *
28092e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2810be0d18dfSAlan Cox  *	detection.
2811be0d18dfSAlan Cox  */
2812be0d18dfSAlan Cox 
2813be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2814be0d18dfSAlan Cox {
2815be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2816be0d18dfSAlan Cox }
2817a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_80wire);
2818be0d18dfSAlan Cox 
2819be0d18dfSAlan Cox /**
2820be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2821be0d18dfSAlan Cox  *	@ap: port
2822be0d18dfSAlan Cox  *
2823be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2824be0d18dfSAlan Cox  */
2825be0d18dfSAlan Cox 
2826be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2827be0d18dfSAlan Cox {
2828be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2829be0d18dfSAlan Cox }
2830a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_unknown);
2831be0d18dfSAlan Cox 
2832be0d18dfSAlan Cox /**
2833c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2834c88f90c3STejun Heo  *	@ap: port
2835c88f90c3STejun Heo  *
2836c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2837c88f90c3STejun Heo  *	transfer mode.
2838c88f90c3STejun Heo  */
2839c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2840c88f90c3STejun Heo {
2841c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2842c88f90c3STejun Heo }
2843a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_ignore);
2844c88f90c3STejun Heo 
2845c88f90c3STejun Heo /**
2846be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2847be0d18dfSAlan Cox  *	@ap: port
2848be0d18dfSAlan Cox  *
2849be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2850be0d18dfSAlan Cox  */
2851be0d18dfSAlan Cox 
2852be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2853be0d18dfSAlan Cox {
2854be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2855be0d18dfSAlan Cox }
2856a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_sata);
2857be0d18dfSAlan Cox 
2858be0d18dfSAlan Cox /**
2859c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2860c6fd2807SJeff Garzik  *	@ap: Bus to probe
2861c6fd2807SJeff Garzik  *
2862c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2863c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2864c6fd2807SJeff Garzik  *	the bus.
2865c6fd2807SJeff Garzik  *
2866c6fd2807SJeff Garzik  *	LOCKING:
2867c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2868c6fd2807SJeff Garzik  *
2869c6fd2807SJeff Garzik  *	RETURNS:
2870c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2871c6fd2807SJeff Garzik  */
2872c6fd2807SJeff Garzik 
2873c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2874c6fd2807SJeff Garzik {
2875c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2876c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2877f58229f8STejun Heo 	int rc;
2878c6fd2807SJeff Garzik 	struct ata_device *dev;
2879c6fd2807SJeff Garzik 
28801eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2881f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2882c6fd2807SJeff Garzik 
2883c6fd2807SJeff Garzik  retry:
28841eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2885cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2886cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2887cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2888cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2889cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2890cdeab114STejun Heo 		 * bus as we may be talking too fast.
2891cdeab114STejun Heo 		 */
2892cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
28935416912aSAaron Lu 		dev->dma_mode = 0xff;
2894cdeab114STejun Heo 
2895cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2896cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2897cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2898cdeab114STejun Heo 		 * configuring devices.
2899cdeab114STejun Heo 		 */
2900cdeab114STejun Heo 		if (ap->ops->set_piomode)
2901cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2902cdeab114STejun Heo 	}
2903cdeab114STejun Heo 
2904c6fd2807SJeff Garzik 	/* reset and determine device classes */
2905c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2906c6fd2807SJeff Garzik 
29071eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
29083e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2909c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2910c6fd2807SJeff Garzik 		else
2911c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2912c6fd2807SJeff Garzik 
2913c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2914c6fd2807SJeff Garzik 	}
2915c6fd2807SJeff Garzik 
2916f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2917f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2918f31f0cc2SJeff Garzik 	   the slave device */
2919f31f0cc2SJeff Garzik 
29201eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2921f58229f8STejun Heo 		if (tries[dev->devno])
2922f58229f8STejun Heo 			dev->class = classes[dev->devno];
2923c6fd2807SJeff Garzik 
2924c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2925c6fd2807SJeff Garzik 			continue;
2926c6fd2807SJeff Garzik 
2927bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2928bff04647STejun Heo 				     dev->id);
2929c6fd2807SJeff Garzik 		if (rc)
2930c6fd2807SJeff Garzik 			goto fail;
2931f31f0cc2SJeff Garzik 	}
2932f31f0cc2SJeff Garzik 
2933be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2934be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2935be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2936be0d18dfSAlan Cox 
29371eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
29381eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
29391eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
29401eca4365STejun Heo 	 * of the link the bridge is which is a problem.
29411eca4365STejun Heo 	 */
29421eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2943614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2944614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2945614fe29bSAlan Cox 
2946f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2947f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2948f31f0cc2SJeff Garzik 
29491eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
29509af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2951efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
29529af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2953c6fd2807SJeff Garzik 		if (rc)
2954c6fd2807SJeff Garzik 			goto fail;
2955c6fd2807SJeff Garzik 	}
2956c6fd2807SJeff Garzik 
2957c6fd2807SJeff Garzik 	/* configure transfer mode */
29580260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
29594ae72a1eSTejun Heo 	if (rc)
2960c6fd2807SJeff Garzik 		goto fail;
2961c6fd2807SJeff Garzik 
29621eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2963c6fd2807SJeff Garzik 		return 0;
2964c6fd2807SJeff Garzik 
2965c6fd2807SJeff Garzik 	return -ENODEV;
2966c6fd2807SJeff Garzik 
2967c6fd2807SJeff Garzik  fail:
29684ae72a1eSTejun Heo 	tries[dev->devno]--;
29694ae72a1eSTejun Heo 
2970c6fd2807SJeff Garzik 	switch (rc) {
2971c6fd2807SJeff Garzik 	case -EINVAL:
29724ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2973c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2974c6fd2807SJeff Garzik 		break;
29754ae72a1eSTejun Heo 
29764ae72a1eSTejun Heo 	case -ENODEV:
29774ae72a1eSTejun Heo 		/* give it just one more chance */
29784ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2979df561f66SGustavo A. R. Silva 		fallthrough;
2980c6fd2807SJeff Garzik 	case -EIO:
29814ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
29824ae72a1eSTejun Heo 			/* This is the last chance, better to slow
29834ae72a1eSTejun Heo 			 * down than lose it.
29844ae72a1eSTejun Heo 			 */
2985a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
29864ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
29874ae72a1eSTejun Heo 		}
2988c6fd2807SJeff Garzik 	}
2989c6fd2807SJeff Garzik 
29904ae72a1eSTejun Heo 	if (!tries[dev->devno])
2991c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2992c6fd2807SJeff Garzik 
2993c6fd2807SJeff Garzik 	goto retry;
2994c6fd2807SJeff Garzik }
2995c6fd2807SJeff Garzik 
2996c6fd2807SJeff Garzik /**
2997c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2998936fd732STejun Heo  *	@link: SATA link to printk link status about
2999c6fd2807SJeff Garzik  *
3000c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
3001c6fd2807SJeff Garzik  *
3002c6fd2807SJeff Garzik  *	LOCKING:
3003c6fd2807SJeff Garzik  *	None.
3004c6fd2807SJeff Garzik  */
30056bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
3006c6fd2807SJeff Garzik {
3007c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
3008c6fd2807SJeff Garzik 
3009936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
3010c6fd2807SJeff Garzik 		return;
3011936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
3012c6fd2807SJeff Garzik 
3013b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
3014c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
3015a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3016c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
3017c6fd2807SJeff Garzik 	} else {
3018a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3019c6fd2807SJeff Garzik 			      sstatus, scontrol);
3020c6fd2807SJeff Garzik 	}
3021c6fd2807SJeff Garzik }
3022c6fd2807SJeff Garzik 
3023c6fd2807SJeff Garzik /**
3024c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
3025c6fd2807SJeff Garzik  *	@adev: device
3026c6fd2807SJeff Garzik  *
3027c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
3028c6fd2807SJeff Garzik  *	present NULL is returned
3029c6fd2807SJeff Garzik  */
3030c6fd2807SJeff Garzik 
3031c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
3032c6fd2807SJeff Garzik {
30339af5c9c9STejun Heo 	struct ata_link *link = adev->link;
30349af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
3035c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
3036c6fd2807SJeff Garzik 		return NULL;
3037c6fd2807SJeff Garzik 	return pair;
3038c6fd2807SJeff Garzik }
3039a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_pair);
3040c6fd2807SJeff Garzik 
3041c6fd2807SJeff Garzik /**
3042c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
3043936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
3044a07d499bSTejun Heo  *	@spd_limit: Additional limit
3045c6fd2807SJeff Garzik  *
3046936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
3047c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
3048c6fd2807SJeff Garzik  *	using sata_set_spd().
3049c6fd2807SJeff Garzik  *
3050a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
3051a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
3052a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
3053a07d499bSTejun Heo  *	supported speed is allowed.
3054a07d499bSTejun Heo  *
3055c6fd2807SJeff Garzik  *	LOCKING:
3056c6fd2807SJeff Garzik  *	Inherited from caller.
3057c6fd2807SJeff Garzik  *
3058c6fd2807SJeff Garzik  *	RETURNS:
3059c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3060c6fd2807SJeff Garzik  */
3061a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3062c6fd2807SJeff Garzik {
3063c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
3064a07d499bSTejun Heo 	int rc, bit;
3065c6fd2807SJeff Garzik 
3066936fd732STejun Heo 	if (!sata_scr_valid(link))
3067008a7896STejun Heo 		return -EOPNOTSUPP;
3068008a7896STejun Heo 
3069008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
3070936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
3071008a7896STejun Heo 	 */
3072936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
30739913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
3074008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
3075008a7896STejun Heo 	else
3076936fd732STejun Heo 		spd = link->sata_spd;
3077c6fd2807SJeff Garzik 
3078936fd732STejun Heo 	mask = link->sata_spd_limit;
3079c6fd2807SJeff Garzik 	if (mask <= 1)
3080c6fd2807SJeff Garzik 		return -EINVAL;
3081008a7896STejun Heo 
3082008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3083a07d499bSTejun Heo 	bit = fls(mask) - 1;
3084a07d499bSTejun Heo 	mask &= ~(1 << bit);
3085c6fd2807SJeff Garzik 
30862dc0b46bSDavid Milburn 	/*
30872dc0b46bSDavid Milburn 	 * Mask off all speeds higher than or equal to the current one.  At
30882dc0b46bSDavid Milburn 	 * this point, if current SPD is not available and we previously
30892dc0b46bSDavid Milburn 	 * recorded the link speed from SStatus, the driver has already
30902dc0b46bSDavid Milburn 	 * masked off the highest bit so mask should already be 1 or 0.
30912dc0b46bSDavid Milburn 	 * Otherwise, we should not force 1.5Gbps on a link where we have
30922dc0b46bSDavid Milburn 	 * not previously recorded speed from SStatus.  Just return in this
30932dc0b46bSDavid Milburn 	 * case.
3094008a7896STejun Heo 	 */
3095008a7896STejun Heo 	if (spd > 1)
3096008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
3097008a7896STejun Heo 	else
30982dc0b46bSDavid Milburn 		return -EINVAL;
3099008a7896STejun Heo 
3100008a7896STejun Heo 	/* were we already at the bottom? */
3101c6fd2807SJeff Garzik 	if (!mask)
3102c6fd2807SJeff Garzik 		return -EINVAL;
3103c6fd2807SJeff Garzik 
3104a07d499bSTejun Heo 	if (spd_limit) {
3105a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3106a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3107a07d499bSTejun Heo 		else {
3108a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3109a07d499bSTejun Heo 			mask = 1 << bit;
3110a07d499bSTejun Heo 		}
3111a07d499bSTejun Heo 	}
3112a07d499bSTejun Heo 
3113936fd732STejun Heo 	link->sata_spd_limit = mask;
3114c6fd2807SJeff Garzik 
3115a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
3116c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
3117c6fd2807SJeff Garzik 
3118c6fd2807SJeff Garzik 	return 0;
3119c6fd2807SJeff Garzik }
3120c6fd2807SJeff Garzik 
3121a9b2c120SBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_ACPI
3122c6fd2807SJeff Garzik /**
3123a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3124a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3125a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3126a0f79b92STejun Heo  *
3127a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3128a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3129a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3130a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3131a0f79b92STejun Heo  *
3132a0f79b92STejun Heo  *	LOCKING:
3133a0f79b92STejun Heo  *	None.
3134a0f79b92STejun Heo  *
3135a0f79b92STejun Heo  *	RETURNS:
3136a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3137a0f79b92STejun Heo  */
3138a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3139a0f79b92STejun Heo {
3140a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3141a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3142a0f79b92STejun Heo 	const struct ata_timing *t;
3143a0f79b92STejun Heo 
3144a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3145a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3146a0f79b92STejun Heo 			base_mode = ent->base;
3147a0f79b92STejun Heo 
3148a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3149a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3150a0f79b92STejun Heo 		unsigned short this_cycle;
3151a0f79b92STejun Heo 
3152a0f79b92STejun Heo 		switch (xfer_shift) {
3153a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3154a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3155a0f79b92STejun Heo 			this_cycle = t->cycle;
3156a0f79b92STejun Heo 			break;
3157a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3158a0f79b92STejun Heo 			this_cycle = t->udma;
3159a0f79b92STejun Heo 			break;
3160a0f79b92STejun Heo 		default:
3161a0f79b92STejun Heo 			return 0xff;
3162a0f79b92STejun Heo 		}
3163a0f79b92STejun Heo 
3164a0f79b92STejun Heo 		if (cycle > this_cycle)
3165a0f79b92STejun Heo 			break;
3166a0f79b92STejun Heo 
3167a0f79b92STejun Heo 		last_mode = t->mode;
3168a0f79b92STejun Heo 	}
3169a0f79b92STejun Heo 
3170a0f79b92STejun Heo 	return last_mode;
3171a0f79b92STejun Heo }
3172a9b2c120SBartlomiej Zolnierkiewicz #endif
3173a0f79b92STejun Heo 
3174a0f79b92STejun Heo /**
3175c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3176c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3177458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3178c6fd2807SJeff Garzik  *
3179c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3180c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3181c6fd2807SJeff Garzik  *	will apply the limit.
3182c6fd2807SJeff Garzik  *
3183c6fd2807SJeff Garzik  *	LOCKING:
3184c6fd2807SJeff Garzik  *	Inherited from caller.
3185c6fd2807SJeff Garzik  *
3186c6fd2807SJeff Garzik  *	RETURNS:
3187c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3188c6fd2807SJeff Garzik  */
3189458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3190c6fd2807SJeff Garzik {
3191458337dbSTejun Heo 	char buf[32];
31927dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
31937dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3194458337dbSTejun Heo 	int quiet, highbit;
3195c6fd2807SJeff Garzik 
3196458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3197458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3198458337dbSTejun Heo 
3199458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3200458337dbSTejun Heo 						  dev->mwdma_mask,
3201c6fd2807SJeff Garzik 						  dev->udma_mask);
3202458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3203c6fd2807SJeff Garzik 
3204458337dbSTejun Heo 	switch (sel) {
3205458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3206458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3207458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3208458337dbSTejun Heo 		break;
3209458337dbSTejun Heo 
3210458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3211458337dbSTejun Heo 		if (udma_mask) {
3212458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3213458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3214458337dbSTejun Heo 			if (!udma_mask)
3215458337dbSTejun Heo 				return -ENOENT;
3216458337dbSTejun Heo 		} else if (mwdma_mask) {
3217458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3218458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3219458337dbSTejun Heo 			if (!mwdma_mask)
3220458337dbSTejun Heo 				return -ENOENT;
3221458337dbSTejun Heo 		}
3222458337dbSTejun Heo 		break;
3223458337dbSTejun Heo 
3224458337dbSTejun Heo 	case ATA_DNXFER_40C:
3225458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3226458337dbSTejun Heo 		break;
3227458337dbSTejun Heo 
3228458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3229458337dbSTejun Heo 		pio_mask &= 1;
3230df561f66SGustavo A. R. Silva 		fallthrough;
3231458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3232458337dbSTejun Heo 		mwdma_mask = 0;
3233458337dbSTejun Heo 		udma_mask = 0;
3234458337dbSTejun Heo 		break;
3235458337dbSTejun Heo 
3236458337dbSTejun Heo 	default:
3237458337dbSTejun Heo 		BUG();
3238458337dbSTejun Heo 	}
3239458337dbSTejun Heo 
3240458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3241458337dbSTejun Heo 
3242458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3243458337dbSTejun Heo 		return -ENOENT;
3244458337dbSTejun Heo 
3245458337dbSTejun Heo 	if (!quiet) {
3246458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3247458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3248458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3249458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3250458337dbSTejun Heo 		else
3251458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3252458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3253458337dbSTejun Heo 
3254a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3255458337dbSTejun Heo 	}
3256c6fd2807SJeff Garzik 
3257c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3258c6fd2807SJeff Garzik 			    &dev->udma_mask);
3259c6fd2807SJeff Garzik 
3260c6fd2807SJeff Garzik 	return 0;
3261c6fd2807SJeff Garzik }
3262c6fd2807SJeff Garzik 
3263c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3264c6fd2807SJeff Garzik {
3265d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
32669af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3267d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
32684055dee7STejun Heo 	const char *dev_err_whine = "";
32694055dee7STejun Heo 	int ign_dev_err = 0;
3270d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3271c6fd2807SJeff Garzik 	int rc;
3272c6fd2807SJeff Garzik 
3273c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3274c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3275c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3276c6fd2807SJeff Garzik 
3277d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3278d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3279d0cb43b3STejun Heo 	else {
3280d0cb43b3STejun Heo 		if (nosetxfer)
3281a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3282d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3283d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3284c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3285d0cb43b3STejun Heo 	}
32862dcb407eSJeff Garzik 
32874055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
32884055dee7STejun Heo 		goto fail;
32892dcb407eSJeff Garzik 
32904055dee7STejun Heo 	/* revalidate */
3291baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3292422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3293baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3294c6fd2807SJeff Garzik 	if (rc)
3295c6fd2807SJeff Garzik 		return rc;
3296c6fd2807SJeff Garzik 
3297b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
32984055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3299b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
33004055dee7STejun Heo 			ign_dev_err = 1;
3301b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3302b93fda12SAlan Cox 		   ATA devices */
3303b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
33044055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
33054055dee7STejun Heo 			ign_dev_err = 1;
3306b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3307b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3308b93fda12SAlan Cox 		   timings and no IORDY */
3309b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3310b93fda12SAlan Cox 			ign_dev_err = 1;
3311b93fda12SAlan Cox 	}
33124055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
33134055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
33144055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
33154055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
33164055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
33174055dee7STejun Heo 		ign_dev_err = 1;
33184055dee7STejun Heo 
33194055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
33204055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
33214055dee7STejun Heo 		ign_dev_err = 1;
33224055dee7STejun Heo 
33234055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
33244055dee7STejun Heo 		if (!ign_dev_err)
33254055dee7STejun Heo 			goto fail;
33264055dee7STejun Heo 		else
33274055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
33284055dee7STejun Heo 	}
33294055dee7STejun Heo 
33304633778bSHannes Reinecke 	ata_dev_dbg(dev, "xfer_shift=%u, xfer_mode=0x%x\n",
3331c6fd2807SJeff Garzik 		    dev->xfer_shift, (int)dev->xfer_mode);
3332c6fd2807SJeff Garzik 
333307b9b6d6SDamien Le Moal 	if (!(ehc->i.flags & ATA_EHI_QUIET) ||
333407b9b6d6SDamien Le Moal 	    ehc->i.flags & ATA_EHI_DID_HARDRESET)
3335a9a79dfeSJoe Perches 		ata_dev_info(dev, "configured for %s%s\n",
33364055dee7STejun Heo 			     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
33374055dee7STejun Heo 			     dev_err_whine);
33384055dee7STejun Heo 
3339c6fd2807SJeff Garzik 	return 0;
33404055dee7STejun Heo 
33414055dee7STejun Heo  fail:
3342a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
33434055dee7STejun Heo 	return -EIO;
3344c6fd2807SJeff Garzik }
3345c6fd2807SJeff Garzik 
3346c6fd2807SJeff Garzik /**
334704351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
33480260731fSTejun Heo  *	@link: link on which timings will be programmed
33491967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3350c6fd2807SJeff Garzik  *
335104351821SAlan  *	Standard implementation of the function used to tune and set
335204351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
335304351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3354c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3355c6fd2807SJeff Garzik  *
3356c6fd2807SJeff Garzik  *	LOCKING:
3357c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3358c6fd2807SJeff Garzik  *
3359c6fd2807SJeff Garzik  *	RETURNS:
3360c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3361c6fd2807SJeff Garzik  */
336204351821SAlan 
33630260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3364c6fd2807SJeff Garzik {
33650260731fSTejun Heo 	struct ata_port *ap = link->ap;
3366c6fd2807SJeff Garzik 	struct ata_device *dev;
3367f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3368c6fd2807SJeff Garzik 
3369c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
33701eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
33717dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3372b3a70601SAlan Cox 		unsigned int mode_mask;
3373c6fd2807SJeff Garzik 
3374b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3375b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3376b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3377b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3378b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3379b3a70601SAlan Cox 
3380c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
338133267325STejun Heo 		ata_force_xfermask(dev);
3382c6fd2807SJeff Garzik 
3383c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3384b3a70601SAlan Cox 
3385b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
338680a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
338780a9c430SSergei Shtylyov 						     dev->udma_mask);
3388b3a70601SAlan Cox 		else
3389b3a70601SAlan Cox 			dma_mask = 0;
3390b3a70601SAlan Cox 
3391c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3392c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3393c6fd2807SJeff Garzik 
3394c6fd2807SJeff Garzik 		found = 1;
3395b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3396c6fd2807SJeff Garzik 			used_dma = 1;
3397c6fd2807SJeff Garzik 	}
3398c6fd2807SJeff Garzik 	if (!found)
3399c6fd2807SJeff Garzik 		goto out;
3400c6fd2807SJeff Garzik 
3401c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
34021eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
340370cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3404a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3405c6fd2807SJeff Garzik 			rc = -EINVAL;
3406c6fd2807SJeff Garzik 			goto out;
3407c6fd2807SJeff Garzik 		}
3408c6fd2807SJeff Garzik 
3409c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3410c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3411c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3412c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3413c6fd2807SJeff Garzik 	}
3414c6fd2807SJeff Garzik 
3415c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
34161eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
34171eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3418c6fd2807SJeff Garzik 			continue;
3419c6fd2807SJeff Garzik 
3420c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3421c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3422c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3423c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3424c6fd2807SJeff Garzik 	}
3425c6fd2807SJeff Garzik 
3426c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
34271eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3428c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3429c6fd2807SJeff Garzik 		if (rc)
3430c6fd2807SJeff Garzik 			goto out;
3431c6fd2807SJeff Garzik 	}
3432c6fd2807SJeff Garzik 
3433c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3434c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3435c6fd2807SJeff Garzik 	 */
3436cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3437032af1ceSAlan 		ap->host->simplex_claimed = ap;
3438c6fd2807SJeff Garzik 
3439c6fd2807SJeff Garzik  out:
3440c6fd2807SJeff Garzik 	if (rc)
3441c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3442c6fd2807SJeff Garzik 	return rc;
3443c6fd2807SJeff Garzik }
3444a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_set_mode);
3445c6fd2807SJeff Garzik 
3446c6fd2807SJeff Garzik /**
3447aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3448aa2731adSTejun Heo  *	@link: link to be waited on
3449aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3450aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3451aa2731adSTejun Heo  *
3452aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3453aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3454aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3455aa2731adSTejun Heo  *	conditions.
3456aa2731adSTejun Heo  *
3457aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3458aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3459aa2731adSTejun Heo  *
3460aa2731adSTejun Heo  *	LOCKING:
3461aa2731adSTejun Heo  *	EH context.
3462aa2731adSTejun Heo  *
3463aa2731adSTejun Heo  *	RETURNS:
3464c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3465aa2731adSTejun Heo  */
3466aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3467aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3468aa2731adSTejun Heo {
3469aa2731adSTejun Heo 	unsigned long start = jiffies;
3470b48d58f5STejun Heo 	unsigned long nodev_deadline;
3471aa2731adSTejun Heo 	int warned = 0;
3472aa2731adSTejun Heo 
3473b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3474b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3475b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3476b48d58f5STejun Heo 	else
3477b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3478b48d58f5STejun Heo 
3479b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3480b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3481b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3482b1c72916STejun Heo 	 */
3483b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3484b1c72916STejun Heo 
3485aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3486aa2731adSTejun Heo 		nodev_deadline = deadline;
3487aa2731adSTejun Heo 
3488aa2731adSTejun Heo 	while (1) {
3489aa2731adSTejun Heo 		unsigned long now = jiffies;
3490aa2731adSTejun Heo 		int ready, tmp;
3491aa2731adSTejun Heo 
3492aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3493aa2731adSTejun Heo 		if (ready > 0)
3494aa2731adSTejun Heo 			return 0;
3495aa2731adSTejun Heo 
3496b48d58f5STejun Heo 		/*
3497b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3498aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3499b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3500b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3501b48d58f5STejun Heo 		 * offline.
3502aa2731adSTejun Heo 		 *
3503aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3504aa2731adSTejun Heo 		 * if status register is read more than once when
3505aa2731adSTejun Heo 		 * there's no device attached.
3506aa2731adSTejun Heo 		 */
3507aa2731adSTejun Heo 		if (ready == -ENODEV) {
3508aa2731adSTejun Heo 			if (ata_link_online(link))
3509aa2731adSTejun Heo 				ready = 0;
3510aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3511aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3512aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3513aa2731adSTejun Heo 				ready = 0;
3514aa2731adSTejun Heo 		}
3515aa2731adSTejun Heo 
3516aa2731adSTejun Heo 		if (ready)
3517aa2731adSTejun Heo 			return ready;
3518aa2731adSTejun Heo 		if (time_after(now, deadline))
3519aa2731adSTejun Heo 			return -EBUSY;
3520aa2731adSTejun Heo 
3521aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3522aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3523a9a79dfeSJoe Perches 			ata_link_warn(link,
3524aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3525aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3526aa2731adSTejun Heo 			warned = 1;
3527aa2731adSTejun Heo 		}
3528aa2731adSTejun Heo 
352997750cebSTejun Heo 		ata_msleep(link->ap, 50);
3530aa2731adSTejun Heo 	}
3531aa2731adSTejun Heo }
3532aa2731adSTejun Heo 
3533aa2731adSTejun Heo /**
3534aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3535aa2731adSTejun Heo  *	@link: link to be waited on
3536aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3537aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3538aa2731adSTejun Heo  *
3539aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3540aa2731adSTejun Heo  *
3541aa2731adSTejun Heo  *	LOCKING:
3542aa2731adSTejun Heo  *	EH context.
3543aa2731adSTejun Heo  *
3544aa2731adSTejun Heo  *	RETURNS:
3545c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3546aa2731adSTejun Heo  */
35472b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3548aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3549aa2731adSTejun Heo {
355097750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3551aa2731adSTejun Heo 
3552aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3553aa2731adSTejun Heo }
3554a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_after_reset);
3555aa2731adSTejun Heo 
3556aa2731adSTejun Heo /**
35570aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3558cc0680a5STejun Heo  *	@link: ATA link to be reset
3559d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3560c6fd2807SJeff Garzik  *
3561cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3562b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3563b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3564b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3565b8cffc6aSTejun Heo  *	should just whine, not fail.
3566c6fd2807SJeff Garzik  *
3567c6fd2807SJeff Garzik  *	LOCKING:
3568c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3569c6fd2807SJeff Garzik  *
3570c6fd2807SJeff Garzik  *	RETURNS:
3571c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3572c6fd2807SJeff Garzik  */
35730aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3574c6fd2807SJeff Garzik {
3575cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3576936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3577c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3578c6fd2807SJeff Garzik 	int rc;
3579c6fd2807SJeff Garzik 
3580c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3581c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3582c6fd2807SJeff Garzik 		return 0;
3583c6fd2807SJeff Garzik 
3584936fd732STejun Heo 	/* if SATA, resume link */
3585a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3586936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3587b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3588b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3589a9a79dfeSJoe Perches 			ata_link_warn(link,
3590a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3591a9a79dfeSJoe Perches 				      rc);
3592c6fd2807SJeff Garzik 	}
3593c6fd2807SJeff Garzik 
359445db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3595b1c72916STejun Heo 	if (ata_phys_link_offline(link))
359645db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
359745db2f6cSTejun Heo 
3598c6fd2807SJeff Garzik 	return 0;
3599c6fd2807SJeff Garzik }
3600a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_prereset);
3601c6fd2807SJeff Garzik 
3602c6fd2807SJeff Garzik /**
360357c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
360457c9efdfSTejun Heo  *	@link: link to reset
360557c9efdfSTejun Heo  *	@class: resulting class of attached device
360657c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
360757c9efdfSTejun Heo  *
360857c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
360957c9efdfSTejun Heo  *
361057c9efdfSTejun Heo  *	LOCKING:
361157c9efdfSTejun Heo  *	Kernel thread context (may sleep)
361257c9efdfSTejun Heo  *
361357c9efdfSTejun Heo  *	RETURNS:
361457c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
361557c9efdfSTejun Heo  */
361657c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
361757c9efdfSTejun Heo 		       unsigned long deadline)
361857c9efdfSTejun Heo {
361957c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
362057c9efdfSTejun Heo 	bool online;
362157c9efdfSTejun Heo 	int rc;
362257c9efdfSTejun Heo 
362357c9efdfSTejun Heo 	/* do hardreset */
362457c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
362557c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
362657c9efdfSTejun Heo }
3627a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(sata_std_hardreset);
362857c9efdfSTejun Heo 
362957c9efdfSTejun Heo /**
3630203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3631cc0680a5STejun Heo  *	@link: the target ata_link
3632c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3633c6fd2807SJeff Garzik  *
3634c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3635c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3636c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3637c6fd2807SJeff Garzik  *
3638c6fd2807SJeff Garzik  *	LOCKING:
3639c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3640c6fd2807SJeff Garzik  */
3641203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3642c6fd2807SJeff Garzik {
3643f046519fSTejun Heo 	u32 serror;
3644f046519fSTejun Heo 
3645f046519fSTejun Heo 	/* reset complete, clear SError */
3646f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3647f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3648f046519fSTejun Heo 
3649c6fd2807SJeff Garzik 	/* print link status */
3650936fd732STejun Heo 	sata_print_link_status(link);
3651c6fd2807SJeff Garzik }
3652a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_postreset);
3653c6fd2807SJeff Garzik 
3654c6fd2807SJeff Garzik /**
3655c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3656c6fd2807SJeff Garzik  *	@dev: device to compare against
3657c6fd2807SJeff Garzik  *	@new_class: class of the new device
3658c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3659c6fd2807SJeff Garzik  *
3660c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3661c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3662c6fd2807SJeff Garzik  *	@new_id.
3663c6fd2807SJeff Garzik  *
3664c6fd2807SJeff Garzik  *	LOCKING:
3665c6fd2807SJeff Garzik  *	None.
3666c6fd2807SJeff Garzik  *
3667c6fd2807SJeff Garzik  *	RETURNS:
3668c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3669c6fd2807SJeff Garzik  */
3670c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3671c6fd2807SJeff Garzik 			       const u16 *new_id)
3672c6fd2807SJeff Garzik {
3673c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3674a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3675a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3676c6fd2807SJeff Garzik 
3677c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3678a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
3679c6fd2807SJeff Garzik 			     dev->class, new_class);
3680c6fd2807SJeff Garzik 		return 0;
3681c6fd2807SJeff Garzik 	}
3682c6fd2807SJeff Garzik 
3683a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3684a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3685a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3686a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3687c6fd2807SJeff Garzik 
3688c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3689a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3690a9a79dfeSJoe Perches 			     model[0], model[1]);
3691c6fd2807SJeff Garzik 		return 0;
3692c6fd2807SJeff Garzik 	}
3693c6fd2807SJeff Garzik 
3694c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3695a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3696a9a79dfeSJoe Perches 			     serial[0], serial[1]);
3697c6fd2807SJeff Garzik 		return 0;
3698c6fd2807SJeff Garzik 	}
3699c6fd2807SJeff Garzik 
3700c6fd2807SJeff Garzik 	return 1;
3701c6fd2807SJeff Garzik }
3702c6fd2807SJeff Garzik 
3703c6fd2807SJeff Garzik /**
3704fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
37053fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3706bff04647STejun Heo  *	@readid_flags: read ID flags
3707c6fd2807SJeff Garzik  *
3708c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3709c6fd2807SJeff Garzik  *	the port.
3710c6fd2807SJeff Garzik  *
3711c6fd2807SJeff Garzik  *	LOCKING:
3712c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3713c6fd2807SJeff Garzik  *
3714c6fd2807SJeff Garzik  *	RETURNS:
3715c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3716c6fd2807SJeff Garzik  */
3717fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3718c6fd2807SJeff Garzik {
3719c6fd2807SJeff Garzik 	unsigned int class = dev->class;
37209af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3721c6fd2807SJeff Garzik 	int rc;
3722c6fd2807SJeff Garzik 
3723c6fd2807SJeff Garzik 	/* read ID data */
3724bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3725c6fd2807SJeff Garzik 	if (rc)
3726fe30911bSTejun Heo 		return rc;
3727c6fd2807SJeff Garzik 
3728c6fd2807SJeff Garzik 	/* is the device still there? */
3729fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3730fe30911bSTejun Heo 		return -ENODEV;
3731c6fd2807SJeff Garzik 
3732c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3733fe30911bSTejun Heo 	return 0;
3734fe30911bSTejun Heo }
3735fe30911bSTejun Heo 
3736fe30911bSTejun Heo /**
3737fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3738fe30911bSTejun Heo  *	@dev: device to revalidate
3739422c9daaSTejun Heo  *	@new_class: new class code
3740fe30911bSTejun Heo  *	@readid_flags: read ID flags
3741fe30911bSTejun Heo  *
3742fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3743fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3744fe30911bSTejun Heo  *
3745fe30911bSTejun Heo  *	LOCKING:
3746fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3747fe30911bSTejun Heo  *
3748fe30911bSTejun Heo  *	RETURNS:
3749fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3750fe30911bSTejun Heo  */
3751422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3752422c9daaSTejun Heo 		       unsigned int readid_flags)
3753fe30911bSTejun Heo {
37546ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
37555920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
3756fe30911bSTejun Heo 	int rc;
3757fe30911bSTejun Heo 
3758fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3759fe30911bSTejun Heo 		return -ENODEV;
3760fe30911bSTejun Heo 
3761422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3762422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
3763f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
3764f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
37659162c657SHannes Reinecke 	    new_class != ATA_DEV_ZAC &&
3766f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
3767a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
3768422c9daaSTejun Heo 			     dev->class, new_class);
3769422c9daaSTejun Heo 		rc = -ENODEV;
3770422c9daaSTejun Heo 		goto fail;
3771422c9daaSTejun Heo 	}
3772422c9daaSTejun Heo 
3773fe30911bSTejun Heo 	/* re-read ID */
3774fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3775fe30911bSTejun Heo 	if (rc)
3776fe30911bSTejun Heo 		goto fail;
3777c6fd2807SJeff Garzik 
3778c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3779efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
37806ddcd3b0STejun Heo 	if (rc)
37816ddcd3b0STejun Heo 		goto fail;
37826ddcd3b0STejun Heo 
37836ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
3784445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
3785445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
3786445d211bSTejun Heo 		return 0;
3787445d211bSTejun Heo 
3788445d211bSTejun Heo 	/* n_sectors has changed */
3789a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
37906ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
37916ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
3792445d211bSTejun Heo 
37935920dadfSTejun Heo 	/*
37945920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
3795445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
3796445d211bSTejun Heo 	 * new size matches it, keep the device.
37975920dadfSTejun Heo 	 */
37985920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3799445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
3800a9a79dfeSJoe Perches 		ata_dev_warn(dev,
38015920dadfSTejun Heo 			     "new n_sectors matches native, probably "
380268939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
380368939ce5STejun Heo 		/* use the larger n_sectors */
3804445d211bSTejun Heo 		return 0;
3805445d211bSTejun Heo 	}
3806445d211bSTejun Heo 
3807445d211bSTejun Heo 	/*
3808445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
3809445d211bSTejun Heo 	 * unlocking HPA in those cases.
3810445d211bSTejun Heo 	 *
3811445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3812445d211bSTejun Heo 	 */
3813445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3814445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3815445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
3816a9a79dfeSJoe Perches 		ata_dev_warn(dev,
3817445d211bSTejun Heo 			     "old n_sectors matches native, probably "
3818445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
3819445d211bSTejun Heo 		/* try unlocking HPA */
3820445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3821445d211bSTejun Heo 		rc = -EIO;
3822445d211bSTejun Heo 	} else
3823445d211bSTejun Heo 		rc = -ENODEV;
3824445d211bSTejun Heo 
3825445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
38265920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
38275920dadfSTejun Heo 	dev->n_sectors = n_sectors;
3828c6fd2807SJeff Garzik  fail:
3829a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
3830c6fd2807SJeff Garzik 	return rc;
3831c6fd2807SJeff Garzik }
3832c6fd2807SJeff Garzik 
38336919a0a6SAlan Cox struct ata_blacklist_entry {
38346919a0a6SAlan Cox 	const char *model_num;
38356919a0a6SAlan Cox 	const char *model_rev;
38366919a0a6SAlan Cox 	unsigned long horkage;
38376919a0a6SAlan Cox };
38386919a0a6SAlan Cox 
38396919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
38406919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
38416919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
38426919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
38436919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
38446919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
38456919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
38466919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
38476919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
38486919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
38496919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
38507da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
38516919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
38526919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
38536919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
38546919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
38556919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
38567da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
38576919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
38586919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
38596919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
38606919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
38616919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
38626919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
38636919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
38646919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
38656919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
38666919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
38676919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
386839f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
3869d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
3870b00622fcSShikha Jain 	{ "VRFDFC22048UCHC-TE*", NULL,		ATA_HORKAGE_NODMA },
38713af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
387250af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
3873a66307d4SHannes Reinecke 	/* Similar story with ASMedia 1092 */
3874a66307d4SHannes Reinecke 	{ "ASMT109x- Config",	NULL,		ATA_HORKAGE_DISABLE },
38756919a0a6SAlan Cox 
387618d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
387740a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
38786a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
3879a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
38800523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
388118d6e9d5SAlbert Lee 
3882af34d637SDavid Milburn 	/*
3883af34d637SDavid Milburn 	 * Causes silent data corruption with higher max sects.
3884af34d637SDavid Milburn 	 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3885af34d637SDavid Milburn 	 */
3886af34d637SDavid Milburn 	{ "ST380013AS",		"3.20",		ATA_HORKAGE_MAX_SEC_1024 },
3887af34d637SDavid Milburn 
38881488a1e3STejun Heo 	/*
3889e0edc8c5STejun Heo 	 * These devices time out with higher max sects.
38901488a1e3STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
38911488a1e3STejun Heo 	 */
3892e0edc8c5STejun Heo 	{ "LITEON CX1-JB*-HP",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
3893db5ff909SXinyu Lin 	{ "LITEON EP1-*",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
38941488a1e3STejun Heo 
38956919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
38966919a0a6SAlan Cox 
38976919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
38986919a0a6SAlan Cox 	/* NCQ is slow */
38996919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
3900459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
390109125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
390209125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
39037acfaf30SPaul Rolland 	/* NCQ is broken */
3904539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
39050e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
3906da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
3907e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
39085ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
3909539cc7c7SJeff Garzik 
3910ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
39114d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3912ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3913d10d491fSTejun Heo 
39144d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3915d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3916d10d491fSTejun Heo 
39174d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3918d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3919d10d491fSTejun Heo 
39204d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3921ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3922ac70a964STejun Heo 
392331f6264eSHans de Goede 	/* drives which fail FPDMA_AA activation (some may freeze afterwards)
392431f6264eSHans de Goede 	   the ST disks also have LPM issues */
39258756a25bSHans de Goede 	{ "ST1000LM024 HN-M101MBB", NULL,	ATA_HORKAGE_BROKEN_FPDMA_AA |
392631f6264eSHans de Goede 						ATA_HORKAGE_NOLPM, },
392708c85d2aSAleksei Mamlin 	{ "VB0250EAVER",	"HPG7",		ATA_HORKAGE_BROKEN_FPDMA_AA },
392887809942SMichele Baldessari 
392936e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
393036e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
393136e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
393236e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
393336e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
39346919a0a6SAlan Cox 
393568b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
393668b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
393768b0ddb2STejun Heo 
39383b545563STejun Heo 	/* Sandisk SD7/8/9s lock up hard on large trims */
39393b545563STejun Heo 	{ "SanDisk SD[789]*",	NULL,		ATA_HORKAGE_MAX_TRIM_128M, },
3940322579dcSTejun Heo 
394116c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
394216c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
394316c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
394416c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
394516c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
39466919a0a6SAlan Cox 
39477831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
39487831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
39497831387bSTejun Heo 
395093328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
395193328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
395293328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
3953b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
395493328e11SAlan Cox 
39556bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
39566bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
3957a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
39587da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
39596bbfd53dSAlan Cox 
39609ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
39619ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
396204d0f1b8SJeff Garzik 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
39639ce8e307SJens Axboe 
39649062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
39659062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
3966c531077fSDaniel J Blueman 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS, },
39679062712fSTejun Heo 
3968d0cb43b3STejun Heo 	/*
3969d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
3970d0cb43b3STejun Heo 	 * device and controller are SATA.
3971d0cb43b3STejun Heo 	 */
3972cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
39733a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
39743a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
3975cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
3976cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
3977d0cb43b3STejun Heo 
3978b17e5729SKai-Heng Feng 	/* Crucial BX100 SSD 500GB has broken LPM support */
39793bf7b5d6SHans de Goede 	{ "CT500BX100SSD1",		NULL,	ATA_HORKAGE_NOLPM },
3980b17e5729SKai-Heng Feng 
3981d418ff56SHans de Goede 	/* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
3982d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
39839c7be59fSHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
39849c7be59fSHans de Goede 						ATA_HORKAGE_NOLPM, },
3985d418ff56SHans de Goede 	/* 512GB MX100 with newer firmware has only LPM issues */
3986d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM |
3987d418ff56SHans de Goede 						ATA_HORKAGE_NOLPM, },
39889c7be59fSHans de Goede 
398962ac3f73SHans de Goede 	/* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
399062ac3f73SHans de Goede 	{ "Crucial_CT480M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
399162ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
399262ac3f73SHans de Goede 						ATA_HORKAGE_NOLPM, },
399362ac3f73SHans de Goede 	{ "Crucial_CT960M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
399462ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
399562ac3f73SHans de Goede 						ATA_HORKAGE_NOLPM, },
399662ac3f73SHans de Goede 
399776936e9aSFrançois Cami 	/* These specific Samsung models/firmware-revs do not handle LPM well */
3998b5b4d3a5SHans de Goede 	{ "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
399976936e9aSFrançois Cami 	{ "SAMSUNG SSD PM830 mSATA *",  "CXM13D1Q", ATA_HORKAGE_NOLPM, },
4000410b5c7bSDiego Viola 	{ "SAMSUNG MZ7TD256HAFV-000L9", NULL,       ATA_HORKAGE_NOLPM, },
4001dd957493SHans de Goede 	{ "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, },
4002b5b4d3a5SHans de Goede 
4003f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
4004136d769eSSudip Mukherjee 	{ "Micron_M500IT_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4005136d769eSSudip Mukherjee 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4006243918beSMartin K. Petersen 	{ "Micron_M500_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4007e61f7d1cSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4008ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4009ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
40109051bd39SMartin K. Petersen 	{ "Micron_M5[15]0_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4011ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4012ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M550*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4013ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4014ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*MX100*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4015ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4016ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 840*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4017ca6bfcb2SJu Hyung Park 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4018ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 850*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
40196fc4d97aSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
40208a6430abSHans de Goede 	{ "Samsung SSD 860*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
40217a8526a5SKate Hsuan 						ATA_HORKAGE_ZERO_AFTER_TRIM |
40227a8526a5SKate Hsuan 						ATA_HORKAGE_NO_NCQ_ON_ATI, },
40238a6430abSHans de Goede 	{ "Samsung SSD 870*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
40247a8526a5SKate Hsuan 						ATA_HORKAGE_ZERO_AFTER_TRIM |
40257a8526a5SKate Hsuan 						ATA_HORKAGE_NO_NCQ_ON_ATI, },
40267a7184b0SGuillermo A. Amaral 	{ "FCCT*M500*",			NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
40277a7184b0SGuillermo A. Amaral 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4028e61f7d1cSMartin K. Petersen 
4029cda57b1bSArne Fitzenreiter 	/* devices that don't properly handle TRIM commands */
4030cda57b1bSArne Fitzenreiter 	{ "SuperSSpeed S238*",		NULL,	ATA_HORKAGE_NOTRIM, },
4031cda57b1bSArne Fitzenreiter 
4032e61f7d1cSMartin K. Petersen 	/*
4033e61f7d1cSMartin K. Petersen 	 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4034e61f7d1cSMartin K. Petersen 	 * (Return Zero After Trim) flags in the ATA Command Set are
4035e61f7d1cSMartin K. Petersen 	 * unreliable in the sense that they only define what happens if
4036e61f7d1cSMartin K. Petersen 	 * the device successfully executed the DSM TRIM command. TRIM
4037e61f7d1cSMartin K. Petersen 	 * is only advisory, however, and the device is free to silently
4038e61f7d1cSMartin K. Petersen 	 * ignore all or parts of the request.
4039e61f7d1cSMartin K. Petersen 	 *
4040e61f7d1cSMartin K. Petersen 	 * Whitelist drives that are known to reliably return zeroes
4041e61f7d1cSMartin K. Petersen 	 * after TRIM.
4042e61f7d1cSMartin K. Petersen 	 */
4043e61f7d1cSMartin K. Petersen 
4044e61f7d1cSMartin K. Petersen 	/*
4045e61f7d1cSMartin K. Petersen 	 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4046e61f7d1cSMartin K. Petersen 	 * that model before whitelisting all other intel SSDs.
4047e61f7d1cSMartin K. Petersen 	 */
4048e61f7d1cSMartin K. Petersen 	{ "INTEL*SSDSC2MH*",		NULL,	0, },
4049e61f7d1cSMartin K. Petersen 
4050ff7f53fbSMartin K. Petersen 	{ "Micron*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4051ff7f53fbSMartin K. Petersen 	{ "Crucial*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4052e61f7d1cSMartin K. Petersen 	{ "INTEL*SSD*", 		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4053e61f7d1cSMartin K. Petersen 	{ "SSD*INTEL*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4054e61f7d1cSMartin K. Petersen 	{ "Samsung*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4055e61f7d1cSMartin K. Petersen 	{ "SAMSUNG*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4056fd6f32f7SJuha-Matti Tilli 	{ "SAMSUNG*MZ7KM*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4057e61f7d1cSMartin K. Petersen 	{ "ST[1248][0248]0[FH]*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4058f78dea06SMarc Carino 
4059ecd75ad5STejun Heo 	/*
4060ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4061ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4062ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4063ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4064ecd75ad5STejun Heo 	 * lost too.
4065ecd75ad5STejun Heo 	 *
4066ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4067ecd75ad5STejun Heo 	 */
4068ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4069ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4070ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4071ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4072ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4073ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4074ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4075ecd75ad5STejun Heo 
40766919a0a6SAlan Cox 	/* End Marker */
40776919a0a6SAlan Cox 	{ }
4078c6fd2807SJeff Garzik };
4079c6fd2807SJeff Garzik 
408075683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4081c6fd2807SJeff Garzik {
40828bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
40838bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
40846919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4085c6fd2807SJeff Garzik 
40868bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
40878bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4088c6fd2807SJeff Garzik 
40896919a0a6SAlan Cox 	while (ad->model_num) {
40901c402799SGeorge Spelvin 		if (glob_match(ad->model_num, model_num)) {
40916919a0a6SAlan Cox 			if (ad->model_rev == NULL)
40926919a0a6SAlan Cox 				return ad->horkage;
40931c402799SGeorge Spelvin 			if (glob_match(ad->model_rev, model_rev))
40946919a0a6SAlan Cox 				return ad->horkage;
4095c6fd2807SJeff Garzik 		}
40966919a0a6SAlan Cox 		ad++;
4097c6fd2807SJeff Garzik 	}
4098c6fd2807SJeff Garzik 	return 0;
4099c6fd2807SJeff Garzik }
4100c6fd2807SJeff Garzik 
41016919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
41026919a0a6SAlan Cox {
41036919a0a6SAlan Cox 	/* We don't support polling DMA.
41046919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
41056919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
41066919a0a6SAlan Cox 	 */
41079af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
41086919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
41096919a0a6SAlan Cox 		return 1;
411075683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
41116919a0a6SAlan Cox }
41126919a0a6SAlan Cox 
4113c6fd2807SJeff Garzik /**
41146bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
41156bbfd53dSAlan Cox  *	@dev: device
41166bbfd53dSAlan Cox  *
41176bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
41186bbfd53dSAlan Cox  *	who can't follow the documentation.
41196bbfd53dSAlan Cox  */
41206bbfd53dSAlan Cox 
41216bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
41226bbfd53dSAlan Cox {
41236bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
41246bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
41256bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
41266bbfd53dSAlan Cox }
41276bbfd53dSAlan Cox 
41286bbfd53dSAlan Cox /**
412915a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
413015a5551cSAlan Cox  *	@ap: port to consider
413115a5551cSAlan Cox  *
413215a5551cSAlan Cox  *	This function encapsulates the policy for speed management
413315a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
413415a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
413515a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
413615a5551cSAlan Cox  *	impacts hotplug at all).
413715a5551cSAlan Cox  *
413815a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
413915a5551cSAlan Cox  */
414015a5551cSAlan Cox 
414115a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
414215a5551cSAlan Cox {
414315a5551cSAlan Cox 	struct ata_link *link;
414415a5551cSAlan Cox 	struct ata_device *dev;
414515a5551cSAlan Cox 
41464a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
414715a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
414815a5551cSAlan Cox 		return 1;
41494a9c7b33STejun Heo 
41504a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
415115a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
415215a5551cSAlan Cox 		return 0;
41534a9c7b33STejun Heo 
41544a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
41554a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
41564a9c7b33STejun Heo 	 * isn't sure.
41574a9c7b33STejun Heo 	 */
4158f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4159f792068eSAlan Cox 		return 0;
416015a5551cSAlan Cox 
41614a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
41624a9c7b33STejun Heo 	 *
41634a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
41644a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
41654a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
41664a9c7b33STejun Heo 	 *   give a valid detect
41674a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
41684a9c7b33STejun Heo 	 *   to colour the choice
416915a5551cSAlan Cox 	 */
41701eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
41711eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
41721eca4365STejun Heo 			if (!ata_is_40wire(dev))
417315a5551cSAlan Cox 				return 0;
417415a5551cSAlan Cox 		}
417515a5551cSAlan Cox 	}
417615a5551cSAlan Cox 	return 1;
417715a5551cSAlan Cox }
417815a5551cSAlan Cox 
417915a5551cSAlan Cox /**
4180c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4181c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4182c6fd2807SJeff Garzik  *
4183c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4184c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4185c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4186c6fd2807SJeff Garzik  *	blacklist, etc...
4187c6fd2807SJeff Garzik  *
4188c6fd2807SJeff Garzik  *	LOCKING:
4189c6fd2807SJeff Garzik  *	None.
4190c6fd2807SJeff Garzik  */
4191c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4192c6fd2807SJeff Garzik {
41939af5c9c9STejun Heo 	struct ata_link *link = dev->link;
41949af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4195cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4196c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4197c6fd2807SJeff Garzik 
4198c6fd2807SJeff Garzik 	/* controller modes available */
4199c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4200c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4201c6fd2807SJeff Garzik 
42028343f889SRobert Hancock 	/* drive modes available */
4203c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4204c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4205c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4206c6fd2807SJeff Garzik 
4207b352e57dSAlan Cox 	/*
4208b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4209b352e57dSAlan Cox 	 *	cable
4210b352e57dSAlan Cox 	 */
4211b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4212b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4213b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4214b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4215b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4216b352e57dSAlan Cox 	}
4217b352e57dSAlan Cox 
4218c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4219c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4220a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4221c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4222c6fd2807SJeff Garzik 	}
4223c6fd2807SJeff Garzik 
422414d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
422514d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4226c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4227a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4228a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4229c6fd2807SJeff Garzik 	}
4230c6fd2807SJeff Garzik 
4231e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4232e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4233e424675fSJeff Garzik 
4234c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4235a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4236c6fd2807SJeff Garzik 
42378343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
42388343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
42398343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
42408343f889SRobert Hancock 	 * solely limited by the cable.
42418343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
42428343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
42438343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
42448343f889SRobert Hancock 	 */
42458343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
42468343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
424715a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4248a9a79dfeSJoe Perches 			ata_dev_warn(dev,
42498343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
42508343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
42518343f889SRobert Hancock 		}
42528343f889SRobert Hancock 
4253c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4254c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4255c6fd2807SJeff Garzik }
4256c6fd2807SJeff Garzik 
4257c6fd2807SJeff Garzik /**
4258c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4259c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4260c6fd2807SJeff Garzik  *
4261c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4262c6fd2807SJeff Garzik  *	on port @ap.
4263c6fd2807SJeff Garzik  *
4264c6fd2807SJeff Garzik  *	LOCKING:
4265c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4266c6fd2807SJeff Garzik  *
4267c6fd2807SJeff Garzik  *	RETURNS:
4268c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4269c6fd2807SJeff Garzik  */
4270c6fd2807SJeff Garzik 
4271c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4272c6fd2807SJeff Garzik {
4273c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4274c6fd2807SJeff Garzik 	unsigned int err_mask;
4275c6fd2807SJeff Garzik 
4276c6fd2807SJeff Garzik 	/* set up set-features taskfile */
42774633778bSHannes Reinecke 	ata_dev_dbg(dev, "set features - xfer mode\n");
4278c6fd2807SJeff Garzik 
4279464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4280464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4281464cf177STejun Heo 	 */
4282c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4283c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4284c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4285464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4286c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4287b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4288b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4289c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4290b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4291b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4292b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4293b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4294b9f8ab2dSAlan Cox 		return 0;
4295c6fd2807SJeff Garzik 
4296d531be2cSMikulas Patocka 	/* On some disks, this command causes spin-up, so we need longer timeout */
4297d531be2cSMikulas Patocka 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4298c6fd2807SJeff Garzik 
4299c6fd2807SJeff Garzik 	return err_mask;
4300c6fd2807SJeff Garzik }
43011152b261STejun Heo 
4302c6fd2807SJeff Garzik /**
4303218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
43049f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
43059f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4306218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
43079f45cbd3SKristen Carlson Accardi  *
43089f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4309218f3d30SJeff Garzik  *	on port @ap with sector count
43109f45cbd3SKristen Carlson Accardi  *
43119f45cbd3SKristen Carlson Accardi  *	LOCKING:
43129f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
43139f45cbd3SKristen Carlson Accardi  *
43149f45cbd3SKristen Carlson Accardi  *	RETURNS:
43159f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
43169f45cbd3SKristen Carlson Accardi  */
43171152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
43189f45cbd3SKristen Carlson Accardi {
43199f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
43209f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
4321974e0a45SDamien Le Moal 	unsigned long timeout = 0;
43229f45cbd3SKristen Carlson Accardi 
43239f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
43244633778bSHannes Reinecke 	ata_dev_dbg(dev, "set features - SATA features\n");
43259f45cbd3SKristen Carlson Accardi 
43269f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
43279f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
43289f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
43299f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
43309f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4331218f3d30SJeff Garzik 	tf.nsect = feature;
43329f45cbd3SKristen Carlson Accardi 
4333974e0a45SDamien Le Moal 	if (enable == SETFEATURES_SPINUP)
4334974e0a45SDamien Le Moal 		timeout = ata_probe_timeout ?
4335974e0a45SDamien Le Moal 			  ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4336974e0a45SDamien Le Moal 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
43379f45cbd3SKristen Carlson Accardi 
43389f45cbd3SKristen Carlson Accardi 	return err_mask;
43399f45cbd3SKristen Carlson Accardi }
4340633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
43419f45cbd3SKristen Carlson Accardi 
43429f45cbd3SKristen Carlson Accardi /**
4343c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4344c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4345c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4346c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4347c6fd2807SJeff Garzik  *
4348c6fd2807SJeff Garzik  *	LOCKING:
4349c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4350c6fd2807SJeff Garzik  *
4351c6fd2807SJeff Garzik  *	RETURNS:
4352c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4353c6fd2807SJeff Garzik  */
4354c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4355c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4356c6fd2807SJeff Garzik {
4357c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4358c6fd2807SJeff Garzik 	unsigned int err_mask;
4359c6fd2807SJeff Garzik 
4360c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4361c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4362c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4363c6fd2807SJeff Garzik 
4364c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
43654633778bSHannes Reinecke 	ata_dev_dbg(dev, "init dev params \n");
4366c6fd2807SJeff Garzik 
4367c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4368c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4369c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4370c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4371c6fd2807SJeff Garzik 	tf.nsect = sectors;
4372c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4373c6fd2807SJeff Garzik 
43742b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
437518b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
437618b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
437718b2466cSAlan Cox 	   drive reported working geometry */
437818b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
437918b2466cSAlan Cox 		err_mask = 0;
4380c6fd2807SJeff Garzik 
4381c6fd2807SJeff Garzik 	return err_mask;
4382c6fd2807SJeff Garzik }
4383c6fd2807SJeff Garzik 
4384c6fd2807SJeff Garzik /**
43855895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4386c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4387c6fd2807SJeff Garzik  *
4388c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4389c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4390c6fd2807SJeff Garzik  *	supplied PACKET command.
4391c6fd2807SJeff Garzik  *
4392c6fd2807SJeff Garzik  *	LOCKING:
4393cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4394c6fd2807SJeff Garzik  *
4395c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4396c6fd2807SJeff Garzik  *               nonzero otherwise
4397c6fd2807SJeff Garzik  */
43985895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4399c6fd2807SJeff Garzik {
4400c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4401c6fd2807SJeff Garzik 
4402b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4403b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4404b9a4197eSTejun Heo 	 */
44056a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
44066a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
44076f23a31dSAlbert Lee 		return 1;
44086f23a31dSAlbert Lee 
4409c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4410b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4411c6fd2807SJeff Garzik 
4412b9a4197eSTejun Heo 	return 0;
4413c6fd2807SJeff Garzik }
4414b9a4197eSTejun Heo 
4415c6fd2807SJeff Garzik /**
441631cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
441731cc23b3STejun Heo  *	@qc: ATA command in question
441831cc23b3STejun Heo  *
441931cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
442031cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
442131cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
442231cc23b3STejun Heo  *	whether a new command @qc can be issued.
442331cc23b3STejun Heo  *
442431cc23b3STejun Heo  *	LOCKING:
442531cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
442631cc23b3STejun Heo  *
442731cc23b3STejun Heo  *	RETURNS:
442831cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
442931cc23b3STejun Heo  */
443031cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
443131cc23b3STejun Heo {
443231cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
443331cc23b3STejun Heo 
4434179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
443531cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
443631cc23b3STejun Heo 			return 0;
443731cc23b3STejun Heo 	} else {
443831cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
443931cc23b3STejun Heo 			return 0;
444031cc23b3STejun Heo 	}
444131cc23b3STejun Heo 
444231cc23b3STejun Heo 	return ATA_DEFER_LINK;
444331cc23b3STejun Heo }
4444a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_qc_defer);
444531cc23b3STejun Heo 
444695364f36SJiri Slaby enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
444795364f36SJiri Slaby {
444895364f36SJiri Slaby 	return AC_ERR_OK;
444995364f36SJiri Slaby }
4450a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
4451c6fd2807SJeff Garzik 
4452c6fd2807SJeff Garzik /**
4453c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4454c6fd2807SJeff Garzik  *	@qc: Command to be associated
4455c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4456c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4457c6fd2807SJeff Garzik  *
4458c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4459c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4460c6fd2807SJeff Garzik  *	elements.
4461c6fd2807SJeff Garzik  *
4462c6fd2807SJeff Garzik  *	LOCKING:
4463cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4464c6fd2807SJeff Garzik  */
4465c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4466c6fd2807SJeff Garzik 		 unsigned int n_elem)
4467c6fd2807SJeff Garzik {
4468ff2aeb1eSTejun Heo 	qc->sg = sg;
4469c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4470ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4471ff2aeb1eSTejun Heo }
4472ff2aeb1eSTejun Heo 
44732874d5eeSGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
44742874d5eeSGeert Uytterhoeven 
44752874d5eeSGeert Uytterhoeven /**
44762874d5eeSGeert Uytterhoeven  *	ata_sg_clean - Unmap DMA memory associated with command
44772874d5eeSGeert Uytterhoeven  *	@qc: Command containing DMA memory to be released
44782874d5eeSGeert Uytterhoeven  *
44792874d5eeSGeert Uytterhoeven  *	Unmap all mapped DMA memory associated with this command.
44802874d5eeSGeert Uytterhoeven  *
44812874d5eeSGeert Uytterhoeven  *	LOCKING:
44822874d5eeSGeert Uytterhoeven  *	spin_lock_irqsave(host lock)
44832874d5eeSGeert Uytterhoeven  */
4484af27e01cSJason Yan static void ata_sg_clean(struct ata_queued_cmd *qc)
44852874d5eeSGeert Uytterhoeven {
44862874d5eeSGeert Uytterhoeven 	struct ata_port *ap = qc->ap;
44872874d5eeSGeert Uytterhoeven 	struct scatterlist *sg = qc->sg;
44882874d5eeSGeert Uytterhoeven 	int dir = qc->dma_dir;
44892874d5eeSGeert Uytterhoeven 
44902874d5eeSGeert Uytterhoeven 	WARN_ON_ONCE(sg == NULL);
44912874d5eeSGeert Uytterhoeven 
44922874d5eeSGeert Uytterhoeven 	if (qc->n_elem)
44932874d5eeSGeert Uytterhoeven 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
44942874d5eeSGeert Uytterhoeven 
44952874d5eeSGeert Uytterhoeven 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
44962874d5eeSGeert Uytterhoeven 	qc->sg = NULL;
44972874d5eeSGeert Uytterhoeven }
44982874d5eeSGeert Uytterhoeven 
4499c6fd2807SJeff Garzik /**
4500c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4501c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4502c6fd2807SJeff Garzik  *
4503c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4504c6fd2807SJeff Garzik  *
4505c6fd2807SJeff Garzik  *	LOCKING:
4506cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4507c6fd2807SJeff Garzik  *
4508c6fd2807SJeff Garzik  *	RETURNS:
4509c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4510c6fd2807SJeff Garzik  *
4511c6fd2807SJeff Garzik  */
4512c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4513c6fd2807SJeff Garzik {
4514c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4515dde20207SJames Bottomley 	unsigned int n_elem;
4516c6fd2807SJeff Garzik 
4517dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4518dde20207SJames Bottomley 	if (n_elem < 1)
4519c6fd2807SJeff Garzik 		return -1;
4520c6fd2807SJeff Garzik 
45215825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4522dde20207SJames Bottomley 	qc->n_elem = n_elem;
4523f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4524c6fd2807SJeff Garzik 
4525c6fd2807SJeff Garzik 	return 0;
4526c6fd2807SJeff Garzik }
4527c6fd2807SJeff Garzik 
45282874d5eeSGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
45292874d5eeSGeert Uytterhoeven 
45302874d5eeSGeert Uytterhoeven static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
45312874d5eeSGeert Uytterhoeven static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
45322874d5eeSGeert Uytterhoeven 
45332874d5eeSGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
45342874d5eeSGeert Uytterhoeven 
4535c6fd2807SJeff Garzik /**
4536c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4537c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4538c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4539c6fd2807SJeff Garzik  *
4540c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4541c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4542c6fd2807SJeff Garzik  *	vice-versa.
4543c6fd2807SJeff Garzik  *
4544c6fd2807SJeff Garzik  *	LOCKING:
4545c6fd2807SJeff Garzik  *	Inherited from caller.
4546c6fd2807SJeff Garzik  */
4547c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4548c6fd2807SJeff Garzik {
4549c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4550c6fd2807SJeff Garzik 	unsigned int i;
4551c6fd2807SJeff Garzik 
4552c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4553c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4554c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4555c6fd2807SJeff Garzik }
4556c6fd2807SJeff Garzik 
4557c6fd2807SJeff Garzik /**
4558c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4559c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
456038755e89SRandy Dunlap  *	@tag: tag
4561c6fd2807SJeff Garzik  *
4562c6fd2807SJeff Garzik  *	LOCKING:
4563c6fd2807SJeff Garzik  *	None.
4564c6fd2807SJeff Garzik  */
4565c6fd2807SJeff Garzik 
456698bd4be1SShaohua Li struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
4567c6fd2807SJeff Garzik {
45689af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4569c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4570c6fd2807SJeff Garzik 
457198bd4be1SShaohua Li 	/* no command while frozen */
457298bd4be1SShaohua Li 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
457398bd4be1SShaohua Li 		return NULL;
457498bd4be1SShaohua Li 
457598bd4be1SShaohua Li 	/* libsas case */
45765067c046SShaohua Li 	if (ap->flags & ATA_FLAG_SAS_HOST) {
457798bd4be1SShaohua Li 		tag = ata_sas_allocate_tag(ap);
457898bd4be1SShaohua Li 		if (tag < 0)
457998bd4be1SShaohua Li 			return NULL;
458098bd4be1SShaohua Li 	}
458198bd4be1SShaohua Li 
458298bd4be1SShaohua Li 	qc = __ata_qc_from_tag(ap, tag);
45835ac40790SJens Axboe 	qc->tag = qc->hw_tag = tag;
4584c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
4585c6fd2807SJeff Garzik 	qc->ap = ap;
4586c6fd2807SJeff Garzik 	qc->dev = dev;
4587c6fd2807SJeff Garzik 
4588c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
4589c6fd2807SJeff Garzik 
4590c6fd2807SJeff Garzik 	return qc;
4591c6fd2807SJeff Garzik }
4592c6fd2807SJeff Garzik 
45938a8bc223STejun Heo /**
45948a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
45958a8bc223STejun Heo  *	@qc: Command to complete
45968a8bc223STejun Heo  *
45978a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
45988a8bc223STejun Heo  *	in case something prevents using it.
45998a8bc223STejun Heo  *
46008a8bc223STejun Heo  *	LOCKING:
46018a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
46028a8bc223STejun Heo  */
46038a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
46048a8bc223STejun Heo {
4605a1104016SJulia Lawall 	struct ata_port *ap;
46068a8bc223STejun Heo 	unsigned int tag;
46078a8bc223STejun Heo 
4608efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4609a1104016SJulia Lawall 	ap = qc->ap;
46108a8bc223STejun Heo 
46118a8bc223STejun Heo 	qc->flags = 0;
46128a8bc223STejun Heo 	tag = qc->tag;
461328361c40SJens Axboe 	if (ata_tag_valid(tag)) {
46148a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
46155067c046SShaohua Li 		if (ap->flags & ATA_FLAG_SAS_HOST)
461698bd4be1SShaohua Li 			ata_sas_free_tag(tag, ap);
46178a8bc223STejun Heo 	}
46188a8bc223STejun Heo }
46198a8bc223STejun Heo 
4620c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4621c6fd2807SJeff Garzik {
4622a1104016SJulia Lawall 	struct ata_port *ap;
4623a1104016SJulia Lawall 	struct ata_link *link;
4624c6fd2807SJeff Garzik 
4625efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4626efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4627a1104016SJulia Lawall 	ap = qc->ap;
4628a1104016SJulia Lawall 	link = qc->dev->link;
4629c6fd2807SJeff Garzik 
4630c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4631c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4632c6fd2807SJeff Garzik 
4633c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4634179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
46354e5b6260SJens Axboe 		link->sactive &= ~(1 << qc->hw_tag);
4636da917d69STejun Heo 		if (!link->sactive)
4637da917d69STejun Heo 			ap->nr_active_links--;
4638da917d69STejun Heo 	} else {
46399af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4640da917d69STejun Heo 		ap->nr_active_links--;
4641da917d69STejun Heo 	}
4642da917d69STejun Heo 
4643da917d69STejun Heo 	/* clear exclusive status */
4644da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4645da917d69STejun Heo 		     ap->excl_link == link))
4646da917d69STejun Heo 		ap->excl_link = NULL;
4647c6fd2807SJeff Garzik 
4648c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4649c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4650c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4651c6fd2807SJeff Garzik 	 */
4652c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4653e3ed8939SJens Axboe 	ap->qc_active &= ~(1ULL << qc->tag);
4654c6fd2807SJeff Garzik 
4655c6fd2807SJeff Garzik 	/* call completion callback */
4656c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4657c6fd2807SJeff Garzik }
4658c6fd2807SJeff Garzik 
465939599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
466039599a53STejun Heo {
466139599a53STejun Heo 	struct ata_port *ap = qc->ap;
466239599a53STejun Heo 
466339599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
466422183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
466539599a53STejun Heo }
466639599a53STejun Heo 
466700115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
466800115e0fSTejun Heo {
466900115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
467000115e0fSTejun Heo 
4671eb0effdfSChristoph Hellwig 	if (!ata_is_data(qc->tf.protocol))
467200115e0fSTejun Heo 		return;
467300115e0fSTejun Heo 
467400115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
467500115e0fSTejun Heo 		return;
467600115e0fSTejun Heo 
467700115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
467800115e0fSTejun Heo }
467900115e0fSTejun Heo 
4680c6fd2807SJeff Garzik /**
4681c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4682c6fd2807SJeff Garzik  *	@qc: Command to complete
4683c6fd2807SJeff Garzik  *
46841aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
46851aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
46861aadf5c3STejun Heo  *
46871aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
46881aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
46891aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
46901aadf5c3STejun Heo  *	properly update IRQ expect state.
4691c6fd2807SJeff Garzik  *
4692c6fd2807SJeff Garzik  *	LOCKING:
4693cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4694c6fd2807SJeff Garzik  */
4695c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4696c6fd2807SJeff Garzik {
4697c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4698c6fd2807SJeff Garzik 
4699eb25cb99SStephan Linz 	/* Trigger the LED (if available) */
4700d1ed7c55SLinus Walleij 	ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
4701eb25cb99SStephan Linz 
4702c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4703c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4704c6fd2807SJeff Garzik 	 *
4705c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4706c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4707c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4708c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4709c6fd2807SJeff Garzik 	 *
4710c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4711c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4712c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4713c6fd2807SJeff Garzik 	 * taken care of.
4714c6fd2807SJeff Garzik 	 */
4715c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
47164dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
47174dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
47184dbfa39bSTejun Heo 
4719c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4720c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4721c6fd2807SJeff Garzik 
4722f08dc1acSTejun Heo 		/*
4723f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
4724f08dc1acSTejun Heo 		 * and always with the result TF filled.
4725f08dc1acSTejun Heo 		 */
4726f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
472739599a53STejun Heo 			fill_result_tf(qc);
4728255c03d1SHannes Reinecke 			trace_ata_qc_complete_internal(qc);
4729f4b31db9STejun Heo 			__ata_qc_complete(qc);
4730c6fd2807SJeff Garzik 			return;
4731c6fd2807SJeff Garzik 		}
4732c6fd2807SJeff Garzik 
4733f08dc1acSTejun Heo 		/*
4734f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
4735f08dc1acSTejun Heo 		 * summon EH.
4736f08dc1acSTejun Heo 		 */
4737f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4738f08dc1acSTejun Heo 			fill_result_tf(qc);
4739255c03d1SHannes Reinecke 			trace_ata_qc_complete_failed(qc);
4740f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
4741f08dc1acSTejun Heo 			return;
4742f08dc1acSTejun Heo 		}
4743f08dc1acSTejun Heo 
47444dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
47454dc738edSTejun Heo 
4746c6fd2807SJeff Garzik 		/* read result TF if requested */
4747c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
474839599a53STejun Heo 			fill_result_tf(qc);
4749c6fd2807SJeff Garzik 
4750255c03d1SHannes Reinecke 		trace_ata_qc_complete_done(qc);
47514dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
47524dbfa39bSTejun Heo 		 * completion.
47534dbfa39bSTejun Heo 		 */
47544dbfa39bSTejun Heo 		switch (qc->tf.command) {
47554dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
47564dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
47570c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_WC_OFF &&
47580c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_ON &&
47590c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_OFF)
47604dbfa39bSTejun Heo 				break;
4761df561f66SGustavo A. R. Silva 			fallthrough;
47624dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
47634dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
47644dbfa39bSTejun Heo 			/* revalidate device */
47654dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
47664dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
47674dbfa39bSTejun Heo 			break;
4768054a5fbaSTejun Heo 
4769054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4770054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4771054a5fbaSTejun Heo 			break;
47724dbfa39bSTejun Heo 		}
47734dbfa39bSTejun Heo 
477400115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
477500115e0fSTejun Heo 			ata_verify_xfer(qc);
477600115e0fSTejun Heo 
4777c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4778c6fd2807SJeff Garzik 	} else {
4779c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4780c6fd2807SJeff Garzik 			return;
4781c6fd2807SJeff Garzik 
4782c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4783c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
478439599a53STejun Heo 			fill_result_tf(qc);
4785c6fd2807SJeff Garzik 
4786c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4787c6fd2807SJeff Garzik 	}
4788c6fd2807SJeff Garzik }
4789a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_qc_complete);
4790c6fd2807SJeff Garzik 
4791c6fd2807SJeff Garzik /**
47928385d756SSascha Hauer  *	ata_qc_get_active - get bitmask of active qcs
47938385d756SSascha Hauer  *	@ap: port in question
47948385d756SSascha Hauer  *
47958385d756SSascha Hauer  *	LOCKING:
47968385d756SSascha Hauer  *	spin_lock_irqsave(host lock)
47978385d756SSascha Hauer  *
47988385d756SSascha Hauer  *	RETURNS:
47998385d756SSascha Hauer  *	Bitmask of active qcs
48008385d756SSascha Hauer  */
48018385d756SSascha Hauer u64 ata_qc_get_active(struct ata_port *ap)
48028385d756SSascha Hauer {
48038385d756SSascha Hauer 	u64 qc_active = ap->qc_active;
48048385d756SSascha Hauer 
48058385d756SSascha Hauer 	/* ATA_TAG_INTERNAL is sent to hw as tag 0 */
48068385d756SSascha Hauer 	if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
48078385d756SSascha Hauer 		qc_active |= (1 << 0);
48088385d756SSascha Hauer 		qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
48098385d756SSascha Hauer 	}
48108385d756SSascha Hauer 
48118385d756SSascha Hauer 	return qc_active;
48128385d756SSascha Hauer }
48138385d756SSascha Hauer EXPORT_SYMBOL_GPL(ata_qc_get_active);
48148385d756SSascha Hauer 
48158385d756SSascha Hauer /**
4816c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4817c6fd2807SJeff Garzik  *	@qc: command to issue to device
4818c6fd2807SJeff Garzik  *
4819c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4820c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4821c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
4822c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
4823c6fd2807SJeff Garzik  *
4824c6fd2807SJeff Garzik  *	LOCKING:
4825cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4826c6fd2807SJeff Garzik  */
4827c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
4828c6fd2807SJeff Garzik {
4829c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
48309af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4831405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
4832c6fd2807SJeff Garzik 
4833c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
4834c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
4835c6fd2807SJeff Garzik 	 * request ATAPI sense.
4836c6fd2807SJeff Garzik 	 */
4837efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
4838c6fd2807SJeff Garzik 
48391973a023STejun Heo 	if (ata_is_ncq(prot)) {
48404e5b6260SJens Axboe 		WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
4841da917d69STejun Heo 
4842da917d69STejun Heo 		if (!link->sactive)
4843da917d69STejun Heo 			ap->nr_active_links++;
48444e5b6260SJens Axboe 		link->sactive |= 1 << qc->hw_tag;
4845c6fd2807SJeff Garzik 	} else {
4846efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
4847da917d69STejun Heo 
4848da917d69STejun Heo 		ap->nr_active_links++;
48499af5c9c9STejun Heo 		link->active_tag = qc->tag;
4850c6fd2807SJeff Garzik 	}
4851c6fd2807SJeff Garzik 
4852c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
4853e3ed8939SJens Axboe 	ap->qc_active |= 1ULL << qc->tag;
4854c6fd2807SJeff Garzik 
485560f5d6efSTejun Heo 	/*
485660f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
4857f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
4858f92a2636STejun Heo 	 */
48599173e5e8SEric Biggers 	if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
486060f5d6efSTejun Heo 		goto sys_err;
4861f92a2636STejun Heo 
4862405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
4863f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
4864c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
486560f5d6efSTejun Heo 			goto sys_err;
4866c6fd2807SJeff Garzik 
4867cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
4868054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
4869cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
4870054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4871054a5fbaSTejun Heo 		ata_link_abort(link);
4872054a5fbaSTejun Heo 		return;
4873054a5fbaSTejun Heo 	}
4874054a5fbaSTejun Heo 
4875fc914faaSHannes Reinecke 	trace_ata_qc_prep(qc);
487695364f36SJiri Slaby 	qc->err_mask |= ap->ops->qc_prep(qc);
487795364f36SJiri Slaby 	if (unlikely(qc->err_mask))
487895364f36SJiri Slaby 		goto err;
4879255c03d1SHannes Reinecke 	trace_ata_qc_issue(qc);
4880c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
4881c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
4882c6fd2807SJeff Garzik 		goto err;
4883c6fd2807SJeff Garzik 	return;
4884c6fd2807SJeff Garzik 
488560f5d6efSTejun Heo sys_err:
4886c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
4887c6fd2807SJeff Garzik err:
4888c6fd2807SJeff Garzik 	ata_qc_complete(qc);
4889c6fd2807SJeff Garzik }
4890c6fd2807SJeff Garzik 
4891c6fd2807SJeff Garzik /**
4892b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
4893936fd732STejun Heo  *	@link: ATA link to test
4894c6fd2807SJeff Garzik  *
4895936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
4896936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
4897936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4898c6fd2807SJeff Garzik  *
4899c6fd2807SJeff Garzik  *	LOCKING:
4900c6fd2807SJeff Garzik  *	None.
4901c6fd2807SJeff Garzik  *
4902c6fd2807SJeff Garzik  *	RETURNS:
4903b5b3fa38STejun Heo  *	True if the port online status is available and online.
4904c6fd2807SJeff Garzik  */
4905b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
4906c6fd2807SJeff Garzik {
4907c6fd2807SJeff Garzik 	u32 sstatus;
4908c6fd2807SJeff Garzik 
4909936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
49109913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
4911b5b3fa38STejun Heo 		return true;
4912b5b3fa38STejun Heo 	return false;
4913c6fd2807SJeff Garzik }
4914c6fd2807SJeff Garzik 
4915c6fd2807SJeff Garzik /**
4916b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
4917936fd732STejun Heo  *	@link: ATA link to test
4918c6fd2807SJeff Garzik  *
4919936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
4920936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
4921936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4922c6fd2807SJeff Garzik  *
4923c6fd2807SJeff Garzik  *	LOCKING:
4924c6fd2807SJeff Garzik  *	None.
4925c6fd2807SJeff Garzik  *
4926c6fd2807SJeff Garzik  *	RETURNS:
4927b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
4928c6fd2807SJeff Garzik  */
4929b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
4930c6fd2807SJeff Garzik {
4931c6fd2807SJeff Garzik 	u32 sstatus;
4932c6fd2807SJeff Garzik 
4933936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
49349913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
4935b5b3fa38STejun Heo 		return true;
4936b5b3fa38STejun Heo 	return false;
4937c6fd2807SJeff Garzik }
4938c6fd2807SJeff Garzik 
4939b1c72916STejun Heo /**
4940b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
4941b1c72916STejun Heo  *	@link: ATA link to test
4942b1c72916STejun Heo  *
4943b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
4944b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
4945b1c72916STejun Heo  *	there's a slave link, this function should only be called on
4946b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
4947b1c72916STejun Heo  *	online.
4948b1c72916STejun Heo  *
4949b1c72916STejun Heo  *	LOCKING:
4950b1c72916STejun Heo  *	None.
4951b1c72916STejun Heo  *
4952b1c72916STejun Heo  *	RETURNS:
4953b1c72916STejun Heo  *	True if the port online status is available and online.
4954b1c72916STejun Heo  */
4955b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
4956b1c72916STejun Heo {
4957b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
4958b1c72916STejun Heo 
4959b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
4960b1c72916STejun Heo 
4961b1c72916STejun Heo 	return ata_phys_link_online(link) ||
4962b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
4963b1c72916STejun Heo }
4964a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_online);
4965b1c72916STejun Heo 
4966b1c72916STejun Heo /**
4967b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
4968b1c72916STejun Heo  *	@link: ATA link to test
4969b1c72916STejun Heo  *
4970b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
4971b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
4972b1c72916STejun Heo  *	there's a slave link, this function should only be called on
4973b1c72916STejun Heo  *	the master link and will return true if both M/S links are
4974b1c72916STejun Heo  *	offline.
4975b1c72916STejun Heo  *
4976b1c72916STejun Heo  *	LOCKING:
4977b1c72916STejun Heo  *	None.
4978b1c72916STejun Heo  *
4979b1c72916STejun Heo  *	RETURNS:
4980b1c72916STejun Heo  *	True if the port offline status is available and offline.
4981b1c72916STejun Heo  */
4982b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
4983b1c72916STejun Heo {
4984b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
4985b1c72916STejun Heo 
4986b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
4987b1c72916STejun Heo 
4988b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
4989b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
4990b1c72916STejun Heo }
4991a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_offline);
4992b1c72916STejun Heo 
49936ffa01d8STejun Heo #ifdef CONFIG_PM
4994bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
4995cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
4996bc6e7c4bSDan Williams 				bool async)
4997c6fd2807SJeff Garzik {
4998e3667ebfSTejun Heo 	struct ata_link *link;
49995ef41082SLin Ming 	unsigned long flags;
5000c6fd2807SJeff Garzik 
5001c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5002c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5003c6fd2807SJeff Garzik 	 */
5004c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5005c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5006c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5007c6fd2807SJeff Garzik 	}
5008c6fd2807SJeff Garzik 
5009c6fd2807SJeff Garzik 	/* request PM ops to EH */
5010c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5011c6fd2807SJeff Garzik 
5012c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5013c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
50141eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5015e3667ebfSTejun Heo 		link->eh_info.action |= action;
5016e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5017e3667ebfSTejun Heo 	}
5018c6fd2807SJeff Garzik 
5019c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5020c6fd2807SJeff Garzik 
5021c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5022c6fd2807SJeff Garzik 
50232fcbdcb4SDan Williams 	if (!async) {
5024c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5025c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5026c6fd2807SJeff Garzik 	}
5027c6fd2807SJeff Garzik }
5028c6fd2807SJeff Garzik 
502933574d68SLin Ming /*
5030bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
5031bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
5032bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
503333574d68SLin Ming  *
503433574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
503533574d68SLin Ming  */
5036bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5037bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
5038bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
5039bc6e7c4bSDan Williams 
5040bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5041bc6e7c4bSDan Williams {
5042bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
50435ef41082SLin Ming }
50445ef41082SLin Ming 
5045bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5046bc6e7c4bSDan Williams {
5047bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5048bc6e7c4bSDan Williams }
5049bc6e7c4bSDan Williams 
5050bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
50512fcbdcb4SDan Williams {
50522fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
50532fcbdcb4SDan Williams 
50545ef41082SLin Ming 	if (pm_runtime_suspended(dev))
50555ef41082SLin Ming 		return 0;
50565ef41082SLin Ming 
5057bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5058f5e6d0d0SAaron Lu 	return 0;
505933574d68SLin Ming }
506033574d68SLin Ming 
5061bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
50622fcbdcb4SDan Williams {
50632fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
50642fcbdcb4SDan Williams 
5065bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5066bc6e7c4bSDan Williams 		return 0;
5067bc6e7c4bSDan Williams 
5068bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5069bc6e7c4bSDan Williams 	return 0;
50702fcbdcb4SDan Williams }
50712fcbdcb4SDan Williams 
5072bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5073e90b1e5aSLin Ming {
5074bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5075bc6e7c4bSDan Williams 	return 0;
5076bc6e7c4bSDan Williams }
5077e90b1e5aSLin Ming 
5078bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5079bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5080bc6e7c4bSDan Williams 
5081bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5082bc6e7c4bSDan Williams {
5083bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5084bc6e7c4bSDan Williams }
5085bc6e7c4bSDan Williams 
5086bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5087bc6e7c4bSDan Williams {
5088bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5089bc6e7c4bSDan Williams }
5090bc6e7c4bSDan Williams 
5091bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5092bc6e7c4bSDan Williams {
5093200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5094e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5095e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5096e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5097bc6e7c4bSDan Williams 	return 0;
5098e90b1e5aSLin Ming }
5099e90b1e5aSLin Ming 
51007e15e9beSAaron Lu /*
51017e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
51027e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
51037e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
51047e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
51057e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
51067e15e9beSAaron Lu  * ODD is attached to the port.
51077e15e9beSAaron Lu  */
51089ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
51099ee4f393SLin Ming {
51107e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
51117e15e9beSAaron Lu 	struct ata_link *link;
51127e15e9beSAaron Lu 	struct ata_device *adev;
51137e15e9beSAaron Lu 
51147e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
51157e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
51167e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
51177e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
51187e15e9beSAaron Lu 				return -EBUSY;
51197e15e9beSAaron Lu 	}
51207e15e9beSAaron Lu 
512145f0a85cSRafael J. Wysocki 	return 0;
51229ee4f393SLin Ming }
51239ee4f393SLin Ming 
5124a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5125a7ff60dbSAaron Lu {
5126bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5127bc6e7c4bSDan Williams 	return 0;
5128a7ff60dbSAaron Lu }
5129a7ff60dbSAaron Lu 
5130a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5131a7ff60dbSAaron Lu {
5132bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5133bc6e7c4bSDan Williams 	return 0;
5134a7ff60dbSAaron Lu }
5135a7ff60dbSAaron Lu 
51365ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5137bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5138bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5139bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5140bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5141bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5142bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
51439ee4f393SLin Ming 
5144a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5145a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
51469ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
51475ef41082SLin Ming };
51485ef41082SLin Ming 
51492fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
51502fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
51512fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
51522fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
51532fcbdcb4SDan Williams  */
5154bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
51552fcbdcb4SDan Williams {
5156bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
51572fcbdcb4SDan Williams }
5158bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
51592fcbdcb4SDan Williams 
5160bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
51612fcbdcb4SDan Williams {
5162bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
51632fcbdcb4SDan Williams }
5164bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
51652fcbdcb4SDan Williams 
5166c6fd2807SJeff Garzik /**
5167cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5168cca3974eSJeff Garzik  *	@host: host to suspend
5169c6fd2807SJeff Garzik  *	@mesg: PM message
5170c6fd2807SJeff Garzik  *
51715ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5172c6fd2807SJeff Garzik  */
5173cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5174c6fd2807SJeff Garzik {
517572ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
51765ef41082SLin Ming 	return 0;
5177c6fd2807SJeff Garzik }
5178a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_suspend);
5179c6fd2807SJeff Garzik 
5180c6fd2807SJeff Garzik /**
5181cca3974eSJeff Garzik  *	ata_host_resume - resume host
5182cca3974eSJeff Garzik  *	@host: host to resume
5183c6fd2807SJeff Garzik  *
51845ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5185c6fd2807SJeff Garzik  */
5186cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5187c6fd2807SJeff Garzik {
518872ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5189c6fd2807SJeff Garzik }
5190a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_resume);
51916ffa01d8STejun Heo #endif
5192c6fd2807SJeff Garzik 
51938df82c13SBhumika Goyal const struct device_type ata_port_type = {
51945ef41082SLin Ming 	.name = "ata_port",
51955ef41082SLin Ming #ifdef CONFIG_PM
51965ef41082SLin Ming 	.pm = &ata_port_pm_ops,
51975ef41082SLin Ming #endif
51985ef41082SLin Ming };
51995ef41082SLin Ming 
5200c6fd2807SJeff Garzik /**
5201c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5202c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5203c6fd2807SJeff Garzik  *
5204c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5205c6fd2807SJeff Garzik  *
5206c6fd2807SJeff Garzik  *	LOCKING:
5207c6fd2807SJeff Garzik  *	Inherited from caller.
5208c6fd2807SJeff Garzik  */
5209c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5210c6fd2807SJeff Garzik {
5211b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
52129af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5213c6fd2807SJeff Garzik 	unsigned long flags;
5214c6fd2807SJeff Garzik 
5215b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
52169af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
52179af5c9c9STejun Heo 	link->sata_spd = 0;
5218c6fd2807SJeff Garzik 
5219c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5220c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5221cca3974eSJeff Garzik 	 * host lock.
5222c6fd2807SJeff Garzik 	 */
5223c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5224c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
52253dcc323fSTejun Heo 	dev->horkage = 0;
5226c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5227c6fd2807SJeff Garzik 
522899cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
522999cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5230c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5231c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5232c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5233c6fd2807SJeff Garzik }
5234c6fd2807SJeff Garzik 
5235c6fd2807SJeff Garzik /**
52364fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
52374fb37a25STejun Heo  *	@ap: ATA port link is attached to
52384fb37a25STejun Heo  *	@link: Link structure to initialize
52398989805dSTejun Heo  *	@pmp: Port multiplier port number
52404fb37a25STejun Heo  *
52414fb37a25STejun Heo  *	Initialize @link.
52424fb37a25STejun Heo  *
52434fb37a25STejun Heo  *	LOCKING:
52444fb37a25STejun Heo  *	Kernel thread context (may sleep)
52454fb37a25STejun Heo  */
5246fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
52474fb37a25STejun Heo {
52484fb37a25STejun Heo 	int i;
52494fb37a25STejun Heo 
52504fb37a25STejun Heo 	/* clear everything except for devices */
5251d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5252d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
52534fb37a25STejun Heo 
52544fb37a25STejun Heo 	link->ap = ap;
52558989805dSTejun Heo 	link->pmp = pmp;
52564fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
52574fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
52584fb37a25STejun Heo 
52594fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
52604fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
52614fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
52624fb37a25STejun Heo 
52634fb37a25STejun Heo 		dev->link = link;
52644fb37a25STejun Heo 		dev->devno = dev - link->device;
5265110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5266110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5267110f66d2STejun Heo #endif
52684fb37a25STejun Heo 		ata_dev_init(dev);
52694fb37a25STejun Heo 	}
52704fb37a25STejun Heo }
52714fb37a25STejun Heo 
52724fb37a25STejun Heo /**
52734fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
52744fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
52754fb37a25STejun Heo  *
5276a31a6997SMauro Carvalho Chehab  *	Initialize ``link->[hw_]sata_spd_limit`` to the currently
52774fb37a25STejun Heo  *	configured value.
52784fb37a25STejun Heo  *
52794fb37a25STejun Heo  *	LOCKING:
52804fb37a25STejun Heo  *	Kernel thread context (may sleep).
52814fb37a25STejun Heo  *
52824fb37a25STejun Heo  *	RETURNS:
52834fb37a25STejun Heo  *	0 on success, -errno on failure.
52844fb37a25STejun Heo  */
5285fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
52864fb37a25STejun Heo {
528733267325STejun Heo 	u8 spd;
52884fb37a25STejun Heo 	int rc;
52894fb37a25STejun Heo 
5290d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
52914fb37a25STejun Heo 	if (rc)
52924fb37a25STejun Heo 		return rc;
52934fb37a25STejun Heo 
5294d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
52954fb37a25STejun Heo 	if (spd)
52964fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
52974fb37a25STejun Heo 
529805944bdfSTejun Heo 	ata_force_link_limits(link);
529933267325STejun Heo 
53004fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
53014fb37a25STejun Heo 
53024fb37a25STejun Heo 	return 0;
53034fb37a25STejun Heo }
53044fb37a25STejun Heo 
53054fb37a25STejun Heo /**
5306f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5307f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5308c6fd2807SJeff Garzik  *
5309f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5310f3187195STejun Heo  *
5311f3187195STejun Heo  *	RETURNS:
5312f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5313c6fd2807SJeff Garzik  *
5314c6fd2807SJeff Garzik  *	LOCKING:
5315f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5316c6fd2807SJeff Garzik  */
5317f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5318c6fd2807SJeff Garzik {
5319f3187195STejun Heo 	struct ata_port *ap;
5320c6fd2807SJeff Garzik 
5321f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5322f3187195STejun Heo 	if (!ap)
5323f3187195STejun Heo 		return NULL;
5324f3187195STejun Heo 
53257b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5326cca3974eSJeff Garzik 	ap->lock = &host->lock;
5327f3187195STejun Heo 	ap->print_id = -1;
5328e628dc99SDavid Milburn 	ap->local_port_no = -1;
5329cca3974eSJeff Garzik 	ap->host = host;
5330f3187195STejun Heo 	ap->dev = host->dev;
5331c6fd2807SJeff Garzik 
5332ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
533365f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
533465f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5335c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5336c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
533745fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
5338b93ab338SKees Cook 	timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5339b93ab338SKees Cook 		    TIMER_DEFERRABLE);
5340c6fd2807SJeff Garzik 
5341c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5342c6fd2807SJeff Garzik 
53438989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5344c6fd2807SJeff Garzik 
5345c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5346c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5347c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5348c6fd2807SJeff Garzik #endif
5349270390e1STejun Heo 	ata_sff_port_init(ap);
5350270390e1STejun Heo 
5351c6fd2807SJeff Garzik 	return ap;
5352c6fd2807SJeff Garzik }
5353c6fd2807SJeff Garzik 
53542623c7a5STaras Kondratiuk static void ata_devres_release(struct device *gendev, void *res)
5355f0d36efdSTejun Heo {
5356f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5357f0d36efdSTejun Heo 	int i;
5358f0d36efdSTejun Heo 
5359f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5360f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5361f0d36efdSTejun Heo 
5362ecef7253STejun Heo 		if (!ap)
5363ecef7253STejun Heo 			continue;
5364ecef7253STejun Heo 
53654911487aSTejun Heo 		if (ap->scsi_host)
53661aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
53671aa506e4STejun Heo 
53682623c7a5STaras Kondratiuk 	}
53692623c7a5STaras Kondratiuk 
53702623c7a5STaras Kondratiuk 	dev_set_drvdata(gendev, NULL);
53712623c7a5STaras Kondratiuk 	ata_host_put(host);
53722623c7a5STaras Kondratiuk }
53732623c7a5STaras Kondratiuk 
53742623c7a5STaras Kondratiuk static void ata_host_release(struct kref *kref)
53752623c7a5STaras Kondratiuk {
53762623c7a5STaras Kondratiuk 	struct ata_host *host = container_of(kref, struct ata_host, kref);
53772623c7a5STaras Kondratiuk 	int i;
53782623c7a5STaras Kondratiuk 
53792623c7a5STaras Kondratiuk 	for (i = 0; i < host->n_ports; i++) {
53802623c7a5STaras Kondratiuk 		struct ata_port *ap = host->ports[i];
53812623c7a5STaras Kondratiuk 
5382633273a3STejun Heo 		kfree(ap->pmp_link);
5383b1c72916STejun Heo 		kfree(ap->slave_link);
53844911487aSTejun Heo 		kfree(ap);
53851aa506e4STejun Heo 		host->ports[i] = NULL;
53861aa506e4STejun Heo 	}
53872623c7a5STaras Kondratiuk 	kfree(host);
53882623c7a5STaras Kondratiuk }
53891aa506e4STejun Heo 
53902623c7a5STaras Kondratiuk void ata_host_get(struct ata_host *host)
53912623c7a5STaras Kondratiuk {
53922623c7a5STaras Kondratiuk 	kref_get(&host->kref);
53932623c7a5STaras Kondratiuk }
53942623c7a5STaras Kondratiuk 
53952623c7a5STaras Kondratiuk void ata_host_put(struct ata_host *host)
53962623c7a5STaras Kondratiuk {
53972623c7a5STaras Kondratiuk 	kref_put(&host->kref, ata_host_release);
5398f0d36efdSTejun Heo }
5399a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_put);
5400f0d36efdSTejun Heo 
5401c6fd2807SJeff Garzik /**
5402f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5403f3187195STejun Heo  *	@dev: generic device this host is associated with
5404f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5405f3187195STejun Heo  *
5406f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5407f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5408f3187195STejun Heo  *	attaches it using ata_host_register().
5409f3187195STejun Heo  *
5410f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5411f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5412f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5413f3187195STejun Heo  *	ports will be automatically freed on registration.
5414f3187195STejun Heo  *
5415f3187195STejun Heo  *	RETURNS:
5416f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5417f3187195STejun Heo  *
5418f3187195STejun Heo  *	LOCKING:
5419f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5420f3187195STejun Heo  */
5421f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5422f3187195STejun Heo {
5423f3187195STejun Heo 	struct ata_host *host;
5424f3187195STejun Heo 	size_t sz;
5425f3187195STejun Heo 	int i;
54262623c7a5STaras Kondratiuk 	void *dr;
5427f3187195STejun Heo 
5428f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5429f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
54302623c7a5STaras Kondratiuk 	host = kzalloc(sz, GFP_KERNEL);
5431f3187195STejun Heo 	if (!host)
54322623c7a5STaras Kondratiuk 		return NULL;
54332623c7a5STaras Kondratiuk 
5434f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5435dafd6c49SColin Ian King 		goto err_free;
5436f3187195STejun Heo 
54372623c7a5STaras Kondratiuk 	dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
54382623c7a5STaras Kondratiuk 	if (!dr)
5439f3187195STejun Heo 		goto err_out;
5440f3187195STejun Heo 
54412623c7a5STaras Kondratiuk 	devres_add(dev, dr);
5442f3187195STejun Heo 	dev_set_drvdata(dev, host);
5443f3187195STejun Heo 
5444f3187195STejun Heo 	spin_lock_init(&host->lock);
5445c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5446f3187195STejun Heo 	host->dev = dev;
5447f3187195STejun Heo 	host->n_ports = max_ports;
54482623c7a5STaras Kondratiuk 	kref_init(&host->kref);
5449f3187195STejun Heo 
5450f3187195STejun Heo 	/* allocate ports bound to this host */
5451f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5452f3187195STejun Heo 		struct ata_port *ap;
5453f3187195STejun Heo 
5454f3187195STejun Heo 		ap = ata_port_alloc(host);
5455f3187195STejun Heo 		if (!ap)
5456f3187195STejun Heo 			goto err_out;
5457f3187195STejun Heo 
5458f3187195STejun Heo 		ap->port_no = i;
5459f3187195STejun Heo 		host->ports[i] = ap;
5460f3187195STejun Heo 	}
5461f3187195STejun Heo 
5462f3187195STejun Heo 	devres_remove_group(dev, NULL);
5463f3187195STejun Heo 	return host;
5464f3187195STejun Heo 
5465f3187195STejun Heo  err_out:
5466f3187195STejun Heo 	devres_release_group(dev, NULL);
5467dafd6c49SColin Ian King  err_free:
5468dafd6c49SColin Ian King 	kfree(host);
5469f3187195STejun Heo 	return NULL;
5470f3187195STejun Heo }
5471a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc);
5472f3187195STejun Heo 
5473f3187195STejun Heo /**
5474f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5475f5cda257STejun Heo  *	@dev: generic device this host is associated with
5476f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5477f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5478f5cda257STejun Heo  *
5479f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5480f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5481f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5482f5cda257STejun Heo  *
5483f5cda257STejun Heo  *	RETURNS:
5484f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5485f5cda257STejun Heo  *
5486f5cda257STejun Heo  *	LOCKING:
5487f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5488f5cda257STejun Heo  */
5489f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5490f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5491f5cda257STejun Heo 				      int n_ports)
5492f5cda257STejun Heo {
5493f5cda257STejun Heo 	const struct ata_port_info *pi;
5494f5cda257STejun Heo 	struct ata_host *host;
5495f5cda257STejun Heo 	int i, j;
5496f5cda257STejun Heo 
5497f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5498f5cda257STejun Heo 	if (!host)
5499f5cda257STejun Heo 		return NULL;
5500f5cda257STejun Heo 
5501f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5502f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5503f5cda257STejun Heo 
5504f5cda257STejun Heo 		if (ppi[j])
5505f5cda257STejun Heo 			pi = ppi[j++];
5506f5cda257STejun Heo 
5507f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5508f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5509f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5510f5cda257STejun Heo 		ap->flags |= pi->flags;
55110c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5512f5cda257STejun Heo 		ap->ops = pi->port_ops;
5513f5cda257STejun Heo 
5514f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5515f5cda257STejun Heo 			host->ops = pi->port_ops;
5516f5cda257STejun Heo 	}
5517f5cda257STejun Heo 
5518f5cda257STejun Heo 	return host;
5519f5cda257STejun Heo }
5520a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
5521f5cda257STejun Heo 
552232ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
552332ebbc0cSTejun Heo {
552432ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
552532ebbc0cSTejun Heo 	int i;
552632ebbc0cSTejun Heo 
552732ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
552832ebbc0cSTejun Heo 
552932ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
553032ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
553132ebbc0cSTejun Heo 
553232ebbc0cSTejun Heo 		if (ap->ops->port_stop)
553332ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
553432ebbc0cSTejun Heo 	}
553532ebbc0cSTejun Heo 
553632ebbc0cSTejun Heo 	if (host->ops->host_stop)
553732ebbc0cSTejun Heo 		host->ops->host_stop(host);
553832ebbc0cSTejun Heo }
553932ebbc0cSTejun Heo 
5540f5cda257STejun Heo /**
5541029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5542029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5543029cfd6bSTejun Heo  *
5544029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5545029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5546029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5547029cfd6bSTejun Heo  *	inheritance chain.
5548029cfd6bSTejun Heo  *
5549029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5550029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5551029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5552029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5553029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5554029cfd6bSTejun Heo  *
5555029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5556029cfd6bSTejun Heo  *
5557029cfd6bSTejun Heo  *	LOCKING:
5558029cfd6bSTejun Heo  *	None.
5559029cfd6bSTejun Heo  */
5560029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5561029cfd6bSTejun Heo {
55622da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5563029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5564029cfd6bSTejun Heo 	void **begin = (void **)ops;
5565029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5566029cfd6bSTejun Heo 	void **pp;
5567029cfd6bSTejun Heo 
5568029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5569029cfd6bSTejun Heo 		return;
5570029cfd6bSTejun Heo 
5571029cfd6bSTejun Heo 	spin_lock(&lock);
5572029cfd6bSTejun Heo 
5573029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5574029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5575029cfd6bSTejun Heo 
5576029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5577029cfd6bSTejun Heo 			if (!*pp)
5578029cfd6bSTejun Heo 				*pp = *inherit;
5579029cfd6bSTejun Heo 	}
5580029cfd6bSTejun Heo 
5581029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5582029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5583029cfd6bSTejun Heo 			*pp = NULL;
5584029cfd6bSTejun Heo 
5585029cfd6bSTejun Heo 	ops->inherits = NULL;
5586029cfd6bSTejun Heo 
5587029cfd6bSTejun Heo 	spin_unlock(&lock);
5588029cfd6bSTejun Heo }
5589029cfd6bSTejun Heo 
5590029cfd6bSTejun Heo /**
5591ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5592ecef7253STejun Heo  *	@host: ATA host to start ports for
5593ecef7253STejun Heo  *
5594ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5595ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5596ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5597f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
5598f3187195STejun Heo  *	first non-dummy port ops.
5599ecef7253STejun Heo  *
5600ecef7253STejun Heo  *	LOCKING:
5601ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5602ecef7253STejun Heo  *
5603ecef7253STejun Heo  *	RETURNS:
5604ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5605ecef7253STejun Heo  */
5606ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5607ecef7253STejun Heo {
560832ebbc0cSTejun Heo 	int have_stop = 0;
560932ebbc0cSTejun Heo 	void *start_dr = NULL;
5610ecef7253STejun Heo 	int i, rc;
5611ecef7253STejun Heo 
5612ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5613ecef7253STejun Heo 		return 0;
5614ecef7253STejun Heo 
5615029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5616029cfd6bSTejun Heo 
5617ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5618ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5619ecef7253STejun Heo 
5620029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5621029cfd6bSTejun Heo 
5622f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5623f3187195STejun Heo 			host->ops = ap->ops;
5624f3187195STejun Heo 
562532ebbc0cSTejun Heo 		if (ap->ops->port_stop)
562632ebbc0cSTejun Heo 			have_stop = 1;
562732ebbc0cSTejun Heo 	}
562832ebbc0cSTejun Heo 
5629355a8031SDamien Le Moal 	if (host->ops && host->ops->host_stop)
563032ebbc0cSTejun Heo 		have_stop = 1;
563132ebbc0cSTejun Heo 
563232ebbc0cSTejun Heo 	if (have_stop) {
563332ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
563432ebbc0cSTejun Heo 		if (!start_dr)
563532ebbc0cSTejun Heo 			return -ENOMEM;
563632ebbc0cSTejun Heo 	}
563732ebbc0cSTejun Heo 
563832ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
563932ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
564032ebbc0cSTejun Heo 
5641ecef7253STejun Heo 		if (ap->ops->port_start) {
5642ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5643ecef7253STejun Heo 			if (rc) {
56440f9fe9b7SAlan Cox 				if (rc != -ENODEV)
5645a44fec1fSJoe Perches 					dev_err(host->dev,
5646a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
5647a44fec1fSJoe Perches 						i, rc);
5648ecef7253STejun Heo 				goto err_out;
5649ecef7253STejun Heo 			}
5650ecef7253STejun Heo 		}
5651ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5652ecef7253STejun Heo 	}
5653ecef7253STejun Heo 
565432ebbc0cSTejun Heo 	if (start_dr)
565532ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5656ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5657ecef7253STejun Heo 	return 0;
5658ecef7253STejun Heo 
5659ecef7253STejun Heo  err_out:
5660ecef7253STejun Heo 	while (--i >= 0) {
5661ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5662ecef7253STejun Heo 
5663ecef7253STejun Heo 		if (ap->ops->port_stop)
5664ecef7253STejun Heo 			ap->ops->port_stop(ap);
5665ecef7253STejun Heo 	}
566632ebbc0cSTejun Heo 	devres_free(start_dr);
5667ecef7253STejun Heo 	return rc;
5668ecef7253STejun Heo }
5669a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_start);
5670ecef7253STejun Heo 
5671ecef7253STejun Heo /**
567294bd5719SMauro Carvalho Chehab  *	ata_host_init - Initialize a host struct for sas (ipr, libsas)
5673cca3974eSJeff Garzik  *	@host:	host to initialize
5674cca3974eSJeff Garzik  *	@dev:	device host is attached to
5675c6fd2807SJeff Garzik  *	@ops:	port_ops
5676c6fd2807SJeff Garzik  *
5677c6fd2807SJeff Garzik  */
5678cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
56798d8e7d13SDan Williams 		   struct ata_port_operations *ops)
5680c6fd2807SJeff Garzik {
5681cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5682c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
568369278f79SJens Axboe 	host->n_tags = ATA_MAX_QUEUE;
5684cca3974eSJeff Garzik 	host->dev = dev;
5685cca3974eSJeff Garzik 	host->ops = ops;
56862fa4a326SJason Yan 	kref_init(&host->kref);
5687c6fd2807SJeff Garzik }
5688a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_init);
5689c6fd2807SJeff Garzik 
56909508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
569179318057SArjan van de Ven {
569279318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
569379318057SArjan van de Ven 	unsigned long flags;
569479318057SArjan van de Ven 
569579318057SArjan van de Ven 	/* kick EH for boot probing */
569679318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
569779318057SArjan van de Ven 
569879318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
56996b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
570079318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
570179318057SArjan van de Ven 
570279318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
570379318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
570479318057SArjan van de Ven 	ata_port_schedule_eh(ap);
570579318057SArjan van de Ven 
570679318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
57079508a66fSDan Williams }
570879318057SArjan van de Ven 
57099508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
57109508a66fSDan Williams {
57119508a66fSDan Williams 	int rc = 0;
57129508a66fSDan Williams 
57139508a66fSDan Williams 	if (ap->ops->error_handler) {
57149508a66fSDan Williams 		__ata_port_probe(ap);
571579318057SArjan van de Ven 		ata_port_wait_eh(ap);
571679318057SArjan van de Ven 	} else {
571779318057SArjan van de Ven 		rc = ata_bus_probe(ap);
5718238c9cf9SJames Bottomley 	}
5719238c9cf9SJames Bottomley 	return rc;
5720238c9cf9SJames Bottomley }
572179318057SArjan van de Ven 
5722238c9cf9SJames Bottomley 
5723238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
5724238c9cf9SJames Bottomley {
5725238c9cf9SJames Bottomley 	struct ata_port *ap = data;
5726238c9cf9SJames Bottomley 
5727238c9cf9SJames Bottomley 	/*
5728238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
5729238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
5730238c9cf9SJames Bottomley 	 * before going further.
5731238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
5732238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
573379318057SArjan van de Ven 	 */
5734238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5735238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
5736238c9cf9SJames Bottomley 
5737238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
5738f29d3b23SArjan van de Ven 
5739f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
5740f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
5741f29d3b23SArjan van de Ven 
5742f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
574379318057SArjan van de Ven }
5744238c9cf9SJames Bottomley 
5745c6fd2807SJeff Garzik /**
5746f3187195STejun Heo  *	ata_host_register - register initialized ATA host
5747f3187195STejun Heo  *	@host: ATA host to register
5748f3187195STejun Heo  *	@sht: template for SCSI host
5749c6fd2807SJeff Garzik  *
5750f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
5751f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
5752f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
5753f3187195STejun Heo  *	probe registered devices.
5754c6fd2807SJeff Garzik  *
5755c6fd2807SJeff Garzik  *	LOCKING:
5756f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5757c6fd2807SJeff Garzik  *
5758c6fd2807SJeff Garzik  *	RETURNS:
5759f3187195STejun Heo  *	0 on success, -errno otherwise.
5760c6fd2807SJeff Garzik  */
5761f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
5762c6fd2807SJeff Garzik {
5763f3187195STejun Heo 	int i, rc;
5764c6fd2807SJeff Garzik 
576569278f79SJens Axboe 	host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
57661871ee13SKevin Hao 
5767f3187195STejun Heo 	/* host must have been started */
5768f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
5769a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
5770f3187195STejun Heo 		WARN_ON(1);
5771f3187195STejun Heo 		return -EINVAL;
577202f076aaSAlan Cox 	}
5773f0d36efdSTejun Heo 
5774f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
5775f3187195STejun Heo 	 * determine the exact number of ports to allocate at
5776f3187195STejun Heo 	 * allocation time.
5777f3187195STejun Heo 	 */
5778f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
5779f3187195STejun Heo 		kfree(host->ports[i]);
5780f0d36efdSTejun Heo 
5781f3187195STejun Heo 	/* give ports names and add SCSI hosts */
5782e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
578385d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
5784e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
5785e628dc99SDavid Milburn 	}
5786d9027470SGwendal Grignou 
5787d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
5788d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
5789d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
5790d9027470SGwendal Grignou 		if (rc) {
5791d9027470SGwendal Grignou 			goto err_tadd;
5792d9027470SGwendal Grignou 		}
5793d9027470SGwendal Grignou 	}
5794d9027470SGwendal Grignou 
5795f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
5796ecef7253STejun Heo 	if (rc)
5797d9027470SGwendal Grignou 		goto err_tadd;
5798ecef7253STejun Heo 
5799f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
5800cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5801cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5802f3187195STejun Heo 		unsigned long xfer_mask;
5803f3187195STejun Heo 
5804f3187195STejun Heo 		/* set SATA cable type if still unset */
5805f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5806f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
5807c6fd2807SJeff Garzik 
5808c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
58094fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
5810b1c72916STejun Heo 		if (ap->slave_link)
5811b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
5812c6fd2807SJeff Garzik 
5813cbcdd875STejun Heo 		/* print per-port info to dmesg */
5814f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5815f3187195STejun Heo 					      ap->udma_mask);
5816f3187195STejun Heo 
5817abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
5818a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
5819a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5820f3187195STejun Heo 				      ata_mode_string(xfer_mask),
5821cbcdd875STejun Heo 				      ap->link.eh_info.desc);
5822abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
5823abf6e8edSTejun Heo 		} else
5824a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
5825c6fd2807SJeff Garzik 	}
5826c6fd2807SJeff Garzik 
5827f6005354SVegard Nossum 	/* perform each probe asynchronously */
5828f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5829f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
5830b5292111SKai-Heng Feng 		ap->cookie = async_schedule(async_port_probe, ap);
5831c6fd2807SJeff Garzik 	}
5832c6fd2807SJeff Garzik 
5833f3187195STejun Heo 	return 0;
5834d9027470SGwendal Grignou 
5835d9027470SGwendal Grignou  err_tadd:
5836d9027470SGwendal Grignou 	while (--i >= 0) {
5837d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
5838d9027470SGwendal Grignou 	}
5839d9027470SGwendal Grignou 	return rc;
5840d9027470SGwendal Grignou 
5841f3187195STejun Heo }
5842a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_register);
5843f3187195STejun Heo 
5844f3187195STejun Heo /**
5845f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
5846f5cda257STejun Heo  *	@host: target ATA host
5847f5cda257STejun Heo  *	@irq: IRQ to request
5848f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
5849f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
5850f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
5851f5cda257STejun Heo  *
5852f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
5853f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
5854c9b5560aSMasanari Iida  *	request IRQ and register it.  This helper takes necessary
5855f5cda257STejun Heo  *	arguments and performs the three steps in one go.
5856f5cda257STejun Heo  *
58573d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
58583d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
58593d46b2e2SPaul Mundt  *	should be NULL.
58603d46b2e2SPaul Mundt  *
5861f5cda257STejun Heo  *	LOCKING:
5862f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5863f5cda257STejun Heo  *
5864f5cda257STejun Heo  *	RETURNS:
5865f5cda257STejun Heo  *	0 on success, -errno otherwise.
5866f5cda257STejun Heo  */
5867f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
5868f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
5869f5cda257STejun Heo 		      struct scsi_host_template *sht)
5870f5cda257STejun Heo {
5871cbcdd875STejun Heo 	int i, rc;
58727e22c002SHeiner Kallweit 	char *irq_desc;
5873f5cda257STejun Heo 
5874f5cda257STejun Heo 	rc = ata_host_start(host);
5875f5cda257STejun Heo 	if (rc)
5876f5cda257STejun Heo 		return rc;
5877f5cda257STejun Heo 
58783d46b2e2SPaul Mundt 	/* Special case for polling mode */
58793d46b2e2SPaul Mundt 	if (!irq) {
58803d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
58813d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
58823d46b2e2SPaul Mundt 	}
58833d46b2e2SPaul Mundt 
58847e22c002SHeiner Kallweit 	irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
58857e22c002SHeiner Kallweit 				  dev_driver_string(host->dev),
58867e22c002SHeiner Kallweit 				  dev_name(host->dev));
58877e22c002SHeiner Kallweit 	if (!irq_desc)
58887e22c002SHeiner Kallweit 		return -ENOMEM;
58897e22c002SHeiner Kallweit 
5890f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
58917e22c002SHeiner Kallweit 			      irq_desc, host);
5892f5cda257STejun Heo 	if (rc)
5893f5cda257STejun Heo 		return rc;
5894f5cda257STejun Heo 
5895cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
5896cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
58974031826bSTejun Heo 
5898f5cda257STejun Heo 	rc = ata_host_register(host, sht);
5899f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
5900f5cda257STejun Heo 	if (rc)
5901f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
5902f5cda257STejun Heo 
5903f5cda257STejun Heo 	return rc;
5904f5cda257STejun Heo }
5905a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_activate);
5906f5cda257STejun Heo 
5907f5cda257STejun Heo /**
5908c9b5560aSMasanari Iida  *	ata_port_detach - Detach ATA port in preparation of device removal
5909c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
5910c6fd2807SJeff Garzik  *
5911c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
5912c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
5913c6fd2807SJeff Garzik  *	be quiescent on return from this function.
5914c6fd2807SJeff Garzik  *
5915c6fd2807SJeff Garzik  *	LOCKING:
5916c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5917c6fd2807SJeff Garzik  */
5918741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
5919c6fd2807SJeff Garzik {
5920c6fd2807SJeff Garzik 	unsigned long flags;
5921a6f9bf4dSLevente Kurusa 	struct ata_link *link;
5922a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
5923c6fd2807SJeff Garzik 
5924c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
5925c6fd2807SJeff Garzik 		goto skip_eh;
5926c6fd2807SJeff Garzik 
5927c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
5928c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5929c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
5930ece180d1STejun Heo 	ata_port_schedule_eh(ap);
5931c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5932c6fd2807SJeff Garzik 
5933ece180d1STejun Heo 	/* wait till EH commits suicide */
5934c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
5935c6fd2807SJeff Garzik 
5936ece180d1STejun Heo 	/* it better be dead now */
5937ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
5938c6fd2807SJeff Garzik 
5939afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
5940c6fd2807SJeff Garzik 
5941c6fd2807SJeff Garzik  skip_eh:
5942a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
5943a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
5944a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
5945a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
5946a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
5947a6f9bf4dSLevente Kurusa 		}
5948a6f9bf4dSLevente Kurusa 	}
5949d9027470SGwendal Grignou 	if (ap->pmp_link) {
5950d9027470SGwendal Grignou 		int i;
5951d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
5952d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
5953d9027470SGwendal Grignou 	}
5954c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
5955cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
5956c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
5957c6fd2807SJeff Garzik }
5958c6fd2807SJeff Garzik 
5959c6fd2807SJeff Garzik /**
59600529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
59610529c159STejun Heo  *	@host: Host to detach
59620529c159STejun Heo  *
59630529c159STejun Heo  *	Detach all ports of @host.
59640529c159STejun Heo  *
59650529c159STejun Heo  *	LOCKING:
59660529c159STejun Heo  *	Kernel thread context (may sleep).
59670529c159STejun Heo  */
59680529c159STejun Heo void ata_host_detach(struct ata_host *host)
59690529c159STejun Heo {
59700529c159STejun Heo 	int i;
59710529c159STejun Heo 
5972b5292111SKai-Heng Feng 	for (i = 0; i < host->n_ports; i++) {
5973130f4cafSJohn Garry 		/* Ensure ata_port probe has completed */
5974b5292111SKai-Heng Feng 		async_synchronize_cookie(host->ports[i]->cookie + 1);
59750529c159STejun Heo 		ata_port_detach(host->ports[i]);
5976b5292111SKai-Heng Feng 	}
5977562f0c2dSTejun Heo 
5978562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
5979562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
59800529c159STejun Heo }
5981a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_detach);
59820529c159STejun Heo 
5983c6fd2807SJeff Garzik #ifdef CONFIG_PCI
5984c6fd2807SJeff Garzik 
5985c6fd2807SJeff Garzik /**
5986c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
5987c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
5988c6fd2807SJeff Garzik  *
5989b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
5990b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
5991b878ca5dSTejun Heo  *	release is handled via devres.
5992c6fd2807SJeff Garzik  *
5993c6fd2807SJeff Garzik  *	LOCKING:
5994c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
5995c6fd2807SJeff Garzik  */
5996c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
5997c6fd2807SJeff Garzik {
599804a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
5999c6fd2807SJeff Garzik 
6000f0d36efdSTejun Heo 	ata_host_detach(host);
6001c6fd2807SJeff Garzik }
6002a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_remove_one);
6003c6fd2807SJeff Garzik 
600410a663a1SPrabhakar Kushwaha void ata_pci_shutdown_one(struct pci_dev *pdev)
600510a663a1SPrabhakar Kushwaha {
600610a663a1SPrabhakar Kushwaha 	struct ata_host *host = pci_get_drvdata(pdev);
600710a663a1SPrabhakar Kushwaha 	int i;
600810a663a1SPrabhakar Kushwaha 
600910a663a1SPrabhakar Kushwaha 	for (i = 0; i < host->n_ports; i++) {
601010a663a1SPrabhakar Kushwaha 		struct ata_port *ap = host->ports[i];
601110a663a1SPrabhakar Kushwaha 
601210a663a1SPrabhakar Kushwaha 		ap->pflags |= ATA_PFLAG_FROZEN;
601310a663a1SPrabhakar Kushwaha 
601410a663a1SPrabhakar Kushwaha 		/* Disable port interrupts */
601510a663a1SPrabhakar Kushwaha 		if (ap->ops->freeze)
601610a663a1SPrabhakar Kushwaha 			ap->ops->freeze(ap);
601710a663a1SPrabhakar Kushwaha 
601810a663a1SPrabhakar Kushwaha 		/* Stop the port DMA engines */
601910a663a1SPrabhakar Kushwaha 		if (ap->ops->port_stop)
602010a663a1SPrabhakar Kushwaha 			ap->ops->port_stop(ap);
602110a663a1SPrabhakar Kushwaha 	}
602210a663a1SPrabhakar Kushwaha }
6023a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
602410a663a1SPrabhakar Kushwaha 
6025c6fd2807SJeff Garzik /* move to PCI subsystem */
6026c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6027c6fd2807SJeff Garzik {
6028c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6029c6fd2807SJeff Garzik 
6030c6fd2807SJeff Garzik 	switch (bits->width) {
6031c6fd2807SJeff Garzik 	case 1: {
6032c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6033c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6034c6fd2807SJeff Garzik 		tmp = tmp8;
6035c6fd2807SJeff Garzik 		break;
6036c6fd2807SJeff Garzik 	}
6037c6fd2807SJeff Garzik 	case 2: {
6038c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6039c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6040c6fd2807SJeff Garzik 		tmp = tmp16;
6041c6fd2807SJeff Garzik 		break;
6042c6fd2807SJeff Garzik 	}
6043c6fd2807SJeff Garzik 	case 4: {
6044c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6045c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6046c6fd2807SJeff Garzik 		tmp = tmp32;
6047c6fd2807SJeff Garzik 		break;
6048c6fd2807SJeff Garzik 	}
6049c6fd2807SJeff Garzik 
6050c6fd2807SJeff Garzik 	default:
6051c6fd2807SJeff Garzik 		return -EINVAL;
6052c6fd2807SJeff Garzik 	}
6053c6fd2807SJeff Garzik 
6054c6fd2807SJeff Garzik 	tmp &= bits->mask;
6055c6fd2807SJeff Garzik 
6056c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6057c6fd2807SJeff Garzik }
6058a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(pci_test_config_bits);
6059c6fd2807SJeff Garzik 
60606ffa01d8STejun Heo #ifdef CONFIG_PM
6061c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6062c6fd2807SJeff Garzik {
6063c6fd2807SJeff Garzik 	pci_save_state(pdev);
6064c6fd2807SJeff Garzik 	pci_disable_device(pdev);
60654c90d971STejun Heo 
60663a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6067c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6068c6fd2807SJeff Garzik }
6069a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6070c6fd2807SJeff Garzik 
6071553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6072c6fd2807SJeff Garzik {
6073553c4aa6STejun Heo 	int rc;
6074553c4aa6STejun Heo 
6075c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6076c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6077553c4aa6STejun Heo 
6078f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6079553c4aa6STejun Heo 	if (rc) {
6080a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6081553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6082553c4aa6STejun Heo 		return rc;
6083553c4aa6STejun Heo 	}
6084553c4aa6STejun Heo 
6085c6fd2807SJeff Garzik 	pci_set_master(pdev);
6086553c4aa6STejun Heo 	return 0;
6087c6fd2807SJeff Garzik }
6088a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6089c6fd2807SJeff Garzik 
6090c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6091c6fd2807SJeff Garzik {
609204a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6093c6fd2807SJeff Garzik 	int rc = 0;
6094c6fd2807SJeff Garzik 
6095cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6096c6fd2807SJeff Garzik 	if (rc)
6097c6fd2807SJeff Garzik 		return rc;
6098c6fd2807SJeff Garzik 
6099c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6100c6fd2807SJeff Garzik 
6101c6fd2807SJeff Garzik 	return 0;
6102c6fd2807SJeff Garzik }
6103a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6104c6fd2807SJeff Garzik 
6105c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6106c6fd2807SJeff Garzik {
610704a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6108553c4aa6STejun Heo 	int rc;
6109c6fd2807SJeff Garzik 
6110553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6111553c4aa6STejun Heo 	if (rc == 0)
6112cca3974eSJeff Garzik 		ata_host_resume(host);
6113553c4aa6STejun Heo 	return rc;
6114c6fd2807SJeff Garzik }
6115a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_resume);
61166ffa01d8STejun Heo #endif /* CONFIG_PM */
6117c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6118c6fd2807SJeff Garzik 
6119b7db04d9SBrian Norris /**
6120b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6121b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6122b7db04d9SBrian Norris  *
6123b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6124b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6125b7db04d9SBrian Norris  *	release is handled via devres.
6126b7db04d9SBrian Norris  *
6127b7db04d9SBrian Norris  *	LOCKING:
6128b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6129b7db04d9SBrian Norris  */
6130b7db04d9SBrian Norris int ata_platform_remove_one(struct platform_device *pdev)
6131b7db04d9SBrian Norris {
6132b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6133b7db04d9SBrian Norris 
6134b7db04d9SBrian Norris 	ata_host_detach(host);
6135b7db04d9SBrian Norris 
6136b7db04d9SBrian Norris 	return 0;
6137b7db04d9SBrian Norris }
6138a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_platform_remove_one);
6139b7db04d9SBrian Norris 
6140bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
614133267325STejun Heo static int __init ata_parse_force_one(char **cur,
614233267325STejun Heo 				      struct ata_force_ent *force_ent,
614333267325STejun Heo 				      const char **reason)
614433267325STejun Heo {
61450f5f264bSRasmus Villemoes 	static const struct ata_force_param force_tbl[] __initconst = {
614633267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
614733267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
614833267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
614933267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
615033267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
615133267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
615233267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
615333267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
615433267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
615533267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
6156d7b16e4fSMartin K. Petersen 		{ "noncqtrim",	.horkage_on	= ATA_HORKAGE_NO_NCQ_TRIM },
6157d7b16e4fSMartin K. Petersen 		{ "ncqtrim",	.horkage_off	= ATA_HORKAGE_NO_NCQ_TRIM },
61587a8526a5SKate Hsuan 		{ "noncqati",	.horkage_on	= ATA_HORKAGE_NO_NCQ_ON_ATI },
61597a8526a5SKate Hsuan 		{ "ncqati",	.horkage_off	= ATA_HORKAGE_NO_NCQ_ON_ATI },
616043c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
616133267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
616233267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
616333267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
616433267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
616533267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
616633267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
616733267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
616833267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
616933267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
617033267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
617133267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
617233267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
617333267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
617433267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
617533267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
617633267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
617733267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
617833267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
617933267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
618033267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
618133267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
618233267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
618333267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
618433267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
618533267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
618633267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
618733267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
618833267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
618933267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
619033267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
619133267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
619233267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
619333267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
619433267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
619505944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
619605944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
619705944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6198ca6d43b0SDan Williams 		{ "rstonce",	.lflags		= ATA_LFLAG_RST_ONCE },
6199966fbe19SVincent Pelletier 		{ "atapi_dmadir", .horkage_on	= ATA_HORKAGE_ATAPI_DMADIR },
6200b8bd6dc3SRobin H. Johnson 		{ "disable",	.horkage_on	= ATA_HORKAGE_DISABLE },
620133267325STejun Heo 	};
620233267325STejun Heo 	char *start = *cur, *p = *cur;
620333267325STejun Heo 	char *id, *val, *endp;
620433267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
620533267325STejun Heo 	int nr_matches = 0, i;
620633267325STejun Heo 
620733267325STejun Heo 	/* find where this param ends and update *cur */
620833267325STejun Heo 	while (*p != '\0' && *p != ',')
620933267325STejun Heo 		p++;
621033267325STejun Heo 
621133267325STejun Heo 	if (*p == '\0')
621233267325STejun Heo 		*cur = p;
621333267325STejun Heo 	else
621433267325STejun Heo 		*cur = p + 1;
621533267325STejun Heo 
621633267325STejun Heo 	*p = '\0';
621733267325STejun Heo 
621833267325STejun Heo 	/* parse */
621933267325STejun Heo 	p = strchr(start, ':');
622033267325STejun Heo 	if (!p) {
622133267325STejun Heo 		val = strstrip(start);
622233267325STejun Heo 		goto parse_val;
622333267325STejun Heo 	}
622433267325STejun Heo 	*p = '\0';
622533267325STejun Heo 
622633267325STejun Heo 	id = strstrip(start);
622733267325STejun Heo 	val = strstrip(p + 1);
622833267325STejun Heo 
622933267325STejun Heo 	/* parse id */
623033267325STejun Heo 	p = strchr(id, '.');
623133267325STejun Heo 	if (p) {
623233267325STejun Heo 		*p++ = '\0';
623333267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
623433267325STejun Heo 		if (p == endp || *endp != '\0') {
623533267325STejun Heo 			*reason = "invalid device";
623633267325STejun Heo 			return -EINVAL;
623733267325STejun Heo 		}
623833267325STejun Heo 	}
623933267325STejun Heo 
624033267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
6241f7cf69aeSTejun Heo 	if (id == endp || *endp != '\0') {
624233267325STejun Heo 		*reason = "invalid port/link";
624333267325STejun Heo 		return -EINVAL;
624433267325STejun Heo 	}
624533267325STejun Heo 
624633267325STejun Heo  parse_val:
624733267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
624833267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
624933267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
625033267325STejun Heo 
625133267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
625233267325STejun Heo 			continue;
625333267325STejun Heo 
625433267325STejun Heo 		nr_matches++;
625533267325STejun Heo 		match_fp = fp;
625633267325STejun Heo 
625733267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
625833267325STejun Heo 			nr_matches = 1;
625933267325STejun Heo 			break;
626033267325STejun Heo 		}
626133267325STejun Heo 	}
626233267325STejun Heo 
626333267325STejun Heo 	if (!nr_matches) {
626433267325STejun Heo 		*reason = "unknown value";
626533267325STejun Heo 		return -EINVAL;
626633267325STejun Heo 	}
626733267325STejun Heo 	if (nr_matches > 1) {
62689de55351SArvind Yadav 		*reason = "ambiguous value";
626933267325STejun Heo 		return -EINVAL;
627033267325STejun Heo 	}
627133267325STejun Heo 
627233267325STejun Heo 	force_ent->param = *match_fp;
627333267325STejun Heo 
627433267325STejun Heo 	return 0;
627533267325STejun Heo }
627633267325STejun Heo 
627733267325STejun Heo static void __init ata_parse_force_param(void)
627833267325STejun Heo {
627933267325STejun Heo 	int idx = 0, size = 1;
628033267325STejun Heo 	int last_port = -1, last_device = -1;
628133267325STejun Heo 	char *p, *cur, *next;
628233267325STejun Heo 
628333267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
628433267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
628533267325STejun Heo 		if (*p == ',')
628633267325STejun Heo 			size++;
628733267325STejun Heo 
62886396bb22SKees Cook 	ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
628933267325STejun Heo 	if (!ata_force_tbl) {
629033267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
629133267325STejun Heo 		       "libata.force ignored\n");
629233267325STejun Heo 		return;
629333267325STejun Heo 	}
629433267325STejun Heo 
629533267325STejun Heo 	/* parse and populate the table */
629633267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
629733267325STejun Heo 		const char *reason = "";
629833267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
629933267325STejun Heo 
630033267325STejun Heo 		next = cur;
630133267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
630233267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
630333267325STejun Heo 			       "parameter \"%s\" (%s)\n",
630433267325STejun Heo 			       cur, reason);
630533267325STejun Heo 			continue;
630633267325STejun Heo 		}
630733267325STejun Heo 
630833267325STejun Heo 		if (te.port == -1) {
630933267325STejun Heo 			te.port = last_port;
631033267325STejun Heo 			te.device = last_device;
631133267325STejun Heo 		}
631233267325STejun Heo 
631333267325STejun Heo 		ata_force_tbl[idx++] = te;
631433267325STejun Heo 
631533267325STejun Heo 		last_port = te.port;
631633267325STejun Heo 		last_device = te.device;
631733267325STejun Heo 	}
631833267325STejun Heo 
631933267325STejun Heo 	ata_force_tbl_size = idx;
632033267325STejun Heo }
6321c6fd2807SJeff Garzik 
6322bf89b0bfSBartlomiej Zolnierkiewicz static void ata_free_force_param(void)
6323bf89b0bfSBartlomiej Zolnierkiewicz {
6324bf89b0bfSBartlomiej Zolnierkiewicz 	kfree(ata_force_tbl);
6325bf89b0bfSBartlomiej Zolnierkiewicz }
6326bf89b0bfSBartlomiej Zolnierkiewicz #else
6327bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_parse_force_param(void) { }
6328bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_free_force_param(void) { }
6329bf89b0bfSBartlomiej Zolnierkiewicz #endif
6330bf89b0bfSBartlomiej Zolnierkiewicz 
6331c6fd2807SJeff Garzik static int __init ata_init(void)
6332c6fd2807SJeff Garzik {
6333d9027470SGwendal Grignou 	int rc;
6334270390e1STejun Heo 
633533267325STejun Heo 	ata_parse_force_param();
633633267325STejun Heo 
6337270390e1STejun Heo 	rc = ata_sff_init();
6338ad72cf98STejun Heo 	if (rc) {
6339bf89b0bfSBartlomiej Zolnierkiewicz 		ata_free_force_param();
6340ad72cf98STejun Heo 		return rc;
6341ad72cf98STejun Heo 	}
6342c6fd2807SJeff Garzik 
6343d9027470SGwendal Grignou 	libata_transport_init();
6344d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6345d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6346d9027470SGwendal Grignou 		ata_sff_exit();
6347d9027470SGwendal Grignou 		rc = -ENOMEM;
6348d9027470SGwendal Grignou 		goto err_out;
6349d9027470SGwendal Grignou 	}
6350d9027470SGwendal Grignou 
6351c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6352c6fd2807SJeff Garzik 	return 0;
6353d9027470SGwendal Grignou 
6354d9027470SGwendal Grignou err_out:
6355d9027470SGwendal Grignou 	return rc;
6356c6fd2807SJeff Garzik }
6357c6fd2807SJeff Garzik 
6358c6fd2807SJeff Garzik static void __exit ata_exit(void)
6359c6fd2807SJeff Garzik {
6360d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6361d9027470SGwendal Grignou 	libata_transport_exit();
6362270390e1STejun Heo 	ata_sff_exit();
6363bf89b0bfSBartlomiej Zolnierkiewicz 	ata_free_force_param();
6364c6fd2807SJeff Garzik }
6365c6fd2807SJeff Garzik 
6366a4625085SBrian King subsys_initcall(ata_init);
6367c6fd2807SJeff Garzik module_exit(ata_exit);
6368c6fd2807SJeff Garzik 
63699990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6370c6fd2807SJeff Garzik 
6371c6fd2807SJeff Garzik int ata_ratelimit(void)
6372c6fd2807SJeff Garzik {
63739990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6374c6fd2807SJeff Garzik }
6375a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ratelimit);
6376c6fd2807SJeff Garzik 
6377c0c362b6STejun Heo /**
6378c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6379c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6380c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6381c0c362b6STejun Heo  *
6382c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6383c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6384c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6385c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6386c0c362b6STejun Heo  *	sleeping.
6387c0c362b6STejun Heo  *
6388c0c362b6STejun Heo  *	LOCKING:
6389c0c362b6STejun Heo  *	Might sleep.
6390c0c362b6STejun Heo  */
639197750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
639297750cebSTejun Heo {
6393c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6394c0c362b6STejun Heo 
6395c0c362b6STejun Heo 	if (owns_eh)
6396c0c362b6STejun Heo 		ata_eh_release(ap);
6397c0c362b6STejun Heo 
6398848c3920SAnil Veliyankara Madam 	if (msecs < 20) {
6399848c3920SAnil Veliyankara Madam 		unsigned long usecs = msecs * USEC_PER_MSEC;
6400848c3920SAnil Veliyankara Madam 		usleep_range(usecs, usecs + 50);
6401848c3920SAnil Veliyankara Madam 	} else {
640297750cebSTejun Heo 		msleep(msecs);
6403848c3920SAnil Veliyankara Madam 	}
6404c0c362b6STejun Heo 
6405c0c362b6STejun Heo 	if (owns_eh)
6406c0c362b6STejun Heo 		ata_eh_acquire(ap);
640797750cebSTejun Heo }
6408a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_msleep);
640997750cebSTejun Heo 
6410c6fd2807SJeff Garzik /**
6411c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
641297750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6413c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6414c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6415c6fd2807SJeff Garzik  *	@val: Wait condition
6416341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6417341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6418c6fd2807SJeff Garzik  *
6419c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6420c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6421c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6422c6fd2807SJeff Garzik  *
6423c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6424c6fd2807SJeff Garzik  *
6425c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6426c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6427c6fd2807SJeff Garzik  *
6428c6fd2807SJeff Garzik  *	LOCKING:
6429c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6430c6fd2807SJeff Garzik  *
6431c6fd2807SJeff Garzik  *	RETURNS:
6432c6fd2807SJeff Garzik  *	The final register value.
6433c6fd2807SJeff Garzik  */
643497750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6435341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6436c6fd2807SJeff Garzik {
6437341c2c95STejun Heo 	unsigned long deadline;
6438c6fd2807SJeff Garzik 	u32 tmp;
6439c6fd2807SJeff Garzik 
6440c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6441c6fd2807SJeff Garzik 
6442c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6443c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6444c6fd2807SJeff Garzik 	 * eat away the timeout.
6445c6fd2807SJeff Garzik 	 */
6446341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6447c6fd2807SJeff Garzik 
6448341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
644997750cebSTejun Heo 		ata_msleep(ap, interval);
6450c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6451c6fd2807SJeff Garzik 	}
6452c6fd2807SJeff Garzik 
6453c6fd2807SJeff Garzik 	return tmp;
6454c6fd2807SJeff Garzik }
6455a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_register);
6456c6fd2807SJeff Garzik 
6457c6fd2807SJeff Garzik /*
6458c6fd2807SJeff Garzik  * Dummy port_ops
6459c6fd2807SJeff Garzik  */
6460c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6461c6fd2807SJeff Garzik {
6462c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6463c6fd2807SJeff Garzik }
6464c6fd2807SJeff Garzik 
6465182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6466182d7bbaSTejun Heo {
6467182d7bbaSTejun Heo 	/* truly dummy */
6468182d7bbaSTejun Heo }
6469182d7bbaSTejun Heo 
6470029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6471c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6472c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6473182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6474e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
6475e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
6476c6fd2807SJeff Garzik };
6477a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
6478c6fd2807SJeff Garzik 
647921b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
648021b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
648121b0ad4fSTejun Heo };
6482a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_info);
648321b0ad4fSTejun Heo 
648406296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
648506296a1eSJoe Perches {
648606296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
648706296a1eSJoe Perches }
648806296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
6489c206a389SHannes Reinecke 
6490c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_tf_load);
6491c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_exec_command);
6492c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_setup);
6493c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_start);
6494c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_status);
6495