libata-scsi.c 87.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
3
4
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
 *  libata-scsi.c - helper library for ATA
 *
 *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
 *    		    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
37
38
39
40
 */

#include <linux/kernel.h>
#include <linux/blkdev.h>
#include <linux/spinlock.h>
#include <scsi/scsi.h>
#include <scsi/scsi_host.h>
41
#include <scsi/scsi_cmnd.h>
42
#include <scsi/scsi_eh.h>
43
#include <scsi/scsi_device.h>
44
#include <scsi/scsi_tcq.h>
45
#include <scsi/scsi_transport.h>
Linus Torvalds's avatar
Linus Torvalds committed
46
#include <linux/libata.h>
47
#include <linux/hdreg.h>
Linus Torvalds's avatar
Linus Torvalds committed
48
49
50
51
#include <asm/uaccess.h>

#include "libata.h"

52
53
#define SECTOR_SIZE	512

54
typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
55
56
57
58
59

static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
					const struct scsi_device *scsidev);
static struct ata_device * ata_scsi_find_dev(struct ata_port *ap,
					    const struct scsi_device *scsidev);
60
61
static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
			      unsigned int id, unsigned int lun);
62

Linus Torvalds's avatar
Linus Torvalds committed
63

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#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


static const u8 def_rw_recovery_mpage[] = {
	RW_RECOVERY_MPAGE,
	RW_RECOVERY_MPAGE_LEN - 2,
	(1 << 7) |	/* AWRE, sat-r06 say it shall be 0 */
	    (1 << 6),	/* ARRE (auto read reallocation) */
	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 */
};

103
104
105
106
/*
 * libata transport template.  libata doesn't do real transport stuff.
 * It just needs the eh_timed_out hook.
 */
107
static struct scsi_transport_template ata_scsi_transport_template = {
108
	.eh_strategy_handler	= ata_scsi_error,
109
	.eh_timed_out		= ata_scsi_timed_out,
Tejun Heo's avatar
Tejun Heo committed
110
	.user_scan		= ata_scsi_user_scan,
111
112
};

Linus Torvalds's avatar
Linus Torvalds committed
113

114
115
116
117
118
119
120
121
static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
				   void (*done)(struct scsi_cmnd *))
{
	ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
	/* "Invalid field in cbd" */
	done(cmd);
}

Linus Torvalds's avatar
Linus Torvalds committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
 *	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;
}

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 *	ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
 *	@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.
 */
static int ata_get_identity(struct scsi_device *sdev, void __user *arg)
{
	struct ata_port *ap = ata_shost_to_port(sdev->host);
	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;
}

190
191
/**
 *	ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap's avatar
Randy Dunlap committed
192
 *	@scsidev: Device to which we are issuing command
193
194
195
196
197
198
199
200
201
202
203
204
 *	@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];
205
	u8 args[4], *argbuf = NULL, *sensebuf = NULL;
206
	int argsize = 0;
207
	enum dma_data_direction data_dir;
208
	int cmd_result;
209

210
	if (arg == NULL)
211
212
213
214
215
		return -EINVAL;

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

216
217
218
219
	sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
	if (!sensebuf)
		return -ENOMEM;

220
221
222
223
224
	memset(scsi_cmd, 0, sizeof(scsi_cmd));

	if (args[3]) {
		argsize = SECTOR_SIZE * args[3];
		argbuf = kmalloc(argsize, GFP_KERNEL);
225
226
227
228
		if (argbuf == NULL) {
			rc = -ENOMEM;
			goto error;
		}
229
230
231
232

		scsi_cmd[1]  = (4 << 1); /* PIO Data-in */
		scsi_cmd[2]  = 0x0e;     /* no off.line or cc, read from dev,
		                            block count in sector count field */
233
		data_dir = DMA_FROM_DEVICE;
234
235
	} else {
		scsi_cmd[1]  = (3 << 1); /* Non-data */
236
		scsi_cmd[2]  = 0x20;     /* cc but no off.line or data xfer */
237
		data_dir = DMA_NONE;
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	}

	scsi_cmd[0] = ATA_16;

	scsi_cmd[4] = args[2];
	if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
		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... */
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
	cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
	                          sensebuf, (10*HZ), 5, 0);

	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);
			if (sshdr.sense_key==0 &&
			    sshdr.asc==0 && sshdr.ascq==0)
				cmd_result &= ~SAM_STAT_CHECK_CONDITION;
		}

		/* Send userspace a few ATA registers (same as drivers/ide) */
		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) */
			if (copy_to_user(arg, args, sizeof(args)))
				rc = -EFAULT;
		}
	}


	if (cmd_result) {
286
287
288
289
290
		rc = -EIO;
		goto error;
	}

	if ((argbuf)
291
	 && copy_to_user(arg + sizeof(args), argbuf, argsize))
