blob: c1cf5e038967fe2b1fdbb2fe0cbdc89bd58a2baa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 libata-scsi.c - helper library for ATA
3
4 Copyright 2003-2004 Red Hat, Inc. All rights reserved.
5 Copyright 2003-2004 Jeff Garzik
6
7 The contents of this file are subject to the Open
8 Software License version 1.1 that can be found at
9 http://www.opensource.org/licenses/osl-1.1.txt and is included herein
10 by reference.
11
12 Alternatively, the contents of this file may be used under the terms
13 of the GNU General Public License version 2 (the "GPL") as distributed
14 in the kernel source COPYING file, in which case the provisions of
15 the GPL are applicable instead of the above. If you wish to allow
16 the use of your version of this file only under the terms of the
17 GPL and not to allow others to use your version of this file under
18 the OSL, indicate your decision by deleting the provisions above and
19 replace them with the notice and other provisions required by the GPL.
20 If you do not delete the provisions above, a recipient may use your
21 version of this file under either the OSL or the GPL.
22
23 */
24
25#include <linux/kernel.h>
26#include <linux/blkdev.h>
27#include <linux/spinlock.h>
28#include <scsi/scsi.h>
29#include "scsi.h"
30#include <scsi/scsi_host.h>
31#include <linux/libata.h>
Jeff Garzikb0955182005-05-12 15:45:22 -040032#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/uaccess.h>
34
35#include "libata.h"
36
Jeff Garzikb0955182005-05-12 15:45:22 -040037#define SECTOR_SIZE 512
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
40static struct ata_device *
41ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
42
43
44/**
45 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
46 * @sdev: SCSI device for which BIOS geometry is to be determined
47 * @bdev: block device associated with @sdev
48 * @capacity: capacity of SCSI device
49 * @geom: location to which geometry will be output
50 *
51 * Generic bios head/sector/cylinder calculator
52 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
53 * mapping. Some situations may arise where the disk is not
54 * bootable if this is not used.
55 *
56 * LOCKING:
57 * Defined by the SCSI layer. We don't really care.
58 *
59 * RETURNS:
60 * Zero.
61 */
62int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
63 sector_t capacity, int geom[])
64{
65 geom[0] = 255;
66 geom[1] = 63;
67 sector_div(capacity, 255*63);
68 geom[2] = capacity;
69
70 return 0;
71}
72
Jeff Garzikb0955182005-05-12 15:45:22 -040073/**
74 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
75 * @dev: Device to whom we are issuing command
76 * @arg: User provided data for issuing command
77 *
78 * LOCKING:
79 * Defined by the SCSI layer. We don't really care.
80 *
81 * RETURNS:
82 * Zero on success, negative errno on error.
83 */
84
85int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
86{
87 int rc = 0;
88 u8 scsi_cmd[MAX_COMMAND_SIZE];
89 u8 args[4], *argbuf = NULL;
90 int argsize = 0;
91 struct scsi_request *sreq;
92
93 if (NULL == (void *)arg)
94 return -EINVAL;
95
96 if (copy_from_user(args, arg, sizeof(args)))
97 return -EFAULT;
98
99 sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
100 if (!sreq)
101 return -EINTR;
102
103 memset(scsi_cmd, 0, sizeof(scsi_cmd));
104
105 if (args[3]) {
106 argsize = SECTOR_SIZE * args[3];
107 argbuf = kmalloc(argsize, GFP_KERNEL);
108 if (argbuf == NULL)
109 return -ENOMEM;
110
111 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
112 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
113 block count in sector count field */
114 sreq->sr_data_direction = DMA_FROM_DEVICE;
115 } else {
116 scsi_cmd[1] = (3 << 1); /* Non-data */
117 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
118 sreq->sr_data_direction = DMA_NONE;
119 }
120
121 scsi_cmd[0] = ATA_16;
122
123 scsi_cmd[4] = args[2];
124 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
125 scsi_cmd[6] = args[3];
126 scsi_cmd[8] = args[1];
127 scsi_cmd[10] = 0x4f;
128 scsi_cmd[12] = 0xc2;
129 } else {
130 scsi_cmd[6] = args[1];
131 }
132 scsi_cmd[14] = args[0];
133
134 /* Good values for timeout and retries? Values below
135 from scsi_ioctl_send_command() for default case... */
136 scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5);
137
138 if (sreq->sr_result) {
139 rc = -EIO;
140 goto error;
141 }
142
143 /* Need code to retrieve data from check condition? */
144
145 if ((argbuf)
146 && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
147 rc = -EFAULT;
148error:
149 scsi_release_request(sreq);
150
151 if (argbuf)
152 kfree(argbuf);
153
154 return rc;
155}
156
157/**
158 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
159 * @dev: Device to whom we are issuing command
160 * @arg: User provided data for issuing command
161 *
162 * LOCKING:
163 * Defined by the SCSI layer. We don't really care.
164 *
165 * RETURNS:
166 * Zero on success, negative errno on error.
167 */
168int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
169{
170 int rc = 0;
171 u8 scsi_cmd[MAX_COMMAND_SIZE];
172 u8 args[7];
173 struct scsi_request *sreq;
174
175 if (NULL == (void *)arg)
176 return -EINVAL;
177
178 if (copy_from_user(args, arg, sizeof(args)))
179 return -EFAULT;
180
181 memset(scsi_cmd, 0, sizeof(scsi_cmd));
182 scsi_cmd[0] = ATA_16;
183 scsi_cmd[1] = (3 << 1); /* Non-data */
184 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
185 scsi_cmd[4] = args[1];
186 scsi_cmd[6] = args[2];
187 scsi_cmd[8] = args[3];
188 scsi_cmd[10] = args[4];
189 scsi_cmd[12] = args[5];
190 scsi_cmd[14] = args[0];
191
192 sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
193 if (!sreq) {
194 rc = -EINTR;
195 goto error;
196 }
197
198 sreq->sr_data_direction = DMA_NONE;
199 /* Good values for timeout and retries? Values below
200 from scsi_ioctl_send_command() for default case... */
201 scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5);
202
203 if (sreq->sr_result) {
204 rc = -EIO;
205 goto error;
206 }
207
208 /* Need code to retrieve data from check condition? */
209
210error:
211 scsi_release_request(sreq);
212 return rc;
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
216{
217 struct ata_port *ap;
218 struct ata_device *dev;
219 int val = -EINVAL, rc = -EINVAL;
220
221 ap = (struct ata_port *) &scsidev->host->hostdata[0];
222 if (!ap)
223 goto out;
224
225 dev = ata_scsi_find_dev(ap, scsidev);
226 if (!dev) {
227 rc = -ENODEV;
228 goto out;
229 }
230
231 switch (cmd) {
232 case ATA_IOC_GET_IO32:
233 val = 0;
234 if (copy_to_user(arg, &val, 1))
235 return -EFAULT;
236 return 0;
237
238 case ATA_IOC_SET_IO32:
239 val = (unsigned long) arg;
240 if (val != 0)
241 return -EINVAL;
242 return 0;
243
Jeff Garzikb0955182005-05-12 15:45:22 -0400244 case HDIO_DRIVE_CMD:
245 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
246 return -EACCES;
247 return ata_cmd_ioctl(scsidev, arg);
248
249 case HDIO_DRIVE_TASK:
250 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
251 return -EACCES;
252 return ata_task_ioctl(scsidev, arg);
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 default:
255 rc = -ENOTTY;
256 break;
257 }
258
259out:
260 return rc;
261}
262
263/**
264 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
265 * @ap: ATA port to which the new command is attached
266 * @dev: ATA device to which the new command is attached
267 * @cmd: SCSI command that originated this ATA command
268 * @done: SCSI command completion function
269 *
270 * Obtain a reference to an unused ata_queued_cmd structure,
271 * which is the basic libata structure representing a single
272 * ATA command sent to the hardware.
273 *
274 * If a command was available, fill in the SCSI-specific
275 * portions of the structure with information on the
276 * current command.
277 *
278 * LOCKING:
279 * spin_lock_irqsave(host_set lock)
280 *
281 * RETURNS:
282 * Command allocated, or %NULL if none available.
283 */
284struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
285 struct ata_device *dev,
286 struct scsi_cmnd *cmd,
287 void (*done)(struct scsi_cmnd *))
288{
289 struct ata_queued_cmd *qc;
290
291 qc = ata_qc_new_init(ap, dev);
292 if (qc) {
293 qc->scsicmd = cmd;
294 qc->scsidone = done;
295
296 if (cmd->use_sg) {
297 qc->sg = (struct scatterlist *) cmd->request_buffer;
298 qc->n_elem = cmd->use_sg;
299 } else {
300 qc->sg = &qc->sgent;
301 qc->n_elem = 1;
302 }
303 } else {
304 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
305 done(cmd);
306 }
307
308 return qc;
309}
310
311/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400312 * ata_dump_status - user friendly display of error info
313 * @id: id of the port in question
314 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400316 * Decode and dump the ATA error/status registers for the user so
317 * that they have some idea what really happened at the non
318 * make-believe layer.
319 *
320 * LOCKING:
321 * inherited from caller
322 */
323void ata_dump_status(unsigned id, struct ata_taskfile *tf)
324{
325 u8 stat = tf->command, err = tf->feature;
326
327 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
328 if (stat & ATA_BUSY) {
329 printk("Busy }\n"); /* Data is not valid in this case */
330 } else {
331 if (stat & 0x40) printk("DriveReady ");
332 if (stat & 0x20) printk("DeviceFault ");
333 if (stat & 0x10) printk("SeekComplete ");
334 if (stat & 0x08) printk("DataRequest ");
335 if (stat & 0x04) printk("CorrectedError ");
336 if (stat & 0x02) printk("Index ");
337 if (stat & 0x01) printk("Error ");
338 printk("}\n");
339
340 if (err) {
341 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
342 if (err & 0x04) printk("DriveStatusError ");
343 if (err & 0x80) {
344 if (err & 0x04) printk("BadCRC ");
345 else printk("Sector ");
346 }
347 if (err & 0x40) printk("UncorrectableError ");
348 if (err & 0x10) printk("SectorIdNotFound ");
349 if (err & 0x02) printk("TrackZeroNotFound ");
350 if (err & 0x01) printk("AddrMarkNotFound ");
351 printk("}\n");
352 }
353 }
354}
355
356/**
357 * ata_to_sense_error - convert ATA error to SCSI error
358 * @drv_stat: value contained in ATA status register
359 * @drv_err: value contained in ATA error register
360 * @sk: the sense key we'll fill out
361 * @asc: the additional sense code we'll fill out
362 * @ascq: the additional sense code qualifier we'll fill out
363 *
364 * Converts an ATA error into a SCSI error. Fill out pointers to
365 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
366 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
368 * LOCKING:
369 * spin_lock_irqsave(host_set lock)
370 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400371void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
372 u8 *ascq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Jeff Garzikb0955182005-05-12 15:45:22 -0400374 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* Based on the 3ware driver translation table */
376 static unsigned char sense_table[][4] = {
377 /* BBD|ECC|ID|MAR */
378 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
379 /* BBD|ECC|ID */
380 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
381 /* ECC|MC|MARK */
382 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
383 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
384 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
385 /* MC|ID|ABRT|TRK0|MARK */
386 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
387 /* MCR|MARK */
388 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
389 /* Bad address mark */
390 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
391 /* TRK0 */
392 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
393 /* Abort & !ICRC */
394 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
395 /* Media change request */
396 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
397 /* SRV */
398 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
399 /* Media change */
400 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
401 /* ECC */
402 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
403 /* BBD - block marked bad */
404 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
405 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
406 };
407 static unsigned char stat_table[][4] = {
408 /* Must be first because BUSY means no other bits valid */
409 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
410 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
411 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
412 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
413 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
414 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /*
417 * Is this an error we can process/parse
418 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400419 if (drv_stat & ATA_BUSY) {
420 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jeff Garzikb0955182005-05-12 15:45:22 -0400423 if (drv_err) {
424 /* Look for drv_err */
425 for (i = 0; sense_table[i][0] != 0xFF; i++) {
426 /* Look for best matches first */
427 if ((sense_table[i][0] & drv_err) ==
428 sense_table[i][0]) {
429 *sk = sense_table[i][1];
430 *asc = sense_table[i][2];
431 *ascq = sense_table[i][3];
432 goto translate_done;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400435 /* No immediate match */
436 printk(KERN_WARNING "ata%u: no sense translation for "
437 "error 0x%02x\n", id, drv_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* Fall back to interpreting status bits */
Jeff Garzikb0955182005-05-12 15:45:22 -0400441 for (i = 0; stat_table[i][0] != 0xFF; i++) {
442 if (stat_table[i][0] & drv_stat) {
443 *sk = stat_table[i][1];
444 *asc = stat_table[i][2];
445 *ascq = stat_table[i][3];
446 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400449 /* No error? Undecoded? */
450 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
451 id, drv_stat);
452
453 /* For our last chance pick, use medium read error because
454 * it's much more common than an ATA drive telling you a write
455 * has failed.
456 */
457 *sk = MEDIUM_ERROR;
458 *asc = 0x11; /* "unrecovered read error" */
459 *ascq = 0x04; /* "auto-reallocation failed" */
460
461 translate_done:
462 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
463 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
464 *sk, *asc, *ascq);
465 return;
466}
467
468/*
469 * ata_gen_ata_desc_sense - Generate check condition sense block.
470 * @qc: Command that completed.
471 *
472 * This function is specific to the ATA descriptor format sense
473 * block specified for the ATA pass through commands. Regardless
474 * of whether the command errored or not, return a sense
475 * block. Copy all controller registers into the sense
476 * block. Clear sense key, ASC & ASCQ if there is no error.
477 *
478 * LOCKING:
479 * spin_lock_irqsave(host_set lock)
480 */
481void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
482{
483 struct scsi_cmnd *cmd = qc->scsicmd;
484 struct ata_taskfile *tf = &qc->tf;
485 unsigned char *sb = cmd->sense_buffer;
486 unsigned char *desc = sb + 8;
487
488 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
489
490 cmd->result = SAM_STAT_CHECK_CONDITION;
491
492 /*
493 * Read the controller registers.
494 */
495 assert(NULL != qc->ap->ops->tf_read);
496 qc->ap->ops->tf_read(qc->ap, tf);
497
498 /*
499 * Use ata_to_sense_error() to map status register bits
500 * onto sense key, asc & ascq.
501 */
502 if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
503 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
504 &sb[1], &sb[2], &sb[3]);
505 sb[1] &= 0x0f;
506 }
507
508 /*
509 * Sense data is current and format is descriptor.
510 */
511 sb[0] = 0x72;
512
513 desc[0] = 0x09;
514
515 /*
516 * Set length of additional sense data.
517 * Since we only populate descriptor 0, the total
518 * length is the same (fixed) length as descriptor 0.
519 */
520 desc[1] = sb[7] = 14;
521
522 /*
523 * Copy registers into sense buffer.
524 */
525 desc[2] = 0x00;
526 desc[3] = tf->feature; /* == error reg */
527 desc[5] = tf->nsect;
528 desc[7] = tf->lbal;
529 desc[9] = tf->lbam;
530 desc[11] = tf->lbah;
531 desc[12] = tf->device;
532 desc[13] = tf->command; /* == status reg */
533
534 /*
535 * Fill in Extend bit, and the high order bytes
536 * if applicable.
537 */
538 if (tf->flags & ATA_TFLAG_LBA48) {
539 desc[2] |= 0x01;
540 desc[4] = tf->hob_nsect;
541 desc[6] = tf->hob_lbal;
542 desc[8] = tf->hob_lbam;
543 desc[10] = tf->hob_lbah;
544 }
545}
546
547/**
548 * ata_gen_fixed_sense - generate a SCSI fixed sense block
549 * @qc: Command that we are erroring out
550 *
551 * Leverage ata_to_sense_error() to give us the codes. Fit our
552 * LBA in here if there's room.
553 *
554 * LOCKING:
555 * inherited from caller
556 */
557void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
558{
559 struct scsi_cmnd *cmd = qc->scsicmd;
560 struct ata_taskfile *tf = &qc->tf;
561 unsigned char *sb = cmd->sense_buffer;
562
563 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
564
565 cmd->result = SAM_STAT_CHECK_CONDITION;
566
567 /*
568 * Read the controller registers.
569 */
570 assert(NULL != qc->ap->ops->tf_read);
571 qc->ap->ops->tf_read(qc->ap, tf);
572
573 /*
574 * Use ata_to_sense_error() to map status register bits
575 * onto sense key, asc & ascq.
576 */
577 if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
578 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
579 &sb[2], &sb[12], &sb[13]);
580 sb[2] &= 0x0f;
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 sb[0] = 0x70;
Jeff Garzikb0955182005-05-12 15:45:22 -0400584 sb[7] = 0x0a;
Jeff Garzik0274aa22005-06-22 13:50:56 -0400585
586#if 0 /* when C/H/S support is merged */
Jeff Garzikb0955182005-05-12 15:45:22 -0400587 if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) {
Jeff Garzik0274aa22005-06-22 13:50:56 -0400588#endif
589 if (!(tf->flags & ATA_TFLAG_LBA48)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400590 /* A small (28b) LBA will fit in the 32b info field */
591 sb[0] |= 0x80; /* set valid bit */
592 sb[3] = tf->device & 0x0f;
593 sb[4] = tf->lbah;
594 sb[5] = tf->lbam;
595 sb[6] = tf->lbal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597}
598
599/**
600 * ata_scsi_slave_config - Set SCSI device attributes
601 * @sdev: SCSI device to examine
602 *
603 * This is called before we actually start reading
604 * and writing to the device, to configure certain
605 * SCSI mid-layer behaviors.
606 *
607 * LOCKING:
608 * Defined by SCSI layer. We don't really care.
609 */
610
611int ata_scsi_slave_config(struct scsi_device *sdev)
612{
613 sdev->use_10_for_rw = 1;
614 sdev->use_10_for_ms = 1;
615
616 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
617
618 if (sdev->id < ATA_MAX_DEVICES) {
619 struct ata_port *ap;
620 struct ata_device *dev;
621
622 ap = (struct ata_port *) &sdev->host->hostdata[0];
623 dev = &ap->device[sdev->id];
624
625 /* TODO: 1024 is an arbitrary number, not the
626 * hardware maximum. This should be increased to
627 * 65534 when Jens Axboe's patch for dynamically
628 * determining max_sectors is merged.
629 */
630 if ((dev->flags & ATA_DFLAG_LBA48) &&
631 ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
John W. Linvillef85bdb92005-05-12 15:49:54 -0400632 /*
633 * do not overwrite sdev->host->max_sectors, since
634 * other drives on this host may not support LBA48
635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 blk_queue_max_sectors(sdev->request_queue, 2048);
637 }
638 }
639
640 return 0; /* scsi layer doesn't check return value, sigh */
641}
642
643/**
644 * ata_scsi_error - SCSI layer error handler callback
645 * @host: SCSI host on which error occurred
646 *
647 * Handles SCSI-layer-thrown error events.
648 *
649 * LOCKING:
650 * Inherited from SCSI layer (none, can sleep)
651 *
652 * RETURNS:
653 * Zero.
654 */
655
656int ata_scsi_error(struct Scsi_Host *host)
657{
658 struct ata_port *ap;
659
660 DPRINTK("ENTER\n");
661
662 ap = (struct ata_port *) &host->hostdata[0];
663 ap->ops->eng_timeout(ap);
664
665 /* TODO: this is per-command; when queueing is supported
666 * this code will either change or move to a more
667 * appropriate place
668 */
669 host->host_failed--;
670
671 DPRINTK("EXIT\n");
672 return 0;
673}
674
675/**
676 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
677 * @qc: Storage for translated ATA taskfile
678 * @scsicmd: SCSI command to translate (ignored)
679 *
680 * Sets up an ATA taskfile to issue FLUSH CACHE or
681 * FLUSH CACHE EXT.
682 *
683 * LOCKING:
684 * spin_lock_irqsave(host_set lock)
685 *
686 * RETURNS:
687 * Zero on success, non-zero on error.
688 */
689
690static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
691{
692 struct ata_taskfile *tf = &qc->tf;
693
694 tf->flags |= ATA_TFLAG_DEVICE;
695 tf->protocol = ATA_PROT_NODATA;
696
697 if ((tf->flags & ATA_TFLAG_LBA48) &&
698 (ata_id_has_flush_ext(qc->dev->id)))
699 tf->command = ATA_CMD_FLUSH_EXT;
700 else
701 tf->command = ATA_CMD_FLUSH;
702
703 return 0;
704}
705
706/**
707 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
708 * @qc: Storage for translated ATA taskfile
709 * @scsicmd: SCSI command to translate
710 *
711 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
712 *
713 * LOCKING:
714 * spin_lock_irqsave(host_set lock)
715 *
716 * RETURNS:
717 * Zero on success, non-zero on error.
718 */
719
720static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
721{
722 struct ata_taskfile *tf = &qc->tf;
723 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
724 u64 dev_sectors = qc->dev->n_sectors;
725 u64 sect = 0;
726 u32 n_sect = 0;
727
728 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
729 tf->protocol = ATA_PROT_NODATA;
730 tf->device |= ATA_LBA;
731
732 if (scsicmd[0] == VERIFY) {
733 sect |= ((u64)scsicmd[2]) << 24;
734 sect |= ((u64)scsicmd[3]) << 16;
735 sect |= ((u64)scsicmd[4]) << 8;
736 sect |= ((u64)scsicmd[5]);
737
738 n_sect |= ((u32)scsicmd[7]) << 8;
739 n_sect |= ((u32)scsicmd[8]);
740 }
741
742 else if (scsicmd[0] == VERIFY_16) {
743 sect |= ((u64)scsicmd[2]) << 56;
744 sect |= ((u64)scsicmd[3]) << 48;
745 sect |= ((u64)scsicmd[4]) << 40;
746 sect |= ((u64)scsicmd[5]) << 32;
747 sect |= ((u64)scsicmd[6]) << 24;
748 sect |= ((u64)scsicmd[7]) << 16;
749 sect |= ((u64)scsicmd[8]) << 8;
750 sect |= ((u64)scsicmd[9]);
751
752 n_sect |= ((u32)scsicmd[10]) << 24;
753 n_sect |= ((u32)scsicmd[11]) << 16;
754 n_sect |= ((u32)scsicmd[12]) << 8;
755 n_sect |= ((u32)scsicmd[13]);
756 }
757
758 else
759 return 1;
760
761 if (!n_sect)
762 return 1;
763 if (sect >= dev_sectors)
764 return 1;
765 if ((sect + n_sect) > dev_sectors)
766 return 1;
767 if (lba48) {
768 if (n_sect > (64 * 1024))
769 return 1;
770 } else {
771 if (n_sect > 256)
772 return 1;
773 }
774
775 if (lba48) {
776 tf->command = ATA_CMD_VERIFY_EXT;
777
778 tf->hob_nsect = (n_sect >> 8) & 0xff;
779
780 tf->hob_lbah = (sect >> 40) & 0xff;
781 tf->hob_lbam = (sect >> 32) & 0xff;
782 tf->hob_lbal = (sect >> 24) & 0xff;
783 } else {
784 tf->command = ATA_CMD_VERIFY;
785
786 tf->device |= (sect >> 24) & 0xf;
787 }
788
789 tf->nsect = n_sect & 0xff;
790
791 tf->lbah = (sect >> 16) & 0xff;
792 tf->lbam = (sect >> 8) & 0xff;
793 tf->lbal = sect & 0xff;
794
795 return 0;
796}
797
798/**
799 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
800 * @qc: Storage for translated ATA taskfile
801 * @scsicmd: SCSI command to translate
802 *
803 * Converts any of six SCSI read/write commands into the
804 * ATA counterpart, including starting sector (LBA),
805 * sector count, and taking into account the device's LBA48
806 * support.
807 *
808 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
809 * %WRITE_16 are currently supported.
810 *
811 * LOCKING:
812 * spin_lock_irqsave(host_set lock)
813 *
814 * RETURNS:
815 * Zero on success, non-zero on error.
816 */
817
818static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
819{
820 struct ata_taskfile *tf = &qc->tf;
821 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
822
823 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
824 tf->protocol = qc->dev->xfer_protocol;
825 tf->device |= ATA_LBA;
826
827 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
828 scsicmd[0] == READ_16) {
829 tf->command = qc->dev->read_cmd;
830 } else {
831 tf->command = qc->dev->write_cmd;
832 tf->flags |= ATA_TFLAG_WRITE;
833 }
834
835 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
836 if (lba48) {
837 tf->hob_nsect = scsicmd[7];
838 tf->hob_lbal = scsicmd[2];
839
840 qc->nsect = ((unsigned int)scsicmd[7] << 8) |
841 scsicmd[8];
842 } else {
843 /* if we don't support LBA48 addressing, the request
844 * -may- be too large. */
845 if ((scsicmd[2] & 0xf0) || scsicmd[7])
846 return 1;
847
848 /* stores LBA27:24 in lower 4 bits of device reg */
849 tf->device |= scsicmd[2];
850
851 qc->nsect = scsicmd[8];
852 }
853
854 tf->nsect = scsicmd[8];
855 tf->lbal = scsicmd[5];
856 tf->lbam = scsicmd[4];
857 tf->lbah = scsicmd[3];
858
859 VPRINTK("ten-byte command\n");
860 return 0;
861 }
862
863 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
864 qc->nsect = tf->nsect = scsicmd[4];
865 tf->lbal = scsicmd[3];
866 tf->lbam = scsicmd[2];
867 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
868
869 VPRINTK("six-byte command\n");
870 return 0;
871 }
872
873 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
874 /* rule out impossible LBAs and sector counts */
875 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
876 return 1;
877
878 if (lba48) {
879 tf->hob_nsect = scsicmd[12];
880 tf->hob_lbal = scsicmd[6];
881 tf->hob_lbam = scsicmd[5];
882 tf->hob_lbah = scsicmd[4];
883
884 qc->nsect = ((unsigned int)scsicmd[12] << 8) |
885 scsicmd[13];
886 } else {
887 /* once again, filter out impossible non-zero values */
888 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
889 (scsicmd[6] & 0xf0))
890 return 1;
891
892 /* stores LBA27:24 in lower 4 bits of device reg */
893 tf->device |= scsicmd[6];
894
895 qc->nsect = scsicmd[13];
896 }
897
898 tf->nsect = scsicmd[13];
899 tf->lbal = scsicmd[9];
900 tf->lbam = scsicmd[8];
901 tf->lbah = scsicmd[7];
902
903 VPRINTK("sixteen-byte command\n");
904 return 0;
905 }
906
907 DPRINTK("no-byte command\n");
908 return 1;
909}
910
911static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
912{
913 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzikb0955182005-05-12 15:45:22 -0400914 int need_sense = drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Jeff Garzikb0955182005-05-12 15:45:22 -0400916 /* For ATA pass thru (SAT) commands, generate a sense block if
917 * user mandated it or if there's an error. Note that if we
918 * generate because the user forced us to, a check condition
919 * is generated and the ATA register values are returned
920 * whether the command completed successfully or not. If there
921 * was no error, SK, ASC and ASCQ will all be zero.
922 */
923 if (((cmd->cmnd[0] == ATA_16) || (cmd->cmnd[0] == ATA_12)) &&
924 ((cmd->cmnd[2] & 0x20) || need_sense)) {
925 ata_gen_ata_desc_sense(qc);
926 } else {
927 if (!need_sense) {
928 cmd->result = SAM_STAT_GOOD;
929 } else {
930 /* TODO: decide which descriptor format to use
931 * for 48b LBA devices and call that here
932 * instead of the fixed desc, which is only
933 * good for smaller LBA (and maybe CHS?)
934 * devices.
935 */
936 ata_gen_fixed_sense(qc);
937 }
938 }
939
940 if (need_sense) {
941 /* The ata_gen_..._sense routines fill in tf */
942 ata_dump_status(qc->ap->id, &qc->tf);
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 qc->scsidone(cmd);
946
947 return 0;
948}
949
950/**
951 * ata_scsi_translate - Translate then issue SCSI command to ATA device
952 * @ap: ATA port to which the command is addressed
953 * @dev: ATA device to which the command is addressed
954 * @cmd: SCSI command to execute
955 * @done: SCSI command completion function
956 * @xlat_func: Actor which translates @cmd to an ATA taskfile
957 *
958 * Our ->queuecommand() function has decided that the SCSI
959 * command issued can be directly translated into an ATA
960 * command, rather than handled internally.
961 *
962 * This function sets up an ata_queued_cmd structure for the
963 * SCSI command, and sends that ata_queued_cmd to the hardware.
964 *
965 * LOCKING:
966 * spin_lock_irqsave(host_set lock)
967 */
968
969static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
970 struct scsi_cmnd *cmd,
971 void (*done)(struct scsi_cmnd *),
972 ata_xlat_func_t xlat_func)
973{
974 struct ata_queued_cmd *qc;
975 u8 *scsicmd = cmd->cmnd;
976
977 VPRINTK("ENTER\n");
978
979 qc = ata_scsi_qc_new(ap, dev, cmd, done);
980 if (!qc)
981 return;
982
983 /* data is present; dma-map it */
Jeff Garzik0274aa22005-06-22 13:50:56 -0400984 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
985 cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (unlikely(cmd->request_bufflen < 1)) {
987 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
988 ap->id, dev->devno);
989 goto err_out;
990 }
991
992 if (cmd->use_sg)
993 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
994 else
995 ata_sg_init_one(qc, cmd->request_buffer,
996 cmd->request_bufflen);
997
998 qc->dma_dir = cmd->sc_data_direction;
999 }
1000
1001 qc->complete_fn = ata_scsi_qc_complete;
1002
1003 if (xlat_func(qc, scsicmd))
1004 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* select device, send command to hardware */
1006 if (ata_qc_issue(qc))
1007 goto err_out;
1008
1009 VPRINTK("EXIT\n");
1010 return;
1011
1012err_out:
1013 ata_qc_free(qc);
1014 ata_bad_cdb(cmd, done);
1015 DPRINTK("EXIT - badcmd\n");
1016}
1017
1018/**
1019 * ata_scsi_rbuf_get - Map response buffer.
1020 * @cmd: SCSI command containing buffer to be mapped.
1021 * @buf_out: Pointer to mapped area.
1022 *
1023 * Maps buffer contained within SCSI command @cmd.
1024 *
1025 * LOCKING:
1026 * spin_lock_irqsave(host_set lock)
1027 *
1028 * RETURNS:
1029 * Length of response buffer.
1030 */
1031
1032static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1033{
1034 u8 *buf;
1035 unsigned int buflen;
1036
1037 if (cmd->use_sg) {
1038 struct scatterlist *sg;
1039
1040 sg = (struct scatterlist *) cmd->request_buffer;
1041 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1042 buflen = sg->length;
1043 } else {
1044 buf = cmd->request_buffer;
1045 buflen = cmd->request_bufflen;
1046 }
1047
1048 *buf_out = buf;
1049 return buflen;
1050}
1051
1052/**
1053 * ata_scsi_rbuf_put - Unmap response buffer.
1054 * @cmd: SCSI command containing buffer to be unmapped.
1055 * @buf: buffer to unmap
1056 *
1057 * Unmaps response buffer contained within @cmd.
1058 *
1059 * LOCKING:
1060 * spin_lock_irqsave(host_set lock)
1061 */
1062
1063static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1064{
1065 if (cmd->use_sg) {
1066 struct scatterlist *sg;
1067
1068 sg = (struct scatterlist *) cmd->request_buffer;
1069 kunmap_atomic(buf - sg->offset, KM_USER0);
1070 }
1071}
1072
1073/**
1074 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1075 * @args: device IDENTIFY data / SCSI command of interest.
1076 * @actor: Callback hook for desired SCSI command simulator
1077 *
1078 * Takes care of the hard work of simulating a SCSI command...
1079 * Mapping the response buffer, calling the command's handler,
1080 * and handling the handler's return value. This return value
1081 * indicates whether the handler wishes the SCSI command to be
1082 * completed successfully, or not.
1083 *
1084 * LOCKING:
1085 * spin_lock_irqsave(host_set lock)
1086 */
1087
1088void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1089 unsigned int (*actor) (struct ata_scsi_args *args,
1090 u8 *rbuf, unsigned int buflen))
1091{
1092 u8 *rbuf;
1093 unsigned int buflen, rc;
1094 struct scsi_cmnd *cmd = args->cmd;
1095
1096 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1097 memset(rbuf, 0, buflen);
1098 rc = actor(args, rbuf, buflen);
1099 ata_scsi_rbuf_put(cmd, rbuf);
1100
1101 if (rc)
1102 ata_bad_cdb(cmd, args->done);
1103 else {
1104 cmd->result = SAM_STAT_GOOD;
1105 args->done(cmd);
1106 }
1107}
1108
1109/**
1110 * ata_scsiop_inq_std - Simulate INQUIRY command
1111 * @args: device IDENTIFY data / SCSI command of interest.
1112 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1113 * @buflen: Response buffer length.
1114 *
1115 * Returns standard device identification data associated
1116 * with non-EVPD INQUIRY command output.
1117 *
1118 * LOCKING:
1119 * spin_lock_irqsave(host_set lock)
1120 */
1121
1122unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1123 unsigned int buflen)
1124{
1125 u8 hdr[] = {
1126 TYPE_DISK,
1127 0,
1128 0x5, /* claim SPC-3 version compatibility */
1129 2,
1130 95 - 4
1131 };
1132
1133 /* set scsi removeable (RMB) bit per ata bit */
1134 if (ata_id_removeable(args->id))
1135 hdr[1] |= (1 << 7);
1136
1137 VPRINTK("ENTER\n");
1138
1139 memcpy(rbuf, hdr, sizeof(hdr));
1140
1141 if (buflen > 35) {
1142 memcpy(&rbuf[8], "ATA ", 8);
1143 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1144 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
1145 if (rbuf[32] == 0 || rbuf[32] == ' ')
1146 memcpy(&rbuf[32], "n/a ", 4);
1147 }
1148
1149 if (buflen > 63) {
1150 const u8 versions[] = {
1151 0x60, /* SAM-3 (no version claimed) */
1152
1153 0x03,
1154 0x20, /* SBC-2 (no version claimed) */
1155
1156 0x02,
1157 0x60 /* SPC-3 (no version claimed) */
1158 };
1159
1160 memcpy(rbuf + 59, versions, sizeof(versions));
1161 }
1162
1163 return 0;
1164}
1165
1166/**
1167 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
1168 * @args: device IDENTIFY data / SCSI command of interest.
1169 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1170 * @buflen: Response buffer length.
1171 *
1172 * Returns list of inquiry EVPD pages available.
1173 *
1174 * LOCKING:
1175 * spin_lock_irqsave(host_set lock)
1176 */
1177
1178unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1179 unsigned int buflen)
1180{
1181 const u8 pages[] = {
1182 0x00, /* page 0x00, this page */
1183 0x80, /* page 0x80, unit serial no page */
1184 0x83 /* page 0x83, device ident page */
1185 };
1186 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
1187
1188 if (buflen > 6)
1189 memcpy(rbuf + 4, pages, sizeof(pages));
1190
1191 return 0;
1192}
1193
1194/**
1195 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
1196 * @args: device IDENTIFY data / SCSI command of interest.
1197 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1198 * @buflen: Response buffer length.
1199 *
1200 * Returns ATA device serial number.
1201 *
1202 * LOCKING:
1203 * spin_lock_irqsave(host_set lock)
1204 */
1205
1206unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1207 unsigned int buflen)
1208{
1209 const u8 hdr[] = {
1210 0,
1211 0x80, /* this page code */
1212 0,
1213 ATA_SERNO_LEN, /* page len */
1214 };
1215 memcpy(rbuf, hdr, sizeof(hdr));
1216
1217 if (buflen > (ATA_SERNO_LEN + 4 - 1))
1218 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
1219 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1220
1221 return 0;
1222}
1223
1224static const char *inq_83_str = "Linux ATA-SCSI simulator";
1225
1226/**
1227 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
1228 * @args: device IDENTIFY data / SCSI command of interest.
1229 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1230 * @buflen: Response buffer length.
1231 *
1232 * Returns device identification. Currently hardcoded to
1233 * return "Linux ATA-SCSI simulator".
1234 *
1235 * LOCKING:
1236 * spin_lock_irqsave(host_set lock)
1237 */
1238
1239unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1240 unsigned int buflen)
1241{
1242 rbuf[1] = 0x83; /* this page code */
1243 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
1244
1245 /* our one and only identification descriptor (vendor-specific) */
1246 if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
1247 rbuf[4 + 0] = 2; /* code set: ASCII */
1248 rbuf[4 + 3] = strlen(inq_83_str);
1249 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
1250 }
1251
1252 return 0;
1253}
1254
1255/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001256 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 * @args: device IDENTIFY data / SCSI command of interest.
1258 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1259 * @buflen: Response buffer length.
1260 *
1261 * No operation. Simply returns success to caller, to indicate
1262 * that the caller should successfully complete this SCSI command.
1263 *
1264 * LOCKING:
1265 * spin_lock_irqsave(host_set lock)
1266 */
1267
1268unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1269 unsigned int buflen)
1270{
1271 VPRINTK("ENTER\n");
1272 return 0;
1273}
1274
1275/**
1276 * ata_msense_push - Push data onto MODE SENSE data output buffer
1277 * @ptr_io: (input/output) Location to store more output data
1278 * @last: End of output data buffer
1279 * @buf: Pointer to BLOB being added to output buffer
1280 * @buflen: Length of BLOB
1281 *
1282 * Store MODE SENSE data on an output buffer.
1283 *
1284 * LOCKING:
1285 * None.
1286 */
1287
1288static void ata_msense_push(u8 **ptr_io, const u8 *last,
1289 const u8 *buf, unsigned int buflen)
1290{
1291 u8 *ptr = *ptr_io;
1292
1293 if ((ptr + buflen - 1) > last)
1294 return;
1295
1296 memcpy(ptr, buf, buflen);
1297
1298 ptr += buflen;
1299
1300 *ptr_io = ptr;
1301}
1302
1303/**
1304 * ata_msense_caching - Simulate MODE SENSE caching info page
1305 * @id: device IDENTIFY data
1306 * @ptr_io: (input/output) Location to store more output data
1307 * @last: End of output data buffer
1308 *
1309 * Generate a caching info page, which conditionally indicates
1310 * write caching to the SCSI layer, depending on device
1311 * capabilities.
1312 *
1313 * LOCKING:
1314 * None.
1315 */
1316
1317static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1318 const u8 *last)
1319{
1320 u8 page[] = {
1321 0x8, /* page code */
1322 0x12, /* page length */
1323 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1324 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1325 };
1326
1327 if (ata_id_wcache_enabled(id))
1328 page[2] |= (1 << 2); /* write cache enable */
1329 if (!ata_id_rahead_enabled(id))
1330 page[12] |= (1 << 5); /* disable read ahead */
1331
1332 ata_msense_push(ptr_io, last, page, sizeof(page));
1333 return sizeof(page);
1334}
1335
1336/**
1337 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1338 * @dev: Device associated with this MODE SENSE command
1339 * @ptr_io: (input/output) Location to store more output data
1340 * @last: End of output data buffer
1341 *
1342 * Generate a generic MODE SENSE control mode page.
1343 *
1344 * LOCKING:
1345 * None.
1346 */
1347
1348static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1349{
1350 const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1351
1352 /* byte 2: set the descriptor format sense data bit (bit 2)
1353 * since we need to support returning this format for SAT
1354 * commands and any SCSI commands against a 48b LBA device.
1355 */
1356
1357 ata_msense_push(ptr_io, last, page, sizeof(page));
1358 return sizeof(page);
1359}
1360
1361/**
1362 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1363 * @dev: Device associated with this MODE SENSE command
1364 * @ptr_io: (input/output) Location to store more output data
1365 * @last: End of output data buffer
1366 *
1367 * Generate a generic MODE SENSE r/w error recovery page.
1368 *
1369 * LOCKING:
1370 * None.
1371 */
1372
1373static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1374{
1375 const u8 page[] = {
1376 0x1, /* page code */
1377 0xa, /* page length */
1378 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1379 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1380 };
1381
1382 ata_msense_push(ptr_io, last, page, sizeof(page));
1383 return sizeof(page);
1384}
1385
1386/**
1387 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1388 * @args: device IDENTIFY data / SCSI command of interest.
1389 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1390 * @buflen: Response buffer length.
1391 *
1392 * Simulate MODE SENSE commands.
1393 *
1394 * LOCKING:
1395 * spin_lock_irqsave(host_set lock)
1396 */
1397
1398unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1399 unsigned int buflen)
1400{
1401 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1402 unsigned int page_control, six_byte, output_len;
1403
1404 VPRINTK("ENTER\n");
1405
1406 six_byte = (scsicmd[0] == MODE_SENSE);
1407
1408 /* we only support saved and current values (which we treat
1409 * in the same manner)
1410 */
1411 page_control = scsicmd[2] >> 6;
1412 if ((page_control != 0) && (page_control != 3))
1413 return 1;
1414
1415 if (six_byte)
1416 output_len = 4;
1417 else
1418 output_len = 8;
1419
1420 p = rbuf + output_len;
1421 last = rbuf + buflen - 1;
1422
1423 switch(scsicmd[2] & 0x3f) {
1424 case 0x01: /* r/w error recovery */
1425 output_len += ata_msense_rw_recovery(&p, last);
1426 break;
1427
1428 case 0x08: /* caching */
1429 output_len += ata_msense_caching(args->id, &p, last);
1430 break;
1431
1432 case 0x0a: { /* control mode */
1433 output_len += ata_msense_ctl_mode(&p, last);
1434 break;
1435 }
1436
1437 case 0x3f: /* all pages */
1438 output_len += ata_msense_rw_recovery(&p, last);
1439 output_len += ata_msense_caching(args->id, &p, last);
1440 output_len += ata_msense_ctl_mode(&p, last);
1441 break;
1442
1443 default: /* invalid page code */
1444 return 1;
1445 }
1446
1447 if (six_byte) {
1448 output_len--;
1449 rbuf[0] = output_len;
1450 } else {
1451 output_len -= 2;
1452 rbuf[0] = output_len >> 8;
1453 rbuf[1] = output_len;
1454 }
1455
1456 return 0;
1457}
1458
1459/**
1460 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1461 * @args: device IDENTIFY data / SCSI command of interest.
1462 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1463 * @buflen: Response buffer length.
1464 *
1465 * Simulate READ CAPACITY commands.
1466 *
1467 * LOCKING:
1468 * spin_lock_irqsave(host_set lock)
1469 */
1470
1471unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1472 unsigned int buflen)
1473{
1474 u64 n_sectors;
1475 u32 tmp;
1476
1477 VPRINTK("ENTER\n");
1478
1479 if (ata_id_has_lba48(args->id))
1480 n_sectors = ata_id_u64(args->id, 100);
1481 else
1482 n_sectors = ata_id_u32(args->id, 60);
1483 n_sectors--; /* ATA TotalUserSectors - 1 */
1484
1485 tmp = n_sectors; /* note: truncates, if lba48 */
1486 if (args->cmd->cmnd[0] == READ_CAPACITY) {
1487 /* sector count, 32-bit */
1488 rbuf[0] = tmp >> (8 * 3);
1489 rbuf[1] = tmp >> (8 * 2);
1490 rbuf[2] = tmp >> (8 * 1);
1491 rbuf[3] = tmp;
1492
1493 /* sector size */
1494 tmp = ATA_SECT_SIZE;
1495 rbuf[6] = tmp >> 8;
1496 rbuf[7] = tmp;
1497
1498 } else {
1499 /* sector count, 64-bit */
1500 rbuf[2] = n_sectors >> (8 * 7);
1501 rbuf[3] = n_sectors >> (8 * 6);
1502 rbuf[4] = n_sectors >> (8 * 5);
1503 rbuf[5] = n_sectors >> (8 * 4);
1504 rbuf[6] = tmp >> (8 * 3);
1505 rbuf[7] = tmp >> (8 * 2);
1506 rbuf[8] = tmp >> (8 * 1);
1507 rbuf[9] = tmp;
1508
1509 /* sector size */
1510 tmp = ATA_SECT_SIZE;
1511 rbuf[12] = tmp >> 8;
1512 rbuf[13] = tmp;
1513 }
1514
1515 return 0;
1516}
1517
1518/**
1519 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1520 * @args: device IDENTIFY data / SCSI command of interest.
1521 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1522 * @buflen: Response buffer length.
1523 *
1524 * Simulate REPORT LUNS command.
1525 *
1526 * LOCKING:
1527 * spin_lock_irqsave(host_set lock)
1528 */
1529
1530unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1531 unsigned int buflen)
1532{
1533 VPRINTK("ENTER\n");
1534 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1535
1536 return 0;
1537}
1538
1539/**
1540 * ata_scsi_badcmd - End a SCSI request with an error
1541 * @cmd: SCSI request to be handled
1542 * @done: SCSI command completion function
1543 * @asc: SCSI-defined additional sense code
1544 * @ascq: SCSI-defined additional sense code qualifier
1545 *
1546 * Helper function that completes a SCSI command with
1547 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1548 * and the specified additional sense codes.
1549 *
1550 * LOCKING:
1551 * spin_lock_irqsave(host_set lock)
1552 */
1553
1554void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1555{
1556 DPRINTK("ENTER\n");
1557 cmd->result = SAM_STAT_CHECK_CONDITION;
1558
1559 cmd->sense_buffer[0] = 0x70;
1560 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1561 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1562 cmd->sense_buffer[12] = asc;
1563 cmd->sense_buffer[13] = ascq;
1564
1565 done(cmd);
1566}
1567
1568static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1569{
1570 struct scsi_cmnd *cmd = qc->scsicmd;
1571
1572 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
1573 DPRINTK("request check condition\n");
1574
1575 cmd->result = SAM_STAT_CHECK_CONDITION;
1576
1577 qc->scsidone(cmd);
1578
1579 return 1;
1580 } else {
1581 u8 *scsicmd = cmd->cmnd;
1582
1583 if (scsicmd[0] == INQUIRY) {
1584 u8 *buf = NULL;
1585 unsigned int buflen;
1586
1587 buflen = ata_scsi_rbuf_get(cmd, &buf);
1588 buf[2] = 0x5;
1589 buf[3] = (buf[3] & 0xf0) | 2;
1590 ata_scsi_rbuf_put(cmd, buf);
1591 }
1592 cmd->result = SAM_STAT_GOOD;
1593 }
1594
1595 qc->scsidone(cmd);
1596
1597 return 0;
1598}
1599/**
1600 * atapi_xlat - Initialize PACKET taskfile
1601 * @qc: command structure to be initialized
1602 * @scsicmd: SCSI CDB associated with this PACKET command
1603 *
1604 * LOCKING:
1605 * spin_lock_irqsave(host_set lock)
1606 *
1607 * RETURNS:
1608 * Zero on success, non-zero on failure.
1609 */
1610
1611static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1612{
1613 struct scsi_cmnd *cmd = qc->scsicmd;
1614 struct ata_device *dev = qc->dev;
1615 int using_pio = (dev->flags & ATA_DFLAG_PIO);
Jeff Garzik0274aa22005-06-22 13:50:56 -04001616 int nodata = (cmd->sc_data_direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 if (!using_pio)
1619 /* Check whether ATAPI DMA is safe */
1620 if (ata_check_atapi_dma(qc))
1621 using_pio = 1;
1622
1623 memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
1624
1625 qc->complete_fn = atapi_qc_complete;
1626
1627 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Jeff Garzik0274aa22005-06-22 13:50:56 -04001628 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 qc->tf.flags |= ATA_TFLAG_WRITE;
1630 DPRINTK("direction: write\n");
1631 }
1632
1633 qc->tf.command = ATA_CMD_PACKET;
1634
1635 /* no data, or PIO data xfer */
1636 if (using_pio || nodata) {
1637 if (nodata)
1638 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
1639 else
1640 qc->tf.protocol = ATA_PROT_ATAPI;
1641 qc->tf.lbam = (8 * 1024) & 0xff;
1642 qc->tf.lbah = (8 * 1024) >> 8;
1643 }
1644
1645 /* DMA data xfer */
1646 else {
1647 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1648 qc->tf.feature |= ATAPI_PKT_DMA;
1649
1650#ifdef ATAPI_ENABLE_DMADIR
1651 /* some SATA bridges need us to indicate data xfer direction */
Jeff Garzik0274aa22005-06-22 13:50:56 -04001652 if (cmd->sc_data_direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 qc->tf.feature |= ATAPI_DMADIR;
1654#endif
1655 }
1656
1657 qc->nbytes = cmd->bufflen;
1658
1659 return 0;
1660}
1661
1662/**
1663 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1664 * @ap: ATA port to which the device is attached
1665 * @scsidev: SCSI device from which we derive the ATA device
1666 *
1667 * Given various information provided in struct scsi_cmnd,
1668 * map that onto an ATA bus, and using that mapping
1669 * determine which ata_device is associated with the
1670 * SCSI command to be sent.
1671 *
1672 * LOCKING:
1673 * spin_lock_irqsave(host_set lock)
1674 *
1675 * RETURNS:
1676 * Associated ATA device, or %NULL if not found.
1677 */
1678
1679static struct ata_device *
1680ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1681{
1682 struct ata_device *dev;
1683
1684 /* skip commands not addressed to targets we simulate */
1685 if (likely(scsidev->id < ATA_MAX_DEVICES))
1686 dev = &ap->device[scsidev->id];
1687 else
1688 return NULL;
1689
1690 if (unlikely((scsidev->channel != 0) ||
1691 (scsidev->lun != 0)))
1692 return NULL;
1693
1694 if (unlikely(!ata_dev_present(dev)))
1695 return NULL;
1696
1697#ifndef ATA_ENABLE_ATAPI
1698 if (unlikely(dev->class == ATA_DEV_ATAPI))
1699 return NULL;
1700#endif
1701
1702 return dev;
1703}
1704
Jeff Garzikb0955182005-05-12 15:45:22 -04001705/*
1706 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
1707 * @byte1: Byte 1 from pass-thru CDB.
1708 *
1709 * RETURNS:
1710 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
1711 */
1712static u8
1713ata_scsi_map_proto(u8 byte1)
1714{
1715 switch((byte1 & 0x1e) >> 1) {
1716 case 3: /* Non-data */
1717 return ATA_PROT_NODATA;
1718
1719 case 6: /* DMA */
1720 return ATA_PROT_DMA;
1721
1722 case 4: /* PIO Data-in */
1723 case 5: /* PIO Data-out */
1724 if (byte1 & 0xe0) {
1725 return ATA_PROT_PIO_MULT;
1726 }
1727 return ATA_PROT_PIO;
1728
1729 case 10: /* Device Reset */
1730 case 0: /* Hard Reset */
1731 case 1: /* SRST */
1732 case 2: /* Bus Idle */
1733 case 7: /* Packet */
1734 case 8: /* DMA Queued */
1735 case 9: /* Device Diagnostic */
1736 case 11: /* UDMA Data-in */
1737 case 12: /* UDMA Data-Out */
1738 case 13: /* FPDMA */
1739 default: /* Reserved */
1740 break;
1741 }
1742
1743 return ATA_PROT_UNKNOWN;
1744}
1745
1746/**
1747 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
1748 * @qc: command structure to be initialized
1749 * @cmd: SCSI command to convert
1750 *
1751 * Handles either 12 or 16-byte versions of the CDB.
1752 *
1753 * RETURNS:
1754 * Zero on success, non-zero on failure.
1755 */
1756static unsigned int
1757ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd)
1758{
1759 struct ata_taskfile *tf = &(qc->tf);
1760 struct scsi_cmnd *cmd = qc->scsicmd;
1761
1762 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
1763 return 1;
1764
1765 /*
1766 * 12 and 16 byte CDBs use different offsets to
1767 * provide the various register values.
1768 */
1769 if (scsicmd[0] == ATA_16) {
1770 /*
1771 * 16-byte CDB - may contain extended commands.
1772 *
1773 * If that is the case, copy the upper byte register values.
1774 */
1775 if (scsicmd[1] & 0x01) {
1776 tf->hob_feature = scsicmd[3];
1777 tf->hob_nsect = scsicmd[5];
1778 tf->hob_lbal = scsicmd[7];
1779 tf->hob_lbam = scsicmd[9];
1780 tf->hob_lbah = scsicmd[11];
1781 tf->flags |= ATA_TFLAG_LBA48;
1782 } else
1783 tf->flags &= ~ATA_TFLAG_LBA48;
1784
1785 /*
1786 * Always copy low byte, device and command registers.
1787 */
1788 tf->feature = scsicmd[4];
1789 tf->nsect = scsicmd[6];
1790 tf->lbal = scsicmd[8];
1791 tf->lbam = scsicmd[10];
1792 tf->lbah = scsicmd[12];
1793 tf->device = scsicmd[13];
1794 tf->command = scsicmd[14];
1795 } else {
1796 /*
1797 * 12-byte CDB - incapable of extended commands.
1798 */
1799 tf->flags &= ~ATA_TFLAG_LBA48;
1800
1801 tf->feature = scsicmd[3];
1802 tf->nsect = scsicmd[4];
1803 tf->lbal = scsicmd[5];
1804 tf->lbam = scsicmd[6];
1805 tf->lbah = scsicmd[7];
1806 tf->device = scsicmd[8];
1807 tf->command = scsicmd[9];
1808 }
1809
1810 /*
1811 * Filter SET_FEATURES - XFER MODE command -- otherwise,
1812 * SET_FEATURES - XFER MODE must be preceded/succeeded
1813 * by an update to hardware-specific registers for each
1814 * controller (i.e. the reason for ->set_piomode(),
1815 * ->set_dmamode(), and ->post_set_mode() hooks).
1816 */
1817 if ((tf->command == ATA_CMD_SET_FEATURES)
1818 && (tf->feature == SETFEATURES_XFER))
1819 return 1;
1820
1821 /*
1822 * Set flags so that all registers will be written,
1823 * and pass on write indication (used for PIO/DMA
1824 * setup.)
1825 */
1826 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
1827
Jeff Garzik0274aa22005-06-22 13:50:56 -04001828 if (cmd->sc_data_direction == DMA_TO_DEVICE)
Jeff Garzikb0955182005-05-12 15:45:22 -04001829 tf->flags |= ATA_TFLAG_WRITE;
1830
1831 /*
1832 * Set transfer length.
1833 *
1834 * TODO: find out if we need to do more here to
1835 * cover scatter/gather case.
1836 */
1837 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
1838
1839 return 0;
1840}
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842/**
1843 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1844 * @dev: ATA device
1845 * @cmd: SCSI command opcode to consider
1846 *
1847 * Look up the SCSI command given, and determine whether the
1848 * SCSI command is to be translated or simulated.
1849 *
1850 * RETURNS:
1851 * Pointer to translation function if possible, %NULL if not.
1852 */
1853
1854static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1855{
1856 switch (cmd) {
1857 case READ_6:
1858 case READ_10:
1859 case READ_16:
1860
1861 case WRITE_6:
1862 case WRITE_10:
1863 case WRITE_16:
1864 return ata_scsi_rw_xlat;
1865
1866 case SYNCHRONIZE_CACHE:
1867 if (ata_try_flush_cache(dev))
1868 return ata_scsi_flush_xlat;
1869 break;
1870
1871 case VERIFY:
1872 case VERIFY_16:
1873 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04001874
1875 case ATA_12:
1876 case ATA_16:
1877 return ata_scsi_pass_thru;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879
1880 return NULL;
1881}
1882
1883/**
1884 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1885 * @ap: ATA port to which the command was being sent
1886 * @cmd: SCSI command to dump
1887 *
1888 * Prints the contents of a SCSI command via printk().
1889 */
1890
1891static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1892 struct scsi_cmnd *cmd)
1893{
1894#ifdef ATA_DEBUG
1895 struct scsi_device *scsidev = cmd->device;
1896 u8 *scsicmd = cmd->cmnd;
1897
1898 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1899 ap->id,
1900 scsidev->channel, scsidev->id, scsidev->lun,
1901 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1902 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1903 scsicmd[8]);
1904#endif
1905}
1906
1907/**
1908 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1909 * @cmd: SCSI command to be sent
1910 * @done: Completion function, called when command is complete
1911 *
1912 * In some cases, this function translates SCSI commands into
1913 * ATA taskfiles, and queues the taskfiles to be sent to
1914 * hardware. In other cases, this function simulates a
1915 * SCSI device by evaluating and responding to certain
1916 * SCSI commands. This creates the overall effect of
1917 * ATA and ATAPI devices appearing as SCSI devices.
1918 *
1919 * LOCKING:
1920 * Releases scsi-layer-held lock, and obtains host_set lock.
1921 *
1922 * RETURNS:
1923 * Zero.
1924 */
1925
1926int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1927{
1928 struct ata_port *ap;
1929 struct ata_device *dev;
1930 struct scsi_device *scsidev = cmd->device;
1931
1932 ap = (struct ata_port *) &scsidev->host->hostdata[0];
1933
1934 ata_scsi_dump_cdb(ap, cmd);
1935
1936 dev = ata_scsi_find_dev(ap, scsidev);
1937 if (unlikely(!dev)) {
1938 cmd->result = (DID_BAD_TARGET << 16);
1939 done(cmd);
1940 goto out_unlock;
1941 }
1942
1943 if (dev->class == ATA_DEV_ATA) {
1944 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
1945 cmd->cmnd[0]);
1946
1947 if (xlat_func)
1948 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1949 else
1950 ata_scsi_simulate(dev->id, cmd, done);
1951 } else
1952 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
1953
1954out_unlock:
1955 return 0;
1956}
1957
1958/**
1959 * ata_scsi_simulate - simulate SCSI command on ATA device
1960 * @id: current IDENTIFY data for target device.
1961 * @cmd: SCSI command being sent to device.
1962 * @done: SCSI command completion function.
1963 *
1964 * Interprets and directly executes a select list of SCSI commands
1965 * that can be handled internally.
1966 *
1967 * LOCKING:
1968 * spin_lock_irqsave(host_set lock)
1969 */
1970
1971void ata_scsi_simulate(u16 *id,
1972 struct scsi_cmnd *cmd,
1973 void (*done)(struct scsi_cmnd *))
1974{
1975 struct ata_scsi_args args;
1976 u8 *scsicmd = cmd->cmnd;
1977
1978 args.id = id;
1979 args.cmd = cmd;
1980 args.done = done;
1981
1982 switch(scsicmd[0]) {
1983 /* no-op's, complete with success */
1984 case SYNCHRONIZE_CACHE:
1985 case REZERO_UNIT:
1986 case SEEK_6:
1987 case SEEK_10:
1988 case TEST_UNIT_READY:
1989 case FORMAT_UNIT: /* FIXME: correct? */
1990 case SEND_DIAGNOSTIC: /* FIXME: correct? */
1991 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1992 break;
1993
1994 case INQUIRY:
1995 if (scsicmd[1] & 2) /* is CmdDt set? */
1996 ata_bad_cdb(cmd, done);
1997 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
1998 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1999 else if (scsicmd[2] == 0x00)
2000 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2001 else if (scsicmd[2] == 0x80)
2002 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2003 else if (scsicmd[2] == 0x83)
2004 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2005 else
2006 ata_bad_cdb(cmd, done);
2007 break;
2008
2009 case MODE_SENSE:
2010 case MODE_SENSE_10:
2011 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2012 break;
2013
2014 case MODE_SELECT: /* unconditionally return */
2015 case MODE_SELECT_10: /* bad-field-in-cdb */
2016 ata_bad_cdb(cmd, done);
2017 break;
2018
2019 case READ_CAPACITY:
2020 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2021 break;
2022
2023 case SERVICE_ACTION_IN:
2024 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2025 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2026 else
2027 ata_bad_cdb(cmd, done);
2028 break;
2029
2030 case REPORT_LUNS:
2031 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2032 break;
2033
Jeff Garzikb0955182005-05-12 15:45:22 -04002034 /* mandatory commands we haven't implemented yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 case REQUEST_SENSE:
2036
2037 /* all other commands */
2038 default:
2039 ata_bad_scsiop(cmd, done);
2040 break;
2041 }
2042}
2043