libata-scsi.c 127 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
 *    		    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',
28
 *  as Documentation/driver-api/libata.rst
29 30 31 32 33
 *
 *  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 <asm/unaligned.h>
53
#include <linux/ioprio.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
static const char *ata_lpm_policy_names[] = {
109 110 111 112
	[ATA_LPM_UNKNOWN]		= "max_performance",
	[ATA_LPM_MAX_POWER]		= "max_performance",
	[ATA_LPM_MED_POWER]		= "medium_power",
	[ATA_LPM_MED_POWER_WITH_DIPM]	= "med_power_with_dipm",
113
	[ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
114
	[ATA_LPM_MIN_POWER]		= "min_power",
115 116
};

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

	/* 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)
134 135
			break;
	}
136
	if (policy == ARRAY_SIZE(ata_lpm_policy_names))
137 138
		return -EINVAL;

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

	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;
			}
		}
	}

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

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

163
	if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
164 165
		return -EINVAL;

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

173 174 175 176 177 178 179
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;
180
	unsigned long now;
181 182 183 184 185
	unsigned int uninitialized_var(msecs);
	int rc = 0;

	ap = ata_shost_to_port(sdev->host);

186
	spin_lock_irq(ap->lock);
187 188 189 190 191 192 193 194 195 196 197
	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;
198
	now = jiffies;
199 200
	if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
	    link->eh_context.unloaded_mask & (1 << dev->devno) &&
201 202
	    time_after(dev->unpark_deadline, now))
		msecs = jiffies_to_msecs(dev->unpark_deadline - now);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
	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;

223 224 225 226
	rc = kstrtol(buf, 10, &input);
	if (rc)
		return rc;
	if (input < -2)
227 228 229 230 231 232 233 234 235 236 237 238 239 240
		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;
	}
241 242
	if (dev->class != ATA_DEV_ATA &&
	    dev->class != ATA_DEV_ZAC) {
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 271 272 273 274 275
		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);

276
static ssize_t ata_ncq_prio_enable_show(struct device *device,
277 278
					struct device_attribute *attr,
					char *buf)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
{
	struct scsi_device *sdev = to_scsi_device(device);
	struct ata_port *ap;
	struct ata_device *dev;
	bool ncq_prio_enable;
	int rc = 0;

	ap = ata_shost_to_port(sdev->host);

	spin_lock_irq(ap->lock);
	dev = ata_scsi_find_dev(ap, sdev);
	if (!dev) {
		rc = -ENODEV;
		goto unlock;
	}

	ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;

unlock:
	spin_unlock_irq(ap->lock);

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

static ssize_t ata_ncq_prio_enable_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;
	int rc;

	rc = kstrtol(buf, 10, &input);
	if (rc)
		return rc;
	if ((input < 0) || (input > 1))
		return -EINVAL;

	ap = ata_shost_to_port(sdev->host);
	dev = ata_scsi_find_dev(ap, sdev);
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	if (unlikely(!dev))
		return  -ENODEV;

	spin_lock_irq(ap->lock);
	if (input)
		dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
	else
		dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;

	dev->link->eh_info.action |= ATA_EH_REVALIDATE;
	dev->link->eh_info.flags |= ATA_EHI_QUIET;
	ata_port_schedule_eh(ap);
	spin_unlock_irq(ap->lock);

	ata_port_wait_eh(ap);
336

337
	if (input) {
338
		spin_lock_irq(ap->lock);
339
		if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
340 341
			dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
			rc = -EIO;
342
		}
343
		spin_unlock_irq(ap->lock);
344
	}
345 346 347 348 349 350 351 352

	return rc ? rc : len;
}

DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
	    ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);

353 354
void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
			u8 sk, u8 asc, u8 ascq)
355
{
356 357
	bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);

358 359 360
	if (!cmd)
		return;

361 362
	cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;

363
	scsi_build_sense_buffer(d_sense, cmd->sense_buffer, sk, asc, ascq);
364 365
}

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
void ata_scsi_set_sense_information(struct ata_device *dev,
				    struct scsi_cmnd *cmd,
				    const struct ata_taskfile *tf)
{
	u64 information;

	if (!cmd)
		return;

	information = ata_tf_read_block(tf, dev);
	if (information == U64_MAX)
		return;

	scsi_set_sense_information(cmd->sense_buffer,
				   SCSI_SENSE_BUFFERSIZE, information);
}

383
static void ata_scsi_set_invalid_field(struct ata_device *dev,
384
				       struct scsi_cmnd *cmd, u16 field, u8 bit)
385 386
{
	ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
387
	/* "Invalid field in CDB" */
388
	scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
389
				     field, bit, 1);