292
293
		rc = -EFAULT;
error:
294
	kfree(sensebuf);
295
	kfree(argbuf);
296
297
298
299
300
	return rc;
}

/**
 *	ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap's avatar
Randy Dunlap committed
301
 *	@scsidev: Device to which we are issuing command
302
303
304
305
306
307
308
309
310
311
312
313
 *	@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];
314
315
	u8 args[7], *sensebuf = NULL;
	int cmd_result;
316

317
	if (arg == NULL)
318
319
320
321
322
		return -EINVAL;

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

323
324
325
326
	sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
	if (!sensebuf)
		return -ENOMEM;

327
328
329
	memset(scsi_cmd, 0, sizeof(scsi_cmd));
	scsi_cmd[0]  = ATA_16;
	scsi_cmd[1]  = (3 << 1); /* Non-data */
330
	scsi_cmd[2]  = 0x20;     /* cc but no off.line or data xfer */
331
332
333
334
335
	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];
336
	scsi_cmd[13] = args[6] & 0x4f;
337
338
339
	scsi_cmd[14] = args[0];

	/* Good values for timeout and retries?  Values below
340
	   from scsi_ioctl_send_command() for default case... */
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
	cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
				sensebuf, (10*HZ), 5, 0);

	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);
			if (sshdr.sense_key==0 &&
				sshdr.asc==0 && sshdr.ascq==0)
				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) {
375
		rc = -EIO;
376
377
		goto error;
	}
378

379
380
 error:
	kfree(sensebuf);
381
382
383
	return rc;
}

Linus Torvalds's avatar
Linus Torvalds committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
{
	int val = -EINVAL, rc = -EINVAL;

	switch (cmd) {
	case ATA_IOC_GET_IO32:
		val = 0;
		if (copy_to_user(arg, &val, 1))
			return -EFAULT;
		return 0;

	case ATA_IOC_SET_IO32:
		val = (unsigned long) arg;
		if (val != 0)
			return -EINVAL;
		return 0;

401
402
403
	case HDIO_GET_IDENTITY:
		return ata_get_identity(scsidev, arg);

404
405
406
407
408
409
410
411
412
413
	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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
	default:
		rc = -ENOTTY;
		break;
	}

	return rc;
}

/**
 *	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
 *	@done: SCSI command completion function
 *
 *	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
437
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
438
439
440
441
 *
 *	RETURNS:
 *	Command allocated, or %NULL if none available.
 */
442
443
444
static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
					      struct scsi_cmnd *cmd,
					      void (*done)(struct scsi_cmnd *))
Linus Torvalds's avatar
Linus Torvalds committed
445
446
447
{
	struct ata_queued_cmd *qc;

Tejun Heo's avatar
Tejun Heo committed
448
	qc = ata_qc_new_init(dev);
Linus Torvalds's avatar
Linus Torvalds committed
449
450
451
452
453
	if (qc) {
		qc->scsicmd = cmd;
		qc->scsidone = done;

		if (cmd->use_sg) {
454
			qc->__sg = (struct scatterlist *) cmd->request_buffer;
Linus Torvalds's avatar
Linus Torvalds committed
455
			qc->n_elem = cmd->use_sg;
456
		} else if (cmd->request_bufflen) {
457
			qc->__sg = &qc->sgent;
Linus Torvalds's avatar
Linus Torvalds committed
458
459
460
461
462
463
464
465
466
467
			qc->n_elem = 1;
		}
	} else {
		cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
		done(cmd);
	}

	return qc;
}

468
469
470
471
472
473
474
475
476
477
478
479
/**
 *	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
 */
