[PATCH] libata: use ATA printk helpers
[linux-2.6.git] / drivers / scsi / libata-scsi.c
bloba9b4083a4f67903ac4826824049089392b95f7fa
1 /*
2 * libata-scsi.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
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.
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.
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.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from
31 * - http://www.t10.org/
32 * - http://www.t13.org/
36 #include <linux/kernel.h>
37 #include <linux/blkdev.h>
38 #include <linux/spinlock.h>
39 #include <scsi/scsi.h>
40 #include <scsi/scsi_host.h>
41 #include <scsi/scsi_eh.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_request.h>
44 #include <scsi/scsi_transport.h>
45 #include <linux/libata.h>
46 #include <linux/hdreg.h>
47 #include <asm/uaccess.h>
49 #include "libata.h"
51 #define SECTOR_SIZE 512
53 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
54 static struct ata_device *
55 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
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
67 static 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
78 static 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
87 static 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 */
97 * libata transport template. libata doesn't do real transport stuff.
98 * It just needs the eh_timed_out hook.
100 struct scsi_transport_template ata_scsi_transport_template = {
101 .eh_strategy_handler = ata_scsi_error,
102 .eh_timed_out = ata_scsi_timed_out,
106 static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
107 void (*done)(struct scsi_cmnd *))
109 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
110 /* "Invalid field in cbd" */
111 done(cmd);
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
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.
126 * LOCKING:
127 * Defined by the SCSI layer. We don't really care.
129 * RETURNS:
130 * Zero.
132 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
133 sector_t capacity, int geom[])
135 geom[0] = 255;
136 geom[1] = 63;
137 sector_div(capacity, 255*63);
138 geom[2] = capacity;
140 return 0;
144 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
145 * @scsidev: Device to which we are issuing command
146 * @arg: User provided data for issuing command
148 * LOCKING:
149 * Defined by the SCSI layer. We don't really care.
151 * RETURNS:
152 * Zero on success, negative errno on error.
155 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
157 int rc = 0;
158 u8 scsi_cmd[MAX_COMMAND_SIZE];
159 u8 args[4], *argbuf = NULL;
160 int argsize = 0;
161 struct scsi_sense_hdr sshdr;
162 enum dma_data_direction data_dir;
164 if (arg == NULL)
165 return -EINVAL;
167 if (copy_from_user(args, arg, sizeof(args)))
168 return -EFAULT;
170 memset(scsi_cmd, 0, sizeof(scsi_cmd));
172 if (args[3]) {
173 argsize = SECTOR_SIZE * args[3];
174 argbuf = kmalloc(argsize, GFP_KERNEL);
175 if (argbuf == NULL) {
176 rc = -ENOMEM;
177 goto error;
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 */
183 data_dir = DMA_FROM_DEVICE;
184 } else {
185 scsi_cmd[1] = (3 << 1); /* Non-data */
186 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
187 data_dir = DMA_NONE;
190 scsi_cmd[0] = ATA_16;
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];
201 scsi_cmd[14] = args[0];
203 /* Good values for timeout and retries? Values below
204 from scsi_ioctl_send_command() for default case... */
205 if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize,
206 &sshdr, (10*HZ), 5)) {
207 rc = -EIO;
208 goto error;
211 /* Need code to retrieve data from check condition? */
213 if ((argbuf)
214 && copy_to_user(arg + sizeof(args), argbuf, argsize))
215 rc = -EFAULT;
216 error:
217 if (argbuf)
218 kfree(argbuf);
220 return rc;
224 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
225 * @scsidev: Device to which we are issuing command
226 * @arg: User provided data for issuing command
228 * LOCKING:
229 * Defined by the SCSI layer. We don't really care.
231 * RETURNS:
232 * Zero on success, negative errno on error.
234 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
236 int rc = 0;
237 u8 scsi_cmd[MAX_COMMAND_SIZE];
238 u8 args[7];
239 struct scsi_sense_hdr sshdr;
241 if (arg == NULL)
242 return -EINVAL;
244 if (copy_from_user(args, arg, sizeof(args)))
245 return -EFAULT;
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];
258 /* Good values for timeout and retries? Values below
259 from scsi_ioctl_send_command() for default case... */
260 if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr,
261 (10*HZ), 5))
262 rc = -EIO;
264 /* Need code to retrieve data from check condition? */
265 return rc;
268 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
270 int val = -EINVAL, rc = -EINVAL;
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;
279 case ATA_IOC_SET_IO32:
280 val = (unsigned long) arg;
281 if (val != 0)
282 return -EINVAL;
283 return 0;
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);
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);
295 default:
296 rc = -ENOTTY;
297 break;
300 return rc;
304 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
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
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.
313 * If a command was available, fill in the SCSI-specific
314 * portions of the structure with information on the
315 * current command.
317 * LOCKING:
318 * spin_lock_irqsave(host_set lock)
320 * RETURNS:
321 * Command allocated, or %NULL if none available.
323 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
324 struct scsi_cmnd *cmd,
325 void (*done)(struct scsi_cmnd *))
327 struct ata_queued_cmd *qc;
329 qc = ata_qc_new_init(dev);
330 if (qc) {
331 qc->scsicmd = cmd;
332 qc->scsidone = done;
334 if (cmd->use_sg) {
335 qc->__sg = (struct scatterlist *) cmd->request_buffer;
336 qc->n_elem = cmd->use_sg;
337 } else {
338 qc->__sg = &qc->sgent;
339 qc->n_elem = 1;
341 } else {
342 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
343 done(cmd);
346 return qc;
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
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.
358 * LOCKING:
359 * inherited from caller
361 void ata_dump_status(unsigned id, struct ata_taskfile *tf)
363 u8 stat = tf->command, err = tf->feature;
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");
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 ");
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");
394 int ata_scsi_device_resume(struct scsi_device *sdev)
396 struct ata_port *ap = ata_shost_to_port(sdev->host);
397 struct ata_device *dev = &ap->device[sdev->id];
399 return ata_device_resume(dev);
402 int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
404 struct ata_port *ap = ata_shost_to_port(sdev->host);
405 struct ata_device *dev = &ap->device[sdev->id];
407 return ata_device_suspend(dev, state);
411 * ata_to_sense_error - convert ATA error to SCSI error
412 * @id: ATA device number
413 * @drv_stat: value contained in ATA status register
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
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.
423 * LOCKING:
424 * spin_lock_irqsave(host_set lock)
426 void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
427 u8 *ascq)
429 int i;
431 /* Based on the 3ware driver translation table */
432 static const unsigned char sense_table[][4] = {
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
463 static const unsigned char stat_table[][4] = {
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
473 * Is this an error we can process/parse
475 if (drv_stat & ATA_BUSY) {
476 drv_err = 0; /* Ignore the err bits, they're invalid */
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 */
483 if ((sense_table[i][0] & drv_err) ==
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;
491 /* No immediate match */
492 printk(KERN_WARNING "ata%u: no sense translation for "
493 "error 0x%02x\n", id, drv_err);
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;
505 /* No error? Undecoded? */
506 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
507 id, drv_stat);
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;
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;
523 * ata_gen_ata_desc_sense - Generate check condition sense block.
524 * @qc: Command that completed.
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.
532 * LOCKING:
533 * spin_lock_irqsave(host_set lock)
535 void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
537 struct scsi_cmnd *cmd = qc->scsicmd;
538 struct ata_taskfile *tf = &qc->result_tf;
539 unsigned char *sb = cmd->sense_buffer;
540 unsigned char *desc = sb + 8;
542 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
544 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
547 * Use ata_to_sense_error() to map status register bits
548 * onto sense key, asc & ascq.
550 if (qc->err_mask ||
551 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
552 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
553 &sb[1], &sb[2], &sb[3]);
554 sb[1] &= 0x0f;
558 * Sense data is current and format is descriptor.
560 sb[0] = 0x72;
562 desc[0] = 0x09;
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.
569 desc[1] = sb[7] = 14;
572 * Copy registers into sense buffer.
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 */
584 * Fill in Extend bit, and the high order bytes
585 * if applicable.
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;
597 * ata_gen_fixed_sense - generate a SCSI fixed sense block
598 * @qc: Command that we are erroring out
600 * Leverage ata_to_sense_error() to give us the codes. Fit our
601 * LBA in here if there's room.
603 * LOCKING:
604 * inherited from caller
606 void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
608 struct scsi_cmnd *cmd = qc->scsicmd;
609 struct ata_taskfile *tf = &qc->result_tf;
610 unsigned char *sb = cmd->sense_buffer;
612 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
614 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
617 * Use ata_to_sense_error() to map status register bits
618 * onto sense key, asc & ascq.
620 if (qc->err_mask ||
621 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
622 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
623 &sb[2], &sb[12], &sb[13]);
624 sb[2] &= 0x0f;
627 sb[0] = 0x70;
628 sb[7] = 0x0a;
630 if (tf->flags & ATA_TFLAG_LBA48) {
631 /* TODO: find solution for LBA48 descriptors */
634 else if (tf->flags & ATA_TFLAG_LBA) {
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;
643 else {
644 /* TODO: C/H/S */
648 static void ata_scsi_sdev_config(struct scsi_device *sdev)
650 sdev->use_10_for_rw = 1;
651 sdev->use_10_for_ms = 1;
654 static void ata_scsi_dev_config(struct scsi_device *sdev,
655 struct ata_device *dev)
657 unsigned int max_sectors;
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.
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;
670 blk_queue_max_sectors(sdev->request_queue, max_sectors);
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.
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);
684 * ata_scsi_slave_config - Set SCSI device attributes
685 * @sdev: SCSI device to examine
687 * This is called before we actually start reading
688 * and writing to the device, to configure certain
689 * SCSI mid-layer behaviors.
691 * LOCKING:
692 * Defined by SCSI layer. We don't really care.
695 int ata_scsi_slave_config(struct scsi_device *sdev)
697 ata_scsi_sdev_config(sdev);
699 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
701 if (sdev->id < ATA_MAX_DEVICES) {
702 struct ata_port *ap;
703 struct ata_device *dev;
705 ap = ata_shost_to_port(sdev->host);
706 dev = &ap->device[sdev->id];
708 ata_scsi_dev_config(sdev, dev);
711 return 0; /* scsi layer doesn't check return value, sigh */
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
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]
724 * LOCKING:
725 * spin_lock_irqsave(host_set lock)
727 * RETURNS:
728 * Zero on success, non-zero on error.
731 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
732 const u8 *scsicmd)
734 struct ata_taskfile *tf = &qc->tf;
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 */
741 if (scsicmd[4] & 0x2)
742 goto invalid_fld; /* LOEJ bit set not supported */
743 if (((scsicmd[4] >> 4) & 0xf) != 0)
744 goto invalid_fld; /* power conditions not supported */
745 if (scsicmd[4] & 0x1) {
746 tf->nsect = 1; /* 1 sector, lba=0 */
748 if (qc->dev->flags & ATA_DFLAG_LBA) {
749 tf->flags |= ATA_TFLAG_LBA;
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 */
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 */
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.
775 return 0;
777 invalid_fld:
778 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
779 /* "Invalid field in cbd" */
780 return 1;
785 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
786 * @qc: Storage for translated ATA taskfile
787 * @scsicmd: SCSI command to translate (ignored)
789 * Sets up an ATA taskfile to issue FLUSH CACHE or
790 * FLUSH CACHE EXT.
792 * LOCKING:
793 * spin_lock_irqsave(host_set lock)
795 * RETURNS:
796 * Zero on success, non-zero on error.
799 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
801 struct ata_taskfile *tf = &qc->tf;
803 tf->flags |= ATA_TFLAG_DEVICE;
804 tf->protocol = ATA_PROT_NODATA;
806 if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
807 (ata_id_has_flush_ext(qc->dev->id)))
808 tf->command = ATA_CMD_FLUSH_EXT;
809 else
810 tf->command = ATA_CMD_FLUSH;
812 return 0;
816 * scsi_6_lba_len - Get LBA and transfer length
817 * @scsicmd: SCSI command to translate
819 * Calculate LBA and transfer length for 6-byte commands.
821 * RETURNS:
822 * @plba: the LBA
823 * @plen: the transfer length
826 static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
828 u64 lba = 0;
829 u32 len = 0;
831 VPRINTK("six-byte command\n");
833 lba |= ((u64)scsicmd[2]) << 8;
834 lba |= ((u64)scsicmd[3]);
836 len |= ((u32)scsicmd[4]);
838 *plba = lba;
839 *plen = len;
843 * scsi_10_lba_len - Get LBA and transfer length
844 * @scsicmd: SCSI command to translate
846 * Calculate LBA and transfer length for 10-byte commands.
848 * RETURNS:
849 * @plba: the LBA
850 * @plen: the transfer length
853 static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
855 u64 lba = 0;
856 u32 len = 0;
858 VPRINTK("ten-byte command\n");
860 lba |= ((u64)scsicmd[2]) << 24;
861 lba |= ((u64)scsicmd[3]) << 16;
862 lba |= ((u64)scsicmd[4]) << 8;
863 lba |= ((u64)scsicmd[5]);
865 len |= ((u32)scsicmd[7]) << 8;
866 len |= ((u32)scsicmd[8]);
868 *plba = lba;
869 *plen = len;
873 * scsi_16_lba_len - Get LBA and transfer length
874 * @scsicmd: SCSI command to translate
876 * Calculate LBA and transfer length for 16-byte commands.
878 * RETURNS:
879 * @plba: the LBA
880 * @plen: the transfer length
883 static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
885 u64 lba = 0;
886 u32 len = 0;
888 VPRINTK("sixteen-byte command\n");
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]);
899 len |= ((u32)scsicmd[10]) << 24;
900 len |= ((u32)scsicmd[11]) << 16;
901 len |= ((u32)scsicmd[12]) << 8;
902 len |= ((u32)scsicmd[13]);
904 *plba = lba;
905 *plen = len;
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
913 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
915 * LOCKING:
916 * spin_lock_irqsave(host_set lock)
918 * RETURNS:
919 * Zero on success, non-zero on error.
922 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
924 struct ata_taskfile *tf = &qc->tf;
925 struct ata_device *dev = qc->dev;
926 u64 dev_sectors = qc->dev->n_sectors;
927 u64 block;
928 u32 n_block;
930 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
931 tf->protocol = ATA_PROT_NODATA;
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);
937 else
938 goto invalid_fld;
940 if (!n_block)
941 goto nothing_to_do;
942 if (block >= dev_sectors)
943 goto out_of_range;
944 if ((block + n_block) > dev_sectors)
945 goto out_of_range;
947 if (dev->flags & ATA_DFLAG_LBA) {
948 tf->flags |= ATA_TFLAG_LBA;
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;
958 /* use LBA48 */
959 tf->flags |= ATA_TFLAG_LBA48;
960 tf->command = ATA_CMD_VERIFY_EXT;
962 tf->hob_nsect = (n_block >> 8) & 0xff;
964 tf->hob_lbah = (block >> 40) & 0xff;
965 tf->hob_lbam = (block >> 32) & 0xff;
966 tf->hob_lbal = (block >> 24) & 0xff;
967 } else
968 /* request too large even for LBA48 */
969 goto out_of_range;
971 tf->nsect = n_block & 0xff;
973 tf->lbah = (block >> 16) & 0xff;
974 tf->lbam = (block >> 8) & 0xff;
975 tf->lbal = block & 0xff;
977 tf->device |= ATA_LBA;
978 } else {
979 /* CHS */
980 u32 sect, head, cyl, track;
982 if (!lba_28_ok(block, n_block))
983 goto out_of_range;
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;
991 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
992 (u32)block, track, cyl, head, sect);
994 /* Check whether the converted CHS can fit.
995 Cylinder: 0-65535
996 Head: 0-15
997 Sector: 1-255*/
998 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
999 goto out_of_range;
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;
1009 return 0;
1011 invalid_fld:
1012 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1013 /* "Invalid field in cbd" */
1014 return 1;
1016 out_of_range:
1017 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1018 /* "Logical Block Address out of range" */
1019 return 1;
1021 nothing_to_do:
1022 qc->scsicmd->result = SAM_STAT_GOOD;
1023 return 1;
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
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.
1036 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1037 * %WRITE_16 are currently supported.
1039 * LOCKING:
1040 * spin_lock_irqsave(host_set lock)
1042 * RETURNS:
1043 * Zero on success, non-zero on error.
1046 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
1048 struct ata_taskfile *tf = &qc->tf;
1049 struct ata_device *dev = qc->dev;
1050 u64 block;
1051 u32 n_block;
1053 qc->flags |= ATA_QCFLAG_IO;
1054 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1056 if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
1057 scsicmd[0] == WRITE_16)
1058 tf->flags |= ATA_TFLAG_WRITE;
1060 /* Calculate the SCSI LBA, transfer length and FUA. */
1061 switch (scsicmd[0]) {
1062 case READ_10:
1063 case WRITE_10:
1064 scsi_10_lba_len(scsicmd, &block, &n_block);
1065 if (unlikely(scsicmd[1] & (1 << 3)))
1066 tf->flags |= ATA_TFLAG_FUA;
1067 break;
1068 case READ_6:
1069 case WRITE_6:
1070 scsi_6_lba_len(scsicmd, &block, &n_block);
1072 /* for 6-byte r/w commands, transfer length 0
1073 * means 256 blocks of data, not 0 block.
1075 if (!n_block)
1076 n_block = 256;
1077 break;
1078 case READ_16:
1079 case WRITE_16:
1080 scsi_16_lba_len(scsicmd, &block, &n_block);
1081 if (unlikely(scsicmd[1] & (1 << 3)))
1082 tf->flags |= ATA_TFLAG_FUA;
1083 break;
1084 default:
1085 DPRINTK("no-byte command\n");
1086 goto invalid_fld;
1089 /* Check and compose ATA command */
1090 if (!n_block)
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.
1096 * WARNING: one or two older ATA drives treat 0 as 0...
1098 goto nothing_to_do;
1100 if (dev->flags & ATA_DFLAG_LBA) {
1101 tf->flags |= ATA_TFLAG_LBA;
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))
1108 goto out_of_range;
1110 /* use LBA48 */
1111 tf->flags |= ATA_TFLAG_LBA48;
1113 tf->hob_nsect = (n_block >> 8) & 0xff;
1115 tf->hob_lbah = (block >> 40) & 0xff;
1116 tf->hob_lbam = (block >> 32) & 0xff;
1117 tf->hob_lbal = (block >> 24) & 0xff;
1118 } else
1119 /* request too large even for LBA48 */
1120 goto out_of_range;
1122 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1123 goto invalid_fld;
1125 qc->nsect = n_block;
1126 tf->nsect = n_block & 0xff;
1128 tf->lbah = (block >> 16) & 0xff;
1129 tf->lbam = (block >> 8) & 0xff;
1130 tf->lbal = block & 0xff;
1132 tf->device |= ATA_LBA;
1133 } else {
1134 /* CHS */
1135 u32 sect, head, cyl, track;
1137 /* The request -may- be too large for CHS addressing. */
1138 if (!lba_28_ok(block, n_block))
1139 goto out_of_range;
1141 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1142 goto invalid_fld;
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;
1150 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1151 (u32)block, track, cyl, head, sect);
1153 /* Check whether the converted CHS can fit.
1154 Cylinder: 0-65535
1155 Head: 0-15
1156 Sector: 1-255*/
1157 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1158 goto out_of_range;
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;
1168 return 0;
1170 invalid_fld:
1171 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1172 /* "Invalid field in cbd" */
1173 return 1;
1175 out_of_range:
1176 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1177 /* "Logical Block Address out of range" */
1178 return 1;
1180 nothing_to_do:
1181 qc->scsicmd->result = SAM_STAT_GOOD;
1182 return 1;
1185 static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1187 struct scsi_cmnd *cmd = qc->scsicmd;
1188 u8 *cdb = cmd->cmnd;
1189 int need_sense = (qc->err_mask != 0);
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.
1198 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1199 ((cdb[2] & 0x20) || need_sense)) {
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.
1211 ata_gen_fixed_sense(qc);
1215 if (need_sense)
1216 ata_dump_status(qc->ap->id, &qc->result_tf);
1218 qc->scsidone(cmd);
1220 ata_qc_free(qc);
1224 * ata_scsi_translate - Translate then issue SCSI command to ATA device
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
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.
1234 * This function sets up an ata_queued_cmd structure for the
1235 * SCSI command, and sends that ata_queued_cmd to the hardware.
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.
1243 * LOCKING:
1244 * spin_lock_irqsave(host_set lock)
1247 static 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)
1251 struct ata_queued_cmd *qc;
1252 u8 *scsicmd = cmd->cmnd;
1254 VPRINTK("ENTER\n");
1256 qc = ata_scsi_qc_new(dev, cmd, done);
1257 if (!qc)
1258 goto err_mem;
1260 /* data is present; dma-map it */
1261 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1262 cmd->sc_data_direction == DMA_TO_DEVICE) {
1263 if (unlikely(cmd->request_bufflen < 1)) {
1264 ata_dev_printk(dev, KERN_WARNING,
1265 "WARNING: zero len r/w req\n");
1266 goto err_did;
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);
1275 qc->dma_dir = cmd->sc_data_direction;
1278 qc->complete_fn = ata_scsi_qc_complete;
1280 if (xlat_func(qc, scsicmd))
1281 goto early_finish;
1283 /* select device, send command to hardware */
1284 ata_qc_issue(qc);
1286 VPRINTK("EXIT\n");
1287 return;
1289 early_finish:
1290 ata_qc_free(qc);
1291 done(cmd);
1292 DPRINTK("EXIT - early finish (good or error)\n");
1293 return;
1295 err_did:
1296 ata_qc_free(qc);
1297 err_mem:
1298 cmd->result = (DID_ERROR << 16);
1299 done(cmd);
1300 DPRINTK("EXIT - internal\n");
1301 return;
1305 * ata_scsi_rbuf_get - Map response buffer.
1306 * @cmd: SCSI command containing buffer to be mapped.
1307 * @buf_out: Pointer to mapped area.
1309 * Maps buffer contained within SCSI command @cmd.
1311 * LOCKING:
1312 * spin_lock_irqsave(host_set lock)
1314 * RETURNS:
1315 * Length of response buffer.
1318 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1320 u8 *buf;
1321 unsigned int buflen;
1323 if (cmd->use_sg) {
1324 struct scatterlist *sg;
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;
1334 *buf_out = buf;
1335 return buflen;
1339 * ata_scsi_rbuf_put - Unmap response buffer.
1340 * @cmd: SCSI command containing buffer to be unmapped.
1341 * @buf: buffer to unmap
1343 * Unmaps response buffer contained within @cmd.
1345 * LOCKING:
1346 * spin_lock_irqsave(host_set lock)
1349 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1351 if (cmd->use_sg) {
1352 struct scatterlist *sg;
1354 sg = (struct scatterlist *) cmd->request_buffer;
1355 kunmap_atomic(buf - sg->offset, KM_USER0);
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
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
1368 * completed successfully (0), or not (in which case cmd->result
1369 * and sense buffer are assumed to be set).
1371 * LOCKING:
1372 * spin_lock_irqsave(host_set lock)
1375 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1376 unsigned int (*actor) (struct ata_scsi_args *args,
1377 u8 *rbuf, unsigned int buflen))
1379 u8 *rbuf;
1380 unsigned int buflen, rc;
1381 struct scsi_cmnd *cmd = args->cmd;
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);
1388 if (rc == 0)
1389 cmd->result = SAM_STAT_GOOD;
1390 args->done(cmd);
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.
1399 * Returns standard device identification data associated
1400 * with non-VPD INQUIRY command output.
1402 * LOCKING:
1403 * spin_lock_irqsave(host_set lock)
1406 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1407 unsigned int buflen)
1409 u8 hdr[] = {
1410 TYPE_DISK,
1412 0x5, /* claim SPC-3 version compatibility */
1414 95 - 4
1417 /* set scsi removeable (RMB) bit per ata bit */
1418 if (ata_id_removeable(args->id))
1419 hdr[1] |= (1 << 7);
1421 VPRINTK("ENTER\n");
1423 memcpy(rbuf, hdr, sizeof(hdr));
1425 if (buflen > 35) {
1426 memcpy(&rbuf[8], "ATA ", 8);
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);
1429 if (rbuf[32] == 0 || rbuf[32] == ' ')
1430 memcpy(&rbuf[32], "n/a ", 4);
1433 if (buflen > 63) {
1434 const u8 versions[] = {
1435 0x60, /* SAM-3 (no version claimed) */
1437 0x03,
1438 0x20, /* SBC-2 (no version claimed) */
1440 0x02,
1441 0x60 /* SPC-3 (no version claimed) */
1444 memcpy(rbuf + 59, versions, sizeof(versions));
1447 return 0;
1451 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
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.
1456 * Returns list of inquiry VPD pages available.
1458 * LOCKING:
1459 * spin_lock_irqsave(host_set lock)
1462 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1463 unsigned int buflen)
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 */
1470 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
1472 if (buflen > 6)
1473 memcpy(rbuf + 4, pages, sizeof(pages));
1475 return 0;
1479 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
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.
1484 * Returns ATA device serial number.
1486 * LOCKING:
1487 * spin_lock_irqsave(host_set lock)
1490 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1491 unsigned int buflen)
1493 const u8 hdr[] = {
1495 0x80, /* this page code */
1497 ATA_SERNO_LEN, /* page len */
1499 memcpy(rbuf, hdr, sizeof(hdr));
1501 if (buflen > (ATA_SERNO_LEN + 4 - 1))
1502 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1503 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1505 return 0;
1509 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
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.
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.
1519 * LOCKING:
1520 * spin_lock_irqsave(host_set lock)
1523 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1524 unsigned int buflen)
1526 int num;
1527 const int sat_model_serial_desc_len = 68;
1528 const int ata_model_byte_len = 40;
1530 rbuf[1] = 0x83; /* this page code */
1531 num = 4;
1533 if (buflen > (ATA_SERNO_LEN + num + 3)) {
1534 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
1535 rbuf[num + 0] = 2;
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;
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 */
1545 rbuf[num + 0] = 2;
1546 rbuf[num + 1] = 1;
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;
1558 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
1559 return 0;
1563 * ata_scsiop_noop - Command handler that simply returns success.
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.
1568 * No operation. Simply returns success to caller, to indicate
1569 * that the caller should successfully complete this SCSI command.
1571 * LOCKING:
1572 * spin_lock_irqsave(host_set lock)
1575 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1576 unsigned int buflen)
1578 VPRINTK("ENTER\n");
1579 return 0;
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
1589 * Store MODE SENSE data on an output buffer.
1591 * LOCKING:
1592 * None.
1595 static void ata_msense_push(u8 **ptr_io, const u8 *last,
1596 const u8 *buf, unsigned int buflen)
1598 u8 *ptr = *ptr_io;
1600 if ((ptr + buflen - 1) > last)
1601 return;
1603 memcpy(ptr, buf, buflen);
1605 ptr += buflen;
1607 *ptr_io = ptr;
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
1616 * Generate a caching info page, which conditionally indicates
1617 * write caching to the SCSI layer, depending on device
1618 * capabilities.
1620 * LOCKING:
1621 * None.
1624 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1625 const u8 *last)
1627 u8 page[CACHE_MPAGE_LEN];
1629 memcpy(page, def_cache_mpage, sizeof(page));
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 */
1635 ata_msense_push(ptr_io, last, page, sizeof(page));
1636 return sizeof(page);
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
1645 * Generate a generic MODE SENSE control mode page.
1647 * LOCKING:
1648 * None.
1651 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1653 ata_msense_push(ptr_io, last, def_control_mpage,
1654 sizeof(def_control_mpage));
1655 return sizeof(def_control_mpage);
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
1664 * Generate a generic MODE SENSE r/w error recovery page.
1666 * LOCKING:
1667 * None.
1670 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1673 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1674 sizeof(def_rw_recovery_mpage));
1675 return sizeof(def_rw_recovery_mpage);
1679 * We can turn this into a real blacklist if it's needed, for now just
1680 * blacklist any Maxtor BANC1G10 revision firmware
1682 static int ata_dev_supports_fua(u16 *id)
1684 unsigned char model[41], fw[9];
1686 if (!libata_fua)
1687 return 0;
1688 if (!ata_id_has_fua(id))
1689 return 0;
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));
1694 if (strcmp(model, "Maxtor"))
1695 return 1;
1696 if (strcmp(fw, "BANC1G10"))
1697 return 1;
1699 return 0; /* blacklisted */
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.
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.
1712 * LOCKING:
1713 * spin_lock_irqsave(host_set lock)
1716 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1717 unsigned int buflen)
1719 struct ata_device *dev = args->dev;
1720 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1721 const u8 sat_blk_desc[] = {
1722 0, 0, 0, 0, /* number of blocks: sat unspecified */
1724 0, 0x2, 0x0 /* block length: 512 bytes */
1726 u8 pg, spg;
1727 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
1728 u8 dpofua;
1730 VPRINTK("ENTER\n");
1732 six_byte = (scsicmd[0] == MODE_SENSE);
1733 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
1735 * LLBA bit in msense(10) ignored (compliant)
1738 page_control = scsicmd[2] >> 6;
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;
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];
1757 minlen = (alloc_len < buflen) ? alloc_len : buflen;
1759 p = rbuf + output_len;
1760 last = rbuf + minlen - 1;
1762 pg = scsicmd[2] & 0x3f;
1763 spg = scsicmd[3];
1765 * No mode subpages supported (yet) but asking for _all_
1766 * subpages may be valid
1768 if (spg && (spg != ALL_SUB_MPAGES))
1769 goto invalid_fld;
1771 switch(pg) {
1772 case RW_RECOVERY_MPAGE:
1773 output_len += ata_msense_rw_recovery(&p, last);
1774 break;
1776 case CACHE_MPAGE:
1777 output_len += ata_msense_caching(args->id, &p, last);
1778 break;
1780 case CONTROL_MPAGE: {
1781 output_len += ata_msense_ctl_mode(&p, last);
1782 break;
1785 case ALL_MPAGES:
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;
1791 default: /* invalid page code */
1792 goto invalid_fld;
1795 if (minlen < 1)
1796 return 0;
1798 dpofua = 0;
1799 if (ata_dev_supports_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 &&
1800 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
1801 dpofua = 1 << 4;
1803 if (six_byte) {
1804 output_len--;
1805 rbuf[0] = output_len;
1806 if (minlen > 2)
1807 rbuf[2] |= dpofua;
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));
1815 } else {
1816 output_len -= 2;
1817 rbuf[0] = output_len >> 8;
1818 if (minlen > 1)
1819 rbuf[1] = output_len;
1820 if (minlen > 3)
1821 rbuf[3] |= dpofua;
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));
1830 return 0;
1832 invalid_fld:
1833 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1834 /* "Invalid field in cbd" */
1835 return 1;
1837 saving_not_supp:
1838 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
1839 /* "Saving parameters not supported" */
1840 return 1;
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.
1849 * Simulate READ CAPACITY commands.
1851 * LOCKING:
1852 * spin_lock_irqsave(host_set lock)
1855 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1856 unsigned int buflen)
1858 u64 n_sectors;
1859 u32 tmp;
1861 VPRINTK("ENTER\n");
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];
1872 if (ata_id_current_chs_valid(args->id))
1873 /* CHS current translation */
1874 n_sectors = ata_id_u32(args->id, 57);
1877 n_sectors--; /* ATA TotalUserSectors - 1 */
1879 if (args->cmd->cmnd[0] == READ_CAPACITY) {
1880 if( n_sectors >= 0xffffffffULL )
1881 tmp = 0xffffffff ; /* Return max count on overflow */
1882 else
1883 tmp = n_sectors ;
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;
1891 /* sector size */
1892 tmp = ATA_SECT_SIZE;
1893 rbuf[6] = tmp >> 8;
1894 rbuf[7] = tmp;
1896 } else {
1897 /* sector count, 64-bit */
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;
1904 rbuf[6] = tmp >> (8 * 3);
1905 rbuf[7] = tmp >> (8 * 2);
1906 rbuf[8] = tmp >> (8 * 1);
1907 rbuf[9] = tmp;
1909 /* sector size */
1910 tmp = ATA_SECT_SIZE;
1911 rbuf[12] = tmp >> 8;
1912 rbuf[13] = tmp;
1915 return 0;
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.
1924 * Simulate REPORT LUNS command.
1926 * LOCKING:
1927 * spin_lock_irqsave(host_set lock)
1930 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1931 unsigned int buflen)
1933 VPRINTK("ENTER\n");
1934 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1936 return 0;
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
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 .
1952 * LOCKING:
1953 * Not required
1956 void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
1958 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
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;
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
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.
1978 * LOCKING:
1979 * spin_lock_irqsave(host_set lock)
1982 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1984 DPRINTK("ENTER\n");
1985 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
1987 done(cmd);
1990 static void atapi_sense_complete(struct ata_queued_cmd *qc)
1992 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
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
1998 ata_gen_ata_desc_sense(qc);
2001 qc->scsidone(qc->scsicmd);
2002 ata_qc_free(qc);
2005 /* is it pointless to prefer PIO for "safety reasons"? */
2006 static inline int ata_pio_use_silly(struct ata_port *ap)
2008 return (ap->flags & ATA_FLAG_PIO_DMA);
2011 static void atapi_request_sense(struct ata_queued_cmd *qc)
2013 struct ata_port *ap = qc->ap;
2014 struct scsi_cmnd *cmd = qc->scsicmd;
2016 DPRINTK("ATAPI request sense\n");
2018 /* FIXME: is this needed? */
2019 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2021 ap->ops->tf_read(ap, &qc->tf);
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;
2027 ata_qc_reinit(qc);
2029 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2030 qc->dma_dir = DMA_FROM_DEVICE;
2032 memset(&qc->cdb, 0, qc->dev->cdb_len);
2033 qc->cdb[0] = REQUEST_SENSE;
2034 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2036 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2037 qc->tf.command = ATA_CMD_PACKET;
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;
2047 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2049 qc->complete_fn = atapi_sense_complete;
2051 ata_qc_issue(qc);
2053 DPRINTK("EXIT\n");
2056 static void atapi_qc_complete(struct ata_queued_cmd *qc)
2058 struct scsi_cmnd *cmd = qc->scsicmd;
2059 unsigned int err_mask = qc->err_mask;
2061 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
2063 if (unlikely(err_mask & AC_ERR_DEV)) {
2064 cmd->result = SAM_STAT_CHECK_CONDITION;
2065 atapi_request_sense(qc);
2066 return;
2067 } else if (unlikely(err_mask)) {
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
2073 ata_gen_ata_desc_sense(qc);
2074 } else {
2075 u8 *scsicmd = cmd->cmnd;
2077 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
2078 u8 *buf = NULL;
2079 unsigned int buflen;
2081 buflen = ata_scsi_rbuf_get(cmd, &buf);
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.
2091 if (buf[2] == 0) {
2092 buf[2] = 0x5;
2093 buf[3] = 0x32;
2096 ata_scsi_rbuf_put(cmd, buf);
2099 cmd->result = SAM_STAT_GOOD;
2102 qc->scsidone(cmd);
2103 ata_qc_free(qc);
2106 * atapi_xlat - Initialize PACKET taskfile
2107 * @qc: command structure to be initialized
2108 * @scsicmd: SCSI CDB associated with this PACKET command
2110 * LOCKING:
2111 * spin_lock_irqsave(host_set lock)
2113 * RETURNS:
2114 * Zero on success, non-zero on failure.
2117 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
2119 struct scsi_cmnd *cmd = qc->scsicmd;
2120 struct ata_device *dev = qc->dev;
2121 int using_pio = (dev->flags & ATA_DFLAG_PIO);
2122 int nodata = (cmd->sc_data_direction == DMA_NONE);
2124 if (!using_pio)
2125 /* Check whether ATAPI DMA is safe */
2126 if (ata_check_atapi_dma(qc))
2127 using_pio = 1;
2129 memcpy(&qc->cdb, scsicmd, dev->cdb_len);
2131 qc->complete_fn = atapi_qc_complete;
2133 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2134 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
2135 qc->tf.flags |= ATA_TFLAG_WRITE;
2136 DPRINTK("direction: write\n");
2139 qc->tf.command = ATA_CMD_PACKET;
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;
2151 /* DMA data xfer */
2152 else {
2153 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2154 qc->tf.feature |= ATAPI_PKT_DMA;
2156 if (atapi_dmadir && (cmd->sc_data_direction != DMA_TO_DEVICE))
2157 /* some SATA bridges need us to indicate data xfer direction */
2158 qc->tf.feature |= ATAPI_DMADIR;
2161 qc->nbytes = cmd->bufflen;
2163 return 0;
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
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.
2176 * LOCKING:
2177 * spin_lock_irqsave(host_set lock)
2179 * RETURNS:
2180 * Associated ATA device, or %NULL if not found.
2183 static struct ata_device *
2184 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
2186 struct ata_device *dev;
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;
2194 if (unlikely((scsidev->channel != 0) ||
2195 (scsidev->lun != 0)))
2196 return NULL;
2198 if (unlikely(!ata_dev_enabled(dev)))
2199 return NULL;
2201 if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
2202 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2203 ata_dev_printk(dev, KERN_WARNING,
2204 "WARNING: ATAPI is %s, device ignored.\n",
2205 atapi_enabled ? "not supported with this driver" : "disabled");
2206 return NULL;
2210 return dev;
2214 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2215 * @byte1: Byte 1 from pass-thru CDB.
2217 * RETURNS:
2218 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2220 static u8
2221 ata_scsi_map_proto(u8 byte1)
2223 switch((byte1 & 0x1e) >> 1) {
2224 case 3: /* Non-data */
2225 return ATA_PROT_NODATA;
2227 case 6: /* DMA */
2228 return ATA_PROT_DMA;
2230 case 4: /* PIO Data-in */
2231 case 5: /* PIO Data-out */
2232 return ATA_PROT_PIO;
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;
2248 return ATA_PROT_UNKNOWN;
2252 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2253 * @qc: command structure to be initialized
2254 * @scsicmd: SCSI command to convert
2256 * Handles either 12 or 16-byte versions of the CDB.
2258 * RETURNS:
2259 * Zero on success, non-zero on failure.
2261 static unsigned int
2262 ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
2264 struct ata_taskfile *tf = &(qc->tf);
2265 struct scsi_cmnd *cmd = qc->scsicmd;
2267 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
2268 goto invalid_fld;
2270 if (scsicmd[1] & 0xe0)
2271 /* PIO multi not supported yet */
2272 goto invalid_fld;
2275 * 12 and 16 byte CDBs use different offsets to
2276 * provide the various register values.
2278 if (scsicmd[0] == ATA_16) {
2280 * 16-byte CDB - may contain extended commands.
2282 * If that is the case, copy the upper byte register values.
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;
2295 * Always copy low byte, device and command registers.
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 {
2306 * 12-byte CDB - incapable of extended commands.
2308 tf->flags &= ~ATA_TFLAG_LBA48;
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];
2319 * If slave is possible, enforce correct master/slave bit
2321 if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
2322 tf->device = qc->dev->devno ?
2323 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
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).
2332 if ((tf->command == ATA_CMD_SET_FEATURES)
2333 && (tf->feature == SETFEATURES_XFER))
2334 goto invalid_fld;
2337 * Set flags so that all registers will be written,
2338 * and pass on write indication (used for PIO/DMA
2339 * setup.)
2341 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2343 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2344 tf->flags |= ATA_TFLAG_WRITE;
2347 * Set transfer length.
2349 * TODO: find out if we need to do more here to
2350 * cover scatter/gather case.
2352 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
2354 /* request result TF */
2355 qc->flags |= ATA_QCFLAG_RESULT_TF;
2357 return 0;
2359 invalid_fld:
2360 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00);
2361 /* "Invalid field in cdb" */
2362 return 1;
2366 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2367 * @dev: ATA device
2368 * @cmd: SCSI command opcode to consider
2370 * Look up the SCSI command given, and determine whether the
2371 * SCSI command is to be translated or simulated.
2373 * RETURNS:
2374 * Pointer to translation function if possible, %NULL if not.
2377 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2379 switch (cmd) {
2380 case READ_6:
2381 case READ_10:
2382 case READ_16:
2384 case WRITE_6:
2385 case WRITE_10:
2386 case WRITE_16:
2387 return ata_scsi_rw_xlat;
2389 case SYNCHRONIZE_CACHE:
2390 if (ata_try_flush_cache(dev))
2391 return ata_scsi_flush_xlat;
2392 break;
2394 case VERIFY:
2395 case VERIFY_16:
2396 return ata_scsi_verify_xlat;
2398 case ATA_12:
2399 case ATA_16:
2400 return ata_scsi_pass_thru;
2402 case START_STOP:
2403 return ata_scsi_start_stop_xlat;
2406 return NULL;
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
2414 * Prints the contents of a SCSI command via printk().
2417 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2418 struct scsi_cmnd *cmd)
2420 #ifdef ATA_DEBUG
2421 struct scsi_device *scsidev = cmd->device;
2422 u8 *scsicmd = cmd->cmnd;
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
2433 static inline void __ata_scsi_queuecmd(struct scsi_cmnd *cmd,
2434 void (*done)(struct scsi_cmnd *),
2435 struct ata_device *dev)
2437 if (dev->class == ATA_DEV_ATA) {
2438 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2439 cmd->cmnd[0]);
2441 if (xlat_func)
2442 ata_scsi_translate(dev, cmd, done, xlat_func);
2443 else
2444 ata_scsi_simulate(dev, cmd, done);
2445 } else
2446 ata_scsi_translate(dev, cmd, done, atapi_xlat);
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
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.
2461 * LOCKING:
2462 * Releases scsi-layer-held lock, and obtains host_set lock.
2464 * RETURNS:
2465 * Zero.
2468 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2470 struct ata_port *ap;
2471 struct ata_device *dev;
2472 struct scsi_device *scsidev = cmd->device;
2473 struct Scsi_Host *shost = scsidev->host;
2475 ap = ata_shost_to_port(shost);
2477 spin_unlock(shost->host_lock);
2478 spin_lock(&ap->host_set->lock);
2480 ata_scsi_dump_cdb(ap, cmd);
2482 dev = ata_scsi_find_dev(ap, scsidev);
2483 if (likely(dev))
2484 __ata_scsi_queuecmd(cmd, done, dev);
2485 else {
2486 cmd->result = (DID_BAD_TARGET << 16);
2487 done(cmd);
2490 spin_unlock(&ap->host_set->lock);
2491 spin_lock(shost->host_lock);
2492 return 0;
2496 * ata_scsi_simulate - simulate SCSI command on ATA device
2497 * @dev: the target device
2498 * @cmd: SCSI command being sent to device.
2499 * @done: SCSI command completion function.
2501 * Interprets and directly executes a select list of SCSI commands
2502 * that can be handled internally.
2504 * LOCKING:
2505 * spin_lock_irqsave(host_set lock)
2508 void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
2509 void (*done)(struct scsi_cmnd *))
2511 struct ata_scsi_args args;
2512 const u8 *scsicmd = cmd->cmnd;
2514 args.dev = dev;
2515 args.id = dev->id;
2516 args.cmd = cmd;
2517 args.done = done;
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;
2531 case INQUIRY:
2532 if (scsicmd[1] & 2) /* is CmdDt set? */
2533 ata_scsi_invalid_field(cmd, done);
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
2543 ata_scsi_invalid_field(cmd, done);
2544 break;
2546 case MODE_SENSE:
2547 case MODE_SENSE_10:
2548 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2549 break;
2551 case MODE_SELECT: /* unconditionally return */
2552 case MODE_SELECT_10: /* bad-field-in-cdb */
2553 ata_scsi_invalid_field(cmd, done);
2554 break;
2556 case READ_CAPACITY:
2557 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2558 break;
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
2564 ata_scsi_invalid_field(cmd, done);
2565 break;
2567 case REPORT_LUNS:
2568 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2569 break;
2571 /* mandatory commands we haven't implemented yet */
2572 case REQUEST_SENSE:
2574 /* all other commands */
2575 default:
2576 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2577 /* "Invalid command operation code" */
2578 done(cmd);
2579 break;
2583 void ata_scsi_scan_host(struct ata_port *ap)
2585 struct ata_device *dev;
2586 unsigned int i;
2588 if (ap->flags & ATA_FLAG_DISABLED)
2589 return;
2591 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2592 dev = &ap->device[i];
2594 if (ata_dev_enabled(dev))
2595 scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);