libata-scsi.c 105 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2 3
 *  libata-scsi.c - helper library for ATA
 *
4
 *  Maintained by:  Tejun Heo <tj@kernel.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *    		    Please ALWAYS copy linux-ide@vger.kernel.org
 *		    on emails.
 *
 *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
 *  Copyright 2003-2004 Jeff Garzik
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 *  libata documentation is available via 'make {ps|pdf}docs',
 *  as Documentation/DocBook/libata.*
 *
 *  Hardware documentation available from
 *  - http://www.t10.org/
 *  - http://www.t13.org/
 *
Linus Torvalds's avatar
Linus Torvalds committed
34 35
 */

36
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39
#include <linux/kernel.h>
#include <linux/blkdev.h>
#include <linux/spinlock.h>
40
#include <linux/export.h>
Linus Torvalds's avatar
Linus Torvalds committed
41 42
#include <scsi/scsi.h>
#include <scsi/scsi_host.h>
43
#include <scsi/scsi_cmnd.h>
44
#include <scsi/scsi_eh.h>
45
#include <scsi/scsi_device.h>
46
#include <scsi/scsi_tcq.h>
47
#include <scsi/scsi_transport.h>
Linus Torvalds's avatar
Linus Torvalds committed
48
#include <linux/libata.h>
49
#include <linux/hdreg.h>
50
#include <linux/uaccess.h>
51
#include <linux/suspend.h>
52
#include <linux/pm_qos.h>
53
#include <asm/unaligned.h>
Linus Torvalds's avatar
Linus Torvalds committed
54 55

#include "libata.h"
56
#include "libata-transport.h"
Linus Torvalds's avatar
Linus Torvalds committed
57

58 59 60 61
#define ATA_SCSI_RBUF_SIZE	4096

static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
62

63
typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
64

65
static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
66
					const struct scsi_device *scsidev);
67
static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
68
					    const struct scsi_device *scsidev);
Linus Torvalds's avatar
Linus Torvalds committed
69

70 71 72 73 74 75 76 77 78 79
#define RW_RECOVERY_MPAGE 0x1
#define RW_RECOVERY_MPAGE_LEN 12
#define CACHE_MPAGE 0x8
#define CACHE_MPAGE_LEN 20
#define CONTROL_MPAGE 0xa
#define CONTROL_MPAGE_LEN 12
#define ALL_MPAGES 0x3f
#define ALL_SUB_MPAGES 0xff


80
static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
81 82
	RW_RECOVERY_MPAGE,
	RW_RECOVERY_MPAGE_LEN - 2,
83
	(1 << 7),	/* AWRE */
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	0,		/* read retry count */
	0, 0, 0, 0,
	0,		/* write retry count */
	0, 0, 0
};

static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
	CACHE_MPAGE,
	CACHE_MPAGE_LEN - 2,
	0,		/* contains WCE, needs to be 0 for logic */
	0, 0, 0, 0, 0, 0, 0, 0, 0,
	0,		/* contains DRA, needs to be 0 for logic */
	0, 0, 0, 0, 0, 0, 0
};

static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
	CONTROL_MPAGE,
	CONTROL_MPAGE_LEN - 2,
	2,	/* DSENSE=0, GLTSD=1 */
	0,	/* [QAM+QERR may be 1, see 05-359r1] */
	0, 0, 0, 0, 0xff, 0xff,
	0, 30	/* extended self test time, see 05-359r1 */
};

108 109 110 111 112
static const char *ata_lpm_policy_names[] = {
	[ATA_LPM_UNKNOWN]	= "max_performance",
	[ATA_LPM_MAX_POWER]	= "max_performance",
	[ATA_LPM_MED_POWER]	= "medium_power",
	[ATA_LPM_MIN_POWER]	= "min_power",
113 114
};

115
static ssize_t ata_scsi_lpm_store(struct device *device,
116 117
				  struct device_attribute *attr,
				  const char *buf, size_t count)
118
{
119
	struct Scsi_Host *shost = class_to_shost(device);
120
	struct ata_port *ap = ata_shost_to_port(shost);
121 122
	struct ata_link *link;
	struct ata_device *dev;
123
	enum ata_lpm_policy policy;
124
	unsigned long flags;
125 126 127 128 129 130 131

	/* UNKNOWN is internal state, iterate from MAX_POWER */
	for (policy = ATA_LPM_MAX_POWER;
	     policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
		const char *name = ata_lpm_policy_names[policy];

		if (strncmp(name, buf, strlen(name)) == 0)
132 133
			break;
	}
134
	if (policy == ARRAY_SIZE(ata_lpm_policy_names))
135 136
		return -EINVAL;

137
	spin_lock_irqsave(ap->lock, flags);
138 139 140 141 142 143 144 145 146 147

	ata_for_each_link(link, ap, EDGE) {
		ata_for_each_dev(dev, &ap->link, ENABLED) {
			if (dev->horkage & ATA_HORKAGE_NOLPM) {
				count = -EOPNOTSUPP;
				goto out_unlock;
			}
		}
	}

148 149
	ap->target_lpm_policy = policy;
	ata_port_schedule_eh(ap);
150
out_unlock:
151
	spin_unlock_irqrestore(ap->lock, flags);
152 153 154
	return count;
}

155 156
static ssize_t ata_scsi_lpm_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
157
{
158
	struct Scsi_Host *shost = class_to_shost(dev);
159 160
	struct ata_port *ap = ata_shost_to_port(shost);

161
	if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
162 163
		return -EINVAL;

164
	return snprintf(buf, PAGE_SIZE, "%s\n",
165
			ata_lpm_policy_names[ap->target_lpm_policy]);
166
}
167
DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
168
	    ata_scsi_lpm_show, ata_scsi_lpm_store);
169
EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
170

171 172 173 174 175 176 177
static ssize_t ata_scsi_park_show(struct device *device,
				  struct device_attribute *attr, char *buf)
{
	struct scsi_device *sdev = to_scsi_device(device);
	struct ata_port *ap;
	struct ata_link *link;
	struct ata_device *dev;
178
	unsigned long flags, now;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	unsigned int uninitialized_var(msecs);
	int rc = 0;

	ap = ata_shost_to_port(sdev->host);

	spin_lock_irqsave(ap->lock, flags);
	dev = ata_scsi_find_dev(ap, sdev);
	if (!dev) {
		rc = -ENODEV;
		goto unlock;
	}
	if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
		rc = -EOPNOTSUPP;
		goto unlock;
	}

	link = dev->link;
196
	now = jiffies;
197 198
	if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
	    link->eh_context.unloaded_mask & (1 << dev->devno) &&