390 391
}

392 393 394 395 396 397 398
static void ata_scsi_set_invalid_parameter(struct ata_device *dev,
					   struct scsi_cmnd *cmd, u16 field)
{
	/* "Invalid field in parameter list" */
	ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x26, 0x0);
	scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
				     field, 0xff, 0);
399 400
}

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
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;
}
423
DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
		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);

448 449
	if (atadev && ap->ops->sw_activity_show &&
	    (ap->flags & ATA_FLAG_SW_ACTIVITY))
450 451 452 453 454 455 456 457 458 459 460 461 462 463
		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;

464 465
	if (atadev && ap->ops->sw_activity_store &&
	    (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
466 467 468 469 470 471 472 473 474 475 476 477
		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;
}
478
DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
479 480 481
			ata_scsi_activity_store);
EXPORT_SYMBOL_GPL(dev_attr_sw_activity);

482 483
struct device_attribute *ata_common_sdev_attrs[] = {
	&dev_attr_unload_heads,
484
	&dev_attr_ncq_prio_enable,
485 486 487 488
	NULL
};
EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);

Linus Torvalds's avatar
Linus Torvalds committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
/**
 *	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;
}

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
/**
 *	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);
}

547 548
/**
 *	ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
549
 *	@ap: target port
550 551 552 553 554 555 556 557 558
 *	@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.
 */
559 560
static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
			    void __user *arg)
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
{
	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;
}

587 588
/**
 *	ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
589
 *	@scsidev: Device to which we are issuing command
590 591 592 593 594 595 596 597 598 599 600
 *	@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;
601
	u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
602
	u8 scsi_cmd[MAX_COMMAND_SIZE];
603
	u8 args[4], *argbuf = NULL;
604
	int argsize = 0;
605
	enum dma_data_direction data_dir;
606
	struct scsi_sense_hdr sshdr;
607
	int cmd_result;
608

609
	if (arg == NULL)
610 611 612 613 614
		return -EINVAL;

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

615
	memset(sensebuf, 0, sizeof(sensebuf));
616 617 618
	memset(scsi_cmd, 0, sizeof(scsi_cmd));

	if (args[3]) {
619
		argsize = ATA_SECT_SIZE * args[3];
620
		argbuf = kmalloc(argsize, GFP_KERNEL);
621 622 623 624
		if (argbuf == NULL) {
			rc = -ENOMEM;
			goto error;
		}
625 626 627

		scsi_cmd[1]  = (4 << 1); /* PIO Data-in */
		scsi_cmd[2]  = 0x0e;     /* no off.line or cc, read from dev,
628
					    block count in sector count field */
629
		data_dir = DMA_FROM_DEVICE;
630 631
	} else {
		scsi_cmd[1]  = (3 << 1); /* Non-data */
632
		scsi_cmd[2]  = 0x20;     /* cc but no off.line or data xfer */
633
		data_dir = DMA_NONE;
634 635 636 637 638
	}

	scsi_cmd[0] = ATA_16;

	scsi_cmd[4] = args[2];
639
	if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
640 641
		scsi_cmd[6]  = args[3];
		scsi_cmd[8]  = args[1];
642 643
		scsi_cmd[10] = ATA_SMART_LBAM_PASS;
		scsi_cmd[12] = ATA_SMART_LBAH_PASS;
644 645 646 647 648 649 650
	} 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... */
651
	cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
652
				  sensebuf, &sshdr, (10*HZ), 5, 0, 0, NULL);
