xref: /openbmc/linux/drivers/ata/libata-core.c (revision d633b8a7)
1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c6fd2807SJeff Garzik /*
3c6fd2807SJeff Garzik  *  libata-core.c - helper library for ATA
4c6fd2807SJeff Garzik  *
5c6fd2807SJeff Garzik  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
6c6fd2807SJeff Garzik  *  Copyright 2003-2004 Jeff Garzik
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
919285f3cSMauro Carvalho Chehab  *  as Documentation/driver-api/libata.rst
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
12c6fd2807SJeff Garzik  *  http://www.sata-io.org/
13c6fd2807SJeff Garzik  *
1492c52c52SAlan Cox  *  Standards documents from:
1592c52c52SAlan Cox  *	http://www.t13.org (ATA standards, PCI DMA IDE spec)
1692c52c52SAlan Cox  *	http://www.t10.org (SCSI MMC - for ATAPI MMC)
1792c52c52SAlan Cox  *	http://www.sata-io.org (SATA)
1892c52c52SAlan Cox  *	http://www.compactflash.org (CF)
1992c52c52SAlan Cox  *	http://www.qic.org (QIC157 - Tape and DSC)
2092c52c52SAlan Cox  *	http://www.ce-ata.org (CE-ATA: not supported)
21a52fbcfcSBartlomiej Zolnierkiewicz  *
22a52fbcfcSBartlomiej Zolnierkiewicz  * libata is essentially a library of internal helper functions for
23a52fbcfcSBartlomiej Zolnierkiewicz  * low-level ATA host controller drivers.  As such, the API/ABI is
24a52fbcfcSBartlomiej Zolnierkiewicz  * likely to change as new drivers are added and updated.
25a52fbcfcSBartlomiej Zolnierkiewicz  * Do not depend on ABI/API stability.
26c6fd2807SJeff Garzik  */
27c6fd2807SJeff Garzik 
28c6fd2807SJeff Garzik #include <linux/kernel.h>
29c6fd2807SJeff Garzik #include <linux/module.h>
30c6fd2807SJeff Garzik #include <linux/pci.h>
31c6fd2807SJeff Garzik #include <linux/init.h>
32c6fd2807SJeff Garzik #include <linux/list.h>
33c6fd2807SJeff Garzik #include <linux/mm.h>
34c6fd2807SJeff Garzik #include <linux/spinlock.h>
35c6fd2807SJeff Garzik #include <linux/blkdev.h>
36c6fd2807SJeff Garzik #include <linux/delay.h>
37c6fd2807SJeff Garzik #include <linux/timer.h>
38848c3920SAnil Veliyankara Madam #include <linux/time.h>
39c6fd2807SJeff Garzik #include <linux/interrupt.h>
40c6fd2807SJeff Garzik #include <linux/completion.h>
41c6fd2807SJeff Garzik #include <linux/suspend.h>
42c6fd2807SJeff Garzik #include <linux/workqueue.h>
43c6fd2807SJeff Garzik #include <linux/scatterlist.h>
442dcb407eSJeff Garzik #include <linux/io.h>
45e18086d6SMark Lord #include <linux/log2.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
47428ac5fcSGeorge Spelvin #include <linux/glob.h>
48c6fd2807SJeff Garzik #include <scsi/scsi.h>
49c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
50c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
51c6fd2807SJeff Garzik #include <linux/libata.h>
52c6fd2807SJeff Garzik #include <asm/byteorder.h>
53fe5af0ccSHannes Reinecke #include <asm/unaligned.h>
54140b5e59STejun Heo #include <linux/cdrom.h>
559990b6f3SAkinobu Mita #include <linux/ratelimit.h>
56eb25cb99SStephan Linz #include <linux/leds.h>
579ee4f393SLin Ming #include <linux/pm_runtime.h>
58b7db04d9SBrian Norris #include <linux/platform_device.h>
59bbf5a097SBartlomiej Zolnierkiewicz #include <asm/setup.h>
60c6fd2807SJeff Garzik 
61255c03d1SHannes Reinecke #define CREATE_TRACE_POINTS
62255c03d1SHannes Reinecke #include <trace/events/libata.h>
63255c03d1SHannes Reinecke 
64c6fd2807SJeff Garzik #include "libata.h"
65d9027470SGwendal Grignou #include "libata-transport.h"
66fda0efc5SJeff Garzik 
67029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
680aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
69203c75b8STejun Heo 	.postreset		= ata_std_postreset,
70a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
71e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
72e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
73029cfd6bSTejun Heo };
74029cfd6bSTejun Heo 
75029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
76029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
77029cfd6bSTejun Heo 
78029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
7957c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
80029cfd6bSTejun Heo };
81a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(sata_port_ops);
82029cfd6bSTejun Heo 
83c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
84c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
85c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
86c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
8775683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
88c6fd2807SJeff Garzik 
89a78f57afSTero Roponen atomic_t ata_print_id = ATOMIC_INIT(0);
90c6fd2807SJeff Garzik 
91bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
9233267325STejun Heo struct ata_force_param {
9333267325STejun Heo 	const char	*name;
948ba5a45cSBartlomiej Zolnierkiewicz 	u8		cbl;
958ba5a45cSBartlomiej Zolnierkiewicz 	u8		spd_limit;
9633267325STejun Heo 	unsigned long	xfer_mask;
9733267325STejun Heo 	unsigned int	horkage_on;
9833267325STejun Heo 	unsigned int	horkage_off;
998ba5a45cSBartlomiej Zolnierkiewicz 	u16		lflags;
10033267325STejun Heo };
10133267325STejun Heo 
10233267325STejun Heo struct ata_force_ent {
10333267325STejun Heo 	int			port;
10433267325STejun Heo 	int			device;
10533267325STejun Heo 	struct ata_force_param	param;
10633267325STejun Heo };
10733267325STejun Heo 
10833267325STejun Heo static struct ata_force_ent *ata_force_tbl;
10933267325STejun Heo static int ata_force_tbl_size;
11033267325STejun Heo 
111bbf5a097SBartlomiej Zolnierkiewicz static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
1127afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1137afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
1148c27ceffSMauro Carvalho Chehab MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
115bf89b0bfSBartlomiej Zolnierkiewicz #endif
11633267325STejun Heo 
1172486fa56STejun Heo static int atapi_enabled = 1;
118c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
119ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
120c6fd2807SJeff Garzik 
121c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
122c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
123ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
124c6fd2807SJeff Garzik 
125baf4fdfaSMark Lord int atapi_passthru16 = 1;
126baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
127ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
128baf4fdfaSMark Lord 
129c6fd2807SJeff Garzik int libata_fua = 0;
130c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
131ad5d8eacSEvgeni Golov MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
132c6fd2807SJeff Garzik 
1332dcb407eSJeff Garzik static int ata_ignore_hpa;
1341e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1351e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1361e999736SAlan Cox 
137b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
138b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
139b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
140b3a70601SAlan Cox 
14187fbc5a0STejun Heo static int ata_probe_timeout;
142c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
143c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
144c6fd2807SJeff Garzik 
1456ebe9d86SJeff Garzik int libata_noacpi = 0;
146d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
147ad5d8eacSEvgeni Golov MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
14811ef697bSKristen Carlson Accardi 
149ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
150ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
151ad5d8eacSEvgeni Golov MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
152ae8d4ee7SAlan Cox 
153e7ecd435STejun Heo static int atapi_an;
154e7ecd435STejun Heo module_param(atapi_an, int, 0444);
155e7ecd435STejun Heo MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
156e7ecd435STejun Heo 
157c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
158c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
159c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
160c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
161c6fd2807SJeff Garzik 
162891fd7c6SDamien Le Moal static inline bool ata_dev_print_info(struct ata_device *dev)
163891fd7c6SDamien Le Moal {
164891fd7c6SDamien Le Moal 	struct ata_eh_context *ehc = &dev->link->eh_context;
165891fd7c6SDamien Le Moal 
166891fd7c6SDamien Le Moal 	return ehc->i.flags & ATA_EHI_PRINTINFO;
167891fd7c6SDamien Le Moal }
168c6fd2807SJeff Garzik 
1699913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1709913ff8aSTejun Heo {
1719913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1729913ff8aSTejun Heo }
1739913ff8aSTejun Heo 
1741eca4365STejun Heo /**
1751eca4365STejun Heo  *	ata_link_next - link iteration helper
1761eca4365STejun Heo  *	@link: the previous link, NULL to start
1771eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1781eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
179aadffb68STejun Heo  *
180aadffb68STejun Heo  *	LOCKING:
181aadffb68STejun Heo  *	Host lock or EH context.
1821eca4365STejun Heo  *
1831eca4365STejun Heo  *	RETURNS:
1841eca4365STejun Heo  *	Pointer to the next link.
185aadffb68STejun Heo  */
1861eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1871eca4365STejun Heo 			       enum ata_link_iter_mode mode)
188aadffb68STejun Heo {
1891eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
1901eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
1911eca4365STejun Heo 
192aadffb68STejun Heo 	/* NULL link indicates start of iteration */
1931eca4365STejun Heo 	if (!link)
1941eca4365STejun Heo 		switch (mode) {
1951eca4365STejun Heo 		case ATA_LITER_EDGE:
1961eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
1971eca4365STejun Heo 			if (sata_pmp_attached(ap))
198aadffb68STejun Heo 				return ap->pmp_link;
199df561f66SGustavo A. R. Silva 			fallthrough;
2001eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
201aadffb68STejun Heo 			return &ap->link;
202aadffb68STejun Heo 		}
203aadffb68STejun Heo 
2041eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2051eca4365STejun Heo 	if (link == &ap->link)
2061eca4365STejun Heo 		switch (mode) {
2071eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2081eca4365STejun Heo 			if (sata_pmp_attached(ap))
209aadffb68STejun Heo 				return ap->pmp_link;
210df561f66SGustavo A. R. Silva 			fallthrough;
2111eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2121eca4365STejun Heo 			if (unlikely(ap->slave_link))
2131eca4365STejun Heo 				return ap->slave_link;
214df561f66SGustavo A. R. Silva 			fallthrough;
2151eca4365STejun Heo 		case ATA_LITER_EDGE:
2161eca4365STejun Heo 			return NULL;
217aadffb68STejun Heo 		}
218aadffb68STejun Heo 
219b1c72916STejun Heo 	/* slave_link excludes PMP */
220b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
221b1c72916STejun Heo 		return NULL;
222b1c72916STejun Heo 
2231eca4365STejun Heo 	/* we were over a PMP link */
224aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
225aadffb68STejun Heo 		return link;
2261eca4365STejun Heo 
2271eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2281eca4365STejun Heo 		return &ap->link;
2291eca4365STejun Heo 
230aadffb68STejun Heo 	return NULL;
231aadffb68STejun Heo }
232a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_next);
233aadffb68STejun Heo 
234c6fd2807SJeff Garzik /**
2351eca4365STejun Heo  *	ata_dev_next - device iteration helper
2361eca4365STejun Heo  *	@dev: the previous device, NULL to start
2371eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2381eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2391eca4365STejun Heo  *
2401eca4365STejun Heo  *	LOCKING:
2411eca4365STejun Heo  *	Host lock or EH context.
2421eca4365STejun Heo  *
2431eca4365STejun Heo  *	RETURNS:
2441eca4365STejun Heo  *	Pointer to the next device.
2451eca4365STejun Heo  */
2461eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2471eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2481eca4365STejun Heo {
2491eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2501eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2511eca4365STejun Heo 
2521eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2531eca4365STejun Heo 	if (!dev)
2541eca4365STejun Heo 		switch (mode) {
2551eca4365STejun Heo 		case ATA_DITER_ENABLED:
2561eca4365STejun Heo 		case ATA_DITER_ALL:
2571eca4365STejun Heo 			dev = link->device;
2581eca4365STejun Heo 			goto check;
2591eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2601eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2611eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2621eca4365STejun Heo 			goto check;
2631eca4365STejun Heo 		}
2641eca4365STejun Heo 
2651eca4365STejun Heo  next:
2661eca4365STejun Heo 	/* move to the next one */
2671eca4365STejun Heo 	switch (mode) {
2681eca4365STejun Heo 	case ATA_DITER_ENABLED:
2691eca4365STejun Heo 	case ATA_DITER_ALL:
2701eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2711eca4365STejun Heo 			goto check;
2721eca4365STejun Heo 		return NULL;
2731eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2741eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2751eca4365STejun Heo 		if (--dev >= link->device)
2761eca4365STejun Heo 			goto check;
2771eca4365STejun Heo 		return NULL;
2781eca4365STejun Heo 	}
2791eca4365STejun Heo 
2801eca4365STejun Heo  check:
2811eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2821eca4365STejun Heo 	    !ata_dev_enabled(dev))
2831eca4365STejun Heo 		goto next;
2841eca4365STejun Heo 	return dev;
2851eca4365STejun Heo }
286a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_next);
2871eca4365STejun Heo 
2881eca4365STejun Heo /**
289b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
290b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
291b1c72916STejun Heo  *
292b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
293b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
294b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
295b1c72916STejun Heo  *
296b1c72916STejun Heo  *	LOCKING:
297b1c72916STejun Heo  *	Don't care.
298b1c72916STejun Heo  *
299b1c72916STejun Heo  *	RETURNS:
300b1c72916STejun Heo  *	Pointer to the found physical link.
301b1c72916STejun Heo  */
302b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
303b1c72916STejun Heo {
304b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
305b1c72916STejun Heo 
306b1c72916STejun Heo 	if (!ap->slave_link)
307b1c72916STejun Heo 		return dev->link;
308b1c72916STejun Heo 	if (!dev->devno)
309b1c72916STejun Heo 		return &ap->link;
310b1c72916STejun Heo 	return ap->slave_link;
311b1c72916STejun Heo }
312b1c72916STejun Heo 
313bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
314b1c72916STejun Heo /**
31533267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3164cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
31733267325STejun Heo  *
31833267325STejun Heo  *	Force cable type according to libata.force and whine about it.
31933267325STejun Heo  *	The last entry which has matching port number is used, so it
32033267325STejun Heo  *	can be specified as part of device force parameters.  For
32133267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
32233267325STejun Heo  *	same effect.
32333267325STejun Heo  *
32433267325STejun Heo  *	LOCKING:
32533267325STejun Heo  *	EH context.
32633267325STejun Heo  */
32733267325STejun Heo void ata_force_cbl(struct ata_port *ap)
32833267325STejun Heo {
32933267325STejun Heo 	int i;
33033267325STejun Heo 
33133267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
33233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
33333267325STejun Heo 
33433267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
33533267325STejun Heo 			continue;
33633267325STejun Heo 
33733267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
33833267325STejun Heo 			continue;
33933267325STejun Heo 
34033267325STejun Heo 		ap->cbl = fe->param.cbl;
341a9a79dfeSJoe Perches 		ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
34233267325STejun Heo 		return;
34333267325STejun Heo 	}
34433267325STejun Heo }
34533267325STejun Heo 
34633267325STejun Heo /**
34705944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
34833267325STejun Heo  *	@link: ATA link of interest
34933267325STejun Heo  *
35005944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
35105944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
35205944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
35305944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
35405944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
35505944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
356b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
357b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
35833267325STejun Heo  *
35933267325STejun Heo  *	LOCKING:
36033267325STejun Heo  *	EH context.
36133267325STejun Heo  */
36205944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
36333267325STejun Heo {
36405944bdfSTejun Heo 	bool did_spd = false;
365b1c72916STejun Heo 	int linkno = link->pmp;
366b1c72916STejun Heo 	int i;
36733267325STejun Heo 
36833267325STejun Heo 	if (ata_is_host_link(link))
369b1c72916STejun Heo 		linkno += 15;
37033267325STejun Heo 
37133267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
37233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
37333267325STejun Heo 
37433267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
37533267325STejun Heo 			continue;
37633267325STejun Heo 
37733267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
37833267325STejun Heo 			continue;
37933267325STejun Heo 
38005944bdfSTejun Heo 		/* only honor the first spd limit */
38105944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
38233267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
383a9a79dfeSJoe Perches 			ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
38405944bdfSTejun Heo 					fe->param.name);
38505944bdfSTejun Heo 			did_spd = true;
38605944bdfSTejun Heo 		}
38705944bdfSTejun Heo 
38805944bdfSTejun Heo 		/* let lflags stack */
38905944bdfSTejun Heo 		if (fe->param.lflags) {
39005944bdfSTejun Heo 			link->flags |= fe->param.lflags;
391a9a79dfeSJoe Perches 			ata_link_notice(link,
39205944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
39305944bdfSTejun Heo 					fe->param.lflags, link->flags);
39405944bdfSTejun Heo 		}
39533267325STejun Heo 	}
39633267325STejun Heo }
39733267325STejun Heo 
39833267325STejun Heo /**
39933267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
40033267325STejun Heo  *	@dev: ATA device of interest
40133267325STejun Heo  *
40233267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
40333267325STejun Heo  *	For consistency with link selection, device number 15 selects
40433267325STejun Heo  *	the first device connected to the host link.
40533267325STejun Heo  *
40633267325STejun Heo  *	LOCKING:
40733267325STejun Heo  *	EH context.
40833267325STejun Heo  */
40933267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
41033267325STejun Heo {
41133267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
41233267325STejun Heo 	int alt_devno = devno;
41333267325STejun Heo 	int i;
41433267325STejun Heo 
415b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
416b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
417b1c72916STejun Heo 		alt_devno += 15;
41833267325STejun Heo 
41933267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
42033267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
42133267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
42233267325STejun Heo 
42333267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
42433267325STejun Heo 			continue;
42533267325STejun Heo 
42633267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
42733267325STejun Heo 		    fe->device != alt_devno)
42833267325STejun Heo 			continue;
42933267325STejun Heo 
43033267325STejun Heo 		if (!fe->param.xfer_mask)
43133267325STejun Heo 			continue;
43233267325STejun Heo 
43333267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
43433267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
43533267325STejun Heo 		if (udma_mask)
43633267325STejun Heo 			dev->udma_mask = udma_mask;
43733267325STejun Heo 		else if (mwdma_mask) {
43833267325STejun Heo 			dev->udma_mask = 0;
43933267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
44033267325STejun Heo 		} else {
44133267325STejun Heo 			dev->udma_mask = 0;
44233267325STejun Heo 			dev->mwdma_mask = 0;
44333267325STejun Heo 			dev->pio_mask = pio_mask;
44433267325STejun Heo 		}
44533267325STejun Heo 
446a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
447a9a79dfeSJoe Perches 			       fe->param.name);
44833267325STejun Heo 		return;
44933267325STejun Heo 	}
45033267325STejun Heo }
45133267325STejun Heo 
45233267325STejun Heo /**
45333267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
45433267325STejun Heo  *	@dev: ATA device of interest
45533267325STejun Heo  *
45633267325STejun Heo  *	Force horkage according to libata.force and whine about it.
45733267325STejun Heo  *	For consistency with link selection, device number 15 selects
45833267325STejun Heo  *	the first device connected to the host link.
45933267325STejun Heo  *
46033267325STejun Heo  *	LOCKING:
46133267325STejun Heo  *	EH context.
46233267325STejun Heo  */
46333267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
46433267325STejun Heo {
46533267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
46633267325STejun Heo 	int alt_devno = devno;
46733267325STejun Heo 	int i;
46833267325STejun Heo 
469b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
470b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
471b1c72916STejun Heo 		alt_devno += 15;
47233267325STejun Heo 
47333267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
47433267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
47533267325STejun Heo 
47633267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
47733267325STejun Heo 			continue;
47833267325STejun Heo 
47933267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
48033267325STejun Heo 		    fe->device != alt_devno)
48133267325STejun Heo 			continue;
48233267325STejun Heo 
48333267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
48433267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
48533267325STejun Heo 			continue;
48633267325STejun Heo 
48733267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
48833267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
48933267325STejun Heo 
490a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
491a9a79dfeSJoe Perches 			       fe->param.name);
49233267325STejun Heo 	}
49333267325STejun Heo }
494bf89b0bfSBartlomiej Zolnierkiewicz #else
495bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_link_limits(struct ata_link *link) { }
496bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_xfermask(struct ata_device *dev) { }
497bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_horkage(struct ata_device *dev) { }
498bf89b0bfSBartlomiej Zolnierkiewicz #endif
49933267325STejun Heo 
50033267325STejun Heo /**
501436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
502436d34b3STejun Heo  *	@opcode: SCSI opcode
503436d34b3STejun Heo  *
504436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
505436d34b3STejun Heo  *
506436d34b3STejun Heo  *	LOCKING:
507436d34b3STejun Heo  *	None.
508436d34b3STejun Heo  *
509436d34b3STejun Heo  *	RETURNS:
510436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
511436d34b3STejun Heo  */
512436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
513436d34b3STejun Heo {
514436d34b3STejun Heo 	switch (opcode) {
515436d34b3STejun Heo 	case GPCMD_READ_10:
516436d34b3STejun Heo 	case GPCMD_READ_12:
517436d34b3STejun Heo 		return ATAPI_READ;
518436d34b3STejun Heo 
519436d34b3STejun Heo 	case GPCMD_WRITE_10:
520436d34b3STejun Heo 	case GPCMD_WRITE_12:
521436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
522436d34b3STejun Heo 		return ATAPI_WRITE;
523436d34b3STejun Heo 
524436d34b3STejun Heo 	case GPCMD_READ_CD:
525436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
526436d34b3STejun Heo 		return ATAPI_READ_CD;
527436d34b3STejun Heo 
528e52dcc48STejun Heo 	case ATA_16:
529e52dcc48STejun Heo 	case ATA_12:
530e52dcc48STejun Heo 		if (atapi_passthru16)
531e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
532df561f66SGustavo A. R. Silva 		fallthrough;
533436d34b3STejun Heo 	default:
534436d34b3STejun Heo 		return ATAPI_MISC;
535436d34b3STejun Heo 	}
536436d34b3STejun Heo }
537a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(atapi_cmd_type);
538436d34b3STejun Heo 
539c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
540c6fd2807SJeff Garzik 	/* pio multi */
541c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
542c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
543c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
544c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
545c6fd2807SJeff Garzik 	0,
546c6fd2807SJeff Garzik 	0,
547c6fd2807SJeff Garzik 	0,
548c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
549c6fd2807SJeff Garzik 	/* pio */
550c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
551c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
552c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
553c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
554c6fd2807SJeff Garzik 	0,
555c6fd2807SJeff Garzik 	0,
556c6fd2807SJeff Garzik 	0,
557c6fd2807SJeff Garzik 	0,
558c6fd2807SJeff Garzik 	/* dma */
559c6fd2807SJeff Garzik 	ATA_CMD_READ,
560c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
561c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
562c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
563c6fd2807SJeff Garzik 	0,
564c6fd2807SJeff Garzik 	0,
565c6fd2807SJeff Garzik 	0,
566c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
567c6fd2807SJeff Garzik };
568c6fd2807SJeff Garzik 
569c6fd2807SJeff Garzik /**
570c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
571bd056d7eSTejun Heo  *	@tf: command to examine and configure
572bd056d7eSTejun Heo  *	@dev: device tf belongs to
573c6fd2807SJeff Garzik  *
574c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
575c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
576c6fd2807SJeff Garzik  *
577c6fd2807SJeff Garzik  *	LOCKING:
578c6fd2807SJeff Garzik  *	caller.
579c6fd2807SJeff Garzik  */
580bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
581c6fd2807SJeff Garzik {
582c6fd2807SJeff Garzik 	u8 cmd;
583c6fd2807SJeff Garzik 
584c6fd2807SJeff Garzik 	int index, fua, lba48, write;
585c6fd2807SJeff Garzik 
586c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
587c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
588c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
589c6fd2807SJeff Garzik 
590c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
591c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
592c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
5939af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
594c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
595c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
596c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
597c6fd2807SJeff Garzik 	} else {
598c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
599c6fd2807SJeff Garzik 		index = 16;
600c6fd2807SJeff Garzik 	}
601c6fd2807SJeff Garzik 
602c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
603c6fd2807SJeff Garzik 	if (cmd) {
604c6fd2807SJeff Garzik 		tf->command = cmd;
605c6fd2807SJeff Garzik 		return 0;
606c6fd2807SJeff Garzik 	}
607c6fd2807SJeff Garzik 	return -1;
608c6fd2807SJeff Garzik }
609c6fd2807SJeff Garzik 
610c6fd2807SJeff Garzik /**
61135b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
61235b649feSTejun Heo  *	@tf: ATA taskfile of interest
61335b649feSTejun Heo  *	@dev: ATA device @tf belongs to
61435b649feSTejun Heo  *
61535b649feSTejun Heo  *	LOCKING:
61635b649feSTejun Heo  *	None.
61735b649feSTejun Heo  *
61835b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
61935b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
62035b649feSTejun Heo  *	flags select the address format to use.
62135b649feSTejun Heo  *
62235b649feSTejun Heo  *	RETURNS:
62335b649feSTejun Heo  *	Block address read from @tf.
62435b649feSTejun Heo  */
625cffd1ee9SHannes Reinecke u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
62635b649feSTejun Heo {
62735b649feSTejun Heo 	u64 block = 0;
62835b649feSTejun Heo 
629fe16d4f2STejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
63035b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
63135b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
63235b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
63344901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
63435b649feSTejun Heo 		} else
63535b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
63635b649feSTejun Heo 
63735b649feSTejun Heo 		block |= tf->lbah << 16;
63835b649feSTejun Heo 		block |= tf->lbam << 8;
63935b649feSTejun Heo 		block |= tf->lbal;
64035b649feSTejun Heo 	} else {
64135b649feSTejun Heo 		u32 cyl, head, sect;
64235b649feSTejun Heo 
64335b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
64435b649feSTejun Heo 		head = tf->device & 0xf;
64535b649feSTejun Heo 		sect = tf->lbal;
64635b649feSTejun Heo 
647ac8672eaSTejun Heo 		if (!sect) {
648a9a79dfeSJoe Perches 			ata_dev_warn(dev,
649a9a79dfeSJoe Perches 				     "device reported invalid CHS sector 0\n");
650cffd1ee9SHannes Reinecke 			return U64_MAX;
651ac8672eaSTejun Heo 		}
652ac8672eaSTejun Heo 
653ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
65435b649feSTejun Heo 	}
65535b649feSTejun Heo 
65635b649feSTejun Heo 	return block;
65735b649feSTejun Heo }
65835b649feSTejun Heo 
65935b649feSTejun Heo /**
660bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
661bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
662bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
663bd056d7eSTejun Heo  *	@block: Block address
664bd056d7eSTejun Heo  *	@n_block: Number of blocks
665bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
666bd056d7eSTejun Heo  *	@tag: tag
6678e061784SAdam Manzanares  *	@class: IO priority class
668bd056d7eSTejun Heo  *
669bd056d7eSTejun Heo  *	LOCKING:
670bd056d7eSTejun Heo  *	None.
671bd056d7eSTejun Heo  *
672bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
673bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
674bd056d7eSTejun Heo  *
675bd056d7eSTejun Heo  *	RETURNS:
676bd056d7eSTejun Heo  *
677bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
678bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
679bd056d7eSTejun Heo  */
680bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
681bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
6828e061784SAdam Manzanares 		    unsigned int tag, int class)
683bd056d7eSTejun Heo {
684bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
685bd056d7eSTejun Heo 	tf->flags |= tf_flags;
686bd056d7eSTejun Heo 
6872e2cc676SJens Axboe 	if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
688bd056d7eSTejun Heo 		/* yay, NCQ */
689bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
690bd056d7eSTejun Heo 			return -ERANGE;
691bd056d7eSTejun Heo 
692bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
693bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
694bd056d7eSTejun Heo 
695bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
696bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
697bd056d7eSTejun Heo 		else
698bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
699bd056d7eSTejun Heo 
700bd056d7eSTejun Heo 		tf->nsect = tag << 3;
701bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
702bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
703bd056d7eSTejun Heo 
704bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
705bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
706bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
707bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
708bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
709bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
710bd056d7eSTejun Heo 
7119ca7cfa4SSergei Shtylyov 		tf->device = ATA_LBA;
712bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
713bd056d7eSTejun Heo 			tf->device |= 1 << 7;
7148e061784SAdam Manzanares 
7152360fa18SDamien Le Moal 		if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE &&
7162360fa18SDamien Le Moal 		    class == IOPRIO_CLASS_RT)
7172360fa18SDamien Le Moal 			tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO;
718bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
719bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
720bd056d7eSTejun Heo 
721bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
722bd056d7eSTejun Heo 			/* use LBA28 */
723bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
724bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
725bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
726bd056d7eSTejun Heo 				return -ERANGE;
727bd056d7eSTejun Heo 
728bd056d7eSTejun Heo 			/* use LBA48 */
729bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
730bd056d7eSTejun Heo 
731bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
732bd056d7eSTejun Heo 
733bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
734bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
735bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
736bd056d7eSTejun Heo 		} else
737bd056d7eSTejun Heo 			/* request too large even for LBA48 */
738bd056d7eSTejun Heo 			return -ERANGE;
739bd056d7eSTejun Heo 
740bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
741bd056d7eSTejun Heo 			return -EINVAL;
742bd056d7eSTejun Heo 
743bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
744bd056d7eSTejun Heo 
745bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
746bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
747bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
748bd056d7eSTejun Heo 
749bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
750bd056d7eSTejun Heo 	} else {
751bd056d7eSTejun Heo 		/* CHS */
752bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
753bd056d7eSTejun Heo 
754bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
755bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
756bd056d7eSTejun Heo 			return -ERANGE;
757bd056d7eSTejun Heo 
758bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
759bd056d7eSTejun Heo 			return -EINVAL;
760bd056d7eSTejun Heo 
761bd056d7eSTejun Heo 		/* Convert LBA to CHS */
762bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
763bd056d7eSTejun Heo 		cyl   = track / dev->heads;
764bd056d7eSTejun Heo 		head  = track % dev->heads;
765bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
766bd056d7eSTejun Heo 
767bd056d7eSTejun Heo 		DPRINTK("block %u track %u cyl %u head %u sect %u\n",
768bd056d7eSTejun Heo 			(u32)block, track, cyl, head, sect);
769bd056d7eSTejun Heo 
770bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
771bd056d7eSTejun Heo 		   Cylinder: 0-65535
772bd056d7eSTejun Heo 		   Head: 0-15
773bd056d7eSTejun Heo 		   Sector: 1-255*/
774bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
775bd056d7eSTejun Heo 			return -ERANGE;
776bd056d7eSTejun Heo 
777bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
778bd056d7eSTejun Heo 		tf->lbal = sect;
779bd056d7eSTejun Heo 		tf->lbam = cyl;
780bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
781bd056d7eSTejun Heo 		tf->device |= head;
782bd056d7eSTejun Heo 	}
783bd056d7eSTejun Heo 
784bd056d7eSTejun Heo 	return 0;
785bd056d7eSTejun Heo }
786bd056d7eSTejun Heo 
787bd056d7eSTejun Heo /**
788c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
789c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
790c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
791c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
792c6fd2807SJeff Garzik  *
793c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
794c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
795c6fd2807SJeff Garzik  *
796c6fd2807SJeff Garzik  *	LOCKING:
797c6fd2807SJeff Garzik  *	None.
798c6fd2807SJeff Garzik  *
799c6fd2807SJeff Garzik  *	RETURNS:
800c6fd2807SJeff Garzik  *	Packed xfer_mask.
801c6fd2807SJeff Garzik  */
8027dc951aeSTejun Heo unsigned long ata_pack_xfermask(unsigned long pio_mask,
8037dc951aeSTejun Heo 				unsigned long mwdma_mask,
8047dc951aeSTejun Heo 				unsigned long udma_mask)
805c6fd2807SJeff Garzik {
806c6fd2807SJeff Garzik 	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
807c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
808c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
809c6fd2807SJeff Garzik }
810a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pack_xfermask);
811c6fd2807SJeff Garzik 
812c6fd2807SJeff Garzik /**
813c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
814c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
815c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
816c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
817c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
818c6fd2807SJeff Garzik  *
819c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
820c9b5560aSMasanari Iida  *	Any NULL destination masks will be ignored.
821c6fd2807SJeff Garzik  */
8227dc951aeSTejun Heo void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
8237dc951aeSTejun Heo 			 unsigned long *mwdma_mask, unsigned long *udma_mask)
824c6fd2807SJeff Garzik {
825c6fd2807SJeff Garzik 	if (pio_mask)
826c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
827c6fd2807SJeff Garzik 	if (mwdma_mask)
828c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
829c6fd2807SJeff Garzik 	if (udma_mask)
830c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
831c6fd2807SJeff Garzik }
832c6fd2807SJeff Garzik 
833c6fd2807SJeff Garzik static const struct ata_xfer_ent {
834c6fd2807SJeff Garzik 	int shift, bits;
835c6fd2807SJeff Garzik 	u8 base;
836c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
83770cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
83870cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
83970cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
840c6fd2807SJeff Garzik 	{ -1, },
841c6fd2807SJeff Garzik };
842c6fd2807SJeff Garzik 
843c6fd2807SJeff Garzik /**
844c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
845c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
846c6fd2807SJeff Garzik  *
847c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
848c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
849c6fd2807SJeff Garzik  *
850c6fd2807SJeff Garzik  *	LOCKING:
851c6fd2807SJeff Garzik  *	None.
852c6fd2807SJeff Garzik  *
853c6fd2807SJeff Garzik  *	RETURNS:
85470cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
855c6fd2807SJeff Garzik  */
8567dc951aeSTejun Heo u8 ata_xfer_mask2mode(unsigned long xfer_mask)
857c6fd2807SJeff Garzik {
858c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
859c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
860c6fd2807SJeff Garzik 
861c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
862c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
863c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
86470cd071eSTejun Heo 	return 0xff;
865c6fd2807SJeff Garzik }
866a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
867c6fd2807SJeff Garzik 
868c6fd2807SJeff Garzik /**
869c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
870c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
871c6fd2807SJeff Garzik  *
872c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
873c6fd2807SJeff Garzik  *
874c6fd2807SJeff Garzik  *	LOCKING:
875c6fd2807SJeff Garzik  *	None.
876c6fd2807SJeff Garzik  *
877c6fd2807SJeff Garzik  *	RETURNS:
878c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
879c6fd2807SJeff Garzik  */
8807dc951aeSTejun Heo unsigned long ata_xfer_mode2mask(u8 xfer_mode)
881c6fd2807SJeff Garzik {
882c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
883c6fd2807SJeff Garzik 
884c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
885c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
88670cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
88770cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
888c6fd2807SJeff Garzik 	return 0;
889c6fd2807SJeff Garzik }
890a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
891c6fd2807SJeff Garzik 
892c6fd2807SJeff Garzik /**
893c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
894c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
895c6fd2807SJeff Garzik  *
896c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
897c6fd2807SJeff Garzik  *
898c6fd2807SJeff Garzik  *	LOCKING:
899c6fd2807SJeff Garzik  *	None.
900c6fd2807SJeff Garzik  *
901c6fd2807SJeff Garzik  *	RETURNS:
902c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
903c6fd2807SJeff Garzik  */
9047dc951aeSTejun Heo int ata_xfer_mode2shift(unsigned long xfer_mode)
905c6fd2807SJeff Garzik {
906c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
907c6fd2807SJeff Garzik 
908c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
909c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
910c6fd2807SJeff Garzik 			return ent->shift;
911c6fd2807SJeff Garzik 	return -1;
912c6fd2807SJeff Garzik }
913a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
914c6fd2807SJeff Garzik 
915c6fd2807SJeff Garzik /**
916c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
917c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
918c6fd2807SJeff Garzik  *
919c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
920c6fd2807SJeff Garzik  *	(highest bit in @modemask).
921c6fd2807SJeff Garzik  *
922c6fd2807SJeff Garzik  *	LOCKING:
923c6fd2807SJeff Garzik  *	None.
924c6fd2807SJeff Garzik  *
925c6fd2807SJeff Garzik  *	RETURNS:
926c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
927c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
928c6fd2807SJeff Garzik  */
9297dc951aeSTejun Heo const char *ata_mode_string(unsigned long xfer_mask)
930c6fd2807SJeff Garzik {
931c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
932c6fd2807SJeff Garzik 		"PIO0",
933c6fd2807SJeff Garzik 		"PIO1",
934c6fd2807SJeff Garzik 		"PIO2",
935c6fd2807SJeff Garzik 		"PIO3",
936c6fd2807SJeff Garzik 		"PIO4",
937b352e57dSAlan Cox 		"PIO5",
938b352e57dSAlan Cox 		"PIO6",
939c6fd2807SJeff Garzik 		"MWDMA0",
940c6fd2807SJeff Garzik 		"MWDMA1",
941c6fd2807SJeff Garzik 		"MWDMA2",
942b352e57dSAlan Cox 		"MWDMA3",
943b352e57dSAlan Cox 		"MWDMA4",
944c6fd2807SJeff Garzik 		"UDMA/16",
945c6fd2807SJeff Garzik 		"UDMA/25",
946c6fd2807SJeff Garzik 		"UDMA/33",
947c6fd2807SJeff Garzik 		"UDMA/44",
948c6fd2807SJeff Garzik 		"UDMA/66",
949c6fd2807SJeff Garzik 		"UDMA/100",
950c6fd2807SJeff Garzik 		"UDMA/133",
951c6fd2807SJeff Garzik 		"UDMA7",
952c6fd2807SJeff Garzik 	};
953c6fd2807SJeff Garzik 	int highbit;
954c6fd2807SJeff Garzik 
955c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
956c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
957c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
958c6fd2807SJeff Garzik 	return "<n/a>";
959c6fd2807SJeff Garzik }
960a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_mode_string);
961c6fd2807SJeff Garzik 
962d9027470SGwendal Grignou const char *sata_spd_string(unsigned int spd)
963c6fd2807SJeff Garzik {
964c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
965c6fd2807SJeff Garzik 		"1.5 Gbps",
966c6fd2807SJeff Garzik 		"3.0 Gbps",
9678522ee25SShane Huang 		"6.0 Gbps",
968c6fd2807SJeff Garzik 	};
969c6fd2807SJeff Garzik 
970c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
971c6fd2807SJeff Garzik 		return "<unknown>";
972c6fd2807SJeff Garzik 	return spd_str[spd - 1];
973c6fd2807SJeff Garzik }
974c6fd2807SJeff Garzik 
975c6fd2807SJeff Garzik /**
976c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
977c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
978c6fd2807SJeff Garzik  *
979c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
980c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
981c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
982c6fd2807SJeff Garzik  *
983c6fd2807SJeff Garzik  *	LOCKING:
984c6fd2807SJeff Garzik  *	None.
985c6fd2807SJeff Garzik  *
986c6fd2807SJeff Garzik  *	RETURNS:
9879162c657SHannes Reinecke  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
9889162c657SHannes Reinecke  *	%ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
989c6fd2807SJeff Garzik  */
990c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
991c6fd2807SJeff Garzik {
992c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
993c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
994c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
995633273a3STejun Heo 	 *
996633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
997633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
998633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
999633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1000633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1001633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1002633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1003633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1004633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1005633273a3STejun Heo 	 * SerialATA.
1006633273a3STejun Heo 	 *
1007633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1008633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
100979b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
101079b42babSTejun Heo 	 * SEMB signature.  This is worked around in
101179b42babSTejun Heo 	 * ata_dev_read_id().
1012c6fd2807SJeff Garzik 	 */
1013633273a3STejun Heo 	if ((tf->lbam == 0) && (tf->lbah == 0)) {
1014c6fd2807SJeff Garzik 		DPRINTK("found ATA device by sig\n");
1015c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1016c6fd2807SJeff Garzik 	}
1017c6fd2807SJeff Garzik 
1018633273a3STejun Heo 	if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1019c6fd2807SJeff Garzik 		DPRINTK("found ATAPI device by sig\n");
1020c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1021c6fd2807SJeff Garzik 	}
1022c6fd2807SJeff Garzik 
1023633273a3STejun Heo 	if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1024633273a3STejun Heo 		DPRINTK("found PMP device by sig\n");
1025633273a3STejun Heo 		return ATA_DEV_PMP;
1026633273a3STejun Heo 	}
1027633273a3STejun Heo 
1028633273a3STejun Heo 	if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
102979b42babSTejun Heo 		DPRINTK("found SEMB device by sig (could be ATA device)\n");
103079b42babSTejun Heo 		return ATA_DEV_SEMB;
1031633273a3STejun Heo 	}
1032633273a3STejun Heo 
10339162c657SHannes Reinecke 	if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
10349162c657SHannes Reinecke 		DPRINTK("found ZAC device by sig\n");
10359162c657SHannes Reinecke 		return ATA_DEV_ZAC;
10369162c657SHannes Reinecke 	}
10379162c657SHannes Reinecke 
1038c6fd2807SJeff Garzik 	DPRINTK("unknown device\n");
1039c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1040c6fd2807SJeff Garzik }
1041a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_classify);
1042c6fd2807SJeff Garzik 
1043c6fd2807SJeff Garzik /**
1044c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1045c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1046c6fd2807SJeff Garzik  *	@s: string into which data is output
1047c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1048c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1049c6fd2807SJeff Garzik  *
1050c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1051c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1052c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1053c6fd2807SJeff Garzik  *
1054c6fd2807SJeff Garzik  *	LOCKING:
1055c6fd2807SJeff Garzik  *	caller.
1056c6fd2807SJeff Garzik  */
1057c6fd2807SJeff Garzik 
1058c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1059c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1060c6fd2807SJeff Garzik {
1061c6fd2807SJeff Garzik 	unsigned int c;
1062c6fd2807SJeff Garzik 
1063963e4975SAlan Cox 	BUG_ON(len & 1);
1064963e4975SAlan Cox 
1065c6fd2807SJeff Garzik 	while (len > 0) {
1066c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1067c6fd2807SJeff Garzik 		*s = c;
1068c6fd2807SJeff Garzik 		s++;
1069c6fd2807SJeff Garzik 
1070c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1071c6fd2807SJeff Garzik 		*s = c;
1072c6fd2807SJeff Garzik 		s++;
1073c6fd2807SJeff Garzik 
1074c6fd2807SJeff Garzik 		ofs++;
1075c6fd2807SJeff Garzik 		len -= 2;
1076c6fd2807SJeff Garzik 	}
1077c6fd2807SJeff Garzik }
1078a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_string);
1079c6fd2807SJeff Garzik 
1080c6fd2807SJeff Garzik /**
1081c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1082c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1083c6fd2807SJeff Garzik  *	@s: string into which data is output
1084c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1085c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1086c6fd2807SJeff Garzik  *
1087c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1088c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1089c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1090c6fd2807SJeff Garzik  *
1091c6fd2807SJeff Garzik  *	LOCKING:
1092c6fd2807SJeff Garzik  *	caller.
1093c6fd2807SJeff Garzik  */
1094c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1095c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1096c6fd2807SJeff Garzik {
1097c6fd2807SJeff Garzik 	unsigned char *p;
1098c6fd2807SJeff Garzik 
1099c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1100c6fd2807SJeff Garzik 
1101c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1102c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1103c6fd2807SJeff Garzik 		p--;
1104c6fd2807SJeff Garzik 	*p = '\0';
1105c6fd2807SJeff Garzik }
1106a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_c_string);
1107c6fd2807SJeff Garzik 
1108db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1109db6f8759STejun Heo {
1110db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1111db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1112968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1113db6f8759STejun Heo 		else
1114968e594aSRobert Hancock 			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1115db6f8759STejun Heo 	} else {
1116db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1117968e594aSRobert Hancock 			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1118968e594aSRobert Hancock 			       id[ATA_ID_CUR_SECTORS];
1119db6f8759STejun Heo 		else
1120968e594aSRobert Hancock 			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1121968e594aSRobert Hancock 			       id[ATA_ID_SECTORS];
1122db6f8759STejun Heo 	}
1123db6f8759STejun Heo }
1124db6f8759STejun Heo 
1125a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
11261e999736SAlan Cox {
11271e999736SAlan Cox 	u64 sectors = 0;
11281e999736SAlan Cox 
11291e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
11301e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1131ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
11321e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11331e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11341e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11351e999736SAlan Cox 
1136a5987e0aSTejun Heo 	return sectors;
11371e999736SAlan Cox }
11381e999736SAlan Cox 
1139a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
11401e999736SAlan Cox {
11411e999736SAlan Cox 	u64 sectors = 0;
11421e999736SAlan Cox 
11431e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
11441e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11451e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11461e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11471e999736SAlan Cox 
1148a5987e0aSTejun Heo 	return sectors;
11491e999736SAlan Cox }
11501e999736SAlan Cox 
11511e999736SAlan Cox /**
1152c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1153c728a914STejun Heo  *	@dev: target device
1154c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
11551e999736SAlan Cox  *
1156c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1157c728a914STejun Heo  *	question.
1158c728a914STejun Heo  *
1159c728a914STejun Heo  *	RETURNS:
1160c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1161c728a914STejun Heo  *	-EIO on other errors.
11621e999736SAlan Cox  */
1163c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
11641e999736SAlan Cox {
1165c728a914STejun Heo 	unsigned int err_mask;
11661e999736SAlan Cox 	struct ata_taskfile tf;
1167c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
11681e999736SAlan Cox 
11691e999736SAlan Cox 	ata_tf_init(dev, &tf);
11701e999736SAlan Cox 
1171c728a914STejun Heo 	/* always clear all address registers */
11721e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1173c728a914STejun Heo 
1174c728a914STejun Heo 	if (lba48) {
1175c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1176c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1177c728a914STejun Heo 	} else
1178c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1179c728a914STejun Heo 
1180bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1181c728a914STejun Heo 	tf.device |= ATA_LBA;
11821e999736SAlan Cox 
11832b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1184c728a914STejun Heo 	if (err_mask) {
1185a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1186a9a79dfeSJoe Perches 			     "failed to read native max address (err_mask=0x%x)\n",
1187a9a79dfeSJoe Perches 			     err_mask);
1188c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1189c728a914STejun Heo 			return -EACCES;
1190c728a914STejun Heo 		return -EIO;
1191c728a914STejun Heo 	}
1192c728a914STejun Heo 
1193c728a914STejun Heo 	if (lba48)
1194a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1195c728a914STejun Heo 	else
1196a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
119793328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
119893328e11SAlan Cox 		(*max_sectors)--;
11991e999736SAlan Cox 	return 0;
12001e999736SAlan Cox }
12011e999736SAlan Cox 
12021e999736SAlan Cox /**
1203c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1204c728a914STejun Heo  *	@dev: target device
12056b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
12061e999736SAlan Cox  *
1207c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1208c728a914STejun Heo  *
1209c728a914STejun Heo  *	RETURNS:
1210c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1211c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1212c728a914STejun Heo  *	errors.
12131e999736SAlan Cox  */
121405027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
12151e999736SAlan Cox {
1216c728a914STejun Heo 	unsigned int err_mask;
12171e999736SAlan Cox 	struct ata_taskfile tf;
1218c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12191e999736SAlan Cox 
12201e999736SAlan Cox 	new_sectors--;
12211e999736SAlan Cox 
12221e999736SAlan Cox 	ata_tf_init(dev, &tf);
12231e999736SAlan Cox 
1224c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
12251e999736SAlan Cox 
1226c728a914STejun Heo 	if (lba48) {
1227c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1228c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
12291e999736SAlan Cox 
12301e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
12311e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
12321e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
12331e582ba4STejun Heo 	} else {
12341e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1235c728a914STejun Heo 
12361e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
12371e582ba4STejun Heo 	}
12381e582ba4STejun Heo 
1239bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1240c728a914STejun Heo 	tf.device |= ATA_LBA;
12411e999736SAlan Cox 
12421e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
12431e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
12441e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
12451e999736SAlan Cox 
12462b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1247c728a914STejun Heo 	if (err_mask) {
1248a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1249a9a79dfeSJoe Perches 			     "failed to set max address (err_mask=0x%x)\n",
1250a9a79dfeSJoe Perches 			     err_mask);
1251c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1252c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1253c728a914STejun Heo 			return -EACCES;
1254c728a914STejun Heo 		return -EIO;
1255c728a914STejun Heo 	}
1256c728a914STejun Heo 
12571e999736SAlan Cox 	return 0;
12581e999736SAlan Cox }
12591e999736SAlan Cox 
12601e999736SAlan Cox /**
12611e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
12621e999736SAlan Cox  *	@dev: Device to resize
12631e999736SAlan Cox  *
12641e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
12651e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
12661e999736SAlan Cox  *	the drive has the HPA feature set enabled.
126705027adcSTejun Heo  *
126805027adcSTejun Heo  *	RETURNS:
126905027adcSTejun Heo  *	0 on success, -errno on failure.
12701e999736SAlan Cox  */
127105027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
12721e999736SAlan Cox {
1273891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
1274445d211bSTejun Heo 	bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
127505027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
127605027adcSTejun Heo 	u64 native_sectors;
1277c728a914STejun Heo 	int rc;
12781e999736SAlan Cox 
127905027adcSTejun Heo 	/* do we need to do it? */
12809162c657SHannes Reinecke 	if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
128105027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
128205027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1283c728a914STejun Heo 		return 0;
12841e999736SAlan Cox 
128505027adcSTejun Heo 	/* read native max address */
128605027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
128705027adcSTejun Heo 	if (rc) {
1288dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1289dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
129005027adcSTejun Heo 		 */
1291445d211bSTejun Heo 		if (rc == -EACCES || !unlock_hpa) {
1292a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1293a9a79dfeSJoe Perches 				     "HPA support seems broken, skipping HPA handling\n");
129405027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
129505027adcSTejun Heo 
129605027adcSTejun Heo 			/* we can continue if device aborted the command */
129705027adcSTejun Heo 			if (rc == -EACCES)
129805027adcSTejun Heo 				rc = 0;
129905027adcSTejun Heo 		}
130005027adcSTejun Heo 
130105027adcSTejun Heo 		return rc;
130205027adcSTejun Heo 	}
13035920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
130405027adcSTejun Heo 
130505027adcSTejun Heo 	/* nothing to do? */
1306445d211bSTejun Heo 	if (native_sectors <= sectors || !unlock_hpa) {
130705027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
130805027adcSTejun Heo 			return 0;
130905027adcSTejun Heo 
131005027adcSTejun Heo 		if (native_sectors > sectors)
1311a9a79dfeSJoe Perches 			ata_dev_info(dev,
131205027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
131305027adcSTejun Heo 				(unsigned long long)sectors,
131405027adcSTejun Heo 				(unsigned long long)native_sectors);
131505027adcSTejun Heo 		else if (native_sectors < sectors)
1316a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1317a9a79dfeSJoe Perches 				"native sectors (%llu) is smaller than sectors (%llu)\n",
131805027adcSTejun Heo 				(unsigned long long)native_sectors,
131905027adcSTejun Heo 				(unsigned long long)sectors);
132005027adcSTejun Heo 		return 0;
13211e999736SAlan Cox 	}
132237301a55STejun Heo 
132305027adcSTejun Heo 	/* let's unlock HPA */
132405027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
132505027adcSTejun Heo 	if (rc == -EACCES) {
132605027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
1327a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1328a9a79dfeSJoe Perches 			     "device aborted resize (%llu -> %llu), skipping HPA handling\n",
132905027adcSTejun Heo 			     (unsigned long long)sectors,
133005027adcSTejun Heo 			     (unsigned long long)native_sectors);
133105027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
133205027adcSTejun Heo 		return 0;
133305027adcSTejun Heo 	} else if (rc)
133405027adcSTejun Heo 		return rc;
133505027adcSTejun Heo 
133605027adcSTejun Heo 	/* re-read IDENTIFY data */
133705027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
133805027adcSTejun Heo 	if (rc) {
1339a9a79dfeSJoe Perches 		ata_dev_err(dev,
1340a9a79dfeSJoe Perches 			    "failed to re-read IDENTIFY data after HPA resizing\n");
134105027adcSTejun Heo 		return rc;
134205027adcSTejun Heo 	}
134305027adcSTejun Heo 
134405027adcSTejun Heo 	if (print_info) {
134505027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
1346a9a79dfeSJoe Perches 		ata_dev_info(dev,
134705027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
134805027adcSTejun Heo 			(unsigned long long)sectors,
134905027adcSTejun Heo 			(unsigned long long)new_sectors,
135005027adcSTejun Heo 			(unsigned long long)native_sectors);
135105027adcSTejun Heo 	}
135205027adcSTejun Heo 
135305027adcSTejun Heo 	return 0;
13541e999736SAlan Cox }
13551e999736SAlan Cox 
1356c6fd2807SJeff Garzik /**
1357c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
1358c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1359c6fd2807SJeff Garzik  *
1360c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1361c6fd2807SJeff Garzik  *	page.
1362c6fd2807SJeff Garzik  *
1363c6fd2807SJeff Garzik  *	LOCKING:
1364c6fd2807SJeff Garzik  *	caller.
1365c6fd2807SJeff Garzik  */
1366c6fd2807SJeff Garzik 
1367c6fd2807SJeff Garzik static inline void ata_dump_id(const u16 *id)
1368c6fd2807SJeff Garzik {
1369c6fd2807SJeff Garzik 	DPRINTK("49==0x%04x  "
1370c6fd2807SJeff Garzik 		"53==0x%04x  "
1371c6fd2807SJeff Garzik 		"63==0x%04x  "
1372c6fd2807SJeff Garzik 		"64==0x%04x  "
1373c6fd2807SJeff Garzik 		"75==0x%04x  \n",
1374c6fd2807SJeff Garzik 		id[49],
1375c6fd2807SJeff Garzik 		id[53],
1376c6fd2807SJeff Garzik 		id[63],
1377c6fd2807SJeff Garzik 		id[64],
1378c6fd2807SJeff Garzik 		id[75]);
1379c6fd2807SJeff Garzik 	DPRINTK("80==0x%04x  "
1380c6fd2807SJeff Garzik 		"81==0x%04x  "
1381c6fd2807SJeff Garzik 		"82==0x%04x  "
1382c6fd2807SJeff Garzik 		"83==0x%04x  "
1383c6fd2807SJeff Garzik 		"84==0x%04x  \n",
1384c6fd2807SJeff Garzik 		id[80],
1385c6fd2807SJeff Garzik 		id[81],
1386c6fd2807SJeff Garzik 		id[82],
1387c6fd2807SJeff Garzik 		id[83],
1388c6fd2807SJeff Garzik 		id[84]);
1389c6fd2807SJeff Garzik 	DPRINTK("88==0x%04x  "
1390c6fd2807SJeff Garzik 		"93==0x%04x\n",
1391c6fd2807SJeff Garzik 		id[88],
1392c6fd2807SJeff Garzik 		id[93]);
1393c6fd2807SJeff Garzik }
1394c6fd2807SJeff Garzik 
1395c6fd2807SJeff Garzik /**
1396c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1397c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1398c6fd2807SJeff Garzik  *
1399c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1400c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1401c6fd2807SJeff Garzik  *
1402c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1403c6fd2807SJeff Garzik  *
1404c6fd2807SJeff Garzik  *	LOCKING:
1405c6fd2807SJeff Garzik  *	None.
1406c6fd2807SJeff Garzik  *
1407c6fd2807SJeff Garzik  *	RETURNS:
1408c6fd2807SJeff Garzik  *	Computed xfermask
1409c6fd2807SJeff Garzik  */
14107dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1411c6fd2807SJeff Garzik {
14127dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1413c6fd2807SJeff Garzik 
1414c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1415c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1416c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1417c6fd2807SJeff Garzik 		pio_mask <<= 3;
1418c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1419c6fd2807SJeff Garzik 	} else {
1420c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1421c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1422c6fd2807SJeff Garzik 		 * a mask.
1423c6fd2807SJeff Garzik 		 */
14247a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
142546767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
142646767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
142746767aebSAlan Cox 		else
142846767aebSAlan Cox 			pio_mask = 1;
1429c6fd2807SJeff Garzik 
1430c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1431c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1432c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1433c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1434c6fd2807SJeff Garzik 		 * will get this right anyway
1435c6fd2807SJeff Garzik 		 */
1436c6fd2807SJeff Garzik 	}
1437c6fd2807SJeff Garzik 
1438c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1439c6fd2807SJeff Garzik 
1440b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1441b352e57dSAlan Cox 		/*
1442b352e57dSAlan Cox 		 *	Process compact flash extended modes
1443b352e57dSAlan Cox 		 */
144462afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
144562afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1446b352e57dSAlan Cox 
1447b352e57dSAlan Cox 		if (pio)
1448b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1449b352e57dSAlan Cox 		if (pio > 1)
1450b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1451b352e57dSAlan Cox 		if (dma)
1452b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1453b352e57dSAlan Cox 		if (dma > 1)
1454b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1455b352e57dSAlan Cox 	}
1456b352e57dSAlan Cox 
1457c6fd2807SJeff Garzik 	udma_mask = 0;
1458c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1459c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1460c6fd2807SJeff Garzik 
1461c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1462c6fd2807SJeff Garzik }
1463a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_xfermask);
1464c6fd2807SJeff Garzik 
14657102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1466c6fd2807SJeff Garzik {
1467c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1468c6fd2807SJeff Garzik 
1469c6fd2807SJeff Garzik 	complete(waiting);
1470c6fd2807SJeff Garzik }
1471c6fd2807SJeff Garzik 
1472c6fd2807SJeff Garzik /**
14732432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1474c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1475c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1476c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1477e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
14785c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
14792432697bSTejun Heo  *	@n_elem: Number of sg entries
14802b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1481c6fd2807SJeff Garzik  *
1482c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1483c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1484c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1485c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1486c6fd2807SJeff Garzik  *	clean up after timeout.
1487c6fd2807SJeff Garzik  *
1488c6fd2807SJeff Garzik  *	LOCKING:
1489c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1490c6fd2807SJeff Garzik  *
1491c6fd2807SJeff Garzik  *	RETURNS:
1492c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1493c6fd2807SJeff Garzik  */
14942432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1495c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
149687260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
14972b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1498c6fd2807SJeff Garzik {
14999af5c9c9STejun Heo 	struct ata_link *link = dev->link;
15009af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1501c6fd2807SJeff Garzik 	u8 command = tf->command;
150287fbc5a0STejun Heo 	int auto_timeout = 0;
1503c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
150428361c40SJens Axboe 	unsigned int preempted_tag;
1505e3ed8939SJens Axboe 	u32 preempted_sactive;
1506e3ed8939SJens Axboe 	u64 preempted_qc_active;
1507da917d69STejun Heo 	int preempted_nr_active_links;
1508c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1509c6fd2807SJeff Garzik 	unsigned long flags;
1510c6fd2807SJeff Garzik 	unsigned int err_mask;
1511c6fd2807SJeff Garzik 	int rc;
1512c6fd2807SJeff Garzik 
1513c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1514c6fd2807SJeff Garzik 
1515c6fd2807SJeff Garzik 	/* no internal command while frozen */
1516c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1517c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1518c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1519c6fd2807SJeff Garzik 	}
1520c6fd2807SJeff Garzik 
1521c6fd2807SJeff Garzik 	/* initialize internal qc */
152228361c40SJens Axboe 	qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
1523c6fd2807SJeff Garzik 
152428361c40SJens Axboe 	qc->tag = ATA_TAG_INTERNAL;
152528361c40SJens Axboe 	qc->hw_tag = 0;
1526c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1527c6fd2807SJeff Garzik 	qc->ap = ap;
1528c6fd2807SJeff Garzik 	qc->dev = dev;
1529c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1530c6fd2807SJeff Garzik 
15319af5c9c9STejun Heo 	preempted_tag = link->active_tag;
15329af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1533c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1534da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
15359af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
15369af5c9c9STejun Heo 	link->sactive = 0;
1537c6fd2807SJeff Garzik 	ap->qc_active = 0;
1538da917d69STejun Heo 	ap->nr_active_links = 0;
1539c6fd2807SJeff Garzik 
1540c6fd2807SJeff Garzik 	/* prepare & issue qc */
1541c6fd2807SJeff Garzik 	qc->tf = *tf;
1542c6fd2807SJeff Garzik 	if (cdb)
1543c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1544e771451cSVincent Pelletier 
1545e771451cSVincent Pelletier 	/* some SATA bridges need us to indicate data xfer direction */
1546e771451cSVincent Pelletier 	if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1547e771451cSVincent Pelletier 	    dma_dir == DMA_FROM_DEVICE)
1548e771451cSVincent Pelletier 		qc->tf.feature |= ATAPI_DMADIR;
1549e771451cSVincent Pelletier 
1550c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1551c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1552c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
15532432697bSTejun Heo 		unsigned int i, buflen = 0;
155487260216SJens Axboe 		struct scatterlist *sg;
15552432697bSTejun Heo 
155687260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
155787260216SJens Axboe 			buflen += sg->length;
15582432697bSTejun Heo 
155987260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
156049c80429SBrian King 		qc->nbytes = buflen;
1561c6fd2807SJeff Garzik 	}
1562c6fd2807SJeff Garzik 
1563c6fd2807SJeff Garzik 	qc->private_data = &wait;
1564c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1565c6fd2807SJeff Garzik 
1566c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1567c6fd2807SJeff Garzik 
1568c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1569c6fd2807SJeff Garzik 
157087fbc5a0STejun Heo 	if (!timeout) {
157187fbc5a0STejun Heo 		if (ata_probe_timeout)
1572341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
157387fbc5a0STejun Heo 		else {
157487fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
157587fbc5a0STejun Heo 			auto_timeout = 1;
157687fbc5a0STejun Heo 		}
157787fbc5a0STejun Heo 	}
15782b789108STejun Heo 
1579c0c362b6STejun Heo 	if (ap->ops->error_handler)
1580c0c362b6STejun Heo 		ata_eh_release(ap);
1581c0c362b6STejun Heo 
15822b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1583c6fd2807SJeff Garzik 
1584c0c362b6STejun Heo 	if (ap->ops->error_handler)
1585c0c362b6STejun Heo 		ata_eh_acquire(ap);
1586c0c362b6STejun Heo 
1587c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1588c6fd2807SJeff Garzik 
1589c6fd2807SJeff Garzik 	if (!rc) {
1590c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1591c6fd2807SJeff Garzik 
1592c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1593c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1594c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1595c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1596c6fd2807SJeff Garzik 		 */
1597c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1598c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1599c6fd2807SJeff Garzik 
1600c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1601c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1602c6fd2807SJeff Garzik 			else
1603c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1604c6fd2807SJeff Garzik 
1605c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1606a9a79dfeSJoe Perches 				ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1607a9a79dfeSJoe Perches 					     command);
1608c6fd2807SJeff Garzik 		}
1609c6fd2807SJeff Garzik 
1610c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1611c6fd2807SJeff Garzik 	}
1612c6fd2807SJeff Garzik 
1613c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1614c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1615c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1616c6fd2807SJeff Garzik 
1617a51d644aSTejun Heo 	/* perform minimal error analysis */
1618a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1619a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1620a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1621a51d644aSTejun Heo 
1622a51d644aSTejun Heo 		if (!qc->err_mask)
1623c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1624a51d644aSTejun Heo 
1625a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1626a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
16272dae9955SDamien Le Moal 	} else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
16282dae9955SDamien Le Moal 		qc->result_tf.command |= ATA_SENSE;
1629c6fd2807SJeff Garzik 	}
1630c6fd2807SJeff Garzik 
1631c6fd2807SJeff Garzik 	/* finish up */
1632c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1633c6fd2807SJeff Garzik 
1634c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1635c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1636c6fd2807SJeff Garzik 
1637c6fd2807SJeff Garzik 	ata_qc_free(qc);
16389af5c9c9STejun Heo 	link->active_tag = preempted_tag;
16399af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1640c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1641da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1642c6fd2807SJeff Garzik 
1643c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1644c6fd2807SJeff Garzik 
164587fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
164687fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
164787fbc5a0STejun Heo 
1648c6fd2807SJeff Garzik 	return err_mask;
1649c6fd2807SJeff Garzik }
1650c6fd2807SJeff Garzik 
1651c6fd2807SJeff Garzik /**
165233480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
16532432697bSTejun Heo  *	@dev: Device to which the command is sent
16542432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
16552432697bSTejun Heo  *	@cdb: CDB for packet command
1656e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
16572432697bSTejun Heo  *	@buf: Data buffer of the command
16582432697bSTejun Heo  *	@buflen: Length of data buffer
16592b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
16602432697bSTejun Heo  *
16612432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
16622432697bSTejun Heo  *	buffer instead of sg list.
16632432697bSTejun Heo  *
16642432697bSTejun Heo  *	LOCKING:
16652432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
16662432697bSTejun Heo  *
16672432697bSTejun Heo  *	RETURNS:
16682432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
16692432697bSTejun Heo  */
16702432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
16712432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
16722b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
16732b789108STejun Heo 			   unsigned long timeout)
16742432697bSTejun Heo {
167533480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
167633480a0eSTejun Heo 	unsigned int n_elem = 0;
16772432697bSTejun Heo 
167833480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
167933480a0eSTejun Heo 		WARN_ON(!buf);
16802432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
168133480a0eSTejun Heo 		psg = &sg;
168233480a0eSTejun Heo 		n_elem++;
168333480a0eSTejun Heo 	}
16842432697bSTejun Heo 
16852b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
16862b789108STejun Heo 				    timeout);
16872432697bSTejun Heo }
16882432697bSTejun Heo 
16892432697bSTejun Heo /**
1690c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1691c6fd2807SJeff Garzik  *	@adev: ATA device
1692c6fd2807SJeff Garzik  *
1693c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1694c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1695c6fd2807SJeff Garzik  */
1696c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1697c6fd2807SJeff Garzik {
16980d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
16990d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
17000d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
17010d9e6659STejun Heo 	 */
17020d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
17030d9e6659STejun Heo 		return 0;
17040d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
17050d9e6659STejun Heo 	 * check as the caller should know this.
17060d9e6659STejun Heo 	 */
17079af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1708c6fd2807SJeff Garzik 		return 0;
17095c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
17105c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
17115c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
17125c18c4d2SDavid Daney 		return 0;
1713432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1714432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1715c6fd2807SJeff Garzik 		return 1;
1716432729f0SAlan Cox 	/* We turn it on when possible */
1717432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1718432729f0SAlan Cox 		return 1;
1719432729f0SAlan Cox 	return 0;
1720432729f0SAlan Cox }
1721a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
1722c6fd2807SJeff Garzik 
1723432729f0SAlan Cox /**
1724432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1725432729f0SAlan Cox  *	@adev: ATA device
1726432729f0SAlan Cox  *
1727432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1728432729f0SAlan Cox  *	-1 if no iordy mode is available.
1729432729f0SAlan Cox  */
1730432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1731432729f0SAlan Cox {
1732c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1733c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1734432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1735c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1736c6fd2807SJeff Garzik 		if (pio) {
1737c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1738c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1739432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1740432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1741c6fd2807SJeff Garzik 		}
1742c6fd2807SJeff Garzik 	}
1743432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1744c6fd2807SJeff Garzik }
1745c6fd2807SJeff Garzik 
1746c6fd2807SJeff Garzik /**
1747963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1748963e4975SAlan Cox  *	@dev: device
1749963e4975SAlan Cox  *	@tf: proposed taskfile
1750963e4975SAlan Cox  *	@id: data buffer
1751963e4975SAlan Cox  *
1752963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1753963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1754963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1755963e4975SAlan Cox  */
1756963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
1757963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
1758963e4975SAlan Cox {
1759963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1760963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1761963e4975SAlan Cox }
1762a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
1763963e4975SAlan Cox 
1764963e4975SAlan Cox /**
1765c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1766c6fd2807SJeff Garzik  *	@dev: target device
1767c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1768bff04647STejun Heo  *	@flags: ATA_READID_* flags
1769c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1770c6fd2807SJeff Garzik  *
1771c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1772c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1773c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1774c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1775c6fd2807SJeff Garzik  *
177650a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
177750a99018SAlan Cox  *	now we abort if we hit that case.
177850a99018SAlan Cox  *
1779c6fd2807SJeff Garzik  *	LOCKING:
1780c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1781c6fd2807SJeff Garzik  *
1782c6fd2807SJeff Garzik  *	RETURNS:
1783c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1784c6fd2807SJeff Garzik  */
1785c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1786bff04647STejun Heo 		    unsigned int flags, u16 *id)
1787c6fd2807SJeff Garzik {
17889af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1789c6fd2807SJeff Garzik 	unsigned int class = *p_class;
1790c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1791c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
1792c6fd2807SJeff Garzik 	const char *reason;
179379b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
179454936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
1795c6fd2807SJeff Garzik 	int rc;
1796c6fd2807SJeff Garzik 
1797c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
1798a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
1799c6fd2807SJeff Garzik 
1800c6fd2807SJeff Garzik retry:
1801c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1802c6fd2807SJeff Garzik 
1803c6fd2807SJeff Garzik 	switch (class) {
180479b42babSTejun Heo 	case ATA_DEV_SEMB:
180579b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
1806df561f66SGustavo A. R. Silva 		fallthrough;
1807c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
18089162c657SHannes Reinecke 	case ATA_DEV_ZAC:
1809c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1810c6fd2807SJeff Garzik 		break;
1811c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1812c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1813c6fd2807SJeff Garzik 		break;
1814c6fd2807SJeff Garzik 	default:
1815c6fd2807SJeff Garzik 		rc = -ENODEV;
1816c6fd2807SJeff Garzik 		reason = "unsupported class";
1817c6fd2807SJeff Garzik 		goto err_out;
1818c6fd2807SJeff Garzik 	}
1819c6fd2807SJeff Garzik 
1820c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
182181afe893STejun Heo 
182281afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
182381afe893STejun Heo 	 * sure those are properly initialized.
182481afe893STejun Heo 	 */
182581afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
182681afe893STejun Heo 
182781afe893STejun Heo 	/* Device presence detection is unreliable on some
182881afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
182981afe893STejun Heo 	 */
183081afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1831c6fd2807SJeff Garzik 
1832963e4975SAlan Cox 	if (ap->ops->read_id)
1833963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
1834963e4975SAlan Cox 	else
1835963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
1836963e4975SAlan Cox 
1837c6fd2807SJeff Garzik 	if (err_mask) {
1838800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1839a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
184055a8e2c8STejun Heo 			return -ENOENT;
184155a8e2c8STejun Heo 		}
184255a8e2c8STejun Heo 
184379b42babSTejun Heo 		if (is_semb) {
1844a9a79dfeSJoe Perches 			ata_dev_info(dev,
1845a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
184679b42babSTejun Heo 			/* SEMB is not supported yet */
184779b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
184879b42babSTejun Heo 			return 0;
184979b42babSTejun Heo 		}
185079b42babSTejun Heo 
18511ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
18521ffc151fSTejun Heo 			/* Device or controller might have reported
18531ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
18541ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
18551ffc151fSTejun Heo 			 * aborted by the device.
185654936f8bSTejun Heo 			 */
18571ffc151fSTejun Heo 			if (may_fallback) {
185854936f8bSTejun Heo 				may_fallback = 0;
185954936f8bSTejun Heo 
186054936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
186154936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
186254936f8bSTejun Heo 				else
186354936f8bSTejun Heo 					class = ATA_DEV_ATA;
186454936f8bSTejun Heo 				goto retry;
186554936f8bSTejun Heo 			}
186654936f8bSTejun Heo 
18671ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
18681ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
18691ffc151fSTejun Heo 			 * sometimes with phantom devices.
18701ffc151fSTejun Heo 			 */
1871a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
18721ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
18731ffc151fSTejun Heo 			return -ENOENT;
18741ffc151fSTejun Heo 		}
18751ffc151fSTejun Heo 
1876c6fd2807SJeff Garzik 		rc = -EIO;
1877c6fd2807SJeff Garzik 		reason = "I/O error";
1878c6fd2807SJeff Garzik 		goto err_out;
1879c6fd2807SJeff Garzik 	}
1880c6fd2807SJeff Garzik 
188143c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1882a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "dumping IDENTIFY data, "
188343c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
188443c9c591STejun Heo 			    class, may_fallback, tried_spinup);
188543c9c591STejun Heo 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
188643c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
188743c9c591STejun Heo 	}
188843c9c591STejun Heo 
188954936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
189054936f8bSTejun Heo 	 * successfully at least once.
189154936f8bSTejun Heo 	 */
189254936f8bSTejun Heo 	may_fallback = 0;
189354936f8bSTejun Heo 
1894c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1895c6fd2807SJeff Garzik 
1896c6fd2807SJeff Garzik 	/* sanity check */
1897c6fd2807SJeff Garzik 	rc = -EINVAL;
18986070068bSAlan Cox 	reason = "device reports invalid type";
18994a3381feSJeff Garzik 
19009162c657SHannes Reinecke 	if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
19014a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
19024a3381feSJeff Garzik 			goto err_out;
1903db63a4c8SAndy Whitcroft 		if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1904db63a4c8SAndy Whitcroft 							ata_id_is_ata(id)) {
1905db63a4c8SAndy Whitcroft 			ata_dev_dbg(dev,
1906db63a4c8SAndy Whitcroft 				"host indicates ignore ATA devices, ignored\n");
1907db63a4c8SAndy Whitcroft 			return -ENOENT;
1908db63a4c8SAndy Whitcroft 		}
19094a3381feSJeff Garzik 	} else {
19104a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1911c6fd2807SJeff Garzik 			goto err_out;
1912c6fd2807SJeff Garzik 	}
1913c6fd2807SJeff Garzik 
1914169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1915169439c2SMark Lord 		tried_spinup = 1;
1916169439c2SMark Lord 		/*
1917169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
1918169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
1919169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
1920169439c2SMark Lord 		 */
1921218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1922fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
1923169439c2SMark Lord 			rc = -EIO;
1924169439c2SMark Lord 			reason = "SPINUP failed";
1925169439c2SMark Lord 			goto err_out;
1926169439c2SMark Lord 		}
1927169439c2SMark Lord 		/*
1928169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
1929169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
1930169439c2SMark Lord 		 */
1931169439c2SMark Lord 		if (id[2] == 0x37c8)
1932169439c2SMark Lord 			goto retry;
1933169439c2SMark Lord 	}
1934169439c2SMark Lord 
19359162c657SHannes Reinecke 	if ((flags & ATA_READID_POSTRESET) &&
19369162c657SHannes Reinecke 	    (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
1937c6fd2807SJeff Garzik 		/*
1938c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
1939c6fd2807SJeff Garzik 		 * SRST RESET
194050a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
194150a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
1942c6fd2807SJeff Garzik 		 * anything else..
1943c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
194450a99018SAlan Cox 		 *
194550a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
1946c9404c9cSAdam Buchbinder 		 * should never trigger.
1947c6fd2807SJeff Garzik 		 */
1948c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1949c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
1950c6fd2807SJeff Garzik 			if (err_mask) {
1951c6fd2807SJeff Garzik 				rc = -EIO;
1952c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
1953c6fd2807SJeff Garzik 				goto err_out;
1954c6fd2807SJeff Garzik 			}
1955c6fd2807SJeff Garzik 
1956c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
1957c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
1958c6fd2807SJeff Garzik 			 */
1959bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
1960c6fd2807SJeff Garzik 			goto retry;
1961c6fd2807SJeff Garzik 		}
1962c6fd2807SJeff Garzik 	}
1963c6fd2807SJeff Garzik 
1964c6fd2807SJeff Garzik 	*p_class = class;
1965c6fd2807SJeff Garzik 
1966c6fd2807SJeff Garzik 	return 0;
1967c6fd2807SJeff Garzik 
1968c6fd2807SJeff Garzik  err_out:
1969c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
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 /**
1976f01f62c2SChristoph Hellwig  *	ata_read_log_page - read a specific log page
1977f01f62c2SChristoph Hellwig  *	@dev: target device
1978f01f62c2SChristoph Hellwig  *	@log: log to read
1979f01f62c2SChristoph Hellwig  *	@page: page to read
1980f01f62c2SChristoph Hellwig  *	@buf: buffer to store read page
1981f01f62c2SChristoph Hellwig  *	@sectors: number of sectors to read
1982f01f62c2SChristoph Hellwig  *
1983f01f62c2SChristoph Hellwig  *	Read log page using READ_LOG_EXT command.
1984f01f62c2SChristoph Hellwig  *
1985f01f62c2SChristoph Hellwig  *	LOCKING:
1986f01f62c2SChristoph Hellwig  *	Kernel thread context (may sleep).
1987f01f62c2SChristoph Hellwig  *
1988f01f62c2SChristoph Hellwig  *	RETURNS:
1989f01f62c2SChristoph Hellwig  *	0 on success, AC_ERR_* mask otherwise.
1990f01f62c2SChristoph Hellwig  */
1991f01f62c2SChristoph Hellwig unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1992f01f62c2SChristoph Hellwig 			       u8 page, void *buf, unsigned int sectors)
1993f01f62c2SChristoph Hellwig {
1994f01f62c2SChristoph Hellwig 	unsigned long ap_flags = dev->link->ap->flags;
1995f01f62c2SChristoph Hellwig 	struct ata_taskfile tf;
1996f01f62c2SChristoph Hellwig 	unsigned int err_mask;
1997f01f62c2SChristoph Hellwig 	bool dma = false;
1998f01f62c2SChristoph Hellwig 
1999f01f62c2SChristoph Hellwig 	DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
2000f01f62c2SChristoph Hellwig 
2001f01f62c2SChristoph Hellwig 	/*
2002f01f62c2SChristoph Hellwig 	 * Return error without actually issuing the command on controllers
2003f01f62c2SChristoph Hellwig 	 * which e.g. lockup on a read log page.
2004f01f62c2SChristoph Hellwig 	 */
2005f01f62c2SChristoph Hellwig 	if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2006f01f62c2SChristoph Hellwig 		return AC_ERR_DEV;
2007f01f62c2SChristoph Hellwig 
2008f01f62c2SChristoph Hellwig retry:
2009f01f62c2SChristoph Hellwig 	ata_tf_init(dev, &tf);
2010f01f62c2SChristoph Hellwig 	if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) &&
20117cfdfdc8SDamien Le Moal 	    !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
2012f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2013f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_DMA;
2014f01f62c2SChristoph Hellwig 		dma = true;
2015f01f62c2SChristoph Hellwig 	} else {
2016f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_EXT;
2017f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_PIO;
2018f01f62c2SChristoph Hellwig 		dma = false;
2019f01f62c2SChristoph Hellwig 	}
2020f01f62c2SChristoph Hellwig 	tf.lbal = log;
2021f01f62c2SChristoph Hellwig 	tf.lbam = page;
2022f01f62c2SChristoph Hellwig 	tf.nsect = sectors;
2023f01f62c2SChristoph Hellwig 	tf.hob_nsect = sectors >> 8;
2024f01f62c2SChristoph Hellwig 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2025f01f62c2SChristoph Hellwig 
2026f01f62c2SChristoph Hellwig 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2027f01f62c2SChristoph Hellwig 				     buf, sectors * ATA_SECT_SIZE, 0);
2028f01f62c2SChristoph Hellwig 
2029fc5c8aa7SDamien Le Moal 	if (err_mask) {
2030fc5c8aa7SDamien Le Moal 		if (dma) {
20317cfdfdc8SDamien Le Moal 			dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
2032f01f62c2SChristoph Hellwig 			goto retry;
2033f01f62c2SChristoph Hellwig 		}
2034fc5c8aa7SDamien Le Moal 		ata_dev_err(dev, "Read log page 0x%02x failed, Emask 0x%x\n",
2035fc5c8aa7SDamien Le Moal 			    (unsigned int)page, err_mask);
2036fc5c8aa7SDamien Le Moal 	}
2037f01f62c2SChristoph Hellwig 
2038f01f62c2SChristoph Hellwig 	return err_mask;
2039f01f62c2SChristoph Hellwig }
2040f01f62c2SChristoph Hellwig 
2041efe205a3SChristoph Hellwig static bool ata_log_supported(struct ata_device *dev, u8 log)
2042efe205a3SChristoph Hellwig {
2043efe205a3SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2044efe205a3SChristoph Hellwig 
2045efe205a3SChristoph Hellwig 	if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2046efe205a3SChristoph Hellwig 		return false;
2047efe205a3SChristoph Hellwig 	return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2048efe205a3SChristoph Hellwig }
2049efe205a3SChristoph Hellwig 
2050a0fd2454SChristoph Hellwig static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2051a0fd2454SChristoph Hellwig {
2052a0fd2454SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2053a0fd2454SChristoph Hellwig 	unsigned int err, i;
2054a0fd2454SChristoph Hellwig 
2055a0fd2454SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2056a0fd2454SChristoph Hellwig 		ata_dev_warn(dev, "ATA Identify Device Log not supported\n");
2057a0fd2454SChristoph Hellwig 		return false;
2058a0fd2454SChristoph Hellwig 	}
2059a0fd2454SChristoph Hellwig 
2060a0fd2454SChristoph Hellwig 	/*
2061a0fd2454SChristoph Hellwig 	 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2062a0fd2454SChristoph Hellwig 	 * supported.
2063a0fd2454SChristoph Hellwig 	 */
2064a0fd2454SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2065a0fd2454SChristoph Hellwig 				1);
2066fc5c8aa7SDamien Le Moal 	if (err)
2067a0fd2454SChristoph Hellwig 		return false;
2068a0fd2454SChristoph Hellwig 
2069a0fd2454SChristoph Hellwig 	for (i = 0; i < ap->sector_buf[8]; i++) {
2070a0fd2454SChristoph Hellwig 		if (ap->sector_buf[9 + i] == page)
2071a0fd2454SChristoph Hellwig 			return true;
2072a0fd2454SChristoph Hellwig 	}
2073a0fd2454SChristoph Hellwig 
2074a0fd2454SChristoph Hellwig 	return false;
2075a0fd2454SChristoph Hellwig }
2076a0fd2454SChristoph Hellwig 
20779062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
20789062712fSTejun Heo {
20799062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
20809062712fSTejun Heo 	u32 target, target_limit;
20819062712fSTejun Heo 
20829062712fSTejun Heo 	if (!sata_scr_valid(plink))
20839062712fSTejun Heo 		return 0;
20849062712fSTejun Heo 
20859062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
20869062712fSTejun Heo 		target = 1;
20879062712fSTejun Heo 	else
20889062712fSTejun Heo 		return 0;
20899062712fSTejun Heo 
20909062712fSTejun Heo 	target_limit = (1 << target) - 1;
20919062712fSTejun Heo 
20929062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
20939062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
20949062712fSTejun Heo 		return 0;
20959062712fSTejun Heo 
20969062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
20979062712fSTejun Heo 
20989062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
20999062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
21009062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
21019062712fSTejun Heo 	 */
21029062712fSTejun Heo 	if (plink->sata_spd > target) {
2103a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
21049062712fSTejun Heo 			     sata_spd_string(target));
21059062712fSTejun Heo 		return -EAGAIN;
21069062712fSTejun Heo 	}
21079062712fSTejun Heo 	return 0;
21089062712fSTejun Heo }
21099062712fSTejun Heo 
2110c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2111c6fd2807SJeff Garzik {
21129af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
21139ce8e307SJens Axboe 
21149ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
21159ce8e307SJens Axboe 		return 0;
21169ce8e307SJens Axboe 
21179af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2118c6fd2807SJeff Garzik }
2119c6fd2807SJeff Garzik 
21205a233551SHannes Reinecke static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
21215a233551SHannes Reinecke {
21225a233551SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
21235a233551SHannes Reinecke 	unsigned int err_mask;
21245a233551SHannes Reinecke 
2125efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2126efe205a3SChristoph Hellwig 		ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
2127fe5af0ccSHannes Reinecke 		return;
2128fe5af0ccSHannes Reinecke 	}
21295a233551SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
21305a233551SHannes Reinecke 				     0, ap->sector_buf, 1);
2131fc5c8aa7SDamien Le Moal 	if (!err_mask) {
21325a233551SHannes Reinecke 		u8 *cmds = dev->ncq_send_recv_cmds;
21335a233551SHannes Reinecke 
21345a233551SHannes Reinecke 		dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
21355a233551SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
21365a233551SHannes Reinecke 
21375a233551SHannes Reinecke 		if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
21385a233551SHannes Reinecke 			ata_dev_dbg(dev, "disabling queued TRIM support\n");
21395a233551SHannes Reinecke 			cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
21405a233551SHannes Reinecke 				~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
21415a233551SHannes Reinecke 		}
21425a233551SHannes Reinecke 	}
21435a233551SHannes Reinecke }
21445a233551SHannes Reinecke 
2145284b3b77SHannes Reinecke static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2146284b3b77SHannes Reinecke {
2147284b3b77SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
2148284b3b77SHannes Reinecke 	unsigned int err_mask;
2149284b3b77SHannes Reinecke 
2150efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
2151284b3b77SHannes Reinecke 		ata_dev_warn(dev,
2152284b3b77SHannes Reinecke 			     "NCQ Send/Recv Log not supported\n");
2153284b3b77SHannes Reinecke 		return;
2154284b3b77SHannes Reinecke 	}
2155284b3b77SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2156284b3b77SHannes Reinecke 				     0, ap->sector_buf, 1);
2157fc5c8aa7SDamien Le Moal 	if (!err_mask) {
2158284b3b77SHannes Reinecke 		u8 *cmds = dev->ncq_non_data_cmds;
2159284b3b77SHannes Reinecke 
2160284b3b77SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2161284b3b77SHannes Reinecke 	}
2162284b3b77SHannes Reinecke }
2163284b3b77SHannes Reinecke 
21648e061784SAdam Manzanares static void ata_dev_config_ncq_prio(struct ata_device *dev)
21658e061784SAdam Manzanares {
21668e061784SAdam Manzanares 	struct ata_port *ap = dev->link->ap;
21678e061784SAdam Manzanares 	unsigned int err_mask;
21688e061784SAdam Manzanares 
21698e061784SAdam Manzanares 	err_mask = ata_read_log_page(dev,
21701d51d5f3SChristoph Hellwig 				     ATA_LOG_IDENTIFY_DEVICE,
21718e061784SAdam Manzanares 				     ATA_LOG_SATA_SETTINGS,
21728e061784SAdam Manzanares 				     ap->sector_buf,
21738e061784SAdam Manzanares 				     1);
2174fc5c8aa7SDamien Le Moal 	if (err_mask)
21752360fa18SDamien Le Moal 		goto not_supported;
21768e061784SAdam Manzanares 
21772360fa18SDamien Le Moal 	if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
21782360fa18SDamien Le Moal 		goto not_supported;
21792360fa18SDamien Le Moal 
21808e061784SAdam Manzanares 	dev->flags |= ATA_DFLAG_NCQ_PRIO;
21818e061784SAdam Manzanares 
21822360fa18SDamien Le Moal 	return;
21832360fa18SDamien Le Moal 
21842360fa18SDamien Le Moal not_supported:
21852360fa18SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
21862360fa18SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
21878e061784SAdam Manzanares }
21888e061784SAdam Manzanares 
2189388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2190c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2191c6fd2807SJeff Garzik {
21929af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2193c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2194388539f3SShaohua Li 	unsigned int err_mask;
2195388539f3SShaohua Li 	char *aa_desc = "";
2196c6fd2807SJeff Garzik 
2197c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2198c6fd2807SJeff Garzik 		desc[0] = '\0';
2199388539f3SShaohua Li 		return 0;
2200c6fd2807SJeff Garzik 	}
2201cba97ea1SBartlomiej Zolnierkiewicz 	if (!IS_ENABLED(CONFIG_SATA_HOST))
2202cba97ea1SBartlomiej Zolnierkiewicz 		return 0;
220375683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
22046919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2205388539f3SShaohua Li 		return 0;
22066919a0a6SAlan Cox 	}
2207c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
220869278f79SJens Axboe 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
2209c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2210c6fd2807SJeff Garzik 	}
2211c6fd2807SJeff Garzik 
2212388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2213388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2214388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2215388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2216388539f3SShaohua Li 			SATA_FPDMA_AA);
2217388539f3SShaohua Li 		if (err_mask) {
2218a9a79dfeSJoe Perches 			ata_dev_err(dev,
2219a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2220a9a79dfeSJoe Perches 				    err_mask);
2221388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2222388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2223388539f3SShaohua Li 				return -EIO;
2224388539f3SShaohua Li 			}
2225388539f3SShaohua Li 		} else
2226388539f3SShaohua Li 			aa_desc = ", AA";
2227388539f3SShaohua Li 	}
2228388539f3SShaohua Li 
2229c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2230388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2231c6fd2807SJeff Garzik 	else
2232388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2233388539f3SShaohua Li 			ddepth, aa_desc);
2234ed36911cSMarc Carino 
2235284b3b77SHannes Reinecke 	if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2236284b3b77SHannes Reinecke 		if (ata_id_has_ncq_send_and_recv(dev->id))
22375a233551SHannes Reinecke 			ata_dev_config_ncq_send_recv(dev);
2238284b3b77SHannes Reinecke 		if (ata_id_has_ncq_non_data(dev->id))
2239284b3b77SHannes Reinecke 			ata_dev_config_ncq_non_data(dev);
22408e061784SAdam Manzanares 		if (ata_id_has_ncq_prio(dev->id))
22418e061784SAdam Manzanares 			ata_dev_config_ncq_prio(dev);
2242ed36911cSMarc Carino 	}
2243ed36911cSMarc Carino 
2244388539f3SShaohua Li 	return 0;
2245c6fd2807SJeff Garzik }
2246c6fd2807SJeff Garzik 
2247e87fd28cSHannes Reinecke static void ata_dev_config_sense_reporting(struct ata_device *dev)
2248e87fd28cSHannes Reinecke {
2249e87fd28cSHannes Reinecke 	unsigned int err_mask;
2250e87fd28cSHannes Reinecke 
2251e87fd28cSHannes Reinecke 	if (!ata_id_has_sense_reporting(dev->id))
2252e87fd28cSHannes Reinecke 		return;
2253e87fd28cSHannes Reinecke 
2254e87fd28cSHannes Reinecke 	if (ata_id_sense_reporting_enabled(dev->id))
2255e87fd28cSHannes Reinecke 		return;
2256e87fd28cSHannes Reinecke 
2257e87fd28cSHannes Reinecke 	err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2258e87fd28cSHannes Reinecke 	if (err_mask) {
2259e87fd28cSHannes Reinecke 		ata_dev_dbg(dev,
2260e87fd28cSHannes Reinecke 			    "failed to enable Sense Data Reporting, Emask 0x%x\n",
2261e87fd28cSHannes Reinecke 			    err_mask);
2262e87fd28cSHannes Reinecke 	}
2263e87fd28cSHannes Reinecke }
2264e87fd28cSHannes Reinecke 
22656d1003aeSHannes Reinecke static void ata_dev_config_zac(struct ata_device *dev)
22666d1003aeSHannes Reinecke {
22676d1003aeSHannes Reinecke 	struct ata_port *ap = dev->link->ap;
22686d1003aeSHannes Reinecke 	unsigned int err_mask;
22696d1003aeSHannes Reinecke 	u8 *identify_buf = ap->sector_buf;
22706d1003aeSHannes Reinecke 
22716d1003aeSHannes Reinecke 	dev->zac_zones_optimal_open = U32_MAX;
22726d1003aeSHannes Reinecke 	dev->zac_zones_optimal_nonseq = U32_MAX;
22736d1003aeSHannes Reinecke 	dev->zac_zones_max_open = U32_MAX;
22746d1003aeSHannes Reinecke 
22756d1003aeSHannes Reinecke 	/*
22766d1003aeSHannes Reinecke 	 * Always set the 'ZAC' flag for Host-managed devices.
22776d1003aeSHannes Reinecke 	 */
22786d1003aeSHannes Reinecke 	if (dev->class == ATA_DEV_ZAC)
22796d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
22806d1003aeSHannes Reinecke 	else if (ata_id_zoned_cap(dev->id) == 0x01)
22816d1003aeSHannes Reinecke 		/*
22826d1003aeSHannes Reinecke 		 * Check for host-aware devices.
22836d1003aeSHannes Reinecke 		 */
22846d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
22856d1003aeSHannes Reinecke 
22866d1003aeSHannes Reinecke 	if (!(dev->flags & ATA_DFLAG_ZAC))
22876d1003aeSHannes Reinecke 		return;
22886d1003aeSHannes Reinecke 
2289a0fd2454SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
22906d1003aeSHannes Reinecke 		ata_dev_warn(dev,
22916d1003aeSHannes Reinecke 			     "ATA Zoned Information Log not supported\n");
22926d1003aeSHannes Reinecke 		return;
22936d1003aeSHannes Reinecke 	}
22946d1003aeSHannes Reinecke 
22956d1003aeSHannes Reinecke 	/*
22966d1003aeSHannes Reinecke 	 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
22976d1003aeSHannes Reinecke 	 */
22981d51d5f3SChristoph Hellwig 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
22996d1003aeSHannes Reinecke 				     ATA_LOG_ZONED_INFORMATION,
23006d1003aeSHannes Reinecke 				     identify_buf, 1);
23016d1003aeSHannes Reinecke 	if (!err_mask) {
23026d1003aeSHannes Reinecke 		u64 zoned_cap, opt_open, opt_nonseq, max_open;
23036d1003aeSHannes Reinecke 
23046d1003aeSHannes Reinecke 		zoned_cap = get_unaligned_le64(&identify_buf[8]);
23056d1003aeSHannes Reinecke 		if ((zoned_cap >> 63))
23066d1003aeSHannes Reinecke 			dev->zac_zoned_cap = (zoned_cap & 1);
23076d1003aeSHannes Reinecke 		opt_open = get_unaligned_le64(&identify_buf[24]);
23086d1003aeSHannes Reinecke 		if ((opt_open >> 63))
23096d1003aeSHannes Reinecke 			dev->zac_zones_optimal_open = (u32)opt_open;
23106d1003aeSHannes Reinecke 		opt_nonseq = get_unaligned_le64(&identify_buf[32]);
23116d1003aeSHannes Reinecke 		if ((opt_nonseq >> 63))
23126d1003aeSHannes Reinecke 			dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
23136d1003aeSHannes Reinecke 		max_open = get_unaligned_le64(&identify_buf[40]);
23146d1003aeSHannes Reinecke 		if ((max_open >> 63))
23156d1003aeSHannes Reinecke 			dev->zac_zones_max_open = (u32)max_open;
23166d1003aeSHannes Reinecke 	}
23176d1003aeSHannes Reinecke }
23186d1003aeSHannes Reinecke 
2319818831c8SChristoph Hellwig static void ata_dev_config_trusted(struct ata_device *dev)
2320818831c8SChristoph Hellwig {
2321818831c8SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2322818831c8SChristoph Hellwig 	u64 trusted_cap;
2323818831c8SChristoph Hellwig 	unsigned int err;
2324818831c8SChristoph Hellwig 
2325e8f11db9SChristoph Hellwig 	if (!ata_id_has_trusted(dev->id))
2326e8f11db9SChristoph Hellwig 		return;
2327e8f11db9SChristoph Hellwig 
2328818831c8SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2329818831c8SChristoph Hellwig 		ata_dev_warn(dev,
2330818831c8SChristoph Hellwig 			     "Security Log not supported\n");
2331818831c8SChristoph Hellwig 		return;
2332818831c8SChristoph Hellwig 	}
2333818831c8SChristoph Hellwig 
2334818831c8SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2335818831c8SChristoph Hellwig 			ap->sector_buf, 1);
2336fc5c8aa7SDamien Le Moal 	if (err)
2337818831c8SChristoph Hellwig 		return;
2338818831c8SChristoph Hellwig 
2339818831c8SChristoph Hellwig 	trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2340818831c8SChristoph Hellwig 	if (!(trusted_cap & (1ULL << 63))) {
2341818831c8SChristoph Hellwig 		ata_dev_dbg(dev,
2342818831c8SChristoph Hellwig 			    "Trusted Computing capability qword not valid!\n");
2343818831c8SChristoph Hellwig 		return;
2344818831c8SChristoph Hellwig 	}
2345818831c8SChristoph Hellwig 
2346818831c8SChristoph Hellwig 	if (trusted_cap & (1 << 0))
2347818831c8SChristoph Hellwig 		dev->flags |= ATA_DFLAG_TRUSTED;
2348818831c8SChristoph Hellwig }
2349818831c8SChristoph Hellwig 
2350891fd7c6SDamien Le Moal static int ata_dev_config_lba(struct ata_device *dev)
2351891fd7c6SDamien Le Moal {
2352891fd7c6SDamien Le Moal 	struct ata_port *ap = dev->link->ap;
2353891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2354891fd7c6SDamien Le Moal 	const char *lba_desc;
2355891fd7c6SDamien Le Moal 	char ncq_desc[24];
2356891fd7c6SDamien Le Moal 	int ret;
2357891fd7c6SDamien Le Moal 
2358891fd7c6SDamien Le Moal 	dev->flags |= ATA_DFLAG_LBA;
2359891fd7c6SDamien Le Moal 
2360891fd7c6SDamien Le Moal 	if (ata_id_has_lba48(id)) {
2361891fd7c6SDamien Le Moal 		lba_desc = "LBA48";
2362891fd7c6SDamien Le Moal 		dev->flags |= ATA_DFLAG_LBA48;
2363891fd7c6SDamien Le Moal 		if (dev->n_sectors >= (1UL << 28) &&
2364891fd7c6SDamien Le Moal 		    ata_id_has_flush_ext(id))
2365891fd7c6SDamien Le Moal 			dev->flags |= ATA_DFLAG_FLUSH_EXT;
2366891fd7c6SDamien Le Moal 	} else {
2367891fd7c6SDamien Le Moal 		lba_desc = "LBA";
2368891fd7c6SDamien Le Moal 	}
2369891fd7c6SDamien Le Moal 
2370891fd7c6SDamien Le Moal 	/* config NCQ */
2371891fd7c6SDamien Le Moal 	ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2372891fd7c6SDamien Le Moal 
2373891fd7c6SDamien Le Moal 	/* print device info to dmesg */
2374891fd7c6SDamien Le Moal 	if (ata_msg_drv(ap) && ata_dev_print_info(dev))
2375891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2376891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u: %s %s\n",
2377891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2378891fd7c6SDamien Le Moal 			     dev->multi_count, lba_desc, ncq_desc);
2379891fd7c6SDamien Le Moal 
2380891fd7c6SDamien Le Moal 	return ret;
2381891fd7c6SDamien Le Moal }
2382891fd7c6SDamien Le Moal 
2383891fd7c6SDamien Le Moal static void ata_dev_config_chs(struct ata_device *dev)
2384891fd7c6SDamien Le Moal {
2385891fd7c6SDamien Le Moal 	struct ata_port *ap = dev->link->ap;
2386891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2387891fd7c6SDamien Le Moal 
2388891fd7c6SDamien Le Moal 	if (ata_id_current_chs_valid(id)) {
2389891fd7c6SDamien Le Moal 		/* Current CHS translation is valid. */
2390891fd7c6SDamien Le Moal 		dev->cylinders = id[54];
2391891fd7c6SDamien Le Moal 		dev->heads     = id[55];
2392891fd7c6SDamien Le Moal 		dev->sectors   = id[56];
2393891fd7c6SDamien Le Moal 	} else {
2394891fd7c6SDamien Le Moal 		/* Default translation */
2395891fd7c6SDamien Le Moal 		dev->cylinders	= id[1];
2396891fd7c6SDamien Le Moal 		dev->heads	= id[3];
2397891fd7c6SDamien Le Moal 		dev->sectors	= id[6];
2398891fd7c6SDamien Le Moal 	}
2399891fd7c6SDamien Le Moal 
2400891fd7c6SDamien Le Moal 	/* print device info to dmesg */
2401891fd7c6SDamien Le Moal 	if (ata_msg_drv(ap) && ata_dev_print_info(dev))
2402891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2403891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u, CHS %u/%u/%u\n",
2404891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2405891fd7c6SDamien Le Moal 			     dev->multi_count, dev->cylinders,
2406891fd7c6SDamien Le Moal 			     dev->heads, dev->sectors);
2407891fd7c6SDamien Le Moal }
2408891fd7c6SDamien Le Moal 
2409d8d8778cSDamien Le Moal static void ata_dev_config_devslp(struct ata_device *dev)
2410d8d8778cSDamien Le Moal {
2411d8d8778cSDamien Le Moal 	u8 *sata_setting = dev->link->ap->sector_buf;
2412d8d8778cSDamien Le Moal 	unsigned int err_mask;
2413d8d8778cSDamien Le Moal 	int i, j;
2414d8d8778cSDamien Le Moal 
2415d8d8778cSDamien Le Moal 	/*
2416d8d8778cSDamien Le Moal 	 * Check device sleep capability. Get DevSlp timing variables
2417d8d8778cSDamien Le Moal 	 * from SATA Settings page of Identify Device Data Log.
2418d8d8778cSDamien Le Moal 	 */
2419d8d8778cSDamien Le Moal 	if (!ata_id_has_devslp(dev->id))
2420d8d8778cSDamien Le Moal 		return;
2421d8d8778cSDamien Le Moal 
2422d8d8778cSDamien Le Moal 	err_mask = ata_read_log_page(dev,
2423d8d8778cSDamien Le Moal 				     ATA_LOG_IDENTIFY_DEVICE,
2424d8d8778cSDamien Le Moal 				     ATA_LOG_SATA_SETTINGS,
2425d8d8778cSDamien Le Moal 				     sata_setting, 1);
2426fc5c8aa7SDamien Le Moal 	if (err_mask)
2427d8d8778cSDamien Le Moal 		return;
2428d8d8778cSDamien Le Moal 
2429d8d8778cSDamien Le Moal 	dev->flags |= ATA_DFLAG_DEVSLP;
2430d8d8778cSDamien Le Moal 	for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2431d8d8778cSDamien Le Moal 		j = ATA_LOG_DEVSLP_OFFSET + i;
2432d8d8778cSDamien Le Moal 		dev->devslp_timing[i] = sata_setting[j];
2433d8d8778cSDamien Le Moal 	}
2434d8d8778cSDamien Le Moal }
2435d8d8778cSDamien Le Moal 
2436*d633b8a7SDamien Le Moal static void ata_dev_print_features(struct ata_device *dev)
2437*d633b8a7SDamien Le Moal {
2438*d633b8a7SDamien Le Moal 	if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
2439*d633b8a7SDamien Le Moal 		return;
2440*d633b8a7SDamien Le Moal 
2441*d633b8a7SDamien Le Moal 	ata_dev_info(dev,
2442*d633b8a7SDamien Le Moal 		     "Features:%s%s%s%s%s\n",
2443*d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
2444*d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
2445*d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
2446*d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
2447*d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "");
2448*d633b8a7SDamien Le Moal }
2449*d633b8a7SDamien Le Moal 
2450c6fd2807SJeff Garzik /**
2451c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2452c6fd2807SJeff Garzik  *	@dev: Target device to configure
2453c6fd2807SJeff Garzik  *
2454c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2455c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2456c6fd2807SJeff Garzik  *
2457c6fd2807SJeff Garzik  *	LOCKING:
2458c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2459c6fd2807SJeff Garzik  *
2460c6fd2807SJeff Garzik  *	RETURNS:
2461c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2462c6fd2807SJeff Garzik  */
2463efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2464c6fd2807SJeff Garzik {
24659af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2466891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
2467c6fd2807SJeff Garzik 	const u16 *id = dev->id;
24687dc951aeSTejun Heo 	unsigned long xfer_mask;
246965fe1f0fSShane Huang 	unsigned int err_mask;
2470b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
24713f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
24723f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2473c6fd2807SJeff Garzik 	int rc;
2474c6fd2807SJeff Garzik 
2475c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2476a9a79dfeSJoe Perches 		ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2477c6fd2807SJeff Garzik 		return 0;
2478c6fd2807SJeff Garzik 	}
2479c6fd2807SJeff Garzik 
2480c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2481a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2482c6fd2807SJeff Garzik 
248375683fe7STejun Heo 	/* set horkage */
248475683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
248533267325STejun Heo 	ata_force_horkage(dev);
248675683fe7STejun Heo 
248750af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2488a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
248950af2fa1STejun Heo 		ata_dev_disable(dev);
249050af2fa1STejun Heo 		return 0;
249150af2fa1STejun Heo 	}
249250af2fa1STejun Heo 
24932486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
24942486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2495a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
24962486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
24972486fa56STejun Heo 			     : "disabled");
24982486fa56STejun Heo 		ata_dev_disable(dev);
24992486fa56STejun Heo 		return 0;
25002486fa56STejun Heo 	}
25012486fa56STejun Heo 
25029062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
25039062712fSTejun Heo 	if (rc)
25049062712fSTejun Heo 		return rc;
25059062712fSTejun Heo 
2506ecd75ad5STejun Heo 	/* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2507ecd75ad5STejun Heo 	if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2508ecd75ad5STejun Heo 	    (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2509ecd75ad5STejun Heo 		dev->horkage |= ATA_HORKAGE_NOLPM;
2510ecd75ad5STejun Heo 
2511240630e6SHans de Goede 	if (ap->flags & ATA_FLAG_NO_LPM)
2512240630e6SHans de Goede 		dev->horkage |= ATA_HORKAGE_NOLPM;
2513240630e6SHans de Goede 
2514ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2515ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2516ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2517ecd75ad5STejun Heo 	}
2518ecd75ad5STejun Heo 
25196746544cSTejun Heo 	/* let ACPI work its magic */
25206746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
25216746544cSTejun Heo 	if (rc)
25226746544cSTejun Heo 		return rc;
252308573a86SKristen Carlson Accardi 
252405027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
252505027adcSTejun Heo 	rc = ata_hpa_resize(dev);
252605027adcSTejun Heo 	if (rc)
252705027adcSTejun Heo 		return rc;
252805027adcSTejun Heo 
2529c6fd2807SJeff Garzik 	/* print device capabilities */
2530c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2531a9a79dfeSJoe Perches 		ata_dev_dbg(dev,
2532c6fd2807SJeff Garzik 			    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2533c6fd2807SJeff Garzik 			    "85:%04x 86:%04x 87:%04x 88:%04x\n",
25347f5e4e8dSHarvey Harrison 			    __func__,
2535c6fd2807SJeff Garzik 			    id[49], id[82], id[83], id[84],
2536c6fd2807SJeff Garzik 			    id[85], id[86], id[87], id[88]);
2537c6fd2807SJeff Garzik 
2538c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2539c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2540c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2541c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2542c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2543c6fd2807SJeff Garzik 	dev->cylinders = 0;
2544c6fd2807SJeff Garzik 	dev->heads = 0;
2545c6fd2807SJeff Garzik 	dev->sectors = 0;
2546e18086d6SMark Lord 	dev->multi_count = 0;
2547c6fd2807SJeff Garzik 
2548c6fd2807SJeff Garzik 	/*
2549c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2550c6fd2807SJeff Garzik 	 */
2551c6fd2807SJeff Garzik 
2552c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2553c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2554c6fd2807SJeff Garzik 
2555c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2556c6fd2807SJeff Garzik 		ata_dump_id(id);
2557c6fd2807SJeff Garzik 
2558ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2559ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2560ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2561ef143d57SAlbert Lee 
2562ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2563ef143d57SAlbert Lee 			sizeof(modelbuf));
2564ef143d57SAlbert Lee 
2565c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
25669162c657SHannes Reinecke 	if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2567b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
256862afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
256962afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2570a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2571a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2572b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2573ae8d4ee7SAlan Cox 		} else {
2574b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2575ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2576ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2577a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2578a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2579ae8d4ee7SAlan Cox 		}
2580b352e57dSAlan Cox 
2581c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2582c6fd2807SJeff Garzik 
2583e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2584e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2585e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2586e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2587e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2588e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2589e18086d6SMark Lord 				if (cnt <= max)
2590e18086d6SMark Lord 					dev->multi_count = cnt;
2591e18086d6SMark Lord 		}
25923f64f565SEric D. Mudama 
2593891fd7c6SDamien Le Moal 		/* print device info to dmesg */
2594891fd7c6SDamien Le Moal 		if (ata_msg_drv(ap) && print_info)
2595891fd7c6SDamien Le Moal 			ata_dev_info(dev, "%s: %s, %s, max %s\n",
2596891fd7c6SDamien Le Moal 				     revbuf, modelbuf, fwrevbuf,
2597891fd7c6SDamien Le Moal 				     ata_mode_string(xfer_mask));
2598891fd7c6SDamien Le Moal 
2599c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2600891fd7c6SDamien Le Moal 			rc = ata_dev_config_lba(dev);
2601388539f3SShaohua Li 			if (rc)
2602388539f3SShaohua Li 				return rc;
2603c6fd2807SJeff Garzik 		} else {
2604891fd7c6SDamien Le Moal 			ata_dev_config_chs(dev);
2605c6fd2807SJeff Garzik 		}
2606c6fd2807SJeff Garzik 
2607d8d8778cSDamien Le Moal 		ata_dev_config_devslp(dev);
2608e87fd28cSHannes Reinecke 		ata_dev_config_sense_reporting(dev);
26096d1003aeSHannes Reinecke 		ata_dev_config_zac(dev);
2610818831c8SChristoph Hellwig 		ata_dev_config_trusted(dev);
2611b1ffbf85SMinwoo Im 		dev->cdb_len = 32;
2612*d633b8a7SDamien Le Moal 
2613*d633b8a7SDamien Le Moal 		if (ata_msg_drv(ap) && print_info)
2614*d633b8a7SDamien Le Moal 			ata_dev_print_features(dev);
2615c6fd2807SJeff Garzik 	}
2616c6fd2807SJeff Garzik 
2617c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2618c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2619854c73a2STejun Heo 		const char *cdb_intr_string = "";
2620854c73a2STejun Heo 		const char *atapi_an_string = "";
262191163006STejun Heo 		const char *dma_dir_string = "";
26227d77b247STejun Heo 		u32 sntf;
2623c6fd2807SJeff Garzik 
2624c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2625c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2626c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2627a9a79dfeSJoe Perches 				ata_dev_warn(dev, "unsupported CDB len\n");
2628c6fd2807SJeff Garzik 			rc = -EINVAL;
2629c6fd2807SJeff Garzik 			goto err_out_nosup;
2630c6fd2807SJeff Garzik 		}
2631c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2632c6fd2807SJeff Garzik 
26337d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
26347d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
26357d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
26367d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
26379f45cbd3SKristen Carlson Accardi 		 */
2638e7ecd435STejun Heo 		if (atapi_an &&
2639e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2640071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
26417d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
26429f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2643218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2644218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2645854c73a2STejun Heo 			if (err_mask)
2646a9a79dfeSJoe Perches 				ata_dev_err(dev,
2647a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2648a9a79dfeSJoe Perches 					    err_mask);
2649854c73a2STejun Heo 			else {
26509f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2651854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2652854c73a2STejun Heo 			}
26539f45cbd3SKristen Carlson Accardi 		}
26549f45cbd3SKristen Carlson Accardi 
2655c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2656c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2657c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2658c6fd2807SJeff Garzik 		}
2659c6fd2807SJeff Garzik 
2660966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
266191163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
266291163006STejun Heo 			dma_dir_string = ", DMADIR";
266391163006STejun Heo 		}
266491163006STejun Heo 
2665afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
2666b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2667afe75951SAaron Lu 			zpodd_init(dev);
2668afe75951SAaron Lu 		}
2669b1354cbbSLin Ming 
2670c6fd2807SJeff Garzik 		/* print device info to dmesg */
2671c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2672a9a79dfeSJoe Perches 			ata_dev_info(dev,
267391163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2674ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2675c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
267691163006STejun Heo 				     cdb_intr_string, atapi_an_string,
267791163006STejun Heo 				     dma_dir_string);
2678c6fd2807SJeff Garzik 	}
2679c6fd2807SJeff Garzik 
2680914ed354STejun Heo 	/* determine max_sectors */
2681914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2682914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2683914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2684914ed354STejun Heo 
2685c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2686c5038fc0SAlan Cox 	   200 sectors */
2687c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2688c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2689a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2690c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2691c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2692c6fd2807SJeff Garzik 	}
2693c6fd2807SJeff Garzik 
2694f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2695f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2696f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2697f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2698f442cd86SAlbert Lee 	}
2699f8d8e579STony Battersby 
270075683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
270103ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
270203ec52deSTejun Heo 					 dev->max_sectors);
270318d6e9d5SAlbert Lee 
2704af34d637SDavid Milburn 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2705af34d637SDavid Milburn 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2706af34d637SDavid Milburn 					 dev->max_sectors);
2707af34d637SDavid Milburn 
2708a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2709a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2710a32450e1SShan Hai 
2711c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2712cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2713c6fd2807SJeff Garzik 
2714c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2715c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2716c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2717c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2718c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2719c5038fc0SAlan Cox 		   bugs */
2720c5038fc0SAlan Cox 
2721c5038fc0SAlan Cox 		if (print_info) {
2722a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2723c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2724a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2725c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2726c5038fc0SAlan Cox 		}
2727c5038fc0SAlan Cox 	}
2728c5038fc0SAlan Cox 
2729ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2730a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2731a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2732ac70a964STejun Heo 	}
2733ac70a964STejun Heo 
2734c6fd2807SJeff Garzik 	return 0;
2735c6fd2807SJeff Garzik 
2736c6fd2807SJeff Garzik err_out_nosup:
2737c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2738a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2739c6fd2807SJeff Garzik 	return rc;
2740c6fd2807SJeff Garzik }
2741c6fd2807SJeff Garzik 
2742c6fd2807SJeff Garzik /**
27432e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2744be0d18dfSAlan Cox  *	@ap: port
2745be0d18dfSAlan Cox  *
27462e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2747be0d18dfSAlan Cox  *	detection.
2748be0d18dfSAlan Cox  */
2749be0d18dfSAlan Cox 
2750be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2751be0d18dfSAlan Cox {
2752be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2753be0d18dfSAlan Cox }
2754a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_40wire);
2755be0d18dfSAlan Cox 
2756be0d18dfSAlan Cox /**
27572e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2758be0d18dfSAlan Cox  *	@ap: port
2759be0d18dfSAlan Cox  *
27602e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2761be0d18dfSAlan Cox  *	detection.
2762be0d18dfSAlan Cox  */
2763be0d18dfSAlan Cox 
2764be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2765be0d18dfSAlan Cox {
2766be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2767be0d18dfSAlan Cox }
2768a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_80wire);
2769be0d18dfSAlan Cox 
2770be0d18dfSAlan Cox /**
2771be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2772be0d18dfSAlan Cox  *	@ap: port
2773be0d18dfSAlan Cox  *
2774be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2775be0d18dfSAlan Cox  */
2776be0d18dfSAlan Cox 
2777be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2778be0d18dfSAlan Cox {
2779be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2780be0d18dfSAlan Cox }
2781a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_unknown);
2782be0d18dfSAlan Cox 
2783be0d18dfSAlan Cox /**
2784c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2785c88f90c3STejun Heo  *	@ap: port
2786c88f90c3STejun Heo  *
2787c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2788c88f90c3STejun Heo  *	transfer mode.
2789c88f90c3STejun Heo  */
2790c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2791c88f90c3STejun Heo {
2792c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2793c88f90c3STejun Heo }
2794a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_ignore);
2795c88f90c3STejun Heo 
2796c88f90c3STejun Heo /**
2797be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2798be0d18dfSAlan Cox  *	@ap: port
2799be0d18dfSAlan Cox  *
2800be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2801be0d18dfSAlan Cox  */
2802be0d18dfSAlan Cox 
2803be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2804be0d18dfSAlan Cox {
2805be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2806be0d18dfSAlan Cox }
2807a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_sata);
2808be0d18dfSAlan Cox 
2809be0d18dfSAlan Cox /**
2810c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2811c6fd2807SJeff Garzik  *	@ap: Bus to probe
2812c6fd2807SJeff Garzik  *
2813c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2814c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2815c6fd2807SJeff Garzik  *	the bus.
2816c6fd2807SJeff Garzik  *
2817c6fd2807SJeff Garzik  *	LOCKING:
2818c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2819c6fd2807SJeff Garzik  *
2820c6fd2807SJeff Garzik  *	RETURNS:
2821c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2822c6fd2807SJeff Garzik  */
2823c6fd2807SJeff Garzik 
2824c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2825c6fd2807SJeff Garzik {
2826c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2827c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2828f58229f8STejun Heo 	int rc;
2829c6fd2807SJeff Garzik 	struct ata_device *dev;
2830c6fd2807SJeff Garzik 
28311eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2832f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2833c6fd2807SJeff Garzik 
2834c6fd2807SJeff Garzik  retry:
28351eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2836cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2837cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2838cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2839cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2840cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2841cdeab114STejun Heo 		 * bus as we may be talking too fast.
2842cdeab114STejun Heo 		 */
2843cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
28445416912aSAaron Lu 		dev->dma_mode = 0xff;
2845cdeab114STejun Heo 
2846cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2847cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2848cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2849cdeab114STejun Heo 		 * configuring devices.
2850cdeab114STejun Heo 		 */
2851cdeab114STejun Heo 		if (ap->ops->set_piomode)
2852cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2853cdeab114STejun Heo 	}
2854cdeab114STejun Heo 
2855c6fd2807SJeff Garzik 	/* reset and determine device classes */
2856c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2857c6fd2807SJeff Garzik 
28581eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
28593e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2860c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2861c6fd2807SJeff Garzik 		else
2862c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2863c6fd2807SJeff Garzik 
2864c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2865c6fd2807SJeff Garzik 	}
2866c6fd2807SJeff Garzik 
2867f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2868f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2869f31f0cc2SJeff Garzik 	   the slave device */
2870f31f0cc2SJeff Garzik 
28711eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2872f58229f8STejun Heo 		if (tries[dev->devno])
2873f58229f8STejun Heo 			dev->class = classes[dev->devno];
2874c6fd2807SJeff Garzik 
2875c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2876c6fd2807SJeff Garzik 			continue;
2877c6fd2807SJeff Garzik 
2878bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2879bff04647STejun Heo 				     dev->id);
2880c6fd2807SJeff Garzik 		if (rc)
2881c6fd2807SJeff Garzik 			goto fail;
2882f31f0cc2SJeff Garzik 	}
2883f31f0cc2SJeff Garzik 
2884be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2885be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2886be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2887be0d18dfSAlan Cox 
28881eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
28891eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
28901eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
28911eca4365STejun Heo 	 * of the link the bridge is which is a problem.
28921eca4365STejun Heo 	 */
28931eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2894614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2895614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2896614fe29bSAlan Cox 
2897f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2898f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2899f31f0cc2SJeff Garzik 
29001eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
29019af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2902efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
29039af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2904c6fd2807SJeff Garzik 		if (rc)
2905c6fd2807SJeff Garzik 			goto fail;
2906c6fd2807SJeff Garzik 	}
2907c6fd2807SJeff Garzik 
2908c6fd2807SJeff Garzik 	/* configure transfer mode */
29090260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
29104ae72a1eSTejun Heo 	if (rc)
2911c6fd2807SJeff Garzik 		goto fail;
2912c6fd2807SJeff Garzik 
29131eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2914c6fd2807SJeff Garzik 		return 0;
2915c6fd2807SJeff Garzik 
2916c6fd2807SJeff Garzik 	return -ENODEV;
2917c6fd2807SJeff Garzik 
2918c6fd2807SJeff Garzik  fail:
29194ae72a1eSTejun Heo 	tries[dev->devno]--;
29204ae72a1eSTejun Heo 
2921c6fd2807SJeff Garzik 	switch (rc) {
2922c6fd2807SJeff Garzik 	case -EINVAL:
29234ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2924c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2925c6fd2807SJeff Garzik 		break;
29264ae72a1eSTejun Heo 
29274ae72a1eSTejun Heo 	case -ENODEV:
29284ae72a1eSTejun Heo 		/* give it just one more chance */
29294ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2930df561f66SGustavo A. R. Silva 		fallthrough;
2931c6fd2807SJeff Garzik 	case -EIO:
29324ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
29334ae72a1eSTejun Heo 			/* This is the last chance, better to slow
29344ae72a1eSTejun Heo 			 * down than lose it.
29354ae72a1eSTejun Heo 			 */
2936a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
29374ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
29384ae72a1eSTejun Heo 		}
2939c6fd2807SJeff Garzik 	}
2940c6fd2807SJeff Garzik 
29414ae72a1eSTejun Heo 	if (!tries[dev->devno])
2942c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2943c6fd2807SJeff Garzik 
2944c6fd2807SJeff Garzik 	goto retry;
2945c6fd2807SJeff Garzik }
2946c6fd2807SJeff Garzik 
2947c6fd2807SJeff Garzik /**
2948c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2949936fd732STejun Heo  *	@link: SATA link to printk link status about
2950c6fd2807SJeff Garzik  *
2951c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2952c6fd2807SJeff Garzik  *
2953c6fd2807SJeff Garzik  *	LOCKING:
2954c6fd2807SJeff Garzik  *	None.
2955c6fd2807SJeff Garzik  */
29566bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2957c6fd2807SJeff Garzik {
2958c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2959c6fd2807SJeff Garzik 
2960936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2961c6fd2807SJeff Garzik 		return;
2962936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2963c6fd2807SJeff Garzik 
2964b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2965c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2966a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
2967c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
2968c6fd2807SJeff Garzik 	} else {
2969a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
2970c6fd2807SJeff Garzik 			      sstatus, scontrol);
2971c6fd2807SJeff Garzik 	}
2972c6fd2807SJeff Garzik }
2973c6fd2807SJeff Garzik 
2974c6fd2807SJeff Garzik /**
2975c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2976c6fd2807SJeff Garzik  *	@adev: device
2977c6fd2807SJeff Garzik  *
2978c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2979c6fd2807SJeff Garzik  *	present NULL is returned
2980c6fd2807SJeff Garzik  */
2981c6fd2807SJeff Garzik 
2982c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2983c6fd2807SJeff Garzik {
29849af5c9c9STejun Heo 	struct ata_link *link = adev->link;
29859af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2986c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2987c6fd2807SJeff Garzik 		return NULL;
2988c6fd2807SJeff Garzik 	return pair;
2989c6fd2807SJeff Garzik }
2990a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_pair);
2991c6fd2807SJeff Garzik 
2992c6fd2807SJeff Garzik /**
2993c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2994936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2995a07d499bSTejun Heo  *	@spd_limit: Additional limit
2996c6fd2807SJeff Garzik  *
2997936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2998c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2999c6fd2807SJeff Garzik  *	using sata_set_spd().
3000c6fd2807SJeff Garzik  *
3001a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
3002a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
3003a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
3004a07d499bSTejun Heo  *	supported speed is allowed.
3005a07d499bSTejun Heo  *
3006c6fd2807SJeff Garzik  *	LOCKING:
3007c6fd2807SJeff Garzik  *	Inherited from caller.
3008c6fd2807SJeff Garzik  *
3009c6fd2807SJeff Garzik  *	RETURNS:
3010c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3011c6fd2807SJeff Garzik  */
3012a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3013c6fd2807SJeff Garzik {
3014c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
3015a07d499bSTejun Heo 	int rc, bit;
3016c6fd2807SJeff Garzik 
3017936fd732STejun Heo 	if (!sata_scr_valid(link))
3018008a7896STejun Heo 		return -EOPNOTSUPP;
3019008a7896STejun Heo 
3020008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
3021936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
3022008a7896STejun Heo 	 */
3023936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
30249913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
3025008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
3026008a7896STejun Heo 	else
3027936fd732STejun Heo 		spd = link->sata_spd;
3028c6fd2807SJeff Garzik 
3029936fd732STejun Heo 	mask = link->sata_spd_limit;
3030c6fd2807SJeff Garzik 	if (mask <= 1)
3031c6fd2807SJeff Garzik 		return -EINVAL;
3032008a7896STejun Heo 
3033008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3034a07d499bSTejun Heo 	bit = fls(mask) - 1;
3035a07d499bSTejun Heo 	mask &= ~(1 << bit);
3036c6fd2807SJeff Garzik 
30372dc0b46bSDavid Milburn 	/*
30382dc0b46bSDavid Milburn 	 * Mask off all speeds higher than or equal to the current one.  At
30392dc0b46bSDavid Milburn 	 * this point, if current SPD is not available and we previously
30402dc0b46bSDavid Milburn 	 * recorded the link speed from SStatus, the driver has already
30412dc0b46bSDavid Milburn 	 * masked off the highest bit so mask should already be 1 or 0.
30422dc0b46bSDavid Milburn 	 * Otherwise, we should not force 1.5Gbps on a link where we have
30432dc0b46bSDavid Milburn 	 * not previously recorded speed from SStatus.  Just return in this
30442dc0b46bSDavid Milburn 	 * case.
3045008a7896STejun Heo 	 */
3046008a7896STejun Heo 	if (spd > 1)
3047008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
3048008a7896STejun Heo 	else
30492dc0b46bSDavid Milburn 		return -EINVAL;
3050008a7896STejun Heo 
3051008a7896STejun Heo 	/* were we already at the bottom? */
3052c6fd2807SJeff Garzik 	if (!mask)
3053c6fd2807SJeff Garzik 		return -EINVAL;
3054c6fd2807SJeff Garzik 
3055a07d499bSTejun Heo 	if (spd_limit) {
3056a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3057a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3058a07d499bSTejun Heo 		else {
3059a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3060a07d499bSTejun Heo 			mask = 1 << bit;
3061a07d499bSTejun Heo 		}
3062a07d499bSTejun Heo 	}
3063a07d499bSTejun Heo 
3064936fd732STejun Heo 	link->sata_spd_limit = mask;
3065c6fd2807SJeff Garzik 
3066a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
3067c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
3068c6fd2807SJeff Garzik 
3069c6fd2807SJeff Garzik 	return 0;
3070c6fd2807SJeff Garzik }
3071c6fd2807SJeff Garzik 
3072a9b2c120SBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_ACPI
3073c6fd2807SJeff Garzik /**
3074a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3075a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3076a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3077a0f79b92STejun Heo  *
3078a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3079a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3080a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3081a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3082a0f79b92STejun Heo  *
3083a0f79b92STejun Heo  *	LOCKING:
3084a0f79b92STejun Heo  *	None.
3085a0f79b92STejun Heo  *
3086a0f79b92STejun Heo  *	RETURNS:
3087a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3088a0f79b92STejun Heo  */
3089a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3090a0f79b92STejun Heo {
3091a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3092a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3093a0f79b92STejun Heo 	const struct ata_timing *t;
3094a0f79b92STejun Heo 
3095a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3096a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3097a0f79b92STejun Heo 			base_mode = ent->base;
3098a0f79b92STejun Heo 
3099a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3100a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3101a0f79b92STejun Heo 		unsigned short this_cycle;
3102a0f79b92STejun Heo 
3103a0f79b92STejun Heo 		switch (xfer_shift) {
3104a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3105a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3106a0f79b92STejun Heo 			this_cycle = t->cycle;
3107a0f79b92STejun Heo 			break;
3108a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3109a0f79b92STejun Heo 			this_cycle = t->udma;
3110a0f79b92STejun Heo 			break;
3111a0f79b92STejun Heo 		default:
3112a0f79b92STejun Heo 			return 0xff;
3113a0f79b92STejun Heo 		}
3114a0f79b92STejun Heo 
3115a0f79b92STejun Heo 		if (cycle > this_cycle)
3116a0f79b92STejun Heo 			break;
3117a0f79b92STejun Heo 
3118a0f79b92STejun Heo 		last_mode = t->mode;
3119a0f79b92STejun Heo 	}
3120a0f79b92STejun Heo 
3121a0f79b92STejun Heo 	return last_mode;
3122a0f79b92STejun Heo }
3123a9b2c120SBartlomiej Zolnierkiewicz #endif
3124a0f79b92STejun Heo 
3125a0f79b92STejun Heo /**
3126c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3127c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3128458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3129c6fd2807SJeff Garzik  *
3130c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3131c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3132c6fd2807SJeff Garzik  *	will apply the limit.
3133c6fd2807SJeff Garzik  *
3134c6fd2807SJeff Garzik  *	LOCKING:
3135c6fd2807SJeff Garzik  *	Inherited from caller.
3136c6fd2807SJeff Garzik  *
3137c6fd2807SJeff Garzik  *	RETURNS:
3138c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3139c6fd2807SJeff Garzik  */
3140458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3141c6fd2807SJeff Garzik {
3142458337dbSTejun Heo 	char buf[32];
31437dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
31447dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3145458337dbSTejun Heo 	int quiet, highbit;
3146c6fd2807SJeff Garzik 
3147458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3148458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3149458337dbSTejun Heo 
3150458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3151458337dbSTejun Heo 						  dev->mwdma_mask,
3152c6fd2807SJeff Garzik 						  dev->udma_mask);
3153458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3154c6fd2807SJeff Garzik 
3155458337dbSTejun Heo 	switch (sel) {
3156458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3157458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3158458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3159458337dbSTejun Heo 		break;
3160458337dbSTejun Heo 
3161458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3162458337dbSTejun Heo 		if (udma_mask) {
3163458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3164458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3165458337dbSTejun Heo 			if (!udma_mask)
3166458337dbSTejun Heo 				return -ENOENT;
3167458337dbSTejun Heo 		} else if (mwdma_mask) {
3168458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3169458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3170458337dbSTejun Heo 			if (!mwdma_mask)
3171458337dbSTejun Heo 				return -ENOENT;
3172458337dbSTejun Heo 		}
3173458337dbSTejun Heo 		break;
3174458337dbSTejun Heo 
3175458337dbSTejun Heo 	case ATA_DNXFER_40C:
3176458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3177458337dbSTejun Heo 		break;
3178458337dbSTejun Heo 
3179458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3180458337dbSTejun Heo 		pio_mask &= 1;
3181df561f66SGustavo A. R. Silva 		fallthrough;
3182458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3183458337dbSTejun Heo 		mwdma_mask = 0;
3184458337dbSTejun Heo 		udma_mask = 0;
3185458337dbSTejun Heo 		break;
3186458337dbSTejun Heo 
3187458337dbSTejun Heo 	default:
3188458337dbSTejun Heo 		BUG();
3189458337dbSTejun Heo 	}
3190458337dbSTejun Heo 
3191458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3192458337dbSTejun Heo 
3193458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3194458337dbSTejun Heo 		return -ENOENT;
3195458337dbSTejun Heo 
3196458337dbSTejun Heo 	if (!quiet) {
3197458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3198458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3199458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3200458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3201458337dbSTejun Heo 		else
3202458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3203458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3204458337dbSTejun Heo 
3205a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3206458337dbSTejun Heo 	}
3207c6fd2807SJeff Garzik 
3208c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3209c6fd2807SJeff Garzik 			    &dev->udma_mask);
3210c6fd2807SJeff Garzik 
3211c6fd2807SJeff Garzik 	return 0;
3212c6fd2807SJeff Garzik }
3213c6fd2807SJeff Garzik 
3214c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3215c6fd2807SJeff Garzik {
3216d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
32179af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3218d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
32194055dee7STejun Heo 	const char *dev_err_whine = "";
32204055dee7STejun Heo 	int ign_dev_err = 0;
3221d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3222c6fd2807SJeff Garzik 	int rc;
3223c6fd2807SJeff Garzik 
3224c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3225c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3226c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3227c6fd2807SJeff Garzik 
3228d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3229d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3230d0cb43b3STejun Heo 	else {
3231d0cb43b3STejun Heo 		if (nosetxfer)
3232a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3233d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3234d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3235c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3236d0cb43b3STejun Heo 	}
32372dcb407eSJeff Garzik 
32384055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
32394055dee7STejun Heo 		goto fail;
32402dcb407eSJeff Garzik 
32414055dee7STejun Heo 	/* revalidate */
3242baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3243422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3244baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3245c6fd2807SJeff Garzik 	if (rc)
3246c6fd2807SJeff Garzik 		return rc;
3247c6fd2807SJeff Garzik 
3248b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
32494055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3250b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
32514055dee7STejun Heo 			ign_dev_err = 1;
3252b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3253b93fda12SAlan Cox 		   ATA devices */
3254b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
32554055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
32564055dee7STejun Heo 			ign_dev_err = 1;
3257b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3258b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3259b93fda12SAlan Cox 		   timings and no IORDY */
3260b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3261b93fda12SAlan Cox 			ign_dev_err = 1;
3262b93fda12SAlan Cox 	}
32634055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
32644055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
32654055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
32664055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
32674055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
32684055dee7STejun Heo 		ign_dev_err = 1;
32694055dee7STejun Heo 
32704055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
32714055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
32724055dee7STejun Heo 		ign_dev_err = 1;
32734055dee7STejun Heo 
32744055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
32754055dee7STejun Heo 		if (!ign_dev_err)
32764055dee7STejun Heo 			goto fail;
32774055dee7STejun Heo 		else
32784055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
32794055dee7STejun Heo 	}
32804055dee7STejun Heo 
3281c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3282c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3283c6fd2807SJeff Garzik 
328407b9b6d6SDamien Le Moal 	if (!(ehc->i.flags & ATA_EHI_QUIET) ||
328507b9b6d6SDamien Le Moal 	    ehc->i.flags & ATA_EHI_DID_HARDRESET)
3286a9a79dfeSJoe Perches 		ata_dev_info(dev, "configured for %s%s\n",
32874055dee7STejun Heo 			     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
32884055dee7STejun Heo 			     dev_err_whine);
32894055dee7STejun Heo 
3290c6fd2807SJeff Garzik 	return 0;
32914055dee7STejun Heo 
32924055dee7STejun Heo  fail:
3293a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
32944055dee7STejun Heo 	return -EIO;
3295c6fd2807SJeff Garzik }
3296c6fd2807SJeff Garzik 
3297c6fd2807SJeff Garzik /**
329804351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
32990260731fSTejun Heo  *	@link: link on which timings will be programmed
33001967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3301c6fd2807SJeff Garzik  *
330204351821SAlan  *	Standard implementation of the function used to tune and set
330304351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
330404351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3305c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3306c6fd2807SJeff Garzik  *
3307c6fd2807SJeff Garzik  *	LOCKING:
3308c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3309c6fd2807SJeff Garzik  *
3310c6fd2807SJeff Garzik  *	RETURNS:
3311c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3312c6fd2807SJeff Garzik  */
331304351821SAlan 
33140260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3315c6fd2807SJeff Garzik {
33160260731fSTejun Heo 	struct ata_port *ap = link->ap;
3317c6fd2807SJeff Garzik 	struct ata_device *dev;
3318f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3319c6fd2807SJeff Garzik 
3320c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
33211eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
33227dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3323b3a70601SAlan Cox 		unsigned int mode_mask;
3324c6fd2807SJeff Garzik 
3325b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3326b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3327b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3328b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3329b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3330b3a70601SAlan Cox 
3331c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
333233267325STejun Heo 		ata_force_xfermask(dev);
3333c6fd2807SJeff Garzik 
3334c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3335b3a70601SAlan Cox 
3336b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
333780a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
333880a9c430SSergei Shtylyov 						     dev->udma_mask);
3339b3a70601SAlan Cox 		else
3340b3a70601SAlan Cox 			dma_mask = 0;
3341b3a70601SAlan Cox 
3342c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3343c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3344c6fd2807SJeff Garzik 
3345c6fd2807SJeff Garzik 		found = 1;
3346b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3347c6fd2807SJeff Garzik 			used_dma = 1;
3348c6fd2807SJeff Garzik 	}
3349c6fd2807SJeff Garzik 	if (!found)
3350c6fd2807SJeff Garzik 		goto out;
3351c6fd2807SJeff Garzik 
3352c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
33531eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
335470cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3355a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3356c6fd2807SJeff Garzik 			rc = -EINVAL;
3357c6fd2807SJeff Garzik 			goto out;
3358c6fd2807SJeff Garzik 		}
3359c6fd2807SJeff Garzik 
3360c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3361c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3362c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3363c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3364c6fd2807SJeff Garzik 	}
3365c6fd2807SJeff Garzik 
3366c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
33671eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
33681eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3369c6fd2807SJeff Garzik 			continue;
3370c6fd2807SJeff Garzik 
3371c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3372c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3373c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3374c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3375c6fd2807SJeff Garzik 	}
3376c6fd2807SJeff Garzik 
3377c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
33781eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3379c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3380c6fd2807SJeff Garzik 		if (rc)
3381c6fd2807SJeff Garzik 			goto out;
3382c6fd2807SJeff Garzik 	}
3383c6fd2807SJeff Garzik 
3384c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3385c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3386c6fd2807SJeff Garzik 	 */
3387cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3388032af1ceSAlan 		ap->host->simplex_claimed = ap;
3389c6fd2807SJeff Garzik 
3390c6fd2807SJeff Garzik  out:
3391c6fd2807SJeff Garzik 	if (rc)
3392c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3393c6fd2807SJeff Garzik 	return rc;
3394c6fd2807SJeff Garzik }
3395a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_set_mode);
3396c6fd2807SJeff Garzik 
3397c6fd2807SJeff Garzik /**
3398aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3399aa2731adSTejun Heo  *	@link: link to be waited on
3400aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3401aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3402aa2731adSTejun Heo  *
3403aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3404aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3405aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3406aa2731adSTejun Heo  *	conditions.
3407aa2731adSTejun Heo  *
3408aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3409aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3410aa2731adSTejun Heo  *
3411aa2731adSTejun Heo  *	LOCKING:
3412aa2731adSTejun Heo  *	EH context.
3413aa2731adSTejun Heo  *
3414aa2731adSTejun Heo  *	RETURNS:
3415c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3416aa2731adSTejun Heo  */
3417aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3418aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3419aa2731adSTejun Heo {
3420aa2731adSTejun Heo 	unsigned long start = jiffies;
3421b48d58f5STejun Heo 	unsigned long nodev_deadline;
3422aa2731adSTejun Heo 	int warned = 0;
3423aa2731adSTejun Heo 
3424b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3425b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3426b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3427b48d58f5STejun Heo 	else
3428b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3429b48d58f5STejun Heo 
3430b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3431b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3432b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3433b1c72916STejun Heo 	 */
3434b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3435b1c72916STejun Heo 
3436aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3437aa2731adSTejun Heo 		nodev_deadline = deadline;
3438aa2731adSTejun Heo 
3439aa2731adSTejun Heo 	while (1) {
3440aa2731adSTejun Heo 		unsigned long now = jiffies;
3441aa2731adSTejun Heo 		int ready, tmp;
3442aa2731adSTejun Heo 
3443aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3444aa2731adSTejun Heo 		if (ready > 0)
3445aa2731adSTejun Heo 			return 0;
3446aa2731adSTejun Heo 
3447b48d58f5STejun Heo 		/*
3448b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3449aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3450b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3451b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3452b48d58f5STejun Heo 		 * offline.
3453aa2731adSTejun Heo 		 *
3454aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3455aa2731adSTejun Heo 		 * if status register is read more than once when
3456aa2731adSTejun Heo 		 * there's no device attached.
3457aa2731adSTejun Heo 		 */
3458aa2731adSTejun Heo 		if (ready == -ENODEV) {
3459aa2731adSTejun Heo 			if (ata_link_online(link))
3460aa2731adSTejun Heo 				ready = 0;
3461aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3462aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3463aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3464aa2731adSTejun Heo 				ready = 0;
3465aa2731adSTejun Heo 		}
3466aa2731adSTejun Heo 
3467aa2731adSTejun Heo 		if (ready)
3468aa2731adSTejun Heo 			return ready;
3469aa2731adSTejun Heo 		if (time_after(now, deadline))
3470aa2731adSTejun Heo 			return -EBUSY;
3471aa2731adSTejun Heo 
3472aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3473aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3474a9a79dfeSJoe Perches 			ata_link_warn(link,
3475aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3476aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3477aa2731adSTejun Heo 			warned = 1;
3478aa2731adSTejun Heo 		}
3479aa2731adSTejun Heo 
348097750cebSTejun Heo 		ata_msleep(link->ap, 50);
3481aa2731adSTejun Heo 	}
3482aa2731adSTejun Heo }
3483aa2731adSTejun Heo 
3484aa2731adSTejun Heo /**
3485aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3486aa2731adSTejun Heo  *	@link: link to be waited on
3487aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3488aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3489aa2731adSTejun Heo  *
3490aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3491aa2731adSTejun Heo  *
3492aa2731adSTejun Heo  *	LOCKING:
3493aa2731adSTejun Heo  *	EH context.
3494aa2731adSTejun Heo  *
3495aa2731adSTejun Heo  *	RETURNS:
3496c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3497aa2731adSTejun Heo  */
34982b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3499aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3500aa2731adSTejun Heo {
350197750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3502aa2731adSTejun Heo 
3503aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3504aa2731adSTejun Heo }
3505a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_after_reset);
3506aa2731adSTejun Heo 
3507aa2731adSTejun Heo /**
35080aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3509cc0680a5STejun Heo  *	@link: ATA link to be reset
3510d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3511c6fd2807SJeff Garzik  *
3512cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3513b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3514b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3515b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3516b8cffc6aSTejun Heo  *	should just whine, not fail.
3517c6fd2807SJeff Garzik  *
3518c6fd2807SJeff Garzik  *	LOCKING:
3519c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3520c6fd2807SJeff Garzik  *
3521c6fd2807SJeff Garzik  *	RETURNS:
3522c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3523c6fd2807SJeff Garzik  */
35240aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3525c6fd2807SJeff Garzik {
3526cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3527936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3528c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3529c6fd2807SJeff Garzik 	int rc;
3530c6fd2807SJeff Garzik 
3531c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3532c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3533c6fd2807SJeff Garzik 		return 0;
3534c6fd2807SJeff Garzik 
3535936fd732STejun Heo 	/* if SATA, resume link */
3536a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3537936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3538b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3539b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3540a9a79dfeSJoe Perches 			ata_link_warn(link,
3541a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3542a9a79dfeSJoe Perches 				      rc);
3543c6fd2807SJeff Garzik 	}
3544c6fd2807SJeff Garzik 
354545db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3546b1c72916STejun Heo 	if (ata_phys_link_offline(link))
354745db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
354845db2f6cSTejun Heo 
3549c6fd2807SJeff Garzik 	return 0;
3550c6fd2807SJeff Garzik }
3551a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_prereset);
3552c6fd2807SJeff Garzik 
3553c6fd2807SJeff Garzik /**
355457c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
355557c9efdfSTejun Heo  *	@link: link to reset
355657c9efdfSTejun Heo  *	@class: resulting class of attached device
355757c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
355857c9efdfSTejun Heo  *
355957c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
356057c9efdfSTejun Heo  *
356157c9efdfSTejun Heo  *	LOCKING:
356257c9efdfSTejun Heo  *	Kernel thread context (may sleep)
356357c9efdfSTejun Heo  *
356457c9efdfSTejun Heo  *	RETURNS:
356557c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
356657c9efdfSTejun Heo  */
356757c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
356857c9efdfSTejun Heo 		       unsigned long deadline)
356957c9efdfSTejun Heo {
357057c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
357157c9efdfSTejun Heo 	bool online;
357257c9efdfSTejun Heo 	int rc;
357357c9efdfSTejun Heo 
357457c9efdfSTejun Heo 	/* do hardreset */
357557c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
357657c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
357757c9efdfSTejun Heo }
3578a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(sata_std_hardreset);
357957c9efdfSTejun Heo 
358057c9efdfSTejun Heo /**
3581203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3582cc0680a5STejun Heo  *	@link: the target ata_link
3583c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3584c6fd2807SJeff Garzik  *
3585c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3586c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3587c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3588c6fd2807SJeff Garzik  *
3589c6fd2807SJeff Garzik  *	LOCKING:
3590c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3591c6fd2807SJeff Garzik  */
3592203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3593c6fd2807SJeff Garzik {
3594f046519fSTejun Heo 	u32 serror;
3595f046519fSTejun Heo 
3596c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3597c6fd2807SJeff Garzik 
3598f046519fSTejun Heo 	/* reset complete, clear SError */
3599f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3600f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3601f046519fSTejun Heo 
3602c6fd2807SJeff Garzik 	/* print link status */
3603936fd732STejun Heo 	sata_print_link_status(link);
3604c6fd2807SJeff Garzik 
3605c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
3606c6fd2807SJeff Garzik }
3607a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_postreset);
3608c6fd2807SJeff Garzik 
3609c6fd2807SJeff Garzik /**
3610c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3611c6fd2807SJeff Garzik  *	@dev: device to compare against
3612c6fd2807SJeff Garzik  *	@new_class: class of the new device
3613c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3614c6fd2807SJeff Garzik  *
3615c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3616c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3617c6fd2807SJeff Garzik  *	@new_id.
3618c6fd2807SJeff Garzik  *
3619c6fd2807SJeff Garzik  *	LOCKING:
3620c6fd2807SJeff Garzik  *	None.
3621c6fd2807SJeff Garzik  *
3622c6fd2807SJeff Garzik  *	RETURNS:
3623c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3624c6fd2807SJeff Garzik  */
3625c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3626c6fd2807SJeff Garzik 			       const u16 *new_id)
3627c6fd2807SJeff Garzik {
3628c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3629a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3630a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3631c6fd2807SJeff Garzik 
3632c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3633a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
3634c6fd2807SJeff Garzik 			     dev->class, new_class);
3635c6fd2807SJeff Garzik 		return 0;
3636c6fd2807SJeff Garzik 	}
3637c6fd2807SJeff Garzik 
3638a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3639a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3640a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3641a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3642c6fd2807SJeff Garzik 
3643c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3644a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3645a9a79dfeSJoe Perches 			     model[0], model[1]);
3646c6fd2807SJeff Garzik 		return 0;
3647c6fd2807SJeff Garzik 	}
3648c6fd2807SJeff Garzik 
3649c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3650a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3651a9a79dfeSJoe Perches 			     serial[0], serial[1]);
3652c6fd2807SJeff Garzik 		return 0;
3653c6fd2807SJeff Garzik 	}
3654c6fd2807SJeff Garzik 
3655c6fd2807SJeff Garzik 	return 1;
3656c6fd2807SJeff Garzik }
3657c6fd2807SJeff Garzik 
3658c6fd2807SJeff Garzik /**
3659fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
36603fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3661bff04647STejun Heo  *	@readid_flags: read ID flags
3662c6fd2807SJeff Garzik  *
3663c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3664c6fd2807SJeff Garzik  *	the port.
3665c6fd2807SJeff Garzik  *
3666c6fd2807SJeff Garzik  *	LOCKING:
3667c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3668c6fd2807SJeff Garzik  *
3669c6fd2807SJeff Garzik  *	RETURNS:
3670c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3671c6fd2807SJeff Garzik  */
3672fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3673c6fd2807SJeff Garzik {
3674c6fd2807SJeff Garzik 	unsigned int class = dev->class;
36759af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3676c6fd2807SJeff Garzik 	int rc;
3677c6fd2807SJeff Garzik 
3678c6fd2807SJeff Garzik 	/* read ID data */
3679bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3680c6fd2807SJeff Garzik 	if (rc)
3681fe30911bSTejun Heo 		return rc;
3682c6fd2807SJeff Garzik 
3683c6fd2807SJeff Garzik 	/* is the device still there? */
3684fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3685fe30911bSTejun Heo 		return -ENODEV;
3686c6fd2807SJeff Garzik 
3687c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3688fe30911bSTejun Heo 	return 0;
3689fe30911bSTejun Heo }
3690fe30911bSTejun Heo 
3691fe30911bSTejun Heo /**
3692fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3693fe30911bSTejun Heo  *	@dev: device to revalidate
3694422c9daaSTejun Heo  *	@new_class: new class code
3695fe30911bSTejun Heo  *	@readid_flags: read ID flags
3696fe30911bSTejun Heo  *
3697fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3698fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3699fe30911bSTejun Heo  *
3700fe30911bSTejun Heo  *	LOCKING:
3701fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3702fe30911bSTejun Heo  *
3703fe30911bSTejun Heo  *	RETURNS:
3704fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3705fe30911bSTejun Heo  */
3706422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3707422c9daaSTejun Heo 		       unsigned int readid_flags)
3708fe30911bSTejun Heo {
37096ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
37105920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
3711fe30911bSTejun Heo 	int rc;
3712fe30911bSTejun Heo 
3713fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3714fe30911bSTejun Heo 		return -ENODEV;
3715fe30911bSTejun Heo 
3716422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3717422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
3718f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
3719f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
37209162c657SHannes Reinecke 	    new_class != ATA_DEV_ZAC &&
3721f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
3722a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
3723422c9daaSTejun Heo 			     dev->class, new_class);
3724422c9daaSTejun Heo 		rc = -ENODEV;
3725422c9daaSTejun Heo 		goto fail;
3726422c9daaSTejun Heo 	}
3727422c9daaSTejun Heo 
3728fe30911bSTejun Heo 	/* re-read ID */
3729fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3730fe30911bSTejun Heo 	if (rc)
3731fe30911bSTejun Heo 		goto fail;
3732c6fd2807SJeff Garzik 
3733c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3734efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
37356ddcd3b0STejun Heo 	if (rc)
37366ddcd3b0STejun Heo 		goto fail;
37376ddcd3b0STejun Heo 
37386ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
3739445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
3740445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
3741445d211bSTejun Heo 		return 0;
3742445d211bSTejun Heo 
3743445d211bSTejun Heo 	/* n_sectors has changed */
3744a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
37456ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
37466ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
3747445d211bSTejun Heo 
37485920dadfSTejun Heo 	/*
37495920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
3750445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
3751445d211bSTejun Heo 	 * new size matches it, keep the device.
37525920dadfSTejun Heo 	 */
37535920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3754445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
3755a9a79dfeSJoe Perches 		ata_dev_warn(dev,
37565920dadfSTejun Heo 			     "new n_sectors matches native, probably "
375768939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
375868939ce5STejun Heo 		/* use the larger n_sectors */
3759445d211bSTejun Heo 		return 0;
3760445d211bSTejun Heo 	}
3761445d211bSTejun Heo 
3762445d211bSTejun Heo 	/*
3763445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
3764445d211bSTejun Heo 	 * unlocking HPA in those cases.
3765445d211bSTejun Heo 	 *
3766445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3767445d211bSTejun Heo 	 */
3768445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3769445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3770445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
3771a9a79dfeSJoe Perches 		ata_dev_warn(dev,
3772445d211bSTejun Heo 			     "old n_sectors matches native, probably "
3773445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
3774445d211bSTejun Heo 		/* try unlocking HPA */
3775445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3776445d211bSTejun Heo 		rc = -EIO;
3777445d211bSTejun Heo 	} else
3778445d211bSTejun Heo 		rc = -ENODEV;
3779445d211bSTejun Heo 
3780445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
37815920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
37825920dadfSTejun Heo 	dev->n_sectors = n_sectors;
3783c6fd2807SJeff Garzik  fail:
3784a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
3785c6fd2807SJeff Garzik 	return rc;
3786c6fd2807SJeff Garzik }
3787c6fd2807SJeff Garzik 
37886919a0a6SAlan Cox struct ata_blacklist_entry {
37896919a0a6SAlan Cox 	const char *model_num;
37906919a0a6SAlan Cox 	const char *model_rev;
37916919a0a6SAlan Cox 	unsigned long horkage;
37926919a0a6SAlan Cox };
37936919a0a6SAlan Cox 
37946919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
37956919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
37966919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
37976919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
37986919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
37996919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
38006919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
38016919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
38026919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
38036919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
38046919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
38057da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
38066919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
38076919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
38086919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
38096919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
38106919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
38117da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
38126919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
38136919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
38146919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
38156919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
38166919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
38176919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
38186919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
38196919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
38206919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
38216919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
38226919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
382339f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
3824d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
3825b00622fcSShikha Jain 	{ "VRFDFC22048UCHC-TE*", NULL,		ATA_HORKAGE_NODMA },
38263af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
382750af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
38286919a0a6SAlan Cox 
382918d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
383040a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
38316a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
3832a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
38330523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
383418d6e9d5SAlbert Lee 
3835af34d637SDavid Milburn 	/*
3836af34d637SDavid Milburn 	 * Causes silent data corruption with higher max sects.
3837af34d637SDavid Milburn 	 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3838af34d637SDavid Milburn 	 */
3839af34d637SDavid Milburn 	{ "ST380013AS",		"3.20",		ATA_HORKAGE_MAX_SEC_1024 },
3840af34d637SDavid Milburn 
38411488a1e3STejun Heo 	/*
3842e0edc8c5STejun Heo 	 * These devices time out with higher max sects.
38431488a1e3STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
38441488a1e3STejun Heo 	 */
3845e0edc8c5STejun Heo 	{ "LITEON CX1-JB*-HP",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
3846db5ff909SXinyu Lin 	{ "LITEON EP1-*",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
38471488a1e3STejun Heo 
38486919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
38496919a0a6SAlan Cox 
38506919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
38516919a0a6SAlan Cox 	/* NCQ is slow */
38526919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
3853459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
385409125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
385509125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
38567acfaf30SPaul Rolland 	/* NCQ is broken */
3857539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
38580e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
3859da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
3860e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
38615ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
3862539cc7c7SJeff Garzik 
3863ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
38644d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3865ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3866d10d491fSTejun Heo 
38674d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3868d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3869d10d491fSTejun Heo 
38704d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3871d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3872d10d491fSTejun Heo 
38734d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3874ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3875ac70a964STejun Heo 
387631f6264eSHans de Goede 	/* drives which fail FPDMA_AA activation (some may freeze afterwards)
387731f6264eSHans de Goede 	   the ST disks also have LPM issues */
38788756a25bSHans de Goede 	{ "ST1000LM024 HN-M101MBB", NULL,	ATA_HORKAGE_BROKEN_FPDMA_AA |
387931f6264eSHans de Goede 						ATA_HORKAGE_NOLPM, },
388008c85d2aSAleksei Mamlin 	{ "VB0250EAVER",	"HPG7",		ATA_HORKAGE_BROKEN_FPDMA_AA },
388187809942SMichele Baldessari 
388236e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
388336e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
388436e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
388536e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
388636e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
38876919a0a6SAlan Cox 
388868b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
388968b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
389068b0ddb2STejun Heo 
38913b545563STejun Heo 	/* Sandisk SD7/8/9s lock up hard on large trims */
38923b545563STejun Heo 	{ "SanDisk SD[789]*",	NULL,		ATA_HORKAGE_MAX_TRIM_128M, },
3893322579dcSTejun Heo 
389416c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
389516c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
389616c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
389716c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
389816c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
38996919a0a6SAlan Cox 
39007831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
39017831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
39027831387bSTejun Heo 
390393328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
390493328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
390593328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
3906b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
390793328e11SAlan Cox 
39086bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
39096bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
3910a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
39117da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
39126bbfd53dSAlan Cox 
39139ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
39149ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
391504d0f1b8SJeff Garzik 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
39169ce8e307SJens Axboe 
39179062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
39189062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
3919c531077fSDaniel J Blueman 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS, },
39209062712fSTejun Heo 
3921d0cb43b3STejun Heo 	/*
3922d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
3923d0cb43b3STejun Heo 	 * device and controller are SATA.
3924d0cb43b3STejun Heo 	 */
3925cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
39263a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
39273a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
3928cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
3929cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
3930d0cb43b3STejun Heo 
3931b17e5729SKai-Heng Feng 	/* Crucial BX100 SSD 500GB has broken LPM support */
39323bf7b5d6SHans de Goede 	{ "CT500BX100SSD1",		NULL,	ATA_HORKAGE_NOLPM },
3933b17e5729SKai-Heng Feng 
3934d418ff56SHans de Goede 	/* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
3935d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
39369c7be59fSHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
39379c7be59fSHans de Goede 						ATA_HORKAGE_NOLPM, },
3938d418ff56SHans de Goede 	/* 512GB MX100 with newer firmware has only LPM issues */
3939d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM |
3940d418ff56SHans de Goede 						ATA_HORKAGE_NOLPM, },
39419c7be59fSHans de Goede 
394262ac3f73SHans de Goede 	/* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
394362ac3f73SHans de Goede 	{ "Crucial_CT480M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
394462ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
394562ac3f73SHans de Goede 						ATA_HORKAGE_NOLPM, },
394662ac3f73SHans de Goede 	{ "Crucial_CT960M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
394762ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
394862ac3f73SHans de Goede 						ATA_HORKAGE_NOLPM, },
394962ac3f73SHans de Goede 
395076936e9aSFrançois Cami 	/* These specific Samsung models/firmware-revs do not handle LPM well */
3951b5b4d3a5SHans de Goede 	{ "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
395276936e9aSFrançois Cami 	{ "SAMSUNG SSD PM830 mSATA *",  "CXM13D1Q", ATA_HORKAGE_NOLPM, },
3953410b5c7bSDiego Viola 	{ "SAMSUNG MZ7TD256HAFV-000L9", NULL,       ATA_HORKAGE_NOLPM, },
3954dd957493SHans de Goede 	{ "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, },
3955b5b4d3a5SHans de Goede 
3956f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
3957136d769eSSudip Mukherjee 	{ "Micron_M500IT_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
3958136d769eSSudip Mukherjee 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
3959243918beSMartin K. Petersen 	{ "Micron_M500_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
3960e61f7d1cSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
3961ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
3962ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
39639051bd39SMartin K. Petersen 	{ "Micron_M5[15]0_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
3964ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
3965ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M550*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
3966ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
3967ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*MX100*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
3968ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
3969ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 840*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
3970ca6bfcb2SJu Hyung Park 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
3971ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 850*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
39726fc4d97aSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
39737a7184b0SGuillermo A. Amaral 	{ "FCCT*M500*",			NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
39747a7184b0SGuillermo A. Amaral 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
3975e61f7d1cSMartin K. Petersen 
3976cda57b1bSArne Fitzenreiter 	/* devices that don't properly handle TRIM commands */
3977cda57b1bSArne Fitzenreiter 	{ "SuperSSpeed S238*",		NULL,	ATA_HORKAGE_NOTRIM, },
3978cda57b1bSArne Fitzenreiter 
3979e61f7d1cSMartin K. Petersen 	/*
3980e61f7d1cSMartin K. Petersen 	 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
3981e61f7d1cSMartin K. Petersen 	 * (Return Zero After Trim) flags in the ATA Command Set are
3982e61f7d1cSMartin K. Petersen 	 * unreliable in the sense that they only define what happens if
3983e61f7d1cSMartin K. Petersen 	 * the device successfully executed the DSM TRIM command. TRIM
3984e61f7d1cSMartin K. Petersen 	 * is only advisory, however, and the device is free to silently
3985e61f7d1cSMartin K. Petersen 	 * ignore all or parts of the request.
3986e61f7d1cSMartin K. Petersen 	 *
3987e61f7d1cSMartin K. Petersen 	 * Whitelist drives that are known to reliably return zeroes
3988e61f7d1cSMartin K. Petersen 	 * after TRIM.
3989e61f7d1cSMartin K. Petersen 	 */
3990e61f7d1cSMartin K. Petersen 
3991e61f7d1cSMartin K. Petersen 	/*
3992e61f7d1cSMartin K. Petersen 	 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
3993e61f7d1cSMartin K. Petersen 	 * that model before whitelisting all other intel SSDs.
3994e61f7d1cSMartin K. Petersen 	 */
3995e61f7d1cSMartin K. Petersen 	{ "INTEL*SSDSC2MH*",		NULL,	0, },
3996e61f7d1cSMartin K. Petersen 
3997ff7f53fbSMartin K. Petersen 	{ "Micron*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
3998ff7f53fbSMartin K. Petersen 	{ "Crucial*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
3999e61f7d1cSMartin K. Petersen 	{ "INTEL*SSD*", 		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4000e61f7d1cSMartin K. Petersen 	{ "SSD*INTEL*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4001e61f7d1cSMartin K. Petersen 	{ "Samsung*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4002e61f7d1cSMartin K. Petersen 	{ "SAMSUNG*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4003fd6f32f7SJuha-Matti Tilli 	{ "SAMSUNG*MZ7KM*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4004e61f7d1cSMartin K. Petersen 	{ "ST[1248][0248]0[FH]*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4005f78dea06SMarc Carino 
4006ecd75ad5STejun Heo 	/*
4007ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4008ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4009ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4010ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4011ecd75ad5STejun Heo 	 * lost too.
4012ecd75ad5STejun Heo 	 *
4013ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4014ecd75ad5STejun Heo 	 */
4015ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4016ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4017ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4018ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4019ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4020ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4021ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4022ecd75ad5STejun Heo 
40236919a0a6SAlan Cox 	/* End Marker */
40246919a0a6SAlan Cox 	{ }
4025c6fd2807SJeff Garzik };
4026c6fd2807SJeff Garzik 
402775683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4028c6fd2807SJeff Garzik {
40298bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
40308bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
40316919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4032c6fd2807SJeff Garzik 
40338bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
40348bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4035c6fd2807SJeff Garzik 
40366919a0a6SAlan Cox 	while (ad->model_num) {
40371c402799SGeorge Spelvin 		if (glob_match(ad->model_num, model_num)) {
40386919a0a6SAlan Cox 			if (ad->model_rev == NULL)
40396919a0a6SAlan Cox 				return ad->horkage;
40401c402799SGeorge Spelvin 			if (glob_match(ad->model_rev, model_rev))
40416919a0a6SAlan Cox 				return ad->horkage;
4042c6fd2807SJeff Garzik 		}
40436919a0a6SAlan Cox 		ad++;
4044c6fd2807SJeff Garzik 	}
4045c6fd2807SJeff Garzik 	return 0;
4046c6fd2807SJeff Garzik }
4047c6fd2807SJeff Garzik 
40486919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
40496919a0a6SAlan Cox {
40506919a0a6SAlan Cox 	/* We don't support polling DMA.
40516919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
40526919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
40536919a0a6SAlan Cox 	 */
40549af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
40556919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
40566919a0a6SAlan Cox 		return 1;
405775683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
40586919a0a6SAlan Cox }
40596919a0a6SAlan Cox 
4060c6fd2807SJeff Garzik /**
40616bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
40626bbfd53dSAlan Cox  *	@dev: device
40636bbfd53dSAlan Cox  *
40646bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
40656bbfd53dSAlan Cox  *	who can't follow the documentation.
40666bbfd53dSAlan Cox  */
40676bbfd53dSAlan Cox 
40686bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
40696bbfd53dSAlan Cox {
40706bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
40716bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
40726bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
40736bbfd53dSAlan Cox }
40746bbfd53dSAlan Cox 
40756bbfd53dSAlan Cox /**
407615a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
407715a5551cSAlan Cox  *	@ap: port to consider
407815a5551cSAlan Cox  *
407915a5551cSAlan Cox  *	This function encapsulates the policy for speed management
408015a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
408115a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
408215a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
408315a5551cSAlan Cox  *	impacts hotplug at all).
408415a5551cSAlan Cox  *
408515a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
408615a5551cSAlan Cox  */
408715a5551cSAlan Cox 
408815a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
408915a5551cSAlan Cox {
409015a5551cSAlan Cox 	struct ata_link *link;
409115a5551cSAlan Cox 	struct ata_device *dev;
409215a5551cSAlan Cox 
40934a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
409415a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
409515a5551cSAlan Cox 		return 1;
40964a9c7b33STejun Heo 
40974a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
409815a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
409915a5551cSAlan Cox 		return 0;
41004a9c7b33STejun Heo 
41014a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
41024a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
41034a9c7b33STejun Heo 	 * isn't sure.
41044a9c7b33STejun Heo 	 */
4105f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4106f792068eSAlan Cox 		return 0;
410715a5551cSAlan Cox 
41084a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
41094a9c7b33STejun Heo 	 *
41104a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
41114a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
41124a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
41134a9c7b33STejun Heo 	 *   give a valid detect
41144a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
41154a9c7b33STejun Heo 	 *   to colour the choice
411615a5551cSAlan Cox 	 */
41171eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
41181eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
41191eca4365STejun Heo 			if (!ata_is_40wire(dev))
412015a5551cSAlan Cox 				return 0;
412115a5551cSAlan Cox 		}
412215a5551cSAlan Cox 	}
412315a5551cSAlan Cox 	return 1;
412415a5551cSAlan Cox }
412515a5551cSAlan Cox 
412615a5551cSAlan Cox /**
4127c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4128c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4129c6fd2807SJeff Garzik  *
4130c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4131c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4132c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4133c6fd2807SJeff Garzik  *	blacklist, etc...
4134c6fd2807SJeff Garzik  *
4135c6fd2807SJeff Garzik  *	LOCKING:
4136c6fd2807SJeff Garzik  *	None.
4137c6fd2807SJeff Garzik  */
4138c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4139c6fd2807SJeff Garzik {
41409af5c9c9STejun Heo 	struct ata_link *link = dev->link;
41419af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4142cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4143c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4144c6fd2807SJeff Garzik 
4145c6fd2807SJeff Garzik 	/* controller modes available */
4146c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4147c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4148c6fd2807SJeff Garzik 
41498343f889SRobert Hancock 	/* drive modes available */
4150c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4151c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4152c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4153c6fd2807SJeff Garzik 
4154b352e57dSAlan Cox 	/*
4155b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4156b352e57dSAlan Cox 	 *	cable
4157b352e57dSAlan Cox 	 */
4158b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4159b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4160b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4161b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4162b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4163b352e57dSAlan Cox 	}
4164b352e57dSAlan Cox 
4165c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4166c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4167a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4168c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4169c6fd2807SJeff Garzik 	}
4170c6fd2807SJeff Garzik 
417114d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
417214d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4173c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4174a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4175a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4176c6fd2807SJeff Garzik 	}
4177c6fd2807SJeff Garzik 
4178e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4179e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4180e424675fSJeff Garzik 
4181c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4182a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4183c6fd2807SJeff Garzik 
41848343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
41858343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
41868343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
41878343f889SRobert Hancock 	 * solely limited by the cable.
41888343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
41898343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
41908343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
41918343f889SRobert Hancock 	 */
41928343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
41938343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
419415a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4195a9a79dfeSJoe Perches 			ata_dev_warn(dev,
41968343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
41978343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
41988343f889SRobert Hancock 		}
41998343f889SRobert Hancock 
4200c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4201c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4202c6fd2807SJeff Garzik }
4203c6fd2807SJeff Garzik 
4204c6fd2807SJeff Garzik /**
4205c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4206c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4207c6fd2807SJeff Garzik  *
4208c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4209c6fd2807SJeff Garzik  *	on port @ap.
4210c6fd2807SJeff Garzik  *
4211c6fd2807SJeff Garzik  *	LOCKING:
4212c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4213c6fd2807SJeff Garzik  *
4214c6fd2807SJeff Garzik  *	RETURNS:
4215c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4216c6fd2807SJeff Garzik  */
4217c6fd2807SJeff Garzik 
4218c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4219c6fd2807SJeff Garzik {
4220c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4221c6fd2807SJeff Garzik 	unsigned int err_mask;
4222c6fd2807SJeff Garzik 
4223c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4224c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4225c6fd2807SJeff Garzik 
4226464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4227464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4228464cf177STejun Heo 	 */
4229c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4230c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4231c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4232464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4233c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4234b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4235b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4236c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4237b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4238b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4239b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4240b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4241b9f8ab2dSAlan Cox 		return 0;
4242c6fd2807SJeff Garzik 
4243d531be2cSMikulas Patocka 	/* On some disks, this command causes spin-up, so we need longer timeout */
4244d531be2cSMikulas Patocka 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4245c6fd2807SJeff Garzik 
4246c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4247c6fd2807SJeff Garzik 	return err_mask;
4248c6fd2807SJeff Garzik }
42491152b261STejun Heo 
4250c6fd2807SJeff Garzik /**
4251218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
42529f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
42539f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4254218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
42559f45cbd3SKristen Carlson Accardi  *
42569f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4257218f3d30SJeff Garzik  *	on port @ap with sector count
42589f45cbd3SKristen Carlson Accardi  *
42599f45cbd3SKristen Carlson Accardi  *	LOCKING:
42609f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
42619f45cbd3SKristen Carlson Accardi  *
42629f45cbd3SKristen Carlson Accardi  *	RETURNS:
42639f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
42649f45cbd3SKristen Carlson Accardi  */
42651152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
42669f45cbd3SKristen Carlson Accardi {
42679f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
42689f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
4269974e0a45SDamien Le Moal 	unsigned long timeout = 0;
42709f45cbd3SKristen Carlson Accardi 
42719f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
42729f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
42739f45cbd3SKristen Carlson Accardi 
42749f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
42759f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
42769f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
42779f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
42789f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4279218f3d30SJeff Garzik 	tf.nsect = feature;
42809f45cbd3SKristen Carlson Accardi 
4281974e0a45SDamien Le Moal 	if (enable == SETFEATURES_SPINUP)
4282974e0a45SDamien Le Moal 		timeout = ata_probe_timeout ?
4283974e0a45SDamien Le Moal 			  ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4284974e0a45SDamien Le Moal 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
42859f45cbd3SKristen Carlson Accardi 
42869f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
42879f45cbd3SKristen Carlson Accardi 	return err_mask;
42889f45cbd3SKristen Carlson Accardi }
4289633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
42909f45cbd3SKristen Carlson Accardi 
42919f45cbd3SKristen Carlson Accardi /**
4292c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4293c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4294c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4295c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4296c6fd2807SJeff Garzik  *
4297c6fd2807SJeff Garzik  *	LOCKING:
4298c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4299c6fd2807SJeff Garzik  *
4300c6fd2807SJeff Garzik  *	RETURNS:
4301c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4302c6fd2807SJeff Garzik  */
4303c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4304c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4305c6fd2807SJeff Garzik {
4306c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4307c6fd2807SJeff Garzik 	unsigned int err_mask;
4308c6fd2807SJeff Garzik 
4309c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4310c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4311c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4312c6fd2807SJeff Garzik 
4313c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4314c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4315c6fd2807SJeff Garzik 
4316c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4317c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4318c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4319c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4320c6fd2807SJeff Garzik 	tf.nsect = sectors;
4321c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4322c6fd2807SJeff Garzik 
43232b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
432418b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
432518b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
432618b2466cSAlan Cox 	   drive reported working geometry */
432718b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
432818b2466cSAlan Cox 		err_mask = 0;
4329c6fd2807SJeff Garzik 
4330c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4331c6fd2807SJeff Garzik 	return err_mask;
4332c6fd2807SJeff Garzik }
4333c6fd2807SJeff Garzik 
4334c6fd2807SJeff Garzik /**
43355895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4336c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4337c6fd2807SJeff Garzik  *
4338c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4339c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4340c6fd2807SJeff Garzik  *	supplied PACKET command.
4341c6fd2807SJeff Garzik  *
4342c6fd2807SJeff Garzik  *	LOCKING:
4343cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4344c6fd2807SJeff Garzik  *
4345c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4346c6fd2807SJeff Garzik  *               nonzero otherwise
4347c6fd2807SJeff Garzik  */
43485895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4349c6fd2807SJeff Garzik {
4350c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4351c6fd2807SJeff Garzik 
4352b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4353b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4354b9a4197eSTejun Heo 	 */
43556a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
43566a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
43576f23a31dSAlbert Lee 		return 1;
43586f23a31dSAlbert Lee 
4359c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4360b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4361c6fd2807SJeff Garzik 
4362b9a4197eSTejun Heo 	return 0;
4363c6fd2807SJeff Garzik }
4364b9a4197eSTejun Heo 
4365c6fd2807SJeff Garzik /**
436631cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
436731cc23b3STejun Heo  *	@qc: ATA command in question
436831cc23b3STejun Heo  *
436931cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
437031cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
437131cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
437231cc23b3STejun Heo  *	whether a new command @qc can be issued.
437331cc23b3STejun Heo  *
437431cc23b3STejun Heo  *	LOCKING:
437531cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
437631cc23b3STejun Heo  *
437731cc23b3STejun Heo  *	RETURNS:
437831cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
437931cc23b3STejun Heo  */
438031cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
438131cc23b3STejun Heo {
438231cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
438331cc23b3STejun Heo 
4384179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
438531cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
438631cc23b3STejun Heo 			return 0;
438731cc23b3STejun Heo 	} else {
438831cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
438931cc23b3STejun Heo 			return 0;
439031cc23b3STejun Heo 	}
439131cc23b3STejun Heo 
439231cc23b3STejun Heo 	return ATA_DEFER_LINK;
439331cc23b3STejun Heo }
4394a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_qc_defer);
439531cc23b3STejun Heo 
439695364f36SJiri Slaby enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
439795364f36SJiri Slaby {
439895364f36SJiri Slaby 	return AC_ERR_OK;
439995364f36SJiri Slaby }
4400a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
4401c6fd2807SJeff Garzik 
4402c6fd2807SJeff Garzik /**
4403c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4404c6fd2807SJeff Garzik  *	@qc: Command to be associated
4405c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4406c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4407c6fd2807SJeff Garzik  *
4408c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4409c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4410c6fd2807SJeff Garzik  *	elements.
4411c6fd2807SJeff Garzik  *
4412c6fd2807SJeff Garzik  *	LOCKING:
4413cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4414c6fd2807SJeff Garzik  */
4415c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4416c6fd2807SJeff Garzik 		 unsigned int n_elem)
4417c6fd2807SJeff Garzik {
4418ff2aeb1eSTejun Heo 	qc->sg = sg;
4419c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4420ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4421ff2aeb1eSTejun Heo }
4422ff2aeb1eSTejun Heo 
44232874d5eeSGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
44242874d5eeSGeert Uytterhoeven 
44252874d5eeSGeert Uytterhoeven /**
44262874d5eeSGeert Uytterhoeven  *	ata_sg_clean - Unmap DMA memory associated with command
44272874d5eeSGeert Uytterhoeven  *	@qc: Command containing DMA memory to be released
44282874d5eeSGeert Uytterhoeven  *
44292874d5eeSGeert Uytterhoeven  *	Unmap all mapped DMA memory associated with this command.
44302874d5eeSGeert Uytterhoeven  *
44312874d5eeSGeert Uytterhoeven  *	LOCKING:
44322874d5eeSGeert Uytterhoeven  *	spin_lock_irqsave(host lock)
44332874d5eeSGeert Uytterhoeven  */
4434af27e01cSJason Yan static void ata_sg_clean(struct ata_queued_cmd *qc)
44352874d5eeSGeert Uytterhoeven {
44362874d5eeSGeert Uytterhoeven 	struct ata_port *ap = qc->ap;
44372874d5eeSGeert Uytterhoeven 	struct scatterlist *sg = qc->sg;
44382874d5eeSGeert Uytterhoeven 	int dir = qc->dma_dir;
44392874d5eeSGeert Uytterhoeven 
44402874d5eeSGeert Uytterhoeven 	WARN_ON_ONCE(sg == NULL);
44412874d5eeSGeert Uytterhoeven 
44422874d5eeSGeert Uytterhoeven 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
44432874d5eeSGeert Uytterhoeven 
44442874d5eeSGeert Uytterhoeven 	if (qc->n_elem)
44452874d5eeSGeert Uytterhoeven 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
44462874d5eeSGeert Uytterhoeven 
44472874d5eeSGeert Uytterhoeven 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
44482874d5eeSGeert Uytterhoeven 	qc->sg = NULL;
44492874d5eeSGeert Uytterhoeven }
44502874d5eeSGeert Uytterhoeven 
4451c6fd2807SJeff Garzik /**
4452c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4453c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4454c6fd2807SJeff Garzik  *
4455c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4456c6fd2807SJeff Garzik  *
4457c6fd2807SJeff Garzik  *	LOCKING:
4458cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4459c6fd2807SJeff Garzik  *
4460c6fd2807SJeff Garzik  *	RETURNS:
4461c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4462c6fd2807SJeff Garzik  *
4463c6fd2807SJeff Garzik  */
4464c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4465c6fd2807SJeff Garzik {
4466c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4467dde20207SJames Bottomley 	unsigned int n_elem;
4468c6fd2807SJeff Garzik 
446944877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4470c6fd2807SJeff Garzik 
4471dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4472dde20207SJames Bottomley 	if (n_elem < 1)
4473c6fd2807SJeff Garzik 		return -1;
4474c6fd2807SJeff Garzik 
4475bb376673SPaul Menzel 	VPRINTK("%d sg elements mapped\n", n_elem);
44765825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4477dde20207SJames Bottomley 	qc->n_elem = n_elem;
4478f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4479c6fd2807SJeff Garzik 
4480c6fd2807SJeff Garzik 	return 0;
4481c6fd2807SJeff Garzik }
4482c6fd2807SJeff Garzik 
44832874d5eeSGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
44842874d5eeSGeert Uytterhoeven 
44852874d5eeSGeert Uytterhoeven static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
44862874d5eeSGeert Uytterhoeven static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
44872874d5eeSGeert Uytterhoeven 
44882874d5eeSGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
44892874d5eeSGeert Uytterhoeven 
4490c6fd2807SJeff Garzik /**
4491c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4492c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4493c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4494c6fd2807SJeff Garzik  *
4495c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4496c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4497c6fd2807SJeff Garzik  *	vice-versa.
4498c6fd2807SJeff Garzik  *
4499c6fd2807SJeff Garzik  *	LOCKING:
4500c6fd2807SJeff Garzik  *	Inherited from caller.
4501c6fd2807SJeff Garzik  */
4502c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4503c6fd2807SJeff Garzik {
4504c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4505c6fd2807SJeff Garzik 	unsigned int i;
4506c6fd2807SJeff Garzik 
4507c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4508c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4509c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4510c6fd2807SJeff Garzik }
4511c6fd2807SJeff Garzik 
4512c6fd2807SJeff Garzik /**
4513c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4514c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
451538755e89SRandy Dunlap  *	@tag: tag
4516c6fd2807SJeff Garzik  *
4517c6fd2807SJeff Garzik  *	LOCKING:
4518c6fd2807SJeff Garzik  *	None.
4519c6fd2807SJeff Garzik  */
4520c6fd2807SJeff Garzik 
452198bd4be1SShaohua Li struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
4522c6fd2807SJeff Garzik {
45239af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4524c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4525c6fd2807SJeff Garzik 
452698bd4be1SShaohua Li 	/* no command while frozen */
452798bd4be1SShaohua Li 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
452898bd4be1SShaohua Li 		return NULL;
452998bd4be1SShaohua Li 
453098bd4be1SShaohua Li 	/* libsas case */
45315067c046SShaohua Li 	if (ap->flags & ATA_FLAG_SAS_HOST) {
453298bd4be1SShaohua Li 		tag = ata_sas_allocate_tag(ap);
453398bd4be1SShaohua Li 		if (tag < 0)
453498bd4be1SShaohua Li 			return NULL;
453598bd4be1SShaohua Li 	}
453698bd4be1SShaohua Li 
453798bd4be1SShaohua Li 	qc = __ata_qc_from_tag(ap, tag);
45385ac40790SJens Axboe 	qc->tag = qc->hw_tag = tag;
4539c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
4540c6fd2807SJeff Garzik 	qc->ap = ap;
4541c6fd2807SJeff Garzik 	qc->dev = dev;
4542c6fd2807SJeff Garzik 
4543c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
4544c6fd2807SJeff Garzik 
4545c6fd2807SJeff Garzik 	return qc;
4546c6fd2807SJeff Garzik }
4547c6fd2807SJeff Garzik 
45488a8bc223STejun Heo /**
45498a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
45508a8bc223STejun Heo  *	@qc: Command to complete
45518a8bc223STejun Heo  *
45528a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
45538a8bc223STejun Heo  *	in case something prevents using it.
45548a8bc223STejun Heo  *
45558a8bc223STejun Heo  *	LOCKING:
45568a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
45578a8bc223STejun Heo  */
45588a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
45598a8bc223STejun Heo {
4560a1104016SJulia Lawall 	struct ata_port *ap;
45618a8bc223STejun Heo 	unsigned int tag;
45628a8bc223STejun Heo 
4563efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4564a1104016SJulia Lawall 	ap = qc->ap;
45658a8bc223STejun Heo 
45668a8bc223STejun Heo 	qc->flags = 0;
45678a8bc223STejun Heo 	tag = qc->tag;
456828361c40SJens Axboe 	if (ata_tag_valid(tag)) {
45698a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
45705067c046SShaohua Li 		if (ap->flags & ATA_FLAG_SAS_HOST)
457198bd4be1SShaohua Li 			ata_sas_free_tag(tag, ap);
45728a8bc223STejun Heo 	}
45738a8bc223STejun Heo }
45748a8bc223STejun Heo 
4575c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4576c6fd2807SJeff Garzik {
4577a1104016SJulia Lawall 	struct ata_port *ap;
4578a1104016SJulia Lawall 	struct ata_link *link;
4579c6fd2807SJeff Garzik 
4580efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4581efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4582a1104016SJulia Lawall 	ap = qc->ap;
4583a1104016SJulia Lawall 	link = qc->dev->link;
4584c6fd2807SJeff Garzik 
4585c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4586c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4587c6fd2807SJeff Garzik 
4588c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4589179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
45904e5b6260SJens Axboe 		link->sactive &= ~(1 << qc->hw_tag);
4591da917d69STejun Heo 		if (!link->sactive)
4592da917d69STejun Heo 			ap->nr_active_links--;
4593da917d69STejun Heo 	} else {
45949af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4595da917d69STejun Heo 		ap->nr_active_links--;
4596da917d69STejun Heo 	}
4597da917d69STejun Heo 
4598da917d69STejun Heo 	/* clear exclusive status */
4599da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4600da917d69STejun Heo 		     ap->excl_link == link))
4601da917d69STejun Heo 		ap->excl_link = NULL;
4602c6fd2807SJeff Garzik 
4603c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4604c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4605c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4606c6fd2807SJeff Garzik 	 */
4607c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4608e3ed8939SJens Axboe 	ap->qc_active &= ~(1ULL << qc->tag);
4609c6fd2807SJeff Garzik 
4610c6fd2807SJeff Garzik 	/* call completion callback */
4611c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4612c6fd2807SJeff Garzik }
4613c6fd2807SJeff Garzik 
461439599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
461539599a53STejun Heo {
461639599a53STejun Heo 	struct ata_port *ap = qc->ap;
461739599a53STejun Heo 
461839599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
461922183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
462039599a53STejun Heo }
462139599a53STejun Heo 
462200115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
462300115e0fSTejun Heo {
462400115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
462500115e0fSTejun Heo 
4626eb0effdfSChristoph Hellwig 	if (!ata_is_data(qc->tf.protocol))
462700115e0fSTejun Heo 		return;
462800115e0fSTejun Heo 
462900115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
463000115e0fSTejun Heo 		return;
463100115e0fSTejun Heo 
463200115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
463300115e0fSTejun Heo }
463400115e0fSTejun Heo 
4635c6fd2807SJeff Garzik /**
4636c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4637c6fd2807SJeff Garzik  *	@qc: Command to complete
4638c6fd2807SJeff Garzik  *
46391aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
46401aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
46411aadf5c3STejun Heo  *
46421aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
46431aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
46441aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
46451aadf5c3STejun Heo  *	properly update IRQ expect state.
4646c6fd2807SJeff Garzik  *
4647c6fd2807SJeff Garzik  *	LOCKING:
4648cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4649c6fd2807SJeff Garzik  */
4650c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4651c6fd2807SJeff Garzik {
4652c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4653c6fd2807SJeff Garzik 
4654eb25cb99SStephan Linz 	/* Trigger the LED (if available) */
4655d1ed7c55SLinus Walleij 	ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
4656eb25cb99SStephan Linz 
4657c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4658c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4659c6fd2807SJeff Garzik 	 *
4660c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4661c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4662c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4663c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4664c6fd2807SJeff Garzik 	 *
4665c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4666c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4667c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4668c6fd2807SJeff Garzik 	 * taken care of.
4669c6fd2807SJeff Garzik 	 */
4670c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
46714dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
46724dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
46734dbfa39bSTejun Heo 
4674c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4675c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4676c6fd2807SJeff Garzik 
4677f08dc1acSTejun Heo 		/*
4678f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
4679f08dc1acSTejun Heo 		 * and always with the result TF filled.
4680f08dc1acSTejun Heo 		 */
4681f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
468239599a53STejun Heo 			fill_result_tf(qc);
4683255c03d1SHannes Reinecke 			trace_ata_qc_complete_internal(qc);
4684f4b31db9STejun Heo 			__ata_qc_complete(qc);
4685c6fd2807SJeff Garzik 			return;
4686c6fd2807SJeff Garzik 		}
4687c6fd2807SJeff Garzik 
4688f08dc1acSTejun Heo 		/*
4689f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
4690f08dc1acSTejun Heo 		 * summon EH.
4691f08dc1acSTejun Heo 		 */
4692f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4693f08dc1acSTejun Heo 			fill_result_tf(qc);
4694255c03d1SHannes Reinecke 			trace_ata_qc_complete_failed(qc);
4695f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
4696f08dc1acSTejun Heo 			return;
4697f08dc1acSTejun Heo 		}
4698f08dc1acSTejun Heo 
46994dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
47004dc738edSTejun Heo 
4701c6fd2807SJeff Garzik 		/* read result TF if requested */
4702c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
470339599a53STejun Heo 			fill_result_tf(qc);
4704c6fd2807SJeff Garzik 
4705255c03d1SHannes Reinecke 		trace_ata_qc_complete_done(qc);
47064dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
47074dbfa39bSTejun Heo 		 * completion.
47084dbfa39bSTejun Heo 		 */
47094dbfa39bSTejun Heo 		switch (qc->tf.command) {
47104dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
47114dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
47120c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_WC_OFF &&
47130c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_ON &&
47140c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_OFF)
47154dbfa39bSTejun Heo 				break;
4716df561f66SGustavo A. R. Silva 			fallthrough;
47174dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
47184dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
47194dbfa39bSTejun Heo 			/* revalidate device */
47204dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
47214dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
47224dbfa39bSTejun Heo 			break;
4723054a5fbaSTejun Heo 
4724054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4725054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4726054a5fbaSTejun Heo 			break;
47274dbfa39bSTejun Heo 		}
47284dbfa39bSTejun Heo 
472900115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
473000115e0fSTejun Heo 			ata_verify_xfer(qc);
473100115e0fSTejun Heo 
4732c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4733c6fd2807SJeff Garzik 	} else {
4734c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4735c6fd2807SJeff Garzik 			return;
4736c6fd2807SJeff Garzik 
4737c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4738c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
473939599a53STejun Heo 			fill_result_tf(qc);
4740c6fd2807SJeff Garzik 
4741c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4742c6fd2807SJeff Garzik 	}
4743c6fd2807SJeff Garzik }
4744a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_qc_complete);
4745c6fd2807SJeff Garzik 
4746c6fd2807SJeff Garzik /**
47478385d756SSascha Hauer  *	ata_qc_get_active - get bitmask of active qcs
47488385d756SSascha Hauer  *	@ap: port in question
47498385d756SSascha Hauer  *
47508385d756SSascha Hauer  *	LOCKING:
47518385d756SSascha Hauer  *	spin_lock_irqsave(host lock)
47528385d756SSascha Hauer  *
47538385d756SSascha Hauer  *	RETURNS:
47548385d756SSascha Hauer  *	Bitmask of active qcs
47558385d756SSascha Hauer  */
47568385d756SSascha Hauer u64 ata_qc_get_active(struct ata_port *ap)
47578385d756SSascha Hauer {
47588385d756SSascha Hauer 	u64 qc_active = ap->qc_active;
47598385d756SSascha Hauer 
47608385d756SSascha Hauer 	/* ATA_TAG_INTERNAL is sent to hw as tag 0 */
47618385d756SSascha Hauer 	if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
47628385d756SSascha Hauer 		qc_active |= (1 << 0);
47638385d756SSascha Hauer 		qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
47648385d756SSascha Hauer 	}
47658385d756SSascha Hauer 
47668385d756SSascha Hauer 	return qc_active;
47678385d756SSascha Hauer }
47688385d756SSascha Hauer EXPORT_SYMBOL_GPL(ata_qc_get_active);
47698385d756SSascha Hauer 
47708385d756SSascha Hauer /**
4771c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4772c6fd2807SJeff Garzik  *	@qc: command to issue to device
4773c6fd2807SJeff Garzik  *
4774c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4775c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4776c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
4777c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
4778c6fd2807SJeff Garzik  *
4779c6fd2807SJeff Garzik  *	LOCKING:
4780cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4781c6fd2807SJeff Garzik  */
4782c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
4783c6fd2807SJeff Garzik {
4784c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
47859af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4786405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
4787c6fd2807SJeff Garzik 
4788c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
4789c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
4790c6fd2807SJeff Garzik 	 * request ATAPI sense.
4791c6fd2807SJeff Garzik 	 */
4792efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
4793c6fd2807SJeff Garzik 
47941973a023STejun Heo 	if (ata_is_ncq(prot)) {
47954e5b6260SJens Axboe 		WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
4796da917d69STejun Heo 
4797da917d69STejun Heo 		if (!link->sactive)
4798da917d69STejun Heo 			ap->nr_active_links++;
47994e5b6260SJens Axboe 		link->sactive |= 1 << qc->hw_tag;
4800c6fd2807SJeff Garzik 	} else {
4801efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
4802da917d69STejun Heo 
4803da917d69STejun Heo 		ap->nr_active_links++;
48049af5c9c9STejun Heo 		link->active_tag = qc->tag;
4805c6fd2807SJeff Garzik 	}
4806c6fd2807SJeff Garzik 
4807c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
4808e3ed8939SJens Axboe 	ap->qc_active |= 1ULL << qc->tag;
4809c6fd2807SJeff Garzik 
481060f5d6efSTejun Heo 	/*
481160f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
4812f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
4813f92a2636STejun Heo 	 */
48149173e5e8SEric Biggers 	if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
481560f5d6efSTejun Heo 		goto sys_err;
4816f92a2636STejun Heo 
4817405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
4818f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
4819c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
482060f5d6efSTejun Heo 			goto sys_err;
4821c6fd2807SJeff Garzik 
4822cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
4823054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
4824cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
4825054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4826054a5fbaSTejun Heo 		ata_link_abort(link);
4827054a5fbaSTejun Heo 		return;
4828054a5fbaSTejun Heo 	}
4829054a5fbaSTejun Heo 
483095364f36SJiri Slaby 	qc->err_mask |= ap->ops->qc_prep(qc);
483195364f36SJiri Slaby 	if (unlikely(qc->err_mask))
483295364f36SJiri Slaby 		goto err;
4833255c03d1SHannes Reinecke 	trace_ata_qc_issue(qc);
4834c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
4835c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
4836c6fd2807SJeff Garzik 		goto err;
4837c6fd2807SJeff Garzik 	return;
4838c6fd2807SJeff Garzik 
483960f5d6efSTejun Heo sys_err:
4840c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
4841c6fd2807SJeff Garzik err:
4842c6fd2807SJeff Garzik 	ata_qc_complete(qc);
4843c6fd2807SJeff Garzik }
4844c6fd2807SJeff Garzik 
4845c6fd2807SJeff Garzik /**
4846b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
4847936fd732STejun Heo  *	@link: ATA link to test
4848c6fd2807SJeff Garzik  *
4849936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
4850936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
4851936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4852c6fd2807SJeff Garzik  *
4853c6fd2807SJeff Garzik  *	LOCKING:
4854c6fd2807SJeff Garzik  *	None.
4855c6fd2807SJeff Garzik  *
4856c6fd2807SJeff Garzik  *	RETURNS:
4857b5b3fa38STejun Heo  *	True if the port online status is available and online.
4858c6fd2807SJeff Garzik  */
4859b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
4860c6fd2807SJeff Garzik {
4861c6fd2807SJeff Garzik 	u32 sstatus;
4862c6fd2807SJeff Garzik 
4863936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
48649913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
4865b5b3fa38STejun Heo 		return true;
4866b5b3fa38STejun Heo 	return false;
4867c6fd2807SJeff Garzik }
4868c6fd2807SJeff Garzik 
4869c6fd2807SJeff Garzik /**
4870b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
4871936fd732STejun Heo  *	@link: ATA link to test
4872c6fd2807SJeff Garzik  *
4873936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
4874936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
4875936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4876c6fd2807SJeff Garzik  *
4877c6fd2807SJeff Garzik  *	LOCKING:
4878c6fd2807SJeff Garzik  *	None.
4879c6fd2807SJeff Garzik  *
4880c6fd2807SJeff Garzik  *	RETURNS:
4881b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
4882c6fd2807SJeff Garzik  */
4883b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
4884c6fd2807SJeff Garzik {
4885c6fd2807SJeff Garzik 	u32 sstatus;
4886c6fd2807SJeff Garzik 
4887936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
48889913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
4889b5b3fa38STejun Heo 		return true;
4890b5b3fa38STejun Heo 	return false;
4891c6fd2807SJeff Garzik }
4892c6fd2807SJeff Garzik 
4893b1c72916STejun Heo /**
4894b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
4895b1c72916STejun Heo  *	@link: ATA link to test
4896b1c72916STejun Heo  *
4897b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
4898b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
4899b1c72916STejun Heo  *	there's a slave link, this function should only be called on
4900b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
4901b1c72916STejun Heo  *	online.
4902b1c72916STejun Heo  *
4903b1c72916STejun Heo  *	LOCKING:
4904b1c72916STejun Heo  *	None.
4905b1c72916STejun Heo  *
4906b1c72916STejun Heo  *	RETURNS:
4907b1c72916STejun Heo  *	True if the port online status is available and online.
4908b1c72916STejun Heo  */
4909b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
4910b1c72916STejun Heo {
4911b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
4912b1c72916STejun Heo 
4913b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
4914b1c72916STejun Heo 
4915b1c72916STejun Heo 	return ata_phys_link_online(link) ||
4916b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
4917b1c72916STejun Heo }
4918a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_online);
4919b1c72916STejun Heo 
4920b1c72916STejun Heo /**
4921b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
4922b1c72916STejun Heo  *	@link: ATA link to test
4923b1c72916STejun Heo  *
4924b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
4925b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
4926b1c72916STejun Heo  *	there's a slave link, this function should only be called on
4927b1c72916STejun Heo  *	the master link and will return true if both M/S links are
4928b1c72916STejun Heo  *	offline.
4929b1c72916STejun Heo  *
4930b1c72916STejun Heo  *	LOCKING:
4931b1c72916STejun Heo  *	None.
4932b1c72916STejun Heo  *
4933b1c72916STejun Heo  *	RETURNS:
4934b1c72916STejun Heo  *	True if the port offline status is available and offline.
4935b1c72916STejun Heo  */
4936b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
4937b1c72916STejun Heo {
4938b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
4939b1c72916STejun Heo 
4940b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
4941b1c72916STejun Heo 
4942b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
4943b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
4944b1c72916STejun Heo }
4945a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_offline);
4946b1c72916STejun Heo 
49476ffa01d8STejun Heo #ifdef CONFIG_PM
4948bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
4949cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
4950bc6e7c4bSDan Williams 				bool async)
4951c6fd2807SJeff Garzik {
4952e3667ebfSTejun Heo 	struct ata_link *link;
49535ef41082SLin Ming 	unsigned long flags;
4954c6fd2807SJeff Garzik 
4955c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
4956c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
4957c6fd2807SJeff Garzik 	 */
4958c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
4959c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
4960c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4961c6fd2807SJeff Garzik 	}
4962c6fd2807SJeff Garzik 
4963c6fd2807SJeff Garzik 	/* request PM ops to EH */
4964c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
4965c6fd2807SJeff Garzik 
4966c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
4967c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
49681eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
4969e3667ebfSTejun Heo 		link->eh_info.action |= action;
4970e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
4971e3667ebfSTejun Heo 	}
4972c6fd2807SJeff Garzik 
4973c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
4974c6fd2807SJeff Garzik 
4975c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
4976c6fd2807SJeff Garzik 
49772fcbdcb4SDan Williams 	if (!async) {
4978c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
4979c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4980c6fd2807SJeff Garzik 	}
4981c6fd2807SJeff Garzik }
4982c6fd2807SJeff Garzik 
498333574d68SLin Ming /*
4984bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
4985bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
4986bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
498733574d68SLin Ming  *
498833574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
498933574d68SLin Ming  */
4990bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
4991bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
4992bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
4993bc6e7c4bSDan Williams 
4994bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
4995bc6e7c4bSDan Williams {
4996bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
49975ef41082SLin Ming }
49985ef41082SLin Ming 
4999bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5000bc6e7c4bSDan Williams {
5001bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5002bc6e7c4bSDan Williams }
5003bc6e7c4bSDan Williams 
5004bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
50052fcbdcb4SDan Williams {
50062fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
50072fcbdcb4SDan Williams 
50085ef41082SLin Ming 	if (pm_runtime_suspended(dev))
50095ef41082SLin Ming 		return 0;
50105ef41082SLin Ming 
5011bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5012f5e6d0d0SAaron Lu 	return 0;
501333574d68SLin Ming }
501433574d68SLin Ming 
5015bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
50162fcbdcb4SDan Williams {
50172fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
50182fcbdcb4SDan Williams 
5019bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5020bc6e7c4bSDan Williams 		return 0;
5021bc6e7c4bSDan Williams 
5022bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5023bc6e7c4bSDan Williams 	return 0;
50242fcbdcb4SDan Williams }
50252fcbdcb4SDan Williams 
5026bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5027e90b1e5aSLin Ming {
5028bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5029bc6e7c4bSDan Williams 	return 0;
5030bc6e7c4bSDan Williams }
5031e90b1e5aSLin Ming 
5032bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5033bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5034bc6e7c4bSDan Williams 
5035bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5036bc6e7c4bSDan Williams {
5037bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5038bc6e7c4bSDan Williams }
5039bc6e7c4bSDan Williams 
5040bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5041bc6e7c4bSDan Williams {
5042bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5043bc6e7c4bSDan Williams }
5044bc6e7c4bSDan Williams 
5045bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5046bc6e7c4bSDan Williams {
5047200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5048e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5049e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5050e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5051bc6e7c4bSDan Williams 	return 0;
5052e90b1e5aSLin Ming }
5053e90b1e5aSLin Ming 
50547e15e9beSAaron Lu /*
50557e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
50567e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
50577e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
50587e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
50597e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
50607e15e9beSAaron Lu  * ODD is attached to the port.
50617e15e9beSAaron Lu  */
50629ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
50639ee4f393SLin Ming {
50647e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
50657e15e9beSAaron Lu 	struct ata_link *link;
50667e15e9beSAaron Lu 	struct ata_device *adev;
50677e15e9beSAaron Lu 
50687e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
50697e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
50707e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
50717e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
50727e15e9beSAaron Lu 				return -EBUSY;
50737e15e9beSAaron Lu 	}
50747e15e9beSAaron Lu 
507545f0a85cSRafael J. Wysocki 	return 0;
50769ee4f393SLin Ming }
50779ee4f393SLin Ming 
5078a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5079a7ff60dbSAaron Lu {
5080bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5081bc6e7c4bSDan Williams 	return 0;
5082a7ff60dbSAaron Lu }
5083a7ff60dbSAaron Lu 
5084a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5085a7ff60dbSAaron Lu {
5086bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5087bc6e7c4bSDan Williams 	return 0;
5088a7ff60dbSAaron Lu }
5089a7ff60dbSAaron Lu 
50905ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5091bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5092bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5093bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5094bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5095bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5096bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
50979ee4f393SLin Ming 
5098a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5099a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
51009ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
51015ef41082SLin Ming };
51025ef41082SLin Ming 
51032fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
51042fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
51052fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
51062fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
51072fcbdcb4SDan Williams  */
5108bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
51092fcbdcb4SDan Williams {
5110bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
51112fcbdcb4SDan Williams }
5112bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
51132fcbdcb4SDan Williams 
5114bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
51152fcbdcb4SDan Williams {
5116bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
51172fcbdcb4SDan Williams }
5118bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
51192fcbdcb4SDan Williams 
5120c6fd2807SJeff Garzik /**
5121cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5122cca3974eSJeff Garzik  *	@host: host to suspend
5123c6fd2807SJeff Garzik  *	@mesg: PM message
5124c6fd2807SJeff Garzik  *
51255ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5126c6fd2807SJeff Garzik  */
5127cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5128c6fd2807SJeff Garzik {
512972ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
51305ef41082SLin Ming 	return 0;
5131c6fd2807SJeff Garzik }
5132a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_suspend);
5133c6fd2807SJeff Garzik 
5134c6fd2807SJeff Garzik /**
5135cca3974eSJeff Garzik  *	ata_host_resume - resume host
5136cca3974eSJeff Garzik  *	@host: host to resume
5137c6fd2807SJeff Garzik  *
51385ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5139c6fd2807SJeff Garzik  */
5140cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5141c6fd2807SJeff Garzik {
514272ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5143c6fd2807SJeff Garzik }
5144a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_resume);
51456ffa01d8STejun Heo #endif
5146c6fd2807SJeff Garzik 
51478df82c13SBhumika Goyal const struct device_type ata_port_type = {
51485ef41082SLin Ming 	.name = "ata_port",
51495ef41082SLin Ming #ifdef CONFIG_PM
51505ef41082SLin Ming 	.pm = &ata_port_pm_ops,
51515ef41082SLin Ming #endif
51525ef41082SLin Ming };
51535ef41082SLin Ming 
5154c6fd2807SJeff Garzik /**
5155c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5156c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5157c6fd2807SJeff Garzik  *
5158c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5159c6fd2807SJeff Garzik  *
5160c6fd2807SJeff Garzik  *	LOCKING:
5161c6fd2807SJeff Garzik  *	Inherited from caller.
5162c6fd2807SJeff Garzik  */
5163c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5164c6fd2807SJeff Garzik {
5165b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
51669af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5167c6fd2807SJeff Garzik 	unsigned long flags;
5168c6fd2807SJeff Garzik 
5169b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
51709af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
51719af5c9c9STejun Heo 	link->sata_spd = 0;
5172c6fd2807SJeff Garzik 
5173c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5174c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5175cca3974eSJeff Garzik 	 * host lock.
5176c6fd2807SJeff Garzik 	 */
5177c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5178c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
51793dcc323fSTejun Heo 	dev->horkage = 0;
5180c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5181c6fd2807SJeff Garzik 
518299cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
518399cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5184c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5185c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5186c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5187c6fd2807SJeff Garzik }
5188c6fd2807SJeff Garzik 
5189c6fd2807SJeff Garzik /**
51904fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
51914fb37a25STejun Heo  *	@ap: ATA port link is attached to
51924fb37a25STejun Heo  *	@link: Link structure to initialize
51938989805dSTejun Heo  *	@pmp: Port multiplier port number
51944fb37a25STejun Heo  *
51954fb37a25STejun Heo  *	Initialize @link.
51964fb37a25STejun Heo  *
51974fb37a25STejun Heo  *	LOCKING:
51984fb37a25STejun Heo  *	Kernel thread context (may sleep)
51994fb37a25STejun Heo  */
5200fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
52014fb37a25STejun Heo {
52024fb37a25STejun Heo 	int i;
52034fb37a25STejun Heo 
52044fb37a25STejun Heo 	/* clear everything except for devices */
5205d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5206d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
52074fb37a25STejun Heo 
52084fb37a25STejun Heo 	link->ap = ap;
52098989805dSTejun Heo 	link->pmp = pmp;
52104fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
52114fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
52124fb37a25STejun Heo 
52134fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
52144fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
52154fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
52164fb37a25STejun Heo 
52174fb37a25STejun Heo 		dev->link = link;
52184fb37a25STejun Heo 		dev->devno = dev - link->device;
5219110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5220110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5221110f66d2STejun Heo #endif
52224fb37a25STejun Heo 		ata_dev_init(dev);
52234fb37a25STejun Heo 	}
52244fb37a25STejun Heo }
52254fb37a25STejun Heo 
52264fb37a25STejun Heo /**
52274fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
52284fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
52294fb37a25STejun Heo  *
5230a31a6997SMauro Carvalho Chehab  *	Initialize ``link->[hw_]sata_spd_limit`` to the currently
52314fb37a25STejun Heo  *	configured value.
52324fb37a25STejun Heo  *
52334fb37a25STejun Heo  *	LOCKING:
52344fb37a25STejun Heo  *	Kernel thread context (may sleep).
52354fb37a25STejun Heo  *
52364fb37a25STejun Heo  *	RETURNS:
52374fb37a25STejun Heo  *	0 on success, -errno on failure.
52384fb37a25STejun Heo  */
5239fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
52404fb37a25STejun Heo {
524133267325STejun Heo 	u8 spd;
52424fb37a25STejun Heo 	int rc;
52434fb37a25STejun Heo 
5244d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
52454fb37a25STejun Heo 	if (rc)
52464fb37a25STejun Heo 		return rc;
52474fb37a25STejun Heo 
5248d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
52494fb37a25STejun Heo 	if (spd)
52504fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
52514fb37a25STejun Heo 
525205944bdfSTejun Heo 	ata_force_link_limits(link);
525333267325STejun Heo 
52544fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
52554fb37a25STejun Heo 
52564fb37a25STejun Heo 	return 0;
52574fb37a25STejun Heo }
52584fb37a25STejun Heo 
52594fb37a25STejun Heo /**
5260f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5261f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5262c6fd2807SJeff Garzik  *
5263f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5264f3187195STejun Heo  *
5265f3187195STejun Heo  *	RETURNS:
5266f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5267c6fd2807SJeff Garzik  *
5268c6fd2807SJeff Garzik  *	LOCKING:
5269f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5270c6fd2807SJeff Garzik  */
5271f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5272c6fd2807SJeff Garzik {
5273f3187195STejun Heo 	struct ata_port *ap;
5274c6fd2807SJeff Garzik 
5275f3187195STejun Heo 	DPRINTK("ENTER\n");
5276f3187195STejun Heo 
5277f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5278f3187195STejun Heo 	if (!ap)
5279f3187195STejun Heo 		return NULL;
5280f3187195STejun Heo 
52817b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5282cca3974eSJeff Garzik 	ap->lock = &host->lock;
5283f3187195STejun Heo 	ap->print_id = -1;
5284e628dc99SDavid Milburn 	ap->local_port_no = -1;
5285cca3974eSJeff Garzik 	ap->host = host;
5286f3187195STejun Heo 	ap->dev = host->dev;
5287c6fd2807SJeff Garzik 
5288c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5289c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5290c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5291c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5292c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5293c6fd2807SJeff Garzik #else
5294c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5295c6fd2807SJeff Garzik #endif
5296c6fd2807SJeff Garzik 
5297ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
529865f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
529965f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5300c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5301c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
530245fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
5303b93ab338SKees Cook 	timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5304b93ab338SKees Cook 		    TIMER_DEFERRABLE);
5305c6fd2807SJeff Garzik 
5306c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5307c6fd2807SJeff Garzik 
53088989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5309c6fd2807SJeff Garzik 
5310c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5311c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5312c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5313c6fd2807SJeff Garzik #endif
5314270390e1STejun Heo 	ata_sff_port_init(ap);
5315270390e1STejun Heo 
5316c6fd2807SJeff Garzik 	return ap;
5317c6fd2807SJeff Garzik }
5318c6fd2807SJeff Garzik 
53192623c7a5STaras Kondratiuk static void ata_devres_release(struct device *gendev, void *res)
5320f0d36efdSTejun Heo {
5321f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5322f0d36efdSTejun Heo 	int i;
5323f0d36efdSTejun Heo 
5324f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5325f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5326f0d36efdSTejun Heo 
5327ecef7253STejun Heo 		if (!ap)
5328ecef7253STejun Heo 			continue;
5329ecef7253STejun Heo 
53304911487aSTejun Heo 		if (ap->scsi_host)
53311aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
53321aa506e4STejun Heo 
53332623c7a5STaras Kondratiuk 	}
53342623c7a5STaras Kondratiuk 
53352623c7a5STaras Kondratiuk 	dev_set_drvdata(gendev, NULL);
53362623c7a5STaras Kondratiuk 	ata_host_put(host);
53372623c7a5STaras Kondratiuk }
53382623c7a5STaras Kondratiuk 
53392623c7a5STaras Kondratiuk static void ata_host_release(struct kref *kref)
53402623c7a5STaras Kondratiuk {
53412623c7a5STaras Kondratiuk 	struct ata_host *host = container_of(kref, struct ata_host, kref);
53422623c7a5STaras Kondratiuk 	int i;
53432623c7a5STaras Kondratiuk 
53442623c7a5STaras Kondratiuk 	for (i = 0; i < host->n_ports; i++) {
53452623c7a5STaras Kondratiuk 		struct ata_port *ap = host->ports[i];
53462623c7a5STaras Kondratiuk 
5347633273a3STejun Heo 		kfree(ap->pmp_link);
5348b1c72916STejun Heo 		kfree(ap->slave_link);
53494911487aSTejun Heo 		kfree(ap);
53501aa506e4STejun Heo 		host->ports[i] = NULL;
53511aa506e4STejun Heo 	}
53522623c7a5STaras Kondratiuk 	kfree(host);
53532623c7a5STaras Kondratiuk }
53541aa506e4STejun Heo 
53552623c7a5STaras Kondratiuk void ata_host_get(struct ata_host *host)
53562623c7a5STaras Kondratiuk {
53572623c7a5STaras Kondratiuk 	kref_get(&host->kref);
53582623c7a5STaras Kondratiuk }
53592623c7a5STaras Kondratiuk 
53602623c7a5STaras Kondratiuk void ata_host_put(struct ata_host *host)
53612623c7a5STaras Kondratiuk {
53622623c7a5STaras Kondratiuk 	kref_put(&host->kref, ata_host_release);
5363f0d36efdSTejun Heo }
5364a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_put);
5365f0d36efdSTejun Heo 
5366c6fd2807SJeff Garzik /**
5367f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5368f3187195STejun Heo  *	@dev: generic device this host is associated with
5369f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5370f3187195STejun Heo  *
5371f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5372f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5373f3187195STejun Heo  *	attaches it using ata_host_register().
5374f3187195STejun Heo  *
5375f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5376f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5377f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5378f3187195STejun Heo  *	ports will be automatically freed on registration.
5379f3187195STejun Heo  *
5380f3187195STejun Heo  *	RETURNS:
5381f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5382f3187195STejun Heo  *
5383f3187195STejun Heo  *	LOCKING:
5384f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5385f3187195STejun Heo  */
5386f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5387f3187195STejun Heo {
5388f3187195STejun Heo 	struct ata_host *host;
5389f3187195STejun Heo 	size_t sz;
5390f3187195STejun Heo 	int i;
53912623c7a5STaras Kondratiuk 	void *dr;
5392f3187195STejun Heo 
5393f3187195STejun Heo 	DPRINTK("ENTER\n");
5394f3187195STejun Heo 
5395f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5396f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
53972623c7a5STaras Kondratiuk 	host = kzalloc(sz, GFP_KERNEL);
5398f3187195STejun Heo 	if (!host)
53992623c7a5STaras Kondratiuk 		return NULL;
54002623c7a5STaras Kondratiuk 
5401f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5402dafd6c49SColin Ian King 		goto err_free;
5403f3187195STejun Heo 
54042623c7a5STaras Kondratiuk 	dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
54052623c7a5STaras Kondratiuk 	if (!dr)
5406f3187195STejun Heo 		goto err_out;
5407f3187195STejun Heo 
54082623c7a5STaras Kondratiuk 	devres_add(dev, dr);
5409f3187195STejun Heo 	dev_set_drvdata(dev, host);
5410f3187195STejun Heo 
5411f3187195STejun Heo 	spin_lock_init(&host->lock);
5412c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5413f3187195STejun Heo 	host->dev = dev;
5414f3187195STejun Heo 	host->n_ports = max_ports;
54152623c7a5STaras Kondratiuk 	kref_init(&host->kref);
5416f3187195STejun Heo 
5417f3187195STejun Heo 	/* allocate ports bound to this host */
5418f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5419f3187195STejun Heo 		struct ata_port *ap;
5420f3187195STejun Heo 
5421f3187195STejun Heo 		ap = ata_port_alloc(host);
5422f3187195STejun Heo 		if (!ap)
5423f3187195STejun Heo 			goto err_out;
5424f3187195STejun Heo 
5425f3187195STejun Heo 		ap->port_no = i;
5426f3187195STejun Heo 		host->ports[i] = ap;
5427f3187195STejun Heo 	}
5428f3187195STejun Heo 
5429f3187195STejun Heo 	devres_remove_group(dev, NULL);
5430f3187195STejun Heo 	return host;
5431f3187195STejun Heo 
5432f3187195STejun Heo  err_out:
5433f3187195STejun Heo 	devres_release_group(dev, NULL);
5434dafd6c49SColin Ian King  err_free:
5435dafd6c49SColin Ian King 	kfree(host);
5436f3187195STejun Heo 	return NULL;
5437f3187195STejun Heo }
5438a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc);
5439f3187195STejun Heo 
5440f3187195STejun Heo /**
5441f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5442f5cda257STejun Heo  *	@dev: generic device this host is associated with
5443f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5444f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5445f5cda257STejun Heo  *
5446f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5447f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5448f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5449f5cda257STejun Heo  *
5450f5cda257STejun Heo  *	RETURNS:
5451f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5452f5cda257STejun Heo  *
5453f5cda257STejun Heo  *	LOCKING:
5454f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5455f5cda257STejun Heo  */
5456f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5457f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5458f5cda257STejun Heo 				      int n_ports)
5459f5cda257STejun Heo {
5460f5cda257STejun Heo 	const struct ata_port_info *pi;
5461f5cda257STejun Heo 	struct ata_host *host;
5462f5cda257STejun Heo 	int i, j;
5463f5cda257STejun Heo 
5464f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5465f5cda257STejun Heo 	if (!host)
5466f5cda257STejun Heo 		return NULL;
5467f5cda257STejun Heo 
5468f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5469f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5470f5cda257STejun Heo 
5471f5cda257STejun Heo 		if (ppi[j])
5472f5cda257STejun Heo 			pi = ppi[j++];
5473f5cda257STejun Heo 
5474f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5475f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5476f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5477f5cda257STejun Heo 		ap->flags |= pi->flags;
54780c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5479f5cda257STejun Heo 		ap->ops = pi->port_ops;
5480f5cda257STejun Heo 
5481f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5482f5cda257STejun Heo 			host->ops = pi->port_ops;
5483f5cda257STejun Heo 	}
5484f5cda257STejun Heo 
5485f5cda257STejun Heo 	return host;
5486f5cda257STejun Heo }
5487a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
5488f5cda257STejun Heo 
548932ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
549032ebbc0cSTejun Heo {
549132ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
549232ebbc0cSTejun Heo 	int i;
549332ebbc0cSTejun Heo 
549432ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
549532ebbc0cSTejun Heo 
549632ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
549732ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
549832ebbc0cSTejun Heo 
549932ebbc0cSTejun Heo 		if (ap->ops->port_stop)
550032ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
550132ebbc0cSTejun Heo 	}
550232ebbc0cSTejun Heo 
550332ebbc0cSTejun Heo 	if (host->ops->host_stop)
550432ebbc0cSTejun Heo 		host->ops->host_stop(host);
550532ebbc0cSTejun Heo }
550632ebbc0cSTejun Heo 
5507f5cda257STejun Heo /**
5508029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5509029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5510029cfd6bSTejun Heo  *
5511029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5512029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5513029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5514029cfd6bSTejun Heo  *	inheritance chain.
5515029cfd6bSTejun Heo  *
5516029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5517029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5518029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5519029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5520029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5521029cfd6bSTejun Heo  *
5522029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5523029cfd6bSTejun Heo  *
5524029cfd6bSTejun Heo  *	LOCKING:
5525029cfd6bSTejun Heo  *	None.
5526029cfd6bSTejun Heo  */
5527029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5528029cfd6bSTejun Heo {
55292da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5530029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5531029cfd6bSTejun Heo 	void **begin = (void **)ops;
5532029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5533029cfd6bSTejun Heo 	void **pp;
5534029cfd6bSTejun Heo 
5535029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5536029cfd6bSTejun Heo 		return;
5537029cfd6bSTejun Heo 
5538029cfd6bSTejun Heo 	spin_lock(&lock);
5539029cfd6bSTejun Heo 
5540029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5541029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5542029cfd6bSTejun Heo 
5543029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5544029cfd6bSTejun Heo 			if (!*pp)
5545029cfd6bSTejun Heo 				*pp = *inherit;
5546029cfd6bSTejun Heo 	}
5547029cfd6bSTejun Heo 
5548029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5549029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5550029cfd6bSTejun Heo 			*pp = NULL;
5551029cfd6bSTejun Heo 
5552029cfd6bSTejun Heo 	ops->inherits = NULL;
5553029cfd6bSTejun Heo 
5554029cfd6bSTejun Heo 	spin_unlock(&lock);
5555029cfd6bSTejun Heo }
5556029cfd6bSTejun Heo 
5557029cfd6bSTejun Heo /**
5558ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5559ecef7253STejun Heo  *	@host: ATA host to start ports for
5560ecef7253STejun Heo  *
5561ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5562ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5563ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5564f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
5565f3187195STejun Heo  *	first non-dummy port ops.
5566ecef7253STejun Heo  *
5567ecef7253STejun Heo  *	LOCKING:
5568ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5569ecef7253STejun Heo  *
5570ecef7253STejun Heo  *	RETURNS:
5571ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5572ecef7253STejun Heo  */
5573ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5574ecef7253STejun Heo {
557532ebbc0cSTejun Heo 	int have_stop = 0;
557632ebbc0cSTejun Heo 	void *start_dr = NULL;
5577ecef7253STejun Heo 	int i, rc;
5578ecef7253STejun Heo 
5579ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5580ecef7253STejun Heo 		return 0;
5581ecef7253STejun Heo 
5582029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5583029cfd6bSTejun Heo 
5584ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5585ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5586ecef7253STejun Heo 
5587029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5588029cfd6bSTejun Heo 
5589f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5590f3187195STejun Heo 			host->ops = ap->ops;
5591f3187195STejun Heo 
559232ebbc0cSTejun Heo 		if (ap->ops->port_stop)
559332ebbc0cSTejun Heo 			have_stop = 1;
559432ebbc0cSTejun Heo 	}
559532ebbc0cSTejun Heo 
5596355a8031SDamien Le Moal 	if (host->ops && host->ops->host_stop)
559732ebbc0cSTejun Heo 		have_stop = 1;
559832ebbc0cSTejun Heo 
559932ebbc0cSTejun Heo 	if (have_stop) {
560032ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
560132ebbc0cSTejun Heo 		if (!start_dr)
560232ebbc0cSTejun Heo 			return -ENOMEM;
560332ebbc0cSTejun Heo 	}
560432ebbc0cSTejun Heo 
560532ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
560632ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
560732ebbc0cSTejun Heo 
5608ecef7253STejun Heo 		if (ap->ops->port_start) {
5609ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5610ecef7253STejun Heo 			if (rc) {
56110f9fe9b7SAlan Cox 				if (rc != -ENODEV)
5612a44fec1fSJoe Perches 					dev_err(host->dev,
5613a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
5614a44fec1fSJoe Perches 						i, rc);
5615ecef7253STejun Heo 				goto err_out;
5616ecef7253STejun Heo 			}
5617ecef7253STejun Heo 		}
5618ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5619ecef7253STejun Heo 	}
5620ecef7253STejun Heo 
562132ebbc0cSTejun Heo 	if (start_dr)
562232ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5623ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5624ecef7253STejun Heo 	return 0;
5625ecef7253STejun Heo 
5626ecef7253STejun Heo  err_out:
5627ecef7253STejun Heo 	while (--i >= 0) {
5628ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5629ecef7253STejun Heo 
5630ecef7253STejun Heo 		if (ap->ops->port_stop)
5631ecef7253STejun Heo 			ap->ops->port_stop(ap);
5632ecef7253STejun Heo 	}
563332ebbc0cSTejun Heo 	devres_free(start_dr);
5634ecef7253STejun Heo 	return rc;
5635ecef7253STejun Heo }
5636a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_start);
5637ecef7253STejun Heo 
5638ecef7253STejun Heo /**
563994bd5719SMauro Carvalho Chehab  *	ata_host_init - Initialize a host struct for sas (ipr, libsas)
5640cca3974eSJeff Garzik  *	@host:	host to initialize
5641cca3974eSJeff Garzik  *	@dev:	device host is attached to
5642c6fd2807SJeff Garzik  *	@ops:	port_ops
5643c6fd2807SJeff Garzik  *
5644c6fd2807SJeff Garzik  */
5645cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
56468d8e7d13SDan Williams 		   struct ata_port_operations *ops)
5647c6fd2807SJeff Garzik {
5648cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5649c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
565069278f79SJens Axboe 	host->n_tags = ATA_MAX_QUEUE;
5651cca3974eSJeff Garzik 	host->dev = dev;
5652cca3974eSJeff Garzik 	host->ops = ops;
56532fa4a326SJason Yan 	kref_init(&host->kref);
5654c6fd2807SJeff Garzik }
5655a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_init);
5656c6fd2807SJeff Garzik 
56579508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
565879318057SArjan van de Ven {
565979318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
566079318057SArjan van de Ven 	unsigned long flags;
566179318057SArjan van de Ven 
566279318057SArjan van de Ven 	/* kick EH for boot probing */
566379318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
566479318057SArjan van de Ven 
566579318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
56666b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
566779318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
566879318057SArjan van de Ven 
566979318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
567079318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
567179318057SArjan van de Ven 	ata_port_schedule_eh(ap);
567279318057SArjan van de Ven 
567379318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
56749508a66fSDan Williams }
567579318057SArjan van de Ven 
56769508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
56779508a66fSDan Williams {
56789508a66fSDan Williams 	int rc = 0;
56799508a66fSDan Williams 
56809508a66fSDan Williams 	if (ap->ops->error_handler) {
56819508a66fSDan Williams 		__ata_port_probe(ap);
568279318057SArjan van de Ven 		ata_port_wait_eh(ap);
568379318057SArjan van de Ven 	} else {
568479318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
568579318057SArjan van de Ven 		rc = ata_bus_probe(ap);
568679318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
5687238c9cf9SJames Bottomley 	}
5688238c9cf9SJames Bottomley 	return rc;
5689238c9cf9SJames Bottomley }
569079318057SArjan van de Ven 
5691238c9cf9SJames Bottomley 
5692238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
5693238c9cf9SJames Bottomley {
5694238c9cf9SJames Bottomley 	struct ata_port *ap = data;
5695238c9cf9SJames Bottomley 
5696238c9cf9SJames Bottomley 	/*
5697238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
5698238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
5699238c9cf9SJames Bottomley 	 * before going further.
5700238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
5701238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
570279318057SArjan van de Ven 	 */
5703238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5704238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
5705238c9cf9SJames Bottomley 
5706238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
5707f29d3b23SArjan van de Ven 
5708f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
5709f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
5710f29d3b23SArjan van de Ven 
5711f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
571279318057SArjan van de Ven }
5713238c9cf9SJames Bottomley 
5714c6fd2807SJeff Garzik /**
5715f3187195STejun Heo  *	ata_host_register - register initialized ATA host
5716f3187195STejun Heo  *	@host: ATA host to register
5717f3187195STejun Heo  *	@sht: template for SCSI host
5718c6fd2807SJeff Garzik  *
5719f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
5720f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
5721f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
5722f3187195STejun Heo  *	probe registered devices.
5723c6fd2807SJeff Garzik  *
5724c6fd2807SJeff Garzik  *	LOCKING:
5725f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5726c6fd2807SJeff Garzik  *
5727c6fd2807SJeff Garzik  *	RETURNS:
5728f3187195STejun Heo  *	0 on success, -errno otherwise.
5729c6fd2807SJeff Garzik  */
5730f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
5731c6fd2807SJeff Garzik {
5732f3187195STejun Heo 	int i, rc;
5733c6fd2807SJeff Garzik 
573469278f79SJens Axboe 	host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
57351871ee13SKevin Hao 
5736f3187195STejun Heo 	/* host must have been started */
5737f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
5738a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
5739f3187195STejun Heo 		WARN_ON(1);
5740f3187195STejun Heo 		return -EINVAL;
574102f076aaSAlan Cox 	}
5742f0d36efdSTejun Heo 
5743f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
5744f3187195STejun Heo 	 * determine the exact number of ports to allocate at
5745f3187195STejun Heo 	 * allocation time.
5746f3187195STejun Heo 	 */
5747f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
5748f3187195STejun Heo 		kfree(host->ports[i]);
5749f0d36efdSTejun Heo 
5750f3187195STejun Heo 	/* give ports names and add SCSI hosts */
5751e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
575285d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
5753e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
5754e628dc99SDavid Milburn 	}
5755d9027470SGwendal Grignou 
5756d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
5757d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
5758d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
5759d9027470SGwendal Grignou 		if (rc) {
5760d9027470SGwendal Grignou 			goto err_tadd;
5761d9027470SGwendal Grignou 		}
5762d9027470SGwendal Grignou 	}
5763d9027470SGwendal Grignou 
5764f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
5765ecef7253STejun Heo 	if (rc)
5766d9027470SGwendal Grignou 		goto err_tadd;
5767ecef7253STejun Heo 
5768f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
5769cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5770cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5771f3187195STejun Heo 		unsigned long xfer_mask;
5772f3187195STejun Heo 
5773f3187195STejun Heo 		/* set SATA cable type if still unset */
5774f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5775f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
5776c6fd2807SJeff Garzik 
5777c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
57784fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
5779b1c72916STejun Heo 		if (ap->slave_link)
5780b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
5781c6fd2807SJeff Garzik 
5782cbcdd875STejun Heo 		/* print per-port info to dmesg */
5783f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5784f3187195STejun Heo 					      ap->udma_mask);
5785f3187195STejun Heo 
5786abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
5787a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
5788a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5789f3187195STejun Heo 				      ata_mode_string(xfer_mask),
5790cbcdd875STejun Heo 				      ap->link.eh_info.desc);
5791abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
5792abf6e8edSTejun Heo 		} else
5793a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
5794c6fd2807SJeff Garzik 	}
5795c6fd2807SJeff Garzik 
5796f6005354SVegard Nossum 	/* perform each probe asynchronously */
5797f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5798f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
5799b5292111SKai-Heng Feng 		ap->cookie = async_schedule(async_port_probe, ap);
5800c6fd2807SJeff Garzik 	}
5801c6fd2807SJeff Garzik 
5802f3187195STejun Heo 	return 0;
5803d9027470SGwendal Grignou 
5804d9027470SGwendal Grignou  err_tadd:
5805d9027470SGwendal Grignou 	while (--i >= 0) {
5806d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
5807d9027470SGwendal Grignou 	}
5808d9027470SGwendal Grignou 	return rc;
5809d9027470SGwendal Grignou 
5810f3187195STejun Heo }
5811a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_register);
5812f3187195STejun Heo 
5813f3187195STejun Heo /**
5814f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
5815f5cda257STejun Heo  *	@host: target ATA host
5816f5cda257STejun Heo  *	@irq: IRQ to request
5817f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
5818f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
5819f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
5820f5cda257STejun Heo  *
5821f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
5822f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
5823c9b5560aSMasanari Iida  *	request IRQ and register it.  This helper takes necessary
5824f5cda257STejun Heo  *	arguments and performs the three steps in one go.
5825f5cda257STejun Heo  *
58263d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
58273d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
58283d46b2e2SPaul Mundt  *	should be NULL.
58293d46b2e2SPaul Mundt  *
5830f5cda257STejun Heo  *	LOCKING:
5831f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5832f5cda257STejun Heo  *
5833f5cda257STejun Heo  *	RETURNS:
5834f5cda257STejun Heo  *	0 on success, -errno otherwise.
5835f5cda257STejun Heo  */
5836f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
5837f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
5838f5cda257STejun Heo 		      struct scsi_host_template *sht)
5839f5cda257STejun Heo {
5840cbcdd875STejun Heo 	int i, rc;
58417e22c002SHeiner Kallweit 	char *irq_desc;
5842f5cda257STejun Heo 
5843f5cda257STejun Heo 	rc = ata_host_start(host);
5844f5cda257STejun Heo 	if (rc)
5845f5cda257STejun Heo 		return rc;
5846f5cda257STejun Heo 
58473d46b2e2SPaul Mundt 	/* Special case for polling mode */
58483d46b2e2SPaul Mundt 	if (!irq) {
58493d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
58503d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
58513d46b2e2SPaul Mundt 	}
58523d46b2e2SPaul Mundt 
58537e22c002SHeiner Kallweit 	irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
58547e22c002SHeiner Kallweit 				  dev_driver_string(host->dev),
58557e22c002SHeiner Kallweit 				  dev_name(host->dev));
58567e22c002SHeiner Kallweit 	if (!irq_desc)
58577e22c002SHeiner Kallweit 		return -ENOMEM;
58587e22c002SHeiner Kallweit 
5859f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
58607e22c002SHeiner Kallweit 			      irq_desc, host);
5861f5cda257STejun Heo 	if (rc)
5862f5cda257STejun Heo 		return rc;
5863f5cda257STejun Heo 
5864cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
5865cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
58664031826bSTejun Heo 
5867f5cda257STejun Heo 	rc = ata_host_register(host, sht);
5868f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
5869f5cda257STejun Heo 	if (rc)
5870f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
5871f5cda257STejun Heo 
5872f5cda257STejun Heo 	return rc;
5873f5cda257STejun Heo }
5874a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_activate);
5875f5cda257STejun Heo 
5876f5cda257STejun Heo /**
5877c9b5560aSMasanari Iida  *	ata_port_detach - Detach ATA port in preparation of device removal
5878c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
5879c6fd2807SJeff Garzik  *
5880c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
5881c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
5882c6fd2807SJeff Garzik  *	be quiescent on return from this function.
5883c6fd2807SJeff Garzik  *
5884c6fd2807SJeff Garzik  *	LOCKING:
5885c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5886c6fd2807SJeff Garzik  */
5887741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
5888c6fd2807SJeff Garzik {
5889c6fd2807SJeff Garzik 	unsigned long flags;
5890a6f9bf4dSLevente Kurusa 	struct ata_link *link;
5891a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
5892c6fd2807SJeff Garzik 
5893c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
5894c6fd2807SJeff Garzik 		goto skip_eh;
5895c6fd2807SJeff Garzik 
5896c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
5897c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5898c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
5899ece180d1STejun Heo 	ata_port_schedule_eh(ap);
5900c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5901c6fd2807SJeff Garzik 
5902ece180d1STejun Heo 	/* wait till EH commits suicide */
5903c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
5904c6fd2807SJeff Garzik 
5905ece180d1STejun Heo 	/* it better be dead now */
5906ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
5907c6fd2807SJeff Garzik 
5908afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
5909c6fd2807SJeff Garzik 
5910c6fd2807SJeff Garzik  skip_eh:
5911a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
5912a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
5913a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
5914a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
5915a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
5916a6f9bf4dSLevente Kurusa 		}
5917a6f9bf4dSLevente Kurusa 	}
5918d9027470SGwendal Grignou 	if (ap->pmp_link) {
5919d9027470SGwendal Grignou 		int i;
5920d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
5921d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
5922d9027470SGwendal Grignou 	}
5923c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
5924cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
5925c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
5926c6fd2807SJeff Garzik }
5927c6fd2807SJeff Garzik 
5928c6fd2807SJeff Garzik /**
59290529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
59300529c159STejun Heo  *	@host: Host to detach
59310529c159STejun Heo  *
59320529c159STejun Heo  *	Detach all ports of @host.
59330529c159STejun Heo  *
59340529c159STejun Heo  *	LOCKING:
59350529c159STejun Heo  *	Kernel thread context (may sleep).
59360529c159STejun Heo  */
59370529c159STejun Heo void ata_host_detach(struct ata_host *host)
59380529c159STejun Heo {
59390529c159STejun Heo 	int i;
59400529c159STejun Heo 
5941b5292111SKai-Heng Feng 	for (i = 0; i < host->n_ports; i++) {
5942130f4cafSJohn Garry 		/* Ensure ata_port probe has completed */
5943b5292111SKai-Heng Feng 		async_synchronize_cookie(host->ports[i]->cookie + 1);
59440529c159STejun Heo 		ata_port_detach(host->ports[i]);
5945b5292111SKai-Heng Feng 	}
5946562f0c2dSTejun Heo 
5947562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
5948562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
59490529c159STejun Heo }
5950a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_detach);
59510529c159STejun Heo 
5952c6fd2807SJeff Garzik #ifdef CONFIG_PCI
5953c6fd2807SJeff Garzik 
5954c6fd2807SJeff Garzik /**
5955c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
5956c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
5957c6fd2807SJeff Garzik  *
5958b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
5959b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
5960b878ca5dSTejun Heo  *	release is handled via devres.
5961c6fd2807SJeff Garzik  *
5962c6fd2807SJeff Garzik  *	LOCKING:
5963c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
5964c6fd2807SJeff Garzik  */
5965c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
5966c6fd2807SJeff Garzik {
596704a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
5968c6fd2807SJeff Garzik 
5969f0d36efdSTejun Heo 	ata_host_detach(host);
5970c6fd2807SJeff Garzik }
5971a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5972c6fd2807SJeff Garzik 
597310a663a1SPrabhakar Kushwaha void ata_pci_shutdown_one(struct pci_dev *pdev)
597410a663a1SPrabhakar Kushwaha {
597510a663a1SPrabhakar Kushwaha 	struct ata_host *host = pci_get_drvdata(pdev);
597610a663a1SPrabhakar Kushwaha 	int i;
597710a663a1SPrabhakar Kushwaha 
597810a663a1SPrabhakar Kushwaha 	for (i = 0; i < host->n_ports; i++) {
597910a663a1SPrabhakar Kushwaha 		struct ata_port *ap = host->ports[i];
598010a663a1SPrabhakar Kushwaha 
598110a663a1SPrabhakar Kushwaha 		ap->pflags |= ATA_PFLAG_FROZEN;
598210a663a1SPrabhakar Kushwaha 
598310a663a1SPrabhakar Kushwaha 		/* Disable port interrupts */
598410a663a1SPrabhakar Kushwaha 		if (ap->ops->freeze)
598510a663a1SPrabhakar Kushwaha 			ap->ops->freeze(ap);
598610a663a1SPrabhakar Kushwaha 
598710a663a1SPrabhakar Kushwaha 		/* Stop the port DMA engines */
598810a663a1SPrabhakar Kushwaha 		if (ap->ops->port_stop)
598910a663a1SPrabhakar Kushwaha 			ap->ops->port_stop(ap);
599010a663a1SPrabhakar Kushwaha 	}
599110a663a1SPrabhakar Kushwaha }
5992a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
599310a663a1SPrabhakar Kushwaha 
5994c6fd2807SJeff Garzik /* move to PCI subsystem */
5995c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
5996c6fd2807SJeff Garzik {
5997c6fd2807SJeff Garzik 	unsigned long tmp = 0;
5998c6fd2807SJeff Garzik 
5999c6fd2807SJeff Garzik 	switch (bits->width) {
6000c6fd2807SJeff Garzik 	case 1: {
6001c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6002c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6003c6fd2807SJeff Garzik 		tmp = tmp8;
6004c6fd2807SJeff Garzik 		break;
6005c6fd2807SJeff Garzik 	}
6006c6fd2807SJeff Garzik 	case 2: {
6007c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6008c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6009c6fd2807SJeff Garzik 		tmp = tmp16;
6010c6fd2807SJeff Garzik 		break;
6011c6fd2807SJeff Garzik 	}
6012c6fd2807SJeff Garzik 	case 4: {
6013c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6014c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6015c6fd2807SJeff Garzik 		tmp = tmp32;
6016c6fd2807SJeff Garzik 		break;
6017c6fd2807SJeff Garzik 	}
6018c6fd2807SJeff Garzik 
6019c6fd2807SJeff Garzik 	default:
6020c6fd2807SJeff Garzik 		return -EINVAL;
6021c6fd2807SJeff Garzik 	}
6022c6fd2807SJeff Garzik 
6023c6fd2807SJeff Garzik 	tmp &= bits->mask;
6024c6fd2807SJeff Garzik 
6025c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6026c6fd2807SJeff Garzik }
6027a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(pci_test_config_bits);
6028c6fd2807SJeff Garzik 
60296ffa01d8STejun Heo #ifdef CONFIG_PM
6030c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6031c6fd2807SJeff Garzik {
6032c6fd2807SJeff Garzik 	pci_save_state(pdev);
6033c6fd2807SJeff Garzik 	pci_disable_device(pdev);
60344c90d971STejun Heo 
60353a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6036c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6037c6fd2807SJeff Garzik }
6038a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6039c6fd2807SJeff Garzik 
6040553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6041c6fd2807SJeff Garzik {
6042553c4aa6STejun Heo 	int rc;
6043553c4aa6STejun Heo 
6044c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6045c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6046553c4aa6STejun Heo 
6047f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6048553c4aa6STejun Heo 	if (rc) {
6049a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6050553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6051553c4aa6STejun Heo 		return rc;
6052553c4aa6STejun Heo 	}
6053553c4aa6STejun Heo 
6054c6fd2807SJeff Garzik 	pci_set_master(pdev);
6055553c4aa6STejun Heo 	return 0;
6056c6fd2807SJeff Garzik }
6057a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6058c6fd2807SJeff Garzik 
6059c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6060c6fd2807SJeff Garzik {
606104a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6062c6fd2807SJeff Garzik 	int rc = 0;
6063c6fd2807SJeff Garzik 
6064cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6065c6fd2807SJeff Garzik 	if (rc)
6066c6fd2807SJeff Garzik 		return rc;
6067c6fd2807SJeff Garzik 
6068c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6069c6fd2807SJeff Garzik 
6070c6fd2807SJeff Garzik 	return 0;
6071c6fd2807SJeff Garzik }
6072a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6073c6fd2807SJeff Garzik 
6074c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6075c6fd2807SJeff Garzik {
607604a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6077553c4aa6STejun Heo 	int rc;
6078c6fd2807SJeff Garzik 
6079553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6080553c4aa6STejun Heo 	if (rc == 0)
6081cca3974eSJeff Garzik 		ata_host_resume(host);
6082553c4aa6STejun Heo 	return rc;
6083c6fd2807SJeff Garzik }
6084a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_resume);
60856ffa01d8STejun Heo #endif /* CONFIG_PM */
6086c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6087c6fd2807SJeff Garzik 
6088b7db04d9SBrian Norris /**
6089b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6090b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6091b7db04d9SBrian Norris  *
6092b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6093b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6094b7db04d9SBrian Norris  *	release is handled via devres.
6095b7db04d9SBrian Norris  *
6096b7db04d9SBrian Norris  *	LOCKING:
6097b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6098b7db04d9SBrian Norris  */
6099b7db04d9SBrian Norris int ata_platform_remove_one(struct platform_device *pdev)
6100b7db04d9SBrian Norris {
6101b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6102b7db04d9SBrian Norris 
6103b7db04d9SBrian Norris 	ata_host_detach(host);
6104b7db04d9SBrian Norris 
6105b7db04d9SBrian Norris 	return 0;
6106b7db04d9SBrian Norris }
6107a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_platform_remove_one);
6108b7db04d9SBrian Norris 
6109bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
611033267325STejun Heo static int __init ata_parse_force_one(char **cur,
611133267325STejun Heo 				      struct ata_force_ent *force_ent,
611233267325STejun Heo 				      const char **reason)
611333267325STejun Heo {
61140f5f264bSRasmus Villemoes 	static const struct ata_force_param force_tbl[] __initconst = {
611533267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
611633267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
611733267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
611833267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
611933267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
612033267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
612133267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
612233267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
612333267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
612433267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
6125d7b16e4fSMartin K. Petersen 		{ "noncqtrim",	.horkage_on	= ATA_HORKAGE_NO_NCQ_TRIM },
6126d7b16e4fSMartin K. Petersen 		{ "ncqtrim",	.horkage_off	= ATA_HORKAGE_NO_NCQ_TRIM },
612743c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
612833267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
612933267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
613033267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
613133267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
613233267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
613333267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
613433267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
613533267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
613633267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
613733267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
613833267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
613933267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
614033267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
614133267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
614233267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
614333267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
614433267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
614533267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
614633267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
614733267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
614833267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
614933267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
615033267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
615133267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
615233267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
615333267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
615433267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
615533267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
615633267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
615733267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
615833267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
615933267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
616033267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
616133267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
616205944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
616305944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
616405944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6165ca6d43b0SDan Williams 		{ "rstonce",	.lflags		= ATA_LFLAG_RST_ONCE },
6166966fbe19SVincent Pelletier 		{ "atapi_dmadir", .horkage_on	= ATA_HORKAGE_ATAPI_DMADIR },
6167b8bd6dc3SRobin H. Johnson 		{ "disable",	.horkage_on	= ATA_HORKAGE_DISABLE },
616833267325STejun Heo 	};
616933267325STejun Heo 	char *start = *cur, *p = *cur;
617033267325STejun Heo 	char *id, *val, *endp;
617133267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
617233267325STejun Heo 	int nr_matches = 0, i;
617333267325STejun Heo 
617433267325STejun Heo 	/* find where this param ends and update *cur */
617533267325STejun Heo 	while (*p != '\0' && *p != ',')
617633267325STejun Heo 		p++;
617733267325STejun Heo 
617833267325STejun Heo 	if (*p == '\0')
617933267325STejun Heo 		*cur = p;
618033267325STejun Heo 	else
618133267325STejun Heo 		*cur = p + 1;
618233267325STejun Heo 
618333267325STejun Heo 	*p = '\0';
618433267325STejun Heo 
618533267325STejun Heo 	/* parse */
618633267325STejun Heo 	p = strchr(start, ':');
618733267325STejun Heo 	if (!p) {
618833267325STejun Heo 		val = strstrip(start);
618933267325STejun Heo 		goto parse_val;
619033267325STejun Heo 	}
619133267325STejun Heo 	*p = '\0';
619233267325STejun Heo 
619333267325STejun Heo 	id = strstrip(start);
619433267325STejun Heo 	val = strstrip(p + 1);
619533267325STejun Heo 
619633267325STejun Heo 	/* parse id */
619733267325STejun Heo 	p = strchr(id, '.');
619833267325STejun Heo 	if (p) {
619933267325STejun Heo 		*p++ = '\0';
620033267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
620133267325STejun Heo 		if (p == endp || *endp != '\0') {
620233267325STejun Heo 			*reason = "invalid device";
620333267325STejun Heo 			return -EINVAL;
620433267325STejun Heo 		}
620533267325STejun Heo 	}
620633267325STejun Heo 
620733267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
6208f7cf69aeSTejun Heo 	if (id == endp || *endp != '\0') {
620933267325STejun Heo 		*reason = "invalid port/link";
621033267325STejun Heo 		return -EINVAL;
621133267325STejun Heo 	}
621233267325STejun Heo 
621333267325STejun Heo  parse_val:
621433267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
621533267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
621633267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
621733267325STejun Heo 
621833267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
621933267325STejun Heo 			continue;
622033267325STejun Heo 
622133267325STejun Heo 		nr_matches++;
622233267325STejun Heo 		match_fp = fp;
622333267325STejun Heo 
622433267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
622533267325STejun Heo 			nr_matches = 1;
622633267325STejun Heo 			break;
622733267325STejun Heo 		}
622833267325STejun Heo 	}
622933267325STejun Heo 
623033267325STejun Heo 	if (!nr_matches) {
623133267325STejun Heo 		*reason = "unknown value";
623233267325STejun Heo 		return -EINVAL;
623333267325STejun Heo 	}
623433267325STejun Heo 	if (nr_matches > 1) {
62359de55351SArvind Yadav 		*reason = "ambiguous value";
623633267325STejun Heo 		return -EINVAL;
623733267325STejun Heo 	}
623833267325STejun Heo 
623933267325STejun Heo 	force_ent->param = *match_fp;
624033267325STejun Heo 
624133267325STejun Heo 	return 0;
624233267325STejun Heo }
624333267325STejun Heo 
624433267325STejun Heo static void __init ata_parse_force_param(void)
624533267325STejun Heo {
624633267325STejun Heo 	int idx = 0, size = 1;
624733267325STejun Heo 	int last_port = -1, last_device = -1;
624833267325STejun Heo 	char *p, *cur, *next;
624933267325STejun Heo 
625033267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
625133267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
625233267325STejun Heo 		if (*p == ',')
625333267325STejun Heo 			size++;
625433267325STejun Heo 
62556396bb22SKees Cook 	ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
625633267325STejun Heo 	if (!ata_force_tbl) {
625733267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
625833267325STejun Heo 		       "libata.force ignored\n");
625933267325STejun Heo 		return;
626033267325STejun Heo 	}
626133267325STejun Heo 
626233267325STejun Heo 	/* parse and populate the table */
626333267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
626433267325STejun Heo 		const char *reason = "";
626533267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
626633267325STejun Heo 
626733267325STejun Heo 		next = cur;
626833267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
626933267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
627033267325STejun Heo 			       "parameter \"%s\" (%s)\n",
627133267325STejun Heo 			       cur, reason);
627233267325STejun Heo 			continue;
627333267325STejun Heo 		}
627433267325STejun Heo 
627533267325STejun Heo 		if (te.port == -1) {
627633267325STejun Heo 			te.port = last_port;
627733267325STejun Heo 			te.device = last_device;
627833267325STejun Heo 		}
627933267325STejun Heo 
628033267325STejun Heo 		ata_force_tbl[idx++] = te;
628133267325STejun Heo 
628233267325STejun Heo 		last_port = te.port;
628333267325STejun Heo 		last_device = te.device;
628433267325STejun Heo 	}
628533267325STejun Heo 
628633267325STejun Heo 	ata_force_tbl_size = idx;
628733267325STejun Heo }
6288c6fd2807SJeff Garzik 
6289bf89b0bfSBartlomiej Zolnierkiewicz static void ata_free_force_param(void)
6290bf89b0bfSBartlomiej Zolnierkiewicz {
6291bf89b0bfSBartlomiej Zolnierkiewicz 	kfree(ata_force_tbl);
6292bf89b0bfSBartlomiej Zolnierkiewicz }
6293bf89b0bfSBartlomiej Zolnierkiewicz #else
6294bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_parse_force_param(void) { }
6295bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_free_force_param(void) { }
6296bf89b0bfSBartlomiej Zolnierkiewicz #endif
6297bf89b0bfSBartlomiej Zolnierkiewicz 
6298c6fd2807SJeff Garzik static int __init ata_init(void)
6299c6fd2807SJeff Garzik {
6300d9027470SGwendal Grignou 	int rc;
6301270390e1STejun Heo 
630233267325STejun Heo 	ata_parse_force_param();
630333267325STejun Heo 
6304270390e1STejun Heo 	rc = ata_sff_init();
6305ad72cf98STejun Heo 	if (rc) {
6306bf89b0bfSBartlomiej Zolnierkiewicz 		ata_free_force_param();
6307ad72cf98STejun Heo 		return rc;
6308ad72cf98STejun Heo 	}
6309c6fd2807SJeff Garzik 
6310d9027470SGwendal Grignou 	libata_transport_init();
6311d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6312d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6313d9027470SGwendal Grignou 		ata_sff_exit();
6314d9027470SGwendal Grignou 		rc = -ENOMEM;
6315d9027470SGwendal Grignou 		goto err_out;
6316d9027470SGwendal Grignou 	}
6317d9027470SGwendal Grignou 
6318c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6319c6fd2807SJeff Garzik 	return 0;
6320d9027470SGwendal Grignou 
6321d9027470SGwendal Grignou err_out:
6322d9027470SGwendal Grignou 	return rc;
6323c6fd2807SJeff Garzik }
6324c6fd2807SJeff Garzik 
6325c6fd2807SJeff Garzik static void __exit ata_exit(void)
6326c6fd2807SJeff Garzik {
6327d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6328d9027470SGwendal Grignou 	libata_transport_exit();
6329270390e1STejun Heo 	ata_sff_exit();
6330bf89b0bfSBartlomiej Zolnierkiewicz 	ata_free_force_param();
6331c6fd2807SJeff Garzik }
6332c6fd2807SJeff Garzik 
6333a4625085SBrian King subsys_initcall(ata_init);
6334c6fd2807SJeff Garzik module_exit(ata_exit);
6335c6fd2807SJeff Garzik 
63369990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6337c6fd2807SJeff Garzik 
6338c6fd2807SJeff Garzik int ata_ratelimit(void)
6339c6fd2807SJeff Garzik {
63409990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6341c6fd2807SJeff Garzik }
6342a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ratelimit);
6343c6fd2807SJeff Garzik 
6344c0c362b6STejun Heo /**
6345c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6346c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6347c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6348c0c362b6STejun Heo  *
6349c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6350c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6351c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6352c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6353c0c362b6STejun Heo  *	sleeping.
6354c0c362b6STejun Heo  *
6355c0c362b6STejun Heo  *	LOCKING:
6356c0c362b6STejun Heo  *	Might sleep.
6357c0c362b6STejun Heo  */
635897750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
635997750cebSTejun Heo {
6360c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6361c0c362b6STejun Heo 
6362c0c362b6STejun Heo 	if (owns_eh)
6363c0c362b6STejun Heo 		ata_eh_release(ap);
6364c0c362b6STejun Heo 
6365848c3920SAnil Veliyankara Madam 	if (msecs < 20) {
6366848c3920SAnil Veliyankara Madam 		unsigned long usecs = msecs * USEC_PER_MSEC;
6367848c3920SAnil Veliyankara Madam 		usleep_range(usecs, usecs + 50);
6368848c3920SAnil Veliyankara Madam 	} else {
636997750cebSTejun Heo 		msleep(msecs);
6370848c3920SAnil Veliyankara Madam 	}
6371c0c362b6STejun Heo 
6372c0c362b6STejun Heo 	if (owns_eh)
6373c0c362b6STejun Heo 		ata_eh_acquire(ap);
637497750cebSTejun Heo }
6375a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_msleep);
637697750cebSTejun Heo 
6377c6fd2807SJeff Garzik /**
6378c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
637997750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6380c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6381c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6382c6fd2807SJeff Garzik  *	@val: Wait condition
6383341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6384341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6385c6fd2807SJeff Garzik  *
6386c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6387c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6388c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6389c6fd2807SJeff Garzik  *
6390c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6391c6fd2807SJeff Garzik  *
6392c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6393c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6394c6fd2807SJeff Garzik  *
6395c6fd2807SJeff Garzik  *	LOCKING:
6396c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6397c6fd2807SJeff Garzik  *
6398c6fd2807SJeff Garzik  *	RETURNS:
6399c6fd2807SJeff Garzik  *	The final register value.
6400c6fd2807SJeff Garzik  */
640197750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6402341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6403c6fd2807SJeff Garzik {
6404341c2c95STejun Heo 	unsigned long deadline;
6405c6fd2807SJeff Garzik 	u32 tmp;
6406c6fd2807SJeff Garzik 
6407c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6408c6fd2807SJeff Garzik 
6409c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6410c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6411c6fd2807SJeff Garzik 	 * eat away the timeout.
6412c6fd2807SJeff Garzik 	 */
6413341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6414c6fd2807SJeff Garzik 
6415341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
641697750cebSTejun Heo 		ata_msleep(ap, interval);
6417c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6418c6fd2807SJeff Garzik 	}
6419c6fd2807SJeff Garzik 
6420c6fd2807SJeff Garzik 	return tmp;
6421c6fd2807SJeff Garzik }
6422a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_register);
6423c6fd2807SJeff Garzik 
6424c6fd2807SJeff Garzik /*
6425c6fd2807SJeff Garzik  * Dummy port_ops
6426c6fd2807SJeff Garzik  */
6427c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6428c6fd2807SJeff Garzik {
6429c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6430c6fd2807SJeff Garzik }
6431c6fd2807SJeff Garzik 
6432182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6433182d7bbaSTejun Heo {
6434182d7bbaSTejun Heo 	/* truly dummy */
6435182d7bbaSTejun Heo }
6436182d7bbaSTejun Heo 
6437029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6438c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6439c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6440182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6441e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
6442e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
6443c6fd2807SJeff Garzik };
6444a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
6445c6fd2807SJeff Garzik 
644621b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
644721b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
644821b0ad4fSTejun Heo };
6449a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_info);
645021b0ad4fSTejun Heo 
6451c6fd2807SJeff Garzik /*
6452a9a79dfeSJoe Perches  * Utility print functions
6453a9a79dfeSJoe Perches  */
6454d7bead1bSJoe Perches void ata_port_printk(const struct ata_port *ap, const char *level,
6455a9a79dfeSJoe Perches 		     const char *fmt, ...)
6456a9a79dfeSJoe Perches {
6457a9a79dfeSJoe Perches 	struct va_format vaf;
6458a9a79dfeSJoe Perches 	va_list args;
6459a9a79dfeSJoe Perches 
6460a9a79dfeSJoe Perches 	va_start(args, fmt);
6461a9a79dfeSJoe Perches 
6462a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6463a9a79dfeSJoe Perches 	vaf.va = &args;
6464a9a79dfeSJoe Perches 
6465d7bead1bSJoe Perches 	printk("%sata%u: %pV", level, ap->print_id, &vaf);
6466a9a79dfeSJoe Perches 
6467a9a79dfeSJoe Perches 	va_end(args);
6468a9a79dfeSJoe Perches }
6469a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_port_printk);
6470a9a79dfeSJoe Perches 
6471d7bead1bSJoe Perches void ata_link_printk(const struct ata_link *link, const char *level,
6472a9a79dfeSJoe Perches 		     const char *fmt, ...)
6473a9a79dfeSJoe Perches {
6474a9a79dfeSJoe Perches 	struct va_format vaf;
6475a9a79dfeSJoe Perches 	va_list args;
6476a9a79dfeSJoe Perches 
6477a9a79dfeSJoe Perches 	va_start(args, fmt);
6478a9a79dfeSJoe Perches 
6479a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6480a9a79dfeSJoe Perches 	vaf.va = &args;
6481a9a79dfeSJoe Perches 
6482a9a79dfeSJoe Perches 	if (sata_pmp_attached(link->ap) || link->ap->slave_link)
6483d7bead1bSJoe Perches 		printk("%sata%u.%02u: %pV",
6484a9a79dfeSJoe Perches 		       level, link->ap->print_id, link->pmp, &vaf);
6485a9a79dfeSJoe Perches 	else
6486d7bead1bSJoe Perches 		printk("%sata%u: %pV",
6487a9a79dfeSJoe Perches 		       level, link->ap->print_id, &vaf);
6488a9a79dfeSJoe Perches 
6489a9a79dfeSJoe Perches 	va_end(args);
6490a9a79dfeSJoe Perches }
6491a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_link_printk);
6492a9a79dfeSJoe Perches 
6493d7bead1bSJoe Perches void ata_dev_printk(const struct ata_device *dev, const char *level,
6494a9a79dfeSJoe Perches 		    const char *fmt, ...)
6495a9a79dfeSJoe Perches {
6496a9a79dfeSJoe Perches 	struct va_format vaf;
6497a9a79dfeSJoe Perches 	va_list args;
6498a9a79dfeSJoe Perches 
6499a9a79dfeSJoe Perches 	va_start(args, fmt);
6500a9a79dfeSJoe Perches 
6501a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6502a9a79dfeSJoe Perches 	vaf.va = &args;
6503a9a79dfeSJoe Perches 
6504d7bead1bSJoe Perches 	printk("%sata%u.%02u: %pV",
6505a9a79dfeSJoe Perches 	       level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
6506a9a79dfeSJoe Perches 	       &vaf);
6507a9a79dfeSJoe Perches 
6508a9a79dfeSJoe Perches 	va_end(args);
6509a9a79dfeSJoe Perches }
6510a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_dev_printk);
6511a9a79dfeSJoe Perches 
651206296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
651306296a1eSJoe Perches {
651406296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
651506296a1eSJoe Perches }
651606296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
6517