199 200
	    time_after(dev->unpark_deadline, now))
		msecs = jiffies_to_msecs(dev->unpark_deadline - now);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	else
		msecs = 0;

unlock:
	spin_unlock_irq(ap->lock);

	return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
}

static ssize_t ata_scsi_park_store(struct device *device,
				   struct device_attribute *attr,
				   const char *buf, size_t len)
{
	struct scsi_device *sdev = to_scsi_device(device);
	struct ata_port *ap;
	struct ata_device *dev;
	long int input;
	unsigned long flags;
	int rc;

	rc = strict_strtol(buf, 10, &input);
	if (rc || input < -2)
		return -EINVAL;
	if (input > ATA_TMOUT_MAX_PARK) {
		rc = -EOVERFLOW;
		input = ATA_TMOUT_MAX_PARK;
	}

	ap = ata_shost_to_port(sdev->host);

	spin_lock_irqsave(ap->lock, flags);
	dev = ata_scsi_find_dev(ap, sdev);
	if (unlikely(!dev)) {
		rc = -ENODEV;
		goto unlock;
	}
	if (dev->class != ATA_DEV_ATA) {
		rc = -EOPNOTSUPP;
		goto unlock;
	}

	if (input >= 0) {
		if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
			rc = -EOPNOTSUPP;
			goto unlock;
		}

		dev->unpark_deadline = ata_deadline(jiffies, input);
		dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
		ata_port_schedule_eh(ap);
		complete(&ap->park_req_pending);
	} else {
		switch (input) {
		case -1:
			dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
			break;
		case -2:
			dev->flags |= ATA_DFLAG_NO_UNLOAD;
			break;
		}
	}
unlock:
	spin_unlock_irqrestore(ap->lock, flags);

	return rc ? rc : len;
}
DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
	    ata_scsi_park_show, ata_scsi_park_store);
EXPORT_SYMBOL_GPL(dev_attr_unload_heads);

271 272 273 274 275 276 277
static void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
{
	cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;

	scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq);
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
static ssize_t
ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
			  const char *buf, size_t count)
{
	struct Scsi_Host *shost = class_to_shost(dev);
	struct ata_port *ap = ata_shost_to_port(shost);
	if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
		return ap->ops->em_store(ap, buf, count);
	return -EINVAL;
}

static ssize_t
ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
	struct Scsi_Host *shost = class_to_shost(dev);
	struct ata_port *ap = ata_shost_to_port(shost);

	if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
		return ap->ops->em_show(ap, buf);
	return -EINVAL;
}
300
DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		ata_scsi_em_message_show, ata_scsi_em_message_store);
EXPORT_SYMBOL_GPL(dev_attr_em_message);

static ssize_t
ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
			      char *buf)
{
	struct Scsi_Host *shost = class_to_shost(dev);
	struct ata_port *ap = ata_shost_to_port(shost);

	return snprintf(buf, 23, "%d\n", ap->em_message_type);
}
DEVICE_ATTR(em_message_type, S_IRUGO,
		  ata_scsi_em_message_type_show, NULL);
EXPORT_SYMBOL_GPL(dev_attr_em_message_type);

static ssize_t
ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct ata_port *ap = ata_shost_to_port(sdev->host);
	struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);

325 326
	if (atadev && ap->ops->sw_activity_show &&
	    (ap->flags & ATA_FLAG_SW_ACTIVITY))
327 328 329 330 331 332 333 334 335 336 337 338 339 340
		return ap->ops->sw_activity_show(atadev, buf);
	return -EINVAL;
}

static ssize_t
ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
	const char *buf, size_t count)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct ata_port *ap = ata_shost_to_port(sdev->host);
	struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
	enum sw_activity val;
	int rc;

341 342
	if (atadev && ap->ops->sw_activity_store &&
	    (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
343 344 345 346 347 348 349 350 351 352 353 354
		val = simple_strtoul(buf, NULL, 0);
		switch (val) {
		case OFF: case BLINK_ON: case BLINK_OFF:
			rc = ap->ops->sw_activity_store(atadev, val);
			if (!rc)
				return count;
			else
				return rc;
		}
	}
	return -EINVAL;
}
355
DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
356 357 358
			ata_scsi_activity_store);
EXPORT_SYMBOL_GPL(dev_attr_sw_activity);

359 360 361 362 363 364
struct device_attribute *ata_common_sdev_attrs[] = {
	&dev_attr_unload_heads,
	NULL
};
EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);

365
static void ata_scsi_invalid_field(struct scsi_cmnd *cmd)
366 367 368
{
	ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
	/* "Invalid field in cbd" */
369
	cmd->scsi_done(cmd);
370 371
}

Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
/**
 *	ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
 *	@sdev: SCSI device for which BIOS geometry is to be determined
 *	@bdev: block device associated with @sdev
 *	@capacity: capacity of SCSI device
 *	@geom: location to which geometry will be output
 *
 *	Generic bios head/sector/cylinder calculator
 *	used by sd. Most BIOSes nowadays expect a XXX/255/16  (CHS)
 *	mapping. Some situations may arise where the disk is not
 *	bootable if this is not used.
 *
 *	LOCKING:
 *	Defined by the SCSI layer.  We don't really care.
 *
 *	RETURNS:
 *	Zero.
 */
int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
		       sector_t capacity, int geom[])
{
	geom[0] = 255;
	geom[1] = 63;
	sector_div(capacity, 255*63);
	geom[2] = capacity;

	return 0;
}

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
/**
 *	ata_scsi_unlock_native_capacity - unlock native capacity
 *	@sdev: SCSI device to adjust device capacity for
 *
 *	This function is called if a partition on @sdev extends beyond
 *	the end of the device.  It requests EH to unlock HPA.
 *
 *	LOCKING:
 *	Defined by the SCSI layer.  Might sleep.
 */
void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
{
	struct ata_port *ap = ata_shost_to_port(sdev->host);
	struct ata_device *dev;
	unsigned long flags;

	spin_lock_irqsave(ap->lock, flags);

	dev = ata_scsi_find_dev(ap, sdev);
	if (dev && dev->n_sectors < dev->n_native_sectors) {
		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
		dev->link->eh_info.action |= ATA_EH_RESET;
		ata_port_schedule_eh(ap);
	}

	spin_unlock_irqrestore(ap->lock, flags);
	ata_port_wait_eh(ap);
}

430 431
/**
 *	ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
432
 *	@ap: target port
433 434 435 436 437 438 439 440 441
 *	@sdev: SCSI device to get identify data for
 *	@arg: User buffer area for identify data
 *
 *	LOCKING:
 *	Defined by the SCSI layer.  We don't really care.
 *
 *	RETURNS:
 *	Zero on success, negative errno on error.
 */