480
static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
{
	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
513
514
/**
 *	ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap's avatar
Randy Dunlap committed
515
 *	@id: ATA device number
Linus Torvalds's avatar
Linus Torvalds committed
516
 *	@drv_stat: value contained in ATA status register
517
518
519
520
 *	@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
521
 *	@verbose: be verbose
Linus Torvalds's avatar
Linus Torvalds committed
522
 *
523
524
525
 *	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
526
527
 *
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
528
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
529
 */
530
531
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
532
{
533
	int i;
Jeff Garzik's avatar
Jeff Garzik committed
534

Linus Torvalds's avatar
Linus Torvalds committed
535
	/* Based on the 3ware driver translation table */
536
	static const unsigned char sense_table[][4] = {
Linus Torvalds's avatar
Linus Torvalds committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
		/* 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
	};
567
	static const unsigned char stat_table[][4] = {
Linus Torvalds's avatar
Linus Torvalds committed
568
569
570
571
572
573
574
575
576
577
578
		/* 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
	 */
579
580
	if (drv_stat & ATA_BUSY) {
		drv_err = 0;	/* Ignore the err bits, they're invalid */
Linus Torvalds's avatar
Linus Torvalds committed
581
	}
582
583
584
585
586

	if (drv_err) {
		/* Look for drv_err */
		for (i = 0; sense_table[i][0] != 0xFF; i++) {
			/* Look for best matches first */
587
			if ((sense_table[i][0] & drv_err) ==
588
589
590
591
592
593
594
595
			    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 */
596
597
598
		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
599
	}
600
601
602
603
604
605
606
607

	/* 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
608
		}
609
610
	}
	/* No error?  Undecoded? */
611
612
613
	if (verbose)
		printk(KERN_WARNING "ata%u: no sense translation for "
		       "status: 0x%02x\n", id, drv_stat);
614

615
616
617
618
619
	/* 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;
620
621

 translate_done:
622
623
624
625
	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);
626
627
628
629
	return;
}

/*
630
 *	ata_gen_passthru_sense - Generate check condition sense block.
631
632
633
634
635
636
637
638
639
 *	@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
 *	block. Clear sense key, ASC & ASCQ if there is no error.
 *
 *	LOCKING:
640
 *	None.
641
 */
642
static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
643
644
{
	struct scsi_cmnd *cmd = qc->scsicmd;
645
	struct ata_taskfile *tf = &qc->result_tf;
646
647
	unsigned char *sb = cmd->sense_buffer;
	unsigned char *desc = sb + 8;
648
	int verbose = qc->ap->ops->error_handler == NULL;
649
650
651

	memset(sb, 0, SCSI_SENSE_BUFFERSIZE);

652
	cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
653
654
655
656
657

	/*
	 * Use ata_to_sense_error() to map status register bits
	 * onto sense key, asc & ascq.
	 */
658
659
	if (qc->err_mask ||
	    tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo's avatar
Tejun Heo committed
660
		ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
661
				   &sb[1], &sb[2], &sb[3], verbose);
662
		sb[1] &= 0x0f;
Linus Torvalds's avatar
Linus Torvalds committed
663
664
	}

665
666
667
668
	/*
	 * Sense data is current and format is descriptor.
	 */
	sb[0] = 0x72;
Linus Torvalds's avatar
Linus Torvalds committed
669

670
671
	desc[0] = 0x09;

672
673
674
	/* set length of additional sense data */
	sb[7] = 14;
	desc[1] = 12;
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697

	/*
	 * 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
698
	}
699
}
Linus Torvalds's avatar
Linus Torvalds committed
700

701
/**
702
 *	ata_gen_ata_sense - generate a SCSI fixed sense block
703
704
 *	@qc: Command that we are erroring out
 *
705
706
 *	Generate sense block for a failed ATA command @qc.  Descriptor
 *	format is used to accomodate LBA48 block address.
707
708
 *
 *	LOCKING:
709
 *	None.
710
 */
711
static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
712
{
713
	struct ata_device *dev = qc->dev;
714
	struct scsi_cmnd *cmd = qc->scsicmd;
715
	struct ata_taskfile *tf = &qc->result_tf;
716
	unsigned char *sb = cmd->sense_buffer;
717
	unsigned char *desc = sb + 8;
718
	int verbose = qc->ap->ops->error_handler == NULL;
719
	u64 block;
720
721
722

	memset(sb, 0, SCSI_SENSE_BUFFERSIZE);

723
	cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
724

725
726
727
728
	/* sense data is current and format is descriptor */
	sb[0] = 0x72;

	/* Use ata_to_sense_error() to map status register bits
729
730
	 * onto sense key, asc & ascq.
	 */
731
732
	if (qc->err_mask ||
	    tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo's avatar
Tejun Heo committed
733
		ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
734
735
				   &sb[1], &sb[2], &sb[3], verbose);
		sb[1] &= 0x0f;
Linus Torvalds's avatar
Linus Torvalds committed
736
737
	}

738
	block = ata_tf_read_block(&qc->result_tf, dev);
739

740
741
742
743
	/* information sense data descriptor */
	sb[7] = 12;
	desc[0] = 0x00;
	desc[1] = 10;
744

745
746
747
748
749
750
751
	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
752
753
}

754
755
756
757
758
759
760
761
762
static void ata_scsi_sdev_config(struct scsi_device *sdev)
{
	sdev->use_10_for_rw = 1;
	sdev->use_10_for_ms = 1;
}

static void ata_scsi_dev_config(struct scsi_device *sdev,
				struct ata_device *dev)
{
763
764
	/* configure max sectors */
	blk_queue_max_sectors(sdev->request_queue, dev->max_sectors);
765

766
	/* SATA DMA transfers must be multiples of 4 byte, so
767
768
769
770
	 * we need to pad ATAPI transfers using an extra sg.
	 * Decrement max hw segments accordingly.
	 */
	if (dev->class == ATA_DEV_ATAPI) {
771
		struct request_queue *q = sdev->request_queue;
772
773
		blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
	}
774
775
776
777
778
779
780
781

	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);
		scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
	}
782
783
}

Linus Torvalds's avatar
Linus Torvalds committed
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/**
 *	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)
{
798
799
800
	struct ata_port *ap = ata_shost_to_port(sdev->host);
	struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);

801
	ata_scsi_sdev_config(sdev);
Linus Torvalds's avatar
Linus Torvalds committed
802
803
804

	blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);

805
806
	sdev->manage_start_stop = 1;

807
	if (dev)
808
		ata_scsi_dev_config(sdev, dev);
Linus Torvalds's avatar
Linus Torvalds committed
809
810
811
812

	return 0;	/* scsi layer doesn't check return value, sigh */
}

813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/**
 *	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);
	unsigned long flags;
	struct ata_device *dev;

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

836
	spin_lock_irqsave(ap->lock, flags);
837
838
839
840
841
842
843
	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);
	}
844
	spin_unlock_irqrestore(ap->lock, flags);
845
846
}

847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/**
 *	ata_scsi_change_queue_depth - SCSI callback for queue depth config
 *	@sdev: SCSI device to configure queue depth for
 *	@queue_depth: new queue depth
 *
 *	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)
{
	struct ata_port *ap = ata_shost_to_port(sdev->host);
	struct ata_device *dev;
866
	unsigned long flags;
867

868
	if (queue_depth < 1 || queue_depth == sdev->queue_depth)
869
870
871
872
873
874
		return sdev->queue_depth;

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

875
	/* NCQ enabled? */
876
	spin_lock_irqsave(ap->lock, flags);
877
878
	dev->flags &= ~ATA_DFLAG_NCQ_OFF;
	if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
879
		dev->flags |= ATA_DFLAG_NCQ_OFF;
880
881
		queue_depth = 1;
	}