653 654 655 656 657 658 659 660

	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) {
661 662
			if (sshdr.sense_key == RECOVERED_ERROR &&
			    sshdr.asc == 0 && sshdr.ascq == 0x1d)
663 664 665 666
				cmd_result &= ~SAM_STAT_CHECK_CONDITION;
		}

		/* Send userspace a few ATA registers (same as drivers/ide) */
667 668 669 670 671
		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) */
672 673 674 675 676 677 678
			if (copy_to_user(arg, args, sizeof(args)))
				rc = -EFAULT;
		}
	}


	if (cmd_result) {
679 680 681 682 683
		rc = -EIO;
		goto error;
	}

	if ((argbuf)
684
	 && copy_to_user(arg + sizeof(args), argbuf, argsize))
685 686
		rc = -EFAULT;
error:
687
	kfree(argbuf);
688 689 690 691 692
	return rc;
}

/**
 *	ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
693
 *	@scsidev: Device to which we are issuing command
694 695 696 697 698 699 700 701 702 703 704
 *	@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;
705
	u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
706
	u8 scsi_cmd[MAX_COMMAND_SIZE];
707
	u8 args[7];
708
	struct scsi_sense_hdr sshdr;
709
	int cmd_result;
710

711
	if (arg == NULL)
712 713 714 715 716
		return -EINVAL;

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

717
	memset(sensebuf, 0, sizeof(sensebuf));
718 719 720
	memset(scsi_cmd, 0, sizeof(scsi_cmd));
	scsi_cmd[0]  = ATA_16;
	scsi_cmd[1]  = (3 << 1); /* Non-data */
721
	scsi_cmd[2]  = 0x20;     /* cc but no off.line or data xfer */
722 723 724 725 726
	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];
727
	scsi_cmd[13] = args[6] & 0x4f;
728 729 730
	scsi_cmd[14] = args[0];

	/* Good values for timeout and retries?  Values below
731
	   from scsi_ioctl_send_command() for default case... */
732
	cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
733
				sensebuf, &sshdr, (10*HZ), 5, 0, 0, NULL);
734 735 736 737 738 739 740 741

	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) {
742 743
			if (sshdr.sense_key == RECOVERED_ERROR &&
			    sshdr.asc == 0 && sshdr.ascq == 0x1d)
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
				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) {
763
		rc = -EIO;
764 765
		goto error;
	}
766

767
 error:
768 769 770
	return rc;
}

771 772 773 774 775 776 777 778 779
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;
}

780 781
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
782
{
783 784
	unsigned long val;
	int rc = -EINVAL;
785
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
786 787

	switch (cmd) {
788
	case HDIO_GET_32BIT:
789 790 791
		spin_lock_irqsave(ap->lock, flags);
		val = ata_ioc32(ap);
		spin_unlock_irqrestore(ap->lock, flags);
792
		return put_user(val, (unsigned long __user *)arg);
Linus Torvalds's avatar
Linus Torvalds committed
793

794
	case HDIO_SET_32BIT:
Linus Torvalds's avatar
Linus Torvalds committed
795
		val = (unsigned long) arg;
796 797 798 799 800 801 802 803 804 805 806 807 808
		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
809

810
	case HDIO_GET_IDENTITY:
811
		return ata_get_identity(ap, scsidev, arg);
812

813 814 815 816 817 818 819 820 821 822
	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
823 824 825 826 827 828 829
	default:
		rc = -ENOTTY;
		break;
	}

	return rc;
}
830 831 832 833 834 835 836 837
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
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852

/**
 *	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
853
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
854 855 856 857
 *
 *	RETURNS:
 *	Command allocated, or %NULL if none available.
 */
858
static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
859
					      struct scsi_cmnd *cmd)