442 443
static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
			    void __user *arg)
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
{
	struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
	u16 __user *dst = arg;
	char buf[40];

	if (!dev)
		return -ENOMSG;

	if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
		return -EFAULT;

	ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
	if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
		return -EFAULT;

	ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
	if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
		return -EFAULT;

	ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
	if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
		return -EFAULT;

	return 0;
}

470 471
/**
 *	ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
472
 *	@scsidev: Device to which we are issuing command
473 474 475 476 477 478 479 480 481 482 483 484
 *	@arg: User provided data for issuing command
 *
 *	LOCKING:
 *	Defined by the SCSI layer.  We don't really care.
 *
 *	RETURNS:
 *	Zero on success, negative errno on error.
 */
int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
{
	int rc = 0;
	u8 scsi_cmd[MAX_COMMAND_SIZE];
485
	u8 args[4], *argbuf = NULL, *sensebuf = NULL;
486
	int argsize = 0;
487
	enum dma_data_direction data_dir;
488
	int cmd_result;
489

490
	if (arg == NULL)
491 492 493 494 495
		return -EINVAL;

	if (copy_from_user(args, arg, sizeof(args)))
		return -EFAULT;

496 497 498 499
	sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
	if (!sensebuf)
		return -ENOMEM;

500 501 502
	memset(scsi_cmd, 0, sizeof(scsi_cmd));

	if (args[3]) {
503
		argsize = ATA_SECT_SIZE * args[3];
504
		argbuf = kmalloc(argsize, GFP_KERNEL);
505 506 507 508
		if (argbuf == NULL) {
			rc = -ENOMEM;
			goto error;
		}
509 510 511

		scsi_cmd[1]  = (4 << 1); /* PIO Data-in */
		scsi_cmd[2]  = 0x0e;     /* no off.line or cc, read from dev,
512
					    block count in sector count field */
513
		data_dir = DMA_FROM_DEVICE;
514 515
	} else {
		scsi_cmd[1]  = (3 << 1); /* Non-data */
516
		scsi_cmd[2]  = 0x20;     /* cc but no off.line or data xfer */
517
		data_dir = DMA_NONE;
518 519 520 521 522
	}

	scsi_cmd[0] = ATA_16;

	scsi_cmd[4] = args[2];
523
	if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
524 525 526 527 528 529 530 531 532 533 534
		scsi_cmd[6]  = args[3];
		scsi_cmd[8]  = args[1];
		scsi_cmd[10] = 0x4f;
		scsi_cmd[12] = 0xc2;
	} else {
		scsi_cmd[6]  = args[1];
	}
	scsi_cmd[14] = args[0];

	/* Good values for timeout and retries?  Values below
	   from scsi_ioctl_send_command() for default case... */
535
	cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
536
				  sensebuf, (10*HZ), 5, 0, NULL);
537 538 539 540 541 542 543 544 545 546

	if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
		u8 *desc = sensebuf + 8;
		cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */

		/* If we set cc then ATA pass-through will cause a
		 * check condition even if no error. Filter that. */
		if (cmd_result & SAM_STAT_CHECK_CONDITION) {
			struct scsi_sense_hdr sshdr;
			scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
547
					     &sshdr);
548 549
			if (sshdr.sense_key == RECOVERED_ERROR &&
			    sshdr.asc == 0 && sshdr.ascq == 0x1d)
550 551 552 553
				cmd_result &= ~SAM_STAT_CHECK_CONDITION;
		}

		/* Send userspace a few ATA registers (same as drivers/ide) */
554 555 556 557 558
		if (sensebuf[0] == 0x72 &&	/* format is "descriptor" */
		    desc[0] == 0x09) {		/* code is "ATA Descriptor" */
			args[0] = desc[13];	/* status */
			args[1] = desc[3];	/* error */
			args[2] = desc[5];	/* sector count (0:7) */
559 560 561 562 563 564 565
			if (copy_to_user(arg, args, sizeof(args)))
				rc = -EFAULT;
		}
	}


	if (cmd_result) {
566 567 568 569 570
		rc = -EIO;
		goto error;
	}

	if ((argbuf)
571
	 && copy_to_user(arg + sizeof(args), argbuf, argsize))
572 573
		rc = -EFAULT;
error:
574
	kfree(sensebuf);
575
	kfree(argbuf);
576 577 578 579 580
	return rc;
}

/**
 *	ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
581
 *	@scsidev: Device to which we are issuing command
582 583 584 585 586 587 588 589 590 591 592 593
 *	@arg: User provided data for issuing command
 *
 *	LOCKING:
 *	Defined by the SCSI layer.  We don't really care.
 *
 *	RETURNS:
 *	Zero on success, negative errno on error.
 */
int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
{
	int rc = 0;
	u8 scsi_cmd[MAX_COMMAND_SIZE];
594 595
	u8 args[7], *sensebuf = NULL;
	int cmd_result;
596

597
	if (arg == NULL)
598 599 600 601 602
		return -EINVAL;

	if (copy_from_user(args, arg, sizeof(args)))
		return -EFAULT;

603 604 605 606
	sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
	if (!sensebuf)
		return -ENOMEM;

607 608 609
	memset(scsi_cmd, 0, sizeof(scsi_cmd));
	scsi_cmd[0]  = ATA_16;
	scsi_cmd[1]  = (3 << 1); /* Non-data */
610
	scsi_cmd[2]  = 0x20;     /* cc but no off.line or data xfer */
611 612 613 614 615
	scsi_cmd[4]  = args[1];
	scsi_cmd[6]  = args[2];
	scsi_cmd[8]  = args[3];
	scsi_cmd[10] = args[4];
	scsi_cmd[12] = args[5];
616
	scsi_cmd[13] = args[6] & 0x4f;
617 618 619
	scsi_cmd[14] = args[0];

	/* Good values for timeout and retries?  Values below
620
	   from scsi_ioctl_send_command() for default case... */
621
	cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
622
				sensebuf, (10*HZ), 5, 0, NULL);
623 624 625 626 627 628 629 630 631 632 633

	if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
		u8 *desc = sensebuf + 8;
		cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */

		/* If we set cc then ATA pass-through will cause a
		 * check condition even if no error. Filter that. */
		if (cmd_result & SAM_STAT_CHECK_CONDITION) {
			struct scsi_sense_hdr sshdr;
			scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
						&sshdr);
