xref: /openbmc/linux/drivers/ata/libata-core.c (revision efcb3cf7)
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 
1671eca4365STejun Heo /**
1681eca4365STejun Heo  *	ata_link_next - link iteration helper
1691eca4365STejun Heo  *	@link: the previous link, NULL to start
1701eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1711eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
172aadffb68STejun Heo  *
173aadffb68STejun Heo  *	LOCKING:
174aadffb68STejun Heo  *	Host lock or EH context.
1751eca4365STejun Heo  *
1761eca4365STejun Heo  *	RETURNS:
1771eca4365STejun Heo  *	Pointer to the next link.
178aadffb68STejun Heo  */
1791eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1801eca4365STejun Heo 			       enum ata_link_iter_mode mode)
181aadffb68STejun Heo {
1821eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
1831eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
1841eca4365STejun Heo 
185aadffb68STejun Heo 	/* NULL link indicates start of iteration */
1861eca4365STejun Heo 	if (!link)
1871eca4365STejun Heo 		switch (mode) {
1881eca4365STejun Heo 		case ATA_LITER_EDGE:
1891eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
1901eca4365STejun Heo 			if (sata_pmp_attached(ap))
191aadffb68STejun Heo 				return ap->pmp_link;
1921eca4365STejun Heo 			/* fall through */
1931eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
194aadffb68STejun Heo 			return &ap->link;
195aadffb68STejun Heo 		}
196aadffb68STejun Heo 
1971eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
1981eca4365STejun Heo 	if (link == &ap->link)
1991eca4365STejun Heo 		switch (mode) {
2001eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2011eca4365STejun Heo 			if (sata_pmp_attached(ap))
202aadffb68STejun Heo 				return ap->pmp_link;
2031eca4365STejun Heo 			/* fall through */
2041eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2051eca4365STejun Heo 			if (unlikely(ap->slave_link))
2061eca4365STejun Heo 				return ap->slave_link;
2071eca4365STejun Heo 			/* fall through */
2081eca4365STejun Heo 		case ATA_LITER_EDGE:
2091eca4365STejun Heo 			return NULL;
210aadffb68STejun Heo 		}
211aadffb68STejun Heo 
212b1c72916STejun Heo 	/* slave_link excludes PMP */
213b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
214b1c72916STejun Heo 		return NULL;
215b1c72916STejun Heo 
2161eca4365STejun Heo 	/* we were over a PMP link */
217aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
218aadffb68STejun Heo 		return link;
2191eca4365STejun Heo 
2201eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2211eca4365STejun Heo 		return &ap->link;
2221eca4365STejun Heo 
223aadffb68STejun Heo 	return NULL;
224aadffb68STejun Heo }
225aadffb68STejun Heo 
226c6fd2807SJeff Garzik /**
2271eca4365STejun Heo  *	ata_dev_next - device iteration helper
2281eca4365STejun Heo  *	@dev: the previous device, NULL to start
2291eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2301eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2311eca4365STejun Heo  *
2321eca4365STejun Heo  *	LOCKING:
2331eca4365STejun Heo  *	Host lock or EH context.
2341eca4365STejun Heo  *
2351eca4365STejun Heo  *	RETURNS:
2361eca4365STejun Heo  *	Pointer to the next device.
2371eca4365STejun Heo  */
2381eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2391eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2401eca4365STejun Heo {
2411eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2421eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2431eca4365STejun Heo 
2441eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2451eca4365STejun Heo 	if (!dev)
2461eca4365STejun Heo 		switch (mode) {
2471eca4365STejun Heo 		case ATA_DITER_ENABLED:
2481eca4365STejun Heo 		case ATA_DITER_ALL:
2491eca4365STejun Heo 			dev = link->device;
2501eca4365STejun Heo 			goto check;
2511eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2521eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2531eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2541eca4365STejun Heo 			goto check;
2551eca4365STejun Heo 		}
2561eca4365STejun Heo 
2571eca4365STejun Heo  next:
2581eca4365STejun Heo 	/* move to the next one */
2591eca4365STejun Heo 	switch (mode) {
2601eca4365STejun Heo 	case ATA_DITER_ENABLED:
2611eca4365STejun Heo 	case ATA_DITER_ALL:
2621eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2631eca4365STejun Heo 			goto check;
2641eca4365STejun Heo 		return NULL;
2651eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2661eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2671eca4365STejun Heo 		if (--dev >= link->device)
2681eca4365STejun Heo 			goto check;
2691eca4365STejun Heo 		return NULL;
2701eca4365STejun Heo 	}
2711eca4365STejun Heo 
2721eca4365STejun Heo  check:
2731eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2741eca4365STejun Heo 	    !ata_dev_enabled(dev))
2751eca4365STejun Heo 		goto next;
2761eca4365STejun Heo 	return dev;
2771eca4365STejun Heo }
2781eca4365STejun Heo 
2791eca4365STejun Heo /**
280b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
281b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
282b1c72916STejun Heo  *
283b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
284b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
285b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
286b1c72916STejun Heo  *
287b1c72916STejun Heo  *	LOCKING:
288b1c72916STejun Heo  *	Don't care.
289b1c72916STejun Heo  *
290b1c72916STejun Heo  *	RETURNS:
291b1c72916STejun Heo  *	Pointer to the found physical link.
292b1c72916STejun Heo  */
293b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
294b1c72916STejun Heo {
295b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
296b1c72916STejun Heo 
297b1c72916STejun Heo 	if (!ap->slave_link)
298b1c72916STejun Heo 		return dev->link;
299b1c72916STejun Heo 	if (!dev->devno)
300b1c72916STejun Heo 		return &ap->link;
301b1c72916STejun Heo 	return ap->slave_link;
302b1c72916STejun Heo }
303b1c72916STejun Heo 
304b1c72916STejun Heo /**
30533267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3064cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
30733267325STejun Heo  *
30833267325STejun Heo  *	Force cable type according to libata.force and whine about it.
30933267325STejun Heo  *	The last entry which has matching port number is used, so it
31033267325STejun Heo  *	can be specified as part of device force parameters.  For
31133267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
31233267325STejun Heo  *	same effect.
31333267325STejun Heo  *
31433267325STejun Heo  *	LOCKING:
31533267325STejun Heo  *	EH context.
31633267325STejun Heo  */
31733267325STejun Heo void ata_force_cbl(struct ata_port *ap)
31833267325STejun Heo {
31933267325STejun Heo 	int i;
32033267325STejun Heo 
32133267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
32233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
32333267325STejun Heo 
32433267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
32533267325STejun Heo 			continue;
32633267325STejun Heo 
32733267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
32833267325STejun Heo 			continue;
32933267325STejun Heo 
33033267325STejun Heo 		ap->cbl = fe->param.cbl;
33133267325STejun Heo 		ata_port_printk(ap, KERN_NOTICE,
33233267325STejun Heo 				"FORCE: cable set to %s\n", fe->param.name);
33333267325STejun Heo 		return;
33433267325STejun Heo 	}
33533267325STejun Heo }
33633267325STejun Heo 
33733267325STejun Heo /**
33805944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
33933267325STejun Heo  *	@link: ATA link of interest
34033267325STejun Heo  *
34105944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
34205944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
34305944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
34405944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
34505944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
34605944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
347b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
348b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
34933267325STejun Heo  *
35033267325STejun Heo  *	LOCKING:
35133267325STejun Heo  *	EH context.
35233267325STejun Heo  */
35305944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
35433267325STejun Heo {
35505944bdfSTejun Heo 	bool did_spd = false;
356b1c72916STejun Heo 	int linkno = link->pmp;
357b1c72916STejun Heo 	int i;
35833267325STejun Heo 
35933267325STejun Heo 	if (ata_is_host_link(link))
360b1c72916STejun Heo 		linkno += 15;
36133267325STejun Heo 
36233267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
36333267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
36433267325STejun Heo 
36533267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
36633267325STejun Heo 			continue;
36733267325STejun Heo 
36833267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
36933267325STejun Heo 			continue;
37033267325STejun Heo 
37105944bdfSTejun Heo 		/* only honor the first spd limit */
37205944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
37333267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
37433267325STejun Heo 			ata_link_printk(link, KERN_NOTICE,
37505944bdfSTejun Heo 					"FORCE: PHY spd limit set to %s\n",
37605944bdfSTejun Heo 					fe->param.name);
37705944bdfSTejun Heo 			did_spd = true;
37805944bdfSTejun Heo 		}
37905944bdfSTejun Heo 
38005944bdfSTejun Heo 		/* let lflags stack */
38105944bdfSTejun Heo 		if (fe->param.lflags) {
38205944bdfSTejun Heo 			link->flags |= fe->param.lflags;
38305944bdfSTejun Heo 			ata_link_printk(link, KERN_NOTICE,
38405944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
38505944bdfSTejun Heo 					fe->param.lflags, link->flags);
38605944bdfSTejun Heo 		}
38733267325STejun Heo 	}
38833267325STejun Heo }
38933267325STejun Heo 
39033267325STejun Heo /**
39133267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
39233267325STejun Heo  *	@dev: ATA device of interest
39333267325STejun Heo  *
39433267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
39533267325STejun Heo  *	For consistency with link selection, device number 15 selects
39633267325STejun Heo  *	the first device connected to the host link.
39733267325STejun Heo  *
39833267325STejun Heo  *	LOCKING:
39933267325STejun Heo  *	EH context.
40033267325STejun Heo  */
40133267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
40233267325STejun Heo {
40333267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
40433267325STejun Heo 	int alt_devno = devno;
40533267325STejun Heo 	int i;
40633267325STejun Heo 
407b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
408b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
409b1c72916STejun Heo 		alt_devno += 15;
41033267325STejun Heo 
41133267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
41233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
41333267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
41433267325STejun Heo 
41533267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
41633267325STejun Heo 			continue;
41733267325STejun Heo 
41833267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
41933267325STejun Heo 		    fe->device != alt_devno)
42033267325STejun Heo 			continue;
42133267325STejun Heo 
42233267325STejun Heo 		if (!fe->param.xfer_mask)
42333267325STejun Heo 			continue;
42433267325STejun Heo 
42533267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
42633267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
42733267325STejun Heo 		if (udma_mask)
42833267325STejun Heo 			dev->udma_mask = udma_mask;
42933267325STejun Heo 		else if (mwdma_mask) {
43033267325STejun Heo 			dev->udma_mask = 0;
43133267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
43233267325STejun Heo 		} else {
43333267325STejun Heo 			dev->udma_mask = 0;
43433267325STejun Heo 			dev->mwdma_mask = 0;
43533267325STejun Heo 			dev->pio_mask = pio_mask;
43633267325STejun Heo 		}
43733267325STejun Heo 
43833267325STejun Heo 		ata_dev_printk(dev, KERN_NOTICE,
43933267325STejun Heo 			"FORCE: xfer_mask set to %s\n", fe->param.name);
44033267325STejun Heo 		return;
44133267325STejun Heo 	}
44233267325STejun Heo }
44333267325STejun Heo 
44433267325STejun Heo /**
44533267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
44633267325STejun Heo  *	@dev: ATA device of interest
44733267325STejun Heo  *
44833267325STejun Heo  *	Force horkage according to libata.force and whine about it.
44933267325STejun Heo  *	For consistency with link selection, device number 15 selects
45033267325STejun Heo  *	the first device connected to the host link.
45133267325STejun Heo  *
45233267325STejun Heo  *	LOCKING:
45333267325STejun Heo  *	EH context.
45433267325STejun Heo  */
45533267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
45633267325STejun Heo {
45733267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
45833267325STejun Heo 	int alt_devno = devno;
45933267325STejun Heo 	int i;
46033267325STejun Heo 
461b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
462b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
463b1c72916STejun Heo 		alt_devno += 15;
46433267325STejun Heo 
46533267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
46633267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
46733267325STejun Heo 
46833267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
46933267325STejun Heo 			continue;
47033267325STejun Heo 
47133267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
47233267325STejun Heo 		    fe->device != alt_devno)
47333267325STejun Heo 			continue;
47433267325STejun Heo 
47533267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
47633267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
47733267325STejun Heo 			continue;
47833267325STejun Heo 
47933267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
48033267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
48133267325STejun Heo 
48233267325STejun Heo 		ata_dev_printk(dev, KERN_NOTICE,
48333267325STejun Heo 			"FORCE: horkage modified (%s)\n", fe->param.name);
48433267325STejun Heo 	}
48533267325STejun Heo }
48633267325STejun Heo 
48733267325STejun Heo /**
488436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
489436d34b3STejun Heo  *	@opcode: SCSI opcode
490436d34b3STejun Heo  *
491436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
492436d34b3STejun Heo  *
493436d34b3STejun Heo  *	LOCKING:
494436d34b3STejun Heo  *	None.
495436d34b3STejun Heo  *
496436d34b3STejun Heo  *	RETURNS:
497436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
498436d34b3STejun Heo  */
499436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
500436d34b3STejun Heo {
501436d34b3STejun Heo 	switch (opcode) {
502436d34b3STejun Heo 	case GPCMD_READ_10:
503436d34b3STejun Heo 	case GPCMD_READ_12:
504436d34b3STejun Heo 		return ATAPI_READ;
505436d34b3STejun Heo 
506436d34b3STejun Heo 	case GPCMD_WRITE_10:
507436d34b3STejun Heo 	case GPCMD_WRITE_12:
508436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
509436d34b3STejun Heo 		return ATAPI_WRITE;
510436d34b3STejun Heo 
511436d34b3STejun Heo 	case GPCMD_READ_CD:
512436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
513436d34b3STejun Heo 		return ATAPI_READ_CD;
514436d34b3STejun Heo 
515e52dcc48STejun Heo 	case ATA_16:
516e52dcc48STejun Heo 	case ATA_12:
517e52dcc48STejun Heo 		if (atapi_passthru16)
518e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
519e52dcc48STejun Heo 		/* fall thru */
520436d34b3STejun Heo 	default:
521436d34b3STejun Heo 		return ATAPI_MISC;
522436d34b3STejun Heo 	}
523436d34b3STejun Heo }
524436d34b3STejun Heo 
525436d34b3STejun Heo /**
526c6fd2807SJeff Garzik  *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
527c6fd2807SJeff Garzik  *	@tf: Taskfile to convert
528c6fd2807SJeff Garzik  *	@pmp: Port multiplier port
5299977126cSTejun Heo  *	@is_cmd: This FIS is for command
5309977126cSTejun Heo  *	@fis: Buffer into which data will output
531c6fd2807SJeff Garzik  *
532c6fd2807SJeff Garzik  *	Converts a standard ATA taskfile to a Serial ATA
533c6fd2807SJeff Garzik  *	FIS structure (Register - Host to Device).
534c6fd2807SJeff Garzik  *
535c6fd2807SJeff Garzik  *	LOCKING:
536c6fd2807SJeff Garzik  *	Inherited from caller.
537c6fd2807SJeff Garzik  */
5389977126cSTejun Heo void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
539c6fd2807SJeff Garzik {
540c6fd2807SJeff Garzik 	fis[0] = 0x27;			/* Register - Host to Device FIS */
5419977126cSTejun Heo 	fis[1] = pmp & 0xf;		/* Port multiplier number*/
5429977126cSTejun Heo 	if (is_cmd)
5439977126cSTejun Heo 		fis[1] |= (1 << 7);	/* bit 7 indicates Command FIS */
5449977126cSTejun Heo 
545c6fd2807SJeff Garzik 	fis[2] = tf->command;
546c6fd2807SJeff Garzik 	fis[3] = tf->feature;
547c6fd2807SJeff Garzik 
548c6fd2807SJeff Garzik 	fis[4] = tf->lbal;
549c6fd2807SJeff Garzik 	fis[5] = tf->lbam;
550c6fd2807SJeff Garzik 	fis[6] = tf->lbah;
551c6fd2807SJeff Garzik 	fis[7] = tf->device;
552c6fd2807SJeff Garzik 
553c6fd2807SJeff Garzik 	fis[8] = tf->hob_lbal;
554c6fd2807SJeff Garzik 	fis[9] = tf->hob_lbam;
555c6fd2807SJeff Garzik 	fis[10] = tf->hob_lbah;
556c6fd2807SJeff Garzik 	fis[11] = tf->hob_feature;
557c6fd2807SJeff Garzik 
558c6fd2807SJeff Garzik 	fis[12] = tf->nsect;
559c6fd2807SJeff Garzik 	fis[13] = tf->hob_nsect;
560c6fd2807SJeff Garzik 	fis[14] = 0;
561c6fd2807SJeff Garzik 	fis[15] = tf->ctl;
562c6fd2807SJeff Garzik 
563c6fd2807SJeff Garzik 	fis[16] = 0;
564c6fd2807SJeff Garzik 	fis[17] = 0;
565c6fd2807SJeff Garzik 	fis[18] = 0;
566c6fd2807SJeff Garzik 	fis[19] = 0;
567c6fd2807SJeff Garzik }
568c6fd2807SJeff Garzik 
569c6fd2807SJeff Garzik /**
570c6fd2807SJeff Garzik  *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
571c6fd2807SJeff Garzik  *	@fis: Buffer from which data will be input
572c6fd2807SJeff Garzik  *	@tf: Taskfile to output
573c6fd2807SJeff Garzik  *
574c6fd2807SJeff Garzik  *	Converts a serial ATA FIS structure to a standard ATA taskfile.
575c6fd2807SJeff Garzik  *
576c6fd2807SJeff Garzik  *	LOCKING:
577c6fd2807SJeff Garzik  *	Inherited from caller.
578c6fd2807SJeff Garzik  */
579c6fd2807SJeff Garzik 
580c6fd2807SJeff Garzik void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
581c6fd2807SJeff Garzik {
582c6fd2807SJeff Garzik 	tf->command	= fis[2];	/* status */
583c6fd2807SJeff Garzik 	tf->feature	= fis[3];	/* error */
584c6fd2807SJeff Garzik 
585c6fd2807SJeff Garzik 	tf->lbal	= fis[4];
586c6fd2807SJeff Garzik 	tf->lbam	= fis[5];
587c6fd2807SJeff Garzik 	tf->lbah	= fis[6];
588c6fd2807SJeff Garzik 	tf->device	= fis[7];
589c6fd2807SJeff Garzik 
590c6fd2807SJeff Garzik 	tf->hob_lbal	= fis[8];
591c6fd2807SJeff Garzik 	tf->hob_lbam	= fis[9];
592c6fd2807SJeff Garzik 	tf->hob_lbah	= fis[10];
593c6fd2807SJeff Garzik 
594c6fd2807SJeff Garzik 	tf->nsect	= fis[12];
595c6fd2807SJeff Garzik 	tf->hob_nsect	= fis[13];
596c6fd2807SJeff Garzik }
597c6fd2807SJeff Garzik 
598c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
599c6fd2807SJeff Garzik 	/* pio multi */
600c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
601c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
602c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
603c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
604c6fd2807SJeff Garzik 	0,
605c6fd2807SJeff Garzik 	0,
606c6fd2807SJeff Garzik 	0,
607c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
608c6fd2807SJeff Garzik 	/* pio */
609c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
610c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
611c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
612c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
613c6fd2807SJeff Garzik 	0,
614c6fd2807SJeff Garzik 	0,
615c6fd2807SJeff Garzik 	0,
616c6fd2807SJeff Garzik 	0,
617c6fd2807SJeff Garzik 	/* dma */
618c6fd2807SJeff Garzik 	ATA_CMD_READ,
619c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
620c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
621c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
622c6fd2807SJeff Garzik 	0,
623c6fd2807SJeff Garzik 	0,
624c6fd2807SJeff Garzik 	0,
625c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
626c6fd2807SJeff Garzik };
627c6fd2807SJeff Garzik 
628c6fd2807SJeff Garzik /**
629c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
630bd056d7eSTejun Heo  *	@tf: command to examine and configure
631bd056d7eSTejun Heo  *	@dev: device tf belongs to
632c6fd2807SJeff Garzik  *
633c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
634c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
635c6fd2807SJeff Garzik  *
636c6fd2807SJeff Garzik  *	LOCKING:
637c6fd2807SJeff Garzik  *	caller.
638c6fd2807SJeff Garzik  */
639bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
640c6fd2807SJeff Garzik {
641c6fd2807SJeff Garzik 	u8 cmd;
642c6fd2807SJeff Garzik 
643c6fd2807SJeff Garzik 	int index, fua, lba48, write;
644c6fd2807SJeff Garzik 
645c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
646c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
647c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
648c6fd2807SJeff Garzik 
649c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
650c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
651c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6529af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
653c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
654c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
655c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
656c6fd2807SJeff Garzik 	} else {
657c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
658c6fd2807SJeff Garzik 		index = 16;
659c6fd2807SJeff Garzik 	}
660c6fd2807SJeff Garzik 
661c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
662c6fd2807SJeff Garzik 	if (cmd) {
663c6fd2807SJeff Garzik 		tf->command = cmd;
664c6fd2807SJeff Garzik 		return 0;
665c6fd2807SJeff Garzik 	}
666c6fd2807SJeff Garzik 	return -1;
667c6fd2807SJeff Garzik }
668c6fd2807SJeff Garzik 
669c6fd2807SJeff Garzik /**
67035b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
67135b649feSTejun Heo  *	@tf: ATA taskfile of interest
67235b649feSTejun Heo  *	@dev: ATA device @tf belongs to
67335b649feSTejun Heo  *
67435b649feSTejun Heo  *	LOCKING:
67535b649feSTejun Heo  *	None.
67635b649feSTejun Heo  *
67735b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
67835b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
67935b649feSTejun Heo  *	flags select the address format to use.
68035b649feSTejun Heo  *
68135b649feSTejun Heo  *	RETURNS:
68235b649feSTejun Heo  *	Block address read from @tf.
68335b649feSTejun Heo  */
68435b649feSTejun Heo u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
68535b649feSTejun Heo {
68635b649feSTejun Heo 	u64 block = 0;
68735b649feSTejun Heo 
68835b649feSTejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
68935b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
69035b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
69135b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
69244901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
69335b649feSTejun Heo 		} else
69435b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
69535b649feSTejun Heo 
69635b649feSTejun Heo 		block |= tf->lbah << 16;
69735b649feSTejun Heo 		block |= tf->lbam << 8;
69835b649feSTejun Heo 		block |= tf->lbal;
69935b649feSTejun Heo 	} else {
70035b649feSTejun Heo 		u32 cyl, head, sect;
70135b649feSTejun Heo 
70235b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
70335b649feSTejun Heo 		head = tf->device & 0xf;
70435b649feSTejun Heo 		sect = tf->lbal;
70535b649feSTejun Heo 
70635b649feSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect;
70735b649feSTejun Heo 	}
70835b649feSTejun Heo 
70935b649feSTejun Heo 	return block;
71035b649feSTejun Heo }
71135b649feSTejun Heo 
71235b649feSTejun Heo /**
713bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
714bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
715bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
716bd056d7eSTejun Heo  *	@block: Block address
717bd056d7eSTejun Heo  *	@n_block: Number of blocks
718bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
719bd056d7eSTejun Heo  *	@tag: tag
720bd056d7eSTejun Heo  *
721bd056d7eSTejun Heo  *	LOCKING:
722bd056d7eSTejun Heo  *	None.
723bd056d7eSTejun Heo  *
724bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
725bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
726bd056d7eSTejun Heo  *
727bd056d7eSTejun Heo  *	RETURNS:
728bd056d7eSTejun Heo  *
729bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
730bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
731bd056d7eSTejun Heo  */
732bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
733bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
734bd056d7eSTejun Heo 		    unsigned int tag)
735bd056d7eSTejun Heo {
736bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
737bd056d7eSTejun Heo 	tf->flags |= tf_flags;
738bd056d7eSTejun Heo 
7396d1245bfSTejun Heo 	if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
740bd056d7eSTejun Heo 		/* yay, NCQ */
741bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
742bd056d7eSTejun Heo 			return -ERANGE;
743bd056d7eSTejun Heo 
744bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
745bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
746bd056d7eSTejun Heo 
747bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
748bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
749bd056d7eSTejun Heo 		else
750bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
751bd056d7eSTejun Heo 
752bd056d7eSTejun Heo 		tf->nsect = tag << 3;
753bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
754bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
755bd056d7eSTejun Heo 
756bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
757bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
758bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
759bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
760bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
761bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
762bd056d7eSTejun Heo 
763bd056d7eSTejun Heo 		tf->device = 1 << 6;
764bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
765bd056d7eSTejun Heo 			tf->device |= 1 << 7;
766bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
767bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
768bd056d7eSTejun Heo 
769bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
770bd056d7eSTejun Heo 			/* use LBA28 */
771bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
772bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
773bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
774bd056d7eSTejun Heo 				return -ERANGE;
775bd056d7eSTejun Heo 
776bd056d7eSTejun Heo 			/* use LBA48 */
777bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
778bd056d7eSTejun Heo 
779bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
780bd056d7eSTejun Heo 
781bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
782bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
783bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
784bd056d7eSTejun Heo 		} else
785bd056d7eSTejun Heo 			/* request too large even for LBA48 */
786bd056d7eSTejun Heo 			return -ERANGE;
787bd056d7eSTejun Heo 
788bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
789bd056d7eSTejun Heo 			return -EINVAL;
790bd056d7eSTejun Heo 
791bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
792bd056d7eSTejun Heo 
793bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
794bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
795bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
796bd056d7eSTejun Heo 
797bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
798bd056d7eSTejun Heo 	} else {
799bd056d7eSTejun Heo 		/* CHS */
800bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
801bd056d7eSTejun Heo 
802bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
803bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
804bd056d7eSTejun Heo 			return -ERANGE;
805bd056d7eSTejun Heo 
806bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
807bd056d7eSTejun Heo 			return -EINVAL;
808bd056d7eSTejun Heo 
809bd056d7eSTejun Heo 		/* Convert LBA to CHS */
810bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
811bd056d7eSTejun Heo 		cyl   = track / dev->heads;
812bd056d7eSTejun Heo 		head  = track % dev->heads;
813bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
814bd056d7eSTejun Heo 
815bd056d7eSTejun Heo 		DPRINTK("block %u track %u cyl %u head %u sect %u\n",
816bd056d7eSTejun Heo 			(u32)block, track, cyl, head, sect);
817bd056d7eSTejun Heo 
818bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
819bd056d7eSTejun Heo 		   Cylinder: 0-65535
820bd056d7eSTejun Heo 		   Head: 0-15
821bd056d7eSTejun Heo 		   Sector: 1-255*/
822bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
823bd056d7eSTejun Heo 			return -ERANGE;
824bd056d7eSTejun Heo 
825bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
826bd056d7eSTejun Heo 		tf->lbal = sect;
827bd056d7eSTejun Heo 		tf->lbam = cyl;
828bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
829bd056d7eSTejun Heo 		tf->device |= head;
830bd056d7eSTejun Heo 	}
831bd056d7eSTejun Heo 
832bd056d7eSTejun Heo 	return 0;
833bd056d7eSTejun Heo }
834bd056d7eSTejun Heo 
835bd056d7eSTejun Heo /**
836c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
837c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
838c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
839c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
840c6fd2807SJeff Garzik  *
841c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
842c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
843c6fd2807SJeff Garzik  *
844c6fd2807SJeff Garzik  *	LOCKING:
845c6fd2807SJeff Garzik  *	None.
846c6fd2807SJeff Garzik  *
847c6fd2807SJeff Garzik  *	RETURNS:
848c6fd2807SJeff Garzik  *	Packed xfer_mask.
849c6fd2807SJeff Garzik  */
8507dc951aeSTejun Heo unsigned long ata_pack_xfermask(unsigned long pio_mask,
8517dc951aeSTejun Heo 				unsigned long mwdma_mask,
8527dc951aeSTejun Heo 				unsigned long udma_mask)
853c6fd2807SJeff Garzik {
854c6fd2807SJeff Garzik 	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
855c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
856c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
857c6fd2807SJeff Garzik }
858c6fd2807SJeff Garzik 
859c6fd2807SJeff Garzik /**
860c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
861c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
862c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
863c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
864c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
865c6fd2807SJeff Garzik  *
866c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
867c6fd2807SJeff Garzik  *	Any NULL distination masks will be ignored.
868c6fd2807SJeff Garzik  */
8697dc951aeSTejun Heo void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
8707dc951aeSTejun Heo 			 unsigned long *mwdma_mask, unsigned long *udma_mask)
871c6fd2807SJeff Garzik {
872c6fd2807SJeff Garzik 	if (pio_mask)
873c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
874c6fd2807SJeff Garzik 	if (mwdma_mask)
875c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
876c6fd2807SJeff Garzik 	if (udma_mask)
877c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
878c6fd2807SJeff Garzik }
879c6fd2807SJeff Garzik 
880c6fd2807SJeff Garzik static const struct ata_xfer_ent {
881c6fd2807SJeff Garzik 	int shift, bits;
882c6fd2807SJeff Garzik 	u8 base;
883c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
88470cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
88570cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
88670cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
887c6fd2807SJeff Garzik 	{ -1, },
888c6fd2807SJeff Garzik };
889c6fd2807SJeff Garzik 
890c6fd2807SJeff Garzik /**
891c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
892c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
893c6fd2807SJeff Garzik  *
894c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
895c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
896c6fd2807SJeff Garzik  *
897c6fd2807SJeff Garzik  *	LOCKING:
898c6fd2807SJeff Garzik  *	None.
899c6fd2807SJeff Garzik  *
900c6fd2807SJeff Garzik  *	RETURNS:
90170cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
902c6fd2807SJeff Garzik  */
9037dc951aeSTejun Heo u8 ata_xfer_mask2mode(unsigned long xfer_mask)
904c6fd2807SJeff Garzik {
905c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
906c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
907c6fd2807SJeff Garzik 
908c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
909c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
910c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
91170cd071eSTejun Heo 	return 0xff;
912c6fd2807SJeff Garzik }
913c6fd2807SJeff Garzik 
914c6fd2807SJeff Garzik /**
915c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
916c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
917c6fd2807SJeff Garzik  *
918c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
919c6fd2807SJeff Garzik  *
920c6fd2807SJeff Garzik  *	LOCKING:
921c6fd2807SJeff Garzik  *	None.
922c6fd2807SJeff Garzik  *
923c6fd2807SJeff Garzik  *	RETURNS:
924c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
925c6fd2807SJeff Garzik  */
9267dc951aeSTejun Heo unsigned long ata_xfer_mode2mask(u8 xfer_mode)
927c6fd2807SJeff Garzik {
928c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
929c6fd2807SJeff Garzik 
930c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
931c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
93270cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
93370cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
934c6fd2807SJeff Garzik 	return 0;
935c6fd2807SJeff Garzik }
936c6fd2807SJeff Garzik 
937c6fd2807SJeff Garzik /**
938c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
939c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
940c6fd2807SJeff Garzik  *
941c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
942c6fd2807SJeff Garzik  *
943c6fd2807SJeff Garzik  *	LOCKING:
944c6fd2807SJeff Garzik  *	None.
945c6fd2807SJeff Garzik  *
946c6fd2807SJeff Garzik  *	RETURNS:
947c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
948c6fd2807SJeff Garzik  */
9497dc951aeSTejun Heo int ata_xfer_mode2shift(unsigned long xfer_mode)
950c6fd2807SJeff Garzik {
951c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
952c6fd2807SJeff Garzik 
953c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
954c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
955c6fd2807SJeff Garzik 			return ent->shift;
956c6fd2807SJeff Garzik 	return -1;
957c6fd2807SJeff Garzik }
958c6fd2807SJeff Garzik 
959c6fd2807SJeff Garzik /**
960c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
961c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
962c6fd2807SJeff Garzik  *
963c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
964c6fd2807SJeff Garzik  *	(highest bit in @modemask).
965c6fd2807SJeff Garzik  *
966c6fd2807SJeff Garzik  *	LOCKING:
967c6fd2807SJeff Garzik  *	None.
968c6fd2807SJeff Garzik  *
969c6fd2807SJeff Garzik  *	RETURNS:
970c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
971c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
972c6fd2807SJeff Garzik  */
9737dc951aeSTejun Heo const char *ata_mode_string(unsigned long xfer_mask)
974c6fd2807SJeff Garzik {
975c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
976c6fd2807SJeff Garzik 		"PIO0",
977c6fd2807SJeff Garzik 		"PIO1",
978c6fd2807SJeff Garzik 		"PIO2",
979c6fd2807SJeff Garzik 		"PIO3",
980c6fd2807SJeff Garzik 		"PIO4",
981b352e57dSAlan Cox 		"PIO5",
982b352e57dSAlan Cox 		"PIO6",
983c6fd2807SJeff Garzik 		"MWDMA0",
984c6fd2807SJeff Garzik 		"MWDMA1",
985c6fd2807SJeff Garzik 		"MWDMA2",
986b352e57dSAlan Cox 		"MWDMA3",
987b352e57dSAlan Cox 		"MWDMA4",
988c6fd2807SJeff Garzik 		"UDMA/16",
989c6fd2807SJeff Garzik 		"UDMA/25",
990c6fd2807SJeff Garzik 		"UDMA/33",
991c6fd2807SJeff Garzik 		"UDMA/44",
992c6fd2807SJeff Garzik 		"UDMA/66",
993c6fd2807SJeff Garzik 		"UDMA/100",
994c6fd2807SJeff Garzik 		"UDMA/133",
995c6fd2807SJeff Garzik 		"UDMA7",
996c6fd2807SJeff Garzik 	};
997c6fd2807SJeff Garzik 	int highbit;
998c6fd2807SJeff Garzik 
999c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
1000c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1001c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
1002c6fd2807SJeff Garzik 	return "<n/a>";
1003c6fd2807SJeff Garzik }
1004c6fd2807SJeff Garzik 
1005c6fd2807SJeff Garzik static const char *sata_spd_string(unsigned int spd)
1006c6fd2807SJeff Garzik {
1007c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
1008c6fd2807SJeff Garzik 		"1.5 Gbps",
1009c6fd2807SJeff Garzik 		"3.0 Gbps",
10108522ee25SShane Huang 		"6.0 Gbps",
1011c6fd2807SJeff Garzik 	};
1012c6fd2807SJeff Garzik 
1013c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1014c6fd2807SJeff Garzik 		return "<unknown>";
1015c6fd2807SJeff Garzik 	return spd_str[spd - 1];
1016c6fd2807SJeff Garzik }
1017c6fd2807SJeff Garzik 
1018c6fd2807SJeff Garzik void ata_dev_disable(struct ata_device *dev)
1019c6fd2807SJeff Garzik {
102009d7f9b0STejun Heo 	if (ata_dev_enabled(dev)) {
10219af5c9c9STejun Heo 		if (ata_msg_drv(dev->link->ap))
1022c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_WARNING, "disabled\n");
1023562f0c2dSTejun Heo 		ata_acpi_on_disable(dev);
10244ae72a1eSTejun Heo 		ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
10254ae72a1eSTejun Heo 					     ATA_DNXFER_QUIET);
1026c6fd2807SJeff Garzik 		dev->class++;
1027c6fd2807SJeff Garzik 	}
1028c6fd2807SJeff Garzik }
1029c6fd2807SJeff Garzik 
1030ca77329fSKristen Carlson Accardi static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
1031ca77329fSKristen Carlson Accardi {
1032ca77329fSKristen Carlson Accardi 	struct ata_link *link = dev->link;
1033ca77329fSKristen Carlson Accardi 	struct ata_port *ap = link->ap;
1034ca77329fSKristen Carlson Accardi 	u32 scontrol;
1035ca77329fSKristen Carlson Accardi 	unsigned int err_mask;
1036ca77329fSKristen Carlson Accardi 	int rc;
1037ca77329fSKristen Carlson Accardi 
1038ca77329fSKristen Carlson Accardi 	/*
1039ca77329fSKristen Carlson Accardi 	 * disallow DIPM for drivers which haven't set
1040ca77329fSKristen Carlson Accardi 	 * ATA_FLAG_IPM.  This is because when DIPM is enabled,
1041ca77329fSKristen Carlson Accardi 	 * phy ready will be set in the interrupt status on
1042ca77329fSKristen Carlson Accardi 	 * state changes, which will cause some drivers to
1043ca77329fSKristen Carlson Accardi 	 * think there are errors - additionally drivers will
1044ca77329fSKristen Carlson Accardi 	 * need to disable hot plug.
1045ca77329fSKristen Carlson Accardi 	 */
1046ca77329fSKristen Carlson Accardi 	if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
1047ca77329fSKristen Carlson Accardi 		ap->pm_policy = NOT_AVAILABLE;
1048ca77329fSKristen Carlson Accardi 		return -EINVAL;
1049ca77329fSKristen Carlson Accardi 	}
1050ca77329fSKristen Carlson Accardi 
1051ca77329fSKristen Carlson Accardi 	/*
1052ca77329fSKristen Carlson Accardi 	 * For DIPM, we will only enable it for the
1053ca77329fSKristen Carlson Accardi 	 * min_power setting.
1054ca77329fSKristen Carlson Accardi 	 *
1055ca77329fSKristen Carlson Accardi 	 * Why?  Because Disks are too stupid to know that
1056ca77329fSKristen Carlson Accardi 	 * If the host rejects a request to go to SLUMBER
1057ca77329fSKristen Carlson Accardi 	 * they should retry at PARTIAL, and instead it
1058ca77329fSKristen Carlson Accardi 	 * just would give up.  So, for medium_power to
1059ca77329fSKristen Carlson Accardi 	 * work at all, we need to only allow HIPM.
1060ca77329fSKristen Carlson Accardi 	 */
1061ca77329fSKristen Carlson Accardi 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
1062ca77329fSKristen Carlson Accardi 	if (rc)
1063ca77329fSKristen Carlson Accardi 		return rc;
1064ca77329fSKristen Carlson Accardi 
1065ca77329fSKristen Carlson Accardi 	switch (policy) {
1066ca77329fSKristen Carlson Accardi 	case MIN_POWER:
1067ca77329fSKristen Carlson Accardi 		/* no restrictions on IPM transitions */
1068ca77329fSKristen Carlson Accardi 		scontrol &= ~(0x3 << 8);
1069ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1070ca77329fSKristen Carlson Accardi 		if (rc)
1071ca77329fSKristen Carlson Accardi 			return rc;
1072ca77329fSKristen Carlson Accardi 
1073ca77329fSKristen Carlson Accardi 		/* enable DIPM */
1074ca77329fSKristen Carlson Accardi 		if (dev->flags & ATA_DFLAG_DIPM)
1075ca77329fSKristen Carlson Accardi 			err_mask = ata_dev_set_feature(dev,
1076ca77329fSKristen Carlson Accardi 					SETFEATURES_SATA_ENABLE, SATA_DIPM);
1077ca77329fSKristen Carlson Accardi 		break;
1078ca77329fSKristen Carlson Accardi 	case MEDIUM_POWER:
1079ca77329fSKristen Carlson Accardi 		/* allow IPM to PARTIAL */
1080ca77329fSKristen Carlson Accardi 		scontrol &= ~(0x1 << 8);
1081ca77329fSKristen Carlson Accardi 		scontrol |= (0x2 << 8);
1082ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1083ca77329fSKristen Carlson Accardi 		if (rc)
1084ca77329fSKristen Carlson Accardi 			return rc;
1085ca77329fSKristen Carlson Accardi 
1086f5456b63SKristen Carlson Accardi 		/*
1087f5456b63SKristen Carlson Accardi 		 * we don't have to disable DIPM since IPM flags
1088f5456b63SKristen Carlson Accardi 		 * disallow transitions to SLUMBER, which effectively
1089f5456b63SKristen Carlson Accardi 		 * disable DIPM if it does not support PARTIAL
1090f5456b63SKristen Carlson Accardi 		 */
1091ca77329fSKristen Carlson Accardi 		break;
1092ca77329fSKristen Carlson Accardi 	case NOT_AVAILABLE:
1093ca77329fSKristen Carlson Accardi 	case MAX_PERFORMANCE:
1094ca77329fSKristen Carlson Accardi 		/* disable all IPM transitions */
1095ca77329fSKristen Carlson Accardi 		scontrol |= (0x3 << 8);
1096ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1097ca77329fSKristen Carlson Accardi 		if (rc)
1098ca77329fSKristen Carlson Accardi 			return rc;
1099ca77329fSKristen Carlson Accardi 
1100f5456b63SKristen Carlson Accardi 		/*
1101f5456b63SKristen Carlson Accardi 		 * we don't have to disable DIPM since IPM flags
1102f5456b63SKristen Carlson Accardi 		 * disallow all transitions which effectively
1103f5456b63SKristen Carlson Accardi 		 * disable DIPM anyway.
1104f5456b63SKristen Carlson Accardi 		 */
1105ca77329fSKristen Carlson Accardi 		break;
1106ca77329fSKristen Carlson Accardi 	}
1107ca77329fSKristen Carlson Accardi 
1108ca77329fSKristen Carlson Accardi 	/* FIXME: handle SET FEATURES failure */
1109ca77329fSKristen Carlson Accardi 	(void) err_mask;
1110ca77329fSKristen Carlson Accardi 
1111ca77329fSKristen Carlson Accardi 	return 0;
1112ca77329fSKristen Carlson Accardi }
1113ca77329fSKristen Carlson Accardi 
1114ca77329fSKristen Carlson Accardi /**
1115ca77329fSKristen Carlson Accardi  *	ata_dev_enable_pm - enable SATA interface power management
111648166fd9SStephen Hemminger  *	@dev:  device to enable power management
111748166fd9SStephen Hemminger  *	@policy: the link power management policy
1118ca77329fSKristen Carlson Accardi  *
1119ca77329fSKristen Carlson Accardi  *	Enable SATA Interface power management.  This will enable
1120ca77329fSKristen Carlson Accardi  *	Device Interface Power Management (DIPM) for min_power
1121ca77329fSKristen Carlson Accardi  * 	policy, and then call driver specific callbacks for
1122ca77329fSKristen Carlson Accardi  *	enabling Host Initiated Power management.
1123ca77329fSKristen Carlson Accardi  *
1124ca77329fSKristen Carlson Accardi  *	Locking: Caller.
1125ca77329fSKristen Carlson Accardi  *	Returns: -EINVAL if IPM is not supported, 0 otherwise.
1126ca77329fSKristen Carlson Accardi  */
1127ca77329fSKristen Carlson Accardi void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
1128ca77329fSKristen Carlson Accardi {
1129ca77329fSKristen Carlson Accardi 	int rc = 0;
1130ca77329fSKristen Carlson Accardi 	struct ata_port *ap = dev->link->ap;
1131ca77329fSKristen Carlson Accardi 
1132ca77329fSKristen Carlson Accardi 	/* set HIPM first, then DIPM */
1133ca77329fSKristen Carlson Accardi 	if (ap->ops->enable_pm)
1134ca77329fSKristen Carlson Accardi 		rc = ap->ops->enable_pm(ap, policy);
1135ca77329fSKristen Carlson Accardi 	if (rc)
1136ca77329fSKristen Carlson Accardi 		goto enable_pm_out;
1137ca77329fSKristen Carlson Accardi 	rc = ata_dev_set_dipm(dev, policy);
1138ca77329fSKristen Carlson Accardi 
1139ca77329fSKristen Carlson Accardi enable_pm_out:
1140ca77329fSKristen Carlson Accardi 	if (rc)
1141ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
1142ca77329fSKristen Carlson Accardi 	else
1143ca77329fSKristen Carlson Accardi 		ap->pm_policy = policy;
1144ca77329fSKristen Carlson Accardi 	return /* rc */;	/* hopefully we can use 'rc' eventually */
1145ca77329fSKristen Carlson Accardi }
1146ca77329fSKristen Carlson Accardi 
11471992a5edSStephen Rothwell #ifdef CONFIG_PM
1148ca77329fSKristen Carlson Accardi /**
1149ca77329fSKristen Carlson Accardi  *	ata_dev_disable_pm - disable SATA interface power management
115048166fd9SStephen Hemminger  *	@dev: device to disable power management
1151ca77329fSKristen Carlson Accardi  *
1152ca77329fSKristen Carlson Accardi  *	Disable SATA Interface power management.  This will disable
1153ca77329fSKristen Carlson Accardi  *	Device Interface Power Management (DIPM) without changing
1154ca77329fSKristen Carlson Accardi  * 	policy,  call driver specific callbacks for disabling Host
1155ca77329fSKristen Carlson Accardi  * 	Initiated Power management.
1156ca77329fSKristen Carlson Accardi  *
1157ca77329fSKristen Carlson Accardi  *	Locking: Caller.
1158ca77329fSKristen Carlson Accardi  *	Returns: void
1159ca77329fSKristen Carlson Accardi  */
1160ca77329fSKristen Carlson Accardi static void ata_dev_disable_pm(struct ata_device *dev)
1161ca77329fSKristen Carlson Accardi {
1162ca77329fSKristen Carlson Accardi 	struct ata_port *ap = dev->link->ap;
1163ca77329fSKristen Carlson Accardi 
1164ca77329fSKristen Carlson Accardi 	ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1165ca77329fSKristen Carlson Accardi 	if (ap->ops->disable_pm)
1166ca77329fSKristen Carlson Accardi 		ap->ops->disable_pm(ap);
1167ca77329fSKristen Carlson Accardi }
11681992a5edSStephen Rothwell #endif	/* CONFIG_PM */
1169ca77329fSKristen Carlson Accardi 
1170ca77329fSKristen Carlson Accardi void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1171ca77329fSKristen Carlson Accardi {
1172ca77329fSKristen Carlson Accardi 	ap->pm_policy = policy;
11733ec25ebdSTejun Heo 	ap->link.eh_info.action |= ATA_EH_LPM;
1174ca77329fSKristen Carlson Accardi 	ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1175ca77329fSKristen Carlson Accardi 	ata_port_schedule_eh(ap);
1176ca77329fSKristen Carlson Accardi }
1177ca77329fSKristen Carlson Accardi 
11781992a5edSStephen Rothwell #ifdef CONFIG_PM
1179ca77329fSKristen Carlson Accardi static void ata_lpm_enable(struct ata_host *host)
1180ca77329fSKristen Carlson Accardi {
1181ca77329fSKristen Carlson Accardi 	struct ata_link *link;
1182ca77329fSKristen Carlson Accardi 	struct ata_port *ap;
1183ca77329fSKristen Carlson Accardi 	struct ata_device *dev;
1184ca77329fSKristen Carlson Accardi 	int i;
1185ca77329fSKristen Carlson Accardi 
1186ca77329fSKristen Carlson Accardi 	for (i = 0; i < host->n_ports; i++) {
1187ca77329fSKristen Carlson Accardi 		ap = host->ports[i];
11881eca4365STejun Heo 		ata_for_each_link(link, ap, EDGE) {
11891eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL)
1190ca77329fSKristen Carlson Accardi 				ata_dev_disable_pm(dev);
1191ca77329fSKristen Carlson Accardi 		}
1192ca77329fSKristen Carlson Accardi 	}
1193ca77329fSKristen Carlson Accardi }
1194ca77329fSKristen Carlson Accardi 
1195ca77329fSKristen Carlson Accardi static void ata_lpm_disable(struct ata_host *host)
1196ca77329fSKristen Carlson Accardi {
1197ca77329fSKristen Carlson Accardi 	int i;
1198ca77329fSKristen Carlson Accardi 
1199ca77329fSKristen Carlson Accardi 	for (i = 0; i < host->n_ports; i++) {
1200ca77329fSKristen Carlson Accardi 		struct ata_port *ap = host->ports[i];
1201ca77329fSKristen Carlson Accardi 		ata_lpm_schedule(ap, ap->pm_policy);
1202ca77329fSKristen Carlson Accardi 	}
1203ca77329fSKristen Carlson Accardi }
12041992a5edSStephen Rothwell #endif	/* CONFIG_PM */
1205ca77329fSKristen Carlson Accardi 
1206c6fd2807SJeff Garzik /**
1207c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
1208c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
1209c6fd2807SJeff Garzik  *
1210c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
1211c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
1212c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
1213c6fd2807SJeff Garzik  *
1214c6fd2807SJeff Garzik  *	LOCKING:
1215c6fd2807SJeff Garzik  *	None.
1216c6fd2807SJeff Garzik  *
1217c6fd2807SJeff Garzik  *	RETURNS:
1218633273a3STejun Heo  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1219633273a3STejun Heo  *	%ATA_DEV_UNKNOWN the event of failure.
1220c6fd2807SJeff Garzik  */
1221c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1222c6fd2807SJeff Garzik {
1223c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1224c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1225c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1226633273a3STejun Heo 	 *
1227633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1228633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1229633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1230633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1231633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1232633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1233633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1234633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1235633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1236633273a3STejun Heo 	 * SerialATA.
1237633273a3STejun Heo 	 *
1238633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1239633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1240c6fd2807SJeff Garzik 	 */
1241633273a3STejun Heo 	if ((tf->lbam == 0) && (tf->lbah == 0)) {
1242c6fd2807SJeff Garzik 		DPRINTK("found ATA device by sig\n");
1243c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1244c6fd2807SJeff Garzik 	}
1245c6fd2807SJeff Garzik 
1246633273a3STejun Heo 	if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1247c6fd2807SJeff Garzik 		DPRINTK("found ATAPI device by sig\n");
1248c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1249c6fd2807SJeff Garzik 	}
1250c6fd2807SJeff Garzik 
1251633273a3STejun Heo 	if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1252633273a3STejun Heo 		DPRINTK("found PMP device by sig\n");
1253633273a3STejun Heo 		return ATA_DEV_PMP;
1254633273a3STejun Heo 	}
1255633273a3STejun Heo 
1256633273a3STejun Heo 	if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
12572dcb407eSJeff Garzik 		printk(KERN_INFO "ata: SEMB device ignored\n");
1258633273a3STejun Heo 		return ATA_DEV_SEMB_UNSUP; /* not yet */
1259633273a3STejun Heo 	}
1260633273a3STejun Heo 
1261c6fd2807SJeff Garzik 	DPRINTK("unknown device\n");
1262c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1263c6fd2807SJeff Garzik }
1264c6fd2807SJeff Garzik 
1265c6fd2807SJeff Garzik /**
1266c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1267c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1268c6fd2807SJeff Garzik  *	@s: string into which data is output
1269c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1270c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1271c6fd2807SJeff Garzik  *
1272c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1273c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1274c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1275c6fd2807SJeff Garzik  *
1276c6fd2807SJeff Garzik  *	LOCKING:
1277c6fd2807SJeff Garzik  *	caller.
1278c6fd2807SJeff Garzik  */
1279c6fd2807SJeff Garzik 
1280c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1281c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1282c6fd2807SJeff Garzik {
1283c6fd2807SJeff Garzik 	unsigned int c;
1284c6fd2807SJeff Garzik 
1285963e4975SAlan Cox 	BUG_ON(len & 1);
1286963e4975SAlan Cox 
1287c6fd2807SJeff Garzik 	while (len > 0) {
1288c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1289c6fd2807SJeff Garzik 		*s = c;
1290c6fd2807SJeff Garzik 		s++;
1291c6fd2807SJeff Garzik 
1292c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1293c6fd2807SJeff Garzik 		*s = c;
1294c6fd2807SJeff Garzik 		s++;
1295c6fd2807SJeff Garzik 
1296c6fd2807SJeff Garzik 		ofs++;
1297c6fd2807SJeff Garzik 		len -= 2;
1298c6fd2807SJeff Garzik 	}
1299c6fd2807SJeff Garzik }
1300c6fd2807SJeff Garzik 
1301c6fd2807SJeff Garzik /**
1302c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1303c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1304c6fd2807SJeff Garzik  *	@s: string into which data is output
1305c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1306c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1307c6fd2807SJeff Garzik  *
1308c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1309c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1310c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1311c6fd2807SJeff Garzik  *
1312c6fd2807SJeff Garzik  *	LOCKING:
1313c6fd2807SJeff Garzik  *	caller.
1314c6fd2807SJeff Garzik  */
1315c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1316c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1317c6fd2807SJeff Garzik {
1318c6fd2807SJeff Garzik 	unsigned char *p;
1319c6fd2807SJeff Garzik 
1320c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1321c6fd2807SJeff Garzik 
1322c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1323c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1324c6fd2807SJeff Garzik 		p--;
1325c6fd2807SJeff Garzik 	*p = '\0';
1326c6fd2807SJeff Garzik }
1327c6fd2807SJeff Garzik 
1328db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1329db6f8759STejun Heo {
1330db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1331db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1332db6f8759STejun Heo 			return ata_id_u64(id, 100);
1333db6f8759STejun Heo 		else
1334db6f8759STejun Heo 			return ata_id_u32(id, 60);
1335db6f8759STejun Heo 	} else {
1336db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1337db6f8759STejun Heo 			return ata_id_u32(id, 57);
1338db6f8759STejun Heo 		else
1339db6f8759STejun Heo 			return id[1] * id[3] * id[6];
1340db6f8759STejun Heo 	}
1341db6f8759STejun Heo }
1342db6f8759STejun Heo 
1343a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
13441e999736SAlan Cox {
13451e999736SAlan Cox 	u64 sectors = 0;
13461e999736SAlan Cox 
13471e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
13481e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1349ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
13501e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
13511e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
13521e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
13531e999736SAlan Cox 
1354a5987e0aSTejun Heo 	return sectors;
13551e999736SAlan Cox }
13561e999736SAlan Cox 
1357a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
13581e999736SAlan Cox {
13591e999736SAlan Cox 	u64 sectors = 0;
13601e999736SAlan Cox 
13611e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
13621e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
13631e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
13641e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
13651e999736SAlan Cox 
1366a5987e0aSTejun Heo 	return sectors;
13671e999736SAlan Cox }
13681e999736SAlan Cox 
13691e999736SAlan Cox /**
1370c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1371c728a914STejun Heo  *	@dev: target device
1372c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
13731e999736SAlan Cox  *
1374c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1375c728a914STejun Heo  *	question.
1376c728a914STejun Heo  *
1377c728a914STejun Heo  *	RETURNS:
1378c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1379c728a914STejun Heo  *	-EIO on other errors.
13801e999736SAlan Cox  */
1381c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
13821e999736SAlan Cox {
1383c728a914STejun Heo 	unsigned int err_mask;
13841e999736SAlan Cox 	struct ata_taskfile tf;
1385c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
13861e999736SAlan Cox 
13871e999736SAlan Cox 	ata_tf_init(dev, &tf);
13881e999736SAlan Cox 
1389c728a914STejun Heo 	/* always clear all address registers */
13901e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1391c728a914STejun Heo 
1392c728a914STejun Heo 	if (lba48) {
1393c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1394c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1395c728a914STejun Heo 	} else
1396c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1397c728a914STejun Heo 
13981e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1399c728a914STejun Heo 	tf.device |= ATA_LBA;
14001e999736SAlan Cox 
14012b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1402c728a914STejun Heo 	if (err_mask) {
1403c728a914STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1404c728a914STejun Heo 			       "max address (err_mask=0x%x)\n", err_mask);
1405c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1406c728a914STejun Heo 			return -EACCES;
1407c728a914STejun Heo 		return -EIO;
1408c728a914STejun Heo 	}
1409c728a914STejun Heo 
1410c728a914STejun Heo 	if (lba48)
1411a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1412c728a914STejun Heo 	else
1413a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
141493328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
141593328e11SAlan Cox 		(*max_sectors)--;
14161e999736SAlan Cox 	return 0;
14171e999736SAlan Cox }
14181e999736SAlan Cox 
14191e999736SAlan Cox /**
1420c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1421c728a914STejun Heo  *	@dev: target device
14226b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
14231e999736SAlan Cox  *
1424c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1425c728a914STejun Heo  *
1426c728a914STejun Heo  *	RETURNS:
1427c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1428c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1429c728a914STejun Heo  *	errors.
14301e999736SAlan Cox  */
143105027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
14321e999736SAlan Cox {
1433c728a914STejun Heo 	unsigned int err_mask;
14341e999736SAlan Cox 	struct ata_taskfile tf;
1435c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
14361e999736SAlan Cox 
14371e999736SAlan Cox 	new_sectors--;
14381e999736SAlan Cox 
14391e999736SAlan Cox 	ata_tf_init(dev, &tf);
14401e999736SAlan Cox 
1441c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
14421e999736SAlan Cox 
1443c728a914STejun Heo 	if (lba48) {
1444c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1445c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
14461e999736SAlan Cox 
14471e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
14481e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
14491e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
14501e582ba4STejun Heo 	} else {
14511e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1452c728a914STejun Heo 
14531e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
14541e582ba4STejun Heo 	}
14551e582ba4STejun Heo 
14561e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1457c728a914STejun Heo 	tf.device |= ATA_LBA;
14581e999736SAlan Cox 
14591e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
14601e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
14611e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
14621e999736SAlan Cox 
14632b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1464c728a914STejun Heo 	if (err_mask) {
1465c728a914STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "failed to set "
1466c728a914STejun Heo 			       "max address (err_mask=0x%x)\n", err_mask);
1467c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1468c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1469c728a914STejun Heo 			return -EACCES;
1470c728a914STejun Heo 		return -EIO;
1471c728a914STejun Heo 	}
1472c728a914STejun Heo 
14731e999736SAlan Cox 	return 0;
14741e999736SAlan Cox }
14751e999736SAlan Cox 
14761e999736SAlan Cox /**
14771e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
14781e999736SAlan Cox  *	@dev: Device to resize
14791e999736SAlan Cox  *
14801e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
14811e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
14821e999736SAlan Cox  *	the drive has the HPA feature set enabled.
148305027adcSTejun Heo  *
148405027adcSTejun Heo  *	RETURNS:
148505027adcSTejun Heo  *	0 on success, -errno on failure.
14861e999736SAlan Cox  */
148705027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
14881e999736SAlan Cox {
148905027adcSTejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
149005027adcSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
149105027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
149205027adcSTejun Heo 	u64 native_sectors;
1493c728a914STejun Heo 	int rc;
14941e999736SAlan Cox 
149505027adcSTejun Heo 	/* do we need to do it? */
149605027adcSTejun Heo 	if (dev->class != ATA_DEV_ATA ||
149705027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
149805027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1499c728a914STejun Heo 		return 0;
15001e999736SAlan Cox 
150105027adcSTejun Heo 	/* read native max address */
150205027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
150305027adcSTejun Heo 	if (rc) {
1504dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1505dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
150605027adcSTejun Heo 		 */
1507dda7aba1STejun Heo 		if (rc == -EACCES || !ata_ignore_hpa) {
150805027adcSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1509dda7aba1STejun Heo 				       "broken, skipping HPA handling\n");
151005027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
151105027adcSTejun Heo 
151205027adcSTejun Heo 			/* we can continue if device aborted the command */
151305027adcSTejun Heo 			if (rc == -EACCES)
151405027adcSTejun Heo 				rc = 0;
151505027adcSTejun Heo 		}
151605027adcSTejun Heo 
151705027adcSTejun Heo 		return rc;
151805027adcSTejun Heo 	}
151905027adcSTejun Heo 
152005027adcSTejun Heo 	/* nothing to do? */
152105027adcSTejun Heo 	if (native_sectors <= sectors || !ata_ignore_hpa) {
152205027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
152305027adcSTejun Heo 			return 0;
152405027adcSTejun Heo 
152505027adcSTejun Heo 		if (native_sectors > sectors)
15261e999736SAlan Cox 			ata_dev_printk(dev, KERN_INFO,
152705027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
152805027adcSTejun Heo 				(unsigned long long)sectors,
152905027adcSTejun Heo 				(unsigned long long)native_sectors);
153005027adcSTejun Heo 		else if (native_sectors < sectors)
153105027adcSTejun Heo 			ata_dev_printk(dev, KERN_WARNING,
153205027adcSTejun Heo 				"native sectors (%llu) is smaller than "
153305027adcSTejun Heo 				"sectors (%llu)\n",
153405027adcSTejun Heo 				(unsigned long long)native_sectors,
153505027adcSTejun Heo 				(unsigned long long)sectors);
153605027adcSTejun Heo 		return 0;
15371e999736SAlan Cox 	}
153837301a55STejun Heo 
153905027adcSTejun Heo 	/* let's unlock HPA */
154005027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
154105027adcSTejun Heo 	if (rc == -EACCES) {
154205027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
154305027adcSTejun Heo 		ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
154405027adcSTejun Heo 			       "(%llu -> %llu), skipping HPA handling\n",
154505027adcSTejun Heo 			       (unsigned long long)sectors,
154605027adcSTejun Heo 			       (unsigned long long)native_sectors);
154705027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
154805027adcSTejun Heo 		return 0;
154905027adcSTejun Heo 	} else if (rc)
155005027adcSTejun Heo 		return rc;
155105027adcSTejun Heo 
155205027adcSTejun Heo 	/* re-read IDENTIFY data */
155305027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
155405027adcSTejun Heo 	if (rc) {
155505027adcSTejun Heo 		ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
155605027adcSTejun Heo 			       "data after HPA resizing\n");
155705027adcSTejun Heo 		return rc;
155805027adcSTejun Heo 	}
155905027adcSTejun Heo 
156005027adcSTejun Heo 	if (print_info) {
156105027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
156205027adcSTejun Heo 		ata_dev_printk(dev, KERN_INFO,
156305027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
156405027adcSTejun Heo 			(unsigned long long)sectors,
156505027adcSTejun Heo 			(unsigned long long)new_sectors,
156605027adcSTejun Heo 			(unsigned long long)native_sectors);
156705027adcSTejun Heo 	}
156805027adcSTejun Heo 
156905027adcSTejun Heo 	return 0;
15701e999736SAlan Cox }
15711e999736SAlan Cox 
1572c6fd2807SJeff Garzik /**
1573c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
1574c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1575c6fd2807SJeff Garzik  *
1576c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1577c6fd2807SJeff Garzik  *	page.
1578c6fd2807SJeff Garzik  *
1579c6fd2807SJeff Garzik  *	LOCKING:
1580c6fd2807SJeff Garzik  *	caller.
1581c6fd2807SJeff Garzik  */
1582c6fd2807SJeff Garzik 
1583c6fd2807SJeff Garzik static inline void ata_dump_id(const u16 *id)
1584c6fd2807SJeff Garzik {
1585c6fd2807SJeff Garzik 	DPRINTK("49==0x%04x  "
1586c6fd2807SJeff Garzik 		"53==0x%04x  "
1587c6fd2807SJeff Garzik 		"63==0x%04x  "
1588c6fd2807SJeff Garzik 		"64==0x%04x  "
1589c6fd2807SJeff Garzik 		"75==0x%04x  \n",
1590c6fd2807SJeff Garzik 		id[49],
1591c6fd2807SJeff Garzik 		id[53],
1592c6fd2807SJeff Garzik 		id[63],
1593c6fd2807SJeff Garzik 		id[64],
1594c6fd2807SJeff Garzik 		id[75]);
1595c6fd2807SJeff Garzik 	DPRINTK("80==0x%04x  "
1596c6fd2807SJeff Garzik 		"81==0x%04x  "
1597c6fd2807SJeff Garzik 		"82==0x%04x  "
1598c6fd2807SJeff Garzik 		"83==0x%04x  "
1599c6fd2807SJeff Garzik 		"84==0x%04x  \n",
1600c6fd2807SJeff Garzik 		id[80],
1601c6fd2807SJeff Garzik 		id[81],
1602c6fd2807SJeff Garzik 		id[82],
1603c6fd2807SJeff Garzik 		id[83],
1604c6fd2807SJeff Garzik 		id[84]);
1605c6fd2807SJeff Garzik 	DPRINTK("88==0x%04x  "
1606c6fd2807SJeff Garzik 		"93==0x%04x\n",
1607c6fd2807SJeff Garzik 		id[88],
1608c6fd2807SJeff Garzik 		id[93]);
1609c6fd2807SJeff Garzik }
1610c6fd2807SJeff Garzik 
1611c6fd2807SJeff Garzik /**
1612c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1613c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1614c6fd2807SJeff Garzik  *
1615c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1616c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1617c6fd2807SJeff Garzik  *
1618c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1619c6fd2807SJeff Garzik  *
1620c6fd2807SJeff Garzik  *	LOCKING:
1621c6fd2807SJeff Garzik  *	None.
1622c6fd2807SJeff Garzik  *
1623c6fd2807SJeff Garzik  *	RETURNS:
1624c6fd2807SJeff Garzik  *	Computed xfermask
1625c6fd2807SJeff Garzik  */
16267dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1627c6fd2807SJeff Garzik {
16287dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1629c6fd2807SJeff Garzik 
1630c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1631c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1632c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1633c6fd2807SJeff Garzik 		pio_mask <<= 3;
1634c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1635c6fd2807SJeff Garzik 	} else {
1636c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1637c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1638c6fd2807SJeff Garzik 		 * a mask.
1639c6fd2807SJeff Garzik 		 */
16407a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
164146767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
164246767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
164346767aebSAlan Cox 		else
164446767aebSAlan Cox 			pio_mask = 1;
1645c6fd2807SJeff Garzik 
1646c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1647c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1648c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1649c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1650c6fd2807SJeff Garzik 		 * will get this right anyway
1651c6fd2807SJeff Garzik 		 */
1652c6fd2807SJeff Garzik 	}
1653c6fd2807SJeff Garzik 
1654c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1655c6fd2807SJeff Garzik 
1656b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1657b352e57dSAlan Cox 		/*
1658b352e57dSAlan Cox 		 *	Process compact flash extended modes
1659b352e57dSAlan Cox 		 */
1660b352e57dSAlan Cox 		int pio = id[163] & 0x7;
1661b352e57dSAlan Cox 		int dma = (id[163] >> 3) & 7;
1662b352e57dSAlan Cox 
1663b352e57dSAlan Cox 		if (pio)
1664b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1665b352e57dSAlan Cox 		if (pio > 1)
1666b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1667b352e57dSAlan Cox 		if (dma)
1668b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1669b352e57dSAlan Cox 		if (dma > 1)
1670b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1671b352e57dSAlan Cox 	}
1672b352e57dSAlan Cox 
1673c6fd2807SJeff Garzik 	udma_mask = 0;
1674c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1675c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1676c6fd2807SJeff Garzik 
1677c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1678c6fd2807SJeff Garzik }
1679c6fd2807SJeff Garzik 
1680c6fd2807SJeff Garzik /**
1681442eacc3SJeff Garzik  *	ata_pio_queue_task - Queue port_task
1682c6fd2807SJeff Garzik  *	@ap: The ata_port to queue port_task for
168365f27f38SDavid Howells  *	@data: data for @fn to use
1684341c2c95STejun Heo  *	@delay: delay time in msecs for workqueue function
1685c6fd2807SJeff Garzik  *
1686c6fd2807SJeff Garzik  *	Schedule @fn(@data) for execution after @delay jiffies using
1687c6fd2807SJeff Garzik  *	port_task.  There is one port_task per port and it's the
1688c6fd2807SJeff Garzik  *	user(low level driver)'s responsibility to make sure that only
1689c6fd2807SJeff Garzik  *	one task is active at any given time.
1690c6fd2807SJeff Garzik  *
1691c6fd2807SJeff Garzik  *	libata core layer takes care of synchronization between
1692442eacc3SJeff Garzik  *	port_task and EH.  ata_pio_queue_task() may be ignored for EH
1693c6fd2807SJeff Garzik  *	synchronization.
1694c6fd2807SJeff Garzik  *
1695c6fd2807SJeff Garzik  *	LOCKING:
1696c6fd2807SJeff Garzik  *	Inherited from caller.
1697c6fd2807SJeff Garzik  */
1698624d5c51STejun Heo void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
1699c6fd2807SJeff Garzik {
170065f27f38SDavid Howells 	ap->port_task_data = data;
1701c6fd2807SJeff Garzik 
170245a66c1cSOleg Nesterov 	/* may fail if ata_port_flush_task() in progress */
1703341c2c95STejun Heo 	queue_delayed_work(ata_wq, &ap->port_task, msecs_to_jiffies(delay));
1704c6fd2807SJeff Garzik }
1705c6fd2807SJeff Garzik 
1706c6fd2807SJeff Garzik /**
1707c6fd2807SJeff Garzik  *	ata_port_flush_task - Flush port_task
1708c6fd2807SJeff Garzik  *	@ap: The ata_port to flush port_task for
1709c6fd2807SJeff Garzik  *
1710c6fd2807SJeff Garzik  *	After this function completes, port_task is guranteed not to
1711c6fd2807SJeff Garzik  *	be running or scheduled.
1712c6fd2807SJeff Garzik  *
1713c6fd2807SJeff Garzik  *	LOCKING:
1714c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1715c6fd2807SJeff Garzik  */
1716c6fd2807SJeff Garzik void ata_port_flush_task(struct ata_port *ap)
1717c6fd2807SJeff Garzik {
1718c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1719c6fd2807SJeff Garzik 
172045a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->port_task);
1721c6fd2807SJeff Garzik 
1722c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
17237f5e4e8dSHarvey Harrison 		ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
1724c6fd2807SJeff Garzik }
1725c6fd2807SJeff Garzik 
17267102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1727c6fd2807SJeff Garzik {
1728c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1729c6fd2807SJeff Garzik 
1730c6fd2807SJeff Garzik 	complete(waiting);
1731c6fd2807SJeff Garzik }
1732c6fd2807SJeff Garzik 
1733c6fd2807SJeff Garzik /**
17342432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1735c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1736c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1737c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1738c6fd2807SJeff Garzik  *	@dma_dir: Data tranfer direction of the command
17395c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
17402432697bSTejun Heo  *	@n_elem: Number of sg entries
17412b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1742c6fd2807SJeff Garzik  *
1743c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1744c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1745c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1746c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1747c6fd2807SJeff Garzik  *	clean up after timeout.
1748c6fd2807SJeff Garzik  *
1749c6fd2807SJeff Garzik  *	LOCKING:
1750c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1751c6fd2807SJeff Garzik  *
1752c6fd2807SJeff Garzik  *	RETURNS:
1753c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1754c6fd2807SJeff Garzik  */
17552432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1756c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
175787260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
17582b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1759c6fd2807SJeff Garzik {
17609af5c9c9STejun Heo 	struct ata_link *link = dev->link;
17619af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1762c6fd2807SJeff Garzik 	u8 command = tf->command;
176387fbc5a0STejun Heo 	int auto_timeout = 0;
1764c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1765c6fd2807SJeff Garzik 	unsigned int tag, preempted_tag;
1766c6fd2807SJeff Garzik 	u32 preempted_sactive, preempted_qc_active;
1767da917d69STejun Heo 	int preempted_nr_active_links;
1768c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1769c6fd2807SJeff Garzik 	unsigned long flags;
1770c6fd2807SJeff Garzik 	unsigned int err_mask;
1771c6fd2807SJeff Garzik 	int rc;
1772c6fd2807SJeff Garzik 
1773c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1774c6fd2807SJeff Garzik 
1775c6fd2807SJeff Garzik 	/* no internal command while frozen */
1776c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1777c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1778c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1779c6fd2807SJeff Garzik 	}
1780c6fd2807SJeff Garzik 
1781c6fd2807SJeff Garzik 	/* initialize internal qc */
1782c6fd2807SJeff Garzik 
1783c6fd2807SJeff Garzik 	/* XXX: Tag 0 is used for drivers with legacy EH as some
1784c6fd2807SJeff Garzik 	 * drivers choke if any other tag is given.  This breaks
1785c6fd2807SJeff Garzik 	 * ata_tag_internal() test for those drivers.  Don't use new
1786c6fd2807SJeff Garzik 	 * EH stuff without converting to it.
1787c6fd2807SJeff Garzik 	 */
1788c6fd2807SJeff Garzik 	if (ap->ops->error_handler)
1789c6fd2807SJeff Garzik 		tag = ATA_TAG_INTERNAL;
1790c6fd2807SJeff Garzik 	else
1791c6fd2807SJeff Garzik 		tag = 0;
1792c6fd2807SJeff Garzik 
17938a8bc223STejun Heo 	if (test_and_set_bit(tag, &ap->qc_allocated))
17948a8bc223STejun Heo 		BUG();
1795c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1796c6fd2807SJeff Garzik 
1797c6fd2807SJeff Garzik 	qc->tag = tag;
1798c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1799c6fd2807SJeff Garzik 	qc->ap = ap;
1800c6fd2807SJeff Garzik 	qc->dev = dev;
1801c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1802c6fd2807SJeff Garzik 
18039af5c9c9STejun Heo 	preempted_tag = link->active_tag;
18049af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1805c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1806da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
18079af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
18089af5c9c9STejun Heo 	link->sactive = 0;
1809c6fd2807SJeff Garzik 	ap->qc_active = 0;
1810da917d69STejun Heo 	ap->nr_active_links = 0;
1811c6fd2807SJeff Garzik 
1812c6fd2807SJeff Garzik 	/* prepare & issue qc */
1813c6fd2807SJeff Garzik 	qc->tf = *tf;
1814c6fd2807SJeff Garzik 	if (cdb)
1815c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1816c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1817c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1818c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
18192432697bSTejun Heo 		unsigned int i, buflen = 0;
182087260216SJens Axboe 		struct scatterlist *sg;
18212432697bSTejun Heo 
182287260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
182387260216SJens Axboe 			buflen += sg->length;
18242432697bSTejun Heo 
182587260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
182649c80429SBrian King 		qc->nbytes = buflen;
1827c6fd2807SJeff Garzik 	}
1828c6fd2807SJeff Garzik 
1829c6fd2807SJeff Garzik 	qc->private_data = &wait;
1830c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1831c6fd2807SJeff Garzik 
1832c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1833c6fd2807SJeff Garzik 
1834c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1835c6fd2807SJeff Garzik 
183687fbc5a0STejun Heo 	if (!timeout) {
183787fbc5a0STejun Heo 		if (ata_probe_timeout)
1838341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
183987fbc5a0STejun Heo 		else {
184087fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
184187fbc5a0STejun Heo 			auto_timeout = 1;
184287fbc5a0STejun Heo 		}
184387fbc5a0STejun Heo 	}
18442b789108STejun Heo 
18452b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1846c6fd2807SJeff Garzik 
1847c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
1848c6fd2807SJeff Garzik 
1849c6fd2807SJeff Garzik 	if (!rc) {
1850c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1851c6fd2807SJeff Garzik 
1852c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1853c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1854c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1855c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1856c6fd2807SJeff Garzik 		 */
1857c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1858c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1859c6fd2807SJeff Garzik 
1860c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1861c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1862c6fd2807SJeff Garzik 			else
1863c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1864c6fd2807SJeff Garzik 
1865c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1866c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
1867c6fd2807SJeff Garzik 					"qc timeout (cmd 0x%x)\n", command);
1868c6fd2807SJeff Garzik 		}
1869c6fd2807SJeff Garzik 
1870c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1871c6fd2807SJeff Garzik 	}
1872c6fd2807SJeff Garzik 
1873c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1874c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1875c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1876c6fd2807SJeff Garzik 
1877a51d644aSTejun Heo 	/* perform minimal error analysis */
1878a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1879a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1880a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1881a51d644aSTejun Heo 
1882a51d644aSTejun Heo 		if (!qc->err_mask)
1883c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1884a51d644aSTejun Heo 
1885a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1886a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
1887c6fd2807SJeff Garzik 	}
1888c6fd2807SJeff Garzik 
1889c6fd2807SJeff Garzik 	/* finish up */
1890c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1891c6fd2807SJeff Garzik 
1892c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1893c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1894c6fd2807SJeff Garzik 
1895c6fd2807SJeff Garzik 	ata_qc_free(qc);
18969af5c9c9STejun Heo 	link->active_tag = preempted_tag;
18979af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1898c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1899da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1900c6fd2807SJeff Garzik 
1901c6fd2807SJeff Garzik 	/* XXX - Some LLDDs (sata_mv) disable port on command failure.
1902c6fd2807SJeff Garzik 	 * Until those drivers are fixed, we detect the condition
1903c6fd2807SJeff Garzik 	 * here, fail the command with AC_ERR_SYSTEM and reenable the
1904c6fd2807SJeff Garzik 	 * port.
1905c6fd2807SJeff Garzik 	 *
1906c6fd2807SJeff Garzik 	 * Note that this doesn't change any behavior as internal
1907c6fd2807SJeff Garzik 	 * command failure results in disabling the device in the
1908c6fd2807SJeff Garzik 	 * higher layer for LLDDs without new reset/EH callbacks.
1909c6fd2807SJeff Garzik 	 *
1910c6fd2807SJeff Garzik 	 * Kill the following code as soon as those drivers are fixed.
1911c6fd2807SJeff Garzik 	 */
1912c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_DISABLED) {
1913c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1914c6fd2807SJeff Garzik 		ata_port_probe(ap);
1915c6fd2807SJeff Garzik 	}
1916c6fd2807SJeff Garzik 
1917c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1918c6fd2807SJeff Garzik 
191987fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
192087fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
192187fbc5a0STejun Heo 
1922c6fd2807SJeff Garzik 	return err_mask;
1923c6fd2807SJeff Garzik }
1924c6fd2807SJeff Garzik 
1925c6fd2807SJeff Garzik /**
192633480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
19272432697bSTejun Heo  *	@dev: Device to which the command is sent
19282432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
19292432697bSTejun Heo  *	@cdb: CDB for packet command
19302432697bSTejun Heo  *	@dma_dir: Data tranfer direction of the command
19312432697bSTejun Heo  *	@buf: Data buffer of the command
19322432697bSTejun Heo  *	@buflen: Length of data buffer
19332b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
19342432697bSTejun Heo  *
19352432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
19362432697bSTejun Heo  *	buffer instead of sg list.
19372432697bSTejun Heo  *
19382432697bSTejun Heo  *	LOCKING:
19392432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
19402432697bSTejun Heo  *
19412432697bSTejun Heo  *	RETURNS:
19422432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
19432432697bSTejun Heo  */
19442432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
19452432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
19462b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
19472b789108STejun Heo 			   unsigned long timeout)
19482432697bSTejun Heo {
194933480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
195033480a0eSTejun Heo 	unsigned int n_elem = 0;
19512432697bSTejun Heo 
195233480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
195333480a0eSTejun Heo 		WARN_ON(!buf);
19542432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
195533480a0eSTejun Heo 		psg = &sg;
195633480a0eSTejun Heo 		n_elem++;
195733480a0eSTejun Heo 	}
19582432697bSTejun Heo 
19592b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
19602b789108STejun Heo 				    timeout);
19612432697bSTejun Heo }
19622432697bSTejun Heo 
19632432697bSTejun Heo /**
1964c6fd2807SJeff Garzik  *	ata_do_simple_cmd - execute simple internal command
1965c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1966c6fd2807SJeff Garzik  *	@cmd: Opcode to execute
1967c6fd2807SJeff Garzik  *
1968c6fd2807SJeff Garzik  *	Execute a 'simple' command, that only consists of the opcode
1969c6fd2807SJeff Garzik  *	'cmd' itself, without filling any other registers
1970c6fd2807SJeff Garzik  *
1971c6fd2807SJeff Garzik  *	LOCKING:
1972c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1973c6fd2807SJeff Garzik  *
1974c6fd2807SJeff Garzik  *	RETURNS:
1975c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1976c6fd2807SJeff Garzik  */
1977c6fd2807SJeff Garzik unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1978c6fd2807SJeff Garzik {
1979c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1980c6fd2807SJeff Garzik 
1981c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1982c6fd2807SJeff Garzik 
1983c6fd2807SJeff Garzik 	tf.command = cmd;
1984c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_DEVICE;
1985c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
1986c6fd2807SJeff Garzik 
19872b789108STejun Heo 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1988c6fd2807SJeff Garzik }
1989c6fd2807SJeff Garzik 
1990c6fd2807SJeff Garzik /**
1991c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1992c6fd2807SJeff Garzik  *	@adev: ATA device
1993c6fd2807SJeff Garzik  *
1994c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1995c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1996c6fd2807SJeff Garzik  */
1997c6fd2807SJeff Garzik 
1998c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1999c6fd2807SJeff Garzik {
2000432729f0SAlan Cox 	/* Controller doesn't support  IORDY. Probably a pointless check
2001432729f0SAlan Cox 	   as the caller should know this */
20029af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
2003c6fd2807SJeff Garzik 		return 0;
20045c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
20055c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
20065c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
20075c18c4d2SDavid Daney 		return 0;
2008432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
2009432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
2010c6fd2807SJeff Garzik 		return 1;
2011432729f0SAlan Cox 	/* We turn it on when possible */
2012432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
2013432729f0SAlan Cox 		return 1;
2014432729f0SAlan Cox 	return 0;
2015432729f0SAlan Cox }
2016c6fd2807SJeff Garzik 
2017432729f0SAlan Cox /**
2018432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
2019432729f0SAlan Cox  *	@adev: ATA device
2020432729f0SAlan Cox  *
2021432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
2022432729f0SAlan Cox  *	-1 if no iordy mode is available.
2023432729f0SAlan Cox  */
2024432729f0SAlan Cox 
2025432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
2026432729f0SAlan Cox {
2027c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
2028c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
2029432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
2030c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
2031c6fd2807SJeff Garzik 		if (pio) {
2032c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
2033c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
2034432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
2035432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
2036c6fd2807SJeff Garzik 		}
2037c6fd2807SJeff Garzik 	}
2038432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
2039c6fd2807SJeff Garzik }
2040c6fd2807SJeff Garzik 
2041c6fd2807SJeff Garzik /**
2042963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
2043963e4975SAlan Cox  *	@dev: device
2044963e4975SAlan Cox  *	@tf: proposed taskfile
2045963e4975SAlan Cox  *	@id: data buffer
2046963e4975SAlan Cox  *
2047963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
2048963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
2049963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
2050963e4975SAlan Cox  */
2051963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
2052963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
2053963e4975SAlan Cox {
2054963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
2055963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
2056963e4975SAlan Cox }
2057963e4975SAlan Cox 
2058963e4975SAlan Cox /**
2059c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
2060c6fd2807SJeff Garzik  *	@dev: target device
2061c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
2062bff04647STejun Heo  *	@flags: ATA_READID_* flags
2063c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
2064c6fd2807SJeff Garzik  *
2065c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
2066c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
2067c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
2068c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
2069c6fd2807SJeff Garzik  *
207050a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
207150a99018SAlan Cox  *	now we abort if we hit that case.
207250a99018SAlan Cox  *
2073c6fd2807SJeff Garzik  *	LOCKING:
2074c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2075c6fd2807SJeff Garzik  *
2076c6fd2807SJeff Garzik  *	RETURNS:
2077c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
2078c6fd2807SJeff Garzik  */
2079c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
2080bff04647STejun Heo 		    unsigned int flags, u16 *id)
2081c6fd2807SJeff Garzik {
20829af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2083c6fd2807SJeff Garzik 	unsigned int class = *p_class;
2084c6fd2807SJeff Garzik 	struct ata_taskfile tf;
2085c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
2086c6fd2807SJeff Garzik 	const char *reason;
208754936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
2088c6fd2807SJeff Garzik 	int rc;
2089c6fd2807SJeff Garzik 
2090c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
20917f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2092c6fd2807SJeff Garzik 
2093c6fd2807SJeff Garzik retry:
2094c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
2095c6fd2807SJeff Garzik 
2096c6fd2807SJeff Garzik 	switch (class) {
2097c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
2098c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
2099c6fd2807SJeff Garzik 		break;
2100c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
2101c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
2102c6fd2807SJeff Garzik 		break;
2103c6fd2807SJeff Garzik 	default:
2104c6fd2807SJeff Garzik 		rc = -ENODEV;
2105c6fd2807SJeff Garzik 		reason = "unsupported class";
2106c6fd2807SJeff Garzik 		goto err_out;
2107c6fd2807SJeff Garzik 	}
2108c6fd2807SJeff Garzik 
2109c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
211081afe893STejun Heo 
211181afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
211281afe893STejun Heo 	 * sure those are properly initialized.
211381afe893STejun Heo 	 */
211481afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
211581afe893STejun Heo 
211681afe893STejun Heo 	/* Device presence detection is unreliable on some
211781afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
211881afe893STejun Heo 	 */
211981afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
2120c6fd2807SJeff Garzik 
2121963e4975SAlan Cox 	if (ap->ops->read_id)
2122963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
2123963e4975SAlan Cox 	else
2124963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
2125963e4975SAlan Cox 
2126c6fd2807SJeff Garzik 	if (err_mask) {
2127800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
21281ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
21291ffc151fSTejun Heo 				       "NODEV after polling detection\n");
213055a8e2c8STejun Heo 			return -ENOENT;
213155a8e2c8STejun Heo 		}
213255a8e2c8STejun Heo 
21331ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
21341ffc151fSTejun Heo 			/* Device or controller might have reported
21351ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
21361ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
21371ffc151fSTejun Heo 			 * aborted by the device.
213854936f8bSTejun Heo 			 */
21391ffc151fSTejun Heo 			if (may_fallback) {
214054936f8bSTejun Heo 				may_fallback = 0;
214154936f8bSTejun Heo 
214254936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
214354936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
214454936f8bSTejun Heo 				else
214554936f8bSTejun Heo 					class = ATA_DEV_ATA;
214654936f8bSTejun Heo 				goto retry;
214754936f8bSTejun Heo 			}
214854936f8bSTejun Heo 
21491ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
21501ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
21511ffc151fSTejun Heo 			 * sometimes with phantom devices.
21521ffc151fSTejun Heo 			 */
21531ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
21541ffc151fSTejun Heo 				       "both IDENTIFYs aborted, assuming NODEV\n");
21551ffc151fSTejun Heo 			return -ENOENT;
21561ffc151fSTejun Heo 		}
21571ffc151fSTejun Heo 
2158c6fd2807SJeff Garzik 		rc = -EIO;
2159c6fd2807SJeff Garzik 		reason = "I/O error";
2160c6fd2807SJeff Garzik 		goto err_out;
2161c6fd2807SJeff Garzik 	}
2162c6fd2807SJeff Garzik 
216354936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
216454936f8bSTejun Heo 	 * successfully at least once.
216554936f8bSTejun Heo 	 */
216654936f8bSTejun Heo 	may_fallback = 0;
216754936f8bSTejun Heo 
2168c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
2169c6fd2807SJeff Garzik 
2170c6fd2807SJeff Garzik 	/* sanity check */
2171c6fd2807SJeff Garzik 	rc = -EINVAL;
21726070068bSAlan Cox 	reason = "device reports invalid type";
21734a3381feSJeff Garzik 
21744a3381feSJeff Garzik 	if (class == ATA_DEV_ATA) {
21754a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
21764a3381feSJeff Garzik 			goto err_out;
21774a3381feSJeff Garzik 	} else {
21784a3381feSJeff Garzik 		if (ata_id_is_ata(id))
2179c6fd2807SJeff Garzik 			goto err_out;
2180c6fd2807SJeff Garzik 	}
2181c6fd2807SJeff Garzik 
2182169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2183169439c2SMark Lord 		tried_spinup = 1;
2184169439c2SMark Lord 		/*
2185169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
2186169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
2187169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
2188169439c2SMark Lord 		 */
2189218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2190fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
2191169439c2SMark Lord 			rc = -EIO;
2192169439c2SMark Lord 			reason = "SPINUP failed";
2193169439c2SMark Lord 			goto err_out;
2194169439c2SMark Lord 		}
2195169439c2SMark Lord 		/*
2196169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
2197169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
2198169439c2SMark Lord 		 */
2199169439c2SMark Lord 		if (id[2] == 0x37c8)
2200169439c2SMark Lord 			goto retry;
2201169439c2SMark Lord 	}
2202169439c2SMark Lord 
2203bff04647STejun Heo 	if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2204c6fd2807SJeff Garzik 		/*
2205c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2206c6fd2807SJeff Garzik 		 * SRST RESET
220750a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
220850a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2209c6fd2807SJeff Garzik 		 * anything else..
2210c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
221150a99018SAlan Cox 		 *
221250a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
221350a99018SAlan Cox 		 * shoud never trigger.
2214c6fd2807SJeff Garzik 		 */
2215c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2216c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2217c6fd2807SJeff Garzik 			if (err_mask) {
2218c6fd2807SJeff Garzik 				rc = -EIO;
2219c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2220c6fd2807SJeff Garzik 				goto err_out;
2221c6fd2807SJeff Garzik 			}
2222c6fd2807SJeff Garzik 
2223c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2224c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2225c6fd2807SJeff Garzik 			 */
2226bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2227c6fd2807SJeff Garzik 			goto retry;
2228c6fd2807SJeff Garzik 		}
2229c6fd2807SJeff Garzik 	}
2230c6fd2807SJeff Garzik 
2231c6fd2807SJeff Garzik 	*p_class = class;
2232c6fd2807SJeff Garzik 
2233c6fd2807SJeff Garzik 	return 0;
2234c6fd2807SJeff Garzik 
2235c6fd2807SJeff Garzik  err_out:
2236c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2237c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2238c6fd2807SJeff Garzik 			       "(%s, err_mask=0x%x)\n", reason, err_mask);
2239c6fd2807SJeff Garzik 	return rc;
2240c6fd2807SJeff Garzik }
2241c6fd2807SJeff Garzik 
2242c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2243c6fd2807SJeff Garzik {
22449af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
22459ce8e307SJens Axboe 
22469ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
22479ce8e307SJens Axboe 		return 0;
22489ce8e307SJens Axboe 
22499af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2250c6fd2807SJeff Garzik }
2251c6fd2807SJeff Garzik 
2252c6fd2807SJeff Garzik static void ata_dev_config_ncq(struct ata_device *dev,
2253c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2254c6fd2807SJeff Garzik {
22559af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2256c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2257c6fd2807SJeff Garzik 
2258c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2259c6fd2807SJeff Garzik 		desc[0] = '\0';
2260c6fd2807SJeff Garzik 		return;
2261c6fd2807SJeff Garzik 	}
226275683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
22636919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
22646919a0a6SAlan Cox 		return;
22656919a0a6SAlan Cox 	}
2266c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2267cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2268c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2269c6fd2807SJeff Garzik 	}
2270c6fd2807SJeff Garzik 
2271c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2272c6fd2807SJeff Garzik 		snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
2273c6fd2807SJeff Garzik 	else
2274c6fd2807SJeff Garzik 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
2275c6fd2807SJeff Garzik }
2276c6fd2807SJeff Garzik 
2277c6fd2807SJeff Garzik /**
2278c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2279c6fd2807SJeff Garzik  *	@dev: Target device to configure
2280c6fd2807SJeff Garzik  *
2281c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2282c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2283c6fd2807SJeff Garzik  *
2284c6fd2807SJeff Garzik  *	LOCKING:
2285c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2286c6fd2807SJeff Garzik  *
2287c6fd2807SJeff Garzik  *	RETURNS:
2288c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2289c6fd2807SJeff Garzik  */
2290efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2291c6fd2807SJeff Garzik {
22929af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
22939af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
22946746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2295c6fd2807SJeff Garzik 	const u16 *id = dev->id;
22967dc951aeSTejun Heo 	unsigned long xfer_mask;
2297b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
22983f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
22993f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2300c6fd2807SJeff Garzik 	int rc;
2301c6fd2807SJeff Garzik 
2302c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
230344877b4eSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
23047f5e4e8dSHarvey Harrison 			       __func__);
2305c6fd2807SJeff Garzik 		return 0;
2306c6fd2807SJeff Garzik 	}
2307c6fd2807SJeff Garzik 
2308c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
23097f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2310c6fd2807SJeff Garzik 
231175683fe7STejun Heo 	/* set horkage */
231275683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
231333267325STejun Heo 	ata_force_horkage(dev);
231475683fe7STejun Heo 
231550af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
231650af2fa1STejun Heo 		ata_dev_printk(dev, KERN_INFO,
231750af2fa1STejun Heo 			       "unsupported device, disabling\n");
231850af2fa1STejun Heo 		ata_dev_disable(dev);
231950af2fa1STejun Heo 		return 0;
232050af2fa1STejun Heo 	}
232150af2fa1STejun Heo 
23222486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
23232486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
23242486fa56STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
23252486fa56STejun Heo 			"WARNING: ATAPI is %s, device ignored.\n",
23262486fa56STejun Heo 			atapi_enabled ? "not supported with this driver"
23272486fa56STejun Heo 				      : "disabled");
23282486fa56STejun Heo 		ata_dev_disable(dev);
23292486fa56STejun Heo 		return 0;
23302486fa56STejun Heo 	}
23312486fa56STejun Heo 
23326746544cSTejun Heo 	/* let ACPI work its magic */
23336746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
23346746544cSTejun Heo 	if (rc)
23356746544cSTejun Heo 		return rc;
233608573a86SKristen Carlson Accardi 
233705027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
233805027adcSTejun Heo 	rc = ata_hpa_resize(dev);
233905027adcSTejun Heo 	if (rc)
234005027adcSTejun Heo 		return rc;
234105027adcSTejun Heo 
2342c6fd2807SJeff Garzik 	/* print device capabilities */
2343c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2344c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
2345c6fd2807SJeff Garzik 			       "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2346c6fd2807SJeff Garzik 			       "85:%04x 86:%04x 87:%04x 88:%04x\n",
23477f5e4e8dSHarvey Harrison 			       __func__,
2348c6fd2807SJeff Garzik 			       id[49], id[82], id[83], id[84],
2349c6fd2807SJeff Garzik 			       id[85], id[86], id[87], id[88]);
2350c6fd2807SJeff Garzik 
2351c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2352c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2353c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2354c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2355c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2356c6fd2807SJeff Garzik 	dev->cylinders = 0;
2357c6fd2807SJeff Garzik 	dev->heads = 0;
2358c6fd2807SJeff Garzik 	dev->sectors = 0;
2359c6fd2807SJeff Garzik 
2360c6fd2807SJeff Garzik 	/*
2361c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2362c6fd2807SJeff Garzik 	 */
2363c6fd2807SJeff Garzik 
2364c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2365c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2366c6fd2807SJeff Garzik 
2367c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2368c6fd2807SJeff Garzik 		ata_dump_id(id);
2369c6fd2807SJeff Garzik 
2370ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2371ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2372ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2373ef143d57SAlbert Lee 
2374ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2375ef143d57SAlbert Lee 			sizeof(modelbuf));
2376ef143d57SAlbert Lee 
2377c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
2378c6fd2807SJeff Garzik 	if (dev->class == ATA_DEV_ATA) {
2379b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
2380b352e57dSAlan Cox 			if (id[162] & 1) /* CPRM may make this media unusable */
238144877b4eSTejun Heo 				ata_dev_printk(dev, KERN_WARNING,
238244877b4eSTejun Heo 					       "supports DRM functions and may "
238344877b4eSTejun Heo 					       "not be fully accessable.\n");
2384b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2385ae8d4ee7SAlan Cox 		} else {
2386b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2387ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2388ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2389ae8d4ee7SAlan Cox 				ata_dev_printk(dev, KERN_WARNING,
2390ae8d4ee7SAlan Cox 					       "supports DRM functions and may "
2391ae8d4ee7SAlan Cox 					       "not be fully accessable.\n");
2392ae8d4ee7SAlan Cox 		}
2393b352e57dSAlan Cox 
2394c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2395c6fd2807SJeff Garzik 
23963f64f565SEric D. Mudama 		if (dev->id[59] & 0x100)
23973f64f565SEric D. Mudama 			dev->multi_count = dev->id[59] & 0xff;
23983f64f565SEric D. Mudama 
2399c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2400c6fd2807SJeff Garzik 			const char *lba_desc;
2401c6fd2807SJeff Garzik 			char ncq_desc[20];
2402c6fd2807SJeff Garzik 
2403c6fd2807SJeff Garzik 			lba_desc = "LBA";
2404c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2405c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2406c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2407c6fd2807SJeff Garzik 				lba_desc = "LBA48";
24086fc49adbSTejun Heo 
24096fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
24106fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
24116fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2412c6fd2807SJeff Garzik 			}
2413c6fd2807SJeff Garzik 
2414c6fd2807SJeff Garzik 			/* config NCQ */
2415c6fd2807SJeff Garzik 			ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2416c6fd2807SJeff Garzik 
2417c6fd2807SJeff Garzik 			/* print device info to dmesg */
24183f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
24193f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
24203f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
24213f64f565SEric D. Mudama 					revbuf, modelbuf, fwrevbuf,
24223f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
24233f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
24243f64f565SEric D. Mudama 					"%Lu sectors, multi %u: %s %s\n",
2425c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
24263f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
24273f64f565SEric D. Mudama 			}
2428c6fd2807SJeff Garzik 		} else {
2429c6fd2807SJeff Garzik 			/* CHS */
2430c6fd2807SJeff Garzik 
2431c6fd2807SJeff Garzik 			/* Default translation */
2432c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2433c6fd2807SJeff Garzik 			dev->heads	= id[3];
2434c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2435c6fd2807SJeff Garzik 
2436c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2437c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2438c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2439c6fd2807SJeff Garzik 				dev->heads     = id[55];
2440c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2441c6fd2807SJeff Garzik 			}
2442c6fd2807SJeff Garzik 
2443c6fd2807SJeff Garzik 			/* print device info to dmesg */
24443f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2445c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_INFO,
24463f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
24473f64f565SEric D. Mudama 					revbuf,	modelbuf, fwrevbuf,
24483f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
24493f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
24503f64f565SEric D. Mudama 					"%Lu sectors, multi %u, CHS %u/%u/%u\n",
24513f64f565SEric D. Mudama 					(unsigned long long)dev->n_sectors,
24523f64f565SEric D. Mudama 					dev->multi_count, dev->cylinders,
24533f64f565SEric D. Mudama 					dev->heads, dev->sectors);
24543f64f565SEric D. Mudama 			}
2455c6fd2807SJeff Garzik 		}
2456c6fd2807SJeff Garzik 
2457c6fd2807SJeff Garzik 		dev->cdb_len = 16;
2458c6fd2807SJeff Garzik 	}
2459c6fd2807SJeff Garzik 
2460c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2461c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2462854c73a2STejun Heo 		const char *cdb_intr_string = "";
2463854c73a2STejun Heo 		const char *atapi_an_string = "";
246491163006STejun Heo 		const char *dma_dir_string = "";
24657d77b247STejun Heo 		u32 sntf;
2466c6fd2807SJeff Garzik 
2467c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2468c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2469c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2470c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
2471c6fd2807SJeff Garzik 					       "unsupported CDB len\n");
2472c6fd2807SJeff Garzik 			rc = -EINVAL;
2473c6fd2807SJeff Garzik 			goto err_out_nosup;
2474c6fd2807SJeff Garzik 		}
2475c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2476c6fd2807SJeff Garzik 
24777d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
24787d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
24797d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
24807d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
24819f45cbd3SKristen Carlson Accardi 		 */
24827d77b247STejun Heo 		if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2483071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
24847d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2485854c73a2STejun Heo 			unsigned int err_mask;
2486854c73a2STejun Heo 
24879f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2488218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2489218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2490854c73a2STejun Heo 			if (err_mask)
24919f45cbd3SKristen Carlson Accardi 				ata_dev_printk(dev, KERN_ERR,
2492854c73a2STejun Heo 					"failed to enable ATAPI AN "
2493854c73a2STejun Heo 					"(err_mask=0x%x)\n", err_mask);
2494854c73a2STejun Heo 			else {
24959f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2496854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2497854c73a2STejun Heo 			}
24989f45cbd3SKristen Carlson Accardi 		}
24999f45cbd3SKristen Carlson Accardi 
2500c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2501c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2502c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2503c6fd2807SJeff Garzik 		}
2504c6fd2807SJeff Garzik 
250591163006STejun Heo 		if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
250691163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
250791163006STejun Heo 			dma_dir_string = ", DMADIR";
250891163006STejun Heo 		}
250991163006STejun Heo 
2510c6fd2807SJeff Garzik 		/* print device info to dmesg */
2511c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2512ef143d57SAlbert Lee 			ata_dev_printk(dev, KERN_INFO,
251391163006STejun Heo 				       "ATAPI: %s, %s, max %s%s%s%s\n",
2514ef143d57SAlbert Lee 				       modelbuf, fwrevbuf,
2515c6fd2807SJeff Garzik 				       ata_mode_string(xfer_mask),
251691163006STejun Heo 				       cdb_intr_string, atapi_an_string,
251791163006STejun Heo 				       dma_dir_string);
2518c6fd2807SJeff Garzik 	}
2519c6fd2807SJeff Garzik 
2520914ed354STejun Heo 	/* determine max_sectors */
2521914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2522914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2523914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2524914ed354STejun Heo 
2525ca77329fSKristen Carlson Accardi 	if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2526ca77329fSKristen Carlson Accardi 		if (ata_id_has_hipm(dev->id))
2527ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_HIPM;
2528ca77329fSKristen Carlson Accardi 		if (ata_id_has_dipm(dev->id))
2529ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_DIPM;
2530ca77329fSKristen Carlson Accardi 	}
2531ca77329fSKristen Carlson Accardi 
2532c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2533c5038fc0SAlan Cox 	   200 sectors */
2534c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2535c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2536c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_INFO,
2537c6fd2807SJeff Garzik 				       "applying bridge limits\n");
2538c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2539c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2540c6fd2807SJeff Garzik 	}
2541c6fd2807SJeff Garzik 
2542f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2543f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2544f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2545f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2546f442cd86SAlbert Lee 	}
2547f8d8e579STony Battersby 
254875683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
254903ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
255003ec52deSTejun Heo 					 dev->max_sectors);
255118d6e9d5SAlbert Lee 
2552ca77329fSKristen Carlson Accardi 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2553ca77329fSKristen Carlson Accardi 		dev->horkage |= ATA_HORKAGE_IPM;
2554ca77329fSKristen Carlson Accardi 
2555ca77329fSKristen Carlson Accardi 		/* reset link pm_policy for this port to no pm */
2556ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
2557ca77329fSKristen Carlson Accardi 	}
2558ca77329fSKristen Carlson Accardi 
2559c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2560cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2561c6fd2807SJeff Garzik 
2562c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2563c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2564c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2565c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2566c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2567c5038fc0SAlan Cox 		   bugs */
2568c5038fc0SAlan Cox 
2569c5038fc0SAlan Cox 		if (print_info) {
2570c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2571c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2572c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2573c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2574c5038fc0SAlan Cox 		}
2575c5038fc0SAlan Cox 	}
2576c5038fc0SAlan Cox 
2577ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2578ac70a964STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "WARNING: device requires "
2579ac70a964STejun Heo 			       "firmware update to be fully functional.\n");
2580ac70a964STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "         contact the vendor "
2581ac70a964STejun Heo 			       "or visit http://ata.wiki.kernel.org.\n");
2582ac70a964STejun Heo 	}
2583ac70a964STejun Heo 
2584c6fd2807SJeff Garzik 	return 0;
2585c6fd2807SJeff Garzik 
2586c6fd2807SJeff Garzik err_out_nosup:
2587c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2588c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
25897f5e4e8dSHarvey Harrison 			       "%s: EXIT, err\n", __func__);
2590c6fd2807SJeff Garzik 	return rc;
2591c6fd2807SJeff Garzik }
2592c6fd2807SJeff Garzik 
2593c6fd2807SJeff Garzik /**
25942e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2595be0d18dfSAlan Cox  *	@ap: port
2596be0d18dfSAlan Cox  *
25972e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2598be0d18dfSAlan Cox  *	detection.
2599be0d18dfSAlan Cox  */
2600be0d18dfSAlan Cox 
2601be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2602be0d18dfSAlan Cox {
2603be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2604be0d18dfSAlan Cox }
2605be0d18dfSAlan Cox 
2606be0d18dfSAlan Cox /**
26072e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2608be0d18dfSAlan Cox  *	@ap: port
2609be0d18dfSAlan Cox  *
26102e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2611be0d18dfSAlan Cox  *	detection.
2612be0d18dfSAlan Cox  */
2613be0d18dfSAlan Cox 
2614be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2615be0d18dfSAlan Cox {
2616be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2617be0d18dfSAlan Cox }
2618be0d18dfSAlan Cox 
2619be0d18dfSAlan Cox /**
2620be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2621be0d18dfSAlan Cox  *	@ap: port
2622be0d18dfSAlan Cox  *
2623be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2624be0d18dfSAlan Cox  */
2625be0d18dfSAlan Cox 
2626be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2627be0d18dfSAlan Cox {
2628be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2629be0d18dfSAlan Cox }
2630be0d18dfSAlan Cox 
2631be0d18dfSAlan Cox /**
2632c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2633c88f90c3STejun Heo  *	@ap: port
2634c88f90c3STejun Heo  *
2635c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2636c88f90c3STejun Heo  *	transfer mode.
2637c88f90c3STejun Heo  */
2638c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2639c88f90c3STejun Heo {
2640c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2641c88f90c3STejun Heo }
2642c88f90c3STejun Heo 
2643c88f90c3STejun Heo /**
2644be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2645be0d18dfSAlan Cox  *	@ap: port
2646be0d18dfSAlan Cox  *
2647be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2648be0d18dfSAlan Cox  */
2649be0d18dfSAlan Cox 
2650be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2651be0d18dfSAlan Cox {
2652be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2653be0d18dfSAlan Cox }
2654be0d18dfSAlan Cox 
2655be0d18dfSAlan Cox /**
2656c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2657c6fd2807SJeff Garzik  *	@ap: Bus to probe
2658c6fd2807SJeff Garzik  *
2659c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2660c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2661c6fd2807SJeff Garzik  *	the bus.
2662c6fd2807SJeff Garzik  *
2663c6fd2807SJeff Garzik  *	LOCKING:
2664c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2665c6fd2807SJeff Garzik  *
2666c6fd2807SJeff Garzik  *	RETURNS:
2667c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2668c6fd2807SJeff Garzik  */
2669c6fd2807SJeff Garzik 
2670c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2671c6fd2807SJeff Garzik {
2672c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2673c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2674f58229f8STejun Heo 	int rc;
2675c6fd2807SJeff Garzik 	struct ata_device *dev;
2676c6fd2807SJeff Garzik 
2677c6fd2807SJeff Garzik 	ata_port_probe(ap);
2678c6fd2807SJeff Garzik 
26791eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2680f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2681c6fd2807SJeff Garzik 
2682c6fd2807SJeff Garzik  retry:
26831eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2684cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2685cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2686cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2687cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2688cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2689cdeab114STejun Heo 		 * bus as we may be talking too fast.
2690cdeab114STejun Heo 		 */
2691cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2692cdeab114STejun Heo 
2693cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2694cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2695cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2696cdeab114STejun Heo 		 * configuring devices.
2697cdeab114STejun Heo 		 */
2698cdeab114STejun Heo 		if (ap->ops->set_piomode)
2699cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2700cdeab114STejun Heo 	}
2701cdeab114STejun Heo 
2702c6fd2807SJeff Garzik 	/* reset and determine device classes */
2703c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2704c6fd2807SJeff Garzik 
27051eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2706c6fd2807SJeff Garzik 		if (!(ap->flags & ATA_FLAG_DISABLED) &&
2707c6fd2807SJeff Garzik 		    dev->class != ATA_DEV_UNKNOWN)
2708c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2709c6fd2807SJeff Garzik 		else
2710c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2711c6fd2807SJeff Garzik 
2712c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2713c6fd2807SJeff Garzik 	}
2714c6fd2807SJeff Garzik 
2715c6fd2807SJeff Garzik 	ata_port_probe(ap);
2716c6fd2807SJeff Garzik 
2717f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2718f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2719f31f0cc2SJeff Garzik 	   the slave device */
2720f31f0cc2SJeff Garzik 
27211eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2722f58229f8STejun Heo 		if (tries[dev->devno])
2723f58229f8STejun Heo 			dev->class = classes[dev->devno];
2724c6fd2807SJeff Garzik 
2725c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2726c6fd2807SJeff Garzik 			continue;
2727c6fd2807SJeff Garzik 
2728bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2729bff04647STejun Heo 				     dev->id);
2730c6fd2807SJeff Garzik 		if (rc)
2731c6fd2807SJeff Garzik 			goto fail;
2732f31f0cc2SJeff Garzik 	}
2733f31f0cc2SJeff Garzik 
2734be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2735be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2736be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2737be0d18dfSAlan Cox 
27381eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
27391eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
27401eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
27411eca4365STejun Heo 	 * of the link the bridge is which is a problem.
27421eca4365STejun Heo 	 */
27431eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2744614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2745614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2746614fe29bSAlan Cox 
2747f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2748f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2749f31f0cc2SJeff Garzik 
27501eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
27519af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2752efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
27539af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2754c6fd2807SJeff Garzik 		if (rc)
2755c6fd2807SJeff Garzik 			goto fail;
2756c6fd2807SJeff Garzik 	}
2757c6fd2807SJeff Garzik 
2758c6fd2807SJeff Garzik 	/* configure transfer mode */
27590260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
27604ae72a1eSTejun Heo 	if (rc)
2761c6fd2807SJeff Garzik 		goto fail;
2762c6fd2807SJeff Garzik 
27631eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2764c6fd2807SJeff Garzik 		return 0;
2765c6fd2807SJeff Garzik 
2766c6fd2807SJeff Garzik 	/* no device present, disable port */
2767c6fd2807SJeff Garzik 	ata_port_disable(ap);
2768c6fd2807SJeff Garzik 	return -ENODEV;
2769c6fd2807SJeff Garzik 
2770c6fd2807SJeff Garzik  fail:
27714ae72a1eSTejun Heo 	tries[dev->devno]--;
27724ae72a1eSTejun Heo 
2773c6fd2807SJeff Garzik 	switch (rc) {
2774c6fd2807SJeff Garzik 	case -EINVAL:
27754ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2776c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2777c6fd2807SJeff Garzik 		break;
27784ae72a1eSTejun Heo 
27794ae72a1eSTejun Heo 	case -ENODEV:
27804ae72a1eSTejun Heo 		/* give it just one more chance */
27814ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2782c6fd2807SJeff Garzik 	case -EIO:
27834ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
27844ae72a1eSTejun Heo 			/* This is the last chance, better to slow
27854ae72a1eSTejun Heo 			 * down than lose it.
27864ae72a1eSTejun Heo 			 */
2787936fd732STejun Heo 			sata_down_spd_limit(&ap->link);
27884ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
27894ae72a1eSTejun Heo 		}
2790c6fd2807SJeff Garzik 	}
2791c6fd2807SJeff Garzik 
27924ae72a1eSTejun Heo 	if (!tries[dev->devno])
2793c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2794c6fd2807SJeff Garzik 
2795c6fd2807SJeff Garzik 	goto retry;
2796c6fd2807SJeff Garzik }
2797c6fd2807SJeff Garzik 
2798c6fd2807SJeff Garzik /**
2799c6fd2807SJeff Garzik  *	ata_port_probe - Mark port as enabled
2800c6fd2807SJeff Garzik  *	@ap: Port for which we indicate enablement
2801c6fd2807SJeff Garzik  *
2802c6fd2807SJeff Garzik  *	Modify @ap data structure such that the system
2803c6fd2807SJeff Garzik  *	thinks that the entire port is enabled.
2804c6fd2807SJeff Garzik  *
2805cca3974eSJeff Garzik  *	LOCKING: host lock, or some other form of
2806c6fd2807SJeff Garzik  *	serialization.
2807c6fd2807SJeff Garzik  */
2808c6fd2807SJeff Garzik 
2809c6fd2807SJeff Garzik void ata_port_probe(struct ata_port *ap)
2810c6fd2807SJeff Garzik {
2811c6fd2807SJeff Garzik 	ap->flags &= ~ATA_FLAG_DISABLED;
2812c6fd2807SJeff Garzik }
2813c6fd2807SJeff Garzik 
2814c6fd2807SJeff Garzik /**
2815c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2816936fd732STejun Heo  *	@link: SATA link to printk link status about
2817c6fd2807SJeff Garzik  *
2818c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2819c6fd2807SJeff Garzik  *
2820c6fd2807SJeff Garzik  *	LOCKING:
2821c6fd2807SJeff Garzik  *	None.
2822c6fd2807SJeff Garzik  */
28236bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2824c6fd2807SJeff Garzik {
2825c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2826c6fd2807SJeff Garzik 
2827936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2828c6fd2807SJeff Garzik 		return;
2829936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2830c6fd2807SJeff Garzik 
2831b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2832c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2833936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2834c6fd2807SJeff Garzik 				"SATA link up %s (SStatus %X SControl %X)\n",
2835c6fd2807SJeff Garzik 				sata_spd_string(tmp), sstatus, scontrol);
2836c6fd2807SJeff Garzik 	} else {
2837936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2838c6fd2807SJeff Garzik 				"SATA link down (SStatus %X SControl %X)\n",
2839c6fd2807SJeff Garzik 				sstatus, scontrol);
2840c6fd2807SJeff Garzik 	}
2841c6fd2807SJeff Garzik }
2842c6fd2807SJeff Garzik 
2843c6fd2807SJeff Garzik /**
2844c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2845c6fd2807SJeff Garzik  *	@adev: device
2846c6fd2807SJeff Garzik  *
2847c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2848c6fd2807SJeff Garzik  *	present NULL is returned
2849c6fd2807SJeff Garzik  */
2850c6fd2807SJeff Garzik 
2851c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2852c6fd2807SJeff Garzik {
28539af5c9c9STejun Heo 	struct ata_link *link = adev->link;
28549af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2855c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2856c6fd2807SJeff Garzik 		return NULL;
2857c6fd2807SJeff Garzik 	return pair;
2858c6fd2807SJeff Garzik }
2859c6fd2807SJeff Garzik 
2860c6fd2807SJeff Garzik /**
2861c6fd2807SJeff Garzik  *	ata_port_disable - Disable port.
2862c6fd2807SJeff Garzik  *	@ap: Port to be disabled.
2863c6fd2807SJeff Garzik  *
2864c6fd2807SJeff Garzik  *	Modify @ap data structure such that the system
2865c6fd2807SJeff Garzik  *	thinks that the entire port is disabled, and should
2866c6fd2807SJeff Garzik  *	never attempt to probe or communicate with devices
2867c6fd2807SJeff Garzik  *	on this port.
2868c6fd2807SJeff Garzik  *
2869cca3974eSJeff Garzik  *	LOCKING: host lock, or some other form of
2870c6fd2807SJeff Garzik  *	serialization.
2871c6fd2807SJeff Garzik  */
2872c6fd2807SJeff Garzik 
2873c6fd2807SJeff Garzik void ata_port_disable(struct ata_port *ap)
2874c6fd2807SJeff Garzik {
28759af5c9c9STejun Heo 	ap->link.device[0].class = ATA_DEV_NONE;
28769af5c9c9STejun Heo 	ap->link.device[1].class = ATA_DEV_NONE;
2877c6fd2807SJeff Garzik 	ap->flags |= ATA_FLAG_DISABLED;
2878c6fd2807SJeff Garzik }
2879c6fd2807SJeff Garzik 
2880c6fd2807SJeff Garzik /**
2881c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2882936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2883c6fd2807SJeff Garzik  *
2884936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2885c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2886c6fd2807SJeff Garzik  *	using sata_set_spd().
2887c6fd2807SJeff Garzik  *
2888c6fd2807SJeff Garzik  *	LOCKING:
2889c6fd2807SJeff Garzik  *	Inherited from caller.
2890c6fd2807SJeff Garzik  *
2891c6fd2807SJeff Garzik  *	RETURNS:
2892c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
2893c6fd2807SJeff Garzik  */
2894936fd732STejun Heo int sata_down_spd_limit(struct ata_link *link)
2895c6fd2807SJeff Garzik {
2896c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
2897c6fd2807SJeff Garzik 	int rc, highbit;
2898c6fd2807SJeff Garzik 
2899936fd732STejun Heo 	if (!sata_scr_valid(link))
2900008a7896STejun Heo 		return -EOPNOTSUPP;
2901008a7896STejun Heo 
2902008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
2903936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
2904008a7896STejun Heo 	 */
2905936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
2906008a7896STejun Heo 	if (rc == 0)
2907008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
2908008a7896STejun Heo 	else
2909936fd732STejun Heo 		spd = link->sata_spd;
2910c6fd2807SJeff Garzik 
2911936fd732STejun Heo 	mask = link->sata_spd_limit;
2912c6fd2807SJeff Garzik 	if (mask <= 1)
2913c6fd2807SJeff Garzik 		return -EINVAL;
2914008a7896STejun Heo 
2915008a7896STejun Heo 	/* unconditionally mask off the highest bit */
2916c6fd2807SJeff Garzik 	highbit = fls(mask) - 1;
2917c6fd2807SJeff Garzik 	mask &= ~(1 << highbit);
2918c6fd2807SJeff Garzik 
2919008a7896STejun Heo 	/* Mask off all speeds higher than or equal to the current
2920008a7896STejun Heo 	 * one.  Force 1.5Gbps if current SPD is not available.
2921008a7896STejun Heo 	 */
2922008a7896STejun Heo 	if (spd > 1)
2923008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
2924008a7896STejun Heo 	else
2925008a7896STejun Heo 		mask &= 1;
2926008a7896STejun Heo 
2927008a7896STejun Heo 	/* were we already at the bottom? */
2928c6fd2807SJeff Garzik 	if (!mask)
2929c6fd2807SJeff Garzik 		return -EINVAL;
2930c6fd2807SJeff Garzik 
2931936fd732STejun Heo 	link->sata_spd_limit = mask;
2932c6fd2807SJeff Garzik 
2933936fd732STejun Heo 	ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
2934c6fd2807SJeff Garzik 			sata_spd_string(fls(mask)));
2935c6fd2807SJeff Garzik 
2936c6fd2807SJeff Garzik 	return 0;
2937c6fd2807SJeff Garzik }
2938c6fd2807SJeff Garzik 
2939936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2940c6fd2807SJeff Garzik {
29415270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
29425270222fSTejun Heo 	u32 limit, target, spd;
2943c6fd2807SJeff Garzik 
29445270222fSTejun Heo 	limit = link->sata_spd_limit;
29455270222fSTejun Heo 
29465270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
29475270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
29485270222fSTejun Heo 	 * configuration.
29495270222fSTejun Heo 	 */
29505270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
29515270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
29525270222fSTejun Heo 
29535270222fSTejun Heo 	if (limit == UINT_MAX)
29545270222fSTejun Heo 		target = 0;
2955c6fd2807SJeff Garzik 	else
29565270222fSTejun Heo 		target = fls(limit);
2957c6fd2807SJeff Garzik 
2958c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
29595270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2960c6fd2807SJeff Garzik 
29615270222fSTejun Heo 	return spd != target;
2962c6fd2807SJeff Garzik }
2963c6fd2807SJeff Garzik 
2964c6fd2807SJeff Garzik /**
2965c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
2966936fd732STejun Heo  *	@link: Link in question
2967c6fd2807SJeff Garzik  *
2968c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
2969936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
2970c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
2971c6fd2807SJeff Garzik  *	configuration.
2972c6fd2807SJeff Garzik  *
2973c6fd2807SJeff Garzik  *	LOCKING:
2974c6fd2807SJeff Garzik  *	Inherited from caller.
2975c6fd2807SJeff Garzik  *
2976c6fd2807SJeff Garzik  *	RETURNS:
2977c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
2978c6fd2807SJeff Garzik  */
29791dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
2980c6fd2807SJeff Garzik {
2981c6fd2807SJeff Garzik 	u32 scontrol;
2982c6fd2807SJeff Garzik 
2983936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2984db64bcf3STejun Heo 		return 1;
2985c6fd2807SJeff Garzik 
2986936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
2987c6fd2807SJeff Garzik }
2988c6fd2807SJeff Garzik 
2989c6fd2807SJeff Garzik /**
2990c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
2991936fd732STejun Heo  *	@link: Link to set SATA spd for
2992c6fd2807SJeff Garzik  *
2993936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
2994c6fd2807SJeff Garzik  *
2995c6fd2807SJeff Garzik  *	LOCKING:
2996c6fd2807SJeff Garzik  *	Inherited from caller.
2997c6fd2807SJeff Garzik  *
2998c6fd2807SJeff Garzik  *	RETURNS:
2999c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
3000c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
3001c6fd2807SJeff Garzik  */
3002936fd732STejun Heo int sata_set_spd(struct ata_link *link)
3003c6fd2807SJeff Garzik {
3004c6fd2807SJeff Garzik 	u32 scontrol;
3005c6fd2807SJeff Garzik 	int rc;
3006c6fd2807SJeff Garzik 
3007936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3008c6fd2807SJeff Garzik 		return rc;
3009c6fd2807SJeff Garzik 
3010936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
3011c6fd2807SJeff Garzik 		return 0;
3012c6fd2807SJeff Garzik 
3013936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3014c6fd2807SJeff Garzik 		return rc;
3015c6fd2807SJeff Garzik 
3016c6fd2807SJeff Garzik 	return 1;
3017c6fd2807SJeff Garzik }
3018c6fd2807SJeff Garzik 
3019c6fd2807SJeff Garzik /*
3020c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
3021c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3022c6fd2807SJeff Garzik  */
3023c6fd2807SJeff Garzik /*
3024b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3025c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3026b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
3027b352e57dSAlan Cox  *
3028b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3029c6fd2807SJeff Garzik  */
3030c6fd2807SJeff Garzik 
3031c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
303270cd071eSTejun Heo /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960,   0 }, */
303370cd071eSTejun Heo 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 600,   0 },
303470cd071eSTejun Heo 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 383,   0 },
303570cd071eSTejun Heo 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 240,   0 },
303670cd071eSTejun Heo 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 180,   0 },
303770cd071eSTejun Heo 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 120,   0 },
303870cd071eSTejun Heo 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 100,   0 },
303970cd071eSTejun Heo 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20,  80,   0 },
3040c6fd2807SJeff Garzik 
304170cd071eSTejun Heo 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 960,   0 },
304270cd071eSTejun Heo 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 480,   0 },
304370cd071eSTejun Heo 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 240,   0 },
3044c6fd2807SJeff Garzik 
304570cd071eSTejun Heo 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 480,   0 },
304670cd071eSTejun Heo 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 150,   0 },
304770cd071eSTejun Heo 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 120,   0 },
3048b352e57dSAlan Cox 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 100,   0 },
304970cd071eSTejun Heo 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20,  80,   0 },
3050c6fd2807SJeff Garzik 
3051c6fd2807SJeff Garzik /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0,   0, 150 }, */
305270cd071eSTejun Heo 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0,   0, 120 },
305370cd071eSTejun Heo 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0,   0,  80 },
305470cd071eSTejun Heo 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0,   0,  60 },
305570cd071eSTejun Heo 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0,   0,  45 },
305670cd071eSTejun Heo 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0,   0,  30 },
305770cd071eSTejun Heo 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0,   0,  20 },
305870cd071eSTejun Heo 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0,   0,  15 },
3059c6fd2807SJeff Garzik 
3060c6fd2807SJeff Garzik 	{ 0xFF }
3061c6fd2807SJeff Garzik };
3062c6fd2807SJeff Garzik 
3063c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
3064c6fd2807SJeff Garzik #define EZ(v, unit)		((v)?ENOUGH(v, unit):0)
3065c6fd2807SJeff Garzik 
3066c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3067c6fd2807SJeff Garzik {
3068c6fd2807SJeff Garzik 	q->setup   = EZ(t->setup   * 1000,  T);
3069c6fd2807SJeff Garzik 	q->act8b   = EZ(t->act8b   * 1000,  T);
3070c6fd2807SJeff Garzik 	q->rec8b   = EZ(t->rec8b   * 1000,  T);
3071c6fd2807SJeff Garzik 	q->cyc8b   = EZ(t->cyc8b   * 1000,  T);
3072c6fd2807SJeff Garzik 	q->active  = EZ(t->active  * 1000,  T);
3073c6fd2807SJeff Garzik 	q->recover = EZ(t->recover * 1000,  T);
3074c6fd2807SJeff Garzik 	q->cycle   = EZ(t->cycle   * 1000,  T);
3075c6fd2807SJeff Garzik 	q->udma    = EZ(t->udma    * 1000, UT);
3076c6fd2807SJeff Garzik }
3077c6fd2807SJeff Garzik 
3078c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3079c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
3080c6fd2807SJeff Garzik {
3081c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
3082c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
3083c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
3084c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
3085c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
3086c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
3087c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
3088c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
3089c6fd2807SJeff Garzik }
3090c6fd2807SJeff Garzik 
30916357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3092c6fd2807SJeff Garzik {
309370cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
3094c6fd2807SJeff Garzik 
309570cd071eSTejun Heo 	while (xfer_mode > t->mode)
309670cd071eSTejun Heo 		t++;
309770cd071eSTejun Heo 
309870cd071eSTejun Heo 	if (xfer_mode == t->mode)
3099c6fd2807SJeff Garzik 		return t;
310070cd071eSTejun Heo 	return NULL;
3101c6fd2807SJeff Garzik }
3102c6fd2807SJeff Garzik 
3103c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3104c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3105c6fd2807SJeff Garzik {
3106c6fd2807SJeff Garzik 	const struct ata_timing *s;
3107c6fd2807SJeff Garzik 	struct ata_timing p;
3108c6fd2807SJeff Garzik 
3109c6fd2807SJeff Garzik 	/*
3110c6fd2807SJeff Garzik 	 * Find the mode.
3111c6fd2807SJeff Garzik 	 */
3112c6fd2807SJeff Garzik 
3113c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3114c6fd2807SJeff Garzik 		return -EINVAL;
3115c6fd2807SJeff Garzik 
3116c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3117c6fd2807SJeff Garzik 
3118c6fd2807SJeff Garzik 	/*
3119c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3120c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3121c6fd2807SJeff Garzik 	 */
3122c6fd2807SJeff Garzik 
3123c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3124c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
3125c6fd2807SJeff Garzik 		if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
3126c6fd2807SJeff Garzik 			if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
3127c6fd2807SJeff Garzik 					    else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
3128c6fd2807SJeff Garzik 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
3129c6fd2807SJeff Garzik 			p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
3130c6fd2807SJeff Garzik 		}
3131c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3132c6fd2807SJeff Garzik 	}
3133c6fd2807SJeff Garzik 
3134c6fd2807SJeff Garzik 	/*
3135c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3136c6fd2807SJeff Garzik 	 */
3137c6fd2807SJeff Garzik 
3138c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3139c6fd2807SJeff Garzik 
3140c6fd2807SJeff Garzik 	/*
3141c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3142c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3143c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3144c6fd2807SJeff Garzik 	 */
3145c6fd2807SJeff Garzik 
3146fd3367afSAlan 	if (speed > XFER_PIO_6) {
3147c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3148c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3149c6fd2807SJeff Garzik 	}
3150c6fd2807SJeff Garzik 
3151c6fd2807SJeff Garzik 	/*
3152c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3153c6fd2807SJeff Garzik 	 */
3154c6fd2807SJeff Garzik 
3155c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3156c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3157c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3158c6fd2807SJeff Garzik 	}
3159c6fd2807SJeff Garzik 
3160c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3161c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3162c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3163c6fd2807SJeff Garzik 	}
31644f701d1eSAlan Cox 
31654f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
31664f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
31674f701d1eSAlan Cox 	   if so we must correct this */
31684f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
31694f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3170c6fd2807SJeff Garzik 
3171c6fd2807SJeff Garzik 	return 0;
3172c6fd2807SJeff Garzik }
3173c6fd2807SJeff Garzik 
3174c6fd2807SJeff Garzik /**
3175a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3176a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3177a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3178a0f79b92STejun Heo  *
3179a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3180a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3181a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3182a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3183a0f79b92STejun Heo  *
3184a0f79b92STejun Heo  *	LOCKING:
3185a0f79b92STejun Heo  *	None.
3186a0f79b92STejun Heo  *
3187a0f79b92STejun Heo  *	RETURNS:
3188a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3189a0f79b92STejun Heo  */
3190a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3191a0f79b92STejun Heo {
3192a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3193a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3194a0f79b92STejun Heo 	const struct ata_timing *t;
3195a0f79b92STejun Heo 
3196a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3197a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3198a0f79b92STejun Heo 			base_mode = ent->base;
3199a0f79b92STejun Heo 
3200a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3201a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3202a0f79b92STejun Heo 		unsigned short this_cycle;
3203a0f79b92STejun Heo 
3204a0f79b92STejun Heo 		switch (xfer_shift) {
3205a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3206a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3207a0f79b92STejun Heo 			this_cycle = t->cycle;
3208a0f79b92STejun Heo 			break;
3209a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3210a0f79b92STejun Heo 			this_cycle = t->udma;
3211a0f79b92STejun Heo 			break;
3212a0f79b92STejun Heo 		default:
3213a0f79b92STejun Heo 			return 0xff;
3214a0f79b92STejun Heo 		}
3215a0f79b92STejun Heo 
3216a0f79b92STejun Heo 		if (cycle > this_cycle)
3217a0f79b92STejun Heo 			break;
3218a0f79b92STejun Heo 
3219a0f79b92STejun Heo 		last_mode = t->mode;
3220a0f79b92STejun Heo 	}
3221a0f79b92STejun Heo 
3222a0f79b92STejun Heo 	return last_mode;
3223a0f79b92STejun Heo }
3224a0f79b92STejun Heo 
3225a0f79b92STejun Heo /**
3226c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3227c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3228458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3229c6fd2807SJeff Garzik  *
3230c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3231c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3232c6fd2807SJeff Garzik  *	will apply the limit.
3233c6fd2807SJeff Garzik  *
3234c6fd2807SJeff Garzik  *	LOCKING:
3235c6fd2807SJeff Garzik  *	Inherited from caller.
3236c6fd2807SJeff Garzik  *
3237c6fd2807SJeff Garzik  *	RETURNS:
3238c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3239c6fd2807SJeff Garzik  */
3240458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3241c6fd2807SJeff Garzik {
3242458337dbSTejun Heo 	char buf[32];
32437dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
32447dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3245458337dbSTejun Heo 	int quiet, highbit;
3246c6fd2807SJeff Garzik 
3247458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3248458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3249458337dbSTejun Heo 
3250458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3251458337dbSTejun Heo 						  dev->mwdma_mask,
3252c6fd2807SJeff Garzik 						  dev->udma_mask);
3253458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3254c6fd2807SJeff Garzik 
3255458337dbSTejun Heo 	switch (sel) {
3256458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3257458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3258458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3259458337dbSTejun Heo 		break;
3260458337dbSTejun Heo 
3261458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3262458337dbSTejun Heo 		if (udma_mask) {
3263458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3264458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3265458337dbSTejun Heo 			if (!udma_mask)
3266458337dbSTejun Heo 				return -ENOENT;
3267458337dbSTejun Heo 		} else if (mwdma_mask) {
3268458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3269458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3270458337dbSTejun Heo 			if (!mwdma_mask)
3271458337dbSTejun Heo 				return -ENOENT;
3272458337dbSTejun Heo 		}
3273458337dbSTejun Heo 		break;
3274458337dbSTejun Heo 
3275458337dbSTejun Heo 	case ATA_DNXFER_40C:
3276458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3277458337dbSTejun Heo 		break;
3278458337dbSTejun Heo 
3279458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3280458337dbSTejun Heo 		pio_mask &= 1;
3281458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3282458337dbSTejun Heo 		mwdma_mask = 0;
3283458337dbSTejun Heo 		udma_mask = 0;
3284458337dbSTejun Heo 		break;
3285458337dbSTejun Heo 
3286458337dbSTejun Heo 	default:
3287458337dbSTejun Heo 		BUG();
3288458337dbSTejun Heo 	}
3289458337dbSTejun Heo 
3290458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3291458337dbSTejun Heo 
3292458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3293458337dbSTejun Heo 		return -ENOENT;
3294458337dbSTejun Heo 
3295458337dbSTejun Heo 	if (!quiet) {
3296458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3297458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3298458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3299458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3300458337dbSTejun Heo 		else
3301458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3302458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3303458337dbSTejun Heo 
3304458337dbSTejun Heo 		ata_dev_printk(dev, KERN_WARNING,
3305458337dbSTejun Heo 			       "limiting speed to %s\n", buf);
3306458337dbSTejun Heo 	}
3307c6fd2807SJeff Garzik 
3308c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3309c6fd2807SJeff Garzik 			    &dev->udma_mask);
3310c6fd2807SJeff Garzik 
3311c6fd2807SJeff Garzik 	return 0;
3312c6fd2807SJeff Garzik }
3313c6fd2807SJeff Garzik 
3314c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3315c6fd2807SJeff Garzik {
33169af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
33174055dee7STejun Heo 	const char *dev_err_whine = "";
33184055dee7STejun Heo 	int ign_dev_err = 0;
3319c6fd2807SJeff Garzik 	unsigned int err_mask;
3320c6fd2807SJeff Garzik 	int rc;
3321c6fd2807SJeff Garzik 
3322c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3323c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3324c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3325c6fd2807SJeff Garzik 
3326c6fd2807SJeff Garzik 	err_mask = ata_dev_set_xfermode(dev);
33272dcb407eSJeff Garzik 
33284055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
33294055dee7STejun Heo 		goto fail;
33302dcb407eSJeff Garzik 
33314055dee7STejun Heo 	/* revalidate */
3332baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3333422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3334baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3335c6fd2807SJeff Garzik 	if (rc)
3336c6fd2807SJeff Garzik 		return rc;
3337c6fd2807SJeff Garzik 
3338b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
33394055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3340b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
33414055dee7STejun Heo 			ign_dev_err = 1;
3342b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3343b93fda12SAlan Cox 		   ATA devices */
3344b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
33454055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
33464055dee7STejun Heo 			ign_dev_err = 1;
3347b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3348b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3349b93fda12SAlan Cox 		   timings and no IORDY */
3350b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3351b93fda12SAlan Cox 			ign_dev_err = 1;
3352b93fda12SAlan Cox 	}
33534055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
33544055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
33554055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
33564055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
33574055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
33584055dee7STejun Heo 		ign_dev_err = 1;
33594055dee7STejun Heo 
33604055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
33614055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
33624055dee7STejun Heo 		ign_dev_err = 1;
33634055dee7STejun Heo 
33644055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
33654055dee7STejun Heo 		if (!ign_dev_err)
33664055dee7STejun Heo 			goto fail;
33674055dee7STejun Heo 		else
33684055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
33694055dee7STejun Heo 	}
33704055dee7STejun Heo 
3371c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3372c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3373c6fd2807SJeff Garzik 
33744055dee7STejun Heo 	ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
33754055dee7STejun Heo 		       ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
33764055dee7STejun Heo 		       dev_err_whine);
33774055dee7STejun Heo 
3378c6fd2807SJeff Garzik 	return 0;
33794055dee7STejun Heo 
33804055dee7STejun Heo  fail:
33814055dee7STejun Heo 	ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
33824055dee7STejun Heo 		       "(err_mask=0x%x)\n", err_mask);
33834055dee7STejun Heo 	return -EIO;
3384c6fd2807SJeff Garzik }
3385c6fd2807SJeff Garzik 
3386c6fd2807SJeff Garzik /**
338704351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
33880260731fSTejun Heo  *	@link: link on which timings will be programmed
33891967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3390c6fd2807SJeff Garzik  *
339104351821SAlan  *	Standard implementation of the function used to tune and set
339204351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
339304351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3394c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3395c6fd2807SJeff Garzik  *
3396c6fd2807SJeff Garzik  *	LOCKING:
3397c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3398c6fd2807SJeff Garzik  *
3399c6fd2807SJeff Garzik  *	RETURNS:
3400c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3401c6fd2807SJeff Garzik  */
340204351821SAlan 
34030260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3404c6fd2807SJeff Garzik {
34050260731fSTejun Heo 	struct ata_port *ap = link->ap;
3406c6fd2807SJeff Garzik 	struct ata_device *dev;
3407f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3408c6fd2807SJeff Garzik 
3409c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
34101eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
34117dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3412b3a70601SAlan Cox 		unsigned int mode_mask;
3413c6fd2807SJeff Garzik 
3414b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3415b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3416b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3417b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3418b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3419b3a70601SAlan Cox 
3420c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
342133267325STejun Heo 		ata_force_xfermask(dev);
3422c6fd2807SJeff Garzik 
3423c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3424c6fd2807SJeff Garzik 		dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3425b3a70601SAlan Cox 
3426b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
3427b3a70601SAlan Cox 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3428b3a70601SAlan Cox 		else
3429b3a70601SAlan Cox 			dma_mask = 0;
3430b3a70601SAlan Cox 
3431c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3432c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3433c6fd2807SJeff Garzik 
3434c6fd2807SJeff Garzik 		found = 1;
3435b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3436c6fd2807SJeff Garzik 			used_dma = 1;
3437c6fd2807SJeff Garzik 	}
3438c6fd2807SJeff Garzik 	if (!found)
3439c6fd2807SJeff Garzik 		goto out;
3440c6fd2807SJeff Garzik 
3441c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
34421eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
344370cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3444c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3445c6fd2807SJeff Garzik 			rc = -EINVAL;
3446c6fd2807SJeff Garzik 			goto out;
3447c6fd2807SJeff Garzik 		}
3448c6fd2807SJeff Garzik 
3449c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3450c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3451c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3452c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3453c6fd2807SJeff Garzik 	}
3454c6fd2807SJeff Garzik 
3455c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
34561eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
34571eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3458c6fd2807SJeff Garzik 			continue;
3459c6fd2807SJeff Garzik 
3460c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3461c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3462c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3463c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3464c6fd2807SJeff Garzik 	}
3465c6fd2807SJeff Garzik 
3466c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
34671eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3468c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3469c6fd2807SJeff Garzik 		if (rc)
3470c6fd2807SJeff Garzik 			goto out;
3471c6fd2807SJeff Garzik 	}
3472c6fd2807SJeff Garzik 
3473c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3474c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3475c6fd2807SJeff Garzik 	 */
3476cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3477032af1ceSAlan 		ap->host->simplex_claimed = ap;
3478c6fd2807SJeff Garzik 
3479c6fd2807SJeff Garzik  out:
3480c6fd2807SJeff Garzik 	if (rc)
3481c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3482c6fd2807SJeff Garzik 	return rc;
3483c6fd2807SJeff Garzik }
3484c6fd2807SJeff Garzik 
3485c6fd2807SJeff Garzik /**
3486aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3487aa2731adSTejun Heo  *	@link: link to be waited on
3488aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3489aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3490aa2731adSTejun Heo  *
3491aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3492aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3493aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3494aa2731adSTejun Heo  *	conditions.
3495aa2731adSTejun Heo  *
3496aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3497aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3498aa2731adSTejun Heo  *
3499aa2731adSTejun Heo  *	LOCKING:
3500aa2731adSTejun Heo  *	EH context.
3501aa2731adSTejun Heo  *
3502aa2731adSTejun Heo  *	RETURNS:
3503aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3504aa2731adSTejun Heo  */
3505aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3506aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3507aa2731adSTejun Heo {
3508aa2731adSTejun Heo 	unsigned long start = jiffies;
3509341c2c95STejun Heo 	unsigned long nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3510aa2731adSTejun Heo 	int warned = 0;
3511aa2731adSTejun Heo 
3512b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3513b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3514b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3515b1c72916STejun Heo 	 */
3516b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3517b1c72916STejun Heo 
3518aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3519aa2731adSTejun Heo 		nodev_deadline = deadline;
3520aa2731adSTejun Heo 
3521aa2731adSTejun Heo 	while (1) {
3522aa2731adSTejun Heo 		unsigned long now = jiffies;
3523aa2731adSTejun Heo 		int ready, tmp;
3524aa2731adSTejun Heo 
3525aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3526aa2731adSTejun Heo 		if (ready > 0)
3527aa2731adSTejun Heo 			return 0;
3528aa2731adSTejun Heo 
3529aa2731adSTejun Heo 		/* -ENODEV could be transient.  Ignore -ENODEV if link
3530aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3531aa2731adSTejun Heo 		 * time to clear 0xff after reset.  For example,
3532aa2731adSTejun Heo 		 * HHD424020F7SV00 iVDR needs >= 800ms while Quantum
3533aa2731adSTejun Heo 		 * GoVault needs even more than that.  Wait for
3534aa2731adSTejun Heo 		 * ATA_TMOUT_FF_WAIT on -ENODEV if link isn't offline.
3535aa2731adSTejun Heo 		 *
3536aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3537aa2731adSTejun Heo 		 * if status register is read more than once when
3538aa2731adSTejun Heo 		 * there's no device attached.
3539aa2731adSTejun Heo 		 */
3540aa2731adSTejun Heo 		if (ready == -ENODEV) {
3541aa2731adSTejun Heo 			if (ata_link_online(link))
3542aa2731adSTejun Heo 				ready = 0;
3543aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3544aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3545aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3546aa2731adSTejun Heo 				ready = 0;
3547aa2731adSTejun Heo 		}
3548aa2731adSTejun Heo 
3549aa2731adSTejun Heo 		if (ready)
3550aa2731adSTejun Heo 			return ready;
3551aa2731adSTejun Heo 		if (time_after(now, deadline))
3552aa2731adSTejun Heo 			return -EBUSY;
3553aa2731adSTejun Heo 
3554aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3555aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3556aa2731adSTejun Heo 			ata_link_printk(link, KERN_WARNING,
3557aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3558aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3559aa2731adSTejun Heo 			warned = 1;
3560aa2731adSTejun Heo 		}
3561aa2731adSTejun Heo 
3562aa2731adSTejun Heo 		msleep(50);
3563aa2731adSTejun Heo 	}
3564aa2731adSTejun Heo }
3565aa2731adSTejun Heo 
3566aa2731adSTejun Heo /**
3567aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3568aa2731adSTejun Heo  *	@link: link to be waited on
3569aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3570aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3571aa2731adSTejun Heo  *
3572aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3573aa2731adSTejun Heo  *
3574aa2731adSTejun Heo  *	LOCKING:
3575aa2731adSTejun Heo  *	EH context.
3576aa2731adSTejun Heo  *
3577aa2731adSTejun Heo  *	RETURNS:
3578aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3579aa2731adSTejun Heo  */
35802b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3581aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3582aa2731adSTejun Heo {
3583341c2c95STejun Heo 	msleep(ATA_WAIT_AFTER_RESET);
3584aa2731adSTejun Heo 
3585aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3586aa2731adSTejun Heo }
3587aa2731adSTejun Heo 
3588aa2731adSTejun Heo /**
3589936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3590936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3591c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3592d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3593c6fd2807SJeff Garzik  *
3594936fd732STejun Heo *	Make sure SStatus of @link reaches stable state, determined by
3595c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3596c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3597d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3598d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3599c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3600c6fd2807SJeff Garzik  *
3601d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3602d4b2bab4STejun Heo  *	two is used.
3603d4b2bab4STejun Heo  *
3604c6fd2807SJeff Garzik  *	LOCKING:
3605c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3606c6fd2807SJeff Garzik  *
3607c6fd2807SJeff Garzik  *	RETURNS:
3608c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3609c6fd2807SJeff Garzik  */
3610936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3611d4b2bab4STejun Heo 		       unsigned long deadline)
3612c6fd2807SJeff Garzik {
3613341c2c95STejun Heo 	unsigned long interval = params[0];
3614341c2c95STejun Heo 	unsigned long duration = params[1];
3615d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3616c6fd2807SJeff Garzik 	u32 last, cur;
3617c6fd2807SJeff Garzik 	int rc;
3618c6fd2807SJeff Garzik 
3619341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3620d4b2bab4STejun Heo 	if (time_before(t, deadline))
3621d4b2bab4STejun Heo 		deadline = t;
3622d4b2bab4STejun Heo 
3623936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3624c6fd2807SJeff Garzik 		return rc;
3625c6fd2807SJeff Garzik 	cur &= 0xf;
3626c6fd2807SJeff Garzik 
3627c6fd2807SJeff Garzik 	last = cur;
3628c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3629c6fd2807SJeff Garzik 
3630c6fd2807SJeff Garzik 	while (1) {
3631341c2c95STejun Heo 		msleep(interval);
3632936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3633c6fd2807SJeff Garzik 			return rc;
3634c6fd2807SJeff Garzik 		cur &= 0xf;
3635c6fd2807SJeff Garzik 
3636c6fd2807SJeff Garzik 		/* DET stable? */
3637c6fd2807SJeff Garzik 		if (cur == last) {
3638d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3639c6fd2807SJeff Garzik 				continue;
3640341c2c95STejun Heo 			if (time_after(jiffies,
3641341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3642c6fd2807SJeff Garzik 				return 0;
3643c6fd2807SJeff Garzik 			continue;
3644c6fd2807SJeff Garzik 		}
3645c6fd2807SJeff Garzik 
3646c6fd2807SJeff Garzik 		/* unstable, start over */
3647c6fd2807SJeff Garzik 		last = cur;
3648c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3649c6fd2807SJeff Garzik 
3650f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3651f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3652f1545154STejun Heo 		 */
3653d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3654f1545154STejun Heo 			return -EPIPE;
3655c6fd2807SJeff Garzik 	}
3656c6fd2807SJeff Garzik }
3657c6fd2807SJeff Garzik 
3658c6fd2807SJeff Garzik /**
3659936fd732STejun Heo  *	sata_link_resume - resume SATA link
3660936fd732STejun Heo  *	@link: ATA link to resume SATA
3661c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3662d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3663c6fd2807SJeff Garzik  *
3664936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3665c6fd2807SJeff Garzik  *
3666c6fd2807SJeff Garzik  *	LOCKING:
3667c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3668c6fd2807SJeff Garzik  *
3669c6fd2807SJeff Garzik  *	RETURNS:
3670c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3671c6fd2807SJeff Garzik  */
3672936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3673d4b2bab4STejun Heo 		     unsigned long deadline)
3674c6fd2807SJeff Garzik {
3675ac371987STejun Heo 	u32 scontrol, serror;
3676c6fd2807SJeff Garzik 	int rc;
3677c6fd2807SJeff Garzik 
3678936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3679c6fd2807SJeff Garzik 		return rc;
3680c6fd2807SJeff Garzik 
3681c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x300;
3682c6fd2807SJeff Garzik 
3683936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3684c6fd2807SJeff Garzik 		return rc;
3685c6fd2807SJeff Garzik 
3686c6fd2807SJeff Garzik 	/* Some PHYs react badly if SStatus is pounded immediately
3687c6fd2807SJeff Garzik 	 * after resuming.  Delay 200ms before debouncing.
3688c6fd2807SJeff Garzik 	 */
3689c6fd2807SJeff Garzik 	msleep(200);
3690c6fd2807SJeff Garzik 
3691ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3692ac371987STejun Heo 		return rc;
3693ac371987STejun Heo 
3694f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3695ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3696ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3697ac371987STejun Heo 
3698f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3699c6fd2807SJeff Garzik }
3700c6fd2807SJeff Garzik 
3701c6fd2807SJeff Garzik /**
37020aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3703cc0680a5STejun Heo  *	@link: ATA link to be reset
3704d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3705c6fd2807SJeff Garzik  *
3706cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3707b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3708b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3709b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3710b8cffc6aSTejun Heo  *	should just whine, not fail.
3711c6fd2807SJeff Garzik  *
3712c6fd2807SJeff Garzik  *	LOCKING:
3713c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3714c6fd2807SJeff Garzik  *
3715c6fd2807SJeff Garzik  *	RETURNS:
3716c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3717c6fd2807SJeff Garzik  */
37180aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3719c6fd2807SJeff Garzik {
3720cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3721936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3722c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3723c6fd2807SJeff Garzik 	int rc;
3724c6fd2807SJeff Garzik 
3725c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3726c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3727c6fd2807SJeff Garzik 		return 0;
3728c6fd2807SJeff Garzik 
3729936fd732STejun Heo 	/* if SATA, resume link */
3730a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3731936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3732b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3733b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3734cc0680a5STejun Heo 			ata_link_printk(link, KERN_WARNING, "failed to resume "
3735c6fd2807SJeff Garzik 					"link for reset (errno=%d)\n", rc);
3736c6fd2807SJeff Garzik 	}
3737c6fd2807SJeff Garzik 
373845db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3739b1c72916STejun Heo 	if (ata_phys_link_offline(link))
374045db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
374145db2f6cSTejun Heo 
3742c6fd2807SJeff Garzik 	return 0;
3743c6fd2807SJeff Garzik }
3744c6fd2807SJeff Garzik 
3745c6fd2807SJeff Garzik /**
3746cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3747cc0680a5STejun Heo  *	@link: link to reset
3748b6103f6dSTejun Heo  *	@timing: timing parameters { interval, duratinon, timeout } in msec
3749d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
37509dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
37519dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3752c6fd2807SJeff Garzik  *
3753cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
37549dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
37559dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
37569dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
37579dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
37589dadd45bSTejun Heo  *	responsibility.
37599dadd45bSTejun Heo  *
37609dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
37619dadd45bSTejun Heo  *	after reset.
3762c6fd2807SJeff Garzik  *
3763c6fd2807SJeff Garzik  *	LOCKING:
3764c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3765c6fd2807SJeff Garzik  *
3766c6fd2807SJeff Garzik  *	RETURNS:
3767c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3768c6fd2807SJeff Garzik  */
3769cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
37709dadd45bSTejun Heo 			unsigned long deadline,
37719dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3772c6fd2807SJeff Garzik {
3773c6fd2807SJeff Garzik 	u32 scontrol;
3774c6fd2807SJeff Garzik 	int rc;
3775c6fd2807SJeff Garzik 
3776c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3777c6fd2807SJeff Garzik 
37789dadd45bSTejun Heo 	if (online)
37799dadd45bSTejun Heo 		*online = false;
37809dadd45bSTejun Heo 
3781936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3782c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
3783c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
3784c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
3785c6fd2807SJeff Garzik 		 * and Sil3124.
3786c6fd2807SJeff Garzik 		 */
3787936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3788b6103f6dSTejun Heo 			goto out;
3789c6fd2807SJeff Garzik 
3790cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
3791c6fd2807SJeff Garzik 
3792936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3793b6103f6dSTejun Heo 			goto out;
3794c6fd2807SJeff Garzik 
3795936fd732STejun Heo 		sata_set_spd(link);
3796c6fd2807SJeff Garzik 	}
3797c6fd2807SJeff Garzik 
3798c6fd2807SJeff Garzik 	/* issue phy wake/reset */
3799936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3800b6103f6dSTejun Heo 		goto out;
3801c6fd2807SJeff Garzik 
3802c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
3803c6fd2807SJeff Garzik 
3804936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3805b6103f6dSTejun Heo 		goto out;
3806c6fd2807SJeff Garzik 
3807c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3808c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
3809c6fd2807SJeff Garzik 	 */
3810c6fd2807SJeff Garzik 	msleep(1);
3811c6fd2807SJeff Garzik 
3812936fd732STejun Heo 	/* bring link back */
3813936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
38149dadd45bSTejun Heo 	if (rc)
38159dadd45bSTejun Heo 		goto out;
38169dadd45bSTejun Heo 	/* if link is offline nothing more to do */
3817b1c72916STejun Heo 	if (ata_phys_link_offline(link))
38189dadd45bSTejun Heo 		goto out;
38199dadd45bSTejun Heo 
38209dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
38219dadd45bSTejun Heo 	if (online)
38229dadd45bSTejun Heo 		*online = true;
38239dadd45bSTejun Heo 
3824071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
38259dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
38269dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
38279dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
38289dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
38299dadd45bSTejun Heo 		 */
38309dadd45bSTejun Heo 		if (check_ready) {
38319dadd45bSTejun Heo 			unsigned long pmp_deadline;
38329dadd45bSTejun Heo 
3833341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
3834341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
38359dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
38369dadd45bSTejun Heo 				pmp_deadline = deadline;
38379dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
38389dadd45bSTejun Heo 		}
38399dadd45bSTejun Heo 		rc = -EAGAIN;
38409dadd45bSTejun Heo 		goto out;
38419dadd45bSTejun Heo 	}
38429dadd45bSTejun Heo 
38439dadd45bSTejun Heo 	rc = 0;
38449dadd45bSTejun Heo 	if (check_ready)
38459dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
3846b6103f6dSTejun Heo  out:
38470cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
38480cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
38490cbf0711STejun Heo 		if (online)
38500cbf0711STejun Heo 			*online = false;
38519dadd45bSTejun Heo 		ata_link_printk(link, KERN_ERR,
38529dadd45bSTejun Heo 				"COMRESET failed (errno=%d)\n", rc);
38530cbf0711STejun Heo 	}
3854b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
3855b6103f6dSTejun Heo 	return rc;
3856b6103f6dSTejun Heo }
3857b6103f6dSTejun Heo 
3858b6103f6dSTejun Heo /**
385957c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
386057c9efdfSTejun Heo  *	@link: link to reset
386157c9efdfSTejun Heo  *	@class: resulting class of attached device
386257c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
386357c9efdfSTejun Heo  *
386457c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
386557c9efdfSTejun Heo  *
386657c9efdfSTejun Heo  *	LOCKING:
386757c9efdfSTejun Heo  *	Kernel thread context (may sleep)
386857c9efdfSTejun Heo  *
386957c9efdfSTejun Heo  *	RETURNS:
387057c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
387157c9efdfSTejun Heo  */
387257c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
387357c9efdfSTejun Heo 		       unsigned long deadline)
387457c9efdfSTejun Heo {
387557c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
387657c9efdfSTejun Heo 	bool online;
387757c9efdfSTejun Heo 	int rc;
387857c9efdfSTejun Heo 
387957c9efdfSTejun Heo 	/* do hardreset */
388057c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
388157c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
388257c9efdfSTejun Heo }
388357c9efdfSTejun Heo 
388457c9efdfSTejun Heo /**
3885203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3886cc0680a5STejun Heo  *	@link: the target ata_link
3887c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3888c6fd2807SJeff Garzik  *
3889c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3890c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3891c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3892c6fd2807SJeff Garzik  *
3893c6fd2807SJeff Garzik  *	LOCKING:
3894c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3895c6fd2807SJeff Garzik  */
3896203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3897c6fd2807SJeff Garzik {
3898f046519fSTejun Heo 	u32 serror;
3899f046519fSTejun Heo 
3900c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3901c6fd2807SJeff Garzik 
3902f046519fSTejun Heo 	/* reset complete, clear SError */
3903f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3904f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3905f046519fSTejun Heo 
3906c6fd2807SJeff Garzik 	/* print link status */
3907936fd732STejun Heo 	sata_print_link_status(link);
3908c6fd2807SJeff Garzik 
3909c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
3910c6fd2807SJeff Garzik }
3911c6fd2807SJeff Garzik 
3912c6fd2807SJeff Garzik /**
3913c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3914c6fd2807SJeff Garzik  *	@dev: device to compare against
3915c6fd2807SJeff Garzik  *	@new_class: class of the new device
3916c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3917c6fd2807SJeff Garzik  *
3918c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3919c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3920c6fd2807SJeff Garzik  *	@new_id.
3921c6fd2807SJeff Garzik  *
3922c6fd2807SJeff Garzik  *	LOCKING:
3923c6fd2807SJeff Garzik  *	None.
3924c6fd2807SJeff Garzik  *
3925c6fd2807SJeff Garzik  *	RETURNS:
3926c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3927c6fd2807SJeff Garzik  */
3928c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3929c6fd2807SJeff Garzik 			       const u16 *new_id)
3930c6fd2807SJeff Garzik {
3931c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3932a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3933a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3934c6fd2807SJeff Garzik 
3935c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3936c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3937c6fd2807SJeff Garzik 			       dev->class, new_class);
3938c6fd2807SJeff Garzik 		return 0;
3939c6fd2807SJeff Garzik 	}
3940c6fd2807SJeff Garzik 
3941a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3942a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3943a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3944a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3945c6fd2807SJeff Garzik 
3946c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3947c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3948c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", model[0], model[1]);
3949c6fd2807SJeff Garzik 		return 0;
3950c6fd2807SJeff Garzik 	}
3951c6fd2807SJeff Garzik 
3952c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3953c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3954c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", serial[0], serial[1]);
3955c6fd2807SJeff Garzik 		return 0;
3956c6fd2807SJeff Garzik 	}
3957c6fd2807SJeff Garzik 
3958c6fd2807SJeff Garzik 	return 1;
3959c6fd2807SJeff Garzik }
3960c6fd2807SJeff Garzik 
3961c6fd2807SJeff Garzik /**
3962fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
39633fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3964bff04647STejun Heo  *	@readid_flags: read ID flags
3965c6fd2807SJeff Garzik  *
3966c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3967c6fd2807SJeff Garzik  *	the port.
3968c6fd2807SJeff Garzik  *
3969c6fd2807SJeff Garzik  *	LOCKING:
3970c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3971c6fd2807SJeff Garzik  *
3972c6fd2807SJeff Garzik  *	RETURNS:
3973c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3974c6fd2807SJeff Garzik  */
3975fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3976c6fd2807SJeff Garzik {
3977c6fd2807SJeff Garzik 	unsigned int class = dev->class;
39789af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3979c6fd2807SJeff Garzik 	int rc;
3980c6fd2807SJeff Garzik 
3981c6fd2807SJeff Garzik 	/* read ID data */
3982bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3983c6fd2807SJeff Garzik 	if (rc)
3984fe30911bSTejun Heo 		return rc;
3985c6fd2807SJeff Garzik 
3986c6fd2807SJeff Garzik 	/* is the device still there? */
3987fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3988fe30911bSTejun Heo 		return -ENODEV;
3989c6fd2807SJeff Garzik 
3990c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3991fe30911bSTejun Heo 	return 0;
3992fe30911bSTejun Heo }
3993fe30911bSTejun Heo 
3994fe30911bSTejun Heo /**
3995fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3996fe30911bSTejun Heo  *	@dev: device to revalidate
3997422c9daaSTejun Heo  *	@new_class: new class code
3998fe30911bSTejun Heo  *	@readid_flags: read ID flags
3999fe30911bSTejun Heo  *
4000fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
4001fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
4002fe30911bSTejun Heo  *
4003fe30911bSTejun Heo  *	LOCKING:
4004fe30911bSTejun Heo  *	Kernel thread context (may sleep)
4005fe30911bSTejun Heo  *
4006fe30911bSTejun Heo  *	RETURNS:
4007fe30911bSTejun Heo  *	0 on success, negative errno otherwise
4008fe30911bSTejun Heo  */
4009422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4010422c9daaSTejun Heo 		       unsigned int readid_flags)
4011fe30911bSTejun Heo {
40126ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
4013fe30911bSTejun Heo 	int rc;
4014fe30911bSTejun Heo 
4015fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
4016fe30911bSTejun Heo 		return -ENODEV;
4017fe30911bSTejun Heo 
4018422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4019422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
4020422c9daaSTejun Heo 	    new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
4021422c9daaSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
4022422c9daaSTejun Heo 			       dev->class, new_class);
4023422c9daaSTejun Heo 		rc = -ENODEV;
4024422c9daaSTejun Heo 		goto fail;
4025422c9daaSTejun Heo 	}
4026422c9daaSTejun Heo 
4027fe30911bSTejun Heo 	/* re-read ID */
4028fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
4029fe30911bSTejun Heo 	if (rc)
4030fe30911bSTejun Heo 		goto fail;
4031c6fd2807SJeff Garzik 
4032c6fd2807SJeff Garzik 	/* configure device according to the new ID */
4033efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
40346ddcd3b0STejun Heo 	if (rc)
40356ddcd3b0STejun Heo 		goto fail;
40366ddcd3b0STejun Heo 
40376ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
4038b54eebd6STejun Heo 	if (dev->class == ATA_DEV_ATA && n_sectors &&
4039b54eebd6STejun Heo 	    dev->n_sectors != n_sectors) {
40406ddcd3b0STejun Heo 		ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
40416ddcd3b0STejun Heo 			       "%llu != %llu\n",
40426ddcd3b0STejun Heo 			       (unsigned long long)n_sectors,
40436ddcd3b0STejun Heo 			       (unsigned long long)dev->n_sectors);
40448270bec4STejun Heo 
40458270bec4STejun Heo 		/* restore original n_sectors */
40468270bec4STejun Heo 		dev->n_sectors = n_sectors;
40478270bec4STejun Heo 
40486ddcd3b0STejun Heo 		rc = -ENODEV;
40496ddcd3b0STejun Heo 		goto fail;
40506ddcd3b0STejun Heo 	}
40516ddcd3b0STejun Heo 
4052c6fd2807SJeff Garzik 	return 0;
4053c6fd2807SJeff Garzik 
4054c6fd2807SJeff Garzik  fail:
4055c6fd2807SJeff Garzik 	ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
4056c6fd2807SJeff Garzik 	return rc;
4057c6fd2807SJeff Garzik }
4058c6fd2807SJeff Garzik 
40596919a0a6SAlan Cox struct ata_blacklist_entry {
40606919a0a6SAlan Cox 	const char *model_num;
40616919a0a6SAlan Cox 	const char *model_rev;
40626919a0a6SAlan Cox 	unsigned long horkage;
40636919a0a6SAlan Cox };
40646919a0a6SAlan Cox 
40656919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
40666919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
40676919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
40686919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
40696919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
40706919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
40716919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
40726919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
40736919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
40746919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
40756919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
40766919a0a6SAlan Cox 	{ "CRD-8480B",		NULL,		ATA_HORKAGE_NODMA },
40776919a0a6SAlan Cox 	{ "CRD-8482B",		NULL,		ATA_HORKAGE_NODMA },
40786919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
40796919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
40806919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
40816919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
40826919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
40836919a0a6SAlan Cox 	{ "HITACHI CDR-8335",	NULL,		ATA_HORKAGE_NODMA },
40846919a0a6SAlan Cox 	{ "HITACHI CDR-8435",	NULL,		ATA_HORKAGE_NODMA },
40856919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
40866919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
40876919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
40886919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
40896919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
40906919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
40916919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
40926919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
40936919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
40946919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
40956919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
409639f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
40973af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
409850af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
40996919a0a6SAlan Cox 
410018d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
410140a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
41026a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
410318d6e9d5SAlbert Lee 
41046919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
41056919a0a6SAlan Cox 
41066919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
41076919a0a6SAlan Cox 	/* NCQ is slow */
41086919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4109459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
411009125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
411109125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
41127acfaf30SPaul Rolland 	/* NCQ is broken */
4113539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
41140e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4115da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4116e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
41175ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4118539cc7c7SJeff Garzik 
4119ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
4120d10d491fSTejun Heo 	{ "ST31500341AS",	"SD15",		ATA_HORKAGE_NONCQ |
4121ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4122d10d491fSTejun Heo 	{ "ST31500341AS",	"SD16",		ATA_HORKAGE_NONCQ |
4123ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4124d10d491fSTejun Heo 	{ "ST31500341AS",	"SD17",		ATA_HORKAGE_NONCQ |
4125ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4126d10d491fSTejun Heo 	{ "ST31500341AS",	"SD18",		ATA_HORKAGE_NONCQ |
4127ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4128d10d491fSTejun Heo 	{ "ST31500341AS",	"SD19",		ATA_HORKAGE_NONCQ |
4129ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4130d10d491fSTejun Heo 
4131d10d491fSTejun Heo 	{ "ST31000333AS",	"SD15",		ATA_HORKAGE_NONCQ |
4132d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4133d10d491fSTejun Heo 	{ "ST31000333AS",	"SD16",		ATA_HORKAGE_NONCQ |
4134d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4135d10d491fSTejun Heo 	{ "ST31000333AS",	"SD17",		ATA_HORKAGE_NONCQ |
4136d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4137d10d491fSTejun Heo 	{ "ST31000333AS",	"SD18",		ATA_HORKAGE_NONCQ |
4138d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4139d10d491fSTejun Heo 	{ "ST31000333AS",	"SD19",		ATA_HORKAGE_NONCQ |
4140d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4141d10d491fSTejun Heo 
4142d10d491fSTejun Heo 	{ "ST3640623AS",	"SD15",		ATA_HORKAGE_NONCQ |
4143d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4144d10d491fSTejun Heo 	{ "ST3640623AS",	"SD16",		ATA_HORKAGE_NONCQ |
4145d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4146d10d491fSTejun Heo 	{ "ST3640623AS",	"SD17",		ATA_HORKAGE_NONCQ |
4147d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4148d10d491fSTejun Heo 	{ "ST3640623AS",	"SD18",		ATA_HORKAGE_NONCQ |
4149d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4150d10d491fSTejun Heo 	{ "ST3640623AS",	"SD19",		ATA_HORKAGE_NONCQ |
4151d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4152d10d491fSTejun Heo 
4153d10d491fSTejun Heo 	{ "ST3640323AS",	"SD15",		ATA_HORKAGE_NONCQ |
4154d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4155d10d491fSTejun Heo 	{ "ST3640323AS",	"SD16",		ATA_HORKAGE_NONCQ |
4156d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4157d10d491fSTejun Heo 	{ "ST3640323AS",	"SD17",		ATA_HORKAGE_NONCQ |
4158d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4159d10d491fSTejun Heo 	{ "ST3640323AS",	"SD18",		ATA_HORKAGE_NONCQ |
4160d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4161d10d491fSTejun Heo 	{ "ST3640323AS",	"SD19",		ATA_HORKAGE_NONCQ |
4162d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4163d10d491fSTejun Heo 
4164d10d491fSTejun Heo 	{ "ST3320813AS",	"SD15",		ATA_HORKAGE_NONCQ |
4165d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4166d10d491fSTejun Heo 	{ "ST3320813AS",	"SD16",		ATA_HORKAGE_NONCQ |
4167d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4168d10d491fSTejun Heo 	{ "ST3320813AS",	"SD17",		ATA_HORKAGE_NONCQ |
4169d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4170d10d491fSTejun Heo 	{ "ST3320813AS",	"SD18",		ATA_HORKAGE_NONCQ |
4171d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4172d10d491fSTejun Heo 	{ "ST3320813AS",	"SD19",		ATA_HORKAGE_NONCQ |
4173d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4174d10d491fSTejun Heo 
4175d10d491fSTejun Heo 	{ "ST3320613AS",	"SD15",		ATA_HORKAGE_NONCQ |
4176d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4177d10d491fSTejun Heo 	{ "ST3320613AS",	"SD16",		ATA_HORKAGE_NONCQ |
4178d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4179d10d491fSTejun Heo 	{ "ST3320613AS",	"SD17",		ATA_HORKAGE_NONCQ |
4180d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4181d10d491fSTejun Heo 	{ "ST3320613AS",	"SD18",		ATA_HORKAGE_NONCQ |
4182d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4183d10d491fSTejun Heo 	{ "ST3320613AS",	"SD19",		ATA_HORKAGE_NONCQ |
4184ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4185ac70a964STejun Heo 
418636e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
418736e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
418836e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
418936e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
419036e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
41916919a0a6SAlan Cox 
419216c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
419316c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
419416c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
419516c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
419616c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
41976919a0a6SAlan Cox 
419893328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
419993328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
420093328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4201b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
420293328e11SAlan Cox 
42036bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
42046bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4205a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
4206a79067e5SAlan Cox 	{ "TSSTcorp CDDVDW SH-S202H", "SB00",	  ATA_HORKAGE_IVB, },
4207a79067e5SAlan Cox 	{ "TSSTcorp CDDVDW SH-S202H", "SB01",	  ATA_HORKAGE_IVB, },
42086bbfd53dSAlan Cox 	{ "TSSTcorp CDDVDW SH-S202J", "SB00",	  ATA_HORKAGE_IVB, },
4209e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202J", "SB01",	  ATA_HORKAGE_IVB, },
4210e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202N", "SB00",	  ATA_HORKAGE_IVB, },
4211e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202N", "SB01",	  ATA_HORKAGE_IVB, },
42126bbfd53dSAlan Cox 
42139ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
42149ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
42159ce8e307SJens Axboe 
42166919a0a6SAlan Cox 	/* End Marker */
42176919a0a6SAlan Cox 	{ }
4218c6fd2807SJeff Garzik };
4219c6fd2807SJeff Garzik 
4220741b7763SAdrian Bunk static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
4221539cc7c7SJeff Garzik {
4222539cc7c7SJeff Garzik 	const char *p;
4223539cc7c7SJeff Garzik 	int len;
4224539cc7c7SJeff Garzik 
4225539cc7c7SJeff Garzik 	/*
4226539cc7c7SJeff Garzik 	 * check for trailing wildcard: *\0
4227539cc7c7SJeff Garzik 	 */
4228539cc7c7SJeff Garzik 	p = strchr(patt, wildchar);
4229539cc7c7SJeff Garzik 	if (p && ((*(p + 1)) == 0))
4230539cc7c7SJeff Garzik 		len = p - patt;
4231317b50b8SAndrew Paprocki 	else {
4232539cc7c7SJeff Garzik 		len = strlen(name);
4233317b50b8SAndrew Paprocki 		if (!len) {
4234317b50b8SAndrew Paprocki 			if (!*patt)
4235317b50b8SAndrew Paprocki 				return 0;
4236317b50b8SAndrew Paprocki 			return -1;
4237317b50b8SAndrew Paprocki 		}
4238317b50b8SAndrew Paprocki 	}
4239539cc7c7SJeff Garzik 
4240539cc7c7SJeff Garzik 	return strncmp(patt, name, len);
4241539cc7c7SJeff Garzik }
4242539cc7c7SJeff Garzik 
424375683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4244c6fd2807SJeff Garzik {
42458bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
42468bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
42476919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4248c6fd2807SJeff Garzik 
42498bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
42508bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4251c6fd2807SJeff Garzik 
42526919a0a6SAlan Cox 	while (ad->model_num) {
4253539cc7c7SJeff Garzik 		if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
42546919a0a6SAlan Cox 			if (ad->model_rev == NULL)
42556919a0a6SAlan Cox 				return ad->horkage;
4256539cc7c7SJeff Garzik 			if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
42576919a0a6SAlan Cox 				return ad->horkage;
4258c6fd2807SJeff Garzik 		}
42596919a0a6SAlan Cox 		ad++;
4260c6fd2807SJeff Garzik 	}
4261c6fd2807SJeff Garzik 	return 0;
4262c6fd2807SJeff Garzik }
4263c6fd2807SJeff Garzik 
42646919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
42656919a0a6SAlan Cox {
42666919a0a6SAlan Cox 	/* We don't support polling DMA.
42676919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
42686919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
42696919a0a6SAlan Cox 	 */
42709af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
42716919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
42726919a0a6SAlan Cox 		return 1;
427375683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
42746919a0a6SAlan Cox }
42756919a0a6SAlan Cox 
4276c6fd2807SJeff Garzik /**
42776bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
42786bbfd53dSAlan Cox  *	@dev: device
42796bbfd53dSAlan Cox  *
42806bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
42816bbfd53dSAlan Cox  *	who can't follow the documentation.
42826bbfd53dSAlan Cox  */
42836bbfd53dSAlan Cox 
42846bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
42856bbfd53dSAlan Cox {
42866bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
42876bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
42886bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
42896bbfd53dSAlan Cox }
42906bbfd53dSAlan Cox 
42916bbfd53dSAlan Cox /**
429215a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
429315a5551cSAlan Cox  *	@ap: port to consider
429415a5551cSAlan Cox  *
429515a5551cSAlan Cox  *	This function encapsulates the policy for speed management
429615a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
429715a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
429815a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
429915a5551cSAlan Cox  *	impacts hotplug at all).
430015a5551cSAlan Cox  *
430115a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
430215a5551cSAlan Cox  */
430315a5551cSAlan Cox 
430415a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
430515a5551cSAlan Cox {
430615a5551cSAlan Cox 	struct ata_link *link;
430715a5551cSAlan Cox 	struct ata_device *dev;
430815a5551cSAlan Cox 
43094a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
431015a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
431115a5551cSAlan Cox 		return 1;
43124a9c7b33STejun Heo 
43134a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
431415a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
431515a5551cSAlan Cox 		return 0;
43164a9c7b33STejun Heo 
43174a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
43184a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
43194a9c7b33STejun Heo 	 * isn't sure.
43204a9c7b33STejun Heo 	 */
4321f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4322f792068eSAlan Cox 		return 0;
432315a5551cSAlan Cox 
43244a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
43254a9c7b33STejun Heo 	 *
43264a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
43274a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
43284a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
43294a9c7b33STejun Heo 	 *   give a valid detect
43304a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
43314a9c7b33STejun Heo 	 *   to colour the choice
433215a5551cSAlan Cox 	 */
43331eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
43341eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
43351eca4365STejun Heo 			if (!ata_is_40wire(dev))
433615a5551cSAlan Cox 				return 0;
433715a5551cSAlan Cox 		}
433815a5551cSAlan Cox 	}
433915a5551cSAlan Cox 	return 1;
434015a5551cSAlan Cox }
434115a5551cSAlan Cox 
434215a5551cSAlan Cox /**
4343c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4344c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4345c6fd2807SJeff Garzik  *
4346c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4347c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4348c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4349c6fd2807SJeff Garzik  *	blacklist, etc...
4350c6fd2807SJeff Garzik  *
4351c6fd2807SJeff Garzik  *	LOCKING:
4352c6fd2807SJeff Garzik  *	None.
4353c6fd2807SJeff Garzik  */
4354c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4355c6fd2807SJeff Garzik {
43569af5c9c9STejun Heo 	struct ata_link *link = dev->link;
43579af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4358cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4359c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4360c6fd2807SJeff Garzik 
4361c6fd2807SJeff Garzik 	/* controller modes available */
4362c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4363c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4364c6fd2807SJeff Garzik 
43658343f889SRobert Hancock 	/* drive modes available */
4366c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4367c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4368c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4369c6fd2807SJeff Garzik 
4370b352e57dSAlan Cox 	/*
4371b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4372b352e57dSAlan Cox 	 *	cable
4373b352e57dSAlan Cox 	 */
4374b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4375b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4376b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4377b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4378b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4379b352e57dSAlan Cox 	}
4380b352e57dSAlan Cox 
4381c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4382c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4383c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
4384c6fd2807SJeff Garzik 			       "device is on DMA blacklist, disabling DMA\n");
4385c6fd2807SJeff Garzik 	}
4386c6fd2807SJeff Garzik 
438714d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
438814d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4389c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4390c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4391c6fd2807SJeff Garzik 			       "other device, disabling DMA\n");
4392c6fd2807SJeff Garzik 	}
4393c6fd2807SJeff Garzik 
4394e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4395e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4396e424675fSJeff Garzik 
4397c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4398a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4399c6fd2807SJeff Garzik 
44008343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
44018343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
44028343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
44038343f889SRobert Hancock 	 * solely limited by the cable.
44048343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
44058343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
44068343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
44078343f889SRobert Hancock 	 */
44088343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
44098343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
441015a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
44118343f889SRobert Hancock 			ata_dev_printk(dev, KERN_WARNING,
44128343f889SRobert Hancock 				 "limited to UDMA/33 due to 40-wire cable\n");
44138343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
44148343f889SRobert Hancock 		}
44158343f889SRobert Hancock 
4416c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4417c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4418c6fd2807SJeff Garzik }
4419c6fd2807SJeff Garzik 
4420c6fd2807SJeff Garzik /**
4421c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4422c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4423c6fd2807SJeff Garzik  *
4424c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4425c6fd2807SJeff Garzik  *	on port @ap.
4426c6fd2807SJeff Garzik  *
4427c6fd2807SJeff Garzik  *	LOCKING:
4428c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4429c6fd2807SJeff Garzik  *
4430c6fd2807SJeff Garzik  *	RETURNS:
4431c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4432c6fd2807SJeff Garzik  */
4433c6fd2807SJeff Garzik 
4434c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4435c6fd2807SJeff Garzik {
4436c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4437c6fd2807SJeff Garzik 	unsigned int err_mask;
4438c6fd2807SJeff Garzik 
4439c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4440c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4441c6fd2807SJeff Garzik 
4442464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4443464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4444464cf177STejun Heo 	 */
4445c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4446c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4447c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4448464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4449c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4450b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4451b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4452c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4453b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4454b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4455b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4456b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4457b9f8ab2dSAlan Cox 		return 0;
4458c6fd2807SJeff Garzik 
44592b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4460c6fd2807SJeff Garzik 
4461c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4462c6fd2807SJeff Garzik 	return err_mask;
4463c6fd2807SJeff Garzik }
4464c6fd2807SJeff Garzik /**
4465218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
44669f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
44679f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4468218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
44699f45cbd3SKristen Carlson Accardi  *
44709f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4471218f3d30SJeff Garzik  *	on port @ap with sector count
44729f45cbd3SKristen Carlson Accardi  *
44739f45cbd3SKristen Carlson Accardi  *	LOCKING:
44749f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
44759f45cbd3SKristen Carlson Accardi  *
44769f45cbd3SKristen Carlson Accardi  *	RETURNS:
44779f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
44789f45cbd3SKristen Carlson Accardi  */
4479218f3d30SJeff Garzik static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4480218f3d30SJeff Garzik 					u8 feature)
44819f45cbd3SKristen Carlson Accardi {
44829f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
44839f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
44849f45cbd3SKristen Carlson Accardi 
44859f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
44869f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
44879f45cbd3SKristen Carlson Accardi 
44889f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
44899f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
44909f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
44919f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
44929f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4493218f3d30SJeff Garzik 	tf.nsect = feature;
44949f45cbd3SKristen Carlson Accardi 
44952b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
44969f45cbd3SKristen Carlson Accardi 
44979f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
44989f45cbd3SKristen Carlson Accardi 	return err_mask;
44999f45cbd3SKristen Carlson Accardi }
45009f45cbd3SKristen Carlson Accardi 
45019f45cbd3SKristen Carlson Accardi /**
4502c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4503c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4504c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4505c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4506c6fd2807SJeff Garzik  *
4507c6fd2807SJeff Garzik  *	LOCKING:
4508c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4509c6fd2807SJeff Garzik  *
4510c6fd2807SJeff Garzik  *	RETURNS:
4511c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4512c6fd2807SJeff Garzik  */
4513c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4514c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4515c6fd2807SJeff Garzik {
4516c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4517c6fd2807SJeff Garzik 	unsigned int err_mask;
4518c6fd2807SJeff Garzik 
4519c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4520c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4521c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4522c6fd2807SJeff Garzik 
4523c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4524c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4525c6fd2807SJeff Garzik 
4526c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4527c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4528c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4529c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4530c6fd2807SJeff Garzik 	tf.nsect = sectors;
4531c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4532c6fd2807SJeff Garzik 
45332b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
453418b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
453518b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
453618b2466cSAlan Cox 	   drive reported working geometry */
453718b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
453818b2466cSAlan Cox 		err_mask = 0;
4539c6fd2807SJeff Garzik 
4540c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4541c6fd2807SJeff Garzik 	return err_mask;
4542c6fd2807SJeff Garzik }
4543c6fd2807SJeff Garzik 
4544c6fd2807SJeff Garzik /**
4545c6fd2807SJeff Garzik  *	ata_sg_clean - Unmap DMA memory associated with command
4546c6fd2807SJeff Garzik  *	@qc: Command containing DMA memory to be released
4547c6fd2807SJeff Garzik  *
4548c6fd2807SJeff Garzik  *	Unmap all mapped DMA memory associated with this command.
4549c6fd2807SJeff Garzik  *
4550c6fd2807SJeff Garzik  *	LOCKING:
4551cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4552c6fd2807SJeff Garzik  */
455370e6ad0cSTejun Heo void ata_sg_clean(struct ata_queued_cmd *qc)
4554c6fd2807SJeff Garzik {
4555c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4556ff2aeb1eSTejun Heo 	struct scatterlist *sg = qc->sg;
4557c6fd2807SJeff Garzik 	int dir = qc->dma_dir;
4558c6fd2807SJeff Garzik 
4559efcb3cf7STejun Heo 	WARN_ON_ONCE(sg == NULL);
4560c6fd2807SJeff Garzik 
4561dde20207SJames Bottomley 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4562c6fd2807SJeff Garzik 
4563dde20207SJames Bottomley 	if (qc->n_elem)
4564dde20207SJames Bottomley 		dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
4565c6fd2807SJeff Garzik 
4566c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
4567ff2aeb1eSTejun Heo 	qc->sg = NULL;
4568c6fd2807SJeff Garzik }
4569c6fd2807SJeff Garzik 
4570c6fd2807SJeff Garzik /**
45715895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4572c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4573c6fd2807SJeff Garzik  *
4574c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4575c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4576c6fd2807SJeff Garzik  *	supplied PACKET command.
4577c6fd2807SJeff Garzik  *
4578c6fd2807SJeff Garzik  *	LOCKING:
4579cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4580c6fd2807SJeff Garzik  *
4581c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4582c6fd2807SJeff Garzik  *               nonzero otherwise
4583c6fd2807SJeff Garzik  */
45845895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4585c6fd2807SJeff Garzik {
4586c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4587c6fd2807SJeff Garzik 
4588b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4589b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4590b9a4197eSTejun Heo 	 */
45916a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
45926a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
45936f23a31dSAlbert Lee 		return 1;
45946f23a31dSAlbert Lee 
4595c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4596b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4597c6fd2807SJeff Garzik 
4598b9a4197eSTejun Heo 	return 0;
4599c6fd2807SJeff Garzik }
4600b9a4197eSTejun Heo 
4601c6fd2807SJeff Garzik /**
460231cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
460331cc23b3STejun Heo  *	@qc: ATA command in question
460431cc23b3STejun Heo  *
460531cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
460631cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
460731cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
460831cc23b3STejun Heo  *	whether a new command @qc can be issued.
460931cc23b3STejun Heo  *
461031cc23b3STejun Heo  *	LOCKING:
461131cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
461231cc23b3STejun Heo  *
461331cc23b3STejun Heo  *	RETURNS:
461431cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
461531cc23b3STejun Heo  */
461631cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
461731cc23b3STejun Heo {
461831cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
461931cc23b3STejun Heo 
462031cc23b3STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
462131cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
462231cc23b3STejun Heo 			return 0;
462331cc23b3STejun Heo 	} else {
462431cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
462531cc23b3STejun Heo 			return 0;
462631cc23b3STejun Heo 	}
462731cc23b3STejun Heo 
462831cc23b3STejun Heo 	return ATA_DEFER_LINK;
462931cc23b3STejun Heo }
463031cc23b3STejun Heo 
4631c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4632c6fd2807SJeff Garzik 
4633c6fd2807SJeff Garzik /**
4634c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4635c6fd2807SJeff Garzik  *	@qc: Command to be associated
4636c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4637c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4638c6fd2807SJeff Garzik  *
4639c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4640c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4641c6fd2807SJeff Garzik  *	elements.
4642c6fd2807SJeff Garzik  *
4643c6fd2807SJeff Garzik  *	LOCKING:
4644cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4645c6fd2807SJeff Garzik  */
4646c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4647c6fd2807SJeff Garzik 		 unsigned int n_elem)
4648c6fd2807SJeff Garzik {
4649ff2aeb1eSTejun Heo 	qc->sg = sg;
4650c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4651ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4652ff2aeb1eSTejun Heo }
4653ff2aeb1eSTejun Heo 
4654c6fd2807SJeff Garzik /**
4655c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4656c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4657c6fd2807SJeff Garzik  *
4658c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4659c6fd2807SJeff Garzik  *
4660c6fd2807SJeff Garzik  *	LOCKING:
4661cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4662c6fd2807SJeff Garzik  *
4663c6fd2807SJeff Garzik  *	RETURNS:
4664c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4665c6fd2807SJeff Garzik  *
4666c6fd2807SJeff Garzik  */
4667c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4668c6fd2807SJeff Garzik {
4669c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4670dde20207SJames Bottomley 	unsigned int n_elem;
4671c6fd2807SJeff Garzik 
467244877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4673c6fd2807SJeff Garzik 
4674dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4675dde20207SJames Bottomley 	if (n_elem < 1)
4676c6fd2807SJeff Garzik 		return -1;
4677c6fd2807SJeff Garzik 
4678dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
4679dde20207SJames Bottomley 
4680dde20207SJames Bottomley 	qc->n_elem = n_elem;
4681f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4682c6fd2807SJeff Garzik 
4683c6fd2807SJeff Garzik 	return 0;
4684c6fd2807SJeff Garzik }
4685c6fd2807SJeff Garzik 
4686c6fd2807SJeff Garzik /**
4687c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4688c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4689c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4690c6fd2807SJeff Garzik  *
4691c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4692c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4693c6fd2807SJeff Garzik  *	vice-versa.
4694c6fd2807SJeff Garzik  *
4695c6fd2807SJeff Garzik  *	LOCKING:
4696c6fd2807SJeff Garzik  *	Inherited from caller.
4697c6fd2807SJeff Garzik  */
4698c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4699c6fd2807SJeff Garzik {
4700c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4701c6fd2807SJeff Garzik 	unsigned int i;
4702c6fd2807SJeff Garzik 
4703c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4704c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4705c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4706c6fd2807SJeff Garzik }
4707c6fd2807SJeff Garzik 
4708c6fd2807SJeff Garzik /**
47098a8bc223STejun Heo  *	ata_qc_new - Request an available ATA command, for queueing
47108a8bc223STejun Heo  *	@ap: Port associated with device @dev
47118a8bc223STejun Heo  *	@dev: Device from whom we request an available command structure
47128a8bc223STejun Heo  *
47138a8bc223STejun Heo  *	LOCKING:
47148a8bc223STejun Heo  *	None.
47158a8bc223STejun Heo  */
47168a8bc223STejun Heo 
47178a8bc223STejun Heo static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
47188a8bc223STejun Heo {
47198a8bc223STejun Heo 	struct ata_queued_cmd *qc = NULL;
47208a8bc223STejun Heo 	unsigned int i;
47218a8bc223STejun Heo 
47228a8bc223STejun Heo 	/* no command while frozen */
47238a8bc223STejun Heo 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
47248a8bc223STejun Heo 		return NULL;
47258a8bc223STejun Heo 
47268a8bc223STejun Heo 	/* the last tag is reserved for internal command. */
47278a8bc223STejun Heo 	for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
47288a8bc223STejun Heo 		if (!test_and_set_bit(i, &ap->qc_allocated)) {
47298a8bc223STejun Heo 			qc = __ata_qc_from_tag(ap, i);
47308a8bc223STejun Heo 			break;
47318a8bc223STejun Heo 		}
47328a8bc223STejun Heo 
47338a8bc223STejun Heo 	if (qc)
47348a8bc223STejun Heo 		qc->tag = i;
47358a8bc223STejun Heo 
47368a8bc223STejun Heo 	return qc;
47378a8bc223STejun Heo }
47388a8bc223STejun Heo 
47398a8bc223STejun Heo /**
4740c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4741c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
4742c6fd2807SJeff Garzik  *
4743c6fd2807SJeff Garzik  *	LOCKING:
4744c6fd2807SJeff Garzik  *	None.
4745c6fd2807SJeff Garzik  */
4746c6fd2807SJeff Garzik 
47478a8bc223STejun Heo struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4748c6fd2807SJeff Garzik {
47499af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4750c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4751c6fd2807SJeff Garzik 
47528a8bc223STejun Heo 	qc = ata_qc_new(ap);
4753c6fd2807SJeff Garzik 	if (qc) {
4754c6fd2807SJeff Garzik 		qc->scsicmd = NULL;
4755c6fd2807SJeff Garzik 		qc->ap = ap;
4756c6fd2807SJeff Garzik 		qc->dev = dev;
4757c6fd2807SJeff Garzik 
4758c6fd2807SJeff Garzik 		ata_qc_reinit(qc);
4759c6fd2807SJeff Garzik 	}
4760c6fd2807SJeff Garzik 
4761c6fd2807SJeff Garzik 	return qc;
4762c6fd2807SJeff Garzik }
4763c6fd2807SJeff Garzik 
47648a8bc223STejun Heo /**
47658a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
47668a8bc223STejun Heo  *	@qc: Command to complete
47678a8bc223STejun Heo  *
47688a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
47698a8bc223STejun Heo  *	in case something prevents using it.
47708a8bc223STejun Heo  *
47718a8bc223STejun Heo  *	LOCKING:
47728a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
47738a8bc223STejun Heo  */
47748a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
47758a8bc223STejun Heo {
47768a8bc223STejun Heo 	struct ata_port *ap = qc->ap;
47778a8bc223STejun Heo 	unsigned int tag;
47788a8bc223STejun Heo 
4779efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
47808a8bc223STejun Heo 
47818a8bc223STejun Heo 	qc->flags = 0;
47828a8bc223STejun Heo 	tag = qc->tag;
47838a8bc223STejun Heo 	if (likely(ata_tag_valid(tag))) {
47848a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
47858a8bc223STejun Heo 		clear_bit(tag, &ap->qc_allocated);
47868a8bc223STejun Heo 	}
47878a8bc223STejun Heo }
47888a8bc223STejun Heo 
4789c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4790c6fd2807SJeff Garzik {
4791c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
47929af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4793c6fd2807SJeff Garzik 
4794efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4795efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4796c6fd2807SJeff Garzik 
4797c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4798c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4799c6fd2807SJeff Garzik 
4800c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4801da917d69STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
48029af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
4803da917d69STejun Heo 		if (!link->sactive)
4804da917d69STejun Heo 			ap->nr_active_links--;
4805da917d69STejun Heo 	} else {
48069af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4807da917d69STejun Heo 		ap->nr_active_links--;
4808da917d69STejun Heo 	}
4809da917d69STejun Heo 
4810da917d69STejun Heo 	/* clear exclusive status */
4811da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4812da917d69STejun Heo 		     ap->excl_link == link))
4813da917d69STejun Heo 		ap->excl_link = NULL;
4814c6fd2807SJeff Garzik 
4815c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4816c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4817c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4818c6fd2807SJeff Garzik 	 */
4819c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4820c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
4821c6fd2807SJeff Garzik 
4822c6fd2807SJeff Garzik 	/* call completion callback */
4823c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4824c6fd2807SJeff Garzik }
4825c6fd2807SJeff Garzik 
482639599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
482739599a53STejun Heo {
482839599a53STejun Heo 	struct ata_port *ap = qc->ap;
482939599a53STejun Heo 
483039599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
483122183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
483239599a53STejun Heo }
483339599a53STejun Heo 
483400115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
483500115e0fSTejun Heo {
483600115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
483700115e0fSTejun Heo 
483800115e0fSTejun Heo 	if (ata_tag_internal(qc->tag))
483900115e0fSTejun Heo 		return;
484000115e0fSTejun Heo 
484100115e0fSTejun Heo 	if (ata_is_nodata(qc->tf.protocol))
484200115e0fSTejun Heo 		return;
484300115e0fSTejun Heo 
484400115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
484500115e0fSTejun Heo 		return;
484600115e0fSTejun Heo 
484700115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
484800115e0fSTejun Heo }
484900115e0fSTejun Heo 
4850c6fd2807SJeff Garzik /**
4851c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4852c6fd2807SJeff Garzik  *	@qc: Command to complete
4853c6fd2807SJeff Garzik  *
4854c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA
4855c6fd2807SJeff Garzik  *	command has completed, with either an ok or not-ok status.
4856c6fd2807SJeff Garzik  *
4857c6fd2807SJeff Garzik  *	LOCKING:
4858cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4859c6fd2807SJeff Garzik  */
4860c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4861c6fd2807SJeff Garzik {
4862c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4863c6fd2807SJeff Garzik 
4864c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4865c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4866c6fd2807SJeff Garzik 	 *
4867c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4868c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4869c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4870c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4871c6fd2807SJeff Garzik 	 *
4872c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4873c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4874c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4875c6fd2807SJeff Garzik 	 * taken care of.
4876c6fd2807SJeff Garzik 	 */
4877c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
48784dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
48794dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
48804dbfa39bSTejun Heo 
4881efcb3cf7STejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
4882c6fd2807SJeff Garzik 
4883c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4884c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4885c6fd2807SJeff Garzik 
4886c6fd2807SJeff Garzik 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4887c6fd2807SJeff Garzik 			if (!ata_tag_internal(qc->tag)) {
4888c6fd2807SJeff Garzik 				/* always fill result TF for failed qc */
488939599a53STejun Heo 				fill_result_tf(qc);
4890c6fd2807SJeff Garzik 				ata_qc_schedule_eh(qc);
4891c6fd2807SJeff Garzik 				return;
4892c6fd2807SJeff Garzik 			}
4893c6fd2807SJeff Garzik 		}
4894c6fd2807SJeff Garzik 
4895c6fd2807SJeff Garzik 		/* read result TF if requested */
4896c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
489739599a53STejun Heo 			fill_result_tf(qc);
4898c6fd2807SJeff Garzik 
48994dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
49004dbfa39bSTejun Heo 		 * completion.
49014dbfa39bSTejun Heo 		 */
49024dbfa39bSTejun Heo 		switch (qc->tf.command) {
49034dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
49044dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
49054dbfa39bSTejun Heo 			    qc->tf.feature != SETFEATURES_WC_OFF)
49064dbfa39bSTejun Heo 				break;
49074dbfa39bSTejun Heo 			/* fall through */
49084dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
49094dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
49104dbfa39bSTejun Heo 			/* revalidate device */
49114dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
49124dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
49134dbfa39bSTejun Heo 			break;
4914054a5fbaSTejun Heo 
4915054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4916054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4917054a5fbaSTejun Heo 			break;
49184dbfa39bSTejun Heo 		}
49194dbfa39bSTejun Heo 
492000115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
492100115e0fSTejun Heo 			ata_verify_xfer(qc);
492200115e0fSTejun Heo 
4923c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4924c6fd2807SJeff Garzik 	} else {
4925c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4926c6fd2807SJeff Garzik 			return;
4927c6fd2807SJeff Garzik 
4928c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4929c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
493039599a53STejun Heo 			fill_result_tf(qc);
4931c6fd2807SJeff Garzik 
4932c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4933c6fd2807SJeff Garzik 	}
4934c6fd2807SJeff Garzik }
4935c6fd2807SJeff Garzik 
4936c6fd2807SJeff Garzik /**
4937c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
4938c6fd2807SJeff Garzik  *	@ap: port in question
4939c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
4940c6fd2807SJeff Garzik  *
4941c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
4942c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
4943c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
4944c6fd2807SJeff Garzik  *	and commands are completed accordingly.
4945c6fd2807SJeff Garzik  *
4946c6fd2807SJeff Garzik  *	LOCKING:
4947cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4948c6fd2807SJeff Garzik  *
4949c6fd2807SJeff Garzik  *	RETURNS:
4950c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
4951c6fd2807SJeff Garzik  */
495279f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
4953c6fd2807SJeff Garzik {
4954c6fd2807SJeff Garzik 	int nr_done = 0;
4955c6fd2807SJeff Garzik 	u32 done_mask;
4956c6fd2807SJeff Garzik 	int i;
4957c6fd2807SJeff Garzik 
4958c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
4959c6fd2807SJeff Garzik 
4960c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
4961c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
4962c6fd2807SJeff Garzik 				"(%08x->%08x)\n", ap->qc_active, qc_active);
4963c6fd2807SJeff Garzik 		return -EINVAL;
4964c6fd2807SJeff Garzik 	}
4965c6fd2807SJeff Garzik 
4966c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_QUEUE; i++) {
4967c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
4968c6fd2807SJeff Garzik 
4969c6fd2807SJeff Garzik 		if (!(done_mask & (1 << i)))
4970c6fd2807SJeff Garzik 			continue;
4971c6fd2807SJeff Garzik 
4972c6fd2807SJeff Garzik 		if ((qc = ata_qc_from_tag(ap, i))) {
4973c6fd2807SJeff Garzik 			ata_qc_complete(qc);
4974c6fd2807SJeff Garzik 			nr_done++;
4975c6fd2807SJeff Garzik 		}
4976c6fd2807SJeff Garzik 	}
4977c6fd2807SJeff Garzik 
4978c6fd2807SJeff Garzik 	return nr_done;
4979c6fd2807SJeff Garzik }
4980c6fd2807SJeff Garzik 
4981c6fd2807SJeff Garzik /**
4982c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4983c6fd2807SJeff Garzik  *	@qc: command to issue to device
4984c6fd2807SJeff Garzik  *
4985c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4986c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4987c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
4988c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
4989c6fd2807SJeff Garzik  *
4990c6fd2807SJeff Garzik  *	LOCKING:
4991cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4992c6fd2807SJeff Garzik  */
4993c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
4994c6fd2807SJeff Garzik {
4995c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
49969af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4997405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
4998c6fd2807SJeff Garzik 
4999c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
5000c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
5001c6fd2807SJeff Garzik 	 * request ATAPI sense.
5002c6fd2807SJeff Garzik 	 */
5003efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5004c6fd2807SJeff Garzik 
50051973a023STejun Heo 	if (ata_is_ncq(prot)) {
5006efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive & (1 << qc->tag));
5007da917d69STejun Heo 
5008da917d69STejun Heo 		if (!link->sactive)
5009da917d69STejun Heo 			ap->nr_active_links++;
50109af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
5011c6fd2807SJeff Garzik 	} else {
5012efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
5013da917d69STejun Heo 
5014da917d69STejun Heo 		ap->nr_active_links++;
50159af5c9c9STejun Heo 		link->active_tag = qc->tag;
5016c6fd2807SJeff Garzik 	}
5017c6fd2807SJeff Garzik 
5018c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
5019c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
5020c6fd2807SJeff Garzik 
5021f92a2636STejun Heo 	/* We guarantee to LLDs that they will have at least one
5022f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5023f92a2636STejun Heo 	 */
5024ff2aeb1eSTejun Heo 	BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
5025f92a2636STejun Heo 
5026405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5027f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5028c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
5029c6fd2807SJeff Garzik 			goto sg_err;
5030c6fd2807SJeff Garzik 
5031cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5032054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5033cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5034054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5035054a5fbaSTejun Heo 		ata_link_abort(link);
5036054a5fbaSTejun Heo 		return;
5037054a5fbaSTejun Heo 	}
5038054a5fbaSTejun Heo 
5039c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5040c6fd2807SJeff Garzik 
5041c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5042c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5043c6fd2807SJeff Garzik 		goto err;
5044c6fd2807SJeff Garzik 	return;
5045c6fd2807SJeff Garzik 
5046c6fd2807SJeff Garzik sg_err:
5047c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5048c6fd2807SJeff Garzik err:
5049c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5050c6fd2807SJeff Garzik }
5051c6fd2807SJeff Garzik 
5052c6fd2807SJeff Garzik /**
5053c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5054936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5055c6fd2807SJeff Garzik  *
5056936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5057c6fd2807SJeff Garzik  *
5058c6fd2807SJeff Garzik  *	LOCKING:
5059c6fd2807SJeff Garzik  *	None.
5060c6fd2807SJeff Garzik  *
5061c6fd2807SJeff Garzik  *	RETURNS:
5062c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5063c6fd2807SJeff Garzik  */
5064936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5065c6fd2807SJeff Garzik {
5066936fd732STejun Heo 	struct ata_port *ap = link->ap;
5067936fd732STejun Heo 
5068a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5069c6fd2807SJeff Garzik }
5070c6fd2807SJeff Garzik 
5071c6fd2807SJeff Garzik /**
5072c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5073936fd732STejun Heo  *	@link: ATA link to read SCR for
5074c6fd2807SJeff Garzik  *	@reg: SCR to read
5075c6fd2807SJeff Garzik  *	@val: Place to store read value
5076c6fd2807SJeff Garzik  *
5077936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5078633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5079633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5080c6fd2807SJeff Garzik  *
5081c6fd2807SJeff Garzik  *	LOCKING:
5082633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5083c6fd2807SJeff Garzik  *
5084c6fd2807SJeff Garzik  *	RETURNS:
5085c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5086c6fd2807SJeff Garzik  */
5087936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5088c6fd2807SJeff Garzik {
5089633273a3STejun Heo 	if (ata_is_host_link(link)) {
5090936fd732STejun Heo 		if (sata_scr_valid(link))
509182ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5092c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5093c6fd2807SJeff Garzik 	}
5094c6fd2807SJeff Garzik 
5095633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5096633273a3STejun Heo }
5097633273a3STejun Heo 
5098c6fd2807SJeff Garzik /**
5099c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5100936fd732STejun Heo  *	@link: ATA link to write SCR for
5101c6fd2807SJeff Garzik  *	@reg: SCR to write
5102c6fd2807SJeff Garzik  *	@val: value to write
5103c6fd2807SJeff Garzik  *
5104936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5105633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5106633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5107c6fd2807SJeff Garzik  *
5108c6fd2807SJeff Garzik  *	LOCKING:
5109633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5110c6fd2807SJeff Garzik  *
5111c6fd2807SJeff Garzik  *	RETURNS:
5112c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5113c6fd2807SJeff Garzik  */
5114936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5115c6fd2807SJeff Garzik {
5116633273a3STejun Heo 	if (ata_is_host_link(link)) {
5117936fd732STejun Heo 		if (sata_scr_valid(link))
511882ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5119c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5120c6fd2807SJeff Garzik 	}
5121c6fd2807SJeff Garzik 
5122633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5123633273a3STejun Heo }
5124633273a3STejun Heo 
5125c6fd2807SJeff Garzik /**
5126c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5127936fd732STejun Heo  *	@link: ATA link to write SCR for
5128c6fd2807SJeff Garzik  *	@reg: SCR to write
5129c6fd2807SJeff Garzik  *	@val: value to write
5130c6fd2807SJeff Garzik  *
5131c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5132c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5133c6fd2807SJeff Garzik  *
5134c6fd2807SJeff Garzik  *	LOCKING:
5135633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5136c6fd2807SJeff Garzik  *
5137c6fd2807SJeff Garzik  *	RETURNS:
5138c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5139c6fd2807SJeff Garzik  */
5140936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5141c6fd2807SJeff Garzik {
5142633273a3STejun Heo 	if (ata_is_host_link(link)) {
5143da3dbb17STejun Heo 		int rc;
5144da3dbb17STejun Heo 
5145936fd732STejun Heo 		if (sata_scr_valid(link)) {
514682ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5147da3dbb17STejun Heo 			if (rc == 0)
514882ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5149da3dbb17STejun Heo 			return rc;
5150c6fd2807SJeff Garzik 		}
5151c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5152c6fd2807SJeff Garzik 	}
5153c6fd2807SJeff Garzik 
5154633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5155633273a3STejun Heo }
5156633273a3STejun Heo 
5157c6fd2807SJeff Garzik /**
5158b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5159936fd732STejun Heo  *	@link: ATA link to test
5160c6fd2807SJeff Garzik  *
5161936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5162936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5163936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5164c6fd2807SJeff Garzik  *
5165c6fd2807SJeff Garzik  *	LOCKING:
5166c6fd2807SJeff Garzik  *	None.
5167c6fd2807SJeff Garzik  *
5168c6fd2807SJeff Garzik  *	RETURNS:
5169b5b3fa38STejun Heo  *	True if the port online status is available and online.
5170c6fd2807SJeff Garzik  */
5171b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5172c6fd2807SJeff Garzik {
5173c6fd2807SJeff Garzik 	u32 sstatus;
5174c6fd2807SJeff Garzik 
5175936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5176936fd732STejun Heo 	    (sstatus & 0xf) == 0x3)
5177b5b3fa38STejun Heo 		return true;
5178b5b3fa38STejun Heo 	return false;
5179c6fd2807SJeff Garzik }
5180c6fd2807SJeff Garzik 
5181c6fd2807SJeff Garzik /**
5182b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5183936fd732STejun Heo  *	@link: ATA link to test
5184c6fd2807SJeff Garzik  *
5185936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5186936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5187936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5188c6fd2807SJeff Garzik  *
5189c6fd2807SJeff Garzik  *	LOCKING:
5190c6fd2807SJeff Garzik  *	None.
5191c6fd2807SJeff Garzik  *
5192c6fd2807SJeff Garzik  *	RETURNS:
5193b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5194c6fd2807SJeff Garzik  */
5195b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5196c6fd2807SJeff Garzik {
5197c6fd2807SJeff Garzik 	u32 sstatus;
5198c6fd2807SJeff Garzik 
5199936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5200936fd732STejun Heo 	    (sstatus & 0xf) != 0x3)
5201b5b3fa38STejun Heo 		return true;
5202b5b3fa38STejun Heo 	return false;
5203c6fd2807SJeff Garzik }
5204c6fd2807SJeff Garzik 
5205b1c72916STejun Heo /**
5206b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5207b1c72916STejun Heo  *	@link: ATA link to test
5208b1c72916STejun Heo  *
5209b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5210b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5211b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5212b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5213b1c72916STejun Heo  *	online.
5214b1c72916STejun Heo  *
5215b1c72916STejun Heo  *	LOCKING:
5216b1c72916STejun Heo  *	None.
5217b1c72916STejun Heo  *
5218b1c72916STejun Heo  *	RETURNS:
5219b1c72916STejun Heo  *	True if the port online status is available and online.
5220b1c72916STejun Heo  */
5221b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5222b1c72916STejun Heo {
5223b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5224b1c72916STejun Heo 
5225b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5226b1c72916STejun Heo 
5227b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5228b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5229b1c72916STejun Heo }
5230b1c72916STejun Heo 
5231b1c72916STejun Heo /**
5232b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5233b1c72916STejun Heo  *	@link: ATA link to test
5234b1c72916STejun Heo  *
5235b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5236b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5237b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5238b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5239b1c72916STejun Heo  *	offline.
5240b1c72916STejun Heo  *
5241b1c72916STejun Heo  *	LOCKING:
5242b1c72916STejun Heo  *	None.
5243b1c72916STejun Heo  *
5244b1c72916STejun Heo  *	RETURNS:
5245b1c72916STejun Heo  *	True if the port offline status is available and offline.
5246b1c72916STejun Heo  */
5247b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5248b1c72916STejun Heo {
5249b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5250b1c72916STejun Heo 
5251b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5252b1c72916STejun Heo 
5253b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5254b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5255b1c72916STejun Heo }
5256b1c72916STejun Heo 
52576ffa01d8STejun Heo #ifdef CONFIG_PM
5258cca3974eSJeff Garzik static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5259cca3974eSJeff Garzik 			       unsigned int action, unsigned int ehi_flags,
5260cca3974eSJeff Garzik 			       int wait)
5261c6fd2807SJeff Garzik {
5262c6fd2807SJeff Garzik 	unsigned long flags;
5263c6fd2807SJeff Garzik 	int i, rc;
5264c6fd2807SJeff Garzik 
5265cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5266cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5267e3667ebfSTejun Heo 		struct ata_link *link;
5268c6fd2807SJeff Garzik 
5269c6fd2807SJeff Garzik 		/* Previous resume operation might still be in
5270c6fd2807SJeff Garzik 		 * progress.  Wait for PM_PENDING to clear.
5271c6fd2807SJeff Garzik 		 */
5272c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5273c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5274c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5275c6fd2807SJeff Garzik 		}
5276c6fd2807SJeff Garzik 
5277c6fd2807SJeff Garzik 		/* request PM ops to EH */
5278c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
5279c6fd2807SJeff Garzik 
5280c6fd2807SJeff Garzik 		ap->pm_mesg = mesg;
5281c6fd2807SJeff Garzik 		if (wait) {
5282c6fd2807SJeff Garzik 			rc = 0;
5283c6fd2807SJeff Garzik 			ap->pm_result = &rc;
5284c6fd2807SJeff Garzik 		}
5285c6fd2807SJeff Garzik 
5286c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_PM_PENDING;
52871eca4365STejun Heo 		ata_for_each_link(link, ap, HOST_FIRST) {
5288e3667ebfSTejun Heo 			link->eh_info.action |= action;
5289e3667ebfSTejun Heo 			link->eh_info.flags |= ehi_flags;
5290e3667ebfSTejun Heo 		}
5291c6fd2807SJeff Garzik 
5292c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
5293c6fd2807SJeff Garzik 
5294c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
5295c6fd2807SJeff Garzik 
5296c6fd2807SJeff Garzik 		/* wait and check result */
5297c6fd2807SJeff Garzik 		if (wait) {
5298c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5299c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5300c6fd2807SJeff Garzik 			if (rc)
5301c6fd2807SJeff Garzik 				return rc;
5302c6fd2807SJeff Garzik 		}
5303c6fd2807SJeff Garzik 	}
5304c6fd2807SJeff Garzik 
5305c6fd2807SJeff Garzik 	return 0;
5306c6fd2807SJeff Garzik }
5307c6fd2807SJeff Garzik 
5308c6fd2807SJeff Garzik /**
5309cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5310cca3974eSJeff Garzik  *	@host: host to suspend
5311c6fd2807SJeff Garzik  *	@mesg: PM message
5312c6fd2807SJeff Garzik  *
5313cca3974eSJeff Garzik  *	Suspend @host.  Actual operation is performed by EH.  This
5314c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and waits for EH
5315c6fd2807SJeff Garzik  *	to finish.
5316c6fd2807SJeff Garzik  *
5317c6fd2807SJeff Garzik  *	LOCKING:
5318c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5319c6fd2807SJeff Garzik  *
5320c6fd2807SJeff Garzik  *	RETURNS:
5321c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
5322c6fd2807SJeff Garzik  */
5323cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5324c6fd2807SJeff Garzik {
53259666f400STejun Heo 	int rc;
5326c6fd2807SJeff Garzik 
5327ca77329fSKristen Carlson Accardi 	/*
5328ca77329fSKristen Carlson Accardi 	 * disable link pm on all ports before requesting
5329ca77329fSKristen Carlson Accardi 	 * any pm activity
5330ca77329fSKristen Carlson Accardi 	 */
5331ca77329fSKristen Carlson Accardi 	ata_lpm_enable(host);
5332ca77329fSKristen Carlson Accardi 
5333cca3974eSJeff Garzik 	rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
533472ad6ec4SJeff Garzik 	if (rc == 0)
533572ad6ec4SJeff Garzik 		host->dev->power.power_state = mesg;
5336c6fd2807SJeff Garzik 	return rc;
5337c6fd2807SJeff Garzik }
5338c6fd2807SJeff Garzik 
5339c6fd2807SJeff Garzik /**
5340cca3974eSJeff Garzik  *	ata_host_resume - resume host
5341cca3974eSJeff Garzik  *	@host: host to resume
5342c6fd2807SJeff Garzik  *
5343cca3974eSJeff Garzik  *	Resume @host.  Actual operation is performed by EH.  This
5344c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and returns.
5345c6fd2807SJeff Garzik  *	Note that all resume operations are performed parallely.
5346c6fd2807SJeff Garzik  *
5347c6fd2807SJeff Garzik  *	LOCKING:
5348c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5349c6fd2807SJeff Garzik  */
5350cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5351c6fd2807SJeff Garzik {
5352cf480626STejun Heo 	ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
5353c6fd2807SJeff Garzik 			    ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
535472ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5355ca77329fSKristen Carlson Accardi 
5356ca77329fSKristen Carlson Accardi 	/* reenable link pm */
5357ca77329fSKristen Carlson Accardi 	ata_lpm_disable(host);
5358c6fd2807SJeff Garzik }
53596ffa01d8STejun Heo #endif
5360c6fd2807SJeff Garzik 
5361c6fd2807SJeff Garzik /**
5362c6fd2807SJeff Garzik  *	ata_port_start - Set port up for dma.
5363c6fd2807SJeff Garzik  *	@ap: Port to initialize
5364c6fd2807SJeff Garzik  *
5365c6fd2807SJeff Garzik  *	Called just after data structures for each port are
5366c6fd2807SJeff Garzik  *	initialized.  Allocates space for PRD table.
5367c6fd2807SJeff Garzik  *
5368c6fd2807SJeff Garzik  *	May be used as the port_start() entry in ata_port_operations.
5369c6fd2807SJeff Garzik  *
5370c6fd2807SJeff Garzik  *	LOCKING:
5371c6fd2807SJeff Garzik  *	Inherited from caller.
5372c6fd2807SJeff Garzik  */
5373c6fd2807SJeff Garzik int ata_port_start(struct ata_port *ap)
5374c6fd2807SJeff Garzik {
5375c6fd2807SJeff Garzik 	struct device *dev = ap->dev;
5376c6fd2807SJeff Garzik 
5377f0d36efdSTejun Heo 	ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
5378f0d36efdSTejun Heo 				      GFP_KERNEL);
5379c6fd2807SJeff Garzik 	if (!ap->prd)
5380c6fd2807SJeff Garzik 		return -ENOMEM;
5381c6fd2807SJeff Garzik 
5382c6fd2807SJeff Garzik 	return 0;
5383c6fd2807SJeff Garzik }
5384c6fd2807SJeff Garzik 
5385c6fd2807SJeff Garzik /**
5386c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5387c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5388c6fd2807SJeff Garzik  *
5389c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5390c6fd2807SJeff Garzik  *
5391c6fd2807SJeff Garzik  *	LOCKING:
5392c6fd2807SJeff Garzik  *	Inherited from caller.
5393c6fd2807SJeff Garzik  */
5394c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5395c6fd2807SJeff Garzik {
5396b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
53979af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5398c6fd2807SJeff Garzik 	unsigned long flags;
5399c6fd2807SJeff Garzik 
5400b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
54019af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
54029af5c9c9STejun Heo 	link->sata_spd = 0;
5403c6fd2807SJeff Garzik 
5404c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5405c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5406cca3974eSJeff Garzik 	 * host lock.
5407c6fd2807SJeff Garzik 	 */
5408c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5409c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
54103dcc323fSTejun Heo 	dev->horkage = 0;
5411c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5412c6fd2807SJeff Garzik 
5413c6fd2807SJeff Garzik 	memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
5414c6fd2807SJeff Garzik 	       sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
5415c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5416c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5417c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5418c6fd2807SJeff Garzik }
5419c6fd2807SJeff Garzik 
5420c6fd2807SJeff Garzik /**
54214fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
54224fb37a25STejun Heo  *	@ap: ATA port link is attached to
54234fb37a25STejun Heo  *	@link: Link structure to initialize
54248989805dSTejun Heo  *	@pmp: Port multiplier port number
54254fb37a25STejun Heo  *
54264fb37a25STejun Heo  *	Initialize @link.
54274fb37a25STejun Heo  *
54284fb37a25STejun Heo  *	LOCKING:
54294fb37a25STejun Heo  *	Kernel thread context (may sleep)
54304fb37a25STejun Heo  */
5431fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
54324fb37a25STejun Heo {
54334fb37a25STejun Heo 	int i;
54344fb37a25STejun Heo 
54354fb37a25STejun Heo 	/* clear everything except for devices */
54364fb37a25STejun Heo 	memset(link, 0, offsetof(struct ata_link, device[0]));
54374fb37a25STejun Heo 
54384fb37a25STejun Heo 	link->ap = ap;
54398989805dSTejun Heo 	link->pmp = pmp;
54404fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
54414fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
54424fb37a25STejun Heo 
54434fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
54444fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
54454fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
54464fb37a25STejun Heo 
54474fb37a25STejun Heo 		dev->link = link;
54484fb37a25STejun Heo 		dev->devno = dev - link->device;
54494fb37a25STejun Heo 		ata_dev_init(dev);
54504fb37a25STejun Heo 	}
54514fb37a25STejun Heo }
54524fb37a25STejun Heo 
54534fb37a25STejun Heo /**
54544fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
54554fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
54564fb37a25STejun Heo  *
54574fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
54584fb37a25STejun Heo  *	configured value.
54594fb37a25STejun Heo  *
54604fb37a25STejun Heo  *	LOCKING:
54614fb37a25STejun Heo  *	Kernel thread context (may sleep).
54624fb37a25STejun Heo  *
54634fb37a25STejun Heo  *	RETURNS:
54644fb37a25STejun Heo  *	0 on success, -errno on failure.
54654fb37a25STejun Heo  */
5466fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
54674fb37a25STejun Heo {
546833267325STejun Heo 	u8 spd;
54694fb37a25STejun Heo 	int rc;
54704fb37a25STejun Heo 
5471d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
54724fb37a25STejun Heo 	if (rc)
54734fb37a25STejun Heo 		return rc;
54744fb37a25STejun Heo 
5475d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
54764fb37a25STejun Heo 	if (spd)
54774fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
54784fb37a25STejun Heo 
547905944bdfSTejun Heo 	ata_force_link_limits(link);
548033267325STejun Heo 
54814fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
54824fb37a25STejun Heo 
54834fb37a25STejun Heo 	return 0;
54844fb37a25STejun Heo }
54854fb37a25STejun Heo 
54864fb37a25STejun Heo /**
5487f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5488f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5489c6fd2807SJeff Garzik  *
5490f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5491f3187195STejun Heo  *
5492f3187195STejun Heo  *	RETURNS:
5493f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5494c6fd2807SJeff Garzik  *
5495c6fd2807SJeff Garzik  *	LOCKING:
5496f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5497c6fd2807SJeff Garzik  */
5498f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5499c6fd2807SJeff Garzik {
5500f3187195STejun Heo 	struct ata_port *ap;
5501c6fd2807SJeff Garzik 
5502f3187195STejun Heo 	DPRINTK("ENTER\n");
5503f3187195STejun Heo 
5504f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5505f3187195STejun Heo 	if (!ap)
5506f3187195STejun Heo 		return NULL;
5507f3187195STejun Heo 
5508f4d6d004STejun Heo 	ap->pflags |= ATA_PFLAG_INITIALIZING;
5509cca3974eSJeff Garzik 	ap->lock = &host->lock;
5510c6fd2807SJeff Garzik 	ap->flags = ATA_FLAG_DISABLED;
5511f3187195STejun Heo 	ap->print_id = -1;
5512c6fd2807SJeff Garzik 	ap->ctl = ATA_DEVCTL_OBS;
5513cca3974eSJeff Garzik 	ap->host = host;
5514f3187195STejun Heo 	ap->dev = host->dev;
5515c6fd2807SJeff Garzik 	ap->last_ctl = 0xFF;
5516c6fd2807SJeff Garzik 
5517c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5518c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5519c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5520c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5521c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5522c6fd2807SJeff Garzik #else
5523c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5524c6fd2807SJeff Garzik #endif
5525c6fd2807SJeff Garzik 
5526127102aeSTejun Heo #ifdef CONFIG_ATA_SFF
5527442eacc3SJeff Garzik 	INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
5528f667fdbbSTejun Heo #else
5529f667fdbbSTejun Heo 	INIT_DELAYED_WORK(&ap->port_task, NULL);
5530127102aeSTejun Heo #endif
553165f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
553265f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5533c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5534c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
553545fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
55365ddf24c5STejun Heo 	init_timer_deferrable(&ap->fastdrain_timer);
55375ddf24c5STejun Heo 	ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
55385ddf24c5STejun Heo 	ap->fastdrain_timer.data = (unsigned long)ap;
5539c6fd2807SJeff Garzik 
5540c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5541c6fd2807SJeff Garzik 
55428989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5543c6fd2807SJeff Garzik 
5544c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5545c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5546c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5547c6fd2807SJeff Garzik #endif
5548c6fd2807SJeff Garzik 	return ap;
5549c6fd2807SJeff Garzik }
5550c6fd2807SJeff Garzik 
5551f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5552f0d36efdSTejun Heo {
5553f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5554f0d36efdSTejun Heo 	int i;
5555f0d36efdSTejun Heo 
5556f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5557f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5558f0d36efdSTejun Heo 
5559ecef7253STejun Heo 		if (!ap)
5560ecef7253STejun Heo 			continue;
5561ecef7253STejun Heo 
55624911487aSTejun Heo 		if (ap->scsi_host)
55631aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
55641aa506e4STejun Heo 
5565633273a3STejun Heo 		kfree(ap->pmp_link);
5566b1c72916STejun Heo 		kfree(ap->slave_link);
55674911487aSTejun Heo 		kfree(ap);
55681aa506e4STejun Heo 		host->ports[i] = NULL;
55691aa506e4STejun Heo 	}
55701aa506e4STejun Heo 
55711aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5572f0d36efdSTejun Heo }
5573f0d36efdSTejun Heo 
5574c6fd2807SJeff Garzik /**
5575f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5576f3187195STejun Heo  *	@dev: generic device this host is associated with
5577f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5578f3187195STejun Heo  *
5579f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5580f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5581f3187195STejun Heo  *	attaches it using ata_host_register().
5582f3187195STejun Heo  *
5583f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5584f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5585f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5586f3187195STejun Heo  *	ports will be automatically freed on registration.
5587f3187195STejun Heo  *
5588f3187195STejun Heo  *	RETURNS:
5589f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5590f3187195STejun Heo  *
5591f3187195STejun Heo  *	LOCKING:
5592f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5593f3187195STejun Heo  */
5594f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5595f3187195STejun Heo {
5596f3187195STejun Heo 	struct ata_host *host;
5597f3187195STejun Heo 	size_t sz;
5598f3187195STejun Heo 	int i;
5599f3187195STejun Heo 
5600f3187195STejun Heo 	DPRINTK("ENTER\n");
5601f3187195STejun Heo 
5602f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5603f3187195STejun Heo 		return NULL;
5604f3187195STejun Heo 
5605f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5606f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5607f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5608f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5609f3187195STejun Heo 	if (!host)
5610f3187195STejun Heo 		goto err_out;
5611f3187195STejun Heo 
5612f3187195STejun Heo 	devres_add(dev, host);
5613f3187195STejun Heo 	dev_set_drvdata(dev, host);
5614f3187195STejun Heo 
5615f3187195STejun Heo 	spin_lock_init(&host->lock);
5616f3187195STejun Heo 	host->dev = dev;
5617f3187195STejun Heo 	host->n_ports = max_ports;
5618f3187195STejun Heo 
5619f3187195STejun Heo 	/* allocate ports bound to this host */
5620f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5621f3187195STejun Heo 		struct ata_port *ap;
5622f3187195STejun Heo 
5623f3187195STejun Heo 		ap = ata_port_alloc(host);
5624f3187195STejun Heo 		if (!ap)
5625f3187195STejun Heo 			goto err_out;
5626f3187195STejun Heo 
5627f3187195STejun Heo 		ap->port_no = i;
5628f3187195STejun Heo 		host->ports[i] = ap;
5629f3187195STejun Heo 	}
5630f3187195STejun Heo 
5631f3187195STejun Heo 	devres_remove_group(dev, NULL);
5632f3187195STejun Heo 	return host;
5633f3187195STejun Heo 
5634f3187195STejun Heo  err_out:
5635f3187195STejun Heo 	devres_release_group(dev, NULL);
5636f3187195STejun Heo 	return NULL;
5637f3187195STejun Heo }
5638f3187195STejun Heo 
5639f3187195STejun Heo /**
5640f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5641f5cda257STejun Heo  *	@dev: generic device this host is associated with
5642f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5643f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5644f5cda257STejun Heo  *
5645f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5646f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5647f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5648f5cda257STejun Heo  *
5649f5cda257STejun Heo  *	RETURNS:
5650f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5651f5cda257STejun Heo  *
5652f5cda257STejun Heo  *	LOCKING:
5653f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5654f5cda257STejun Heo  */
5655f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5656f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5657f5cda257STejun Heo 				      int n_ports)
5658f5cda257STejun Heo {
5659f5cda257STejun Heo 	const struct ata_port_info *pi;
5660f5cda257STejun Heo 	struct ata_host *host;
5661f5cda257STejun Heo 	int i, j;
5662f5cda257STejun Heo 
5663f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5664f5cda257STejun Heo 	if (!host)
5665f5cda257STejun Heo 		return NULL;
5666f5cda257STejun Heo 
5667f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5668f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5669f5cda257STejun Heo 
5670f5cda257STejun Heo 		if (ppi[j])
5671f5cda257STejun Heo 			pi = ppi[j++];
5672f5cda257STejun Heo 
5673f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5674f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5675f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5676f5cda257STejun Heo 		ap->flags |= pi->flags;
56770c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5678f5cda257STejun Heo 		ap->ops = pi->port_ops;
5679f5cda257STejun Heo 
5680f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5681f5cda257STejun Heo 			host->ops = pi->port_ops;
5682f5cda257STejun Heo 	}
5683f5cda257STejun Heo 
5684f5cda257STejun Heo 	return host;
5685f5cda257STejun Heo }
5686f5cda257STejun Heo 
5687b1c72916STejun Heo /**
5688b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
5689b1c72916STejun Heo  *	@ap: port to initialize slave link for
5690b1c72916STejun Heo  *
5691b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
5692b1c72916STejun Heo  *	link handling on the port.
5693b1c72916STejun Heo  *
5694b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
5695b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
5696b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
5697b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
5698b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
5699b1c72916STejun Heo  *	and slave.
5700b1c72916STejun Heo  *
5701b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
5702b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
5703b1c72916STejun Heo  *	interface with both master and slave devices but also have
5704b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
5705b1c72916STejun Heo  *	need separate links for physical link handling
5706b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
5707b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
5708b1c72916STejun Heo  *	issue, softreset).
5709b1c72916STejun Heo  *
5710b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
5711b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
5712b1c72916STejun Heo  *	anything other than physical link handling, the default host
5713b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
5714b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
5715b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
5716b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
5717b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
5718b1c72916STejun Heo  *	looks like the following.
5719b1c72916STejun Heo  *
5720b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5721b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
5722b1c72916STejun Heo  *
5723b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
5724b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
5725b1c72916STejun Heo  *	both (the standard method will work just fine).
5726b1c72916STejun Heo  *
5727b1c72916STejun Heo  *	LOCKING:
5728b1c72916STejun Heo  *	Should be called before host is registered.
5729b1c72916STejun Heo  *
5730b1c72916STejun Heo  *	RETURNS:
5731b1c72916STejun Heo  *	0 on success, -errno on failure.
5732b1c72916STejun Heo  */
5733b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
5734b1c72916STejun Heo {
5735b1c72916STejun Heo 	struct ata_link *link;
5736b1c72916STejun Heo 
5737b1c72916STejun Heo 	WARN_ON(ap->slave_link);
5738b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
5739b1c72916STejun Heo 
5740b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
5741b1c72916STejun Heo 	if (!link)
5742b1c72916STejun Heo 		return -ENOMEM;
5743b1c72916STejun Heo 
5744b1c72916STejun Heo 	ata_link_init(ap, link, 1);
5745b1c72916STejun Heo 	ap->slave_link = link;
5746b1c72916STejun Heo 	return 0;
5747b1c72916STejun Heo }
5748b1c72916STejun Heo 
574932ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
575032ebbc0cSTejun Heo {
575132ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
575232ebbc0cSTejun Heo 	int i;
575332ebbc0cSTejun Heo 
575432ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
575532ebbc0cSTejun Heo 
575632ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
575732ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
575832ebbc0cSTejun Heo 
575932ebbc0cSTejun Heo 		if (ap->ops->port_stop)
576032ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
576132ebbc0cSTejun Heo 	}
576232ebbc0cSTejun Heo 
576332ebbc0cSTejun Heo 	if (host->ops->host_stop)
576432ebbc0cSTejun Heo 		host->ops->host_stop(host);
576532ebbc0cSTejun Heo }
576632ebbc0cSTejun Heo 
5767f5cda257STejun Heo /**
5768029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5769029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5770029cfd6bSTejun Heo  *
5771029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5772029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5773029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5774029cfd6bSTejun Heo  *	inheritance chain.
5775029cfd6bSTejun Heo  *
5776029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5777029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5778029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5779029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5780029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5781029cfd6bSTejun Heo  *
5782029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5783029cfd6bSTejun Heo  *
5784029cfd6bSTejun Heo  *	LOCKING:
5785029cfd6bSTejun Heo  *	None.
5786029cfd6bSTejun Heo  */
5787029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5788029cfd6bSTejun Heo {
57892da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5790029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5791029cfd6bSTejun Heo 	void **begin = (void **)ops;
5792029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5793029cfd6bSTejun Heo 	void **pp;
5794029cfd6bSTejun Heo 
5795029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5796029cfd6bSTejun Heo 		return;
5797029cfd6bSTejun Heo 
5798029cfd6bSTejun Heo 	spin_lock(&lock);
5799029cfd6bSTejun Heo 
5800029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5801029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5802029cfd6bSTejun Heo 
5803029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5804029cfd6bSTejun Heo 			if (!*pp)
5805029cfd6bSTejun Heo 				*pp = *inherit;
5806029cfd6bSTejun Heo 	}
5807029cfd6bSTejun Heo 
5808029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5809029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5810029cfd6bSTejun Heo 			*pp = NULL;
5811029cfd6bSTejun Heo 
5812029cfd6bSTejun Heo 	ops->inherits = NULL;
5813029cfd6bSTejun Heo 
5814029cfd6bSTejun Heo 	spin_unlock(&lock);
5815029cfd6bSTejun Heo }
5816029cfd6bSTejun Heo 
5817029cfd6bSTejun Heo /**
5818ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5819ecef7253STejun Heo  *	@host: ATA host to start ports for
5820ecef7253STejun Heo  *
5821ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5822ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5823ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5824f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
5825f3187195STejun Heo  *	first non-dummy port ops.
5826ecef7253STejun Heo  *
5827ecef7253STejun Heo  *	LOCKING:
5828ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5829ecef7253STejun Heo  *
5830ecef7253STejun Heo  *	RETURNS:
5831ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5832ecef7253STejun Heo  */
5833ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5834ecef7253STejun Heo {
583532ebbc0cSTejun Heo 	int have_stop = 0;
583632ebbc0cSTejun Heo 	void *start_dr = NULL;
5837ecef7253STejun Heo 	int i, rc;
5838ecef7253STejun Heo 
5839ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5840ecef7253STejun Heo 		return 0;
5841ecef7253STejun Heo 
5842029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5843029cfd6bSTejun Heo 
5844ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5845ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5846ecef7253STejun Heo 
5847029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5848029cfd6bSTejun Heo 
5849f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5850f3187195STejun Heo 			host->ops = ap->ops;
5851f3187195STejun Heo 
585232ebbc0cSTejun Heo 		if (ap->ops->port_stop)
585332ebbc0cSTejun Heo 			have_stop = 1;
585432ebbc0cSTejun Heo 	}
585532ebbc0cSTejun Heo 
585632ebbc0cSTejun Heo 	if (host->ops->host_stop)
585732ebbc0cSTejun Heo 		have_stop = 1;
585832ebbc0cSTejun Heo 
585932ebbc0cSTejun Heo 	if (have_stop) {
586032ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
586132ebbc0cSTejun Heo 		if (!start_dr)
586232ebbc0cSTejun Heo 			return -ENOMEM;
586332ebbc0cSTejun Heo 	}
586432ebbc0cSTejun Heo 
586532ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
586632ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
586732ebbc0cSTejun Heo 
5868ecef7253STejun Heo 		if (ap->ops->port_start) {
5869ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5870ecef7253STejun Heo 			if (rc) {
58710f9fe9b7SAlan Cox 				if (rc != -ENODEV)
58720f757743SAndrew Morton 					dev_printk(KERN_ERR, host->dev,
58730f757743SAndrew Morton 						"failed to start port %d "
58740f757743SAndrew Morton 						"(errno=%d)\n", i, rc);
5875ecef7253STejun Heo 				goto err_out;
5876ecef7253STejun Heo 			}
5877ecef7253STejun Heo 		}
5878ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5879ecef7253STejun Heo 	}
5880ecef7253STejun Heo 
588132ebbc0cSTejun Heo 	if (start_dr)
588232ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5883ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5884ecef7253STejun Heo 	return 0;
5885ecef7253STejun Heo 
5886ecef7253STejun Heo  err_out:
5887ecef7253STejun Heo 	while (--i >= 0) {
5888ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5889ecef7253STejun Heo 
5890ecef7253STejun Heo 		if (ap->ops->port_stop)
5891ecef7253STejun Heo 			ap->ops->port_stop(ap);
5892ecef7253STejun Heo 	}
589332ebbc0cSTejun Heo 	devres_free(start_dr);
5894ecef7253STejun Heo 	return rc;
5895ecef7253STejun Heo }
5896ecef7253STejun Heo 
5897ecef7253STejun Heo /**
5898cca3974eSJeff Garzik  *	ata_sas_host_init - Initialize a host struct
5899cca3974eSJeff Garzik  *	@host:	host to initialize
5900cca3974eSJeff Garzik  *	@dev:	device host is attached to
5901cca3974eSJeff Garzik  *	@flags:	host flags
5902c6fd2807SJeff Garzik  *	@ops:	port_ops
5903c6fd2807SJeff Garzik  *
5904c6fd2807SJeff Garzik  *	LOCKING:
5905c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
5906c6fd2807SJeff Garzik  *
5907c6fd2807SJeff Garzik  */
5908f3187195STejun Heo /* KILLME - the only user left is ipr */
5909cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
5910029cfd6bSTejun Heo 		   unsigned long flags, struct ata_port_operations *ops)
5911c6fd2807SJeff Garzik {
5912cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5913cca3974eSJeff Garzik 	host->dev = dev;
5914cca3974eSJeff Garzik 	host->flags = flags;
5915cca3974eSJeff Garzik 	host->ops = ops;
5916c6fd2807SJeff Garzik }
5917c6fd2807SJeff Garzik 
591879318057SArjan van de Ven 
591979318057SArjan van de Ven static void async_port_probe(void *data, async_cookie_t cookie)
592079318057SArjan van de Ven {
592179318057SArjan van de Ven 	int rc;
592279318057SArjan van de Ven 	struct ata_port *ap = data;
592379318057SArjan van de Ven 	/* probe */
592479318057SArjan van de Ven 	if (ap->ops->error_handler) {
592579318057SArjan van de Ven 		struct ata_eh_info *ehi = &ap->link.eh_info;
592679318057SArjan van de Ven 		unsigned long flags;
592779318057SArjan van de Ven 
592879318057SArjan van de Ven 		ata_port_probe(ap);
592979318057SArjan van de Ven 
593079318057SArjan van de Ven 		/* kick EH for boot probing */
593179318057SArjan van de Ven 		spin_lock_irqsave(ap->lock, flags);
593279318057SArjan van de Ven 
593379318057SArjan van de Ven 		ehi->probe_mask |= ATA_ALL_DEVICES;
593479318057SArjan van de Ven 		ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
593579318057SArjan van de Ven 		ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
593679318057SArjan van de Ven 
593779318057SArjan van de Ven 		ap->pflags &= ~ATA_PFLAG_INITIALIZING;
593879318057SArjan van de Ven 		ap->pflags |= ATA_PFLAG_LOADING;
593979318057SArjan van de Ven 		ata_port_schedule_eh(ap);
594079318057SArjan van de Ven 
594179318057SArjan van de Ven 		spin_unlock_irqrestore(ap->lock, flags);
594279318057SArjan van de Ven 
594379318057SArjan van de Ven 		/* wait for EH to finish */
594479318057SArjan van de Ven 		ata_port_wait_eh(ap);
594579318057SArjan van de Ven 	} else {
594679318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
594779318057SArjan van de Ven 		rc = ata_bus_probe(ap);
594879318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
594979318057SArjan van de Ven 
595079318057SArjan van de Ven 		if (rc) {
595179318057SArjan van de Ven 			/* FIXME: do something useful here?
595279318057SArjan van de Ven 			 * Current libata behavior will
595379318057SArjan van de Ven 			 * tear down everything when
595479318057SArjan van de Ven 			 * the module is removed
595579318057SArjan van de Ven 			 * or the h/w is unplugged.
595679318057SArjan van de Ven 			 */
595779318057SArjan van de Ven 		}
595879318057SArjan van de Ven 	}
5959f29d3b23SArjan van de Ven 
5960f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
5961f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
5962f29d3b23SArjan van de Ven 
5963f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
5964f29d3b23SArjan van de Ven 
596579318057SArjan van de Ven }
5966c6fd2807SJeff Garzik /**
5967f3187195STejun Heo  *	ata_host_register - register initialized ATA host
5968f3187195STejun Heo  *	@host: ATA host to register
5969f3187195STejun Heo  *	@sht: template for SCSI host
5970c6fd2807SJeff Garzik  *
5971f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
5972f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
5973f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
5974f3187195STejun Heo  *	probe registered devices.
5975c6fd2807SJeff Garzik  *
5976c6fd2807SJeff Garzik  *	LOCKING:
5977f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5978c6fd2807SJeff Garzik  *
5979c6fd2807SJeff Garzik  *	RETURNS:
5980f3187195STejun Heo  *	0 on success, -errno otherwise.
5981c6fd2807SJeff Garzik  */
5982f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
5983c6fd2807SJeff Garzik {
5984f3187195STejun Heo 	int i, rc;
5985c6fd2807SJeff Garzik 
5986f3187195STejun Heo 	/* host must have been started */
5987f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
5988f3187195STejun Heo 		dev_printk(KERN_ERR, host->dev,
5989f3187195STejun Heo 			   "BUG: trying to register unstarted host\n");
5990f3187195STejun Heo 		WARN_ON(1);
5991f3187195STejun Heo 		return -EINVAL;
599202f076aaSAlan Cox 	}
5993f0d36efdSTejun Heo 
5994f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
5995f3187195STejun Heo 	 * determine the exact number of ports to allocate at
5996f3187195STejun Heo 	 * allocation time.
5997f3187195STejun Heo 	 */
5998f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
5999f3187195STejun Heo 		kfree(host->ports[i]);
6000f0d36efdSTejun Heo 
6001f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6002f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++)
6003f3187195STejun Heo 		host->ports[i]->print_id = ata_print_id++;
6004c6fd2807SJeff Garzik 
6005f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6006ecef7253STejun Heo 	if (rc)
6007f3187195STejun Heo 		return rc;
6008ecef7253STejun Heo 
6009fafbae87STejun Heo 	/* associate with ACPI nodes */
6010fafbae87STejun Heo 	ata_acpi_associate(host);
6011fafbae87STejun Heo 
6012f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6013cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6014cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6015f3187195STejun Heo 		unsigned long xfer_mask;
6016f3187195STejun Heo 
6017f3187195STejun Heo 		/* set SATA cable type if still unset */
6018f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6019f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6020c6fd2807SJeff Garzik 
6021c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
60224fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6023b1c72916STejun Heo 		if (ap->slave_link)
6024b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6025c6fd2807SJeff Garzik 
6026cbcdd875STejun Heo 		/* print per-port info to dmesg */
6027f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6028f3187195STejun Heo 					      ap->udma_mask);
6029f3187195STejun Heo 
6030abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6031cbcdd875STejun Heo 			ata_port_printk(ap, KERN_INFO,
6032cbcdd875STejun Heo 					"%cATA max %s %s\n",
6033a16abc0bSTejun Heo 					(ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6034f3187195STejun Heo 					ata_mode_string(xfer_mask),
6035cbcdd875STejun Heo 					ap->link.eh_info.desc);
6036abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6037abf6e8edSTejun Heo 		} else
6038f3187195STejun Heo 			ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6039c6fd2807SJeff Garzik 	}
6040c6fd2807SJeff Garzik 
6041f3187195STejun Heo 	/* perform each probe synchronously */
6042f3187195STejun Heo 	DPRINTK("probe begin\n");
6043f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6044f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
604579318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6046c6fd2807SJeff Garzik 	}
6047f29d3b23SArjan van de Ven 	DPRINTK("probe end\n");
6048c6fd2807SJeff Garzik 
6049f3187195STejun Heo 	return 0;
6050f3187195STejun Heo }
6051f3187195STejun Heo 
6052f3187195STejun Heo /**
6053f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6054f5cda257STejun Heo  *	@host: target ATA host
6055f5cda257STejun Heo  *	@irq: IRQ to request
6056f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6057f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6058f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6059f5cda257STejun Heo  *
6060f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6061f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6062f5cda257STejun Heo  *	request IRQ and register it.  This helper takes necessasry
6063f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6064f5cda257STejun Heo  *
60653d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
60663d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
60673d46b2e2SPaul Mundt  *	should be NULL.
60683d46b2e2SPaul Mundt  *
6069f5cda257STejun Heo  *	LOCKING:
6070f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6071f5cda257STejun Heo  *
6072f5cda257STejun Heo  *	RETURNS:
6073f5cda257STejun Heo  *	0 on success, -errno otherwise.
6074f5cda257STejun Heo  */
6075f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6076f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6077f5cda257STejun Heo 		      struct scsi_host_template *sht)
6078f5cda257STejun Heo {
6079cbcdd875STejun Heo 	int i, rc;
6080f5cda257STejun Heo 
6081f5cda257STejun Heo 	rc = ata_host_start(host);
6082f5cda257STejun Heo 	if (rc)
6083f5cda257STejun Heo 		return rc;
6084f5cda257STejun Heo 
60853d46b2e2SPaul Mundt 	/* Special case for polling mode */
60863d46b2e2SPaul Mundt 	if (!irq) {
60873d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
60883d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
60893d46b2e2SPaul Mundt 	}
60903d46b2e2SPaul Mundt 
6091f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6092f5cda257STejun Heo 			      dev_driver_string(host->dev), host);
6093f5cda257STejun Heo 	if (rc)
6094f5cda257STejun Heo 		return rc;
6095f5cda257STejun Heo 
6096cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6097cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
60984031826bSTejun Heo 
6099f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6100f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6101f5cda257STejun Heo 	if (rc)
6102f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6103f5cda257STejun Heo 
6104f5cda257STejun Heo 	return rc;
6105f5cda257STejun Heo }
6106f5cda257STejun Heo 
6107f5cda257STejun Heo /**
6108c6fd2807SJeff Garzik  *	ata_port_detach - Detach ATA port in prepration of device removal
6109c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6110c6fd2807SJeff Garzik  *
6111c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6112c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6113c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6114c6fd2807SJeff Garzik  *
6115c6fd2807SJeff Garzik  *	LOCKING:
6116c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6117c6fd2807SJeff Garzik  */
6118741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6119c6fd2807SJeff Garzik {
6120c6fd2807SJeff Garzik 	unsigned long flags;
6121c6fd2807SJeff Garzik 
6122c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6123c6fd2807SJeff Garzik 		goto skip_eh;
6124c6fd2807SJeff Garzik 
6125c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6126c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6127c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6128ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6129c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6130c6fd2807SJeff Garzik 
6131ece180d1STejun Heo 	/* wait till EH commits suicide */
6132c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6133c6fd2807SJeff Garzik 
6134ece180d1STejun Heo 	/* it better be dead now */
6135ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6136c6fd2807SJeff Garzik 
613745a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->hotplug_task);
6138c6fd2807SJeff Garzik 
6139c6fd2807SJeff Garzik  skip_eh:
6140c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6141cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6142c6fd2807SJeff Garzik }
6143c6fd2807SJeff Garzik 
6144c6fd2807SJeff Garzik /**
61450529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
61460529c159STejun Heo  *	@host: Host to detach
61470529c159STejun Heo  *
61480529c159STejun Heo  *	Detach all ports of @host.
61490529c159STejun Heo  *
61500529c159STejun Heo  *	LOCKING:
61510529c159STejun Heo  *	Kernel thread context (may sleep).
61520529c159STejun Heo  */
61530529c159STejun Heo void ata_host_detach(struct ata_host *host)
61540529c159STejun Heo {
61550529c159STejun Heo 	int i;
61560529c159STejun Heo 
61570529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
61580529c159STejun Heo 		ata_port_detach(host->ports[i]);
6159562f0c2dSTejun Heo 
6160562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6161562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
61620529c159STejun Heo }
61630529c159STejun Heo 
6164c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6165c6fd2807SJeff Garzik 
6166c6fd2807SJeff Garzik /**
6167c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6168c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6169c6fd2807SJeff Garzik  *
6170b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6171b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6172b878ca5dSTejun Heo  *	release is handled via devres.
6173c6fd2807SJeff Garzik  *
6174c6fd2807SJeff Garzik  *	LOCKING:
6175c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6176c6fd2807SJeff Garzik  */
6177c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6178c6fd2807SJeff Garzik {
61792855568bSJeff Garzik 	struct device *dev = &pdev->dev;
6180cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(dev);
6181c6fd2807SJeff Garzik 
6182f0d36efdSTejun Heo 	ata_host_detach(host);
6183c6fd2807SJeff Garzik }
6184c6fd2807SJeff Garzik 
6185c6fd2807SJeff Garzik /* move to PCI subsystem */
6186c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6187c6fd2807SJeff Garzik {
6188c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6189c6fd2807SJeff Garzik 
6190c6fd2807SJeff Garzik 	switch (bits->width) {
6191c6fd2807SJeff Garzik 	case 1: {
6192c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6193c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6194c6fd2807SJeff Garzik 		tmp = tmp8;
6195c6fd2807SJeff Garzik 		break;
6196c6fd2807SJeff Garzik 	}
6197c6fd2807SJeff Garzik 	case 2: {
6198c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6199c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6200c6fd2807SJeff Garzik 		tmp = tmp16;
6201c6fd2807SJeff Garzik 		break;
6202c6fd2807SJeff Garzik 	}
6203c6fd2807SJeff Garzik 	case 4: {
6204c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6205c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6206c6fd2807SJeff Garzik 		tmp = tmp32;
6207c6fd2807SJeff Garzik 		break;
6208c6fd2807SJeff Garzik 	}
6209c6fd2807SJeff Garzik 
6210c6fd2807SJeff Garzik 	default:
6211c6fd2807SJeff Garzik 		return -EINVAL;
6212c6fd2807SJeff Garzik 	}
6213c6fd2807SJeff Garzik 
6214c6fd2807SJeff Garzik 	tmp &= bits->mask;
6215c6fd2807SJeff Garzik 
6216c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6217c6fd2807SJeff Garzik }
6218c6fd2807SJeff Garzik 
62196ffa01d8STejun Heo #ifdef CONFIG_PM
6220c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6221c6fd2807SJeff Garzik {
6222c6fd2807SJeff Garzik 	pci_save_state(pdev);
6223c6fd2807SJeff Garzik 	pci_disable_device(pdev);
62244c90d971STejun Heo 
62253a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6226c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6227c6fd2807SJeff Garzik }
6228c6fd2807SJeff Garzik 
6229553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6230c6fd2807SJeff Garzik {
6231553c4aa6STejun Heo 	int rc;
6232553c4aa6STejun Heo 
6233c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6234c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6235553c4aa6STejun Heo 
6236f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6237553c4aa6STejun Heo 	if (rc) {
6238553c4aa6STejun Heo 		dev_printk(KERN_ERR, &pdev->dev,
6239553c4aa6STejun Heo 			   "failed to enable device after resume (%d)\n", rc);
6240553c4aa6STejun Heo 		return rc;
6241553c4aa6STejun Heo 	}
6242553c4aa6STejun Heo 
6243c6fd2807SJeff Garzik 	pci_set_master(pdev);
6244553c4aa6STejun Heo 	return 0;
6245c6fd2807SJeff Garzik }
6246c6fd2807SJeff Garzik 
6247c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6248c6fd2807SJeff Garzik {
6249cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6250c6fd2807SJeff Garzik 	int rc = 0;
6251c6fd2807SJeff Garzik 
6252cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6253c6fd2807SJeff Garzik 	if (rc)
6254c6fd2807SJeff Garzik 		return rc;
6255c6fd2807SJeff Garzik 
6256c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6257c6fd2807SJeff Garzik 
6258c6fd2807SJeff Garzik 	return 0;
6259c6fd2807SJeff Garzik }
6260c6fd2807SJeff Garzik 
6261c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6262c6fd2807SJeff Garzik {
6263cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6264553c4aa6STejun Heo 	int rc;
6265c6fd2807SJeff Garzik 
6266553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6267553c4aa6STejun Heo 	if (rc == 0)
6268cca3974eSJeff Garzik 		ata_host_resume(host);
6269553c4aa6STejun Heo 	return rc;
6270c6fd2807SJeff Garzik }
62716ffa01d8STejun Heo #endif /* CONFIG_PM */
62726ffa01d8STejun Heo 
6273c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6274c6fd2807SJeff Garzik 
627533267325STejun Heo static int __init ata_parse_force_one(char **cur,
627633267325STejun Heo 				      struct ata_force_ent *force_ent,
627733267325STejun Heo 				      const char **reason)
627833267325STejun Heo {
627933267325STejun Heo 	/* FIXME: Currently, there's no way to tag init const data and
628033267325STejun Heo 	 * using __initdata causes build failure on some versions of
628133267325STejun Heo 	 * gcc.  Once __initdataconst is implemented, add const to the
628233267325STejun Heo 	 * following structure.
628333267325STejun Heo 	 */
628433267325STejun Heo 	static struct ata_force_param force_tbl[] __initdata = {
628533267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
628633267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
628733267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
628833267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
628933267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
629033267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
629133267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
629233267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
629333267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
629433267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
629533267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
629633267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
629733267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
629833267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
629933267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
630033267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
630133267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
630233267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
630333267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
630433267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
630533267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
630633267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
630733267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
630833267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
630933267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
631033267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
631133267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
631233267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
631333267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
631433267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
631533267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
631633267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
631733267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
631833267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
631933267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
632033267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
632133267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
632233267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
632333267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
632433267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
632533267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
632633267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
632733267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
632833267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
632905944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
633005944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
633105944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
633233267325STejun Heo 	};
633333267325STejun Heo 	char *start = *cur, *p = *cur;
633433267325STejun Heo 	char *id, *val, *endp;
633533267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
633633267325STejun Heo 	int nr_matches = 0, i;
633733267325STejun Heo 
633833267325STejun Heo 	/* find where this param ends and update *cur */
633933267325STejun Heo 	while (*p != '\0' && *p != ',')
634033267325STejun Heo 		p++;
634133267325STejun Heo 
634233267325STejun Heo 	if (*p == '\0')
634333267325STejun Heo 		*cur = p;
634433267325STejun Heo 	else
634533267325STejun Heo 		*cur = p + 1;
634633267325STejun Heo 
634733267325STejun Heo 	*p = '\0';
634833267325STejun Heo 
634933267325STejun Heo 	/* parse */
635033267325STejun Heo 	p = strchr(start, ':');
635133267325STejun Heo 	if (!p) {
635233267325STejun Heo 		val = strstrip(start);
635333267325STejun Heo 		goto parse_val;
635433267325STejun Heo 	}
635533267325STejun Heo 	*p = '\0';
635633267325STejun Heo 
635733267325STejun Heo 	id = strstrip(start);
635833267325STejun Heo 	val = strstrip(p + 1);
635933267325STejun Heo 
636033267325STejun Heo 	/* parse id */
636133267325STejun Heo 	p = strchr(id, '.');
636233267325STejun Heo 	if (p) {
636333267325STejun Heo 		*p++ = '\0';
636433267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
636533267325STejun Heo 		if (p == endp || *endp != '\0') {
636633267325STejun Heo 			*reason = "invalid device";
636733267325STejun Heo 			return -EINVAL;
636833267325STejun Heo 		}
636933267325STejun Heo 	}
637033267325STejun Heo 
637133267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
637233267325STejun Heo 	if (p == endp || *endp != '\0') {
637333267325STejun Heo 		*reason = "invalid port/link";
637433267325STejun Heo 		return -EINVAL;
637533267325STejun Heo 	}
637633267325STejun Heo 
637733267325STejun Heo  parse_val:
637833267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
637933267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
638033267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
638133267325STejun Heo 
638233267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
638333267325STejun Heo 			continue;
638433267325STejun Heo 
638533267325STejun Heo 		nr_matches++;
638633267325STejun Heo 		match_fp = fp;
638733267325STejun Heo 
638833267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
638933267325STejun Heo 			nr_matches = 1;
639033267325STejun Heo 			break;
639133267325STejun Heo 		}
639233267325STejun Heo 	}
639333267325STejun Heo 
639433267325STejun Heo 	if (!nr_matches) {
639533267325STejun Heo 		*reason = "unknown value";
639633267325STejun Heo 		return -EINVAL;
639733267325STejun Heo 	}
639833267325STejun Heo 	if (nr_matches > 1) {
639933267325STejun Heo 		*reason = "ambigious value";
640033267325STejun Heo 		return -EINVAL;
640133267325STejun Heo 	}
640233267325STejun Heo 
640333267325STejun Heo 	force_ent->param = *match_fp;
640433267325STejun Heo 
640533267325STejun Heo 	return 0;
640633267325STejun Heo }
640733267325STejun Heo 
640833267325STejun Heo static void __init ata_parse_force_param(void)
640933267325STejun Heo {
641033267325STejun Heo 	int idx = 0, size = 1;
641133267325STejun Heo 	int last_port = -1, last_device = -1;
641233267325STejun Heo 	char *p, *cur, *next;
641333267325STejun Heo 
641433267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
641533267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
641633267325STejun Heo 		if (*p == ',')
641733267325STejun Heo 			size++;
641833267325STejun Heo 
641933267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
642033267325STejun Heo 	if (!ata_force_tbl) {
642133267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
642233267325STejun Heo 		       "libata.force ignored\n");
642333267325STejun Heo 		return;
642433267325STejun Heo 	}
642533267325STejun Heo 
642633267325STejun Heo 	/* parse and populate the table */
642733267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
642833267325STejun Heo 		const char *reason = "";
642933267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
643033267325STejun Heo 
643133267325STejun Heo 		next = cur;
643233267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
643333267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
643433267325STejun Heo 			       "parameter \"%s\" (%s)\n",
643533267325STejun Heo 			       cur, reason);
643633267325STejun Heo 			continue;
643733267325STejun Heo 		}
643833267325STejun Heo 
643933267325STejun Heo 		if (te.port == -1) {
644033267325STejun Heo 			te.port = last_port;
644133267325STejun Heo 			te.device = last_device;
644233267325STejun Heo 		}
644333267325STejun Heo 
644433267325STejun Heo 		ata_force_tbl[idx++] = te;
644533267325STejun Heo 
644633267325STejun Heo 		last_port = te.port;
644733267325STejun Heo 		last_device = te.device;
644833267325STejun Heo 	}
644933267325STejun Heo 
645033267325STejun Heo 	ata_force_tbl_size = idx;
645133267325STejun Heo }
6452c6fd2807SJeff Garzik 
6453c6fd2807SJeff Garzik static int __init ata_init(void)
6454c6fd2807SJeff Garzik {
645533267325STejun Heo 	ata_parse_force_param();
645633267325STejun Heo 
6457c6fd2807SJeff Garzik 	ata_wq = create_workqueue("ata");
6458c6fd2807SJeff Garzik 	if (!ata_wq)
645949ea3b04SElias Oltmanns 		goto free_force_tbl;
6460c6fd2807SJeff Garzik 
6461c6fd2807SJeff Garzik 	ata_aux_wq = create_singlethread_workqueue("ata_aux");
646249ea3b04SElias Oltmanns 	if (!ata_aux_wq)
646349ea3b04SElias Oltmanns 		goto free_wq;
6464c6fd2807SJeff Garzik 
6465c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6466c6fd2807SJeff Garzik 	return 0;
646749ea3b04SElias Oltmanns 
646849ea3b04SElias Oltmanns free_wq:
646949ea3b04SElias Oltmanns 	destroy_workqueue(ata_wq);
647049ea3b04SElias Oltmanns free_force_tbl:
647149ea3b04SElias Oltmanns 	kfree(ata_force_tbl);
647249ea3b04SElias Oltmanns 	return -ENOMEM;
6473c6fd2807SJeff Garzik }
6474c6fd2807SJeff Garzik 
6475c6fd2807SJeff Garzik static void __exit ata_exit(void)
6476c6fd2807SJeff Garzik {
647733267325STejun Heo 	kfree(ata_force_tbl);
6478c6fd2807SJeff Garzik 	destroy_workqueue(ata_wq);
6479c6fd2807SJeff Garzik 	destroy_workqueue(ata_aux_wq);
6480c6fd2807SJeff Garzik }
6481c6fd2807SJeff Garzik 
6482a4625085SBrian King subsys_initcall(ata_init);
6483c6fd2807SJeff Garzik module_exit(ata_exit);
6484c6fd2807SJeff Garzik 
6485c6fd2807SJeff Garzik static unsigned long ratelimit_time;
6486c6fd2807SJeff Garzik static DEFINE_SPINLOCK(ata_ratelimit_lock);
6487c6fd2807SJeff Garzik 
6488c6fd2807SJeff Garzik int ata_ratelimit(void)
6489c6fd2807SJeff Garzik {
6490c6fd2807SJeff Garzik 	int rc;
6491c6fd2807SJeff Garzik 	unsigned long flags;
6492c6fd2807SJeff Garzik 
6493c6fd2807SJeff Garzik 	spin_lock_irqsave(&ata_ratelimit_lock, flags);
6494c6fd2807SJeff Garzik 
6495c6fd2807SJeff Garzik 	if (time_after(jiffies, ratelimit_time)) {
6496c6fd2807SJeff Garzik 		rc = 1;
6497c6fd2807SJeff Garzik 		ratelimit_time = jiffies + (HZ/5);
6498c6fd2807SJeff Garzik 	} else
6499c6fd2807SJeff Garzik 		rc = 0;
6500c6fd2807SJeff Garzik 
6501c6fd2807SJeff Garzik 	spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6502c6fd2807SJeff Garzik 
6503c6fd2807SJeff Garzik 	return rc;
6504c6fd2807SJeff Garzik }
6505c6fd2807SJeff Garzik 
6506c6fd2807SJeff Garzik /**
6507c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
6508c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6509c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6510c6fd2807SJeff Garzik  *	@val: Wait condition
6511341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6512341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6513c6fd2807SJeff Garzik  *
6514c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6515c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6516c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6517c6fd2807SJeff Garzik  *
6518c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6519c6fd2807SJeff Garzik  *
6520c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6521c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6522c6fd2807SJeff Garzik  *
6523c6fd2807SJeff Garzik  *	LOCKING:
6524c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6525c6fd2807SJeff Garzik  *
6526c6fd2807SJeff Garzik  *	RETURNS:
6527c6fd2807SJeff Garzik  *	The final register value.
6528c6fd2807SJeff Garzik  */
6529c6fd2807SJeff Garzik u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6530341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6531c6fd2807SJeff Garzik {
6532341c2c95STejun Heo 	unsigned long deadline;
6533c6fd2807SJeff Garzik 	u32 tmp;
6534c6fd2807SJeff Garzik 
6535c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6536c6fd2807SJeff Garzik 
6537c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6538c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6539c6fd2807SJeff Garzik 	 * eat away the timeout.
6540c6fd2807SJeff Garzik 	 */
6541341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6542c6fd2807SJeff Garzik 
6543341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6544341c2c95STejun Heo 		msleep(interval);
6545c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6546c6fd2807SJeff Garzik 	}
6547c6fd2807SJeff Garzik 
6548c6fd2807SJeff Garzik 	return tmp;
6549c6fd2807SJeff Garzik }
6550c6fd2807SJeff Garzik 
6551c6fd2807SJeff Garzik /*
6552c6fd2807SJeff Garzik  * Dummy port_ops
6553c6fd2807SJeff Garzik  */
6554c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6555c6fd2807SJeff Garzik {
6556c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6557c6fd2807SJeff Garzik }
6558c6fd2807SJeff Garzik 
6559182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6560182d7bbaSTejun Heo {
6561182d7bbaSTejun Heo 	/* truly dummy */
6562182d7bbaSTejun Heo }
6563182d7bbaSTejun Heo 
6564029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6565c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6566c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6567182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6568c6fd2807SJeff Garzik };
6569c6fd2807SJeff Garzik 
657021b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
657121b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
657221b0ad4fSTejun Heo };
657321b0ad4fSTejun Heo 
6574c6fd2807SJeff Garzik /*
6575c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
6576c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
6577c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
6578c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
6579c6fd2807SJeff Garzik  */
6580c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6581c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6582c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6583029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
6584029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
6585c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
658621b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
65871eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
65881eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
6589c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
6590cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
6591f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
6592f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6593b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
6594ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
6595f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
6596f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
65970529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
6598c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
6599c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
6600c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6601436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
6602c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6603c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
66046357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
66056357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
66066357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
66076357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
66086357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
66096357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
66106357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
6611c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_start);
661204351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
661331cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6614c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
6615c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_probe);
661610305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
6617c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
6618aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6619936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
6620936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
66210aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
6622cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
662357c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
6624203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
6625c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
6626c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
6627c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_disable);
6628c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
6629c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
6630c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
6631c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6632c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6633c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6634c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6635c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
6636c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
6637c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
6638c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6639936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
6640936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
66416ffa01d8STejun Heo #ifdef CONFIG_PM
6642cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
6643cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
66446ffa01d8STejun Heo #endif /* CONFIG_PM */
6645c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
6646c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
6647963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6648c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6649c6fd2807SJeff Garzik 
6650c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
66516357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6652c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
6653c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
6654a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6655c6fd2807SJeff Garzik 
6656c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6657c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
6658c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
66596ffa01d8STejun Heo #ifdef CONFIG_PM
6660c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6661c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6662c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6663c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
66646ffa01d8STejun Heo #endif /* CONFIG_PM */
6665c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6666c6fd2807SJeff Garzik 
6667b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6668b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6669b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6670cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
6671cbcdd875STejun Heo #ifdef CONFIG_PCI
6672cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6673cbcdd875STejun Heo #endif /* CONFIG_PCI */
6674c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6675dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
6676c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
6677c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
66787d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
6679c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6680c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6681c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6682c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
668310acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
6684c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
6685a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
6686be0d18dfSAlan Cox 
6687be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
6688be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
6689be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
6690c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
6691be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
6692