Linus Torvalds's avatar
Linus Torvalds committed
860 861 862
{
	struct ata_queued_cmd *qc;

Shaohua Li's avatar
Shaohua Li committed
863
	qc = ata_qc_new_init(dev, cmd->request->tag);
Linus Torvalds's avatar
Linus Torvalds committed
864 865
	if (qc) {
		qc->scsicmd = cmd;
866
		qc->scsidone = cmd->scsi_done;
Linus Torvalds's avatar
Linus Torvalds committed
867

Tejun Heo's avatar
Tejun Heo committed
868
		qc->sg = scsi_sglist(cmd);
869
		qc->n_elem = scsi_sg_count(cmd);
870 871 872

		if (cmd->request->rq_flags & RQF_QUIET)
			qc->flags |= ATA_QCFLAG_QUIET;
Linus Torvalds's avatar
Linus Torvalds committed
873 874
	} else {
		cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
875
		cmd->scsi_done(cmd);
Linus Torvalds's avatar
Linus Torvalds committed
876 877 878 879 880
	}

	return qc;
}

881 882 883 884 885 886 887 888
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;
}

889 890 891 892 893 894 895 896 897 898 899 900
/**
 *	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
 */
901
static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
902 903 904
{
	u8 stat = tf->command, err = tf->feature;

905
	pr_warn("ata%u: status=0x%02x { ", id, stat);
906
	if (stat & ATA_BUSY) {
907
		pr_cont("Busy }\n");	/* Data is not valid in this case */
908
	} else {
909 910 911 912 913 914 915 916
		if (stat & ATA_DRDY)	pr_cont("DriveReady ");
		if (stat & ATA_DF)	pr_cont("DeviceFault ");
		if (stat & ATA_DSC)	pr_cont("SeekComplete ");
		if (stat & ATA_DRQ)	pr_cont("DataRequest ");
		if (stat & ATA_CORR)	pr_cont("CorrectedError ");
		if (stat & ATA_SENSE)	pr_cont("Sense ");
		if (stat & ATA_ERR)	pr_cont("Error ");
		pr_cont("}\n");
917 918

		if (err) {
919 920
			pr_warn("ata%u: error=0x%02x { ", id, err);
			if (err & ATA_ABORTED)	pr_cont("DriveStatusError ");
921 922
			if (err & ATA_ICRC) {
				if (err & ATA_ABORTED)
923 924
						pr_cont("BadCRC ");
				else		pr_cont("Sector ");
925
			}
926 927 928 929 930
			if (err & ATA_UNC)	pr_cont("UncorrectableError ");
			if (err & ATA_IDNF)	pr_cont("SectorIdNotFound ");
			if (err & ATA_TRK0NF)	pr_cont("TrackZeroNotFound ");
			if (err & ATA_AMNF)	pr_cont("AddrMarkNotFound ");
			pr_cont("}\n");
931 932 933 934
		}
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
935 936
/**
 *	ata_to_sense_error - convert ATA error to SCSI error
937
 *	@id: ATA device number
Linus Torvalds's avatar
Linus Torvalds committed
938
 *	@drv_stat: value contained in ATA status register
939 940 941 942
 *	@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
943
 *	@verbose: be verbose
Linus Torvalds's avatar
Linus Torvalds committed
944
 *
945 946 947
 *	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
948 949
 *
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
950
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
951
 */
952 953
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
954
{
955
	int i;
Jeff Garzik's avatar
Jeff Garzik committed
956

Linus Torvalds's avatar
Linus Torvalds committed
957
	/* Based on the 3ware driver translation table */
958
	static const unsigned char sense_table[][4] = {
Linus Torvalds's avatar
Linus Torvalds committed
959
		/* BBD|ECC|ID|MAR */
960 961
		{0xd1,		ABORTED_COMMAND, 0x00, 0x00},
			// Device busy                  Aborted command
Linus Torvalds's avatar
Linus Torvalds committed
962
		/* BBD|ECC|ID */
963 964
		{0xd0,		ABORTED_COMMAND, 0x00, 0x00},
			// Device busy                  Aborted command
Linus Torvalds's avatar
Linus Torvalds committed
965
		/* ECC|MC|MARK */
966 967
		{0x61,		HARDWARE_ERROR, 0x00, 0x00},
			// Device fault                 Hardware error
Linus Torvalds's avatar
Linus Torvalds committed
968
		/* ICRC|ABRT */		/* NB: ICRC & !ABRT is BBD */
969 970
		{0x84,		ABORTED_COMMAND, 0x47, 0x00},
			// Data CRC error               SCSI parity error
Linus Torvalds's avatar
Linus Torvalds committed
971
		/* MC|ID|ABRT|TRK0|MARK */
972 973
		{0x37,		NOT_READY, 0x04, 0x00},
			// Unit offline                 Not ready
Linus Torvalds's avatar
Linus Torvalds committed
974
		/* MCR|MARK */
975 976
		{0x09,		NOT_READY, 0x04, 0x00},
			// Unrecovered disk error       Not ready
Linus Torvalds's avatar
Linus Torvalds committed
977
		/*  Bad address mark */
978 979 980 981 982
		{0x01,		MEDIUM_ERROR, 0x13, 0x00},
			// Address mark not found for data field
		/* TRK0 - Track 0 not found */
		{0x02,		HARDWARE_ERROR, 0x00, 0x00},
			// Hardware error
983
		/* Abort: 0x04 is not translated here, see below */
Linus Torvalds's avatar
Linus Torvalds committed
984
		/* Media change request */
985 986 987 988 989 990 991 992 993 994 995
		{0x08,		NOT_READY, 0x04, 0x00},
			// FIXME: faking offline
		/* SRV/IDNF - ID not found */
		{0x10,		ILLEGAL_REQUEST, 0x21, 0x00},
			// Logical address out of range
		/* MC - Media Changed */
		{0x20,		UNIT_ATTENTION, 0x28, 0x00},
			// Not ready to ready change, medium may have changed
		/* ECC - Uncorrectable ECC error */
		{0x40,		MEDIUM_ERROR, 0x11, 0x04},
			// Unrecovered read error
Linus Torvalds's avatar
Linus Torvalds committed
996
		/* BBD - block marked bad */
997 998
		{0x80,		MEDIUM_ERROR, 0x11, 0x04},
			// Block marked bad	Medium error, unrecovered read error
Linus Torvalds's avatar
Linus Torvalds committed
999 1000
		{0xFF, 0xFF, 0xFF, 0xFF}, // END mark
	};
1001
	static const unsigned char stat_table[][4] = {
Linus Torvalds's avatar
Linus Torvalds committed
1002
		/* Must be first because BUSY means no other bits valid */
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
		{0x80,		ABORTED_COMMAND, 0x47, 0x00},
		// Busy, fake parity for now
		{0x40,		ILLEGAL_REQUEST, 0x21, 0x04},
		// Device ready, unaligned write command
		{0x20,		HARDWARE_ERROR,  0x44, 0x00},
		// Device fault, internal target failure
		{0x08,		ABORTED_COMMAND, 0x47, 0x00},
		// Timed out in xfer, fake parity for now
		{0x04,		RECOVERED_ERROR, 0x11, 0x00},
		// Recovered ECC error	  Medium error, recovered
Linus Torvalds's avatar
Linus Torvalds committed
1013 1014 1015 1016 1017 1018
		{0xFF, 0xFF, 0xFF, 0xFF}, // END mark
	};

	/*
	 *	Is this an error we can process/parse
	 */
1019 1020
	if (drv_stat & ATA_BUSY) {
		drv_err = 0;	/* Ignore the err bits, they're invalid */
Linus Torvalds's avatar
Linus Torvalds committed
1021
	}
1022 1023 1024 1025 1026

	if (drv_err) {
		/* Look for drv_err */
		for (i = 0; sense_table[i][0] != 0xFF; i++) {
			/* Look for best matches first */
1027
			if ((sense_table[i][0] & drv_err) ==
1028 1029 1030 1031 1032 1033 1034
			    sense_table[i][0]) {
				*sk = sense_table[i][1];
				*asc = sense_table[i][2];
				*ascq = sense_table[i][3];
				goto translate_done;
			}
		}
Linus Torvalds's avatar
Linus Torvalds committed
1035
	}
1036

1037 1038 1039 1040 1041
	/*
	 * Fall back to interpreting status bits.  Note that if the drv_err
	 * has only the ABRT bit set, we decode drv_stat.  ABRT by itself
	 * is not descriptive enough.
	 */
1042 1043 1044 1045 1046 1047
	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
1048
		}
1049 1050
	}

1051 1052 1053 1054
	/*
	 * 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.
	 */
1055 1056 1057
	*sk = ABORTED_COMMAND;
	*asc = 0x00;
	*ascq = 0x00;
1058 1059

 translate_done:
1060
	if (verbose)
1061
		pr_err("ata%u: translated ATA stat/err 0x%02x/%02x to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
1062
		       id, drv_stat, drv_err, *sk, *asc, *ascq);
1063 1064 1065 1066
	return;
}