634 635
			if (sshdr.sense_key == RECOVERED_ERROR &&
			    sshdr.asc == 0 && sshdr.ascq == 0x1d)
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
				cmd_result &= ~SAM_STAT_CHECK_CONDITION;
		}

		/* Send userspace ATA registers */
		if (sensebuf[0] == 0x72 &&	/* format is "descriptor" */
				desc[0] == 0x09) {/* code is "ATA Descriptor" */
			args[0] = desc[13];	/* status */
			args[1] = desc[3];	/* error */
			args[2] = desc[5];	/* sector count (0:7) */
			args[3] = desc[7];	/* lbal */
			args[4] = desc[9];	/* lbam */
			args[5] = desc[11];	/* lbah */
			args[6] = desc[12];	/* select */
			if (copy_to_user(arg, args, sizeof(args)))
				rc = -EFAULT;
		}
	}

	if (cmd_result) {
655
		rc = -EIO;
656 657
		goto error;
	}
658

659 660
 error:
	kfree(sensebuf);
661 662 663
	return rc;
}

664 665 666 667 668 669 670 671 672
static int ata_ioc32(struct ata_port *ap)
{
	if (ap->flags & ATA_FLAG_PIO_DMA)
		return 1;
	if (ap->pflags & ATA_PFLAG_PIO32)
		return 1;
	return 0;
}

673 674
int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
		     int cmd, void __user *arg)
Linus Torvalds's avatar
Linus Torvalds committed
675 676
{
	int val = -EINVAL, rc = -EINVAL;
677
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
678 679 680

	switch (cmd) {
	case ATA_IOC_GET_IO32:
681 682 683
		spin_lock_irqsave(ap->lock, flags);
		val = ata_ioc32(ap);
		spin_unlock_irqrestore(ap->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
684 685 686 687 688 689
		if (copy_to_user(arg, &val, 1))
			return -EFAULT;
		return 0;

	case ATA_IOC_SET_IO32:
		val = (unsigned long) arg;
690 691 692 693 694 695 696 697 698 699 700 701 702
		rc = 0;
		spin_lock_irqsave(ap->lock, flags);
		if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
			if (val)
				ap->pflags |= ATA_PFLAG_PIO32;
			else
				ap->pflags &= ~ATA_PFLAG_PIO32;
		} else {
			if (val != ata_ioc32(ap))
				rc = -EINVAL;
		}
		spin_unlock_irqrestore(ap->lock, flags);
		return rc;
Linus Torvalds's avatar
Linus Torvalds committed
703

704
	case HDIO_GET_IDENTITY:
705
		return ata_get_identity(ap, scsidev, arg);
706

707 708 709 710 711 712 713 714 715 716
	case HDIO_DRIVE_CMD:
		if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
			return -EACCES;
		return ata_cmd_ioctl(scsidev, arg);

	case HDIO_DRIVE_TASK:
		if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
			return -EACCES;
		return ata_task_ioctl(scsidev, arg);

Linus Torvalds's avatar
Linus Torvalds committed
717 718 719 720 721 722 723
	default:
		rc = -ENOTTY;
		break;
	}

	return rc;
}
724 725 726 727 728 729 730 731
EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);

int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
{
	return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
				scsidev, cmd, arg);
}
EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
Linus Torvalds's avatar
Linus Torvalds committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

/**
 *	ata_scsi_qc_new - acquire new ata_queued_cmd reference
 *	@dev: ATA device to which the new command is attached
 *	@cmd: SCSI command that originated this ATA command
 *
 *	Obtain a reference to an unused ata_queued_cmd structure,
 *	which is the basic libata structure representing a single
 *	ATA command sent to the hardware.
 *
 *	If a command was available, fill in the SCSI-specific
 *	portions of the structure with information on the
 *	current command.
 *
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
747
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
748 749 750 751
 *
 *	RETURNS:
 *	Command allocated, or %NULL if none available.
 */
752
static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
753
					      struct scsi_cmnd *cmd)
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756
{
	struct ata_queued_cmd *qc;

757
	qc = ata_qc_new_init(dev);
Linus Torvalds's avatar
Linus Torvalds committed
758 759
	if (qc) {
		qc->scsicmd = cmd;
760
		qc->scsidone = cmd->scsi_done;
Linus Torvalds's avatar
Linus Torvalds committed
761

762
		qc->sg = scsi_sglist(cmd);
763
		qc->n_elem = scsi_sg_count(cmd);
Linus Torvalds's avatar
Linus Torvalds committed
764 765
	} else {
		cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
766
		cmd->scsi_done(cmd);
Linus Torvalds's avatar
Linus Torvalds committed
767 768 769 770 771
	}

	return qc;
}

772 773 774 775 776 777 778 779
static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
{
	struct scsi_cmnd *scmd = qc->scsicmd;

	qc->extrabytes = scmd->request->extra_len;
	qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
}

780 781 782 783 784 785 786 787 788 789 790 791
/**
 *	ata_dump_status - user friendly display of error info
 *	@id: id of the port in question
 *	@tf: ptr to filled out taskfile
 *
 *	Decode and dump the ATA error/status registers for the user so
 *	that they have some idea what really happened at the non
 *	make-believe layer.
 *
 *	LOCKING:
 *	inherited from caller
 */
