ASoC: Ensure we delay long enough for WM8994 FLL to lock
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ata / libata-scsi.c
blobca972bc3e3c746550c050d351e28d0ea618d904e
1 /*
2 * libata-scsi.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from
31 * - http://www.t10.org/
32 * - http://www.t13.org/
36 #include <linux/slab.h>
37 #include <linux/kernel.h>
38 #include <linux/blkdev.h>
39 #include <linux/spinlock.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_host.h>
42 #include <scsi/scsi_cmnd.h>
43 #include <scsi/scsi_eh.h>
44 #include <scsi/scsi_device.h>
45 #include <scsi/scsi_tcq.h>
46 #include <scsi/scsi_transport.h>
47 #include <linux/libata.h>
48 #include <linux/hdreg.h>
49 #include <linux/uaccess.h>
50 #include <linux/suspend.h>
51 #include <asm/unaligned.h>
53 #include "libata.h"
55 #define SECTOR_SIZE 512
56 #define ATA_SCSI_RBUF_SIZE 4096
58 static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
59 static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
61 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
63 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
64 const struct scsi_device *scsidev);
65 static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
66 const struct scsi_device *scsidev);
67 static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
68 unsigned int id, unsigned int lun);
71 #define RW_RECOVERY_MPAGE 0x1
72 #define RW_RECOVERY_MPAGE_LEN 12
73 #define CACHE_MPAGE 0x8
74 #define CACHE_MPAGE_LEN 20
75 #define CONTROL_MPAGE 0xa
76 #define CONTROL_MPAGE_LEN 12
77 #define ALL_MPAGES 0x3f
78 #define ALL_SUB_MPAGES 0xff
81 static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
82 RW_RECOVERY_MPAGE,
83 RW_RECOVERY_MPAGE_LEN - 2,
84 (1 << 7), /* AWRE */
85 0, /* read retry count */
86 0, 0, 0, 0,
87 0, /* write retry count */
88 0, 0, 0
91 static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
92 CACHE_MPAGE,
93 CACHE_MPAGE_LEN - 2,
94 0, /* contains WCE, needs to be 0 for logic */
95 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 0, /* contains DRA, needs to be 0 for logic */
97 0, 0, 0, 0, 0, 0, 0
100 static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
101 CONTROL_MPAGE,
102 CONTROL_MPAGE_LEN - 2,
103 2, /* DSENSE=0, GLTSD=1 */
104 0, /* [QAM+QERR may be 1, see 05-359r1] */
105 0, 0, 0, 0, 0xff, 0xff,
106 0, 30 /* extended self test time, see 05-359r1 */
110 * libata transport template. libata doesn't do real transport stuff.
111 * It just needs the eh_timed_out hook.
113 static struct scsi_transport_template ata_scsi_transport_template = {
114 .eh_strategy_handler = ata_scsi_error,
115 .eh_timed_out = ata_scsi_timed_out,
116 .user_scan = ata_scsi_user_scan,
120 static const struct {
121 enum link_pm value;
122 const char *name;
123 } link_pm_policy[] = {
124 { NOT_AVAILABLE, "max_performance" },
125 { MIN_POWER, "min_power" },
126 { MAX_PERFORMANCE, "max_performance" },
127 { MEDIUM_POWER, "medium_power" },
130 static const char *ata_scsi_lpm_get(enum link_pm policy)
132 int i;
134 for (i = 0; i < ARRAY_SIZE(link_pm_policy); i++)
135 if (link_pm_policy[i].value == policy)
136 return link_pm_policy[i].name;
138 return NULL;
141 static ssize_t ata_scsi_lpm_put(struct device *dev,
142 struct device_attribute *attr,
143 const char *buf, size_t count)
145 struct Scsi_Host *shost = class_to_shost(dev);
146 struct ata_port *ap = ata_shost_to_port(shost);
147 enum link_pm policy = 0;
148 int i;
151 * we are skipping array location 0 on purpose - this
152 * is because a value of NOT_AVAILABLE is displayed
153 * to the user as max_performance, but when the user
154 * writes "max_performance", they actually want the
155 * value to match MAX_PERFORMANCE.
157 for (i = 1; i < ARRAY_SIZE(link_pm_policy); i++) {
158 const int len = strlen(link_pm_policy[i].name);
159 if (strncmp(link_pm_policy[i].name, buf, len) == 0) {
160 policy = link_pm_policy[i].value;
161 break;
164 if (!policy)
165 return -EINVAL;
167 ata_lpm_schedule(ap, policy);
168 return count;
171 static ssize_t
172 ata_scsi_lpm_show(struct device *dev, struct device_attribute *attr, char *buf)
174 struct Scsi_Host *shost = class_to_shost(dev);
175 struct ata_port *ap = ata_shost_to_port(shost);
176 const char *policy =
177 ata_scsi_lpm_get(ap->pm_policy);
179 if (!policy)
180 return -EINVAL;
182 return snprintf(buf, 23, "%s\n", policy);
184 DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
185 ata_scsi_lpm_show, ata_scsi_lpm_put);
186 EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
188 static ssize_t ata_scsi_park_show(struct device *device,
189 struct device_attribute *attr, char *buf)
191 struct scsi_device *sdev = to_scsi_device(device);
192 struct ata_port *ap;
193 struct ata_link *link;
194 struct ata_device *dev;
195 unsigned long flags, now;
196 unsigned int uninitialized_var(msecs);
197 int rc = 0;
199 ap = ata_shost_to_port(sdev->host);
201 spin_lock_irqsave(ap->lock, flags);
202 dev = ata_scsi_find_dev(ap, sdev);
203 if (!dev) {
204 rc = -ENODEV;
205 goto unlock;
207 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
208 rc = -EOPNOTSUPP;
209 goto unlock;
212 link = dev->link;
213 now = jiffies;
214 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
215 link->eh_context.unloaded_mask & (1 << dev->devno) &&
216 time_after(dev->unpark_deadline, now))
217 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
218 else
219 msecs = 0;
221 unlock:
222 spin_unlock_irq(ap->lock);
224 return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
227 static ssize_t ata_scsi_park_store(struct device *device,
228 struct device_attribute *attr,
229 const char *buf, size_t len)
231 struct scsi_device *sdev = to_scsi_device(device);
232 struct ata_port *ap;
233 struct ata_device *dev;
234 long int input;
235 unsigned long flags;
236 int rc;
238 rc = strict_strtol(buf, 10, &input);
239 if (rc || input < -2)
240 return -EINVAL;
241 if (input > ATA_TMOUT_MAX_PARK) {
242 rc = -EOVERFLOW;
243 input = ATA_TMOUT_MAX_PARK;
246 ap = ata_shost_to_port(sdev->host);
248 spin_lock_irqsave(ap->lock, flags);
249 dev = ata_scsi_find_dev(ap, sdev);
250 if (unlikely(!dev)) {
251 rc = -ENODEV;
252 goto unlock;
254 if (dev->class != ATA_DEV_ATA) {
255 rc = -EOPNOTSUPP;
256 goto unlock;
259 if (input >= 0) {
260 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
261 rc = -EOPNOTSUPP;
262 goto unlock;
265 dev->unpark_deadline = ata_deadline(jiffies, input);
266 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
267 ata_port_schedule_eh(ap);
268 complete(&ap->park_req_pending);
269 } else {
270 switch (input) {
271 case -1:
272 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
273 break;
274 case -2:
275 dev->flags |= ATA_DFLAG_NO_UNLOAD;
276 break;
279 unlock:
280 spin_unlock_irqrestore(ap->lock, flags);
282 return rc ? rc : len;
284 DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
285 ata_scsi_park_show, ata_scsi_park_store);
286 EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
288 static void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
290 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
292 scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq);
295 static ssize_t
296 ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
297 const char *buf, size_t count)
299 struct Scsi_Host *shost = class_to_shost(dev);
300 struct ata_port *ap = ata_shost_to_port(shost);
301 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
302 return ap->ops->em_store(ap, buf, count);
303 return -EINVAL;
306 static ssize_t
307 ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
308 char *buf)
310 struct Scsi_Host *shost = class_to_shost(dev);
311 struct ata_port *ap = ata_shost_to_port(shost);
313 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
314 return ap->ops->em_show(ap, buf);
315 return -EINVAL;
317 DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
318 ata_scsi_em_message_show, ata_scsi_em_message_store);
319 EXPORT_SYMBOL_GPL(dev_attr_em_message);
321 static ssize_t
322 ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
323 char *buf)
325 struct Scsi_Host *shost = class_to_shost(dev);
326 struct ata_port *ap = ata_shost_to_port(shost);
328 return snprintf(buf, 23, "%d\n", ap->em_message_type);
330 DEVICE_ATTR(em_message_type, S_IRUGO,
331 ata_scsi_em_message_type_show, NULL);
332 EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
334 static ssize_t
335 ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
336 char *buf)
338 struct scsi_device *sdev = to_scsi_device(dev);
339 struct ata_port *ap = ata_shost_to_port(sdev->host);
340 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
342 if (ap->ops->sw_activity_show && (ap->flags & ATA_FLAG_SW_ACTIVITY))
343 return ap->ops->sw_activity_show(atadev, buf);
344 return -EINVAL;
347 static ssize_t
348 ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
349 const char *buf, size_t count)
351 struct scsi_device *sdev = to_scsi_device(dev);
352 struct ata_port *ap = ata_shost_to_port(sdev->host);
353 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
354 enum sw_activity val;
355 int rc;
357 if (ap->ops->sw_activity_store && (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
358 val = simple_strtoul(buf, NULL, 0);
359 switch (val) {
360 case OFF: case BLINK_ON: case BLINK_OFF:
361 rc = ap->ops->sw_activity_store(atadev, val);
362 if (!rc)
363 return count;
364 else
365 return rc;
368 return -EINVAL;
370 DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
371 ata_scsi_activity_store);
372 EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
374 struct device_attribute *ata_common_sdev_attrs[] = {
375 &dev_attr_unload_heads,
376 NULL
378 EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
380 static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
381 void (*done)(struct scsi_cmnd *))
383 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
384 /* "Invalid field in cbd" */
385 done(cmd);
389 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
390 * @sdev: SCSI device for which BIOS geometry is to be determined
391 * @bdev: block device associated with @sdev
392 * @capacity: capacity of SCSI device
393 * @geom: location to which geometry will be output
395 * Generic bios head/sector/cylinder calculator
396 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
397 * mapping. Some situations may arise where the disk is not
398 * bootable if this is not used.
400 * LOCKING:
401 * Defined by the SCSI layer. We don't really care.
403 * RETURNS:
404 * Zero.
406 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
407 sector_t capacity, int geom[])
409 geom[0] = 255;
410 geom[1] = 63;
411 sector_div(capacity, 255*63);
412 geom[2] = capacity;
414 return 0;
418 * ata_scsi_unlock_native_capacity - unlock native capacity
419 * @sdev: SCSI device to adjust device capacity for
421 * This function is called if a partition on @sdev extends beyond
422 * the end of the device. It requests EH to unlock HPA.
424 * LOCKING:
425 * Defined by the SCSI layer. Might sleep.
427 void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
429 struct ata_port *ap = ata_shost_to_port(sdev->host);
430 struct ata_device *dev;
431 unsigned long flags;
433 spin_lock_irqsave(ap->lock, flags);
435 dev = ata_scsi_find_dev(ap, sdev);
436 if (dev && dev->n_sectors < dev->n_native_sectors) {
437 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
438 dev->link->eh_info.action |= ATA_EH_RESET;
439 ata_port_schedule_eh(ap);
442 spin_unlock_irqrestore(ap->lock, flags);
443 ata_port_wait_eh(ap);
447 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
448 * @ap: target port
449 * @sdev: SCSI device to get identify data for
450 * @arg: User buffer area for identify data
452 * LOCKING:
453 * Defined by the SCSI layer. We don't really care.
455 * RETURNS:
456 * Zero on success, negative errno on error.
458 static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
459 void __user *arg)
461 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
462 u16 __user *dst = arg;
463 char buf[40];
465 if (!dev)
466 return -ENOMSG;
468 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
469 return -EFAULT;
471 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
472 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
473 return -EFAULT;
475 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
476 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
477 return -EFAULT;
479 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
480 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
481 return -EFAULT;
483 return 0;
487 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
488 * @scsidev: Device to which we are issuing command
489 * @arg: User provided data for issuing command
491 * LOCKING:
492 * Defined by the SCSI layer. We don't really care.
494 * RETURNS:
495 * Zero on success, negative errno on error.
497 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
499 int rc = 0;
500 u8 scsi_cmd[MAX_COMMAND_SIZE];
501 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
502 int argsize = 0;
503 enum dma_data_direction data_dir;
504 int cmd_result;
506 if (arg == NULL)
507 return -EINVAL;
509 if (copy_from_user(args, arg, sizeof(args)))
510 return -EFAULT;
512 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
513 if (!sensebuf)
514 return -ENOMEM;
516 memset(scsi_cmd, 0, sizeof(scsi_cmd));
518 if (args[3]) {
519 argsize = SECTOR_SIZE * args[3];
520 argbuf = kmalloc(argsize, GFP_KERNEL);
521 if (argbuf == NULL) {
522 rc = -ENOMEM;
523 goto error;
526 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
527 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
528 block count in sector count field */
529 data_dir = DMA_FROM_DEVICE;
530 } else {
531 scsi_cmd[1] = (3 << 1); /* Non-data */
532 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
533 data_dir = DMA_NONE;
536 scsi_cmd[0] = ATA_16;
538 scsi_cmd[4] = args[2];
539 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
540 scsi_cmd[6] = args[3];
541 scsi_cmd[8] = args[1];
542 scsi_cmd[10] = 0x4f;
543 scsi_cmd[12] = 0xc2;
544 } else {
545 scsi_cmd[6] = args[1];
547 scsi_cmd[14] = args[0];
549 /* Good values for timeout and retries? Values below
550 from scsi_ioctl_send_command() for default case... */
551 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
552 sensebuf, (10*HZ), 5, 0, NULL);
554 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
555 u8 *desc = sensebuf + 8;
556 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
558 /* If we set cc then ATA pass-through will cause a
559 * check condition even if no error. Filter that. */
560 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
561 struct scsi_sense_hdr sshdr;
562 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
563 &sshdr);
564 if (sshdr.sense_key == 0 &&
565 sshdr.asc == 0 && sshdr.ascq == 0)
566 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
569 /* Send userspace a few ATA registers (same as drivers/ide) */
570 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
571 desc[0] == 0x09) { /* code is "ATA Descriptor" */
572 args[0] = desc[13]; /* status */
573 args[1] = desc[3]; /* error */
574 args[2] = desc[5]; /* sector count (0:7) */
575 if (copy_to_user(arg, args, sizeof(args)))
576 rc = -EFAULT;
581 if (cmd_result) {
582 rc = -EIO;
583 goto error;
586 if ((argbuf)
587 && copy_to_user(arg + sizeof(args), argbuf, argsize))
588 rc = -EFAULT;
589 error:
590 kfree(sensebuf);
591 kfree(argbuf);
592 return rc;
596 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
597 * @scsidev: Device to which we are issuing command
598 * @arg: User provided data for issuing command
600 * LOCKING:
601 * Defined by the SCSI layer. We don't really care.
603 * RETURNS:
604 * Zero on success, negative errno on error.
606 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
608 int rc = 0;
609 u8 scsi_cmd[MAX_COMMAND_SIZE];
610 u8 args[7], *sensebuf = NULL;
611 int cmd_result;
613 if (arg == NULL)
614 return -EINVAL;
616 if (copy_from_user(args, arg, sizeof(args)))
617 return -EFAULT;
619 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
620 if (!sensebuf)
621 return -ENOMEM;
623 memset(scsi_cmd, 0, sizeof(scsi_cmd));
624 scsi_cmd[0] = ATA_16;
625 scsi_cmd[1] = (3 << 1); /* Non-data */
626 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
627 scsi_cmd[4] = args[1];
628 scsi_cmd[6] = args[2];
629 scsi_cmd[8] = args[3];
630 scsi_cmd[10] = args[4];
631 scsi_cmd[12] = args[5];
632 scsi_cmd[13] = args[6] & 0x4f;
633 scsi_cmd[14] = args[0];
635 /* Good values for timeout and retries? Values below
636 from scsi_ioctl_send_command() for default case... */
637 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
638 sensebuf, (10*HZ), 5, 0, NULL);
640 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
641 u8 *desc = sensebuf + 8;
642 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
644 /* If we set cc then ATA pass-through will cause a
645 * check condition even if no error. Filter that. */
646 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
647 struct scsi_sense_hdr sshdr;
648 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
649 &sshdr);
650 if (sshdr.sense_key == 0 &&
651 sshdr.asc == 0 && sshdr.ascq == 0)
652 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
655 /* Send userspace ATA registers */
656 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
657 desc[0] == 0x09) {/* code is "ATA Descriptor" */
658 args[0] = desc[13]; /* status */
659 args[1] = desc[3]; /* error */
660 args[2] = desc[5]; /* sector count (0:7) */
661 args[3] = desc[7]; /* lbal */
662 args[4] = desc[9]; /* lbam */
663 args[5] = desc[11]; /* lbah */
664 args[6] = desc[12]; /* select */
665 if (copy_to_user(arg, args, sizeof(args)))
666 rc = -EFAULT;
670 if (cmd_result) {
671 rc = -EIO;
672 goto error;
675 error:
676 kfree(sensebuf);
677 return rc;
680 static int ata_ioc32(struct ata_port *ap)
682 if (ap->flags & ATA_FLAG_PIO_DMA)
683 return 1;
684 if (ap->pflags & ATA_PFLAG_PIO32)
685 return 1;
686 return 0;
689 int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
690 int cmd, void __user *arg)
692 int val = -EINVAL, rc = -EINVAL;
693 unsigned long flags;
695 switch (cmd) {
696 case ATA_IOC_GET_IO32:
697 spin_lock_irqsave(ap->lock, flags);
698 val = ata_ioc32(ap);
699 spin_unlock_irqrestore(ap->lock, flags);
700 if (copy_to_user(arg, &val, 1))
701 return -EFAULT;
702 return 0;
704 case ATA_IOC_SET_IO32:
705 val = (unsigned long) arg;
706 rc = 0;
707 spin_lock_irqsave(ap->lock, flags);
708 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
709 if (val)
710 ap->pflags |= ATA_PFLAG_PIO32;
711 else
712 ap->pflags &= ~ATA_PFLAG_PIO32;
713 } else {
714 if (val != ata_ioc32(ap))
715 rc = -EINVAL;
717 spin_unlock_irqrestore(ap->lock, flags);
718 return rc;
720 case HDIO_GET_IDENTITY:
721 return ata_get_identity(ap, scsidev, arg);
723 case HDIO_DRIVE_CMD:
724 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
725 return -EACCES;
726 return ata_cmd_ioctl(scsidev, arg);
728 case HDIO_DRIVE_TASK:
729 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
730 return -EACCES;
731 return ata_task_ioctl(scsidev, arg);
733 default:
734 rc = -ENOTTY;
735 break;
738 return rc;
740 EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
742 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
744 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
745 scsidev, cmd, arg);
747 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
750 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
751 * @dev: ATA device to which the new command is attached
752 * @cmd: SCSI command that originated this ATA command
753 * @done: SCSI command completion function
755 * Obtain a reference to an unused ata_queued_cmd structure,
756 * which is the basic libata structure representing a single
757 * ATA command sent to the hardware.
759 * If a command was available, fill in the SCSI-specific
760 * portions of the structure with information on the
761 * current command.
763 * LOCKING:
764 * spin_lock_irqsave(host lock)
766 * RETURNS:
767 * Command allocated, or %NULL if none available.
769 static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
770 struct scsi_cmnd *cmd,
771 void (*done)(struct scsi_cmnd *))
773 struct ata_queued_cmd *qc;
775 qc = ata_qc_new_init(dev);
776 if (qc) {
777 qc->scsicmd = cmd;
778 qc->scsidone = done;
780 qc->sg = scsi_sglist(cmd);
781 qc->n_elem = scsi_sg_count(cmd);
782 } else {
783 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
784 done(cmd);
787 return qc;
790 static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
792 struct scsi_cmnd *scmd = qc->scsicmd;
794 qc->extrabytes = scmd->request->extra_len;
795 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
799 * ata_dump_status - user friendly display of error info
800 * @id: id of the port in question
801 * @tf: ptr to filled out taskfile
803 * Decode and dump the ATA error/status registers for the user so
804 * that they have some idea what really happened at the non
805 * make-believe layer.
807 * LOCKING:
808 * inherited from caller
810 static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
812 u8 stat = tf->command, err = tf->feature;
814 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
815 if (stat & ATA_BUSY) {
816 printk("Busy }\n"); /* Data is not valid in this case */
817 } else {
818 if (stat & 0x40) printk("DriveReady ");
819 if (stat & 0x20) printk("DeviceFault ");
820 if (stat & 0x10) printk("SeekComplete ");
821 if (stat & 0x08) printk("DataRequest ");
822 if (stat & 0x04) printk("CorrectedError ");
823 if (stat & 0x02) printk("Index ");
824 if (stat & 0x01) printk("Error ");
825 printk("}\n");
827 if (err) {
828 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
829 if (err & 0x04) printk("DriveStatusError ");
830 if (err & 0x80) {
831 if (err & 0x04) printk("BadCRC ");
832 else printk("Sector ");
834 if (err & 0x40) printk("UncorrectableError ");
835 if (err & 0x10) printk("SectorIdNotFound ");
836 if (err & 0x02) printk("TrackZeroNotFound ");
837 if (err & 0x01) printk("AddrMarkNotFound ");
838 printk("}\n");
844 * ata_to_sense_error - convert ATA error to SCSI error
845 * @id: ATA device number
846 * @drv_stat: value contained in ATA status register
847 * @drv_err: value contained in ATA error register
848 * @sk: the sense key we'll fill out
849 * @asc: the additional sense code we'll fill out
850 * @ascq: the additional sense code qualifier we'll fill out
851 * @verbose: be verbose
853 * Converts an ATA error into a SCSI error. Fill out pointers to
854 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
855 * format sense blocks.
857 * LOCKING:
858 * spin_lock_irqsave(host lock)
860 static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
861 u8 *asc, u8 *ascq, int verbose)
863 int i;
865 /* Based on the 3ware driver translation table */
866 static const unsigned char sense_table[][4] = {
867 /* BBD|ECC|ID|MAR */
868 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
869 /* BBD|ECC|ID */
870 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
871 /* ECC|MC|MARK */
872 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
873 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
874 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
875 /* MC|ID|ABRT|TRK0|MARK */
876 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
877 /* MCR|MARK */
878 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
879 /* Bad address mark */
880 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
881 /* TRK0 */
882 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
883 /* Abort & !ICRC */
884 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
885 /* Media change request */
886 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
887 /* SRV */
888 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
889 /* Media change */
890 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
891 /* ECC */
892 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
893 /* BBD - block marked bad */
894 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
895 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
897 static const unsigned char stat_table[][4] = {
898 /* Must be first because BUSY means no other bits valid */
899 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
900 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
901 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
902 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
903 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
907 * Is this an error we can process/parse
909 if (drv_stat & ATA_BUSY) {
910 drv_err = 0; /* Ignore the err bits, they're invalid */
913 if (drv_err) {
914 /* Look for drv_err */
915 for (i = 0; sense_table[i][0] != 0xFF; i++) {
916 /* Look for best matches first */
917 if ((sense_table[i][0] & drv_err) ==
918 sense_table[i][0]) {
919 *sk = sense_table[i][1];
920 *asc = sense_table[i][2];
921 *ascq = sense_table[i][3];
922 goto translate_done;
925 /* No immediate match */
926 if (verbose)
927 printk(KERN_WARNING "ata%u: no sense translation for "
928 "error 0x%02x\n", id, drv_err);
931 /* Fall back to interpreting status bits */
932 for (i = 0; stat_table[i][0] != 0xFF; i++) {
933 if (stat_table[i][0] & drv_stat) {
934 *sk = stat_table[i][1];
935 *asc = stat_table[i][2];
936 *ascq = stat_table[i][3];
937 goto translate_done;
940 /* No error? Undecoded? */
941 if (verbose)
942 printk(KERN_WARNING "ata%u: no sense translation for "
943 "status: 0x%02x\n", id, drv_stat);
945 /* We need a sensible error return here, which is tricky, and one
946 that won't cause people to do things like return a disk wrongly */
947 *sk = ABORTED_COMMAND;
948 *asc = 0x00;
949 *ascq = 0x00;
951 translate_done:
952 if (verbose)
953 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
954 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
955 id, drv_stat, drv_err, *sk, *asc, *ascq);
956 return;
960 * ata_gen_passthru_sense - Generate check condition sense block.
961 * @qc: Command that completed.
963 * This function is specific to the ATA descriptor format sense
964 * block specified for the ATA pass through commands. Regardless
965 * of whether the command errored or not, return a sense
966 * block. Copy all controller registers into the sense
967 * block. Clear sense key, ASC & ASCQ if there is no error.
969 * LOCKING:
970 * None.
972 static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
974 struct scsi_cmnd *cmd = qc->scsicmd;
975 struct ata_taskfile *tf = &qc->result_tf;
976 unsigned char *sb = cmd->sense_buffer;
977 unsigned char *desc = sb + 8;
978 int verbose = qc->ap->ops->error_handler == NULL;
980 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
982 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
985 * Use ata_to_sense_error() to map status register bits
986 * onto sense key, asc & ascq.
988 if (qc->err_mask ||
989 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
990 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
991 &sb[1], &sb[2], &sb[3], verbose);
992 sb[1] &= 0x0f;
996 * Sense data is current and format is descriptor.
998 sb[0] = 0x72;
1000 desc[0] = 0x09;
1002 /* set length of additional sense data */
1003 sb[7] = 14;
1004 desc[1] = 12;
1007 * Copy registers into sense buffer.
1009 desc[2] = 0x00;
1010 desc[3] = tf->feature; /* == error reg */
1011 desc[5] = tf->nsect;
1012 desc[7] = tf->lbal;
1013 desc[9] = tf->lbam;
1014 desc[11] = tf->lbah;
1015 desc[12] = tf->device;
1016 desc[13] = tf->command; /* == status reg */
1019 * Fill in Extend bit, and the high order bytes
1020 * if applicable.
1022 if (tf->flags & ATA_TFLAG_LBA48) {
1023 desc[2] |= 0x01;
1024 desc[4] = tf->hob_nsect;
1025 desc[6] = tf->hob_lbal;
1026 desc[8] = tf->hob_lbam;
1027 desc[10] = tf->hob_lbah;
1032 * ata_gen_ata_sense - generate a SCSI fixed sense block
1033 * @qc: Command that we are erroring out
1035 * Generate sense block for a failed ATA command @qc. Descriptor
1036 * format is used to accomodate LBA48 block address.
1038 * LOCKING:
1039 * None.
1041 static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
1043 struct ata_device *dev = qc->dev;
1044 struct scsi_cmnd *cmd = qc->scsicmd;
1045 struct ata_taskfile *tf = &qc->result_tf;
1046 unsigned char *sb = cmd->sense_buffer;
1047 unsigned char *desc = sb + 8;
1048 int verbose = qc->ap->ops->error_handler == NULL;
1049 u64 block;
1051 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1053 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1055 /* sense data is current and format is descriptor */
1056 sb[0] = 0x72;
1058 /* Use ata_to_sense_error() to map status register bits
1059 * onto sense key, asc & ascq.
1061 if (qc->err_mask ||
1062 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
1063 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
1064 &sb[1], &sb[2], &sb[3], verbose);
1065 sb[1] &= 0x0f;
1068 block = ata_tf_read_block(&qc->result_tf, dev);
1070 /* information sense data descriptor */
1071 sb[7] = 12;
1072 desc[0] = 0x00;
1073 desc[1] = 10;
1075 desc[2] |= 0x80; /* valid */
1076 desc[6] = block >> 40;
1077 desc[7] = block >> 32;
1078 desc[8] = block >> 24;
1079 desc[9] = block >> 16;
1080 desc[10] = block >> 8;
1081 desc[11] = block;
1084 static void ata_scsi_sdev_config(struct scsi_device *sdev)
1086 sdev->use_10_for_rw = 1;
1087 sdev->use_10_for_ms = 1;
1089 /* Schedule policy is determined by ->qc_defer() callback and
1090 * it needs to see every deferred qc. Set dev_blocked to 1 to
1091 * prevent SCSI midlayer from automatically deferring
1092 * requests.
1094 sdev->max_device_blocked = 1;
1098 * atapi_drain_needed - Check whether data transfer may overflow
1099 * @rq: request to be checked
1101 * ATAPI commands which transfer variable length data to host
1102 * might overflow due to application error or hardare bug. This
1103 * function checks whether overflow should be drained and ignored
1104 * for @request.
1106 * LOCKING:
1107 * None.
1109 * RETURNS:
1110 * 1 if ; otherwise, 0.
1112 static int atapi_drain_needed(struct request *rq)
1114 if (likely(!blk_pc_request(rq)))
1115 return 0;
1117 if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_RW))
1118 return 0;
1120 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
1123 static int ata_scsi_dev_config(struct scsi_device *sdev,
1124 struct ata_device *dev)
1126 if (!ata_id_has_unload(dev->id))
1127 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1129 /* configure max sectors */
1130 blk_queue_max_hw_sectors(sdev->request_queue, dev->max_sectors);
1132 sdev->sector_size = ATA_SECT_SIZE;
1134 if (dev->class == ATA_DEV_ATAPI) {
1135 struct request_queue *q = sdev->request_queue;
1136 void *buf;
1138 /* set DMA padding */
1139 blk_queue_update_dma_pad(sdev->request_queue,
1140 ATA_DMA_PAD_SZ - 1);
1142 /* configure draining */
1143 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1144 if (!buf) {
1145 ata_dev_printk(dev, KERN_ERR,
1146 "drain buffer allocation failed\n");
1147 return -ENOMEM;
1150 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1151 } else {
1152 sdev->manage_start_stop = 1;
1156 * ata_pio_sectors() expects buffer for each sector to not cross
1157 * page boundary. Enforce it by requiring buffers to be sector
1158 * aligned, which works iff sector_size is not larger than
1159 * PAGE_SIZE. ATAPI devices also need the alignment as
1160 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1162 if (sdev->sector_size > PAGE_SIZE)
1163 ata_dev_printk(dev, KERN_WARNING,
1164 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1165 sdev->sector_size);
1167 blk_queue_update_dma_alignment(sdev->request_queue,
1168 sdev->sector_size - 1);
1170 if (dev->flags & ATA_DFLAG_AN)
1171 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1173 if (dev->flags & ATA_DFLAG_NCQ) {
1174 int depth;
1176 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1177 depth = min(ATA_MAX_QUEUE - 1, depth);
1178 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
1181 return 0;
1185 * ata_scsi_slave_config - Set SCSI device attributes
1186 * @sdev: SCSI device to examine
1188 * This is called before we actually start reading
1189 * and writing to the device, to configure certain
1190 * SCSI mid-layer behaviors.
1192 * LOCKING:
1193 * Defined by SCSI layer. We don't really care.
1196 int ata_scsi_slave_config(struct scsi_device *sdev)
1198 struct ata_port *ap = ata_shost_to_port(sdev->host);
1199 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
1200 int rc = 0;
1202 ata_scsi_sdev_config(sdev);
1204 if (dev)
1205 rc = ata_scsi_dev_config(sdev, dev);
1207 return rc;
1211 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1212 * @sdev: SCSI device to be destroyed
1214 * @sdev is about to be destroyed for hot/warm unplugging. If
1215 * this unplugging was initiated by libata as indicated by NULL
1216 * dev->sdev, this function doesn't have to do anything.
1217 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1218 * Clear dev->sdev, schedule the device for ATA detach and invoke
1219 * EH.
1221 * LOCKING:
1222 * Defined by SCSI layer. We don't really care.
1224 void ata_scsi_slave_destroy(struct scsi_device *sdev)
1226 struct ata_port *ap = ata_shost_to_port(sdev->host);
1227 struct request_queue *q = sdev->request_queue;
1228 unsigned long flags;
1229 struct ata_device *dev;
1231 if (!ap->ops->error_handler)
1232 return;
1234 spin_lock_irqsave(ap->lock, flags);
1235 dev = __ata_scsi_find_dev(ap, sdev);
1236 if (dev && dev->sdev) {
1237 /* SCSI device already in CANCEL state, no need to offline it */
1238 dev->sdev = NULL;
1239 dev->flags |= ATA_DFLAG_DETACH;
1240 ata_port_schedule_eh(ap);
1242 spin_unlock_irqrestore(ap->lock, flags);
1244 kfree(q->dma_drain_buffer);
1245 q->dma_drain_buffer = NULL;
1246 q->dma_drain_size = 0;
1250 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1251 * @sdev: SCSI device to configure queue depth for
1252 * @queue_depth: new queue depth
1253 * @reason: calling context
1255 * This is libata standard hostt->change_queue_depth callback.
1256 * SCSI will call into this callback when user tries to set queue
1257 * depth via sysfs.
1259 * LOCKING:
1260 * SCSI layer (we don't care)
1262 * RETURNS:
1263 * Newly configured queue depth.
1265 int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth,
1266 int reason)
1268 struct ata_port *ap = ata_shost_to_port(sdev->host);
1269 struct ata_device *dev;
1270 unsigned long flags;
1272 if (reason != SCSI_QDEPTH_DEFAULT)
1273 return -EOPNOTSUPP;
1275 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1276 return sdev->queue_depth;
1278 dev = ata_scsi_find_dev(ap, sdev);
1279 if (!dev || !ata_dev_enabled(dev))
1280 return sdev->queue_depth;
1282 /* NCQ enabled? */
1283 spin_lock_irqsave(ap->lock, flags);
1284 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1285 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1286 dev->flags |= ATA_DFLAG_NCQ_OFF;
1287 queue_depth = 1;
1289 spin_unlock_irqrestore(ap->lock, flags);
1291 /* limit and apply queue depth */
1292 queue_depth = min(queue_depth, sdev->host->can_queue);
1293 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1294 queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
1296 if (sdev->queue_depth == queue_depth)
1297 return -EINVAL;
1299 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
1300 return queue_depth;
1304 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1305 * @qc: Storage for translated ATA taskfile
1307 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1308 * (to start). Perhaps these commands should be preceded by
1309 * CHECK POWER MODE to see what power mode the device is already in.
1310 * [See SAT revision 5 at www.t10.org]
1312 * LOCKING:
1313 * spin_lock_irqsave(host lock)
1315 * RETURNS:
1316 * Zero on success, non-zero on error.
1318 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
1320 struct scsi_cmnd *scmd = qc->scsicmd;
1321 struct ata_taskfile *tf = &qc->tf;
1322 const u8 *cdb = scmd->cmnd;
1324 if (scmd->cmd_len < 5)
1325 goto invalid_fld;
1327 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1328 tf->protocol = ATA_PROT_NODATA;
1329 if (cdb[1] & 0x1) {
1330 ; /* ignore IMMED bit, violates sat-r05 */
1332 if (cdb[4] & 0x2)
1333 goto invalid_fld; /* LOEJ bit set not supported */
1334 if (((cdb[4] >> 4) & 0xf) != 0)
1335 goto invalid_fld; /* power conditions not supported */
1337 if (cdb[4] & 0x1) {
1338 tf->nsect = 1; /* 1 sector, lba=0 */
1340 if (qc->dev->flags & ATA_DFLAG_LBA) {
1341 tf->flags |= ATA_TFLAG_LBA;
1343 tf->lbah = 0x0;
1344 tf->lbam = 0x0;
1345 tf->lbal = 0x0;
1346 tf->device |= ATA_LBA;
1347 } else {
1348 /* CHS */
1349 tf->lbal = 0x1; /* sect */
1350 tf->lbam = 0x0; /* cyl low */
1351 tf->lbah = 0x0; /* cyl high */
1354 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
1355 } else {
1356 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1357 * or S5) causing some drives to spin up and down again.
1359 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1360 system_state == SYSTEM_POWER_OFF)
1361 goto skip;
1363 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1364 system_entering_hibernation())
1365 goto skip;
1367 /* Issue ATA STANDBY IMMEDIATE command */
1368 tf->command = ATA_CMD_STANDBYNOW1;
1372 * Standby and Idle condition timers could be implemented but that
1373 * would require libata to implement the Power condition mode page
1374 * and allow the user to change it. Changing mode pages requires
1375 * MODE SELECT to be implemented.
1378 return 0;
1380 invalid_fld:
1381 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
1382 /* "Invalid field in cbd" */
1383 return 1;
1384 skip:
1385 scmd->result = SAM_STAT_GOOD;
1386 return 1;
1391 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1392 * @qc: Storage for translated ATA taskfile
1394 * Sets up an ATA taskfile to issue FLUSH CACHE or
1395 * FLUSH CACHE EXT.
1397 * LOCKING:
1398 * spin_lock_irqsave(host lock)
1400 * RETURNS:
1401 * Zero on success, non-zero on error.
1403 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
1405 struct ata_taskfile *tf = &qc->tf;
1407 tf->flags |= ATA_TFLAG_DEVICE;
1408 tf->protocol = ATA_PROT_NODATA;
1410 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
1411 tf->command = ATA_CMD_FLUSH_EXT;
1412 else
1413 tf->command = ATA_CMD_FLUSH;
1415 /* flush is critical for IO integrity, consider it an IO command */
1416 qc->flags |= ATA_QCFLAG_IO;
1418 return 0;
1422 * scsi_6_lba_len - Get LBA and transfer length
1423 * @cdb: SCSI command to translate
1425 * Calculate LBA and transfer length for 6-byte commands.
1427 * RETURNS:
1428 * @plba: the LBA
1429 * @plen: the transfer length
1431 static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1433 u64 lba = 0;
1434 u32 len;
1436 VPRINTK("six-byte command\n");
1438 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
1439 lba |= ((u64)cdb[2]) << 8;
1440 lba |= ((u64)cdb[3]);
1442 len = cdb[4];
1444 *plba = lba;
1445 *plen = len;
1449 * scsi_10_lba_len - Get LBA and transfer length
1450 * @cdb: SCSI command to translate
1452 * Calculate LBA and transfer length for 10-byte commands.
1454 * RETURNS:
1455 * @plba: the LBA
1456 * @plen: the transfer length
1458 static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1460 u64 lba = 0;
1461 u32 len = 0;
1463 VPRINTK("ten-byte command\n");
1465 lba |= ((u64)cdb[2]) << 24;
1466 lba |= ((u64)cdb[3]) << 16;
1467 lba |= ((u64)cdb[4]) << 8;
1468 lba |= ((u64)cdb[5]);
1470 len |= ((u32)cdb[7]) << 8;
1471 len |= ((u32)cdb[8]);
1473 *plba = lba;
1474 *plen = len;
1478 * scsi_16_lba_len - Get LBA and transfer length
1479 * @cdb: SCSI command to translate
1481 * Calculate LBA and transfer length for 16-byte commands.
1483 * RETURNS:
1484 * @plba: the LBA
1485 * @plen: the transfer length
1487 static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1489 u64 lba = 0;
1490 u32 len = 0;
1492 VPRINTK("sixteen-byte command\n");
1494 lba |= ((u64)cdb[2]) << 56;
1495 lba |= ((u64)cdb[3]) << 48;
1496 lba |= ((u64)cdb[4]) << 40;
1497 lba |= ((u64)cdb[5]) << 32;
1498 lba |= ((u64)cdb[6]) << 24;
1499 lba |= ((u64)cdb[7]) << 16;
1500 lba |= ((u64)cdb[8]) << 8;
1501 lba |= ((u64)cdb[9]);
1503 len |= ((u32)cdb[10]) << 24;
1504 len |= ((u32)cdb[11]) << 16;
1505 len |= ((u32)cdb[12]) << 8;
1506 len |= ((u32)cdb[13]);
1508 *plba = lba;
1509 *plen = len;
1513 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1514 * @qc: Storage for translated ATA taskfile
1516 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1518 * LOCKING:
1519 * spin_lock_irqsave(host lock)
1521 * RETURNS:
1522 * Zero on success, non-zero on error.
1524 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
1526 struct scsi_cmnd *scmd = qc->scsicmd;
1527 struct ata_taskfile *tf = &qc->tf;
1528 struct ata_device *dev = qc->dev;
1529 u64 dev_sectors = qc->dev->n_sectors;
1530 const u8 *cdb = scmd->cmnd;
1531 u64 block;
1532 u32 n_block;
1534 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1535 tf->protocol = ATA_PROT_NODATA;
1537 if (cdb[0] == VERIFY) {
1538 if (scmd->cmd_len < 10)
1539 goto invalid_fld;
1540 scsi_10_lba_len(cdb, &block, &n_block);
1541 } else if (cdb[0] == VERIFY_16) {
1542 if (scmd->cmd_len < 16)
1543 goto invalid_fld;
1544 scsi_16_lba_len(cdb, &block, &n_block);
1545 } else
1546 goto invalid_fld;
1548 if (!n_block)
1549 goto nothing_to_do;
1550 if (block >= dev_sectors)
1551 goto out_of_range;
1552 if ((block + n_block) > dev_sectors)
1553 goto out_of_range;
1555 if (dev->flags & ATA_DFLAG_LBA) {
1556 tf->flags |= ATA_TFLAG_LBA;
1558 if (lba_28_ok(block, n_block)) {
1559 /* use LBA28 */
1560 tf->command = ATA_CMD_VERIFY;
1561 tf->device |= (block >> 24) & 0xf;
1562 } else if (lba_48_ok(block, n_block)) {
1563 if (!(dev->flags & ATA_DFLAG_LBA48))
1564 goto out_of_range;
1566 /* use LBA48 */
1567 tf->flags |= ATA_TFLAG_LBA48;
1568 tf->command = ATA_CMD_VERIFY_EXT;
1570 tf->hob_nsect = (n_block >> 8) & 0xff;
1572 tf->hob_lbah = (block >> 40) & 0xff;
1573 tf->hob_lbam = (block >> 32) & 0xff;
1574 tf->hob_lbal = (block >> 24) & 0xff;
1575 } else
1576 /* request too large even for LBA48 */
1577 goto out_of_range;
1579 tf->nsect = n_block & 0xff;
1581 tf->lbah = (block >> 16) & 0xff;
1582 tf->lbam = (block >> 8) & 0xff;
1583 tf->lbal = block & 0xff;
1585 tf->device |= ATA_LBA;
1586 } else {
1587 /* CHS */
1588 u32 sect, head, cyl, track;
1590 if (!lba_28_ok(block, n_block))
1591 goto out_of_range;
1593 /* Convert LBA to CHS */
1594 track = (u32)block / dev->sectors;
1595 cyl = track / dev->heads;
1596 head = track % dev->heads;
1597 sect = (u32)block % dev->sectors + 1;
1599 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1600 (u32)block, track, cyl, head, sect);
1602 /* Check whether the converted CHS can fit.
1603 Cylinder: 0-65535
1604 Head: 0-15
1605 Sector: 1-255*/
1606 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1607 goto out_of_range;
1609 tf->command = ATA_CMD_VERIFY;
1610 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1611 tf->lbal = sect;
1612 tf->lbam = cyl;
1613 tf->lbah = cyl >> 8;
1614 tf->device |= head;
1617 return 0;
1619 invalid_fld:
1620 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
1621 /* "Invalid field in cbd" */
1622 return 1;
1624 out_of_range:
1625 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1626 /* "Logical Block Address out of range" */
1627 return 1;
1629 nothing_to_do:
1630 scmd->result = SAM_STAT_GOOD;
1631 return 1;
1635 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1636 * @qc: Storage for translated ATA taskfile
1638 * Converts any of six SCSI read/write commands into the
1639 * ATA counterpart, including starting sector (LBA),
1640 * sector count, and taking into account the device's LBA48
1641 * support.
1643 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1644 * %WRITE_16 are currently supported.
1646 * LOCKING:
1647 * spin_lock_irqsave(host lock)
1649 * RETURNS:
1650 * Zero on success, non-zero on error.
1652 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
1654 struct scsi_cmnd *scmd = qc->scsicmd;
1655 const u8 *cdb = scmd->cmnd;
1656 unsigned int tf_flags = 0;
1657 u64 block;
1658 u32 n_block;
1659 int rc;
1661 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
1662 tf_flags |= ATA_TFLAG_WRITE;
1664 /* Calculate the SCSI LBA, transfer length and FUA. */
1665 switch (cdb[0]) {
1666 case READ_10:
1667 case WRITE_10:
1668 if (unlikely(scmd->cmd_len < 10))
1669 goto invalid_fld;
1670 scsi_10_lba_len(cdb, &block, &n_block);
1671 if (unlikely(cdb[1] & (1 << 3)))
1672 tf_flags |= ATA_TFLAG_FUA;
1673 break;
1674 case READ_6:
1675 case WRITE_6:
1676 if (unlikely(scmd->cmd_len < 6))
1677 goto invalid_fld;
1678 scsi_6_lba_len(cdb, &block, &n_block);
1680 /* for 6-byte r/w commands, transfer length 0
1681 * means 256 blocks of data, not 0 block.
1683 if (!n_block)
1684 n_block = 256;
1685 break;
1686 case READ_16:
1687 case WRITE_16:
1688 if (unlikely(scmd->cmd_len < 16))
1689 goto invalid_fld;
1690 scsi_16_lba_len(cdb, &block, &n_block);
1691 if (unlikely(cdb[1] & (1 << 3)))
1692 tf_flags |= ATA_TFLAG_FUA;
1693 break;
1694 default:
1695 DPRINTK("no-byte command\n");
1696 goto invalid_fld;
1699 /* Check and compose ATA command */
1700 if (!n_block)
1701 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1702 * length 0 means transfer 0 block of data.
1703 * However, for ATA R/W commands, sector count 0 means
1704 * 256 or 65536 sectors, not 0 sectors as in SCSI.
1706 * WARNING: one or two older ATA drives treat 0 as 0...
1708 goto nothing_to_do;
1710 qc->flags |= ATA_QCFLAG_IO;
1711 qc->nbytes = n_block * ATA_SECT_SIZE;
1713 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1714 qc->tag);
1715 if (likely(rc == 0))
1716 return 0;
1718 if (rc == -ERANGE)
1719 goto out_of_range;
1720 /* treat all other errors as -EINVAL, fall through */
1721 invalid_fld:
1722 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
1723 /* "Invalid field in cbd" */
1724 return 1;
1726 out_of_range:
1727 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1728 /* "Logical Block Address out of range" */
1729 return 1;
1731 nothing_to_do:
1732 scmd->result = SAM_STAT_GOOD;
1733 return 1;
1736 static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1738 struct ata_port *ap = qc->ap;
1739 struct scsi_cmnd *cmd = qc->scsicmd;
1740 u8 *cdb = cmd->cmnd;
1741 int need_sense = (qc->err_mask != 0);
1743 /* For ATA pass thru (SAT) commands, generate a sense block if
1744 * user mandated it or if there's an error. Note that if we
1745 * generate because the user forced us to, a check condition
1746 * is generated and the ATA register values are returned
1747 * whether the command completed successfully or not. If there
1748 * was no error, SK, ASC and ASCQ will all be zero.
1750 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1751 ((cdb[2] & 0x20) || need_sense)) {
1752 ata_gen_passthru_sense(qc);
1753 } else {
1754 if (!need_sense) {
1755 cmd->result = SAM_STAT_GOOD;
1756 } else {
1757 /* TODO: decide which descriptor format to use
1758 * for 48b LBA devices and call that here
1759 * instead of the fixed desc, which is only
1760 * good for smaller LBA (and maybe CHS?)
1761 * devices.
1763 ata_gen_ata_sense(qc);
1767 if (need_sense && !ap->ops->error_handler)
1768 ata_dump_status(ap->print_id, &qc->result_tf);
1770 qc->scsidone(cmd);
1772 ata_qc_free(qc);
1776 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1777 * @dev: ATA device to which the command is addressed
1778 * @cmd: SCSI command to execute
1779 * @done: SCSI command completion function
1780 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1782 * Our ->queuecommand() function has decided that the SCSI
1783 * command issued can be directly translated into an ATA
1784 * command, rather than handled internally.
1786 * This function sets up an ata_queued_cmd structure for the
1787 * SCSI command, and sends that ata_queued_cmd to the hardware.
1789 * The xlat_func argument (actor) returns 0 if ready to execute
1790 * ATA command, else 1 to finish translation. If 1 is returned
1791 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1792 * to be set reflecting an error condition or clean (early)
1793 * termination.
1795 * LOCKING:
1796 * spin_lock_irqsave(host lock)
1798 * RETURNS:
1799 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1800 * needs to be deferred.
1802 static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1803 void (*done)(struct scsi_cmnd *),
1804 ata_xlat_func_t xlat_func)
1806 struct ata_port *ap = dev->link->ap;
1807 struct ata_queued_cmd *qc;
1808 int rc;
1810 VPRINTK("ENTER\n");
1812 qc = ata_scsi_qc_new(dev, cmd, done);
1813 if (!qc)
1814 goto err_mem;
1816 /* data is present; dma-map it */
1817 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1818 cmd->sc_data_direction == DMA_TO_DEVICE) {
1819 if (unlikely(scsi_bufflen(cmd) < 1)) {
1820 ata_dev_printk(dev, KERN_WARNING,
1821 "WARNING: zero len r/w req\n");
1822 goto err_did;
1825 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
1827 qc->dma_dir = cmd->sc_data_direction;
1830 qc->complete_fn = ata_scsi_qc_complete;
1832 if (xlat_func(qc))
1833 goto early_finish;
1835 if (ap->ops->qc_defer) {
1836 if ((rc = ap->ops->qc_defer(qc)))
1837 goto defer;
1840 /* select device, send command to hardware */
1841 ata_qc_issue(qc);
1843 VPRINTK("EXIT\n");
1844 return 0;
1846 early_finish:
1847 ata_qc_free(qc);
1848 qc->scsidone(cmd);
1849 DPRINTK("EXIT - early finish (good or error)\n");
1850 return 0;
1852 err_did:
1853 ata_qc_free(qc);
1854 cmd->result = (DID_ERROR << 16);
1855 qc->scsidone(cmd);
1856 err_mem:
1857 DPRINTK("EXIT - internal\n");
1858 return 0;
1860 defer:
1861 ata_qc_free(qc);
1862 DPRINTK("EXIT - defer\n");
1863 if (rc == ATA_DEFER_LINK)
1864 return SCSI_MLQUEUE_DEVICE_BUSY;
1865 else
1866 return SCSI_MLQUEUE_HOST_BUSY;
1870 * ata_scsi_rbuf_get - Map response buffer.
1871 * @cmd: SCSI command containing buffer to be mapped.
1872 * @flags: unsigned long variable to store irq enable status
1873 * @copy_in: copy in from user buffer
1875 * Prepare buffer for simulated SCSI commands.
1877 * LOCKING:
1878 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
1880 * RETURNS:
1881 * Pointer to response buffer.
1883 static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
1884 unsigned long *flags)
1886 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
1888 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
1889 if (copy_in)
1890 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1891 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1892 return ata_scsi_rbuf;
1896 * ata_scsi_rbuf_put - Unmap response buffer.
1897 * @cmd: SCSI command containing buffer to be unmapped.
1898 * @copy_out: copy out result
1899 * @flags: @flags passed to ata_scsi_rbuf_get()
1901 * Returns rbuf buffer. The result is copied to @cmd's buffer if
1902 * @copy_back is true.
1904 * LOCKING:
1905 * Unlocks ata_scsi_rbuf_lock.
1907 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
1908 unsigned long *flags)
1910 if (copy_out)
1911 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1912 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1913 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
1917 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1918 * @args: device IDENTIFY data / SCSI command of interest.
1919 * @actor: Callback hook for desired SCSI command simulator
1921 * Takes care of the hard work of simulating a SCSI command...
1922 * Mapping the response buffer, calling the command's handler,
1923 * and handling the handler's return value. This return value
1924 * indicates whether the handler wishes the SCSI command to be
1925 * completed successfully (0), or not (in which case cmd->result
1926 * and sense buffer are assumed to be set).
1928 * LOCKING:
1929 * spin_lock_irqsave(host lock)
1931 static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1932 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
1934 u8 *rbuf;
1935 unsigned int rc;
1936 struct scsi_cmnd *cmd = args->cmd;
1937 unsigned long flags;
1939 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
1940 rc = actor(args, rbuf);
1941 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
1943 if (rc == 0)
1944 cmd->result = SAM_STAT_GOOD;
1945 args->done(cmd);
1949 * ata_scsiop_inq_std - Simulate INQUIRY command
1950 * @args: device IDENTIFY data / SCSI command of interest.
1951 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1953 * Returns standard device identification data associated
1954 * with non-VPD INQUIRY command output.
1956 * LOCKING:
1957 * spin_lock_irqsave(host lock)
1959 static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
1961 const u8 versions[] = {
1962 0x60, /* SAM-3 (no version claimed) */
1964 0x03,
1965 0x20, /* SBC-2 (no version claimed) */
1967 0x02,
1968 0x60 /* SPC-3 (no version claimed) */
1970 u8 hdr[] = {
1971 TYPE_DISK,
1973 0x5, /* claim SPC-3 version compatibility */
1975 95 - 4
1978 VPRINTK("ENTER\n");
1980 /* set scsi removeable (RMB) bit per ata bit */
1981 if (ata_id_removeable(args->id))
1982 hdr[1] |= (1 << 7);
1984 memcpy(rbuf, hdr, sizeof(hdr));
1985 memcpy(&rbuf[8], "ATA ", 8);
1986 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
1987 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
1989 if (rbuf[32] == 0 || rbuf[32] == ' ')
1990 memcpy(&rbuf[32], "n/a ", 4);
1992 memcpy(rbuf + 59, versions, sizeof(versions));
1994 return 0;
1998 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
1999 * @args: device IDENTIFY data / SCSI command of interest.
2000 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2002 * Returns list of inquiry VPD pages available.
2004 * LOCKING:
2005 * spin_lock_irqsave(host lock)
2007 static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
2009 const u8 pages[] = {
2010 0x00, /* page 0x00, this page */
2011 0x80, /* page 0x80, unit serial no page */
2012 0x83, /* page 0x83, device ident page */
2013 0x89, /* page 0x89, ata info page */
2014 0xb0, /* page 0xb0, block limits page */
2015 0xb1, /* page 0xb1, block device characteristics page */
2018 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
2019 memcpy(rbuf + 4, pages, sizeof(pages));
2020 return 0;
2024 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
2025 * @args: device IDENTIFY data / SCSI command of interest.
2026 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2028 * Returns ATA device serial number.
2030 * LOCKING:
2031 * spin_lock_irqsave(host lock)
2033 static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
2035 const u8 hdr[] = {
2037 0x80, /* this page code */
2039 ATA_ID_SERNO_LEN, /* page len */
2042 memcpy(rbuf, hdr, sizeof(hdr));
2043 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2044 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2045 return 0;
2049 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
2050 * @args: device IDENTIFY data / SCSI command of interest.
2051 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2053 * Yields two logical unit device identification designators:
2054 * - vendor specific ASCII containing the ATA serial number
2055 * - SAT defined "t10 vendor id based" containing ASCII vendor
2056 * name ("ATA "), model and serial numbers.
2058 * LOCKING:
2059 * spin_lock_irqsave(host lock)
2061 static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
2063 const int sat_model_serial_desc_len = 68;
2064 int num;
2066 rbuf[1] = 0x83; /* this page code */
2067 num = 4;
2069 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2070 rbuf[num + 0] = 2;
2071 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2072 num += 4;
2073 ata_id_string(args->id, (unsigned char *) rbuf + num,
2074 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2075 num += ATA_ID_SERNO_LEN;
2077 /* SAT defined lu model and serial numbers descriptor */
2078 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2079 rbuf[num + 0] = 2;
2080 rbuf[num + 1] = 1;
2081 rbuf[num + 3] = sat_model_serial_desc_len;
2082 num += 4;
2083 memcpy(rbuf + num, "ATA ", 8);
2084 num += 8;
2085 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2086 ATA_ID_PROD_LEN);
2087 num += ATA_ID_PROD_LEN;
2088 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2089 ATA_ID_SERNO_LEN);
2090 num += ATA_ID_SERNO_LEN;
2092 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
2093 return 0;
2097 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2098 * @args: device IDENTIFY data / SCSI command of interest.
2099 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2101 * Yields SAT-specified ATA VPD page.
2103 * LOCKING:
2104 * spin_lock_irqsave(host lock)
2106 static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
2108 struct ata_taskfile tf;
2110 memset(&tf, 0, sizeof(tf));
2112 rbuf[1] = 0x89; /* our page code */
2113 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2114 rbuf[3] = (0x238 & 0xff);
2116 memcpy(&rbuf[8], "linux ", 8);
2117 memcpy(&rbuf[16], "libata ", 16);
2118 memcpy(&rbuf[32], DRV_VERSION, 4);
2119 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
2121 /* we don't store the ATA device signature, so we fake it */
2123 tf.command = ATA_DRDY; /* really, this is Status reg */
2124 tf.lbal = 0x1;
2125 tf.nsect = 0x1;
2127 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
2128 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
2130 rbuf[56] = ATA_CMD_ID_ATA;
2132 memcpy(&rbuf[60], &args->id[0], 512);
2133 return 0;
2136 static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2138 u32 min_io_sectors;
2140 rbuf[1] = 0xb0;
2141 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2144 * Optimal transfer length granularity.
2146 * This is always one physical block, but for disks with a smaller
2147 * logical than physical sector size we need to figure out what the
2148 * latter is.
2150 if (ata_id_has_large_logical_sectors(args->id))
2151 min_io_sectors = ata_id_logical_per_physical_sectors(args->id);
2152 else
2153 min_io_sectors = 1;
2154 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2157 * Optimal unmap granularity.
2159 * The ATA spec doesn't even know about a granularity or alignment
2160 * for the TRIM command. We can leave away most of the unmap related
2161 * VPD page entries, but we have specifify a granularity to signal
2162 * that we support some form of unmap - in thise case via WRITE SAME
2163 * with the unmap bit set.
2165 if (ata_id_has_trim(args->id)) {
2166 put_unaligned_be32(65535 * 512 / 8, &rbuf[20]);
2167 put_unaligned_be32(1, &rbuf[28]);
2170 return 0;
2173 static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2175 int form_factor = ata_id_form_factor(args->id);
2176 int media_rotation_rate = ata_id_rotation_rate(args->id);
2178 rbuf[1] = 0xb1;
2179 rbuf[3] = 0x3c;
2180 rbuf[4] = media_rotation_rate >> 8;
2181 rbuf[5] = media_rotation_rate;
2182 rbuf[7] = form_factor;
2184 return 0;
2188 * ata_scsiop_noop - Command handler that simply returns success.
2189 * @args: device IDENTIFY data / SCSI command of interest.
2190 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2192 * No operation. Simply returns success to caller, to indicate
2193 * that the caller should successfully complete this SCSI command.
2195 * LOCKING:
2196 * spin_lock_irqsave(host lock)
2198 static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
2200 VPRINTK("ENTER\n");
2201 return 0;
2205 * ata_msense_caching - Simulate MODE SENSE caching info page
2206 * @id: device IDENTIFY data
2207 * @buf: output buffer
2209 * Generate a caching info page, which conditionally indicates
2210 * write caching to the SCSI layer, depending on device
2211 * capabilities.
2213 * LOCKING:
2214 * None.
2216 static unsigned int ata_msense_caching(u16 *id, u8 *buf)
2218 memcpy(buf, def_cache_mpage, sizeof(def_cache_mpage));
2219 if (ata_id_wcache_enabled(id))
2220 buf[2] |= (1 << 2); /* write cache enable */
2221 if (!ata_id_rahead_enabled(id))
2222 buf[12] |= (1 << 5); /* disable read ahead */
2223 return sizeof(def_cache_mpage);
2227 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
2228 * @buf: output buffer
2230 * Generate a generic MODE SENSE control mode page.
2232 * LOCKING:
2233 * None.
2235 static unsigned int ata_msense_ctl_mode(u8 *buf)
2237 memcpy(buf, def_control_mpage, sizeof(def_control_mpage));
2238 return sizeof(def_control_mpage);
2242 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
2243 * @buf: output buffer
2245 * Generate a generic MODE SENSE r/w error recovery page.
2247 * LOCKING:
2248 * None.
2250 static unsigned int ata_msense_rw_recovery(u8 *buf)
2252 memcpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage));
2253 return sizeof(def_rw_recovery_mpage);
2257 * We can turn this into a real blacklist if it's needed, for now just
2258 * blacklist any Maxtor BANC1G10 revision firmware
2260 static int ata_dev_supports_fua(u16 *id)
2262 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
2264 if (!libata_fua)
2265 return 0;
2266 if (!ata_id_has_fua(id))
2267 return 0;
2269 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2270 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
2272 if (strcmp(model, "Maxtor"))
2273 return 1;
2274 if (strcmp(fw, "BANC1G10"))
2275 return 1;
2277 return 0; /* blacklisted */
2281 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2282 * @args: device IDENTIFY data / SCSI command of interest.
2283 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2285 * Simulate MODE SENSE commands. Assume this is invoked for direct
2286 * access devices (e.g. disks) only. There should be no block
2287 * descriptor for other device types.
2289 * LOCKING:
2290 * spin_lock_irqsave(host lock)
2292 static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
2294 struct ata_device *dev = args->dev;
2295 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
2296 const u8 sat_blk_desc[] = {
2297 0, 0, 0, 0, /* number of blocks: sat unspecified */
2299 0, 0x2, 0x0 /* block length: 512 bytes */
2301 u8 pg, spg;
2302 unsigned int ebd, page_control, six_byte;
2303 u8 dpofua;
2305 VPRINTK("ENTER\n");
2307 six_byte = (scsicmd[0] == MODE_SENSE);
2308 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2310 * LLBA bit in msense(10) ignored (compliant)
2313 page_control = scsicmd[2] >> 6;
2314 switch (page_control) {
2315 case 0: /* current */
2316 break; /* supported */
2317 case 3: /* saved */
2318 goto saving_not_supp;
2319 case 1: /* changeable */
2320 case 2: /* defaults */
2321 default:
2322 goto invalid_fld;
2325 if (six_byte)
2326 p += 4 + (ebd ? 8 : 0);
2327 else
2328 p += 8 + (ebd ? 8 : 0);
2330 pg = scsicmd[2] & 0x3f;
2331 spg = scsicmd[3];
2333 * No mode subpages supported (yet) but asking for _all_
2334 * subpages may be valid
2336 if (spg && (spg != ALL_SUB_MPAGES))
2337 goto invalid_fld;
2339 switch(pg) {
2340 case RW_RECOVERY_MPAGE:
2341 p += ata_msense_rw_recovery(p);
2342 break;
2344 case CACHE_MPAGE:
2345 p += ata_msense_caching(args->id, p);
2346 break;
2348 case CONTROL_MPAGE:
2349 p += ata_msense_ctl_mode(p);
2350 break;
2352 case ALL_MPAGES:
2353 p += ata_msense_rw_recovery(p);
2354 p += ata_msense_caching(args->id, p);
2355 p += ata_msense_ctl_mode(p);
2356 break;
2358 default: /* invalid page code */
2359 goto invalid_fld;
2362 dpofua = 0;
2363 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
2364 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2365 dpofua = 1 << 4;
2367 if (six_byte) {
2368 rbuf[0] = p - rbuf - 1;
2369 rbuf[2] |= dpofua;
2370 if (ebd) {
2371 rbuf[3] = sizeof(sat_blk_desc);
2372 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
2374 } else {
2375 unsigned int output_len = p - rbuf - 2;
2377 rbuf[0] = output_len >> 8;
2378 rbuf[1] = output_len;
2379 rbuf[3] |= dpofua;
2380 if (ebd) {
2381 rbuf[7] = sizeof(sat_blk_desc);
2382 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
2385 return 0;
2387 invalid_fld:
2388 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
2389 /* "Invalid field in cbd" */
2390 return 1;
2392 saving_not_supp:
2393 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2394 /* "Saving parameters not supported" */
2395 return 1;
2399 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2400 * @args: device IDENTIFY data / SCSI command of interest.
2401 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2403 * Simulate READ CAPACITY commands.
2405 * LOCKING:
2406 * None.
2408 static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
2410 struct ata_device *dev = args->dev;
2411 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
2412 u8 log_per_phys = 0;
2413 u16 lowest_aligned = 0;
2414 u16 word_106 = dev->id[106];
2415 u16 word_209 = dev->id[209];
2417 if ((word_106 & 0xc000) == 0x4000) {
2418 /* Number and offset of logical sectors per physical sector */
2419 if (word_106 & (1 << 13))
2420 log_per_phys = word_106 & 0xf;
2421 if ((word_209 & 0xc000) == 0x4000) {
2422 u16 first = dev->id[209] & 0x3fff;
2423 if (first > 0)
2424 lowest_aligned = (1 << log_per_phys) - first;
2428 VPRINTK("ENTER\n");
2430 if (args->cmd->cmnd[0] == READ_CAPACITY) {
2431 if (last_lba >= 0xffffffffULL)
2432 last_lba = 0xffffffff;
2434 /* sector count, 32-bit */
2435 rbuf[0] = last_lba >> (8 * 3);
2436 rbuf[1] = last_lba >> (8 * 2);
2437 rbuf[2] = last_lba >> (8 * 1);
2438 rbuf[3] = last_lba;
2440 /* sector size */
2441 rbuf[6] = ATA_SECT_SIZE >> 8;
2442 rbuf[7] = ATA_SECT_SIZE & 0xff;
2443 } else {
2444 /* sector count, 64-bit */
2445 rbuf[0] = last_lba >> (8 * 7);
2446 rbuf[1] = last_lba >> (8 * 6);
2447 rbuf[2] = last_lba >> (8 * 5);
2448 rbuf[3] = last_lba >> (8 * 4);
2449 rbuf[4] = last_lba >> (8 * 3);
2450 rbuf[5] = last_lba >> (8 * 2);
2451 rbuf[6] = last_lba >> (8 * 1);
2452 rbuf[7] = last_lba;
2454 /* sector size */
2455 rbuf[10] = ATA_SECT_SIZE >> 8;
2456 rbuf[11] = ATA_SECT_SIZE & 0xff;
2458 rbuf[12] = 0;
2459 rbuf[13] = log_per_phys;
2460 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2461 rbuf[15] = lowest_aligned;
2463 if (ata_id_has_trim(args->id)) {
2464 rbuf[14] |= 0x80; /* TPE */
2466 if (ata_id_has_zero_after_trim(args->id))
2467 rbuf[14] |= 0x40; /* TPRZ */
2471 return 0;
2475 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2476 * @args: device IDENTIFY data / SCSI command of interest.
2477 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2479 * Simulate REPORT LUNS command.
2481 * LOCKING:
2482 * spin_lock_irqsave(host lock)
2484 static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
2486 VPRINTK("ENTER\n");
2487 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2489 return 0;
2492 static void atapi_sense_complete(struct ata_queued_cmd *qc)
2494 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
2495 /* FIXME: not quite right; we don't want the
2496 * translation of taskfile registers into
2497 * a sense descriptors, since that's only
2498 * correct for ATA, not ATAPI
2500 ata_gen_passthru_sense(qc);
2503 qc->scsidone(qc->scsicmd);
2504 ata_qc_free(qc);
2507 /* is it pointless to prefer PIO for "safety reasons"? */
2508 static inline int ata_pio_use_silly(struct ata_port *ap)
2510 return (ap->flags & ATA_FLAG_PIO_DMA);
2513 static void atapi_request_sense(struct ata_queued_cmd *qc)
2515 struct ata_port *ap = qc->ap;
2516 struct scsi_cmnd *cmd = qc->scsicmd;
2518 DPRINTK("ATAPI request sense\n");
2520 /* FIXME: is this needed? */
2521 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2523 #ifdef CONFIG_ATA_SFF
2524 if (ap->ops->sff_tf_read)
2525 ap->ops->sff_tf_read(ap, &qc->tf);
2526 #endif
2528 /* fill these in, for the case where they are -not- overwritten */
2529 cmd->sense_buffer[0] = 0x70;
2530 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2532 ata_qc_reinit(qc);
2534 /* setup sg table and init transfer direction */
2535 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
2536 ata_sg_init(qc, &qc->sgent, 1);
2537 qc->dma_dir = DMA_FROM_DEVICE;
2539 memset(&qc->cdb, 0, qc->dev->cdb_len);
2540 qc->cdb[0] = REQUEST_SENSE;
2541 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2543 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2544 qc->tf.command = ATA_CMD_PACKET;
2546 if (ata_pio_use_silly(ap)) {
2547 qc->tf.protocol = ATAPI_PROT_DMA;
2548 qc->tf.feature |= ATAPI_PKT_DMA;
2549 } else {
2550 qc->tf.protocol = ATAPI_PROT_PIO;
2551 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2552 qc->tf.lbah = 0;
2554 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2556 qc->complete_fn = atapi_sense_complete;
2558 ata_qc_issue(qc);
2560 DPRINTK("EXIT\n");
2563 static void atapi_qc_complete(struct ata_queued_cmd *qc)
2565 struct scsi_cmnd *cmd = qc->scsicmd;
2566 unsigned int err_mask = qc->err_mask;
2568 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
2570 /* handle completion from new EH */
2571 if (unlikely(qc->ap->ops->error_handler &&
2572 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2574 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2575 /* FIXME: not quite right; we don't want the
2576 * translation of taskfile registers into a
2577 * sense descriptors, since that's only
2578 * correct for ATA, not ATAPI
2580 ata_gen_passthru_sense(qc);
2583 /* SCSI EH automatically locks door if sdev->locked is
2584 * set. Sometimes door lock request continues to
2585 * fail, for example, when no media is present. This
2586 * creates a loop - SCSI EH issues door lock which
2587 * fails and gets invoked again to acquire sense data
2588 * for the failed command.
2590 * If door lock fails, always clear sdev->locked to
2591 * avoid this infinite loop.
2593 * This may happen before SCSI scan is complete. Make
2594 * sure qc->dev->sdev isn't NULL before dereferencing.
2596 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
2597 qc->dev->sdev->locked = 0;
2599 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2600 qc->scsidone(cmd);
2601 ata_qc_free(qc);
2602 return;
2605 /* successful completion or old EH failure path */
2606 if (unlikely(err_mask & AC_ERR_DEV)) {
2607 cmd->result = SAM_STAT_CHECK_CONDITION;
2608 atapi_request_sense(qc);
2609 return;
2610 } else if (unlikely(err_mask)) {
2611 /* FIXME: not quite right; we don't want the
2612 * translation of taskfile registers into
2613 * a sense descriptors, since that's only
2614 * correct for ATA, not ATAPI
2616 ata_gen_passthru_sense(qc);
2617 } else {
2618 u8 *scsicmd = cmd->cmnd;
2620 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
2621 unsigned long flags;
2622 u8 *buf;
2624 buf = ata_scsi_rbuf_get(cmd, true, &flags);
2626 /* ATAPI devices typically report zero for their SCSI version,
2627 * and sometimes deviate from the spec WRT response data
2628 * format. If SCSI version is reported as zero like normal,
2629 * then we make the following fixups: 1) Fake MMC-5 version,
2630 * to indicate to the Linux scsi midlayer this is a modern
2631 * device. 2) Ensure response data format / ATAPI information
2632 * are always correct.
2634 if (buf[2] == 0) {
2635 buf[2] = 0x5;
2636 buf[3] = 0x32;
2639 ata_scsi_rbuf_put(cmd, true, &flags);
2642 cmd->result = SAM_STAT_GOOD;
2645 qc->scsidone(cmd);
2646 ata_qc_free(qc);
2649 * atapi_xlat - Initialize PACKET taskfile
2650 * @qc: command structure to be initialized
2652 * LOCKING:
2653 * spin_lock_irqsave(host lock)
2655 * RETURNS:
2656 * Zero on success, non-zero on failure.
2658 static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
2660 struct scsi_cmnd *scmd = qc->scsicmd;
2661 struct ata_device *dev = qc->dev;
2662 int nodata = (scmd->sc_data_direction == DMA_NONE);
2663 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
2664 unsigned int nbytes;
2666 memset(qc->cdb, 0, dev->cdb_len);
2667 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
2669 qc->complete_fn = atapi_qc_complete;
2671 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2672 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2673 qc->tf.flags |= ATA_TFLAG_WRITE;
2674 DPRINTK("direction: write\n");
2677 qc->tf.command = ATA_CMD_PACKET;
2678 ata_qc_set_pc_nbytes(qc);
2680 /* check whether ATAPI DMA is safe */
2681 if (!nodata && !using_pio && atapi_check_dma(qc))
2682 using_pio = 1;
2684 /* Some controller variants snoop this value for Packet
2685 * transfers to do state machine and FIFO management. Thus we
2686 * want to set it properly, and for DMA where it is
2687 * effectively meaningless.
2689 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
2691 /* Most ATAPI devices which honor transfer chunk size don't
2692 * behave according to the spec when odd chunk size which
2693 * matches the transfer length is specified. If the number of
2694 * bytes to transfer is 2n+1. According to the spec, what
2695 * should happen is to indicate that 2n+1 is going to be
2696 * transferred and transfer 2n+2 bytes where the last byte is
2697 * padding.
2699 * In practice, this doesn't happen. ATAPI devices first
2700 * indicate and transfer 2n bytes and then indicate and
2701 * transfer 2 bytes where the last byte is padding.
2703 * This inconsistency confuses several controllers which
2704 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2705 * These controllers use actual number of transferred bytes to
2706 * update DMA poitner and transfer of 4n+2 bytes make those
2707 * controller push DMA pointer by 4n+4 bytes because SATA data
2708 * FISes are aligned to 4 bytes. This causes data corruption
2709 * and buffer overrun.
2711 * Always setting nbytes to even number solves this problem
2712 * because then ATAPI devices don't have to split data at 2n
2713 * boundaries.
2715 if (nbytes & 0x1)
2716 nbytes++;
2718 qc->tf.lbam = (nbytes & 0xFF);
2719 qc->tf.lbah = (nbytes >> 8);
2721 if (nodata)
2722 qc->tf.protocol = ATAPI_PROT_NODATA;
2723 else if (using_pio)
2724 qc->tf.protocol = ATAPI_PROT_PIO;
2725 else {
2726 /* DMA data xfer */
2727 qc->tf.protocol = ATAPI_PROT_DMA;
2728 qc->tf.feature |= ATAPI_PKT_DMA;
2730 if ((dev->flags & ATA_DFLAG_DMADIR) &&
2731 (scmd->sc_data_direction != DMA_TO_DEVICE))
2732 /* some SATA bridges need us to indicate data xfer direction */
2733 qc->tf.feature |= ATAPI_DMADIR;
2737 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2738 as ATAPI tape drives don't get this right otherwise */
2739 return 0;
2742 static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
2744 if (!sata_pmp_attached(ap)) {
2745 if (likely(devno < ata_link_max_devices(&ap->link)))
2746 return &ap->link.device[devno];
2747 } else {
2748 if (likely(devno < ap->nr_pmp_links))
2749 return &ap->pmp_link[devno].device[0];
2752 return NULL;
2755 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
2756 const struct scsi_device *scsidev)
2758 int devno;
2760 /* skip commands not addressed to targets we simulate */
2761 if (!sata_pmp_attached(ap)) {
2762 if (unlikely(scsidev->channel || scsidev->lun))
2763 return NULL;
2764 devno = scsidev->id;
2765 } else {
2766 if (unlikely(scsidev->id || scsidev->lun))
2767 return NULL;
2768 devno = scsidev->channel;
2771 return ata_find_dev(ap, devno);
2775 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2776 * @ap: ATA port to which the device is attached
2777 * @scsidev: SCSI device from which we derive the ATA device
2779 * Given various information provided in struct scsi_cmnd,
2780 * map that onto an ATA bus, and using that mapping
2781 * determine which ata_device is associated with the
2782 * SCSI command to be sent.
2784 * LOCKING:
2785 * spin_lock_irqsave(host lock)
2787 * RETURNS:
2788 * Associated ATA device, or %NULL if not found.
2790 static struct ata_device *
2791 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
2793 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
2795 if (unlikely(!dev || !ata_dev_enabled(dev)))
2796 return NULL;
2798 return dev;
2802 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2803 * @byte1: Byte 1 from pass-thru CDB.
2805 * RETURNS:
2806 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2808 static u8
2809 ata_scsi_map_proto(u8 byte1)
2811 switch((byte1 & 0x1e) >> 1) {
2812 case 3: /* Non-data */
2813 return ATA_PROT_NODATA;
2815 case 6: /* DMA */
2816 case 10: /* UDMA Data-in */
2817 case 11: /* UDMA Data-Out */
2818 return ATA_PROT_DMA;
2820 case 4: /* PIO Data-in */
2821 case 5: /* PIO Data-out */
2822 return ATA_PROT_PIO;
2824 case 0: /* Hard Reset */
2825 case 1: /* SRST */
2826 case 8: /* Device Diagnostic */
2827 case 9: /* Device Reset */
2828 case 7: /* DMA Queued */
2829 case 12: /* FPDMA */
2830 case 15: /* Return Response Info */
2831 default: /* Reserved */
2832 break;
2835 return ATA_PROT_UNKNOWN;
2839 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2840 * @qc: command structure to be initialized
2842 * Handles either 12 or 16-byte versions of the CDB.
2844 * RETURNS:
2845 * Zero on success, non-zero on failure.
2847 static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
2849 struct ata_taskfile *tf = &(qc->tf);
2850 struct scsi_cmnd *scmd = qc->scsicmd;
2851 struct ata_device *dev = qc->dev;
2852 const u8 *cdb = scmd->cmnd;
2854 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
2855 goto invalid_fld;
2858 * 12 and 16 byte CDBs use different offsets to
2859 * provide the various register values.
2861 if (cdb[0] == ATA_16) {
2863 * 16-byte CDB - may contain extended commands.
2865 * If that is the case, copy the upper byte register values.
2867 if (cdb[1] & 0x01) {
2868 tf->hob_feature = cdb[3];
2869 tf->hob_nsect = cdb[5];
2870 tf->hob_lbal = cdb[7];
2871 tf->hob_lbam = cdb[9];
2872 tf->hob_lbah = cdb[11];
2873 tf->flags |= ATA_TFLAG_LBA48;
2874 } else
2875 tf->flags &= ~ATA_TFLAG_LBA48;
2878 * Always copy low byte, device and command registers.
2880 tf->feature = cdb[4];
2881 tf->nsect = cdb[6];
2882 tf->lbal = cdb[8];
2883 tf->lbam = cdb[10];
2884 tf->lbah = cdb[12];
2885 tf->device = cdb[13];
2886 tf->command = cdb[14];
2887 } else {
2889 * 12-byte CDB - incapable of extended commands.
2891 tf->flags &= ~ATA_TFLAG_LBA48;
2893 tf->feature = cdb[3];
2894 tf->nsect = cdb[4];
2895 tf->lbal = cdb[5];
2896 tf->lbam = cdb[6];
2897 tf->lbah = cdb[7];
2898 tf->device = cdb[8];
2899 tf->command = cdb[9];
2902 /* enforce correct master/slave bit */
2903 tf->device = dev->devno ?
2904 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
2906 /* READ/WRITE LONG use a non-standard sect_size */
2907 qc->sect_size = ATA_SECT_SIZE;
2908 switch (tf->command) {
2909 case ATA_CMD_READ_LONG:
2910 case ATA_CMD_READ_LONG_ONCE:
2911 case ATA_CMD_WRITE_LONG:
2912 case ATA_CMD_WRITE_LONG_ONCE:
2913 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
2914 goto invalid_fld;
2915 qc->sect_size = scsi_bufflen(scmd);
2919 * Set flags so that all registers will be written, pass on
2920 * write indication (used for PIO/DMA setup), result TF is
2921 * copied back and we don't whine too much about its failure.
2923 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2924 if (scmd->sc_data_direction == DMA_TO_DEVICE)
2925 tf->flags |= ATA_TFLAG_WRITE;
2927 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
2930 * Set transfer length.
2932 * TODO: find out if we need to do more here to
2933 * cover scatter/gather case.
2935 ata_qc_set_pc_nbytes(qc);
2937 /* We may not issue DMA commands if no DMA mode is set */
2938 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
2939 goto invalid_fld;
2941 /* sanity check for pio multi commands */
2942 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
2943 goto invalid_fld;
2945 if (is_multi_taskfile(tf)) {
2946 unsigned int multi_count = 1 << (cdb[1] >> 5);
2948 /* compare the passed through multi_count
2949 * with the cached multi_count of libata
2951 if (multi_count != dev->multi_count)
2952 ata_dev_printk(dev, KERN_WARNING,
2953 "invalid multi_count %u ignored\n",
2954 multi_count);
2958 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2959 * SET_FEATURES - XFER MODE must be preceded/succeeded
2960 * by an update to hardware-specific registers for each
2961 * controller (i.e. the reason for ->set_piomode(),
2962 * ->set_dmamode(), and ->post_set_mode() hooks).
2964 if (tf->command == ATA_CMD_SET_FEATURES &&
2965 tf->feature == SETFEATURES_XFER)
2966 goto invalid_fld;
2969 * Filter TPM commands by default. These provide an
2970 * essentially uncontrolled encrypted "back door" between
2971 * applications and the disk. Set libata.allow_tpm=1 if you
2972 * have a real reason for wanting to use them. This ensures
2973 * that installed software cannot easily mess stuff up without
2974 * user intent. DVR type users will probably ship with this enabled
2975 * for movie content management.
2977 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
2978 * for this and should do in future but that it is not sufficient as
2979 * DCS is an optional feature set. Thus we also do the software filter
2980 * so that we comply with the TC consortium stated goal that the user
2981 * can turn off TC features of their system.
2983 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
2984 goto invalid_fld;
2986 return 0;
2988 invalid_fld:
2989 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
2990 /* "Invalid field in cdb" */
2991 return 1;
2994 static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
2996 struct ata_taskfile *tf = &qc->tf;
2997 struct scsi_cmnd *scmd = qc->scsicmd;
2998 struct ata_device *dev = qc->dev;
2999 const u8 *cdb = scmd->cmnd;
3000 u64 block;
3001 u32 n_block;
3002 u32 size;
3003 void *buf;
3005 /* we may not issue DMA commands if no DMA mode is set */
3006 if (unlikely(!dev->dma_mode))
3007 goto invalid_fld;
3009 if (unlikely(scmd->cmd_len < 16))
3010 goto invalid_fld;
3011 scsi_16_lba_len(cdb, &block, &n_block);
3013 /* for now we only support WRITE SAME with the unmap bit set */
3014 if (unlikely(!(cdb[1] & 0x8)))
3015 goto invalid_fld;
3018 * WRITE SAME always has a sector sized buffer as payload, this
3019 * should never be a multiple entry S/G list.
3021 if (!scsi_sg_count(scmd))
3022 goto invalid_fld;
3024 buf = page_address(sg_page(scsi_sglist(scmd)));
3025 size = ata_set_lba_range_entries(buf, 512, block, n_block);
3027 tf->protocol = ATA_PROT_DMA;
3028 tf->hob_feature = 0;
3029 tf->feature = ATA_DSM_TRIM;
3030 tf->hob_nsect = (size / 512) >> 8;
3031 tf->nsect = size / 512;
3032 tf->command = ATA_CMD_DSM;
3033 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3034 ATA_TFLAG_WRITE;
3036 ata_qc_set_pc_nbytes(qc);
3038 return 0;
3040 invalid_fld:
3041 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
3042 /* "Invalid field in cdb" */
3043 return 1;
3047 * ata_get_xlat_func - check if SCSI to ATA translation is possible
3048 * @dev: ATA device
3049 * @cmd: SCSI command opcode to consider
3051 * Look up the SCSI command given, and determine whether the
3052 * SCSI command is to be translated or simulated.
3054 * RETURNS:
3055 * Pointer to translation function if possible, %NULL if not.
3058 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
3060 switch (cmd) {
3061 case READ_6:
3062 case READ_10:
3063 case READ_16:
3065 case WRITE_6:
3066 case WRITE_10:
3067 case WRITE_16:
3068 return ata_scsi_rw_xlat;
3070 case WRITE_SAME_16:
3071 return ata_scsi_write_same_xlat;
3073 case SYNCHRONIZE_CACHE:
3074 if (ata_try_flush_cache(dev))
3075 return ata_scsi_flush_xlat;
3076 break;
3078 case VERIFY:
3079 case VERIFY_16:
3080 return ata_scsi_verify_xlat;
3082 case ATA_12:
3083 case ATA_16:
3084 return ata_scsi_pass_thru;
3086 case START_STOP:
3087 return ata_scsi_start_stop_xlat;
3090 return NULL;
3094 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
3095 * @ap: ATA port to which the command was being sent
3096 * @cmd: SCSI command to dump
3098 * Prints the contents of a SCSI command via printk().
3101 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
3102 struct scsi_cmnd *cmd)
3104 #ifdef ATA_DEBUG
3105 struct scsi_device *scsidev = cmd->device;
3106 u8 *scsicmd = cmd->cmnd;
3108 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
3109 ap->print_id,
3110 scsidev->channel, scsidev->id, scsidev->lun,
3111 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
3112 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
3113 scsicmd[8]);
3114 #endif
3117 static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
3118 void (*done)(struct scsi_cmnd *),
3119 struct ata_device *dev)
3121 u8 scsi_op = scmd->cmnd[0];
3122 ata_xlat_func_t xlat_func;
3123 int rc = 0;
3125 if (dev->class == ATA_DEV_ATA) {
3126 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
3127 goto bad_cdb_len;
3129 xlat_func = ata_get_xlat_func(dev, scsi_op);
3130 } else {
3131 if (unlikely(!scmd->cmd_len))
3132 goto bad_cdb_len;
3134 xlat_func = NULL;
3135 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
3136 /* relay SCSI command to ATAPI device */
3137 int len = COMMAND_SIZE(scsi_op);
3138 if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
3139 goto bad_cdb_len;
3141 xlat_func = atapi_xlat;
3142 } else {
3143 /* ATA_16 passthru, treat as an ATA command */
3144 if (unlikely(scmd->cmd_len > 16))
3145 goto bad_cdb_len;
3147 xlat_func = ata_get_xlat_func(dev, scsi_op);
3151 if (xlat_func)
3152 rc = ata_scsi_translate(dev, scmd, done, xlat_func);
3153 else
3154 ata_scsi_simulate(dev, scmd, done);
3156 return rc;
3158 bad_cdb_len:
3159 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
3160 scmd->cmd_len, scsi_op, dev->cdb_len);
3161 scmd->result = DID_ERROR << 16;
3162 done(scmd);
3163 return 0;
3167 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
3168 * @cmd: SCSI command to be sent
3169 * @done: Completion function, called when command is complete
3171 * In some cases, this function translates SCSI commands into
3172 * ATA taskfiles, and queues the taskfiles to be sent to
3173 * hardware. In other cases, this function simulates a
3174 * SCSI device by evaluating and responding to certain
3175 * SCSI commands. This creates the overall effect of
3176 * ATA and ATAPI devices appearing as SCSI devices.
3178 * LOCKING:
3179 * Releases scsi-layer-held lock, and obtains host lock.
3181 * RETURNS:
3182 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3183 * 0 otherwise.
3185 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
3187 struct ata_port *ap;
3188 struct ata_device *dev;
3189 struct scsi_device *scsidev = cmd->device;
3190 struct Scsi_Host *shost = scsidev->host;
3191 int rc = 0;
3193 ap = ata_shost_to_port(shost);
3195 spin_unlock(shost->host_lock);
3196 spin_lock(ap->lock);
3198 ata_scsi_dump_cdb(ap, cmd);
3200 dev = ata_scsi_find_dev(ap, scsidev);
3201 if (likely(dev))
3202 rc = __ata_scsi_queuecmd(cmd, done, dev);
3203 else {
3204 cmd->result = (DID_BAD_TARGET << 16);
3205 done(cmd);
3208 spin_unlock(ap->lock);
3209 spin_lock(shost->host_lock);
3210 return rc;
3214 * ata_scsi_simulate - simulate SCSI command on ATA device
3215 * @dev: the target device
3216 * @cmd: SCSI command being sent to device.
3217 * @done: SCSI command completion function.
3219 * Interprets and directly executes a select list of SCSI commands
3220 * that can be handled internally.
3222 * LOCKING:
3223 * spin_lock_irqsave(host lock)
3226 void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
3227 void (*done)(struct scsi_cmnd *))
3229 struct ata_scsi_args args;
3230 const u8 *scsicmd = cmd->cmnd;
3231 u8 tmp8;
3233 args.dev = dev;
3234 args.id = dev->id;
3235 args.cmd = cmd;
3236 args.done = done;
3238 switch(scsicmd[0]) {
3239 /* TODO: worth improving? */
3240 case FORMAT_UNIT:
3241 ata_scsi_invalid_field(cmd, done);
3242 break;
3244 case INQUIRY:
3245 if (scsicmd[1] & 2) /* is CmdDt set? */
3246 ata_scsi_invalid_field(cmd, done);
3247 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
3248 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
3249 else switch (scsicmd[2]) {
3250 case 0x00:
3251 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
3252 break;
3253 case 0x80:
3254 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
3255 break;
3256 case 0x83:
3257 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
3258 break;
3259 case 0x89:
3260 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
3261 break;
3262 case 0xb0:
3263 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
3264 break;
3265 case 0xb1:
3266 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
3267 break;
3268 default:
3269 ata_scsi_invalid_field(cmd, done);
3270 break;
3272 break;
3274 case MODE_SENSE:
3275 case MODE_SENSE_10:
3276 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
3277 break;
3279 case MODE_SELECT: /* unconditionally return */
3280 case MODE_SELECT_10: /* bad-field-in-cdb */
3281 ata_scsi_invalid_field(cmd, done);
3282 break;
3284 case READ_CAPACITY:
3285 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3286 break;
3288 case SERVICE_ACTION_IN:
3289 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
3290 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3291 else
3292 ata_scsi_invalid_field(cmd, done);
3293 break;
3295 case REPORT_LUNS:
3296 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
3297 break;
3299 case REQUEST_SENSE:
3300 ata_scsi_set_sense(cmd, 0, 0, 0);
3301 cmd->result = (DRIVER_SENSE << 24);
3302 done(cmd);
3303 break;
3305 /* if we reach this, then writeback caching is disabled,
3306 * turning this into a no-op.
3308 case SYNCHRONIZE_CACHE:
3309 /* fall through */
3311 /* no-op's, complete with success */
3312 case REZERO_UNIT:
3313 case SEEK_6:
3314 case SEEK_10:
3315 case TEST_UNIT_READY:
3316 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3317 break;
3319 case SEND_DIAGNOSTIC:
3320 tmp8 = scsicmd[1] & ~(1 << 3);
3321 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
3322 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3323 else
3324 ata_scsi_invalid_field(cmd, done);
3325 break;
3327 /* all other commands */
3328 default:
3329 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
3330 /* "Invalid command operation code" */
3331 done(cmd);
3332 break;
3336 int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
3338 int i, rc;
3340 for (i = 0; i < host->n_ports; i++) {
3341 struct ata_port *ap = host->ports[i];
3342 struct Scsi_Host *shost;
3344 rc = -ENOMEM;
3345 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
3346 if (!shost)
3347 goto err_alloc;
3349 *(struct ata_port **)&shost->hostdata[0] = ap;
3350 ap->scsi_host = shost;
3352 shost->transportt = &ata_scsi_transport_template;
3353 shost->unique_id = ap->print_id;
3354 shost->max_id = 16;
3355 shost->max_lun = 1;
3356 shost->max_channel = 1;
3357 shost->max_cmd_len = 16;
3359 /* Schedule policy is determined by ->qc_defer()
3360 * callback and it needs to see every deferred qc.
3361 * Set host_blocked to 1 to prevent SCSI midlayer from
3362 * automatically deferring requests.
3364 shost->max_host_blocked = 1;
3366 rc = scsi_add_host(ap->scsi_host, ap->host->dev);
3367 if (rc)
3368 goto err_add;
3371 return 0;
3373 err_add:
3374 scsi_host_put(host->ports[i]->scsi_host);
3375 err_alloc:
3376 while (--i >= 0) {
3377 struct Scsi_Host *shost = host->ports[i]->scsi_host;
3379 scsi_remove_host(shost);
3380 scsi_host_put(shost);
3382 return rc;
3385 void ata_scsi_scan_host(struct ata_port *ap, int sync)
3387 int tries = 5;
3388 struct ata_device *last_failed_dev = NULL;
3389 struct ata_link *link;
3390 struct ata_device *dev;
3392 repeat:
3393 ata_for_each_link(link, ap, EDGE) {
3394 ata_for_each_dev(dev, link, ENABLED) {
3395 struct scsi_device *sdev;
3396 int channel = 0, id = 0;
3398 if (dev->sdev)
3399 continue;
3401 if (ata_is_host_link(link))
3402 id = dev->devno;
3403 else
3404 channel = link->pmp;
3406 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
3407 NULL);
3408 if (!IS_ERR(sdev)) {
3409 dev->sdev = sdev;
3410 scsi_device_put(sdev);
3415 /* If we scanned while EH was in progress or allocation
3416 * failure occurred, scan would have failed silently. Check
3417 * whether all devices are attached.
3419 ata_for_each_link(link, ap, EDGE) {
3420 ata_for_each_dev(dev, link, ENABLED) {
3421 if (!dev->sdev)
3422 goto exit_loop;
3425 exit_loop:
3426 if (!link)
3427 return;
3429 /* we're missing some SCSI devices */
3430 if (sync) {
3431 /* If caller requested synchrnous scan && we've made
3432 * any progress, sleep briefly and repeat.
3434 if (dev != last_failed_dev) {
3435 msleep(100);
3436 last_failed_dev = dev;
3437 goto repeat;
3440 /* We might be failing to detect boot device, give it
3441 * a few more chances.
3443 if (--tries) {
3444 msleep(100);
3445 goto repeat;
3448 ata_port_printk(ap, KERN_ERR, "WARNING: synchronous SCSI scan "
3449 "failed without making any progress,\n"
3450 " switching to async\n");
3453 queue_delayed_work(ata_aux_wq, &ap->hotplug_task,
3454 round_jiffies_relative(HZ));
3458 * ata_scsi_offline_dev - offline attached SCSI device
3459 * @dev: ATA device to offline attached SCSI device for
3461 * This function is called from ata_eh_hotplug() and responsible
3462 * for taking the SCSI device attached to @dev offline. This
3463 * function is called with host lock which protects dev->sdev
3464 * against clearing.
3466 * LOCKING:
3467 * spin_lock_irqsave(host lock)
3469 * RETURNS:
3470 * 1 if attached SCSI device exists, 0 otherwise.
3472 int ata_scsi_offline_dev(struct ata_device *dev)
3474 if (dev->sdev) {
3475 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
3476 return 1;
3478 return 0;
3482 * ata_scsi_remove_dev - remove attached SCSI device
3483 * @dev: ATA device to remove attached SCSI device for
3485 * This function is called from ata_eh_scsi_hotplug() and
3486 * responsible for removing the SCSI device attached to @dev.
3488 * LOCKING:
3489 * Kernel thread context (may sleep).
3491 static void ata_scsi_remove_dev(struct ata_device *dev)
3493 struct ata_port *ap = dev->link->ap;
3494 struct scsi_device *sdev;
3495 unsigned long flags;
3497 /* Alas, we need to grab scan_mutex to ensure SCSI device
3498 * state doesn't change underneath us and thus
3499 * scsi_device_get() always succeeds. The mutex locking can
3500 * be removed if there is __scsi_device_get() interface which
3501 * increments reference counts regardless of device state.
3503 mutex_lock(&ap->scsi_host->scan_mutex);
3504 spin_lock_irqsave(ap->lock, flags);
3506 /* clearing dev->sdev is protected by host lock */
3507 sdev = dev->sdev;
3508 dev->sdev = NULL;
3510 if (sdev) {
3511 /* If user initiated unplug races with us, sdev can go
3512 * away underneath us after the host lock and
3513 * scan_mutex are released. Hold onto it.
3515 if (scsi_device_get(sdev) == 0) {
3516 /* The following ensures the attached sdev is
3517 * offline on return from ata_scsi_offline_dev()
3518 * regardless it wins or loses the race
3519 * against this function.
3521 scsi_device_set_state(sdev, SDEV_OFFLINE);
3522 } else {
3523 WARN_ON(1);
3524 sdev = NULL;
3528 spin_unlock_irqrestore(ap->lock, flags);
3529 mutex_unlock(&ap->scsi_host->scan_mutex);
3531 if (sdev) {
3532 ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
3533 dev_name(&sdev->sdev_gendev));
3535 scsi_remove_device(sdev);
3536 scsi_device_put(sdev);
3540 static void ata_scsi_handle_link_detach(struct ata_link *link)
3542 struct ata_port *ap = link->ap;
3543 struct ata_device *dev;
3545 ata_for_each_dev(dev, link, ALL) {
3546 unsigned long flags;
3548 if (!(dev->flags & ATA_DFLAG_DETACHED))
3549 continue;
3551 spin_lock_irqsave(ap->lock, flags);
3552 dev->flags &= ~ATA_DFLAG_DETACHED;
3553 spin_unlock_irqrestore(ap->lock, flags);
3555 ata_scsi_remove_dev(dev);
3560 * ata_scsi_media_change_notify - send media change event
3561 * @dev: Pointer to the disk device with media change event
3563 * Tell the block layer to send a media change notification
3564 * event.
3566 * LOCKING:
3567 * spin_lock_irqsave(host lock)
3569 void ata_scsi_media_change_notify(struct ata_device *dev)
3571 if (dev->sdev)
3572 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
3573 GFP_ATOMIC);
3577 * ata_scsi_hotplug - SCSI part of hotplug
3578 * @work: Pointer to ATA port to perform SCSI hotplug on
3580 * Perform SCSI part of hotplug. It's executed from a separate
3581 * workqueue after EH completes. This is necessary because SCSI
3582 * hot plugging requires working EH and hot unplugging is
3583 * synchronized with hot plugging with a mutex.
3585 * LOCKING:
3586 * Kernel thread context (may sleep).
3588 void ata_scsi_hotplug(struct work_struct *work)
3590 struct ata_port *ap =
3591 container_of(work, struct ata_port, hotplug_task.work);
3592 int i;
3594 if (ap->pflags & ATA_PFLAG_UNLOADING) {
3595 DPRINTK("ENTER/EXIT - unloading\n");
3596 return;
3599 DPRINTK("ENTER\n");
3601 /* Unplug detached devices. We cannot use link iterator here
3602 * because PMP links have to be scanned even if PMP is
3603 * currently not attached. Iterate manually.
3605 ata_scsi_handle_link_detach(&ap->link);
3606 if (ap->pmp_link)
3607 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
3608 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
3610 /* scan for new ones */
3611 ata_scsi_scan_host(ap, 0);
3613 DPRINTK("EXIT\n");
3617 * ata_scsi_user_scan - indication for user-initiated bus scan
3618 * @shost: SCSI host to scan
3619 * @channel: Channel to scan
3620 * @id: ID to scan
3621 * @lun: LUN to scan
3623 * This function is called when user explicitly requests bus
3624 * scan. Set probe pending flag and invoke EH.
3626 * LOCKING:
3627 * SCSI layer (we don't care)
3629 * RETURNS:
3630 * Zero.
3632 static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
3633 unsigned int id, unsigned int lun)
3635 struct ata_port *ap = ata_shost_to_port(shost);
3636 unsigned long flags;
3637 int devno, rc = 0;
3639 if (!ap->ops->error_handler)
3640 return -EOPNOTSUPP;
3642 if (lun != SCAN_WILD_CARD && lun)
3643 return -EINVAL;
3645 if (!sata_pmp_attached(ap)) {
3646 if (channel != SCAN_WILD_CARD && channel)
3647 return -EINVAL;
3648 devno = id;
3649 } else {
3650 if (id != SCAN_WILD_CARD && id)
3651 return -EINVAL;
3652 devno = channel;
3655 spin_lock_irqsave(ap->lock, flags);
3657 if (devno == SCAN_WILD_CARD) {
3658 struct ata_link *link;
3660 ata_for_each_link(link, ap, EDGE) {
3661 struct ata_eh_info *ehi = &link->eh_info;
3662 ehi->probe_mask |= ATA_ALL_DEVICES;
3663 ehi->action |= ATA_EH_RESET;
3665 } else {
3666 struct ata_device *dev = ata_find_dev(ap, devno);
3668 if (dev) {
3669 struct ata_eh_info *ehi = &dev->link->eh_info;
3670 ehi->probe_mask |= 1 << dev->devno;
3671 ehi->action |= ATA_EH_RESET;
3672 } else
3673 rc = -EINVAL;
3676 if (rc == 0) {
3677 ata_port_schedule_eh(ap);
3678 spin_unlock_irqrestore(ap->lock, flags);
3679 ata_port_wait_eh(ap);
3680 } else
3681 spin_unlock_irqrestore(ap->lock, flags);
3683 return rc;
3687 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
3688 * @work: Pointer to ATA port to perform scsi_rescan_device()
3690 * After ATA pass thru (SAT) commands are executed successfully,
3691 * libata need to propagate the changes to SCSI layer. This
3692 * function must be executed from ata_aux_wq such that sdev
3693 * attach/detach don't race with rescan.
3695 * LOCKING:
3696 * Kernel thread context (may sleep).
3698 void ata_scsi_dev_rescan(struct work_struct *work)
3700 struct ata_port *ap =
3701 container_of(work, struct ata_port, scsi_rescan_task);
3702 struct ata_link *link;
3703 struct ata_device *dev;
3704 unsigned long flags;
3706 spin_lock_irqsave(ap->lock, flags);
3708 ata_for_each_link(link, ap, EDGE) {
3709 ata_for_each_dev(dev, link, ENABLED) {
3710 struct scsi_device *sdev = dev->sdev;
3712 if (!sdev)
3713 continue;
3714 if (scsi_device_get(sdev))
3715 continue;
3717 spin_unlock_irqrestore(ap->lock, flags);
3718 scsi_rescan_device(&(sdev->sdev_gendev));
3719 scsi_device_put(sdev);
3720 spin_lock_irqsave(ap->lock, flags);
3724 spin_unlock_irqrestore(ap->lock, flags);
3728 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
3729 * @host: ATA host container for all SAS ports
3730 * @port_info: Information from low-level host driver
3731 * @shost: SCSI host that the scsi device is attached to
3733 * LOCKING:
3734 * PCI/etc. bus probe sem.
3736 * RETURNS:
3737 * ata_port pointer on success / NULL on failure.
3740 struct ata_port *ata_sas_port_alloc(struct ata_host *host,
3741 struct ata_port_info *port_info,
3742 struct Scsi_Host *shost)
3744 struct ata_port *ap;
3746 ap = ata_port_alloc(host);
3747 if (!ap)
3748 return NULL;
3750 ap->port_no = 0;
3751 ap->lock = shost->host_lock;
3752 ap->pio_mask = port_info->pio_mask;
3753 ap->mwdma_mask = port_info->mwdma_mask;
3754 ap->udma_mask = port_info->udma_mask;
3755 ap->flags |= port_info->flags;
3756 ap->ops = port_info->port_ops;
3757 ap->cbl = ATA_CBL_SATA;
3759 return ap;
3761 EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
3764 * ata_sas_port_start - Set port up for dma.
3765 * @ap: Port to initialize
3767 * Called just after data structures for each port are
3768 * initialized.
3770 * May be used as the port_start() entry in ata_port_operations.
3772 * LOCKING:
3773 * Inherited from caller.
3775 int ata_sas_port_start(struct ata_port *ap)
3777 return 0;
3779 EXPORT_SYMBOL_GPL(ata_sas_port_start);
3782 * ata_port_stop - Undo ata_sas_port_start()
3783 * @ap: Port to shut down
3785 * May be used as the port_stop() entry in ata_port_operations.
3787 * LOCKING:
3788 * Inherited from caller.
3791 void ata_sas_port_stop(struct ata_port *ap)
3794 EXPORT_SYMBOL_GPL(ata_sas_port_stop);
3797 * ata_sas_port_init - Initialize a SATA device
3798 * @ap: SATA port to initialize
3800 * LOCKING:
3801 * PCI/etc. bus probe sem.
3803 * RETURNS:
3804 * Zero on success, non-zero on error.
3807 int ata_sas_port_init(struct ata_port *ap)
3809 int rc = ap->ops->port_start(ap);
3811 if (!rc) {
3812 ap->print_id = ata_print_id++;
3813 rc = ata_bus_probe(ap);
3816 return rc;
3818 EXPORT_SYMBOL_GPL(ata_sas_port_init);
3821 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
3822 * @ap: SATA port to destroy
3826 void ata_sas_port_destroy(struct ata_port *ap)
3828 if (ap->ops->port_stop)
3829 ap->ops->port_stop(ap);
3830 kfree(ap);
3832 EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
3835 * ata_sas_slave_configure - Default slave_config routine for libata devices
3836 * @sdev: SCSI device to configure
3837 * @ap: ATA port to which SCSI device is attached
3839 * RETURNS:
3840 * Zero.
3843 int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
3845 ata_scsi_sdev_config(sdev);
3846 ata_scsi_dev_config(sdev, ap->link.device);
3847 return 0;
3849 EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
3852 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
3853 * @cmd: SCSI command to be sent
3854 * @done: Completion function, called when command is complete
3855 * @ap: ATA port to which the command is being sent
3857 * RETURNS:
3858 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3859 * 0 otherwise.
3862 int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
3863 struct ata_port *ap)
3865 int rc = 0;
3867 ata_scsi_dump_cdb(ap, cmd);
3869 if (likely(ata_dev_enabled(ap->link.device)))
3870 rc = __ata_scsi_queuecmd(cmd, done, ap->link.device);
3871 else {
3872 cmd->result = (DID_BAD_TARGET << 16);
3873 done(cmd);
3875 return rc;
3877 EXPORT_SYMBOL_GPL(ata_sas_queuecmd);