/*
1067
 *	ata_gen_passthru_sense - Generate check condition sense block.
1068 1069 1070 1071 1072 1073
 *	@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
1074 1075 1076 1077 1078
 *	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
 *      
1079 1080
 *
 *	LOCKING:
1081
 *	None.
1082
 */
1083
static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
1084 1085
{
	struct scsi_cmnd *cmd = qc->scsicmd;
1086
	struct ata_taskfile *tf = &qc->result_tf;
1087 1088
	unsigned char *sb = cmd->sense_buffer;
	unsigned char *desc = sb + 8;
1089
	int verbose = qc->ap->ops->error_handler == NULL;
1090
	u8 sense_key, asc, ascq;
1091 1092 1093

	memset(sb, 0, SCSI_SENSE_BUFFERSIZE);

1094
	cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1095 1096 1097 1098 1099

	/*
	 * Use ata_to_sense_error() to map status register bits
	 * onto sense key, asc & ascq.
	 */
1100 1101
	if (qc->err_mask ||
	    tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo's avatar
Tejun Heo committed
1102
		ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
1103
				   &sense_key, &asc, &ascq, verbose);
1104
		ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
1105
	} else {
1106 1107 1108 1109 1110 1111
		/*
		 * ATA PASS-THROUGH INFORMATION AVAILABLE
		 * Always in descriptor format sense.
		 */
		scsi_build_sense_buffer(1, cmd->sense_buffer,
					RECOVERED_ERROR, 0, 0x1D);
Linus Torvalds's avatar
Linus Torvalds committed
1112 1113
	}

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
		u8 len;

		/* descriptor format */
		len = sb[7];
		desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
		if (!desc) {
			if (SCSI_SENSE_BUFFERSIZE < len + 14)
				return;
			sb[7] = len + 14;
			desc = sb + 8 + len;
		}
		desc[0] = 9;
		desc[1] = 12;
		/*
		 * 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 */