792
static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
{
	u8 stat = tf->command, err = tf->feature;

	printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
	if (stat & ATA_BUSY) {
		printk("Busy }\n");	/* Data is not valid in this case */
	} else {
		if (stat & 0x40)	printk("DriveReady ");
		if (stat & 0x20)	printk("DeviceFault ");
		if (stat & 0x10)	printk("SeekComplete ");
		if (stat & 0x08)	printk("DataRequest ");
		if (stat & 0x04)	printk("CorrectedError ");
		if (stat & 0x02)	printk("Index ");
		if (stat & 0x01)	printk("Error ");
		printk("}\n");

		if (err) {
			printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
			if (err & 0x04)		printk("DriveStatusError ");
			if (err & 0x80) {
				if (err & 0x04)	printk("BadCRC ");
				else		printk("Sector ");
			}
			if (err & 0x40)		printk("UncorrectableError ");
			if (err & 0x10)		printk("SectorIdNotFound ");
			if (err & 0x02)		printk("TrackZeroNotFound ");
			if (err & 0x01)		printk("AddrMarkNotFound ");
			printk("}\n");
		}
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
825 826
/**
 *	ata_to_sense_error - convert ATA error to SCSI error
827
 *	@id: ATA device number
Linus Torvalds's avatar
Linus Torvalds committed
828
 *	@drv_stat: value contained in ATA status register
829 830 831 832
 *	@drv_err: value contained in ATA error register
 *	@sk: the sense key we'll fill out
 *	@asc: the additional sense code we'll fill out
 *	@ascq: the additional sense code qualifier we'll fill out
833
 *	@verbose: be verbose
Linus Torvalds's avatar
Linus Torvalds committed
834
 *
835 836 837
 *	Converts an ATA error into a SCSI error.  Fill out pointers to
 *	SK, ASC, and ASCQ bytes for later use in fixed or descriptor
 *	format sense blocks.
Linus Torvalds's avatar
Linus Torvalds committed
838 839
 *
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
840
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
841
 */
842 843
static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
			       u8 *asc, u8 *ascq, int verbose)
Linus Torvalds's avatar
Linus Torvalds committed
844
{
845
	int i;
Jeff Garzik's avatar
Jeff Garzik committed
846

Linus Torvalds's avatar
Linus Torvalds committed
847
	/* Based on the 3ware driver translation table */
848
	static const unsigned char sense_table[][4] = {
Linus Torvalds's avatar
Linus Torvalds committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
		/* BBD|ECC|ID|MAR */
		{0xd1, 		ABORTED_COMMAND, 0x00, 0x00}, 	// Device busy                  Aborted command
		/* BBD|ECC|ID */
		{0xd0,  	ABORTED_COMMAND, 0x00, 0x00}, 	// Device busy                  Aborted command
		/* ECC|MC|MARK */
		{0x61, 		HARDWARE_ERROR, 0x00, 0x00}, 	// Device fault                 Hardware error
		/* ICRC|ABRT */		/* NB: ICRC & !ABRT is BBD */
		{0x84, 		ABORTED_COMMAND, 0x47, 0x00}, 	// Data CRC error               SCSI parity error
		/* MC|ID|ABRT|TRK0|MARK */
		{0x37, 		NOT_READY, 0x04, 0x00}, 	// Unit offline                 Not ready
		/* MCR|MARK */
		{0x09, 		NOT_READY, 0x04, 0x00}, 	// Unrecovered disk error       Not ready
		/*  Bad address mark */
		{0x01, 		MEDIUM_ERROR, 0x13, 0x00}, 	// Address mark not found       Address mark not found for data field
		/* TRK0 */
		{0x02, 		HARDWARE_ERROR, 0x00, 0x00}, 	// Track 0 not found		  Hardware error
		/* Abort & !ICRC */
		{0x04, 		ABORTED_COMMAND, 0x00, 0x00}, 	// Aborted command              Aborted command
		/* Media change request */
		{0x08, 		NOT_READY, 0x04, 0x00}, 	// Media change request	  FIXME: faking offline
		/* SRV */
		{0x10, 		ABORTED_COMMAND, 0x14, 0x00}, 	// ID not found                 Recorded entity not found
		/* Media change */
		{0x08,  	NOT_READY, 0x04, 0x00}, 	// Media change		  FIXME: faking offline
		/* ECC */
		{0x40, 		MEDIUM_ERROR, 0x11, 0x04}, 	// Uncorrectable ECC error      Unrecovered read error
		/* BBD - block marked bad */
		{0x80, 		MEDIUM_ERROR, 0x11, 0x04}, 	// Block marked bad		  Medium error, unrecovered read error
		{0xFF, 0xFF, 0xFF, 0xFF}, // END mark
	};
879
	static const unsigned char stat_table[][4] = {
Linus Torvalds's avatar
Linus Torvalds committed
880 881 882 883 884 885 886 887 888 889 890
		/* Must be first because BUSY means no other bits valid */
		{0x80, 		ABORTED_COMMAND, 0x47, 0x00},	// Busy, fake parity for now
		{0x20, 		HARDWARE_ERROR,  0x00, 0x00}, 	// Device fault
		{0x08, 		ABORTED_COMMAND, 0x47, 0x00},	// Timed out in xfer, fake parity for now
		{0x04, 		RECOVERED_ERROR, 0x11, 0x00},	// Recovered ECC error	  Medium error, recovered
		{0xFF, 0xFF, 0xFF, 0xFF}, // END mark
	};

	/*
	 *	Is this an error we can process/parse
	 */
891 892
	if (drv_stat & ATA_BUSY) {
		drv_err = 0;	/* Ignore the err bits, they're invalid */
Linus Torvalds's avatar
Linus Torvalds committed
893
	}
894 895 896 897 898

	if (drv_err) {
		/* Look for drv_err */
		for (i = 0; sense_table[i][0] != 0xFF; i++) {
			/* Look for best matches first */
899
			if ((sense_table[i][0] & drv_err) ==
900 901 902 903 904 905 906 907
			    sense_table[i][0]) {
				*sk = sense_table[i][1];
				*asc = sense_table[i][2];
				*ascq = sense_table[i][3];
				goto translate_done;
			}
		}
		/* No immediate match */
908 909 910
		if (verbose)
			printk(KERN_WARNING "ata%u: no sense translation for "
			       "error 0x%02x\n", id, drv_err);
Linus Torvalds's avatar
Linus Torvalds committed
911
	}
912 913 914 915 916 917 918 919

	/* Fall back to interpreting status bits */
	for (i = 0; stat_table[i][0] != 0xFF; i++) {
		if (stat_table[i][0] & drv_stat) {
			*sk = stat_table[i][1];
			*asc = stat_table[i][2];
			*ascq = stat_table[i][3];
			goto translate_done;
Linus Torvalds's avatar
Linus Torvalds committed
920
		}
921 922
	}
	/* No error?  Undecoded? */
923 924 925
	if (verbose)
		printk(KERN_WARNING "ata%u: no sense translation for "
		       "status: 0x%02x\n", id, drv_stat);
926

927 928 929 930 931
	/* We need a sensible error return here, which is tricky, and one
	   that won't cause people to do things like return a disk wrongly */
	*sk = ABORTED_COMMAND;
	*asc = 0x00;
	*ascq = 0x00;
932 933

 translate_done:
934 935 936 937
	if (verbose)
		printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
		       "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
		       id, drv_stat, drv_err, *sk, *asc, *ascq);
938 939 940 941
	return;
}

/*
942
 *	ata_gen_passthru_sense - Generate check condition sense block.
943 944 945 946 947 948
 *	@qc: Command that completed.
 *
 *	This function is specific to the ATA descriptor format sense
 *	block specified for the ATA pass through commands.  Regardless
 *	of whether the command errored or not, return a sense
 *	block. Copy all controller registers into the sense
949 950 951 952 953
 *	block. If there was no error, we get the request from an ATA
 *	passthrough command, so we use the following sense data:
 *	sk = RECOVERED ERROR
 *	asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
 *      
954 955
 *
 *	LOCKING:
956
 *	None.
957
 */