882
883
	spin_unlock_irqrestore(ap->lock, flags);

884
885
886
887
888
889
890
891
892
	/* 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);
893
894
895
	return queue_depth;
}

896
/* XXX: for spindown warning */
897
898
899
900
901
902
903
static void ata_delayed_done_timerfn(unsigned long arg)
{
	struct scsi_cmnd *scmd = (void *)arg;

	scmd->scsi_done(scmd);
}

904
/* XXX: for spindown warning */
905
906
907
908
909
910
911
912
static void ata_delayed_done(struct scsi_cmnd *scmd)
{
	static struct timer_list timer;

	setup_timer(&timer, ata_delayed_done_timerfn, (unsigned long)scmd);
	mod_timer(&timer, jiffies + 5 * HZ);
}

913
914
915
916
917
918
919
920
921
922
/**
 *	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
923
 *	spin_lock_irqsave(host lock)
924
925
926
927
 *
 *	RETURNS:
 *	Zero on success, non-zero on error.
 */
928
static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
929
{
930
	struct scsi_cmnd *scmd = qc->scsicmd;
931
	struct ata_taskfile *tf = &qc->tf;
932
	const u8 *cdb = scmd->cmnd;
933

934
935
936
	if (scmd->cmd_len < 5)
		goto invalid_fld;

937
938
	tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
	tf->protocol = ATA_PROT_NODATA;
939
	if (cdb[1] & 0x1) {
940
941
		;	/* ignore IMMED bit, violates sat-r05 */
	}
942
	if (cdb[4] & 0x2)
943
		goto invalid_fld;       /* LOEJ bit set not supported */
944
	if (((cdb[4] >> 4) & 0xf) != 0)
945
		goto invalid_fld;       /* power conditions not supported */
946
	if (cdb[4] & 0x1) {
947
		tf->nsect = 1;	/* 1 sector, lba=0 */
948
949

		if (qc->dev->flags & ATA_DFLAG_LBA) {
950
			tf->flags |= ATA_TFLAG_LBA;
951
952
953
954
955
956
957
958
959
960
961
962

			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 */
		}

963
		tf->command = ATA_CMD_VERIFY;	/* READ VERIFY */
964
965
966
967
968
	} else {
		/* XXX: This is for backward compatibility, will be
		 * removed.  Read Documentation/feature-removal-schedule.txt
		 * for more info.
		 */
969
		if ((qc->dev->flags & ATA_DFLAG_SPUNDOWN) &&
970
971
		    (system_state == SYSTEM_HALT ||
		     system_state == SYSTEM_POWER_OFF)) {
972
			static unsigned long warned = 0;
973

974
			if (!test_and_set_bit(0, &warned)) {
975
976
977
978
979
980
				ata_dev_printk(qc->dev, KERN_WARNING,
					"DISK MIGHT NOT BE SPUN DOWN PROPERLY. "
					"UPDATE SHUTDOWN UTILITY\n");
				ata_dev_printk(qc->dev, KERN_WARNING,
					"For more info, visit "
					"http://linux-ata.org/shutdown.html\n");
981
982
983
984
985
986

				/* ->scsi_done is not used, use it for
				 * delayed completion.
				 */
				scmd->scsi_done = qc->scsidone;
				qc->scsidone = ata_delayed_done;
987
988
989
990
991
			}
			scmd->result = SAM_STAT_GOOD;
			return 1;
		}

992
993
		/* Issue ATA STANDBY IMMEDIATE command */
		tf->command = ATA_CMD_STANDBYNOW1;
994
	}
995

996
997
998
999
1000
1001
1002
1003
	/*
	 * 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;
1004
1005

invalid_fld:
1006
	ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
1007
1008
	/* "Invalid field in cbd" */
	return 1;
1009
1010
1011
}


