xref: /kernel/linux/linux-5.10/drivers/ide/ide-iops.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (C) 2000-2002	Andre Hedrick <andre@linux-ide.org>
48c2ecf20Sopenharmony_ci *  Copyright (C) 2003		Red Hat
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/types.h>
108c2ecf20Sopenharmony_ci#include <linux/string.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/timer.h>
138c2ecf20Sopenharmony_ci#include <linux/mm.h>
148c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
158c2ecf20Sopenharmony_ci#include <linux/major.h>
168c2ecf20Sopenharmony_ci#include <linux/errno.h>
178c2ecf20Sopenharmony_ci#include <linux/genhd.h>
188c2ecf20Sopenharmony_ci#include <linux/blkpg.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <linux/pci.h>
218c2ecf20Sopenharmony_ci#include <linux/delay.h>
228c2ecf20Sopenharmony_ci#include <linux/ide.h>
238c2ecf20Sopenharmony_ci#include <linux/bitops.h>
248c2ecf20Sopenharmony_ci#include <linux/nmi.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
278c2ecf20Sopenharmony_ci#include <asm/irq.h>
288c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
298c2ecf20Sopenharmony_ci#include <asm/io.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_civoid SELECT_MASK(ide_drive_t *drive, int mask)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	const struct ide_port_ops *port_ops = drive->hwif->port_ops;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	if (port_ops && port_ops->maskproc)
368c2ecf20Sopenharmony_ci		port_ops->maskproc(drive, mask);
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ciu8 ide_read_error(ide_drive_t *drive)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	struct ide_taskfile tf;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_ERROR);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	return tf.error;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ide_read_error);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_civoid ide_fix_driveid(u16 *id)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci#ifndef __LITTLE_ENDIAN
528c2ecf20Sopenharmony_ci# ifdef __BIG_ENDIAN
538c2ecf20Sopenharmony_ci	int i;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	for (i = 0; i < 256; i++)
568c2ecf20Sopenharmony_ci		id[i] = __le16_to_cpu(id[i]);
578c2ecf20Sopenharmony_ci# else
588c2ecf20Sopenharmony_ci#  error "Please fix <asm/byteorder.h>"
598c2ecf20Sopenharmony_ci# endif
608c2ecf20Sopenharmony_ci#endif
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/*
648c2ecf20Sopenharmony_ci * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
658c2ecf20Sopenharmony_ci * removing leading/trailing blanks and compressing internal blanks.
668c2ecf20Sopenharmony_ci * It is primarily used to tidy up the model name/number fields as
678c2ecf20Sopenharmony_ci * returned by the ATA_CMD_ID_ATA[PI] commands.
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_civoid ide_fixstring(u8 *s, const int bytecount, const int byteswap)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	u8 *p, *end = &s[bytecount & ~1]; /* bytecount must be even */
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	if (byteswap) {
758c2ecf20Sopenharmony_ci		/* convert from big-endian to host byte order */
768c2ecf20Sopenharmony_ci		for (p = s ; p != end ; p += 2)
778c2ecf20Sopenharmony_ci			be16_to_cpus((u16 *) p);
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* strip leading blanks */
818c2ecf20Sopenharmony_ci	p = s;
828c2ecf20Sopenharmony_ci	while (s != end && *s == ' ')
838c2ecf20Sopenharmony_ci		++s;
848c2ecf20Sopenharmony_ci	/* compress internal blanks and strip trailing blanks */
858c2ecf20Sopenharmony_ci	while (s != end && *s) {
868c2ecf20Sopenharmony_ci		if (*s++ != ' ' || (s != end && *s && *s != ' '))
878c2ecf20Sopenharmony_ci			*p++ = *(s-1);
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci	/* wipe out trailing garbage */
908c2ecf20Sopenharmony_ci	while (p != end)
918c2ecf20Sopenharmony_ci		*p++ = '\0';
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ide_fixstring);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/*
968c2ecf20Sopenharmony_ci * This routine busy-waits for the drive status to be not "busy".
978c2ecf20Sopenharmony_ci * It then checks the status for all of the "good" bits and none
988c2ecf20Sopenharmony_ci * of the "bad" bits, and if all is okay it returns 0.  All other
998c2ecf20Sopenharmony_ci * cases return error -- caller may then invoke ide_error().
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * This routine should get fixed to not hog the cpu during extra long waits..
1028c2ecf20Sopenharmony_ci * That could be done by busy-waiting for the first jiffy or two, and then
1038c2ecf20Sopenharmony_ci * setting a timer to wake up at half second intervals thereafter,
1048c2ecf20Sopenharmony_ci * until timeout is achieved, before timing out.
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_ciint __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad,
1078c2ecf20Sopenharmony_ci		    unsigned long timeout, u8 *rstat)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	ide_hwif_t *hwif = drive->hwif;
1108c2ecf20Sopenharmony_ci	const struct ide_tp_ops *tp_ops = hwif->tp_ops;
1118c2ecf20Sopenharmony_ci	unsigned long flags;
1128c2ecf20Sopenharmony_ci	bool irqs_threaded = force_irqthreads;
1138c2ecf20Sopenharmony_ci	int i;
1148c2ecf20Sopenharmony_ci	u8 stat;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	udelay(1);	/* spec allows drive 400ns to assert "BUSY" */
1178c2ecf20Sopenharmony_ci	stat = tp_ops->read_status(hwif);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (stat & ATA_BUSY) {
1208c2ecf20Sopenharmony_ci		if (!irqs_threaded) {
1218c2ecf20Sopenharmony_ci			local_save_flags(flags);
1228c2ecf20Sopenharmony_ci			local_irq_enable_in_hardirq();
1238c2ecf20Sopenharmony_ci		}
1248c2ecf20Sopenharmony_ci		timeout += jiffies;
1258c2ecf20Sopenharmony_ci		while ((stat = tp_ops->read_status(hwif)) & ATA_BUSY) {
1268c2ecf20Sopenharmony_ci			if (time_after(jiffies, timeout)) {
1278c2ecf20Sopenharmony_ci				/*
1288c2ecf20Sopenharmony_ci				 * One last read after the timeout in case
1298c2ecf20Sopenharmony_ci				 * heavy interrupt load made us not make any
1308c2ecf20Sopenharmony_ci				 * progress during the timeout..
1318c2ecf20Sopenharmony_ci				 */
1328c2ecf20Sopenharmony_ci				stat = tp_ops->read_status(hwif);
1338c2ecf20Sopenharmony_ci				if ((stat & ATA_BUSY) == 0)
1348c2ecf20Sopenharmony_ci					break;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci				if (!irqs_threaded)
1378c2ecf20Sopenharmony_ci					local_irq_restore(flags);
1388c2ecf20Sopenharmony_ci				*rstat = stat;
1398c2ecf20Sopenharmony_ci				return -EBUSY;
1408c2ecf20Sopenharmony_ci			}
1418c2ecf20Sopenharmony_ci		}
1428c2ecf20Sopenharmony_ci		if (!irqs_threaded)
1438c2ecf20Sopenharmony_ci			local_irq_restore(flags);
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci	/*
1468c2ecf20Sopenharmony_ci	 * Allow status to settle, then read it again.
1478c2ecf20Sopenharmony_ci	 * A few rare drives vastly violate the 400ns spec here,
1488c2ecf20Sopenharmony_ci	 * so we'll wait up to 10usec for a "good" status
1498c2ecf20Sopenharmony_ci	 * rather than expensively fail things immediately.
1508c2ecf20Sopenharmony_ci	 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
1518c2ecf20Sopenharmony_ci	 */
1528c2ecf20Sopenharmony_ci	for (i = 0; i < 10; i++) {
1538c2ecf20Sopenharmony_ci		udelay(1);
1548c2ecf20Sopenharmony_ci		stat = tp_ops->read_status(hwif);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci		if (OK_STAT(stat, good, bad)) {
1578c2ecf20Sopenharmony_ci			*rstat = stat;
1588c2ecf20Sopenharmony_ci			return 0;
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci	*rstat = stat;
1628c2ecf20Sopenharmony_ci	return -EFAULT;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/*
1668c2ecf20Sopenharmony_ci * In case of error returns error value after doing "*startstop = ide_error()".
1678c2ecf20Sopenharmony_ci * The caller should return the updated value of "startstop" in this case,
1688c2ecf20Sopenharmony_ci * "startstop" is unchanged when the function returns 0.
1698c2ecf20Sopenharmony_ci */
1708c2ecf20Sopenharmony_ciint ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good,
1718c2ecf20Sopenharmony_ci		  u8 bad, unsigned long timeout)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	int err;
1748c2ecf20Sopenharmony_ci	u8 stat;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	/* bail early if we've exceeded max_failures */
1778c2ecf20Sopenharmony_ci	if (drive->max_failures && (drive->failures > drive->max_failures)) {
1788c2ecf20Sopenharmony_ci		*startstop = ide_stopped;
1798c2ecf20Sopenharmony_ci		return 1;
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	err = __ide_wait_stat(drive, good, bad, timeout, &stat);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	if (err) {
1858c2ecf20Sopenharmony_ci		char *s = (err == -EBUSY) ? "status timeout" : "status error";
1868c2ecf20Sopenharmony_ci		*startstop = ide_error(drive, s, stat);
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	return err;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ide_wait_stat);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/**
1948c2ecf20Sopenharmony_ci *	ide_in_drive_list	-	look for drive in black/white list
1958c2ecf20Sopenharmony_ci *	@id: drive identifier
1968c2ecf20Sopenharmony_ci *	@table: list to inspect
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci *	Look for a drive in the blacklist and the whitelist tables
1998c2ecf20Sopenharmony_ci *	Returns 1 if the drive is found in the table.
2008c2ecf20Sopenharmony_ci */
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ciint ide_in_drive_list(u16 *id, const struct drive_list_entry *table)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	for ( ; table->id_model; table++)
2058c2ecf20Sopenharmony_ci		if ((!strcmp(table->id_model, (char *)&id[ATA_ID_PROD])) &&
2068c2ecf20Sopenharmony_ci		    (!table->id_firmware ||
2078c2ecf20Sopenharmony_ci		     strstr((char *)&id[ATA_ID_FW_REV], table->id_firmware)))
2088c2ecf20Sopenharmony_ci			return 1;
2098c2ecf20Sopenharmony_ci	return 0;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ide_in_drive_list);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/*
2148c2ecf20Sopenharmony_ci * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
2158c2ecf20Sopenharmony_ci * Some optical devices with the buggy firmwares have the same problem.
2168c2ecf20Sopenharmony_ci */
2178c2ecf20Sopenharmony_cistatic const struct drive_list_entry ivb_list[] = {
2188c2ecf20Sopenharmony_ci	{ "QUANTUM FIREBALLlct10 05"	, "A03.0900"	},
2198c2ecf20Sopenharmony_ci	{ "QUANTUM FIREBALLlct20 30"	, "APL.0900"	},
2208c2ecf20Sopenharmony_ci	{ "TSSTcorp CDDVDW SH-S202J"	, "SB00"	},
2218c2ecf20Sopenharmony_ci	{ "TSSTcorp CDDVDW SH-S202J"	, "SB01"	},
2228c2ecf20Sopenharmony_ci	{ "TSSTcorp CDDVDW SH-S202N"	, "SB00"	},
2238c2ecf20Sopenharmony_ci	{ "TSSTcorp CDDVDW SH-S202N"	, "SB01"	},
2248c2ecf20Sopenharmony_ci	{ "TSSTcorp CDDVDW SH-S202H"	, "SB00"	},
2258c2ecf20Sopenharmony_ci	{ "TSSTcorp CDDVDW SH-S202H"	, "SB01"	},
2268c2ecf20Sopenharmony_ci	{ "SAMSUNG SP0822N"		, "WA100-10"	},
2278c2ecf20Sopenharmony_ci	{ NULL				, NULL		}
2288c2ecf20Sopenharmony_ci};
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci/*
2318c2ecf20Sopenharmony_ci *  All hosts that use the 80c ribbon must use!
2328c2ecf20Sopenharmony_ci *  The name is derived from upper byte of word 93 and the 80c ribbon.
2338c2ecf20Sopenharmony_ci */
2348c2ecf20Sopenharmony_ciu8 eighty_ninty_three(ide_drive_t *drive)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	ide_hwif_t *hwif = drive->hwif;
2378c2ecf20Sopenharmony_ci	u16 *id = drive->id;
2388c2ecf20Sopenharmony_ci	int ivb = ide_in_drive_list(id, ivb_list);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	if (hwif->cbl == ATA_CBL_SATA || hwif->cbl == ATA_CBL_PATA40_SHORT)
2418c2ecf20Sopenharmony_ci		return 1;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	if (ivb)
2448c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
2458c2ecf20Sopenharmony_ci				  drive->name);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	if (ata_id_is_sata(id) && !ivb)
2488c2ecf20Sopenharmony_ci		return 1;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
2518c2ecf20Sopenharmony_ci		goto no_80w;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	/*
2548c2ecf20Sopenharmony_ci	 * FIXME:
2558c2ecf20Sopenharmony_ci	 * - change master/slave IDENTIFY order
2568c2ecf20Sopenharmony_ci	 * - force bit13 (80c cable present) check also for !ivb devices
2578c2ecf20Sopenharmony_ci	 *   (unless the slave device is pre-ATA3)
2588c2ecf20Sopenharmony_ci	 */
2598c2ecf20Sopenharmony_ci	if (id[ATA_ID_HW_CONFIG] & 0x4000)
2608c2ecf20Sopenharmony_ci		return 1;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	if (ivb) {
2638c2ecf20Sopenharmony_ci		const char *model = (char *)&id[ATA_ID_PROD];
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci		if (strstr(model, "TSSTcorp CDDVDW SH-S202")) {
2668c2ecf20Sopenharmony_ci			/*
2678c2ecf20Sopenharmony_ci			 * These ATAPI devices always report 80c cable
2688c2ecf20Sopenharmony_ci			 * so we have to depend on the host in this case.
2698c2ecf20Sopenharmony_ci			 */
2708c2ecf20Sopenharmony_ci			if (hwif->cbl == ATA_CBL_PATA80)
2718c2ecf20Sopenharmony_ci				return 1;
2728c2ecf20Sopenharmony_ci		} else {
2738c2ecf20Sopenharmony_ci			/* Depend on the device side cable detection. */
2748c2ecf20Sopenharmony_ci			if (id[ATA_ID_HW_CONFIG] & 0x2000)
2758c2ecf20Sopenharmony_ci				return 1;
2768c2ecf20Sopenharmony_ci		}
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_cino_80w:
2798c2ecf20Sopenharmony_ci	if (drive->dev_flags & IDE_DFLAG_UDMA33_WARNED)
2808c2ecf20Sopenharmony_ci		return 0;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
2838c2ecf20Sopenharmony_ci			    "limiting max speed to UDMA33\n",
2848c2ecf20Sopenharmony_ci			    drive->name,
2858c2ecf20Sopenharmony_ci			    hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	drive->dev_flags |= IDE_DFLAG_UDMA33_WARNED;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	return 0;
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic const char *nien_quirk_list[] = {
2938c2ecf20Sopenharmony_ci	"QUANTUM FIREBALLlct08 08",
2948c2ecf20Sopenharmony_ci	"QUANTUM FIREBALLP KA6.4",
2958c2ecf20Sopenharmony_ci	"QUANTUM FIREBALLP KA9.1",
2968c2ecf20Sopenharmony_ci	"QUANTUM FIREBALLP KX13.6",
2978c2ecf20Sopenharmony_ci	"QUANTUM FIREBALLP KX20.5",
2988c2ecf20Sopenharmony_ci	"QUANTUM FIREBALLP KX27.3",
2998c2ecf20Sopenharmony_ci	"QUANTUM FIREBALLP LM20.4",
3008c2ecf20Sopenharmony_ci	"QUANTUM FIREBALLP LM20.5",
3018c2ecf20Sopenharmony_ci	"FUJITSU MHZ2160BH G2",
3028c2ecf20Sopenharmony_ci	NULL
3038c2ecf20Sopenharmony_ci};
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_civoid ide_check_nien_quirk_list(ide_drive_t *drive)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	const char **list, *m = (char *)&drive->id[ATA_ID_PROD];
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	for (list = nien_quirk_list; *list != NULL; list++)
3108c2ecf20Sopenharmony_ci		if (strstr(m, *list) != NULL) {
3118c2ecf20Sopenharmony_ci			drive->dev_flags |= IDE_DFLAG_NIEN_QUIRK;
3128c2ecf20Sopenharmony_ci			return;
3138c2ecf20Sopenharmony_ci		}
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ciint ide_driveid_update(ide_drive_t *drive)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	u16 *id;
3198c2ecf20Sopenharmony_ci	int rc;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	id = kmalloc(SECTOR_SIZE, GFP_ATOMIC);
3228c2ecf20Sopenharmony_ci	if (id == NULL)
3238c2ecf20Sopenharmony_ci		return 0;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	SELECT_MASK(drive, 1);
3268c2ecf20Sopenharmony_ci	rc = ide_dev_read_id(drive, ATA_CMD_ID_ATA, id, 1);
3278c2ecf20Sopenharmony_ci	SELECT_MASK(drive, 0);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	if (rc)
3308c2ecf20Sopenharmony_ci		goto out_err;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	drive->id[ATA_ID_UDMA_MODES]  = id[ATA_ID_UDMA_MODES];
3338c2ecf20Sopenharmony_ci	drive->id[ATA_ID_MWDMA_MODES] = id[ATA_ID_MWDMA_MODES];
3348c2ecf20Sopenharmony_ci	drive->id[ATA_ID_SWDMA_MODES] = id[ATA_ID_SWDMA_MODES];
3358c2ecf20Sopenharmony_ci	drive->id[ATA_ID_CFA_MODES]   = id[ATA_ID_CFA_MODES];
3368c2ecf20Sopenharmony_ci	/* anything more ? */
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	kfree(id);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	return 1;
3418c2ecf20Sopenharmony_ciout_err:
3428c2ecf20Sopenharmony_ci	if (rc == 2)
3438c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: %s: bad status\n", drive->name, __func__);
3448c2ecf20Sopenharmony_ci	kfree(id);
3458c2ecf20Sopenharmony_ci	return 0;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ciint ide_config_drive_speed(ide_drive_t *drive, u8 speed)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	ide_hwif_t *hwif = drive->hwif;
3518c2ecf20Sopenharmony_ci	const struct ide_tp_ops *tp_ops = hwif->tp_ops;
3528c2ecf20Sopenharmony_ci	struct ide_taskfile tf;
3538c2ecf20Sopenharmony_ci	u16 *id = drive->id, i;
3548c2ecf20Sopenharmony_ci	int error = 0;
3558c2ecf20Sopenharmony_ci	u8 stat;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_IDEDMA
3588c2ecf20Sopenharmony_ci	if (hwif->dma_ops)	/* check if host supports DMA */
3598c2ecf20Sopenharmony_ci		hwif->dma_ops->dma_host_set(drive, 0);
3608c2ecf20Sopenharmony_ci#endif
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	/* Skip setting PIO flow-control modes on pre-EIDE drives */
3638c2ecf20Sopenharmony_ci	if ((speed & 0xf8) == XFER_PIO_0 && ata_id_has_iordy(drive->id) == 0)
3648c2ecf20Sopenharmony_ci		goto skip;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/*
3678c2ecf20Sopenharmony_ci	 * Don't use ide_wait_cmd here - it will
3688c2ecf20Sopenharmony_ci	 * attempt to set_geometry and recalibrate,
3698c2ecf20Sopenharmony_ci	 * but for some reason these don't work at
3708c2ecf20Sopenharmony_ci	 * this point (lost interrupt).
3718c2ecf20Sopenharmony_ci	 */
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	udelay(1);
3748c2ecf20Sopenharmony_ci	tp_ops->dev_select(drive);
3758c2ecf20Sopenharmony_ci	SELECT_MASK(drive, 1);
3768c2ecf20Sopenharmony_ci	udelay(1);
3778c2ecf20Sopenharmony_ci	tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	memset(&tf, 0, sizeof(tf));
3808c2ecf20Sopenharmony_ci	tf.feature = SETFEATURES_XFER;
3818c2ecf20Sopenharmony_ci	tf.nsect   = speed;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE | IDE_VALID_NSECT);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	tp_ops->exec_command(hwif, ATA_CMD_SET_FEATURES);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	if (drive->dev_flags & IDE_DFLAG_NIEN_QUIRK)
3888c2ecf20Sopenharmony_ci		tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	error = __ide_wait_stat(drive, drive->ready_stat,
3918c2ecf20Sopenharmony_ci				ATA_BUSY | ATA_DRQ | ATA_ERR,
3928c2ecf20Sopenharmony_ci				WAIT_CMD, &stat);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	SELECT_MASK(drive, 0);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	if (error) {
3978c2ecf20Sopenharmony_ci		(void) ide_dump_status(drive, "set_drive_speed_status", stat);
3988c2ecf20Sopenharmony_ci		return error;
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	if (speed >= XFER_SW_DMA_0) {
4028c2ecf20Sopenharmony_ci		id[ATA_ID_UDMA_MODES]  &= ~0xFF00;
4038c2ecf20Sopenharmony_ci		id[ATA_ID_MWDMA_MODES] &= ~0x0700;
4048c2ecf20Sopenharmony_ci		id[ATA_ID_SWDMA_MODES] &= ~0x0700;
4058c2ecf20Sopenharmony_ci		if (ata_id_is_cfa(id))
4068c2ecf20Sopenharmony_ci			id[ATA_ID_CFA_MODES] &= ~0x0E00;
4078c2ecf20Sopenharmony_ci	} else	if (ata_id_is_cfa(id))
4088c2ecf20Sopenharmony_ci		id[ATA_ID_CFA_MODES] &= ~0x01C0;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci skip:
4118c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_IDEDMA
4128c2ecf20Sopenharmony_ci	if (speed >= XFER_SW_DMA_0 && (drive->dev_flags & IDE_DFLAG_USING_DMA))
4138c2ecf20Sopenharmony_ci		hwif->dma_ops->dma_host_set(drive, 1);
4148c2ecf20Sopenharmony_ci	else if (hwif->dma_ops)	/* check if host supports DMA */
4158c2ecf20Sopenharmony_ci		ide_dma_off_quietly(drive);
4168c2ecf20Sopenharmony_ci#endif
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	if (speed >= XFER_UDMA_0) {
4198c2ecf20Sopenharmony_ci		i = 1 << (speed - XFER_UDMA_0);
4208c2ecf20Sopenharmony_ci		id[ATA_ID_UDMA_MODES] |= (i << 8 | i);
4218c2ecf20Sopenharmony_ci	} else if (ata_id_is_cfa(id) && speed >= XFER_MW_DMA_3) {
4228c2ecf20Sopenharmony_ci		i = speed - XFER_MW_DMA_2;
4238c2ecf20Sopenharmony_ci		id[ATA_ID_CFA_MODES] |= i << 9;
4248c2ecf20Sopenharmony_ci	} else if (speed >= XFER_MW_DMA_0) {
4258c2ecf20Sopenharmony_ci		i = 1 << (speed - XFER_MW_DMA_0);
4268c2ecf20Sopenharmony_ci		id[ATA_ID_MWDMA_MODES] |= (i << 8 | i);
4278c2ecf20Sopenharmony_ci	} else if (speed >= XFER_SW_DMA_0) {
4288c2ecf20Sopenharmony_ci		i = 1 << (speed - XFER_SW_DMA_0);
4298c2ecf20Sopenharmony_ci		id[ATA_ID_SWDMA_MODES] |= (i << 8 | i);
4308c2ecf20Sopenharmony_ci	} else if (ata_id_is_cfa(id) && speed >= XFER_PIO_5) {
4318c2ecf20Sopenharmony_ci		i = speed - XFER_PIO_4;
4328c2ecf20Sopenharmony_ci		id[ATA_ID_CFA_MODES] |= i << 6;
4338c2ecf20Sopenharmony_ci	}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	if (!drive->init_speed)
4368c2ecf20Sopenharmony_ci		drive->init_speed = speed;
4378c2ecf20Sopenharmony_ci	drive->current_speed = speed;
4388c2ecf20Sopenharmony_ci	return error;
4398c2ecf20Sopenharmony_ci}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci/*
4428c2ecf20Sopenharmony_ci * This should get invoked any time we exit the driver to
4438c2ecf20Sopenharmony_ci * wait for an interrupt response from a drive.  handler() points
4448c2ecf20Sopenharmony_ci * at the appropriate code to handle the next interrupt, and a
4458c2ecf20Sopenharmony_ci * timer is started to prevent us from waiting forever in case
4468c2ecf20Sopenharmony_ci * something goes wrong (see the ide_timer_expiry() handler later on).
4478c2ecf20Sopenharmony_ci *
4488c2ecf20Sopenharmony_ci * See also ide_execute_command
4498c2ecf20Sopenharmony_ci */
4508c2ecf20Sopenharmony_civoid __ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
4518c2ecf20Sopenharmony_ci		       unsigned int timeout)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	ide_hwif_t *hwif = drive->hwif;
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	BUG_ON(hwif->handler);
4568c2ecf20Sopenharmony_ci	hwif->handler		= handler;
4578c2ecf20Sopenharmony_ci	hwif->timer.expires	= jiffies + timeout;
4588c2ecf20Sopenharmony_ci	hwif->req_gen_timer	= hwif->req_gen;
4598c2ecf20Sopenharmony_ci	add_timer(&hwif->timer);
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_civoid ide_set_handler(ide_drive_t *drive, ide_handler_t *handler,
4638c2ecf20Sopenharmony_ci		     unsigned int timeout)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	ide_hwif_t *hwif = drive->hwif;
4668c2ecf20Sopenharmony_ci	unsigned long flags;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hwif->lock, flags);
4698c2ecf20Sopenharmony_ci	__ide_set_handler(drive, handler, timeout);
4708c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hwif->lock, flags);
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ide_set_handler);
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci/**
4758c2ecf20Sopenharmony_ci *	ide_execute_command	-	execute an IDE command
4768c2ecf20Sopenharmony_ci *	@drive: IDE drive to issue the command against
4778c2ecf20Sopenharmony_ci *	@cmd: command
4788c2ecf20Sopenharmony_ci *	@handler: handler for next phase
4798c2ecf20Sopenharmony_ci *	@timeout: timeout for command
4808c2ecf20Sopenharmony_ci *
4818c2ecf20Sopenharmony_ci *	Helper function to issue an IDE command. This handles the
4828c2ecf20Sopenharmony_ci *	atomicity requirements, command timing and ensures that the
4838c2ecf20Sopenharmony_ci *	handler and IRQ setup do not race. All IDE command kick off
4848c2ecf20Sopenharmony_ci *	should go via this function or do equivalent locking.
4858c2ecf20Sopenharmony_ci */
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_civoid ide_execute_command(ide_drive_t *drive, struct ide_cmd *cmd,
4888c2ecf20Sopenharmony_ci			 ide_handler_t *handler, unsigned timeout)
4898c2ecf20Sopenharmony_ci{
4908c2ecf20Sopenharmony_ci	ide_hwif_t *hwif = drive->hwif;
4918c2ecf20Sopenharmony_ci	unsigned long flags;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hwif->lock, flags);
4948c2ecf20Sopenharmony_ci	if ((cmd->protocol != ATAPI_PROT_DMA &&
4958c2ecf20Sopenharmony_ci	     cmd->protocol != ATAPI_PROT_PIO) ||
4968c2ecf20Sopenharmony_ci	    (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT))
4978c2ecf20Sopenharmony_ci		__ide_set_handler(drive, handler, timeout);
4988c2ecf20Sopenharmony_ci	hwif->tp_ops->exec_command(hwif, cmd->tf.command);
4998c2ecf20Sopenharmony_ci	/*
5008c2ecf20Sopenharmony_ci	 * Drive takes 400nS to respond, we must avoid the IRQ being
5018c2ecf20Sopenharmony_ci	 * serviced before that.
5028c2ecf20Sopenharmony_ci	 *
5038c2ecf20Sopenharmony_ci	 * FIXME: we could skip this delay with care on non shared devices
5048c2ecf20Sopenharmony_ci	 */
5058c2ecf20Sopenharmony_ci	ndelay(400);
5068c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hwif->lock, flags);
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci/*
5108c2ecf20Sopenharmony_ci * ide_wait_not_busy() waits for the currently selected device on the hwif
5118c2ecf20Sopenharmony_ci * to report a non-busy status, see comments in ide_probe_port().
5128c2ecf20Sopenharmony_ci */
5138c2ecf20Sopenharmony_ciint ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
5148c2ecf20Sopenharmony_ci{
5158c2ecf20Sopenharmony_ci	u8 stat = 0;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	while (timeout--) {
5188c2ecf20Sopenharmony_ci		/*
5198c2ecf20Sopenharmony_ci		 * Turn this into a schedule() sleep once I'm sure
5208c2ecf20Sopenharmony_ci		 * about locking issues (2.5 work ?).
5218c2ecf20Sopenharmony_ci		 */
5228c2ecf20Sopenharmony_ci		mdelay(1);
5238c2ecf20Sopenharmony_ci		stat = hwif->tp_ops->read_status(hwif);
5248c2ecf20Sopenharmony_ci		if ((stat & ATA_BUSY) == 0)
5258c2ecf20Sopenharmony_ci			return 0;
5268c2ecf20Sopenharmony_ci		/*
5278c2ecf20Sopenharmony_ci		 * Assume a value of 0xff means nothing is connected to
5288c2ecf20Sopenharmony_ci		 * the interface and it doesn't implement the pull-down
5298c2ecf20Sopenharmony_ci		 * resistor on D7.
5308c2ecf20Sopenharmony_ci		 */
5318c2ecf20Sopenharmony_ci		if (stat == 0xff)
5328c2ecf20Sopenharmony_ci			return -ENODEV;
5338c2ecf20Sopenharmony_ci		touch_nmi_watchdog();
5348c2ecf20Sopenharmony_ci	}
5358c2ecf20Sopenharmony_ci	return -EBUSY;
5368c2ecf20Sopenharmony_ci}
537