958
static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
959 960
{
	struct scsi_cmnd *cmd = qc->scsicmd;
961
	struct ata_taskfile *tf = &qc->result_tf;
962 963
	unsigned char *sb = cmd->sense_buffer;
	unsigned char *desc = sb + 8;
964
	int verbose = qc->ap->ops->error_handler == NULL;
965 966 967

	memset(sb, 0, SCSI_SENSE_BUFFERSIZE);

968
	cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
969 970 971 972 973

	/*
	 * Use ata_to_sense_error() to map status register bits
	 * onto sense key, asc & ascq.
	 */
974 975
	if (qc->err_mask ||
	    tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
976
		ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
977
				   &sb[1], &sb[2], &sb[3], verbose);
978
		sb[1] &= 0x0f;
979 980 981 982
	} else {
		sb[1] = RECOVERED_ERROR;
		sb[2] = 0;
		sb[3] = 0x1D;
Linus Torvalds's avatar
Linus Torvalds committed
983 984
	}

985 986 987 988
	/*
	 * Sense data is current and format is descriptor.
	 */
	sb[0] = 0x72;
Linus Torvalds's avatar
Linus Torvalds committed
989

990 991
	desc[0] = 0x09;

992 993 994
	/* set length of additional sense data */
	sb[7] = 14;
	desc[1] = 12;
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

	/*
	 * Copy registers into sense buffer.
	 */
	desc[2] = 0x00;
	desc[3] = tf->feature;	/* == error reg */
	desc[5] = tf->nsect;
	desc[7] = tf->lbal;
	desc[9] = tf->lbam;
	desc[11] = tf->lbah;
	desc[12] = tf->device;
	desc[13] = tf->command; /* == status reg */

	/*
	 * Fill in Extend bit, and the high order bytes
	 * if applicable.
	 */
	if (tf->flags & ATA_TFLAG_LBA48) {
		desc[2] |= 0x01;
		desc[4] = tf->hob_nsect;
		desc[6] = tf->hob_lbal;
		desc[8] = tf->hob_lbam;
		desc[10] = tf->hob_lbah;
Linus Torvalds's avatar
Linus Torvalds committed
1018
	}
1019
}
Linus Torvalds's avatar
Linus Torvalds committed
1020

1021
/**
1022
 *	ata_gen_ata_sense - generate a SCSI fixed sense block
1023 1024
 *	@qc: Command that we are erroring out
 *
1025
 *	Generate sense block for a failed ATA command @qc.  Descriptor
1026
 *	format is used to accommodate LBA48 block address.
1027 1028
 *
 *	LOCKING:
1029
 *	None.
1030
 */
1031
static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
1032
{
1033
	struct ata_device *dev = qc->dev;
1034
	struct scsi_cmnd *cmd = qc->scsicmd;
1035
	struct ata_taskfile *tf = &qc->result_tf;
1036
	unsigned char *sb = cmd->sense_buffer;
1037
	unsigned char *desc = sb + 8;
1038
	int verbose = qc->ap->ops->error_handler == NULL;
1039
	u64 block;
1040 1041 1042

	memset(sb, 0, SCSI_SENSE_BUFFERSIZE);

1043
	cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1044

1045 1046 1047 1048
	/* sense data is current and format is descriptor */
	sb[0] = 0x72;

	/* Use ata_to_sense_error() to map status register bits
1049 1050
	 * onto sense key, asc & ascq.
	 */
1051 1052
	if (qc->err_mask ||
	    tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
1053
		ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
1054 1055
				   &sb[1], &sb[2], &sb[3], verbose);
		sb[1] &= 0x0f;
Linus Torvalds's avatar
Linus Torvalds committed
1056 1057
	}

1058
	block = ata_tf_read_block(&qc->result_tf, dev);
1059

1060 1061 1062 1063
	/* information sense data descriptor */
	sb[7] = 12;
	desc[0] = 0x00;
	desc[1] = 10;
1064

1065 1066 1067 1068 1069 1070 1071
	desc[2] |= 0x80;	/* valid */
	desc[6] = block >> 40;
	desc[7] = block >> 32;
	desc[8] = block >> 24;
	desc[9] = block >> 16;
	desc[10] = block >> 8;
	desc[11] = block;
Linus Torvalds's avatar
Linus Torvalds committed
1072 1073
}

1074 1075 1076 1077
static void ata_scsi_sdev_config(struct scsi_device *sdev)
{
	sdev->use_10_for_rw = 1;
	sdev->use_10_for_ms = 1;
1078
	sdev->no_report_opcodes = 1;
1079
	sdev->no_write_same = 1;
1080 1081 1082 1083 1084 1085 1086

	/* Schedule policy is determined by ->qc_defer() callback and
	 * it needs to see every deferred qc.  Set dev_blocked to 1 to
	 * prevent SCSI midlayer from automatically deferring
	 * requests.
	 */
	sdev->max_device_blocked = 1;
1087 1088
}

1089 1090
/**
 *	atapi_drain_needed - Check whether data transfer may overflow
1091
 *	@rq: request to be checked
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
 *
 *	ATAPI commands which transfer variable length data to host
 *	might overflow due to application error or hardare bug.  This
 *	function checks whether overflow should be drained and ignored
 *	for @request.
 *
 *	LOCKING:
 *	None.
 *
 *	RETURNS:
 *	1 if ; otherwise, 0.
 */
static int atapi_drain_needed(struct request *rq)
{
1106
	if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
1107 1108
		return 0;

1109
	if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE))
1110 1111 1112 1113 1114 1115 1116
		return 0;

	return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
}

static int ata_scsi_dev_config(struct scsi_device *sdev,
			       struct ata_device *dev)
1117
{
1118 1119
	struct request_queue *q = sdev->request_queue;

1120 1121 1122
	if (!ata_id_has_unload(dev->id))
		dev->flags |= ATA_DFLAG_NO_UNLOAD;

1123
	/* configure max sectors */
1124
	blk_queue_max_hw_sectors(q, dev->max_sectors);
1125

1126 1127 1128
	if (dev->class == ATA_DEV_ATAPI) {
		void *buf;

1129 1130 1131
		sdev->sector_size = ATA_SECT_SIZE;

		/* set DMA padding */
1132
		blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
1133 1134 1135 1136

		/* configure draining */
		buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
		if (!buf) {
1137
			ata_dev_err(dev, "drain buffer allocation failed\n");
1138 1139 1140 1141 1142
			return -ENOMEM;
		}

		blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
	} else {
1143
		sdev->sector_size = ata_id_logical_sector_size(dev->id);
1144
		sdev->manage_start_stop = 1;
1145
	}
1146

1147 1148 1149 1150 1151 1152 1153 1154
	/*
	 * ata_pio_sectors() expects buffer for each sector to not cross
	 * page boundary.  Enforce it by requiring buffers to be sector
	 * aligned, which works iff sector_size is not larger than
	 * PAGE_SIZE.  ATAPI devices also need the alignment as
	 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
	 */
	if (sdev->sector_size > PAGE_SIZE)
1155
		ata_dev_warn(dev,
1156 1157 1158
			"sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
			sdev->sector_size);

1159
	blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
1160

1161 1162 1163
	if (dev->flags & ATA_DFLAG_AN)
		set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);

