1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * libata-scsi.c - helper library for ATA
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
8 * libata documentation is available via 'make {ps|pdf}docs',
9 * as Documentation/driver-api/libata.rst
11 * Hardware documentation available from
12 * - http://www.t10.org/
13 * - http://www.t13.org/
16 #include <linux/compat.h>
17 #include <linux/slab.h>
18 #include <linux/kernel.h>
19 #include <linux/blkdev.h>
20 #include <linux/spinlock.h>
21 #include <linux/export.h>
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_host.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_eh.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_tcq.h>
28 #include <scsi/scsi_transport.h>
29 #include <linux/libata.h>
30 #include <linux/hdreg.h>
31 #include <linux/uaccess.h>
32 #include <linux/suspend.h>
33 #include <asm/unaligned.h>
34 #include <linux/ioprio.h>
38 #include "libata-transport.h"
40 #define ATA_SCSI_RBUF_SIZE 576
42 static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
43 static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
45 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
47 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
48 const struct scsi_device *scsidev);
50 #define RW_RECOVERY_MPAGE 0x1
51 #define RW_RECOVERY_MPAGE_LEN 12
52 #define CACHE_MPAGE 0x8
53 #define CACHE_MPAGE_LEN 20
54 #define CONTROL_MPAGE 0xa
55 #define CONTROL_MPAGE_LEN 12
56 #define ALL_MPAGES 0x3f
57 #define ALL_SUB_MPAGES 0xff
60 static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
62 RW_RECOVERY_MPAGE_LEN - 2,
64 0, /* read retry count */
66 0, /* write retry count */
70 static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
73 0, /* contains WCE, needs to be 0 for logic */
74 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, /* contains DRA, needs to be 0 for logic */
79 static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
81 CONTROL_MPAGE_LEN - 2,
82 2, /* DSENSE=0, GLTSD=1 */
83 0, /* [QAM+QERR may be 1, see 05-359r1] */
84 0, 0, 0, 0, 0xff, 0xff,
85 0, 30 /* extended self test time, see 05-359r1 */
88 static ssize_t ata_scsi_park_show(struct device *device,
89 struct device_attribute *attr, char *buf)
91 struct scsi_device *sdev = to_scsi_device(device);
93 struct ata_link *link;
94 struct ata_device *dev;
99 ap = ata_shost_to_port(sdev->host);
101 spin_lock_irq(ap->lock);
102 dev = ata_scsi_find_dev(ap, sdev);
107 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
114 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
115 link->eh_context.unloaded_mask & (1 << dev->devno) &&
116 time_after(dev->unpark_deadline, now))
117 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
122 spin_unlock_irq(ap->lock);
124 return rc ? rc : sysfs_emit(buf, "%u\n", msecs);
127 static ssize_t ata_scsi_park_store(struct device *device,
128 struct device_attribute *attr,
129 const char *buf, size_t len)
131 struct scsi_device *sdev = to_scsi_device(device);
133 struct ata_device *dev;
138 rc = kstrtol(buf, 10, &input);
143 if (input > ATA_TMOUT_MAX_PARK) {
145 input = ATA_TMOUT_MAX_PARK;
148 ap = ata_shost_to_port(sdev->host);
150 spin_lock_irqsave(ap->lock, flags);
151 dev = ata_scsi_find_dev(ap, sdev);
152 if (unlikely(!dev)) {
156 if (dev->class != ATA_DEV_ATA &&
157 dev->class != ATA_DEV_ZAC) {
163 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
168 dev->unpark_deadline = ata_deadline(jiffies, input);
169 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
170 ata_port_schedule_eh(ap);
171 complete(&ap->park_req_pending);
175 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
178 dev->flags |= ATA_DFLAG_NO_UNLOAD;
183 spin_unlock_irqrestore(ap->lock, flags);
185 return rc ? rc : len;
187 DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
188 ata_scsi_park_show, ata_scsi_park_store);
189 EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
191 bool ata_scsi_sense_is_valid(u8 sk, u8 asc, u8 ascq)
194 * If sk == NO_SENSE, and asc + ascq == NO ADDITIONAL SENSE INFORMATION,
195 * then there is no sense data to add.
197 if (sk == 0 && asc == 0 && ascq == 0)
200 /* If sk > COMPLETED, sense data is bogus. */
207 void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
208 u8 sk, u8 asc, u8 ascq)
210 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
215 scsi_build_sense(cmd, d_sense, sk, asc, ascq);
218 void ata_scsi_set_sense_information(struct ata_device *dev,
219 struct scsi_cmnd *cmd,
220 const struct ata_taskfile *tf)
227 information = ata_tf_read_block(tf, dev);
228 if (information == U64_MAX)
231 scsi_set_sense_information(cmd->sense_buffer,
232 SCSI_SENSE_BUFFERSIZE, information);
235 static void ata_scsi_set_invalid_field(struct ata_device *dev,
236 struct scsi_cmnd *cmd, u16 field, u8 bit)
238 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
239 /* "Invalid field in CDB" */
240 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
244 static void ata_scsi_set_invalid_parameter(struct ata_device *dev,
245 struct scsi_cmnd *cmd, u16 field)
247 /* "Invalid field in parameter list" */
248 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x26, 0x0);
249 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
253 static struct attribute *ata_common_sdev_attrs[] = {
254 &dev_attr_unload_heads.attr,
258 static const struct attribute_group ata_common_sdev_attr_group = {
259 .attrs = ata_common_sdev_attrs
262 const struct attribute_group *ata_common_sdev_groups[] = {
263 &ata_common_sdev_attr_group,
266 EXPORT_SYMBOL_GPL(ata_common_sdev_groups);
269 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
270 * @sdev: SCSI device for which BIOS geometry is to be determined
271 * @bdev: block device associated with @sdev
272 * @capacity: capacity of SCSI device
273 * @geom: location to which geometry will be output
275 * Generic bios head/sector/cylinder calculator
276 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
277 * mapping. Some situations may arise where the disk is not
278 * bootable if this is not used.
281 * Defined by the SCSI layer. We don't really care.
286 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
287 sector_t capacity, int geom[])
291 sector_div(capacity, 255*63);
296 EXPORT_SYMBOL_GPL(ata_std_bios_param);
299 * ata_scsi_unlock_native_capacity - unlock native capacity
300 * @sdev: SCSI device to adjust device capacity for
302 * This function is called if a partition on @sdev extends beyond
303 * the end of the device. It requests EH to unlock HPA.
306 * Defined by the SCSI layer. Might sleep.
308 void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
310 struct ata_port *ap = ata_shost_to_port(sdev->host);
311 struct ata_device *dev;
314 spin_lock_irqsave(ap->lock, flags);
316 dev = ata_scsi_find_dev(ap, sdev);
317 if (dev && dev->n_sectors < dev->n_native_sectors) {
318 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
319 dev->link->eh_info.action |= ATA_EH_RESET;
320 ata_port_schedule_eh(ap);
323 spin_unlock_irqrestore(ap->lock, flags);
324 ata_port_wait_eh(ap);
326 EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
329 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
331 * @sdev: SCSI device to get identify data for
332 * @arg: User buffer area for identify data
335 * Defined by the SCSI layer. We don't really care.
338 * Zero on success, negative errno on error.
340 static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
343 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
344 u16 __user *dst = arg;
350 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
353 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
354 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
357 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
358 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
361 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
362 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
369 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
370 * @scsidev: Device to which we are issuing command
371 * @arg: User provided data for issuing command
374 * Defined by the SCSI layer. We don't really care.
377 * Zero on success, negative errno on error.
379 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
382 u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
383 u8 scsi_cmd[MAX_COMMAND_SIZE];
384 u8 args[4], *argbuf = NULL;
386 struct scsi_sense_hdr sshdr;
387 const struct scsi_exec_args exec_args = {
390 .sense_len = sizeof(sensebuf),
397 if (copy_from_user(args, arg, sizeof(args)))
400 memset(sensebuf, 0, sizeof(sensebuf));
401 memset(scsi_cmd, 0, sizeof(scsi_cmd));
404 argsize = ATA_SECT_SIZE * args[3];
405 argbuf = kmalloc(argsize, GFP_KERNEL);
406 if (argbuf == NULL) {
411 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
412 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
413 block count in sector count field */
415 scsi_cmd[1] = (3 << 1); /* Non-data */
416 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
419 scsi_cmd[0] = ATA_16;
421 scsi_cmd[4] = args[2];
422 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
423 scsi_cmd[6] = args[3];
424 scsi_cmd[8] = args[1];
425 scsi_cmd[10] = ATA_SMART_LBAM_PASS;
426 scsi_cmd[12] = ATA_SMART_LBAH_PASS;
428 scsi_cmd[6] = args[1];
430 scsi_cmd[14] = args[0];
432 /* Good values for timeout and retries? Values below
433 from scsi_ioctl_send_command() for default case... */
434 cmd_result = scsi_execute_cmd(scsidev, scsi_cmd, REQ_OP_DRV_IN, argbuf,
435 argsize, 10 * HZ, 5, &exec_args);
436 if (cmd_result < 0) {
440 if (scsi_sense_valid(&sshdr)) {/* sense data available */
441 u8 *desc = sensebuf + 8;
443 /* If we set cc then ATA pass-through will cause a
444 * check condition even if no error. Filter that. */
445 if (scsi_status_is_check_condition(cmd_result)) {
446 if (sshdr.sense_key == RECOVERED_ERROR &&
447 sshdr.asc == 0 && sshdr.ascq == 0x1d)
448 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
451 /* Send userspace a few ATA registers (same as drivers/ide) */
452 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
453 desc[0] == 0x09) { /* code is "ATA Descriptor" */
454 args[0] = desc[13]; /* status */
455 args[1] = desc[3]; /* error */
456 args[2] = desc[5]; /* sector count (0:7) */
457 if (copy_to_user(arg, args, sizeof(args)))
469 && copy_to_user(arg + sizeof(args), argbuf, argsize))
477 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
478 * @scsidev: Device to which we are issuing command
479 * @arg: User provided data for issuing command
482 * Defined by the SCSI layer. We don't really care.
485 * Zero on success, negative errno on error.
487 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
490 u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
491 u8 scsi_cmd[MAX_COMMAND_SIZE];
493 struct scsi_sense_hdr sshdr;
495 const struct scsi_exec_args exec_args = {
498 .sense_len = sizeof(sensebuf),
504 if (copy_from_user(args, arg, sizeof(args)))
507 memset(sensebuf, 0, sizeof(sensebuf));
508 memset(scsi_cmd, 0, sizeof(scsi_cmd));
509 scsi_cmd[0] = ATA_16;
510 scsi_cmd[1] = (3 << 1); /* Non-data */
511 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
512 scsi_cmd[4] = args[1];
513 scsi_cmd[6] = args[2];
514 scsi_cmd[8] = args[3];
515 scsi_cmd[10] = args[4];
516 scsi_cmd[12] = args[5];
517 scsi_cmd[13] = args[6] & 0x4f;
518 scsi_cmd[14] = args[0];
520 /* Good values for timeout and retries? Values below
521 from scsi_ioctl_send_command() for default case... */
522 cmd_result = scsi_execute_cmd(scsidev, scsi_cmd, REQ_OP_DRV_IN, NULL,
523 0, 10 * HZ, 5, &exec_args);
524 if (cmd_result < 0) {
528 if (scsi_sense_valid(&sshdr)) {/* sense data available */
529 u8 *desc = sensebuf + 8;
531 /* If we set cc then ATA pass-through will cause a
532 * check condition even if no error. Filter that. */
533 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
534 if (sshdr.sense_key == RECOVERED_ERROR &&
535 sshdr.asc == 0 && sshdr.ascq == 0x1d)
536 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
539 /* Send userspace ATA registers */
540 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
541 desc[0] == 0x09) {/* code is "ATA Descriptor" */
542 args[0] = desc[13]; /* status */
543 args[1] = desc[3]; /* error */
544 args[2] = desc[5]; /* sector count (0:7) */
545 args[3] = desc[7]; /* lbal */
546 args[4] = desc[9]; /* lbam */
547 args[5] = desc[11]; /* lbah */
548 args[6] = desc[12]; /* select */
549 if (copy_to_user(arg, args, sizeof(args)))
563 static bool ata_ioc32(struct ata_port *ap)
565 if (ap->flags & ATA_FLAG_PIO_DMA)
567 if (ap->pflags & ATA_PFLAG_PIO32)
573 * This handles both native and compat commands, so anything added
574 * here must have a compatible argument, or check in_compat_syscall()
576 int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
577 unsigned int cmd, void __user *arg)
585 spin_lock_irqsave(ap->lock, flags);
587 spin_unlock_irqrestore(ap->lock, flags);
589 if (in_compat_syscall())
590 return put_user(val, (compat_ulong_t __user *)arg);
592 return put_user(val, (unsigned long __user *)arg);
595 val = (unsigned long) arg;
597 spin_lock_irqsave(ap->lock, flags);
598 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
600 ap->pflags |= ATA_PFLAG_PIO32;
602 ap->pflags &= ~ATA_PFLAG_PIO32;
604 if (val != ata_ioc32(ap))
607 spin_unlock_irqrestore(ap->lock, flags);
610 case HDIO_GET_IDENTITY:
611 return ata_get_identity(ap, scsidev, arg);
614 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
616 return ata_cmd_ioctl(scsidev, arg);
618 case HDIO_DRIVE_TASK:
619 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
621 return ata_task_ioctl(scsidev, arg);
630 EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
632 int ata_scsi_ioctl(struct scsi_device *scsidev, unsigned int cmd,
635 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
638 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
641 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
642 * @dev: ATA device to which the new command is attached
643 * @cmd: SCSI command that originated this ATA command
645 * Obtain a reference to an unused ata_queued_cmd structure,
646 * which is the basic libata structure representing a single
647 * ATA command sent to the hardware.
649 * If a command was available, fill in the SCSI-specific
650 * portions of the structure with information on the
654 * spin_lock_irqsave(host lock)
657 * Command allocated, or %NULL if none available.
659 static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
660 struct scsi_cmnd *cmd)
662 struct ata_port *ap = dev->link->ap;
663 struct ata_queued_cmd *qc;
666 if (unlikely(ata_port_is_frozen(ap)))
669 if (ap->flags & ATA_FLAG_SAS_HOST) {
671 * SAS hosts may queue > ATA_MAX_QUEUE commands so use
672 * unique per-device budget token as a tag.
674 if (WARN_ON_ONCE(cmd->budget_token >= ATA_MAX_QUEUE))
676 tag = cmd->budget_token;
678 tag = scsi_cmd_to_rq(cmd)->tag;
681 qc = __ata_qc_from_tag(ap, tag);
682 qc->tag = qc->hw_tag = tag;
689 qc->scsidone = scsi_done;
691 qc->sg = scsi_sglist(cmd);
692 qc->n_elem = scsi_sg_count(cmd);
694 if (scsi_cmd_to_rq(cmd)->rq_flags & RQF_QUIET)
695 qc->flags |= ATA_QCFLAG_QUIET;
700 set_host_byte(cmd, DID_OK);
701 set_status_byte(cmd, SAM_STAT_TASK_SET_FULL);
706 static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
708 struct scsi_cmnd *scmd = qc->scsicmd;
710 qc->extrabytes = scmd->extra_len;
711 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
715 * ata_dump_status - user friendly display of error info
716 * @ap: the port in question
717 * @tf: ptr to filled out taskfile
719 * Decode and dump the ATA error/status registers for the user so
720 * that they have some idea what really happened at the non
721 * make-believe layer.
724 * inherited from caller
726 static void ata_dump_status(struct ata_port *ap, struct ata_taskfile *tf)
728 u8 stat = tf->status, err = tf->error;
730 if (stat & ATA_BUSY) {
731 ata_port_warn(ap, "status=0x%02x {Busy} ", stat);
733 ata_port_warn(ap, "status=0x%02x { %s%s%s%s%s%s%s} ", stat,
734 stat & ATA_DRDY ? "DriveReady " : "",
735 stat & ATA_DF ? "DeviceFault " : "",
736 stat & ATA_DSC ? "SeekComplete " : "",
737 stat & ATA_DRQ ? "DataRequest " : "",
738 stat & ATA_CORR ? "CorrectedError " : "",
739 stat & ATA_SENSE ? "Sense " : "",
740 stat & ATA_ERR ? "Error " : "");
742 ata_port_warn(ap, "error=0x%02x {%s%s%s%s%s%s", err,
744 "DriveStatusError " : "",
747 "BadCRC " : "Sector ") : "",
748 err & ATA_UNC ? "UncorrectableError " : "",
749 err & ATA_IDNF ? "SectorIdNotFound " : "",
750 err & ATA_TRK0NF ? "TrackZeroNotFound " : "",
751 err & ATA_AMNF ? "AddrMarkNotFound " : "");
756 * ata_to_sense_error - convert ATA error to SCSI error
757 * @id: ATA device number
758 * @drv_stat: value contained in ATA status register
759 * @drv_err: value contained in ATA error register
760 * @sk: the sense key we'll fill out
761 * @asc: the additional sense code we'll fill out
762 * @ascq: the additional sense code qualifier we'll fill out
763 * @verbose: be verbose
765 * Converts an ATA error into a SCSI error. Fill out pointers to
766 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
767 * format sense blocks.
770 * spin_lock_irqsave(host lock)
772 static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
773 u8 *asc, u8 *ascq, int verbose)
777 /* Based on the 3ware driver translation table */
778 static const unsigned char sense_table[][4] = {
780 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
781 // Device busy Aborted command
783 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
784 // Device busy Aborted command
786 {0x61, HARDWARE_ERROR, 0x00, 0x00},
787 // Device fault Hardware error
788 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
789 {0x84, ABORTED_COMMAND, 0x47, 0x00},
790 // Data CRC error SCSI parity error
791 /* MC|ID|ABRT|TRK0|MARK */
792 {0x37, NOT_READY, 0x04, 0x00},
793 // Unit offline Not ready
795 {0x09, NOT_READY, 0x04, 0x00},
796 // Unrecovered disk error Not ready
797 /* Bad address mark */
798 {0x01, MEDIUM_ERROR, 0x13, 0x00},
799 // Address mark not found for data field
800 /* TRK0 - Track 0 not found */
801 {0x02, HARDWARE_ERROR, 0x00, 0x00},
803 /* Abort: 0x04 is not translated here, see below */
804 /* Media change request */
805 {0x08, NOT_READY, 0x04, 0x00},
806 // FIXME: faking offline
807 /* SRV/IDNF - ID not found */
808 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
809 // Logical address out of range
810 /* MC - Media Changed */
811 {0x20, UNIT_ATTENTION, 0x28, 0x00},
812 // Not ready to ready change, medium may have changed
813 /* ECC - Uncorrectable ECC error */
814 {0x40, MEDIUM_ERROR, 0x11, 0x04},
815 // Unrecovered read error
816 /* BBD - block marked bad */
817 {0x80, MEDIUM_ERROR, 0x11, 0x04},
818 // Block marked bad Medium error, unrecovered read error
819 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
821 static const unsigned char stat_table[][4] = {
822 /* Must be first because BUSY means no other bits valid */
823 {0x80, ABORTED_COMMAND, 0x47, 0x00},
824 // Busy, fake parity for now
825 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
826 // Device ready, unaligned write command
827 {0x20, HARDWARE_ERROR, 0x44, 0x00},
828 // Device fault, internal target failure
829 {0x08, ABORTED_COMMAND, 0x47, 0x00},
830 // Timed out in xfer, fake parity for now
831 {0x04, RECOVERED_ERROR, 0x11, 0x00},
832 // Recovered ECC error Medium error, recovered
833 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
837 * Is this an error we can process/parse
839 if (drv_stat & ATA_BUSY) {
840 drv_err = 0; /* Ignore the err bits, they're invalid */
844 /* Look for drv_err */
845 for (i = 0; sense_table[i][0] != 0xFF; i++) {
846 /* Look for best matches first */
847 if ((sense_table[i][0] & drv_err) ==
849 *sk = sense_table[i][1];
850 *asc = sense_table[i][2];
851 *ascq = sense_table[i][3];
858 * Fall back to interpreting status bits. Note that if the drv_err
859 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
860 * is not descriptive enough.
862 for (i = 0; stat_table[i][0] != 0xFF; i++) {
863 if (stat_table[i][0] & drv_stat) {
864 *sk = stat_table[i][1];
865 *asc = stat_table[i][2];
866 *ascq = stat_table[i][3];
872 * We need a sensible error return here, which is tricky, and one
873 * that won't cause people to do things like return a disk wrongly.
875 *sk = ABORTED_COMMAND;
881 pr_err("ata%u: translated ATA stat/err 0x%02x/%02x to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
882 id, drv_stat, drv_err, *sk, *asc, *ascq);
887 * ata_gen_passthru_sense - Generate check condition sense block.
888 * @qc: Command that completed.
890 * This function is specific to the ATA descriptor format sense
891 * block specified for the ATA pass through commands. Regardless
892 * of whether the command errored or not, return a sense
893 * block. Copy all controller registers into the sense
894 * block. If there was no error, we get the request from an ATA
895 * passthrough command, so we use the following sense data:
896 * sk = RECOVERED ERROR
897 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
903 static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
905 struct scsi_cmnd *cmd = qc->scsicmd;
906 struct ata_taskfile *tf = &qc->result_tf;
907 unsigned char *sb = cmd->sense_buffer;
908 unsigned char *desc = sb + 8;
909 int verbose = qc->ap->ops->error_handler == NULL;
910 u8 sense_key, asc, ascq;
912 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
915 * Use ata_to_sense_error() to map status register bits
916 * onto sense key, asc & ascq.
919 tf->status & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
920 ata_to_sense_error(qc->ap->print_id, tf->status, tf->error,
921 &sense_key, &asc, &ascq, verbose);
922 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
925 * ATA PASS-THROUGH INFORMATION AVAILABLE
926 * Always in descriptor format sense.
928 scsi_build_sense(cmd, 1, RECOVERED_ERROR, 0, 0x1D);
931 if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
934 /* descriptor format */
936 desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
938 if (SCSI_SENSE_BUFFERSIZE < len + 14)
946 * Copy registers into sense buffer.
954 desc[12] = tf->device;
955 desc[13] = tf->status;
958 * Fill in Extend bit, and the high order bytes
961 if (tf->flags & ATA_TFLAG_LBA48) {
963 desc[4] = tf->hob_nsect;
964 desc[6] = tf->hob_lbal;
965 desc[8] = tf->hob_lbam;
966 desc[10] = tf->hob_lbah;
969 /* Fixed sense format */
971 desc[1] = tf->status;
972 desc[2] = tf->device;
975 if (tf->flags & ATA_TFLAG_LBA48) {
979 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
989 * ata_gen_ata_sense - generate a SCSI fixed sense block
990 * @qc: Command that we are erroring out
992 * Generate sense block for a failed ATA command @qc. Descriptor
993 * format is used to accommodate LBA48 block address.
998 static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
1000 struct ata_device *dev = qc->dev;
1001 struct scsi_cmnd *cmd = qc->scsicmd;
1002 struct ata_taskfile *tf = &qc->result_tf;
1003 unsigned char *sb = cmd->sense_buffer;
1004 int verbose = qc->ap->ops->error_handler == NULL;
1006 u8 sense_key, asc, ascq;
1008 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1010 if (ata_dev_disabled(dev)) {
1011 /* Device disabled after error recovery */
1012 /* LOGICAL UNIT NOT READY, HARD RESET REQUIRED */
1013 ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
1016 /* Use ata_to_sense_error() to map status register bits
1017 * onto sense key, asc & ascq.
1020 tf->status & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
1021 ata_to_sense_error(qc->ap->print_id, tf->status, tf->error,
1022 &sense_key, &asc, &ascq, verbose);
1023 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
1025 /* Could not decode error */
1026 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
1027 tf->status, qc->err_mask);
1028 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
1032 block = ata_tf_read_block(&qc->result_tf, dev);
1033 if (block == U64_MAX)
1036 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
1039 void ata_scsi_sdev_config(struct scsi_device *sdev)
1041 sdev->use_10_for_rw = 1;
1042 sdev->use_10_for_ms = 1;
1043 sdev->no_write_same = 1;
1045 /* Schedule policy is determined by ->qc_defer() callback and
1046 * it needs to see every deferred qc. Set dev_blocked to 1 to
1047 * prevent SCSI midlayer from automatically deferring
1050 sdev->max_device_blocked = 1;
1054 * ata_scsi_dma_need_drain - Check whether data transfer may overflow
1055 * @rq: request to be checked
1057 * ATAPI commands which transfer variable length data to host
1058 * might overflow due to application error or hardware bug. This
1059 * function checks whether overflow should be drained and ignored
1066 * 1 if ; otherwise, 0.
1068 bool ata_scsi_dma_need_drain(struct request *rq)
1070 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
1072 return atapi_cmd_type(scmd->cmnd[0]) == ATAPI_MISC;
1074 EXPORT_SYMBOL_GPL(ata_scsi_dma_need_drain);
1076 int ata_scsi_dev_config(struct scsi_device *sdev, struct ata_device *dev)
1078 struct request_queue *q = sdev->request_queue;
1081 if (!ata_id_has_unload(dev->id))
1082 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1084 /* configure max sectors */
1085 dev->max_sectors = min(dev->max_sectors, sdev->host->max_sectors);
1086 blk_queue_max_hw_sectors(q, dev->max_sectors);
1088 if (dev->class == ATA_DEV_ATAPI) {
1089 sdev->sector_size = ATA_SECT_SIZE;
1091 /* set DMA padding */
1092 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
1094 /* make room for appending the drain */
1095 blk_queue_max_segments(q, queue_max_segments(q) - 1);
1097 sdev->dma_drain_len = ATAPI_MAX_DRAIN;
1098 sdev->dma_drain_buf = kmalloc(sdev->dma_drain_len, GFP_NOIO);
1099 if (!sdev->dma_drain_buf) {
1100 ata_dev_err(dev, "drain buffer allocation failed\n");
1104 sdev->sector_size = ata_id_logical_sector_size(dev->id);
1105 sdev->manage_start_stop = 1;
1109 * ata_pio_sectors() expects buffer for each sector to not cross
1110 * page boundary. Enforce it by requiring buffers to be sector
1111 * aligned, which works iff sector_size is not larger than
1112 * PAGE_SIZE. ATAPI devices also need the alignment as
1113 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1115 if (sdev->sector_size > PAGE_SIZE)
1117 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1120 blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
1122 if (dev->flags & ATA_DFLAG_AN)
1123 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1125 if (dev->flags & ATA_DFLAG_NCQ)
1126 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1127 depth = min(ATA_MAX_QUEUE, depth);
1128 scsi_change_queue_depth(sdev, depth);
1130 if (dev->flags & ATA_DFLAG_TRUSTED)
1131 sdev->security_supported = 1;
1138 * ata_scsi_slave_config - Set SCSI device attributes
1139 * @sdev: SCSI device to examine
1141 * This is called before we actually start reading
1142 * and writing to the device, to configure certain
1143 * SCSI mid-layer behaviors.
1146 * Defined by SCSI layer. We don't really care.
1149 int ata_scsi_slave_config(struct scsi_device *sdev)
1151 struct ata_port *ap = ata_shost_to_port(sdev->host);
1152 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
1155 ata_scsi_sdev_config(sdev);
1158 rc = ata_scsi_dev_config(sdev, dev);
1162 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
1165 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1166 * @sdev: SCSI device to be destroyed
1168 * @sdev is about to be destroyed for hot/warm unplugging. If
1169 * this unplugging was initiated by libata as indicated by NULL
1170 * dev->sdev, this function doesn't have to do anything.
1171 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1172 * Clear dev->sdev, schedule the device for ATA detach and invoke
1176 * Defined by SCSI layer. We don't really care.
1178 void ata_scsi_slave_destroy(struct scsi_device *sdev)
1180 struct ata_port *ap = ata_shost_to_port(sdev->host);
1181 unsigned long flags;
1182 struct ata_device *dev;
1184 if (!ap->ops->error_handler)
1187 spin_lock_irqsave(ap->lock, flags);
1188 dev = __ata_scsi_find_dev(ap, sdev);
1189 if (dev && dev->sdev) {
1190 /* SCSI device already in CANCEL state, no need to offline it */
1192 dev->flags |= ATA_DFLAG_DETACH;
1193 ata_port_schedule_eh(ap);
1195 spin_unlock_irqrestore(ap->lock, flags);
1197 kfree(sdev->dma_drain_buf);
1199 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
1202 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1203 * @qc: Storage for translated ATA taskfile
1205 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1206 * (to start). Perhaps these commands should be preceded by
1207 * CHECK POWER MODE to see what power mode the device is already in.
1208 * [See SAT revision 5 at www.t10.org]
1211 * spin_lock_irqsave(host lock)
1214 * Zero on success, non-zero on error.
1216 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
1218 struct scsi_cmnd *scmd = qc->scsicmd;
1219 struct ata_taskfile *tf = &qc->tf;
1220 const u8 *cdb = scmd->cmnd;
1224 if (scmd->cmd_len < 5) {
1229 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1230 tf->protocol = ATA_PROT_NODATA;
1232 ; /* ignore IMMED bit, violates sat-r05 */
1237 goto invalid_fld; /* LOEJ bit set not supported */
1239 if (((cdb[4] >> 4) & 0xf) != 0) {
1242 goto invalid_fld; /* power conditions not supported */
1246 tf->nsect = 1; /* 1 sector, lba=0 */
1248 if (qc->dev->flags & ATA_DFLAG_LBA) {
1249 tf->flags |= ATA_TFLAG_LBA;
1254 tf->device |= ATA_LBA;
1257 tf->lbal = 0x1; /* sect */
1258 tf->lbam = 0x0; /* cyl low */
1259 tf->lbah = 0x0; /* cyl high */
1262 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
1264 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1265 * or S5) causing some drives to spin up and down again.
1267 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1268 system_state == SYSTEM_POWER_OFF)
1271 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1272 system_entering_hibernation())
1275 /* Issue ATA STANDBY IMMEDIATE command */
1276 tf->command = ATA_CMD_STANDBYNOW1;
1280 * Standby and Idle condition timers could be implemented but that
1281 * would require libata to implement the Power condition mode page
1282 * and allow the user to change it. Changing mode pages requires
1283 * MODE SELECT to be implemented.
1289 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
1292 scmd->result = SAM_STAT_GOOD;
1298 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1299 * @qc: Storage for translated ATA taskfile
1301 * Sets up an ATA taskfile to issue FLUSH CACHE or
1305 * spin_lock_irqsave(host lock)
1308 * Zero on success, non-zero on error.
1310 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
1312 struct ata_taskfile *tf = &qc->tf;
1314 tf->flags |= ATA_TFLAG_DEVICE;
1315 tf->protocol = ATA_PROT_NODATA;
1317 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
1318 tf->command = ATA_CMD_FLUSH_EXT;
1320 tf->command = ATA_CMD_FLUSH;
1322 /* flush is critical for IO integrity, consider it an IO command */
1323 qc->flags |= ATA_QCFLAG_IO;
1329 * scsi_6_lba_len - Get LBA and transfer length
1330 * @cdb: SCSI command to translate
1332 * Calculate LBA and transfer length for 6-byte commands.
1336 * @plen: the transfer length
1338 static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1343 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
1344 lba |= ((u64)cdb[2]) << 8;
1345 lba |= ((u64)cdb[3]);
1354 * scsi_10_lba_len - Get LBA and transfer length
1355 * @cdb: SCSI command to translate
1357 * Calculate LBA and transfer length for 10-byte commands.
1361 * @plen: the transfer length
1363 static inline void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1365 *plba = get_unaligned_be32(&cdb[2]);
1366 *plen = get_unaligned_be16(&cdb[7]);
1370 * scsi_16_lba_len - Get LBA and transfer length
1371 * @cdb: SCSI command to translate
1373 * Calculate LBA and transfer length for 16-byte commands.
1377 * @plen: the transfer length
1379 static inline void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1381 *plba = get_unaligned_be64(&cdb[2]);
1382 *plen = get_unaligned_be32(&cdb[10]);
1386 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1387 * @qc: Storage for translated ATA taskfile
1389 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1392 * spin_lock_irqsave(host lock)
1395 * Zero on success, non-zero on error.
1397 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
1399 struct scsi_cmnd *scmd = qc->scsicmd;
1400 struct ata_taskfile *tf = &qc->tf;
1401 struct ata_device *dev = qc->dev;
1402 u64 dev_sectors = qc->dev->n_sectors;
1403 const u8 *cdb = scmd->cmnd;
1408 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1409 tf->protocol = ATA_PROT_NODATA;
1413 if (scmd->cmd_len < 10) {
1417 scsi_10_lba_len(cdb, &block, &n_block);
1420 if (scmd->cmd_len < 16) {
1424 scsi_16_lba_len(cdb, &block, &n_block);
1433 if (block >= dev_sectors)
1435 if ((block + n_block) > dev_sectors)
1438 if (dev->flags & ATA_DFLAG_LBA) {
1439 tf->flags |= ATA_TFLAG_LBA;
1441 if (lba_28_ok(block, n_block)) {
1443 tf->command = ATA_CMD_VERIFY;
1444 tf->device |= (block >> 24) & 0xf;
1445 } else if (lba_48_ok(block, n_block)) {
1446 if (!(dev->flags & ATA_DFLAG_LBA48))
1450 tf->flags |= ATA_TFLAG_LBA48;
1451 tf->command = ATA_CMD_VERIFY_EXT;
1453 tf->hob_nsect = (n_block >> 8) & 0xff;
1455 tf->hob_lbah = (block >> 40) & 0xff;
1456 tf->hob_lbam = (block >> 32) & 0xff;
1457 tf->hob_lbal = (block >> 24) & 0xff;
1459 /* request too large even for LBA48 */
1462 tf->nsect = n_block & 0xff;
1464 tf->lbah = (block >> 16) & 0xff;
1465 tf->lbam = (block >> 8) & 0xff;
1466 tf->lbal = block & 0xff;
1468 tf->device |= ATA_LBA;
1471 u32 sect, head, cyl, track;
1473 if (!lba_28_ok(block, n_block))
1476 /* Convert LBA to CHS */
1477 track = (u32)block / dev->sectors;
1478 cyl = track / dev->heads;
1479 head = track % dev->heads;
1480 sect = (u32)block % dev->sectors + 1;
1482 /* Check whether the converted CHS can fit.
1486 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1489 tf->command = ATA_CMD_VERIFY;
1490 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1493 tf->lbah = cyl >> 8;
1500 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
1504 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1505 /* "Logical Block Address out of range" */
1509 scmd->result = SAM_STAT_GOOD;
1513 static bool ata_check_nblocks(struct scsi_cmnd *scmd, u32 n_blocks)
1515 struct request *rq = scsi_cmd_to_rq(scmd);
1518 if (!blk_rq_is_passthrough(rq))
1521 req_blocks = blk_rq_bytes(rq) / scmd->device->sector_size;
1522 if (n_blocks > req_blocks)
1529 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1530 * @qc: Storage for translated ATA taskfile
1532 * Converts any of six SCSI read/write commands into the
1533 * ATA counterpart, including starting sector (LBA),
1534 * sector count, and taking into account the device's LBA48
1537 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1538 * %WRITE_16 are currently supported.
1541 * spin_lock_irqsave(host lock)
1544 * Zero on success, non-zero on error.
1546 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
1548 struct scsi_cmnd *scmd = qc->scsicmd;
1549 const u8 *cdb = scmd->cmnd;
1550 struct request *rq = scsi_cmd_to_rq(scmd);
1551 int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
1552 unsigned int tf_flags = 0;
1562 tf_flags |= ATA_TFLAG_WRITE;
1566 /* Calculate the SCSI LBA, transfer length and FUA. */
1570 if (unlikely(scmd->cmd_len < 10)) {
1574 scsi_10_lba_len(cdb, &block, &n_block);
1575 if (cdb[1] & (1 << 3))
1576 tf_flags |= ATA_TFLAG_FUA;
1577 if (!ata_check_nblocks(scmd, n_block))
1582 if (unlikely(scmd->cmd_len < 6)) {
1586 scsi_6_lba_len(cdb, &block, &n_block);
1588 /* for 6-byte r/w commands, transfer length 0
1589 * means 256 blocks of data, not 0 block.
1593 if (!ata_check_nblocks(scmd, n_block))
1598 if (unlikely(scmd->cmd_len < 16)) {
1602 scsi_16_lba_len(cdb, &block, &n_block);
1603 if (cdb[1] & (1 << 3))
1604 tf_flags |= ATA_TFLAG_FUA;
1605 if (!ata_check_nblocks(scmd, n_block))
1613 /* Check and compose ATA command */
1615 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1616 * length 0 means transfer 0 block of data.
1617 * However, for ATA R/W commands, sector count 0 means
1618 * 256 or 65536 sectors, not 0 sectors as in SCSI.
1620 * WARNING: one or two older ATA drives treat 0 as 0...
1624 qc->flags |= ATA_QCFLAG_IO;
1625 qc->nbytes = n_block * scmd->device->sector_size;
1627 rc = ata_build_rw_tf(qc, block, n_block, tf_flags, class);
1628 if (likely(rc == 0))
1633 /* treat all other errors as -EINVAL, fall through */
1635 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
1639 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1640 /* "Logical Block Address out of range" */
1644 scmd->result = SAM_STAT_GOOD;
1648 static void ata_qc_done(struct ata_queued_cmd *qc)
1650 struct scsi_cmnd *cmd = qc->scsicmd;
1651 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1657 static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1659 struct ata_port *ap = qc->ap;
1660 struct scsi_cmnd *cmd = qc->scsicmd;
1661 u8 *cdb = cmd->cmnd;
1662 int need_sense = (qc->err_mask != 0) &&
1663 !(qc->flags & ATA_QCFLAG_SENSE_VALID);
1665 /* For ATA pass thru (SAT) commands, generate a sense block if
1666 * user mandated it or if there's an error. Note that if we
1667 * generate because the user forced us to [CK_COND =1], a check
1668 * condition is generated and the ATA register values are returned
1669 * whether the command completed successfully or not. If there
1670 * was no error, we use the following sense data:
1671 * sk = RECOVERED ERROR
1672 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
1674 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1675 ((cdb[2] & 0x20) || need_sense))
1676 ata_gen_passthru_sense(qc);
1677 else if (need_sense)
1678 ata_gen_ata_sense(qc);
1680 /* Keep the SCSI ML and status byte, clear host byte. */
1681 cmd->result &= 0x0000ffff;
1683 if (need_sense && !ap->ops->error_handler)
1684 ata_dump_status(ap, &qc->result_tf);
1690 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1691 * @dev: ATA device to which the command is addressed
1692 * @cmd: SCSI command to execute
1693 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1695 * Our ->queuecommand() function has decided that the SCSI
1696 * command issued can be directly translated into an ATA
1697 * command, rather than handled internally.
1699 * This function sets up an ata_queued_cmd structure for the
1700 * SCSI command, and sends that ata_queued_cmd to the hardware.
1702 * The xlat_func argument (actor) returns 0 if ready to execute
1703 * ATA command, else 1 to finish translation. If 1 is returned
1704 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1705 * to be set reflecting an error condition or clean (early)
1709 * spin_lock_irqsave(host lock)
1712 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1713 * needs to be deferred.
1715 static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1716 ata_xlat_func_t xlat_func)
1718 struct ata_port *ap = dev->link->ap;
1719 struct ata_queued_cmd *qc;
1722 qc = ata_scsi_qc_new(dev, cmd);
1726 /* data is present; dma-map it */
1727 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1728 cmd->sc_data_direction == DMA_TO_DEVICE) {
1729 if (unlikely(scsi_bufflen(cmd) < 1)) {
1730 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
1734 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
1736 qc->dma_dir = cmd->sc_data_direction;
1739 qc->complete_fn = ata_scsi_qc_complete;
1744 if (ap->ops->qc_defer) {
1745 if ((rc = ap->ops->qc_defer(qc)))
1749 /* select device, send command to hardware */
1761 cmd->result = (DID_ERROR << 16);
1768 if (rc == ATA_DEFER_LINK)
1769 return SCSI_MLQUEUE_DEVICE_BUSY;
1771 return SCSI_MLQUEUE_HOST_BUSY;
1774 struct ata_scsi_args {
1775 struct ata_device *dev;
1777 struct scsi_cmnd *cmd;
1781 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1782 * @args: device IDENTIFY data / SCSI command of interest.
1783 * @actor: Callback hook for desired SCSI command simulator
1785 * Takes care of the hard work of simulating a SCSI command...
1786 * Mapping the response buffer, calling the command's handler,
1787 * and handling the handler's return value. This return value
1788 * indicates whether the handler wishes the SCSI command to be
1789 * completed successfully (0), or not (in which case cmd->result
1790 * and sense buffer are assumed to be set).
1793 * spin_lock_irqsave(host lock)
1795 static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1796 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
1799 struct scsi_cmnd *cmd = args->cmd;
1800 unsigned long flags;
1802 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
1804 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
1805 rc = actor(args, ata_scsi_rbuf);
1807 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1808 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1810 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
1813 cmd->result = SAM_STAT_GOOD;
1817 * ata_scsiop_inq_std - Simulate INQUIRY command
1818 * @args: device IDENTIFY data / SCSI command of interest.
1819 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1821 * Returns standard device identification data associated
1822 * with non-VPD INQUIRY command output.
1825 * spin_lock_irqsave(host lock)
1827 static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
1829 static const u8 versions[] = {
1831 0x60, /* SAM-3 (no version claimed) */
1834 0x20, /* SBC-2 (no version claimed) */
1837 0x00 /* SPC-3 (no version claimed) */
1839 static const u8 versions_zbc[] = {
1841 0xA0, /* SAM-5 (no version claimed) */
1844 0x00, /* SBC-4 (no version claimed) */
1847 0xC0, /* SPC-5 (no version claimed) */
1856 0x5, /* claim SPC-3 version compatibility */
1864 /* set scsi removable (RMB) bit per ata bit, or if the
1865 * AHCI port says it's external (Hotplug-capable, eSATA).
1867 if (ata_id_removable(args->id) ||
1868 (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
1871 if (args->dev->class == ATA_DEV_ZAC) {
1873 hdr[2] = 0x7; /* claim SPC-5 version compatibility */
1876 memcpy(rbuf, hdr, sizeof(hdr));
1877 memcpy(&rbuf[8], "ATA ", 8);
1878 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
1880 /* From SAT, use last 2 words from fw rev unless they are spaces */
1881 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
1882 if (strncmp(&rbuf[32], " ", 4) == 0)
1883 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
1885 if (rbuf[32] == 0 || rbuf[32] == ' ')
1886 memcpy(&rbuf[32], "n/a ", 4);
1888 if (ata_id_zoned_cap(args->id) || args->dev->class == ATA_DEV_ZAC)
1889 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
1891 memcpy(rbuf + 58, versions, sizeof(versions));
1897 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
1898 * @args: device IDENTIFY data / SCSI command of interest.
1899 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1901 * Returns list of inquiry VPD pages available.
1904 * spin_lock_irqsave(host lock)
1906 static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
1908 int i, num_pages = 0;
1909 static const u8 pages[] = {
1910 0x00, /* page 0x00, this page */
1911 0x80, /* page 0x80, unit serial no page */
1912 0x83, /* page 0x83, device ident page */
1913 0x89, /* page 0x89, ata info page */
1914 0xb0, /* page 0xb0, block limits page */
1915 0xb1, /* page 0xb1, block device characteristics page */
1916 0xb2, /* page 0xb2, thin provisioning page */
1917 0xb6, /* page 0xb6, zoned block device characteristics */
1918 0xb9, /* page 0xb9, concurrent positioning ranges */
1921 for (i = 0; i < sizeof(pages); i++) {
1922 if (pages[i] == 0xb6 &&
1923 !(args->dev->flags & ATA_DFLAG_ZAC))
1925 rbuf[num_pages + 4] = pages[i];
1928 rbuf[3] = num_pages; /* number of supported VPD pages */
1933 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
1934 * @args: device IDENTIFY data / SCSI command of interest.
1935 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1937 * Returns ATA device serial number.
1940 * spin_lock_irqsave(host lock)
1942 static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
1944 static const u8 hdr[] = {
1946 0x80, /* this page code */
1948 ATA_ID_SERNO_LEN, /* page len */
1951 memcpy(rbuf, hdr, sizeof(hdr));
1952 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1953 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
1958 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
1959 * @args: device IDENTIFY data / SCSI command of interest.
1960 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1962 * Yields two logical unit device identification designators:
1963 * - vendor specific ASCII containing the ATA serial number
1964 * - SAT defined "t10 vendor id based" containing ASCII vendor
1965 * name ("ATA "), model and serial numbers.
1968 * spin_lock_irqsave(host lock)
1970 static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
1972 const int sat_model_serial_desc_len = 68;
1975 rbuf[1] = 0x83; /* this page code */
1978 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
1980 rbuf[num + 3] = ATA_ID_SERNO_LEN;
1982 ata_id_string(args->id, (unsigned char *) rbuf + num,
1983 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
1984 num += ATA_ID_SERNO_LEN;
1986 /* SAT defined lu model and serial numbers descriptor */
1987 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
1990 rbuf[num + 3] = sat_model_serial_desc_len;
1992 memcpy(rbuf + num, "ATA ", 8);
1994 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
1996 num += ATA_ID_PROD_LEN;
1997 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
1999 num += ATA_ID_SERNO_LEN;
2001 if (ata_id_has_wwn(args->id)) {
2002 /* SAT defined lu world wide name */
2003 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2006 rbuf[num + 3] = ATA_ID_WWN_LEN;
2008 ata_id_string(args->id, (unsigned char *) rbuf + num,
2009 ATA_ID_WWN, ATA_ID_WWN_LEN);
2010 num += ATA_ID_WWN_LEN;
2012 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
2017 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2018 * @args: device IDENTIFY data / SCSI command of interest.
2019 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2021 * Yields SAT-specified ATA VPD page.
2024 * spin_lock_irqsave(host lock)
2026 static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
2028 rbuf[1] = 0x89; /* our page code */
2029 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2030 rbuf[3] = (0x238 & 0xff);
2032 memcpy(&rbuf[8], "linux ", 8);
2033 memcpy(&rbuf[16], "libata ", 16);
2034 memcpy(&rbuf[32], DRV_VERSION, 4);
2036 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
2037 rbuf[37] = (1 << 7); /* bit 7 indicates Command FIS */
2040 /* we don't store the ATA device signature, so we fake it */
2041 rbuf[38] = ATA_DRDY; /* really, this is Status reg */
2045 rbuf[56] = ATA_CMD_ID_ATA;
2047 memcpy(&rbuf[60], &args->id[0], 512);
2051 static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2053 struct ata_device *dev = args->dev;
2057 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2060 * Optimal transfer length granularity.
2062 * This is always one physical block, but for disks with a smaller
2063 * logical than physical sector size we need to figure out what the
2066 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
2067 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2070 * Optimal unmap granularity.
2072 * The ATA spec doesn't even know about a granularity or alignment
2073 * for the TRIM command. We can leave away most of the unmap related
2074 * VPD page entries, but we have specifify a granularity to signal
2075 * that we support some form of unmap - in thise case via WRITE SAME
2076 * with the unmap bit set.
2078 if (ata_id_has_trim(args->id)) {
2079 u64 max_blocks = 65535 * ATA_MAX_TRIM_RNUM;
2081 if (dev->horkage & ATA_HORKAGE_MAX_TRIM_128M)
2082 max_blocks = 128 << (20 - SECTOR_SHIFT);
2084 put_unaligned_be64(max_blocks, &rbuf[36]);
2085 put_unaligned_be32(1, &rbuf[28]);
2091 static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2093 int form_factor = ata_id_form_factor(args->id);
2094 int media_rotation_rate = ata_id_rotation_rate(args->id);
2095 u8 zoned = ata_id_zoned_cap(args->id);
2099 rbuf[4] = media_rotation_rate >> 8;
2100 rbuf[5] = media_rotation_rate;
2101 rbuf[7] = form_factor;
2103 rbuf[8] = (zoned << 4);
2108 static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
2110 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2113 rbuf[5] = 1 << 6; /* TPWS */
2118 static unsigned int ata_scsiop_inq_b6(struct ata_scsi_args *args, u8 *rbuf)
2121 * zbc-r05 SCSI Zoned Block device characteristics VPD page
2127 * URSWRZ bit is only meaningful for host-managed ZAC drives
2129 if (args->dev->zac_zoned_cap & 1)
2131 put_unaligned_be32(args->dev->zac_zones_optimal_open, &rbuf[8]);
2132 put_unaligned_be32(args->dev->zac_zones_optimal_nonseq, &rbuf[12]);
2133 put_unaligned_be32(args->dev->zac_zones_max_open, &rbuf[16]);
2138 static unsigned int ata_scsiop_inq_b9(struct ata_scsi_args *args, u8 *rbuf)
2140 struct ata_cpr_log *cpr_log = args->dev->cpr_log;
2141 u8 *desc = &rbuf[64];
2144 /* SCSI Concurrent Positioning Ranges VPD page: SBC-5 rev 1 or later */
2146 put_unaligned_be16(64 + (int)cpr_log->nr_cpr * 32 - 4, &rbuf[2]);
2148 for (i = 0; i < cpr_log->nr_cpr; i++, desc += 32) {
2149 desc[0] = cpr_log->cpr[i].num;
2150 desc[1] = cpr_log->cpr[i].num_storage_elements;
2151 put_unaligned_be64(cpr_log->cpr[i].start_lba, &desc[8]);
2152 put_unaligned_be64(cpr_log->cpr[i].num_lbas, &desc[16]);
2159 * modecpy - Prepare response for MODE SENSE
2160 * @dest: output buffer
2161 * @src: data being copied
2162 * @n: length of mode page
2163 * @changeable: whether changeable parameters are requested
2165 * Generate a generic MODE SENSE page for either current or changeable
2171 static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2174 memcpy(dest, src, 2);
2175 memset(dest + 2, 0, n - 2);
2177 memcpy(dest, src, n);
2182 * ata_msense_caching - Simulate MODE SENSE caching info page
2183 * @id: device IDENTIFY data
2184 * @buf: output buffer
2185 * @changeable: whether changeable parameters are requested
2187 * Generate a caching info page, which conditionally indicates
2188 * write caching to the SCSI layer, depending on device
2194 static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
2196 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
2198 buf[2] |= (1 << 2); /* ata_mselect_caching() */
2200 buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */
2201 buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */
2203 return sizeof(def_cache_mpage);
2207 * ata_msense_control - Simulate MODE SENSE control mode page
2208 * @dev: ATA device of interest
2209 * @buf: output buffer
2210 * @changeable: whether changeable parameters are requested
2212 * Generate a generic MODE SENSE control mode page.
2217 static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
2220 modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
2222 buf[2] |= (1 << 2); /* ata_mselect_control() */
2224 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
2226 buf[2] |= (d_sense << 2); /* descriptor format sense data */
2228 return sizeof(def_control_mpage);
2232 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
2233 * @buf: output buffer
2234 * @changeable: whether changeable parameters are requested
2236 * Generate a generic MODE SENSE r/w error recovery page.
2241 static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
2243 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2245 return sizeof(def_rw_recovery_mpage);
2249 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2250 * @args: device IDENTIFY data / SCSI command of interest.
2251 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2253 * Simulate MODE SENSE commands. Assume this is invoked for direct
2254 * access devices (e.g. disks) only. There should be no block
2255 * descriptor for other device types.
2258 * spin_lock_irqsave(host lock)
2260 static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
2262 struct ata_device *dev = args->dev;
2263 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
2264 static const u8 sat_blk_desc[] = {
2265 0, 0, 0, 0, /* number of blocks: sat unspecified */
2267 0, 0x2, 0x0 /* block length: 512 bytes */
2270 unsigned int ebd, page_control, six_byte;
2271 u8 dpofua = 0, bp = 0xff;
2274 six_byte = (scsicmd[0] == MODE_SENSE);
2275 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2277 * LLBA bit in msense(10) ignored (compliant)
2280 page_control = scsicmd[2] >> 6;
2281 switch (page_control) {
2282 case 0: /* current */
2283 case 1: /* changeable */
2284 case 2: /* defaults */
2285 break; /* supported */
2287 goto saving_not_supp;
2295 p += 4 + (ebd ? 8 : 0);
2297 p += 8 + (ebd ? 8 : 0);
2299 pg = scsicmd[2] & 0x3f;
2302 * No mode subpages supported (yet) but asking for _all_
2303 * subpages may be valid
2305 if (spg && (spg != ALL_SUB_MPAGES)) {
2311 case RW_RECOVERY_MPAGE:
2312 p += ata_msense_rw_recovery(p, page_control == 1);
2316 p += ata_msense_caching(args->id, p, page_control == 1);
2320 p += ata_msense_control(args->dev, p, page_control == 1);
2324 p += ata_msense_rw_recovery(p, page_control == 1);
2325 p += ata_msense_caching(args->id, p, page_control == 1);
2326 p += ata_msense_control(args->dev, p, page_control == 1);
2329 default: /* invalid page code */
2334 if (dev->flags & ATA_DFLAG_FUA)
2338 rbuf[0] = p - rbuf - 1;
2341 rbuf[3] = sizeof(sat_blk_desc);
2342 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
2345 unsigned int output_len = p - rbuf - 2;
2347 rbuf[0] = output_len >> 8;
2348 rbuf[1] = output_len;
2351 rbuf[7] = sizeof(sat_blk_desc);
2352 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
2358 ata_scsi_set_invalid_field(dev, args->cmd, fp, bp);
2362 ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2363 /* "Saving parameters not supported" */
2368 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2369 * @args: device IDENTIFY data / SCSI command of interest.
2370 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2372 * Simulate READ CAPACITY commands.
2377 static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
2379 struct ata_device *dev = args->dev;
2380 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
2381 u32 sector_size; /* physical sector size in bytes */
2385 sector_size = ata_id_logical_sector_size(dev->id);
2386 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2387 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
2389 if (args->cmd->cmnd[0] == READ_CAPACITY) {
2390 if (last_lba >= 0xffffffffULL)
2391 last_lba = 0xffffffff;
2393 /* sector count, 32-bit */
2394 rbuf[0] = last_lba >> (8 * 3);
2395 rbuf[1] = last_lba >> (8 * 2);
2396 rbuf[2] = last_lba >> (8 * 1);
2400 rbuf[4] = sector_size >> (8 * 3);
2401 rbuf[5] = sector_size >> (8 * 2);
2402 rbuf[6] = sector_size >> (8 * 1);
2403 rbuf[7] = sector_size;
2405 /* sector count, 64-bit */
2406 rbuf[0] = last_lba >> (8 * 7);
2407 rbuf[1] = last_lba >> (8 * 6);
2408 rbuf[2] = last_lba >> (8 * 5);
2409 rbuf[3] = last_lba >> (8 * 4);
2410 rbuf[4] = last_lba >> (8 * 3);
2411 rbuf[5] = last_lba >> (8 * 2);
2412 rbuf[6] = last_lba >> (8 * 1);
2416 rbuf[ 8] = sector_size >> (8 * 3);
2417 rbuf[ 9] = sector_size >> (8 * 2);
2418 rbuf[10] = sector_size >> (8 * 1);
2419 rbuf[11] = sector_size;
2422 rbuf[13] = log2_per_phys;
2423 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2424 rbuf[15] = lowest_aligned;
2426 if (ata_id_has_trim(args->id) &&
2427 !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
2428 rbuf[14] |= 0x80; /* LBPME */
2430 if (ata_id_has_zero_after_trim(args->id) &&
2431 dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
2432 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2433 rbuf[14] |= 0x40; /* LBPRZ */
2436 if (ata_id_zoned_cap(args->id) ||
2437 args->dev->class == ATA_DEV_ZAC)
2438 rbuf[12] = (1 << 4); /* RC_BASIS */
2444 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2445 * @args: device IDENTIFY data / SCSI command of interest.
2446 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2448 * Simulate REPORT LUNS command.
2451 * spin_lock_irqsave(host lock)
2453 static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
2455 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2460 static void atapi_sense_complete(struct ata_queued_cmd *qc)
2462 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
2463 /* FIXME: not quite right; we don't want the
2464 * translation of taskfile registers into
2465 * a sense descriptors, since that's only
2466 * correct for ATA, not ATAPI
2468 ata_gen_passthru_sense(qc);
2474 /* is it pointless to prefer PIO for "safety reasons"? */
2475 static inline int ata_pio_use_silly(struct ata_port *ap)
2477 return (ap->flags & ATA_FLAG_PIO_DMA);
2480 static void atapi_request_sense(struct ata_queued_cmd *qc)
2482 struct ata_port *ap = qc->ap;
2483 struct scsi_cmnd *cmd = qc->scsicmd;
2485 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2487 #ifdef CONFIG_ATA_SFF
2488 if (ap->ops->sff_tf_read)
2489 ap->ops->sff_tf_read(ap, &qc->tf);
2492 /* fill these in, for the case where they are -not- overwritten */
2493 cmd->sense_buffer[0] = 0x70;
2494 cmd->sense_buffer[2] = qc->tf.error >> 4;
2498 /* setup sg table and init transfer direction */
2499 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
2500 ata_sg_init(qc, &qc->sgent, 1);
2501 qc->dma_dir = DMA_FROM_DEVICE;
2503 memset(&qc->cdb, 0, qc->dev->cdb_len);
2504 qc->cdb[0] = REQUEST_SENSE;
2505 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2507 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2508 qc->tf.command = ATA_CMD_PACKET;
2510 if (ata_pio_use_silly(ap)) {
2511 qc->tf.protocol = ATAPI_PROT_DMA;
2512 qc->tf.feature |= ATAPI_PKT_DMA;
2514 qc->tf.protocol = ATAPI_PROT_PIO;
2515 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2518 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2520 qc->complete_fn = atapi_sense_complete;
2526 * ATAPI devices typically report zero for their SCSI version, and sometimes
2527 * deviate from the spec WRT response data format. If SCSI version is
2528 * reported as zero like normal, then we make the following fixups:
2529 * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a
2531 * 2) Ensure response data format / ATAPI information are always correct.
2533 static void atapi_fixup_inquiry(struct scsi_cmnd *cmd)
2537 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2542 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2545 static void atapi_qc_complete(struct ata_queued_cmd *qc)
2547 struct scsi_cmnd *cmd = qc->scsicmd;
2548 unsigned int err_mask = qc->err_mask;
2550 /* handle completion from new EH */
2551 if (unlikely(qc->ap->ops->error_handler &&
2552 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2554 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2555 /* FIXME: not quite right; we don't want the
2556 * translation of taskfile registers into a
2557 * sense descriptors, since that's only
2558 * correct for ATA, not ATAPI
2560 ata_gen_passthru_sense(qc);
2563 /* SCSI EH automatically locks door if sdev->locked is
2564 * set. Sometimes door lock request continues to
2565 * fail, for example, when no media is present. This
2566 * creates a loop - SCSI EH issues door lock which
2567 * fails and gets invoked again to acquire sense data
2568 * for the failed command.
2570 * If door lock fails, always clear sdev->locked to
2571 * avoid this infinite loop.
2573 * This may happen before SCSI scan is complete. Make
2574 * sure qc->dev->sdev isn't NULL before dereferencing.
2576 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
2577 qc->dev->sdev->locked = 0;
2579 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2584 /* successful completion or old EH failure path */
2585 if (unlikely(err_mask & AC_ERR_DEV)) {
2586 cmd->result = SAM_STAT_CHECK_CONDITION;
2587 atapi_request_sense(qc);
2589 } else if (unlikely(err_mask)) {
2590 /* FIXME: not quite right; we don't want the
2591 * translation of taskfile registers into
2592 * a sense descriptors, since that's only
2593 * correct for ATA, not ATAPI
2595 ata_gen_passthru_sense(qc);
2597 if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0)
2598 atapi_fixup_inquiry(cmd);
2599 cmd->result = SAM_STAT_GOOD;
2605 * atapi_xlat - Initialize PACKET taskfile
2606 * @qc: command structure to be initialized
2609 * spin_lock_irqsave(host lock)
2612 * Zero on success, non-zero on failure.
2614 static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
2616 struct scsi_cmnd *scmd = qc->scsicmd;
2617 struct ata_device *dev = qc->dev;
2618 int nodata = (scmd->sc_data_direction == DMA_NONE);
2619 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
2620 unsigned int nbytes;
2622 memset(qc->cdb, 0, dev->cdb_len);
2623 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
2625 qc->complete_fn = atapi_qc_complete;
2627 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2628 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2629 qc->tf.flags |= ATA_TFLAG_WRITE;
2632 qc->tf.command = ATA_CMD_PACKET;
2633 ata_qc_set_pc_nbytes(qc);
2635 /* check whether ATAPI DMA is safe */
2636 if (!nodata && !using_pio && atapi_check_dma(qc))
2639 /* Some controller variants snoop this value for Packet
2640 * transfers to do state machine and FIFO management. Thus we
2641 * want to set it properly, and for DMA where it is
2642 * effectively meaningless.
2644 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
2646 /* Most ATAPI devices which honor transfer chunk size don't
2647 * behave according to the spec when odd chunk size which
2648 * matches the transfer length is specified. If the number of
2649 * bytes to transfer is 2n+1. According to the spec, what
2650 * should happen is to indicate that 2n+1 is going to be
2651 * transferred and transfer 2n+2 bytes where the last byte is
2654 * In practice, this doesn't happen. ATAPI devices first
2655 * indicate and transfer 2n bytes and then indicate and
2656 * transfer 2 bytes where the last byte is padding.
2658 * This inconsistency confuses several controllers which
2659 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2660 * These controllers use actual number of transferred bytes to
2661 * update DMA pointer and transfer of 4n+2 bytes make those
2662 * controller push DMA pointer by 4n+4 bytes because SATA data
2663 * FISes are aligned to 4 bytes. This causes data corruption
2664 * and buffer overrun.
2666 * Always setting nbytes to even number solves this problem
2667 * because then ATAPI devices don't have to split data at 2n
2673 qc->tf.lbam = (nbytes & 0xFF);
2674 qc->tf.lbah = (nbytes >> 8);
2677 qc->tf.protocol = ATAPI_PROT_NODATA;
2679 qc->tf.protocol = ATAPI_PROT_PIO;
2682 qc->tf.protocol = ATAPI_PROT_DMA;
2683 qc->tf.feature |= ATAPI_PKT_DMA;
2685 if ((dev->flags & ATA_DFLAG_DMADIR) &&
2686 (scmd->sc_data_direction != DMA_TO_DEVICE))
2687 /* some SATA bridges need us to indicate data xfer direction */
2688 qc->tf.feature |= ATAPI_DMADIR;
2692 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2693 as ATAPI tape drives don't get this right otherwise */
2697 static struct ata_device *ata_find_dev(struct ata_port *ap, unsigned int devno)
2700 * For the non-PMP case, ata_link_max_devices() returns 1 (SATA case),
2701 * or 2 (IDE master + slave case). However, the former case includes
2702 * libsas hosted devices which are numbered per scsi host, leading
2703 * to devno potentially being larger than 0 but with each struct
2704 * ata_device having its own struct ata_port and struct ata_link.
2705 * To accommodate these, ignore devno and always use device number 0.
2707 if (likely(!sata_pmp_attached(ap))) {
2708 int link_max_devices = ata_link_max_devices(&ap->link);
2710 if (link_max_devices == 1)
2711 return &ap->link.device[0];
2713 if (devno < link_max_devices)
2714 return &ap->link.device[devno];
2720 * For PMP-attached devices, the device number corresponds to C
2721 * (channel) of SCSI [H:C:I:L], indicating the port pmp link
2724 if (devno < ap->nr_pmp_links)
2725 return &ap->pmp_link[devno].device[0];
2730 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
2731 const struct scsi_device *scsidev)
2735 /* skip commands not addressed to targets we simulate */
2736 if (!sata_pmp_attached(ap)) {
2737 if (unlikely(scsidev->channel || scsidev->lun))
2739 devno = scsidev->id;
2741 if (unlikely(scsidev->id || scsidev->lun))
2743 devno = scsidev->channel;
2746 return ata_find_dev(ap, devno);
2750 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2751 * @ap: ATA port to which the device is attached
2752 * @scsidev: SCSI device from which we derive the ATA device
2754 * Given various information provided in struct scsi_cmnd,
2755 * map that onto an ATA bus, and using that mapping
2756 * determine which ata_device is associated with the
2757 * SCSI command to be sent.
2760 * spin_lock_irqsave(host lock)
2763 * Associated ATA device, or %NULL if not found.
2766 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
2768 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
2770 if (unlikely(!dev || !ata_dev_enabled(dev)))
2777 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2778 * @byte1: Byte 1 from pass-thru CDB.
2781 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2784 ata_scsi_map_proto(u8 byte1)
2786 switch((byte1 & 0x1e) >> 1) {
2787 case 3: /* Non-data */
2788 return ATA_PROT_NODATA;
2791 case 10: /* UDMA Data-in */
2792 case 11: /* UDMA Data-Out */
2793 return ATA_PROT_DMA;
2795 case 4: /* PIO Data-in */
2796 case 5: /* PIO Data-out */
2797 return ATA_PROT_PIO;
2799 case 12: /* FPDMA */
2800 return ATA_PROT_NCQ;
2802 case 0: /* Hard Reset */
2804 case 8: /* Device Diagnostic */
2805 case 9: /* Device Reset */
2806 case 7: /* DMA Queued */
2807 case 15: /* Return Response Info */
2808 default: /* Reserved */
2812 return ATA_PROT_UNKNOWN;
2816 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2817 * @qc: command structure to be initialized
2819 * Handles either 12, 16, or 32-byte versions of the CDB.
2822 * Zero on success, non-zero on failure.
2824 static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
2826 struct ata_taskfile *tf = &(qc->tf);
2827 struct scsi_cmnd *scmd = qc->scsicmd;
2828 struct ata_device *dev = qc->dev;
2829 const u8 *cdb = scmd->cmnd;
2833 /* 7Fh variable length cmd means a ata pass-thru(32) */
2834 if (cdb[0] == VARIABLE_LENGTH_CMD)
2837 tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]);
2838 if (tf->protocol == ATA_PROT_UNKNOWN) {
2843 if ((cdb[2 + cdb_offset] & 0x3) == 0) {
2845 * When T_LENGTH is zero (No data is transferred), dir should
2848 if (scmd->sc_data_direction != DMA_NONE) {
2849 fp = 2 + cdb_offset;
2853 if (ata_is_ncq(tf->protocol))
2854 tf->protocol = ATA_PROT_NCQ_NODATA;
2858 tf->flags |= ATA_TFLAG_LBA;
2861 * 12 and 16 byte CDBs use different offsets to
2862 * provide the various register values.
2867 * 16-byte CDB - may contain extended commands.
2869 * If that is the case, copy the upper byte register values.
2871 if (cdb[1] & 0x01) {
2872 tf->hob_feature = cdb[3];
2873 tf->hob_nsect = cdb[5];
2874 tf->hob_lbal = cdb[7];
2875 tf->hob_lbam = cdb[9];
2876 tf->hob_lbah = cdb[11];
2877 tf->flags |= ATA_TFLAG_LBA48;
2879 tf->flags &= ~ATA_TFLAG_LBA48;
2882 * Always copy low byte, device and command registers.
2884 tf->feature = cdb[4];
2889 tf->device = cdb[13];
2890 tf->command = cdb[14];
2894 * 12-byte CDB - incapable of extended commands.
2896 tf->flags &= ~ATA_TFLAG_LBA48;
2898 tf->feature = cdb[3];
2903 tf->device = cdb[8];
2904 tf->command = cdb[9];
2908 * 32-byte CDB - may contain extended command fields.
2910 * If that is the case, copy the upper byte register values.
2912 if (cdb[10] & 0x01) {
2913 tf->hob_feature = cdb[20];
2914 tf->hob_nsect = cdb[22];
2915 tf->hob_lbal = cdb[16];
2916 tf->hob_lbam = cdb[15];
2917 tf->hob_lbah = cdb[14];
2918 tf->flags |= ATA_TFLAG_LBA48;
2920 tf->flags &= ~ATA_TFLAG_LBA48;
2922 tf->feature = cdb[21];
2923 tf->nsect = cdb[23];
2927 tf->device = cdb[24];
2928 tf->command = cdb[25];
2929 tf->auxiliary = get_unaligned_be32(&cdb[28]);
2933 /* For NCQ commands copy the tag value */
2934 if (ata_is_ncq(tf->protocol))
2935 tf->nsect = qc->hw_tag << 3;
2937 /* enforce correct master/slave bit */
2938 tf->device = dev->devno ?
2939 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
2941 switch (tf->command) {
2942 /* READ/WRITE LONG use a non-standard sect_size */
2943 case ATA_CMD_READ_LONG:
2944 case ATA_CMD_READ_LONG_ONCE:
2945 case ATA_CMD_WRITE_LONG:
2946 case ATA_CMD_WRITE_LONG_ONCE:
2947 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
2951 qc->sect_size = scsi_bufflen(scmd);
2954 /* commands using reported Logical Block size (e.g. 512 or 4K) */
2955 case ATA_CMD_CFA_WRITE_NE:
2956 case ATA_CMD_CFA_TRANS_SECT:
2957 case ATA_CMD_CFA_WRITE_MULT_NE:
2958 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
2960 case ATA_CMD_READ_EXT:
2961 case ATA_CMD_READ_QUEUED:
2962 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
2963 case ATA_CMD_FPDMA_READ:
2964 case ATA_CMD_READ_MULTI:
2965 case ATA_CMD_READ_MULTI_EXT:
2966 case ATA_CMD_PIO_READ:
2967 case ATA_CMD_PIO_READ_EXT:
2968 case ATA_CMD_READ_STREAM_DMA_EXT:
2969 case ATA_CMD_READ_STREAM_EXT:
2970 case ATA_CMD_VERIFY:
2971 case ATA_CMD_VERIFY_EXT:
2973 case ATA_CMD_WRITE_EXT:
2974 case ATA_CMD_WRITE_FUA_EXT:
2975 case ATA_CMD_WRITE_QUEUED:
2976 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
2977 case ATA_CMD_FPDMA_WRITE:
2978 case ATA_CMD_WRITE_MULTI:
2979 case ATA_CMD_WRITE_MULTI_EXT:
2980 case ATA_CMD_WRITE_MULTI_FUA_EXT:
2981 case ATA_CMD_PIO_WRITE:
2982 case ATA_CMD_PIO_WRITE_EXT:
2983 case ATA_CMD_WRITE_STREAM_DMA_EXT:
2984 case ATA_CMD_WRITE_STREAM_EXT:
2985 qc->sect_size = scmd->device->sector_size;
2988 /* Everything else uses 512 byte "sectors" */
2990 qc->sect_size = ATA_SECT_SIZE;
2994 * Set flags so that all registers will be written, pass on
2995 * write indication (used for PIO/DMA setup), result TF is
2996 * copied back and we don't whine too much about its failure.
2998 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2999 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3000 tf->flags |= ATA_TFLAG_WRITE;
3002 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3005 * Set transfer length.
3007 * TODO: find out if we need to do more here to
3008 * cover scatter/gather case.
3010 ata_qc_set_pc_nbytes(qc);
3012 /* We may not issue DMA commands if no DMA mode is set */
3013 if (tf->protocol == ATA_PROT_DMA && !ata_dma_enabled(dev)) {
3018 /* We may not issue NCQ commands to devices not supporting NCQ */
3019 if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) {
3024 /* sanity check for pio multi commands */
3025 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
3030 if (is_multi_taskfile(tf)) {
3031 unsigned int multi_count = 1 << (cdb[1] >> 5);
3033 /* compare the passed through multi_count
3034 * with the cached multi_count of libata
3036 if (multi_count != dev->multi_count)
3037 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3042 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3043 * SET_FEATURES - XFER MODE must be preceded/succeeded
3044 * by an update to hardware-specific registers for each
3045 * controller (i.e. the reason for ->set_piomode(),
3046 * ->set_dmamode(), and ->post_set_mode() hooks).
3048 if (tf->command == ATA_CMD_SET_FEATURES &&
3049 tf->feature == SETFEATURES_XFER) {
3050 fp = (cdb[0] == ATA_16) ? 4 : 3;
3055 * Filter TPM commands by default. These provide an
3056 * essentially uncontrolled encrypted "back door" between
3057 * applications and the disk. Set libata.allow_tpm=1 if you
3058 * have a real reason for wanting to use them. This ensures
3059 * that installed software cannot easily mess stuff up without
3060 * user intent. DVR type users will probably ship with this enabled
3061 * for movie content management.
3063 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3064 * for this and should do in future but that it is not sufficient as
3065 * DCS is an optional feature set. Thus we also do the software filter
3066 * so that we comply with the TC consortium stated goal that the user
3067 * can turn off TC features of their system.
3069 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
3070 fp = (cdb[0] == ATA_16) ? 14 : 9;
3077 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
3082 * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim
3083 * @cmd: SCSI command being translated
3084 * @trmax: Maximum number of entries that will fit in sector_size bytes.
3085 * @sector: Starting sector
3086 * @count: Total Range of request in logical sectors
3088 * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted
3091 * Upto 64 entries of the format:
3092 * 63:48 Range Length
3095 * Range Length of 0 is ignored.
3096 * LBA's should be sorted order and not overlap.
3098 * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET
3100 * Return: Number of bytes copied into sglist.
3102 static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax,
3103 u64 sector, u32 count)
3105 struct scsi_device *sdp = cmd->device;
3106 size_t len = sdp->sector_size;
3110 unsigned long flags;
3112 WARN_ON(len > ATA_SCSI_RBUF_SIZE);
3114 if (len > ATA_SCSI_RBUF_SIZE)
3115 len = ATA_SCSI_RBUF_SIZE;
3117 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
3118 buf = ((void *)ata_scsi_rbuf);
3119 memset(buf, 0, len);
3121 u64 entry = sector |
3122 ((u64)(count > 0xffff ? 0xffff : count) << 48);
3123 buf[i++] = __cpu_to_le64(entry);
3124 if (count <= 0xffff)
3129 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
3130 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
3136 * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same
3137 * @qc: Command to be translated
3139 * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or
3140 * an SCT Write Same command.
3141 * Based on WRITE SAME has the UNMAP flag:
3143 * - When set translate to DSM TRIM
3144 * - When clear translate to SCT Write Same
3146 static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3148 struct ata_taskfile *tf = &qc->tf;
3149 struct scsi_cmnd *scmd = qc->scsicmd;
3150 struct scsi_device *sdp = scmd->device;
3151 size_t len = sdp->sector_size;
3152 struct ata_device *dev = qc->dev;
3153 const u8 *cdb = scmd->cmnd;
3156 const u32 trmax = len >> 3;
3160 u8 unmap = cdb[1] & 0x8;
3162 /* we may not issue DMA commands if no DMA mode is set */
3163 if (unlikely(!ata_dma_enabled(dev)))
3164 goto invalid_opcode;
3167 * We only allow sending this command through the block layer,
3168 * as it modifies the DATA OUT buffer, which would corrupt user
3169 * memory for SG_IO commands.
3171 if (unlikely(blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))))
3172 goto invalid_opcode;
3174 if (unlikely(scmd->cmd_len < 16)) {
3178 scsi_16_lba_len(cdb, &block, &n_block);
3181 (dev->horkage & ATA_HORKAGE_NOTRIM) ||
3182 !ata_id_has_trim(dev->id)) {
3187 /* If the request is too large the cmd is invalid */
3188 if (n_block > 0xffff * trmax) {
3194 * WRITE SAME always has a sector sized buffer as payload, this
3195 * should never be a multiple entry S/G list.
3197 if (!scsi_sg_count(scmd))
3198 goto invalid_param_len;
3201 * size must match sector size in bytes
3202 * For DATA SET MANAGEMENT TRIM in ACS-2 nsect (aka count)
3203 * is defined as number of 512 byte blocks to be transferred.
3206 size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block);
3208 goto invalid_param_len;
3210 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3211 /* Newer devices support queued TRIM commands */
3212 tf->protocol = ATA_PROT_NCQ;
3213 tf->command = ATA_CMD_FPDMA_SEND;
3214 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
3215 tf->nsect = qc->hw_tag << 3;
3216 tf->hob_feature = (size / 512) >> 8;
3217 tf->feature = size / 512;
3221 tf->protocol = ATA_PROT_DMA;
3222 tf->hob_feature = 0;
3223 tf->feature = ATA_DSM_TRIM;
3224 tf->hob_nsect = (size / 512) >> 8;
3225 tf->nsect = size / 512;
3226 tf->command = ATA_CMD_DSM;
3229 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3232 ata_qc_set_pc_nbytes(qc);
3237 ata_scsi_set_invalid_field(dev, scmd, fp, bp);
3240 /* "Parameter list length error" */
3241 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3244 /* "Invalid command operation code" */
3245 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
3250 * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN
3251 * @args: device MAINTENANCE_IN data / SCSI command of interest.
3252 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
3254 * Yields a subset to satisfy scsi_report_opcode()
3257 * spin_lock_irqsave(host lock)
3259 static unsigned int ata_scsiop_maint_in(struct ata_scsi_args *args, u8 *rbuf)
3261 struct ata_device *dev = args->dev;
3262 u8 *cdb = args->cmd->cmnd;
3264 unsigned int err = 0;
3266 if (cdb[2] != 1 && cdb[2] != 3) {
3267 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
3277 case SERVICE_ACTION_IN_16:
3280 case SYNCHRONIZE_CACHE:
3281 case SYNCHRONIZE_CACHE_16:
3285 case TEST_UNIT_READY:
3286 case SEND_DIAGNOSTIC:
3287 case MAINTENANCE_IN:
3299 case MODE_SELECT_10:
3305 if (ata_id_zoned_cap(dev->id) ||
3306 dev->class == ATA_DEV_ZAC)
3309 case SECURITY_PROTOCOL_IN:
3310 case SECURITY_PROTOCOL_OUT:
3311 if (dev->flags & ATA_DFLAG_TRUSTED)
3318 rbuf[1] = supported; /* supported */
3323 * ata_scsi_report_zones_complete - convert ATA output
3324 * @qc: command structure returning the data
3326 * Convert T-13 little-endian field representation into
3327 * T-10 big-endian field representation.
3330 static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
3332 struct scsi_cmnd *scmd = qc->scsicmd;
3333 struct sg_mapping_iter miter;
3334 unsigned long flags;
3335 unsigned int bytes = 0;
3337 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
3338 SG_MITER_TO_SG | SG_MITER_ATOMIC);
3340 local_irq_save(flags);
3341 while (sg_miter_next(&miter)) {
3342 unsigned int offset = 0;
3347 u64 max_lba, opt_lba;
3350 /* Swizzle header */
3352 list_length = get_unaligned_le32(&hdr[0]);
3353 same = get_unaligned_le16(&hdr[4]);
3354 max_lba = get_unaligned_le64(&hdr[8]);
3355 opt_lba = get_unaligned_le64(&hdr[16]);
3356 put_unaligned_be32(list_length, &hdr[0]);
3357 hdr[4] = same & 0xf;
3358 put_unaligned_be64(max_lba, &hdr[8]);
3359 put_unaligned_be64(opt_lba, &hdr[16]);
3363 while (offset < miter.length) {
3365 u8 cond, type, non_seq, reset;
3366 u64 size, start, wp;
3368 /* Swizzle zone descriptor */
3369 rec = miter.addr + offset;
3370 type = rec[0] & 0xf;
3371 cond = (rec[1] >> 4) & 0xf;
3372 non_seq = (rec[1] & 2);
3373 reset = (rec[1] & 1);
3374 size = get_unaligned_le64(&rec[8]);
3375 start = get_unaligned_le64(&rec[16]);
3376 wp = get_unaligned_le64(&rec[24]);
3378 rec[1] = (cond << 4) | non_seq | reset;
3379 put_unaligned_be64(size, &rec[8]);
3380 put_unaligned_be64(start, &rec[16]);
3381 put_unaligned_be64(wp, &rec[24]);
3382 WARN_ON(offset + 64 > miter.length);
3387 sg_miter_stop(&miter);
3388 local_irq_restore(flags);
3390 ata_scsi_qc_complete(qc);
3393 static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
3395 struct ata_taskfile *tf = &qc->tf;
3396 struct scsi_cmnd *scmd = qc->scsicmd;
3397 const u8 *cdb = scmd->cmnd;
3398 u16 sect, fp = (u16)-1;
3399 u8 sa, options, bp = 0xff;
3403 if (unlikely(scmd->cmd_len < 16)) {
3404 ata_dev_warn(qc->dev, "invalid cdb length %d\n",
3409 scsi_16_lba_len(cdb, &block, &n_block);
3410 if (n_block != scsi_bufflen(scmd)) {
3411 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
3412 n_block, scsi_bufflen(scmd));
3413 goto invalid_param_len;
3416 if (sa != ZI_REPORT_ZONES) {
3417 ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
3422 * ZAC allows only for transfers in 512 byte blocks,
3423 * and uses a 16 bit value for the transfer count.
3425 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
3426 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
3427 goto invalid_param_len;
3429 sect = n_block / 512;
3430 options = cdb[14] & 0xbf;
3432 if (ata_ncq_enabled(qc->dev) &&
3433 ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
3434 tf->protocol = ATA_PROT_NCQ;
3435 tf->command = ATA_CMD_FPDMA_RECV;
3436 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
3437 tf->nsect = qc->hw_tag << 3;
3438 tf->feature = sect & 0xff;
3439 tf->hob_feature = (sect >> 8) & 0xff;
3440 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
3442 tf->command = ATA_CMD_ZAC_MGMT_IN;
3443 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
3444 tf->protocol = ATA_PROT_DMA;
3445 tf->hob_feature = options;
3446 tf->hob_nsect = (sect >> 8) & 0xff;
3447 tf->nsect = sect & 0xff;
3449 tf->device = ATA_LBA;
3450 tf->lbah = (block >> 16) & 0xff;
3451 tf->lbam = (block >> 8) & 0xff;
3452 tf->lbal = block & 0xff;
3453 tf->hob_lbah = (block >> 40) & 0xff;
3454 tf->hob_lbam = (block >> 32) & 0xff;
3455 tf->hob_lbal = (block >> 24) & 0xff;
3457 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3458 qc->flags |= ATA_QCFLAG_RESULT_TF;
3460 ata_qc_set_pc_nbytes(qc);
3462 qc->complete_fn = ata_scsi_report_zones_complete;
3467 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
3471 /* "Parameter list length error" */
3472 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3476 static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
3478 struct ata_taskfile *tf = &qc->tf;
3479 struct scsi_cmnd *scmd = qc->scsicmd;
3480 struct ata_device *dev = qc->dev;
3481 const u8 *cdb = scmd->cmnd;
3487 if (unlikely(scmd->cmd_len < 16)) {
3493 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
3494 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
3499 scsi_16_lba_len(cdb, &block, &n_block);
3502 * ZAC MANAGEMENT OUT doesn't define any length
3504 goto invalid_param_len;
3507 all = cdb[14] & 0x1;
3510 * Ignore the block address (zone ID) as defined by ZBC.
3513 } else if (block >= dev->n_sectors) {
3515 * Block must be a valid zone ID (a zone start LBA).
3521 if (ata_ncq_enabled(qc->dev) &&
3522 ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
3523 tf->protocol = ATA_PROT_NCQ_NODATA;
3524 tf->command = ATA_CMD_NCQ_NON_DATA;
3525 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
3526 tf->nsect = qc->hw_tag << 3;
3527 tf->auxiliary = sa | ((u16)all << 8);
3529 tf->protocol = ATA_PROT_NODATA;
3530 tf->command = ATA_CMD_ZAC_MGMT_OUT;
3532 tf->hob_feature = all;
3534 tf->lbah = (block >> 16) & 0xff;
3535 tf->lbam = (block >> 8) & 0xff;
3536 tf->lbal = block & 0xff;
3537 tf->hob_lbah = (block >> 40) & 0xff;
3538 tf->hob_lbam = (block >> 32) & 0xff;
3539 tf->hob_lbal = (block >> 24) & 0xff;
3540 tf->device = ATA_LBA;
3541 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3546 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
3549 /* "Parameter list length error" */
3550 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3555 * ata_mselect_caching - Simulate MODE SELECT for caching info page
3556 * @qc: Storage for translated ATA taskfile
3557 * @buf: input buffer
3558 * @len: number of valid bytes in the input buffer
3559 * @fp: out parameter for the failed field on error
3561 * Prepare a taskfile to modify caching information for the device.
3566 static int ata_mselect_caching(struct ata_queued_cmd *qc,
3567 const u8 *buf, int len, u16 *fp)
3569 struct ata_taskfile *tf = &qc->tf;
3570 struct ata_device *dev = qc->dev;
3571 u8 mpage[CACHE_MPAGE_LEN];
3576 * The first two bytes of def_cache_mpage are a header, so offsets
3577 * in mpage are off by 2 compared to buf. Same for len.
3580 if (len != CACHE_MPAGE_LEN - 2) {
3581 *fp = min(len, CACHE_MPAGE_LEN - 2);
3585 wce = buf[0] & (1 << 2);
3588 * Check that read-only bits are not modified.
3590 ata_msense_caching(dev->id, mpage, false);
3591 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
3594 if (mpage[i + 2] != buf[i]) {
3600 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3601 tf->protocol = ATA_PROT_NODATA;
3603 tf->command = ATA_CMD_SET_FEATURES;
3604 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3609 * ata_mselect_control - Simulate MODE SELECT for control page
3610 * @qc: Storage for translated ATA taskfile
3611 * @buf: input buffer
3612 * @len: number of valid bytes in the input buffer
3613 * @fp: out parameter for the failed field on error
3615 * Prepare a taskfile to modify caching information for the device.
3620 static int ata_mselect_control(struct ata_queued_cmd *qc,
3621 const u8 *buf, int len, u16 *fp)
3623 struct ata_device *dev = qc->dev;
3624 u8 mpage[CONTROL_MPAGE_LEN];
3629 * The first two bytes of def_control_mpage are a header, so offsets
3630 * in mpage are off by 2 compared to buf. Same for len.
3633 if (len != CONTROL_MPAGE_LEN - 2) {
3634 *fp = min(len, CONTROL_MPAGE_LEN - 2);
3638 d_sense = buf[0] & (1 << 2);
3641 * Check that read-only bits are not modified.
3643 ata_msense_control(dev, mpage, false);
3644 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
3647 if (mpage[2 + i] != buf[i]) {
3652 if (d_sense & (1 << 2))
3653 dev->flags |= ATA_DFLAG_D_SENSE;
3655 dev->flags &= ~ATA_DFLAG_D_SENSE;
3660 * ata_scsi_mode_select_xlat - Simulate MODE SELECT 6, 10 commands
3661 * @qc: Storage for translated ATA taskfile
3663 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
3664 * Assume this is invoked for direct access devices (e.g. disks) only.
3665 * There should be no block descriptor for other device types.
3668 * spin_lock_irqsave(host lock)
3670 static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
3672 struct scsi_cmnd *scmd = qc->scsicmd;
3673 const u8 *cdb = scmd->cmnd;
3675 unsigned six_byte, pg_len, hdr_len, bd_len;
3680 const u8 *p = buffer;
3682 six_byte = (cdb[0] == MODE_SELECT);
3684 if (scmd->cmd_len < 5) {
3692 if (scmd->cmd_len < 9) {
3697 len = get_unaligned_be16(&cdb[7]);
3701 /* We only support PF=1, SP=0. */
3702 if ((cdb[1] & 0x11) != 0x10) {
3704 bp = (cdb[1] & 0x01) ? 1 : 5;
3708 /* Test early for possible overrun. */
3709 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
3710 goto invalid_param_len;
3712 /* Move past header and block descriptors. */
3714 goto invalid_param_len;
3716 if (!sg_copy_to_buffer(scsi_sglist(scmd), scsi_sg_count(scmd),
3717 buffer, sizeof(buffer)))
3718 goto invalid_param_len;
3723 bd_len = get_unaligned_be16(&p[6]);
3728 goto invalid_param_len;
3729 if (bd_len != 0 && bd_len != 8) {
3730 fp = (six_byte) ? 3 : 6;
3731 fp += bd_len + hdr_len;
3740 /* Parse both possible formats for the mode page headers. */
3744 goto invalid_param_len;
3747 pg_len = get_unaligned_be16(&p[2]);
3752 goto invalid_param_len;
3761 * No mode subpages supported (yet) but asking for _all_
3762 * subpages may be valid
3764 if (spg && (spg != ALL_SUB_MPAGES)) {
3765 fp = (p[0] & 0x40) ? 1 : 0;
3766 fp += hdr_len + bd_len;
3770 goto invalid_param_len;
3774 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
3775 fp += hdr_len + bd_len;
3780 if (ata_mselect_control(qc, p, pg_len, &fp) < 0) {
3781 fp += hdr_len + bd_len;
3784 goto skip; /* No ATA command to send */
3787 default: /* invalid page code */
3788 fp = bd_len + hdr_len;
3793 * Only one page has changeable data, so we only support setting one
3802 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
3806 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
3810 /* "Parameter list length error" */
3811 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3815 scmd->result = SAM_STAT_GOOD;
3819 static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma)
3822 return ATA_CMD_TRUSTED_NONDATA;
3824 return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND;
3826 return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV;
3829 static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc)
3831 struct scsi_cmnd *scmd = qc->scsicmd;
3832 const u8 *cdb = scmd->cmnd;
3833 struct ata_taskfile *tf = &qc->tf;
3835 bool send = (cdb[0] == SECURITY_PROTOCOL_OUT);
3836 u16 spsp = get_unaligned_be16(&cdb[2]);
3837 u32 len = get_unaligned_be32(&cdb[6]);
3838 bool dma = !(qc->dev->flags & ATA_DFLAG_PIO);
3841 * We don't support the ATA "security" protocol.
3844 ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0);
3848 if (cdb[4] & 7) { /* INC_512 */
3850 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
3854 if (len > 0x01fffe00) {
3855 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
3859 /* convert to the sector-based ATA addressing */
3860 len = (len + 511) / 512;
3863 tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO;
3864 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA;
3866 tf->flags |= ATA_TFLAG_WRITE;
3867 tf->command = ata_scsi_trusted_op(len, send, dma);
3869 tf->lbam = spsp & 0xff;
3870 tf->lbah = spsp >> 8;
3873 tf->nsect = len & 0xff;
3874 tf->lbal = len >> 8;
3877 tf->lbah = (1 << 7);
3880 ata_qc_set_pc_nbytes(qc);
3885 * ata_scsi_var_len_cdb_xlat - SATL variable length CDB to Handler
3886 * @qc: Command to be translated
3888 * Translate a SCSI variable length CDB to specified commands.
3889 * It checks a service action value in CDB to call corresponding handler.
3892 * Zero on success, non-zero on failure
3895 static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc)
3897 struct scsi_cmnd *scmd = qc->scsicmd;
3898 const u8 *cdb = scmd->cmnd;
3899 const u16 sa = get_unaligned_be16(&cdb[8]);
3902 * if service action represents a ata pass-thru(32) command,
3903 * then pass it to ata_scsi_pass_thru handler.
3906 return ata_scsi_pass_thru(qc);
3908 /* unsupported service action */
3913 * ata_get_xlat_func - check if SCSI to ATA translation is possible
3915 * @cmd: SCSI command opcode to consider
3917 * Look up the SCSI command given, and determine whether the
3918 * SCSI command is to be translated or simulated.
3921 * Pointer to translation function if possible, %NULL if not.
3924 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
3934 return ata_scsi_rw_xlat;
3937 return ata_scsi_write_same_xlat;
3939 case SYNCHRONIZE_CACHE:
3940 case SYNCHRONIZE_CACHE_16:
3941 if (ata_try_flush_cache(dev))
3942 return ata_scsi_flush_xlat;
3947 return ata_scsi_verify_xlat;
3951 return ata_scsi_pass_thru;
3953 case VARIABLE_LENGTH_CMD:
3954 return ata_scsi_var_len_cdb_xlat;
3957 case MODE_SELECT_10:
3958 return ata_scsi_mode_select_xlat;
3961 return ata_scsi_zbc_in_xlat;
3964 return ata_scsi_zbc_out_xlat;
3966 case SECURITY_PROTOCOL_IN:
3967 case SECURITY_PROTOCOL_OUT:
3968 if (!(dev->flags & ATA_DFLAG_TRUSTED))
3970 return ata_scsi_security_inout_xlat;
3973 return ata_scsi_start_stop_xlat;
3979 int __ata_scsi_queuecmd(struct scsi_cmnd *scmd, struct ata_device *dev)
3981 struct ata_port *ap = dev->link->ap;
3982 u8 scsi_op = scmd->cmnd[0];
3983 ata_xlat_func_t xlat_func;
3986 * scsi_queue_rq() will defer commands if scsi_host_in_recovery().
3987 * However, this check is done without holding the ap->lock (a libata
3988 * specific lock), so we can have received an error irq since then,
3989 * therefore we must check if EH is pending, while holding ap->lock.
3991 if (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS))
3992 return SCSI_MLQUEUE_DEVICE_BUSY;
3994 if (unlikely(!scmd->cmd_len))
3997 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
3998 if (unlikely(scmd->cmd_len > dev->cdb_len))
4001 xlat_func = ata_get_xlat_func(dev, scsi_op);
4002 } else if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
4003 /* relay SCSI command to ATAPI device */
4004 int len = COMMAND_SIZE(scsi_op);
4006 if (unlikely(len > scmd->cmd_len ||
4007 len > dev->cdb_len ||
4008 scmd->cmd_len > ATAPI_CDB_LEN))
4011 xlat_func = atapi_xlat;
4013 /* ATA_16 passthru, treat as an ATA command */
4014 if (unlikely(scmd->cmd_len > 16))
4017 xlat_func = ata_get_xlat_func(dev, scsi_op);
4021 return ata_scsi_translate(dev, scmd, xlat_func);
4023 ata_scsi_simulate(dev, scmd);
4028 scmd->result = DID_ERROR << 16;
4034 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
4035 * @shost: SCSI host of command to be sent
4036 * @cmd: SCSI command to be sent
4038 * In some cases, this function translates SCSI commands into
4039 * ATA taskfiles, and queues the taskfiles to be sent to
4040 * hardware. In other cases, this function simulates a
4041 * SCSI device by evaluating and responding to certain
4042 * SCSI commands. This creates the overall effect of
4043 * ATA and ATAPI devices appearing as SCSI devices.
4049 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
4052 int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
4054 struct ata_port *ap;
4055 struct ata_device *dev;
4056 struct scsi_device *scsidev = cmd->device;
4058 unsigned long irq_flags;
4060 ap = ata_shost_to_port(shost);
4062 spin_lock_irqsave(ap->lock, irq_flags);
4064 dev = ata_scsi_find_dev(ap, scsidev);
4066 rc = __ata_scsi_queuecmd(cmd, dev);
4068 cmd->result = (DID_BAD_TARGET << 16);
4072 spin_unlock_irqrestore(ap->lock, irq_flags);
4076 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4079 * ata_scsi_simulate - simulate SCSI command on ATA device
4080 * @dev: the target device
4081 * @cmd: SCSI command being sent to device.
4083 * Interprets and directly executes a select list of SCSI commands
4084 * that can be handled internally.
4087 * spin_lock_irqsave(host lock)
4090 void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
4092 struct ata_scsi_args args;
4093 const u8 *scsicmd = cmd->cmnd;
4100 switch(scsicmd[0]) {
4102 if (scsicmd[1] & 2) /* is CmdDt set? */
4103 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4104 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
4105 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
4106 else switch (scsicmd[2]) {
4108 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
4111 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
4114 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
4117 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
4120 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
4123 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
4126 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
4129 if (dev->flags & ATA_DFLAG_ZAC)
4130 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b6);
4132 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
4136 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b9);
4138 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
4141 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
4148 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
4152 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4155 case SERVICE_ACTION_IN_16:
4156 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
4157 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4159 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4163 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
4167 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
4170 /* if we reach this, then writeback caching is disabled,
4171 * turning this into a no-op.
4173 case SYNCHRONIZE_CACHE:
4174 case SYNCHRONIZE_CACHE_16:
4177 /* no-op's, complete with success */
4181 case TEST_UNIT_READY:
4184 case SEND_DIAGNOSTIC:
4185 tmp8 = scsicmd[1] & ~(1 << 3);
4186 if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4])
4187 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4190 case MAINTENANCE_IN:
4191 if (scsicmd[1] == MI_REPORT_SUPPORTED_OPERATION_CODES)
4192 ata_scsi_rbuf_fill(&args, ata_scsiop_maint_in);
4194 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4197 /* all other commands */
4199 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
4200 /* "Invalid command operation code" */
4207 int ata_scsi_add_hosts(struct ata_host *host, const struct scsi_host_template *sht)
4211 for (i = 0; i < host->n_ports; i++) {
4212 struct ata_port *ap = host->ports[i];
4213 struct Scsi_Host *shost;
4216 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
4220 shost->eh_noresume = 1;
4221 *(struct ata_port **)&shost->hostdata[0] = ap;
4222 ap->scsi_host = shost;
4224 shost->transportt = ata_scsi_transport_template;
4225 shost->unique_id = ap->print_id;
4228 shost->max_channel = 1;
4229 shost->max_cmd_len = 32;
4231 /* Schedule policy is determined by ->qc_defer()
4232 * callback and it needs to see every deferred qc.
4233 * Set host_blocked to 1 to prevent SCSI midlayer from
4234 * automatically deferring requests.
4236 shost->max_host_blocked = 1;
4238 rc = scsi_add_host_with_dma(shost, &ap->tdev, ap->host->dev);
4247 struct Scsi_Host *shost = host->ports[i]->scsi_host;
4249 /* scsi_host_put() is in ata_devres_release() */
4250 scsi_remove_host(shost);
4256 static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
4258 struct scsi_device *sdev = dev->sdev;
4259 struct device *d = ap->host->dev;
4260 struct device_node *np = d->of_node;
4261 struct device_node *child;
4263 for_each_available_child_of_node(np, child) {
4267 ret = of_property_read_u32(child, "reg", &val);
4270 if (val == dev->devno) {
4271 dev_dbg(d, "found matching device node\n");
4272 sdev->sdev_gendev.of_node = child;
4278 static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
4283 void ata_scsi_scan_host(struct ata_port *ap, int sync)
4286 struct ata_device *last_failed_dev = NULL;
4287 struct ata_link *link;
4288 struct ata_device *dev;
4291 ata_for_each_link(link, ap, EDGE) {
4292 ata_for_each_dev(dev, link, ENABLED) {
4293 struct scsi_device *sdev;
4294 int channel = 0, id = 0;
4299 if (ata_is_host_link(link))
4302 channel = link->pmp;
4304 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
4306 if (!IS_ERR(sdev)) {
4308 ata_scsi_assign_ofnode(dev, ap);
4309 scsi_device_put(sdev);
4316 /* If we scanned while EH was in progress or allocation
4317 * failure occurred, scan would have failed silently. Check
4318 * whether all devices are attached.
4320 ata_for_each_link(link, ap, EDGE) {
4321 ata_for_each_dev(dev, link, ENABLED) {
4330 /* we're missing some SCSI devices */
4332 /* If caller requested synchrnous scan && we've made
4333 * any progress, sleep briefly and repeat.
4335 if (dev != last_failed_dev) {
4337 last_failed_dev = dev;
4341 /* We might be failing to detect boot device, give it
4342 * a few more chances.
4350 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
4353 queue_delayed_work(system_long_wq, &ap->hotplug_task,
4354 round_jiffies_relative(HZ));
4358 * ata_scsi_offline_dev - offline attached SCSI device
4359 * @dev: ATA device to offline attached SCSI device for
4361 * This function is called from ata_eh_hotplug() and responsible
4362 * for taking the SCSI device attached to @dev offline. This
4363 * function is called with host lock which protects dev->sdev
4367 * spin_lock_irqsave(host lock)
4370 * 1 if attached SCSI device exists, 0 otherwise.
4372 int ata_scsi_offline_dev(struct ata_device *dev)
4375 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
4382 * ata_scsi_remove_dev - remove attached SCSI device
4383 * @dev: ATA device to remove attached SCSI device for
4385 * This function is called from ata_eh_scsi_hotplug() and
4386 * responsible for removing the SCSI device attached to @dev.
4389 * Kernel thread context (may sleep).
4391 static void ata_scsi_remove_dev(struct ata_device *dev)
4393 struct ata_port *ap = dev->link->ap;
4394 struct scsi_device *sdev;
4395 unsigned long flags;
4397 /* Alas, we need to grab scan_mutex to ensure SCSI device
4398 * state doesn't change underneath us and thus
4399 * scsi_device_get() always succeeds. The mutex locking can
4400 * be removed if there is __scsi_device_get() interface which
4401 * increments reference counts regardless of device state.
4403 mutex_lock(&ap->scsi_host->scan_mutex);
4404 spin_lock_irqsave(ap->lock, flags);
4406 /* clearing dev->sdev is protected by host lock */
4411 /* If user initiated unplug races with us, sdev can go
4412 * away underneath us after the host lock and
4413 * scan_mutex are released. Hold onto it.
4415 if (scsi_device_get(sdev) == 0) {
4416 /* The following ensures the attached sdev is
4417 * offline on return from ata_scsi_offline_dev()
4418 * regardless it wins or loses the race
4419 * against this function.
4421 scsi_device_set_state(sdev, SDEV_OFFLINE);
4428 spin_unlock_irqrestore(ap->lock, flags);
4429 mutex_unlock(&ap->scsi_host->scan_mutex);
4432 ata_dev_info(dev, "detaching (SCSI %s)\n",
4433 dev_name(&sdev->sdev_gendev));
4435 scsi_remove_device(sdev);
4436 scsi_device_put(sdev);
4440 static void ata_scsi_handle_link_detach(struct ata_link *link)
4442 struct ata_port *ap = link->ap;
4443 struct ata_device *dev;
4445 ata_for_each_dev(dev, link, ALL) {
4446 unsigned long flags;
4448 if (!(dev->flags & ATA_DFLAG_DETACHED))
4451 spin_lock_irqsave(ap->lock, flags);
4452 dev->flags &= ~ATA_DFLAG_DETACHED;
4453 spin_unlock_irqrestore(ap->lock, flags);
4455 if (zpodd_dev_enabled(dev))
4458 ata_scsi_remove_dev(dev);
4463 * ata_scsi_media_change_notify - send media change event
4464 * @dev: Pointer to the disk device with media change event
4466 * Tell the block layer to send a media change notification
4470 * spin_lock_irqsave(host lock)
4472 void ata_scsi_media_change_notify(struct ata_device *dev)
4475 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
4480 * ata_scsi_hotplug - SCSI part of hotplug
4481 * @work: Pointer to ATA port to perform SCSI hotplug on
4483 * Perform SCSI part of hotplug. It's executed from a separate
4484 * workqueue after EH completes. This is necessary because SCSI
4485 * hot plugging requires working EH and hot unplugging is
4486 * synchronized with hot plugging with a mutex.
4489 * Kernel thread context (may sleep).
4491 void ata_scsi_hotplug(struct work_struct *work)
4493 struct ata_port *ap =
4494 container_of(work, struct ata_port, hotplug_task.work);
4497 if (ap->pflags & ATA_PFLAG_UNLOADING)
4500 mutex_lock(&ap->scsi_scan_mutex);
4502 /* Unplug detached devices. We cannot use link iterator here
4503 * because PMP links have to be scanned even if PMP is
4504 * currently not attached. Iterate manually.
4506 ata_scsi_handle_link_detach(&ap->link);
4508 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
4509 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
4511 /* scan for new ones */
4512 ata_scsi_scan_host(ap, 0);
4514 mutex_unlock(&ap->scsi_scan_mutex);
4518 * ata_scsi_user_scan - indication for user-initiated bus scan
4519 * @shost: SCSI host to scan
4520 * @channel: Channel to scan
4524 * This function is called when user explicitly requests bus
4525 * scan. Set probe pending flag and invoke EH.
4528 * SCSI layer (we don't care)
4533 int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
4534 unsigned int id, u64 lun)
4536 struct ata_port *ap = ata_shost_to_port(shost);
4537 unsigned long flags;
4540 if (!ap->ops->error_handler)
4543 if (lun != SCAN_WILD_CARD && lun)
4546 if (!sata_pmp_attached(ap)) {
4547 if (channel != SCAN_WILD_CARD && channel)
4551 if (id != SCAN_WILD_CARD && id)
4556 spin_lock_irqsave(ap->lock, flags);
4558 if (devno == SCAN_WILD_CARD) {
4559 struct ata_link *link;
4561 ata_for_each_link(link, ap, EDGE) {
4562 struct ata_eh_info *ehi = &link->eh_info;
4563 ehi->probe_mask |= ATA_ALL_DEVICES;
4564 ehi->action |= ATA_EH_RESET;
4567 struct ata_device *dev = ata_find_dev(ap, devno);
4570 struct ata_eh_info *ehi = &dev->link->eh_info;
4571 ehi->probe_mask |= 1 << dev->devno;
4572 ehi->action |= ATA_EH_RESET;
4578 ata_port_schedule_eh(ap);
4579 spin_unlock_irqrestore(ap->lock, flags);
4580 ata_port_wait_eh(ap);
4582 spin_unlock_irqrestore(ap->lock, flags);
4588 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
4589 * @work: Pointer to ATA port to perform scsi_rescan_device()
4591 * After ATA pass thru (SAT) commands are executed successfully,
4592 * libata need to propagate the changes to SCSI layer.
4595 * Kernel thread context (may sleep).
4597 void ata_scsi_dev_rescan(struct work_struct *work)
4599 struct ata_port *ap =
4600 container_of(work, struct ata_port, scsi_rescan_task);
4601 struct ata_link *link;
4602 struct ata_device *dev;
4603 unsigned long flags;
4605 mutex_lock(&ap->scsi_scan_mutex);
4606 spin_lock_irqsave(ap->lock, flags);
4608 ata_for_each_link(link, ap, EDGE) {
4609 ata_for_each_dev(dev, link, ENABLED) {
4610 struct scsi_device *sdev = dev->sdev;
4614 if (scsi_device_get(sdev))
4617 spin_unlock_irqrestore(ap->lock, flags);
4618 scsi_rescan_device(&(sdev->sdev_gendev));
4619 scsi_device_put(sdev);
4620 spin_lock_irqsave(ap->lock, flags);
4624 spin_unlock_irqrestore(ap->lock, flags);
4625 mutex_unlock(&ap->scsi_scan_mutex);