Linus Torvalds's avatar
Linus Torvalds committed
1012
1013
1014
1015
1016
1017
1018
1019
/**
 *	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
1020
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
1021
1022
1023
1024
 *
 *	RETURNS:
 *	Zero on success, non-zero on error.
 */
1025
static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds's avatar
Linus Torvalds committed
1026
1027
1028
1029
1030
1031
{
	struct ata_taskfile *tf = &qc->tf;

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

1032
	if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds's avatar
Linus Torvalds committed
1033
1034
1035
1036
1037
1038
1039
		tf->command = ATA_CMD_FLUSH_EXT;
	else
		tf->command = ATA_CMD_FLUSH;

	return 0;
}

1040
1041
/**
 *	scsi_6_lba_len - Get LBA and transfer length
1042
 *	@cdb: SCSI command to translate
1043
1044
1045
1046
1047
1048
1049
 *
 *	Calculate LBA and transfer length for 6-byte commands.
 *
 *	RETURNS:
 *	@plba: the LBA
 *	@plen: the transfer length
 */
1050
static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1051
1052
{
	u64 lba = 0;
1053
	u32 len;
1054
1055
1056

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

1057
	lba |= ((u64)(cdb[1] & 0x1f)) << 16;
1058
1059
	lba |= ((u64)cdb[2]) << 8;
	lba |= ((u64)cdb[3]);
1060

1061
	len = cdb[4];
1062
1063
1064
1065
1066
1067
1068

	*plba = lba;
	*plen = len;
}

/**
 *	scsi_10_lba_len - Get LBA and transfer length
1069
 *	@cdb: SCSI command to translate
1070
1071
1072
1073
1074
1075
1076
 *
 *	Calculate LBA and transfer length for 10-byte commands.
 *
 *	RETURNS:
 *	@plba: the LBA
 *	@plen: the transfer length
 */