1164 1165 1166 1167 1168
	if (dev->flags & ATA_DFLAG_NCQ) {
		int depth;

		depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
		depth = min(ATA_MAX_QUEUE - 1, depth);
1169
		scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
1170
	}
1171

1172 1173
	blk_queue_flush_queueable(q, false);

1174
	dev->sdev = sdev;
1175
	return 0;
1176 1177
}

Linus Torvalds's avatar
Linus Torvalds committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
/**
 *	ata_scsi_slave_config - Set SCSI device attributes
 *	@sdev: SCSI device to examine
 *
 *	This is called before we actually start reading
 *	and writing to the device, to configure certain
 *	SCSI mid-layer behaviors.
 *
 *	LOCKING:
 *	Defined by SCSI layer.  We don't really care.
 */

int ata_scsi_slave_config(struct scsi_device *sdev)
{
1192 1193
	struct ata_port *ap = ata_shost_to_port(sdev->host);
	struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
1194
	int rc = 0;
1195

1196
	ata_scsi_sdev_config(sdev);
Linus Torvalds's avatar
Linus Torvalds committed
1197

1198
	if (dev)
1199
		rc = ata_scsi_dev_config(sdev, dev);
Linus Torvalds's avatar
Linus Torvalds committed
1200

1201
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
1202 1203
}

1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
/**
 *	ata_scsi_slave_destroy - SCSI device is about to be destroyed
 *	@sdev: SCSI device to be destroyed
 *
 *	@sdev is about to be destroyed for hot/warm unplugging.  If
 *	this unplugging was initiated by libata as indicated by NULL
 *	dev->sdev, this function doesn't have to do anything.
 *	Otherwise, SCSI layer initiated warm-unplug is in progress.
 *	Clear dev->sdev, schedule the device for ATA detach and invoke
 *	EH.
 *
 *	LOCKING:
 *	Defined by SCSI layer.  We don't really care.
 */
void ata_scsi_slave_destroy(struct scsi_device *sdev)
{
	struct ata_port *ap = ata_shost_to_port(sdev->host);
1221
	struct request_queue *q = sdev->request_queue;
1222 1223 1224 1225 1226 1227
	unsigned long flags;
	struct ata_device *dev;

	if (!ap->ops->error_handler)
		return;

1228
	spin_lock_irqsave(ap->lock, flags);
1229 1230 1231 1232 1233 1234 1235
	dev = __ata_scsi_find_dev(ap, sdev);
	if (dev && dev->sdev) {
		/* SCSI device already in CANCEL state, no need to offline it */
		dev->sdev = NULL;
		dev->flags |= ATA_DFLAG_DETACH;
		ata_port_schedule_eh(ap);
	}
1236
	spin_unlock_irqrestore(ap->lock, flags);
1237 1238 1239 1240

	kfree(q->dma_drain_buffer);
	q->dma_drain_buffer = NULL;
	q->dma_drain_size = 0;
1241 1242
}

1243
/**
1244
 *	__ata_change_queue_depth - helper for ata_scsi_change_queue_depth
1245 1246 1247 1248
 *	@ap: ATA port to which the device change the queue depth
 *	@sdev: SCSI device to configure queue depth for
 *	@queue_depth: new queue depth
 *	@reason: calling context
1249
 *
1250 1251
 *	libsas and libata have different approaches for associating a sdev to
 *	its ata_port.
1252 1253
 *
 */
1254 1255
int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
			     int queue_depth, int reason)
1256 1257
{
	struct ata_device *dev;
1258
	unsigned long flags;
1259

1260 1261 1262
	if (reason != SCSI_QDEPTH_DEFAULT)
		return -EOPNOTSUPP;

1263
	if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1264 1265 1266 1267 1268 1269
		return sdev->queue_depth;

	dev = ata_scsi_find_dev(ap, sdev);
	if (!dev || !ata_dev_enabled(dev))
		return sdev->queue_depth;

1270
	/* NCQ enabled? */
1271
	spin_lock_irqsave(ap->lock, flags);
1272 1273
	dev->flags &= ~ATA_DFLAG_NCQ_OFF;
	if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1274
		dev->flags |= ATA_DFLAG_NCQ_OFF;
1275 1276
		queue_depth = 1;
	}
1277 1278
	spin_unlock_irqrestore(ap->lock, flags);

1279 1280 1281 1282 1283 1284 1285 1286 1287
	/* limit and apply queue depth */
	queue_depth = min(queue_depth, sdev->host->can_queue);
	queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
	queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);

	if (sdev->queue_depth == queue_depth)
		return -EINVAL;

	scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
1288 1289 1290
	return queue_depth;
}

1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
/**
 *	ata_scsi_change_queue_depth - SCSI callback for queue depth config
 *	@sdev: SCSI device to configure queue depth for
 *	@queue_depth: new queue depth
 *	@reason: calling context
 *
 *	This is libata standard hostt->change_queue_depth callback.
 *	SCSI will call into this callback when user tries to set queue
 *	depth via sysfs.
 *
 *	LOCKING:
 *	SCSI layer (we don't care)
 *
 *	RETURNS:
 *	Newly configured queue depth.
 */
int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth,
				int reason)
{
	struct ata_port *ap = ata_shost_to_port(sdev->host);

	return __ata_change_queue_depth(ap, sdev, queue_depth, reason);
}

1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
/**
 *	ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
 *	@qc: Storage for translated ATA taskfile
 *
 *	Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
 *	(to start). Perhaps these commands should be preceded by
 *	CHECK POWER MODE to see what power mode the device is already in.
 *	[See SAT revision 5 at www.t10.org]
 *
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
1325
 *	spin_lock_irqsave(host lock)
1326 1327 1328 1329
 *
 *	RETURNS:
 *	Zero on success, non-zero on error.
 */