1139

1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
		/*
		 * 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;
		}
	} else {
		/* Fixed sense format */
		desc[0] = tf->feature;
		desc[1] = tf->command; /* status */
		desc[2] = tf->device;
		desc[3] = tf->nsect;
1157
		desc[7] = 0;
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
		if (tf->flags & ATA_TFLAG_LBA48)  {
			desc[8] |= 0x80;
			if (tf->hob_nsect)
				desc[8] |= 0x40;
			if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
				desc[8] |= 0x20;
		}
		desc[9] = tf->lbal;
		desc[10] = tf->lbam;
		desc[11] = tf->lbah;
Linus Torvalds's avatar
Linus Torvalds committed
1168
	}
1169
}
Linus Torvalds's avatar
Linus Torvalds committed
1170

1171
/**
1172
 *	ata_gen_ata_sense - generate a SCSI fixed sense block
1173 1174
 *	@qc: Command that we are erroring out
 *
1175
 *	Generate sense block for a failed ATA command @qc.  Descriptor
Lucas De Marchi's avatar
Lucas De Marchi committed
1176
 *	format is used to accommodate LBA48 block address.
1177 1178
 *
 *	LOCKING:
1179
 *	None.
1180
 */
1181
static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
1182
{
1183
	struct ata_device *dev = qc->dev;
1184
	struct scsi_cmnd *cmd = qc->scsicmd;
1185
	struct ata_taskfile *tf = &qc->result_tf;
1186
	unsigned char *sb = cmd->sense_buffer;
1187
	int verbose = qc->ap->ops->error_handler == NULL;
1188
	u64 block;
1189
	u8 sense_key, asc, ascq;
1190 1191 1192

	memset(sb, 0, SCSI_SENSE_BUFFERSIZE);

1193
	cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1194

1195 1196 1197 1198 1199 1200
	if (ata_dev_disabled(dev)) {
		/* Device disabled after error recovery */
		/* LOGICAL UNIT NOT READY, HARD RESET REQUIRED */
		ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
		return;
	}
1201
	/* Use ata_to_sense_error() to map status register bits
1202 1203
	 * onto sense key, asc & ascq.
	 */
1204 1205
	if (qc->err_mask ||
	    tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo's avatar
Tejun Heo committed
1206
		ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
1207
				   &sense_key, &asc, &ascq, verbose);
1208
		ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
1209 1210 1211 1212
	} else {
		/* Could not decode error */
		ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
			     tf->command, qc->err_mask);