1077
static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1078
1079
1080
1081
1082
1083
{
	u64 lba = 0;
	u32 len = 0;

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

1084
1085
1086
1087
	lba |= ((u64)cdb[2]) << 24;
	lba |= ((u64)cdb[3]) << 16;
	lba |= ((u64)cdb[4]) << 8;
	lba |= ((u64)cdb[5]);
1088

1089
1090
	len |= ((u32)cdb[7]) << 8;
	len |= ((u32)cdb[8]);
1091
1092
1093
1094
1095
1096
1097

	*plba = lba;
	*plen = len;
}

/**
 *	scsi_16_lba_len - Get LBA and transfer length
1098
 *	@cdb: SCSI command to translate
1099
1100
1101
1102
1103
1104
1105
 *
 *	Calculate LBA and transfer length for 16-byte commands.
 *
 *	RETURNS:
 *	@plba: the LBA
 *	@plen: the transfer length
 */
1106
static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1107
1108
1109
1110
1111
1112
{
	u64 lba = 0;
	u32 len = 0;

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

1113
1114
1115
1116
1117
1118
1119
1120
	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]);
1121

1122
1123
1124
1125
	len |= ((u32)cdb[10]) << 24;
	len |= ((u32)cdb[11]) << 16;
	len |= ((u32)cdb[12]) << 8;
	len |= ((u32)cdb[13]);
1126
1127
1128
1129
1130

	*plba = lba;
	*plen = len;
}

Linus Torvalds's avatar
Linus Torvalds committed
1131
1132
1133
1134
1135
1136
1137
/**
 *	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
1138
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
1139
1140
1141
1142
 *
 *	RETURNS:
 *	Zero on success, non-zero on error.
 */