1330
static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
1331
{
1332
	struct scsi_cmnd *scmd = qc->scsicmd;
1333
	struct ata_taskfile *tf = &qc->tf;
1334
	const u8 *cdb = scmd->cmnd;
1335

1336 1337 1338
	if (scmd->cmd_len < 5)
		goto invalid_fld;

1339 1340
	tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
	tf->protocol = ATA_PROT_NODATA;
1341
	if (cdb[1] & 0x1) {
1342 1343
		;	/* ignore IMMED bit, violates sat-r05 */
	}
1344
	if (cdb[4] & 0x2)
1345
		goto invalid_fld;       /* LOEJ bit set not supported */
1346
	if (((cdb[4] >> 4) & 0xf) != 0)
1347
		goto invalid_fld;       /* power conditions not supported */
1348

1349
	if (cdb[4] & 0x1) {
1350
		tf->nsect = 1;	/* 1 sector, lba=0 */
1351 1352

		if (qc->dev->flags & ATA_DFLAG_LBA) {
1353
			tf->flags |= ATA_TFLAG_LBA;
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365

			tf->lbah = 0x0;
			tf->lbam = 0x0;
			tf->lbal = 0x0;
			tf->device |= ATA_LBA;
		} else {
			/* CHS */
			tf->lbal = 0x1; /* sect */
			tf->lbam = 0x0; /* cyl low */
			tf->lbah = 0x0; /* cyl high */
		}

1366
		tf->command = ATA_CMD_VERIFY;	/* READ VERIFY */
1367
	} else {
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
		/* Some odd clown BIOSen issue spindown on power off (ACPI S4
		 * or S5) causing some drives to spin up and down again.
		 */
		if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
		    system_state == SYSTEM_POWER_OFF)
			goto skip;

		if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
		     system_entering_hibernation())
			goto skip;

1379 1380
		/* Issue ATA STANDBY IMMEDIATE command */
		tf->command = ATA_CMD_STANDBYNOW1;
1381
	}
1382

1383 1384 1385 1386 1387 1388 1389 1390
	/*
	 * Standby and Idle condition timers could be implemented but that
	 * would require libata to implement the Power condition mode page
	 * and allow the user to change it. Changing mode pages requires
	 * MODE SELECT to be implemented.
	 */

	return 0;
1391

1392
 invalid_fld:
1393
	ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
1394 1395
	/* "Invalid field in cbd" */
	return 1;
1396 1397 1398
 skip:
	scmd->result = SAM_STAT_GOOD;
	return 1;
1399 1400 1401
}


Linus Torvalds's avatar
Linus Torvalds committed
1402 1403 1404 1405 1406 1407 1408 1409
/**
 *	ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
 *	@qc: Storage for translated ATA taskfile
 *
 *	Sets up an ATA taskfile to issue FLUSH CACHE or
 *	FLUSH CACHE EXT.
 *
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
1410
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
1411 1412 1413 1414
 *
 *	RETURNS:
 *	Zero on success, non-zero on error.
 */
1415
static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds's avatar
Linus Torvalds committed
1416 1417 1418 1419 1420 1421
{
	struct ata_taskfile *tf = &qc->tf;

	tf->flags |= ATA_TFLAG_DEVICE;
	tf->protocol = ATA_PROT_NODATA;

1422
	if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds's avatar
Linus Torvalds committed
1423 1424 1425 1426
		tf->command = ATA_CMD_FLUSH_EXT;
	else
		tf->command = ATA_CMD_FLUSH;

1427 1428 1429
	/* flush is critical for IO integrity, consider it an IO command */
	qc->flags |= ATA_QCFLAG_IO;

Linus Torvalds's avatar
Linus Torvalds committed
1430 1431 1432
	return 0;
}

1433 1434
/**
 *	scsi_6_lba_len - Get LBA and transfer length
1435
 *	@cdb: SCSI command to translate
1436 1437 1438 1439 1440 1441 1442
 *
 *	Calculate LBA and transfer length for 6-byte commands.
 *
 *	RETURNS:
 *	@plba: the LBA
 *	@plen: the transfer length
 */
1443
static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1444 1445
{
	u64 lba = 0;
1446
	u32 len;
1447 1448 1449

	VPRINTK("six-byte command\n");

1450
	lba |= ((u64)(cdb[1] & 0x1f)) << 16;
1451 1452
	lba |= ((u64)cdb[2]) << 8;
	lba |= ((u64)cdb[3]);
1453

1454
	len = cdb[4];
1455 1456 1457 1458 1459 1460 1461

	*plba = lba;
	*plen = len;
}

/**
 *	scsi_10_lba_len - Get LBA and transfer length
1462
 *	@cdb: SCSI command to translate
1463 1464 1465 1466 1467 1468 1469
 *
 *	Calculate LBA and transfer length for 10-byte commands.
 *
 *	RETURNS:
 *	@plba: the LBA
 *	@plen: the transfer length
 */
1470
static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1471 1472 1473 1474 1475 1476
{
	u64 lba = 0;
	u32 len = 0;

	VPRINTK("ten-byte command\n");

1477 1478 1479 1480
	lba |= ((u64)cdb[2]) << 24;
	lba |= ((u64)cdb[3]) << 16;
	lba |= ((u64)cdb[4]) << 8;
	lba |= ((u64)cdb[5]);
1481

1482 1483
	len |= ((u32)cdb[7]) << 8;
	len |= ((u32)cdb[8]);
1484 1485 1486 1487 1488 1489 1490

	*plba = lba;
	*plen = len;
}

/**
 *	scsi_16_lba_len - Get LBA and transfer length
1491
 *	@cdb: SCSI command to translate
1492 1493 1494 1495 1496 1497 1498
 *
 *	Calculate LBA and transfer length for 16-byte commands.
 *
 *	RETURNS:
 *	@plba: the LBA
 *	@plen: the transfer length
 */
1499
static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1500 1501 1502 1503 1504 1505
{
	u64 lba = 0;
	u32 len = 0;

	VPRINTK("sixteen-byte command\n");

1506 1507 1508 1509 1510 1511 1512 1513
	lba |= ((u64)cdb[2]) << 56;
	lba |= ((u64)cdb[3]) << 48;
	lba |= ((u64)cdb[4]) << 40;
	lba |= ((u64)cdb[5]) << 32;
	lba |= ((u64)cdb[6]) << 24;
	lba |= ((u64)cdb[7]) << 16;
	lba |= ((u64)cdb[8]) << 8;
	lba |= ((u64)cdb[9]);
1514

1515 1516 1517 1518
	len |= ((u32)cdb[10]) << 24;
	len |= ((u32)cdb[11]) << 16;
	len |= ((u32)cdb[12]) << 8;
	len |= ((u32)cdb[13]);
1519 1520 1521 1522 1523

	*plba = lba;
	*plen = len;
}

Linus Torvalds's avatar
Linus Torvalds committed
1524 1525 1526 1527 1528 1529 1530
/**
 *	ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
 *	@qc: Storage for translated ATA taskfile
 *
 *	Converts SCSI VERIFY command to an ATA READ VERIFY command.
 *
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
1531
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
1532 1533 1534 1535
 *
 *	RETURNS:
 *	Zero on success, non-zero on error.
 */
1536
static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds's avatar
Linus Torvalds committed
1537
{