xref: /openbmc/linux/drivers/ata/libata-core.c (revision 99618f8a)
1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c6fd2807SJeff Garzik /*
3c6fd2807SJeff Garzik  *  libata-core.c - helper library for ATA
4c6fd2807SJeff Garzik  *
5c6fd2807SJeff Garzik  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
6c6fd2807SJeff Garzik  *  Copyright 2003-2004 Jeff Garzik
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
919285f3cSMauro Carvalho Chehab  *  as Documentation/driver-api/libata.rst
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
12c6fd2807SJeff Garzik  *  http://www.sata-io.org/
13c6fd2807SJeff Garzik  *
1492c52c52SAlan Cox  *  Standards documents from:
1592c52c52SAlan Cox  *	http://www.t13.org (ATA standards, PCI DMA IDE spec)
1692c52c52SAlan Cox  *	http://www.t10.org (SCSI MMC - for ATAPI MMC)
1792c52c52SAlan Cox  *	http://www.sata-io.org (SATA)
1892c52c52SAlan Cox  *	http://www.compactflash.org (CF)
1992c52c52SAlan Cox  *	http://www.qic.org (QIC157 - Tape and DSC)
2092c52c52SAlan Cox  *	http://www.ce-ata.org (CE-ATA: not supported)
21a52fbcfcSBartlomiej Zolnierkiewicz  *
22a52fbcfcSBartlomiej Zolnierkiewicz  * libata is essentially a library of internal helper functions for
23a52fbcfcSBartlomiej Zolnierkiewicz  * low-level ATA host controller drivers.  As such, the API/ABI is
24a52fbcfcSBartlomiej Zolnierkiewicz  * likely to change as new drivers are added and updated.
25a52fbcfcSBartlomiej Zolnierkiewicz  * Do not depend on ABI/API stability.
26c6fd2807SJeff Garzik  */
27c6fd2807SJeff Garzik 
28c6fd2807SJeff Garzik #include <linux/kernel.h>
29c6fd2807SJeff Garzik #include <linux/module.h>
30c6fd2807SJeff Garzik #include <linux/pci.h>
31c6fd2807SJeff Garzik #include <linux/init.h>
32c6fd2807SJeff Garzik #include <linux/list.h>
33c6fd2807SJeff Garzik #include <linux/mm.h>
34c6fd2807SJeff Garzik #include <linux/spinlock.h>
35c6fd2807SJeff Garzik #include <linux/blkdev.h>
36c6fd2807SJeff Garzik #include <linux/delay.h>
37c6fd2807SJeff Garzik #include <linux/timer.h>
38848c3920SAnil Veliyankara Madam #include <linux/time.h>
39c6fd2807SJeff Garzik #include <linux/interrupt.h>
40c6fd2807SJeff Garzik #include <linux/completion.h>
41c6fd2807SJeff Garzik #include <linux/suspend.h>
42c6fd2807SJeff Garzik #include <linux/workqueue.h>
43c6fd2807SJeff Garzik #include <linux/scatterlist.h>
442dcb407eSJeff Garzik #include <linux/io.h>
45e18086d6SMark Lord #include <linux/log2.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
47428ac5fcSGeorge Spelvin #include <linux/glob.h>
48c6fd2807SJeff Garzik #include <scsi/scsi.h>
49c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
50c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
51c6fd2807SJeff Garzik #include <linux/libata.h>
52c6fd2807SJeff Garzik #include <asm/byteorder.h>
53fe5af0ccSHannes Reinecke #include <asm/unaligned.h>
54140b5e59STejun Heo #include <linux/cdrom.h>
559990b6f3SAkinobu Mita #include <linux/ratelimit.h>
56eb25cb99SStephan Linz #include <linux/leds.h>
579ee4f393SLin Ming #include <linux/pm_runtime.h>
58b7db04d9SBrian Norris #include <linux/platform_device.h>
59bbf5a097SBartlomiej Zolnierkiewicz #include <asm/setup.h>
60c6fd2807SJeff Garzik 
61255c03d1SHannes Reinecke #define CREATE_TRACE_POINTS
62255c03d1SHannes Reinecke #include <trace/events/libata.h>
63255c03d1SHannes Reinecke 
64c6fd2807SJeff Garzik #include "libata.h"
65d9027470SGwendal Grignou #include "libata-transport.h"
66fda0efc5SJeff Garzik 
67029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
680aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
69203c75b8STejun Heo 	.postreset		= ata_std_postreset,
70a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
71e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
72e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
73029cfd6bSTejun Heo };
74029cfd6bSTejun Heo 
75029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
76029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
77029cfd6bSTejun Heo 
78029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
7957c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
80029cfd6bSTejun Heo };
81a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(sata_port_ops);
82029cfd6bSTejun Heo 
83c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
84c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
85c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
86c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
8775683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
88c6fd2807SJeff Garzik 
89a78f57afSTero Roponen atomic_t ata_print_id = ATOMIC_INIT(0);
90c6fd2807SJeff Garzik 
91bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
9233267325STejun Heo struct ata_force_param {
9333267325STejun Heo 	const char	*name;
948ba5a45cSBartlomiej Zolnierkiewicz 	u8		cbl;
958ba5a45cSBartlomiej Zolnierkiewicz 	u8		spd_limit;
96f0a6d77bSSergey Shtylyov 	unsigned int	xfer_mask;
9733267325STejun Heo 	unsigned int	horkage_on;
9833267325STejun Heo 	unsigned int	horkage_off;
993af9ca4dSDamien Le Moal 	u16		lflags_on;
1003af9ca4dSDamien Le Moal 	u16		lflags_off;
10133267325STejun Heo };
10233267325STejun Heo 
10333267325STejun Heo struct ata_force_ent {
10433267325STejun Heo 	int			port;
10533267325STejun Heo 	int			device;
10633267325STejun Heo 	struct ata_force_param	param;
10733267325STejun Heo };
10833267325STejun Heo 
10933267325STejun Heo static struct ata_force_ent *ata_force_tbl;
11033267325STejun Heo static int ata_force_tbl_size;
11133267325STejun Heo 
112bbf5a097SBartlomiej Zolnierkiewicz static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
1137afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1147afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
1158c27ceffSMauro Carvalho Chehab MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
116bf89b0bfSBartlomiej Zolnierkiewicz #endif
11733267325STejun Heo 
1182486fa56STejun Heo static int atapi_enabled = 1;
119c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
120ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
121c6fd2807SJeff Garzik 
122c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
123c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
124ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
125c6fd2807SJeff Garzik 
126baf4fdfaSMark Lord int atapi_passthru16 = 1;
127baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
128ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
129baf4fdfaSMark Lord 
130c6fd2807SJeff Garzik int libata_fua = 0;
131c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
132ad5d8eacSEvgeni Golov MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
133c6fd2807SJeff Garzik 
1342dcb407eSJeff Garzik static int ata_ignore_hpa;
1351e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1361e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1371e999736SAlan Cox 
138b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
139b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
140b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
141b3a70601SAlan Cox 
14287fbc5a0STejun Heo static int ata_probe_timeout;
143c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
144c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
145c6fd2807SJeff Garzik 
1466ebe9d86SJeff Garzik int libata_noacpi = 0;
147d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
148ad5d8eacSEvgeni Golov MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
14911ef697bSKristen Carlson Accardi 
150ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
151ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
152ad5d8eacSEvgeni Golov MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
153ae8d4ee7SAlan Cox 
154e7ecd435STejun Heo static int atapi_an;
155e7ecd435STejun Heo module_param(atapi_an, int, 0444);
156e7ecd435STejun Heo MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
157e7ecd435STejun Heo 
158c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
159c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
160c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
161c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
162c6fd2807SJeff Garzik 
ata_dev_print_info(struct ata_device * dev)163891fd7c6SDamien Le Moal static inline bool ata_dev_print_info(struct ata_device *dev)
164891fd7c6SDamien Le Moal {
165891fd7c6SDamien Le Moal 	struct ata_eh_context *ehc = &dev->link->eh_context;
166891fd7c6SDamien Le Moal 
167891fd7c6SDamien Le Moal 	return ehc->i.flags & ATA_EHI_PRINTINFO;
168891fd7c6SDamien Le Moal }
169c6fd2807SJeff Garzik 
ata_sstatus_online(u32 sstatus)1709913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1719913ff8aSTejun Heo {
1729913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1739913ff8aSTejun Heo }
1749913ff8aSTejun Heo 
1751eca4365STejun Heo /**
1761eca4365STejun Heo  *	ata_link_next - link iteration helper
1771eca4365STejun Heo  *	@link: the previous link, NULL to start
1781eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1791eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
180aadffb68STejun Heo  *
181aadffb68STejun Heo  *	LOCKING:
182aadffb68STejun Heo  *	Host lock or EH context.
1831eca4365STejun Heo  *
1841eca4365STejun Heo  *	RETURNS:
1851eca4365STejun Heo  *	Pointer to the next link.
186aadffb68STejun Heo  */
ata_link_next(struct ata_link * link,struct ata_port * ap,enum ata_link_iter_mode mode)1871eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1881eca4365STejun Heo 			       enum ata_link_iter_mode mode)
189aadffb68STejun Heo {
1901eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
1911eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
1921eca4365STejun Heo 
193aadffb68STejun Heo 	/* NULL link indicates start of iteration */
1941eca4365STejun Heo 	if (!link)
1951eca4365STejun Heo 		switch (mode) {
1961eca4365STejun Heo 		case ATA_LITER_EDGE:
1971eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
1981eca4365STejun Heo 			if (sata_pmp_attached(ap))
199aadffb68STejun Heo 				return ap->pmp_link;
200df561f66SGustavo A. R. Silva 			fallthrough;
2011eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
202aadffb68STejun Heo 			return &ap->link;
203aadffb68STejun Heo 		}
204aadffb68STejun Heo 
2051eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2061eca4365STejun Heo 	if (link == &ap->link)
2071eca4365STejun Heo 		switch (mode) {
2081eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2091eca4365STejun Heo 			if (sata_pmp_attached(ap))
210aadffb68STejun Heo 				return ap->pmp_link;
211df561f66SGustavo A. R. Silva 			fallthrough;
2121eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2131eca4365STejun Heo 			if (unlikely(ap->slave_link))
2141eca4365STejun Heo 				return ap->slave_link;
215df561f66SGustavo A. R. Silva 			fallthrough;
2161eca4365STejun Heo 		case ATA_LITER_EDGE:
2171eca4365STejun Heo 			return NULL;
218aadffb68STejun Heo 		}
219aadffb68STejun Heo 
220b1c72916STejun Heo 	/* slave_link excludes PMP */
221b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
222b1c72916STejun Heo 		return NULL;
223b1c72916STejun Heo 
2241eca4365STejun Heo 	/* we were over a PMP link */
225aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
226aadffb68STejun Heo 		return link;
2271eca4365STejun Heo 
2281eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2291eca4365STejun Heo 		return &ap->link;
2301eca4365STejun Heo 
231aadffb68STejun Heo 	return NULL;
232aadffb68STejun Heo }
233a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_next);
234aadffb68STejun Heo 
235c6fd2807SJeff Garzik /**
2361eca4365STejun Heo  *	ata_dev_next - device iteration helper
2371eca4365STejun Heo  *	@dev: the previous device, NULL to start
2381eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2391eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2401eca4365STejun Heo  *
2411eca4365STejun Heo  *	LOCKING:
2421eca4365STejun Heo  *	Host lock or EH context.
2431eca4365STejun Heo  *
2441eca4365STejun Heo  *	RETURNS:
2451eca4365STejun Heo  *	Pointer to the next device.
2461eca4365STejun Heo  */
ata_dev_next(struct ata_device * dev,struct ata_link * link,enum ata_dev_iter_mode mode)2471eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2481eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2491eca4365STejun Heo {
2501eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2511eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2521eca4365STejun Heo 
2531eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2541eca4365STejun Heo 	if (!dev)
2551eca4365STejun Heo 		switch (mode) {
2561eca4365STejun Heo 		case ATA_DITER_ENABLED:
2571eca4365STejun Heo 		case ATA_DITER_ALL:
2581eca4365STejun Heo 			dev = link->device;
2591eca4365STejun Heo 			goto check;
2601eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2611eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2621eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2631eca4365STejun Heo 			goto check;
2641eca4365STejun Heo 		}
2651eca4365STejun Heo 
2661eca4365STejun Heo  next:
2671eca4365STejun Heo 	/* move to the next one */
2681eca4365STejun Heo 	switch (mode) {
2691eca4365STejun Heo 	case ATA_DITER_ENABLED:
2701eca4365STejun Heo 	case ATA_DITER_ALL:
2711eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2721eca4365STejun Heo 			goto check;
2731eca4365STejun Heo 		return NULL;
2741eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2751eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2761eca4365STejun Heo 		if (--dev >= link->device)
2771eca4365STejun Heo 			goto check;
2781eca4365STejun Heo 		return NULL;
2791eca4365STejun Heo 	}
2801eca4365STejun Heo 
2811eca4365STejun Heo  check:
2821eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2831eca4365STejun Heo 	    !ata_dev_enabled(dev))
2841eca4365STejun Heo 		goto next;
2851eca4365STejun Heo 	return dev;
2861eca4365STejun Heo }
287a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_next);
2881eca4365STejun Heo 
2891eca4365STejun Heo /**
290b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
291b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
292b1c72916STejun Heo  *
293b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
294b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
295b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
296b1c72916STejun Heo  *
297b1c72916STejun Heo  *	LOCKING:
298b1c72916STejun Heo  *	Don't care.
299b1c72916STejun Heo  *
300b1c72916STejun Heo  *	RETURNS:
301b1c72916STejun Heo  *	Pointer to the found physical link.
302b1c72916STejun Heo  */
ata_dev_phys_link(struct ata_device * dev)303b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
304b1c72916STejun Heo {
305b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
306b1c72916STejun Heo 
307b1c72916STejun Heo 	if (!ap->slave_link)
308b1c72916STejun Heo 		return dev->link;
309b1c72916STejun Heo 	if (!dev->devno)
310b1c72916STejun Heo 		return &ap->link;
311b1c72916STejun Heo 	return ap->slave_link;
312b1c72916STejun Heo }
313b1c72916STejun Heo 
314bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
315b1c72916STejun Heo /**
31633267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3174cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
31833267325STejun Heo  *
31933267325STejun Heo  *	Force cable type according to libata.force and whine about it.
32033267325STejun Heo  *	The last entry which has matching port number is used, so it
32133267325STejun Heo  *	can be specified as part of device force parameters.  For
32233267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
32333267325STejun Heo  *	same effect.
32433267325STejun Heo  *
32533267325STejun Heo  *	LOCKING:
32633267325STejun Heo  *	EH context.
32733267325STejun Heo  */
ata_force_cbl(struct ata_port * ap)32833267325STejun Heo void ata_force_cbl(struct ata_port *ap)
32933267325STejun Heo {
33033267325STejun Heo 	int i;
33133267325STejun Heo 
33233267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
33333267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
33433267325STejun Heo 
33533267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
33633267325STejun Heo 			continue;
33733267325STejun Heo 
33833267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
33933267325STejun Heo 			continue;
34033267325STejun Heo 
34133267325STejun Heo 		ap->cbl = fe->param.cbl;
342a9a79dfeSJoe Perches 		ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
34333267325STejun Heo 		return;
34433267325STejun Heo 	}
34533267325STejun Heo }
34633267325STejun Heo 
34733267325STejun Heo /**
34805944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
34933267325STejun Heo  *	@link: ATA link of interest
35033267325STejun Heo  *
35105944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
35205944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
35305944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
35405944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
35505944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
35605944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
357b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
358b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
35933267325STejun Heo  *
36033267325STejun Heo  *	LOCKING:
36133267325STejun Heo  *	EH context.
36233267325STejun Heo  */
ata_force_link_limits(struct ata_link * link)36305944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
36433267325STejun Heo {
36505944bdfSTejun Heo 	bool did_spd = false;
366b1c72916STejun Heo 	int linkno = link->pmp;
367b1c72916STejun Heo 	int i;
36833267325STejun Heo 
36933267325STejun Heo 	if (ata_is_host_link(link))
370b1c72916STejun Heo 		linkno += 15;
37133267325STejun Heo 
37233267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
37333267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
37433267325STejun Heo 
37533267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
37633267325STejun Heo 			continue;
37733267325STejun Heo 
37833267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
37933267325STejun Heo 			continue;
38033267325STejun Heo 
38105944bdfSTejun Heo 		/* only honor the first spd limit */
38205944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
38333267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
384a9a79dfeSJoe Perches 			ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
38505944bdfSTejun Heo 					fe->param.name);
38605944bdfSTejun Heo 			did_spd = true;
38705944bdfSTejun Heo 		}
38805944bdfSTejun Heo 
38905944bdfSTejun Heo 		/* let lflags stack */
3903af9ca4dSDamien Le Moal 		if (fe->param.lflags_on) {
3913af9ca4dSDamien Le Moal 			link->flags |= fe->param.lflags_on;
392a9a79dfeSJoe Perches 			ata_link_notice(link,
39305944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
3943af9ca4dSDamien Le Moal 					fe->param.lflags_on, link->flags);
3953af9ca4dSDamien Le Moal 		}
3963af9ca4dSDamien Le Moal 		if (fe->param.lflags_off) {
3973af9ca4dSDamien Le Moal 			link->flags &= ~fe->param.lflags_off;
3983af9ca4dSDamien Le Moal 			ata_link_notice(link,
3993af9ca4dSDamien Le Moal 				"FORCE: link flag 0x%x cleared -> 0x%x\n",
4003af9ca4dSDamien Le Moal 				fe->param.lflags_off, link->flags);
40105944bdfSTejun Heo 		}
40233267325STejun Heo 	}
40333267325STejun Heo }
40433267325STejun Heo 
40533267325STejun Heo /**
40633267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
40733267325STejun Heo  *	@dev: ATA device of interest
40833267325STejun Heo  *
40933267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
41033267325STejun Heo  *	For consistency with link selection, device number 15 selects
41133267325STejun Heo  *	the first device connected to the host link.
41233267325STejun Heo  *
41333267325STejun Heo  *	LOCKING:
41433267325STejun Heo  *	EH context.
41533267325STejun Heo  */
ata_force_xfermask(struct ata_device * dev)41633267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
41733267325STejun Heo {
41833267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
41933267325STejun Heo 	int alt_devno = devno;
42033267325STejun Heo 	int i;
42133267325STejun Heo 
422b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
423b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
424b1c72916STejun Heo 		alt_devno += 15;
42533267325STejun Heo 
42633267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
42733267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
428f0a6d77bSSergey Shtylyov 		unsigned int pio_mask, mwdma_mask, udma_mask;
42933267325STejun Heo 
43033267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
43133267325STejun Heo 			continue;
43233267325STejun Heo 
43333267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
43433267325STejun Heo 		    fe->device != alt_devno)
43533267325STejun Heo 			continue;
43633267325STejun Heo 
43733267325STejun Heo 		if (!fe->param.xfer_mask)
43833267325STejun Heo 			continue;
43933267325STejun Heo 
44033267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
44133267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
44233267325STejun Heo 		if (udma_mask)
44333267325STejun Heo 			dev->udma_mask = udma_mask;
44433267325STejun Heo 		else if (mwdma_mask) {
44533267325STejun Heo 			dev->udma_mask = 0;
44633267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
44733267325STejun Heo 		} else {
44833267325STejun Heo 			dev->udma_mask = 0;
44933267325STejun Heo 			dev->mwdma_mask = 0;
45033267325STejun Heo 			dev->pio_mask = pio_mask;
45133267325STejun Heo 		}
45233267325STejun Heo 
453a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
454a9a79dfeSJoe Perches 			       fe->param.name);
45533267325STejun Heo 		return;
45633267325STejun Heo 	}
45733267325STejun Heo }
45833267325STejun Heo 
45933267325STejun Heo /**
46033267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
46133267325STejun Heo  *	@dev: ATA device of interest
46233267325STejun Heo  *
46333267325STejun Heo  *	Force horkage according to libata.force and whine about it.
46433267325STejun Heo  *	For consistency with link selection, device number 15 selects
46533267325STejun Heo  *	the first device connected to the host link.
46633267325STejun Heo  *
46733267325STejun Heo  *	LOCKING:
46833267325STejun Heo  *	EH context.
46933267325STejun Heo  */
ata_force_horkage(struct ata_device * dev)47033267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
47133267325STejun Heo {
47233267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
47333267325STejun Heo 	int alt_devno = devno;
47433267325STejun Heo 	int i;
47533267325STejun Heo 
476b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
477b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
478b1c72916STejun Heo 		alt_devno += 15;
47933267325STejun Heo 
48033267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
48133267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
48233267325STejun Heo 
48333267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
48433267325STejun Heo 			continue;
48533267325STejun Heo 
48633267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
48733267325STejun Heo 		    fe->device != alt_devno)
48833267325STejun Heo 			continue;
48933267325STejun Heo 
49033267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
49133267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
49233267325STejun Heo 			continue;
49333267325STejun Heo 
49433267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
49533267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
49633267325STejun Heo 
497a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
498a9a79dfeSJoe Perches 			       fe->param.name);
49933267325STejun Heo 	}
50033267325STejun Heo }
501bf89b0bfSBartlomiej Zolnierkiewicz #else
ata_force_link_limits(struct ata_link * link)502bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_link_limits(struct ata_link *link) { }
ata_force_xfermask(struct ata_device * dev)503bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_xfermask(struct ata_device *dev) { }
ata_force_horkage(struct ata_device * dev)504bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_horkage(struct ata_device *dev) { }
505bf89b0bfSBartlomiej Zolnierkiewicz #endif
50633267325STejun Heo 
50733267325STejun Heo /**
508436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
509436d34b3STejun Heo  *	@opcode: SCSI opcode
510436d34b3STejun Heo  *
511436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
512436d34b3STejun Heo  *
513436d34b3STejun Heo  *	LOCKING:
514436d34b3STejun Heo  *	None.
515436d34b3STejun Heo  *
516436d34b3STejun Heo  *	RETURNS:
517436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
518436d34b3STejun Heo  */
atapi_cmd_type(u8 opcode)519436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
520436d34b3STejun Heo {
521436d34b3STejun Heo 	switch (opcode) {
522436d34b3STejun Heo 	case GPCMD_READ_10:
523436d34b3STejun Heo 	case GPCMD_READ_12:
524436d34b3STejun Heo 		return ATAPI_READ;
525436d34b3STejun Heo 
526436d34b3STejun Heo 	case GPCMD_WRITE_10:
527436d34b3STejun Heo 	case GPCMD_WRITE_12:
528436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
529436d34b3STejun Heo 		return ATAPI_WRITE;
530436d34b3STejun Heo 
531436d34b3STejun Heo 	case GPCMD_READ_CD:
532436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
533436d34b3STejun Heo 		return ATAPI_READ_CD;
534436d34b3STejun Heo 
535e52dcc48STejun Heo 	case ATA_16:
536e52dcc48STejun Heo 	case ATA_12:
537e52dcc48STejun Heo 		if (atapi_passthru16)
538e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
539df561f66SGustavo A. R. Silva 		fallthrough;
540436d34b3STejun Heo 	default:
541436d34b3STejun Heo 		return ATAPI_MISC;
542436d34b3STejun Heo 	}
543436d34b3STejun Heo }
544a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(atapi_cmd_type);
545436d34b3STejun Heo 
546c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
547c6fd2807SJeff Garzik 	/* pio multi */
548c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
549c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
550c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
551c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
552c6fd2807SJeff Garzik 	0,
553c6fd2807SJeff Garzik 	0,
554c6fd2807SJeff Garzik 	0,
555bc9af490SDamien Le Moal 	0,
556c6fd2807SJeff Garzik 	/* pio */
557c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
558c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
559c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
560c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
561c6fd2807SJeff Garzik 	0,
562c6fd2807SJeff Garzik 	0,
563c6fd2807SJeff Garzik 	0,
564c6fd2807SJeff Garzik 	0,
565c6fd2807SJeff Garzik 	/* dma */
566c6fd2807SJeff Garzik 	ATA_CMD_READ,
567c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
568c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
569c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
570c6fd2807SJeff Garzik 	0,
571c6fd2807SJeff Garzik 	0,
572c6fd2807SJeff Garzik 	0,
573c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
574c6fd2807SJeff Garzik };
575c6fd2807SJeff Garzik 
576c6fd2807SJeff Garzik /**
57777839debSDamien Le Moal  *	ata_set_rwcmd_protocol - set taskfile r/w command and protocol
57877839debSDamien Le Moal  *	@dev: target device for the taskfile
57977839debSDamien Le Moal  *	@tf: taskfile to examine and configure
580c6fd2807SJeff Garzik  *
58177839debSDamien Le Moal  *	Examine the device configuration and tf->flags to determine
58277839debSDamien Le Moal  *	the proper read/write command and protocol to use for @tf.
583c6fd2807SJeff Garzik  *
584c6fd2807SJeff Garzik  *	LOCKING:
585c6fd2807SJeff Garzik  *	caller.
586c6fd2807SJeff Garzik  */
ata_set_rwcmd_protocol(struct ata_device * dev,struct ata_taskfile * tf)58777839debSDamien Le Moal static bool ata_set_rwcmd_protocol(struct ata_device *dev,
58877839debSDamien Le Moal 				   struct ata_taskfile *tf)
589c6fd2807SJeff Garzik {
590c6fd2807SJeff Garzik 	u8 cmd;
591c6fd2807SJeff Garzik 
592c6fd2807SJeff Garzik 	int index, fua, lba48, write;
593c6fd2807SJeff Garzik 
594c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
595c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
596c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
597c6fd2807SJeff Garzik 
598c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
599c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
600c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6019af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
602c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
603c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
604c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
605c6fd2807SJeff Garzik 	} else {
606c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
607c6fd2807SJeff Garzik 		index = 16;
608c6fd2807SJeff Garzik 	}
609c6fd2807SJeff Garzik 
610c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
61177839debSDamien Le Moal 	if (!cmd)
61277839debSDamien Le Moal 		return false;
61377839debSDamien Le Moal 
614c6fd2807SJeff Garzik 	tf->command = cmd;
61577839debSDamien Le Moal 
61677839debSDamien Le Moal 	return true;
617c6fd2807SJeff Garzik }
618c6fd2807SJeff Garzik 
619c6fd2807SJeff Garzik /**
62035b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
62135b649feSTejun Heo  *	@tf: ATA taskfile of interest
62235b649feSTejun Heo  *	@dev: ATA device @tf belongs to
62335b649feSTejun Heo  *
62435b649feSTejun Heo  *	LOCKING:
62535b649feSTejun Heo  *	None.
62635b649feSTejun Heo  *
62735b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
62835b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
62935b649feSTejun Heo  *	flags select the address format to use.
63035b649feSTejun Heo  *
63135b649feSTejun Heo  *	RETURNS:
63235b649feSTejun Heo  *	Block address read from @tf.
63335b649feSTejun Heo  */
ata_tf_read_block(const struct ata_taskfile * tf,struct ata_device * dev)634cffd1ee9SHannes Reinecke u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
63535b649feSTejun Heo {
63635b649feSTejun Heo 	u64 block = 0;
63735b649feSTejun Heo 
638fe16d4f2STejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
63935b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
64035b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
64135b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
64244901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
64335b649feSTejun Heo 		} else
64435b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
64535b649feSTejun Heo 
64635b649feSTejun Heo 		block |= tf->lbah << 16;
64735b649feSTejun Heo 		block |= tf->lbam << 8;
64835b649feSTejun Heo 		block |= tf->lbal;
64935b649feSTejun Heo 	} else {
65035b649feSTejun Heo 		u32 cyl, head, sect;
65135b649feSTejun Heo 
65235b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
65335b649feSTejun Heo 		head = tf->device & 0xf;
65435b649feSTejun Heo 		sect = tf->lbal;
65535b649feSTejun Heo 
656ac8672eaSTejun Heo 		if (!sect) {
657a9a79dfeSJoe Perches 			ata_dev_warn(dev,
658a9a79dfeSJoe Perches 				     "device reported invalid CHS sector 0\n");
659cffd1ee9SHannes Reinecke 			return U64_MAX;
660ac8672eaSTejun Heo 		}
661ac8672eaSTejun Heo 
662ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
66335b649feSTejun Heo 	}
66435b649feSTejun Heo 
66535b649feSTejun Heo 	return block;
66635b649feSTejun Heo }
66735b649feSTejun Heo 
668eafe804bSDamien Le Moal /*
669eafe804bSDamien Le Moal  * Set a taskfile command duration limit index.
670eafe804bSDamien Le Moal  */
ata_set_tf_cdl(struct ata_queued_cmd * qc,int cdl)671eafe804bSDamien Le Moal static inline void ata_set_tf_cdl(struct ata_queued_cmd *qc, int cdl)
672eafe804bSDamien Le Moal {
673eafe804bSDamien Le Moal 	struct ata_taskfile *tf = &qc->tf;
674eafe804bSDamien Le Moal 
675eafe804bSDamien Le Moal 	if (tf->protocol == ATA_PROT_NCQ)
676eafe804bSDamien Le Moal 		tf->auxiliary |= cdl;
677eafe804bSDamien Le Moal 	else
678eafe804bSDamien Le Moal 		tf->feature |= cdl;
679eafe804bSDamien Le Moal 
68018bd7718SNiklas Cassel 	/*
68118bd7718SNiklas Cassel 	 * Mark this command as having a CDL and request the result
68218bd7718SNiklas Cassel 	 * task file so that we can inspect the sense data available
68318bd7718SNiklas Cassel 	 * bit on completion.
68418bd7718SNiklas Cassel 	 */
68518bd7718SNiklas Cassel 	qc->flags |= ATA_QCFLAG_HAS_CDL | ATA_QCFLAG_RESULT_TF;
686eafe804bSDamien Le Moal }
687eafe804bSDamien Le Moal 
68835b649feSTejun Heo /**
689bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
690066de3b9SDamien Le Moal  *	@qc: Metadata associated with the taskfile to build
691bd056d7eSTejun Heo  *	@block: Block address
692bd056d7eSTejun Heo  *	@n_block: Number of blocks
693bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
694eafe804bSDamien Le Moal  *	@cdl: Command duration limit index
6958e061784SAdam Manzanares  *	@class: IO priority class
696bd056d7eSTejun Heo  *
697bd056d7eSTejun Heo  *	LOCKING:
698bd056d7eSTejun Heo  *	None.
699bd056d7eSTejun Heo  *
700066de3b9SDamien Le Moal  *	Build ATA taskfile for the command @qc for read/write request described
701066de3b9SDamien Le Moal  *	by @block, @n_block, @tf_flags and @class.
702bd056d7eSTejun Heo  *
703bd056d7eSTejun Heo  *	RETURNS:
704bd056d7eSTejun Heo  *
705bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
706bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
707bd056d7eSTejun Heo  */
ata_build_rw_tf(struct ata_queued_cmd * qc,u64 block,u32 n_block,unsigned int tf_flags,int cdl,int class)708066de3b9SDamien Le Moal int ata_build_rw_tf(struct ata_queued_cmd *qc, u64 block, u32 n_block,
709eafe804bSDamien Le Moal 		    unsigned int tf_flags, int cdl, int class)
710bd056d7eSTejun Heo {
711066de3b9SDamien Le Moal 	struct ata_taskfile *tf = &qc->tf;
712066de3b9SDamien Le Moal 	struct ata_device *dev = qc->dev;
713066de3b9SDamien Le Moal 
714bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
715bd056d7eSTejun Heo 	tf->flags |= tf_flags;
716bd056d7eSTejun Heo 
717066de3b9SDamien Le Moal 	if (ata_ncq_enabled(dev)) {
718bd056d7eSTejun Heo 		/* yay, NCQ */
719bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
720bd056d7eSTejun Heo 			return -ERANGE;
721bd056d7eSTejun Heo 
722bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
723bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
724bd056d7eSTejun Heo 
725bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
726bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
727bd056d7eSTejun Heo 		else
728bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
729bd056d7eSTejun Heo 
730066de3b9SDamien Le Moal 		tf->nsect = qc->hw_tag << 3;
731bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
732bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
733bd056d7eSTejun Heo 
734bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
735bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
736bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
737bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
738bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
739bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
740bd056d7eSTejun Heo 
7419ca7cfa4SSergei Shtylyov 		tf->device = ATA_LBA;
742bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
743bd056d7eSTejun Heo 			tf->device |= 1 << 7;
7448e061784SAdam Manzanares 
745e00923c5SDamien Le Moal 		if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED &&
7462360fa18SDamien Le Moal 		    class == IOPRIO_CLASS_RT)
7472360fa18SDamien Le Moal 			tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO;
748eafe804bSDamien Le Moal 
749eafe804bSDamien Le Moal 		if ((dev->flags & ATA_DFLAG_CDL_ENABLED) && cdl)
750eafe804bSDamien Le Moal 			ata_set_tf_cdl(qc, cdl);
751eafe804bSDamien Le Moal 
752bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
753bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
754bd056d7eSTejun Heo 
755eafe804bSDamien Le Moal 		if ((dev->flags & ATA_DFLAG_CDL_ENABLED) && cdl)
756eafe804bSDamien Le Moal 			ata_set_tf_cdl(qc, cdl);
757eafe804bSDamien Le Moal 
758eafe804bSDamien Le Moal 		/* Both FUA writes and a CDL index require 48-bit commands */
759eafe804bSDamien Le Moal 		if (!(tf->flags & ATA_TFLAG_FUA) &&
760eafe804bSDamien Le Moal 		    !(qc->flags & ATA_QCFLAG_HAS_CDL) &&
761eafe804bSDamien Le Moal 		    lba_28_ok(block, n_block)) {
762bd056d7eSTejun Heo 			/* use LBA28 */
763bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
764bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
765bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
766bd056d7eSTejun Heo 				return -ERANGE;
767bd056d7eSTejun Heo 
768bd056d7eSTejun Heo 			/* use LBA48 */
769bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
770bd056d7eSTejun Heo 
771bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
772bd056d7eSTejun Heo 
773bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
774bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
775bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
776bc9af490SDamien Le Moal 		} else {
777bd056d7eSTejun Heo 			/* request too large even for LBA48 */
778bd056d7eSTejun Heo 			return -ERANGE;
779bc9af490SDamien Le Moal 		}
780bd056d7eSTejun Heo 
78177839debSDamien Le Moal 		if (unlikely(!ata_set_rwcmd_protocol(dev, tf)))
782bd056d7eSTejun Heo 			return -EINVAL;
783bd056d7eSTejun Heo 
784bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
785bd056d7eSTejun Heo 
786bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
787bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
788bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
789bd056d7eSTejun Heo 
790bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
791bd056d7eSTejun Heo 	} else {
792bd056d7eSTejun Heo 		/* CHS */
793bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
794bd056d7eSTejun Heo 
795bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
796bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
797bd056d7eSTejun Heo 			return -ERANGE;
798bd056d7eSTejun Heo 
79977839debSDamien Le Moal 		if (unlikely(!ata_set_rwcmd_protocol(dev, tf)))
800bd056d7eSTejun Heo 			return -EINVAL;
801bd056d7eSTejun Heo 
802bd056d7eSTejun Heo 		/* Convert LBA to CHS */
803bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
804bd056d7eSTejun Heo 		cyl   = track / dev->heads;
805bd056d7eSTejun Heo 		head  = track % dev->heads;
806bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
807bd056d7eSTejun Heo 
808bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
809bd056d7eSTejun Heo 		   Cylinder: 0-65535
810bd056d7eSTejun Heo 		   Head: 0-15
811bd056d7eSTejun Heo 		   Sector: 1-255*/
812bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
813bd056d7eSTejun Heo 			return -ERANGE;
814bd056d7eSTejun Heo 
815bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
816bd056d7eSTejun Heo 		tf->lbal = sect;
817bd056d7eSTejun Heo 		tf->lbam = cyl;
818bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
819bd056d7eSTejun Heo 		tf->device |= head;
820bd056d7eSTejun Heo 	}
821bd056d7eSTejun Heo 
822bd056d7eSTejun Heo 	return 0;
823bd056d7eSTejun Heo }
824bd056d7eSTejun Heo 
825bd056d7eSTejun Heo /**
826c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
827c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
828c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
829c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
830c6fd2807SJeff Garzik  *
831c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
832c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
833c6fd2807SJeff Garzik  *
834c6fd2807SJeff Garzik  *	LOCKING:
835c6fd2807SJeff Garzik  *	None.
836c6fd2807SJeff Garzik  *
837c6fd2807SJeff Garzik  *	RETURNS:
838c6fd2807SJeff Garzik  *	Packed xfer_mask.
839c6fd2807SJeff Garzik  */
ata_pack_xfermask(unsigned int pio_mask,unsigned int mwdma_mask,unsigned int udma_mask)840f0a6d77bSSergey Shtylyov unsigned int ata_pack_xfermask(unsigned int pio_mask,
841f0a6d77bSSergey Shtylyov 			       unsigned int mwdma_mask,
842f0a6d77bSSergey Shtylyov 			       unsigned int udma_mask)
843c6fd2807SJeff Garzik {
844c6fd2807SJeff Garzik 	return	((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
845c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
846c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
847c6fd2807SJeff Garzik }
848a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pack_xfermask);
849c6fd2807SJeff Garzik 
850c6fd2807SJeff Garzik /**
851c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
852c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
853c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
854c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
855c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
856c6fd2807SJeff Garzik  *
857c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
858c9b5560aSMasanari Iida  *	Any NULL destination masks will be ignored.
859c6fd2807SJeff Garzik  */
ata_unpack_xfermask(unsigned int xfer_mask,unsigned int * pio_mask,unsigned int * mwdma_mask,unsigned int * udma_mask)860f0a6d77bSSergey Shtylyov void ata_unpack_xfermask(unsigned int xfer_mask, unsigned int *pio_mask,
861f0a6d77bSSergey Shtylyov 			 unsigned int *mwdma_mask, unsigned int *udma_mask)
862c6fd2807SJeff Garzik {
863c6fd2807SJeff Garzik 	if (pio_mask)
864c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
865c6fd2807SJeff Garzik 	if (mwdma_mask)
866c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
867c6fd2807SJeff Garzik 	if (udma_mask)
868c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
869c6fd2807SJeff Garzik }
870c6fd2807SJeff Garzik 
871c6fd2807SJeff Garzik static const struct ata_xfer_ent {
872c6fd2807SJeff Garzik 	int shift, bits;
873c6fd2807SJeff Garzik 	u8 base;
874c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
87570cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
87670cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
87770cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
878c6fd2807SJeff Garzik 	{ -1, },
879c6fd2807SJeff Garzik };
880c6fd2807SJeff Garzik 
881c6fd2807SJeff Garzik /**
882c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
883c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
884c6fd2807SJeff Garzik  *
885c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
886c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
887c6fd2807SJeff Garzik  *
888c6fd2807SJeff Garzik  *	LOCKING:
889c6fd2807SJeff Garzik  *	None.
890c6fd2807SJeff Garzik  *
891c6fd2807SJeff Garzik  *	RETURNS:
89270cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
893c6fd2807SJeff Garzik  */
ata_xfer_mask2mode(unsigned int xfer_mask)894f0a6d77bSSergey Shtylyov u8 ata_xfer_mask2mode(unsigned int xfer_mask)
895c6fd2807SJeff Garzik {
896c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
897c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
898c6fd2807SJeff Garzik 
899c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
900c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
901c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
90270cd071eSTejun Heo 	return 0xff;
903c6fd2807SJeff Garzik }
904a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
905c6fd2807SJeff Garzik 
906c6fd2807SJeff Garzik /**
907c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
908c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
909c6fd2807SJeff Garzik  *
910c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
911c6fd2807SJeff Garzik  *
912c6fd2807SJeff Garzik  *	LOCKING:
913c6fd2807SJeff Garzik  *	None.
914c6fd2807SJeff Garzik  *
915c6fd2807SJeff Garzik  *	RETURNS:
916c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
917c6fd2807SJeff Garzik  */
ata_xfer_mode2mask(u8 xfer_mode)918f0a6d77bSSergey Shtylyov unsigned int ata_xfer_mode2mask(u8 xfer_mode)
919c6fd2807SJeff Garzik {
920c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
921c6fd2807SJeff Garzik 
922c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
923c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
92470cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
92570cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
926c6fd2807SJeff Garzik 	return 0;
927c6fd2807SJeff Garzik }
928a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
929c6fd2807SJeff Garzik 
930c6fd2807SJeff Garzik /**
931c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
932c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
933c6fd2807SJeff Garzik  *
934c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
935c6fd2807SJeff Garzik  *
936c6fd2807SJeff Garzik  *	LOCKING:
937c6fd2807SJeff Garzik  *	None.
938c6fd2807SJeff Garzik  *
939c6fd2807SJeff Garzik  *	RETURNS:
940c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
941c6fd2807SJeff Garzik  */
ata_xfer_mode2shift(u8 xfer_mode)942a28c1ab3SSergey Shtylyov int ata_xfer_mode2shift(u8 xfer_mode)
943c6fd2807SJeff Garzik {
944c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
945c6fd2807SJeff Garzik 
946c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
947c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
948c6fd2807SJeff Garzik 			return ent->shift;
949c6fd2807SJeff Garzik 	return -1;
950c6fd2807SJeff Garzik }
951a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
952c6fd2807SJeff Garzik 
953c6fd2807SJeff Garzik /**
954c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
955c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
956c6fd2807SJeff Garzik  *
957c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
958c6fd2807SJeff Garzik  *	(highest bit in @modemask).
959c6fd2807SJeff Garzik  *
960c6fd2807SJeff Garzik  *	LOCKING:
961c6fd2807SJeff Garzik  *	None.
962c6fd2807SJeff Garzik  *
963c6fd2807SJeff Garzik  *	RETURNS:
964c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
965c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
966c6fd2807SJeff Garzik  */
ata_mode_string(unsigned int xfer_mask)967f0a6d77bSSergey Shtylyov const char *ata_mode_string(unsigned int xfer_mask)
968c6fd2807SJeff Garzik {
969c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
970c6fd2807SJeff Garzik 		"PIO0",
971c6fd2807SJeff Garzik 		"PIO1",
972c6fd2807SJeff Garzik 		"PIO2",
973c6fd2807SJeff Garzik 		"PIO3",
974c6fd2807SJeff Garzik 		"PIO4",
975b352e57dSAlan Cox 		"PIO5",
976b352e57dSAlan Cox 		"PIO6",
977c6fd2807SJeff Garzik 		"MWDMA0",
978c6fd2807SJeff Garzik 		"MWDMA1",
979c6fd2807SJeff Garzik 		"MWDMA2",
980b352e57dSAlan Cox 		"MWDMA3",
981b352e57dSAlan Cox 		"MWDMA4",
982c6fd2807SJeff Garzik 		"UDMA/16",
983c6fd2807SJeff Garzik 		"UDMA/25",
984c6fd2807SJeff Garzik 		"UDMA/33",
985c6fd2807SJeff Garzik 		"UDMA/44",
986c6fd2807SJeff Garzik 		"UDMA/66",
987c6fd2807SJeff Garzik 		"UDMA/100",
988c6fd2807SJeff Garzik 		"UDMA/133",
989c6fd2807SJeff Garzik 		"UDMA7",
990c6fd2807SJeff Garzik 	};
991c6fd2807SJeff Garzik 	int highbit;
992c6fd2807SJeff Garzik 
993c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
994c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
995c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
996c6fd2807SJeff Garzik 	return "<n/a>";
997c6fd2807SJeff Garzik }
998a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_mode_string);
999c6fd2807SJeff Garzik 
sata_spd_string(unsigned int spd)1000d9027470SGwendal Grignou const char *sata_spd_string(unsigned int spd)
1001c6fd2807SJeff Garzik {
1002c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
1003c6fd2807SJeff Garzik 		"1.5 Gbps",
1004c6fd2807SJeff Garzik 		"3.0 Gbps",
10058522ee25SShane Huang 		"6.0 Gbps",
1006c6fd2807SJeff Garzik 	};
1007c6fd2807SJeff Garzik 
1008c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1009c6fd2807SJeff Garzik 		return "<unknown>";
1010c6fd2807SJeff Garzik 	return spd_str[spd - 1];
1011c6fd2807SJeff Garzik }
1012c6fd2807SJeff Garzik 
1013c6fd2807SJeff Garzik /**
1014c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
1015c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
1016c6fd2807SJeff Garzik  *
1017c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
1018c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
1019c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
1020c6fd2807SJeff Garzik  *
1021c6fd2807SJeff Garzik  *	LOCKING:
1022c6fd2807SJeff Garzik  *	None.
1023c6fd2807SJeff Garzik  *
1024c6fd2807SJeff Garzik  *	RETURNS:
10259162c657SHannes Reinecke  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
10269162c657SHannes Reinecke  *	%ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
1027c6fd2807SJeff Garzik  */
ata_dev_classify(const struct ata_taskfile * tf)1028c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1029c6fd2807SJeff Garzik {
1030c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1031c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1032c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1033633273a3STejun Heo 	 *
1034633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1035633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1036633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1037633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1038633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1039633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1040633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1041633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1042633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1043633273a3STejun Heo 	 * SerialATA.
1044633273a3STejun Heo 	 *
1045633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1046633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
104779b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
104879b42babSTejun Heo 	 * SEMB signature.  This is worked around in
104979b42babSTejun Heo 	 * ata_dev_read_id().
1050c6fd2807SJeff Garzik 	 */
10516c952a0dSHannes Reinecke 	if (tf->lbam == 0 && tf->lbah == 0)
1052c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1053c6fd2807SJeff Garzik 
10546c952a0dSHannes Reinecke 	if (tf->lbam == 0x14 && tf->lbah == 0xeb)
1055c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1056c6fd2807SJeff Garzik 
10576c952a0dSHannes Reinecke 	if (tf->lbam == 0x69 && tf->lbah == 0x96)
1058633273a3STejun Heo 		return ATA_DEV_PMP;
1059633273a3STejun Heo 
10606c952a0dSHannes Reinecke 	if (tf->lbam == 0x3c && tf->lbah == 0xc3)
106179b42babSTejun Heo 		return ATA_DEV_SEMB;
1062633273a3STejun Heo 
10636c952a0dSHannes Reinecke 	if (tf->lbam == 0xcd && tf->lbah == 0xab)
10649162c657SHannes Reinecke 		return ATA_DEV_ZAC;
10659162c657SHannes Reinecke 
1066c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1067c6fd2807SJeff Garzik }
1068a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_classify);
1069c6fd2807SJeff Garzik 
1070c6fd2807SJeff Garzik /**
1071c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1072c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1073c6fd2807SJeff Garzik  *	@s: string into which data is output
1074c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1075c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1076c6fd2807SJeff Garzik  *
1077c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1078c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1079c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1080c6fd2807SJeff Garzik  *
1081c6fd2807SJeff Garzik  *	LOCKING:
1082c6fd2807SJeff Garzik  *	caller.
1083c6fd2807SJeff Garzik  */
1084c6fd2807SJeff Garzik 
ata_id_string(const u16 * id,unsigned char * s,unsigned int ofs,unsigned int len)1085c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1086c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1087c6fd2807SJeff Garzik {
1088c6fd2807SJeff Garzik 	unsigned int c;
1089c6fd2807SJeff Garzik 
1090963e4975SAlan Cox 	BUG_ON(len & 1);
1091963e4975SAlan Cox 
1092c6fd2807SJeff Garzik 	while (len > 0) {
1093c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1094c6fd2807SJeff Garzik 		*s = c;
1095c6fd2807SJeff Garzik 		s++;
1096c6fd2807SJeff Garzik 
1097c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1098c6fd2807SJeff Garzik 		*s = c;
1099c6fd2807SJeff Garzik 		s++;
1100c6fd2807SJeff Garzik 
1101c6fd2807SJeff Garzik 		ofs++;
1102c6fd2807SJeff Garzik 		len -= 2;
1103c6fd2807SJeff Garzik 	}
1104c6fd2807SJeff Garzik }
1105a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_string);
1106c6fd2807SJeff Garzik 
1107c6fd2807SJeff Garzik /**
1108c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1109c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1110c6fd2807SJeff Garzik  *	@s: string into which data is output
1111c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1112c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1113c6fd2807SJeff Garzik  *
1114c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1115c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1116c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1117c6fd2807SJeff Garzik  *
1118c6fd2807SJeff Garzik  *	LOCKING:
1119c6fd2807SJeff Garzik  *	caller.
1120c6fd2807SJeff Garzik  */
ata_id_c_string(const u16 * id,unsigned char * s,unsigned int ofs,unsigned int len)1121c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1122c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1123c6fd2807SJeff Garzik {
1124c6fd2807SJeff Garzik 	unsigned char *p;
1125c6fd2807SJeff Garzik 
1126c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1127c6fd2807SJeff Garzik 
1128c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1129c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1130c6fd2807SJeff Garzik 		p--;
1131c6fd2807SJeff Garzik 	*p = '\0';
1132c6fd2807SJeff Garzik }
1133a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_c_string);
1134c6fd2807SJeff Garzik 
ata_id_n_sectors(const u16 * id)1135db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1136db6f8759STejun Heo {
1137db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1138db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1139968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
11405eb8deb4SSergey Shtylyov 
1141968e594aSRobert Hancock 		return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1142db6f8759STejun Heo 	}
11435eb8deb4SSergey Shtylyov 
11445eb8deb4SSergey Shtylyov 	if (ata_id_current_chs_valid(id))
11455eb8deb4SSergey Shtylyov 		return (u32)id[ATA_ID_CUR_CYLS] * (u32)id[ATA_ID_CUR_HEADS] *
11465eb8deb4SSergey Shtylyov 		       (u32)id[ATA_ID_CUR_SECTORS];
11475eb8deb4SSergey Shtylyov 
11485eb8deb4SSergey Shtylyov 	return (u32)id[ATA_ID_CYLS] * (u32)id[ATA_ID_HEADS] *
11495eb8deb4SSergey Shtylyov 	       (u32)id[ATA_ID_SECTORS];
1150db6f8759STejun Heo }
1151db6f8759STejun Heo 
ata_tf_to_lba48(const struct ata_taskfile * tf)1152a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
11531e999736SAlan Cox {
11541e999736SAlan Cox 	u64 sectors = 0;
11551e999736SAlan Cox 
11561e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
11571e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1158ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
11591e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11601e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11611e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11621e999736SAlan Cox 
1163a5987e0aSTejun Heo 	return sectors;
11641e999736SAlan Cox }
11651e999736SAlan Cox 
ata_tf_to_lba(const struct ata_taskfile * tf)1166a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
11671e999736SAlan Cox {
11681e999736SAlan Cox 	u64 sectors = 0;
11691e999736SAlan Cox 
11701e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
11711e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11721e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11731e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11741e999736SAlan Cox 
1175a5987e0aSTejun Heo 	return sectors;
11761e999736SAlan Cox }
11771e999736SAlan Cox 
11781e999736SAlan Cox /**
1179c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1180c728a914STejun Heo  *	@dev: target device
1181c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
11821e999736SAlan Cox  *
1183c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1184c728a914STejun Heo  *	question.
1185c728a914STejun Heo  *
1186c728a914STejun Heo  *	RETURNS:
1187c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1188c728a914STejun Heo  *	-EIO on other errors.
11891e999736SAlan Cox  */
ata_read_native_max_address(struct ata_device * dev,u64 * max_sectors)1190c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
11911e999736SAlan Cox {
1192c728a914STejun Heo 	unsigned int err_mask;
11931e999736SAlan Cox 	struct ata_taskfile tf;
1194c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
11951e999736SAlan Cox 
11961e999736SAlan Cox 	ata_tf_init(dev, &tf);
11971e999736SAlan Cox 
1198c728a914STejun Heo 	/* always clear all address registers */
11991e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1200c728a914STejun Heo 
1201c728a914STejun Heo 	if (lba48) {
1202c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1203c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1204c728a914STejun Heo 	} else
1205c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1206c728a914STejun Heo 
1207bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1208c728a914STejun Heo 	tf.device |= ATA_LBA;
12091e999736SAlan Cox 
12102b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1211c728a914STejun Heo 	if (err_mask) {
1212a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1213a9a79dfeSJoe Perches 			     "failed to read native max address (err_mask=0x%x)\n",
1214a9a79dfeSJoe Perches 			     err_mask);
1215efcef265SSergey Shtylyov 		if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
1216c728a914STejun Heo 			return -EACCES;
1217c728a914STejun Heo 		return -EIO;
1218c728a914STejun Heo 	}
1219c728a914STejun Heo 
1220c728a914STejun Heo 	if (lba48)
1221a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1222c728a914STejun Heo 	else
1223a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
122493328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
122593328e11SAlan Cox 		(*max_sectors)--;
12261e999736SAlan Cox 	return 0;
12271e999736SAlan Cox }
12281e999736SAlan Cox 
12291e999736SAlan Cox /**
1230c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1231c728a914STejun Heo  *	@dev: target device
12326b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
12331e999736SAlan Cox  *
1234c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1235c728a914STejun Heo  *
1236c728a914STejun Heo  *	RETURNS:
1237c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1238c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1239c728a914STejun Heo  *	errors.
12401e999736SAlan Cox  */
ata_set_max_sectors(struct ata_device * dev,u64 new_sectors)124105027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
12421e999736SAlan Cox {
1243c728a914STejun Heo 	unsigned int err_mask;
12441e999736SAlan Cox 	struct ata_taskfile tf;
1245c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12461e999736SAlan Cox 
12471e999736SAlan Cox 	new_sectors--;
12481e999736SAlan Cox 
12491e999736SAlan Cox 	ata_tf_init(dev, &tf);
12501e999736SAlan Cox 
1251c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
12521e999736SAlan Cox 
1253c728a914STejun Heo 	if (lba48) {
1254c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1255c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
12561e999736SAlan Cox 
12571e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
12581e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
12591e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
12601e582ba4STejun Heo 	} else {
12611e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1262c728a914STejun Heo 
12631e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
12641e582ba4STejun Heo 	}
12651e582ba4STejun Heo 
1266bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1267c728a914STejun Heo 	tf.device |= ATA_LBA;
12681e999736SAlan Cox 
12691e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
12701e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
12711e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
12721e999736SAlan Cox 
12732b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1274c728a914STejun Heo 	if (err_mask) {
1275a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1276a9a79dfeSJoe Perches 			     "failed to set max address (err_mask=0x%x)\n",
1277a9a79dfeSJoe Perches 			     err_mask);
1278c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1279efcef265SSergey Shtylyov 		    (tf.error & (ATA_ABORTED | ATA_IDNF)))
1280c728a914STejun Heo 			return -EACCES;
1281c728a914STejun Heo 		return -EIO;
1282c728a914STejun Heo 	}
1283c728a914STejun Heo 
12841e999736SAlan Cox 	return 0;
12851e999736SAlan Cox }
12861e999736SAlan Cox 
12871e999736SAlan Cox /**
12881e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
12891e999736SAlan Cox  *	@dev: Device to resize
12901e999736SAlan Cox  *
12911e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
12921e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
12931e999736SAlan Cox  *	the drive has the HPA feature set enabled.
129405027adcSTejun Heo  *
129505027adcSTejun Heo  *	RETURNS:
129605027adcSTejun Heo  *	0 on success, -errno on failure.
12971e999736SAlan Cox  */
ata_hpa_resize(struct ata_device * dev)129805027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
12991e999736SAlan Cox {
1300891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
1301445d211bSTejun Heo 	bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
130205027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
130305027adcSTejun Heo 	u64 native_sectors;
1304c728a914STejun Heo 	int rc;
13051e999736SAlan Cox 
130605027adcSTejun Heo 	/* do we need to do it? */
13079162c657SHannes Reinecke 	if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
130805027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
130905027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1310c728a914STejun Heo 		return 0;
13111e999736SAlan Cox 
131205027adcSTejun Heo 	/* read native max address */
131305027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
131405027adcSTejun Heo 	if (rc) {
1315dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1316dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
131705027adcSTejun Heo 		 */
1318445d211bSTejun Heo 		if (rc == -EACCES || !unlock_hpa) {
1319a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1320a9a79dfeSJoe Perches 				     "HPA support seems broken, skipping HPA handling\n");
132105027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
132205027adcSTejun Heo 
132305027adcSTejun Heo 			/* we can continue if device aborted the command */
132405027adcSTejun Heo 			if (rc == -EACCES)
132505027adcSTejun Heo 				rc = 0;
132605027adcSTejun Heo 		}
132705027adcSTejun Heo 
132805027adcSTejun Heo 		return rc;
132905027adcSTejun Heo 	}
13305920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
133105027adcSTejun Heo 
133205027adcSTejun Heo 	/* nothing to do? */
1333445d211bSTejun Heo 	if (native_sectors <= sectors || !unlock_hpa) {
133405027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
133505027adcSTejun Heo 			return 0;
133605027adcSTejun Heo 
133705027adcSTejun Heo 		if (native_sectors > sectors)
1338a9a79dfeSJoe Perches 			ata_dev_info(dev,
133905027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
134005027adcSTejun Heo 				(unsigned long long)sectors,
134105027adcSTejun Heo 				(unsigned long long)native_sectors);
134205027adcSTejun Heo 		else if (native_sectors < sectors)
1343a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1344a9a79dfeSJoe Perches 				"native sectors (%llu) is smaller than sectors (%llu)\n",
134505027adcSTejun Heo 				(unsigned long long)native_sectors,
134605027adcSTejun Heo 				(unsigned long long)sectors);
134705027adcSTejun Heo 		return 0;
13481e999736SAlan Cox 	}
134937301a55STejun Heo 
135005027adcSTejun Heo 	/* let's unlock HPA */
135105027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
135205027adcSTejun Heo 	if (rc == -EACCES) {
135305027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
1354a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1355a9a79dfeSJoe Perches 			     "device aborted resize (%llu -> %llu), skipping HPA handling\n",
135605027adcSTejun Heo 			     (unsigned long long)sectors,
135705027adcSTejun Heo 			     (unsigned long long)native_sectors);
135805027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
135905027adcSTejun Heo 		return 0;
136005027adcSTejun Heo 	} else if (rc)
136105027adcSTejun Heo 		return rc;
136205027adcSTejun Heo 
136305027adcSTejun Heo 	/* re-read IDENTIFY data */
136405027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
136505027adcSTejun Heo 	if (rc) {
1366a9a79dfeSJoe Perches 		ata_dev_err(dev,
1367a9a79dfeSJoe Perches 			    "failed to re-read IDENTIFY data after HPA resizing\n");
136805027adcSTejun Heo 		return rc;
136905027adcSTejun Heo 	}
137005027adcSTejun Heo 
137105027adcSTejun Heo 	if (print_info) {
137205027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
1373a9a79dfeSJoe Perches 		ata_dev_info(dev,
137405027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
137505027adcSTejun Heo 			(unsigned long long)sectors,
137605027adcSTejun Heo 			(unsigned long long)new_sectors,
137705027adcSTejun Heo 			(unsigned long long)native_sectors);
137805027adcSTejun Heo 	}
137905027adcSTejun Heo 
138005027adcSTejun Heo 	return 0;
13811e999736SAlan Cox }
13821e999736SAlan Cox 
1383c6fd2807SJeff Garzik /**
1384c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
13856044f3c4SHannes Reinecke  *	@dev: device from which the information is fetched
1386c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1387c6fd2807SJeff Garzik  *
1388c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1389c6fd2807SJeff Garzik  *	page.
1390c6fd2807SJeff Garzik  *
1391c6fd2807SJeff Garzik  *	LOCKING:
1392c6fd2807SJeff Garzik  *	caller.
1393c6fd2807SJeff Garzik  */
1394c6fd2807SJeff Garzik 
ata_dump_id(struct ata_device * dev,const u16 * id)13956044f3c4SHannes Reinecke static inline void ata_dump_id(struct ata_device *dev, const u16 *id)
1396c6fd2807SJeff Garzik {
13976044f3c4SHannes Reinecke 	ata_dev_dbg(dev,
13986044f3c4SHannes Reinecke 		"49==0x%04x  53==0x%04x  63==0x%04x  64==0x%04x  75==0x%04x\n"
13996044f3c4SHannes Reinecke 		"80==0x%04x  81==0x%04x  82==0x%04x  83==0x%04x  84==0x%04x\n"
14006044f3c4SHannes Reinecke 		"88==0x%04x  93==0x%04x\n",
14016044f3c4SHannes Reinecke 		id[49], id[53], id[63], id[64], id[75], id[80],
14026044f3c4SHannes Reinecke 		id[81], id[82], id[83], id[84], id[88], id[93]);
1403c6fd2807SJeff Garzik }
1404c6fd2807SJeff Garzik 
1405c6fd2807SJeff Garzik /**
1406c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1407c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1408c6fd2807SJeff Garzik  *
1409c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1410c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1411c6fd2807SJeff Garzik  *
1412c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1413c6fd2807SJeff Garzik  *
1414c6fd2807SJeff Garzik  *	LOCKING:
1415c6fd2807SJeff Garzik  *	None.
1416c6fd2807SJeff Garzik  *
1417c6fd2807SJeff Garzik  *	RETURNS:
1418c6fd2807SJeff Garzik  *	Computed xfermask
1419c6fd2807SJeff Garzik  */
ata_id_xfermask(const u16 * id)1420f0a6d77bSSergey Shtylyov unsigned int ata_id_xfermask(const u16 *id)
1421c6fd2807SJeff Garzik {
1422f0a6d77bSSergey Shtylyov 	unsigned int pio_mask, mwdma_mask, udma_mask;
1423c6fd2807SJeff Garzik 
1424c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1425c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1426c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1427c6fd2807SJeff Garzik 		pio_mask <<= 3;
1428c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1429c6fd2807SJeff Garzik 	} else {
1430c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1431c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1432c6fd2807SJeff Garzik 		 * a mask.
1433c6fd2807SJeff Garzik 		 */
14347a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
143546767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
143646767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
143746767aebSAlan Cox 		else
143846767aebSAlan Cox 			pio_mask = 1;
1439c6fd2807SJeff Garzik 
1440c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1441c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1442e0af10acSDiego Viola 		 * process. However it is the speeds not the modes that
1443c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1444c6fd2807SJeff Garzik 		 * will get this right anyway
1445c6fd2807SJeff Garzik 		 */
1446c6fd2807SJeff Garzik 	}
1447c6fd2807SJeff Garzik 
1448c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1449c6fd2807SJeff Garzik 
1450b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1451b352e57dSAlan Cox 		/*
1452b352e57dSAlan Cox 		 *	Process compact flash extended modes
1453b352e57dSAlan Cox 		 */
145462afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
145562afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1456b352e57dSAlan Cox 
1457b352e57dSAlan Cox 		if (pio)
1458b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1459b352e57dSAlan Cox 		if (pio > 1)
1460b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1461b352e57dSAlan Cox 		if (dma)
1462b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1463b352e57dSAlan Cox 		if (dma > 1)
1464b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1465b352e57dSAlan Cox 	}
1466b352e57dSAlan Cox 
1467c6fd2807SJeff Garzik 	udma_mask = 0;
1468c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1469c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1470c6fd2807SJeff Garzik 
1471c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1472c6fd2807SJeff Garzik }
1473a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_xfermask);
1474c6fd2807SJeff Garzik 
ata_qc_complete_internal(struct ata_queued_cmd * qc)14757102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1476c6fd2807SJeff Garzik {
1477c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1478c6fd2807SJeff Garzik 
1479c6fd2807SJeff Garzik 	complete(waiting);
1480c6fd2807SJeff Garzik }
1481c6fd2807SJeff Garzik 
1482c6fd2807SJeff Garzik /**
14832432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1484c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1485c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1486c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1487e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
14885c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
14892432697bSTejun Heo  *	@n_elem: Number of sg entries
14902b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1491c6fd2807SJeff Garzik  *
1492c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1493c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1494c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1495c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1496c6fd2807SJeff Garzik  *	clean up after timeout.
1497c6fd2807SJeff Garzik  *
1498c6fd2807SJeff Garzik  *	LOCKING:
1499c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1500c6fd2807SJeff Garzik  *
1501c6fd2807SJeff Garzik  *	RETURNS:
1502c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1503c6fd2807SJeff Garzik  */
ata_exec_internal_sg(struct ata_device * dev,struct ata_taskfile * tf,const u8 * cdb,int dma_dir,struct scatterlist * sgl,unsigned int n_elem,unsigned int timeout)15044d6119f0SSergey Shtylyov static unsigned ata_exec_internal_sg(struct ata_device *dev,
1505c6fd2807SJeff Garzik 				     struct ata_taskfile *tf, const u8 *cdb,
150687260216SJens Axboe 				     int dma_dir, struct scatterlist *sgl,
150761176eedSSergey Shtylyov 				     unsigned int n_elem, unsigned int timeout)
1508c6fd2807SJeff Garzik {
15099af5c9c9STejun Heo 	struct ata_link *link = dev->link;
15109af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1511c6fd2807SJeff Garzik 	u8 command = tf->command;
151287fbc5a0STejun Heo 	int auto_timeout = 0;
1513c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
151428361c40SJens Axboe 	unsigned int preempted_tag;
1515e3ed8939SJens Axboe 	u32 preempted_sactive;
1516e3ed8939SJens Axboe 	u64 preempted_qc_active;
1517da917d69STejun Heo 	int preempted_nr_active_links;
1518c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1519c6fd2807SJeff Garzik 	unsigned long flags;
1520c6fd2807SJeff Garzik 	unsigned int err_mask;
1521c6fd2807SJeff Garzik 	int rc;
1522c6fd2807SJeff Garzik 
1523c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1524c6fd2807SJeff Garzik 
1525c6fd2807SJeff Garzik 	/* no internal command while frozen */
15264cb7c6f1SNiklas Cassel 	if (ata_port_is_frozen(ap)) {
1527c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1528c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1529c6fd2807SJeff Garzik 	}
1530c6fd2807SJeff Garzik 
1531c6fd2807SJeff Garzik 	/* initialize internal qc */
153228361c40SJens Axboe 	qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
1533c6fd2807SJeff Garzik 
153428361c40SJens Axboe 	qc->tag = ATA_TAG_INTERNAL;
153528361c40SJens Axboe 	qc->hw_tag = 0;
1536c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1537c6fd2807SJeff Garzik 	qc->ap = ap;
1538c6fd2807SJeff Garzik 	qc->dev = dev;
1539c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1540c6fd2807SJeff Garzik 
15419af5c9c9STejun Heo 	preempted_tag = link->active_tag;
15429af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1543c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1544da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
15459af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
15469af5c9c9STejun Heo 	link->sactive = 0;
1547c6fd2807SJeff Garzik 	ap->qc_active = 0;
1548da917d69STejun Heo 	ap->nr_active_links = 0;
1549c6fd2807SJeff Garzik 
1550c6fd2807SJeff Garzik 	/* prepare & issue qc */
1551c6fd2807SJeff Garzik 	qc->tf = *tf;
1552c6fd2807SJeff Garzik 	if (cdb)
1553c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1554e771451cSVincent Pelletier 
1555e771451cSVincent Pelletier 	/* some SATA bridges need us to indicate data xfer direction */
1556e771451cSVincent Pelletier 	if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1557e771451cSVincent Pelletier 	    dma_dir == DMA_FROM_DEVICE)
1558e771451cSVincent Pelletier 		qc->tf.feature |= ATAPI_DMADIR;
1559e771451cSVincent Pelletier 
1560c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1561c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1562c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
15632432697bSTejun Heo 		unsigned int i, buflen = 0;
156487260216SJens Axboe 		struct scatterlist *sg;
15652432697bSTejun Heo 
156687260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
156787260216SJens Axboe 			buflen += sg->length;
15682432697bSTejun Heo 
156987260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
157049c80429SBrian King 		qc->nbytes = buflen;
1571c6fd2807SJeff Garzik 	}
1572c6fd2807SJeff Garzik 
1573c6fd2807SJeff Garzik 	qc->private_data = &wait;
1574c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1575c6fd2807SJeff Garzik 
1576c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1577c6fd2807SJeff Garzik 
1578c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1579c6fd2807SJeff Garzik 
158087fbc5a0STejun Heo 	if (!timeout) {
158187fbc5a0STejun Heo 		if (ata_probe_timeout)
1582341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
158387fbc5a0STejun Heo 		else {
158487fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
158587fbc5a0STejun Heo 			auto_timeout = 1;
158687fbc5a0STejun Heo 		}
158787fbc5a0STejun Heo 	}
15882b789108STejun Heo 
1589c0c362b6STejun Heo 	ata_eh_release(ap);
1590c0c362b6STejun Heo 
15912b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1592c6fd2807SJeff Garzik 
1593c0c362b6STejun Heo 	ata_eh_acquire(ap);
1594c0c362b6STejun Heo 
1595c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1596c6fd2807SJeff Garzik 
1597c6fd2807SJeff Garzik 	if (!rc) {
1598c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1599c6fd2807SJeff Garzik 
1600c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1601c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1602c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1603c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1604c6fd2807SJeff Garzik 		 */
1605c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1606c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1607c6fd2807SJeff Garzik 
1608c6fd2807SJeff Garzik 			ata_port_freeze(ap);
1609c6fd2807SJeff Garzik 
161016169fb7STomas Henzl 			ata_dev_warn(dev, "qc timeout after %u msecs (cmd 0x%x)\n",
161116169fb7STomas Henzl 				     timeout, command);
1612c6fd2807SJeff Garzik 		}
1613c6fd2807SJeff Garzik 
1614c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1615c6fd2807SJeff Garzik 	}
1616c6fd2807SJeff Garzik 
1617c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1618c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1619c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1620c6fd2807SJeff Garzik 
1621a51d644aSTejun Heo 	/* perform minimal error analysis */
162287629312SNiklas Cassel 	if (qc->flags & ATA_QCFLAG_EH) {
1623efcef265SSergey Shtylyov 		if (qc->result_tf.status & (ATA_ERR | ATA_DF))
1624a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1625a51d644aSTejun Heo 
1626a51d644aSTejun Heo 		if (!qc->err_mask)
1627c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1628a51d644aSTejun Heo 
1629a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1630a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
16312dae9955SDamien Le Moal 	} else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1632efcef265SSergey Shtylyov 		qc->result_tf.status |= ATA_SENSE;
1633c6fd2807SJeff Garzik 	}
1634c6fd2807SJeff Garzik 
1635c6fd2807SJeff Garzik 	/* finish up */
1636c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1637c6fd2807SJeff Garzik 
1638c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1639c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1640c6fd2807SJeff Garzik 
1641c6fd2807SJeff Garzik 	ata_qc_free(qc);
16429af5c9c9STejun Heo 	link->active_tag = preempted_tag;
16439af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1644c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1645da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1646c6fd2807SJeff Garzik 
1647c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1648c6fd2807SJeff Garzik 
164987fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
165087fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
165187fbc5a0STejun Heo 
1652c6fd2807SJeff Garzik 	return err_mask;
1653c6fd2807SJeff Garzik }
1654c6fd2807SJeff Garzik 
1655c6fd2807SJeff Garzik /**
165633480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
16572432697bSTejun Heo  *	@dev: Device to which the command is sent
16582432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
16592432697bSTejun Heo  *	@cdb: CDB for packet command
1660e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
16612432697bSTejun Heo  *	@buf: Data buffer of the command
16622432697bSTejun Heo  *	@buflen: Length of data buffer
16632b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
16642432697bSTejun Heo  *
16652432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
16662432697bSTejun Heo  *	buffer instead of sg list.
16672432697bSTejun Heo  *
16682432697bSTejun Heo  *	LOCKING:
16692432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
16702432697bSTejun Heo  *
16712432697bSTejun Heo  *	RETURNS:
16722432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
16732432697bSTejun Heo  */
ata_exec_internal(struct ata_device * dev,struct ata_taskfile * tf,const u8 * cdb,int dma_dir,void * buf,unsigned int buflen,unsigned int timeout)16742432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
16752432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
16762b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
167761176eedSSergey Shtylyov 			   unsigned int timeout)
16782432697bSTejun Heo {
167933480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
168033480a0eSTejun Heo 	unsigned int n_elem = 0;
16812432697bSTejun Heo 
168233480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
168333480a0eSTejun Heo 		WARN_ON(!buf);
16842432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
168533480a0eSTejun Heo 		psg = &sg;
168633480a0eSTejun Heo 		n_elem++;
168733480a0eSTejun Heo 	}
16882432697bSTejun Heo 
16892b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
16902b789108STejun Heo 				    timeout);
16912432697bSTejun Heo }
16922432697bSTejun Heo 
16932432697bSTejun Heo /**
1694c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1695c6fd2807SJeff Garzik  *	@adev: ATA device
1696c6fd2807SJeff Garzik  *
1697c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1698c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1699c6fd2807SJeff Garzik  */
ata_pio_need_iordy(const struct ata_device * adev)1700c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1701c6fd2807SJeff Garzik {
17020d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
17030d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
17040d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
17050d9e6659STejun Heo 	 */
17060d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
17070d9e6659STejun Heo 		return 0;
17080d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
17090d9e6659STejun Heo 	 * check as the caller should know this.
17100d9e6659STejun Heo 	 */
17119af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1712c6fd2807SJeff Garzik 		return 0;
17135c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
17145c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
17155c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
17165c18c4d2SDavid Daney 		return 0;
1717432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1718432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1719c6fd2807SJeff Garzik 		return 1;
1720432729f0SAlan Cox 	/* We turn it on when possible */
1721432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1722432729f0SAlan Cox 		return 1;
1723432729f0SAlan Cox 	return 0;
1724432729f0SAlan Cox }
1725a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
1726c6fd2807SJeff Garzik 
1727432729f0SAlan Cox /**
1728432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1729432729f0SAlan Cox  *	@adev: ATA device
1730432729f0SAlan Cox  *
1731432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1732432729f0SAlan Cox  *	-1 if no iordy mode is available.
1733432729f0SAlan Cox  */
ata_pio_mask_no_iordy(const struct ata_device * adev)1734432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1735432729f0SAlan Cox {
1736c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1737c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1738432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1739c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1740c6fd2807SJeff Garzik 		if (pio) {
1741c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1742c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1743432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1744432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1745c6fd2807SJeff Garzik 		}
1746c6fd2807SJeff Garzik 	}
1747432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1748c6fd2807SJeff Garzik }
1749c6fd2807SJeff Garzik 
1750c6fd2807SJeff Garzik /**
1751963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1752963e4975SAlan Cox  *	@dev: device
1753963e4975SAlan Cox  *	@tf: proposed taskfile
1754963e4975SAlan Cox  *	@id: data buffer
1755963e4975SAlan Cox  *
1756963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1757963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1758963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1759963e4975SAlan Cox  */
ata_do_dev_read_id(struct ata_device * dev,struct ata_taskfile * tf,__le16 * id)1760963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
17610561e514SDamien Le Moal 				struct ata_taskfile *tf, __le16 *id)
1762963e4975SAlan Cox {
1763963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1764963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1765963e4975SAlan Cox }
1766a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
1767963e4975SAlan Cox 
1768963e4975SAlan Cox /**
1769c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1770c6fd2807SJeff Garzik  *	@dev: target device
1771c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1772bff04647STejun Heo  *	@flags: ATA_READID_* flags
1773c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1774c6fd2807SJeff Garzik  *
1775c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1776c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1777c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1778c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1779c6fd2807SJeff Garzik  *
178050a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
178150a99018SAlan Cox  *	now we abort if we hit that case.
178250a99018SAlan Cox  *
1783c6fd2807SJeff Garzik  *	LOCKING:
1784c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1785c6fd2807SJeff Garzik  *
1786c6fd2807SJeff Garzik  *	RETURNS:
1787c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1788c6fd2807SJeff Garzik  */
ata_dev_read_id(struct ata_device * dev,unsigned int * p_class,unsigned int flags,u16 * id)1789c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1790bff04647STejun Heo 		    unsigned int flags, u16 *id)
1791c6fd2807SJeff Garzik {
17929af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1793c6fd2807SJeff Garzik 	unsigned int class = *p_class;
1794c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1795c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
1796c6fd2807SJeff Garzik 	const char *reason;
179779b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
179854936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
1799c6fd2807SJeff Garzik 	int rc;
1800c6fd2807SJeff Garzik 
1801c6fd2807SJeff Garzik retry:
1802c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1803c6fd2807SJeff Garzik 
1804c6fd2807SJeff Garzik 	switch (class) {
180579b42babSTejun Heo 	case ATA_DEV_SEMB:
180679b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
1807df561f66SGustavo A. R. Silva 		fallthrough;
1808c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
18099162c657SHannes Reinecke 	case ATA_DEV_ZAC:
1810c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1811c6fd2807SJeff Garzik 		break;
1812c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1813c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1814c6fd2807SJeff Garzik 		break;
1815c6fd2807SJeff Garzik 	default:
1816c6fd2807SJeff Garzik 		rc = -ENODEV;
1817c6fd2807SJeff Garzik 		reason = "unsupported class";
1818c6fd2807SJeff Garzik 		goto err_out;
1819c6fd2807SJeff Garzik 	}
1820c6fd2807SJeff Garzik 
1821c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
182281afe893STejun Heo 
182381afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
182481afe893STejun Heo 	 * sure those are properly initialized.
182581afe893STejun Heo 	 */
182681afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
182781afe893STejun Heo 
182881afe893STejun Heo 	/* Device presence detection is unreliable on some
182981afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
183081afe893STejun Heo 	 */
183181afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1832c6fd2807SJeff Garzik 
1833963e4975SAlan Cox 	if (ap->ops->read_id)
18340561e514SDamien Le Moal 		err_mask = ap->ops->read_id(dev, &tf, (__le16 *)id);
1835963e4975SAlan Cox 	else
18360561e514SDamien Le Moal 		err_mask = ata_do_dev_read_id(dev, &tf, (__le16 *)id);
1837963e4975SAlan Cox 
1838c6fd2807SJeff Garzik 	if (err_mask) {
1839800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1840a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
184155a8e2c8STejun Heo 			return -ENOENT;
184255a8e2c8STejun Heo 		}
184355a8e2c8STejun Heo 
184479b42babSTejun Heo 		if (is_semb) {
1845a9a79dfeSJoe Perches 			ata_dev_info(dev,
1846a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
184779b42babSTejun Heo 			/* SEMB is not supported yet */
184879b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
184979b42babSTejun Heo 			return 0;
185079b42babSTejun Heo 		}
185179b42babSTejun Heo 
1852efcef265SSergey Shtylyov 		if ((err_mask == AC_ERR_DEV) && (tf.error & ATA_ABORTED)) {
18531ffc151fSTejun Heo 			/* Device or controller might have reported
18541ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
18551ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
18561ffc151fSTejun Heo 			 * aborted by the device.
185754936f8bSTejun Heo 			 */
18581ffc151fSTejun Heo 			if (may_fallback) {
185954936f8bSTejun Heo 				may_fallback = 0;
186054936f8bSTejun Heo 
186154936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
186254936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
186354936f8bSTejun Heo 				else
186454936f8bSTejun Heo 					class = ATA_DEV_ATA;
186554936f8bSTejun Heo 				goto retry;
186654936f8bSTejun Heo 			}
186754936f8bSTejun Heo 
18681ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
18691ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
18701ffc151fSTejun Heo 			 * sometimes with phantom devices.
18711ffc151fSTejun Heo 			 */
1872a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
18731ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
18741ffc151fSTejun Heo 			return -ENOENT;
18751ffc151fSTejun Heo 		}
18761ffc151fSTejun Heo 
1877c6fd2807SJeff Garzik 		rc = -EIO;
1878c6fd2807SJeff Garzik 		reason = "I/O error";
1879c6fd2807SJeff Garzik 		goto err_out;
1880c6fd2807SJeff Garzik 	}
1881c6fd2807SJeff Garzik 
188243c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
18834baa5745SHannes Reinecke 		ata_dev_info(dev, "dumping IDENTIFY data, "
188443c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
188543c9c591STejun Heo 			    class, may_fallback, tried_spinup);
18864baa5745SHannes Reinecke 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
188743c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
188843c9c591STejun Heo 	}
188943c9c591STejun Heo 
189054936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
189154936f8bSTejun Heo 	 * successfully at least once.
189254936f8bSTejun Heo 	 */
189354936f8bSTejun Heo 	may_fallback = 0;
189454936f8bSTejun Heo 
1895c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1896c6fd2807SJeff Garzik 
1897c6fd2807SJeff Garzik 	/* sanity check */
1898c6fd2807SJeff Garzik 	rc = -EINVAL;
18996070068bSAlan Cox 	reason = "device reports invalid type";
19004a3381feSJeff Garzik 
19019162c657SHannes Reinecke 	if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
19024a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
19034a3381feSJeff Garzik 			goto err_out;
1904db63a4c8SAndy Whitcroft 		if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1905db63a4c8SAndy Whitcroft 							ata_id_is_ata(id)) {
1906db63a4c8SAndy Whitcroft 			ata_dev_dbg(dev,
1907db63a4c8SAndy Whitcroft 				"host indicates ignore ATA devices, ignored\n");
1908db63a4c8SAndy Whitcroft 			return -ENOENT;
1909db63a4c8SAndy Whitcroft 		}
19104a3381feSJeff Garzik 	} else {
19114a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1912c6fd2807SJeff Garzik 			goto err_out;
1913c6fd2807SJeff Garzik 	}
1914c6fd2807SJeff Garzik 
1915169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1916169439c2SMark Lord 		tried_spinup = 1;
1917169439c2SMark Lord 		/*
1918169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
1919169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
1920169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
1921169439c2SMark Lord 		 */
1922218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1923fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
1924169439c2SMark Lord 			rc = -EIO;
1925169439c2SMark Lord 			reason = "SPINUP failed";
1926169439c2SMark Lord 			goto err_out;
1927169439c2SMark Lord 		}
1928169439c2SMark Lord 		/*
1929169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
1930169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
1931169439c2SMark Lord 		 */
1932169439c2SMark Lord 		if (id[2] == 0x37c8)
1933169439c2SMark Lord 			goto retry;
1934169439c2SMark Lord 	}
1935169439c2SMark Lord 
19369162c657SHannes Reinecke 	if ((flags & ATA_READID_POSTRESET) &&
19379162c657SHannes Reinecke 	    (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
1938c6fd2807SJeff Garzik 		/*
1939c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
1940c6fd2807SJeff Garzik 		 * SRST RESET
194150a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
194250a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
1943c6fd2807SJeff Garzik 		 * anything else..
1944c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
194550a99018SAlan Cox 		 *
194650a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
1947c9404c9cSAdam Buchbinder 		 * should never trigger.
1948c6fd2807SJeff Garzik 		 */
1949c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1950c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
1951c6fd2807SJeff Garzik 			if (err_mask) {
1952c6fd2807SJeff Garzik 				rc = -EIO;
1953c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
1954c6fd2807SJeff Garzik 				goto err_out;
1955c6fd2807SJeff Garzik 			}
1956c6fd2807SJeff Garzik 
1957c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
1958c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
1959c6fd2807SJeff Garzik 			 */
1960bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
1961c6fd2807SJeff Garzik 			goto retry;
1962c6fd2807SJeff Garzik 		}
1963c6fd2807SJeff Garzik 	}
1964c6fd2807SJeff Garzik 
1965c6fd2807SJeff Garzik 	*p_class = class;
1966c6fd2807SJeff Garzik 
1967c6fd2807SJeff Garzik 	return 0;
1968c6fd2807SJeff Garzik 
1969c6fd2807SJeff Garzik  err_out:
1970a9a79dfeSJoe Perches 	ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1971a9a79dfeSJoe Perches 		     reason, err_mask);
1972c6fd2807SJeff Garzik 	return rc;
1973c6fd2807SJeff Garzik }
1974c6fd2807SJeff Garzik 
1975f01f62c2SChristoph Hellwig /**
1976aa3998dbSDamien Le Moal  *	ata_dev_power_set_standby - Set a device power mode to standby
1977aa3998dbSDamien Le Moal  *	@dev: target device
1978aa3998dbSDamien Le Moal  *
1979aa3998dbSDamien Le Moal  *	Issue a STANDBY IMMEDIATE command to set a device power mode to standby.
1980aa3998dbSDamien Le Moal  *	For an HDD device, this spins down the disks.
1981aa3998dbSDamien Le Moal  *
1982aa3998dbSDamien Le Moal  *	LOCKING:
1983aa3998dbSDamien Le Moal  *	Kernel thread context (may sleep).
1984aa3998dbSDamien Le Moal  */
ata_dev_power_set_standby(struct ata_device * dev)1985aa3998dbSDamien Le Moal void ata_dev_power_set_standby(struct ata_device *dev)
1986aa3998dbSDamien Le Moal {
1987aa3998dbSDamien Le Moal 	unsigned long ap_flags = dev->link->ap->flags;
1988aa3998dbSDamien Le Moal 	struct ata_taskfile tf;
1989aa3998dbSDamien Le Moal 	unsigned int err_mask;
1990aa3998dbSDamien Le Moal 
1991aa3998dbSDamien Le Moal 	/* Issue STANDBY IMMEDIATE command only if supported by the device */
1992aa3998dbSDamien Le Moal 	if (dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC)
1993aa3998dbSDamien Le Moal 		return;
1994aa3998dbSDamien Le Moal 
1995aa3998dbSDamien Le Moal 	/*
1996aa3998dbSDamien Le Moal 	 * Some odd clown BIOSes issue spindown on power off (ACPI S4 or S5)
1997aa3998dbSDamien Le Moal 	 * causing some drives to spin up and down again. For these, do nothing
1998aa3998dbSDamien Le Moal 	 * if we are being called on shutdown.
1999aa3998dbSDamien Le Moal 	 */
2000aa3998dbSDamien Le Moal 	if ((ap_flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
2001aa3998dbSDamien Le Moal 	    system_state == SYSTEM_POWER_OFF)
2002aa3998dbSDamien Le Moal 		return;
2003aa3998dbSDamien Le Moal 
2004aa3998dbSDamien Le Moal 	if ((ap_flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
2005aa3998dbSDamien Le Moal 	    system_entering_hibernation())
2006aa3998dbSDamien Le Moal 		return;
2007aa3998dbSDamien Le Moal 
2008aa3998dbSDamien Le Moal 	ata_tf_init(dev, &tf);
2009aa3998dbSDamien Le Moal 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
2010aa3998dbSDamien Le Moal 	tf.protocol = ATA_PROT_NODATA;
2011aa3998dbSDamien Le Moal 	tf.command = ATA_CMD_STANDBYNOW1;
2012aa3998dbSDamien Le Moal 
2013aa3998dbSDamien Le Moal 	ata_dev_notice(dev, "Entering standby power mode\n");
2014aa3998dbSDamien Le Moal 
2015aa3998dbSDamien Le Moal 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
2016aa3998dbSDamien Le Moal 	if (err_mask)
2017aa3998dbSDamien Le Moal 		ata_dev_err(dev, "STANDBY IMMEDIATE failed (err_mask=0x%x)\n",
2018aa3998dbSDamien Le Moal 			    err_mask);
2019aa3998dbSDamien Le Moal }
2020aa3998dbSDamien Le Moal 
2021aa3998dbSDamien Le Moal /**
2022aa3998dbSDamien Le Moal  *	ata_dev_power_set_active -  Set a device power mode to active
2023aa3998dbSDamien Le Moal  *	@dev: target device
2024aa3998dbSDamien Le Moal  *
2025aa3998dbSDamien Le Moal  *	Issue a VERIFY command to enter to ensure that the device is in the
2026aa3998dbSDamien Le Moal  *	active power mode. For a spun-down HDD (standby or idle power mode),
2027aa3998dbSDamien Le Moal  *	the VERIFY command will complete after the disk spins up.
2028aa3998dbSDamien Le Moal  *
2029aa3998dbSDamien Le Moal  *	LOCKING:
2030aa3998dbSDamien Le Moal  *	Kernel thread context (may sleep).
2031aa3998dbSDamien Le Moal  */
ata_dev_power_set_active(struct ata_device * dev)2032aa3998dbSDamien Le Moal void ata_dev_power_set_active(struct ata_device *dev)
2033aa3998dbSDamien Le Moal {
2034aa3998dbSDamien Le Moal 	struct ata_taskfile tf;
2035aa3998dbSDamien Le Moal 	unsigned int err_mask;
2036aa3998dbSDamien Le Moal 
2037699e2648SDamien Le Moal 	/* If the device is already sleeping, do nothing. */
2038699e2648SDamien Le Moal 	if (dev->flags & ATA_DFLAG_SLEEPING)
2039699e2648SDamien Le Moal 		return;
2040699e2648SDamien Le Moal 
2041aa3998dbSDamien Le Moal 	/*
2042aa3998dbSDamien Le Moal 	 * Issue READ VERIFY SECTORS command for 1 sector at lba=0 only
2043aa3998dbSDamien Le Moal 	 * if supported by the device.
2044aa3998dbSDamien Le Moal 	 */
2045aa3998dbSDamien Le Moal 	if (dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC)
2046aa3998dbSDamien Le Moal 		return;
2047aa3998dbSDamien Le Moal 
2048aa3998dbSDamien Le Moal 	ata_tf_init(dev, &tf);
2049aa3998dbSDamien Le Moal 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
2050aa3998dbSDamien Le Moal 	tf.protocol = ATA_PROT_NODATA;
2051aa3998dbSDamien Le Moal 	tf.command = ATA_CMD_VERIFY;
2052aa3998dbSDamien Le Moal 	tf.nsect = 1;
2053aa3998dbSDamien Le Moal 	if (dev->flags & ATA_DFLAG_LBA) {
2054aa3998dbSDamien Le Moal 		tf.flags |= ATA_TFLAG_LBA;
2055aa3998dbSDamien Le Moal 		tf.device |= ATA_LBA;
2056aa3998dbSDamien Le Moal 	} else {
2057aa3998dbSDamien Le Moal 		/* CHS */
2058aa3998dbSDamien Le Moal 		tf.lbal = 0x1; /* sect */
2059aa3998dbSDamien Le Moal 	}
2060aa3998dbSDamien Le Moal 
2061aa3998dbSDamien Le Moal 	ata_dev_notice(dev, "Entering active power mode\n");
2062aa3998dbSDamien Le Moal 
2063aa3998dbSDamien Le Moal 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
2064aa3998dbSDamien Le Moal 	if (err_mask)
2065aa3998dbSDamien Le Moal 		ata_dev_err(dev, "VERIFY failed (err_mask=0x%x)\n",
2066aa3998dbSDamien Le Moal 			    err_mask);
2067aa3998dbSDamien Le Moal }
2068aa3998dbSDamien Le Moal 
2069aa3998dbSDamien Le Moal /**
2070f01f62c2SChristoph Hellwig  *	ata_read_log_page - read a specific log page
2071f01f62c2SChristoph Hellwig  *	@dev: target device
2072f01f62c2SChristoph Hellwig  *	@log: log to read
2073f01f62c2SChristoph Hellwig  *	@page: page to read
2074f01f62c2SChristoph Hellwig  *	@buf: buffer to store read page
2075f01f62c2SChristoph Hellwig  *	@sectors: number of sectors to read
2076f01f62c2SChristoph Hellwig  *
2077f01f62c2SChristoph Hellwig  *	Read log page using READ_LOG_EXT command.
2078f01f62c2SChristoph Hellwig  *
2079f01f62c2SChristoph Hellwig  *	LOCKING:
2080f01f62c2SChristoph Hellwig  *	Kernel thread context (may sleep).
2081f01f62c2SChristoph Hellwig  *
2082f01f62c2SChristoph Hellwig  *	RETURNS:
2083f01f62c2SChristoph Hellwig  *	0 on success, AC_ERR_* mask otherwise.
2084f01f62c2SChristoph Hellwig  */
ata_read_log_page(struct ata_device * dev,u8 log,u8 page,void * buf,unsigned int sectors)2085f01f62c2SChristoph Hellwig unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
2086f01f62c2SChristoph Hellwig 			       u8 page, void *buf, unsigned int sectors)
2087f01f62c2SChristoph Hellwig {
2088f01f62c2SChristoph Hellwig 	unsigned long ap_flags = dev->link->ap->flags;
2089f01f62c2SChristoph Hellwig 	struct ata_taskfile tf;
2090f01f62c2SChristoph Hellwig 	unsigned int err_mask;
2091f01f62c2SChristoph Hellwig 	bool dma = false;
2092f01f62c2SChristoph Hellwig 
20934633778bSHannes Reinecke 	ata_dev_dbg(dev, "read log page - log 0x%x, page 0x%x\n", log, page);
2094f01f62c2SChristoph Hellwig 
2095f01f62c2SChristoph Hellwig 	/*
2096f01f62c2SChristoph Hellwig 	 * Return error without actually issuing the command on controllers
2097f01f62c2SChristoph Hellwig 	 * which e.g. lockup on a read log page.
2098f01f62c2SChristoph Hellwig 	 */
2099f01f62c2SChristoph Hellwig 	if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2100f01f62c2SChristoph Hellwig 		return AC_ERR_DEV;
2101f01f62c2SChristoph Hellwig 
2102f01f62c2SChristoph Hellwig retry:
2103f01f62c2SChristoph Hellwig 	ata_tf_init(dev, &tf);
2104f971a854SReimar Döffinger 	if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) &&
21057cfdfdc8SDamien Le Moal 	    !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
2106f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2107f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_DMA;
2108f01f62c2SChristoph Hellwig 		dma = true;
2109f01f62c2SChristoph Hellwig 	} else {
2110f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_EXT;
2111f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_PIO;
2112f01f62c2SChristoph Hellwig 		dma = false;
2113f01f62c2SChristoph Hellwig 	}
2114f01f62c2SChristoph Hellwig 	tf.lbal = log;
2115f01f62c2SChristoph Hellwig 	tf.lbam = page;
2116f01f62c2SChristoph Hellwig 	tf.nsect = sectors;
2117f01f62c2SChristoph Hellwig 	tf.hob_nsect = sectors >> 8;
2118f01f62c2SChristoph Hellwig 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2119f01f62c2SChristoph Hellwig 
2120f01f62c2SChristoph Hellwig 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2121f01f62c2SChristoph Hellwig 				     buf, sectors * ATA_SECT_SIZE, 0);
2122f01f62c2SChristoph Hellwig 
2123fc5c8aa7SDamien Le Moal 	if (err_mask) {
2124fc5c8aa7SDamien Le Moal 		if (dma) {
21257cfdfdc8SDamien Le Moal 			dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
21265122e53eSNiklas Cassel 			if (!ata_port_is_frozen(dev->link->ap))
2127f01f62c2SChristoph Hellwig 				goto retry;
2128f01f62c2SChristoph Hellwig 		}
212923ef63d5SDamien Le Moal 		ata_dev_err(dev,
213023ef63d5SDamien Le Moal 			    "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n",
213123ef63d5SDamien Le Moal 			    (unsigned int)log, (unsigned int)page, err_mask);
2132fc5c8aa7SDamien Le Moal 	}
2133f01f62c2SChristoph Hellwig 
2134f01f62c2SChristoph Hellwig 	return err_mask;
2135f01f62c2SChristoph Hellwig }
2136f01f62c2SChristoph Hellwig 
ata_log_supported(struct ata_device * dev,u8 log)2137c745dfc5STyler Erickson static int ata_log_supported(struct ata_device *dev, u8 log)
2138efe205a3SChristoph Hellwig {
2139efe205a3SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2140efe205a3SChristoph Hellwig 
2141ac9f0c81SAnton Lundin 	if (dev->horkage & ATA_HORKAGE_NO_LOG_DIR)
2142c745dfc5STyler Erickson 		return 0;
2143ac9f0c81SAnton Lundin 
2144efe205a3SChristoph Hellwig 	if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2145c745dfc5STyler Erickson 		return 0;
2146c745dfc5STyler Erickson 	return get_unaligned_le16(&ap->sector_buf[log * 2]);
2147efe205a3SChristoph Hellwig }
2148efe205a3SChristoph Hellwig 
ata_identify_page_supported(struct ata_device * dev,u8 page)2149a0fd2454SChristoph Hellwig static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2150a0fd2454SChristoph Hellwig {
2151a0fd2454SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2152a0fd2454SChristoph Hellwig 	unsigned int err, i;
2153a0fd2454SChristoph Hellwig 
2154636f6e2aSDamien Le Moal 	if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG)
2155636f6e2aSDamien Le Moal 		return false;
2156636f6e2aSDamien Le Moal 
2157a0fd2454SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2158636f6e2aSDamien Le Moal 		/*
2159636f6e2aSDamien Le Moal 		 * IDENTIFY DEVICE data log is defined as mandatory starting
2160636f6e2aSDamien Le Moal 		 * with ACS-3 (ATA version 10). Warn about the missing log
2161636f6e2aSDamien Le Moal 		 * for drives which implement this ATA level or above.
2162636f6e2aSDamien Le Moal 		 */
2163636f6e2aSDamien Le Moal 		if (ata_id_major_version(dev->id) >= 10)
2164636f6e2aSDamien Le Moal 			ata_dev_warn(dev,
2165636f6e2aSDamien Le Moal 				"ATA Identify Device Log not supported\n");
2166636f6e2aSDamien Le Moal 		dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG;
2167a0fd2454SChristoph Hellwig 		return false;
2168a0fd2454SChristoph Hellwig 	}
2169a0fd2454SChristoph Hellwig 
2170a0fd2454SChristoph Hellwig 	/*
2171a0fd2454SChristoph Hellwig 	 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2172a0fd2454SChristoph Hellwig 	 * supported.
2173a0fd2454SChristoph Hellwig 	 */
2174a0fd2454SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2175a0fd2454SChristoph Hellwig 				1);
2176fc5c8aa7SDamien Le Moal 	if (err)
2177a0fd2454SChristoph Hellwig 		return false;
2178a0fd2454SChristoph Hellwig 
2179a0fd2454SChristoph Hellwig 	for (i = 0; i < ap->sector_buf[8]; i++) {
2180a0fd2454SChristoph Hellwig 		if (ap->sector_buf[9 + i] == page)
2181a0fd2454SChristoph Hellwig 			return true;
2182a0fd2454SChristoph Hellwig 	}
2183a0fd2454SChristoph Hellwig 
2184a0fd2454SChristoph Hellwig 	return false;
2185a0fd2454SChristoph Hellwig }
2186a0fd2454SChristoph Hellwig 
ata_do_link_spd_horkage(struct ata_device * dev)21879062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
21889062712fSTejun Heo {
21899062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
21909062712fSTejun Heo 	u32 target, target_limit;
21919062712fSTejun Heo 
21929062712fSTejun Heo 	if (!sata_scr_valid(plink))
21939062712fSTejun Heo 		return 0;
21949062712fSTejun Heo 
21959062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
21969062712fSTejun Heo 		target = 1;
21979062712fSTejun Heo 	else
21989062712fSTejun Heo 		return 0;
21999062712fSTejun Heo 
22009062712fSTejun Heo 	target_limit = (1 << target) - 1;
22019062712fSTejun Heo 
22029062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
22039062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
22049062712fSTejun Heo 		return 0;
22059062712fSTejun Heo 
22069062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
22079062712fSTejun Heo 
22089062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
22099062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
22109062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
22119062712fSTejun Heo 	 */
22129062712fSTejun Heo 	if (plink->sata_spd > target) {
2213a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
22149062712fSTejun Heo 			     sata_spd_string(target));
22159062712fSTejun Heo 		return -EAGAIN;
22169062712fSTejun Heo 	}
22179062712fSTejun Heo 	return 0;
22189062712fSTejun Heo }
22199062712fSTejun Heo 
ata_dev_knobble(struct ata_device * dev)2220c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2221c6fd2807SJeff Garzik {
22229af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
22239ce8e307SJens Axboe 
22249ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
22259ce8e307SJens Axboe 		return 0;
22269ce8e307SJens Axboe 
22279af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2228c6fd2807SJeff Garzik }
2229c6fd2807SJeff Garzik 
ata_dev_config_ncq_send_recv(struct ata_device * dev)22305a233551SHannes Reinecke static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
22315a233551SHannes Reinecke {
22325a233551SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
22335a233551SHannes Reinecke 	unsigned int err_mask;
22345a233551SHannes Reinecke 
2235efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2236efe205a3SChristoph Hellwig 		ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
2237fe5af0ccSHannes Reinecke 		return;
2238fe5af0ccSHannes Reinecke 	}
22395a233551SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
22405a233551SHannes Reinecke 				     0, ap->sector_buf, 1);
2241fc5c8aa7SDamien Le Moal 	if (!err_mask) {
22425a233551SHannes Reinecke 		u8 *cmds = dev->ncq_send_recv_cmds;
22435a233551SHannes Reinecke 
22445a233551SHannes Reinecke 		dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
22455a233551SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
22465a233551SHannes Reinecke 
22475a233551SHannes Reinecke 		if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
22485a233551SHannes Reinecke 			ata_dev_dbg(dev, "disabling queued TRIM support\n");
22495a233551SHannes Reinecke 			cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
22505a233551SHannes Reinecke 				~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
22515a233551SHannes Reinecke 		}
22525a233551SHannes Reinecke 	}
22535a233551SHannes Reinecke }
22545a233551SHannes Reinecke 
ata_dev_config_ncq_non_data(struct ata_device * dev)2255284b3b77SHannes Reinecke static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2256284b3b77SHannes Reinecke {
2257284b3b77SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
2258284b3b77SHannes Reinecke 	unsigned int err_mask;
2259284b3b77SHannes Reinecke 
2260efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
2261284b3b77SHannes Reinecke 		ata_dev_warn(dev,
2262284b3b77SHannes Reinecke 			     "NCQ Send/Recv Log not supported\n");
2263284b3b77SHannes Reinecke 		return;
2264284b3b77SHannes Reinecke 	}
2265284b3b77SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2266284b3b77SHannes Reinecke 				     0, ap->sector_buf, 1);
2267fc5c8aa7SDamien Le Moal 	if (!err_mask) {
2268284b3b77SHannes Reinecke 		u8 *cmds = dev->ncq_non_data_cmds;
2269284b3b77SHannes Reinecke 
2270284b3b77SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2271284b3b77SHannes Reinecke 	}
2272284b3b77SHannes Reinecke }
2273284b3b77SHannes Reinecke 
ata_dev_config_ncq_prio(struct ata_device * dev)22748e061784SAdam Manzanares static void ata_dev_config_ncq_prio(struct ata_device *dev)
22758e061784SAdam Manzanares {
22768e061784SAdam Manzanares 	struct ata_port *ap = dev->link->ap;
22778e061784SAdam Manzanares 	unsigned int err_mask;
22788e061784SAdam Manzanares 
227906f6c4c6SDamien Le Moal 	if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
228006f6c4c6SDamien Le Moal 		return;
228106f6c4c6SDamien Le Moal 
22828e061784SAdam Manzanares 	err_mask = ata_read_log_page(dev,
22831d51d5f3SChristoph Hellwig 				     ATA_LOG_IDENTIFY_DEVICE,
22848e061784SAdam Manzanares 				     ATA_LOG_SATA_SETTINGS,
22858e061784SAdam Manzanares 				     ap->sector_buf,
22868e061784SAdam Manzanares 				     1);
2287fc5c8aa7SDamien Le Moal 	if (err_mask)
22882360fa18SDamien Le Moal 		goto not_supported;
22898e061784SAdam Manzanares 
22902360fa18SDamien Le Moal 	if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
22912360fa18SDamien Le Moal 		goto not_supported;
22922360fa18SDamien Le Moal 
22938e061784SAdam Manzanares 	dev->flags |= ATA_DFLAG_NCQ_PRIO;
22948e061784SAdam Manzanares 
22952360fa18SDamien Le Moal 	return;
22962360fa18SDamien Le Moal 
22972360fa18SDamien Le Moal not_supported:
2298e00923c5SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED;
22992360fa18SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
23008e061784SAdam Manzanares }
23018e061784SAdam Manzanares 
ata_dev_check_adapter(struct ata_device * dev,unsigned short vendor_id)23027a8526a5SKate Hsuan static bool ata_dev_check_adapter(struct ata_device *dev,
23037a8526a5SKate Hsuan 				  unsigned short vendor_id)
23047a8526a5SKate Hsuan {
23057a8526a5SKate Hsuan 	struct pci_dev *pcidev = NULL;
23067a8526a5SKate Hsuan 	struct device *parent_dev = NULL;
23077a8526a5SKate Hsuan 
23087a8526a5SKate Hsuan 	for (parent_dev = dev->tdev.parent; parent_dev != NULL;
23097a8526a5SKate Hsuan 	     parent_dev = parent_dev->parent) {
23107a8526a5SKate Hsuan 		if (dev_is_pci(parent_dev)) {
23117a8526a5SKate Hsuan 			pcidev = to_pci_dev(parent_dev);
23127a8526a5SKate Hsuan 			if (pcidev->vendor == vendor_id)
23137a8526a5SKate Hsuan 				return true;
23147a8526a5SKate Hsuan 			break;
23157a8526a5SKate Hsuan 		}
23167a8526a5SKate Hsuan 	}
23177a8526a5SKate Hsuan 
23187a8526a5SKate Hsuan 	return false;
23197a8526a5SKate Hsuan }
23207a8526a5SKate Hsuan 
ata_dev_config_ncq(struct ata_device * dev,char * desc,size_t desc_sz)2321388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2322c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2323c6fd2807SJeff Garzik {
23249af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2325c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2326388539f3SShaohua Li 	unsigned int err_mask;
2327388539f3SShaohua Li 	char *aa_desc = "";
2328c6fd2807SJeff Garzik 
2329c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2330c6fd2807SJeff Garzik 		desc[0] = '\0';
2331388539f3SShaohua Li 		return 0;
2332c6fd2807SJeff Garzik 	}
2333cba97ea1SBartlomiej Zolnierkiewicz 	if (!IS_ENABLED(CONFIG_SATA_HOST))
2334cba97ea1SBartlomiej Zolnierkiewicz 		return 0;
233575683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
23366919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2337388539f3SShaohua Li 		return 0;
23386919a0a6SAlan Cox 	}
23397a8526a5SKate Hsuan 
23407a8526a5SKate Hsuan 	if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI &&
23417a8526a5SKate Hsuan 	    ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) {
23427a8526a5SKate Hsuan 		snprintf(desc, desc_sz, "NCQ (not used)");
23437a8526a5SKate Hsuan 		return 0;
23447a8526a5SKate Hsuan 	}
23457a8526a5SKate Hsuan 
2346c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
234769278f79SJens Axboe 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
2348c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2349c6fd2807SJeff Garzik 	}
2350c6fd2807SJeff Garzik 
2351388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2352388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2353388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2354388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2355388539f3SShaohua Li 			SATA_FPDMA_AA);
2356388539f3SShaohua Li 		if (err_mask) {
2357a9a79dfeSJoe Perches 			ata_dev_err(dev,
2358a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2359a9a79dfeSJoe Perches 				    err_mask);
2360388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2361388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2362388539f3SShaohua Li 				return -EIO;
2363388539f3SShaohua Li 			}
2364388539f3SShaohua Li 		} else
2365388539f3SShaohua Li 			aa_desc = ", AA";
2366388539f3SShaohua Li 	}
2367388539f3SShaohua Li 
2368c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2369388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2370c6fd2807SJeff Garzik 	else
2371388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2372388539f3SShaohua Li 			ddepth, aa_desc);
2373ed36911cSMarc Carino 
2374284b3b77SHannes Reinecke 	if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2375284b3b77SHannes Reinecke 		if (ata_id_has_ncq_send_and_recv(dev->id))
23765a233551SHannes Reinecke 			ata_dev_config_ncq_send_recv(dev);
2377284b3b77SHannes Reinecke 		if (ata_id_has_ncq_non_data(dev->id))
2378284b3b77SHannes Reinecke 			ata_dev_config_ncq_non_data(dev);
23798e061784SAdam Manzanares 		if (ata_id_has_ncq_prio(dev->id))
23808e061784SAdam Manzanares 			ata_dev_config_ncq_prio(dev);
2381ed36911cSMarc Carino 	}
2382ed36911cSMarc Carino 
2383388539f3SShaohua Li 	return 0;
2384c6fd2807SJeff Garzik }
2385c6fd2807SJeff Garzik 
ata_dev_config_sense_reporting(struct ata_device * dev)2386e87fd28cSHannes Reinecke static void ata_dev_config_sense_reporting(struct ata_device *dev)
2387e87fd28cSHannes Reinecke {
2388e87fd28cSHannes Reinecke 	unsigned int err_mask;
2389e87fd28cSHannes Reinecke 
2390e87fd28cSHannes Reinecke 	if (!ata_id_has_sense_reporting(dev->id))
2391e87fd28cSHannes Reinecke 		return;
2392e87fd28cSHannes Reinecke 
2393e87fd28cSHannes Reinecke 	if (ata_id_sense_reporting_enabled(dev->id))
2394e87fd28cSHannes Reinecke 		return;
2395e87fd28cSHannes Reinecke 
2396e87fd28cSHannes Reinecke 	err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2397e87fd28cSHannes Reinecke 	if (err_mask) {
2398e87fd28cSHannes Reinecke 		ata_dev_dbg(dev,
2399e87fd28cSHannes Reinecke 			    "failed to enable Sense Data Reporting, Emask 0x%x\n",
2400e87fd28cSHannes Reinecke 			    err_mask);
2401e87fd28cSHannes Reinecke 	}
2402e87fd28cSHannes Reinecke }
2403e87fd28cSHannes Reinecke 
ata_dev_config_zac(struct ata_device * dev)24046d1003aeSHannes Reinecke static void ata_dev_config_zac(struct ata_device *dev)
24056d1003aeSHannes Reinecke {
24066d1003aeSHannes Reinecke 	struct ata_port *ap = dev->link->ap;
24076d1003aeSHannes Reinecke 	unsigned int err_mask;
24086d1003aeSHannes Reinecke 	u8 *identify_buf = ap->sector_buf;
24096d1003aeSHannes Reinecke 
24106d1003aeSHannes Reinecke 	dev->zac_zones_optimal_open = U32_MAX;
24116d1003aeSHannes Reinecke 	dev->zac_zones_optimal_nonseq = U32_MAX;
24126d1003aeSHannes Reinecke 	dev->zac_zones_max_open = U32_MAX;
24136d1003aeSHannes Reinecke 
24146d1003aeSHannes Reinecke 	/*
24156d1003aeSHannes Reinecke 	 * Always set the 'ZAC' flag for Host-managed devices.
24166d1003aeSHannes Reinecke 	 */
24176d1003aeSHannes Reinecke 	if (dev->class == ATA_DEV_ZAC)
24186d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
24196d1003aeSHannes Reinecke 	else if (ata_id_zoned_cap(dev->id) == 0x01)
24206d1003aeSHannes Reinecke 		/*
24216d1003aeSHannes Reinecke 		 * Check for host-aware devices.
24226d1003aeSHannes Reinecke 		 */
24236d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
24246d1003aeSHannes Reinecke 
24256d1003aeSHannes Reinecke 	if (!(dev->flags & ATA_DFLAG_ZAC))
24266d1003aeSHannes Reinecke 		return;
24276d1003aeSHannes Reinecke 
2428a0fd2454SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
24296d1003aeSHannes Reinecke 		ata_dev_warn(dev,
24306d1003aeSHannes Reinecke 			     "ATA Zoned Information Log not supported\n");
24316d1003aeSHannes Reinecke 		return;
24326d1003aeSHannes Reinecke 	}
24336d1003aeSHannes Reinecke 
24346d1003aeSHannes Reinecke 	/*
24356d1003aeSHannes Reinecke 	 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
24366d1003aeSHannes Reinecke 	 */
24371d51d5f3SChristoph Hellwig 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
24386d1003aeSHannes Reinecke 				     ATA_LOG_ZONED_INFORMATION,
24396d1003aeSHannes Reinecke 				     identify_buf, 1);
24406d1003aeSHannes Reinecke 	if (!err_mask) {
24416d1003aeSHannes Reinecke 		u64 zoned_cap, opt_open, opt_nonseq, max_open;
24426d1003aeSHannes Reinecke 
24436d1003aeSHannes Reinecke 		zoned_cap = get_unaligned_le64(&identify_buf[8]);
24446d1003aeSHannes Reinecke 		if ((zoned_cap >> 63))
24456d1003aeSHannes Reinecke 			dev->zac_zoned_cap = (zoned_cap & 1);
24466d1003aeSHannes Reinecke 		opt_open = get_unaligned_le64(&identify_buf[24]);
24476d1003aeSHannes Reinecke 		if ((opt_open >> 63))
24486d1003aeSHannes Reinecke 			dev->zac_zones_optimal_open = (u32)opt_open;
24496d1003aeSHannes Reinecke 		opt_nonseq = get_unaligned_le64(&identify_buf[32]);
24506d1003aeSHannes Reinecke 		if ((opt_nonseq >> 63))
24516d1003aeSHannes Reinecke 			dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
24526d1003aeSHannes Reinecke 		max_open = get_unaligned_le64(&identify_buf[40]);
24536d1003aeSHannes Reinecke 		if ((max_open >> 63))
24546d1003aeSHannes Reinecke 			dev->zac_zones_max_open = (u32)max_open;
24556d1003aeSHannes Reinecke 	}
24566d1003aeSHannes Reinecke }
24576d1003aeSHannes Reinecke 
ata_dev_config_trusted(struct ata_device * dev)2458818831c8SChristoph Hellwig static void ata_dev_config_trusted(struct ata_device *dev)
2459818831c8SChristoph Hellwig {
2460818831c8SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2461818831c8SChristoph Hellwig 	u64 trusted_cap;
2462818831c8SChristoph Hellwig 	unsigned int err;
2463818831c8SChristoph Hellwig 
2464e8f11db9SChristoph Hellwig 	if (!ata_id_has_trusted(dev->id))
2465e8f11db9SChristoph Hellwig 		return;
2466e8f11db9SChristoph Hellwig 
2467818831c8SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2468818831c8SChristoph Hellwig 		ata_dev_warn(dev,
2469818831c8SChristoph Hellwig 			     "Security Log not supported\n");
2470818831c8SChristoph Hellwig 		return;
2471818831c8SChristoph Hellwig 	}
2472818831c8SChristoph Hellwig 
2473818831c8SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2474818831c8SChristoph Hellwig 			ap->sector_buf, 1);
2475fc5c8aa7SDamien Le Moal 	if (err)
2476818831c8SChristoph Hellwig 		return;
2477818831c8SChristoph Hellwig 
2478818831c8SChristoph Hellwig 	trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2479818831c8SChristoph Hellwig 	if (!(trusted_cap & (1ULL << 63))) {
2480818831c8SChristoph Hellwig 		ata_dev_dbg(dev,
2481818831c8SChristoph Hellwig 			    "Trusted Computing capability qword not valid!\n");
2482818831c8SChristoph Hellwig 		return;
2483818831c8SChristoph Hellwig 	}
2484818831c8SChristoph Hellwig 
2485818831c8SChristoph Hellwig 	if (trusted_cap & (1 << 0))
2486818831c8SChristoph Hellwig 		dev->flags |= ATA_DFLAG_TRUSTED;
2487818831c8SChristoph Hellwig }
2488818831c8SChristoph Hellwig 
ata_dev_config_cdl(struct ata_device * dev)248962e4a60eSDamien Le Moal static void ata_dev_config_cdl(struct ata_device *dev)
249062e4a60eSDamien Le Moal {
249162e4a60eSDamien Le Moal 	struct ata_port *ap = dev->link->ap;
249262e4a60eSDamien Le Moal 	unsigned int err_mask;
2493df60f9c6SDamien Le Moal 	bool cdl_enabled;
249462e4a60eSDamien Le Moal 	u64 val;
249562e4a60eSDamien Le Moal 
249699618f8aSIgor Pylypiv 	if (ata_id_major_version(dev->id) < 11)
249762e4a60eSDamien Le Moal 		goto not_supported;
249862e4a60eSDamien Le Moal 
249962e4a60eSDamien Le Moal 	if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE) ||
2500df60f9c6SDamien Le Moal 	    !ata_identify_page_supported(dev, ATA_LOG_SUPPORTED_CAPABILITIES) ||
2501df60f9c6SDamien Le Moal 	    !ata_identify_page_supported(dev, ATA_LOG_CURRENT_SETTINGS))
250262e4a60eSDamien Le Moal 		goto not_supported;
250362e4a60eSDamien Le Moal 
250462e4a60eSDamien Le Moal 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
250562e4a60eSDamien Le Moal 				     ATA_LOG_SUPPORTED_CAPABILITIES,
250662e4a60eSDamien Le Moal 				     ap->sector_buf, 1);
250762e4a60eSDamien Le Moal 	if (err_mask)
250862e4a60eSDamien Le Moal 		goto not_supported;
250962e4a60eSDamien Le Moal 
251062e4a60eSDamien Le Moal 	/* Check Command Duration Limit Supported bits */
251162e4a60eSDamien Le Moal 	val = get_unaligned_le64(&ap->sector_buf[168]);
251262e4a60eSDamien Le Moal 	if (!(val & BIT_ULL(63)) || !(val & BIT_ULL(0)))
251362e4a60eSDamien Le Moal 		goto not_supported;
251462e4a60eSDamien Le Moal 
251562e4a60eSDamien Le Moal 	/* Warn the user if command duration guideline is not supported */
251662e4a60eSDamien Le Moal 	if (!(val & BIT_ULL(1)))
251762e4a60eSDamien Le Moal 		ata_dev_warn(dev,
251862e4a60eSDamien Le Moal 			"Command duration guideline is not supported\n");
251962e4a60eSDamien Le Moal 
252062e4a60eSDamien Le Moal 	/*
252118bd7718SNiklas Cassel 	 * We must have support for the sense data for successful NCQ commands
252218bd7718SNiklas Cassel 	 * log indicated by the successful NCQ command sense data supported bit.
252318bd7718SNiklas Cassel 	 */
252418bd7718SNiklas Cassel 	val = get_unaligned_le64(&ap->sector_buf[8]);
252518bd7718SNiklas Cassel 	if (!(val & BIT_ULL(63)) || !(val & BIT_ULL(47))) {
252618bd7718SNiklas Cassel 		ata_dev_warn(dev,
252718bd7718SNiklas Cassel 			"CDL supported but Successful NCQ Command Sense Data is not supported\n");
252818bd7718SNiklas Cassel 		goto not_supported;
252918bd7718SNiklas Cassel 	}
253018bd7718SNiklas Cassel 
253118bd7718SNiklas Cassel 	/* Without NCQ autosense, the successful NCQ commands log is useless. */
253218bd7718SNiklas Cassel 	if (!ata_id_has_ncq_autosense(dev->id)) {
253318bd7718SNiklas Cassel 		ata_dev_warn(dev,
253418bd7718SNiklas Cassel 			"CDL supported but NCQ autosense is not supported\n");
253518bd7718SNiklas Cassel 		goto not_supported;
253618bd7718SNiklas Cassel 	}
253718bd7718SNiklas Cassel 
253818bd7718SNiklas Cassel 	/*
2539df60f9c6SDamien Le Moal 	 * If CDL is marked as enabled, make sure the feature is enabled too.
2540df60f9c6SDamien Le Moal 	 * Conversely, if CDL is disabled, make sure the feature is turned off.
2541df60f9c6SDamien Le Moal 	 */
2542df60f9c6SDamien Le Moal 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
2543df60f9c6SDamien Le Moal 				     ATA_LOG_CURRENT_SETTINGS,
2544df60f9c6SDamien Le Moal 				     ap->sector_buf, 1);
2545df60f9c6SDamien Le Moal 	if (err_mask)
2546df60f9c6SDamien Le Moal 		goto not_supported;
2547df60f9c6SDamien Le Moal 
2548df60f9c6SDamien Le Moal 	val = get_unaligned_le64(&ap->sector_buf[8]);
2549df60f9c6SDamien Le Moal 	cdl_enabled = val & BIT_ULL(63) && val & BIT_ULL(21);
2550df60f9c6SDamien Le Moal 	if (dev->flags & ATA_DFLAG_CDL_ENABLED) {
2551df60f9c6SDamien Le Moal 		if (!cdl_enabled) {
2552df60f9c6SDamien Le Moal 			/* Enable CDL on the device */
2553df60f9c6SDamien Le Moal 			err_mask = ata_dev_set_feature(dev, SETFEATURES_CDL, 1);
2554df60f9c6SDamien Le Moal 			if (err_mask) {
2555df60f9c6SDamien Le Moal 				ata_dev_err(dev,
2556df60f9c6SDamien Le Moal 					    "Enable CDL feature failed\n");
2557df60f9c6SDamien Le Moal 				goto not_supported;
2558df60f9c6SDamien Le Moal 			}
2559df60f9c6SDamien Le Moal 		}
2560df60f9c6SDamien Le Moal 	} else {
2561df60f9c6SDamien Le Moal 		if (cdl_enabled) {
2562df60f9c6SDamien Le Moal 			/* Disable CDL on the device */
2563df60f9c6SDamien Le Moal 			err_mask = ata_dev_set_feature(dev, SETFEATURES_CDL, 0);
2564df60f9c6SDamien Le Moal 			if (err_mask) {
2565df60f9c6SDamien Le Moal 				ata_dev_err(dev,
2566df60f9c6SDamien Le Moal 					    "Disable CDL feature failed\n");
2567df60f9c6SDamien Le Moal 				goto not_supported;
2568df60f9c6SDamien Le Moal 			}
2569df60f9c6SDamien Le Moal 		}
2570df60f9c6SDamien Le Moal 	}
2571df60f9c6SDamien Le Moal 
2572df60f9c6SDamien Le Moal 	/*
257318bd7718SNiklas Cassel 	 * While CDL itself has to be enabled using sysfs, CDL requires that
257418bd7718SNiklas Cassel 	 * sense data for successful NCQ commands is enabled to work properly.
257518bd7718SNiklas Cassel 	 * Just like ata_dev_config_sense_reporting(), enable it unconditionally
257618bd7718SNiklas Cassel 	 * if supported.
257718bd7718SNiklas Cassel 	 */
257818bd7718SNiklas Cassel 	if (!(val & BIT_ULL(63)) || !(val & BIT_ULL(18))) {
257918bd7718SNiklas Cassel 		err_mask = ata_dev_set_feature(dev,
258018bd7718SNiklas Cassel 					SETFEATURE_SENSE_DATA_SUCC_NCQ, 0x1);
258118bd7718SNiklas Cassel 		if (err_mask) {
258218bd7718SNiklas Cassel 			ata_dev_warn(dev,
258318bd7718SNiklas Cassel 				     "failed to enable Sense Data for successful NCQ commands, Emask 0x%x\n",
258418bd7718SNiklas Cassel 				     err_mask);
258518bd7718SNiklas Cassel 			goto not_supported;
258618bd7718SNiklas Cassel 		}
258718bd7718SNiklas Cassel 	}
258818bd7718SNiklas Cassel 
258918bd7718SNiklas Cassel 	/*
259018bd7718SNiklas Cassel 	 * Allocate a buffer to handle reading the sense data for successful
259118bd7718SNiklas Cassel 	 * NCQ Commands log page for commands using a CDL with one of the limit
259218bd7718SNiklas Cassel 	 * policy set to 0xD (successful completion with sense data available
259318bd7718SNiklas Cassel 	 * bit set).
259418bd7718SNiklas Cassel 	 */
259518bd7718SNiklas Cassel 	if (!ap->ncq_sense_buf) {
259618bd7718SNiklas Cassel 		ap->ncq_sense_buf = kmalloc(ATA_LOG_SENSE_NCQ_SIZE, GFP_KERNEL);
259718bd7718SNiklas Cassel 		if (!ap->ncq_sense_buf)
259818bd7718SNiklas Cassel 			goto not_supported;
259918bd7718SNiklas Cassel 	}
260018bd7718SNiklas Cassel 
260118bd7718SNiklas Cassel 	/*
260262e4a60eSDamien Le Moal 	 * Command duration limits is supported: cache the CDL log page 18h
260362e4a60eSDamien Le Moal 	 * (command duration descriptors).
260462e4a60eSDamien Le Moal 	 */
260562e4a60eSDamien Le Moal 	err_mask = ata_read_log_page(dev, ATA_LOG_CDL, 0, ap->sector_buf, 1);
260662e4a60eSDamien Le Moal 	if (err_mask) {
260762e4a60eSDamien Le Moal 		ata_dev_warn(dev, "Read Command Duration Limits log failed\n");
260862e4a60eSDamien Le Moal 		goto not_supported;
260962e4a60eSDamien Le Moal 	}
261062e4a60eSDamien Le Moal 
261162e4a60eSDamien Le Moal 	memcpy(dev->cdl, ap->sector_buf, ATA_LOG_CDL_SIZE);
261262e4a60eSDamien Le Moal 	dev->flags |= ATA_DFLAG_CDL;
261362e4a60eSDamien Le Moal 
261462e4a60eSDamien Le Moal 	return;
261562e4a60eSDamien Le Moal 
261662e4a60eSDamien Le Moal not_supported:
2617df60f9c6SDamien Le Moal 	dev->flags &= ~(ATA_DFLAG_CDL | ATA_DFLAG_CDL_ENABLED);
261818bd7718SNiklas Cassel 	kfree(ap->ncq_sense_buf);
261918bd7718SNiklas Cassel 	ap->ncq_sense_buf = NULL;
262062e4a60eSDamien Le Moal }
262162e4a60eSDamien Le Moal 
ata_dev_config_lba(struct ata_device * dev)2622891fd7c6SDamien Le Moal static int ata_dev_config_lba(struct ata_device *dev)
2623891fd7c6SDamien Le Moal {
2624891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2625891fd7c6SDamien Le Moal 	const char *lba_desc;
2626ed518d9bSDamien Le Moal 	char ncq_desc[32];
2627891fd7c6SDamien Le Moal 	int ret;
2628891fd7c6SDamien Le Moal 
2629891fd7c6SDamien Le Moal 	dev->flags |= ATA_DFLAG_LBA;
2630891fd7c6SDamien Le Moal 
2631891fd7c6SDamien Le Moal 	if (ata_id_has_lba48(id)) {
2632891fd7c6SDamien Le Moal 		lba_desc = "LBA48";
2633891fd7c6SDamien Le Moal 		dev->flags |= ATA_DFLAG_LBA48;
2634891fd7c6SDamien Le Moal 		if (dev->n_sectors >= (1UL << 28) &&
2635891fd7c6SDamien Le Moal 		    ata_id_has_flush_ext(id))
2636891fd7c6SDamien Le Moal 			dev->flags |= ATA_DFLAG_FLUSH_EXT;
2637891fd7c6SDamien Le Moal 	} else {
2638891fd7c6SDamien Le Moal 		lba_desc = "LBA";
2639891fd7c6SDamien Le Moal 	}
2640891fd7c6SDamien Le Moal 
2641891fd7c6SDamien Le Moal 	/* config NCQ */
2642891fd7c6SDamien Le Moal 	ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2643891fd7c6SDamien Le Moal 
2644891fd7c6SDamien Le Moal 	/* print device info to dmesg */
26451c95a27cSHannes Reinecke 	if (ata_dev_print_info(dev))
2646891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2647891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u: %s %s\n",
2648891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2649891fd7c6SDamien Le Moal 			     dev->multi_count, lba_desc, ncq_desc);
2650891fd7c6SDamien Le Moal 
2651891fd7c6SDamien Le Moal 	return ret;
2652891fd7c6SDamien Le Moal }
2653891fd7c6SDamien Le Moal 
ata_dev_config_chs(struct ata_device * dev)2654891fd7c6SDamien Le Moal static void ata_dev_config_chs(struct ata_device *dev)
2655891fd7c6SDamien Le Moal {
2656891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2657891fd7c6SDamien Le Moal 
2658891fd7c6SDamien Le Moal 	if (ata_id_current_chs_valid(id)) {
2659891fd7c6SDamien Le Moal 		/* Current CHS translation is valid. */
2660891fd7c6SDamien Le Moal 		dev->cylinders = id[54];
2661891fd7c6SDamien Le Moal 		dev->heads     = id[55];
2662891fd7c6SDamien Le Moal 		dev->sectors   = id[56];
2663891fd7c6SDamien Le Moal 	} else {
2664891fd7c6SDamien Le Moal 		/* Default translation */
2665891fd7c6SDamien Le Moal 		dev->cylinders	= id[1];
2666891fd7c6SDamien Le Moal 		dev->heads	= id[3];
2667891fd7c6SDamien Le Moal 		dev->sectors	= id[6];
2668891fd7c6SDamien Le Moal 	}
2669891fd7c6SDamien Le Moal 
2670891fd7c6SDamien Le Moal 	/* print device info to dmesg */
26711c95a27cSHannes Reinecke 	if (ata_dev_print_info(dev))
2672891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2673891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u, CHS %u/%u/%u\n",
2674891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2675891fd7c6SDamien Le Moal 			     dev->multi_count, dev->cylinders,
2676891fd7c6SDamien Le Moal 			     dev->heads, dev->sectors);
2677891fd7c6SDamien Le Moal }
2678891fd7c6SDamien Le Moal 
ata_dev_config_fua(struct ata_device * dev)26794d2e4980SDamien Le Moal static void ata_dev_config_fua(struct ata_device *dev)
26804d2e4980SDamien Le Moal {
26814d2e4980SDamien Le Moal 	/* Ignore FUA support if its use is disabled globally */
26824d2e4980SDamien Le Moal 	if (!libata_fua)
26834d2e4980SDamien Le Moal 		goto nofua;
26844d2e4980SDamien Le Moal 
26854d2e4980SDamien Le Moal 	/* Ignore devices without support for WRITE DMA FUA EXT */
26864d2e4980SDamien Le Moal 	if (!(dev->flags & ATA_DFLAG_LBA48) || !ata_id_has_fua(dev->id))
26874d2e4980SDamien Le Moal 		goto nofua;
26884d2e4980SDamien Le Moal 
26894d2e4980SDamien Le Moal 	/* Ignore known bad devices and devices that lack NCQ support */
26904d2e4980SDamien Le Moal 	if (!ata_ncq_supported(dev) || (dev->horkage & ATA_HORKAGE_NO_FUA))
26914d2e4980SDamien Le Moal 		goto nofua;
26924d2e4980SDamien Le Moal 
26934d2e4980SDamien Le Moal 	dev->flags |= ATA_DFLAG_FUA;
26944d2e4980SDamien Le Moal 
26954d2e4980SDamien Le Moal 	return;
26964d2e4980SDamien Le Moal 
26974d2e4980SDamien Le Moal nofua:
26984d2e4980SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_FUA;
26994d2e4980SDamien Le Moal }
27004d2e4980SDamien Le Moal 
ata_dev_config_devslp(struct ata_device * dev)2701d8d8778cSDamien Le Moal static void ata_dev_config_devslp(struct ata_device *dev)
2702d8d8778cSDamien Le Moal {
2703d8d8778cSDamien Le Moal 	u8 *sata_setting = dev->link->ap->sector_buf;
2704d8d8778cSDamien Le Moal 	unsigned int err_mask;
2705d8d8778cSDamien Le Moal 	int i, j;
2706d8d8778cSDamien Le Moal 
2707d8d8778cSDamien Le Moal 	/*
2708d8d8778cSDamien Le Moal 	 * Check device sleep capability. Get DevSlp timing variables
2709d8d8778cSDamien Le Moal 	 * from SATA Settings page of Identify Device Data Log.
2710d8d8778cSDamien Le Moal 	 */
271106f6c4c6SDamien Le Moal 	if (!ata_id_has_devslp(dev->id) ||
271206f6c4c6SDamien Le Moal 	    !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
2713d8d8778cSDamien Le Moal 		return;
2714d8d8778cSDamien Le Moal 
2715d8d8778cSDamien Le Moal 	err_mask = ata_read_log_page(dev,
2716d8d8778cSDamien Le Moal 				     ATA_LOG_IDENTIFY_DEVICE,
2717d8d8778cSDamien Le Moal 				     ATA_LOG_SATA_SETTINGS,
2718d8d8778cSDamien Le Moal 				     sata_setting, 1);
2719fc5c8aa7SDamien Le Moal 	if (err_mask)
2720d8d8778cSDamien Le Moal 		return;
2721d8d8778cSDamien Le Moal 
2722d8d8778cSDamien Le Moal 	dev->flags |= ATA_DFLAG_DEVSLP;
2723d8d8778cSDamien Le Moal 	for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2724d8d8778cSDamien Le Moal 		j = ATA_LOG_DEVSLP_OFFSET + i;
2725d8d8778cSDamien Le Moal 		dev->devslp_timing[i] = sata_setting[j];
2726d8d8778cSDamien Le Moal 	}
2727d8d8778cSDamien Le Moal }
2728d8d8778cSDamien Le Moal 
ata_dev_config_cpr(struct ata_device * dev)2729fe22e1c2SDamien Le Moal static void ata_dev_config_cpr(struct ata_device *dev)
2730fe22e1c2SDamien Le Moal {
2731fe22e1c2SDamien Le Moal 	unsigned int err_mask;
2732fe22e1c2SDamien Le Moal 	size_t buf_len;
2733fe22e1c2SDamien Le Moal 	int i, nr_cpr = 0;
2734fe22e1c2SDamien Le Moal 	struct ata_cpr_log *cpr_log = NULL;
2735fe22e1c2SDamien Le Moal 	u8 *desc, *buf = NULL;
2736fe22e1c2SDamien Le Moal 
2737c745dfc5STyler Erickson 	if (ata_id_major_version(dev->id) < 11)
2738c745dfc5STyler Erickson 		goto out;
2739c745dfc5STyler Erickson 
2740c745dfc5STyler Erickson 	buf_len = ata_log_supported(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES);
2741c745dfc5STyler Erickson 	if (buf_len == 0)
2742fe22e1c2SDamien Le Moal 		goto out;
2743fe22e1c2SDamien Le Moal 
2744fe22e1c2SDamien Le Moal 	/*
2745fda17afcSDamien Le Moal 	 * Read the concurrent positioning ranges log (0x47). We can have at
2746c745dfc5STyler Erickson 	 * most 255 32B range descriptors plus a 64B header. This log varies in
2747c745dfc5STyler Erickson 	 * size, so use the size reported in the GPL directory. Reading beyond
2748c745dfc5STyler Erickson 	 * the supported length will result in an error.
2749fe22e1c2SDamien Le Moal 	 */
2750c745dfc5STyler Erickson 	buf_len <<= 9;
2751fe22e1c2SDamien Le Moal 	buf = kzalloc(buf_len, GFP_KERNEL);
2752fe22e1c2SDamien Le Moal 	if (!buf)
2753fe22e1c2SDamien Le Moal 		goto out;
2754fe22e1c2SDamien Le Moal 
2755fda17afcSDamien Le Moal 	err_mask = ata_read_log_page(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES,
2756fda17afcSDamien Le Moal 				     0, buf, buf_len >> 9);
2757fe22e1c2SDamien Le Moal 	if (err_mask)
2758fe22e1c2SDamien Le Moal 		goto out;
2759fe22e1c2SDamien Le Moal 
2760fe22e1c2SDamien Le Moal 	nr_cpr = buf[0];
2761fe22e1c2SDamien Le Moal 	if (!nr_cpr)
2762fe22e1c2SDamien Le Moal 		goto out;
2763fe22e1c2SDamien Le Moal 
2764fe22e1c2SDamien Le Moal 	cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL);
2765fe22e1c2SDamien Le Moal 	if (!cpr_log)
2766fe22e1c2SDamien Le Moal 		goto out;
2767fe22e1c2SDamien Le Moal 
2768fe22e1c2SDamien Le Moal 	cpr_log->nr_cpr = nr_cpr;
2769fe22e1c2SDamien Le Moal 	desc = &buf[64];
2770fe22e1c2SDamien Le Moal 	for (i = 0; i < nr_cpr; i++, desc += 32) {
2771fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num = desc[0];
2772fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num_storage_elements = desc[1];
2773fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]);
2774fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]);
2775fe22e1c2SDamien Le Moal 	}
2776fe22e1c2SDamien Le Moal 
2777fe22e1c2SDamien Le Moal out:
2778fe22e1c2SDamien Le Moal 	swap(dev->cpr_log, cpr_log);
2779fe22e1c2SDamien Le Moal 	kfree(cpr_log);
2780fe22e1c2SDamien Le Moal 	kfree(buf);
2781fe22e1c2SDamien Le Moal }
2782fe22e1c2SDamien Le Moal 
ata_dev_print_features(struct ata_device * dev)2783d633b8a7SDamien Le Moal static void ata_dev_print_features(struct ata_device *dev)
2784d633b8a7SDamien Le Moal {
2785d633b8a7SDamien Le Moal 	if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
2786d633b8a7SDamien Le Moal 		return;
2787d633b8a7SDamien Le Moal 
2788d633b8a7SDamien Le Moal 	ata_dev_info(dev,
278962e4a60eSDamien Le Moal 		     "Features:%s%s%s%s%s%s%s%s\n",
27904d2e4980SDamien Le Moal 		     dev->flags & ATA_DFLAG_FUA ? " FUA" : "",
2791d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
2792d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
2793d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
2794d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
2795fe22e1c2SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "",
279662e4a60eSDamien Le Moal 		     dev->flags & ATA_DFLAG_CDL ? " CDL" : "",
2797fe22e1c2SDamien Le Moal 		     dev->cpr_log ? " CPR" : "");
2798d633b8a7SDamien Le Moal }
2799d633b8a7SDamien Le Moal 
2800c6fd2807SJeff Garzik /**
2801c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2802c6fd2807SJeff Garzik  *	@dev: Target device to configure
2803c6fd2807SJeff Garzik  *
2804c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2805c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2806c6fd2807SJeff Garzik  *
2807c6fd2807SJeff Garzik  *	LOCKING:
2808c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2809c6fd2807SJeff Garzik  *
2810c6fd2807SJeff Garzik  *	RETURNS:
2811c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2812c6fd2807SJeff Garzik  */
ata_dev_configure(struct ata_device * dev)2813efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2814c6fd2807SJeff Garzik {
28159af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2816891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
2817c6fd2807SJeff Garzik 	const u16 *id = dev->id;
2818f0a6d77bSSergey Shtylyov 	unsigned int xfer_mask;
281965fe1f0fSShane Huang 	unsigned int err_mask;
2820b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
28213f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
28223f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2823c6fd2807SJeff Garzik 	int rc;
2824c6fd2807SJeff Garzik 
282596c810f2SHannes Reinecke 	if (!ata_dev_enabled(dev)) {
282696c810f2SHannes Reinecke 		ata_dev_dbg(dev, "no device\n");
2827c6fd2807SJeff Garzik 		return 0;
2828c6fd2807SJeff Garzik 	}
2829c6fd2807SJeff Garzik 
283075683fe7STejun Heo 	/* set horkage */
283175683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
283233267325STejun Heo 	ata_force_horkage(dev);
283375683fe7STejun Heo 
283450af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2835a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
283650af2fa1STejun Heo 		ata_dev_disable(dev);
283750af2fa1STejun Heo 		return 0;
283850af2fa1STejun Heo 	}
283950af2fa1STejun Heo 
28402486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
28412486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2842a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
28432486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
28442486fa56STejun Heo 			     : "disabled");
28452486fa56STejun Heo 		ata_dev_disable(dev);
28462486fa56STejun Heo 		return 0;
28472486fa56STejun Heo 	}
28482486fa56STejun Heo 
28499062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
28509062712fSTejun Heo 	if (rc)
28519062712fSTejun Heo 		return rc;
28529062712fSTejun Heo 
2853ecd75ad5STejun Heo 	/* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2854ecd75ad5STejun Heo 	if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2855ecd75ad5STejun Heo 	    (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2856ecd75ad5STejun Heo 		dev->horkage |= ATA_HORKAGE_NOLPM;
2857ecd75ad5STejun Heo 
2858240630e6SHans de Goede 	if (ap->flags & ATA_FLAG_NO_LPM)
2859240630e6SHans de Goede 		dev->horkage |= ATA_HORKAGE_NOLPM;
2860240630e6SHans de Goede 
2861ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2862ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2863ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2864ecd75ad5STejun Heo 	}
2865ecd75ad5STejun Heo 
28666746544cSTejun Heo 	/* let ACPI work its magic */
28676746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
28686746544cSTejun Heo 	if (rc)
28696746544cSTejun Heo 		return rc;
287008573a86SKristen Carlson Accardi 
287105027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
287205027adcSTejun Heo 	rc = ata_hpa_resize(dev);
287305027adcSTejun Heo 	if (rc)
287405027adcSTejun Heo 		return rc;
287505027adcSTejun Heo 
2876c6fd2807SJeff Garzik 	/* print device capabilities */
2877a9a79dfeSJoe Perches 	ata_dev_dbg(dev,
2878c6fd2807SJeff Garzik 		    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2879c6fd2807SJeff Garzik 		    "85:%04x 86:%04x 87:%04x 88:%04x\n",
28807f5e4e8dSHarvey Harrison 		    __func__,
2881c6fd2807SJeff Garzik 		    id[49], id[82], id[83], id[84],
2882c6fd2807SJeff Garzik 		    id[85], id[86], id[87], id[88]);
2883c6fd2807SJeff Garzik 
2884c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2885c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2886c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2887c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2888c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2889c6fd2807SJeff Garzik 	dev->cylinders = 0;
2890c6fd2807SJeff Garzik 	dev->heads = 0;
2891c6fd2807SJeff Garzik 	dev->sectors = 0;
2892e18086d6SMark Lord 	dev->multi_count = 0;
2893c6fd2807SJeff Garzik 
2894c6fd2807SJeff Garzik 	/*
2895c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2896c6fd2807SJeff Garzik 	 */
2897c6fd2807SJeff Garzik 
2898c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2899c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2900c6fd2807SJeff Garzik 
29016044f3c4SHannes Reinecke 	ata_dump_id(dev, id);
2902c6fd2807SJeff Garzik 
2903ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2904ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2905ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2906ef143d57SAlbert Lee 
2907ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2908ef143d57SAlbert Lee 			sizeof(modelbuf));
2909ef143d57SAlbert Lee 
2910c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
29119162c657SHannes Reinecke 	if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2912b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
291362afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
291462afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2915a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2916a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2917b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2918ae8d4ee7SAlan Cox 		} else {
2919b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2920ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2921ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2922a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2923a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2924ae8d4ee7SAlan Cox 		}
2925b352e57dSAlan Cox 
2926c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2927c6fd2807SJeff Garzik 
2928e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2929e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2930e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2931e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2932e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2933e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2934e18086d6SMark Lord 				if (cnt <= max)
2935e18086d6SMark Lord 					dev->multi_count = cnt;
2936e18086d6SMark Lord 		}
29373f64f565SEric D. Mudama 
2938891fd7c6SDamien Le Moal 		/* print device info to dmesg */
29391c95a27cSHannes Reinecke 		if (print_info)
2940891fd7c6SDamien Le Moal 			ata_dev_info(dev, "%s: %s, %s, max %s\n",
2941891fd7c6SDamien Le Moal 				     revbuf, modelbuf, fwrevbuf,
2942891fd7c6SDamien Le Moal 				     ata_mode_string(xfer_mask));
2943891fd7c6SDamien Le Moal 
2944c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2945891fd7c6SDamien Le Moal 			rc = ata_dev_config_lba(dev);
2946388539f3SShaohua Li 			if (rc)
2947388539f3SShaohua Li 				return rc;
2948c6fd2807SJeff Garzik 		} else {
2949891fd7c6SDamien Le Moal 			ata_dev_config_chs(dev);
2950c6fd2807SJeff Garzik 		}
2951c6fd2807SJeff Garzik 
29524d2e4980SDamien Le Moal 		ata_dev_config_fua(dev);
2953d8d8778cSDamien Le Moal 		ata_dev_config_devslp(dev);
2954e87fd28cSHannes Reinecke 		ata_dev_config_sense_reporting(dev);
29556d1003aeSHannes Reinecke 		ata_dev_config_zac(dev);
2956818831c8SChristoph Hellwig 		ata_dev_config_trusted(dev);
2957fe22e1c2SDamien Le Moal 		ata_dev_config_cpr(dev);
295862e4a60eSDamien Le Moal 		ata_dev_config_cdl(dev);
2959b1ffbf85SMinwoo Im 		dev->cdb_len = 32;
2960d633b8a7SDamien Le Moal 
29611c95a27cSHannes Reinecke 		if (print_info)
2962d633b8a7SDamien Le Moal 			ata_dev_print_features(dev);
2963c6fd2807SJeff Garzik 	}
2964c6fd2807SJeff Garzik 
2965c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2966c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2967854c73a2STejun Heo 		const char *cdb_intr_string = "";
2968854c73a2STejun Heo 		const char *atapi_an_string = "";
296991163006STejun Heo 		const char *dma_dir_string = "";
29707d77b247STejun Heo 		u32 sntf;
2971c6fd2807SJeff Garzik 
2972c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2973c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
297416d42467SHannes Reinecke 			ata_dev_warn(dev, "unsupported CDB len %d\n", rc);
2975c6fd2807SJeff Garzik 			rc = -EINVAL;
2976c6fd2807SJeff Garzik 			goto err_out_nosup;
2977c6fd2807SJeff Garzik 		}
2978c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2979c6fd2807SJeff Garzik 
29807d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
29817d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
29827d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
29837d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
29849f45cbd3SKristen Carlson Accardi 		 */
2985e7ecd435STejun Heo 		if (atapi_an &&
2986e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2987071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
29887d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
29899f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2990218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2991218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2992854c73a2STejun Heo 			if (err_mask)
2993a9a79dfeSJoe Perches 				ata_dev_err(dev,
2994a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2995a9a79dfeSJoe Perches 					    err_mask);
2996854c73a2STejun Heo 			else {
29979f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2998854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2999854c73a2STejun Heo 			}
30009f45cbd3SKristen Carlson Accardi 		}
30019f45cbd3SKristen Carlson Accardi 
3002c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
3003c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
3004c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
3005c6fd2807SJeff Garzik 		}
3006c6fd2807SJeff Garzik 
3007966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
300891163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
300991163006STejun Heo 			dma_dir_string = ", DMADIR";
301091163006STejun Heo 		}
301191163006STejun Heo 
3012afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
3013b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
3014afe75951SAaron Lu 			zpodd_init(dev);
3015afe75951SAaron Lu 		}
3016b1354cbbSLin Ming 
3017c6fd2807SJeff Garzik 		/* print device info to dmesg */
30181c95a27cSHannes Reinecke 		if (print_info)
3019a9a79dfeSJoe Perches 			ata_dev_info(dev,
302091163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
3021ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
3022c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
302391163006STejun Heo 				     cdb_intr_string, atapi_an_string,
302491163006STejun Heo 				     dma_dir_string);
3025c6fd2807SJeff Garzik 	}
3026c6fd2807SJeff Garzik 
3027914ed354STejun Heo 	/* determine max_sectors */
3028914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
3029914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
3030914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
3031914ed354STejun Heo 
3032c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
3033c5038fc0SAlan Cox 	   200 sectors */
3034c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
30351c95a27cSHannes Reinecke 		if (print_info)
3036a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
3037c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
3038c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
3039c6fd2807SJeff Garzik 	}
3040c6fd2807SJeff Garzik 
3041f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
3042f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
3043f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
3044f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
3045f442cd86SAlbert Lee 	}
3046f8d8e579STony Battersby 
304775683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
304803ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
304903ec52deSTejun Heo 					 dev->max_sectors);
305018d6e9d5SAlbert Lee 
3051af34d637SDavid Milburn 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
3052af34d637SDavid Milburn 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
3053af34d637SDavid Milburn 					 dev->max_sectors);
3054af34d637SDavid Milburn 
3055a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
3056a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
3057a32450e1SShan Hai 
3058c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
3059cd0d3bbcSAlan 		ap->ops->dev_config(dev);
3060c6fd2807SJeff Garzik 
3061c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
3062c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
3063c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
3064c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
3065c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
3066c5038fc0SAlan Cox 		   bugs */
3067c5038fc0SAlan Cox 
3068c5038fc0SAlan Cox 		if (print_info) {
3069a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3070c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
3071a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3072c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
3073c5038fc0SAlan Cox 		}
3074c5038fc0SAlan Cox 	}
3075c5038fc0SAlan Cox 
3076ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
3077a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
3078a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
3079ac70a964STejun Heo 	}
3080ac70a964STejun Heo 
3081c6fd2807SJeff Garzik 	return 0;
3082c6fd2807SJeff Garzik 
3083c6fd2807SJeff Garzik err_out_nosup:
3084c6fd2807SJeff Garzik 	return rc;
3085c6fd2807SJeff Garzik }
3086c6fd2807SJeff Garzik 
3087c6fd2807SJeff Garzik /**
30882e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
3089be0d18dfSAlan Cox  *	@ap: port
3090be0d18dfSAlan Cox  *
30912e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
3092be0d18dfSAlan Cox  *	detection.
3093be0d18dfSAlan Cox  */
3094be0d18dfSAlan Cox 
ata_cable_40wire(struct ata_port * ap)3095be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
3096be0d18dfSAlan Cox {
3097be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
3098be0d18dfSAlan Cox }
3099a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_40wire);
3100be0d18dfSAlan Cox 
3101be0d18dfSAlan Cox /**
31022e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
3103be0d18dfSAlan Cox  *	@ap: port
3104be0d18dfSAlan Cox  *
31052e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
3106be0d18dfSAlan Cox  *	detection.
3107be0d18dfSAlan Cox  */
3108be0d18dfSAlan Cox 
ata_cable_80wire(struct ata_port * ap)3109be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
3110be0d18dfSAlan Cox {
3111be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
3112be0d18dfSAlan Cox }
3113a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_80wire);
3114be0d18dfSAlan Cox 
3115be0d18dfSAlan Cox /**
3116be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
3117be0d18dfSAlan Cox  *	@ap: port
3118be0d18dfSAlan Cox  *
3119be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
3120be0d18dfSAlan Cox  */
3121be0d18dfSAlan Cox 
ata_cable_unknown(struct ata_port * ap)3122be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
3123be0d18dfSAlan Cox {
3124be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
3125be0d18dfSAlan Cox }
3126a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_unknown);
3127be0d18dfSAlan Cox 
3128be0d18dfSAlan Cox /**
3129c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
3130c88f90c3STejun Heo  *	@ap: port
3131c88f90c3STejun Heo  *
3132c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
3133c88f90c3STejun Heo  *	transfer mode.
3134c88f90c3STejun Heo  */
ata_cable_ignore(struct ata_port * ap)3135c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
3136c88f90c3STejun Heo {
3137c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
3138c88f90c3STejun Heo }
3139a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_ignore);
3140c88f90c3STejun Heo 
3141c88f90c3STejun Heo /**
3142be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
3143be0d18dfSAlan Cox  *	@ap: port
3144be0d18dfSAlan Cox  *
3145be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
3146be0d18dfSAlan Cox  */
3147be0d18dfSAlan Cox 
ata_cable_sata(struct ata_port * ap)3148be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
3149be0d18dfSAlan Cox {
3150be0d18dfSAlan Cox 	return ATA_CBL_SATA;
3151be0d18dfSAlan Cox }
3152a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_sata);
3153be0d18dfSAlan Cox 
3154be0d18dfSAlan Cox /**
3155c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
3156936fd732STejun Heo  *	@link: SATA link to printk link status about
3157c6fd2807SJeff Garzik  *
3158c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
3159c6fd2807SJeff Garzik  *
3160c6fd2807SJeff Garzik  *	LOCKING:
3161c6fd2807SJeff Garzik  *	None.
3162c6fd2807SJeff Garzik  */
sata_print_link_status(struct ata_link * link)31636bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
3164c6fd2807SJeff Garzik {
3165c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
3166c6fd2807SJeff Garzik 
3167936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
3168c6fd2807SJeff Garzik 		return;
316955d5ba55SLi Zhong 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
317055d5ba55SLi Zhong 		return;
3171c6fd2807SJeff Garzik 
3172b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
3173c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
3174a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3175c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
3176c6fd2807SJeff Garzik 	} else {
3177a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3178c6fd2807SJeff Garzik 			      sstatus, scontrol);
3179c6fd2807SJeff Garzik 	}
3180c6fd2807SJeff Garzik }
3181c6fd2807SJeff Garzik 
3182c6fd2807SJeff Garzik /**
3183c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
3184c6fd2807SJeff Garzik  *	@adev: device
3185c6fd2807SJeff Garzik  *
3186c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
3187c6fd2807SJeff Garzik  *	present NULL is returned
3188c6fd2807SJeff Garzik  */
3189c6fd2807SJeff Garzik 
ata_dev_pair(struct ata_device * adev)3190c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
3191c6fd2807SJeff Garzik {
31929af5c9c9STejun Heo 	struct ata_link *link = adev->link;
31939af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
3194c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
3195c6fd2807SJeff Garzik 		return NULL;
3196c6fd2807SJeff Garzik 	return pair;
3197c6fd2807SJeff Garzik }
3198a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_pair);
3199c6fd2807SJeff Garzik 
3200c6fd2807SJeff Garzik /**
3201c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
3202936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
3203a07d499bSTejun Heo  *	@spd_limit: Additional limit
3204c6fd2807SJeff Garzik  *
3205936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
3206c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
3207c6fd2807SJeff Garzik  *	using sata_set_spd().
3208c6fd2807SJeff Garzik  *
3209a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
3210a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
3211a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
3212a07d499bSTejun Heo  *	supported speed is allowed.
3213a07d499bSTejun Heo  *
3214c6fd2807SJeff Garzik  *	LOCKING:
3215c6fd2807SJeff Garzik  *	Inherited from caller.
3216c6fd2807SJeff Garzik  *
3217c6fd2807SJeff Garzik  *	RETURNS:
3218c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3219c6fd2807SJeff Garzik  */
sata_down_spd_limit(struct ata_link * link,u32 spd_limit)3220a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3221c6fd2807SJeff Garzik {
3222c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
3223a07d499bSTejun Heo 	int rc, bit;
3224c6fd2807SJeff Garzik 
3225936fd732STejun Heo 	if (!sata_scr_valid(link))
3226008a7896STejun Heo 		return -EOPNOTSUPP;
3227008a7896STejun Heo 
3228008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
3229936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
3230008a7896STejun Heo 	 */
3231936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
32329913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
3233008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
3234008a7896STejun Heo 	else
3235936fd732STejun Heo 		spd = link->sata_spd;
3236c6fd2807SJeff Garzik 
3237936fd732STejun Heo 	mask = link->sata_spd_limit;
3238c6fd2807SJeff Garzik 	if (mask <= 1)
3239c6fd2807SJeff Garzik 		return -EINVAL;
3240008a7896STejun Heo 
3241008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3242a07d499bSTejun Heo 	bit = fls(mask) - 1;
3243a07d499bSTejun Heo 	mask &= ~(1 << bit);
3244c6fd2807SJeff Garzik 
32452dc0b46bSDavid Milburn 	/*
32462dc0b46bSDavid Milburn 	 * Mask off all speeds higher than or equal to the current one.  At
32472dc0b46bSDavid Milburn 	 * this point, if current SPD is not available and we previously
32482dc0b46bSDavid Milburn 	 * recorded the link speed from SStatus, the driver has already
32492dc0b46bSDavid Milburn 	 * masked off the highest bit so mask should already be 1 or 0.
32502dc0b46bSDavid Milburn 	 * Otherwise, we should not force 1.5Gbps on a link where we have
32512dc0b46bSDavid Milburn 	 * not previously recorded speed from SStatus.  Just return in this
32522dc0b46bSDavid Milburn 	 * case.
3253008a7896STejun Heo 	 */
3254008a7896STejun Heo 	if (spd > 1)
3255008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
325669f2c934SDamien Le Moal 	else if (link->sata_spd)
32572dc0b46bSDavid Milburn 		return -EINVAL;
3258008a7896STejun Heo 
3259008a7896STejun Heo 	/* were we already at the bottom? */
3260c6fd2807SJeff Garzik 	if (!mask)
3261c6fd2807SJeff Garzik 		return -EINVAL;
3262c6fd2807SJeff Garzik 
3263a07d499bSTejun Heo 	if (spd_limit) {
3264a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3265a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3266a07d499bSTejun Heo 		else {
3267a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3268a07d499bSTejun Heo 			mask = 1 << bit;
3269a07d499bSTejun Heo 		}
3270a07d499bSTejun Heo 	}
3271a07d499bSTejun Heo 
3272936fd732STejun Heo 	link->sata_spd_limit = mask;
3273c6fd2807SJeff Garzik 
3274a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
3275c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
3276c6fd2807SJeff Garzik 
3277c6fd2807SJeff Garzik 	return 0;
3278c6fd2807SJeff Garzik }
3279c6fd2807SJeff Garzik 
3280a9b2c120SBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_ACPI
3281c6fd2807SJeff Garzik /**
3282a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3283a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3284a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3285a0f79b92STejun Heo  *
3286a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3287a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3288a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3289a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3290a0f79b92STejun Heo  *
3291a0f79b92STejun Heo  *	LOCKING:
3292a0f79b92STejun Heo  *	None.
3293a0f79b92STejun Heo  *
3294a0f79b92STejun Heo  *	RETURNS:
3295a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3296a0f79b92STejun Heo  */
ata_timing_cycle2mode(unsigned int xfer_shift,int cycle)3297a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3298a0f79b92STejun Heo {
3299a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3300a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3301a0f79b92STejun Heo 	const struct ata_timing *t;
3302a0f79b92STejun Heo 
3303a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3304a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3305a0f79b92STejun Heo 			base_mode = ent->base;
3306a0f79b92STejun Heo 
3307a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3308a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3309a0f79b92STejun Heo 		unsigned short this_cycle;
3310a0f79b92STejun Heo 
3311a0f79b92STejun Heo 		switch (xfer_shift) {
3312a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3313a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3314a0f79b92STejun Heo 			this_cycle = t->cycle;
3315a0f79b92STejun Heo 			break;
3316a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3317a0f79b92STejun Heo 			this_cycle = t->udma;
3318a0f79b92STejun Heo 			break;
3319a0f79b92STejun Heo 		default:
3320a0f79b92STejun Heo 			return 0xff;
3321a0f79b92STejun Heo 		}
3322a0f79b92STejun Heo 
3323a0f79b92STejun Heo 		if (cycle > this_cycle)
3324a0f79b92STejun Heo 			break;
3325a0f79b92STejun Heo 
3326a0f79b92STejun Heo 		last_mode = t->mode;
3327a0f79b92STejun Heo 	}
3328a0f79b92STejun Heo 
3329a0f79b92STejun Heo 	return last_mode;
3330a0f79b92STejun Heo }
3331a9b2c120SBartlomiej Zolnierkiewicz #endif
3332a0f79b92STejun Heo 
3333a0f79b92STejun Heo /**
3334c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3335c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3336458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3337c6fd2807SJeff Garzik  *
3338c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3339c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3340c6fd2807SJeff Garzik  *	will apply the limit.
3341c6fd2807SJeff Garzik  *
3342c6fd2807SJeff Garzik  *	LOCKING:
3343c6fd2807SJeff Garzik  *	Inherited from caller.
3344c6fd2807SJeff Garzik  *
3345c6fd2807SJeff Garzik  *	RETURNS:
3346c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3347c6fd2807SJeff Garzik  */
ata_down_xfermask_limit(struct ata_device * dev,unsigned int sel)3348458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3349c6fd2807SJeff Garzik {
3350458337dbSTejun Heo 	char buf[32];
3351f0a6d77bSSergey Shtylyov 	unsigned int orig_mask, xfer_mask;
3352f0a6d77bSSergey Shtylyov 	unsigned int pio_mask, mwdma_mask, udma_mask;
3353458337dbSTejun Heo 	int quiet, highbit;
3354c6fd2807SJeff Garzik 
3355458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3356458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3357458337dbSTejun Heo 
3358458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3359458337dbSTejun Heo 						  dev->mwdma_mask,
3360c6fd2807SJeff Garzik 						  dev->udma_mask);
3361458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3362c6fd2807SJeff Garzik 
3363458337dbSTejun Heo 	switch (sel) {
3364458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3365458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3366458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3367458337dbSTejun Heo 		break;
3368458337dbSTejun Heo 
3369458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3370458337dbSTejun Heo 		if (udma_mask) {
3371458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3372458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3373458337dbSTejun Heo 			if (!udma_mask)
3374458337dbSTejun Heo 				return -ENOENT;
3375458337dbSTejun Heo 		} else if (mwdma_mask) {
3376458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3377458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3378458337dbSTejun Heo 			if (!mwdma_mask)
3379458337dbSTejun Heo 				return -ENOENT;
3380458337dbSTejun Heo 		}
3381458337dbSTejun Heo 		break;
3382458337dbSTejun Heo 
3383458337dbSTejun Heo 	case ATA_DNXFER_40C:
3384458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3385458337dbSTejun Heo 		break;
3386458337dbSTejun Heo 
3387458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3388458337dbSTejun Heo 		pio_mask &= 1;
3389df561f66SGustavo A. R. Silva 		fallthrough;
3390458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3391458337dbSTejun Heo 		mwdma_mask = 0;
3392458337dbSTejun Heo 		udma_mask = 0;
3393458337dbSTejun Heo 		break;
3394458337dbSTejun Heo 
3395458337dbSTejun Heo 	default:
3396458337dbSTejun Heo 		BUG();
3397458337dbSTejun Heo 	}
3398458337dbSTejun Heo 
3399458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3400458337dbSTejun Heo 
3401458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3402458337dbSTejun Heo 		return -ENOENT;
3403458337dbSTejun Heo 
3404458337dbSTejun Heo 	if (!quiet) {
3405458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3406458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3407458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3408458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3409458337dbSTejun Heo 		else
3410458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3411458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3412458337dbSTejun Heo 
3413a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3414458337dbSTejun Heo 	}
3415c6fd2807SJeff Garzik 
3416c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3417c6fd2807SJeff Garzik 			    &dev->udma_mask);
3418c6fd2807SJeff Garzik 
3419c6fd2807SJeff Garzik 	return 0;
3420c6fd2807SJeff Garzik }
3421c6fd2807SJeff Garzik 
ata_dev_set_mode(struct ata_device * dev)3422c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3423c6fd2807SJeff Garzik {
3424d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
34259af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3426d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
34274055dee7STejun Heo 	const char *dev_err_whine = "";
34284055dee7STejun Heo 	int ign_dev_err = 0;
3429d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3430c6fd2807SJeff Garzik 	int rc;
3431c6fd2807SJeff Garzik 
3432c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3433c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3434c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3435c6fd2807SJeff Garzik 
3436d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3437d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3438d0cb43b3STejun Heo 	else {
3439d0cb43b3STejun Heo 		if (nosetxfer)
3440a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3441d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3442d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3443c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3444d0cb43b3STejun Heo 	}
34452dcb407eSJeff Garzik 
34464055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
34474055dee7STejun Heo 		goto fail;
34482dcb407eSJeff Garzik 
34494055dee7STejun Heo 	/* revalidate */
3450baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3451422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3452baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3453c6fd2807SJeff Garzik 	if (rc)
3454c6fd2807SJeff Garzik 		return rc;
3455c6fd2807SJeff Garzik 
3456b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
34574055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3458b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
34594055dee7STejun Heo 			ign_dev_err = 1;
3460b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3461b93fda12SAlan Cox 		   ATA devices */
3462b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
34634055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
34644055dee7STejun Heo 			ign_dev_err = 1;
3465b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3466b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3467b93fda12SAlan Cox 		   timings and no IORDY */
3468b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3469b93fda12SAlan Cox 			ign_dev_err = 1;
3470b93fda12SAlan Cox 	}
34714055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
34724055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
34734055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
34744055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
34754055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
34764055dee7STejun Heo 		ign_dev_err = 1;
34774055dee7STejun Heo 
34784055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
34794055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
34804055dee7STejun Heo 		ign_dev_err = 1;
34814055dee7STejun Heo 
34824055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
34834055dee7STejun Heo 		if (!ign_dev_err)
34844055dee7STejun Heo 			goto fail;
34854055dee7STejun Heo 		else
34864055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
34874055dee7STejun Heo 	}
34884055dee7STejun Heo 
34894633778bSHannes Reinecke 	ata_dev_dbg(dev, "xfer_shift=%u, xfer_mode=0x%x\n",
3490c6fd2807SJeff Garzik 		    dev->xfer_shift, (int)dev->xfer_mode);
3491c6fd2807SJeff Garzik 
349207b9b6d6SDamien Le Moal 	if (!(ehc->i.flags & ATA_EHI_QUIET) ||
349307b9b6d6SDamien Le Moal 	    ehc->i.flags & ATA_EHI_DID_HARDRESET)
3494a9a79dfeSJoe Perches 		ata_dev_info(dev, "configured for %s%s\n",
34954055dee7STejun Heo 			     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
34964055dee7STejun Heo 			     dev_err_whine);
34974055dee7STejun Heo 
3498c6fd2807SJeff Garzik 	return 0;
34994055dee7STejun Heo 
35004055dee7STejun Heo  fail:
3501a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
35024055dee7STejun Heo 	return -EIO;
3503c6fd2807SJeff Garzik }
3504c6fd2807SJeff Garzik 
3505c6fd2807SJeff Garzik /**
350604351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
35070260731fSTejun Heo  *	@link: link on which timings will be programmed
35081967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3509c6fd2807SJeff Garzik  *
351004351821SAlan  *	Standard implementation of the function used to tune and set
351104351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
351204351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3513c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3514c6fd2807SJeff Garzik  *
3515c6fd2807SJeff Garzik  *	LOCKING:
3516c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3517c6fd2807SJeff Garzik  *
3518c6fd2807SJeff Garzik  *	RETURNS:
3519c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3520c6fd2807SJeff Garzik  */
352104351821SAlan 
ata_do_set_mode(struct ata_link * link,struct ata_device ** r_failed_dev)35220260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3523c6fd2807SJeff Garzik {
35240260731fSTejun Heo 	struct ata_port *ap = link->ap;
3525c6fd2807SJeff Garzik 	struct ata_device *dev;
3526f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3527c6fd2807SJeff Garzik 
3528c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
35291eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3530f0a6d77bSSergey Shtylyov 		unsigned int pio_mask, dma_mask;
3531b3a70601SAlan Cox 		unsigned int mode_mask;
3532c6fd2807SJeff Garzik 
3533b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3534b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3535b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3536b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3537b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3538b3a70601SAlan Cox 
3539c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
354033267325STejun Heo 		ata_force_xfermask(dev);
3541c6fd2807SJeff Garzik 
3542c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3543b3a70601SAlan Cox 
3544b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
354580a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
354680a9c430SSergei Shtylyov 						     dev->udma_mask);
3547b3a70601SAlan Cox 		else
3548b3a70601SAlan Cox 			dma_mask = 0;
3549b3a70601SAlan Cox 
3550c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3551c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3552c6fd2807SJeff Garzik 
3553c6fd2807SJeff Garzik 		found = 1;
3554b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3555c6fd2807SJeff Garzik 			used_dma = 1;
3556c6fd2807SJeff Garzik 	}
3557c6fd2807SJeff Garzik 	if (!found)
3558c6fd2807SJeff Garzik 		goto out;
3559c6fd2807SJeff Garzik 
3560c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
35611eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
356270cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3563a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3564c6fd2807SJeff Garzik 			rc = -EINVAL;
3565c6fd2807SJeff Garzik 			goto out;
3566c6fd2807SJeff Garzik 		}
3567c6fd2807SJeff Garzik 
3568c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3569c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3570c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3571c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3572c6fd2807SJeff Garzik 	}
3573c6fd2807SJeff Garzik 
3574c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
35751eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
35761eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3577c6fd2807SJeff Garzik 			continue;
3578c6fd2807SJeff Garzik 
3579c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3580c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3581c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3582c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3583c6fd2807SJeff Garzik 	}
3584c6fd2807SJeff Garzik 
3585c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
35861eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3587c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3588c6fd2807SJeff Garzik 		if (rc)
3589c6fd2807SJeff Garzik 			goto out;
3590c6fd2807SJeff Garzik 	}
3591c6fd2807SJeff Garzik 
3592c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3593c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3594c6fd2807SJeff Garzik 	 */
3595cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3596032af1ceSAlan 		ap->host->simplex_claimed = ap;
3597c6fd2807SJeff Garzik 
3598c6fd2807SJeff Garzik  out:
3599c6fd2807SJeff Garzik 	if (rc)
3600c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3601c6fd2807SJeff Garzik 	return rc;
3602c6fd2807SJeff Garzik }
3603a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_set_mode);
3604c6fd2807SJeff Garzik 
3605c6fd2807SJeff Garzik /**
3606aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3607aa2731adSTejun Heo  *	@link: link to be waited on
3608aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3609aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3610aa2731adSTejun Heo  *
3611aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3612aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3613aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3614aa2731adSTejun Heo  *	conditions.
3615aa2731adSTejun Heo  *
3616aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3617aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3618aa2731adSTejun Heo  *
3619aa2731adSTejun Heo  *	LOCKING:
3620aa2731adSTejun Heo  *	EH context.
3621aa2731adSTejun Heo  *
3622aa2731adSTejun Heo  *	RETURNS:
3623c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3624aa2731adSTejun Heo  */
ata_wait_ready(struct ata_link * link,unsigned long deadline,int (* check_ready)(struct ata_link * link))3625aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3626aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3627aa2731adSTejun Heo {
3628aa2731adSTejun Heo 	unsigned long start = jiffies;
3629b48d58f5STejun Heo 	unsigned long nodev_deadline;
3630aa2731adSTejun Heo 	int warned = 0;
3631aa2731adSTejun Heo 
3632b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3633b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3634b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3635b48d58f5STejun Heo 	else
3636b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3637b48d58f5STejun Heo 
3638b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3639b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3640b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3641b1c72916STejun Heo 	 */
3642b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3643b1c72916STejun Heo 
3644aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3645aa2731adSTejun Heo 		nodev_deadline = deadline;
3646aa2731adSTejun Heo 
3647aa2731adSTejun Heo 	while (1) {
3648aa2731adSTejun Heo 		unsigned long now = jiffies;
3649aa2731adSTejun Heo 		int ready, tmp;
3650aa2731adSTejun Heo 
3651aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3652aa2731adSTejun Heo 		if (ready > 0)
3653aa2731adSTejun Heo 			return 0;
3654aa2731adSTejun Heo 
3655b48d58f5STejun Heo 		/*
3656b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3657aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3658b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3659b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3660b48d58f5STejun Heo 		 * offline.
3661aa2731adSTejun Heo 		 *
3662aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3663aa2731adSTejun Heo 		 * if status register is read more than once when
3664aa2731adSTejun Heo 		 * there's no device attached.
3665aa2731adSTejun Heo 		 */
3666aa2731adSTejun Heo 		if (ready == -ENODEV) {
3667aa2731adSTejun Heo 			if (ata_link_online(link))
3668aa2731adSTejun Heo 				ready = 0;
3669aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3670aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3671aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3672aa2731adSTejun Heo 				ready = 0;
3673aa2731adSTejun Heo 		}
3674aa2731adSTejun Heo 
3675aa2731adSTejun Heo 		if (ready)
3676aa2731adSTejun Heo 			return ready;
3677aa2731adSTejun Heo 		if (time_after(now, deadline))
3678aa2731adSTejun Heo 			return -EBUSY;
3679aa2731adSTejun Heo 
3680aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3681aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3682a9a79dfeSJoe Perches 			ata_link_warn(link,
3683aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3684aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3685aa2731adSTejun Heo 			warned = 1;
3686aa2731adSTejun Heo 		}
3687aa2731adSTejun Heo 
368897750cebSTejun Heo 		ata_msleep(link->ap, 50);
3689aa2731adSTejun Heo 	}
3690aa2731adSTejun Heo }
3691aa2731adSTejun Heo 
3692aa2731adSTejun Heo /**
3693aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3694aa2731adSTejun Heo  *	@link: link to be waited on
3695aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3696aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3697aa2731adSTejun Heo  *
3698aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3699aa2731adSTejun Heo  *
3700aa2731adSTejun Heo  *	LOCKING:
3701aa2731adSTejun Heo  *	EH context.
3702aa2731adSTejun Heo  *
3703aa2731adSTejun Heo  *	RETURNS:
3704c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3705aa2731adSTejun Heo  */
ata_wait_after_reset(struct ata_link * link,unsigned long deadline,int (* check_ready)(struct ata_link * link))37062b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3707aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3708aa2731adSTejun Heo {
370997750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3710aa2731adSTejun Heo 
3711aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3712aa2731adSTejun Heo }
3713a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_after_reset);
3714aa2731adSTejun Heo 
3715aa2731adSTejun Heo /**
37160aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3717cc0680a5STejun Heo  *	@link: ATA link to be reset
3718d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3719c6fd2807SJeff Garzik  *
3720cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3721b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3722b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3723b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3724b8cffc6aSTejun Heo  *	should just whine, not fail.
3725c6fd2807SJeff Garzik  *
3726c6fd2807SJeff Garzik  *	LOCKING:
3727c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3728c6fd2807SJeff Garzik  *
3729c6fd2807SJeff Garzik  *	RETURNS:
3730ac1eb665SSergey Shtylyov  *	Always 0.
3731c6fd2807SJeff Garzik  */
ata_std_prereset(struct ata_link * link,unsigned long deadline)37320aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3733c6fd2807SJeff Garzik {
3734cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3735936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3736d14d41ccSSergey Shtylyov 	const unsigned int *timing = sata_ehc_deb_timing(ehc);
3737c6fd2807SJeff Garzik 	int rc;
3738c6fd2807SJeff Garzik 
3739c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3740c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3741c6fd2807SJeff Garzik 		return 0;
3742c6fd2807SJeff Garzik 
3743936fd732STejun Heo 	/* if SATA, resume link */
3744a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3745936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3746b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3747b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3748a9a79dfeSJoe Perches 			ata_link_warn(link,
3749a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3750a9a79dfeSJoe Perches 				      rc);
3751c6fd2807SJeff Garzik 	}
3752c6fd2807SJeff Garzik 
375345db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3754b1c72916STejun Heo 	if (ata_phys_link_offline(link))
375545db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
375645db2f6cSTejun Heo 
3757c6fd2807SJeff Garzik 	return 0;
3758c6fd2807SJeff Garzik }
3759a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_prereset);
3760c6fd2807SJeff Garzik 
3761c6fd2807SJeff Garzik /**
376257c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
376357c9efdfSTejun Heo  *	@link: link to reset
376457c9efdfSTejun Heo  *	@class: resulting class of attached device
376557c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
376657c9efdfSTejun Heo  *
376757c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
376857c9efdfSTejun Heo  *
376957c9efdfSTejun Heo  *	LOCKING:
377057c9efdfSTejun Heo  *	Kernel thread context (may sleep)
377157c9efdfSTejun Heo  *
377257c9efdfSTejun Heo  *	RETURNS:
377357c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
377457c9efdfSTejun Heo  */
sata_std_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)377557c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
377657c9efdfSTejun Heo 		       unsigned long deadline)
377757c9efdfSTejun Heo {
3778d14d41ccSSergey Shtylyov 	const unsigned int *timing = sata_ehc_deb_timing(&link->eh_context);
377957c9efdfSTejun Heo 	bool online;
378057c9efdfSTejun Heo 	int rc;
378157c9efdfSTejun Heo 
378257c9efdfSTejun Heo 	/* do hardreset */
378357c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
378457c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
378557c9efdfSTejun Heo }
3786a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(sata_std_hardreset);
378757c9efdfSTejun Heo 
378857c9efdfSTejun Heo /**
3789203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3790cc0680a5STejun Heo  *	@link: the target ata_link
3791c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3792c6fd2807SJeff Garzik  *
3793c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3794c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3795c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3796c6fd2807SJeff Garzik  *
3797c6fd2807SJeff Garzik  *	LOCKING:
3798c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3799c6fd2807SJeff Garzik  */
ata_std_postreset(struct ata_link * link,unsigned int * classes)3800203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3801c6fd2807SJeff Garzik {
3802f046519fSTejun Heo 	u32 serror;
3803f046519fSTejun Heo 
3804f046519fSTejun Heo 	/* reset complete, clear SError */
3805f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3806f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3807f046519fSTejun Heo 
3808c6fd2807SJeff Garzik 	/* print link status */
3809936fd732STejun Heo 	sata_print_link_status(link);
3810c6fd2807SJeff Garzik }
3811a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_postreset);
3812c6fd2807SJeff Garzik 
3813c6fd2807SJeff Garzik /**
3814c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3815c6fd2807SJeff Garzik  *	@dev: device to compare against
3816c6fd2807SJeff Garzik  *	@new_class: class of the new device
3817c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3818c6fd2807SJeff Garzik  *
3819c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3820c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3821c6fd2807SJeff Garzik  *	@new_id.
3822c6fd2807SJeff Garzik  *
3823c6fd2807SJeff Garzik  *	LOCKING:
3824c6fd2807SJeff Garzik  *	None.
3825c6fd2807SJeff Garzik  *
3826c6fd2807SJeff Garzik  *	RETURNS:
3827c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3828c6fd2807SJeff Garzik  */
ata_dev_same_device(struct ata_device * dev,unsigned int new_class,const u16 * new_id)3829c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3830c6fd2807SJeff Garzik 			       const u16 *new_id)
3831c6fd2807SJeff Garzik {
3832c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3833a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3834a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3835c6fd2807SJeff Garzik 
3836c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3837a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
3838c6fd2807SJeff Garzik 			     dev->class, new_class);
3839c6fd2807SJeff Garzik 		return 0;
3840c6fd2807SJeff Garzik 	}
3841c6fd2807SJeff Garzik 
3842a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3843a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3844a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3845a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3846c6fd2807SJeff Garzik 
3847c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3848a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3849a9a79dfeSJoe Perches 			     model[0], model[1]);
3850c6fd2807SJeff Garzik 		return 0;
3851c6fd2807SJeff Garzik 	}
3852c6fd2807SJeff Garzik 
3853c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3854a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3855a9a79dfeSJoe Perches 			     serial[0], serial[1]);
3856c6fd2807SJeff Garzik 		return 0;
3857c6fd2807SJeff Garzik 	}
3858c6fd2807SJeff Garzik 
3859c6fd2807SJeff Garzik 	return 1;
3860c6fd2807SJeff Garzik }
3861c6fd2807SJeff Garzik 
3862c6fd2807SJeff Garzik /**
3863fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
38643fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3865bff04647STejun Heo  *	@readid_flags: read ID flags
3866c6fd2807SJeff Garzik  *
3867c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3868c6fd2807SJeff Garzik  *	the port.
3869c6fd2807SJeff Garzik  *
3870c6fd2807SJeff Garzik  *	LOCKING:
3871c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3872c6fd2807SJeff Garzik  *
3873c6fd2807SJeff Garzik  *	RETURNS:
3874c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3875c6fd2807SJeff Garzik  */
ata_dev_reread_id(struct ata_device * dev,unsigned int readid_flags)3876fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3877c6fd2807SJeff Garzik {
3878c6fd2807SJeff Garzik 	unsigned int class = dev->class;
38799af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3880c6fd2807SJeff Garzik 	int rc;
3881c6fd2807SJeff Garzik 
3882c6fd2807SJeff Garzik 	/* read ID data */
3883bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3884c6fd2807SJeff Garzik 	if (rc)
3885fe30911bSTejun Heo 		return rc;
3886c6fd2807SJeff Garzik 
3887c6fd2807SJeff Garzik 	/* is the device still there? */
3888fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3889fe30911bSTejun Heo 		return -ENODEV;
3890c6fd2807SJeff Garzik 
3891c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3892fe30911bSTejun Heo 	return 0;
3893fe30911bSTejun Heo }
3894fe30911bSTejun Heo 
3895fe30911bSTejun Heo /**
3896fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3897fe30911bSTejun Heo  *	@dev: device to revalidate
3898422c9daaSTejun Heo  *	@new_class: new class code
3899fe30911bSTejun Heo  *	@readid_flags: read ID flags
3900fe30911bSTejun Heo  *
3901fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3902fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3903fe30911bSTejun Heo  *
3904fe30911bSTejun Heo  *	LOCKING:
3905fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3906fe30911bSTejun Heo  *
3907fe30911bSTejun Heo  *	RETURNS:
3908fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3909fe30911bSTejun Heo  */
ata_dev_revalidate(struct ata_device * dev,unsigned int new_class,unsigned int readid_flags)3910422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3911422c9daaSTejun Heo 		       unsigned int readid_flags)
3912fe30911bSTejun Heo {
39136ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
39145920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
3915fe30911bSTejun Heo 	int rc;
3916fe30911bSTejun Heo 
3917fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3918fe30911bSTejun Heo 		return -ENODEV;
3919fe30911bSTejun Heo 
3920422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
392112e2e17dSYahu Gao 	if (ata_class_enabled(new_class) && new_class == ATA_DEV_PMP) {
3922a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
3923422c9daaSTejun Heo 			     dev->class, new_class);
3924422c9daaSTejun Heo 		rc = -ENODEV;
3925422c9daaSTejun Heo 		goto fail;
3926422c9daaSTejun Heo 	}
3927422c9daaSTejun Heo 
3928fe30911bSTejun Heo 	/* re-read ID */
3929fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3930fe30911bSTejun Heo 	if (rc)
3931fe30911bSTejun Heo 		goto fail;
3932c6fd2807SJeff Garzik 
3933c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3934efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
39356ddcd3b0STejun Heo 	if (rc)
39366ddcd3b0STejun Heo 		goto fail;
39376ddcd3b0STejun Heo 
39386ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
3939445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
3940445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
3941445d211bSTejun Heo 		return 0;
3942445d211bSTejun Heo 
3943445d211bSTejun Heo 	/* n_sectors has changed */
3944a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
39456ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
39466ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
3947445d211bSTejun Heo 
39485920dadfSTejun Heo 	/*
39495920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
3950445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
3951445d211bSTejun Heo 	 * new size matches it, keep the device.
39525920dadfSTejun Heo 	 */
39535920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3954445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
3955a9a79dfeSJoe Perches 		ata_dev_warn(dev,
39565920dadfSTejun Heo 			     "new n_sectors matches native, probably "
395768939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
395868939ce5STejun Heo 		/* use the larger n_sectors */
3959445d211bSTejun Heo 		return 0;
3960445d211bSTejun Heo 	}
3961445d211bSTejun Heo 
3962445d211bSTejun Heo 	/*
3963445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
3964445d211bSTejun Heo 	 * unlocking HPA in those cases.
3965445d211bSTejun Heo 	 *
3966445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3967445d211bSTejun Heo 	 */
3968445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3969445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3970445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
3971a9a79dfeSJoe Perches 		ata_dev_warn(dev,
3972445d211bSTejun Heo 			     "old n_sectors matches native, probably "
3973445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
3974445d211bSTejun Heo 		/* try unlocking HPA */
3975445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3976445d211bSTejun Heo 		rc = -EIO;
3977445d211bSTejun Heo 	} else
3978445d211bSTejun Heo 		rc = -ENODEV;
3979445d211bSTejun Heo 
3980445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
39815920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
39825920dadfSTejun Heo 	dev->n_sectors = n_sectors;
3983c6fd2807SJeff Garzik  fail:
3984a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
3985c6fd2807SJeff Garzik 	return rc;
3986c6fd2807SJeff Garzik }
3987c6fd2807SJeff Garzik 
39886919a0a6SAlan Cox struct ata_blacklist_entry {
39896919a0a6SAlan Cox 	const char *model_num;
39906919a0a6SAlan Cox 	const char *model_rev;
39916919a0a6SAlan Cox 	unsigned long horkage;
39926919a0a6SAlan Cox };
39936919a0a6SAlan Cox 
39946919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
39956919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
39966919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
39976919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
39986919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
39996919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
40006919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
40016919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
40026919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
40036919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
40046919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
40057da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
40066919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
40076919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
40086919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
40096919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
40106919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
40117da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
40126919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
40136919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
40146919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
40156919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
40166919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
40176919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
40186919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
40196919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
40206919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
40216919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
40226919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
402339f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
4024d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
4025b00622fcSShikha Jain 	{ "VRFDFC22048UCHC-TE*", NULL,		ATA_HORKAGE_NODMA },
40263af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
402750af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
4028a66307d4SHannes Reinecke 	/* Similar story with ASMedia 1092 */
4029a66307d4SHannes Reinecke 	{ "ASMT109x- Config",	NULL,		ATA_HORKAGE_DISABLE },
40306919a0a6SAlan Cox 
403118d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
403240a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
40336a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
4034a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
40350523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
403618d6e9d5SAlbert Lee 
4037af34d637SDavid Milburn 	/*
4038af34d637SDavid Milburn 	 * Causes silent data corruption with higher max sects.
4039af34d637SDavid Milburn 	 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
4040af34d637SDavid Milburn 	 */
4041af34d637SDavid Milburn 	{ "ST380013AS",		"3.20",		ATA_HORKAGE_MAX_SEC_1024 },
4042af34d637SDavid Milburn 
40431488a1e3STejun Heo 	/*
4044e0edc8c5STejun Heo 	 * These devices time out with higher max sects.
40451488a1e3STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
40461488a1e3STejun Heo 	 */
4047e0edc8c5STejun Heo 	{ "LITEON CX1-JB*-HP",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
4048db5ff909SXinyu Lin 	{ "LITEON EP1-*",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
40491488a1e3STejun Heo 
40506919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
40516919a0a6SAlan Cox 
40526919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
40536919a0a6SAlan Cox 	/* NCQ is slow */
40546919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4055ef1429c0SDamien Le Moal 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ },
405609125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
405709125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
40587acfaf30SPaul Rolland 	/* NCQ is broken */
4059539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
40600e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4061da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4062e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
40635ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4064539cc7c7SJeff Garzik 
4065ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
40664d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4067ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4068d10d491fSTejun Heo 
40694d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4070d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4071d10d491fSTejun Heo 
40724d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4073d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4074d10d491fSTejun Heo 
40754d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4076ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4077ac70a964STejun Heo 
407831f6264eSHans de Goede 	/* drives which fail FPDMA_AA activation (some may freeze afterwards)
407931f6264eSHans de Goede 	   the ST disks also have LPM issues */
40808756a25bSHans de Goede 	{ "ST1000LM024 HN-M101MBB", NULL,	ATA_HORKAGE_BROKEN_FPDMA_AA |
4081ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
408208c85d2aSAleksei Mamlin 	{ "VB0250EAVER",	"HPG7",		ATA_HORKAGE_BROKEN_FPDMA_AA },
408387809942SMichele Baldessari 
408436e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
408536e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
4086ef1429c0SDamien Le Moal 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ },
4087ef1429c0SDamien Le Moal 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ },
4088ef1429c0SDamien Le Moal 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ },
40896919a0a6SAlan Cox 
409068b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
4091ef1429c0SDamien Le Moal 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ },
409268b0ddb2STejun Heo 
40933b545563STejun Heo 	/* Sandisk SD7/8/9s lock up hard on large trims */
4094ef1429c0SDamien Le Moal 	{ "SanDisk SD[789]*",	NULL,		ATA_HORKAGE_MAX_TRIM_128M },
4095322579dcSTejun Heo 
409616c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
4097ef1429c0SDamien Le Moal 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA },
409816c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
409916c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
410016c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
41016919a0a6SAlan Cox 
41027831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
41037831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
41047831387bSTejun Heo 
410593328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
4106ef1429c0SDamien Le Moal 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE },
4107ef1429c0SDamien Le Moal 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE },
4108ef1429c0SDamien Le Moal 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE },
410993328e11SAlan Cox 
41106bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
4111ef1429c0SDamien Le Moal 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB },
4112a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
4113ef1429c0SDamien Le Moal 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB },
41146bbfd53dSAlan Cox 
41159ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
4116ef1429c0SDamien Le Moal 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK },
4117ef1429c0SDamien Le Moal 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK },
41189ce8e307SJens Axboe 
41199062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
4120ef1429c0SDamien Le Moal 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS },
4121ef1429c0SDamien Le Moal 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS },
41229062712fSTejun Heo 
4123d0cb43b3STejun Heo 	/*
4124d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4125d0cb43b3STejun Heo 	 * device and controller are SATA.
4126d0cb43b3STejun Heo 	 */
4127cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
41283a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
41293a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4130cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4131cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4132d0cb43b3STejun Heo 
4133ea08aec7SNiklas Cassel 	/* These specific Pioneer models have LPM issues */
4134ea08aec7SNiklas Cassel 	{ "PIONEER BD-RW   BDR-207M",	NULL,	ATA_HORKAGE_NOLPM },
4135ea08aec7SNiklas Cassel 	{ "PIONEER BD-RW   BDR-205",	NULL,	ATA_HORKAGE_NOLPM },
4136ea08aec7SNiklas Cassel 
4137b17e5729SKai-Heng Feng 	/* Crucial BX100 SSD 500GB has broken LPM support */
41383bf7b5d6SHans de Goede 	{ "CT500BX100SSD1",		NULL,	ATA_HORKAGE_NOLPM },
4139b17e5729SKai-Heng Feng 
4140d418ff56SHans de Goede 	/* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4141d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
41429c7be59fSHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4143ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
4144d418ff56SHans de Goede 	/* 512GB MX100 with newer firmware has only LPM issues */
4145d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM |
4146ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
41479c7be59fSHans de Goede 
414862ac3f73SHans de Goede 	/* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
414962ac3f73SHans de Goede 	{ "Crucial_CT480M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
415062ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4151ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
415262ac3f73SHans de Goede 	{ "Crucial_CT960M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
415362ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4154ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
415562ac3f73SHans de Goede 
415676936e9aSFrançois Cami 	/* These specific Samsung models/firmware-revs do not handle LPM well */
4157ef1429c0SDamien Le Moal 	{ "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM },
4158ef1429c0SDamien Le Moal 	{ "SAMSUNG SSD PM830 mSATA *",  "CXM13D1Q", ATA_HORKAGE_NOLPM },
4159ef1429c0SDamien Le Moal 	{ "SAMSUNG MZ7TD256HAFV-000L9", NULL,       ATA_HORKAGE_NOLPM },
4160ef1429c0SDamien Le Moal 	{ "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM },
4161b5b4d3a5SHans de Goede 
4162f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
4163136d769eSSudip Mukherjee 	{ "Micron_M500IT_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4164ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4165243918beSMartin K. Petersen 	{ "Micron_M500_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4166ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
41679051bd39SMartin K. Petersen 	{ "Micron_M5[15]0_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4168ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
416927fd0710SPawel Zmarzly 	{ "Micron_1100_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
417027fd0710SPawel Zmarzly 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
417127fd0710SPawel Zmarzly 	{ "Crucial_CT*M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
417227fd0710SPawel Zmarzly 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4173ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M550*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4174ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4175ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*MX100*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4176ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
417753997522SChristian Lamparter 	{ "Samsung SSD 840 EVO*",	NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
417853997522SChristian Lamparter 						ATA_HORKAGE_NO_DMA_LOG |
4179ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4180ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 840*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4181ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4182ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 850*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4183ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
41848a6430abSHans de Goede 	{ "Samsung SSD 860*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
41857a8526a5SKate Hsuan 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4186ef1429c0SDamien Le Moal 						ATA_HORKAGE_NO_NCQ_ON_ATI },
41878a6430abSHans de Goede 	{ "Samsung SSD 870*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
41887a8526a5SKate Hsuan 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4189ef1429c0SDamien Le Moal 						ATA_HORKAGE_NO_NCQ_ON_ATI },
4190ead08957SPatrick McLean 	{ "SAMSUNG*MZ7LH*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4191ead08957SPatrick McLean 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4192ead08957SPatrick McLean 						ATA_HORKAGE_NO_NCQ_ON_ATI, },
41937a7184b0SGuillermo A. Amaral 	{ "FCCT*M500*",			NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4194ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4195e61f7d1cSMartin K. Petersen 
4196cda57b1bSArne Fitzenreiter 	/* devices that don't properly handle TRIM commands */
4197ef1429c0SDamien Le Moal 	{ "SuperSSpeed S238*",		NULL,	ATA_HORKAGE_NOTRIM },
4198ef1429c0SDamien Le Moal 	{ "M88V29*",			NULL,	ATA_HORKAGE_NOTRIM },
4199cda57b1bSArne Fitzenreiter 
4200e61f7d1cSMartin K. Petersen 	/*
4201e61f7d1cSMartin K. Petersen 	 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4202e61f7d1cSMartin K. Petersen 	 * (Return Zero After Trim) flags in the ATA Command Set are
4203e61f7d1cSMartin K. Petersen 	 * unreliable in the sense that they only define what happens if
4204e61f7d1cSMartin K. Petersen 	 * the device successfully executed the DSM TRIM command. TRIM
4205e61f7d1cSMartin K. Petersen 	 * is only advisory, however, and the device is free to silently
4206e61f7d1cSMartin K. Petersen 	 * ignore all or parts of the request.
4207e61f7d1cSMartin K. Petersen 	 *
4208e61f7d1cSMartin K. Petersen 	 * Whitelist drives that are known to reliably return zeroes
4209e61f7d1cSMartin K. Petersen 	 * after TRIM.
4210e61f7d1cSMartin K. Petersen 	 */
4211e61f7d1cSMartin K. Petersen 
4212e61f7d1cSMartin K. Petersen 	/*
4213e61f7d1cSMartin K. Petersen 	 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4214e61f7d1cSMartin K. Petersen 	 * that model before whitelisting all other intel SSDs.
4215e61f7d1cSMartin K. Petersen 	 */
4216ef1429c0SDamien Le Moal 	{ "INTEL*SSDSC2MH*",		NULL,	0 },
4217e61f7d1cSMartin K. Petersen 
4218ef1429c0SDamien Le Moal 	{ "Micron*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4219ef1429c0SDamien Le Moal 	{ "Crucial*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4220ef1429c0SDamien Le Moal 	{ "INTEL*SSD*", 		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4221ef1429c0SDamien Le Moal 	{ "SSD*INTEL*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4222ef1429c0SDamien Le Moal 	{ "Samsung*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4223ef1429c0SDamien Le Moal 	{ "SAMSUNG*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4224ef1429c0SDamien Le Moal 	{ "SAMSUNG*MZ7KM*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4225ef1429c0SDamien Le Moal 	{ "ST[1248][0248]0[FH]*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4226f78dea06SMarc Carino 
4227ecd75ad5STejun Heo 	/*
4228ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4229ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4230ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4231ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4232ecd75ad5STejun Heo 	 * lost too.
4233ecd75ad5STejun Heo 	 *
4234ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4235ecd75ad5STejun Heo 	 */
4236ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4237ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4238ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4239ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4240ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4241ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4242ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4243ecd75ad5STejun Heo 
4244ac9f0c81SAnton Lundin 	/*
4245ac9f0c81SAnton Lundin 	 * This sata dom device goes on a walkabout when the ATA_LOG_DIRECTORY
4246ac9f0c81SAnton Lundin 	 * log page is accessed. Ensure we never ask for this log page with
4247ac9f0c81SAnton Lundin 	 * these devices.
4248ac9f0c81SAnton Lundin 	 */
4249ac9f0c81SAnton Lundin 	{ "SATADOM-ML 3ME",		NULL,	ATA_HORKAGE_NO_LOG_DIR },
4250ac9f0c81SAnton Lundin 
42514d2e4980SDamien Le Moal 	/* Buggy FUA */
42524d2e4980SDamien Le Moal 	{ "Maxtor",		"BANC1G10",	ATA_HORKAGE_NO_FUA },
425338d43122SDamien Le Moal 	{ "WDC*WD2500J*",	NULL,		ATA_HORKAGE_NO_FUA },
425438d43122SDamien Le Moal 	{ "OCZ-VERTEX*",	NULL,		ATA_HORKAGE_NO_FUA },
425538d43122SDamien Le Moal 	{ "INTEL*SSDSC2CT*",	NULL,		ATA_HORKAGE_NO_FUA },
42564d2e4980SDamien Le Moal 
42576919a0a6SAlan Cox 	/* End Marker */
42586919a0a6SAlan Cox 	{ }
4259c6fd2807SJeff Garzik };
4260c6fd2807SJeff Garzik 
ata_dev_blacklisted(const struct ata_device * dev)426175683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4262c6fd2807SJeff Garzik {
42638bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
42648bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
42656919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4266c6fd2807SJeff Garzik 
42678bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
42688bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4269c6fd2807SJeff Garzik 
42706919a0a6SAlan Cox 	while (ad->model_num) {
42711c402799SGeorge Spelvin 		if (glob_match(ad->model_num, model_num)) {
42726919a0a6SAlan Cox 			if (ad->model_rev == NULL)
42736919a0a6SAlan Cox 				return ad->horkage;
42741c402799SGeorge Spelvin 			if (glob_match(ad->model_rev, model_rev))
42756919a0a6SAlan Cox 				return ad->horkage;
4276c6fd2807SJeff Garzik 		}
42776919a0a6SAlan Cox 		ad++;
4278c6fd2807SJeff Garzik 	}
4279c6fd2807SJeff Garzik 	return 0;
4280c6fd2807SJeff Garzik }
4281c6fd2807SJeff Garzik 
ata_dma_blacklisted(const struct ata_device * dev)42826919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
42836919a0a6SAlan Cox {
42846919a0a6SAlan Cox 	/* We don't support polling DMA.
42856919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
42866919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
42876919a0a6SAlan Cox 	 */
42889af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
42896919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
42906919a0a6SAlan Cox 		return 1;
429175683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
42926919a0a6SAlan Cox }
42936919a0a6SAlan Cox 
4294c6fd2807SJeff Garzik /**
42956bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
42966bbfd53dSAlan Cox  *	@dev: device
42976bbfd53dSAlan Cox  *
42986bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
42996bbfd53dSAlan Cox  *	who can't follow the documentation.
43006bbfd53dSAlan Cox  */
43016bbfd53dSAlan Cox 
ata_is_40wire(struct ata_device * dev)43026bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
43036bbfd53dSAlan Cox {
43046bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
43056bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
43066bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
43076bbfd53dSAlan Cox }
43086bbfd53dSAlan Cox 
43096bbfd53dSAlan Cox /**
431015a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
431115a5551cSAlan Cox  *	@ap: port to consider
431215a5551cSAlan Cox  *
431315a5551cSAlan Cox  *	This function encapsulates the policy for speed management
431415a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
431515a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
431615a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
431715a5551cSAlan Cox  *	impacts hotplug at all).
431815a5551cSAlan Cox  *
431915a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
432015a5551cSAlan Cox  */
432115a5551cSAlan Cox 
cable_is_40wire(struct ata_port * ap)432215a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
432315a5551cSAlan Cox {
432415a5551cSAlan Cox 	struct ata_link *link;
432515a5551cSAlan Cox 	struct ata_device *dev;
432615a5551cSAlan Cox 
43274a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
432815a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
432915a5551cSAlan Cox 		return 1;
43304a9c7b33STejun Heo 
43314a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
433215a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
433315a5551cSAlan Cox 		return 0;
43344a9c7b33STejun Heo 
43354a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
43364a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
43374a9c7b33STejun Heo 	 * isn't sure.
43384a9c7b33STejun Heo 	 */
4339f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4340f792068eSAlan Cox 		return 0;
434115a5551cSAlan Cox 
43424a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
43434a9c7b33STejun Heo 	 *
43444a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
43454a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
43464a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
43474a9c7b33STejun Heo 	 *   give a valid detect
43484a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
43494a9c7b33STejun Heo 	 *   to colour the choice
435015a5551cSAlan Cox 	 */
43511eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
43521eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
43531eca4365STejun Heo 			if (!ata_is_40wire(dev))
435415a5551cSAlan Cox 				return 0;
435515a5551cSAlan Cox 		}
435615a5551cSAlan Cox 	}
435715a5551cSAlan Cox 	return 1;
435815a5551cSAlan Cox }
435915a5551cSAlan Cox 
436015a5551cSAlan Cox /**
4361c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4362c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4363c6fd2807SJeff Garzik  *
4364c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4365c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4366c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4367c6fd2807SJeff Garzik  *	blacklist, etc...
4368c6fd2807SJeff Garzik  *
4369c6fd2807SJeff Garzik  *	LOCKING:
4370c6fd2807SJeff Garzik  *	None.
4371c6fd2807SJeff Garzik  */
ata_dev_xfermask(struct ata_device * dev)4372c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4373c6fd2807SJeff Garzik {
43749af5c9c9STejun Heo 	struct ata_link *link = dev->link;
43759af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4376cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4377f0a6d77bSSergey Shtylyov 	unsigned int xfer_mask;
4378c6fd2807SJeff Garzik 
4379c6fd2807SJeff Garzik 	/* controller modes available */
4380c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4381c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4382c6fd2807SJeff Garzik 
43838343f889SRobert Hancock 	/* drive modes available */
4384c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4385c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4386c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4387c6fd2807SJeff Garzik 
4388b352e57dSAlan Cox 	/*
4389b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4390b352e57dSAlan Cox 	 *	cable
4391b352e57dSAlan Cox 	 */
4392b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4393b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4394b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4395b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4396b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4397b352e57dSAlan Cox 	}
4398b352e57dSAlan Cox 
4399c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4400c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4401a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4402c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4403c6fd2807SJeff Garzik 	}
4404c6fd2807SJeff Garzik 
440514d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
440614d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4407c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4408a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4409a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4410c6fd2807SJeff Garzik 	}
4411c6fd2807SJeff Garzik 
4412e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4413e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4414e424675fSJeff Garzik 
4415c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4416a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4417c6fd2807SJeff Garzik 
44188343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
44198343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
44208343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
44218343f889SRobert Hancock 	 * solely limited by the cable.
44228343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
44238343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
44248343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
44258343f889SRobert Hancock 	 */
44268343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
44278343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
442815a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4429a9a79dfeSJoe Perches 			ata_dev_warn(dev,
44308343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
44318343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
44328343f889SRobert Hancock 		}
44338343f889SRobert Hancock 
4434c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4435c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4436c6fd2807SJeff Garzik }
4437c6fd2807SJeff Garzik 
4438c6fd2807SJeff Garzik /**
4439c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4440c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4441c6fd2807SJeff Garzik  *
4442c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4443c6fd2807SJeff Garzik  *	on port @ap.
4444c6fd2807SJeff Garzik  *
4445c6fd2807SJeff Garzik  *	LOCKING:
4446c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4447c6fd2807SJeff Garzik  *
4448c6fd2807SJeff Garzik  *	RETURNS:
4449c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4450c6fd2807SJeff Garzik  */
4451c6fd2807SJeff Garzik 
ata_dev_set_xfermode(struct ata_device * dev)4452c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4453c6fd2807SJeff Garzik {
4454c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4455c6fd2807SJeff Garzik 
4456c6fd2807SJeff Garzik 	/* set up set-features taskfile */
44574633778bSHannes Reinecke 	ata_dev_dbg(dev, "set features - xfer mode\n");
4458c6fd2807SJeff Garzik 
4459464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4460464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4461464cf177STejun Heo 	 */
4462c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4463c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4464c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4465464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4466c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4467b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4468b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4469c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4470b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4471b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4472b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4473b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4474b9f8ab2dSAlan Cox 		return 0;
4475c6fd2807SJeff Garzik 
4476024811a2SDamien Le Moal 	/*
4477024811a2SDamien Le Moal 	 * On some disks, this command causes spin-up, so we need longer
4478024811a2SDamien Le Moal 	 * timeout.
4479024811a2SDamien Le Moal 	 */
4480024811a2SDamien Le Moal 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4481c6fd2807SJeff Garzik }
44821152b261STejun Heo 
4483c6fd2807SJeff Garzik /**
448499ad3f9fSNiklas Cassel  *	ata_dev_set_feature - Issue SET FEATURES
44859f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
448699ad3f9fSNiklas Cassel  *	@subcmd: The SET FEATURES subcommand to be sent
448799ad3f9fSNiklas Cassel  *	@action: The sector count represents a subcommand specific action
44889f45cbd3SKristen Carlson Accardi  *
448999ad3f9fSNiklas Cassel  *	Issue SET FEATURES command to device @dev on port @ap with sector count
44909f45cbd3SKristen Carlson Accardi  *
44919f45cbd3SKristen Carlson Accardi  *	LOCKING:
44929f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
44939f45cbd3SKristen Carlson Accardi  *
44949f45cbd3SKristen Carlson Accardi  *	RETURNS:
44959f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
44969f45cbd3SKristen Carlson Accardi  */
ata_dev_set_feature(struct ata_device * dev,u8 subcmd,u8 action)449799ad3f9fSNiklas Cassel unsigned int ata_dev_set_feature(struct ata_device *dev, u8 subcmd, u8 action)
44989f45cbd3SKristen Carlson Accardi {
44999f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
450061176eedSSergey Shtylyov 	unsigned int timeout = 0;
45019f45cbd3SKristen Carlson Accardi 
45029f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
450399ad3f9fSNiklas Cassel 	ata_dev_dbg(dev, "set features\n");
45049f45cbd3SKristen Carlson Accardi 
45059f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
45069f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
450799ad3f9fSNiklas Cassel 	tf.feature = subcmd;
45089f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
45099f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
451099ad3f9fSNiklas Cassel 	tf.nsect = action;
45119f45cbd3SKristen Carlson Accardi 
451299ad3f9fSNiklas Cassel 	if (subcmd == SETFEATURES_SPINUP)
4513974e0a45SDamien Le Moal 		timeout = ata_probe_timeout ?
4514974e0a45SDamien Le Moal 			  ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
45159f45cbd3SKristen Carlson Accardi 
4516614065abSJinpeng Cui 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
45179f45cbd3SKristen Carlson Accardi }
4518633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
45199f45cbd3SKristen Carlson Accardi 
45209f45cbd3SKristen Carlson Accardi /**
4521c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4522c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4523c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4524c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4525c6fd2807SJeff Garzik  *
4526c6fd2807SJeff Garzik  *	LOCKING:
4527c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4528c6fd2807SJeff Garzik  *
4529c6fd2807SJeff Garzik  *	RETURNS:
4530c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4531c6fd2807SJeff Garzik  */
ata_dev_init_params(struct ata_device * dev,u16 heads,u16 sectors)4532c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4533c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4534c6fd2807SJeff Garzik {
4535c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4536c6fd2807SJeff Garzik 	unsigned int err_mask;
4537c6fd2807SJeff Garzik 
4538c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4539c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4540c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4541c6fd2807SJeff Garzik 
4542c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
45434633778bSHannes Reinecke 	ata_dev_dbg(dev, "init dev params \n");
4544c6fd2807SJeff Garzik 
4545c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4546c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4547c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4548c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4549c6fd2807SJeff Garzik 	tf.nsect = sectors;
4550c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4551c6fd2807SJeff Garzik 
45522b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
455318b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
455418b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
455518b2466cSAlan Cox 	   drive reported working geometry */
4556efcef265SSergey Shtylyov 	if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
455718b2466cSAlan Cox 		err_mask = 0;
4558c6fd2807SJeff Garzik 
4559c6fd2807SJeff Garzik 	return err_mask;
4560c6fd2807SJeff Garzik }
4561c6fd2807SJeff Garzik 
4562c6fd2807SJeff Garzik /**
45635895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4564c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4565c6fd2807SJeff Garzik  *
4566c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4567c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4568c6fd2807SJeff Garzik  *	supplied PACKET command.
4569c6fd2807SJeff Garzik  *
4570c6fd2807SJeff Garzik  *	LOCKING:
4571cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4572c6fd2807SJeff Garzik  *
4573c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4574c6fd2807SJeff Garzik  *               nonzero otherwise
4575c6fd2807SJeff Garzik  */
atapi_check_dma(struct ata_queued_cmd * qc)45765895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4577c6fd2807SJeff Garzik {
4578c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4579c6fd2807SJeff Garzik 
4580b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4581b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4582b9a4197eSTejun Heo 	 */
45836a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
45846a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
45856f23a31dSAlbert Lee 		return 1;
45866f23a31dSAlbert Lee 
4587c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4588b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4589c6fd2807SJeff Garzik 
4590b9a4197eSTejun Heo 	return 0;
4591c6fd2807SJeff Garzik }
4592b9a4197eSTejun Heo 
4593c6fd2807SJeff Garzik /**
459431cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
459531cc23b3STejun Heo  *	@qc: ATA command in question
459631cc23b3STejun Heo  *
459731cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
459831cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
459931cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
460031cc23b3STejun Heo  *	whether a new command @qc can be issued.
460131cc23b3STejun Heo  *
460231cc23b3STejun Heo  *	LOCKING:
460331cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
460431cc23b3STejun Heo  *
460531cc23b3STejun Heo  *	RETURNS:
460631cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
460731cc23b3STejun Heo  */
ata_std_qc_defer(struct ata_queued_cmd * qc)460831cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
460931cc23b3STejun Heo {
461031cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
461131cc23b3STejun Heo 
4612179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
461331cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
461431cc23b3STejun Heo 			return 0;
461531cc23b3STejun Heo 	} else {
461631cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
461731cc23b3STejun Heo 			return 0;
461831cc23b3STejun Heo 	}
461931cc23b3STejun Heo 
462031cc23b3STejun Heo 	return ATA_DEFER_LINK;
462131cc23b3STejun Heo }
4622a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_qc_defer);
462331cc23b3STejun Heo 
ata_noop_qc_prep(struct ata_queued_cmd * qc)462495364f36SJiri Slaby enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
462595364f36SJiri Slaby {
462695364f36SJiri Slaby 	return AC_ERR_OK;
462795364f36SJiri Slaby }
4628a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
4629c6fd2807SJeff Garzik 
4630c6fd2807SJeff Garzik /**
4631c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4632c6fd2807SJeff Garzik  *	@qc: Command to be associated
4633c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4634c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4635c6fd2807SJeff Garzik  *
4636c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4637c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4638c6fd2807SJeff Garzik  *	elements.
4639c6fd2807SJeff Garzik  *
4640c6fd2807SJeff Garzik  *	LOCKING:
4641cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4642c6fd2807SJeff Garzik  */
ata_sg_init(struct ata_queued_cmd * qc,struct scatterlist * sg,unsigned int n_elem)4643c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4644c6fd2807SJeff Garzik 		 unsigned int n_elem)
4645c6fd2807SJeff Garzik {
4646ff2aeb1eSTejun Heo 	qc->sg = sg;
4647c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4648ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4649ff2aeb1eSTejun Heo }
4650ff2aeb1eSTejun Heo 
46512874d5eeSGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
46522874d5eeSGeert Uytterhoeven 
46532874d5eeSGeert Uytterhoeven /**
46542874d5eeSGeert Uytterhoeven  *	ata_sg_clean - Unmap DMA memory associated with command
46552874d5eeSGeert Uytterhoeven  *	@qc: Command containing DMA memory to be released
46562874d5eeSGeert Uytterhoeven  *
46572874d5eeSGeert Uytterhoeven  *	Unmap all mapped DMA memory associated with this command.
46582874d5eeSGeert Uytterhoeven  *
46592874d5eeSGeert Uytterhoeven  *	LOCKING:
46602874d5eeSGeert Uytterhoeven  *	spin_lock_irqsave(host lock)
46612874d5eeSGeert Uytterhoeven  */
ata_sg_clean(struct ata_queued_cmd * qc)4662af27e01cSJason Yan static void ata_sg_clean(struct ata_queued_cmd *qc)
46632874d5eeSGeert Uytterhoeven {
46642874d5eeSGeert Uytterhoeven 	struct ata_port *ap = qc->ap;
46652874d5eeSGeert Uytterhoeven 	struct scatterlist *sg = qc->sg;
46662874d5eeSGeert Uytterhoeven 	int dir = qc->dma_dir;
46672874d5eeSGeert Uytterhoeven 
46682874d5eeSGeert Uytterhoeven 	WARN_ON_ONCE(sg == NULL);
46692874d5eeSGeert Uytterhoeven 
46702874d5eeSGeert Uytterhoeven 	if (qc->n_elem)
46712874d5eeSGeert Uytterhoeven 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
46722874d5eeSGeert Uytterhoeven 
46732874d5eeSGeert Uytterhoeven 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
46742874d5eeSGeert Uytterhoeven 	qc->sg = NULL;
46752874d5eeSGeert Uytterhoeven }
46762874d5eeSGeert Uytterhoeven 
4677c6fd2807SJeff Garzik /**
4678c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4679c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4680c6fd2807SJeff Garzik  *
4681c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4682c6fd2807SJeff Garzik  *
4683c6fd2807SJeff Garzik  *	LOCKING:
4684cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4685c6fd2807SJeff Garzik  *
4686c6fd2807SJeff Garzik  *	RETURNS:
4687c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4688c6fd2807SJeff Garzik  *
4689c6fd2807SJeff Garzik  */
ata_sg_setup(struct ata_queued_cmd * qc)4690c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4691c6fd2807SJeff Garzik {
4692c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4693dde20207SJames Bottomley 	unsigned int n_elem;
4694c6fd2807SJeff Garzik 
4695dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4696dde20207SJames Bottomley 	if (n_elem < 1)
4697c6fd2807SJeff Garzik 		return -1;
4698c6fd2807SJeff Garzik 
46995825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4700dde20207SJames Bottomley 	qc->n_elem = n_elem;
4701f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4702c6fd2807SJeff Garzik 
4703c6fd2807SJeff Garzik 	return 0;
4704c6fd2807SJeff Garzik }
4705c6fd2807SJeff Garzik 
47062874d5eeSGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
47072874d5eeSGeert Uytterhoeven 
ata_sg_clean(struct ata_queued_cmd * qc)47082874d5eeSGeert Uytterhoeven static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
ata_sg_setup(struct ata_queued_cmd * qc)47092874d5eeSGeert Uytterhoeven static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
47102874d5eeSGeert Uytterhoeven 
47112874d5eeSGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
47122874d5eeSGeert Uytterhoeven 
4713c6fd2807SJeff Garzik /**
4714c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4715c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4716c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4717c6fd2807SJeff Garzik  *
4718c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4719c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4720c6fd2807SJeff Garzik  *	vice-versa.
4721c6fd2807SJeff Garzik  *
4722c6fd2807SJeff Garzik  *	LOCKING:
4723c6fd2807SJeff Garzik  *	Inherited from caller.
4724c6fd2807SJeff Garzik  */
swap_buf_le16(u16 * buf,unsigned int buf_words)4725c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4726c6fd2807SJeff Garzik {
4727c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4728c6fd2807SJeff Garzik 	unsigned int i;
4729c6fd2807SJeff Garzik 
4730c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4731c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4732c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4733c6fd2807SJeff Garzik }
4734c6fd2807SJeff Garzik 
4735c6fd2807SJeff Garzik /**
47368a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
47378a8bc223STejun Heo  *	@qc: Command to complete
47388a8bc223STejun Heo  *
47398a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
47408a8bc223STejun Heo  *	in case something prevents using it.
47418a8bc223STejun Heo  *
47428a8bc223STejun Heo  *	LOCKING:
47438a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
47448a8bc223STejun Heo  */
ata_qc_free(struct ata_queued_cmd * qc)47458a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
47468a8bc223STejun Heo {
47478a8bc223STejun Heo 	qc->flags = 0;
47484f1a22eeSJohn Garry 	if (ata_tag_valid(qc->tag))
47498a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
47508a8bc223STejun Heo }
47518a8bc223STejun Heo 
__ata_qc_complete(struct ata_queued_cmd * qc)4752c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4753c6fd2807SJeff Garzik {
4754a1104016SJulia Lawall 	struct ata_port *ap;
4755a1104016SJulia Lawall 	struct ata_link *link;
4756c6fd2807SJeff Garzik 
4757efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4758efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4759a1104016SJulia Lawall 	ap = qc->ap;
4760a1104016SJulia Lawall 	link = qc->dev->link;
4761c6fd2807SJeff Garzik 
4762c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4763c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4764c6fd2807SJeff Garzik 
4765c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4766179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
47674e5b6260SJens Axboe 		link->sactive &= ~(1 << qc->hw_tag);
4768da917d69STejun Heo 		if (!link->sactive)
4769da917d69STejun Heo 			ap->nr_active_links--;
4770da917d69STejun Heo 	} else {
47719af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4772da917d69STejun Heo 		ap->nr_active_links--;
4773da917d69STejun Heo 	}
4774da917d69STejun Heo 
4775da917d69STejun Heo 	/* clear exclusive status */
4776da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4777da917d69STejun Heo 		     ap->excl_link == link))
4778da917d69STejun Heo 		ap->excl_link = NULL;
4779c6fd2807SJeff Garzik 
4780c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4781c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4782c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4783c6fd2807SJeff Garzik 	 */
4784c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4785e3ed8939SJens Axboe 	ap->qc_active &= ~(1ULL << qc->tag);
4786c6fd2807SJeff Garzik 
4787c6fd2807SJeff Garzik 	/* call completion callback */
4788c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4789c6fd2807SJeff Garzik }
4790c6fd2807SJeff Garzik 
fill_result_tf(struct ata_queued_cmd * qc)479139599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
479239599a53STejun Heo {
479339599a53STejun Heo 	struct ata_port *ap = qc->ap;
479439599a53STejun Heo 
479539599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
479622183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
479739599a53STejun Heo }
479839599a53STejun Heo 
ata_verify_xfer(struct ata_queued_cmd * qc)479900115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
480000115e0fSTejun Heo {
480100115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
480200115e0fSTejun Heo 
4803eb0effdfSChristoph Hellwig 	if (!ata_is_data(qc->tf.protocol))
480400115e0fSTejun Heo 		return;
480500115e0fSTejun Heo 
480600115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
480700115e0fSTejun Heo 		return;
480800115e0fSTejun Heo 
480900115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
481000115e0fSTejun Heo }
481100115e0fSTejun Heo 
4812c6fd2807SJeff Garzik /**
4813c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4814c6fd2807SJeff Garzik  *	@qc: Command to complete
4815c6fd2807SJeff Garzik  *
48161aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
48171aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
48181aadf5c3STejun Heo  *
48191aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
48201aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
48211aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
48221aadf5c3STejun Heo  *	properly update IRQ expect state.
4823c6fd2807SJeff Garzik  *
4824c6fd2807SJeff Garzik  *	LOCKING:
4825cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4826c6fd2807SJeff Garzik  */
ata_qc_complete(struct ata_queued_cmd * qc)4827c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4828c6fd2807SJeff Garzik {
4829c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4830ff8072d5SHannes Reinecke 	struct ata_device *dev = qc->dev;
4831ff8072d5SHannes Reinecke 	struct ata_eh_info *ehi = &dev->link->eh_info;
4832c6fd2807SJeff Garzik 
4833eb25cb99SStephan Linz 	/* Trigger the LED (if available) */
4834d1ed7c55SLinus Walleij 	ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
4835eb25cb99SStephan Linz 
4836ff8072d5SHannes Reinecke 	/*
4837ff8072d5SHannes Reinecke 	 * In order to synchronize EH with the regular execution path, a qc that
4838ff8072d5SHannes Reinecke 	 * is owned by EH is marked with ATA_QCFLAG_EH.
4839c6fd2807SJeff Garzik 	 *
4840ff8072d5SHannes Reinecke 	 * The normal execution path is responsible for not accessing a qc owned
4841ff8072d5SHannes Reinecke 	 * by EH.  libata core enforces the rule by returning NULL from
4842ff8072d5SHannes Reinecke 	 * ata_qc_from_tag() for qcs owned by EH.
4843c6fd2807SJeff Garzik 	 */
4844c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
484587629312SNiklas Cassel 		qc->flags |= ATA_QCFLAG_EH;
4846c6fd2807SJeff Garzik 
4847f08dc1acSTejun Heo 	/*
4848ff8072d5SHannes Reinecke 	 * Finish internal commands without any further processing and always
4849ff8072d5SHannes Reinecke 	 * with the result TF filled.
4850f08dc1acSTejun Heo 	 */
4851f08dc1acSTejun Heo 	if (unlikely(ata_tag_internal(qc->tag))) {
485239599a53STejun Heo 		fill_result_tf(qc);
4853255c03d1SHannes Reinecke 		trace_ata_qc_complete_internal(qc);
4854f4b31db9STejun Heo 		__ata_qc_complete(qc);
4855c6fd2807SJeff Garzik 		return;
4856c6fd2807SJeff Garzik 	}
4857c6fd2807SJeff Garzik 
4858ff8072d5SHannes Reinecke 	/* Non-internal qc has failed.  Fill the result TF and summon EH. */
485987629312SNiklas Cassel 	if (unlikely(qc->flags & ATA_QCFLAG_EH)) {
4860f08dc1acSTejun Heo 		fill_result_tf(qc);
4861255c03d1SHannes Reinecke 		trace_ata_qc_complete_failed(qc);
4862f08dc1acSTejun Heo 		ata_qc_schedule_eh(qc);
4863f08dc1acSTejun Heo 		return;
4864f08dc1acSTejun Heo 	}
4865f08dc1acSTejun Heo 
48664cb7c6f1SNiklas Cassel 	WARN_ON_ONCE(ata_port_is_frozen(ap));
48674dc738edSTejun Heo 
4868c6fd2807SJeff Garzik 	/* read result TF if requested */
4869c6fd2807SJeff Garzik 	if (qc->flags & ATA_QCFLAG_RESULT_TF)
487039599a53STejun Heo 		fill_result_tf(qc);
4871c6fd2807SJeff Garzik 
4872255c03d1SHannes Reinecke 	trace_ata_qc_complete_done(qc);
487318bd7718SNiklas Cassel 
487418bd7718SNiklas Cassel 	/*
4875ff8072d5SHannes Reinecke 	 * For CDL commands that completed without an error, check if we have
4876ff8072d5SHannes Reinecke 	 * sense data (ATA_SENSE is set). If we do, then the command may have
4877ff8072d5SHannes Reinecke 	 * been aborted by the device due to a limit timeout using the policy
4878ff8072d5SHannes Reinecke 	 * 0xD. For these commands, invoke EH to get the command sense data.
487918bd7718SNiklas Cassel 	 */
48805e35a9acSNiklas Cassel 	if (qc->flags & ATA_QCFLAG_HAS_CDL &&
48815e35a9acSNiklas Cassel 	    qc->result_tf.status & ATA_SENSE) {
488218bd7718SNiklas Cassel 		/*
4883ff8072d5SHannes Reinecke 		 * Tell SCSI EH to not overwrite scmd->result even if this
4884ff8072d5SHannes Reinecke 		 * command is finished with result SAM_STAT_GOOD.
488518bd7718SNiklas Cassel 		 */
488618bd7718SNiklas Cassel 		qc->scsicmd->flags |= SCMD_FORCE_EH_SUCCESS;
488718bd7718SNiklas Cassel 		qc->flags |= ATA_QCFLAG_EH_SUCCESS_CMD;
488818bd7718SNiklas Cassel 		ehi->dev_action[dev->devno] |= ATA_EH_GET_SUCCESS_SENSE;
488918bd7718SNiklas Cassel 
489018bd7718SNiklas Cassel 		/*
4891ff8072d5SHannes Reinecke 		 * set pending so that ata_qc_schedule_eh() does not trigger
4892ff8072d5SHannes Reinecke 		 * fast drain, and freeze the port.
489318bd7718SNiklas Cassel 		 */
489418bd7718SNiklas Cassel 		ap->pflags |= ATA_PFLAG_EH_PENDING;
489518bd7718SNiklas Cassel 		ata_qc_schedule_eh(qc);
489618bd7718SNiklas Cassel 		return;
489718bd7718SNiklas Cassel 	}
489818bd7718SNiklas Cassel 
4899ff8072d5SHannes Reinecke 	/* Some commands need post-processing after successful completion. */
49004dbfa39bSTejun Heo 	switch (qc->tf.command) {
49014dbfa39bSTejun Heo 	case ATA_CMD_SET_FEATURES:
49024dbfa39bSTejun Heo 		if (qc->tf.feature != SETFEATURES_WC_ON &&
49030c12735eSTom Yan 		    qc->tf.feature != SETFEATURES_WC_OFF &&
49040c12735eSTom Yan 		    qc->tf.feature != SETFEATURES_RA_ON &&
49050c12735eSTom Yan 		    qc->tf.feature != SETFEATURES_RA_OFF)
49064dbfa39bSTejun Heo 			break;
4907df561f66SGustavo A. R. Silva 		fallthrough;
49084dbfa39bSTejun Heo 	case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
49094dbfa39bSTejun Heo 	case ATA_CMD_SET_MULTI: /* multi_count changed */
49104dbfa39bSTejun Heo 		/* revalidate device */
49114dbfa39bSTejun Heo 		ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
49124dbfa39bSTejun Heo 		ata_port_schedule_eh(ap);
49134dbfa39bSTejun Heo 		break;
4914054a5fbaSTejun Heo 
4915054a5fbaSTejun Heo 	case ATA_CMD_SLEEP:
4916054a5fbaSTejun Heo 		dev->flags |= ATA_DFLAG_SLEEPING;
4917054a5fbaSTejun Heo 		break;
49184dbfa39bSTejun Heo 	}
49194dbfa39bSTejun Heo 
492000115e0fSTejun Heo 	if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
492100115e0fSTejun Heo 		ata_verify_xfer(qc);
492200115e0fSTejun Heo 
4923c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
4924c6fd2807SJeff Garzik }
4925a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_qc_complete);
4926c6fd2807SJeff Garzik 
4927c6fd2807SJeff Garzik /**
49288385d756SSascha Hauer  *	ata_qc_get_active - get bitmask of active qcs
49298385d756SSascha Hauer  *	@ap: port in question
49308385d756SSascha Hauer  *
49318385d756SSascha Hauer  *	LOCKING:
49328385d756SSascha Hauer  *	spin_lock_irqsave(host lock)
49338385d756SSascha Hauer  *
49348385d756SSascha Hauer  *	RETURNS:
49358385d756SSascha Hauer  *	Bitmask of active qcs
49368385d756SSascha Hauer  */
ata_qc_get_active(struct ata_port * ap)49378385d756SSascha Hauer u64 ata_qc_get_active(struct ata_port *ap)
49388385d756SSascha Hauer {
49398385d756SSascha Hauer 	u64 qc_active = ap->qc_active;
49408385d756SSascha Hauer 
49418385d756SSascha Hauer 	/* ATA_TAG_INTERNAL is sent to hw as tag 0 */
49428385d756SSascha Hauer 	if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
49438385d756SSascha Hauer 		qc_active |= (1 << 0);
49448385d756SSascha Hauer 		qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
49458385d756SSascha Hauer 	}
49468385d756SSascha Hauer 
49478385d756SSascha Hauer 	return qc_active;
49488385d756SSascha Hauer }
49498385d756SSascha Hauer EXPORT_SYMBOL_GPL(ata_qc_get_active);
49508385d756SSascha Hauer 
49518385d756SSascha Hauer /**
4952c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4953c6fd2807SJeff Garzik  *	@qc: command to issue to device
4954c6fd2807SJeff Garzik  *
4955c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4956c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4957c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
4958c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
4959c6fd2807SJeff Garzik  *
4960c6fd2807SJeff Garzik  *	LOCKING:
4961cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4962c6fd2807SJeff Garzik  */
ata_qc_issue(struct ata_queued_cmd * qc)4963c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
4964c6fd2807SJeff Garzik {
4965c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
49669af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4967405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
4968c6fd2807SJeff Garzik 
4969ff8072d5SHannes Reinecke 	/* Make sure only one non-NCQ command is outstanding. */
4970ff8072d5SHannes Reinecke 	WARN_ON_ONCE(ata_tag_valid(link->active_tag));
4971c6fd2807SJeff Garzik 
49721973a023STejun Heo 	if (ata_is_ncq(prot)) {
49734e5b6260SJens Axboe 		WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
4974da917d69STejun Heo 
4975da917d69STejun Heo 		if (!link->sactive)
4976da917d69STejun Heo 			ap->nr_active_links++;
49774e5b6260SJens Axboe 		link->sactive |= 1 << qc->hw_tag;
4978c6fd2807SJeff Garzik 	} else {
4979efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
4980da917d69STejun Heo 
4981da917d69STejun Heo 		ap->nr_active_links++;
49829af5c9c9STejun Heo 		link->active_tag = qc->tag;
4983c6fd2807SJeff Garzik 	}
4984c6fd2807SJeff Garzik 
4985c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
4986e3ed8939SJens Axboe 	ap->qc_active |= 1ULL << qc->tag;
4987c6fd2807SJeff Garzik 
498860f5d6efSTejun Heo 	/*
498960f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
4990f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
4991f92a2636STejun Heo 	 */
49929173e5e8SEric Biggers 	if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
499360f5d6efSTejun Heo 		goto sys_err;
4994f92a2636STejun Heo 
4995405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
4996f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
4997c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
499860f5d6efSTejun Heo 			goto sys_err;
4999c6fd2807SJeff Garzik 
5000cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5001054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5002cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5003054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5004054a5fbaSTejun Heo 		ata_link_abort(link);
5005054a5fbaSTejun Heo 		return;
5006054a5fbaSTejun Heo 	}
5007054a5fbaSTejun Heo 
5008fc914faaSHannes Reinecke 	trace_ata_qc_prep(qc);
500995364f36SJiri Slaby 	qc->err_mask |= ap->ops->qc_prep(qc);
501095364f36SJiri Slaby 	if (unlikely(qc->err_mask))
501195364f36SJiri Slaby 		goto err;
5012255c03d1SHannes Reinecke 	trace_ata_qc_issue(qc);
5013c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5014c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5015c6fd2807SJeff Garzik 		goto err;
5016c6fd2807SJeff Garzik 	return;
5017c6fd2807SJeff Garzik 
501860f5d6efSTejun Heo sys_err:
5019c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5020c6fd2807SJeff Garzik err:
5021c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5022c6fd2807SJeff Garzik }
5023c6fd2807SJeff Garzik 
5024c6fd2807SJeff Garzik /**
5025b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5026936fd732STejun Heo  *	@link: ATA link to test
5027c6fd2807SJeff Garzik  *
5028936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5029936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5030936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5031c6fd2807SJeff Garzik  *
5032c6fd2807SJeff Garzik  *	LOCKING:
5033c6fd2807SJeff Garzik  *	None.
5034c6fd2807SJeff Garzik  *
5035c6fd2807SJeff Garzik  *	RETURNS:
5036b5b3fa38STejun Heo  *	True if the port online status is available and online.
5037c6fd2807SJeff Garzik  */
ata_phys_link_online(struct ata_link * link)5038b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5039c6fd2807SJeff Garzik {
5040c6fd2807SJeff Garzik 	u32 sstatus;
5041c6fd2807SJeff Garzik 
5042936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
50439913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5044b5b3fa38STejun Heo 		return true;
5045b5b3fa38STejun Heo 	return false;
5046c6fd2807SJeff Garzik }
5047c6fd2807SJeff Garzik 
5048c6fd2807SJeff Garzik /**
5049b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5050936fd732STejun Heo  *	@link: ATA link to test
5051c6fd2807SJeff Garzik  *
5052936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5053936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5054936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5055c6fd2807SJeff Garzik  *
5056c6fd2807SJeff Garzik  *	LOCKING:
5057c6fd2807SJeff Garzik  *	None.
5058c6fd2807SJeff Garzik  *
5059c6fd2807SJeff Garzik  *	RETURNS:
5060b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5061c6fd2807SJeff Garzik  */
ata_phys_link_offline(struct ata_link * link)5062b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5063c6fd2807SJeff Garzik {
5064c6fd2807SJeff Garzik 	u32 sstatus;
5065c6fd2807SJeff Garzik 
5066936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
50679913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5068b5b3fa38STejun Heo 		return true;
5069b5b3fa38STejun Heo 	return false;
5070c6fd2807SJeff Garzik }
5071c6fd2807SJeff Garzik 
5072b1c72916STejun Heo /**
5073b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5074b1c72916STejun Heo  *	@link: ATA link to test
5075b1c72916STejun Heo  *
5076b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5077b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5078b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5079b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5080b1c72916STejun Heo  *	online.
5081b1c72916STejun Heo  *
5082b1c72916STejun Heo  *	LOCKING:
5083b1c72916STejun Heo  *	None.
5084b1c72916STejun Heo  *
5085b1c72916STejun Heo  *	RETURNS:
5086b1c72916STejun Heo  *	True if the port online status is available and online.
5087b1c72916STejun Heo  */
ata_link_online(struct ata_link * link)5088b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5089b1c72916STejun Heo {
5090b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5091b1c72916STejun Heo 
5092b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5093b1c72916STejun Heo 
5094b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5095b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5096b1c72916STejun Heo }
5097a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_online);
5098b1c72916STejun Heo 
5099b1c72916STejun Heo /**
5100b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5101b1c72916STejun Heo  *	@link: ATA link to test
5102b1c72916STejun Heo  *
5103b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5104b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5105b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5106b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5107b1c72916STejun Heo  *	offline.
5108b1c72916STejun Heo  *
5109b1c72916STejun Heo  *	LOCKING:
5110b1c72916STejun Heo  *	None.
5111b1c72916STejun Heo  *
5112b1c72916STejun Heo  *	RETURNS:
5113b1c72916STejun Heo  *	True if the port offline status is available and offline.
5114b1c72916STejun Heo  */
ata_link_offline(struct ata_link * link)5115b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5116b1c72916STejun Heo {
5117b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5118b1c72916STejun Heo 
5119b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5120b1c72916STejun Heo 
5121b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5122b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5123b1c72916STejun Heo }
5124a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_offline);
5125b1c72916STejun Heo 
51266ffa01d8STejun Heo #ifdef CONFIG_PM
ata_port_request_pm(struct ata_port * ap,pm_message_t mesg,unsigned int action,unsigned int ehi_flags,bool async)5127bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5128cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
5129bc6e7c4bSDan Williams 				bool async)
5130c6fd2807SJeff Garzik {
5131e3667ebfSTejun Heo 	struct ata_link *link;
51325ef41082SLin Ming 	unsigned long flags;
5133c6fd2807SJeff Garzik 
5134c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5135c6fd2807SJeff Garzik 
51363b8e0af4SDamien Le Moal 	/*
51373b8e0af4SDamien Le Moal 	 * A previous PM operation might still be in progress. Wait for
51383b8e0af4SDamien Le Moal 	 * ATA_PFLAG_PM_PENDING to clear.
51393b8e0af4SDamien Le Moal 	 */
51403b8e0af4SDamien Le Moal 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
51413b8e0af4SDamien Le Moal 		spin_unlock_irqrestore(ap->lock, flags);
51423b8e0af4SDamien Le Moal 		ata_port_wait_eh(ap);
51433b8e0af4SDamien Le Moal 		spin_lock_irqsave(ap->lock, flags);
51443b8e0af4SDamien Le Moal 	}
51453b8e0af4SDamien Le Moal 
51463b8e0af4SDamien Le Moal 	/* Request PM operation to EH */
5147c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5148c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
51491eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5150e3667ebfSTejun Heo 		link->eh_info.action |= action;
5151e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5152e3667ebfSTejun Heo 	}
5153c6fd2807SJeff Garzik 
5154c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5155c6fd2807SJeff Garzik 
5156c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5157c6fd2807SJeff Garzik 
51583b8e0af4SDamien Le Moal 	if (!async)
5159c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5160c6fd2807SJeff Garzik }
5161c6fd2807SJeff Garzik 
516233574d68SLin Ming /*
5163bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
5164bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
5165bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
516633574d68SLin Ming  *
516733574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
516833574d68SLin Ming  */
5169bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5170bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
5171bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
5172bc6e7c4bSDan Williams 
ata_port_suspend(struct ata_port * ap,pm_message_t mesg)5173bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5174bc6e7c4bSDan Williams {
51758b4d9469SDamien Le Moal 	/*
51768b4d9469SDamien Le Moal 	 * We are about to suspend the port, so we do not care about
51778b4d9469SDamien Le Moal 	 * scsi_rescan_device() calls scheduled by previous resume operations.
51788b4d9469SDamien Le Moal 	 * The next resume will schedule the rescan again. So cancel any rescan
51798b4d9469SDamien Le Moal 	 * that is not done yet.
51808b4d9469SDamien Le Moal 	 */
51818b4d9469SDamien Le Moal 	cancel_delayed_work_sync(&ap->scsi_rescan_task);
51828b4d9469SDamien Le Moal 
5183bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
51845ef41082SLin Ming }
51855ef41082SLin Ming 
ata_port_suspend_async(struct ata_port * ap,pm_message_t mesg)5186bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5187bc6e7c4bSDan Williams {
51888b4d9469SDamien Le Moal 	/*
51898b4d9469SDamien Le Moal 	 * We are about to suspend the port, so we do not care about
51908b4d9469SDamien Le Moal 	 * scsi_rescan_device() calls scheduled by previous resume operations.
51918b4d9469SDamien Le Moal 	 * The next resume will schedule the rescan again. So cancel any rescan
51928b4d9469SDamien Le Moal 	 * that is not done yet.
51938b4d9469SDamien Le Moal 	 */
51948b4d9469SDamien Le Moal 	cancel_delayed_work_sync(&ap->scsi_rescan_task);
51958b4d9469SDamien Le Moal 
5196bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5197bc6e7c4bSDan Williams }
5198bc6e7c4bSDan Williams 
ata_port_pm_suspend(struct device * dev)5199bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
52002fcbdcb4SDan Williams {
52012fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
52022fcbdcb4SDan Williams 
52035ef41082SLin Ming 	if (pm_runtime_suspended(dev))
52045ef41082SLin Ming 		return 0;
52055ef41082SLin Ming 
5206bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5207f5e6d0d0SAaron Lu 	return 0;
520833574d68SLin Ming }
520933574d68SLin Ming 
ata_port_pm_freeze(struct device * dev)5210bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
52112fcbdcb4SDan Williams {
52122fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
52132fcbdcb4SDan Williams 
5214bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5215bc6e7c4bSDan Williams 		return 0;
5216bc6e7c4bSDan Williams 
5217bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5218bc6e7c4bSDan Williams 	return 0;
52192fcbdcb4SDan Williams }
52202fcbdcb4SDan Williams 
ata_port_pm_poweroff(struct device * dev)5221bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5222e90b1e5aSLin Ming {
5223bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5224bc6e7c4bSDan Williams 	return 0;
5225bc6e7c4bSDan Williams }
5226e90b1e5aSLin Ming 
5227bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5228bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5229bc6e7c4bSDan Williams 
ata_port_resume(struct ata_port * ap,pm_message_t mesg)5230bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5231bc6e7c4bSDan Williams {
5232bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5233bc6e7c4bSDan Williams }
5234bc6e7c4bSDan Williams 
ata_port_resume_async(struct ata_port * ap,pm_message_t mesg)5235bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5236bc6e7c4bSDan Williams {
5237bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5238bc6e7c4bSDan Williams }
5239bc6e7c4bSDan Williams 
ata_port_pm_resume(struct device * dev)5240bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5241bc6e7c4bSDan Williams {
5242200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5243e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5244e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5245e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5246bc6e7c4bSDan Williams 	return 0;
5247e90b1e5aSLin Ming }
5248e90b1e5aSLin Ming 
52497e15e9beSAaron Lu /*
52507e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
52517e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
52527e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
52537e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
52547e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
52557e15e9beSAaron Lu  * ODD is attached to the port.
52567e15e9beSAaron Lu  */
ata_port_runtime_idle(struct device * dev)52579ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
52589ee4f393SLin Ming {
52597e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
52607e15e9beSAaron Lu 	struct ata_link *link;
52617e15e9beSAaron Lu 	struct ata_device *adev;
52627e15e9beSAaron Lu 
52637e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
52647e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
52657e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
52667e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
52677e15e9beSAaron Lu 				return -EBUSY;
52687e15e9beSAaron Lu 	}
52697e15e9beSAaron Lu 
527045f0a85cSRafael J. Wysocki 	return 0;
52719ee4f393SLin Ming }
52729ee4f393SLin Ming 
ata_port_runtime_suspend(struct device * dev)5273a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5274a7ff60dbSAaron Lu {
5275bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5276bc6e7c4bSDan Williams 	return 0;
5277a7ff60dbSAaron Lu }
5278a7ff60dbSAaron Lu 
ata_port_runtime_resume(struct device * dev)5279a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5280a7ff60dbSAaron Lu {
5281bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5282bc6e7c4bSDan Williams 	return 0;
5283a7ff60dbSAaron Lu }
5284a7ff60dbSAaron Lu 
52855ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5286bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5287bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5288bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5289bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5290bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5291bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
52929ee4f393SLin Ming 
5293a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5294a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
52959ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
52965ef41082SLin Ming };
52975ef41082SLin Ming 
52982fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
52992fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
53002fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
53012fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
53022fcbdcb4SDan Williams  */
ata_sas_port_suspend(struct ata_port * ap)5303bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
53042fcbdcb4SDan Williams {
5305bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
53062fcbdcb4SDan Williams }
5307bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
53082fcbdcb4SDan Williams 
ata_sas_port_resume(struct ata_port * ap)5309bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
53102fcbdcb4SDan Williams {
5311bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
53122fcbdcb4SDan Williams }
5313bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
53142fcbdcb4SDan Williams 
5315c6fd2807SJeff Garzik /**
5316cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5317cca3974eSJeff Garzik  *	@host: host to suspend
5318c6fd2807SJeff Garzik  *	@mesg: PM message
5319c6fd2807SJeff Garzik  *
53205ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5321c6fd2807SJeff Garzik  */
ata_host_suspend(struct ata_host * host,pm_message_t mesg)5322ec87cf37SSergey Shtylyov void ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5323c6fd2807SJeff Garzik {
532472ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
5325c6fd2807SJeff Garzik }
5326a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_suspend);
5327c6fd2807SJeff Garzik 
5328c6fd2807SJeff Garzik /**
5329cca3974eSJeff Garzik  *	ata_host_resume - resume host
5330cca3974eSJeff Garzik  *	@host: host to resume
5331c6fd2807SJeff Garzik  *
53325ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5333c6fd2807SJeff Garzik  */
ata_host_resume(struct ata_host * host)5334cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5335c6fd2807SJeff Garzik {
533672ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5337c6fd2807SJeff Garzik }
5338a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_resume);
53396ffa01d8STejun Heo #endif
5340c6fd2807SJeff Garzik 
53418df82c13SBhumika Goyal const struct device_type ata_port_type = {
534275e2bd5fSDamien Le Moal 	.name = ATA_PORT_TYPE_NAME,
53435ef41082SLin Ming #ifdef CONFIG_PM
53445ef41082SLin Ming 	.pm = &ata_port_pm_ops,
53455ef41082SLin Ming #endif
53465ef41082SLin Ming };
53475ef41082SLin Ming 
5348c6fd2807SJeff Garzik /**
5349c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5350c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5351c6fd2807SJeff Garzik  *
5352c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5353c6fd2807SJeff Garzik  *
5354c6fd2807SJeff Garzik  *	LOCKING:
5355c6fd2807SJeff Garzik  *	Inherited from caller.
5356c6fd2807SJeff Garzik  */
ata_dev_init(struct ata_device * dev)5357c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5358c6fd2807SJeff Garzik {
5359b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
53609af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5361c6fd2807SJeff Garzik 	unsigned long flags;
5362c6fd2807SJeff Garzik 
5363b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
53649af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
53659af5c9c9STejun Heo 	link->sata_spd = 0;
5366c6fd2807SJeff Garzik 
5367c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5368c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5369cca3974eSJeff Garzik 	 * host lock.
5370c6fd2807SJeff Garzik 	 */
5371c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5372c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
53733dcc323fSTejun Heo 	dev->horkage = 0;
5374c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5375c6fd2807SJeff Garzik 
537699cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
537799cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5378c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5379c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5380c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5381c6fd2807SJeff Garzik }
5382c6fd2807SJeff Garzik 
5383c6fd2807SJeff Garzik /**
53844fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
53854fb37a25STejun Heo  *	@ap: ATA port link is attached to
53864fb37a25STejun Heo  *	@link: Link structure to initialize
53878989805dSTejun Heo  *	@pmp: Port multiplier port number
53884fb37a25STejun Heo  *
53894fb37a25STejun Heo  *	Initialize @link.
53904fb37a25STejun Heo  *
53914fb37a25STejun Heo  *	LOCKING:
53924fb37a25STejun Heo  *	Kernel thread context (may sleep)
53934fb37a25STejun Heo  */
ata_link_init(struct ata_port * ap,struct ata_link * link,int pmp)5394fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
53954fb37a25STejun Heo {
53964fb37a25STejun Heo 	int i;
53974fb37a25STejun Heo 
53984fb37a25STejun Heo 	/* clear everything except for devices */
5399d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5400d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
54014fb37a25STejun Heo 
54024fb37a25STejun Heo 	link->ap = ap;
54038989805dSTejun Heo 	link->pmp = pmp;
54044fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
54054fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
54064fb37a25STejun Heo 
54074fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
54084fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
54094fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
54104fb37a25STejun Heo 
54114fb37a25STejun Heo 		dev->link = link;
54124fb37a25STejun Heo 		dev->devno = dev - link->device;
5413110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5414110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5415110f66d2STejun Heo #endif
54164fb37a25STejun Heo 		ata_dev_init(dev);
54174fb37a25STejun Heo 	}
54184fb37a25STejun Heo }
54194fb37a25STejun Heo 
54204fb37a25STejun Heo /**
54214fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
54224fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
54234fb37a25STejun Heo  *
5424a31a6997SMauro Carvalho Chehab  *	Initialize ``link->[hw_]sata_spd_limit`` to the currently
54254fb37a25STejun Heo  *	configured value.
54264fb37a25STejun Heo  *
54274fb37a25STejun Heo  *	LOCKING:
54284fb37a25STejun Heo  *	Kernel thread context (may sleep).
54294fb37a25STejun Heo  *
54304fb37a25STejun Heo  *	RETURNS:
54314fb37a25STejun Heo  *	0 on success, -errno on failure.
54324fb37a25STejun Heo  */
sata_link_init_spd(struct ata_link * link)5433fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
54344fb37a25STejun Heo {
543533267325STejun Heo 	u8 spd;
54364fb37a25STejun Heo 	int rc;
54374fb37a25STejun Heo 
5438d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
54394fb37a25STejun Heo 	if (rc)
54404fb37a25STejun Heo 		return rc;
54414fb37a25STejun Heo 
5442d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
54434fb37a25STejun Heo 	if (spd)
54444fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
54454fb37a25STejun Heo 
544605944bdfSTejun Heo 	ata_force_link_limits(link);
544733267325STejun Heo 
54484fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
54494fb37a25STejun Heo 
54504fb37a25STejun Heo 	return 0;
54514fb37a25STejun Heo }
54524fb37a25STejun Heo 
54534fb37a25STejun Heo /**
5454f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5455f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5456c6fd2807SJeff Garzik  *
5457f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5458f3187195STejun Heo  *
5459f3187195STejun Heo  *	RETURNS:
5460f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5461c6fd2807SJeff Garzik  *
5462c6fd2807SJeff Garzik  *	LOCKING:
5463f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5464c6fd2807SJeff Garzik  */
ata_port_alloc(struct ata_host * host)5465f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5466c6fd2807SJeff Garzik {
5467f3187195STejun Heo 	struct ata_port *ap;
5468c6fd2807SJeff Garzik 
5469f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5470f3187195STejun Heo 	if (!ap)
5471f3187195STejun Heo 		return NULL;
5472f3187195STejun Heo 
54737b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5474cca3974eSJeff Garzik 	ap->lock = &host->lock;
5475f3187195STejun Heo 	ap->print_id = -1;
5476e628dc99SDavid Milburn 	ap->local_port_no = -1;
5477cca3974eSJeff Garzik 	ap->host = host;
5478f3187195STejun Heo 	ap->dev = host->dev;
5479c6fd2807SJeff Garzik 
5480ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
548165f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
54826aa0365aSDamien Le Moal 	INIT_DELAYED_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5483c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5484c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
548545fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
5486b93ab338SKees Cook 	timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5487b93ab338SKees Cook 		    TIMER_DEFERRABLE);
5488c6fd2807SJeff Garzik 
5489c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5490c6fd2807SJeff Garzik 
54918989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5492c6fd2807SJeff Garzik 
5493c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5494c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5495c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5496c6fd2807SJeff Garzik #endif
5497270390e1STejun Heo 	ata_sff_port_init(ap);
5498270390e1STejun Heo 
5499c6fd2807SJeff Garzik 	return ap;
5500c6fd2807SJeff Garzik }
5501c6fd2807SJeff Garzik 
ata_devres_release(struct device * gendev,void * res)55022623c7a5STaras Kondratiuk static void ata_devres_release(struct device *gendev, void *res)
5503f0d36efdSTejun Heo {
5504f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5505f0d36efdSTejun Heo 	int i;
5506f0d36efdSTejun Heo 
5507f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5508f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5509f0d36efdSTejun Heo 
5510ecef7253STejun Heo 		if (!ap)
5511ecef7253STejun Heo 			continue;
5512ecef7253STejun Heo 
55134911487aSTejun Heo 		if (ap->scsi_host)
55141aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
55151aa506e4STejun Heo 
55162623c7a5STaras Kondratiuk 	}
55172623c7a5STaras Kondratiuk 
55182623c7a5STaras Kondratiuk 	dev_set_drvdata(gendev, NULL);
55192623c7a5STaras Kondratiuk 	ata_host_put(host);
55202623c7a5STaras Kondratiuk }
55212623c7a5STaras Kondratiuk 
ata_host_release(struct kref * kref)55222623c7a5STaras Kondratiuk static void ata_host_release(struct kref *kref)
55232623c7a5STaras Kondratiuk {
55242623c7a5STaras Kondratiuk 	struct ata_host *host = container_of(kref, struct ata_host, kref);
55252623c7a5STaras Kondratiuk 	int i;
55262623c7a5STaras Kondratiuk 
55272623c7a5STaras Kondratiuk 	for (i = 0; i < host->n_ports; i++) {
55282623c7a5STaras Kondratiuk 		struct ata_port *ap = host->ports[i];
55292623c7a5STaras Kondratiuk 
5530633273a3STejun Heo 		kfree(ap->pmp_link);
5531b1c72916STejun Heo 		kfree(ap->slave_link);
553218bd7718SNiklas Cassel 		kfree(ap->ncq_sense_buf);
55334911487aSTejun Heo 		kfree(ap);
55341aa506e4STejun Heo 		host->ports[i] = NULL;
55351aa506e4STejun Heo 	}
55362623c7a5STaras Kondratiuk 	kfree(host);
55372623c7a5STaras Kondratiuk }
55381aa506e4STejun Heo 
ata_host_get(struct ata_host * host)55392623c7a5STaras Kondratiuk void ata_host_get(struct ata_host *host)
55402623c7a5STaras Kondratiuk {
55412623c7a5STaras Kondratiuk 	kref_get(&host->kref);
55422623c7a5STaras Kondratiuk }
55432623c7a5STaras Kondratiuk 
ata_host_put(struct ata_host * host)55442623c7a5STaras Kondratiuk void ata_host_put(struct ata_host *host)
55452623c7a5STaras Kondratiuk {
55462623c7a5STaras Kondratiuk 	kref_put(&host->kref, ata_host_release);
5547f0d36efdSTejun Heo }
5548a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_put);
5549f0d36efdSTejun Heo 
5550c6fd2807SJeff Garzik /**
5551f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5552f3187195STejun Heo  *	@dev: generic device this host is associated with
5553f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5554f3187195STejun Heo  *
5555f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5556f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5557f3187195STejun Heo  *	attaches it using ata_host_register().
5558f3187195STejun Heo  *
5559f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5560f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5561f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5562f3187195STejun Heo  *	ports will be automatically freed on registration.
5563f3187195STejun Heo  *
5564f3187195STejun Heo  *	RETURNS:
5565f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5566f3187195STejun Heo  *
5567f3187195STejun Heo  *	LOCKING:
5568f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5569f3187195STejun Heo  */
ata_host_alloc(struct device * dev,int max_ports)5570f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5571f3187195STejun Heo {
5572f3187195STejun Heo 	struct ata_host *host;
5573f3187195STejun Heo 	size_t sz;
5574f3187195STejun Heo 	int i;
55752623c7a5STaras Kondratiuk 	void *dr;
5576f3187195STejun Heo 
5577f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5578f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
55792623c7a5STaras Kondratiuk 	host = kzalloc(sz, GFP_KERNEL);
5580f3187195STejun Heo 	if (!host)
55812623c7a5STaras Kondratiuk 		return NULL;
55822623c7a5STaras Kondratiuk 
5583f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5584dafd6c49SColin Ian King 		goto err_free;
5585f3187195STejun Heo 
55862623c7a5STaras Kondratiuk 	dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
55872623c7a5STaras Kondratiuk 	if (!dr)
5588f3187195STejun Heo 		goto err_out;
5589f3187195STejun Heo 
55902623c7a5STaras Kondratiuk 	devres_add(dev, dr);
5591f3187195STejun Heo 	dev_set_drvdata(dev, host);
5592f3187195STejun Heo 
5593f3187195STejun Heo 	spin_lock_init(&host->lock);
5594c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5595f3187195STejun Heo 	host->dev = dev;
5596f3187195STejun Heo 	host->n_ports = max_ports;
55972623c7a5STaras Kondratiuk 	kref_init(&host->kref);
5598f3187195STejun Heo 
5599f3187195STejun Heo 	/* allocate ports bound to this host */
5600f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5601f3187195STejun Heo 		struct ata_port *ap;
5602f3187195STejun Heo 
5603f3187195STejun Heo 		ap = ata_port_alloc(host);
5604f3187195STejun Heo 		if (!ap)
5605f3187195STejun Heo 			goto err_out;
5606f3187195STejun Heo 
5607f3187195STejun Heo 		ap->port_no = i;
5608f3187195STejun Heo 		host->ports[i] = ap;
5609f3187195STejun Heo 	}
5610f3187195STejun Heo 
5611f3187195STejun Heo 	devres_remove_group(dev, NULL);
5612f3187195STejun Heo 	return host;
5613f3187195STejun Heo 
5614f3187195STejun Heo  err_out:
5615f3187195STejun Heo 	devres_release_group(dev, NULL);
5616dafd6c49SColin Ian King  err_free:
5617dafd6c49SColin Ian King 	kfree(host);
5618f3187195STejun Heo 	return NULL;
5619f3187195STejun Heo }
5620a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc);
5621f3187195STejun Heo 
5622f3187195STejun Heo /**
5623f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5624f5cda257STejun Heo  *	@dev: generic device this host is associated with
5625f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5626f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5627f5cda257STejun Heo  *
5628f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5629f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5630f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5631f5cda257STejun Heo  *
5632f5cda257STejun Heo  *	RETURNS:
5633f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5634f5cda257STejun Heo  *
5635f5cda257STejun Heo  *	LOCKING:
5636f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5637f5cda257STejun Heo  */
ata_host_alloc_pinfo(struct device * dev,const struct ata_port_info * const * ppi,int n_ports)5638f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5639f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5640f5cda257STejun Heo 				      int n_ports)
5641f5cda257STejun Heo {
5642bf476fe2SSergey Shtylyov 	const struct ata_port_info *pi = &ata_dummy_port_info;
5643f5cda257STejun Heo 	struct ata_host *host;
5644f5cda257STejun Heo 	int i, j;
5645f5cda257STejun Heo 
5646f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5647f5cda257STejun Heo 	if (!host)
5648f5cda257STejun Heo 		return NULL;
5649f5cda257STejun Heo 
5650bf476fe2SSergey Shtylyov 	for (i = 0, j = 0; i < host->n_ports; i++) {
5651f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5652f5cda257STejun Heo 
5653f5cda257STejun Heo 		if (ppi[j])
5654f5cda257STejun Heo 			pi = ppi[j++];
5655f5cda257STejun Heo 
5656f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5657f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5658f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5659f5cda257STejun Heo 		ap->flags |= pi->flags;
56600c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5661f5cda257STejun Heo 		ap->ops = pi->port_ops;
5662f5cda257STejun Heo 
5663f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5664f5cda257STejun Heo 			host->ops = pi->port_ops;
5665f5cda257STejun Heo 	}
5666f5cda257STejun Heo 
5667f5cda257STejun Heo 	return host;
5668f5cda257STejun Heo }
5669a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
5670f5cda257STejun Heo 
ata_host_stop(struct device * gendev,void * res)567132ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
567232ebbc0cSTejun Heo {
567332ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
567432ebbc0cSTejun Heo 	int i;
567532ebbc0cSTejun Heo 
567632ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
567732ebbc0cSTejun Heo 
567832ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
567932ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
568032ebbc0cSTejun Heo 
568132ebbc0cSTejun Heo 		if (ap->ops->port_stop)
568232ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
568332ebbc0cSTejun Heo 	}
568432ebbc0cSTejun Heo 
568532ebbc0cSTejun Heo 	if (host->ops->host_stop)
568632ebbc0cSTejun Heo 		host->ops->host_stop(host);
568732ebbc0cSTejun Heo }
568832ebbc0cSTejun Heo 
5689f5cda257STejun Heo /**
5690029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5691029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5692029cfd6bSTejun Heo  *
5693029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5694029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5695029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5696029cfd6bSTejun Heo  *	inheritance chain.
5697029cfd6bSTejun Heo  *
5698029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5699029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5700029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5701029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5702029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5703029cfd6bSTejun Heo  *
5704029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5705029cfd6bSTejun Heo  *
5706029cfd6bSTejun Heo  *	LOCKING:
5707029cfd6bSTejun Heo  *	None.
5708029cfd6bSTejun Heo  */
ata_finalize_port_ops(struct ata_port_operations * ops)5709029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5710029cfd6bSTejun Heo {
57112da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5712029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5713029cfd6bSTejun Heo 	void **begin = (void **)ops;
5714029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5715029cfd6bSTejun Heo 	void **pp;
5716029cfd6bSTejun Heo 
5717029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5718029cfd6bSTejun Heo 		return;
5719029cfd6bSTejun Heo 
5720029cfd6bSTejun Heo 	spin_lock(&lock);
5721029cfd6bSTejun Heo 
5722029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5723029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5724029cfd6bSTejun Heo 
5725029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5726029cfd6bSTejun Heo 			if (!*pp)
5727029cfd6bSTejun Heo 				*pp = *inherit;
5728029cfd6bSTejun Heo 	}
5729029cfd6bSTejun Heo 
5730029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5731029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5732029cfd6bSTejun Heo 			*pp = NULL;
5733029cfd6bSTejun Heo 
5734029cfd6bSTejun Heo 	ops->inherits = NULL;
5735029cfd6bSTejun Heo 
5736029cfd6bSTejun Heo 	spin_unlock(&lock);
5737029cfd6bSTejun Heo }
5738029cfd6bSTejun Heo 
5739029cfd6bSTejun Heo /**
5740ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5741ecef7253STejun Heo  *	@host: ATA host to start ports for
5742ecef7253STejun Heo  *
5743ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5744ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5745ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5746e0af10acSDiego Viola  *	once.  If host->ops is not initialized yet, it is set to the
5747f3187195STejun Heo  *	first non-dummy port ops.
5748ecef7253STejun Heo  *
5749ecef7253STejun Heo  *	LOCKING:
5750ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5751ecef7253STejun Heo  *
5752ecef7253STejun Heo  *	RETURNS:
5753ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5754ecef7253STejun Heo  */
ata_host_start(struct ata_host * host)5755ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5756ecef7253STejun Heo {
575732ebbc0cSTejun Heo 	int have_stop = 0;
575832ebbc0cSTejun Heo 	void *start_dr = NULL;
5759ecef7253STejun Heo 	int i, rc;
5760ecef7253STejun Heo 
5761ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5762ecef7253STejun Heo 		return 0;
5763ecef7253STejun Heo 
5764029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5765029cfd6bSTejun Heo 
5766ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5767ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5768ecef7253STejun Heo 
5769029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5770029cfd6bSTejun Heo 
5771f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5772f3187195STejun Heo 			host->ops = ap->ops;
5773f3187195STejun Heo 
577432ebbc0cSTejun Heo 		if (ap->ops->port_stop)
577532ebbc0cSTejun Heo 			have_stop = 1;
577632ebbc0cSTejun Heo 	}
577732ebbc0cSTejun Heo 
5778355a8031SDamien Le Moal 	if (host->ops && host->ops->host_stop)
577932ebbc0cSTejun Heo 		have_stop = 1;
578032ebbc0cSTejun Heo 
578132ebbc0cSTejun Heo 	if (have_stop) {
578232ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
578332ebbc0cSTejun Heo 		if (!start_dr)
578432ebbc0cSTejun Heo 			return -ENOMEM;
578532ebbc0cSTejun Heo 	}
578632ebbc0cSTejun Heo 
578732ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
578832ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
578932ebbc0cSTejun Heo 
5790ecef7253STejun Heo 		if (ap->ops->port_start) {
5791ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5792ecef7253STejun Heo 			if (rc) {
57930f9fe9b7SAlan Cox 				if (rc != -ENODEV)
5794a44fec1fSJoe Perches 					dev_err(host->dev,
5795a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
5796a44fec1fSJoe Perches 						i, rc);
5797ecef7253STejun Heo 				goto err_out;
5798ecef7253STejun Heo 			}
5799ecef7253STejun Heo 		}
5800ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5801ecef7253STejun Heo 	}
5802ecef7253STejun Heo 
580332ebbc0cSTejun Heo 	if (start_dr)
580432ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5805ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5806ecef7253STejun Heo 	return 0;
5807ecef7253STejun Heo 
5808ecef7253STejun Heo  err_out:
5809ecef7253STejun Heo 	while (--i >= 0) {
5810ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5811ecef7253STejun Heo 
5812ecef7253STejun Heo 		if (ap->ops->port_stop)
5813ecef7253STejun Heo 			ap->ops->port_stop(ap);
5814ecef7253STejun Heo 	}
581532ebbc0cSTejun Heo 	devres_free(start_dr);
5816ecef7253STejun Heo 	return rc;
5817ecef7253STejun Heo }
5818a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_start);
5819ecef7253STejun Heo 
5820ecef7253STejun Heo /**
582194bd5719SMauro Carvalho Chehab  *	ata_host_init - Initialize a host struct for sas (ipr, libsas)
5822cca3974eSJeff Garzik  *	@host:	host to initialize
5823cca3974eSJeff Garzik  *	@dev:	device host is attached to
5824c6fd2807SJeff Garzik  *	@ops:	port_ops
5825c6fd2807SJeff Garzik  *
5826c6fd2807SJeff Garzik  */
ata_host_init(struct ata_host * host,struct device * dev,struct ata_port_operations * ops)5827cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
58288d8e7d13SDan Williams 		   struct ata_port_operations *ops)
5829c6fd2807SJeff Garzik {
5830cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5831c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
583269278f79SJens Axboe 	host->n_tags = ATA_MAX_QUEUE;
5833cca3974eSJeff Garzik 	host->dev = dev;
5834cca3974eSJeff Garzik 	host->ops = ops;
58352fa4a326SJason Yan 	kref_init(&host->kref);
5836c6fd2807SJeff Garzik }
5837a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_init);
5838c6fd2807SJeff Garzik 
ata_port_probe(struct ata_port * ap)5839a76f1b63SHannes Reinecke void ata_port_probe(struct ata_port *ap)
584079318057SArjan van de Ven {
584179318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
584279318057SArjan van de Ven 	unsigned long flags;
584379318057SArjan van de Ven 
584479318057SArjan van de Ven 	/* kick EH for boot probing */
584579318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
584679318057SArjan van de Ven 
584779318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
58486b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
584979318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
585079318057SArjan van de Ven 
585179318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
585279318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
585379318057SArjan van de Ven 	ata_port_schedule_eh(ap);
585479318057SArjan van de Ven 
585579318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
58569508a66fSDan Williams }
5857a76f1b63SHannes Reinecke EXPORT_SYMBOL_GPL(ata_port_probe);
585879318057SArjan van de Ven 
async_port_probe(void * data,async_cookie_t cookie)5859238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
5860238c9cf9SJames Bottomley {
5861238c9cf9SJames Bottomley 	struct ata_port *ap = data;
5862238c9cf9SJames Bottomley 
5863238c9cf9SJames Bottomley 	/*
5864238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
5865238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
5866238c9cf9SJames Bottomley 	 * before going further.
5867238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
5868238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
586979318057SArjan van de Ven 	 */
5870238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5871238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
5872238c9cf9SJames Bottomley 
5873a76f1b63SHannes Reinecke 	ata_port_probe(ap);
587477461a3fSHannes Reinecke 	ata_port_wait_eh(ap);
5875f29d3b23SArjan van de Ven 
5876f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
5877f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
5878f29d3b23SArjan van de Ven 
5879f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
588079318057SArjan van de Ven }
5881238c9cf9SJames Bottomley 
5882c6fd2807SJeff Garzik /**
5883f3187195STejun Heo  *	ata_host_register - register initialized ATA host
5884f3187195STejun Heo  *	@host: ATA host to register
5885f3187195STejun Heo  *	@sht: template for SCSI host
5886c6fd2807SJeff Garzik  *
5887f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
5888f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
5889f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
5890f3187195STejun Heo  *	probe registered devices.
5891c6fd2807SJeff Garzik  *
5892c6fd2807SJeff Garzik  *	LOCKING:
5893f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5894c6fd2807SJeff Garzik  *
5895c6fd2807SJeff Garzik  *	RETURNS:
5896f3187195STejun Heo  *	0 on success, -errno otherwise.
5897c6fd2807SJeff Garzik  */
ata_host_register(struct ata_host * host,const struct scsi_host_template * sht)589825df73d9SBart Van Assche int ata_host_register(struct ata_host *host, const struct scsi_host_template *sht)
5899c6fd2807SJeff Garzik {
5900f3187195STejun Heo 	int i, rc;
5901c6fd2807SJeff Garzik 
590269278f79SJens Axboe 	host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
59031871ee13SKevin Hao 
5904f3187195STejun Heo 	/* host must have been started */
5905f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
5906a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
5907f3187195STejun Heo 		WARN_ON(1);
5908f3187195STejun Heo 		return -EINVAL;
590902f076aaSAlan Cox 	}
5910f0d36efdSTejun Heo 
5911f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
5912f3187195STejun Heo 	 * determine the exact number of ports to allocate at
5913f3187195STejun Heo 	 * allocation time.
5914f3187195STejun Heo 	 */
5915f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
5916f3187195STejun Heo 		kfree(host->ports[i]);
5917f0d36efdSTejun Heo 
5918f3187195STejun Heo 	/* give ports names and add SCSI hosts */
5919e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
592085d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
5921e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
5922e628dc99SDavid Milburn 	}
5923d9027470SGwendal Grignou 
5924d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
5925d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
5926d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
5927d9027470SGwendal Grignou 		if (rc) {
5928d9027470SGwendal Grignou 			goto err_tadd;
5929d9027470SGwendal Grignou 		}
5930d9027470SGwendal Grignou 	}
5931d9027470SGwendal Grignou 
5932f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
5933ecef7253STejun Heo 	if (rc)
5934d9027470SGwendal Grignou 		goto err_tadd;
5935ecef7253STejun Heo 
5936f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
5937cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5938cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5939f0a6d77bSSergey Shtylyov 		unsigned int xfer_mask;
5940f3187195STejun Heo 
5941f3187195STejun Heo 		/* set SATA cable type if still unset */
5942f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5943f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
5944c6fd2807SJeff Garzik 
5945c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
59464fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
5947b1c72916STejun Heo 		if (ap->slave_link)
5948b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
5949c6fd2807SJeff Garzik 
5950cbcdd875STejun Heo 		/* print per-port info to dmesg */
5951f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5952f3187195STejun Heo 					      ap->udma_mask);
5953f3187195STejun Heo 
5954abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
5955a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
5956a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5957f3187195STejun Heo 				      ata_mode_string(xfer_mask),
5958cbcdd875STejun Heo 				      ap->link.eh_info.desc);
5959abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
5960abf6e8edSTejun Heo 		} else
5961a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
5962c6fd2807SJeff Garzik 	}
5963c6fd2807SJeff Garzik 
5964f6005354SVegard Nossum 	/* perform each probe asynchronously */
5965f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5966f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
5967b5292111SKai-Heng Feng 		ap->cookie = async_schedule(async_port_probe, ap);
5968c6fd2807SJeff Garzik 	}
5969c6fd2807SJeff Garzik 
5970f3187195STejun Heo 	return 0;
5971d9027470SGwendal Grignou 
5972d9027470SGwendal Grignou  err_tadd:
5973d9027470SGwendal Grignou 	while (--i >= 0) {
5974d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
5975d9027470SGwendal Grignou 	}
5976d9027470SGwendal Grignou 	return rc;
5977d9027470SGwendal Grignou 
5978f3187195STejun Heo }
5979a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_register);
5980f3187195STejun Heo 
5981f3187195STejun Heo /**
5982f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
5983f5cda257STejun Heo  *	@host: target ATA host
5984f5cda257STejun Heo  *	@irq: IRQ to request
5985f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
5986f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
5987f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
5988f5cda257STejun Heo  *
5989f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
5990f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
5991c9b5560aSMasanari Iida  *	request IRQ and register it.  This helper takes necessary
5992f5cda257STejun Heo  *	arguments and performs the three steps in one go.
5993f5cda257STejun Heo  *
59943d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
59953d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
59963d46b2e2SPaul Mundt  *	should be NULL.
59973d46b2e2SPaul Mundt  *
5998f5cda257STejun Heo  *	LOCKING:
5999f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6000f5cda257STejun Heo  *
6001f5cda257STejun Heo  *	RETURNS:
6002f5cda257STejun Heo  *	0 on success, -errno otherwise.
6003f5cda257STejun Heo  */
ata_host_activate(struct ata_host * host,int irq,irq_handler_t irq_handler,unsigned long irq_flags,const struct scsi_host_template * sht)6004f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6005f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
600625df73d9SBart Van Assche 		      const struct scsi_host_template *sht)
6007f5cda257STejun Heo {
6008cbcdd875STejun Heo 	int i, rc;
60097e22c002SHeiner Kallweit 	char *irq_desc;
6010f5cda257STejun Heo 
6011f5cda257STejun Heo 	rc = ata_host_start(host);
6012f5cda257STejun Heo 	if (rc)
6013f5cda257STejun Heo 		return rc;
6014f5cda257STejun Heo 
60153d46b2e2SPaul Mundt 	/* Special case for polling mode */
60163d46b2e2SPaul Mundt 	if (!irq) {
60173d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
60183d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
60193d46b2e2SPaul Mundt 	}
60203d46b2e2SPaul Mundt 
60217e22c002SHeiner Kallweit 	irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
60227e22c002SHeiner Kallweit 				  dev_driver_string(host->dev),
60237e22c002SHeiner Kallweit 				  dev_name(host->dev));
60247e22c002SHeiner Kallweit 	if (!irq_desc)
60257e22c002SHeiner Kallweit 		return -ENOMEM;
60267e22c002SHeiner Kallweit 
6027f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
60287e22c002SHeiner Kallweit 			      irq_desc, host);
6029f5cda257STejun Heo 	if (rc)
6030f5cda257STejun Heo 		return rc;
6031f5cda257STejun Heo 
6032cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6033cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
60344031826bSTejun Heo 
6035f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6036f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6037f5cda257STejun Heo 	if (rc)
6038f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6039f5cda257STejun Heo 
6040f5cda257STejun Heo 	return rc;
6041f5cda257STejun Heo }
6042a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_activate);
6043f5cda257STejun Heo 
6044f5cda257STejun Heo /**
6045c9b5560aSMasanari Iida  *	ata_port_detach - Detach ATA port in preparation of device removal
6046c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6047c6fd2807SJeff Garzik  *
6048c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6049c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6050c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6051c6fd2807SJeff Garzik  *
6052c6fd2807SJeff Garzik  *	LOCKING:
6053c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6054c6fd2807SJeff Garzik  */
ata_port_detach(struct ata_port * ap)6055741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6056c6fd2807SJeff Garzik {
6057c6fd2807SJeff Garzik 	unsigned long flags;
6058a6f9bf4dSLevente Kurusa 	struct ata_link *link;
6059a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
6060c6fd2807SJeff Garzik 
606184d76529SDamien Le Moal 	/* Wait for any ongoing EH */
606284d76529SDamien Le Moal 	ata_port_wait_eh(ap);
606384d76529SDamien Le Moal 
606484d76529SDamien Le Moal 	mutex_lock(&ap->scsi_scan_mutex);
6065c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
606684d76529SDamien Le Moal 
606784d76529SDamien Le Moal 	/* Remove scsi devices */
606884d76529SDamien Le Moal 	ata_for_each_link(link, ap, HOST_FIRST) {
606984d76529SDamien Le Moal 		ata_for_each_dev(dev, link, ALL) {
607084d76529SDamien Le Moal 			if (dev->sdev) {
607184d76529SDamien Le Moal 				spin_unlock_irqrestore(ap->lock, flags);
607284d76529SDamien Le Moal 				scsi_remove_device(dev->sdev);
607384d76529SDamien Le Moal 				spin_lock_irqsave(ap->lock, flags);
607484d76529SDamien Le Moal 				dev->sdev = NULL;
607584d76529SDamien Le Moal 			}
607684d76529SDamien Le Moal 		}
607784d76529SDamien Le Moal 	}
607884d76529SDamien Le Moal 
607984d76529SDamien Le Moal 	/* Tell EH to disable all devices */
6080c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6081ece180d1STejun Heo 	ata_port_schedule_eh(ap);
608284d76529SDamien Le Moal 
6083c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
608484d76529SDamien Le Moal 	mutex_unlock(&ap->scsi_scan_mutex);
6085c6fd2807SJeff Garzik 
6086ece180d1STejun Heo 	/* wait till EH commits suicide */
6087c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6088c6fd2807SJeff Garzik 
6089ece180d1STejun Heo 	/* it better be dead now */
6090ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6091c6fd2807SJeff Garzik 
6092afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
60936aa0365aSDamien Le Moal 	cancel_delayed_work_sync(&ap->scsi_rescan_task);
6094c6fd2807SJeff Garzik 
6095a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
6096a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
6097a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
6098a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
6099a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
6100a6f9bf4dSLevente Kurusa 		}
6101a6f9bf4dSLevente Kurusa 	}
6102d9027470SGwendal Grignou 	if (ap->pmp_link) {
6103d9027470SGwendal Grignou 		int i;
6104d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6105d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
6106d9027470SGwendal Grignou 	}
6107c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6108cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6109c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
6110c6fd2807SJeff Garzik }
6111c6fd2807SJeff Garzik 
6112c6fd2807SJeff Garzik /**
61130529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
61140529c159STejun Heo  *	@host: Host to detach
61150529c159STejun Heo  *
61160529c159STejun Heo  *	Detach all ports of @host.
61170529c159STejun Heo  *
61180529c159STejun Heo  *	LOCKING:
61190529c159STejun Heo  *	Kernel thread context (may sleep).
61200529c159STejun Heo  */
ata_host_detach(struct ata_host * host)61210529c159STejun Heo void ata_host_detach(struct ata_host *host)
61220529c159STejun Heo {
61230529c159STejun Heo 	int i;
61240529c159STejun Heo 
6125b5292111SKai-Heng Feng 	for (i = 0; i < host->n_ports; i++) {
6126130f4cafSJohn Garry 		/* Ensure ata_port probe has completed */
6127b5292111SKai-Heng Feng 		async_synchronize_cookie(host->ports[i]->cookie + 1);
61280529c159STejun Heo 		ata_port_detach(host->ports[i]);
6129b5292111SKai-Heng Feng 	}
6130562f0c2dSTejun Heo 
6131562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6132562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
61330529c159STejun Heo }
6134a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_detach);
61350529c159STejun Heo 
6136c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6137c6fd2807SJeff Garzik 
6138c6fd2807SJeff Garzik /**
6139c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6140c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6141c6fd2807SJeff Garzik  *
6142b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6143b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6144b878ca5dSTejun Heo  *	release is handled via devres.
6145c6fd2807SJeff Garzik  *
6146c6fd2807SJeff Garzik  *	LOCKING:
6147c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6148c6fd2807SJeff Garzik  */
ata_pci_remove_one(struct pci_dev * pdev)6149c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6150c6fd2807SJeff Garzik {
615104a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6152c6fd2807SJeff Garzik 
6153f0d36efdSTejun Heo 	ata_host_detach(host);
6154c6fd2807SJeff Garzik }
6155a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_remove_one);
6156c6fd2807SJeff Garzik 
ata_pci_shutdown_one(struct pci_dev * pdev)615710a663a1SPrabhakar Kushwaha void ata_pci_shutdown_one(struct pci_dev *pdev)
615810a663a1SPrabhakar Kushwaha {
615910a663a1SPrabhakar Kushwaha 	struct ata_host *host = pci_get_drvdata(pdev);
616010a663a1SPrabhakar Kushwaha 	int i;
616110a663a1SPrabhakar Kushwaha 
616210a663a1SPrabhakar Kushwaha 	for (i = 0; i < host->n_ports; i++) {
616310a663a1SPrabhakar Kushwaha 		struct ata_port *ap = host->ports[i];
616410a663a1SPrabhakar Kushwaha 
616510a663a1SPrabhakar Kushwaha 		ap->pflags |= ATA_PFLAG_FROZEN;
616610a663a1SPrabhakar Kushwaha 
616710a663a1SPrabhakar Kushwaha 		/* Disable port interrupts */
616810a663a1SPrabhakar Kushwaha 		if (ap->ops->freeze)
616910a663a1SPrabhakar Kushwaha 			ap->ops->freeze(ap);
617010a663a1SPrabhakar Kushwaha 
617110a663a1SPrabhakar Kushwaha 		/* Stop the port DMA engines */
617210a663a1SPrabhakar Kushwaha 		if (ap->ops->port_stop)
617310a663a1SPrabhakar Kushwaha 			ap->ops->port_stop(ap);
617410a663a1SPrabhakar Kushwaha 	}
617510a663a1SPrabhakar Kushwaha }
6176a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
617710a663a1SPrabhakar Kushwaha 
6178c6fd2807SJeff Garzik /* move to PCI subsystem */
pci_test_config_bits(struct pci_dev * pdev,const struct pci_bits * bits)6179c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6180c6fd2807SJeff Garzik {
6181c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6182c6fd2807SJeff Garzik 
6183c6fd2807SJeff Garzik 	switch (bits->width) {
6184c6fd2807SJeff Garzik 	case 1: {
6185c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6186c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6187c6fd2807SJeff Garzik 		tmp = tmp8;
6188c6fd2807SJeff Garzik 		break;
6189c6fd2807SJeff Garzik 	}
6190c6fd2807SJeff Garzik 	case 2: {
6191c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6192c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6193c6fd2807SJeff Garzik 		tmp = tmp16;
6194c6fd2807SJeff Garzik 		break;
6195c6fd2807SJeff Garzik 	}
6196c6fd2807SJeff Garzik 	case 4: {
6197c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6198c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6199c6fd2807SJeff Garzik 		tmp = tmp32;
6200c6fd2807SJeff Garzik 		break;
6201c6fd2807SJeff Garzik 	}
6202c6fd2807SJeff Garzik 
6203c6fd2807SJeff Garzik 	default:
6204c6fd2807SJeff Garzik 		return -EINVAL;
6205c6fd2807SJeff Garzik 	}
6206c6fd2807SJeff Garzik 
6207c6fd2807SJeff Garzik 	tmp &= bits->mask;
6208c6fd2807SJeff Garzik 
6209c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6210c6fd2807SJeff Garzik }
6211a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(pci_test_config_bits);
6212c6fd2807SJeff Garzik 
62136ffa01d8STejun Heo #ifdef CONFIG_PM
ata_pci_device_do_suspend(struct pci_dev * pdev,pm_message_t mesg)6214c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6215c6fd2807SJeff Garzik {
6216c6fd2807SJeff Garzik 	pci_save_state(pdev);
6217c6fd2807SJeff Garzik 	pci_disable_device(pdev);
62184c90d971STejun Heo 
62193a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6220c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6221c6fd2807SJeff Garzik }
6222a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6223c6fd2807SJeff Garzik 
ata_pci_device_do_resume(struct pci_dev * pdev)6224553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6225c6fd2807SJeff Garzik {
6226553c4aa6STejun Heo 	int rc;
6227553c4aa6STejun Heo 
6228c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6229c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6230553c4aa6STejun Heo 
6231f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6232553c4aa6STejun Heo 	if (rc) {
6233a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6234553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6235553c4aa6STejun Heo 		return rc;
6236553c4aa6STejun Heo 	}
6237553c4aa6STejun Heo 
6238c6fd2807SJeff Garzik 	pci_set_master(pdev);
6239553c4aa6STejun Heo 	return 0;
6240c6fd2807SJeff Garzik }
6241a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6242c6fd2807SJeff Garzik 
ata_pci_device_suspend(struct pci_dev * pdev,pm_message_t mesg)6243c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6244c6fd2807SJeff Garzik {
624504a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6246c6fd2807SJeff Garzik 
6247ec87cf37SSergey Shtylyov 	ata_host_suspend(host, mesg);
6248c6fd2807SJeff Garzik 
6249c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6250c6fd2807SJeff Garzik 
6251c6fd2807SJeff Garzik 	return 0;
6252c6fd2807SJeff Garzik }
6253a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6254c6fd2807SJeff Garzik 
ata_pci_device_resume(struct pci_dev * pdev)6255c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6256c6fd2807SJeff Garzik {
625704a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6258553c4aa6STejun Heo 	int rc;
6259c6fd2807SJeff Garzik 
6260553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6261553c4aa6STejun Heo 	if (rc == 0)
6262cca3974eSJeff Garzik 		ata_host_resume(host);
6263553c4aa6STejun Heo 	return rc;
6264c6fd2807SJeff Garzik }
6265a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_resume);
62666ffa01d8STejun Heo #endif /* CONFIG_PM */
6267c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6268c6fd2807SJeff Garzik 
6269b7db04d9SBrian Norris /**
6270b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6271b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6272b7db04d9SBrian Norris  *
6273b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6274b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6275b7db04d9SBrian Norris  *	release is handled via devres.
6276b7db04d9SBrian Norris  *
6277b7db04d9SBrian Norris  *	LOCKING:
6278b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6279b7db04d9SBrian Norris  */
ata_platform_remove_one(struct platform_device * pdev)6280a7eb54d4SUwe Kleine-König void ata_platform_remove_one(struct platform_device *pdev)
6281b7db04d9SBrian Norris {
6282b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6283b7db04d9SBrian Norris 
6284b7db04d9SBrian Norris 	ata_host_detach(host);
6285b7db04d9SBrian Norris }
6286a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_platform_remove_one);
6287b7db04d9SBrian Norris 
6288bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
6289168af4afSDamien Le Moal 
6290168af4afSDamien Le Moal #define force_cbl(name, flag)				\
6291168af4afSDamien Le Moal 	{ #name,	.cbl		= (flag) }
6292168af4afSDamien Le Moal 
6293168af4afSDamien Le Moal #define force_spd_limit(spd, val)			\
6294168af4afSDamien Le Moal 	{ #spd,	.spd_limit		= (val) }
6295168af4afSDamien Le Moal 
6296168af4afSDamien Le Moal #define force_xfer(mode, shift)				\
6297168af4afSDamien Le Moal 	{ #mode,	.xfer_mask	= (1UL << (shift)) }
6298168af4afSDamien Le Moal 
62993af9ca4dSDamien Le Moal #define force_lflag_on(name, flags)			\
63003af9ca4dSDamien Le Moal 	{ #name,	.lflags_on	= (flags) }
63013af9ca4dSDamien Le Moal 
63023af9ca4dSDamien Le Moal #define force_lflag_onoff(name, flags)			\
63033af9ca4dSDamien Le Moal 	{ "no" #name,	.lflags_on	= (flags) },	\
63043af9ca4dSDamien Le Moal 	{ #name,	.lflags_off	= (flags) }
6305168af4afSDamien Le Moal 
6306168af4afSDamien Le Moal #define force_horkage_on(name, flag)			\
6307168af4afSDamien Le Moal 	{ #name,	.horkage_on	= (flag) }
6308168af4afSDamien Le Moal 
6309168af4afSDamien Le Moal #define force_horkage_onoff(name, flag)			\
6310168af4afSDamien Le Moal 	{ "no" #name,	.horkage_on	= (flag) },	\
6311168af4afSDamien Le Moal 	{ #name,	.horkage_off	= (flag) }
6312168af4afSDamien Le Moal 
6313168af4afSDamien Le Moal static const struct ata_force_param force_tbl[] __initconst = {
6314168af4afSDamien Le Moal 	force_cbl(40c,			ATA_CBL_PATA40),
6315168af4afSDamien Le Moal 	force_cbl(80c,			ATA_CBL_PATA80),
6316168af4afSDamien Le Moal 	force_cbl(short40c,		ATA_CBL_PATA40_SHORT),
6317168af4afSDamien Le Moal 	force_cbl(unk,			ATA_CBL_PATA_UNK),
6318168af4afSDamien Le Moal 	force_cbl(ign,			ATA_CBL_PATA_IGN),
6319168af4afSDamien Le Moal 	force_cbl(sata,			ATA_CBL_SATA),
6320168af4afSDamien Le Moal 
6321168af4afSDamien Le Moal 	force_spd_limit(1.5Gbps,	1),
6322168af4afSDamien Le Moal 	force_spd_limit(3.0Gbps,	2),
6323168af4afSDamien Le Moal 
6324168af4afSDamien Le Moal 	force_xfer(pio0,		ATA_SHIFT_PIO + 0),
6325168af4afSDamien Le Moal 	force_xfer(pio1,		ATA_SHIFT_PIO + 1),
6326168af4afSDamien Le Moal 	force_xfer(pio2,		ATA_SHIFT_PIO + 2),
6327168af4afSDamien Le Moal 	force_xfer(pio3,		ATA_SHIFT_PIO + 3),
6328168af4afSDamien Le Moal 	force_xfer(pio4,		ATA_SHIFT_PIO + 4),
6329168af4afSDamien Le Moal 	force_xfer(pio5,		ATA_SHIFT_PIO + 5),
6330168af4afSDamien Le Moal 	force_xfer(pio6,		ATA_SHIFT_PIO + 6),
6331168af4afSDamien Le Moal 	force_xfer(mwdma0,		ATA_SHIFT_MWDMA + 0),
6332168af4afSDamien Le Moal 	force_xfer(mwdma1,		ATA_SHIFT_MWDMA + 1),
6333168af4afSDamien Le Moal 	force_xfer(mwdma2,		ATA_SHIFT_MWDMA + 2),
6334168af4afSDamien Le Moal 	force_xfer(mwdma3,		ATA_SHIFT_MWDMA + 3),
6335168af4afSDamien Le Moal 	force_xfer(mwdma4,		ATA_SHIFT_MWDMA + 4),
6336168af4afSDamien Le Moal 	force_xfer(udma0,		ATA_SHIFT_UDMA + 0),
6337168af4afSDamien Le Moal 	force_xfer(udma16,		ATA_SHIFT_UDMA + 0),
6338168af4afSDamien Le Moal 	force_xfer(udma/16,		ATA_SHIFT_UDMA + 0),
6339168af4afSDamien Le Moal 	force_xfer(udma1,		ATA_SHIFT_UDMA + 1),
6340168af4afSDamien Le Moal 	force_xfer(udma25,		ATA_SHIFT_UDMA + 1),
6341168af4afSDamien Le Moal 	force_xfer(udma/25,		ATA_SHIFT_UDMA + 1),
6342168af4afSDamien Le Moal 	force_xfer(udma2,		ATA_SHIFT_UDMA + 2),
6343168af4afSDamien Le Moal 	force_xfer(udma33,		ATA_SHIFT_UDMA + 2),
6344168af4afSDamien Le Moal 	force_xfer(udma/33,		ATA_SHIFT_UDMA + 2),
6345168af4afSDamien Le Moal 	force_xfer(udma3,		ATA_SHIFT_UDMA + 3),
6346168af4afSDamien Le Moal 	force_xfer(udma44,		ATA_SHIFT_UDMA + 3),
6347168af4afSDamien Le Moal 	force_xfer(udma/44,		ATA_SHIFT_UDMA + 3),
6348168af4afSDamien Le Moal 	force_xfer(udma4,		ATA_SHIFT_UDMA + 4),
6349168af4afSDamien Le Moal 	force_xfer(udma66,		ATA_SHIFT_UDMA + 4),
6350168af4afSDamien Le Moal 	force_xfer(udma/66,		ATA_SHIFT_UDMA + 4),
6351168af4afSDamien Le Moal 	force_xfer(udma5,		ATA_SHIFT_UDMA + 5),
6352168af4afSDamien Le Moal 	force_xfer(udma100,		ATA_SHIFT_UDMA + 5),
6353168af4afSDamien Le Moal 	force_xfer(udma/100,		ATA_SHIFT_UDMA + 5),
6354168af4afSDamien Le Moal 	force_xfer(udma6,		ATA_SHIFT_UDMA + 6),
6355168af4afSDamien Le Moal 	force_xfer(udma133,		ATA_SHIFT_UDMA + 6),
6356168af4afSDamien Le Moal 	force_xfer(udma/133,		ATA_SHIFT_UDMA + 6),
6357168af4afSDamien Le Moal 	force_xfer(udma7,		ATA_SHIFT_UDMA + 7),
6358168af4afSDamien Le Moal 
63593af9ca4dSDamien Le Moal 	force_lflag_on(nohrst,		ATA_LFLAG_NO_HRST),
63603af9ca4dSDamien Le Moal 	force_lflag_on(nosrst,		ATA_LFLAG_NO_SRST),
63613af9ca4dSDamien Le Moal 	force_lflag_on(norst,		ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST),
63623af9ca4dSDamien Le Moal 	force_lflag_on(rstonce,		ATA_LFLAG_RST_ONCE),
63633af9ca4dSDamien Le Moal 	force_lflag_onoff(dbdelay,	ATA_LFLAG_NO_DEBOUNCE_DELAY),
6364168af4afSDamien Le Moal 
6365168af4afSDamien Le Moal 	force_horkage_onoff(ncq,	ATA_HORKAGE_NONCQ),
6366168af4afSDamien Le Moal 	force_horkage_onoff(ncqtrim,	ATA_HORKAGE_NO_NCQ_TRIM),
6367168af4afSDamien Le Moal 	force_horkage_onoff(ncqati,	ATA_HORKAGE_NO_NCQ_ON_ATI),
6368168af4afSDamien Le Moal 
63692c33bbdaSDamien Le Moal 	force_horkage_onoff(trim,	ATA_HORKAGE_NOTRIM),
63702c33bbdaSDamien Le Moal 	force_horkage_on(trim_zero,	ATA_HORKAGE_ZERO_AFTER_TRIM),
63712c33bbdaSDamien Le Moal 	force_horkage_on(max_trim_128m, ATA_HORKAGE_MAX_TRIM_128M),
63722c33bbdaSDamien Le Moal 
63732c33bbdaSDamien Le Moal 	force_horkage_onoff(dma,	ATA_HORKAGE_NODMA),
6374168af4afSDamien Le Moal 	force_horkage_on(atapi_dmadir,	ATA_HORKAGE_ATAPI_DMADIR),
63752c33bbdaSDamien Le Moal 	force_horkage_on(atapi_mod16_dma, ATA_HORKAGE_ATAPI_MOD16_DMA),
63762c33bbdaSDamien Le Moal 
63772c33bbdaSDamien Le Moal 	force_horkage_onoff(dmalog,	ATA_HORKAGE_NO_DMA_LOG),
63782c33bbdaSDamien Le Moal 	force_horkage_onoff(iddevlog,	ATA_HORKAGE_NO_ID_DEV_LOG),
63792c33bbdaSDamien Le Moal 	force_horkage_onoff(logdir,	ATA_HORKAGE_NO_LOG_DIR),
63802c33bbdaSDamien Le Moal 
63812c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_128,	ATA_HORKAGE_MAX_SEC_128),
63822c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_1024,	ATA_HORKAGE_MAX_SEC_1024),
63832c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_lba48,	ATA_HORKAGE_MAX_SEC_LBA48),
63842c33bbdaSDamien Le Moal 
63852c33bbdaSDamien Le Moal 	force_horkage_onoff(lpm,	ATA_HORKAGE_NOLPM),
63862c33bbdaSDamien Le Moal 	force_horkage_onoff(setxfer,	ATA_HORKAGE_NOSETXFER),
63872c33bbdaSDamien Le Moal 	force_horkage_on(dump_id,	ATA_HORKAGE_DUMP_ID),
63884d2e4980SDamien Le Moal 	force_horkage_onoff(fua,	ATA_HORKAGE_NO_FUA),
63892c33bbdaSDamien Le Moal 
63902c33bbdaSDamien Le Moal 	force_horkage_on(disable,	ATA_HORKAGE_DISABLE),
6391168af4afSDamien Le Moal };
6392168af4afSDamien Le Moal 
ata_parse_force_one(char ** cur,struct ata_force_ent * force_ent,const char ** reason)639333267325STejun Heo static int __init ata_parse_force_one(char **cur,
639433267325STejun Heo 				      struct ata_force_ent *force_ent,
639533267325STejun Heo 				      const char **reason)
639633267325STejun Heo {
639733267325STejun Heo 	char *start = *cur, *p = *cur;
639833267325STejun Heo 	char *id, *val, *endp;
639933267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
640033267325STejun Heo 	int nr_matches = 0, i;
640133267325STejun Heo 
640233267325STejun Heo 	/* find where this param ends and update *cur */
640333267325STejun Heo 	while (*p != '\0' && *p != ',')
640433267325STejun Heo 		p++;
640533267325STejun Heo 
640633267325STejun Heo 	if (*p == '\0')
640733267325STejun Heo 		*cur = p;
640833267325STejun Heo 	else
640933267325STejun Heo 		*cur = p + 1;
641033267325STejun Heo 
641133267325STejun Heo 	*p = '\0';
641233267325STejun Heo 
641333267325STejun Heo 	/* parse */
641433267325STejun Heo 	p = strchr(start, ':');
641533267325STejun Heo 	if (!p) {
641633267325STejun Heo 		val = strstrip(start);
641733267325STejun Heo 		goto parse_val;
641833267325STejun Heo 	}
641933267325STejun Heo 	*p = '\0';
642033267325STejun Heo 
642133267325STejun Heo 	id = strstrip(start);
642233267325STejun Heo 	val = strstrip(p + 1);
642333267325STejun Heo 
642433267325STejun Heo 	/* parse id */
642533267325STejun Heo 	p = strchr(id, '.');
642633267325STejun Heo 	if (p) {
642733267325STejun Heo 		*p++ = '\0';
642833267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
642933267325STejun Heo 		if (p == endp || *endp != '\0') {
643033267325STejun Heo 			*reason = "invalid device";
643133267325STejun Heo 			return -EINVAL;
643233267325STejun Heo 		}
643333267325STejun Heo 	}
643433267325STejun Heo 
643533267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
6436f7cf69aeSTejun Heo 	if (id == endp || *endp != '\0') {
643733267325STejun Heo 		*reason = "invalid port/link";
643833267325STejun Heo 		return -EINVAL;
643933267325STejun Heo 	}
644033267325STejun Heo 
644133267325STejun Heo  parse_val:
644233267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
644333267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
644433267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
644533267325STejun Heo 
644633267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
644733267325STejun Heo 			continue;
644833267325STejun Heo 
644933267325STejun Heo 		nr_matches++;
645033267325STejun Heo 		match_fp = fp;
645133267325STejun Heo 
645233267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
645333267325STejun Heo 			nr_matches = 1;
645433267325STejun Heo 			break;
645533267325STejun Heo 		}
645633267325STejun Heo 	}
645733267325STejun Heo 
645833267325STejun Heo 	if (!nr_matches) {
645933267325STejun Heo 		*reason = "unknown value";
646033267325STejun Heo 		return -EINVAL;
646133267325STejun Heo 	}
646233267325STejun Heo 	if (nr_matches > 1) {
64639de55351SArvind Yadav 		*reason = "ambiguous value";
646433267325STejun Heo 		return -EINVAL;
646533267325STejun Heo 	}
646633267325STejun Heo 
646733267325STejun Heo 	force_ent->param = *match_fp;
646833267325STejun Heo 
646933267325STejun Heo 	return 0;
647033267325STejun Heo }
647133267325STejun Heo 
ata_parse_force_param(void)647233267325STejun Heo static void __init ata_parse_force_param(void)
647333267325STejun Heo {
647433267325STejun Heo 	int idx = 0, size = 1;
647533267325STejun Heo 	int last_port = -1, last_device = -1;
647633267325STejun Heo 	char *p, *cur, *next;
647733267325STejun Heo 
6478168af4afSDamien Le Moal 	/* Calculate maximum number of params and allocate ata_force_tbl */
647933267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
648033267325STejun Heo 		if (*p == ',')
648133267325STejun Heo 			size++;
648233267325STejun Heo 
64836396bb22SKees Cook 	ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
648433267325STejun Heo 	if (!ata_force_tbl) {
648533267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
648633267325STejun Heo 		       "libata.force ignored\n");
648733267325STejun Heo 		return;
648833267325STejun Heo 	}
648933267325STejun Heo 
649033267325STejun Heo 	/* parse and populate the table */
649133267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
649233267325STejun Heo 		const char *reason = "";
649333267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
649433267325STejun Heo 
649533267325STejun Heo 		next = cur;
649633267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
649733267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
649833267325STejun Heo 			       "parameter \"%s\" (%s)\n",
649933267325STejun Heo 			       cur, reason);
650033267325STejun Heo 			continue;
650133267325STejun Heo 		}
650233267325STejun Heo 
650333267325STejun Heo 		if (te.port == -1) {
650433267325STejun Heo 			te.port = last_port;
650533267325STejun Heo 			te.device = last_device;
650633267325STejun Heo 		}
650733267325STejun Heo 
650833267325STejun Heo 		ata_force_tbl[idx++] = te;
650933267325STejun Heo 
651033267325STejun Heo 		last_port = te.port;
651133267325STejun Heo 		last_device = te.device;
651233267325STejun Heo 	}
651333267325STejun Heo 
651433267325STejun Heo 	ata_force_tbl_size = idx;
651533267325STejun Heo }
6516c6fd2807SJeff Garzik 
ata_free_force_param(void)6517bf89b0bfSBartlomiej Zolnierkiewicz static void ata_free_force_param(void)
6518bf89b0bfSBartlomiej Zolnierkiewicz {
6519bf89b0bfSBartlomiej Zolnierkiewicz 	kfree(ata_force_tbl);
6520bf89b0bfSBartlomiej Zolnierkiewicz }
6521bf89b0bfSBartlomiej Zolnierkiewicz #else
ata_parse_force_param(void)6522bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_parse_force_param(void) { }
ata_free_force_param(void)6523bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_free_force_param(void) { }
6524bf89b0bfSBartlomiej Zolnierkiewicz #endif
6525bf89b0bfSBartlomiej Zolnierkiewicz 
ata_init(void)6526c6fd2807SJeff Garzik static int __init ata_init(void)
6527c6fd2807SJeff Garzik {
6528d9027470SGwendal Grignou 	int rc;
6529270390e1STejun Heo 
653033267325STejun Heo 	ata_parse_force_param();
653133267325STejun Heo 
6532270390e1STejun Heo 	rc = ata_sff_init();
6533ad72cf98STejun Heo 	if (rc) {
6534bf89b0bfSBartlomiej Zolnierkiewicz 		ata_free_force_param();
6535ad72cf98STejun Heo 		return rc;
6536ad72cf98STejun Heo 	}
6537c6fd2807SJeff Garzik 
6538d9027470SGwendal Grignou 	libata_transport_init();
6539d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6540d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6541d9027470SGwendal Grignou 		ata_sff_exit();
6542d9027470SGwendal Grignou 		rc = -ENOMEM;
6543d9027470SGwendal Grignou 		goto err_out;
6544d9027470SGwendal Grignou 	}
6545d9027470SGwendal Grignou 
6546c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6547c6fd2807SJeff Garzik 	return 0;
6548d9027470SGwendal Grignou 
6549d9027470SGwendal Grignou err_out:
6550d9027470SGwendal Grignou 	return rc;
6551c6fd2807SJeff Garzik }
6552c6fd2807SJeff Garzik 
ata_exit(void)6553c6fd2807SJeff Garzik static void __exit ata_exit(void)
6554c6fd2807SJeff Garzik {
6555d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6556d9027470SGwendal Grignou 	libata_transport_exit();
6557270390e1STejun Heo 	ata_sff_exit();
6558bf89b0bfSBartlomiej Zolnierkiewicz 	ata_free_force_param();
6559c6fd2807SJeff Garzik }
6560c6fd2807SJeff Garzik 
6561a4625085SBrian King subsys_initcall(ata_init);
6562c6fd2807SJeff Garzik module_exit(ata_exit);
6563c6fd2807SJeff Garzik 
65649990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6565c6fd2807SJeff Garzik 
ata_ratelimit(void)6566c6fd2807SJeff Garzik int ata_ratelimit(void)
6567c6fd2807SJeff Garzik {
65689990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6569c6fd2807SJeff Garzik }
6570a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ratelimit);
6571c6fd2807SJeff Garzik 
6572c0c362b6STejun Heo /**
6573c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6574c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6575c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6576c0c362b6STejun Heo  *
6577c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6578c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6579c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6580c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6581c0c362b6STejun Heo  *	sleeping.
6582c0c362b6STejun Heo  *
6583c0c362b6STejun Heo  *	LOCKING:
6584c0c362b6STejun Heo  *	Might sleep.
6585c0c362b6STejun Heo  */
ata_msleep(struct ata_port * ap,unsigned int msecs)658697750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
658797750cebSTejun Heo {
6588c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6589c0c362b6STejun Heo 
6590c0c362b6STejun Heo 	if (owns_eh)
6591c0c362b6STejun Heo 		ata_eh_release(ap);
6592c0c362b6STejun Heo 
6593848c3920SAnil Veliyankara Madam 	if (msecs < 20) {
6594848c3920SAnil Veliyankara Madam 		unsigned long usecs = msecs * USEC_PER_MSEC;
6595848c3920SAnil Veliyankara Madam 		usleep_range(usecs, usecs + 50);
6596848c3920SAnil Veliyankara Madam 	} else {
659797750cebSTejun Heo 		msleep(msecs);
6598848c3920SAnil Veliyankara Madam 	}
6599c0c362b6STejun Heo 
6600c0c362b6STejun Heo 	if (owns_eh)
6601c0c362b6STejun Heo 		ata_eh_acquire(ap);
660297750cebSTejun Heo }
6603a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_msleep);
660497750cebSTejun Heo 
6605c6fd2807SJeff Garzik /**
6606c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
660797750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6608c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6609c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6610c6fd2807SJeff Garzik  *	@val: Wait condition
6611341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6612341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6613c6fd2807SJeff Garzik  *
6614c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6615c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6616c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6617c6fd2807SJeff Garzik  *
6618c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6619c6fd2807SJeff Garzik  *
6620c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6621c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6622c6fd2807SJeff Garzik  *
6623c6fd2807SJeff Garzik  *	LOCKING:
6624c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6625c6fd2807SJeff Garzik  *
6626c6fd2807SJeff Garzik  *	RETURNS:
6627c6fd2807SJeff Garzik  *	The final register value.
6628c6fd2807SJeff Garzik  */
ata_wait_register(struct ata_port * ap,void __iomem * reg,u32 mask,u32 val,unsigned int interval,unsigned int timeout)662997750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
663084abed36SSergey Shtylyov 		      unsigned int interval, unsigned int timeout)
6631c6fd2807SJeff Garzik {
6632341c2c95STejun Heo 	unsigned long deadline;
6633c6fd2807SJeff Garzik 	u32 tmp;
6634c6fd2807SJeff Garzik 
6635c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6636c6fd2807SJeff Garzik 
6637c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6638c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6639c6fd2807SJeff Garzik 	 * eat away the timeout.
6640c6fd2807SJeff Garzik 	 */
6641341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6642c6fd2807SJeff Garzik 
6643341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
664497750cebSTejun Heo 		ata_msleep(ap, interval);
6645c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6646c6fd2807SJeff Garzik 	}
6647c6fd2807SJeff Garzik 
6648c6fd2807SJeff Garzik 	return tmp;
6649c6fd2807SJeff Garzik }
6650a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_register);
6651c6fd2807SJeff Garzik 
6652c6fd2807SJeff Garzik /*
6653c6fd2807SJeff Garzik  * Dummy port_ops
6654c6fd2807SJeff Garzik  */
ata_dummy_qc_issue(struct ata_queued_cmd * qc)6655c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6656c6fd2807SJeff Garzik {
6657c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6658c6fd2807SJeff Garzik }
6659c6fd2807SJeff Garzik 
ata_dummy_error_handler(struct ata_port * ap)6660182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6661182d7bbaSTejun Heo {
6662182d7bbaSTejun Heo 	/* truly dummy */
6663182d7bbaSTejun Heo }
6664182d7bbaSTejun Heo 
6665029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6666c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6667c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6668182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6669e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
6670e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
6671c6fd2807SJeff Garzik };
6672a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
6673c6fd2807SJeff Garzik 
667421b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
667521b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
667621b0ad4fSTejun Heo };
6677a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_info);
667821b0ad4fSTejun Heo 
ata_print_version(const struct device * dev,const char * version)667906296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
668006296a1eSJoe Perches {
668106296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
668206296a1eSJoe Perches }
668306296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
6684c206a389SHannes Reinecke 
6685c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_tf_load);
6686c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_exec_command);
6687c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_setup);
6688c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_start);
6689c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_status);
6690