xref: /openbmc/linux/drivers/ata/libata-core.c (revision 1a660164)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  *  libata-core.c - helper library for ATA
3c6fd2807SJeff Garzik  *
4c6fd2807SJeff Garzik  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5c6fd2807SJeff Garzik  *    		    Please ALWAYS copy linux-ide@vger.kernel.org
6c6fd2807SJeff Garzik  *		    on emails.
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
9c6fd2807SJeff Garzik  *  Copyright 2003-2004 Jeff Garzik
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *
12c6fd2807SJeff Garzik  *  This program is free software; you can redistribute it and/or modify
13c6fd2807SJeff Garzik  *  it under the terms of the GNU General Public License as published by
14c6fd2807SJeff Garzik  *  the Free Software Foundation; either version 2, or (at your option)
15c6fd2807SJeff Garzik  *  any later version.
16c6fd2807SJeff Garzik  *
17c6fd2807SJeff Garzik  *  This program is distributed in the hope that it will be useful,
18c6fd2807SJeff Garzik  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19c6fd2807SJeff Garzik  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c6fd2807SJeff Garzik  *  GNU General Public License for more details.
21c6fd2807SJeff Garzik  *
22c6fd2807SJeff Garzik  *  You should have received a copy of the GNU General Public License
23c6fd2807SJeff Garzik  *  along with this program; see the file COPYING.  If not, write to
24c6fd2807SJeff Garzik  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25c6fd2807SJeff Garzik  *
26c6fd2807SJeff Garzik  *
27c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
28c6fd2807SJeff Garzik  *  as Documentation/DocBook/libata.*
29c6fd2807SJeff Garzik  *
30c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
31c6fd2807SJeff Garzik  *  http://www.sata-io.org/
32c6fd2807SJeff Garzik  *
3392c52c52SAlan Cox  *  Standards documents from:
3492c52c52SAlan Cox  *	http://www.t13.org (ATA standards, PCI DMA IDE spec)
3592c52c52SAlan Cox  *	http://www.t10.org (SCSI MMC - for ATAPI MMC)
3692c52c52SAlan Cox  *	http://www.sata-io.org (SATA)
3792c52c52SAlan Cox  *	http://www.compactflash.org (CF)
3892c52c52SAlan Cox  *	http://www.qic.org (QIC157 - Tape and DSC)
3992c52c52SAlan Cox  *	http://www.ce-ata.org (CE-ATA: not supported)
4092c52c52SAlan Cox  *
41c6fd2807SJeff Garzik  */
42c6fd2807SJeff Garzik 
43c6fd2807SJeff Garzik #include <linux/kernel.h>
44c6fd2807SJeff Garzik #include <linux/module.h>
45c6fd2807SJeff Garzik #include <linux/pci.h>
46c6fd2807SJeff Garzik #include <linux/init.h>
47c6fd2807SJeff Garzik #include <linux/list.h>
48c6fd2807SJeff Garzik #include <linux/mm.h>
49c6fd2807SJeff Garzik #include <linux/spinlock.h>
50c6fd2807SJeff Garzik #include <linux/blkdev.h>
51c6fd2807SJeff Garzik #include <linux/delay.h>
52c6fd2807SJeff Garzik #include <linux/timer.h>
53c6fd2807SJeff Garzik #include <linux/interrupt.h>
54c6fd2807SJeff Garzik #include <linux/completion.h>
55c6fd2807SJeff Garzik #include <linux/suspend.h>
56c6fd2807SJeff Garzik #include <linux/workqueue.h>
57c6fd2807SJeff Garzik #include <linux/scatterlist.h>
582dcb407eSJeff Garzik #include <linux/io.h>
5979318057SArjan van de Ven #include <linux/async.h>
60c6fd2807SJeff Garzik #include <scsi/scsi.h>
61c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
62c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
63c6fd2807SJeff Garzik #include <linux/libata.h>
64c6fd2807SJeff Garzik #include <asm/byteorder.h>
65140b5e59STejun Heo #include <linux/cdrom.h>
66c6fd2807SJeff Garzik 
67c6fd2807SJeff Garzik #include "libata.h"
68c6fd2807SJeff Garzik 
69fda0efc5SJeff Garzik 
70c6fd2807SJeff Garzik /* debounce timing parameters in msecs { interval, duration, timeout } */
71c6fd2807SJeff Garzik const unsigned long sata_deb_timing_normal[]		= {   5,  100, 2000 };
72c6fd2807SJeff Garzik const unsigned long sata_deb_timing_hotplug[]		= {  25,  500, 2000 };
73c6fd2807SJeff Garzik const unsigned long sata_deb_timing_long[]		= { 100, 2000, 5000 };
74c6fd2807SJeff Garzik 
75029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
760aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
77203c75b8STejun Heo 	.postreset		= ata_std_postreset,
78a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
79029cfd6bSTejun Heo };
80029cfd6bSTejun Heo 
81029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
82029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
83029cfd6bSTejun Heo 
84029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
8557c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
86029cfd6bSTejun Heo };
87029cfd6bSTejun Heo 
88c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
89c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
90c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
91218f3d30SJeff Garzik static unsigned int ata_dev_set_feature(struct ata_device *dev,
92218f3d30SJeff Garzik 					u8 enable, u8 feature);
93c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
9475683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
95c6fd2807SJeff Garzik 
96f3187195STejun Heo unsigned int ata_print_id = 1;
97c6fd2807SJeff Garzik static struct workqueue_struct *ata_wq;
98c6fd2807SJeff Garzik 
99c6fd2807SJeff Garzik struct workqueue_struct *ata_aux_wq;
100c6fd2807SJeff Garzik 
10133267325STejun Heo struct ata_force_param {
10233267325STejun Heo 	const char	*name;
10333267325STejun Heo 	unsigned int	cbl;
10433267325STejun Heo 	int		spd_limit;
10533267325STejun Heo 	unsigned long	xfer_mask;
10633267325STejun Heo 	unsigned int	horkage_on;
10733267325STejun Heo 	unsigned int	horkage_off;
10805944bdfSTejun Heo 	unsigned int	lflags;
10933267325STejun Heo };
11033267325STejun Heo 
11133267325STejun Heo struct ata_force_ent {
11233267325STejun Heo 	int			port;
11333267325STejun Heo 	int			device;
11433267325STejun Heo 	struct ata_force_param	param;
11533267325STejun Heo };
11633267325STejun Heo 
11733267325STejun Heo static struct ata_force_ent *ata_force_tbl;
11833267325STejun Heo static int ata_force_tbl_size;
11933267325STejun Heo 
12033267325STejun Heo static char ata_force_param_buf[PAGE_SIZE] __initdata;
1217afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1227afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
12333267325STejun Heo MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
12433267325STejun Heo 
1252486fa56STejun Heo static int atapi_enabled = 1;
126c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
127c6fd2807SJeff Garzik MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
128c6fd2807SJeff Garzik 
129c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
130c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
131c6fd2807SJeff Garzik MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
132c6fd2807SJeff Garzik 
133baf4fdfaSMark Lord int atapi_passthru16 = 1;
134baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
135baf4fdfaSMark Lord MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
136baf4fdfaSMark Lord 
137c6fd2807SJeff Garzik int libata_fua = 0;
138c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
139c6fd2807SJeff Garzik MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
140c6fd2807SJeff Garzik 
1412dcb407eSJeff Garzik static int ata_ignore_hpa;
1421e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1431e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1441e999736SAlan Cox 
145b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
146b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
147b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
148b3a70601SAlan Cox 
14987fbc5a0STejun Heo static int ata_probe_timeout;
150c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
151c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
152c6fd2807SJeff Garzik 
1536ebe9d86SJeff Garzik int libata_noacpi = 0;
154d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
1556ebe9d86SJeff Garzik MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
15611ef697bSKristen Carlson Accardi 
157ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
158ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
159ae8d4ee7SAlan Cox MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
160ae8d4ee7SAlan Cox 
161c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
162c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
163c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
164c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
165c6fd2807SJeff Garzik 
166c6fd2807SJeff Garzik 
1679913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1689913ff8aSTejun Heo {
1699913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1709913ff8aSTejun Heo }
1719913ff8aSTejun Heo 
1721eca4365STejun Heo /**
1731eca4365STejun Heo  *	ata_link_next - link iteration helper
1741eca4365STejun Heo  *	@link: the previous link, NULL to start
1751eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1761eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
177aadffb68STejun Heo  *
178aadffb68STejun Heo  *	LOCKING:
179aadffb68STejun Heo  *	Host lock or EH context.
1801eca4365STejun Heo  *
1811eca4365STejun Heo  *	RETURNS:
1821eca4365STejun Heo  *	Pointer to the next link.
183aadffb68STejun Heo  */
1841eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1851eca4365STejun Heo 			       enum ata_link_iter_mode mode)
186aadffb68STejun Heo {
1871eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
1881eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
1891eca4365STejun Heo 
190aadffb68STejun Heo 	/* NULL link indicates start of iteration */
1911eca4365STejun Heo 	if (!link)
1921eca4365STejun Heo 		switch (mode) {
1931eca4365STejun Heo 		case ATA_LITER_EDGE:
1941eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
1951eca4365STejun Heo 			if (sata_pmp_attached(ap))
196aadffb68STejun Heo 				return ap->pmp_link;
1971eca4365STejun Heo 			/* fall through */
1981eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
199aadffb68STejun Heo 			return &ap->link;
200aadffb68STejun Heo 		}
201aadffb68STejun Heo 
2021eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2031eca4365STejun Heo 	if (link == &ap->link)
2041eca4365STejun Heo 		switch (mode) {
2051eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2061eca4365STejun Heo 			if (sata_pmp_attached(ap))
207aadffb68STejun Heo 				return ap->pmp_link;
2081eca4365STejun Heo 			/* fall through */
2091eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2101eca4365STejun Heo 			if (unlikely(ap->slave_link))
2111eca4365STejun Heo 				return ap->slave_link;
2121eca4365STejun Heo 			/* fall through */
2131eca4365STejun Heo 		case ATA_LITER_EDGE:
2141eca4365STejun Heo 			return NULL;
215aadffb68STejun Heo 		}
216aadffb68STejun Heo 
217b1c72916STejun Heo 	/* slave_link excludes PMP */
218b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
219b1c72916STejun Heo 		return NULL;
220b1c72916STejun Heo 
2211eca4365STejun Heo 	/* we were over a PMP link */
222aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
223aadffb68STejun Heo 		return link;
2241eca4365STejun Heo 
2251eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2261eca4365STejun Heo 		return &ap->link;
2271eca4365STejun Heo 
228aadffb68STejun Heo 	return NULL;
229aadffb68STejun Heo }
230aadffb68STejun Heo 
231c6fd2807SJeff Garzik /**
2321eca4365STejun Heo  *	ata_dev_next - device iteration helper
2331eca4365STejun Heo  *	@dev: the previous device, NULL to start
2341eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2351eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2361eca4365STejun Heo  *
2371eca4365STejun Heo  *	LOCKING:
2381eca4365STejun Heo  *	Host lock or EH context.
2391eca4365STejun Heo  *
2401eca4365STejun Heo  *	RETURNS:
2411eca4365STejun Heo  *	Pointer to the next device.
2421eca4365STejun Heo  */
2431eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2441eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2451eca4365STejun Heo {
2461eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2471eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2481eca4365STejun Heo 
2491eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2501eca4365STejun Heo 	if (!dev)
2511eca4365STejun Heo 		switch (mode) {
2521eca4365STejun Heo 		case ATA_DITER_ENABLED:
2531eca4365STejun Heo 		case ATA_DITER_ALL:
2541eca4365STejun Heo 			dev = link->device;
2551eca4365STejun Heo 			goto check;
2561eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2571eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2581eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2591eca4365STejun Heo 			goto check;
2601eca4365STejun Heo 		}
2611eca4365STejun Heo 
2621eca4365STejun Heo  next:
2631eca4365STejun Heo 	/* move to the next one */
2641eca4365STejun Heo 	switch (mode) {
2651eca4365STejun Heo 	case ATA_DITER_ENABLED:
2661eca4365STejun Heo 	case ATA_DITER_ALL:
2671eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2681eca4365STejun Heo 			goto check;
2691eca4365STejun Heo 		return NULL;
2701eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2711eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2721eca4365STejun Heo 		if (--dev >= link->device)
2731eca4365STejun Heo 			goto check;
2741eca4365STejun Heo 		return NULL;
2751eca4365STejun Heo 	}
2761eca4365STejun Heo 
2771eca4365STejun Heo  check:
2781eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2791eca4365STejun Heo 	    !ata_dev_enabled(dev))
2801eca4365STejun Heo 		goto next;
2811eca4365STejun Heo 	return dev;
2821eca4365STejun Heo }
2831eca4365STejun Heo 
2841eca4365STejun Heo /**
285b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
286b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
287b1c72916STejun Heo  *
288b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
289b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
290b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
291b1c72916STejun Heo  *
292b1c72916STejun Heo  *	LOCKING:
293b1c72916STejun Heo  *	Don't care.
294b1c72916STejun Heo  *
295b1c72916STejun Heo  *	RETURNS:
296b1c72916STejun Heo  *	Pointer to the found physical link.
297b1c72916STejun Heo  */
298b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
299b1c72916STejun Heo {
300b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
301b1c72916STejun Heo 
302b1c72916STejun Heo 	if (!ap->slave_link)
303b1c72916STejun Heo 		return dev->link;
304b1c72916STejun Heo 	if (!dev->devno)
305b1c72916STejun Heo 		return &ap->link;
306b1c72916STejun Heo 	return ap->slave_link;
307b1c72916STejun Heo }
308b1c72916STejun Heo 
309b1c72916STejun Heo /**
31033267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3114cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
31233267325STejun Heo  *
31333267325STejun Heo  *	Force cable type according to libata.force and whine about it.
31433267325STejun Heo  *	The last entry which has matching port number is used, so it
31533267325STejun Heo  *	can be specified as part of device force parameters.  For
31633267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
31733267325STejun Heo  *	same effect.
31833267325STejun Heo  *
31933267325STejun Heo  *	LOCKING:
32033267325STejun Heo  *	EH context.
32133267325STejun Heo  */
32233267325STejun Heo void ata_force_cbl(struct ata_port *ap)
32333267325STejun Heo {
32433267325STejun Heo 	int i;
32533267325STejun Heo 
32633267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
32733267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
32833267325STejun Heo 
32933267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
33033267325STejun Heo 			continue;
33133267325STejun Heo 
33233267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
33333267325STejun Heo 			continue;
33433267325STejun Heo 
33533267325STejun Heo 		ap->cbl = fe->param.cbl;
33633267325STejun Heo 		ata_port_printk(ap, KERN_NOTICE,
33733267325STejun Heo 				"FORCE: cable set to %s\n", fe->param.name);
33833267325STejun Heo 		return;
33933267325STejun Heo 	}
34033267325STejun Heo }
34133267325STejun Heo 
34233267325STejun Heo /**
34305944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
34433267325STejun Heo  *	@link: ATA link of interest
34533267325STejun Heo  *
34605944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
34705944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
34805944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
34905944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
35005944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
35105944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
352b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
353b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
35433267325STejun Heo  *
35533267325STejun Heo  *	LOCKING:
35633267325STejun Heo  *	EH context.
35733267325STejun Heo  */
35805944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
35933267325STejun Heo {
36005944bdfSTejun Heo 	bool did_spd = false;
361b1c72916STejun Heo 	int linkno = link->pmp;
362b1c72916STejun Heo 	int i;
36333267325STejun Heo 
36433267325STejun Heo 	if (ata_is_host_link(link))
365b1c72916STejun Heo 		linkno += 15;
36633267325STejun Heo 
36733267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
36833267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
36933267325STejun Heo 
37033267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
37133267325STejun Heo 			continue;
37233267325STejun Heo 
37333267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
37433267325STejun Heo 			continue;
37533267325STejun Heo 
37605944bdfSTejun Heo 		/* only honor the first spd limit */
37705944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
37833267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
37933267325STejun Heo 			ata_link_printk(link, KERN_NOTICE,
38005944bdfSTejun Heo 					"FORCE: PHY spd limit set to %s\n",
38105944bdfSTejun Heo 					fe->param.name);
38205944bdfSTejun Heo 			did_spd = true;
38305944bdfSTejun Heo 		}
38405944bdfSTejun Heo 
38505944bdfSTejun Heo 		/* let lflags stack */
38605944bdfSTejun Heo 		if (fe->param.lflags) {
38705944bdfSTejun Heo 			link->flags |= fe->param.lflags;
38805944bdfSTejun Heo 			ata_link_printk(link, KERN_NOTICE,
38905944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
39005944bdfSTejun Heo 					fe->param.lflags, link->flags);
39105944bdfSTejun Heo 		}
39233267325STejun Heo 	}
39333267325STejun Heo }
39433267325STejun Heo 
39533267325STejun Heo /**
39633267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
39733267325STejun Heo  *	@dev: ATA device of interest
39833267325STejun Heo  *
39933267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
40033267325STejun Heo  *	For consistency with link selection, device number 15 selects
40133267325STejun Heo  *	the first device connected to the host link.
40233267325STejun Heo  *
40333267325STejun Heo  *	LOCKING:
40433267325STejun Heo  *	EH context.
40533267325STejun Heo  */
40633267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
40733267325STejun Heo {
40833267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
40933267325STejun Heo 	int alt_devno = devno;
41033267325STejun Heo 	int i;
41133267325STejun Heo 
412b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
413b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
414b1c72916STejun Heo 		alt_devno += 15;
41533267325STejun Heo 
41633267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
41733267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
41833267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
41933267325STejun Heo 
42033267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
42133267325STejun Heo 			continue;
42233267325STejun Heo 
42333267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
42433267325STejun Heo 		    fe->device != alt_devno)
42533267325STejun Heo 			continue;
42633267325STejun Heo 
42733267325STejun Heo 		if (!fe->param.xfer_mask)
42833267325STejun Heo 			continue;
42933267325STejun Heo 
43033267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
43133267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
43233267325STejun Heo 		if (udma_mask)
43333267325STejun Heo 			dev->udma_mask = udma_mask;
43433267325STejun Heo 		else if (mwdma_mask) {
43533267325STejun Heo 			dev->udma_mask = 0;
43633267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
43733267325STejun Heo 		} else {
43833267325STejun Heo 			dev->udma_mask = 0;
43933267325STejun Heo 			dev->mwdma_mask = 0;
44033267325STejun Heo 			dev->pio_mask = pio_mask;
44133267325STejun Heo 		}
44233267325STejun Heo 
44333267325STejun Heo 		ata_dev_printk(dev, KERN_NOTICE,
44433267325STejun Heo 			"FORCE: xfer_mask set to %s\n", fe->param.name);
44533267325STejun Heo 		return;
44633267325STejun Heo 	}
44733267325STejun Heo }
44833267325STejun Heo 
44933267325STejun Heo /**
45033267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
45133267325STejun Heo  *	@dev: ATA device of interest
45233267325STejun Heo  *
45333267325STejun Heo  *	Force horkage according to libata.force and whine about it.
45433267325STejun Heo  *	For consistency with link selection, device number 15 selects
45533267325STejun Heo  *	the first device connected to the host link.
45633267325STejun Heo  *
45733267325STejun Heo  *	LOCKING:
45833267325STejun Heo  *	EH context.
45933267325STejun Heo  */
46033267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
46133267325STejun Heo {
46233267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
46333267325STejun Heo 	int alt_devno = devno;
46433267325STejun Heo 	int i;
46533267325STejun Heo 
466b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
467b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
468b1c72916STejun Heo 		alt_devno += 15;
46933267325STejun Heo 
47033267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
47133267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
47233267325STejun Heo 
47333267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
47433267325STejun Heo 			continue;
47533267325STejun Heo 
47633267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
47733267325STejun Heo 		    fe->device != alt_devno)
47833267325STejun Heo 			continue;
47933267325STejun Heo 
48033267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
48133267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
48233267325STejun Heo 			continue;
48333267325STejun Heo 
48433267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
48533267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
48633267325STejun Heo 
48733267325STejun Heo 		ata_dev_printk(dev, KERN_NOTICE,
48833267325STejun Heo 			"FORCE: horkage modified (%s)\n", fe->param.name);
48933267325STejun Heo 	}
49033267325STejun Heo }
49133267325STejun Heo 
49233267325STejun Heo /**
493436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
494436d34b3STejun Heo  *	@opcode: SCSI opcode
495436d34b3STejun Heo  *
496436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
497436d34b3STejun Heo  *
498436d34b3STejun Heo  *	LOCKING:
499436d34b3STejun Heo  *	None.
500436d34b3STejun Heo  *
501436d34b3STejun Heo  *	RETURNS:
502436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
503436d34b3STejun Heo  */
504436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
505436d34b3STejun Heo {
506436d34b3STejun Heo 	switch (opcode) {
507436d34b3STejun Heo 	case GPCMD_READ_10:
508436d34b3STejun Heo 	case GPCMD_READ_12:
509436d34b3STejun Heo 		return ATAPI_READ;
510436d34b3STejun Heo 
511436d34b3STejun Heo 	case GPCMD_WRITE_10:
512436d34b3STejun Heo 	case GPCMD_WRITE_12:
513436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
514436d34b3STejun Heo 		return ATAPI_WRITE;
515436d34b3STejun Heo 
516436d34b3STejun Heo 	case GPCMD_READ_CD:
517436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
518436d34b3STejun Heo 		return ATAPI_READ_CD;
519436d34b3STejun Heo 
520e52dcc48STejun Heo 	case ATA_16:
521e52dcc48STejun Heo 	case ATA_12:
522e52dcc48STejun Heo 		if (atapi_passthru16)
523e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
524e52dcc48STejun Heo 		/* fall thru */
525436d34b3STejun Heo 	default:
526436d34b3STejun Heo 		return ATAPI_MISC;
527436d34b3STejun Heo 	}
528436d34b3STejun Heo }
529436d34b3STejun Heo 
530436d34b3STejun Heo /**
531c6fd2807SJeff Garzik  *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
532c6fd2807SJeff Garzik  *	@tf: Taskfile to convert
533c6fd2807SJeff Garzik  *	@pmp: Port multiplier port
5349977126cSTejun Heo  *	@is_cmd: This FIS is for command
5359977126cSTejun Heo  *	@fis: Buffer into which data will output
536c6fd2807SJeff Garzik  *
537c6fd2807SJeff Garzik  *	Converts a standard ATA taskfile to a Serial ATA
538c6fd2807SJeff Garzik  *	FIS structure (Register - Host to Device).
539c6fd2807SJeff Garzik  *
540c6fd2807SJeff Garzik  *	LOCKING:
541c6fd2807SJeff Garzik  *	Inherited from caller.
542c6fd2807SJeff Garzik  */
5439977126cSTejun Heo void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
544c6fd2807SJeff Garzik {
545c6fd2807SJeff Garzik 	fis[0] = 0x27;			/* Register - Host to Device FIS */
5469977126cSTejun Heo 	fis[1] = pmp & 0xf;		/* Port multiplier number*/
5479977126cSTejun Heo 	if (is_cmd)
5489977126cSTejun Heo 		fis[1] |= (1 << 7);	/* bit 7 indicates Command FIS */
5499977126cSTejun Heo 
550c6fd2807SJeff Garzik 	fis[2] = tf->command;
551c6fd2807SJeff Garzik 	fis[3] = tf->feature;
552c6fd2807SJeff Garzik 
553c6fd2807SJeff Garzik 	fis[4] = tf->lbal;
554c6fd2807SJeff Garzik 	fis[5] = tf->lbam;
555c6fd2807SJeff Garzik 	fis[6] = tf->lbah;
556c6fd2807SJeff Garzik 	fis[7] = tf->device;
557c6fd2807SJeff Garzik 
558c6fd2807SJeff Garzik 	fis[8] = tf->hob_lbal;
559c6fd2807SJeff Garzik 	fis[9] = tf->hob_lbam;
560c6fd2807SJeff Garzik 	fis[10] = tf->hob_lbah;
561c6fd2807SJeff Garzik 	fis[11] = tf->hob_feature;
562c6fd2807SJeff Garzik 
563c6fd2807SJeff Garzik 	fis[12] = tf->nsect;
564c6fd2807SJeff Garzik 	fis[13] = tf->hob_nsect;
565c6fd2807SJeff Garzik 	fis[14] = 0;
566c6fd2807SJeff Garzik 	fis[15] = tf->ctl;
567c6fd2807SJeff Garzik 
568c6fd2807SJeff Garzik 	fis[16] = 0;
569c6fd2807SJeff Garzik 	fis[17] = 0;
570c6fd2807SJeff Garzik 	fis[18] = 0;
571c6fd2807SJeff Garzik 	fis[19] = 0;
572c6fd2807SJeff Garzik }
573c6fd2807SJeff Garzik 
574c6fd2807SJeff Garzik /**
575c6fd2807SJeff Garzik  *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
576c6fd2807SJeff Garzik  *	@fis: Buffer from which data will be input
577c6fd2807SJeff Garzik  *	@tf: Taskfile to output
578c6fd2807SJeff Garzik  *
579c6fd2807SJeff Garzik  *	Converts a serial ATA FIS structure to a standard ATA taskfile.
580c6fd2807SJeff Garzik  *
581c6fd2807SJeff Garzik  *	LOCKING:
582c6fd2807SJeff Garzik  *	Inherited from caller.
583c6fd2807SJeff Garzik  */
584c6fd2807SJeff Garzik 
585c6fd2807SJeff Garzik void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
586c6fd2807SJeff Garzik {
587c6fd2807SJeff Garzik 	tf->command	= fis[2];	/* status */
588c6fd2807SJeff Garzik 	tf->feature	= fis[3];	/* error */
589c6fd2807SJeff Garzik 
590c6fd2807SJeff Garzik 	tf->lbal	= fis[4];
591c6fd2807SJeff Garzik 	tf->lbam	= fis[5];
592c6fd2807SJeff Garzik 	tf->lbah	= fis[6];
593c6fd2807SJeff Garzik 	tf->device	= fis[7];
594c6fd2807SJeff Garzik 
595c6fd2807SJeff Garzik 	tf->hob_lbal	= fis[8];
596c6fd2807SJeff Garzik 	tf->hob_lbam	= fis[9];
597c6fd2807SJeff Garzik 	tf->hob_lbah	= fis[10];
598c6fd2807SJeff Garzik 
599c6fd2807SJeff Garzik 	tf->nsect	= fis[12];
600c6fd2807SJeff Garzik 	tf->hob_nsect	= fis[13];
601c6fd2807SJeff Garzik }
602c6fd2807SJeff Garzik 
603c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
604c6fd2807SJeff Garzik 	/* pio multi */
605c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
606c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
607c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
608c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
609c6fd2807SJeff Garzik 	0,
610c6fd2807SJeff Garzik 	0,
611c6fd2807SJeff Garzik 	0,
612c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
613c6fd2807SJeff Garzik 	/* pio */
614c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
615c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
616c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
617c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
618c6fd2807SJeff Garzik 	0,
619c6fd2807SJeff Garzik 	0,
620c6fd2807SJeff Garzik 	0,
621c6fd2807SJeff Garzik 	0,
622c6fd2807SJeff Garzik 	/* dma */
623c6fd2807SJeff Garzik 	ATA_CMD_READ,
624c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
625c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
626c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
627c6fd2807SJeff Garzik 	0,
628c6fd2807SJeff Garzik 	0,
629c6fd2807SJeff Garzik 	0,
630c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
631c6fd2807SJeff Garzik };
632c6fd2807SJeff Garzik 
633c6fd2807SJeff Garzik /**
634c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
635bd056d7eSTejun Heo  *	@tf: command to examine and configure
636bd056d7eSTejun Heo  *	@dev: device tf belongs to
637c6fd2807SJeff Garzik  *
638c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
639c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
640c6fd2807SJeff Garzik  *
641c6fd2807SJeff Garzik  *	LOCKING:
642c6fd2807SJeff Garzik  *	caller.
643c6fd2807SJeff Garzik  */
644bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
645c6fd2807SJeff Garzik {
646c6fd2807SJeff Garzik 	u8 cmd;
647c6fd2807SJeff Garzik 
648c6fd2807SJeff Garzik 	int index, fua, lba48, write;
649c6fd2807SJeff Garzik 
650c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
651c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
652c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
653c6fd2807SJeff Garzik 
654c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
655c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
656c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6579af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
658c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
659c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
660c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
661c6fd2807SJeff Garzik 	} else {
662c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
663c6fd2807SJeff Garzik 		index = 16;
664c6fd2807SJeff Garzik 	}
665c6fd2807SJeff Garzik 
666c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
667c6fd2807SJeff Garzik 	if (cmd) {
668c6fd2807SJeff Garzik 		tf->command = cmd;
669c6fd2807SJeff Garzik 		return 0;
670c6fd2807SJeff Garzik 	}
671c6fd2807SJeff Garzik 	return -1;
672c6fd2807SJeff Garzik }
673c6fd2807SJeff Garzik 
674c6fd2807SJeff Garzik /**
67535b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
67635b649feSTejun Heo  *	@tf: ATA taskfile of interest
67735b649feSTejun Heo  *	@dev: ATA device @tf belongs to
67835b649feSTejun Heo  *
67935b649feSTejun Heo  *	LOCKING:
68035b649feSTejun Heo  *	None.
68135b649feSTejun Heo  *
68235b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
68335b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
68435b649feSTejun Heo  *	flags select the address format to use.
68535b649feSTejun Heo  *
68635b649feSTejun Heo  *	RETURNS:
68735b649feSTejun Heo  *	Block address read from @tf.
68835b649feSTejun Heo  */
68935b649feSTejun Heo u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
69035b649feSTejun Heo {
69135b649feSTejun Heo 	u64 block = 0;
69235b649feSTejun Heo 
69335b649feSTejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
69435b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
69535b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
69635b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
69744901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
69835b649feSTejun Heo 		} else
69935b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
70035b649feSTejun Heo 
70135b649feSTejun Heo 		block |= tf->lbah << 16;
70235b649feSTejun Heo 		block |= tf->lbam << 8;
70335b649feSTejun Heo 		block |= tf->lbal;
70435b649feSTejun Heo 	} else {
70535b649feSTejun Heo 		u32 cyl, head, sect;
70635b649feSTejun Heo 
70735b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
70835b649feSTejun Heo 		head = tf->device & 0xf;
70935b649feSTejun Heo 		sect = tf->lbal;
71035b649feSTejun Heo 
71135b649feSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect;
71235b649feSTejun Heo 	}
71335b649feSTejun Heo 
71435b649feSTejun Heo 	return block;
71535b649feSTejun Heo }
71635b649feSTejun Heo 
71735b649feSTejun Heo /**
718bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
719bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
720bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
721bd056d7eSTejun Heo  *	@block: Block address
722bd056d7eSTejun Heo  *	@n_block: Number of blocks
723bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
724bd056d7eSTejun Heo  *	@tag: tag
725bd056d7eSTejun Heo  *
726bd056d7eSTejun Heo  *	LOCKING:
727bd056d7eSTejun Heo  *	None.
728bd056d7eSTejun Heo  *
729bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
730bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
731bd056d7eSTejun Heo  *
732bd056d7eSTejun Heo  *	RETURNS:
733bd056d7eSTejun Heo  *
734bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
735bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
736bd056d7eSTejun Heo  */
737bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
738bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
739bd056d7eSTejun Heo 		    unsigned int tag)
740bd056d7eSTejun Heo {
741bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
742bd056d7eSTejun Heo 	tf->flags |= tf_flags;
743bd056d7eSTejun Heo 
7446d1245bfSTejun Heo 	if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
745bd056d7eSTejun Heo 		/* yay, NCQ */
746bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
747bd056d7eSTejun Heo 			return -ERANGE;
748bd056d7eSTejun Heo 
749bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
750bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
751bd056d7eSTejun Heo 
752bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
753bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
754bd056d7eSTejun Heo 		else
755bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
756bd056d7eSTejun Heo 
757bd056d7eSTejun Heo 		tf->nsect = tag << 3;
758bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
759bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
760bd056d7eSTejun Heo 
761bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
762bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
763bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
764bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
765bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
766bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
767bd056d7eSTejun Heo 
768bd056d7eSTejun Heo 		tf->device = 1 << 6;
769bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
770bd056d7eSTejun Heo 			tf->device |= 1 << 7;
771bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
772bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
773bd056d7eSTejun Heo 
774bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
775bd056d7eSTejun Heo 			/* use LBA28 */
776bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
777bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
778bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
779bd056d7eSTejun Heo 				return -ERANGE;
780bd056d7eSTejun Heo 
781bd056d7eSTejun Heo 			/* use LBA48 */
782bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
783bd056d7eSTejun Heo 
784bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
785bd056d7eSTejun Heo 
786bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
787bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
788bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
789bd056d7eSTejun Heo 		} else
790bd056d7eSTejun Heo 			/* request too large even for LBA48 */
791bd056d7eSTejun Heo 			return -ERANGE;
792bd056d7eSTejun Heo 
793bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
794bd056d7eSTejun Heo 			return -EINVAL;
795bd056d7eSTejun Heo 
796bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
797bd056d7eSTejun Heo 
798bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
799bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
800bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
801bd056d7eSTejun Heo 
802bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
803bd056d7eSTejun Heo 	} else {
804bd056d7eSTejun Heo 		/* CHS */
805bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
806bd056d7eSTejun Heo 
807bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
808bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
809bd056d7eSTejun Heo 			return -ERANGE;
810bd056d7eSTejun Heo 
811bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
812bd056d7eSTejun Heo 			return -EINVAL;
813bd056d7eSTejun Heo 
814bd056d7eSTejun Heo 		/* Convert LBA to CHS */
815bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
816bd056d7eSTejun Heo 		cyl   = track / dev->heads;
817bd056d7eSTejun Heo 		head  = track % dev->heads;
818bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
819bd056d7eSTejun Heo 
820bd056d7eSTejun Heo 		DPRINTK("block %u track %u cyl %u head %u sect %u\n",
821bd056d7eSTejun Heo 			(u32)block, track, cyl, head, sect);
822bd056d7eSTejun Heo 
823bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
824bd056d7eSTejun Heo 		   Cylinder: 0-65535
825bd056d7eSTejun Heo 		   Head: 0-15
826bd056d7eSTejun Heo 		   Sector: 1-255*/
827bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
828bd056d7eSTejun Heo 			return -ERANGE;
829bd056d7eSTejun Heo 
830bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
831bd056d7eSTejun Heo 		tf->lbal = sect;
832bd056d7eSTejun Heo 		tf->lbam = cyl;
833bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
834bd056d7eSTejun Heo 		tf->device |= head;
835bd056d7eSTejun Heo 	}
836bd056d7eSTejun Heo 
837bd056d7eSTejun Heo 	return 0;
838bd056d7eSTejun Heo }
839bd056d7eSTejun Heo 
840bd056d7eSTejun Heo /**
841c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
842c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
843c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
844c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
845c6fd2807SJeff Garzik  *
846c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
847c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
848c6fd2807SJeff Garzik  *
849c6fd2807SJeff Garzik  *	LOCKING:
850c6fd2807SJeff Garzik  *	None.
851c6fd2807SJeff Garzik  *
852c6fd2807SJeff Garzik  *	RETURNS:
853c6fd2807SJeff Garzik  *	Packed xfer_mask.
854c6fd2807SJeff Garzik  */
8557dc951aeSTejun Heo unsigned long ata_pack_xfermask(unsigned long pio_mask,
8567dc951aeSTejun Heo 				unsigned long mwdma_mask,
8577dc951aeSTejun Heo 				unsigned long udma_mask)
858c6fd2807SJeff Garzik {
859c6fd2807SJeff Garzik 	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
860c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
861c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
862c6fd2807SJeff Garzik }
863c6fd2807SJeff Garzik 
864c6fd2807SJeff Garzik /**
865c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
866c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
867c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
868c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
869c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
870c6fd2807SJeff Garzik  *
871c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
872c6fd2807SJeff Garzik  *	Any NULL distination masks will be ignored.
873c6fd2807SJeff Garzik  */
8747dc951aeSTejun Heo void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
8757dc951aeSTejun Heo 			 unsigned long *mwdma_mask, unsigned long *udma_mask)
876c6fd2807SJeff Garzik {
877c6fd2807SJeff Garzik 	if (pio_mask)
878c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
879c6fd2807SJeff Garzik 	if (mwdma_mask)
880c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
881c6fd2807SJeff Garzik 	if (udma_mask)
882c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
883c6fd2807SJeff Garzik }
884c6fd2807SJeff Garzik 
885c6fd2807SJeff Garzik static const struct ata_xfer_ent {
886c6fd2807SJeff Garzik 	int shift, bits;
887c6fd2807SJeff Garzik 	u8 base;
888c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
88970cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
89070cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
89170cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
892c6fd2807SJeff Garzik 	{ -1, },
893c6fd2807SJeff Garzik };
894c6fd2807SJeff Garzik 
895c6fd2807SJeff Garzik /**
896c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
897c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
898c6fd2807SJeff Garzik  *
899c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
900c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
901c6fd2807SJeff Garzik  *
902c6fd2807SJeff Garzik  *	LOCKING:
903c6fd2807SJeff Garzik  *	None.
904c6fd2807SJeff Garzik  *
905c6fd2807SJeff Garzik  *	RETURNS:
90670cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
907c6fd2807SJeff Garzik  */
9087dc951aeSTejun Heo u8 ata_xfer_mask2mode(unsigned long xfer_mask)
909c6fd2807SJeff Garzik {
910c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
911c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
912c6fd2807SJeff Garzik 
913c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
914c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
915c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
91670cd071eSTejun Heo 	return 0xff;
917c6fd2807SJeff Garzik }
918c6fd2807SJeff Garzik 
919c6fd2807SJeff Garzik /**
920c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
921c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
922c6fd2807SJeff Garzik  *
923c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
924c6fd2807SJeff Garzik  *
925c6fd2807SJeff Garzik  *	LOCKING:
926c6fd2807SJeff Garzik  *	None.
927c6fd2807SJeff Garzik  *
928c6fd2807SJeff Garzik  *	RETURNS:
929c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
930c6fd2807SJeff Garzik  */
9317dc951aeSTejun Heo unsigned long ata_xfer_mode2mask(u8 xfer_mode)
932c6fd2807SJeff Garzik {
933c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
934c6fd2807SJeff Garzik 
935c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
936c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
93770cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
93870cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
939c6fd2807SJeff Garzik 	return 0;
940c6fd2807SJeff Garzik }
941c6fd2807SJeff Garzik 
942c6fd2807SJeff Garzik /**
943c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
944c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
945c6fd2807SJeff Garzik  *
946c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
947c6fd2807SJeff Garzik  *
948c6fd2807SJeff Garzik  *	LOCKING:
949c6fd2807SJeff Garzik  *	None.
950c6fd2807SJeff Garzik  *
951c6fd2807SJeff Garzik  *	RETURNS:
952c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
953c6fd2807SJeff Garzik  */
9547dc951aeSTejun Heo int ata_xfer_mode2shift(unsigned long xfer_mode)
955c6fd2807SJeff Garzik {
956c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
957c6fd2807SJeff Garzik 
958c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
959c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
960c6fd2807SJeff Garzik 			return ent->shift;
961c6fd2807SJeff Garzik 	return -1;
962c6fd2807SJeff Garzik }
963c6fd2807SJeff Garzik 
964c6fd2807SJeff Garzik /**
965c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
966c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
967c6fd2807SJeff Garzik  *
968c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
969c6fd2807SJeff Garzik  *	(highest bit in @modemask).
970c6fd2807SJeff Garzik  *
971c6fd2807SJeff Garzik  *	LOCKING:
972c6fd2807SJeff Garzik  *	None.
973c6fd2807SJeff Garzik  *
974c6fd2807SJeff Garzik  *	RETURNS:
975c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
976c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
977c6fd2807SJeff Garzik  */
9787dc951aeSTejun Heo const char *ata_mode_string(unsigned long xfer_mask)
979c6fd2807SJeff Garzik {
980c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
981c6fd2807SJeff Garzik 		"PIO0",
982c6fd2807SJeff Garzik 		"PIO1",
983c6fd2807SJeff Garzik 		"PIO2",
984c6fd2807SJeff Garzik 		"PIO3",
985c6fd2807SJeff Garzik 		"PIO4",
986b352e57dSAlan Cox 		"PIO5",
987b352e57dSAlan Cox 		"PIO6",
988c6fd2807SJeff Garzik 		"MWDMA0",
989c6fd2807SJeff Garzik 		"MWDMA1",
990c6fd2807SJeff Garzik 		"MWDMA2",
991b352e57dSAlan Cox 		"MWDMA3",
992b352e57dSAlan Cox 		"MWDMA4",
993c6fd2807SJeff Garzik 		"UDMA/16",
994c6fd2807SJeff Garzik 		"UDMA/25",
995c6fd2807SJeff Garzik 		"UDMA/33",
996c6fd2807SJeff Garzik 		"UDMA/44",
997c6fd2807SJeff Garzik 		"UDMA/66",
998c6fd2807SJeff Garzik 		"UDMA/100",
999c6fd2807SJeff Garzik 		"UDMA/133",
1000c6fd2807SJeff Garzik 		"UDMA7",
1001c6fd2807SJeff Garzik 	};
1002c6fd2807SJeff Garzik 	int highbit;
1003c6fd2807SJeff Garzik 
1004c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
1005c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1006c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
1007c6fd2807SJeff Garzik 	return "<n/a>";
1008c6fd2807SJeff Garzik }
1009c6fd2807SJeff Garzik 
1010c6fd2807SJeff Garzik static const char *sata_spd_string(unsigned int spd)
1011c6fd2807SJeff Garzik {
1012c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
1013c6fd2807SJeff Garzik 		"1.5 Gbps",
1014c6fd2807SJeff Garzik 		"3.0 Gbps",
10158522ee25SShane Huang 		"6.0 Gbps",
1016c6fd2807SJeff Garzik 	};
1017c6fd2807SJeff Garzik 
1018c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1019c6fd2807SJeff Garzik 		return "<unknown>";
1020c6fd2807SJeff Garzik 	return spd_str[spd - 1];
1021c6fd2807SJeff Garzik }
1022c6fd2807SJeff Garzik 
1023ca77329fSKristen Carlson Accardi static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
1024ca77329fSKristen Carlson Accardi {
1025ca77329fSKristen Carlson Accardi 	struct ata_link *link = dev->link;
1026ca77329fSKristen Carlson Accardi 	struct ata_port *ap = link->ap;
1027ca77329fSKristen Carlson Accardi 	u32 scontrol;
1028ca77329fSKristen Carlson Accardi 	unsigned int err_mask;
1029ca77329fSKristen Carlson Accardi 	int rc;
1030ca77329fSKristen Carlson Accardi 
1031ca77329fSKristen Carlson Accardi 	/*
1032ca77329fSKristen Carlson Accardi 	 * disallow DIPM for drivers which haven't set
1033ca77329fSKristen Carlson Accardi 	 * ATA_FLAG_IPM.  This is because when DIPM is enabled,
1034ca77329fSKristen Carlson Accardi 	 * phy ready will be set in the interrupt status on
1035ca77329fSKristen Carlson Accardi 	 * state changes, which will cause some drivers to
1036ca77329fSKristen Carlson Accardi 	 * think there are errors - additionally drivers will
1037ca77329fSKristen Carlson Accardi 	 * need to disable hot plug.
1038ca77329fSKristen Carlson Accardi 	 */
1039ca77329fSKristen Carlson Accardi 	if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
1040ca77329fSKristen Carlson Accardi 		ap->pm_policy = NOT_AVAILABLE;
1041ca77329fSKristen Carlson Accardi 		return -EINVAL;
1042ca77329fSKristen Carlson Accardi 	}
1043ca77329fSKristen Carlson Accardi 
1044ca77329fSKristen Carlson Accardi 	/*
1045ca77329fSKristen Carlson Accardi 	 * For DIPM, we will only enable it for the
1046ca77329fSKristen Carlson Accardi 	 * min_power setting.
1047ca77329fSKristen Carlson Accardi 	 *
1048ca77329fSKristen Carlson Accardi 	 * Why?  Because Disks are too stupid to know that
1049ca77329fSKristen Carlson Accardi 	 * If the host rejects a request to go to SLUMBER
1050ca77329fSKristen Carlson Accardi 	 * they should retry at PARTIAL, and instead it
1051ca77329fSKristen Carlson Accardi 	 * just would give up.  So, for medium_power to
1052ca77329fSKristen Carlson Accardi 	 * work at all, we need to only allow HIPM.
1053ca77329fSKristen Carlson Accardi 	 */
1054ca77329fSKristen Carlson Accardi 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
1055ca77329fSKristen Carlson Accardi 	if (rc)
1056ca77329fSKristen Carlson Accardi 		return rc;
1057ca77329fSKristen Carlson Accardi 
1058ca77329fSKristen Carlson Accardi 	switch (policy) {
1059ca77329fSKristen Carlson Accardi 	case MIN_POWER:
1060ca77329fSKristen Carlson Accardi 		/* no restrictions on IPM transitions */
1061ca77329fSKristen Carlson Accardi 		scontrol &= ~(0x3 << 8);
1062ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1063ca77329fSKristen Carlson Accardi 		if (rc)
1064ca77329fSKristen Carlson Accardi 			return rc;
1065ca77329fSKristen Carlson Accardi 
1066ca77329fSKristen Carlson Accardi 		/* enable DIPM */
1067ca77329fSKristen Carlson Accardi 		if (dev->flags & ATA_DFLAG_DIPM)
1068ca77329fSKristen Carlson Accardi 			err_mask = ata_dev_set_feature(dev,
1069ca77329fSKristen Carlson Accardi 					SETFEATURES_SATA_ENABLE, SATA_DIPM);
1070ca77329fSKristen Carlson Accardi 		break;
1071ca77329fSKristen Carlson Accardi 	case MEDIUM_POWER:
1072ca77329fSKristen Carlson Accardi 		/* allow IPM to PARTIAL */
1073ca77329fSKristen Carlson Accardi 		scontrol &= ~(0x1 << 8);
1074ca77329fSKristen Carlson Accardi 		scontrol |= (0x2 << 8);
1075ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1076ca77329fSKristen Carlson Accardi 		if (rc)
1077ca77329fSKristen Carlson Accardi 			return rc;
1078ca77329fSKristen Carlson Accardi 
1079f5456b63SKristen Carlson Accardi 		/*
1080f5456b63SKristen Carlson Accardi 		 * we don't have to disable DIPM since IPM flags
1081f5456b63SKristen Carlson Accardi 		 * disallow transitions to SLUMBER, which effectively
1082f5456b63SKristen Carlson Accardi 		 * disable DIPM if it does not support PARTIAL
1083f5456b63SKristen Carlson Accardi 		 */
1084ca77329fSKristen Carlson Accardi 		break;
1085ca77329fSKristen Carlson Accardi 	case NOT_AVAILABLE:
1086ca77329fSKristen Carlson Accardi 	case MAX_PERFORMANCE:
1087ca77329fSKristen Carlson Accardi 		/* disable all IPM transitions */
1088ca77329fSKristen Carlson Accardi 		scontrol |= (0x3 << 8);
1089ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1090ca77329fSKristen Carlson Accardi 		if (rc)
1091ca77329fSKristen Carlson Accardi 			return rc;
1092ca77329fSKristen Carlson Accardi 
1093f5456b63SKristen Carlson Accardi 		/*
1094f5456b63SKristen Carlson Accardi 		 * we don't have to disable DIPM since IPM flags
1095f5456b63SKristen Carlson Accardi 		 * disallow all transitions which effectively
1096f5456b63SKristen Carlson Accardi 		 * disable DIPM anyway.
1097f5456b63SKristen Carlson Accardi 		 */
1098ca77329fSKristen Carlson Accardi 		break;
1099ca77329fSKristen Carlson Accardi 	}
1100ca77329fSKristen Carlson Accardi 
1101ca77329fSKristen Carlson Accardi 	/* FIXME: handle SET FEATURES failure */
1102ca77329fSKristen Carlson Accardi 	(void) err_mask;
1103ca77329fSKristen Carlson Accardi 
1104ca77329fSKristen Carlson Accardi 	return 0;
1105ca77329fSKristen Carlson Accardi }
1106ca77329fSKristen Carlson Accardi 
1107ca77329fSKristen Carlson Accardi /**
1108ca77329fSKristen Carlson Accardi  *	ata_dev_enable_pm - enable SATA interface power management
110948166fd9SStephen Hemminger  *	@dev:  device to enable power management
111048166fd9SStephen Hemminger  *	@policy: the link power management policy
1111ca77329fSKristen Carlson Accardi  *
1112ca77329fSKristen Carlson Accardi  *	Enable SATA Interface power management.  This will enable
1113ca77329fSKristen Carlson Accardi  *	Device Interface Power Management (DIPM) for min_power
1114ca77329fSKristen Carlson Accardi  * 	policy, and then call driver specific callbacks for
1115ca77329fSKristen Carlson Accardi  *	enabling Host Initiated Power management.
1116ca77329fSKristen Carlson Accardi  *
1117ca77329fSKristen Carlson Accardi  *	Locking: Caller.
1118ca77329fSKristen Carlson Accardi  *	Returns: -EINVAL if IPM is not supported, 0 otherwise.
1119ca77329fSKristen Carlson Accardi  */
1120ca77329fSKristen Carlson Accardi void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
1121ca77329fSKristen Carlson Accardi {
1122ca77329fSKristen Carlson Accardi 	int rc = 0;
1123ca77329fSKristen Carlson Accardi 	struct ata_port *ap = dev->link->ap;
1124ca77329fSKristen Carlson Accardi 
1125ca77329fSKristen Carlson Accardi 	/* set HIPM first, then DIPM */
1126ca77329fSKristen Carlson Accardi 	if (ap->ops->enable_pm)
1127ca77329fSKristen Carlson Accardi 		rc = ap->ops->enable_pm(ap, policy);
1128ca77329fSKristen Carlson Accardi 	if (rc)
1129ca77329fSKristen Carlson Accardi 		goto enable_pm_out;
1130ca77329fSKristen Carlson Accardi 	rc = ata_dev_set_dipm(dev, policy);
1131ca77329fSKristen Carlson Accardi 
1132ca77329fSKristen Carlson Accardi enable_pm_out:
1133ca77329fSKristen Carlson Accardi 	if (rc)
1134ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
1135ca77329fSKristen Carlson Accardi 	else
1136ca77329fSKristen Carlson Accardi 		ap->pm_policy = policy;
1137ca77329fSKristen Carlson Accardi 	return /* rc */;	/* hopefully we can use 'rc' eventually */
1138ca77329fSKristen Carlson Accardi }
1139ca77329fSKristen Carlson Accardi 
11401992a5edSStephen Rothwell #ifdef CONFIG_PM
1141ca77329fSKristen Carlson Accardi /**
1142ca77329fSKristen Carlson Accardi  *	ata_dev_disable_pm - disable SATA interface power management
114348166fd9SStephen Hemminger  *	@dev: device to disable power management
1144ca77329fSKristen Carlson Accardi  *
1145ca77329fSKristen Carlson Accardi  *	Disable SATA Interface power management.  This will disable
1146ca77329fSKristen Carlson Accardi  *	Device Interface Power Management (DIPM) without changing
1147ca77329fSKristen Carlson Accardi  * 	policy,  call driver specific callbacks for disabling Host
1148ca77329fSKristen Carlson Accardi  * 	Initiated Power management.
1149ca77329fSKristen Carlson Accardi  *
1150ca77329fSKristen Carlson Accardi  *	Locking: Caller.
1151ca77329fSKristen Carlson Accardi  *	Returns: void
1152ca77329fSKristen Carlson Accardi  */
1153ca77329fSKristen Carlson Accardi static void ata_dev_disable_pm(struct ata_device *dev)
1154ca77329fSKristen Carlson Accardi {
1155ca77329fSKristen Carlson Accardi 	struct ata_port *ap = dev->link->ap;
1156ca77329fSKristen Carlson Accardi 
1157ca77329fSKristen Carlson Accardi 	ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1158ca77329fSKristen Carlson Accardi 	if (ap->ops->disable_pm)
1159ca77329fSKristen Carlson Accardi 		ap->ops->disable_pm(ap);
1160ca77329fSKristen Carlson Accardi }
11611992a5edSStephen Rothwell #endif	/* CONFIG_PM */
1162ca77329fSKristen Carlson Accardi 
1163ca77329fSKristen Carlson Accardi void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1164ca77329fSKristen Carlson Accardi {
1165ca77329fSKristen Carlson Accardi 	ap->pm_policy = policy;
11663ec25ebdSTejun Heo 	ap->link.eh_info.action |= ATA_EH_LPM;
1167ca77329fSKristen Carlson Accardi 	ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1168ca77329fSKristen Carlson Accardi 	ata_port_schedule_eh(ap);
1169ca77329fSKristen Carlson Accardi }
1170ca77329fSKristen Carlson Accardi 
11711992a5edSStephen Rothwell #ifdef CONFIG_PM
1172ca77329fSKristen Carlson Accardi static void ata_lpm_enable(struct ata_host *host)
1173ca77329fSKristen Carlson Accardi {
1174ca77329fSKristen Carlson Accardi 	struct ata_link *link;
1175ca77329fSKristen Carlson Accardi 	struct ata_port *ap;
1176ca77329fSKristen Carlson Accardi 	struct ata_device *dev;
1177ca77329fSKristen Carlson Accardi 	int i;
1178ca77329fSKristen Carlson Accardi 
1179ca77329fSKristen Carlson Accardi 	for (i = 0; i < host->n_ports; i++) {
1180ca77329fSKristen Carlson Accardi 		ap = host->ports[i];
11811eca4365STejun Heo 		ata_for_each_link(link, ap, EDGE) {
11821eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL)
1183ca77329fSKristen Carlson Accardi 				ata_dev_disable_pm(dev);
1184ca77329fSKristen Carlson Accardi 		}
1185ca77329fSKristen Carlson Accardi 	}
1186ca77329fSKristen Carlson Accardi }
1187ca77329fSKristen Carlson Accardi 
1188ca77329fSKristen Carlson Accardi static void ata_lpm_disable(struct ata_host *host)
1189ca77329fSKristen Carlson Accardi {
1190ca77329fSKristen Carlson Accardi 	int i;
1191ca77329fSKristen Carlson Accardi 
1192ca77329fSKristen Carlson Accardi 	for (i = 0; i < host->n_ports; i++) {
1193ca77329fSKristen Carlson Accardi 		struct ata_port *ap = host->ports[i];
1194ca77329fSKristen Carlson Accardi 		ata_lpm_schedule(ap, ap->pm_policy);
1195ca77329fSKristen Carlson Accardi 	}
1196ca77329fSKristen Carlson Accardi }
11971992a5edSStephen Rothwell #endif	/* CONFIG_PM */
1198ca77329fSKristen Carlson Accardi 
1199c6fd2807SJeff Garzik /**
1200c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
1201c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
1202c6fd2807SJeff Garzik  *
1203c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
1204c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
1205c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
1206c6fd2807SJeff Garzik  *
1207c6fd2807SJeff Garzik  *	LOCKING:
1208c6fd2807SJeff Garzik  *	None.
1209c6fd2807SJeff Garzik  *
1210c6fd2807SJeff Garzik  *	RETURNS:
1211633273a3STejun Heo  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1212633273a3STejun Heo  *	%ATA_DEV_UNKNOWN the event of failure.
1213c6fd2807SJeff Garzik  */
1214c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1215c6fd2807SJeff Garzik {
1216c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1217c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1218c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1219633273a3STejun Heo 	 *
1220633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1221633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1222633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1223633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1224633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1225633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1226633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1227633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1228633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1229633273a3STejun Heo 	 * SerialATA.
1230633273a3STejun Heo 	 *
1231633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1232633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1233c6fd2807SJeff Garzik 	 */
1234633273a3STejun Heo 	if ((tf->lbam == 0) && (tf->lbah == 0)) {
1235c6fd2807SJeff Garzik 		DPRINTK("found ATA device by sig\n");
1236c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1237c6fd2807SJeff Garzik 	}
1238c6fd2807SJeff Garzik 
1239633273a3STejun Heo 	if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1240c6fd2807SJeff Garzik 		DPRINTK("found ATAPI device by sig\n");
1241c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1242c6fd2807SJeff Garzik 	}
1243c6fd2807SJeff Garzik 
1244633273a3STejun Heo 	if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1245633273a3STejun Heo 		DPRINTK("found PMP device by sig\n");
1246633273a3STejun Heo 		return ATA_DEV_PMP;
1247633273a3STejun Heo 	}
1248633273a3STejun Heo 
1249633273a3STejun Heo 	if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
12502dcb407eSJeff Garzik 		printk(KERN_INFO "ata: SEMB device ignored\n");
1251633273a3STejun Heo 		return ATA_DEV_SEMB_UNSUP; /* not yet */
1252633273a3STejun Heo 	}
1253633273a3STejun Heo 
1254c6fd2807SJeff Garzik 	DPRINTK("unknown device\n");
1255c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1256c6fd2807SJeff Garzik }
1257c6fd2807SJeff Garzik 
1258c6fd2807SJeff Garzik /**
1259c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1260c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1261c6fd2807SJeff Garzik  *	@s: string into which data is output
1262c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1263c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1264c6fd2807SJeff Garzik  *
1265c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1266c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1267c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1268c6fd2807SJeff Garzik  *
1269c6fd2807SJeff Garzik  *	LOCKING:
1270c6fd2807SJeff Garzik  *	caller.
1271c6fd2807SJeff Garzik  */
1272c6fd2807SJeff Garzik 
1273c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1274c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1275c6fd2807SJeff Garzik {
1276c6fd2807SJeff Garzik 	unsigned int c;
1277c6fd2807SJeff Garzik 
1278963e4975SAlan Cox 	BUG_ON(len & 1);
1279963e4975SAlan Cox 
1280c6fd2807SJeff Garzik 	while (len > 0) {
1281c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1282c6fd2807SJeff Garzik 		*s = c;
1283c6fd2807SJeff Garzik 		s++;
1284c6fd2807SJeff Garzik 
1285c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1286c6fd2807SJeff Garzik 		*s = c;
1287c6fd2807SJeff Garzik 		s++;
1288c6fd2807SJeff Garzik 
1289c6fd2807SJeff Garzik 		ofs++;
1290c6fd2807SJeff Garzik 		len -= 2;
1291c6fd2807SJeff Garzik 	}
1292c6fd2807SJeff Garzik }
1293c6fd2807SJeff Garzik 
1294c6fd2807SJeff Garzik /**
1295c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1296c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1297c6fd2807SJeff Garzik  *	@s: string into which data is output
1298c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1299c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1300c6fd2807SJeff Garzik  *
1301c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1302c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1303c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1304c6fd2807SJeff Garzik  *
1305c6fd2807SJeff Garzik  *	LOCKING:
1306c6fd2807SJeff Garzik  *	caller.
1307c6fd2807SJeff Garzik  */
1308c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1309c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1310c6fd2807SJeff Garzik {
1311c6fd2807SJeff Garzik 	unsigned char *p;
1312c6fd2807SJeff Garzik 
1313c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1314c6fd2807SJeff Garzik 
1315c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1316c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1317c6fd2807SJeff Garzik 		p--;
1318c6fd2807SJeff Garzik 	*p = '\0';
1319c6fd2807SJeff Garzik }
1320c6fd2807SJeff Garzik 
1321db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1322db6f8759STejun Heo {
1323db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1324db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1325968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1326db6f8759STejun Heo 		else
1327968e594aSRobert Hancock 			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1328db6f8759STejun Heo 	} else {
1329db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1330968e594aSRobert Hancock 			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1331968e594aSRobert Hancock 			       id[ATA_ID_CUR_SECTORS];
1332db6f8759STejun Heo 		else
1333968e594aSRobert Hancock 			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1334968e594aSRobert Hancock 			       id[ATA_ID_SECTORS];
1335db6f8759STejun Heo 	}
1336db6f8759STejun Heo }
1337db6f8759STejun Heo 
1338a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
13391e999736SAlan Cox {
13401e999736SAlan Cox 	u64 sectors = 0;
13411e999736SAlan Cox 
13421e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
13431e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1344ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
13451e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
13461e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
13471e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
13481e999736SAlan Cox 
1349a5987e0aSTejun Heo 	return sectors;
13501e999736SAlan Cox }
13511e999736SAlan Cox 
1352a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
13531e999736SAlan Cox {
13541e999736SAlan Cox 	u64 sectors = 0;
13551e999736SAlan Cox 
13561e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
13571e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
13581e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
13591e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
13601e999736SAlan Cox 
1361a5987e0aSTejun Heo 	return sectors;
13621e999736SAlan Cox }
13631e999736SAlan Cox 
13641e999736SAlan Cox /**
1365c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1366c728a914STejun Heo  *	@dev: target device
1367c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
13681e999736SAlan Cox  *
1369c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1370c728a914STejun Heo  *	question.
1371c728a914STejun Heo  *
1372c728a914STejun Heo  *	RETURNS:
1373c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1374c728a914STejun Heo  *	-EIO on other errors.
13751e999736SAlan Cox  */
1376c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
13771e999736SAlan Cox {
1378c728a914STejun Heo 	unsigned int err_mask;
13791e999736SAlan Cox 	struct ata_taskfile tf;
1380c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
13811e999736SAlan Cox 
13821e999736SAlan Cox 	ata_tf_init(dev, &tf);
13831e999736SAlan Cox 
1384c728a914STejun Heo 	/* always clear all address registers */
13851e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1386c728a914STejun Heo 
1387c728a914STejun Heo 	if (lba48) {
1388c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1389c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1390c728a914STejun Heo 	} else
1391c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1392c728a914STejun Heo 
13931e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1394c728a914STejun Heo 	tf.device |= ATA_LBA;
13951e999736SAlan Cox 
13962b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1397c728a914STejun Heo 	if (err_mask) {
1398c728a914STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1399c728a914STejun Heo 			       "max address (err_mask=0x%x)\n", err_mask);
1400c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1401c728a914STejun Heo 			return -EACCES;
1402c728a914STejun Heo 		return -EIO;
1403c728a914STejun Heo 	}
1404c728a914STejun Heo 
1405c728a914STejun Heo 	if (lba48)
1406a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1407c728a914STejun Heo 	else
1408a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
140993328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
141093328e11SAlan Cox 		(*max_sectors)--;
14111e999736SAlan Cox 	return 0;
14121e999736SAlan Cox }
14131e999736SAlan Cox 
14141e999736SAlan Cox /**
1415c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1416c728a914STejun Heo  *	@dev: target device
14176b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
14181e999736SAlan Cox  *
1419c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1420c728a914STejun Heo  *
1421c728a914STejun Heo  *	RETURNS:
1422c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1423c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1424c728a914STejun Heo  *	errors.
14251e999736SAlan Cox  */
142605027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
14271e999736SAlan Cox {
1428c728a914STejun Heo 	unsigned int err_mask;
14291e999736SAlan Cox 	struct ata_taskfile tf;
1430c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
14311e999736SAlan Cox 
14321e999736SAlan Cox 	new_sectors--;
14331e999736SAlan Cox 
14341e999736SAlan Cox 	ata_tf_init(dev, &tf);
14351e999736SAlan Cox 
1436c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
14371e999736SAlan Cox 
1438c728a914STejun Heo 	if (lba48) {
1439c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1440c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
14411e999736SAlan Cox 
14421e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
14431e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
14441e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
14451e582ba4STejun Heo 	} else {
14461e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1447c728a914STejun Heo 
14481e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
14491e582ba4STejun Heo 	}
14501e582ba4STejun Heo 
14511e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1452c728a914STejun Heo 	tf.device |= ATA_LBA;
14531e999736SAlan Cox 
14541e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
14551e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
14561e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
14571e999736SAlan Cox 
14582b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1459c728a914STejun Heo 	if (err_mask) {
1460c728a914STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "failed to set "
1461c728a914STejun Heo 			       "max address (err_mask=0x%x)\n", err_mask);
1462c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1463c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1464c728a914STejun Heo 			return -EACCES;
1465c728a914STejun Heo 		return -EIO;
1466c728a914STejun Heo 	}
1467c728a914STejun Heo 
14681e999736SAlan Cox 	return 0;
14691e999736SAlan Cox }
14701e999736SAlan Cox 
14711e999736SAlan Cox /**
14721e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
14731e999736SAlan Cox  *	@dev: Device to resize
14741e999736SAlan Cox  *
14751e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
14761e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
14771e999736SAlan Cox  *	the drive has the HPA feature set enabled.
147805027adcSTejun Heo  *
147905027adcSTejun Heo  *	RETURNS:
148005027adcSTejun Heo  *	0 on success, -errno on failure.
14811e999736SAlan Cox  */
148205027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
14831e999736SAlan Cox {
148405027adcSTejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
148505027adcSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
148605027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
148705027adcSTejun Heo 	u64 native_sectors;
1488c728a914STejun Heo 	int rc;
14891e999736SAlan Cox 
149005027adcSTejun Heo 	/* do we need to do it? */
149105027adcSTejun Heo 	if (dev->class != ATA_DEV_ATA ||
149205027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
149305027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1494c728a914STejun Heo 		return 0;
14951e999736SAlan Cox 
149605027adcSTejun Heo 	/* read native max address */
149705027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
149805027adcSTejun Heo 	if (rc) {
1499dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1500dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
150105027adcSTejun Heo 		 */
1502dda7aba1STejun Heo 		if (rc == -EACCES || !ata_ignore_hpa) {
150305027adcSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1504dda7aba1STejun Heo 				       "broken, skipping HPA handling\n");
150505027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
150605027adcSTejun Heo 
150705027adcSTejun Heo 			/* we can continue if device aborted the command */
150805027adcSTejun Heo 			if (rc == -EACCES)
150905027adcSTejun Heo 				rc = 0;
151005027adcSTejun Heo 		}
151105027adcSTejun Heo 
151205027adcSTejun Heo 		return rc;
151305027adcSTejun Heo 	}
151405027adcSTejun Heo 
151505027adcSTejun Heo 	/* nothing to do? */
151605027adcSTejun Heo 	if (native_sectors <= sectors || !ata_ignore_hpa) {
151705027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
151805027adcSTejun Heo 			return 0;
151905027adcSTejun Heo 
152005027adcSTejun Heo 		if (native_sectors > sectors)
15211e999736SAlan Cox 			ata_dev_printk(dev, KERN_INFO,
152205027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
152305027adcSTejun Heo 				(unsigned long long)sectors,
152405027adcSTejun Heo 				(unsigned long long)native_sectors);
152505027adcSTejun Heo 		else if (native_sectors < sectors)
152605027adcSTejun Heo 			ata_dev_printk(dev, KERN_WARNING,
152705027adcSTejun Heo 				"native sectors (%llu) is smaller than "
152805027adcSTejun Heo 				"sectors (%llu)\n",
152905027adcSTejun Heo 				(unsigned long long)native_sectors,
153005027adcSTejun Heo 				(unsigned long long)sectors);
153105027adcSTejun Heo 		return 0;
15321e999736SAlan Cox 	}
153337301a55STejun Heo 
153405027adcSTejun Heo 	/* let's unlock HPA */
153505027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
153605027adcSTejun Heo 	if (rc == -EACCES) {
153705027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
153805027adcSTejun Heo 		ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
153905027adcSTejun Heo 			       "(%llu -> %llu), skipping HPA handling\n",
154005027adcSTejun Heo 			       (unsigned long long)sectors,
154105027adcSTejun Heo 			       (unsigned long long)native_sectors);
154205027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
154305027adcSTejun Heo 		return 0;
154405027adcSTejun Heo 	} else if (rc)
154505027adcSTejun Heo 		return rc;
154605027adcSTejun Heo 
154705027adcSTejun Heo 	/* re-read IDENTIFY data */
154805027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
154905027adcSTejun Heo 	if (rc) {
155005027adcSTejun Heo 		ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
155105027adcSTejun Heo 			       "data after HPA resizing\n");
155205027adcSTejun Heo 		return rc;
155305027adcSTejun Heo 	}
155405027adcSTejun Heo 
155505027adcSTejun Heo 	if (print_info) {
155605027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
155705027adcSTejun Heo 		ata_dev_printk(dev, KERN_INFO,
155805027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
155905027adcSTejun Heo 			(unsigned long long)sectors,
156005027adcSTejun Heo 			(unsigned long long)new_sectors,
156105027adcSTejun Heo 			(unsigned long long)native_sectors);
156205027adcSTejun Heo 	}
156305027adcSTejun Heo 
156405027adcSTejun Heo 	return 0;
15651e999736SAlan Cox }
15661e999736SAlan Cox 
1567c6fd2807SJeff Garzik /**
1568c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
1569c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1570c6fd2807SJeff Garzik  *
1571c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1572c6fd2807SJeff Garzik  *	page.
1573c6fd2807SJeff Garzik  *
1574c6fd2807SJeff Garzik  *	LOCKING:
1575c6fd2807SJeff Garzik  *	caller.
1576c6fd2807SJeff Garzik  */
1577c6fd2807SJeff Garzik 
1578c6fd2807SJeff Garzik static inline void ata_dump_id(const u16 *id)
1579c6fd2807SJeff Garzik {
1580c6fd2807SJeff Garzik 	DPRINTK("49==0x%04x  "
1581c6fd2807SJeff Garzik 		"53==0x%04x  "
1582c6fd2807SJeff Garzik 		"63==0x%04x  "
1583c6fd2807SJeff Garzik 		"64==0x%04x  "
1584c6fd2807SJeff Garzik 		"75==0x%04x  \n",
1585c6fd2807SJeff Garzik 		id[49],
1586c6fd2807SJeff Garzik 		id[53],
1587c6fd2807SJeff Garzik 		id[63],
1588c6fd2807SJeff Garzik 		id[64],
1589c6fd2807SJeff Garzik 		id[75]);
1590c6fd2807SJeff Garzik 	DPRINTK("80==0x%04x  "
1591c6fd2807SJeff Garzik 		"81==0x%04x  "
1592c6fd2807SJeff Garzik 		"82==0x%04x  "
1593c6fd2807SJeff Garzik 		"83==0x%04x  "
1594c6fd2807SJeff Garzik 		"84==0x%04x  \n",
1595c6fd2807SJeff Garzik 		id[80],
1596c6fd2807SJeff Garzik 		id[81],
1597c6fd2807SJeff Garzik 		id[82],
1598c6fd2807SJeff Garzik 		id[83],
1599c6fd2807SJeff Garzik 		id[84]);
1600c6fd2807SJeff Garzik 	DPRINTK("88==0x%04x  "
1601c6fd2807SJeff Garzik 		"93==0x%04x\n",
1602c6fd2807SJeff Garzik 		id[88],
1603c6fd2807SJeff Garzik 		id[93]);
1604c6fd2807SJeff Garzik }
1605c6fd2807SJeff Garzik 
1606c6fd2807SJeff Garzik /**
1607c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1608c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1609c6fd2807SJeff Garzik  *
1610c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1611c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1612c6fd2807SJeff Garzik  *
1613c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1614c6fd2807SJeff Garzik  *
1615c6fd2807SJeff Garzik  *	LOCKING:
1616c6fd2807SJeff Garzik  *	None.
1617c6fd2807SJeff Garzik  *
1618c6fd2807SJeff Garzik  *	RETURNS:
1619c6fd2807SJeff Garzik  *	Computed xfermask
1620c6fd2807SJeff Garzik  */
16217dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1622c6fd2807SJeff Garzik {
16237dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1624c6fd2807SJeff Garzik 
1625c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1626c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1627c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1628c6fd2807SJeff Garzik 		pio_mask <<= 3;
1629c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1630c6fd2807SJeff Garzik 	} else {
1631c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1632c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1633c6fd2807SJeff Garzik 		 * a mask.
1634c6fd2807SJeff Garzik 		 */
16357a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
163646767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
163746767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
163846767aebSAlan Cox 		else
163946767aebSAlan Cox 			pio_mask = 1;
1640c6fd2807SJeff Garzik 
1641c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1642c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1643c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1644c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1645c6fd2807SJeff Garzik 		 * will get this right anyway
1646c6fd2807SJeff Garzik 		 */
1647c6fd2807SJeff Garzik 	}
1648c6fd2807SJeff Garzik 
1649c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1650c6fd2807SJeff Garzik 
1651b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1652b352e57dSAlan Cox 		/*
1653b352e57dSAlan Cox 		 *	Process compact flash extended modes
1654b352e57dSAlan Cox 		 */
1655b352e57dSAlan Cox 		int pio = id[163] & 0x7;
1656b352e57dSAlan Cox 		int dma = (id[163] >> 3) & 7;
1657b352e57dSAlan Cox 
1658b352e57dSAlan Cox 		if (pio)
1659b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1660b352e57dSAlan Cox 		if (pio > 1)
1661b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1662b352e57dSAlan Cox 		if (dma)
1663b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1664b352e57dSAlan Cox 		if (dma > 1)
1665b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1666b352e57dSAlan Cox 	}
1667b352e57dSAlan Cox 
1668c6fd2807SJeff Garzik 	udma_mask = 0;
1669c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1670c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1671c6fd2807SJeff Garzik 
1672c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1673c6fd2807SJeff Garzik }
1674c6fd2807SJeff Garzik 
1675c6fd2807SJeff Garzik /**
1676442eacc3SJeff Garzik  *	ata_pio_queue_task - Queue port_task
1677c6fd2807SJeff Garzik  *	@ap: The ata_port to queue port_task for
167865f27f38SDavid Howells  *	@data: data for @fn to use
1679341c2c95STejun Heo  *	@delay: delay time in msecs for workqueue function
1680c6fd2807SJeff Garzik  *
1681c6fd2807SJeff Garzik  *	Schedule @fn(@data) for execution after @delay jiffies using
1682c6fd2807SJeff Garzik  *	port_task.  There is one port_task per port and it's the
1683c6fd2807SJeff Garzik  *	user(low level driver)'s responsibility to make sure that only
1684c6fd2807SJeff Garzik  *	one task is active at any given time.
1685c6fd2807SJeff Garzik  *
1686c6fd2807SJeff Garzik  *	libata core layer takes care of synchronization between
1687442eacc3SJeff Garzik  *	port_task and EH.  ata_pio_queue_task() may be ignored for EH
1688c6fd2807SJeff Garzik  *	synchronization.
1689c6fd2807SJeff Garzik  *
1690c6fd2807SJeff Garzik  *	LOCKING:
1691c6fd2807SJeff Garzik  *	Inherited from caller.
1692c6fd2807SJeff Garzik  */
1693624d5c51STejun Heo void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
1694c6fd2807SJeff Garzik {
169565f27f38SDavid Howells 	ap->port_task_data = data;
1696c6fd2807SJeff Garzik 
169745a66c1cSOleg Nesterov 	/* may fail if ata_port_flush_task() in progress */
1698341c2c95STejun Heo 	queue_delayed_work(ata_wq, &ap->port_task, msecs_to_jiffies(delay));
1699c6fd2807SJeff Garzik }
1700c6fd2807SJeff Garzik 
1701c6fd2807SJeff Garzik /**
1702c6fd2807SJeff Garzik  *	ata_port_flush_task - Flush port_task
1703c6fd2807SJeff Garzik  *	@ap: The ata_port to flush port_task for
1704c6fd2807SJeff Garzik  *
1705c6fd2807SJeff Garzik  *	After this function completes, port_task is guranteed not to
1706c6fd2807SJeff Garzik  *	be running or scheduled.
1707c6fd2807SJeff Garzik  *
1708c6fd2807SJeff Garzik  *	LOCKING:
1709c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1710c6fd2807SJeff Garzik  */
1711c6fd2807SJeff Garzik void ata_port_flush_task(struct ata_port *ap)
1712c6fd2807SJeff Garzik {
1713c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1714c6fd2807SJeff Garzik 
171545a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->port_task);
1716c6fd2807SJeff Garzik 
1717c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
17187f5e4e8dSHarvey Harrison 		ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
1719c6fd2807SJeff Garzik }
1720c6fd2807SJeff Garzik 
17217102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1722c6fd2807SJeff Garzik {
1723c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1724c6fd2807SJeff Garzik 
1725c6fd2807SJeff Garzik 	complete(waiting);
1726c6fd2807SJeff Garzik }
1727c6fd2807SJeff Garzik 
1728c6fd2807SJeff Garzik /**
17292432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1730c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1731c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1732c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1733c6fd2807SJeff Garzik  *	@dma_dir: Data tranfer direction of the command
17345c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
17352432697bSTejun Heo  *	@n_elem: Number of sg entries
17362b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1737c6fd2807SJeff Garzik  *
1738c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1739c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1740c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1741c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1742c6fd2807SJeff Garzik  *	clean up after timeout.
1743c6fd2807SJeff Garzik  *
1744c6fd2807SJeff Garzik  *	LOCKING:
1745c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1746c6fd2807SJeff Garzik  *
1747c6fd2807SJeff Garzik  *	RETURNS:
1748c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1749c6fd2807SJeff Garzik  */
17502432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1751c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
175287260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
17532b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1754c6fd2807SJeff Garzik {
17559af5c9c9STejun Heo 	struct ata_link *link = dev->link;
17569af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1757c6fd2807SJeff Garzik 	u8 command = tf->command;
175887fbc5a0STejun Heo 	int auto_timeout = 0;
1759c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1760c6fd2807SJeff Garzik 	unsigned int tag, preempted_tag;
1761c6fd2807SJeff Garzik 	u32 preempted_sactive, preempted_qc_active;
1762da917d69STejun Heo 	int preempted_nr_active_links;
1763c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1764c6fd2807SJeff Garzik 	unsigned long flags;
1765c6fd2807SJeff Garzik 	unsigned int err_mask;
1766c6fd2807SJeff Garzik 	int rc;
1767c6fd2807SJeff Garzik 
1768c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1769c6fd2807SJeff Garzik 
1770c6fd2807SJeff Garzik 	/* no internal command while frozen */
1771c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1772c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1773c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1774c6fd2807SJeff Garzik 	}
1775c6fd2807SJeff Garzik 
1776c6fd2807SJeff Garzik 	/* initialize internal qc */
1777c6fd2807SJeff Garzik 
1778c6fd2807SJeff Garzik 	/* XXX: Tag 0 is used for drivers with legacy EH as some
1779c6fd2807SJeff Garzik 	 * drivers choke if any other tag is given.  This breaks
1780c6fd2807SJeff Garzik 	 * ata_tag_internal() test for those drivers.  Don't use new
1781c6fd2807SJeff Garzik 	 * EH stuff without converting to it.
1782c6fd2807SJeff Garzik 	 */
1783c6fd2807SJeff Garzik 	if (ap->ops->error_handler)
1784c6fd2807SJeff Garzik 		tag = ATA_TAG_INTERNAL;
1785c6fd2807SJeff Garzik 	else
1786c6fd2807SJeff Garzik 		tag = 0;
1787c6fd2807SJeff Garzik 
17888a8bc223STejun Heo 	if (test_and_set_bit(tag, &ap->qc_allocated))
17898a8bc223STejun Heo 		BUG();
1790c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1791c6fd2807SJeff Garzik 
1792c6fd2807SJeff Garzik 	qc->tag = tag;
1793c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1794c6fd2807SJeff Garzik 	qc->ap = ap;
1795c6fd2807SJeff Garzik 	qc->dev = dev;
1796c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1797c6fd2807SJeff Garzik 
17989af5c9c9STejun Heo 	preempted_tag = link->active_tag;
17999af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1800c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1801da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
18029af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
18039af5c9c9STejun Heo 	link->sactive = 0;
1804c6fd2807SJeff Garzik 	ap->qc_active = 0;
1805da917d69STejun Heo 	ap->nr_active_links = 0;
1806c6fd2807SJeff Garzik 
1807c6fd2807SJeff Garzik 	/* prepare & issue qc */
1808c6fd2807SJeff Garzik 	qc->tf = *tf;
1809c6fd2807SJeff Garzik 	if (cdb)
1810c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1811c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1812c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1813c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
18142432697bSTejun Heo 		unsigned int i, buflen = 0;
181587260216SJens Axboe 		struct scatterlist *sg;
18162432697bSTejun Heo 
181787260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
181887260216SJens Axboe 			buflen += sg->length;
18192432697bSTejun Heo 
182087260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
182149c80429SBrian King 		qc->nbytes = buflen;
1822c6fd2807SJeff Garzik 	}
1823c6fd2807SJeff Garzik 
1824c6fd2807SJeff Garzik 	qc->private_data = &wait;
1825c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1826c6fd2807SJeff Garzik 
1827c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1828c6fd2807SJeff Garzik 
1829c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1830c6fd2807SJeff Garzik 
183187fbc5a0STejun Heo 	if (!timeout) {
183287fbc5a0STejun Heo 		if (ata_probe_timeout)
1833341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
183487fbc5a0STejun Heo 		else {
183587fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
183687fbc5a0STejun Heo 			auto_timeout = 1;
183787fbc5a0STejun Heo 		}
183887fbc5a0STejun Heo 	}
18392b789108STejun Heo 
18402b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1841c6fd2807SJeff Garzik 
1842c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
1843c6fd2807SJeff Garzik 
1844c6fd2807SJeff Garzik 	if (!rc) {
1845c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1846c6fd2807SJeff Garzik 
1847c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1848c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1849c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1850c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1851c6fd2807SJeff Garzik 		 */
1852c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1853c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1854c6fd2807SJeff Garzik 
1855c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1856c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1857c6fd2807SJeff Garzik 			else
1858c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1859c6fd2807SJeff Garzik 
1860c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1861c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
1862c6fd2807SJeff Garzik 					"qc timeout (cmd 0x%x)\n", command);
1863c6fd2807SJeff Garzik 		}
1864c6fd2807SJeff Garzik 
1865c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1866c6fd2807SJeff Garzik 	}
1867c6fd2807SJeff Garzik 
1868c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1869c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1870c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1871c6fd2807SJeff Garzik 
1872a51d644aSTejun Heo 	/* perform minimal error analysis */
1873a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1874a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1875a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1876a51d644aSTejun Heo 
1877a51d644aSTejun Heo 		if (!qc->err_mask)
1878c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1879a51d644aSTejun Heo 
1880a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1881a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
1882c6fd2807SJeff Garzik 	}
1883c6fd2807SJeff Garzik 
1884c6fd2807SJeff Garzik 	/* finish up */
1885c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1886c6fd2807SJeff Garzik 
1887c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1888c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1889c6fd2807SJeff Garzik 
1890c6fd2807SJeff Garzik 	ata_qc_free(qc);
18919af5c9c9STejun Heo 	link->active_tag = preempted_tag;
18929af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1893c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1894da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1895c6fd2807SJeff Garzik 
1896c6fd2807SJeff Garzik 	/* XXX - Some LLDDs (sata_mv) disable port on command failure.
1897c6fd2807SJeff Garzik 	 * Until those drivers are fixed, we detect the condition
1898c6fd2807SJeff Garzik 	 * here, fail the command with AC_ERR_SYSTEM and reenable the
1899c6fd2807SJeff Garzik 	 * port.
1900c6fd2807SJeff Garzik 	 *
1901c6fd2807SJeff Garzik 	 * Note that this doesn't change any behavior as internal
1902c6fd2807SJeff Garzik 	 * command failure results in disabling the device in the
1903c6fd2807SJeff Garzik 	 * higher layer for LLDDs without new reset/EH callbacks.
1904c6fd2807SJeff Garzik 	 *
1905c6fd2807SJeff Garzik 	 * Kill the following code as soon as those drivers are fixed.
1906c6fd2807SJeff Garzik 	 */
1907c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_DISABLED) {
1908c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1909c6fd2807SJeff Garzik 		ata_port_probe(ap);
1910c6fd2807SJeff Garzik 	}
1911c6fd2807SJeff Garzik 
1912c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1913c6fd2807SJeff Garzik 
191487fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
191587fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
191687fbc5a0STejun Heo 
1917c6fd2807SJeff Garzik 	return err_mask;
1918c6fd2807SJeff Garzik }
1919c6fd2807SJeff Garzik 
1920c6fd2807SJeff Garzik /**
192133480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
19222432697bSTejun Heo  *	@dev: Device to which the command is sent
19232432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
19242432697bSTejun Heo  *	@cdb: CDB for packet command
19252432697bSTejun Heo  *	@dma_dir: Data tranfer direction of the command
19262432697bSTejun Heo  *	@buf: Data buffer of the command
19272432697bSTejun Heo  *	@buflen: Length of data buffer
19282b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
19292432697bSTejun Heo  *
19302432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
19312432697bSTejun Heo  *	buffer instead of sg list.
19322432697bSTejun Heo  *
19332432697bSTejun Heo  *	LOCKING:
19342432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
19352432697bSTejun Heo  *
19362432697bSTejun Heo  *	RETURNS:
19372432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
19382432697bSTejun Heo  */
19392432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
19402432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
19412b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
19422b789108STejun Heo 			   unsigned long timeout)
19432432697bSTejun Heo {
194433480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
194533480a0eSTejun Heo 	unsigned int n_elem = 0;
19462432697bSTejun Heo 
194733480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
194833480a0eSTejun Heo 		WARN_ON(!buf);
19492432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
195033480a0eSTejun Heo 		psg = &sg;
195133480a0eSTejun Heo 		n_elem++;
195233480a0eSTejun Heo 	}
19532432697bSTejun Heo 
19542b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
19552b789108STejun Heo 				    timeout);
19562432697bSTejun Heo }
19572432697bSTejun Heo 
19582432697bSTejun Heo /**
1959c6fd2807SJeff Garzik  *	ata_do_simple_cmd - execute simple internal command
1960c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1961c6fd2807SJeff Garzik  *	@cmd: Opcode to execute
1962c6fd2807SJeff Garzik  *
1963c6fd2807SJeff Garzik  *	Execute a 'simple' command, that only consists of the opcode
1964c6fd2807SJeff Garzik  *	'cmd' itself, without filling any other registers
1965c6fd2807SJeff Garzik  *
1966c6fd2807SJeff Garzik  *	LOCKING:
1967c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1968c6fd2807SJeff Garzik  *
1969c6fd2807SJeff Garzik  *	RETURNS:
1970c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1971c6fd2807SJeff Garzik  */
1972c6fd2807SJeff Garzik unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1973c6fd2807SJeff Garzik {
1974c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1975c6fd2807SJeff Garzik 
1976c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1977c6fd2807SJeff Garzik 
1978c6fd2807SJeff Garzik 	tf.command = cmd;
1979c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_DEVICE;
1980c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
1981c6fd2807SJeff Garzik 
19822b789108STejun Heo 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1983c6fd2807SJeff Garzik }
1984c6fd2807SJeff Garzik 
1985c6fd2807SJeff Garzik /**
1986c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1987c6fd2807SJeff Garzik  *	@adev: ATA device
1988c6fd2807SJeff Garzik  *
1989c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1990c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1991c6fd2807SJeff Garzik  */
1992c6fd2807SJeff Garzik 
1993c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1994c6fd2807SJeff Garzik {
1995432729f0SAlan Cox 	/* Controller doesn't support  IORDY. Probably a pointless check
1996432729f0SAlan Cox 	   as the caller should know this */
19979af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1998c6fd2807SJeff Garzik 		return 0;
19995c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
20005c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
20015c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
20025c18c4d2SDavid Daney 		return 0;
2003432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
2004432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
2005c6fd2807SJeff Garzik 		return 1;
2006432729f0SAlan Cox 	/* We turn it on when possible */
2007432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
2008432729f0SAlan Cox 		return 1;
2009432729f0SAlan Cox 	return 0;
2010432729f0SAlan Cox }
2011c6fd2807SJeff Garzik 
2012432729f0SAlan Cox /**
2013432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
2014432729f0SAlan Cox  *	@adev: ATA device
2015432729f0SAlan Cox  *
2016432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
2017432729f0SAlan Cox  *	-1 if no iordy mode is available.
2018432729f0SAlan Cox  */
2019432729f0SAlan Cox 
2020432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
2021432729f0SAlan Cox {
2022c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
2023c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
2024432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
2025c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
2026c6fd2807SJeff Garzik 		if (pio) {
2027c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
2028c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
2029432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
2030432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
2031c6fd2807SJeff Garzik 		}
2032c6fd2807SJeff Garzik 	}
2033432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
2034c6fd2807SJeff Garzik }
2035c6fd2807SJeff Garzik 
2036c6fd2807SJeff Garzik /**
2037963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
2038963e4975SAlan Cox  *	@dev: device
2039963e4975SAlan Cox  *	@tf: proposed taskfile
2040963e4975SAlan Cox  *	@id: data buffer
2041963e4975SAlan Cox  *
2042963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
2043963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
2044963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
2045963e4975SAlan Cox  */
2046963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
2047963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
2048963e4975SAlan Cox {
2049963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
2050963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
2051963e4975SAlan Cox }
2052963e4975SAlan Cox 
2053963e4975SAlan Cox /**
2054c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
2055c6fd2807SJeff Garzik  *	@dev: target device
2056c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
2057bff04647STejun Heo  *	@flags: ATA_READID_* flags
2058c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
2059c6fd2807SJeff Garzik  *
2060c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
2061c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
2062c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
2063c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
2064c6fd2807SJeff Garzik  *
206550a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
206650a99018SAlan Cox  *	now we abort if we hit that case.
206750a99018SAlan Cox  *
2068c6fd2807SJeff Garzik  *	LOCKING:
2069c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2070c6fd2807SJeff Garzik  *
2071c6fd2807SJeff Garzik  *	RETURNS:
2072c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
2073c6fd2807SJeff Garzik  */
2074c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
2075bff04647STejun Heo 		    unsigned int flags, u16 *id)
2076c6fd2807SJeff Garzik {
20779af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2078c6fd2807SJeff Garzik 	unsigned int class = *p_class;
2079c6fd2807SJeff Garzik 	struct ata_taskfile tf;
2080c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
2081c6fd2807SJeff Garzik 	const char *reason;
208254936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
2083c6fd2807SJeff Garzik 	int rc;
2084c6fd2807SJeff Garzik 
2085c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
20867f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2087c6fd2807SJeff Garzik 
2088c6fd2807SJeff Garzik retry:
2089c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
2090c6fd2807SJeff Garzik 
2091c6fd2807SJeff Garzik 	switch (class) {
2092c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
2093c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
2094c6fd2807SJeff Garzik 		break;
2095c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
2096c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
2097c6fd2807SJeff Garzik 		break;
2098c6fd2807SJeff Garzik 	default:
2099c6fd2807SJeff Garzik 		rc = -ENODEV;
2100c6fd2807SJeff Garzik 		reason = "unsupported class";
2101c6fd2807SJeff Garzik 		goto err_out;
2102c6fd2807SJeff Garzik 	}
2103c6fd2807SJeff Garzik 
2104c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
210581afe893STejun Heo 
210681afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
210781afe893STejun Heo 	 * sure those are properly initialized.
210881afe893STejun Heo 	 */
210981afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
211081afe893STejun Heo 
211181afe893STejun Heo 	/* Device presence detection is unreliable on some
211281afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
211381afe893STejun Heo 	 */
211481afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
2115c6fd2807SJeff Garzik 
2116963e4975SAlan Cox 	if (ap->ops->read_id)
2117963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
2118963e4975SAlan Cox 	else
2119963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
2120963e4975SAlan Cox 
2121c6fd2807SJeff Garzik 	if (err_mask) {
2122800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
21231ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
21241ffc151fSTejun Heo 				       "NODEV after polling detection\n");
212555a8e2c8STejun Heo 			return -ENOENT;
212655a8e2c8STejun Heo 		}
212755a8e2c8STejun Heo 
21281ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
21291ffc151fSTejun Heo 			/* Device or controller might have reported
21301ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
21311ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
21321ffc151fSTejun Heo 			 * aborted by the device.
213354936f8bSTejun Heo 			 */
21341ffc151fSTejun Heo 			if (may_fallback) {
213554936f8bSTejun Heo 				may_fallback = 0;
213654936f8bSTejun Heo 
213754936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
213854936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
213954936f8bSTejun Heo 				else
214054936f8bSTejun Heo 					class = ATA_DEV_ATA;
214154936f8bSTejun Heo 				goto retry;
214254936f8bSTejun Heo 			}
214354936f8bSTejun Heo 
21441ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
21451ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
21461ffc151fSTejun Heo 			 * sometimes with phantom devices.
21471ffc151fSTejun Heo 			 */
21481ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
21491ffc151fSTejun Heo 				       "both IDENTIFYs aborted, assuming NODEV\n");
21501ffc151fSTejun Heo 			return -ENOENT;
21511ffc151fSTejun Heo 		}
21521ffc151fSTejun Heo 
2153c6fd2807SJeff Garzik 		rc = -EIO;
2154c6fd2807SJeff Garzik 		reason = "I/O error";
2155c6fd2807SJeff Garzik 		goto err_out;
2156c6fd2807SJeff Garzik 	}
2157c6fd2807SJeff Garzik 
215854936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
215954936f8bSTejun Heo 	 * successfully at least once.
216054936f8bSTejun Heo 	 */
216154936f8bSTejun Heo 	may_fallback = 0;
216254936f8bSTejun Heo 
2163c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
2164c6fd2807SJeff Garzik 
2165c6fd2807SJeff Garzik 	/* sanity check */
2166c6fd2807SJeff Garzik 	rc = -EINVAL;
21676070068bSAlan Cox 	reason = "device reports invalid type";
21684a3381feSJeff Garzik 
21694a3381feSJeff Garzik 	if (class == ATA_DEV_ATA) {
21704a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
21714a3381feSJeff Garzik 			goto err_out;
21724a3381feSJeff Garzik 	} else {
21734a3381feSJeff Garzik 		if (ata_id_is_ata(id))
2174c6fd2807SJeff Garzik 			goto err_out;
2175c6fd2807SJeff Garzik 	}
2176c6fd2807SJeff Garzik 
2177169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2178169439c2SMark Lord 		tried_spinup = 1;
2179169439c2SMark Lord 		/*
2180169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
2181169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
2182169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
2183169439c2SMark Lord 		 */
2184218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2185fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
2186169439c2SMark Lord 			rc = -EIO;
2187169439c2SMark Lord 			reason = "SPINUP failed";
2188169439c2SMark Lord 			goto err_out;
2189169439c2SMark Lord 		}
2190169439c2SMark Lord 		/*
2191169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
2192169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
2193169439c2SMark Lord 		 */
2194169439c2SMark Lord 		if (id[2] == 0x37c8)
2195169439c2SMark Lord 			goto retry;
2196169439c2SMark Lord 	}
2197169439c2SMark Lord 
2198bff04647STejun Heo 	if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2199c6fd2807SJeff Garzik 		/*
2200c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2201c6fd2807SJeff Garzik 		 * SRST RESET
220250a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
220350a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2204c6fd2807SJeff Garzik 		 * anything else..
2205c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
220650a99018SAlan Cox 		 *
220750a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
220850a99018SAlan Cox 		 * shoud never trigger.
2209c6fd2807SJeff Garzik 		 */
2210c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2211c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2212c6fd2807SJeff Garzik 			if (err_mask) {
2213c6fd2807SJeff Garzik 				rc = -EIO;
2214c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2215c6fd2807SJeff Garzik 				goto err_out;
2216c6fd2807SJeff Garzik 			}
2217c6fd2807SJeff Garzik 
2218c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2219c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2220c6fd2807SJeff Garzik 			 */
2221bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2222c6fd2807SJeff Garzik 			goto retry;
2223c6fd2807SJeff Garzik 		}
2224c6fd2807SJeff Garzik 	}
2225c6fd2807SJeff Garzik 
2226c6fd2807SJeff Garzik 	*p_class = class;
2227c6fd2807SJeff Garzik 
2228c6fd2807SJeff Garzik 	return 0;
2229c6fd2807SJeff Garzik 
2230c6fd2807SJeff Garzik  err_out:
2231c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2232c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2233c6fd2807SJeff Garzik 			       "(%s, err_mask=0x%x)\n", reason, err_mask);
2234c6fd2807SJeff Garzik 	return rc;
2235c6fd2807SJeff Garzik }
2236c6fd2807SJeff Garzik 
22379062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
22389062712fSTejun Heo {
22399062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
22409062712fSTejun Heo 	u32 target, target_limit;
22419062712fSTejun Heo 
22429062712fSTejun Heo 	if (!sata_scr_valid(plink))
22439062712fSTejun Heo 		return 0;
22449062712fSTejun Heo 
22459062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
22469062712fSTejun Heo 		target = 1;
22479062712fSTejun Heo 	else
22489062712fSTejun Heo 		return 0;
22499062712fSTejun Heo 
22509062712fSTejun Heo 	target_limit = (1 << target) - 1;
22519062712fSTejun Heo 
22529062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
22539062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
22549062712fSTejun Heo 		return 0;
22559062712fSTejun Heo 
22569062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
22579062712fSTejun Heo 
22589062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
22599062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
22609062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
22619062712fSTejun Heo 	 */
22629062712fSTejun Heo 	if (plink->sata_spd > target) {
22639062712fSTejun Heo 		ata_dev_printk(dev, KERN_INFO,
22649062712fSTejun Heo 			       "applying link speed limit horkage to %s\n",
22659062712fSTejun Heo 			       sata_spd_string(target));
22669062712fSTejun Heo 		return -EAGAIN;
22679062712fSTejun Heo 	}
22689062712fSTejun Heo 	return 0;
22699062712fSTejun Heo }
22709062712fSTejun Heo 
2271c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2272c6fd2807SJeff Garzik {
22739af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
22749ce8e307SJens Axboe 
22759ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
22769ce8e307SJens Axboe 		return 0;
22779ce8e307SJens Axboe 
22789af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2279c6fd2807SJeff Garzik }
2280c6fd2807SJeff Garzik 
2281c6fd2807SJeff Garzik static void ata_dev_config_ncq(struct ata_device *dev,
2282c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2283c6fd2807SJeff Garzik {
22849af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2285c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2286c6fd2807SJeff Garzik 
2287c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2288c6fd2807SJeff Garzik 		desc[0] = '\0';
2289c6fd2807SJeff Garzik 		return;
2290c6fd2807SJeff Garzik 	}
229175683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
22926919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
22936919a0a6SAlan Cox 		return;
22946919a0a6SAlan Cox 	}
2295c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2296cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2297c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2298c6fd2807SJeff Garzik 	}
2299c6fd2807SJeff Garzik 
2300c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2301c6fd2807SJeff Garzik 		snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
2302c6fd2807SJeff Garzik 	else
2303c6fd2807SJeff Garzik 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
2304c6fd2807SJeff Garzik }
2305c6fd2807SJeff Garzik 
2306c6fd2807SJeff Garzik /**
2307c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2308c6fd2807SJeff Garzik  *	@dev: Target device to configure
2309c6fd2807SJeff Garzik  *
2310c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2311c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2312c6fd2807SJeff Garzik  *
2313c6fd2807SJeff Garzik  *	LOCKING:
2314c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2315c6fd2807SJeff Garzik  *
2316c6fd2807SJeff Garzik  *	RETURNS:
2317c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2318c6fd2807SJeff Garzik  */
2319efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2320c6fd2807SJeff Garzik {
23219af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
23229af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
23236746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2324c6fd2807SJeff Garzik 	const u16 *id = dev->id;
23257dc951aeSTejun Heo 	unsigned long xfer_mask;
2326b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
23273f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
23283f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2329c6fd2807SJeff Garzik 	int rc;
2330c6fd2807SJeff Garzik 
2331c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
233244877b4eSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
23337f5e4e8dSHarvey Harrison 			       __func__);
2334c6fd2807SJeff Garzik 		return 0;
2335c6fd2807SJeff Garzik 	}
2336c6fd2807SJeff Garzik 
2337c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
23387f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2339c6fd2807SJeff Garzik 
234075683fe7STejun Heo 	/* set horkage */
234175683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
234233267325STejun Heo 	ata_force_horkage(dev);
234375683fe7STejun Heo 
234450af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
234550af2fa1STejun Heo 		ata_dev_printk(dev, KERN_INFO,
234650af2fa1STejun Heo 			       "unsupported device, disabling\n");
234750af2fa1STejun Heo 		ata_dev_disable(dev);
234850af2fa1STejun Heo 		return 0;
234950af2fa1STejun Heo 	}
235050af2fa1STejun Heo 
23512486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
23522486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
23532486fa56STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
23542486fa56STejun Heo 			"WARNING: ATAPI is %s, device ignored.\n",
23552486fa56STejun Heo 			atapi_enabled ? "not supported with this driver"
23562486fa56STejun Heo 				      : "disabled");
23572486fa56STejun Heo 		ata_dev_disable(dev);
23582486fa56STejun Heo 		return 0;
23592486fa56STejun Heo 	}
23602486fa56STejun Heo 
23619062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
23629062712fSTejun Heo 	if (rc)
23639062712fSTejun Heo 		return rc;
23649062712fSTejun Heo 
23656746544cSTejun Heo 	/* let ACPI work its magic */
23666746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
23676746544cSTejun Heo 	if (rc)
23686746544cSTejun Heo 		return rc;
236908573a86SKristen Carlson Accardi 
237005027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
237105027adcSTejun Heo 	rc = ata_hpa_resize(dev);
237205027adcSTejun Heo 	if (rc)
237305027adcSTejun Heo 		return rc;
237405027adcSTejun Heo 
2375c6fd2807SJeff Garzik 	/* print device capabilities */
2376c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2377c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
2378c6fd2807SJeff Garzik 			       "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2379c6fd2807SJeff Garzik 			       "85:%04x 86:%04x 87:%04x 88:%04x\n",
23807f5e4e8dSHarvey Harrison 			       __func__,
2381c6fd2807SJeff Garzik 			       id[49], id[82], id[83], id[84],
2382c6fd2807SJeff Garzik 			       id[85], id[86], id[87], id[88]);
2383c6fd2807SJeff Garzik 
2384c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2385c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2386c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2387c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2388c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2389c6fd2807SJeff Garzik 	dev->cylinders = 0;
2390c6fd2807SJeff Garzik 	dev->heads = 0;
2391c6fd2807SJeff Garzik 	dev->sectors = 0;
2392c6fd2807SJeff Garzik 
2393c6fd2807SJeff Garzik 	/*
2394c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2395c6fd2807SJeff Garzik 	 */
2396c6fd2807SJeff Garzik 
2397c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2398c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2399c6fd2807SJeff Garzik 
2400c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2401c6fd2807SJeff Garzik 		ata_dump_id(id);
2402c6fd2807SJeff Garzik 
2403ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2404ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2405ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2406ef143d57SAlbert Lee 
2407ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2408ef143d57SAlbert Lee 			sizeof(modelbuf));
2409ef143d57SAlbert Lee 
2410c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
2411c6fd2807SJeff Garzik 	if (dev->class == ATA_DEV_ATA) {
2412b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
2413b352e57dSAlan Cox 			if (id[162] & 1) /* CPRM may make this media unusable */
241444877b4eSTejun Heo 				ata_dev_printk(dev, KERN_WARNING,
241544877b4eSTejun Heo 					       "supports DRM functions and may "
241644877b4eSTejun Heo 					       "not be fully accessable.\n");
2417b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2418ae8d4ee7SAlan Cox 		} else {
2419b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2420ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2421ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2422ae8d4ee7SAlan Cox 				ata_dev_printk(dev, KERN_WARNING,
2423ae8d4ee7SAlan Cox 					       "supports DRM functions and may "
2424ae8d4ee7SAlan Cox 					       "not be fully accessable.\n");
2425ae8d4ee7SAlan Cox 		}
2426b352e57dSAlan Cox 
2427c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2428c6fd2807SJeff Garzik 
24293f64f565SEric D. Mudama 		if (dev->id[59] & 0x100)
24303f64f565SEric D. Mudama 			dev->multi_count = dev->id[59] & 0xff;
24313f64f565SEric D. Mudama 
2432c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2433c6fd2807SJeff Garzik 			const char *lba_desc;
2434c6fd2807SJeff Garzik 			char ncq_desc[20];
2435c6fd2807SJeff Garzik 
2436c6fd2807SJeff Garzik 			lba_desc = "LBA";
2437c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2438c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2439c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2440c6fd2807SJeff Garzik 				lba_desc = "LBA48";
24416fc49adbSTejun Heo 
24426fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
24436fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
24446fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2445c6fd2807SJeff Garzik 			}
2446c6fd2807SJeff Garzik 
2447c6fd2807SJeff Garzik 			/* config NCQ */
2448c6fd2807SJeff Garzik 			ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2449c6fd2807SJeff Garzik 
2450c6fd2807SJeff Garzik 			/* print device info to dmesg */
24513f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
24523f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
24533f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
24543f64f565SEric D. Mudama 					revbuf, modelbuf, fwrevbuf,
24553f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
24563f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
24573f64f565SEric D. Mudama 					"%Lu sectors, multi %u: %s %s\n",
2458c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
24593f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
24603f64f565SEric D. Mudama 			}
2461c6fd2807SJeff Garzik 		} else {
2462c6fd2807SJeff Garzik 			/* CHS */
2463c6fd2807SJeff Garzik 
2464c6fd2807SJeff Garzik 			/* Default translation */
2465c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2466c6fd2807SJeff Garzik 			dev->heads	= id[3];
2467c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2468c6fd2807SJeff Garzik 
2469c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2470c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2471c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2472c6fd2807SJeff Garzik 				dev->heads     = id[55];
2473c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2474c6fd2807SJeff Garzik 			}
2475c6fd2807SJeff Garzik 
2476c6fd2807SJeff Garzik 			/* print device info to dmesg */
24773f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2478c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_INFO,
24793f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
24803f64f565SEric D. Mudama 					revbuf,	modelbuf, fwrevbuf,
24813f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
24823f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
24833f64f565SEric D. Mudama 					"%Lu sectors, multi %u, CHS %u/%u/%u\n",
24843f64f565SEric D. Mudama 					(unsigned long long)dev->n_sectors,
24853f64f565SEric D. Mudama 					dev->multi_count, dev->cylinders,
24863f64f565SEric D. Mudama 					dev->heads, dev->sectors);
24873f64f565SEric D. Mudama 			}
2488c6fd2807SJeff Garzik 		}
2489c6fd2807SJeff Garzik 
2490c6fd2807SJeff Garzik 		dev->cdb_len = 16;
2491c6fd2807SJeff Garzik 	}
2492c6fd2807SJeff Garzik 
2493c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2494c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2495854c73a2STejun Heo 		const char *cdb_intr_string = "";
2496854c73a2STejun Heo 		const char *atapi_an_string = "";
249791163006STejun Heo 		const char *dma_dir_string = "";
24987d77b247STejun Heo 		u32 sntf;
2499c6fd2807SJeff Garzik 
2500c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2501c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2502c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2503c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
2504c6fd2807SJeff Garzik 					       "unsupported CDB len\n");
2505c6fd2807SJeff Garzik 			rc = -EINVAL;
2506c6fd2807SJeff Garzik 			goto err_out_nosup;
2507c6fd2807SJeff Garzik 		}
2508c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2509c6fd2807SJeff Garzik 
25107d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
25117d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
25127d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
25137d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
25149f45cbd3SKristen Carlson Accardi 		 */
25157d77b247STejun Heo 		if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2516071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
25177d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2518854c73a2STejun Heo 			unsigned int err_mask;
2519854c73a2STejun Heo 
25209f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2521218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2522218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2523854c73a2STejun Heo 			if (err_mask)
25249f45cbd3SKristen Carlson Accardi 				ata_dev_printk(dev, KERN_ERR,
2525854c73a2STejun Heo 					"failed to enable ATAPI AN "
2526854c73a2STejun Heo 					"(err_mask=0x%x)\n", err_mask);
2527854c73a2STejun Heo 			else {
25289f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2529854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2530854c73a2STejun Heo 			}
25319f45cbd3SKristen Carlson Accardi 		}
25329f45cbd3SKristen Carlson Accardi 
2533c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2534c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2535c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2536c6fd2807SJeff Garzik 		}
2537c6fd2807SJeff Garzik 
253891163006STejun Heo 		if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
253991163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
254091163006STejun Heo 			dma_dir_string = ", DMADIR";
254191163006STejun Heo 		}
254291163006STejun Heo 
2543c6fd2807SJeff Garzik 		/* print device info to dmesg */
2544c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2545ef143d57SAlbert Lee 			ata_dev_printk(dev, KERN_INFO,
254691163006STejun Heo 				       "ATAPI: %s, %s, max %s%s%s%s\n",
2547ef143d57SAlbert Lee 				       modelbuf, fwrevbuf,
2548c6fd2807SJeff Garzik 				       ata_mode_string(xfer_mask),
254991163006STejun Heo 				       cdb_intr_string, atapi_an_string,
255091163006STejun Heo 				       dma_dir_string);
2551c6fd2807SJeff Garzik 	}
2552c6fd2807SJeff Garzik 
2553914ed354STejun Heo 	/* determine max_sectors */
2554914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2555914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2556914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2557914ed354STejun Heo 
2558ca77329fSKristen Carlson Accardi 	if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2559ca77329fSKristen Carlson Accardi 		if (ata_id_has_hipm(dev->id))
2560ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_HIPM;
2561ca77329fSKristen Carlson Accardi 		if (ata_id_has_dipm(dev->id))
2562ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_DIPM;
2563ca77329fSKristen Carlson Accardi 	}
2564ca77329fSKristen Carlson Accardi 
2565c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2566c5038fc0SAlan Cox 	   200 sectors */
2567c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2568c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2569c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_INFO,
2570c6fd2807SJeff Garzik 				       "applying bridge limits\n");
2571c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2572c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2573c6fd2807SJeff Garzik 	}
2574c6fd2807SJeff Garzik 
2575f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2576f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2577f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2578f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2579f442cd86SAlbert Lee 	}
2580f8d8e579STony Battersby 
258175683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
258203ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
258303ec52deSTejun Heo 					 dev->max_sectors);
258418d6e9d5SAlbert Lee 
2585ca77329fSKristen Carlson Accardi 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2586ca77329fSKristen Carlson Accardi 		dev->horkage |= ATA_HORKAGE_IPM;
2587ca77329fSKristen Carlson Accardi 
2588ca77329fSKristen Carlson Accardi 		/* reset link pm_policy for this port to no pm */
2589ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
2590ca77329fSKristen Carlson Accardi 	}
2591ca77329fSKristen Carlson Accardi 
2592c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2593cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2594c6fd2807SJeff Garzik 
2595c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2596c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2597c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2598c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2599c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2600c5038fc0SAlan Cox 		   bugs */
2601c5038fc0SAlan Cox 
2602c5038fc0SAlan Cox 		if (print_info) {
2603c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2604c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2605c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2606c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2607c5038fc0SAlan Cox 		}
2608c5038fc0SAlan Cox 	}
2609c5038fc0SAlan Cox 
2610ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2611ac70a964STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "WARNING: device requires "
2612ac70a964STejun Heo 			       "firmware update to be fully functional.\n");
2613ac70a964STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "         contact the vendor "
2614ac70a964STejun Heo 			       "or visit http://ata.wiki.kernel.org.\n");
2615ac70a964STejun Heo 	}
2616ac70a964STejun Heo 
2617c6fd2807SJeff Garzik 	return 0;
2618c6fd2807SJeff Garzik 
2619c6fd2807SJeff Garzik err_out_nosup:
2620c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2621c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
26227f5e4e8dSHarvey Harrison 			       "%s: EXIT, err\n", __func__);
2623c6fd2807SJeff Garzik 	return rc;
2624c6fd2807SJeff Garzik }
2625c6fd2807SJeff Garzik 
2626c6fd2807SJeff Garzik /**
26272e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2628be0d18dfSAlan Cox  *	@ap: port
2629be0d18dfSAlan Cox  *
26302e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2631be0d18dfSAlan Cox  *	detection.
2632be0d18dfSAlan Cox  */
2633be0d18dfSAlan Cox 
2634be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2635be0d18dfSAlan Cox {
2636be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2637be0d18dfSAlan Cox }
2638be0d18dfSAlan Cox 
2639be0d18dfSAlan Cox /**
26402e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2641be0d18dfSAlan Cox  *	@ap: port
2642be0d18dfSAlan Cox  *
26432e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2644be0d18dfSAlan Cox  *	detection.
2645be0d18dfSAlan Cox  */
2646be0d18dfSAlan Cox 
2647be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2648be0d18dfSAlan Cox {
2649be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2650be0d18dfSAlan Cox }
2651be0d18dfSAlan Cox 
2652be0d18dfSAlan Cox /**
2653be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2654be0d18dfSAlan Cox  *	@ap: port
2655be0d18dfSAlan Cox  *
2656be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2657be0d18dfSAlan Cox  */
2658be0d18dfSAlan Cox 
2659be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2660be0d18dfSAlan Cox {
2661be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2662be0d18dfSAlan Cox }
2663be0d18dfSAlan Cox 
2664be0d18dfSAlan Cox /**
2665c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2666c88f90c3STejun Heo  *	@ap: port
2667c88f90c3STejun Heo  *
2668c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2669c88f90c3STejun Heo  *	transfer mode.
2670c88f90c3STejun Heo  */
2671c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2672c88f90c3STejun Heo {
2673c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2674c88f90c3STejun Heo }
2675c88f90c3STejun Heo 
2676c88f90c3STejun Heo /**
2677be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2678be0d18dfSAlan Cox  *	@ap: port
2679be0d18dfSAlan Cox  *
2680be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2681be0d18dfSAlan Cox  */
2682be0d18dfSAlan Cox 
2683be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2684be0d18dfSAlan Cox {
2685be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2686be0d18dfSAlan Cox }
2687be0d18dfSAlan Cox 
2688be0d18dfSAlan Cox /**
2689c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2690c6fd2807SJeff Garzik  *	@ap: Bus to probe
2691c6fd2807SJeff Garzik  *
2692c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2693c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2694c6fd2807SJeff Garzik  *	the bus.
2695c6fd2807SJeff Garzik  *
2696c6fd2807SJeff Garzik  *	LOCKING:
2697c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2698c6fd2807SJeff Garzik  *
2699c6fd2807SJeff Garzik  *	RETURNS:
2700c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2701c6fd2807SJeff Garzik  */
2702c6fd2807SJeff Garzik 
2703c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2704c6fd2807SJeff Garzik {
2705c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2706c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2707f58229f8STejun Heo 	int rc;
2708c6fd2807SJeff Garzik 	struct ata_device *dev;
2709c6fd2807SJeff Garzik 
2710c6fd2807SJeff Garzik 	ata_port_probe(ap);
2711c6fd2807SJeff Garzik 
27121eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2713f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2714c6fd2807SJeff Garzik 
2715c6fd2807SJeff Garzik  retry:
27161eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2717cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2718cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2719cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2720cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2721cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2722cdeab114STejun Heo 		 * bus as we may be talking too fast.
2723cdeab114STejun Heo 		 */
2724cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2725cdeab114STejun Heo 
2726cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2727cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2728cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2729cdeab114STejun Heo 		 * configuring devices.
2730cdeab114STejun Heo 		 */
2731cdeab114STejun Heo 		if (ap->ops->set_piomode)
2732cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2733cdeab114STejun Heo 	}
2734cdeab114STejun Heo 
2735c6fd2807SJeff Garzik 	/* reset and determine device classes */
2736c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2737c6fd2807SJeff Garzik 
27381eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2739c6fd2807SJeff Garzik 		if (!(ap->flags & ATA_FLAG_DISABLED) &&
2740c6fd2807SJeff Garzik 		    dev->class != ATA_DEV_UNKNOWN)
2741c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2742c6fd2807SJeff Garzik 		else
2743c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2744c6fd2807SJeff Garzik 
2745c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2746c6fd2807SJeff Garzik 	}
2747c6fd2807SJeff Garzik 
2748c6fd2807SJeff Garzik 	ata_port_probe(ap);
2749c6fd2807SJeff Garzik 
2750f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2751f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2752f31f0cc2SJeff Garzik 	   the slave device */
2753f31f0cc2SJeff Garzik 
27541eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2755f58229f8STejun Heo 		if (tries[dev->devno])
2756f58229f8STejun Heo 			dev->class = classes[dev->devno];
2757c6fd2807SJeff Garzik 
2758c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2759c6fd2807SJeff Garzik 			continue;
2760c6fd2807SJeff Garzik 
2761bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2762bff04647STejun Heo 				     dev->id);
2763c6fd2807SJeff Garzik 		if (rc)
2764c6fd2807SJeff Garzik 			goto fail;
2765f31f0cc2SJeff Garzik 	}
2766f31f0cc2SJeff Garzik 
2767be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2768be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2769be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2770be0d18dfSAlan Cox 
27711eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
27721eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
27731eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
27741eca4365STejun Heo 	 * of the link the bridge is which is a problem.
27751eca4365STejun Heo 	 */
27761eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2777614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2778614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2779614fe29bSAlan Cox 
2780f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2781f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2782f31f0cc2SJeff Garzik 
27831eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
27849af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2785efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
27869af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2787c6fd2807SJeff Garzik 		if (rc)
2788c6fd2807SJeff Garzik 			goto fail;
2789c6fd2807SJeff Garzik 	}
2790c6fd2807SJeff Garzik 
2791c6fd2807SJeff Garzik 	/* configure transfer mode */
27920260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
27934ae72a1eSTejun Heo 	if (rc)
2794c6fd2807SJeff Garzik 		goto fail;
2795c6fd2807SJeff Garzik 
27961eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2797c6fd2807SJeff Garzik 		return 0;
2798c6fd2807SJeff Garzik 
2799c6fd2807SJeff Garzik 	/* no device present, disable port */
2800c6fd2807SJeff Garzik 	ata_port_disable(ap);
2801c6fd2807SJeff Garzik 	return -ENODEV;
2802c6fd2807SJeff Garzik 
2803c6fd2807SJeff Garzik  fail:
28044ae72a1eSTejun Heo 	tries[dev->devno]--;
28054ae72a1eSTejun Heo 
2806c6fd2807SJeff Garzik 	switch (rc) {
2807c6fd2807SJeff Garzik 	case -EINVAL:
28084ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2809c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2810c6fd2807SJeff Garzik 		break;
28114ae72a1eSTejun Heo 
28124ae72a1eSTejun Heo 	case -ENODEV:
28134ae72a1eSTejun Heo 		/* give it just one more chance */
28144ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2815c6fd2807SJeff Garzik 	case -EIO:
28164ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
28174ae72a1eSTejun Heo 			/* This is the last chance, better to slow
28184ae72a1eSTejun Heo 			 * down than lose it.
28194ae72a1eSTejun Heo 			 */
2820a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
28214ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
28224ae72a1eSTejun Heo 		}
2823c6fd2807SJeff Garzik 	}
2824c6fd2807SJeff Garzik 
28254ae72a1eSTejun Heo 	if (!tries[dev->devno])
2826c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2827c6fd2807SJeff Garzik 
2828c6fd2807SJeff Garzik 	goto retry;
2829c6fd2807SJeff Garzik }
2830c6fd2807SJeff Garzik 
2831c6fd2807SJeff Garzik /**
2832c6fd2807SJeff Garzik  *	ata_port_probe - Mark port as enabled
2833c6fd2807SJeff Garzik  *	@ap: Port for which we indicate enablement
2834c6fd2807SJeff Garzik  *
2835c6fd2807SJeff Garzik  *	Modify @ap data structure such that the system
2836c6fd2807SJeff Garzik  *	thinks that the entire port is enabled.
2837c6fd2807SJeff Garzik  *
2838cca3974eSJeff Garzik  *	LOCKING: host lock, or some other form of
2839c6fd2807SJeff Garzik  *	serialization.
2840c6fd2807SJeff Garzik  */
2841c6fd2807SJeff Garzik 
2842c6fd2807SJeff Garzik void ata_port_probe(struct ata_port *ap)
2843c6fd2807SJeff Garzik {
2844c6fd2807SJeff Garzik 	ap->flags &= ~ATA_FLAG_DISABLED;
2845c6fd2807SJeff Garzik }
2846c6fd2807SJeff Garzik 
2847c6fd2807SJeff Garzik /**
2848c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2849936fd732STejun Heo  *	@link: SATA link to printk link status about
2850c6fd2807SJeff Garzik  *
2851c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2852c6fd2807SJeff Garzik  *
2853c6fd2807SJeff Garzik  *	LOCKING:
2854c6fd2807SJeff Garzik  *	None.
2855c6fd2807SJeff Garzik  */
28566bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2857c6fd2807SJeff Garzik {
2858c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2859c6fd2807SJeff Garzik 
2860936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2861c6fd2807SJeff Garzik 		return;
2862936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2863c6fd2807SJeff Garzik 
2864b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2865c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2866936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2867c6fd2807SJeff Garzik 				"SATA link up %s (SStatus %X SControl %X)\n",
2868c6fd2807SJeff Garzik 				sata_spd_string(tmp), sstatus, scontrol);
2869c6fd2807SJeff Garzik 	} else {
2870936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2871c6fd2807SJeff Garzik 				"SATA link down (SStatus %X SControl %X)\n",
2872c6fd2807SJeff Garzik 				sstatus, scontrol);
2873c6fd2807SJeff Garzik 	}
2874c6fd2807SJeff Garzik }
2875c6fd2807SJeff Garzik 
2876c6fd2807SJeff Garzik /**
2877c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2878c6fd2807SJeff Garzik  *	@adev: device
2879c6fd2807SJeff Garzik  *
2880c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2881c6fd2807SJeff Garzik  *	present NULL is returned
2882c6fd2807SJeff Garzik  */
2883c6fd2807SJeff Garzik 
2884c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2885c6fd2807SJeff Garzik {
28869af5c9c9STejun Heo 	struct ata_link *link = adev->link;
28879af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2888c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2889c6fd2807SJeff Garzik 		return NULL;
2890c6fd2807SJeff Garzik 	return pair;
2891c6fd2807SJeff Garzik }
2892c6fd2807SJeff Garzik 
2893c6fd2807SJeff Garzik /**
2894c6fd2807SJeff Garzik  *	ata_port_disable - Disable port.
2895c6fd2807SJeff Garzik  *	@ap: Port to be disabled.
2896c6fd2807SJeff Garzik  *
2897c6fd2807SJeff Garzik  *	Modify @ap data structure such that the system
2898c6fd2807SJeff Garzik  *	thinks that the entire port is disabled, and should
2899c6fd2807SJeff Garzik  *	never attempt to probe or communicate with devices
2900c6fd2807SJeff Garzik  *	on this port.
2901c6fd2807SJeff Garzik  *
2902cca3974eSJeff Garzik  *	LOCKING: host lock, or some other form of
2903c6fd2807SJeff Garzik  *	serialization.
2904c6fd2807SJeff Garzik  */
2905c6fd2807SJeff Garzik 
2906c6fd2807SJeff Garzik void ata_port_disable(struct ata_port *ap)
2907c6fd2807SJeff Garzik {
29089af5c9c9STejun Heo 	ap->link.device[0].class = ATA_DEV_NONE;
29099af5c9c9STejun Heo 	ap->link.device[1].class = ATA_DEV_NONE;
2910c6fd2807SJeff Garzik 	ap->flags |= ATA_FLAG_DISABLED;
2911c6fd2807SJeff Garzik }
2912c6fd2807SJeff Garzik 
2913c6fd2807SJeff Garzik /**
2914c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2915936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2916a07d499bSTejun Heo  *	@spd_limit: Additional limit
2917c6fd2807SJeff Garzik  *
2918936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2919c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2920c6fd2807SJeff Garzik  *	using sata_set_spd().
2921c6fd2807SJeff Garzik  *
2922a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
2923a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
2924a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
2925a07d499bSTejun Heo  *	supported speed is allowed.
2926a07d499bSTejun Heo  *
2927c6fd2807SJeff Garzik  *	LOCKING:
2928c6fd2807SJeff Garzik  *	Inherited from caller.
2929c6fd2807SJeff Garzik  *
2930c6fd2807SJeff Garzik  *	RETURNS:
2931c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
2932c6fd2807SJeff Garzik  */
2933a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2934c6fd2807SJeff Garzik {
2935c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
2936a07d499bSTejun Heo 	int rc, bit;
2937c6fd2807SJeff Garzik 
2938936fd732STejun Heo 	if (!sata_scr_valid(link))
2939008a7896STejun Heo 		return -EOPNOTSUPP;
2940008a7896STejun Heo 
2941008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
2942936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
2943008a7896STejun Heo 	 */
2944936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
29459913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
2946008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
2947008a7896STejun Heo 	else
2948936fd732STejun Heo 		spd = link->sata_spd;
2949c6fd2807SJeff Garzik 
2950936fd732STejun Heo 	mask = link->sata_spd_limit;
2951c6fd2807SJeff Garzik 	if (mask <= 1)
2952c6fd2807SJeff Garzik 		return -EINVAL;
2953008a7896STejun Heo 
2954008a7896STejun Heo 	/* unconditionally mask off the highest bit */
2955a07d499bSTejun Heo 	bit = fls(mask) - 1;
2956a07d499bSTejun Heo 	mask &= ~(1 << bit);
2957c6fd2807SJeff Garzik 
2958008a7896STejun Heo 	/* Mask off all speeds higher than or equal to the current
2959008a7896STejun Heo 	 * one.  Force 1.5Gbps if current SPD is not available.
2960008a7896STejun Heo 	 */
2961008a7896STejun Heo 	if (spd > 1)
2962008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
2963008a7896STejun Heo 	else
2964008a7896STejun Heo 		mask &= 1;
2965008a7896STejun Heo 
2966008a7896STejun Heo 	/* were we already at the bottom? */
2967c6fd2807SJeff Garzik 	if (!mask)
2968c6fd2807SJeff Garzik 		return -EINVAL;
2969c6fd2807SJeff Garzik 
2970a07d499bSTejun Heo 	if (spd_limit) {
2971a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
2972a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
2973a07d499bSTejun Heo 		else {
2974a07d499bSTejun Heo 			bit = ffs(mask) - 1;
2975a07d499bSTejun Heo 			mask = 1 << bit;
2976a07d499bSTejun Heo 		}
2977a07d499bSTejun Heo 	}
2978a07d499bSTejun Heo 
2979936fd732STejun Heo 	link->sata_spd_limit = mask;
2980c6fd2807SJeff Garzik 
2981936fd732STejun Heo 	ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
2982c6fd2807SJeff Garzik 			sata_spd_string(fls(mask)));
2983c6fd2807SJeff Garzik 
2984c6fd2807SJeff Garzik 	return 0;
2985c6fd2807SJeff Garzik }
2986c6fd2807SJeff Garzik 
2987936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2988c6fd2807SJeff Garzik {
29895270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
29905270222fSTejun Heo 	u32 limit, target, spd;
2991c6fd2807SJeff Garzik 
29925270222fSTejun Heo 	limit = link->sata_spd_limit;
29935270222fSTejun Heo 
29945270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
29955270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
29965270222fSTejun Heo 	 * configuration.
29975270222fSTejun Heo 	 */
29985270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
29995270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
30005270222fSTejun Heo 
30015270222fSTejun Heo 	if (limit == UINT_MAX)
30025270222fSTejun Heo 		target = 0;
3003c6fd2807SJeff Garzik 	else
30045270222fSTejun Heo 		target = fls(limit);
3005c6fd2807SJeff Garzik 
3006c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
30075270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
3008c6fd2807SJeff Garzik 
30095270222fSTejun Heo 	return spd != target;
3010c6fd2807SJeff Garzik }
3011c6fd2807SJeff Garzik 
3012c6fd2807SJeff Garzik /**
3013c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
3014936fd732STejun Heo  *	@link: Link in question
3015c6fd2807SJeff Garzik  *
3016c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
3017936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
3018c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
3019c6fd2807SJeff Garzik  *	configuration.
3020c6fd2807SJeff Garzik  *
3021c6fd2807SJeff Garzik  *	LOCKING:
3022c6fd2807SJeff Garzik  *	Inherited from caller.
3023c6fd2807SJeff Garzik  *
3024c6fd2807SJeff Garzik  *	RETURNS:
3025c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
3026c6fd2807SJeff Garzik  */
30271dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
3028c6fd2807SJeff Garzik {
3029c6fd2807SJeff Garzik 	u32 scontrol;
3030c6fd2807SJeff Garzik 
3031936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3032db64bcf3STejun Heo 		return 1;
3033c6fd2807SJeff Garzik 
3034936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
3035c6fd2807SJeff Garzik }
3036c6fd2807SJeff Garzik 
3037c6fd2807SJeff Garzik /**
3038c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
3039936fd732STejun Heo  *	@link: Link to set SATA spd for
3040c6fd2807SJeff Garzik  *
3041936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
3042c6fd2807SJeff Garzik  *
3043c6fd2807SJeff Garzik  *	LOCKING:
3044c6fd2807SJeff Garzik  *	Inherited from caller.
3045c6fd2807SJeff Garzik  *
3046c6fd2807SJeff Garzik  *	RETURNS:
3047c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
3048c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
3049c6fd2807SJeff Garzik  */
3050936fd732STejun Heo int sata_set_spd(struct ata_link *link)
3051c6fd2807SJeff Garzik {
3052c6fd2807SJeff Garzik 	u32 scontrol;
3053c6fd2807SJeff Garzik 	int rc;
3054c6fd2807SJeff Garzik 
3055936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3056c6fd2807SJeff Garzik 		return rc;
3057c6fd2807SJeff Garzik 
3058936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
3059c6fd2807SJeff Garzik 		return 0;
3060c6fd2807SJeff Garzik 
3061936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3062c6fd2807SJeff Garzik 		return rc;
3063c6fd2807SJeff Garzik 
3064c6fd2807SJeff Garzik 	return 1;
3065c6fd2807SJeff Garzik }
3066c6fd2807SJeff Garzik 
3067c6fd2807SJeff Garzik /*
3068c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
3069c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3070c6fd2807SJeff Garzik  */
3071c6fd2807SJeff Garzik /*
3072b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3073c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3074b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
3075b352e57dSAlan Cox  *
3076b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3077c6fd2807SJeff Garzik  */
3078c6fd2807SJeff Garzik 
3079c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
30803ada9c12SDavid Daney /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0,  960,   0 }, */
30813ada9c12SDavid Daney 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 0,  600,   0 },
30823ada9c12SDavid Daney 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 0,  383,   0 },
30833ada9c12SDavid Daney 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 0,  240,   0 },
30843ada9c12SDavid Daney 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 0,  180,   0 },
30853ada9c12SDavid Daney 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 0,  120,   0 },
30863ada9c12SDavid Daney 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 0,  100,   0 },
30873ada9c12SDavid Daney 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20, 0,   80,   0 },
3088c6fd2807SJeff Garzik 
30893ada9c12SDavid Daney 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 50, 960,   0 },
30903ada9c12SDavid Daney 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 30, 480,   0 },
30913ada9c12SDavid Daney 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 20, 240,   0 },
3092c6fd2807SJeff Garzik 
30933ada9c12SDavid Daney 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 20, 480,   0 },
30943ada9c12SDavid Daney 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 5,  150,   0 },
30953ada9c12SDavid Daney 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 5,  120,   0 },
30963ada9c12SDavid Daney 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 5,  100,   0 },
30973ada9c12SDavid Daney 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20, 5,   80,   0 },
3098c6fd2807SJeff Garzik 
30993ada9c12SDavid Daney /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0, 0,    0, 150 }, */
31003ada9c12SDavid Daney 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0, 0,    0, 120 },
31013ada9c12SDavid Daney 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0, 0,    0,  80 },
31023ada9c12SDavid Daney 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0, 0,    0,  60 },
31033ada9c12SDavid Daney 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0, 0,    0,  45 },
31043ada9c12SDavid Daney 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0, 0,    0,  30 },
31053ada9c12SDavid Daney 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0, 0,    0,  20 },
31063ada9c12SDavid Daney 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0, 0,    0,  15 },
3107c6fd2807SJeff Garzik 
3108c6fd2807SJeff Garzik 	{ 0xFF }
3109c6fd2807SJeff Garzik };
3110c6fd2807SJeff Garzik 
3111c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
3112c6fd2807SJeff Garzik #define EZ(v, unit)		((v)?ENOUGH(v, unit):0)
3113c6fd2807SJeff Garzik 
3114c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3115c6fd2807SJeff Garzik {
3116c6fd2807SJeff Garzik 	q->setup	= EZ(t->setup      * 1000,  T);
3117c6fd2807SJeff Garzik 	q->act8b	= EZ(t->act8b      * 1000,  T);
3118c6fd2807SJeff Garzik 	q->rec8b	= EZ(t->rec8b      * 1000,  T);
3119c6fd2807SJeff Garzik 	q->cyc8b	= EZ(t->cyc8b      * 1000,  T);
3120c6fd2807SJeff Garzik 	q->active	= EZ(t->active     * 1000,  T);
3121c6fd2807SJeff Garzik 	q->recover	= EZ(t->recover    * 1000,  T);
31223ada9c12SDavid Daney 	q->dmack_hold	= EZ(t->dmack_hold * 1000,  T);
3123c6fd2807SJeff Garzik 	q->cycle	= EZ(t->cycle      * 1000,  T);
3124c6fd2807SJeff Garzik 	q->udma		= EZ(t->udma       * 1000, UT);
3125c6fd2807SJeff Garzik }
3126c6fd2807SJeff Garzik 
3127c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3128c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
3129c6fd2807SJeff Garzik {
3130c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
3131c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
3132c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
3133c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
3134c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
3135c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
31363ada9c12SDavid Daney 	if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
3137c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
3138c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
3139c6fd2807SJeff Garzik }
3140c6fd2807SJeff Garzik 
31416357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3142c6fd2807SJeff Garzik {
314370cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
3144c6fd2807SJeff Garzik 
314570cd071eSTejun Heo 	while (xfer_mode > t->mode)
314670cd071eSTejun Heo 		t++;
314770cd071eSTejun Heo 
314870cd071eSTejun Heo 	if (xfer_mode == t->mode)
3149c6fd2807SJeff Garzik 		return t;
315070cd071eSTejun Heo 	return NULL;
3151c6fd2807SJeff Garzik }
3152c6fd2807SJeff Garzik 
3153c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3154c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3155c6fd2807SJeff Garzik {
3156c6fd2807SJeff Garzik 	const struct ata_timing *s;
3157c6fd2807SJeff Garzik 	struct ata_timing p;
3158c6fd2807SJeff Garzik 
3159c6fd2807SJeff Garzik 	/*
3160c6fd2807SJeff Garzik 	 * Find the mode.
3161c6fd2807SJeff Garzik 	 */
3162c6fd2807SJeff Garzik 
3163c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3164c6fd2807SJeff Garzik 		return -EINVAL;
3165c6fd2807SJeff Garzik 
3166c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3167c6fd2807SJeff Garzik 
3168c6fd2807SJeff Garzik 	/*
3169c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3170c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3171c6fd2807SJeff Garzik 	 */
3172c6fd2807SJeff Garzik 
3173c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3174c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
3175c6fd2807SJeff Garzik 		if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
3176c6fd2807SJeff Garzik 			if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
3177c6fd2807SJeff Garzik 					    else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
3178c6fd2807SJeff Garzik 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
3179c6fd2807SJeff Garzik 			p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
3180c6fd2807SJeff Garzik 		}
3181c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3182c6fd2807SJeff Garzik 	}
3183c6fd2807SJeff Garzik 
3184c6fd2807SJeff Garzik 	/*
3185c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3186c6fd2807SJeff Garzik 	 */
3187c6fd2807SJeff Garzik 
3188c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3189c6fd2807SJeff Garzik 
3190c6fd2807SJeff Garzik 	/*
3191c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3192c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3193c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3194c6fd2807SJeff Garzik 	 */
3195c6fd2807SJeff Garzik 
3196fd3367afSAlan 	if (speed > XFER_PIO_6) {
3197c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3198c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3199c6fd2807SJeff Garzik 	}
3200c6fd2807SJeff Garzik 
3201c6fd2807SJeff Garzik 	/*
3202c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3203c6fd2807SJeff Garzik 	 */
3204c6fd2807SJeff Garzik 
3205c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3206c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3207c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3208c6fd2807SJeff Garzik 	}
3209c6fd2807SJeff Garzik 
3210c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3211c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3212c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3213c6fd2807SJeff Garzik 	}
32144f701d1eSAlan Cox 
32154f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
32164f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
32174f701d1eSAlan Cox 	   if so we must correct this */
32184f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
32194f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3220c6fd2807SJeff Garzik 
3221c6fd2807SJeff Garzik 	return 0;
3222c6fd2807SJeff Garzik }
3223c6fd2807SJeff Garzik 
3224c6fd2807SJeff Garzik /**
3225a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3226a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3227a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3228a0f79b92STejun Heo  *
3229a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3230a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3231a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3232a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3233a0f79b92STejun Heo  *
3234a0f79b92STejun Heo  *	LOCKING:
3235a0f79b92STejun Heo  *	None.
3236a0f79b92STejun Heo  *
3237a0f79b92STejun Heo  *	RETURNS:
3238a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3239a0f79b92STejun Heo  */
3240a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3241a0f79b92STejun Heo {
3242a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3243a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3244a0f79b92STejun Heo 	const struct ata_timing *t;
3245a0f79b92STejun Heo 
3246a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3247a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3248a0f79b92STejun Heo 			base_mode = ent->base;
3249a0f79b92STejun Heo 
3250a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3251a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3252a0f79b92STejun Heo 		unsigned short this_cycle;
3253a0f79b92STejun Heo 
3254a0f79b92STejun Heo 		switch (xfer_shift) {
3255a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3256a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3257a0f79b92STejun Heo 			this_cycle = t->cycle;
3258a0f79b92STejun Heo 			break;
3259a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3260a0f79b92STejun Heo 			this_cycle = t->udma;
3261a0f79b92STejun Heo 			break;
3262a0f79b92STejun Heo 		default:
3263a0f79b92STejun Heo 			return 0xff;
3264a0f79b92STejun Heo 		}
3265a0f79b92STejun Heo 
3266a0f79b92STejun Heo 		if (cycle > this_cycle)
3267a0f79b92STejun Heo 			break;
3268a0f79b92STejun Heo 
3269a0f79b92STejun Heo 		last_mode = t->mode;
3270a0f79b92STejun Heo 	}
3271a0f79b92STejun Heo 
3272a0f79b92STejun Heo 	return last_mode;
3273a0f79b92STejun Heo }
3274a0f79b92STejun Heo 
3275a0f79b92STejun Heo /**
3276c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3277c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3278458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3279c6fd2807SJeff Garzik  *
3280c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3281c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3282c6fd2807SJeff Garzik  *	will apply the limit.
3283c6fd2807SJeff Garzik  *
3284c6fd2807SJeff Garzik  *	LOCKING:
3285c6fd2807SJeff Garzik  *	Inherited from caller.
3286c6fd2807SJeff Garzik  *
3287c6fd2807SJeff Garzik  *	RETURNS:
3288c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3289c6fd2807SJeff Garzik  */
3290458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3291c6fd2807SJeff Garzik {
3292458337dbSTejun Heo 	char buf[32];
32937dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
32947dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3295458337dbSTejun Heo 	int quiet, highbit;
3296c6fd2807SJeff Garzik 
3297458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3298458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3299458337dbSTejun Heo 
3300458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3301458337dbSTejun Heo 						  dev->mwdma_mask,
3302c6fd2807SJeff Garzik 						  dev->udma_mask);
3303458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3304c6fd2807SJeff Garzik 
3305458337dbSTejun Heo 	switch (sel) {
3306458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3307458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3308458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3309458337dbSTejun Heo 		break;
3310458337dbSTejun Heo 
3311458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3312458337dbSTejun Heo 		if (udma_mask) {
3313458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3314458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3315458337dbSTejun Heo 			if (!udma_mask)
3316458337dbSTejun Heo 				return -ENOENT;
3317458337dbSTejun Heo 		} else if (mwdma_mask) {
3318458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3319458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3320458337dbSTejun Heo 			if (!mwdma_mask)
3321458337dbSTejun Heo 				return -ENOENT;
3322458337dbSTejun Heo 		}
3323458337dbSTejun Heo 		break;
3324458337dbSTejun Heo 
3325458337dbSTejun Heo 	case ATA_DNXFER_40C:
3326458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3327458337dbSTejun Heo 		break;
3328458337dbSTejun Heo 
3329458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3330458337dbSTejun Heo 		pio_mask &= 1;
3331458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3332458337dbSTejun Heo 		mwdma_mask = 0;
3333458337dbSTejun Heo 		udma_mask = 0;
3334458337dbSTejun Heo 		break;
3335458337dbSTejun Heo 
3336458337dbSTejun Heo 	default:
3337458337dbSTejun Heo 		BUG();
3338458337dbSTejun Heo 	}
3339458337dbSTejun Heo 
3340458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3341458337dbSTejun Heo 
3342458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3343458337dbSTejun Heo 		return -ENOENT;
3344458337dbSTejun Heo 
3345458337dbSTejun Heo 	if (!quiet) {
3346458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3347458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3348458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3349458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3350458337dbSTejun Heo 		else
3351458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3352458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3353458337dbSTejun Heo 
3354458337dbSTejun Heo 		ata_dev_printk(dev, KERN_WARNING,
3355458337dbSTejun Heo 			       "limiting speed to %s\n", buf);
3356458337dbSTejun Heo 	}
3357c6fd2807SJeff Garzik 
3358c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3359c6fd2807SJeff Garzik 			    &dev->udma_mask);
3360c6fd2807SJeff Garzik 
3361c6fd2807SJeff Garzik 	return 0;
3362c6fd2807SJeff Garzik }
3363c6fd2807SJeff Garzik 
3364c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3365c6fd2807SJeff Garzik {
33669af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
33674055dee7STejun Heo 	const char *dev_err_whine = "";
33684055dee7STejun Heo 	int ign_dev_err = 0;
3369c6fd2807SJeff Garzik 	unsigned int err_mask;
3370c6fd2807SJeff Garzik 	int rc;
3371c6fd2807SJeff Garzik 
3372c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3373c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3374c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3375c6fd2807SJeff Garzik 
3376c6fd2807SJeff Garzik 	err_mask = ata_dev_set_xfermode(dev);
33772dcb407eSJeff Garzik 
33784055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
33794055dee7STejun Heo 		goto fail;
33802dcb407eSJeff Garzik 
33814055dee7STejun Heo 	/* revalidate */
3382baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3383422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3384baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3385c6fd2807SJeff Garzik 	if (rc)
3386c6fd2807SJeff Garzik 		return rc;
3387c6fd2807SJeff Garzik 
3388b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
33894055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3390b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
33914055dee7STejun Heo 			ign_dev_err = 1;
3392b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3393b93fda12SAlan Cox 		   ATA devices */
3394b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
33954055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
33964055dee7STejun Heo 			ign_dev_err = 1;
3397b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3398b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3399b93fda12SAlan Cox 		   timings and no IORDY */
3400b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3401b93fda12SAlan Cox 			ign_dev_err = 1;
3402b93fda12SAlan Cox 	}
34034055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
34044055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
34054055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
34064055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
34074055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
34084055dee7STejun Heo 		ign_dev_err = 1;
34094055dee7STejun Heo 
34104055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
34114055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
34124055dee7STejun Heo 		ign_dev_err = 1;
34134055dee7STejun Heo 
34144055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
34154055dee7STejun Heo 		if (!ign_dev_err)
34164055dee7STejun Heo 			goto fail;
34174055dee7STejun Heo 		else
34184055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
34194055dee7STejun Heo 	}
34204055dee7STejun Heo 
3421c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3422c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3423c6fd2807SJeff Garzik 
34244055dee7STejun Heo 	ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
34254055dee7STejun Heo 		       ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
34264055dee7STejun Heo 		       dev_err_whine);
34274055dee7STejun Heo 
3428c6fd2807SJeff Garzik 	return 0;
34294055dee7STejun Heo 
34304055dee7STejun Heo  fail:
34314055dee7STejun Heo 	ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
34324055dee7STejun Heo 		       "(err_mask=0x%x)\n", err_mask);
34334055dee7STejun Heo 	return -EIO;
3434c6fd2807SJeff Garzik }
3435c6fd2807SJeff Garzik 
3436c6fd2807SJeff Garzik /**
343704351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
34380260731fSTejun Heo  *	@link: link on which timings will be programmed
34391967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3440c6fd2807SJeff Garzik  *
344104351821SAlan  *	Standard implementation of the function used to tune and set
344204351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
344304351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3444c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3445c6fd2807SJeff Garzik  *
3446c6fd2807SJeff Garzik  *	LOCKING:
3447c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3448c6fd2807SJeff Garzik  *
3449c6fd2807SJeff Garzik  *	RETURNS:
3450c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3451c6fd2807SJeff Garzik  */
345204351821SAlan 
34530260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3454c6fd2807SJeff Garzik {
34550260731fSTejun Heo 	struct ata_port *ap = link->ap;
3456c6fd2807SJeff Garzik 	struct ata_device *dev;
3457f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3458c6fd2807SJeff Garzik 
3459c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
34601eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
34617dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3462b3a70601SAlan Cox 		unsigned int mode_mask;
3463c6fd2807SJeff Garzik 
3464b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3465b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3466b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3467b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3468b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3469b3a70601SAlan Cox 
3470c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
347133267325STejun Heo 		ata_force_xfermask(dev);
3472c6fd2807SJeff Garzik 
3473c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3474c6fd2807SJeff Garzik 		dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3475b3a70601SAlan Cox 
3476b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
3477b3a70601SAlan Cox 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3478b3a70601SAlan Cox 		else
3479b3a70601SAlan Cox 			dma_mask = 0;
3480b3a70601SAlan Cox 
3481c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3482c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3483c6fd2807SJeff Garzik 
3484c6fd2807SJeff Garzik 		found = 1;
3485b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3486c6fd2807SJeff Garzik 			used_dma = 1;
3487c6fd2807SJeff Garzik 	}
3488c6fd2807SJeff Garzik 	if (!found)
3489c6fd2807SJeff Garzik 		goto out;
3490c6fd2807SJeff Garzik 
3491c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
34921eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
349370cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3494c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3495c6fd2807SJeff Garzik 			rc = -EINVAL;
3496c6fd2807SJeff Garzik 			goto out;
3497c6fd2807SJeff Garzik 		}
3498c6fd2807SJeff Garzik 
3499c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3500c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3501c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3502c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3503c6fd2807SJeff Garzik 	}
3504c6fd2807SJeff Garzik 
3505c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
35061eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
35071eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3508c6fd2807SJeff Garzik 			continue;
3509c6fd2807SJeff Garzik 
3510c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3511c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3512c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3513c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3514c6fd2807SJeff Garzik 	}
3515c6fd2807SJeff Garzik 
3516c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
35171eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3518c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3519c6fd2807SJeff Garzik 		if (rc)
3520c6fd2807SJeff Garzik 			goto out;
3521c6fd2807SJeff Garzik 	}
3522c6fd2807SJeff Garzik 
3523c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3524c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3525c6fd2807SJeff Garzik 	 */
3526cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3527032af1ceSAlan 		ap->host->simplex_claimed = ap;
3528c6fd2807SJeff Garzik 
3529c6fd2807SJeff Garzik  out:
3530c6fd2807SJeff Garzik 	if (rc)
3531c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3532c6fd2807SJeff Garzik 	return rc;
3533c6fd2807SJeff Garzik }
3534c6fd2807SJeff Garzik 
3535c6fd2807SJeff Garzik /**
3536aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3537aa2731adSTejun Heo  *	@link: link to be waited on
3538aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3539aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3540aa2731adSTejun Heo  *
3541aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3542aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3543aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3544aa2731adSTejun Heo  *	conditions.
3545aa2731adSTejun Heo  *
3546aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3547aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3548aa2731adSTejun Heo  *
3549aa2731adSTejun Heo  *	LOCKING:
3550aa2731adSTejun Heo  *	EH context.
3551aa2731adSTejun Heo  *
3552aa2731adSTejun Heo  *	RETURNS:
3553aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3554aa2731adSTejun Heo  */
3555aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3556aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3557aa2731adSTejun Heo {
3558aa2731adSTejun Heo 	unsigned long start = jiffies;
3559341c2c95STejun Heo 	unsigned long nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3560aa2731adSTejun Heo 	int warned = 0;
3561aa2731adSTejun Heo 
3562b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3563b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3564b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3565b1c72916STejun Heo 	 */
3566b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3567b1c72916STejun Heo 
3568aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3569aa2731adSTejun Heo 		nodev_deadline = deadline;
3570aa2731adSTejun Heo 
3571aa2731adSTejun Heo 	while (1) {
3572aa2731adSTejun Heo 		unsigned long now = jiffies;
3573aa2731adSTejun Heo 		int ready, tmp;
3574aa2731adSTejun Heo 
3575aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3576aa2731adSTejun Heo 		if (ready > 0)
3577aa2731adSTejun Heo 			return 0;
3578aa2731adSTejun Heo 
3579aa2731adSTejun Heo 		/* -ENODEV could be transient.  Ignore -ENODEV if link
3580aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3581aa2731adSTejun Heo 		 * time to clear 0xff after reset.  For example,
3582aa2731adSTejun Heo 		 * HHD424020F7SV00 iVDR needs >= 800ms while Quantum
3583aa2731adSTejun Heo 		 * GoVault needs even more than that.  Wait for
3584aa2731adSTejun Heo 		 * ATA_TMOUT_FF_WAIT on -ENODEV if link isn't offline.
3585aa2731adSTejun Heo 		 *
3586aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3587aa2731adSTejun Heo 		 * if status register is read more than once when
3588aa2731adSTejun Heo 		 * there's no device attached.
3589aa2731adSTejun Heo 		 */
3590aa2731adSTejun Heo 		if (ready == -ENODEV) {
3591aa2731adSTejun Heo 			if (ata_link_online(link))
3592aa2731adSTejun Heo 				ready = 0;
3593aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3594aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3595aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3596aa2731adSTejun Heo 				ready = 0;
3597aa2731adSTejun Heo 		}
3598aa2731adSTejun Heo 
3599aa2731adSTejun Heo 		if (ready)
3600aa2731adSTejun Heo 			return ready;
3601aa2731adSTejun Heo 		if (time_after(now, deadline))
3602aa2731adSTejun Heo 			return -EBUSY;
3603aa2731adSTejun Heo 
3604aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3605aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3606aa2731adSTejun Heo 			ata_link_printk(link, KERN_WARNING,
3607aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3608aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3609aa2731adSTejun Heo 			warned = 1;
3610aa2731adSTejun Heo 		}
3611aa2731adSTejun Heo 
3612aa2731adSTejun Heo 		msleep(50);
3613aa2731adSTejun Heo 	}
3614aa2731adSTejun Heo }
3615aa2731adSTejun Heo 
3616aa2731adSTejun Heo /**
3617aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3618aa2731adSTejun Heo  *	@link: link to be waited on
3619aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3620aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3621aa2731adSTejun Heo  *
3622aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3623aa2731adSTejun Heo  *
3624aa2731adSTejun Heo  *	LOCKING:
3625aa2731adSTejun Heo  *	EH context.
3626aa2731adSTejun Heo  *
3627aa2731adSTejun Heo  *	RETURNS:
3628aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3629aa2731adSTejun Heo  */
36302b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3631aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3632aa2731adSTejun Heo {
3633341c2c95STejun Heo 	msleep(ATA_WAIT_AFTER_RESET);
3634aa2731adSTejun Heo 
3635aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3636aa2731adSTejun Heo }
3637aa2731adSTejun Heo 
3638aa2731adSTejun Heo /**
3639936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3640936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3641c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3642d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3643c6fd2807SJeff Garzik  *
3644936fd732STejun Heo *	Make sure SStatus of @link reaches stable state, determined by
3645c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3646c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3647d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3648d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3649c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3650c6fd2807SJeff Garzik  *
3651d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3652d4b2bab4STejun Heo  *	two is used.
3653d4b2bab4STejun Heo  *
3654c6fd2807SJeff Garzik  *	LOCKING:
3655c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3656c6fd2807SJeff Garzik  *
3657c6fd2807SJeff Garzik  *	RETURNS:
3658c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3659c6fd2807SJeff Garzik  */
3660936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3661d4b2bab4STejun Heo 		       unsigned long deadline)
3662c6fd2807SJeff Garzik {
3663341c2c95STejun Heo 	unsigned long interval = params[0];
3664341c2c95STejun Heo 	unsigned long duration = params[1];
3665d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3666c6fd2807SJeff Garzik 	u32 last, cur;
3667c6fd2807SJeff Garzik 	int rc;
3668c6fd2807SJeff Garzik 
3669341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3670d4b2bab4STejun Heo 	if (time_before(t, deadline))
3671d4b2bab4STejun Heo 		deadline = t;
3672d4b2bab4STejun Heo 
3673936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3674c6fd2807SJeff Garzik 		return rc;
3675c6fd2807SJeff Garzik 	cur &= 0xf;
3676c6fd2807SJeff Garzik 
3677c6fd2807SJeff Garzik 	last = cur;
3678c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3679c6fd2807SJeff Garzik 
3680c6fd2807SJeff Garzik 	while (1) {
3681341c2c95STejun Heo 		msleep(interval);
3682936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3683c6fd2807SJeff Garzik 			return rc;
3684c6fd2807SJeff Garzik 		cur &= 0xf;
3685c6fd2807SJeff Garzik 
3686c6fd2807SJeff Garzik 		/* DET stable? */
3687c6fd2807SJeff Garzik 		if (cur == last) {
3688d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3689c6fd2807SJeff Garzik 				continue;
3690341c2c95STejun Heo 			if (time_after(jiffies,
3691341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3692c6fd2807SJeff Garzik 				return 0;
3693c6fd2807SJeff Garzik 			continue;
3694c6fd2807SJeff Garzik 		}
3695c6fd2807SJeff Garzik 
3696c6fd2807SJeff Garzik 		/* unstable, start over */
3697c6fd2807SJeff Garzik 		last = cur;
3698c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3699c6fd2807SJeff Garzik 
3700f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3701f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3702f1545154STejun Heo 		 */
3703d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3704f1545154STejun Heo 			return -EPIPE;
3705c6fd2807SJeff Garzik 	}
3706c6fd2807SJeff Garzik }
3707c6fd2807SJeff Garzik 
3708c6fd2807SJeff Garzik /**
3709936fd732STejun Heo  *	sata_link_resume - resume SATA link
3710936fd732STejun Heo  *	@link: ATA link to resume SATA
3711c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3712d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3713c6fd2807SJeff Garzik  *
3714936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3715c6fd2807SJeff Garzik  *
3716c6fd2807SJeff Garzik  *	LOCKING:
3717c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3718c6fd2807SJeff Garzik  *
3719c6fd2807SJeff Garzik  *	RETURNS:
3720c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3721c6fd2807SJeff Garzik  */
3722936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3723d4b2bab4STejun Heo 		     unsigned long deadline)
3724c6fd2807SJeff Garzik {
3725ac371987STejun Heo 	u32 scontrol, serror;
3726c6fd2807SJeff Garzik 	int rc;
3727c6fd2807SJeff Garzik 
3728936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3729c6fd2807SJeff Garzik 		return rc;
3730c6fd2807SJeff Garzik 
3731c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x300;
3732c6fd2807SJeff Garzik 
3733936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3734c6fd2807SJeff Garzik 		return rc;
3735c6fd2807SJeff Garzik 
3736c6fd2807SJeff Garzik 	/* Some PHYs react badly if SStatus is pounded immediately
3737c6fd2807SJeff Garzik 	 * after resuming.  Delay 200ms before debouncing.
3738c6fd2807SJeff Garzik 	 */
3739c6fd2807SJeff Garzik 	msleep(200);
3740c6fd2807SJeff Garzik 
3741ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3742ac371987STejun Heo 		return rc;
3743ac371987STejun Heo 
3744f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3745ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3746ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3747ac371987STejun Heo 
3748f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3749c6fd2807SJeff Garzik }
3750c6fd2807SJeff Garzik 
3751c6fd2807SJeff Garzik /**
37520aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3753cc0680a5STejun Heo  *	@link: ATA link to be reset
3754d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3755c6fd2807SJeff Garzik  *
3756cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3757b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3758b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3759b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3760b8cffc6aSTejun Heo  *	should just whine, not fail.
3761c6fd2807SJeff Garzik  *
3762c6fd2807SJeff Garzik  *	LOCKING:
3763c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3764c6fd2807SJeff Garzik  *
3765c6fd2807SJeff Garzik  *	RETURNS:
3766c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3767c6fd2807SJeff Garzik  */
37680aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3769c6fd2807SJeff Garzik {
3770cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3771936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3772c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3773c6fd2807SJeff Garzik 	int rc;
3774c6fd2807SJeff Garzik 
3775c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3776c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3777c6fd2807SJeff Garzik 		return 0;
3778c6fd2807SJeff Garzik 
3779936fd732STejun Heo 	/* if SATA, resume link */
3780a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3781936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3782b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3783b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3784cc0680a5STejun Heo 			ata_link_printk(link, KERN_WARNING, "failed to resume "
3785c6fd2807SJeff Garzik 					"link for reset (errno=%d)\n", rc);
3786c6fd2807SJeff Garzik 	}
3787c6fd2807SJeff Garzik 
378845db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3789b1c72916STejun Heo 	if (ata_phys_link_offline(link))
379045db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
379145db2f6cSTejun Heo 
3792c6fd2807SJeff Garzik 	return 0;
3793c6fd2807SJeff Garzik }
3794c6fd2807SJeff Garzik 
3795c6fd2807SJeff Garzik /**
3796cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3797cc0680a5STejun Heo  *	@link: link to reset
3798b6103f6dSTejun Heo  *	@timing: timing parameters { interval, duratinon, timeout } in msec
3799d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
38009dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
38019dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3802c6fd2807SJeff Garzik  *
3803cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
38049dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
38059dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
38069dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
38079dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
38089dadd45bSTejun Heo  *	responsibility.
38099dadd45bSTejun Heo  *
38109dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
38119dadd45bSTejun Heo  *	after reset.
3812c6fd2807SJeff Garzik  *
3813c6fd2807SJeff Garzik  *	LOCKING:
3814c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3815c6fd2807SJeff Garzik  *
3816c6fd2807SJeff Garzik  *	RETURNS:
3817c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3818c6fd2807SJeff Garzik  */
3819cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
38209dadd45bSTejun Heo 			unsigned long deadline,
38219dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3822c6fd2807SJeff Garzik {
3823c6fd2807SJeff Garzik 	u32 scontrol;
3824c6fd2807SJeff Garzik 	int rc;
3825c6fd2807SJeff Garzik 
3826c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3827c6fd2807SJeff Garzik 
38289dadd45bSTejun Heo 	if (online)
38299dadd45bSTejun Heo 		*online = false;
38309dadd45bSTejun Heo 
3831936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3832c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
3833c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
3834c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
3835c6fd2807SJeff Garzik 		 * and Sil3124.
3836c6fd2807SJeff Garzik 		 */
3837936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3838b6103f6dSTejun Heo 			goto out;
3839c6fd2807SJeff Garzik 
3840cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
3841c6fd2807SJeff Garzik 
3842936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3843b6103f6dSTejun Heo 			goto out;
3844c6fd2807SJeff Garzik 
3845936fd732STejun Heo 		sata_set_spd(link);
3846c6fd2807SJeff Garzik 	}
3847c6fd2807SJeff Garzik 
3848c6fd2807SJeff Garzik 	/* issue phy wake/reset */
3849936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3850b6103f6dSTejun Heo 		goto out;
3851c6fd2807SJeff Garzik 
3852c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
3853c6fd2807SJeff Garzik 
3854936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3855b6103f6dSTejun Heo 		goto out;
3856c6fd2807SJeff Garzik 
3857c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3858c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
3859c6fd2807SJeff Garzik 	 */
3860c6fd2807SJeff Garzik 	msleep(1);
3861c6fd2807SJeff Garzik 
3862936fd732STejun Heo 	/* bring link back */
3863936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
38649dadd45bSTejun Heo 	if (rc)
38659dadd45bSTejun Heo 		goto out;
38669dadd45bSTejun Heo 	/* if link is offline nothing more to do */
3867b1c72916STejun Heo 	if (ata_phys_link_offline(link))
38689dadd45bSTejun Heo 		goto out;
38699dadd45bSTejun Heo 
38709dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
38719dadd45bSTejun Heo 	if (online)
38729dadd45bSTejun Heo 		*online = true;
38739dadd45bSTejun Heo 
3874071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
38759dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
38769dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
38779dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
38789dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
38799dadd45bSTejun Heo 		 */
38809dadd45bSTejun Heo 		if (check_ready) {
38819dadd45bSTejun Heo 			unsigned long pmp_deadline;
38829dadd45bSTejun Heo 
3883341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
3884341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
38859dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
38869dadd45bSTejun Heo 				pmp_deadline = deadline;
38879dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
38889dadd45bSTejun Heo 		}
38899dadd45bSTejun Heo 		rc = -EAGAIN;
38909dadd45bSTejun Heo 		goto out;
38919dadd45bSTejun Heo 	}
38929dadd45bSTejun Heo 
38939dadd45bSTejun Heo 	rc = 0;
38949dadd45bSTejun Heo 	if (check_ready)
38959dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
3896b6103f6dSTejun Heo  out:
38970cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
38980cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
38990cbf0711STejun Heo 		if (online)
39000cbf0711STejun Heo 			*online = false;
39019dadd45bSTejun Heo 		ata_link_printk(link, KERN_ERR,
39029dadd45bSTejun Heo 				"COMRESET failed (errno=%d)\n", rc);
39030cbf0711STejun Heo 	}
3904b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
3905b6103f6dSTejun Heo 	return rc;
3906b6103f6dSTejun Heo }
3907b6103f6dSTejun Heo 
3908b6103f6dSTejun Heo /**
390957c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
391057c9efdfSTejun Heo  *	@link: link to reset
391157c9efdfSTejun Heo  *	@class: resulting class of attached device
391257c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
391357c9efdfSTejun Heo  *
391457c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
391557c9efdfSTejun Heo  *
391657c9efdfSTejun Heo  *	LOCKING:
391757c9efdfSTejun Heo  *	Kernel thread context (may sleep)
391857c9efdfSTejun Heo  *
391957c9efdfSTejun Heo  *	RETURNS:
392057c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
392157c9efdfSTejun Heo  */
392257c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
392357c9efdfSTejun Heo 		       unsigned long deadline)
392457c9efdfSTejun Heo {
392557c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
392657c9efdfSTejun Heo 	bool online;
392757c9efdfSTejun Heo 	int rc;
392857c9efdfSTejun Heo 
392957c9efdfSTejun Heo 	/* do hardreset */
393057c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
393157c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
393257c9efdfSTejun Heo }
393357c9efdfSTejun Heo 
393457c9efdfSTejun Heo /**
3935203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3936cc0680a5STejun Heo  *	@link: the target ata_link
3937c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3938c6fd2807SJeff Garzik  *
3939c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3940c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3941c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3942c6fd2807SJeff Garzik  *
3943c6fd2807SJeff Garzik  *	LOCKING:
3944c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3945c6fd2807SJeff Garzik  */
3946203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3947c6fd2807SJeff Garzik {
3948f046519fSTejun Heo 	u32 serror;
3949f046519fSTejun Heo 
3950c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3951c6fd2807SJeff Garzik 
3952f046519fSTejun Heo 	/* reset complete, clear SError */
3953f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3954f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3955f046519fSTejun Heo 
3956c6fd2807SJeff Garzik 	/* print link status */
3957936fd732STejun Heo 	sata_print_link_status(link);
3958c6fd2807SJeff Garzik 
3959c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
3960c6fd2807SJeff Garzik }
3961c6fd2807SJeff Garzik 
3962c6fd2807SJeff Garzik /**
3963c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3964c6fd2807SJeff Garzik  *	@dev: device to compare against
3965c6fd2807SJeff Garzik  *	@new_class: class of the new device
3966c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3967c6fd2807SJeff Garzik  *
3968c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3969c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3970c6fd2807SJeff Garzik  *	@new_id.
3971c6fd2807SJeff Garzik  *
3972c6fd2807SJeff Garzik  *	LOCKING:
3973c6fd2807SJeff Garzik  *	None.
3974c6fd2807SJeff Garzik  *
3975c6fd2807SJeff Garzik  *	RETURNS:
3976c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3977c6fd2807SJeff Garzik  */
3978c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3979c6fd2807SJeff Garzik 			       const u16 *new_id)
3980c6fd2807SJeff Garzik {
3981c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3982a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3983a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3984c6fd2807SJeff Garzik 
3985c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3986c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3987c6fd2807SJeff Garzik 			       dev->class, new_class);
3988c6fd2807SJeff Garzik 		return 0;
3989c6fd2807SJeff Garzik 	}
3990c6fd2807SJeff Garzik 
3991a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3992a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3993a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3994a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3995c6fd2807SJeff Garzik 
3996c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3997c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3998c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", model[0], model[1]);
3999c6fd2807SJeff Garzik 		return 0;
4000c6fd2807SJeff Garzik 	}
4001c6fd2807SJeff Garzik 
4002c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
4003c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
4004c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", serial[0], serial[1]);
4005c6fd2807SJeff Garzik 		return 0;
4006c6fd2807SJeff Garzik 	}
4007c6fd2807SJeff Garzik 
4008c6fd2807SJeff Garzik 	return 1;
4009c6fd2807SJeff Garzik }
4010c6fd2807SJeff Garzik 
4011c6fd2807SJeff Garzik /**
4012fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
40133fae450cSHenrik Kretzschmar  *	@dev: target ATA device
4014bff04647STejun Heo  *	@readid_flags: read ID flags
4015c6fd2807SJeff Garzik  *
4016c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
4017c6fd2807SJeff Garzik  *	the port.
4018c6fd2807SJeff Garzik  *
4019c6fd2807SJeff Garzik  *	LOCKING:
4020c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4021c6fd2807SJeff Garzik  *
4022c6fd2807SJeff Garzik  *	RETURNS:
4023c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
4024c6fd2807SJeff Garzik  */
4025fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4026c6fd2807SJeff Garzik {
4027c6fd2807SJeff Garzik 	unsigned int class = dev->class;
40289af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
4029c6fd2807SJeff Garzik 	int rc;
4030c6fd2807SJeff Garzik 
4031c6fd2807SJeff Garzik 	/* read ID data */
4032bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
4033c6fd2807SJeff Garzik 	if (rc)
4034fe30911bSTejun Heo 		return rc;
4035c6fd2807SJeff Garzik 
4036c6fd2807SJeff Garzik 	/* is the device still there? */
4037fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
4038fe30911bSTejun Heo 		return -ENODEV;
4039c6fd2807SJeff Garzik 
4040c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4041fe30911bSTejun Heo 	return 0;
4042fe30911bSTejun Heo }
4043fe30911bSTejun Heo 
4044fe30911bSTejun Heo /**
4045fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
4046fe30911bSTejun Heo  *	@dev: device to revalidate
4047422c9daaSTejun Heo  *	@new_class: new class code
4048fe30911bSTejun Heo  *	@readid_flags: read ID flags
4049fe30911bSTejun Heo  *
4050fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
4051fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
4052fe30911bSTejun Heo  *
4053fe30911bSTejun Heo  *	LOCKING:
4054fe30911bSTejun Heo  *	Kernel thread context (may sleep)
4055fe30911bSTejun Heo  *
4056fe30911bSTejun Heo  *	RETURNS:
4057fe30911bSTejun Heo  *	0 on success, negative errno otherwise
4058fe30911bSTejun Heo  */
4059422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4060422c9daaSTejun Heo 		       unsigned int readid_flags)
4061fe30911bSTejun Heo {
40626ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
4063fe30911bSTejun Heo 	int rc;
4064fe30911bSTejun Heo 
4065fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
4066fe30911bSTejun Heo 		return -ENODEV;
4067fe30911bSTejun Heo 
4068422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4069422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
4070422c9daaSTejun Heo 	    new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
4071422c9daaSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
4072422c9daaSTejun Heo 			       dev->class, new_class);
4073422c9daaSTejun Heo 		rc = -ENODEV;
4074422c9daaSTejun Heo 		goto fail;
4075422c9daaSTejun Heo 	}
4076422c9daaSTejun Heo 
4077fe30911bSTejun Heo 	/* re-read ID */
4078fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
4079fe30911bSTejun Heo 	if (rc)
4080fe30911bSTejun Heo 		goto fail;
4081c6fd2807SJeff Garzik 
4082c6fd2807SJeff Garzik 	/* configure device according to the new ID */
4083efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
40846ddcd3b0STejun Heo 	if (rc)
40856ddcd3b0STejun Heo 		goto fail;
40866ddcd3b0STejun Heo 
40876ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
4088b54eebd6STejun Heo 	if (dev->class == ATA_DEV_ATA && n_sectors &&
4089b54eebd6STejun Heo 	    dev->n_sectors != n_sectors) {
40906ddcd3b0STejun Heo 		ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
40916ddcd3b0STejun Heo 			       "%llu != %llu\n",
40926ddcd3b0STejun Heo 			       (unsigned long long)n_sectors,
40936ddcd3b0STejun Heo 			       (unsigned long long)dev->n_sectors);
40948270bec4STejun Heo 
40958270bec4STejun Heo 		/* restore original n_sectors */
40968270bec4STejun Heo 		dev->n_sectors = n_sectors;
40978270bec4STejun Heo 
40986ddcd3b0STejun Heo 		rc = -ENODEV;
40996ddcd3b0STejun Heo 		goto fail;
41006ddcd3b0STejun Heo 	}
41016ddcd3b0STejun Heo 
4102c6fd2807SJeff Garzik 	return 0;
4103c6fd2807SJeff Garzik 
4104c6fd2807SJeff Garzik  fail:
4105c6fd2807SJeff Garzik 	ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
4106c6fd2807SJeff Garzik 	return rc;
4107c6fd2807SJeff Garzik }
4108c6fd2807SJeff Garzik 
41096919a0a6SAlan Cox struct ata_blacklist_entry {
41106919a0a6SAlan Cox 	const char *model_num;
41116919a0a6SAlan Cox 	const char *model_rev;
41126919a0a6SAlan Cox 	unsigned long horkage;
41136919a0a6SAlan Cox };
41146919a0a6SAlan Cox 
41156919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
41166919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
41176919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
41186919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
41196919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
41206919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
41216919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
41226919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
41236919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
41246919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
41256919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
41266919a0a6SAlan Cox 	{ "CRD-8480B",		NULL,		ATA_HORKAGE_NODMA },
41276919a0a6SAlan Cox 	{ "CRD-8482B",		NULL,		ATA_HORKAGE_NODMA },
41286919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
41296919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
41306919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
41316919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
41326919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
41336919a0a6SAlan Cox 	{ "HITACHI CDR-8335",	NULL,		ATA_HORKAGE_NODMA },
41346919a0a6SAlan Cox 	{ "HITACHI CDR-8435",	NULL,		ATA_HORKAGE_NODMA },
41356919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
41366919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
41376919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
41386919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
41396919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
41406919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
41416919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
41426919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
41436919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
41446919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
41456919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
414639f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
41473af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
414850af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
41496919a0a6SAlan Cox 
415018d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
415140a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
41526a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
415318d6e9d5SAlbert Lee 
41546919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
41556919a0a6SAlan Cox 
41566919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
41576919a0a6SAlan Cox 	/* NCQ is slow */
41586919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4159459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
416009125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
416109125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
41627acfaf30SPaul Rolland 	/* NCQ is broken */
4163539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
41640e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4165da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4166e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
41675ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4168539cc7c7SJeff Garzik 
4169ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
4170d10d491fSTejun Heo 	{ "ST31500341AS",	"SD15",		ATA_HORKAGE_NONCQ |
4171ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4172d10d491fSTejun Heo 	{ "ST31500341AS",	"SD16",		ATA_HORKAGE_NONCQ |
4173ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4174d10d491fSTejun Heo 	{ "ST31500341AS",	"SD17",		ATA_HORKAGE_NONCQ |
4175ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4176d10d491fSTejun Heo 	{ "ST31500341AS",	"SD18",		ATA_HORKAGE_NONCQ |
4177ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4178d10d491fSTejun Heo 	{ "ST31500341AS",	"SD19",		ATA_HORKAGE_NONCQ |
4179ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4180d10d491fSTejun Heo 
4181d10d491fSTejun Heo 	{ "ST31000333AS",	"SD15",		ATA_HORKAGE_NONCQ |
4182d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4183d10d491fSTejun Heo 	{ "ST31000333AS",	"SD16",		ATA_HORKAGE_NONCQ |
4184d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4185d10d491fSTejun Heo 	{ "ST31000333AS",	"SD17",		ATA_HORKAGE_NONCQ |
4186d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4187d10d491fSTejun Heo 	{ "ST31000333AS",	"SD18",		ATA_HORKAGE_NONCQ |
4188d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4189d10d491fSTejun Heo 	{ "ST31000333AS",	"SD19",		ATA_HORKAGE_NONCQ |
4190d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4191d10d491fSTejun Heo 
4192d10d491fSTejun Heo 	{ "ST3640623AS",	"SD15",		ATA_HORKAGE_NONCQ |
4193d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4194d10d491fSTejun Heo 	{ "ST3640623AS",	"SD16",		ATA_HORKAGE_NONCQ |
4195d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4196d10d491fSTejun Heo 	{ "ST3640623AS",	"SD17",		ATA_HORKAGE_NONCQ |
4197d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4198d10d491fSTejun Heo 	{ "ST3640623AS",	"SD18",		ATA_HORKAGE_NONCQ |
4199d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4200d10d491fSTejun Heo 	{ "ST3640623AS",	"SD19",		ATA_HORKAGE_NONCQ |
4201d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4202d10d491fSTejun Heo 
4203d10d491fSTejun Heo 	{ "ST3640323AS",	"SD15",		ATA_HORKAGE_NONCQ |
4204d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4205d10d491fSTejun Heo 	{ "ST3640323AS",	"SD16",		ATA_HORKAGE_NONCQ |
4206d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4207d10d491fSTejun Heo 	{ "ST3640323AS",	"SD17",		ATA_HORKAGE_NONCQ |
4208d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4209d10d491fSTejun Heo 	{ "ST3640323AS",	"SD18",		ATA_HORKAGE_NONCQ |
4210d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4211d10d491fSTejun Heo 	{ "ST3640323AS",	"SD19",		ATA_HORKAGE_NONCQ |
4212d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4213d10d491fSTejun Heo 
4214d10d491fSTejun Heo 	{ "ST3320813AS",	"SD15",		ATA_HORKAGE_NONCQ |
4215d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4216d10d491fSTejun Heo 	{ "ST3320813AS",	"SD16",		ATA_HORKAGE_NONCQ |
4217d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4218d10d491fSTejun Heo 	{ "ST3320813AS",	"SD17",		ATA_HORKAGE_NONCQ |
4219d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4220d10d491fSTejun Heo 	{ "ST3320813AS",	"SD18",		ATA_HORKAGE_NONCQ |
4221d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4222d10d491fSTejun Heo 	{ "ST3320813AS",	"SD19",		ATA_HORKAGE_NONCQ |
4223d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4224d10d491fSTejun Heo 
4225d10d491fSTejun Heo 	{ "ST3320613AS",	"SD15",		ATA_HORKAGE_NONCQ |
4226d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4227d10d491fSTejun Heo 	{ "ST3320613AS",	"SD16",		ATA_HORKAGE_NONCQ |
4228d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4229d10d491fSTejun Heo 	{ "ST3320613AS",	"SD17",		ATA_HORKAGE_NONCQ |
4230d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4231d10d491fSTejun Heo 	{ "ST3320613AS",	"SD18",		ATA_HORKAGE_NONCQ |
4232d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4233d10d491fSTejun Heo 	{ "ST3320613AS",	"SD19",		ATA_HORKAGE_NONCQ |
4234ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4235ac70a964STejun Heo 
423636e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
423736e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
423836e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
423936e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
424036e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
42416919a0a6SAlan Cox 
424216c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
424316c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
424416c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
424516c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
424616c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
42476919a0a6SAlan Cox 
424893328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
424993328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
425093328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4251b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
425293328e11SAlan Cox 
42536bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
42546bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4255a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
4256a79067e5SAlan Cox 	{ "TSSTcorp CDDVDW SH-S202H", "SB00",	  ATA_HORKAGE_IVB, },
4257a79067e5SAlan Cox 	{ "TSSTcorp CDDVDW SH-S202H", "SB01",	  ATA_HORKAGE_IVB, },
42586bbfd53dSAlan Cox 	{ "TSSTcorp CDDVDW SH-S202J", "SB00",	  ATA_HORKAGE_IVB, },
4259e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202J", "SB01",	  ATA_HORKAGE_IVB, },
4260e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202N", "SB00",	  ATA_HORKAGE_IVB, },
4261e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202N", "SB01",	  ATA_HORKAGE_IVB, },
42626bbfd53dSAlan Cox 
42639ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
42649ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
42659ce8e307SJens Axboe 
42669062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
42679062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
42689062712fSTejun Heo 
42696919a0a6SAlan Cox 	/* End Marker */
42706919a0a6SAlan Cox 	{ }
4271c6fd2807SJeff Garzik };
4272c6fd2807SJeff Garzik 
4273741b7763SAdrian Bunk static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
4274539cc7c7SJeff Garzik {
4275539cc7c7SJeff Garzik 	const char *p;
4276539cc7c7SJeff Garzik 	int len;
4277539cc7c7SJeff Garzik 
4278539cc7c7SJeff Garzik 	/*
4279539cc7c7SJeff Garzik 	 * check for trailing wildcard: *\0
4280539cc7c7SJeff Garzik 	 */
4281539cc7c7SJeff Garzik 	p = strchr(patt, wildchar);
4282539cc7c7SJeff Garzik 	if (p && ((*(p + 1)) == 0))
4283539cc7c7SJeff Garzik 		len = p - patt;
4284317b50b8SAndrew Paprocki 	else {
4285539cc7c7SJeff Garzik 		len = strlen(name);
4286317b50b8SAndrew Paprocki 		if (!len) {
4287317b50b8SAndrew Paprocki 			if (!*patt)
4288317b50b8SAndrew Paprocki 				return 0;
4289317b50b8SAndrew Paprocki 			return -1;
4290317b50b8SAndrew Paprocki 		}
4291317b50b8SAndrew Paprocki 	}
4292539cc7c7SJeff Garzik 
4293539cc7c7SJeff Garzik 	return strncmp(patt, name, len);
4294539cc7c7SJeff Garzik }
4295539cc7c7SJeff Garzik 
429675683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4297c6fd2807SJeff Garzik {
42988bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
42998bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
43006919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4301c6fd2807SJeff Garzik 
43028bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
43038bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4304c6fd2807SJeff Garzik 
43056919a0a6SAlan Cox 	while (ad->model_num) {
4306539cc7c7SJeff Garzik 		if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
43076919a0a6SAlan Cox 			if (ad->model_rev == NULL)
43086919a0a6SAlan Cox 				return ad->horkage;
4309539cc7c7SJeff Garzik 			if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
43106919a0a6SAlan Cox 				return ad->horkage;
4311c6fd2807SJeff Garzik 		}
43126919a0a6SAlan Cox 		ad++;
4313c6fd2807SJeff Garzik 	}
4314c6fd2807SJeff Garzik 	return 0;
4315c6fd2807SJeff Garzik }
4316c6fd2807SJeff Garzik 
43176919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
43186919a0a6SAlan Cox {
43196919a0a6SAlan Cox 	/* We don't support polling DMA.
43206919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
43216919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
43226919a0a6SAlan Cox 	 */
43239af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
43246919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
43256919a0a6SAlan Cox 		return 1;
432675683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
43276919a0a6SAlan Cox }
43286919a0a6SAlan Cox 
4329c6fd2807SJeff Garzik /**
43306bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
43316bbfd53dSAlan Cox  *	@dev: device
43326bbfd53dSAlan Cox  *
43336bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
43346bbfd53dSAlan Cox  *	who can't follow the documentation.
43356bbfd53dSAlan Cox  */
43366bbfd53dSAlan Cox 
43376bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
43386bbfd53dSAlan Cox {
43396bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
43406bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
43416bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
43426bbfd53dSAlan Cox }
43436bbfd53dSAlan Cox 
43446bbfd53dSAlan Cox /**
434515a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
434615a5551cSAlan Cox  *	@ap: port to consider
434715a5551cSAlan Cox  *
434815a5551cSAlan Cox  *	This function encapsulates the policy for speed management
434915a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
435015a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
435115a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
435215a5551cSAlan Cox  *	impacts hotplug at all).
435315a5551cSAlan Cox  *
435415a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
435515a5551cSAlan Cox  */
435615a5551cSAlan Cox 
435715a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
435815a5551cSAlan Cox {
435915a5551cSAlan Cox 	struct ata_link *link;
436015a5551cSAlan Cox 	struct ata_device *dev;
436115a5551cSAlan Cox 
43624a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
436315a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
436415a5551cSAlan Cox 		return 1;
43654a9c7b33STejun Heo 
43664a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
436715a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
436815a5551cSAlan Cox 		return 0;
43694a9c7b33STejun Heo 
43704a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
43714a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
43724a9c7b33STejun Heo 	 * isn't sure.
43734a9c7b33STejun Heo 	 */
4374f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4375f792068eSAlan Cox 		return 0;
437615a5551cSAlan Cox 
43774a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
43784a9c7b33STejun Heo 	 *
43794a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
43804a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
43814a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
43824a9c7b33STejun Heo 	 *   give a valid detect
43834a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
43844a9c7b33STejun Heo 	 *   to colour the choice
438515a5551cSAlan Cox 	 */
43861eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
43871eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
43881eca4365STejun Heo 			if (!ata_is_40wire(dev))
438915a5551cSAlan Cox 				return 0;
439015a5551cSAlan Cox 		}
439115a5551cSAlan Cox 	}
439215a5551cSAlan Cox 	return 1;
439315a5551cSAlan Cox }
439415a5551cSAlan Cox 
439515a5551cSAlan Cox /**
4396c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4397c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4398c6fd2807SJeff Garzik  *
4399c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4400c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4401c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4402c6fd2807SJeff Garzik  *	blacklist, etc...
4403c6fd2807SJeff Garzik  *
4404c6fd2807SJeff Garzik  *	LOCKING:
4405c6fd2807SJeff Garzik  *	None.
4406c6fd2807SJeff Garzik  */
4407c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4408c6fd2807SJeff Garzik {
44099af5c9c9STejun Heo 	struct ata_link *link = dev->link;
44109af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4411cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4412c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4413c6fd2807SJeff Garzik 
4414c6fd2807SJeff Garzik 	/* controller modes available */
4415c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4416c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4417c6fd2807SJeff Garzik 
44188343f889SRobert Hancock 	/* drive modes available */
4419c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4420c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4421c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4422c6fd2807SJeff Garzik 
4423b352e57dSAlan Cox 	/*
4424b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4425b352e57dSAlan Cox 	 *	cable
4426b352e57dSAlan Cox 	 */
4427b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4428b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4429b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4430b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4431b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4432b352e57dSAlan Cox 	}
4433b352e57dSAlan Cox 
4434c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4435c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4436c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
4437c6fd2807SJeff Garzik 			       "device is on DMA blacklist, disabling DMA\n");
4438c6fd2807SJeff Garzik 	}
4439c6fd2807SJeff Garzik 
444014d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
444114d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4442c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4443c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4444c6fd2807SJeff Garzik 			       "other device, disabling DMA\n");
4445c6fd2807SJeff Garzik 	}
4446c6fd2807SJeff Garzik 
4447e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4448e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4449e424675fSJeff Garzik 
4450c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4451a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4452c6fd2807SJeff Garzik 
44538343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
44548343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
44558343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
44568343f889SRobert Hancock 	 * solely limited by the cable.
44578343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
44588343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
44598343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
44608343f889SRobert Hancock 	 */
44618343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
44628343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
446315a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
44648343f889SRobert Hancock 			ata_dev_printk(dev, KERN_WARNING,
44658343f889SRobert Hancock 				 "limited to UDMA/33 due to 40-wire cable\n");
44668343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
44678343f889SRobert Hancock 		}
44688343f889SRobert Hancock 
4469c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4470c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4471c6fd2807SJeff Garzik }
4472c6fd2807SJeff Garzik 
4473c6fd2807SJeff Garzik /**
4474c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4475c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4476c6fd2807SJeff Garzik  *
4477c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4478c6fd2807SJeff Garzik  *	on port @ap.
4479c6fd2807SJeff Garzik  *
4480c6fd2807SJeff Garzik  *	LOCKING:
4481c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4482c6fd2807SJeff Garzik  *
4483c6fd2807SJeff Garzik  *	RETURNS:
4484c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4485c6fd2807SJeff Garzik  */
4486c6fd2807SJeff Garzik 
4487c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4488c6fd2807SJeff Garzik {
4489c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4490c6fd2807SJeff Garzik 	unsigned int err_mask;
4491c6fd2807SJeff Garzik 
4492c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4493c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4494c6fd2807SJeff Garzik 
4495464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4496464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4497464cf177STejun Heo 	 */
4498c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4499c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4500c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4501464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4502c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4503b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4504b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4505c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4506b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4507b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4508b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4509b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4510b9f8ab2dSAlan Cox 		return 0;
4511c6fd2807SJeff Garzik 
45122b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4513c6fd2807SJeff Garzik 
4514c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4515c6fd2807SJeff Garzik 	return err_mask;
4516c6fd2807SJeff Garzik }
4517c6fd2807SJeff Garzik /**
4518218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
45199f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
45209f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4521218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
45229f45cbd3SKristen Carlson Accardi  *
45239f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4524218f3d30SJeff Garzik  *	on port @ap with sector count
45259f45cbd3SKristen Carlson Accardi  *
45269f45cbd3SKristen Carlson Accardi  *	LOCKING:
45279f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
45289f45cbd3SKristen Carlson Accardi  *
45299f45cbd3SKristen Carlson Accardi  *	RETURNS:
45309f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
45319f45cbd3SKristen Carlson Accardi  */
4532218f3d30SJeff Garzik static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4533218f3d30SJeff Garzik 					u8 feature)
45349f45cbd3SKristen Carlson Accardi {
45359f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
45369f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
45379f45cbd3SKristen Carlson Accardi 
45389f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
45399f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
45409f45cbd3SKristen Carlson Accardi 
45419f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
45429f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
45439f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
45449f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
45459f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4546218f3d30SJeff Garzik 	tf.nsect = feature;
45479f45cbd3SKristen Carlson Accardi 
45482b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
45499f45cbd3SKristen Carlson Accardi 
45509f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
45519f45cbd3SKristen Carlson Accardi 	return err_mask;
45529f45cbd3SKristen Carlson Accardi }
45539f45cbd3SKristen Carlson Accardi 
45549f45cbd3SKristen Carlson Accardi /**
4555c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4556c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4557c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4558c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4559c6fd2807SJeff Garzik  *
4560c6fd2807SJeff Garzik  *	LOCKING:
4561c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4562c6fd2807SJeff Garzik  *
4563c6fd2807SJeff Garzik  *	RETURNS:
4564c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4565c6fd2807SJeff Garzik  */
4566c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4567c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4568c6fd2807SJeff Garzik {
4569c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4570c6fd2807SJeff Garzik 	unsigned int err_mask;
4571c6fd2807SJeff Garzik 
4572c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4573c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4574c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4575c6fd2807SJeff Garzik 
4576c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4577c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4578c6fd2807SJeff Garzik 
4579c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4580c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4581c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4582c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4583c6fd2807SJeff Garzik 	tf.nsect = sectors;
4584c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4585c6fd2807SJeff Garzik 
45862b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
458718b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
458818b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
458918b2466cSAlan Cox 	   drive reported working geometry */
459018b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
459118b2466cSAlan Cox 		err_mask = 0;
4592c6fd2807SJeff Garzik 
4593c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4594c6fd2807SJeff Garzik 	return err_mask;
4595c6fd2807SJeff Garzik }
4596c6fd2807SJeff Garzik 
4597c6fd2807SJeff Garzik /**
4598c6fd2807SJeff Garzik  *	ata_sg_clean - Unmap DMA memory associated with command
4599c6fd2807SJeff Garzik  *	@qc: Command containing DMA memory to be released
4600c6fd2807SJeff Garzik  *
4601c6fd2807SJeff Garzik  *	Unmap all mapped DMA memory associated with this command.
4602c6fd2807SJeff Garzik  *
4603c6fd2807SJeff Garzik  *	LOCKING:
4604cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4605c6fd2807SJeff Garzik  */
460670e6ad0cSTejun Heo void ata_sg_clean(struct ata_queued_cmd *qc)
4607c6fd2807SJeff Garzik {
4608c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4609ff2aeb1eSTejun Heo 	struct scatterlist *sg = qc->sg;
4610c6fd2807SJeff Garzik 	int dir = qc->dma_dir;
4611c6fd2807SJeff Garzik 
4612efcb3cf7STejun Heo 	WARN_ON_ONCE(sg == NULL);
4613c6fd2807SJeff Garzik 
4614dde20207SJames Bottomley 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4615c6fd2807SJeff Garzik 
4616dde20207SJames Bottomley 	if (qc->n_elem)
46175825627cSFUJITA Tomonori 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4618c6fd2807SJeff Garzik 
4619c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
4620ff2aeb1eSTejun Heo 	qc->sg = NULL;
4621c6fd2807SJeff Garzik }
4622c6fd2807SJeff Garzik 
4623c6fd2807SJeff Garzik /**
46245895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4625c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4626c6fd2807SJeff Garzik  *
4627c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4628c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4629c6fd2807SJeff Garzik  *	supplied PACKET command.
4630c6fd2807SJeff Garzik  *
4631c6fd2807SJeff Garzik  *	LOCKING:
4632cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4633c6fd2807SJeff Garzik  *
4634c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4635c6fd2807SJeff Garzik  *               nonzero otherwise
4636c6fd2807SJeff Garzik  */
46375895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4638c6fd2807SJeff Garzik {
4639c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4640c6fd2807SJeff Garzik 
4641b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4642b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4643b9a4197eSTejun Heo 	 */
46446a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
46456a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
46466f23a31dSAlbert Lee 		return 1;
46476f23a31dSAlbert Lee 
4648c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4649b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4650c6fd2807SJeff Garzik 
4651b9a4197eSTejun Heo 	return 0;
4652c6fd2807SJeff Garzik }
4653b9a4197eSTejun Heo 
4654c6fd2807SJeff Garzik /**
465531cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
465631cc23b3STejun Heo  *	@qc: ATA command in question
465731cc23b3STejun Heo  *
465831cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
465931cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
466031cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
466131cc23b3STejun Heo  *	whether a new command @qc can be issued.
466231cc23b3STejun Heo  *
466331cc23b3STejun Heo  *	LOCKING:
466431cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
466531cc23b3STejun Heo  *
466631cc23b3STejun Heo  *	RETURNS:
466731cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
466831cc23b3STejun Heo  */
466931cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
467031cc23b3STejun Heo {
467131cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
467231cc23b3STejun Heo 
467331cc23b3STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
467431cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
467531cc23b3STejun Heo 			return 0;
467631cc23b3STejun Heo 	} else {
467731cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
467831cc23b3STejun Heo 			return 0;
467931cc23b3STejun Heo 	}
468031cc23b3STejun Heo 
468131cc23b3STejun Heo 	return ATA_DEFER_LINK;
468231cc23b3STejun Heo }
468331cc23b3STejun Heo 
4684c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4685c6fd2807SJeff Garzik 
4686c6fd2807SJeff Garzik /**
4687c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4688c6fd2807SJeff Garzik  *	@qc: Command to be associated
4689c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4690c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4691c6fd2807SJeff Garzik  *
4692c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4693c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4694c6fd2807SJeff Garzik  *	elements.
4695c6fd2807SJeff Garzik  *
4696c6fd2807SJeff Garzik  *	LOCKING:
4697cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4698c6fd2807SJeff Garzik  */
4699c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4700c6fd2807SJeff Garzik 		 unsigned int n_elem)
4701c6fd2807SJeff Garzik {
4702ff2aeb1eSTejun Heo 	qc->sg = sg;
4703c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4704ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4705ff2aeb1eSTejun Heo }
4706ff2aeb1eSTejun Heo 
4707c6fd2807SJeff Garzik /**
4708c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4709c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4710c6fd2807SJeff Garzik  *
4711c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4712c6fd2807SJeff Garzik  *
4713c6fd2807SJeff Garzik  *	LOCKING:
4714cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4715c6fd2807SJeff Garzik  *
4716c6fd2807SJeff Garzik  *	RETURNS:
4717c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4718c6fd2807SJeff Garzik  *
4719c6fd2807SJeff Garzik  */
4720c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4721c6fd2807SJeff Garzik {
4722c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4723dde20207SJames Bottomley 	unsigned int n_elem;
4724c6fd2807SJeff Garzik 
472544877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4726c6fd2807SJeff Garzik 
4727dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4728dde20207SJames Bottomley 	if (n_elem < 1)
4729c6fd2807SJeff Garzik 		return -1;
4730c6fd2807SJeff Garzik 
4731dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
47325825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4733dde20207SJames Bottomley 	qc->n_elem = n_elem;
4734f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4735c6fd2807SJeff Garzik 
4736c6fd2807SJeff Garzik 	return 0;
4737c6fd2807SJeff Garzik }
4738c6fd2807SJeff Garzik 
4739c6fd2807SJeff Garzik /**
4740c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4741c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4742c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4743c6fd2807SJeff Garzik  *
4744c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4745c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4746c6fd2807SJeff Garzik  *	vice-versa.
4747c6fd2807SJeff Garzik  *
4748c6fd2807SJeff Garzik  *	LOCKING:
4749c6fd2807SJeff Garzik  *	Inherited from caller.
4750c6fd2807SJeff Garzik  */
4751c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4752c6fd2807SJeff Garzik {
4753c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4754c6fd2807SJeff Garzik 	unsigned int i;
4755c6fd2807SJeff Garzik 
4756c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4757c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4758c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4759c6fd2807SJeff Garzik }
4760c6fd2807SJeff Garzik 
4761c6fd2807SJeff Garzik /**
47628a8bc223STejun Heo  *	ata_qc_new - Request an available ATA command, for queueing
47635eb66fe0SRandy Dunlap  *	@ap: target port
47648a8bc223STejun Heo  *
47658a8bc223STejun Heo  *	LOCKING:
47668a8bc223STejun Heo  *	None.
47678a8bc223STejun Heo  */
47688a8bc223STejun Heo 
47698a8bc223STejun Heo static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
47708a8bc223STejun Heo {
47718a8bc223STejun Heo 	struct ata_queued_cmd *qc = NULL;
47728a8bc223STejun Heo 	unsigned int i;
47738a8bc223STejun Heo 
47748a8bc223STejun Heo 	/* no command while frozen */
47758a8bc223STejun Heo 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
47768a8bc223STejun Heo 		return NULL;
47778a8bc223STejun Heo 
47788a8bc223STejun Heo 	/* the last tag is reserved for internal command. */
47798a8bc223STejun Heo 	for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
47808a8bc223STejun Heo 		if (!test_and_set_bit(i, &ap->qc_allocated)) {
47818a8bc223STejun Heo 			qc = __ata_qc_from_tag(ap, i);
47828a8bc223STejun Heo 			break;
47838a8bc223STejun Heo 		}
47848a8bc223STejun Heo 
47858a8bc223STejun Heo 	if (qc)
47868a8bc223STejun Heo 		qc->tag = i;
47878a8bc223STejun Heo 
47888a8bc223STejun Heo 	return qc;
47898a8bc223STejun Heo }
47908a8bc223STejun Heo 
47918a8bc223STejun Heo /**
4792c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4793c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
4794c6fd2807SJeff Garzik  *
4795c6fd2807SJeff Garzik  *	LOCKING:
4796c6fd2807SJeff Garzik  *	None.
4797c6fd2807SJeff Garzik  */
4798c6fd2807SJeff Garzik 
47998a8bc223STejun Heo struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4800c6fd2807SJeff Garzik {
48019af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4802c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4803c6fd2807SJeff Garzik 
48048a8bc223STejun Heo 	qc = ata_qc_new(ap);
4805c6fd2807SJeff Garzik 	if (qc) {
4806c6fd2807SJeff Garzik 		qc->scsicmd = NULL;
4807c6fd2807SJeff Garzik 		qc->ap = ap;
4808c6fd2807SJeff Garzik 		qc->dev = dev;
4809c6fd2807SJeff Garzik 
4810c6fd2807SJeff Garzik 		ata_qc_reinit(qc);
4811c6fd2807SJeff Garzik 	}
4812c6fd2807SJeff Garzik 
4813c6fd2807SJeff Garzik 	return qc;
4814c6fd2807SJeff Garzik }
4815c6fd2807SJeff Garzik 
48168a8bc223STejun Heo /**
48178a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
48188a8bc223STejun Heo  *	@qc: Command to complete
48198a8bc223STejun Heo  *
48208a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
48218a8bc223STejun Heo  *	in case something prevents using it.
48228a8bc223STejun Heo  *
48238a8bc223STejun Heo  *	LOCKING:
48248a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
48258a8bc223STejun Heo  */
48268a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
48278a8bc223STejun Heo {
48288a8bc223STejun Heo 	struct ata_port *ap = qc->ap;
48298a8bc223STejun Heo 	unsigned int tag;
48308a8bc223STejun Heo 
4831efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
48328a8bc223STejun Heo 
48338a8bc223STejun Heo 	qc->flags = 0;
48348a8bc223STejun Heo 	tag = qc->tag;
48358a8bc223STejun Heo 	if (likely(ata_tag_valid(tag))) {
48368a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
48378a8bc223STejun Heo 		clear_bit(tag, &ap->qc_allocated);
48388a8bc223STejun Heo 	}
48398a8bc223STejun Heo }
48408a8bc223STejun Heo 
4841c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4842c6fd2807SJeff Garzik {
4843c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
48449af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4845c6fd2807SJeff Garzik 
4846efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4847efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4848c6fd2807SJeff Garzik 
4849c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4850c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4851c6fd2807SJeff Garzik 
4852c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4853da917d69STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
48549af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
4855da917d69STejun Heo 		if (!link->sactive)
4856da917d69STejun Heo 			ap->nr_active_links--;
4857da917d69STejun Heo 	} else {
48589af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4859da917d69STejun Heo 		ap->nr_active_links--;
4860da917d69STejun Heo 	}
4861da917d69STejun Heo 
4862da917d69STejun Heo 	/* clear exclusive status */
4863da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4864da917d69STejun Heo 		     ap->excl_link == link))
4865da917d69STejun Heo 		ap->excl_link = NULL;
4866c6fd2807SJeff Garzik 
4867c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4868c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4869c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4870c6fd2807SJeff Garzik 	 */
4871c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4872c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
4873c6fd2807SJeff Garzik 
4874c6fd2807SJeff Garzik 	/* call completion callback */
4875c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4876c6fd2807SJeff Garzik }
4877c6fd2807SJeff Garzik 
487839599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
487939599a53STejun Heo {
488039599a53STejun Heo 	struct ata_port *ap = qc->ap;
488139599a53STejun Heo 
488239599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
488322183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
488439599a53STejun Heo }
488539599a53STejun Heo 
488600115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
488700115e0fSTejun Heo {
488800115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
488900115e0fSTejun Heo 
489000115e0fSTejun Heo 	if (ata_tag_internal(qc->tag))
489100115e0fSTejun Heo 		return;
489200115e0fSTejun Heo 
489300115e0fSTejun Heo 	if (ata_is_nodata(qc->tf.protocol))
489400115e0fSTejun Heo 		return;
489500115e0fSTejun Heo 
489600115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
489700115e0fSTejun Heo 		return;
489800115e0fSTejun Heo 
489900115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
490000115e0fSTejun Heo }
490100115e0fSTejun Heo 
4902c6fd2807SJeff Garzik /**
4903c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4904c6fd2807SJeff Garzik  *	@qc: Command to complete
4905c6fd2807SJeff Garzik  *
4906c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA
4907c6fd2807SJeff Garzik  *	command has completed, with either an ok or not-ok status.
4908c6fd2807SJeff Garzik  *
4909c6fd2807SJeff Garzik  *	LOCKING:
4910cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4911c6fd2807SJeff Garzik  */
4912c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4913c6fd2807SJeff Garzik {
4914c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4915c6fd2807SJeff Garzik 
4916c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4917c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4918c6fd2807SJeff Garzik 	 *
4919c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4920c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4921c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4922c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4923c6fd2807SJeff Garzik 	 *
4924c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4925c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4926c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4927c6fd2807SJeff Garzik 	 * taken care of.
4928c6fd2807SJeff Garzik 	 */
4929c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
49304dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
49314dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
49324dbfa39bSTejun Heo 
4933efcb3cf7STejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
4934c6fd2807SJeff Garzik 
4935c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4936c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4937c6fd2807SJeff Garzik 
4938c6fd2807SJeff Garzik 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4939c6fd2807SJeff Garzik 			if (!ata_tag_internal(qc->tag)) {
4940c6fd2807SJeff Garzik 				/* always fill result TF for failed qc */
494139599a53STejun Heo 				fill_result_tf(qc);
4942c6fd2807SJeff Garzik 				ata_qc_schedule_eh(qc);
4943c6fd2807SJeff Garzik 				return;
4944c6fd2807SJeff Garzik 			}
4945c6fd2807SJeff Garzik 		}
4946c6fd2807SJeff Garzik 
4947c6fd2807SJeff Garzik 		/* read result TF if requested */
4948c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
494939599a53STejun Heo 			fill_result_tf(qc);
4950c6fd2807SJeff Garzik 
49514dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
49524dbfa39bSTejun Heo 		 * completion.
49534dbfa39bSTejun Heo 		 */
49544dbfa39bSTejun Heo 		switch (qc->tf.command) {
49554dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
49564dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
49574dbfa39bSTejun Heo 			    qc->tf.feature != SETFEATURES_WC_OFF)
49584dbfa39bSTejun Heo 				break;
49594dbfa39bSTejun Heo 			/* fall through */
49604dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
49614dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
49624dbfa39bSTejun Heo 			/* revalidate device */
49634dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
49644dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
49654dbfa39bSTejun Heo 			break;
4966054a5fbaSTejun Heo 
4967054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4968054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4969054a5fbaSTejun Heo 			break;
49704dbfa39bSTejun Heo 		}
49714dbfa39bSTejun Heo 
497200115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
497300115e0fSTejun Heo 			ata_verify_xfer(qc);
497400115e0fSTejun Heo 
4975c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4976c6fd2807SJeff Garzik 	} else {
4977c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4978c6fd2807SJeff Garzik 			return;
4979c6fd2807SJeff Garzik 
4980c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4981c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
498239599a53STejun Heo 			fill_result_tf(qc);
4983c6fd2807SJeff Garzik 
4984c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4985c6fd2807SJeff Garzik 	}
4986c6fd2807SJeff Garzik }
4987c6fd2807SJeff Garzik 
4988c6fd2807SJeff Garzik /**
4989c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
4990c6fd2807SJeff Garzik  *	@ap: port in question
4991c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
4992c6fd2807SJeff Garzik  *
4993c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
4994c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
4995c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
4996c6fd2807SJeff Garzik  *	and commands are completed accordingly.
4997c6fd2807SJeff Garzik  *
4998c6fd2807SJeff Garzik  *	LOCKING:
4999cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5000c6fd2807SJeff Garzik  *
5001c6fd2807SJeff Garzik  *	RETURNS:
5002c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
5003c6fd2807SJeff Garzik  */
500479f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
5005c6fd2807SJeff Garzik {
5006c6fd2807SJeff Garzik 	int nr_done = 0;
5007c6fd2807SJeff Garzik 	u32 done_mask;
5008c6fd2807SJeff Garzik 	int i;
5009c6fd2807SJeff Garzik 
5010c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
5011c6fd2807SJeff Garzik 
5012c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
5013c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5014c6fd2807SJeff Garzik 				"(%08x->%08x)\n", ap->qc_active, qc_active);
5015c6fd2807SJeff Garzik 		return -EINVAL;
5016c6fd2807SJeff Garzik 	}
5017c6fd2807SJeff Garzik 
5018c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_QUEUE; i++) {
5019c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
5020c6fd2807SJeff Garzik 
5021c6fd2807SJeff Garzik 		if (!(done_mask & (1 << i)))
5022c6fd2807SJeff Garzik 			continue;
5023c6fd2807SJeff Garzik 
5024c6fd2807SJeff Garzik 		if ((qc = ata_qc_from_tag(ap, i))) {
5025c6fd2807SJeff Garzik 			ata_qc_complete(qc);
5026c6fd2807SJeff Garzik 			nr_done++;
5027c6fd2807SJeff Garzik 		}
5028c6fd2807SJeff Garzik 	}
5029c6fd2807SJeff Garzik 
5030c6fd2807SJeff Garzik 	return nr_done;
5031c6fd2807SJeff Garzik }
5032c6fd2807SJeff Garzik 
5033c6fd2807SJeff Garzik /**
5034c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
5035c6fd2807SJeff Garzik  *	@qc: command to issue to device
5036c6fd2807SJeff Garzik  *
5037c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
5038c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
5039c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
5040c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
5041c6fd2807SJeff Garzik  *
5042c6fd2807SJeff Garzik  *	LOCKING:
5043cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5044c6fd2807SJeff Garzik  */
5045c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
5046c6fd2807SJeff Garzik {
5047c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
50489af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
5049405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
5050c6fd2807SJeff Garzik 
5051c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
5052c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
5053c6fd2807SJeff Garzik 	 * request ATAPI sense.
5054c6fd2807SJeff Garzik 	 */
5055efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5056c6fd2807SJeff Garzik 
50571973a023STejun Heo 	if (ata_is_ncq(prot)) {
5058efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive & (1 << qc->tag));
5059da917d69STejun Heo 
5060da917d69STejun Heo 		if (!link->sactive)
5061da917d69STejun Heo 			ap->nr_active_links++;
50629af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
5063c6fd2807SJeff Garzik 	} else {
5064efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
5065da917d69STejun Heo 
5066da917d69STejun Heo 		ap->nr_active_links++;
50679af5c9c9STejun Heo 		link->active_tag = qc->tag;
5068c6fd2807SJeff Garzik 	}
5069c6fd2807SJeff Garzik 
5070c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
5071c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
5072c6fd2807SJeff Garzik 
5073f92a2636STejun Heo 	/* We guarantee to LLDs that they will have at least one
5074f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5075f92a2636STejun Heo 	 */
5076ff2aeb1eSTejun Heo 	BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
5077f92a2636STejun Heo 
5078405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5079f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5080c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
5081c6fd2807SJeff Garzik 			goto sg_err;
5082c6fd2807SJeff Garzik 
5083cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5084054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5085cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5086054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5087054a5fbaSTejun Heo 		ata_link_abort(link);
5088054a5fbaSTejun Heo 		return;
5089054a5fbaSTejun Heo 	}
5090054a5fbaSTejun Heo 
5091c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5092c6fd2807SJeff Garzik 
5093c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5094c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5095c6fd2807SJeff Garzik 		goto err;
5096c6fd2807SJeff Garzik 	return;
5097c6fd2807SJeff Garzik 
5098c6fd2807SJeff Garzik sg_err:
5099c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5100c6fd2807SJeff Garzik err:
5101c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5102c6fd2807SJeff Garzik }
5103c6fd2807SJeff Garzik 
5104c6fd2807SJeff Garzik /**
5105c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5106936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5107c6fd2807SJeff Garzik  *
5108936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5109c6fd2807SJeff Garzik  *
5110c6fd2807SJeff Garzik  *	LOCKING:
5111c6fd2807SJeff Garzik  *	None.
5112c6fd2807SJeff Garzik  *
5113c6fd2807SJeff Garzik  *	RETURNS:
5114c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5115c6fd2807SJeff Garzik  */
5116936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5117c6fd2807SJeff Garzik {
5118936fd732STejun Heo 	struct ata_port *ap = link->ap;
5119936fd732STejun Heo 
5120a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5121c6fd2807SJeff Garzik }
5122c6fd2807SJeff Garzik 
5123c6fd2807SJeff Garzik /**
5124c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5125936fd732STejun Heo  *	@link: ATA link to read SCR for
5126c6fd2807SJeff Garzik  *	@reg: SCR to read
5127c6fd2807SJeff Garzik  *	@val: Place to store read value
5128c6fd2807SJeff Garzik  *
5129936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5130633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5131633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5132c6fd2807SJeff Garzik  *
5133c6fd2807SJeff Garzik  *	LOCKING:
5134633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5135c6fd2807SJeff Garzik  *
5136c6fd2807SJeff Garzik  *	RETURNS:
5137c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5138c6fd2807SJeff Garzik  */
5139936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5140c6fd2807SJeff Garzik {
5141633273a3STejun Heo 	if (ata_is_host_link(link)) {
5142936fd732STejun Heo 		if (sata_scr_valid(link))
514382ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5144c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5145c6fd2807SJeff Garzik 	}
5146c6fd2807SJeff Garzik 
5147633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5148633273a3STejun Heo }
5149633273a3STejun Heo 
5150c6fd2807SJeff Garzik /**
5151c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5152936fd732STejun Heo  *	@link: ATA link to write SCR for
5153c6fd2807SJeff Garzik  *	@reg: SCR to write
5154c6fd2807SJeff Garzik  *	@val: value to write
5155c6fd2807SJeff Garzik  *
5156936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5157633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5158633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5159c6fd2807SJeff Garzik  *
5160c6fd2807SJeff Garzik  *	LOCKING:
5161633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5162c6fd2807SJeff Garzik  *
5163c6fd2807SJeff Garzik  *	RETURNS:
5164c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5165c6fd2807SJeff Garzik  */
5166936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5167c6fd2807SJeff Garzik {
5168633273a3STejun Heo 	if (ata_is_host_link(link)) {
5169936fd732STejun Heo 		if (sata_scr_valid(link))
517082ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5171c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5172c6fd2807SJeff Garzik 	}
5173c6fd2807SJeff Garzik 
5174633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5175633273a3STejun Heo }
5176633273a3STejun Heo 
5177c6fd2807SJeff Garzik /**
5178c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5179936fd732STejun Heo  *	@link: ATA link to write SCR for
5180c6fd2807SJeff Garzik  *	@reg: SCR to write
5181c6fd2807SJeff Garzik  *	@val: value to write
5182c6fd2807SJeff Garzik  *
5183c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5184c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5185c6fd2807SJeff Garzik  *
5186c6fd2807SJeff Garzik  *	LOCKING:
5187633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5188c6fd2807SJeff Garzik  *
5189c6fd2807SJeff Garzik  *	RETURNS:
5190c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5191c6fd2807SJeff Garzik  */
5192936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5193c6fd2807SJeff Garzik {
5194633273a3STejun Heo 	if (ata_is_host_link(link)) {
5195da3dbb17STejun Heo 		int rc;
5196da3dbb17STejun Heo 
5197936fd732STejun Heo 		if (sata_scr_valid(link)) {
519882ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5199da3dbb17STejun Heo 			if (rc == 0)
520082ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5201da3dbb17STejun Heo 			return rc;
5202c6fd2807SJeff Garzik 		}
5203c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5204c6fd2807SJeff Garzik 	}
5205c6fd2807SJeff Garzik 
5206633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5207633273a3STejun Heo }
5208633273a3STejun Heo 
5209c6fd2807SJeff Garzik /**
5210b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5211936fd732STejun Heo  *	@link: ATA link to test
5212c6fd2807SJeff Garzik  *
5213936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5214936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5215936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5216c6fd2807SJeff Garzik  *
5217c6fd2807SJeff Garzik  *	LOCKING:
5218c6fd2807SJeff Garzik  *	None.
5219c6fd2807SJeff Garzik  *
5220c6fd2807SJeff Garzik  *	RETURNS:
5221b5b3fa38STejun Heo  *	True if the port online status is available and online.
5222c6fd2807SJeff Garzik  */
5223b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5224c6fd2807SJeff Garzik {
5225c6fd2807SJeff Garzik 	u32 sstatus;
5226c6fd2807SJeff Garzik 
5227936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
52289913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5229b5b3fa38STejun Heo 		return true;
5230b5b3fa38STejun Heo 	return false;
5231c6fd2807SJeff Garzik }
5232c6fd2807SJeff Garzik 
5233c6fd2807SJeff Garzik /**
5234b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5235936fd732STejun Heo  *	@link: ATA link to test
5236c6fd2807SJeff Garzik  *
5237936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5238936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5239936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5240c6fd2807SJeff Garzik  *
5241c6fd2807SJeff Garzik  *	LOCKING:
5242c6fd2807SJeff Garzik  *	None.
5243c6fd2807SJeff Garzik  *
5244c6fd2807SJeff Garzik  *	RETURNS:
5245b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5246c6fd2807SJeff Garzik  */
5247b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5248c6fd2807SJeff Garzik {
5249c6fd2807SJeff Garzik 	u32 sstatus;
5250c6fd2807SJeff Garzik 
5251936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
52529913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5253b5b3fa38STejun Heo 		return true;
5254b5b3fa38STejun Heo 	return false;
5255c6fd2807SJeff Garzik }
5256c6fd2807SJeff Garzik 
5257b1c72916STejun Heo /**
5258b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5259b1c72916STejun Heo  *	@link: ATA link to test
5260b1c72916STejun Heo  *
5261b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5262b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5263b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5264b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5265b1c72916STejun Heo  *	online.
5266b1c72916STejun Heo  *
5267b1c72916STejun Heo  *	LOCKING:
5268b1c72916STejun Heo  *	None.
5269b1c72916STejun Heo  *
5270b1c72916STejun Heo  *	RETURNS:
5271b1c72916STejun Heo  *	True if the port online status is available and online.
5272b1c72916STejun Heo  */
5273b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5274b1c72916STejun Heo {
5275b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5276b1c72916STejun Heo 
5277b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5278b1c72916STejun Heo 
5279b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5280b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5281b1c72916STejun Heo }
5282b1c72916STejun Heo 
5283b1c72916STejun Heo /**
5284b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5285b1c72916STejun Heo  *	@link: ATA link to test
5286b1c72916STejun Heo  *
5287b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5288b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5289b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5290b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5291b1c72916STejun Heo  *	offline.
5292b1c72916STejun Heo  *
5293b1c72916STejun Heo  *	LOCKING:
5294b1c72916STejun Heo  *	None.
5295b1c72916STejun Heo  *
5296b1c72916STejun Heo  *	RETURNS:
5297b1c72916STejun Heo  *	True if the port offline status is available and offline.
5298b1c72916STejun Heo  */
5299b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5300b1c72916STejun Heo {
5301b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5302b1c72916STejun Heo 
5303b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5304b1c72916STejun Heo 
5305b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5306b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5307b1c72916STejun Heo }
5308b1c72916STejun Heo 
53096ffa01d8STejun Heo #ifdef CONFIG_PM
5310cca3974eSJeff Garzik static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5311cca3974eSJeff Garzik 			       unsigned int action, unsigned int ehi_flags,
5312cca3974eSJeff Garzik 			       int wait)
5313c6fd2807SJeff Garzik {
5314c6fd2807SJeff Garzik 	unsigned long flags;
5315c6fd2807SJeff Garzik 	int i, rc;
5316c6fd2807SJeff Garzik 
5317cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5318cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5319e3667ebfSTejun Heo 		struct ata_link *link;
5320c6fd2807SJeff Garzik 
5321c6fd2807SJeff Garzik 		/* Previous resume operation might still be in
5322c6fd2807SJeff Garzik 		 * progress.  Wait for PM_PENDING to clear.
5323c6fd2807SJeff Garzik 		 */
5324c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5325c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5326c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5327c6fd2807SJeff Garzik 		}
5328c6fd2807SJeff Garzik 
5329c6fd2807SJeff Garzik 		/* request PM ops to EH */
5330c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
5331c6fd2807SJeff Garzik 
5332c6fd2807SJeff Garzik 		ap->pm_mesg = mesg;
5333c6fd2807SJeff Garzik 		if (wait) {
5334c6fd2807SJeff Garzik 			rc = 0;
5335c6fd2807SJeff Garzik 			ap->pm_result = &rc;
5336c6fd2807SJeff Garzik 		}
5337c6fd2807SJeff Garzik 
5338c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_PM_PENDING;
53391eca4365STejun Heo 		ata_for_each_link(link, ap, HOST_FIRST) {
5340e3667ebfSTejun Heo 			link->eh_info.action |= action;
5341e3667ebfSTejun Heo 			link->eh_info.flags |= ehi_flags;
5342e3667ebfSTejun Heo 		}
5343c6fd2807SJeff Garzik 
5344c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
5345c6fd2807SJeff Garzik 
5346c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
5347c6fd2807SJeff Garzik 
5348c6fd2807SJeff Garzik 		/* wait and check result */
5349c6fd2807SJeff Garzik 		if (wait) {
5350c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5351c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5352c6fd2807SJeff Garzik 			if (rc)
5353c6fd2807SJeff Garzik 				return rc;
5354c6fd2807SJeff Garzik 		}
5355c6fd2807SJeff Garzik 	}
5356c6fd2807SJeff Garzik 
5357c6fd2807SJeff Garzik 	return 0;
5358c6fd2807SJeff Garzik }
5359c6fd2807SJeff Garzik 
5360c6fd2807SJeff Garzik /**
5361cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5362cca3974eSJeff Garzik  *	@host: host to suspend
5363c6fd2807SJeff Garzik  *	@mesg: PM message
5364c6fd2807SJeff Garzik  *
5365cca3974eSJeff Garzik  *	Suspend @host.  Actual operation is performed by EH.  This
5366c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and waits for EH
5367c6fd2807SJeff Garzik  *	to finish.
5368c6fd2807SJeff Garzik  *
5369c6fd2807SJeff Garzik  *	LOCKING:
5370c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5371c6fd2807SJeff Garzik  *
5372c6fd2807SJeff Garzik  *	RETURNS:
5373c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
5374c6fd2807SJeff Garzik  */
5375cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5376c6fd2807SJeff Garzik {
53779666f400STejun Heo 	int rc;
5378c6fd2807SJeff Garzik 
5379ca77329fSKristen Carlson Accardi 	/*
5380ca77329fSKristen Carlson Accardi 	 * disable link pm on all ports before requesting
5381ca77329fSKristen Carlson Accardi 	 * any pm activity
5382ca77329fSKristen Carlson Accardi 	 */
5383ca77329fSKristen Carlson Accardi 	ata_lpm_enable(host);
5384ca77329fSKristen Carlson Accardi 
5385cca3974eSJeff Garzik 	rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
538672ad6ec4SJeff Garzik 	if (rc == 0)
538772ad6ec4SJeff Garzik 		host->dev->power.power_state = mesg;
5388c6fd2807SJeff Garzik 	return rc;
5389c6fd2807SJeff Garzik }
5390c6fd2807SJeff Garzik 
5391c6fd2807SJeff Garzik /**
5392cca3974eSJeff Garzik  *	ata_host_resume - resume host
5393cca3974eSJeff Garzik  *	@host: host to resume
5394c6fd2807SJeff Garzik  *
5395cca3974eSJeff Garzik  *	Resume @host.  Actual operation is performed by EH.  This
5396c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and returns.
5397c6fd2807SJeff Garzik  *	Note that all resume operations are performed parallely.
5398c6fd2807SJeff Garzik  *
5399c6fd2807SJeff Garzik  *	LOCKING:
5400c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5401c6fd2807SJeff Garzik  */
5402cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5403c6fd2807SJeff Garzik {
5404cf480626STejun Heo 	ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
5405c6fd2807SJeff Garzik 			    ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
540672ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5407ca77329fSKristen Carlson Accardi 
5408ca77329fSKristen Carlson Accardi 	/* reenable link pm */
5409ca77329fSKristen Carlson Accardi 	ata_lpm_disable(host);
5410c6fd2807SJeff Garzik }
54116ffa01d8STejun Heo #endif
5412c6fd2807SJeff Garzik 
5413c6fd2807SJeff Garzik /**
5414c6fd2807SJeff Garzik  *	ata_port_start - Set port up for dma.
5415c6fd2807SJeff Garzik  *	@ap: Port to initialize
5416c6fd2807SJeff Garzik  *
5417c6fd2807SJeff Garzik  *	Called just after data structures for each port are
5418c6fd2807SJeff Garzik  *	initialized.  Allocates space for PRD table.
5419c6fd2807SJeff Garzik  *
5420c6fd2807SJeff Garzik  *	May be used as the port_start() entry in ata_port_operations.
5421c6fd2807SJeff Garzik  *
5422c6fd2807SJeff Garzik  *	LOCKING:
5423c6fd2807SJeff Garzik  *	Inherited from caller.
5424c6fd2807SJeff Garzik  */
5425c6fd2807SJeff Garzik int ata_port_start(struct ata_port *ap)
5426c6fd2807SJeff Garzik {
5427c6fd2807SJeff Garzik 	struct device *dev = ap->dev;
5428c6fd2807SJeff Garzik 
5429f0d36efdSTejun Heo 	ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
5430f0d36efdSTejun Heo 				      GFP_KERNEL);
5431c6fd2807SJeff Garzik 	if (!ap->prd)
5432c6fd2807SJeff Garzik 		return -ENOMEM;
5433c6fd2807SJeff Garzik 
5434c6fd2807SJeff Garzik 	return 0;
5435c6fd2807SJeff Garzik }
5436c6fd2807SJeff Garzik 
5437c6fd2807SJeff Garzik /**
5438c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5439c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5440c6fd2807SJeff Garzik  *
5441c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5442c6fd2807SJeff Garzik  *
5443c6fd2807SJeff Garzik  *	LOCKING:
5444c6fd2807SJeff Garzik  *	Inherited from caller.
5445c6fd2807SJeff Garzik  */
5446c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5447c6fd2807SJeff Garzik {
5448b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
54499af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5450c6fd2807SJeff Garzik 	unsigned long flags;
5451c6fd2807SJeff Garzik 
5452b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
54539af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
54549af5c9c9STejun Heo 	link->sata_spd = 0;
5455c6fd2807SJeff Garzik 
5456c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5457c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5458cca3974eSJeff Garzik 	 * host lock.
5459c6fd2807SJeff Garzik 	 */
5460c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5461c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
54623dcc323fSTejun Heo 	dev->horkage = 0;
5463c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5464c6fd2807SJeff Garzik 
546599cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
546699cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5467c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5468c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5469c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5470c6fd2807SJeff Garzik }
5471c6fd2807SJeff Garzik 
5472c6fd2807SJeff Garzik /**
54734fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
54744fb37a25STejun Heo  *	@ap: ATA port link is attached to
54754fb37a25STejun Heo  *	@link: Link structure to initialize
54768989805dSTejun Heo  *	@pmp: Port multiplier port number
54774fb37a25STejun Heo  *
54784fb37a25STejun Heo  *	Initialize @link.
54794fb37a25STejun Heo  *
54804fb37a25STejun Heo  *	LOCKING:
54814fb37a25STejun Heo  *	Kernel thread context (may sleep)
54824fb37a25STejun Heo  */
5483fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
54844fb37a25STejun Heo {
54854fb37a25STejun Heo 	int i;
54864fb37a25STejun Heo 
54874fb37a25STejun Heo 	/* clear everything except for devices */
54884fb37a25STejun Heo 	memset(link, 0, offsetof(struct ata_link, device[0]));
54894fb37a25STejun Heo 
54904fb37a25STejun Heo 	link->ap = ap;
54918989805dSTejun Heo 	link->pmp = pmp;
54924fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
54934fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
54944fb37a25STejun Heo 
54954fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
54964fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
54974fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
54984fb37a25STejun Heo 
54994fb37a25STejun Heo 		dev->link = link;
55004fb37a25STejun Heo 		dev->devno = dev - link->device;
55014fb37a25STejun Heo 		ata_dev_init(dev);
55024fb37a25STejun Heo 	}
55034fb37a25STejun Heo }
55044fb37a25STejun Heo 
55054fb37a25STejun Heo /**
55064fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
55074fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
55084fb37a25STejun Heo  *
55094fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
55104fb37a25STejun Heo  *	configured value.
55114fb37a25STejun Heo  *
55124fb37a25STejun Heo  *	LOCKING:
55134fb37a25STejun Heo  *	Kernel thread context (may sleep).
55144fb37a25STejun Heo  *
55154fb37a25STejun Heo  *	RETURNS:
55164fb37a25STejun Heo  *	0 on success, -errno on failure.
55174fb37a25STejun Heo  */
5518fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
55194fb37a25STejun Heo {
552033267325STejun Heo 	u8 spd;
55214fb37a25STejun Heo 	int rc;
55224fb37a25STejun Heo 
5523d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
55244fb37a25STejun Heo 	if (rc)
55254fb37a25STejun Heo 		return rc;
55264fb37a25STejun Heo 
5527d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
55284fb37a25STejun Heo 	if (spd)
55294fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
55304fb37a25STejun Heo 
553105944bdfSTejun Heo 	ata_force_link_limits(link);
553233267325STejun Heo 
55334fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
55344fb37a25STejun Heo 
55354fb37a25STejun Heo 	return 0;
55364fb37a25STejun Heo }
55374fb37a25STejun Heo 
55384fb37a25STejun Heo /**
5539f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5540f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5541c6fd2807SJeff Garzik  *
5542f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5543f3187195STejun Heo  *
5544f3187195STejun Heo  *	RETURNS:
5545f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5546c6fd2807SJeff Garzik  *
5547c6fd2807SJeff Garzik  *	LOCKING:
5548f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5549c6fd2807SJeff Garzik  */
5550f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5551c6fd2807SJeff Garzik {
5552f3187195STejun Heo 	struct ata_port *ap;
5553c6fd2807SJeff Garzik 
5554f3187195STejun Heo 	DPRINTK("ENTER\n");
5555f3187195STejun Heo 
5556f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5557f3187195STejun Heo 	if (!ap)
5558f3187195STejun Heo 		return NULL;
5559f3187195STejun Heo 
5560f4d6d004STejun Heo 	ap->pflags |= ATA_PFLAG_INITIALIZING;
5561cca3974eSJeff Garzik 	ap->lock = &host->lock;
5562c6fd2807SJeff Garzik 	ap->flags = ATA_FLAG_DISABLED;
5563f3187195STejun Heo 	ap->print_id = -1;
5564c6fd2807SJeff Garzik 	ap->ctl = ATA_DEVCTL_OBS;
5565cca3974eSJeff Garzik 	ap->host = host;
5566f3187195STejun Heo 	ap->dev = host->dev;
5567c6fd2807SJeff Garzik 	ap->last_ctl = 0xFF;
5568c6fd2807SJeff Garzik 
5569c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5570c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5571c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5572c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5573c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5574c6fd2807SJeff Garzik #else
5575c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5576c6fd2807SJeff Garzik #endif
5577c6fd2807SJeff Garzik 
5578127102aeSTejun Heo #ifdef CONFIG_ATA_SFF
5579442eacc3SJeff Garzik 	INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
5580f667fdbbSTejun Heo #else
5581f667fdbbSTejun Heo 	INIT_DELAYED_WORK(&ap->port_task, NULL);
5582127102aeSTejun Heo #endif
558365f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
558465f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5585c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5586c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
558745fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
55885ddf24c5STejun Heo 	init_timer_deferrable(&ap->fastdrain_timer);
55895ddf24c5STejun Heo 	ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
55905ddf24c5STejun Heo 	ap->fastdrain_timer.data = (unsigned long)ap;
5591c6fd2807SJeff Garzik 
5592c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5593c6fd2807SJeff Garzik 
55948989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5595c6fd2807SJeff Garzik 
5596c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5597c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5598c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5599c6fd2807SJeff Garzik #endif
5600c6fd2807SJeff Garzik 	return ap;
5601c6fd2807SJeff Garzik }
5602c6fd2807SJeff Garzik 
5603f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5604f0d36efdSTejun Heo {
5605f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5606f0d36efdSTejun Heo 	int i;
5607f0d36efdSTejun Heo 
5608f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5609f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5610f0d36efdSTejun Heo 
5611ecef7253STejun Heo 		if (!ap)
5612ecef7253STejun Heo 			continue;
5613ecef7253STejun Heo 
56144911487aSTejun Heo 		if (ap->scsi_host)
56151aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
56161aa506e4STejun Heo 
5617633273a3STejun Heo 		kfree(ap->pmp_link);
5618b1c72916STejun Heo 		kfree(ap->slave_link);
56194911487aSTejun Heo 		kfree(ap);
56201aa506e4STejun Heo 		host->ports[i] = NULL;
56211aa506e4STejun Heo 	}
56221aa506e4STejun Heo 
56231aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5624f0d36efdSTejun Heo }
5625f0d36efdSTejun Heo 
5626c6fd2807SJeff Garzik /**
5627f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5628f3187195STejun Heo  *	@dev: generic device this host is associated with
5629f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5630f3187195STejun Heo  *
5631f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5632f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5633f3187195STejun Heo  *	attaches it using ata_host_register().
5634f3187195STejun Heo  *
5635f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5636f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5637f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5638f3187195STejun Heo  *	ports will be automatically freed on registration.
5639f3187195STejun Heo  *
5640f3187195STejun Heo  *	RETURNS:
5641f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5642f3187195STejun Heo  *
5643f3187195STejun Heo  *	LOCKING:
5644f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5645f3187195STejun Heo  */
5646f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5647f3187195STejun Heo {
5648f3187195STejun Heo 	struct ata_host *host;
5649f3187195STejun Heo 	size_t sz;
5650f3187195STejun Heo 	int i;
5651f3187195STejun Heo 
5652f3187195STejun Heo 	DPRINTK("ENTER\n");
5653f3187195STejun Heo 
5654f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5655f3187195STejun Heo 		return NULL;
5656f3187195STejun Heo 
5657f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5658f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5659f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5660f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5661f3187195STejun Heo 	if (!host)
5662f3187195STejun Heo 		goto err_out;
5663f3187195STejun Heo 
5664f3187195STejun Heo 	devres_add(dev, host);
5665f3187195STejun Heo 	dev_set_drvdata(dev, host);
5666f3187195STejun Heo 
5667f3187195STejun Heo 	spin_lock_init(&host->lock);
5668f3187195STejun Heo 	host->dev = dev;
5669f3187195STejun Heo 	host->n_ports = max_ports;
5670f3187195STejun Heo 
5671f3187195STejun Heo 	/* allocate ports bound to this host */
5672f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5673f3187195STejun Heo 		struct ata_port *ap;
5674f3187195STejun Heo 
5675f3187195STejun Heo 		ap = ata_port_alloc(host);
5676f3187195STejun Heo 		if (!ap)
5677f3187195STejun Heo 			goto err_out;
5678f3187195STejun Heo 
5679f3187195STejun Heo 		ap->port_no = i;
5680f3187195STejun Heo 		host->ports[i] = ap;
5681f3187195STejun Heo 	}
5682f3187195STejun Heo 
5683f3187195STejun Heo 	devres_remove_group(dev, NULL);
5684f3187195STejun Heo 	return host;
5685f3187195STejun Heo 
5686f3187195STejun Heo  err_out:
5687f3187195STejun Heo 	devres_release_group(dev, NULL);
5688f3187195STejun Heo 	return NULL;
5689f3187195STejun Heo }
5690f3187195STejun Heo 
5691f3187195STejun Heo /**
5692f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5693f5cda257STejun Heo  *	@dev: generic device this host is associated with
5694f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5695f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5696f5cda257STejun Heo  *
5697f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5698f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5699f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5700f5cda257STejun Heo  *
5701f5cda257STejun Heo  *	RETURNS:
5702f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5703f5cda257STejun Heo  *
5704f5cda257STejun Heo  *	LOCKING:
5705f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5706f5cda257STejun Heo  */
5707f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5708f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5709f5cda257STejun Heo 				      int n_ports)
5710f5cda257STejun Heo {
5711f5cda257STejun Heo 	const struct ata_port_info *pi;
5712f5cda257STejun Heo 	struct ata_host *host;
5713f5cda257STejun Heo 	int i, j;
5714f5cda257STejun Heo 
5715f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5716f5cda257STejun Heo 	if (!host)
5717f5cda257STejun Heo 		return NULL;
5718f5cda257STejun Heo 
5719f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5720f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5721f5cda257STejun Heo 
5722f5cda257STejun Heo 		if (ppi[j])
5723f5cda257STejun Heo 			pi = ppi[j++];
5724f5cda257STejun Heo 
5725f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5726f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5727f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5728f5cda257STejun Heo 		ap->flags |= pi->flags;
57290c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5730f5cda257STejun Heo 		ap->ops = pi->port_ops;
5731f5cda257STejun Heo 
5732f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5733f5cda257STejun Heo 			host->ops = pi->port_ops;
5734f5cda257STejun Heo 	}
5735f5cda257STejun Heo 
5736f5cda257STejun Heo 	return host;
5737f5cda257STejun Heo }
5738f5cda257STejun Heo 
5739b1c72916STejun Heo /**
5740b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
5741b1c72916STejun Heo  *	@ap: port to initialize slave link for
5742b1c72916STejun Heo  *
5743b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
5744b1c72916STejun Heo  *	link handling on the port.
5745b1c72916STejun Heo  *
5746b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
5747b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
5748b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
5749b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
5750b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
5751b1c72916STejun Heo  *	and slave.
5752b1c72916STejun Heo  *
5753b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
5754b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
5755b1c72916STejun Heo  *	interface with both master and slave devices but also have
5756b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
5757b1c72916STejun Heo  *	need separate links for physical link handling
5758b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
5759b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
5760b1c72916STejun Heo  *	issue, softreset).
5761b1c72916STejun Heo  *
5762b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
5763b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
5764b1c72916STejun Heo  *	anything other than physical link handling, the default host
5765b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
5766b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
5767b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
5768b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
5769b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
5770b1c72916STejun Heo  *	looks like the following.
5771b1c72916STejun Heo  *
5772b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5773b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
5774b1c72916STejun Heo  *
5775b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
5776b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
5777b1c72916STejun Heo  *	both (the standard method will work just fine).
5778b1c72916STejun Heo  *
5779b1c72916STejun Heo  *	LOCKING:
5780b1c72916STejun Heo  *	Should be called before host is registered.
5781b1c72916STejun Heo  *
5782b1c72916STejun Heo  *	RETURNS:
5783b1c72916STejun Heo  *	0 on success, -errno on failure.
5784b1c72916STejun Heo  */
5785b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
5786b1c72916STejun Heo {
5787b1c72916STejun Heo 	struct ata_link *link;
5788b1c72916STejun Heo 
5789b1c72916STejun Heo 	WARN_ON(ap->slave_link);
5790b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
5791b1c72916STejun Heo 
5792b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
5793b1c72916STejun Heo 	if (!link)
5794b1c72916STejun Heo 		return -ENOMEM;
5795b1c72916STejun Heo 
5796b1c72916STejun Heo 	ata_link_init(ap, link, 1);
5797b1c72916STejun Heo 	ap->slave_link = link;
5798b1c72916STejun Heo 	return 0;
5799b1c72916STejun Heo }
5800b1c72916STejun Heo 
580132ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
580232ebbc0cSTejun Heo {
580332ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
580432ebbc0cSTejun Heo 	int i;
580532ebbc0cSTejun Heo 
580632ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
580732ebbc0cSTejun Heo 
580832ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
580932ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
581032ebbc0cSTejun Heo 
581132ebbc0cSTejun Heo 		if (ap->ops->port_stop)
581232ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
581332ebbc0cSTejun Heo 	}
581432ebbc0cSTejun Heo 
581532ebbc0cSTejun Heo 	if (host->ops->host_stop)
581632ebbc0cSTejun Heo 		host->ops->host_stop(host);
581732ebbc0cSTejun Heo }
581832ebbc0cSTejun Heo 
5819f5cda257STejun Heo /**
5820029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5821029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5822029cfd6bSTejun Heo  *
5823029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5824029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5825029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5826029cfd6bSTejun Heo  *	inheritance chain.
5827029cfd6bSTejun Heo  *
5828029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5829029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5830029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5831029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5832029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5833029cfd6bSTejun Heo  *
5834029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5835029cfd6bSTejun Heo  *
5836029cfd6bSTejun Heo  *	LOCKING:
5837029cfd6bSTejun Heo  *	None.
5838029cfd6bSTejun Heo  */
5839029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5840029cfd6bSTejun Heo {
58412da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5842029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5843029cfd6bSTejun Heo 	void **begin = (void **)ops;
5844029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5845029cfd6bSTejun Heo 	void **pp;
5846029cfd6bSTejun Heo 
5847029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5848029cfd6bSTejun Heo 		return;
5849029cfd6bSTejun Heo 
5850029cfd6bSTejun Heo 	spin_lock(&lock);
5851029cfd6bSTejun Heo 
5852029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5853029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5854029cfd6bSTejun Heo 
5855029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5856029cfd6bSTejun Heo 			if (!*pp)
5857029cfd6bSTejun Heo 				*pp = *inherit;
5858029cfd6bSTejun Heo 	}
5859029cfd6bSTejun Heo 
5860029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5861029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5862029cfd6bSTejun Heo 			*pp = NULL;
5863029cfd6bSTejun Heo 
5864029cfd6bSTejun Heo 	ops->inherits = NULL;
5865029cfd6bSTejun Heo 
5866029cfd6bSTejun Heo 	spin_unlock(&lock);
5867029cfd6bSTejun Heo }
5868029cfd6bSTejun Heo 
5869029cfd6bSTejun Heo /**
5870ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5871ecef7253STejun Heo  *	@host: ATA host to start ports for
5872ecef7253STejun Heo  *
5873ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5874ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5875ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5876f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
5877f3187195STejun Heo  *	first non-dummy port ops.
5878ecef7253STejun Heo  *
5879ecef7253STejun Heo  *	LOCKING:
5880ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5881ecef7253STejun Heo  *
5882ecef7253STejun Heo  *	RETURNS:
5883ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5884ecef7253STejun Heo  */
5885ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5886ecef7253STejun Heo {
588732ebbc0cSTejun Heo 	int have_stop = 0;
588832ebbc0cSTejun Heo 	void *start_dr = NULL;
5889ecef7253STejun Heo 	int i, rc;
5890ecef7253STejun Heo 
5891ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5892ecef7253STejun Heo 		return 0;
5893ecef7253STejun Heo 
5894029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5895029cfd6bSTejun Heo 
5896ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5897ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5898ecef7253STejun Heo 
5899029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5900029cfd6bSTejun Heo 
5901f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5902f3187195STejun Heo 			host->ops = ap->ops;
5903f3187195STejun Heo 
590432ebbc0cSTejun Heo 		if (ap->ops->port_stop)
590532ebbc0cSTejun Heo 			have_stop = 1;
590632ebbc0cSTejun Heo 	}
590732ebbc0cSTejun Heo 
590832ebbc0cSTejun Heo 	if (host->ops->host_stop)
590932ebbc0cSTejun Heo 		have_stop = 1;
591032ebbc0cSTejun Heo 
591132ebbc0cSTejun Heo 	if (have_stop) {
591232ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
591332ebbc0cSTejun Heo 		if (!start_dr)
591432ebbc0cSTejun Heo 			return -ENOMEM;
591532ebbc0cSTejun Heo 	}
591632ebbc0cSTejun Heo 
591732ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
591832ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
591932ebbc0cSTejun Heo 
5920ecef7253STejun Heo 		if (ap->ops->port_start) {
5921ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5922ecef7253STejun Heo 			if (rc) {
59230f9fe9b7SAlan Cox 				if (rc != -ENODEV)
59240f757743SAndrew Morton 					dev_printk(KERN_ERR, host->dev,
59250f757743SAndrew Morton 						"failed to start port %d "
59260f757743SAndrew Morton 						"(errno=%d)\n", i, rc);
5927ecef7253STejun Heo 				goto err_out;
5928ecef7253STejun Heo 			}
5929ecef7253STejun Heo 		}
5930ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5931ecef7253STejun Heo 	}
5932ecef7253STejun Heo 
593332ebbc0cSTejun Heo 	if (start_dr)
593432ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5935ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5936ecef7253STejun Heo 	return 0;
5937ecef7253STejun Heo 
5938ecef7253STejun Heo  err_out:
5939ecef7253STejun Heo 	while (--i >= 0) {
5940ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5941ecef7253STejun Heo 
5942ecef7253STejun Heo 		if (ap->ops->port_stop)
5943ecef7253STejun Heo 			ap->ops->port_stop(ap);
5944ecef7253STejun Heo 	}
594532ebbc0cSTejun Heo 	devres_free(start_dr);
5946ecef7253STejun Heo 	return rc;
5947ecef7253STejun Heo }
5948ecef7253STejun Heo 
5949ecef7253STejun Heo /**
5950cca3974eSJeff Garzik  *	ata_sas_host_init - Initialize a host struct
5951cca3974eSJeff Garzik  *	@host:	host to initialize
5952cca3974eSJeff Garzik  *	@dev:	device host is attached to
5953cca3974eSJeff Garzik  *	@flags:	host flags
5954c6fd2807SJeff Garzik  *	@ops:	port_ops
5955c6fd2807SJeff Garzik  *
5956c6fd2807SJeff Garzik  *	LOCKING:
5957c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
5958c6fd2807SJeff Garzik  *
5959c6fd2807SJeff Garzik  */
5960f3187195STejun Heo /* KILLME - the only user left is ipr */
5961cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
5962029cfd6bSTejun Heo 		   unsigned long flags, struct ata_port_operations *ops)
5963c6fd2807SJeff Garzik {
5964cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5965cca3974eSJeff Garzik 	host->dev = dev;
5966cca3974eSJeff Garzik 	host->flags = flags;
5967cca3974eSJeff Garzik 	host->ops = ops;
5968c6fd2807SJeff Garzik }
5969c6fd2807SJeff Garzik 
597079318057SArjan van de Ven 
597179318057SArjan van de Ven static void async_port_probe(void *data, async_cookie_t cookie)
597279318057SArjan van de Ven {
597379318057SArjan van de Ven 	int rc;
597479318057SArjan van de Ven 	struct ata_port *ap = data;
5975886ad09fSArjan van de Ven 
5976886ad09fSArjan van de Ven 	/*
5977886ad09fSArjan van de Ven 	 * If we're not allowed to scan this host in parallel,
5978886ad09fSArjan van de Ven 	 * we need to wait until all previous scans have completed
5979886ad09fSArjan van de Ven 	 * before going further.
5980fa853a48SArjan van de Ven 	 * Jeff Garzik says this is only within a controller, so we
5981fa853a48SArjan van de Ven 	 * don't need to wait for port 0, only for later ports.
5982886ad09fSArjan van de Ven 	 */
5983fa853a48SArjan van de Ven 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5984886ad09fSArjan van de Ven 		async_synchronize_cookie(cookie);
5985886ad09fSArjan van de Ven 
598679318057SArjan van de Ven 	/* probe */
598779318057SArjan van de Ven 	if (ap->ops->error_handler) {
598879318057SArjan van de Ven 		struct ata_eh_info *ehi = &ap->link.eh_info;
598979318057SArjan van de Ven 		unsigned long flags;
599079318057SArjan van de Ven 
599179318057SArjan van de Ven 		ata_port_probe(ap);
599279318057SArjan van de Ven 
599379318057SArjan van de Ven 		/* kick EH for boot probing */
599479318057SArjan van de Ven 		spin_lock_irqsave(ap->lock, flags);
599579318057SArjan van de Ven 
599679318057SArjan van de Ven 		ehi->probe_mask |= ATA_ALL_DEVICES;
599779318057SArjan van de Ven 		ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
599879318057SArjan van de Ven 		ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
599979318057SArjan van de Ven 
600079318057SArjan van de Ven 		ap->pflags &= ~ATA_PFLAG_INITIALIZING;
600179318057SArjan van de Ven 		ap->pflags |= ATA_PFLAG_LOADING;
600279318057SArjan van de Ven 		ata_port_schedule_eh(ap);
600379318057SArjan van de Ven 
600479318057SArjan van de Ven 		spin_unlock_irqrestore(ap->lock, flags);
600579318057SArjan van de Ven 
600679318057SArjan van de Ven 		/* wait for EH to finish */
600779318057SArjan van de Ven 		ata_port_wait_eh(ap);
600879318057SArjan van de Ven 	} else {
600979318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
601079318057SArjan van de Ven 		rc = ata_bus_probe(ap);
601179318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
601279318057SArjan van de Ven 
601379318057SArjan van de Ven 		if (rc) {
601479318057SArjan van de Ven 			/* FIXME: do something useful here?
601579318057SArjan van de Ven 			 * Current libata behavior will
601679318057SArjan van de Ven 			 * tear down everything when
601779318057SArjan van de Ven 			 * the module is removed
601879318057SArjan van de Ven 			 * or the h/w is unplugged.
601979318057SArjan van de Ven 			 */
602079318057SArjan van de Ven 		}
602179318057SArjan van de Ven 	}
6022f29d3b23SArjan van de Ven 
6023f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
6024f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
6025f29d3b23SArjan van de Ven 
6026f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
6027f29d3b23SArjan van de Ven 
602879318057SArjan van de Ven }
6029c6fd2807SJeff Garzik /**
6030f3187195STejun Heo  *	ata_host_register - register initialized ATA host
6031f3187195STejun Heo  *	@host: ATA host to register
6032f3187195STejun Heo  *	@sht: template for SCSI host
6033c6fd2807SJeff Garzik  *
6034f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6035f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6036f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6037f3187195STejun Heo  *	probe registered devices.
6038c6fd2807SJeff Garzik  *
6039c6fd2807SJeff Garzik  *	LOCKING:
6040f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6041c6fd2807SJeff Garzik  *
6042c6fd2807SJeff Garzik  *	RETURNS:
6043f3187195STejun Heo  *	0 on success, -errno otherwise.
6044c6fd2807SJeff Garzik  */
6045f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6046c6fd2807SJeff Garzik {
6047f3187195STejun Heo 	int i, rc;
6048c6fd2807SJeff Garzik 
6049f3187195STejun Heo 	/* host must have been started */
6050f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6051f3187195STejun Heo 		dev_printk(KERN_ERR, host->dev,
6052f3187195STejun Heo 			   "BUG: trying to register unstarted host\n");
6053f3187195STejun Heo 		WARN_ON(1);
6054f3187195STejun Heo 		return -EINVAL;
605502f076aaSAlan Cox 	}
6056f0d36efdSTejun Heo 
6057f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6058f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6059f3187195STejun Heo 	 * allocation time.
6060f3187195STejun Heo 	 */
6061f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6062f3187195STejun Heo 		kfree(host->ports[i]);
6063f0d36efdSTejun Heo 
6064f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6065f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++)
6066f3187195STejun Heo 		host->ports[i]->print_id = ata_print_id++;
6067c6fd2807SJeff Garzik 
6068f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6069ecef7253STejun Heo 	if (rc)
6070f3187195STejun Heo 		return rc;
6071ecef7253STejun Heo 
6072fafbae87STejun Heo 	/* associate with ACPI nodes */
6073fafbae87STejun Heo 	ata_acpi_associate(host);
6074fafbae87STejun Heo 
6075f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6076cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6077cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6078f3187195STejun Heo 		unsigned long xfer_mask;
6079f3187195STejun Heo 
6080f3187195STejun Heo 		/* set SATA cable type if still unset */
6081f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6082f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6083c6fd2807SJeff Garzik 
6084c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
60854fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6086b1c72916STejun Heo 		if (ap->slave_link)
6087b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6088c6fd2807SJeff Garzik 
6089cbcdd875STejun Heo 		/* print per-port info to dmesg */
6090f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6091f3187195STejun Heo 					      ap->udma_mask);
6092f3187195STejun Heo 
6093abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6094cbcdd875STejun Heo 			ata_port_printk(ap, KERN_INFO,
6095cbcdd875STejun Heo 					"%cATA max %s %s\n",
6096a16abc0bSTejun Heo 					(ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6097f3187195STejun Heo 					ata_mode_string(xfer_mask),
6098cbcdd875STejun Heo 					ap->link.eh_info.desc);
6099abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6100abf6e8edSTejun Heo 		} else
6101f3187195STejun Heo 			ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6102c6fd2807SJeff Garzik 	}
6103c6fd2807SJeff Garzik 
6104f3187195STejun Heo 	/* perform each probe synchronously */
6105f3187195STejun Heo 	DPRINTK("probe begin\n");
6106f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6107f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
610879318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6109c6fd2807SJeff Garzik 	}
6110f29d3b23SArjan van de Ven 	DPRINTK("probe end\n");
6111c6fd2807SJeff Garzik 
6112f3187195STejun Heo 	return 0;
6113f3187195STejun Heo }
6114f3187195STejun Heo 
6115f3187195STejun Heo /**
6116f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6117f5cda257STejun Heo  *	@host: target ATA host
6118f5cda257STejun Heo  *	@irq: IRQ to request
6119f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6120f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6121f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6122f5cda257STejun Heo  *
6123f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6124f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6125f5cda257STejun Heo  *	request IRQ and register it.  This helper takes necessasry
6126f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6127f5cda257STejun Heo  *
61283d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
61293d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
61303d46b2e2SPaul Mundt  *	should be NULL.
61313d46b2e2SPaul Mundt  *
6132f5cda257STejun Heo  *	LOCKING:
6133f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6134f5cda257STejun Heo  *
6135f5cda257STejun Heo  *	RETURNS:
6136f5cda257STejun Heo  *	0 on success, -errno otherwise.
6137f5cda257STejun Heo  */
6138f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6139f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6140f5cda257STejun Heo 		      struct scsi_host_template *sht)
6141f5cda257STejun Heo {
6142cbcdd875STejun Heo 	int i, rc;
6143f5cda257STejun Heo 
6144f5cda257STejun Heo 	rc = ata_host_start(host);
6145f5cda257STejun Heo 	if (rc)
6146f5cda257STejun Heo 		return rc;
6147f5cda257STejun Heo 
61483d46b2e2SPaul Mundt 	/* Special case for polling mode */
61493d46b2e2SPaul Mundt 	if (!irq) {
61503d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
61513d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
61523d46b2e2SPaul Mundt 	}
61533d46b2e2SPaul Mundt 
6154f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6155f5cda257STejun Heo 			      dev_driver_string(host->dev), host);
6156f5cda257STejun Heo 	if (rc)
6157f5cda257STejun Heo 		return rc;
6158f5cda257STejun Heo 
6159cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6160cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
61614031826bSTejun Heo 
6162f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6163f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6164f5cda257STejun Heo 	if (rc)
6165f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6166f5cda257STejun Heo 
6167f5cda257STejun Heo 	return rc;
6168f5cda257STejun Heo }
6169f5cda257STejun Heo 
6170f5cda257STejun Heo /**
6171c6fd2807SJeff Garzik  *	ata_port_detach - Detach ATA port in prepration of device removal
6172c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6173c6fd2807SJeff Garzik  *
6174c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6175c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6176c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6177c6fd2807SJeff Garzik  *
6178c6fd2807SJeff Garzik  *	LOCKING:
6179c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6180c6fd2807SJeff Garzik  */
6181741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6182c6fd2807SJeff Garzik {
6183c6fd2807SJeff Garzik 	unsigned long flags;
6184c6fd2807SJeff Garzik 
6185c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6186c6fd2807SJeff Garzik 		goto skip_eh;
6187c6fd2807SJeff Garzik 
6188c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6189c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6190c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6191ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6192c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6193c6fd2807SJeff Garzik 
6194ece180d1STejun Heo 	/* wait till EH commits suicide */
6195c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6196c6fd2807SJeff Garzik 
6197ece180d1STejun Heo 	/* it better be dead now */
6198ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6199c6fd2807SJeff Garzik 
620045a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->hotplug_task);
6201c6fd2807SJeff Garzik 
6202c6fd2807SJeff Garzik  skip_eh:
6203c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6204cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6205c6fd2807SJeff Garzik }
6206c6fd2807SJeff Garzik 
6207c6fd2807SJeff Garzik /**
62080529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
62090529c159STejun Heo  *	@host: Host to detach
62100529c159STejun Heo  *
62110529c159STejun Heo  *	Detach all ports of @host.
62120529c159STejun Heo  *
62130529c159STejun Heo  *	LOCKING:
62140529c159STejun Heo  *	Kernel thread context (may sleep).
62150529c159STejun Heo  */
62160529c159STejun Heo void ata_host_detach(struct ata_host *host)
62170529c159STejun Heo {
62180529c159STejun Heo 	int i;
62190529c159STejun Heo 
62200529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
62210529c159STejun Heo 		ata_port_detach(host->ports[i]);
6222562f0c2dSTejun Heo 
6223562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6224562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
62250529c159STejun Heo }
62260529c159STejun Heo 
6227c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6228c6fd2807SJeff Garzik 
6229c6fd2807SJeff Garzik /**
6230c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6231c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6232c6fd2807SJeff Garzik  *
6233b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6234b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6235b878ca5dSTejun Heo  *	release is handled via devres.
6236c6fd2807SJeff Garzik  *
6237c6fd2807SJeff Garzik  *	LOCKING:
6238c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6239c6fd2807SJeff Garzik  */
6240c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6241c6fd2807SJeff Garzik {
62422855568bSJeff Garzik 	struct device *dev = &pdev->dev;
6243cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(dev);
6244c6fd2807SJeff Garzik 
6245f0d36efdSTejun Heo 	ata_host_detach(host);
6246c6fd2807SJeff Garzik }
6247c6fd2807SJeff Garzik 
6248c6fd2807SJeff Garzik /* move to PCI subsystem */
6249c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6250c6fd2807SJeff Garzik {
6251c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6252c6fd2807SJeff Garzik 
6253c6fd2807SJeff Garzik 	switch (bits->width) {
6254c6fd2807SJeff Garzik 	case 1: {
6255c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6256c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6257c6fd2807SJeff Garzik 		tmp = tmp8;
6258c6fd2807SJeff Garzik 		break;
6259c6fd2807SJeff Garzik 	}
6260c6fd2807SJeff Garzik 	case 2: {
6261c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6262c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6263c6fd2807SJeff Garzik 		tmp = tmp16;
6264c6fd2807SJeff Garzik 		break;
6265c6fd2807SJeff Garzik 	}
6266c6fd2807SJeff Garzik 	case 4: {
6267c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6268c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6269c6fd2807SJeff Garzik 		tmp = tmp32;
6270c6fd2807SJeff Garzik 		break;
6271c6fd2807SJeff Garzik 	}
6272c6fd2807SJeff Garzik 
6273c6fd2807SJeff Garzik 	default:
6274c6fd2807SJeff Garzik 		return -EINVAL;
6275c6fd2807SJeff Garzik 	}
6276c6fd2807SJeff Garzik 
6277c6fd2807SJeff Garzik 	tmp &= bits->mask;
6278c6fd2807SJeff Garzik 
6279c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6280c6fd2807SJeff Garzik }
6281c6fd2807SJeff Garzik 
62826ffa01d8STejun Heo #ifdef CONFIG_PM
6283c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6284c6fd2807SJeff Garzik {
6285c6fd2807SJeff Garzik 	pci_save_state(pdev);
6286c6fd2807SJeff Garzik 	pci_disable_device(pdev);
62874c90d971STejun Heo 
62883a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6289c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6290c6fd2807SJeff Garzik }
6291c6fd2807SJeff Garzik 
6292553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6293c6fd2807SJeff Garzik {
6294553c4aa6STejun Heo 	int rc;
6295553c4aa6STejun Heo 
6296c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6297c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6298553c4aa6STejun Heo 
6299f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6300553c4aa6STejun Heo 	if (rc) {
6301553c4aa6STejun Heo 		dev_printk(KERN_ERR, &pdev->dev,
6302553c4aa6STejun Heo 			   "failed to enable device after resume (%d)\n", rc);
6303553c4aa6STejun Heo 		return rc;
6304553c4aa6STejun Heo 	}
6305553c4aa6STejun Heo 
6306c6fd2807SJeff Garzik 	pci_set_master(pdev);
6307553c4aa6STejun Heo 	return 0;
6308c6fd2807SJeff Garzik }
6309c6fd2807SJeff Garzik 
6310c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6311c6fd2807SJeff Garzik {
6312cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6313c6fd2807SJeff Garzik 	int rc = 0;
6314c6fd2807SJeff Garzik 
6315cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6316c6fd2807SJeff Garzik 	if (rc)
6317c6fd2807SJeff Garzik 		return rc;
6318c6fd2807SJeff Garzik 
6319c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6320c6fd2807SJeff Garzik 
6321c6fd2807SJeff Garzik 	return 0;
6322c6fd2807SJeff Garzik }
6323c6fd2807SJeff Garzik 
6324c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6325c6fd2807SJeff Garzik {
6326cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6327553c4aa6STejun Heo 	int rc;
6328c6fd2807SJeff Garzik 
6329553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6330553c4aa6STejun Heo 	if (rc == 0)
6331cca3974eSJeff Garzik 		ata_host_resume(host);
6332553c4aa6STejun Heo 	return rc;
6333c6fd2807SJeff Garzik }
63346ffa01d8STejun Heo #endif /* CONFIG_PM */
63356ffa01d8STejun Heo 
6336c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6337c6fd2807SJeff Garzik 
633833267325STejun Heo static int __init ata_parse_force_one(char **cur,
633933267325STejun Heo 				      struct ata_force_ent *force_ent,
634033267325STejun Heo 				      const char **reason)
634133267325STejun Heo {
634233267325STejun Heo 	/* FIXME: Currently, there's no way to tag init const data and
634333267325STejun Heo 	 * using __initdata causes build failure on some versions of
634433267325STejun Heo 	 * gcc.  Once __initdataconst is implemented, add const to the
634533267325STejun Heo 	 * following structure.
634633267325STejun Heo 	 */
634733267325STejun Heo 	static struct ata_force_param force_tbl[] __initdata = {
634833267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
634933267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
635033267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
635133267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
635233267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
635333267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
635433267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
635533267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
635633267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
635733267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
635833267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
635933267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
636033267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
636133267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
636233267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
636333267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
636433267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
636533267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
636633267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
636733267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
636833267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
636933267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
637033267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
637133267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
637233267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
637333267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
637433267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
637533267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
637633267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
637733267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
637833267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
637933267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
638033267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
638133267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
638233267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
638333267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
638433267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
638533267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
638633267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
638733267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
638833267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
638933267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
639033267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
639133267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
639205944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
639305944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
639405944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
639533267325STejun Heo 	};
639633267325STejun Heo 	char *start = *cur, *p = *cur;
639733267325STejun Heo 	char *id, *val, *endp;
639833267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
639933267325STejun Heo 	int nr_matches = 0, i;
640033267325STejun Heo 
640133267325STejun Heo 	/* find where this param ends and update *cur */
640233267325STejun Heo 	while (*p != '\0' && *p != ',')
640333267325STejun Heo 		p++;
640433267325STejun Heo 
640533267325STejun Heo 	if (*p == '\0')
640633267325STejun Heo 		*cur = p;
640733267325STejun Heo 	else
640833267325STejun Heo 		*cur = p + 1;
640933267325STejun Heo 
641033267325STejun Heo 	*p = '\0';
641133267325STejun Heo 
641233267325STejun Heo 	/* parse */
641333267325STejun Heo 	p = strchr(start, ':');
641433267325STejun Heo 	if (!p) {
641533267325STejun Heo 		val = strstrip(start);
641633267325STejun Heo 		goto parse_val;
641733267325STejun Heo 	}
641833267325STejun Heo 	*p = '\0';
641933267325STejun Heo 
642033267325STejun Heo 	id = strstrip(start);
642133267325STejun Heo 	val = strstrip(p + 1);
642233267325STejun Heo 
642333267325STejun Heo 	/* parse id */
642433267325STejun Heo 	p = strchr(id, '.');
642533267325STejun Heo 	if (p) {
642633267325STejun Heo 		*p++ = '\0';
642733267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
642833267325STejun Heo 		if (p == endp || *endp != '\0') {
642933267325STejun Heo 			*reason = "invalid device";
643033267325STejun Heo 			return -EINVAL;
643133267325STejun Heo 		}
643233267325STejun Heo 	}
643333267325STejun Heo 
643433267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
643533267325STejun Heo 	if (p == endp || *endp != '\0') {
643633267325STejun Heo 		*reason = "invalid port/link";
643733267325STejun Heo 		return -EINVAL;
643833267325STejun Heo 	}
643933267325STejun Heo 
644033267325STejun Heo  parse_val:
644133267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
644233267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
644333267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
644433267325STejun Heo 
644533267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
644633267325STejun Heo 			continue;
644733267325STejun Heo 
644833267325STejun Heo 		nr_matches++;
644933267325STejun Heo 		match_fp = fp;
645033267325STejun Heo 
645133267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
645233267325STejun Heo 			nr_matches = 1;
645333267325STejun Heo 			break;
645433267325STejun Heo 		}
645533267325STejun Heo 	}
645633267325STejun Heo 
645733267325STejun Heo 	if (!nr_matches) {
645833267325STejun Heo 		*reason = "unknown value";
645933267325STejun Heo 		return -EINVAL;
646033267325STejun Heo 	}
646133267325STejun Heo 	if (nr_matches > 1) {
646233267325STejun Heo 		*reason = "ambigious value";
646333267325STejun Heo 		return -EINVAL;
646433267325STejun Heo 	}
646533267325STejun Heo 
646633267325STejun Heo 	force_ent->param = *match_fp;
646733267325STejun Heo 
646833267325STejun Heo 	return 0;
646933267325STejun Heo }
647033267325STejun Heo 
647133267325STejun Heo static void __init ata_parse_force_param(void)
647233267325STejun Heo {
647333267325STejun Heo 	int idx = 0, size = 1;
647433267325STejun Heo 	int last_port = -1, last_device = -1;
647533267325STejun Heo 	char *p, *cur, *next;
647633267325STejun Heo 
647733267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
647833267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
647933267325STejun Heo 		if (*p == ',')
648033267325STejun Heo 			size++;
648133267325STejun Heo 
648233267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
648333267325STejun Heo 	if (!ata_force_tbl) {
648433267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
648533267325STejun Heo 		       "libata.force ignored\n");
648633267325STejun Heo 		return;
648733267325STejun Heo 	}
648833267325STejun Heo 
648933267325STejun Heo 	/* parse and populate the table */
649033267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
649133267325STejun Heo 		const char *reason = "";
649233267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
649333267325STejun Heo 
649433267325STejun Heo 		next = cur;
649533267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
649633267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
649733267325STejun Heo 			       "parameter \"%s\" (%s)\n",
649833267325STejun Heo 			       cur, reason);
649933267325STejun Heo 			continue;
650033267325STejun Heo 		}
650133267325STejun Heo 
650233267325STejun Heo 		if (te.port == -1) {
650333267325STejun Heo 			te.port = last_port;
650433267325STejun Heo 			te.device = last_device;
650533267325STejun Heo 		}
650633267325STejun Heo 
650733267325STejun Heo 		ata_force_tbl[idx++] = te;
650833267325STejun Heo 
650933267325STejun Heo 		last_port = te.port;
651033267325STejun Heo 		last_device = te.device;
651133267325STejun Heo 	}
651233267325STejun Heo 
651333267325STejun Heo 	ata_force_tbl_size = idx;
651433267325STejun Heo }
6515c6fd2807SJeff Garzik 
6516c6fd2807SJeff Garzik static int __init ata_init(void)
6517c6fd2807SJeff Garzik {
651833267325STejun Heo 	ata_parse_force_param();
651933267325STejun Heo 
6520c6fd2807SJeff Garzik 	ata_wq = create_workqueue("ata");
6521c6fd2807SJeff Garzik 	if (!ata_wq)
652249ea3b04SElias Oltmanns 		goto free_force_tbl;
6523c6fd2807SJeff Garzik 
6524c6fd2807SJeff Garzik 	ata_aux_wq = create_singlethread_workqueue("ata_aux");
652549ea3b04SElias Oltmanns 	if (!ata_aux_wq)
652649ea3b04SElias Oltmanns 		goto free_wq;
6527c6fd2807SJeff Garzik 
6528c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6529c6fd2807SJeff Garzik 	return 0;
653049ea3b04SElias Oltmanns 
653149ea3b04SElias Oltmanns free_wq:
653249ea3b04SElias Oltmanns 	destroy_workqueue(ata_wq);
653349ea3b04SElias Oltmanns free_force_tbl:
653449ea3b04SElias Oltmanns 	kfree(ata_force_tbl);
653549ea3b04SElias Oltmanns 	return -ENOMEM;
6536c6fd2807SJeff Garzik }
6537c6fd2807SJeff Garzik 
6538c6fd2807SJeff Garzik static void __exit ata_exit(void)
6539c6fd2807SJeff Garzik {
654033267325STejun Heo 	kfree(ata_force_tbl);
6541c6fd2807SJeff Garzik 	destroy_workqueue(ata_wq);
6542c6fd2807SJeff Garzik 	destroy_workqueue(ata_aux_wq);
6543c6fd2807SJeff Garzik }
6544c6fd2807SJeff Garzik 
6545a4625085SBrian King subsys_initcall(ata_init);
6546c6fd2807SJeff Garzik module_exit(ata_exit);
6547c6fd2807SJeff Garzik 
6548c6fd2807SJeff Garzik static unsigned long ratelimit_time;
6549c6fd2807SJeff Garzik static DEFINE_SPINLOCK(ata_ratelimit_lock);
6550c6fd2807SJeff Garzik 
6551c6fd2807SJeff Garzik int ata_ratelimit(void)
6552c6fd2807SJeff Garzik {
6553c6fd2807SJeff Garzik 	int rc;
6554c6fd2807SJeff Garzik 	unsigned long flags;
6555c6fd2807SJeff Garzik 
6556c6fd2807SJeff Garzik 	spin_lock_irqsave(&ata_ratelimit_lock, flags);
6557c6fd2807SJeff Garzik 
6558c6fd2807SJeff Garzik 	if (time_after(jiffies, ratelimit_time)) {
6559c6fd2807SJeff Garzik 		rc = 1;
6560c6fd2807SJeff Garzik 		ratelimit_time = jiffies + (HZ/5);
6561c6fd2807SJeff Garzik 	} else
6562c6fd2807SJeff Garzik 		rc = 0;
6563c6fd2807SJeff Garzik 
6564c6fd2807SJeff Garzik 	spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6565c6fd2807SJeff Garzik 
6566c6fd2807SJeff Garzik 	return rc;
6567c6fd2807SJeff Garzik }
6568c6fd2807SJeff Garzik 
6569c6fd2807SJeff Garzik /**
6570c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
6571c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6572c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6573c6fd2807SJeff Garzik  *	@val: Wait condition
6574341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6575341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6576c6fd2807SJeff Garzik  *
6577c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6578c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6579c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6580c6fd2807SJeff Garzik  *
6581c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6582c6fd2807SJeff Garzik  *
6583c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6584c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6585c6fd2807SJeff Garzik  *
6586c6fd2807SJeff Garzik  *	LOCKING:
6587c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6588c6fd2807SJeff Garzik  *
6589c6fd2807SJeff Garzik  *	RETURNS:
6590c6fd2807SJeff Garzik  *	The final register value.
6591c6fd2807SJeff Garzik  */
6592c6fd2807SJeff Garzik u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6593341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6594c6fd2807SJeff Garzik {
6595341c2c95STejun Heo 	unsigned long deadline;
6596c6fd2807SJeff Garzik 	u32 tmp;
6597c6fd2807SJeff Garzik 
6598c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6599c6fd2807SJeff Garzik 
6600c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6601c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6602c6fd2807SJeff Garzik 	 * eat away the timeout.
6603c6fd2807SJeff Garzik 	 */
6604341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6605c6fd2807SJeff Garzik 
6606341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6607341c2c95STejun Heo 		msleep(interval);
6608c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6609c6fd2807SJeff Garzik 	}
6610c6fd2807SJeff Garzik 
6611c6fd2807SJeff Garzik 	return tmp;
6612c6fd2807SJeff Garzik }
6613c6fd2807SJeff Garzik 
6614c6fd2807SJeff Garzik /*
6615c6fd2807SJeff Garzik  * Dummy port_ops
6616c6fd2807SJeff Garzik  */
6617c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6618c6fd2807SJeff Garzik {
6619c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6620c6fd2807SJeff Garzik }
6621c6fd2807SJeff Garzik 
6622182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6623182d7bbaSTejun Heo {
6624182d7bbaSTejun Heo 	/* truly dummy */
6625182d7bbaSTejun Heo }
6626182d7bbaSTejun Heo 
6627029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6628c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6629c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6630182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6631c6fd2807SJeff Garzik };
6632c6fd2807SJeff Garzik 
663321b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
663421b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
663521b0ad4fSTejun Heo };
663621b0ad4fSTejun Heo 
6637c6fd2807SJeff Garzik /*
6638c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
6639c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
6640c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
6641c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
6642c6fd2807SJeff Garzik  */
6643c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6644c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6645c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6646029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
6647029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
6648c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
664921b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
66501eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
66511eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
6652c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
6653cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
6654f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
6655f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6656b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
6657ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
6658f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
6659f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
66600529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
6661c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
6662c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
6663c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6664436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
6665c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6666c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
66676357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
66686357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
66696357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
66706357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
66716357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
66726357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
66736357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
6674c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_start);
667504351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
667631cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6677c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
6678c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_probe);
667910305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
6680c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
6681aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6682936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
6683936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
66840aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
6685cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
668657c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
6687203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
6688c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
6689c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
6690c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_disable);
6691c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
6692c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
6693c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6694c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6695c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6696c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6697c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
6698c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
6699c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
6700c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6701936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
6702936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
67036ffa01d8STejun Heo #ifdef CONFIG_PM
6704cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
6705cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
67066ffa01d8STejun Heo #endif /* CONFIG_PM */
6707c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
6708c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
6709963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6710c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6711c6fd2807SJeff Garzik 
67121a660164SMark Lord EXPORT_SYMBOL_GPL(ata_pio_queue_task);
6713c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
67146357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6715c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
6716c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
6717a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6718c6fd2807SJeff Garzik 
6719c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6720c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
6721c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
67226ffa01d8STejun Heo #ifdef CONFIG_PM
6723c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6724c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6725c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6726c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
67276ffa01d8STejun Heo #endif /* CONFIG_PM */
6728c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6729c6fd2807SJeff Garzik 
6730b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6731b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6732b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6733cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
6734cbcdd875STejun Heo #ifdef CONFIG_PCI
6735cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6736cbcdd875STejun Heo #endif /* CONFIG_PCI */
6737c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6738dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
6739c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
6740c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
67417d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
6742c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6743c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6744c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6745c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
674610acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
6747c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
6748a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
6749be0d18dfSAlan Cox 
6750be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
6751be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
6752be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
6753c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
6754be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
6755