1213
		ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
1214
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1215 1216
	}

1217
	block = ata_tf_read_block(&qc->result_tf, dev);
1218 1219
	if (block == U64_MAX)
		return;
1220

1221
	scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
Linus Torvalds's avatar
Linus Torvalds committed
1222 1223
}

1224 1225 1226 1227
static void ata_scsi_sdev_config(struct scsi_device *sdev)
{
	sdev->use_10_for_rw = 1;
	sdev->use_10_for_ms = 1;
1228
	sdev->no_write_same = 1;
1229 1230 1231 1232 1233 1234 1235

	/* 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;
1236 1237
}

1238 1239
/**
 *	atapi_drain_needed - Check whether data transfer may overflow
1240
 *	@rq: request to be checked
1241 1242
 *
 *	ATAPI commands which transfer variable length data to host
1243
 *	might overflow due to application error or hardware bug.  This
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
 *	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)
{
1255
	if (likely(!blk_rq_is_passthrough(rq)))
1256 1257
		return 0;

1258
	if (!blk_rq_bytes(rq) || op_is_write(req_op(rq)))
1259 1260
		return 0;

1261
	return atapi_cmd_type(scsi_req(rq)->cmd[0]) == ATAPI_MISC;
1262 1263 1264 1265
}

static int ata_scsi_dev_config(struct scsi_device *sdev,
			       struct ata_device *dev)
1266
{
1267 1268
	struct request_queue *q = sdev->request_queue;

1269 1270 1271
	if (!ata_id_has_unload(dev->id))
		dev->flags |= ATA_DFLAG_NO_UNLOAD;

1272
	/* configure max sectors */
1273
	blk_queue_max_hw_sectors(q, dev->max_sectors);
1274

1275 1276 1277
	if (dev->class == ATA_DEV_ATAPI) {
		void *buf;

1278 1279 1280
		sdev->sector_size = ATA_SECT_SIZE;

		/* set DMA padding */
1281
		blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
1282 1283 1284 1285

		/* configure draining */
		buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
		if (!buf) {
1286
			ata_dev_err(dev, "drain buffer allocation failed\n");
1287 1288 1289 1290 1291
			return -ENOMEM;
		}

		blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
	} else {
1292
		sdev->sector_size = ata_id_logical_sector_size(dev->id);
1293
		sdev->manage_start_stop = 1;
1294
	}
1295

1296 1297 1298 1299 1300 1301 1302 1303
	/*
	 * 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)
1304
		ata_dev_warn(dev,
1305 1306 1307
			"sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
			sdev->sector_size);

1308
	blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
1309

1310 1311 1312
	if (dev->flags & ATA_DFLAG_AN)
		set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);

1313 1314 1315 1316
	if (dev->flags & ATA_DFLAG_NCQ) {
		int depth;

		depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1317
		depth = min(ATA_MAX_QUEUE, depth);
1318
		scsi_change_queue_depth(sdev, depth);