1143
static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds's avatar
Linus Torvalds committed
1144
{
1145
	struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds's avatar
Linus Torvalds committed
1146
	struct ata_taskfile *tf = &qc->tf;
1147
	struct ata_device *dev = qc->dev;
Linus Torvalds's avatar
Linus Torvalds committed
1148
	u64 dev_sectors = qc->dev->n_sectors;
1149
	const u8 *cdb = scmd->cmnd;
1150
1151
	u64 block;
	u32 n_block;
Linus Torvalds's avatar
Linus Torvalds committed
1152
1153
1154
1155

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

1156
1157
1158
	if (cdb[0] == VERIFY) {
		if (scmd->cmd_len < 10)
			goto invalid_fld;
1159
		scsi_10_lba_len(cdb, &block, &n_block);
1160
1161
1162
	} else if (cdb[0] == VERIFY_16) {
		if (scmd->cmd_len < 16)
			goto invalid_fld;
1163
		scsi_16_lba_len(cdb, &block, &n_block);
1164
	} else
1165
		goto invalid_fld;
Linus Torvalds's avatar
Linus Torvalds committed
1166

1167
	if (!n_block)
1168
		goto nothing_to_do;
1169
	if (block >= dev_sectors)
1170
		goto out_of_range;
1171
	if ((block + n_block) > dev_sectors)
1172
		goto out_of_range;
Linus Torvalds's avatar
Linus Torvalds committed
1173

1174
1175
1176
	if (dev->flags & ATA_DFLAG_LBA) {
		tf->flags |= ATA_TFLAG_LBA;

1177
1178
1179
1180
1181
1182
1183
		if (lba_28_ok(block, n_block)) {
			/* use LBA28 */
			tf->command = ATA_CMD_VERIFY;
			tf->device |= (block >> 24) & 0xf;
		} else if (lba_48_ok(block, n_block)) {
			if (!(dev->flags & ATA_DFLAG_LBA48))
				goto out_of_range;
1184
1185
1186

			/* use LBA48 */
			tf->flags |= ATA_TFLAG_LBA48;
1187
			tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds's avatar
Linus Torvalds committed
1188

1189
			tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds's avatar
Linus Torvalds committed
1190

1191
1192
1193
			tf->hob_lbah = (block >> 40) & 0xff;
			tf->hob_lbam = (block >> 32) & 0xff;
			tf->hob_lbal = (block >> 24) & 0xff;
1194
1195
1196
		} else
			/* request too large even for LBA48 */
			goto out_of_range;
1197
1198

		tf->nsect = n_block & 0xff;
Linus Torvalds's avatar
Linus Torvalds committed
1199

1200
1201
1202
		tf->lbah = (block >> 16) & 0xff;
		tf->lbam = (block >> 8) & 0xff;
		tf->lbal = block & 0xff;
Linus Torvalds's avatar
Linus Torvalds committed
1203

1204
1205
1206
1207
1208
		tf->device |= ATA_LBA;
	} else {
		/* CHS */
		u32 sect, head, cyl, track;

1209
1210
		if (!lba_28_ok(block, n_block))
			goto out_of_range;
1211

1212
1213
1214
1215
1216
1217
		/* Convert LBA to CHS */
		track = (u32)block / dev->sectors;
		cyl   = track / dev->heads;
		head  = track % dev->heads;
		sect  = (u32)block % dev->sectors + 1;

1218
1219
		DPRINTK("block %u track %u cyl %u head %u sect %u\n",
			(u32)block, track, cyl, head, sect);
1220
1221
1222

		/* Check whether the converted CHS can fit.
		   Cylinder: 0-65535
1223
1224
		   Head: 0-15
		   Sector: 1-255*/
1225
		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1226
			goto out_of_range;
1227

1228
1229
1230
1231
1232
1233
1234
		tf->command = ATA_CMD_VERIFY;
		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
		tf->lbal = sect;
		tf->lbam = cyl;
		tf->lbah = cyl >> 8;
		tf->device |= head;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1235
1236

	return 0;
1237
1238

invalid_fld:
1239
	ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
1240
1241
1242
1243
	/* "Invalid field in cbd" */
	return 1;

out_of_range:
1244
	ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1245
1246
1247
1248
	/* "Logical Block Address out of range" */
	return 1;

nothing_to_do:
1249
	scmd->result = SAM_STAT_GOOD;
1250
	return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
}

/**
 *	ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
 *	@qc: Storage for translated ATA taskfile
 *
 *	Converts any of six SCSI read/write commands into the
 *	ATA counterpart, including starting sector (LBA),
 *	sector count, and taking into account the device's LBA48
 *	support.
 *
 *	Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
 *	%WRITE_16 are currently supported.
 *
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
1266
 *	spin_lock_irqsave(host lock)
Linus Torvalds's avatar
Linus Torvalds committed
1267
1268
1269
1270
 *
 *	RETURNS:
 *	Zero on success, non-zero on error.
 */
1271
static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds's avatar
Linus Torvalds committed
1272
{
1273
	struct scsi_cmnd *scmd = qc->scsicmd;
1274
	const u8 *cdb = scmd->cmnd;
1275
	unsigned int tf_flags = 0;
1276
1277
	u64 block;
	u32 n_block;
1278
	int rc;
Linus Torvalds's avatar
Linus Torvalds committed
1279

1280
	if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
1281
		tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds's avatar
Linus Torvalds committed
1282

Tejun Heo's avatar
Tejun Heo committed
1283
	/* Calculate the SCSI LBA, transfer length and FUA. */
1284
	switch (cdb[0]) {
1285
1286
	case READ_10:
	case WRITE_10:
1287
1288
		if (unlikely(scmd->cmd_len < 10))
			goto invalid_fld;
1289
1290
		scsi_10_lba_len(cdb, &block, &n_block);
		if (unlikely(cdb[1] & (1 << 3)))
1291
			tf_flags |= ATA_TFLAG_FUA;
1292
1293
1294
		break;
	case READ_6:
	case WRITE_6:
1295
1296
		if (unlikely(scmd->cmd_len < 6))
			goto invalid_fld;
1297
		scsi_6_lba_len(cdb, &block, &n_block);
1298
1299
1300
1301

		/* for 6-byte r/w commands, transfer length 0
		 * means 256 blocks of data, not 0 block.
		 */
1302
1303
		if (!n_block)
			n_block = 256;
1304
1305
1306
		break;
	case READ_16:
	case WRITE_16:
1307
1308
		if (unlikely(scmd->cmd_len < 16))
			goto invalid_fld;
1309
1310
		scsi_16_lba_len(cdb, &block, &n_block);
		if (unlikely(cdb[1] & (1 << 3)))
1311
			tf_flags |= ATA_TFLAG_FUA;
1312
1313
		break;
	default:
1314
		DPRINTK("no-byte command\n");
1315
		goto invalid_fld;
Linus Torvalds's avatar
Linus Torvalds committed
1316
1317
	}