[PATCH] libata-eh-fw: update ata_exec_internal() for new EH
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / libata-scsi.c
CommitLineData
1da177e4 1/*
af36d7f0
JG
2 * libata-scsi.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from
31 * - http://www.t10.org/
32 * - http://www.t13.org/
33 *
1da177e4
LT
34 */
35
36#include <linux/kernel.h>
37#include <linux/blkdev.h>
38#include <linux/spinlock.h>
39#include <scsi/scsi.h>
1da177e4 40#include <scsi/scsi_host.h>
85837ebd 41#include <scsi/scsi_eh.h>
005a5a06 42#include <scsi/scsi_device.h>
193515d5 43#include <scsi/scsi_request.h>
30afc84c 44#include <scsi/scsi_transport.h>
1da177e4 45#include <linux/libata.h>
b095518e 46#include <linux/hdreg.h>
1da177e4
LT
47#include <asm/uaccess.h>
48
49#include "libata.h"
50
b095518e
JG
51#define SECTOR_SIZE 512
52
057ace5e 53typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
1da177e4 54static struct ata_device *
057ace5e 55ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
1da177e4 56
00ac37f5
DG
57#define RW_RECOVERY_MPAGE 0x1
58#define RW_RECOVERY_MPAGE_LEN 12
59#define CACHE_MPAGE 0x8
60#define CACHE_MPAGE_LEN 20
61#define CONTROL_MPAGE 0xa
62#define CONTROL_MPAGE_LEN 12
63#define ALL_MPAGES 0x3f
64#define ALL_SUB_MPAGES 0xff
65
66
67static const u8 def_rw_recovery_mpage[] = {
68 RW_RECOVERY_MPAGE,
69 RW_RECOVERY_MPAGE_LEN - 2,
70 (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */
71 (1 << 6), /* ARRE (auto read reallocation) */
72 0, /* read retry count */
73 0, 0, 0, 0,
74 0, /* write retry count */
75 0, 0, 0
76};
77
78static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
79 CACHE_MPAGE,
80 CACHE_MPAGE_LEN - 2,
81 0, /* contains WCE, needs to be 0 for logic */
82 0, 0, 0, 0, 0, 0, 0, 0, 0,
83 0, /* contains DRA, needs to be 0 for logic */
84 0, 0, 0, 0, 0, 0, 0
85};
86
87static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
88 CONTROL_MPAGE,
89 CONTROL_MPAGE_LEN - 2,
90 2, /* DSENSE=0, GLTSD=1 */
91 0, /* [QAM+QERR may be 1, see 05-359r1] */
92 0, 0, 0, 0, 0xff, 0xff,
93 0, 30 /* extended self test time, see 05-359r1 */
94};
95
30afc84c
TH
96/*
97 * libata transport template. libata doesn't do real transport stuff.
98 * It just needs the eh_timed_out hook.
99 */
100struct scsi_transport_template ata_scsi_transport_template = {
9227c33d 101 .eh_strategy_handler = ata_scsi_error,
30afc84c
TH
102 .eh_timed_out = ata_scsi_timed_out,
103};
104
1da177e4 105
ae006510
DG
106static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
107 void (*done)(struct scsi_cmnd *))
108{
109 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
110 /* "Invalid field in cbd" */
111 done(cmd);
112}
113
1da177e4
LT
114/**
115 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
116 * @sdev: SCSI device for which BIOS geometry is to be determined
117 * @bdev: block device associated with @sdev
118 * @capacity: capacity of SCSI device
119 * @geom: location to which geometry will be output
120 *
121 * Generic bios head/sector/cylinder calculator
122 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
123 * mapping. Some situations may arise where the disk is not
124 * bootable if this is not used.
125 *
126 * LOCKING:
127 * Defined by the SCSI layer. We don't really care.
128 *
129 * RETURNS:
130 * Zero.
131 */
132int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
133 sector_t capacity, int geom[])
134{
135 geom[0] = 255;
136 geom[1] = 63;
137 sector_div(capacity, 255*63);
138 geom[2] = capacity;
139
140 return 0;
141}
142
b095518e
JG
143/**
144 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
8e8b77dd 145 * @scsidev: Device to which we are issuing command
b095518e
JG
146 * @arg: User provided data for issuing command
147 *
148 * LOCKING:
149 * Defined by the SCSI layer. We don't really care.
150 *
151 * RETURNS:
152 * Zero on success, negative errno on error.
153 */
154
155int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
156{
157 int rc = 0;
158 u8 scsi_cmd[MAX_COMMAND_SIZE];
159 u8 args[4], *argbuf = NULL;
160 int argsize = 0;
85837ebd
MC
161 struct scsi_sense_hdr sshdr;
162 enum dma_data_direction data_dir;
b095518e 163
c893a3ae 164 if (arg == NULL)
b095518e
JG
165 return -EINVAL;
166
167 if (copy_from_user(args, arg, sizeof(args)))
168 return -EFAULT;
169
b095518e
JG
170 memset(scsi_cmd, 0, sizeof(scsi_cmd));
171
172 if (args[3]) {
173 argsize = SECTOR_SIZE * args[3];
174 argbuf = kmalloc(argsize, GFP_KERNEL);
54dac83c
JR
175 if (argbuf == NULL) {
176 rc = -ENOMEM;
177 goto error;
178 }
b095518e
JG
179
180 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
181 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
182 block count in sector count field */
85837ebd 183 data_dir = DMA_FROM_DEVICE;
b095518e
JG
184 } else {
185 scsi_cmd[1] = (3 << 1); /* Non-data */
186 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
85837ebd 187 data_dir = DMA_NONE;
b095518e
JG
188 }
189
190 scsi_cmd[0] = ATA_16;
191
192 scsi_cmd[4] = args[2];
193 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
194 scsi_cmd[6] = args[3];
195 scsi_cmd[8] = args[1];
196 scsi_cmd[10] = 0x4f;
197 scsi_cmd[12] = 0xc2;
198 } else {
199 scsi_cmd[6] = args[1];
200 }
201 scsi_cmd[14] = args[0];
202
203 /* Good values for timeout and retries? Values below
204 from scsi_ioctl_send_command() for default case... */
85837ebd
MC
205 if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize,
206 &sshdr, (10*HZ), 5)) {
b095518e
JG
207 rc = -EIO;
208 goto error;
209 }
210
211 /* Need code to retrieve data from check condition? */
212
213 if ((argbuf)
c893a3ae 214 && copy_to_user(arg + sizeof(args), argbuf, argsize))
b095518e
JG
215 rc = -EFAULT;
216error:
b095518e
JG
217 if (argbuf)
218 kfree(argbuf);
219
220 return rc;
221}
222
223/**
224 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
8e8b77dd 225 * @scsidev: Device to which we are issuing command
b095518e
JG
226 * @arg: User provided data for issuing command
227 *
228 * LOCKING:
229 * Defined by the SCSI layer. We don't really care.
230 *
231 * RETURNS:
232 * Zero on success, negative errno on error.
233 */
234int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
235{
236 int rc = 0;
237 u8 scsi_cmd[MAX_COMMAND_SIZE];
238 u8 args[7];
85837ebd 239 struct scsi_sense_hdr sshdr;
b095518e 240
c893a3ae 241 if (arg == NULL)
b095518e
JG
242 return -EINVAL;
243
244 if (copy_from_user(args, arg, sizeof(args)))
245 return -EFAULT;
246
247 memset(scsi_cmd, 0, sizeof(scsi_cmd));
248 scsi_cmd[0] = ATA_16;
249 scsi_cmd[1] = (3 << 1); /* Non-data */
250 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
251 scsi_cmd[4] = args[1];
252 scsi_cmd[6] = args[2];
253 scsi_cmd[8] = args[3];
254 scsi_cmd[10] = args[4];
255 scsi_cmd[12] = args[5];
256 scsi_cmd[14] = args[0];
257
b095518e 258 /* Good values for timeout and retries? Values below
2e9edbf8 259 from scsi_ioctl_send_command() for default case... */
85837ebd
MC
260 if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr,
261 (10*HZ), 5))
b095518e 262 rc = -EIO;
b095518e
JG
263
264 /* Need code to retrieve data from check condition? */
b095518e
JG
265 return rc;
266}
267
1da177e4
LT
268int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
269{
1da177e4
LT
270 int val = -EINVAL, rc = -EINVAL;
271
1da177e4
LT
272 switch (cmd) {
273 case ATA_IOC_GET_IO32:
274 val = 0;
275 if (copy_to_user(arg, &val, 1))
276 return -EFAULT;
277 return 0;
278
279 case ATA_IOC_SET_IO32:
280 val = (unsigned long) arg;
281 if (val != 0)
282 return -EINVAL;
283 return 0;
284
b095518e
JG
285 case HDIO_DRIVE_CMD:
286 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
287 return -EACCES;
288 return ata_cmd_ioctl(scsidev, arg);
289
290 case HDIO_DRIVE_TASK:
291 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
292 return -EACCES;
293 return ata_task_ioctl(scsidev, arg);
294
1da177e4
LT
295 default:
296 rc = -ENOTTY;
297 break;
298 }
299
1da177e4
LT
300 return rc;
301}
302
303/**
304 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
1da177e4
LT
305 * @dev: ATA device to which the new command is attached
306 * @cmd: SCSI command that originated this ATA command
307 * @done: SCSI command completion function
308 *
309 * Obtain a reference to an unused ata_queued_cmd structure,
310 * which is the basic libata structure representing a single
311 * ATA command sent to the hardware.
312 *
313 * If a command was available, fill in the SCSI-specific
314 * portions of the structure with information on the
315 * current command.
316 *
317 * LOCKING:
318 * spin_lock_irqsave(host_set lock)
319 *
320 * RETURNS:
321 * Command allocated, or %NULL if none available.
322 */
3373efd8 323struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
1da177e4
LT
324 struct scsi_cmnd *cmd,
325 void (*done)(struct scsi_cmnd *))
326{
327 struct ata_queued_cmd *qc;
328
3373efd8 329 qc = ata_qc_new_init(dev);
1da177e4
LT
330 if (qc) {
331 qc->scsicmd = cmd;
332 qc->scsidone = done;
333
334 if (cmd->use_sg) {
cedc9a47 335 qc->__sg = (struct scatterlist *) cmd->request_buffer;
1da177e4
LT
336 qc->n_elem = cmd->use_sg;
337 } else {
cedc9a47 338 qc->__sg = &qc->sgent;
1da177e4
LT
339 qc->n_elem = 1;
340 }
341 } else {
342 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
343 done(cmd);
344 }
345
346 return qc;
347}
348
b095518e
JG
349/**
350 * ata_dump_status - user friendly display of error info
351 * @id: id of the port in question
352 * @tf: ptr to filled out taskfile
353 *
354 * Decode and dump the ATA error/status registers for the user so
355 * that they have some idea what really happened at the non
356 * make-believe layer.
357 *
358 * LOCKING:
359 * inherited from caller
360 */
361void ata_dump_status(unsigned id, struct ata_taskfile *tf)
362{
363 u8 stat = tf->command, err = tf->feature;
364
365 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
366 if (stat & ATA_BUSY) {
367 printk("Busy }\n"); /* Data is not valid in this case */
368 } else {
369 if (stat & 0x40) printk("DriveReady ");
370 if (stat & 0x20) printk("DeviceFault ");
371 if (stat & 0x10) printk("SeekComplete ");
372 if (stat & 0x08) printk("DataRequest ");
373 if (stat & 0x04) printk("CorrectedError ");
374 if (stat & 0x02) printk("Index ");
375 if (stat & 0x01) printk("Error ");
376 printk("}\n");
377
378 if (err) {
379 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
380 if (err & 0x04) printk("DriveStatusError ");
381 if (err & 0x80) {
382 if (err & 0x04) printk("BadCRC ");
383 else printk("Sector ");
384 }
385 if (err & 0x40) printk("UncorrectableError ");
386 if (err & 0x10) printk("SectorIdNotFound ");
387 if (err & 0x02) printk("TrackZeroNotFound ");
388 if (err & 0x01) printk("AddrMarkNotFound ");
389 printk("}\n");
390 }
391 }
392}
393
9b847548
JA
394int ata_scsi_device_resume(struct scsi_device *sdev)
395{
35bb94b1 396 struct ata_port *ap = ata_shost_to_port(sdev->host);
9b847548
JA
397 struct ata_device *dev = &ap->device[sdev->id];
398
3373efd8 399 return ata_device_resume(dev);
9b847548
JA
400}
401
082776e4 402int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
9b847548 403{
35bb94b1 404 struct ata_port *ap = ata_shost_to_port(sdev->host);
9b847548
JA
405 struct ata_device *dev = &ap->device[sdev->id];
406
3373efd8 407 return ata_device_suspend(dev, state);
9b847548
JA
408}
409
1da177e4
LT
410/**
411 * ata_to_sense_error - convert ATA error to SCSI error
8e8b77dd 412 * @id: ATA device number
1da177e4 413 * @drv_stat: value contained in ATA status register
b095518e
JG
414 * @drv_err: value contained in ATA error register
415 * @sk: the sense key we'll fill out
416 * @asc: the additional sense code we'll fill out
417 * @ascq: the additional sense code qualifier we'll fill out
1da177e4 418 *
b095518e
JG
419 * Converts an ATA error into a SCSI error. Fill out pointers to
420 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
421 * format sense blocks.
1da177e4
LT
422 *
423 * LOCKING:
424 * spin_lock_irqsave(host_set lock)
425 */
2e9edbf8 426void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
b095518e 427 u8 *ascq)
1da177e4 428{
b095518e 429 int i;
ffe75ef6 430
1da177e4 431 /* Based on the 3ware driver translation table */
98ac62de 432 static const unsigned char sense_table[][4] = {
1da177e4
LT
433 /* BBD|ECC|ID|MAR */
434 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
435 /* BBD|ECC|ID */
436 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
437 /* ECC|MC|MARK */
438 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
439 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
440 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
441 /* MC|ID|ABRT|TRK0|MARK */
442 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
443 /* MCR|MARK */
444 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
445 /* Bad address mark */
446 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
447 /* TRK0 */
448 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
449 /* Abort & !ICRC */
450 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
451 /* Media change request */
452 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
453 /* SRV */
454 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
455 /* Media change */
456 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
457 /* ECC */
458 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
459 /* BBD - block marked bad */
460 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
461 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
462 };
98ac62de 463 static const unsigned char stat_table[][4] = {
1da177e4
LT
464 /* Must be first because BUSY means no other bits valid */
465 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
466 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
467 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
468 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
469 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
470 };
1da177e4 471
1da177e4
LT
472 /*
473 * Is this an error we can process/parse
474 */
b095518e
JG
475 if (drv_stat & ATA_BUSY) {
476 drv_err = 0; /* Ignore the err bits, they're invalid */
1da177e4 477 }
b095518e
JG
478
479 if (drv_err) {
480 /* Look for drv_err */
481 for (i = 0; sense_table[i][0] != 0xFF; i++) {
482 /* Look for best matches first */
2e9edbf8 483 if ((sense_table[i][0] & drv_err) ==
b095518e
JG
484 sense_table[i][0]) {
485 *sk = sense_table[i][1];
486 *asc = sense_table[i][2];
487 *ascq = sense_table[i][3];
488 goto translate_done;
489 }
490 }
491 /* No immediate match */
492 printk(KERN_WARNING "ata%u: no sense translation for "
493 "error 0x%02x\n", id, drv_err);
1da177e4 494 }
b095518e
JG
495
496 /* Fall back to interpreting status bits */
497 for (i = 0; stat_table[i][0] != 0xFF; i++) {
498 if (stat_table[i][0] & drv_stat) {
499 *sk = stat_table[i][1];
500 *asc = stat_table[i][2];
501 *ascq = stat_table[i][3];
502 goto translate_done;
1da177e4 503 }
b095518e
JG
504 }
505 /* No error? Undecoded? */
2e9edbf8 506 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
b095518e
JG
507 id, drv_stat);
508
2d202024
AC
509 /* We need a sensible error return here, which is tricky, and one
510 that won't cause people to do things like return a disk wrongly */
511 *sk = ABORTED_COMMAND;
512 *asc = 0x00;
513 *ascq = 0x00;
b095518e
JG
514
515 translate_done:
516 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
517 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
518 *sk, *asc, *ascq);
519 return;
520}
521
522/*
523 * ata_gen_ata_desc_sense - Generate check condition sense block.
524 * @qc: Command that completed.
525 *
526 * This function is specific to the ATA descriptor format sense
527 * block specified for the ATA pass through commands. Regardless
528 * of whether the command errored or not, return a sense
529 * block. Copy all controller registers into the sense
530 * block. Clear sense key, ASC & ASCQ if there is no error.
531 *
532 * LOCKING:
533 * spin_lock_irqsave(host_set lock)
534 */
535void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
536{
537 struct scsi_cmnd *cmd = qc->scsicmd;
e61e0672 538 struct ata_taskfile *tf = &qc->result_tf;
b095518e
JG
539 unsigned char *sb = cmd->sense_buffer;
540 unsigned char *desc = sb + 8;
541
542 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
543
0e5dec47 544 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
b095518e 545
b095518e
JG
546 /*
547 * Use ata_to_sense_error() to map status register bits
548 * onto sense key, asc & ascq.
549 */
058e55e1
TH
550 if (qc->err_mask ||
551 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
b095518e
JG
552 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
553 &sb[1], &sb[2], &sb[3]);
554 sb[1] &= 0x0f;
1da177e4
LT
555 }
556
b095518e
JG
557 /*
558 * Sense data is current and format is descriptor.
559 */
560 sb[0] = 0x72;
1da177e4 561
b095518e
JG
562 desc[0] = 0x09;
563
564 /*
565 * Set length of additional sense data.
566 * Since we only populate descriptor 0, the total
567 * length is the same (fixed) length as descriptor 0.
568 */
569 desc[1] = sb[7] = 14;
570
571 /*
572 * Copy registers into sense buffer.
573 */
574 desc[2] = 0x00;
575 desc[3] = tf->feature; /* == error reg */
576 desc[5] = tf->nsect;
577 desc[7] = tf->lbal;
578 desc[9] = tf->lbam;
579 desc[11] = tf->lbah;
580 desc[12] = tf->device;
581 desc[13] = tf->command; /* == status reg */
582
583 /*
584 * Fill in Extend bit, and the high order bytes
585 * if applicable.
586 */
587 if (tf->flags & ATA_TFLAG_LBA48) {
588 desc[2] |= 0x01;
589 desc[4] = tf->hob_nsect;
590 desc[6] = tf->hob_lbal;
591 desc[8] = tf->hob_lbam;
592 desc[10] = tf->hob_lbah;
1da177e4 593 }
b095518e 594}
1da177e4 595
b095518e
JG
596/**
597 * ata_gen_fixed_sense - generate a SCSI fixed sense block
598 * @qc: Command that we are erroring out
599 *
600 * Leverage ata_to_sense_error() to give us the codes. Fit our
601 * LBA in here if there's room.
602 *
603 * LOCKING:
604 * inherited from caller
605 */
606void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
607{
608 struct scsi_cmnd *cmd = qc->scsicmd;
e61e0672 609 struct ata_taskfile *tf = &qc->result_tf;
b095518e
JG
610 unsigned char *sb = cmd->sense_buffer;
611
612 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
613
0e5dec47 614 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
b095518e 615
b095518e
JG
616 /*
617 * Use ata_to_sense_error() to map status register bits
618 * onto sense key, asc & ascq.
619 */
058e55e1
TH
620 if (qc->err_mask ||
621 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
b095518e
JG
622 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
623 &sb[2], &sb[12], &sb[13]);
624 sb[2] &= 0x0f;
1da177e4 625 }
1da177e4
LT
626
627 sb[0] = 0x70;
b095518e 628 sb[7] = 0x0a;
0274aa25 629
a7dac447
JG
630 if (tf->flags & ATA_TFLAG_LBA48) {
631 /* TODO: find solution for LBA48 descriptors */
632 }
633
634 else if (tf->flags & ATA_TFLAG_LBA) {
b095518e
JG
635 /* A small (28b) LBA will fit in the 32b info field */
636 sb[0] |= 0x80; /* set valid bit */
637 sb[3] = tf->device & 0x0f;
638 sb[4] = tf->lbah;
639 sb[5] = tf->lbam;
640 sb[6] = tf->lbal;
1da177e4 641 }
a7dac447
JG
642
643 else {
644 /* TODO: C/H/S */
645 }
1da177e4
LT
646}
647
a6cce2a7
BK
648static void ata_scsi_sdev_config(struct scsi_device *sdev)
649{
650 sdev->use_10_for_rw = 1;
651 sdev->use_10_for_ms = 1;
652}
653
654static void ata_scsi_dev_config(struct scsi_device *sdev,
655 struct ata_device *dev)
656{
657 unsigned int max_sectors;
658
659 /* TODO: 2048 is an arbitrary number, not the
660 * hardware maximum. This should be increased to
661 * 65534 when Jens Axboe's patch for dynamically
662 * determining max_sectors is merged.
663 */
664 max_sectors = ATA_MAX_SECTORS;
665 if (dev->flags & ATA_DFLAG_LBA48)
666 max_sectors = 2048;
667 if (dev->max_sectors)
668 max_sectors = dev->max_sectors;
669
670 blk_queue_max_sectors(sdev->request_queue, max_sectors);
671
672 /*
673 * SATA DMA transfers must be multiples of 4 byte, so
674 * we need to pad ATAPI transfers using an extra sg.
675 * Decrement max hw segments accordingly.
676 */
677 if (dev->class == ATA_DEV_ATAPI) {
678 request_queue_t *q = sdev->request_queue;
679 blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
680 }
681}
682
1da177e4
LT
683/**
684 * ata_scsi_slave_config - Set SCSI device attributes
685 * @sdev: SCSI device to examine
686 *
687 * This is called before we actually start reading
688 * and writing to the device, to configure certain
689 * SCSI mid-layer behaviors.
690 *
691 * LOCKING:
692 * Defined by SCSI layer. We don't really care.
693 */
694
695int ata_scsi_slave_config(struct scsi_device *sdev)
696{
a6cce2a7 697 ata_scsi_sdev_config(sdev);
1da177e4
LT
698
699 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
700
701 if (sdev->id < ATA_MAX_DEVICES) {
702 struct ata_port *ap;
703 struct ata_device *dev;
704
35bb94b1 705 ap = ata_shost_to_port(sdev->host);
1da177e4
LT
706 dev = &ap->device[sdev->id];
707
a6cce2a7 708 ata_scsi_dev_config(sdev, dev);
1da177e4
LT
709 }
710
711 return 0; /* scsi layer doesn't check return value, sigh */
712}
713
972dcafb
DG
714/**
715 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
716 * @qc: Storage for translated ATA taskfile
717 * @scsicmd: SCSI command to translate
718 *
719 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
720 * (to start). Perhaps these commands should be preceded by
721 * CHECK POWER MODE to see what power mode the device is already in.
722 * [See SAT revision 5 at www.t10.org]
723 *
724 * LOCKING:
725 * spin_lock_irqsave(host_set lock)
726 *
727 * RETURNS:
728 * Zero on success, non-zero on error.
729 */
730
731static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
057ace5e 732 const u8 *scsicmd)
972dcafb
DG
733{
734 struct ata_taskfile *tf = &qc->tf;
735
736 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
737 tf->protocol = ATA_PROT_NODATA;
738 if (scsicmd[1] & 0x1) {
739 ; /* ignore IMMED bit, violates sat-r05 */
740 }
741 if (scsicmd[4] & 0x2)
ae006510 742 goto invalid_fld; /* LOEJ bit set not supported */
972dcafb 743 if (((scsicmd[4] >> 4) & 0xf) != 0)
ae006510 744 goto invalid_fld; /* power conditions not supported */
972dcafb
DG
745 if (scsicmd[4] & 0x1) {
746 tf->nsect = 1; /* 1 sector, lba=0 */
9d5b1302
AL
747
748 if (qc->dev->flags & ATA_DFLAG_LBA) {
c44078c0 749 tf->flags |= ATA_TFLAG_LBA;
9d5b1302
AL
750
751 tf->lbah = 0x0;
752 tf->lbam = 0x0;
753 tf->lbal = 0x0;
754 tf->device |= ATA_LBA;
755 } else {
756 /* CHS */
757 tf->lbal = 0x1; /* sect */
758 tf->lbam = 0x0; /* cyl low */
759 tf->lbah = 0x0; /* cyl high */
760 }
761
972dcafb
DG
762 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
763 } else {
764 tf->nsect = 0; /* time period value (0 implies now) */
765 tf->command = ATA_CMD_STANDBY;
766 /* Consider: ATA STANDBY IMMEDIATE command */
767 }
768 /*
769 * Standby and Idle condition timers could be implemented but that
770 * would require libata to implement the Power condition mode page
771 * and allow the user to change it. Changing mode pages requires
772 * MODE SELECT to be implemented.
773 */
774
775 return 0;
ae006510
DG
776
777invalid_fld:
778 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
779 /* "Invalid field in cbd" */
780 return 1;
972dcafb
DG
781}
782
783
1da177e4
LT
784/**
785 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
786 * @qc: Storage for translated ATA taskfile
787 * @scsicmd: SCSI command to translate (ignored)
788 *
789 * Sets up an ATA taskfile to issue FLUSH CACHE or
790 * FLUSH CACHE EXT.
791 *
792 * LOCKING:
793 * spin_lock_irqsave(host_set lock)
794 *
795 * RETURNS:
796 * Zero on success, non-zero on error.
797 */
798
057ace5e 799static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
1da177e4
LT
800{
801 struct ata_taskfile *tf = &qc->tf;
802
803 tf->flags |= ATA_TFLAG_DEVICE;
804 tf->protocol = ATA_PROT_NODATA;
805
07506697 806 if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
1da177e4
LT
807 (ata_id_has_flush_ext(qc->dev->id)))
808 tf->command = ATA_CMD_FLUSH_EXT;
809 else
810 tf->command = ATA_CMD_FLUSH;
811
812 return 0;
813}
814
3aef5231
AL
815/**
816 * scsi_6_lba_len - Get LBA and transfer length
817 * @scsicmd: SCSI command to translate
818 *
819 * Calculate LBA and transfer length for 6-byte commands.
820 *
821 * RETURNS:
822 * @plba: the LBA
823 * @plen: the transfer length
824 */
825
057ace5e 826static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
3aef5231
AL
827{
828 u64 lba = 0;
829 u32 len = 0;
830
831 VPRINTK("six-byte command\n");
832
833 lba |= ((u64)scsicmd[2]) << 8;
834 lba |= ((u64)scsicmd[3]);
835
836 len |= ((u32)scsicmd[4]);
837
838 *plba = lba;
839 *plen = len;
840}
841
842/**
843 * scsi_10_lba_len - Get LBA and transfer length
844 * @scsicmd: SCSI command to translate
845 *
846 * Calculate LBA and transfer length for 10-byte commands.
847 *
848 * RETURNS:
849 * @plba: the LBA
850 * @plen: the transfer length
851 */
852
057ace5e 853static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
3aef5231
AL
854{
855 u64 lba = 0;
856 u32 len = 0;
857
858 VPRINTK("ten-byte command\n");
859
860 lba |= ((u64)scsicmd[2]) << 24;
861 lba |= ((u64)scsicmd[3]) << 16;
862 lba |= ((u64)scsicmd[4]) << 8;
863 lba |= ((u64)scsicmd[5]);
864
865 len |= ((u32)scsicmd[7]) << 8;
866 len |= ((u32)scsicmd[8]);
867
868 *plba = lba;
869 *plen = len;
870}
871
872/**
873 * scsi_16_lba_len - Get LBA and transfer length
874 * @scsicmd: SCSI command to translate
875 *
876 * Calculate LBA and transfer length for 16-byte commands.
877 *
878 * RETURNS:
879 * @plba: the LBA
880 * @plen: the transfer length
881 */
882
057ace5e 883static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
3aef5231
AL
884{
885 u64 lba = 0;
886 u32 len = 0;
887
888 VPRINTK("sixteen-byte command\n");
889
890 lba |= ((u64)scsicmd[2]) << 56;
891 lba |= ((u64)scsicmd[3]) << 48;
892 lba |= ((u64)scsicmd[4]) << 40;
893 lba |= ((u64)scsicmd[5]) << 32;
894 lba |= ((u64)scsicmd[6]) << 24;
895 lba |= ((u64)scsicmd[7]) << 16;
896 lba |= ((u64)scsicmd[8]) << 8;
897 lba |= ((u64)scsicmd[9]);
898
899 len |= ((u32)scsicmd[10]) << 24;
900 len |= ((u32)scsicmd[11]) << 16;
901 len |= ((u32)scsicmd[12]) << 8;
902 len |= ((u32)scsicmd[13]);
903
904 *plba = lba;
905 *plen = len;
906}
907
1da177e4
LT
908/**
909 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
910 * @qc: Storage for translated ATA taskfile
911 * @scsicmd: SCSI command to translate
912 *
913 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
914 *
915 * LOCKING:
916 * spin_lock_irqsave(host_set lock)
917 *
918 * RETURNS:
919 * Zero on success, non-zero on error.
920 */
921
057ace5e 922static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
1da177e4
LT
923{
924 struct ata_taskfile *tf = &qc->tf;
8bf62ece 925 struct ata_device *dev = qc->dev;
1da177e4 926 u64 dev_sectors = qc->dev->n_sectors;
3aef5231
AL
927 u64 block;
928 u32 n_block;
1da177e4
LT
929
930 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
931 tf->protocol = ATA_PROT_NODATA;
1da177e4 932
3aef5231
AL
933 if (scsicmd[0] == VERIFY)
934 scsi_10_lba_len(scsicmd, &block, &n_block);
935 else if (scsicmd[0] == VERIFY_16)
936 scsi_16_lba_len(scsicmd, &block, &n_block);
1da177e4 937 else
ae006510 938 goto invalid_fld;
1da177e4 939
8bf62ece 940 if (!n_block)
ae006510 941 goto nothing_to_do;
8bf62ece 942 if (block >= dev_sectors)
ae006510 943 goto out_of_range;
8bf62ece 944 if ((block + n_block) > dev_sectors)
ae006510 945 goto out_of_range;
1da177e4 946
07506697
AL
947 if (dev->flags & ATA_DFLAG_LBA) {
948 tf->flags |= ATA_TFLAG_LBA;
949
c6a33e24
AL
950 if (lba_28_ok(block, n_block)) {
951 /* use LBA28 */
952 tf->command = ATA_CMD_VERIFY;
953 tf->device |= (block >> 24) & 0xf;
954 } else if (lba_48_ok(block, n_block)) {
955 if (!(dev->flags & ATA_DFLAG_LBA48))
956 goto out_of_range;
07506697
AL
957
958 /* use LBA48 */
959 tf->flags |= ATA_TFLAG_LBA48;
8bf62ece 960 tf->command = ATA_CMD_VERIFY_EXT;
1da177e4 961
8bf62ece 962 tf->hob_nsect = (n_block >> 8) & 0xff;
1da177e4 963
8bf62ece
AL
964 tf->hob_lbah = (block >> 40) & 0xff;
965 tf->hob_lbam = (block >> 32) & 0xff;
966 tf->hob_lbal = (block >> 24) & 0xff;
c6a33e24
AL
967 } else
968 /* request too large even for LBA48 */
969 goto out_of_range;
8bf62ece
AL
970
971 tf->nsect = n_block & 0xff;
1da177e4 972
8bf62ece
AL
973 tf->lbah = (block >> 16) & 0xff;
974 tf->lbam = (block >> 8) & 0xff;
975 tf->lbal = block & 0xff;
1da177e4 976
8bf62ece
AL
977 tf->device |= ATA_LBA;
978 } else {
979 /* CHS */
980 u32 sect, head, cyl, track;
981
c6a33e24
AL
982 if (!lba_28_ok(block, n_block))
983 goto out_of_range;
07506697 984
8bf62ece
AL
985 /* Convert LBA to CHS */
986 track = (u32)block / dev->sectors;
987 cyl = track / dev->heads;
988 head = track % dev->heads;
989 sect = (u32)block % dev->sectors + 1;
990
c187c4b5
AL
991 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
992 (u32)block, track, cyl, head, sect);
2e9edbf8
JG
993
994 /* Check whether the converted CHS can fit.
995 Cylinder: 0-65535
8bf62ece
AL
996 Head: 0-15
997 Sector: 1-255*/
2e9edbf8 998 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
ae006510 999 goto out_of_range;
2e9edbf8 1000
8bf62ece
AL
1001 tf->command = ATA_CMD_VERIFY;
1002 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1003 tf->lbal = sect;
1004 tf->lbam = cyl;
1005 tf->lbah = cyl >> 8;
1006 tf->device |= head;
1007 }
1da177e4
LT
1008
1009 return 0;
ae006510
DG
1010
1011invalid_fld:
1012 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1013 /* "Invalid field in cbd" */
1014 return 1;
1015
1016out_of_range:
1017 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1018 /* "Logical Block Address out of range" */
1019 return 1;
1020
1021nothing_to_do:
1022 qc->scsicmd->result = SAM_STAT_GOOD;
1023 return 1;
1da177e4
LT
1024}
1025
1026/**
1027 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1028 * @qc: Storage for translated ATA taskfile
1029 * @scsicmd: SCSI command to translate
1030 *
1031 * Converts any of six SCSI read/write commands into the
1032 * ATA counterpart, including starting sector (LBA),
1033 * sector count, and taking into account the device's LBA48
1034 * support.
1035 *
1036 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1037 * %WRITE_16 are currently supported.
1038 *
1039 * LOCKING:
1040 * spin_lock_irqsave(host_set lock)
1041 *
1042 * RETURNS:
1043 * Zero on success, non-zero on error.
1044 */
1045
057ace5e 1046static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
1da177e4
LT
1047{
1048 struct ata_taskfile *tf = &qc->tf;
8bf62ece 1049 struct ata_device *dev = qc->dev;
3aef5231
AL
1050 u64 block;
1051 u32 n_block;
1da177e4 1052
27197367 1053 qc->flags |= ATA_QCFLAG_IO;
1da177e4 1054 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1da177e4 1055
8cbd6df1
AL
1056 if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
1057 scsicmd[0] == WRITE_16)
1da177e4 1058 tf->flags |= ATA_TFLAG_WRITE;
1da177e4 1059
9a3dccc4 1060 /* Calculate the SCSI LBA, transfer length and FUA. */
3aef5231
AL
1061 switch (scsicmd[0]) {
1062 case READ_10:
1063 case WRITE_10:
1064 scsi_10_lba_len(scsicmd, &block, &n_block);
9a3dccc4
TH
1065 if (unlikely(scsicmd[1] & (1 << 3)))
1066 tf->flags |= ATA_TFLAG_FUA;
3aef5231
AL
1067 break;
1068 case READ_6:
1069 case WRITE_6:
1070 scsi_6_lba_len(scsicmd, &block, &n_block);
c187c4b5
AL
1071
1072 /* for 6-byte r/w commands, transfer length 0
1073 * means 256 blocks of data, not 0 block.
1074 */
76b2bf9b
JG
1075 if (!n_block)
1076 n_block = 256;
3aef5231
AL
1077 break;
1078 case READ_16:
1079 case WRITE_16:
1080 scsi_16_lba_len(scsicmd, &block, &n_block);
9a3dccc4
TH
1081 if (unlikely(scsicmd[1] & (1 << 3)))
1082 tf->flags |= ATA_TFLAG_FUA;
3aef5231
AL
1083 break;
1084 default:
8bf62ece 1085 DPRINTK("no-byte command\n");
ae006510 1086 goto invalid_fld;
1da177e4
LT
1087 }
1088
8bf62ece
AL
1089 /* Check and compose ATA command */
1090 if (!n_block)
c187c4b5
AL
1091 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1092 * length 0 means transfer 0 block of data.
1093 * However, for ATA R/W commands, sector count 0 means
1094 * 256 or 65536 sectors, not 0 sectors as in SCSI.
f51750d5
AC
1095 *
1096 * WARNING: one or two older ATA drives treat 0 as 0...
c187c4b5 1097 */
ae006510 1098 goto nothing_to_do;
1da177e4 1099
07506697
AL
1100 if (dev->flags & ATA_DFLAG_LBA) {
1101 tf->flags |= ATA_TFLAG_LBA;
1102
c6a33e24
AL
1103 if (lba_28_ok(block, n_block)) {
1104 /* use LBA28 */
1105 tf->device |= (block >> 24) & 0xf;
1106 } else if (lba_48_ok(block, n_block)) {
1107 if (!(dev->flags & ATA_DFLAG_LBA48))
ae006510 1108 goto out_of_range;
1da177e4 1109
07506697
AL
1110 /* use LBA48 */
1111 tf->flags |= ATA_TFLAG_LBA48;
1112
8bf62ece
AL
1113 tf->hob_nsect = (n_block >> 8) & 0xff;
1114
1115 tf->hob_lbah = (block >> 40) & 0xff;
1116 tf->hob_lbam = (block >> 32) & 0xff;
1117 tf->hob_lbal = (block >> 24) & 0xff;
c6a33e24
AL
1118 } else
1119 /* request too large even for LBA48 */
1120 goto out_of_range;
c187c4b5 1121
9a3dccc4
TH
1122 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1123 goto invalid_fld;
8cbd6df1 1124
8bf62ece
AL
1125 qc->nsect = n_block;
1126 tf->nsect = n_block & 0xff;
1da177e4 1127
8bf62ece
AL
1128 tf->lbah = (block >> 16) & 0xff;
1129 tf->lbam = (block >> 8) & 0xff;
1130 tf->lbal = block & 0xff;
1da177e4 1131
8bf62ece 1132 tf->device |= ATA_LBA;
2e9edbf8 1133 } else {
8bf62ece
AL
1134 /* CHS */
1135 u32 sect, head, cyl, track;
1136
1137 /* The request -may- be too large for CHS addressing. */
c6a33e24 1138 if (!lba_28_ok(block, n_block))
ae006510 1139 goto out_of_range;
c187c4b5 1140
9a3dccc4
TH
1141 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1142 goto invalid_fld;
8cbd6df1 1143
8bf62ece
AL
1144 /* Convert LBA to CHS */
1145 track = (u32)block / dev->sectors;
1146 cyl = track / dev->heads;
1147 head = track % dev->heads;
1148 sect = (u32)block % dev->sectors + 1;
1149
c187c4b5 1150 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
8bf62ece 1151 (u32)block, track, cyl, head, sect);
c187c4b5 1152
2e9edbf8
JG
1153 /* Check whether the converted CHS can fit.
1154 Cylinder: 0-65535
8bf62ece
AL
1155 Head: 0-15
1156 Sector: 1-255*/
c187c4b5 1157 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
ae006510 1158 goto out_of_range;
c187c4b5 1159
8bf62ece
AL
1160 qc->nsect = n_block;
1161 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1162 tf->lbal = sect;
1163 tf->lbam = cyl;
1164 tf->lbah = cyl >> 8;
1165 tf->device |= head;
1da177e4
LT
1166 }
1167
8bf62ece 1168 return 0;
ae006510
DG
1169
1170invalid_fld:
1171 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1172 /* "Invalid field in cbd" */
1173 return 1;
1174
1175out_of_range:
1176 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1177 /* "Logical Block Address out of range" */
1178 return 1;
1179
1180nothing_to_do:
1181 qc->scsicmd->result = SAM_STAT_GOOD;
1182 return 1;
1da177e4
LT
1183}
1184
77853bf2 1185static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1da177e4
LT
1186{
1187 struct scsi_cmnd *cmd = qc->scsicmd;
a7dac447 1188 u8 *cdb = cmd->cmnd;
a22e2eb0 1189 int need_sense = (qc->err_mask != 0);
b095518e
JG
1190
1191 /* For ATA pass thru (SAT) commands, generate a sense block if
1192 * user mandated it or if there's an error. Note that if we
1193 * generate because the user forced us to, a check condition
1194 * is generated and the ATA register values are returned
1195 * whether the command completed successfully or not. If there
1196 * was no error, SK, ASC and ASCQ will all be zero.
1197 */
a7dac447
JG
1198 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1199 ((cdb[2] & 0x20) || need_sense)) {
b095518e
JG
1200 ata_gen_ata_desc_sense(qc);
1201 } else {
1202 if (!need_sense) {
1203 cmd->result = SAM_STAT_GOOD;
1204 } else {
1205 /* TODO: decide which descriptor format to use
1206 * for 48b LBA devices and call that here
1207 * instead of the fixed desc, which is only
1208 * good for smaller LBA (and maybe CHS?)
1209 * devices.
1210 */
1211 ata_gen_fixed_sense(qc);
1212 }
1213 }
1da177e4 1214
e61e0672
TH
1215 if (need_sense)
1216 ata_dump_status(qc->ap->id, &qc->result_tf);
1da177e4
LT
1217
1218 qc->scsidone(cmd);
1219
77853bf2 1220 ata_qc_free(qc);
1da177e4
LT
1221}
1222
1223/**
1224 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1da177e4
LT
1225 * @dev: ATA device to which the command is addressed
1226 * @cmd: SCSI command to execute
1227 * @done: SCSI command completion function
1228 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1229 *
1230 * Our ->queuecommand() function has decided that the SCSI
1231 * command issued can be directly translated into an ATA
1232 * command, rather than handled internally.
1233 *
1234 * This function sets up an ata_queued_cmd structure for the
1235 * SCSI command, and sends that ata_queued_cmd to the hardware.
1236 *
ae006510
DG
1237 * The xlat_func argument (actor) returns 0 if ready to execute
1238 * ATA command, else 1 to finish translation. If 1 is returned
1239 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1240 * to be set reflecting an error condition or clean (early)
1241 * termination.
1242 *
1da177e4
LT
1243 * LOCKING:
1244 * spin_lock_irqsave(host_set lock)
1245 */
1246
3373efd8
TH
1247static void ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1248 void (*done)(struct scsi_cmnd *),
1249 ata_xlat_func_t xlat_func)
1da177e4
LT
1250{
1251 struct ata_queued_cmd *qc;
1252 u8 *scsicmd = cmd->cmnd;
1253
1254 VPRINTK("ENTER\n");
1255
3373efd8 1256 qc = ata_scsi_qc_new(dev, cmd, done);
1da177e4 1257 if (!qc)
ae006510 1258 goto err_mem;
1da177e4
LT
1259
1260 /* data is present; dma-map it */
be7db055
CH
1261 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1262 cmd->sc_data_direction == DMA_TO_DEVICE) {
1da177e4 1263 if (unlikely(cmd->request_bufflen < 1)) {
f15a1daf
TH
1264 ata_dev_printk(dev, KERN_WARNING,
1265 "WARNING: zero len r/w req\n");
ae006510 1266 goto err_did;
1da177e4
LT
1267 }
1268
1269 if (cmd->use_sg)
1270 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1271 else
1272 ata_sg_init_one(qc, cmd->request_buffer,
1273 cmd->request_bufflen);
1274
1275 qc->dma_dir = cmd->sc_data_direction;
1276 }
1277
1278 qc->complete_fn = ata_scsi_qc_complete;
1279
1280 if (xlat_func(qc, scsicmd))
ae006510 1281 goto early_finish;
1da177e4
LT
1282
1283 /* select device, send command to hardware */
8e0e694a 1284 ata_qc_issue(qc);
1da177e4
LT
1285
1286 VPRINTK("EXIT\n");
1287 return;
1288
ae006510
DG
1289early_finish:
1290 ata_qc_free(qc);
1291 done(cmd);
1292 DPRINTK("EXIT - early finish (good or error)\n");
1293 return;
1294
1295err_did:
1da177e4 1296 ata_qc_free(qc);
ae006510
DG
1297err_mem:
1298 cmd->result = (DID_ERROR << 16);
1299 done(cmd);
1300 DPRINTK("EXIT - internal\n");
1301 return;
1da177e4
LT
1302}
1303
1304/**
1305 * ata_scsi_rbuf_get - Map response buffer.
1306 * @cmd: SCSI command containing buffer to be mapped.
1307 * @buf_out: Pointer to mapped area.
1308 *
1309 * Maps buffer contained within SCSI command @cmd.
1310 *
1311 * LOCKING:
1312 * spin_lock_irqsave(host_set lock)
1313 *
1314 * RETURNS:
1315 * Length of response buffer.
1316 */
1317
1318static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1319{
1320 u8 *buf;
1321 unsigned int buflen;
1322
1323 if (cmd->use_sg) {
1324 struct scatterlist *sg;
1325
1326 sg = (struct scatterlist *) cmd->request_buffer;
1327 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1328 buflen = sg->length;
1329 } else {
1330 buf = cmd->request_buffer;
1331 buflen = cmd->request_bufflen;
1332 }
1333
1334 *buf_out = buf;
1335 return buflen;
1336}
1337
1338/**
1339 * ata_scsi_rbuf_put - Unmap response buffer.
1340 * @cmd: SCSI command containing buffer to be unmapped.
1341 * @buf: buffer to unmap
1342 *
1343 * Unmaps response buffer contained within @cmd.
1344 *
1345 * LOCKING:
1346 * spin_lock_irqsave(host_set lock)
1347 */
1348
1349static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1350{
1351 if (cmd->use_sg) {
1352 struct scatterlist *sg;
1353
1354 sg = (struct scatterlist *) cmd->request_buffer;
1355 kunmap_atomic(buf - sg->offset, KM_USER0);
1356 }
1357}
1358
1359/**
1360 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1361 * @args: device IDENTIFY data / SCSI command of interest.
1362 * @actor: Callback hook for desired SCSI command simulator
1363 *
1364 * Takes care of the hard work of simulating a SCSI command...
1365 * Mapping the response buffer, calling the command's handler,
1366 * and handling the handler's return value. This return value
1367 * indicates whether the handler wishes the SCSI command to be
ae006510
DG
1368 * completed successfully (0), or not (in which case cmd->result
1369 * and sense buffer are assumed to be set).
1da177e4
LT
1370 *
1371 * LOCKING:
1372 * spin_lock_irqsave(host_set lock)
1373 */
1374
1375void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1376 unsigned int (*actor) (struct ata_scsi_args *args,
1377 u8 *rbuf, unsigned int buflen))
1378{
1379 u8 *rbuf;
1380 unsigned int buflen, rc;
1381 struct scsi_cmnd *cmd = args->cmd;
1382
1383 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1384 memset(rbuf, 0, buflen);
1385 rc = actor(args, rbuf, buflen);
1386 ata_scsi_rbuf_put(cmd, rbuf);
1387
ae006510 1388 if (rc == 0)
1da177e4 1389 cmd->result = SAM_STAT_GOOD;
ae006510 1390 args->done(cmd);
1da177e4
LT
1391}
1392
1393/**
1394 * ata_scsiop_inq_std - Simulate INQUIRY command
1395 * @args: device IDENTIFY data / SCSI command of interest.
1396 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1397 * @buflen: Response buffer length.
1398 *
1399 * Returns standard device identification data associated
b142eb65 1400 * with non-VPD INQUIRY command output.
1da177e4
LT
1401 *
1402 * LOCKING:
1403 * spin_lock_irqsave(host_set lock)
1404 */
1405
1406unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1407 unsigned int buflen)
1408{
1409 u8 hdr[] = {
1410 TYPE_DISK,
1411 0,
1412 0x5, /* claim SPC-3 version compatibility */
1413 2,
1414 95 - 4
1415 };
1416
1417 /* set scsi removeable (RMB) bit per ata bit */
1418 if (ata_id_removeable(args->id))
1419 hdr[1] |= (1 << 7);
1420
1421 VPRINTK("ENTER\n");
1422
1423 memcpy(rbuf, hdr, sizeof(hdr));
1424
1425 if (buflen > 35) {
1426 memcpy(&rbuf[8], "ATA ", 8);
6a62a04d
TH
1427 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1428 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
1da177e4
LT
1429 if (rbuf[32] == 0 || rbuf[32] == ' ')
1430 memcpy(&rbuf[32], "n/a ", 4);
1431 }
1432
1433 if (buflen > 63) {
1434 const u8 versions[] = {
1435 0x60, /* SAM-3 (no version claimed) */
1436
1437 0x03,
1438 0x20, /* SBC-2 (no version claimed) */
1439
1440 0x02,
1441 0x60 /* SPC-3 (no version claimed) */
1442 };
1443
1444 memcpy(rbuf + 59, versions, sizeof(versions));
1445 }
1446
1447 return 0;
1448}
1449
1450/**
b142eb65 1451 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
1da177e4
LT
1452 * @args: device IDENTIFY data / SCSI command of interest.
1453 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1454 * @buflen: Response buffer length.
1455 *
b142eb65 1456 * Returns list of inquiry VPD pages available.
1da177e4
LT
1457 *
1458 * LOCKING:
1459 * spin_lock_irqsave(host_set lock)
1460 */
1461
1462unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1463 unsigned int buflen)
1464{
1465 const u8 pages[] = {
1466 0x00, /* page 0x00, this page */
1467 0x80, /* page 0x80, unit serial no page */
1468 0x83 /* page 0x83, device ident page */
1469 };
b142eb65 1470 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
1da177e4
LT
1471
1472 if (buflen > 6)
1473 memcpy(rbuf + 4, pages, sizeof(pages));
1474
1475 return 0;
1476}
1477
1478/**
b142eb65 1479 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
1da177e4
LT
1480 * @args: device IDENTIFY data / SCSI command of interest.
1481 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1482 * @buflen: Response buffer length.
1483 *
1484 * Returns ATA device serial number.
1485 *
1486 * LOCKING:
1487 * spin_lock_irqsave(host_set lock)
1488 */
1489
1490unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1491 unsigned int buflen)
1492{
1493 const u8 hdr[] = {
1494 0,
1495 0x80, /* this page code */
1496 0,
1497 ATA_SERNO_LEN, /* page len */
1498 };
1499 memcpy(rbuf, hdr, sizeof(hdr));
1500
1501 if (buflen > (ATA_SERNO_LEN + 4 - 1))
6a62a04d
TH
1502 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1503 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1da177e4
LT
1504
1505 return 0;
1506}
1507
1da177e4 1508/**
b142eb65 1509 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
1da177e4
LT
1510 * @args: device IDENTIFY data / SCSI command of interest.
1511 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1512 * @buflen: Response buffer length.
1513 *
b142eb65
JG
1514 * Yields two logical unit device identification designators:
1515 * - vendor specific ASCII containing the ATA serial number
1516 * - SAT defined "t10 vendor id based" containing ASCII vendor
1517 * name ("ATA "), model and serial numbers.
1da177e4
LT
1518 *
1519 * LOCKING:
1520 * spin_lock_irqsave(host_set lock)
1521 */
1522
1523unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1524 unsigned int buflen)
1525{
b142eb65
JG
1526 int num;
1527 const int sat_model_serial_desc_len = 68;
1528 const int ata_model_byte_len = 40;
1da177e4 1529
b142eb65
JG
1530 rbuf[1] = 0x83; /* this page code */
1531 num = 4;
1532
1533 if (buflen > (ATA_SERNO_LEN + num + 3)) {
1534 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2e9edbf8 1535 rbuf[num + 0] = 2;
b142eb65
JG
1536 rbuf[num + 3] = ATA_SERNO_LEN;
1537 num += 4;
1538 ata_id_string(args->id, (unsigned char *) rbuf + num,
1539 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1540 num += ATA_SERNO_LEN;
1da177e4 1541 }
b142eb65
JG
1542 if (buflen > (sat_model_serial_desc_len + num + 3)) {
1543 /* SAT defined lu model and serial numbers descriptor */
1544 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2e9edbf8
JG
1545 rbuf[num + 0] = 2;
1546 rbuf[num + 1] = 1;
b142eb65
JG
1547 rbuf[num + 3] = sat_model_serial_desc_len;
1548 num += 4;
1549 memcpy(rbuf + num, "ATA ", 8);
1550 num += 8;
1551 ata_id_string(args->id, (unsigned char *) rbuf + num,
1552 ATA_ID_PROD_OFS, ata_model_byte_len);
1553 num += ata_model_byte_len;
1554 ata_id_string(args->id, (unsigned char *) rbuf + num,
1555 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1556 num += ATA_SERNO_LEN;
1557 }
1558 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
1da177e4
LT
1559 return 0;
1560}
1561
1562/**
0cba632b 1563 * ata_scsiop_noop - Command handler that simply returns success.
1da177e4
LT
1564 * @args: device IDENTIFY data / SCSI command of interest.
1565 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1566 * @buflen: Response buffer length.
1567 *
1568 * No operation. Simply returns success to caller, to indicate
1569 * that the caller should successfully complete this SCSI command.
1570 *
1571 * LOCKING:
1572 * spin_lock_irqsave(host_set lock)
1573 */
1574
1575unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1576 unsigned int buflen)
1577{
1578 VPRINTK("ENTER\n");
1579 return 0;
1580}
1581
1582/**
1583 * ata_msense_push - Push data onto MODE SENSE data output buffer
1584 * @ptr_io: (input/output) Location to store more output data
1585 * @last: End of output data buffer
1586 * @buf: Pointer to BLOB being added to output buffer
1587 * @buflen: Length of BLOB
1588 *
1589 * Store MODE SENSE data on an output buffer.
1590 *
1591 * LOCKING:
1592 * None.
1593 */
1594
1595static void ata_msense_push(u8 **ptr_io, const u8 *last,
1596 const u8 *buf, unsigned int buflen)
1597{
1598 u8 *ptr = *ptr_io;
1599
1600 if ((ptr + buflen - 1) > last)
1601 return;
1602
1603 memcpy(ptr, buf, buflen);
1604
1605 ptr += buflen;
1606
1607 *ptr_io = ptr;
1608}
1609
1610/**
1611 * ata_msense_caching - Simulate MODE SENSE caching info page
1612 * @id: device IDENTIFY data
1613 * @ptr_io: (input/output) Location to store more output data
1614 * @last: End of output data buffer
1615 *
1616 * Generate a caching info page, which conditionally indicates
1617 * write caching to the SCSI layer, depending on device
1618 * capabilities.
1619 *
1620 * LOCKING:
1621 * None.
1622 */
1623
1624static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1625 const u8 *last)
1626{
00ac37f5 1627 u8 page[CACHE_MPAGE_LEN];
1da177e4 1628
00ac37f5 1629 memcpy(page, def_cache_mpage, sizeof(page));
1da177e4
LT
1630 if (ata_id_wcache_enabled(id))
1631 page[2] |= (1 << 2); /* write cache enable */
1632 if (!ata_id_rahead_enabled(id))
1633 page[12] |= (1 << 5); /* disable read ahead */
1634
1635 ata_msense_push(ptr_io, last, page, sizeof(page));
1636 return sizeof(page);
1637}
1638
1639/**
1640 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1641 * @dev: Device associated with this MODE SENSE command
1642 * @ptr_io: (input/output) Location to store more output data
1643 * @last: End of output data buffer
1644 *
1645 * Generate a generic MODE SENSE control mode page.
1646 *
1647 * LOCKING:
1648 * None.
1649 */
1650
1651static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1652{
00ac37f5
DG
1653 ata_msense_push(ptr_io, last, def_control_mpage,
1654 sizeof(def_control_mpage));
1655 return sizeof(def_control_mpage);
1da177e4
LT
1656}
1657
1658/**
1659 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1660 * @dev: Device associated with this MODE SENSE command
1661 * @ptr_io: (input/output) Location to store more output data
1662 * @last: End of output data buffer
1663 *
1664 * Generate a generic MODE SENSE r/w error recovery page.
1665 *
1666 * LOCKING:
1667 * None.
1668 */
1669
1670static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1671{
1da177e4 1672
00ac37f5
DG
1673 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1674 sizeof(def_rw_recovery_mpage));
1675 return sizeof(def_rw_recovery_mpage);
1da177e4
LT
1676}
1677
48bdc8ec
JA
1678/*
1679 * We can turn this into a real blacklist if it's needed, for now just
1680 * blacklist any Maxtor BANC1G10 revision firmware
1681 */
1682static int ata_dev_supports_fua(u16 *id)
1683{
1684 unsigned char model[41], fw[9];
1685
c3c013a2
JG
1686 if (!libata_fua)
1687 return 0;
48bdc8ec
JA
1688 if (!ata_id_has_fua(id))
1689 return 0;
1690
6a62a04d
TH
1691 ata_id_c_string(id, model, ATA_ID_PROD_OFS, sizeof(model));
1692 ata_id_c_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw));
48bdc8ec 1693
2e02671d 1694 if (strcmp(model, "Maxtor"))
48bdc8ec 1695 return 1;
2e02671d 1696 if (strcmp(fw, "BANC1G10"))
48bdc8ec
JA
1697 return 1;
1698
1699 return 0; /* blacklisted */
1700}
1701
1da177e4
LT
1702/**
1703 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1704 * @args: device IDENTIFY data / SCSI command of interest.
1705 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1706 * @buflen: Response buffer length.
1707 *
00ac37f5
DG
1708 * Simulate MODE SENSE commands. Assume this is invoked for direct
1709 * access devices (e.g. disks) only. There should be no block
1710 * descriptor for other device types.
1da177e4
LT
1711 *
1712 * LOCKING:
1713 * spin_lock_irqsave(host_set lock)
1714 */
1715
1716unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1717 unsigned int buflen)
1718{
9a3dccc4 1719 struct ata_device *dev = args->dev;
1da177e4 1720 u8 *scsicmd = args->cmd->cmnd, *p, *last;
00ac37f5
DG
1721 const u8 sat_blk_desc[] = {
1722 0, 0, 0, 0, /* number of blocks: sat unspecified */
1723 0,
1724 0, 0x2, 0x0 /* block length: 512 bytes */
1725 };
1726 u8 pg, spg;
1727 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
9a3dccc4 1728 u8 dpofua;
1da177e4
LT
1729
1730 VPRINTK("ENTER\n");
1731
1732 six_byte = (scsicmd[0] == MODE_SENSE);
00ac37f5
DG
1733 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
1734 /*
1735 * LLBA bit in msense(10) ignored (compliant)
1da177e4 1736 */
00ac37f5 1737
1da177e4 1738 page_control = scsicmd[2] >> 6;
ae006510
DG
1739 switch (page_control) {
1740 case 0: /* current */
1741 break; /* supported */
1742 case 3: /* saved */
1743 goto saving_not_supp;
1744 case 1: /* changeable */
1745 case 2: /* defaults */
1746 default:
1747 goto invalid_fld;
1748 }
1da177e4 1749
00ac37f5
DG
1750 if (six_byte) {
1751 output_len = 4 + (ebd ? 8 : 0);
1752 alloc_len = scsicmd[4];
1753 } else {
1754 output_len = 8 + (ebd ? 8 : 0);
1755 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
1756 }
1757 minlen = (alloc_len < buflen) ? alloc_len : buflen;
1da177e4
LT
1758
1759 p = rbuf + output_len;
00ac37f5 1760 last = rbuf + minlen - 1;
1da177e4 1761
00ac37f5
DG
1762 pg = scsicmd[2] & 0x3f;
1763 spg = scsicmd[3];
1764 /*
1765 * No mode subpages supported (yet) but asking for _all_
1766 * subpages may be valid
1767 */
1768 if (spg && (spg != ALL_SUB_MPAGES))
1769 goto invalid_fld;
1770
1771 switch(pg) {
1772 case RW_RECOVERY_MPAGE:
1da177e4
LT
1773 output_len += ata_msense_rw_recovery(&p, last);
1774 break;
1775
00ac37f5 1776 case CACHE_MPAGE:
1da177e4
LT
1777 output_len += ata_msense_caching(args->id, &p, last);
1778 break;
1779
00ac37f5 1780 case CONTROL_MPAGE: {
1da177e4
LT
1781 output_len += ata_msense_ctl_mode(&p, last);
1782 break;
1783 }
1784
00ac37f5 1785 case ALL_MPAGES:
1da177e4
LT
1786 output_len += ata_msense_rw_recovery(&p, last);
1787 output_len += ata_msense_caching(args->id, &p, last);
1788 output_len += ata_msense_ctl_mode(&p, last);
1789 break;
1790
1791 default: /* invalid page code */
ae006510 1792 goto invalid_fld;
1da177e4
LT
1793 }
1794
00ac37f5
DG
1795 if (minlen < 1)
1796 return 0;
9a3dccc4
TH
1797
1798 dpofua = 0;
48bdc8ec 1799 if (ata_dev_supports_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 &&
9a3dccc4
TH
1800 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
1801 dpofua = 1 << 4;
1802
1da177e4
LT
1803 if (six_byte) {
1804 output_len--;
1805 rbuf[0] = output_len;
9a3dccc4
TH
1806 if (minlen > 2)
1807 rbuf[2] |= dpofua;
00ac37f5
DG
1808 if (ebd) {
1809 if (minlen > 3)
1810 rbuf[3] = sizeof(sat_blk_desc);
1811 if (minlen > 11)
1812 memcpy(rbuf + 4, sat_blk_desc,
1813 sizeof(sat_blk_desc));
1814 }
1da177e4
LT
1815 } else {
1816 output_len -= 2;
1817 rbuf[0] = output_len >> 8;
00ac37f5
DG
1818 if (minlen > 1)
1819 rbuf[1] = output_len;
9a3dccc4
TH
1820 if (minlen > 3)
1821 rbuf[3] |= dpofua;
00ac37f5
DG
1822 if (ebd) {
1823 if (minlen > 7)
1824 rbuf[7] = sizeof(sat_blk_desc);
1825 if (minlen > 15)
1826 memcpy(rbuf + 8, sat_blk_desc,
1827 sizeof(sat_blk_desc));
1828 }
1da177e4 1829 }
1da177e4 1830 return 0;
ae006510
DG
1831
1832invalid_fld:
1833 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1834 /* "Invalid field in cbd" */
1835 return 1;
1836
1837saving_not_supp:
1838 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
1839 /* "Saving parameters not supported" */
1840 return 1;
1da177e4
LT
1841}
1842
1843/**
1844 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1845 * @args: device IDENTIFY data / SCSI command of interest.
1846 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1847 * @buflen: Response buffer length.
1848 *
1849 * Simulate READ CAPACITY commands.
1850 *
1851 * LOCKING:
1852 * spin_lock_irqsave(host_set lock)
1853 */
1854
1855unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1856 unsigned int buflen)
1857{
1858 u64 n_sectors;
1859 u32 tmp;
1860
1861 VPRINTK("ENTER\n");
1862
8bf62ece
AL
1863 if (ata_id_has_lba(args->id)) {
1864 if (ata_id_has_lba48(args->id))
1865 n_sectors = ata_id_u64(args->id, 100);
1866 else
1867 n_sectors = ata_id_u32(args->id, 60);
1868 } else {
1869 /* CHS default translation */
1870 n_sectors = args->id[1] * args->id[3] * args->id[6];
1871
1872 if (ata_id_current_chs_valid(args->id))
1873 /* CHS current translation */
1874 n_sectors = ata_id_u32(args->id, 57);
1875 }
1876
1da177e4
LT
1877 n_sectors--; /* ATA TotalUserSectors - 1 */
1878
1da177e4 1879 if (args->cmd->cmnd[0] == READ_CAPACITY) {
0c144d0d
PP
1880 if( n_sectors >= 0xffffffffULL )
1881 tmp = 0xffffffff ; /* Return max count on overflow */
1882 else
1883 tmp = n_sectors ;
1884
1da177e4
LT
1885 /* sector count, 32-bit */
1886 rbuf[0] = tmp >> (8 * 3);
1887 rbuf[1] = tmp >> (8 * 2);
1888 rbuf[2] = tmp >> (8 * 1);
1889 rbuf[3] = tmp;
1890
1891 /* sector size */
1892 tmp = ATA_SECT_SIZE;
1893 rbuf[6] = tmp >> 8;
1894 rbuf[7] = tmp;
1895
1896 } else {
1897 /* sector count, 64-bit */
0c144d0d
PP
1898 tmp = n_sectors >> (8 * 4);
1899 rbuf[2] = tmp >> (8 * 3);
1900 rbuf[3] = tmp >> (8 * 2);
1901 rbuf[4] = tmp >> (8 * 1);
1902 rbuf[5] = tmp;
1903 tmp = n_sectors;
1da177e4
LT
1904 rbuf[6] = tmp >> (8 * 3);
1905 rbuf[7] = tmp >> (8 * 2);
1906 rbuf[8] = tmp >> (8 * 1);
1907 rbuf[9] = tmp;
1908
1909 /* sector size */
1910 tmp = ATA_SECT_SIZE;
1911 rbuf[12] = tmp >> 8;
1912 rbuf[13] = tmp;
1913 }
1914
1915 return 0;
1916}
1917
1918/**
1919 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1920 * @args: device IDENTIFY data / SCSI command of interest.
1921 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1922 * @buflen: Response buffer length.
1923 *
1924 * Simulate REPORT LUNS command.
1925 *
1926 * LOCKING:
1927 * spin_lock_irqsave(host_set lock)
1928 */
1929
1930unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1931 unsigned int buflen)
1932{
1933 VPRINTK("ENTER\n");
1934 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1935
1936 return 0;
1937}
1938
845c5834
DG
1939/**
1940 * ata_scsi_set_sense - Set SCSI sense data and status
1941 * @cmd: SCSI request to be handled
1942 * @sk: SCSI-defined sense key
1943 * @asc: SCSI-defined additional sense code
1944 * @ascq: SCSI-defined additional sense code qualifier
1945 *
1946 * Helper function that builds a valid fixed format, current
1947 * response code and the given sense key (sk), additional sense
1948 * code (asc) and additional sense code qualifier (ascq) with
1949 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
1950 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
1951 *
1952 * LOCKING:
1953 * Not required
1954 */
1955
1956void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
1957{
1958 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1959
1960 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
1961 cmd->sense_buffer[2] = sk;
1962 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
1963 cmd->sense_buffer[12] = asc;
1964 cmd->sense_buffer[13] = ascq;
1965}
1966
1da177e4
LT
1967/**
1968 * ata_scsi_badcmd - End a SCSI request with an error
1969 * @cmd: SCSI request to be handled
1970 * @done: SCSI command completion function
1971 * @asc: SCSI-defined additional sense code
1972 * @ascq: SCSI-defined additional sense code qualifier
1973 *
1974 * Helper function that completes a SCSI command with
1975 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1976 * and the specified additional sense codes.
1977 *
1978 * LOCKING:
1979 * spin_lock_irqsave(host_set lock)
1980 */
1981
1982void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1983{
1984 DPRINTK("ENTER\n");
ae006510 1985 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
1da177e4
LT
1986
1987 done(cmd);
1988}
1989
77853bf2 1990static void atapi_sense_complete(struct ata_queued_cmd *qc)
a939c963 1991{
74e6c8c3 1992 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
c6e6e666
JG
1993 /* FIXME: not quite right; we don't want the
1994 * translation of taskfile registers into
1995 * a sense descriptors, since that's only
1996 * correct for ATA, not ATAPI
1997 */
1998 ata_gen_ata_desc_sense(qc);
74e6c8c3 1999 }
a939c963 2000
c6e6e666 2001 qc->scsidone(qc->scsicmd);
77853bf2 2002 ata_qc_free(qc);
c6e6e666 2003}
a939c963 2004
c6e6e666
JG
2005/* is it pointless to prefer PIO for "safety reasons"? */
2006static inline int ata_pio_use_silly(struct ata_port *ap)
2007{
2008 return (ap->flags & ATA_FLAG_PIO_DMA);
2009}
2010
2011static void atapi_request_sense(struct ata_queued_cmd *qc)
2012{
2013 struct ata_port *ap = qc->ap;
2014 struct scsi_cmnd *cmd = qc->scsicmd;
2015
2016 DPRINTK("ATAPI request sense\n");
a939c963
JG
2017
2018 /* FIXME: is this needed? */
2019 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2020
c6e6e666
JG
2021 ap->ops->tf_read(ap, &qc->tf);
2022
2023 /* fill these in, for the case where they are -not- overwritten */
2024 cmd->sense_buffer[0] = 0x70;
2025 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2026
2027 ata_qc_reinit(qc);
2028
a939c963
JG
2029 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2030 qc->dma_dir = DMA_FROM_DEVICE;
2031
6e7846e9 2032 memset(&qc->cdb, 0, qc->dev->cdb_len);
a939c963
JG
2033 qc->cdb[0] = REQUEST_SENSE;
2034 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2035
2036 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2037 qc->tf.command = ATA_CMD_PACKET;
2038
c6e6e666
JG
2039 if (ata_pio_use_silly(ap)) {
2040 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2041 qc->tf.feature |= ATAPI_PKT_DMA;
2042 } else {
2043 qc->tf.protocol = ATA_PROT_ATAPI;
2044 qc->tf.lbam = (8 * 1024) & 0xff;
2045 qc->tf.lbah = (8 * 1024) >> 8;
2046 }
a939c963
JG
2047 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2048
c6e6e666 2049 qc->complete_fn = atapi_sense_complete;
a939c963 2050
8e0e694a 2051 ata_qc_issue(qc);
a939c963
JG
2052
2053 DPRINTK("EXIT\n");
2054}
2055
77853bf2 2056static void atapi_qc_complete(struct ata_queued_cmd *qc)
1da177e4
LT
2057{
2058 struct scsi_cmnd *cmd = qc->scsicmd;
a22e2eb0 2059 unsigned int err_mask = qc->err_mask;
1da177e4 2060
a7dac447 2061 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
e12669e7 2062
a7dac447 2063 if (unlikely(err_mask & AC_ERR_DEV)) {
1da177e4 2064 cmd->result = SAM_STAT_CHECK_CONDITION;
c6e6e666 2065 atapi_request_sense(qc);
77853bf2 2066 return;
74e6c8c3 2067 } else if (unlikely(err_mask)) {
a7dac447
JG
2068 /* FIXME: not quite right; we don't want the
2069 * translation of taskfile registers into
2070 * a sense descriptors, since that's only
2071 * correct for ATA, not ATAPI
2072 */
2073 ata_gen_ata_desc_sense(qc);
74e6c8c3 2074 } else {
1da177e4
LT
2075 u8 *scsicmd = cmd->cmnd;
2076
fd71da46 2077 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
1da177e4
LT
2078 u8 *buf = NULL;
2079 unsigned int buflen;
2080
2081 buflen = ata_scsi_rbuf_get(cmd, &buf);
a15dbeb4
JG
2082
2083 /* ATAPI devices typically report zero for their SCSI version,
2084 * and sometimes deviate from the spec WRT response data
2085 * format. If SCSI version is reported as zero like normal,
2086 * then we make the following fixups: 1) Fake MMC-5 version,
2087 * to indicate to the Linux scsi midlayer this is a modern
2088 * device. 2) Ensure response data format / ATAPI information
2089 * are always correct.
a15dbeb4
JG
2090 */
2091 if (buf[2] == 0) {
2092 buf[2] = 0x5;
2093 buf[3] = 0x32;
2094 }
2095
1da177e4
LT
2096 ata_scsi_rbuf_put(cmd, buf);
2097 }
a15dbeb4 2098
1da177e4
LT
2099 cmd->result = SAM_STAT_GOOD;
2100 }
2101
2102 qc->scsidone(cmd);
77853bf2 2103 ata_qc_free(qc);
1da177e4
LT
2104}
2105/**
2106 * atapi_xlat - Initialize PACKET taskfile
2107 * @qc: command structure to be initialized
2108 * @scsicmd: SCSI CDB associated with this PACKET command
2109 *
2110 * LOCKING:
2111 * spin_lock_irqsave(host_set lock)
2112 *
2113 * RETURNS:
2114 * Zero on success, non-zero on failure.
2115 */
2116
057ace5e 2117static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
1da177e4
LT
2118{
2119 struct scsi_cmnd *cmd = qc->scsicmd;
2120 struct ata_device *dev = qc->dev;
2121 int using_pio = (dev->flags & ATA_DFLAG_PIO);
be7db055 2122 int nodata = (cmd->sc_data_direction == DMA_NONE);
1da177e4
LT
2123
2124 if (!using_pio)
2125 /* Check whether ATAPI DMA is safe */
2126 if (ata_check_atapi_dma(qc))
2127 using_pio = 1;
2128
6e7846e9 2129 memcpy(&qc->cdb, scsicmd, dev->cdb_len);
1da177e4
LT
2130
2131 qc->complete_fn = atapi_qc_complete;
2132
2133 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
be7db055 2134 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1da177e4
LT
2135 qc->tf.flags |= ATA_TFLAG_WRITE;
2136 DPRINTK("direction: write\n");
2137 }
2138
2139 qc->tf.command = ATA_CMD_PACKET;
2140
2141 /* no data, or PIO data xfer */
2142 if (using_pio || nodata) {
2143 if (nodata)
2144 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
2145 else
2146 qc->tf.protocol = ATA_PROT_ATAPI;
2147 qc->tf.lbam = (8 * 1024) & 0xff;
2148 qc->tf.lbah = (8 * 1024) >> 8;
2149 }
2150
2151 /* DMA data xfer */
2152 else {
2153 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2154 qc->tf.feature |= ATAPI_PKT_DMA;
2155
95de719a
AL
2156 if (atapi_dmadir && (cmd->sc_data_direction != DMA_TO_DEVICE))
2157 /* some SATA bridges need us to indicate data xfer direction */
1da177e4 2158 qc->tf.feature |= ATAPI_DMADIR;
1da177e4
LT
2159 }
2160
2161 qc->nbytes = cmd->bufflen;
2162
2163 return 0;
2164}
2165
2166/**
2167 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2168 * @ap: ATA port to which the device is attached
2169 * @scsidev: SCSI device from which we derive the ATA device
2170 *
2171 * Given various information provided in struct scsi_cmnd,
2172 * map that onto an ATA bus, and using that mapping
2173 * determine which ata_device is associated with the
2174 * SCSI command to be sent.
2175 *
2176 * LOCKING:
2177 * spin_lock_irqsave(host_set lock)
2178 *
2179 * RETURNS:
2180 * Associated ATA device, or %NULL if not found.
2181 */
2182
2183static struct ata_device *
057ace5e 2184ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
1da177e4
LT
2185{
2186 struct ata_device *dev;
2187
2188 /* skip commands not addressed to targets we simulate */
2189 if (likely(scsidev->id < ATA_MAX_DEVICES))
2190 dev = &ap->device[scsidev->id];
2191 else
2192 return NULL;
2193
2194 if (unlikely((scsidev->channel != 0) ||
2195 (scsidev->lun != 0)))
2196 return NULL;
2197
e1211e3f 2198 if (unlikely(!ata_dev_enabled(dev)))
1da177e4
LT
2199 return NULL;
2200
50630195
JG
2201 if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
2202 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
f15a1daf
TH
2203 ata_dev_printk(dev, KERN_WARNING,
2204 "WARNING: ATAPI is %s, device ignored.\n",
2205 atapi_enabled ? "not supported with this driver" : "disabled");
1623c81e 2206 return NULL;
50630195 2207 }
1623c81e 2208 }
1da177e4
LT
2209
2210 return dev;
2211}
2212
b095518e
JG
2213/*
2214 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2215 * @byte1: Byte 1 from pass-thru CDB.
2216 *
2217 * RETURNS:
2218 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2219 */
2220static u8
2221ata_scsi_map_proto(u8 byte1)
2222{
2223 switch((byte1 & 0x1e) >> 1) {
2224 case 3: /* Non-data */
2225 return ATA_PROT_NODATA;
2226
2227 case 6: /* DMA */
2228 return ATA_PROT_DMA;
2229
2230 case 4: /* PIO Data-in */
2231 case 5: /* PIO Data-out */
b095518e
JG
2232 return ATA_PROT_PIO;
2233
2234 case 10: /* Device Reset */
2235 case 0: /* Hard Reset */
2236 case 1: /* SRST */
2237 case 2: /* Bus Idle */
2238 case 7: /* Packet */
2239 case 8: /* DMA Queued */
2240 case 9: /* Device Diagnostic */
2241 case 11: /* UDMA Data-in */
2242 case 12: /* UDMA Data-Out */
2243 case 13: /* FPDMA */
2244 default: /* Reserved */
2245 break;
2246 }
2247
2248 return ATA_PROT_UNKNOWN;
2249}
2250
2251/**
2252 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2253 * @qc: command structure to be initialized
8e8b77dd 2254 * @scsicmd: SCSI command to convert
b095518e
JG
2255 *
2256 * Handles either 12 or 16-byte versions of the CDB.
2257 *
2258 * RETURNS:
2259 * Zero on success, non-zero on failure.
2260 */
2261static unsigned int
00ac37f5 2262ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
b095518e
JG
2263{
2264 struct ata_taskfile *tf = &(qc->tf);
2265 struct scsi_cmnd *cmd = qc->scsicmd;
2266
2267 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
9a405257 2268 goto invalid_fld;
b095518e 2269
f59b0cf8
AL
2270 if (scsicmd[1] & 0xe0)
2271 /* PIO multi not supported yet */
2272 goto invalid_fld;
2273
b095518e
JG
2274 /*
2275 * 12 and 16 byte CDBs use different offsets to
2276 * provide the various register values.
2277 */
2278 if (scsicmd[0] == ATA_16) {
2279 /*
2280 * 16-byte CDB - may contain extended commands.
2281 *
2282 * If that is the case, copy the upper byte register values.
2283 */
2284 if (scsicmd[1] & 0x01) {
2285 tf->hob_feature = scsicmd[3];
2286 tf->hob_nsect = scsicmd[5];
2287 tf->hob_lbal = scsicmd[7];
2288 tf->hob_lbam = scsicmd[9];
2289 tf->hob_lbah = scsicmd[11];
2290 tf->flags |= ATA_TFLAG_LBA48;
2291 } else
2292 tf->flags &= ~ATA_TFLAG_LBA48;
2293
2294 /*
2295 * Always copy low byte, device and command registers.
2296 */
2297 tf->feature = scsicmd[4];
2298 tf->nsect = scsicmd[6];
2299 tf->lbal = scsicmd[8];
2300 tf->lbam = scsicmd[10];
2301 tf->lbah = scsicmd[12];
2302 tf->device = scsicmd[13];
2303 tf->command = scsicmd[14];
2304 } else {
2305 /*
2306 * 12-byte CDB - incapable of extended commands.
2307 */
2308 tf->flags &= ~ATA_TFLAG_LBA48;
2309
2310 tf->feature = scsicmd[3];
2311 tf->nsect = scsicmd[4];
2312 tf->lbal = scsicmd[5];
2313 tf->lbam = scsicmd[6];
2314 tf->lbah = scsicmd[7];
2315 tf->device = scsicmd[8];
2316 tf->command = scsicmd[9];
2317 }
dcc2d1e7
ML
2318 /*
2319 * If slave is possible, enforce correct master/slave bit
2320 */
2321 if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
2322 tf->device = qc->dev->devno ?
2323 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
b095518e
JG
2324
2325 /*
2326 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2327 * SET_FEATURES - XFER MODE must be preceded/succeeded
2328 * by an update to hardware-specific registers for each
2329 * controller (i.e. the reason for ->set_piomode(),
2330 * ->set_dmamode(), and ->post_set_mode() hooks).
2331 */
2332 if ((tf->command == ATA_CMD_SET_FEATURES)
2333 && (tf->feature == SETFEATURES_XFER))
9a405257 2334 goto invalid_fld;
b095518e
JG
2335
2336 /*
2337 * Set flags so that all registers will be written,
2338 * and pass on write indication (used for PIO/DMA
2339 * setup.)
2340 */
2341 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2342
0274aa25 2343 if (cmd->sc_data_direction == DMA_TO_DEVICE)
b095518e
JG
2344 tf->flags |= ATA_TFLAG_WRITE;
2345
2346 /*
2347 * Set transfer length.
2348 *
2349 * TODO: find out if we need to do more here to
2350 * cover scatter/gather case.
2351 */
2352 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
2353
e61e0672
TH
2354 /* request result TF */
2355 qc->flags |= ATA_QCFLAG_RESULT_TF;
2356
b095518e 2357 return 0;
9a405257
TH
2358
2359 invalid_fld:
2360 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00);
2361 /* "Invalid field in cdb" */
2362 return 1;
b095518e
JG
2363}
2364
1da177e4
LT
2365/**
2366 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2367 * @dev: ATA device
2368 * @cmd: SCSI command opcode to consider
2369 *
2370 * Look up the SCSI command given, and determine whether the
2371 * SCSI command is to be translated or simulated.
2372 *
2373 * RETURNS:
2374 * Pointer to translation function if possible, %NULL if not.
2375 */
2376
2377static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2378{
2379 switch (cmd) {
2380 case READ_6:
2381 case READ_10:
2382 case READ_16:
2383
2384 case WRITE_6:
2385 case WRITE_10:
2386 case WRITE_16:
2387 return ata_scsi_rw_xlat;
2388
2389 case SYNCHRONIZE_CACHE:
2390 if (ata_try_flush_cache(dev))
2391 return ata_scsi_flush_xlat;
2392 break;
2393
2394 case VERIFY:
2395 case VERIFY_16:
2396 return ata_scsi_verify_xlat;
b095518e
JG
2397
2398 case ATA_12:
2399 case ATA_16:
2400 return ata_scsi_pass_thru;
da61396d 2401
972dcafb
DG
2402 case START_STOP:
2403 return ata_scsi_start_stop_xlat;
1da177e4
LT
2404 }
2405
2406 return NULL;
2407}
2408
2409/**
2410 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
2411 * @ap: ATA port to which the command was being sent
2412 * @cmd: SCSI command to dump
2413 *
2414 * Prints the contents of a SCSI command via printk().
2415 */
2416
2417static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2418 struct scsi_cmnd *cmd)
2419{
2420#ifdef ATA_DEBUG
2421 struct scsi_device *scsidev = cmd->device;
2422 u8 *scsicmd = cmd->cmnd;
2423
2424 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2425 ap->id,
2426 scsidev->channel, scsidev->id, scsidev->lun,
2427 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
2428 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
2429 scsicmd[8]);
2430#endif
2431}
2432
3373efd8
TH
2433static inline void __ata_scsi_queuecmd(struct scsi_cmnd *cmd,
2434 void (*done)(struct scsi_cmnd *),
2435 struct ata_device *dev)
eb3f0f9c
BK
2436{
2437 if (dev->class == ATA_DEV_ATA) {
2438 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2439 cmd->cmnd[0]);
2440
2441 if (xlat_func)
3373efd8 2442 ata_scsi_translate(dev, cmd, done, xlat_func);
eb3f0f9c 2443 else
3373efd8 2444 ata_scsi_simulate(dev, cmd, done);
eb3f0f9c 2445 } else
3373efd8 2446 ata_scsi_translate(dev, cmd, done, atapi_xlat);
eb3f0f9c
BK
2447}
2448
1da177e4
LT
2449/**
2450 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
2451 * @cmd: SCSI command to be sent
2452 * @done: Completion function, called when command is complete
2453 *
2454 * In some cases, this function translates SCSI commands into
2455 * ATA taskfiles, and queues the taskfiles to be sent to
2456 * hardware. In other cases, this function simulates a
2457 * SCSI device by evaluating and responding to certain
2458 * SCSI commands. This creates the overall effect of
2459 * ATA and ATAPI devices appearing as SCSI devices.
2460 *
2461 * LOCKING:
2462 * Releases scsi-layer-held lock, and obtains host_set lock.
2463 *
2464 * RETURNS:
2465 * Zero.
2466 */
2467
2468int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2469{
2470 struct ata_port *ap;
2471 struct ata_device *dev;
2472 struct scsi_device *scsidev = cmd->device;
005a5a06 2473 struct Scsi_Host *shost = scsidev->host;
1da177e4 2474
35bb94b1 2475 ap = ata_shost_to_port(shost);
005a5a06
JG
2476
2477 spin_unlock(shost->host_lock);
2478 spin_lock(&ap->host_set->lock);
1da177e4
LT
2479
2480 ata_scsi_dump_cdb(ap, cmd);
2481
2482 dev = ata_scsi_find_dev(ap, scsidev);
eb3f0f9c 2483 if (likely(dev))
3373efd8 2484 __ata_scsi_queuecmd(cmd, done, dev);
eb3f0f9c 2485 else {
1da177e4
LT
2486 cmd->result = (DID_BAD_TARGET << 16);
2487 done(cmd);
1da177e4
LT
2488 }
2489
005a5a06
JG
2490 spin_unlock(&ap->host_set->lock);
2491 spin_lock(shost->host_lock);
1da177e4
LT
2492 return 0;
2493}
2494
2495/**
2496 * ata_scsi_simulate - simulate SCSI command on ATA device
c893a3ae 2497 * @dev: the target device
1da177e4
LT
2498 * @cmd: SCSI command being sent to device.
2499 * @done: SCSI command completion function.
2500 *
2501 * Interprets and directly executes a select list of SCSI commands
2502 * that can be handled internally.
2503 *
2504 * LOCKING:
2505 * spin_lock_irqsave(host_set lock)
2506 */
2507
3373efd8 2508void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
1da177e4
LT
2509 void (*done)(struct scsi_cmnd *))
2510{
2511 struct ata_scsi_args args;
057ace5e 2512 const u8 *scsicmd = cmd->cmnd;
1da177e4 2513
9a3dccc4
TH
2514 args.dev = dev;
2515 args.id = dev->id;
1da177e4
LT
2516 args.cmd = cmd;
2517 args.done = done;
2518
2519 switch(scsicmd[0]) {
2520 /* no-op's, complete with success */
2521 case SYNCHRONIZE_CACHE:
2522 case REZERO_UNIT:
2523 case SEEK_6:
2524 case SEEK_10:
2525 case TEST_UNIT_READY:
2526 case FORMAT_UNIT: /* FIXME: correct? */
2527 case SEND_DIAGNOSTIC: /* FIXME: correct? */
2528 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2529 break;
2530
2531 case INQUIRY:
2532 if (scsicmd[1] & 2) /* is CmdDt set? */
ae006510 2533 ata_scsi_invalid_field(cmd, done);
1da177e4
LT
2534 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2535 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2536 else if (scsicmd[2] == 0x00)
2537 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2538 else if (scsicmd[2] == 0x80)
2539 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2540 else if (scsicmd[2] == 0x83)
2541 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2542 else
ae006510 2543 ata_scsi_invalid_field(cmd, done);
1da177e4
LT
2544 break;
2545
2546 case MODE_SENSE:
2547 case MODE_SENSE_10:
2548 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2549 break;
2550
2551 case MODE_SELECT: /* unconditionally return */
2552 case MODE_SELECT_10: /* bad-field-in-cdb */
ae006510 2553 ata_scsi_invalid_field(cmd, done);
1da177e4
LT
2554 break;
2555
2556 case READ_CAPACITY:
2557 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2558 break;
2559
2560 case SERVICE_ACTION_IN:
2561 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2562 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2563 else
ae006510 2564 ata_scsi_invalid_field(cmd, done);
1da177e4
LT
2565 break;
2566
2567 case REPORT_LUNS:
2568 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2569 break;
2570
b095518e 2571 /* mandatory commands we haven't implemented yet */
1da177e4
LT
2572 case REQUEST_SENSE:
2573
2574 /* all other commands */
2575 default:
ae006510
DG
2576 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2577 /* "Invalid command operation code" */
2578 done(cmd);
1da177e4
LT
2579 break;
2580 }
2581}
2582
644dd0cc
JG
2583void ata_scsi_scan_host(struct ata_port *ap)
2584{
3f19ee8c 2585 struct ata_device *dev;
644dd0cc
JG
2586 unsigned int i;
2587
198e0fed 2588 if (ap->flags & ATA_FLAG_DISABLED)
644dd0cc
JG
2589 return;
2590
3f19ee8c
JG
2591 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2592 dev = &ap->device[i];
2593
e1211e3f 2594 if (ata_dev_enabled(dev))
3f19ee8c
JG
2595 scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
2596 }
644dd0cc
JG
2597}
2598
7b70fc03
TH
2599/**
2600 * ata_schedule_scsi_eh - schedule EH for SCSI host
2601 * @shost: SCSI host to invoke error handling on.
2602 *
2603 * Schedule SCSI EH without scmd. This is a hack.
2604 *
2605 * LOCKING:
2606 * spin_lock_irqsave(host_set lock)
2607 **/
2608void ata_schedule_scsi_eh(struct Scsi_Host *shost)
2609{
2610 unsigned long flags;
2611
2612 spin_lock_irqsave(shost->host_lock, flags);
2613
2614 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
2615 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
2616 shost->host_eh_scheduled++;
2617 scsi_eh_wakeup(shost);
2618 }
2619
2620 spin_unlock_irqrestore(shost->host_lock, flags);
2621}