Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Garzik | af36d7f | 2005-08-28 20:18:39 -0400 | [diff] [blame] | 2 | * libata-scsi.c - helper library for ATA |
| 3 | * |
Tejun Heo | 8c3d3d4 | 2013-05-14 11:09:50 -0700 | [diff] [blame] | 4 | * Maintained by: Tejun Heo <tj@kernel.org> |
Jeff Garzik | af36d7f | 2005-08-28 20:18:39 -0400 | [diff] [blame] | 5 | * Please ALWAYS copy linux-ide@vger.kernel.org |
| 6 | * on emails. |
| 7 | * |
| 8 | * Copyright 2003-2004 Red Hat, Inc. All rights reserved. |
| 9 | * Copyright 2003-2004 Jeff Garzik |
| 10 | * |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2, or (at your option) |
| 15 | * any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; see the file COPYING. If not, write to |
| 24 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 | * |
| 26 | * |
| 27 | * libata documentation is available via 'make {ps|pdf}docs', |
| 28 | * as Documentation/DocBook/libata.* |
| 29 | * |
| 30 | * Hardware documentation available from |
| 31 | * - http://www.t10.org/ |
| 32 | * - http://www.t13.org/ |
| 33 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
| 35 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/kernel.h> |
| 38 | #include <linux/blkdev.h> |
| 39 | #include <linux/spinlock.h> |
Paul Gortmaker | 38789fd | 2011-07-17 15:33:58 -0400 | [diff] [blame] | 40 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <scsi/scsi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <scsi/scsi_host.h> |
Christoph Hellwig | beb4048 | 2006-06-10 18:01:03 +0200 | [diff] [blame] | 43 | #include <scsi/scsi_cmnd.h> |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 44 | #include <scsi/scsi_eh.h> |
Jeff Garzik | 005a5a0 | 2005-10-30 23:31:48 -0500 | [diff] [blame] | 45 | #include <scsi/scsi_device.h> |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 46 | #include <scsi/scsi_tcq.h> |
Tejun Heo | 30afc84 | 2006-03-18 18:40:14 +0900 | [diff] [blame] | 47 | #include <scsi/scsi_transport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/libata.h> |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 49 | #include <linux/hdreg.h> |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 50 | #include <linux/uaccess.h> |
Rafael J. Wysocki | 2a6e58d | 2009-01-19 20:56:43 +0100 | [diff] [blame] | 51 | #include <linux/suspend.h> |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 52 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | #include "libata.h" |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 55 | #include "libata-transport.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 57 | #define ATA_SCSI_RBUF_SIZE 4096 |
| 58 | |
| 59 | static DEFINE_SPINLOCK(ata_scsi_rbuf_lock); |
| 60 | static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE]; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 61 | |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 62 | typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc); |
Tejun Heo | ab5b3a5 | 2006-05-31 18:27:34 +0900 | [diff] [blame] | 63 | |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 64 | static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap, |
Tejun Heo | ab5b3a5 | 2006-05-31 18:27:34 +0900 | [diff] [blame] | 65 | const struct scsi_device *scsidev); |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 66 | static struct ata_device *ata_scsi_find_dev(struct ata_port *ap, |
Tejun Heo | ab5b3a5 | 2006-05-31 18:27:34 +0900 | [diff] [blame] | 67 | const struct scsi_device *scsidev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 69 | #define RW_RECOVERY_MPAGE 0x1 |
| 70 | #define RW_RECOVERY_MPAGE_LEN 12 |
| 71 | #define CACHE_MPAGE 0x8 |
| 72 | #define CACHE_MPAGE_LEN 20 |
| 73 | #define CONTROL_MPAGE 0xa |
| 74 | #define CONTROL_MPAGE_LEN 12 |
| 75 | #define ALL_MPAGES 0x3f |
| 76 | #define ALL_SUB_MPAGES 0xff |
| 77 | |
| 78 | |
Jeff Garzik | 24f7568 | 2007-09-21 07:54:49 -0400 | [diff] [blame] | 79 | static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = { |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 80 | RW_RECOVERY_MPAGE, |
| 81 | RW_RECOVERY_MPAGE_LEN - 2, |
Jeff Garzik | 24f7568 | 2007-09-21 07:54:49 -0400 | [diff] [blame] | 82 | (1 << 7), /* AWRE */ |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 83 | 0, /* read retry count */ |
| 84 | 0, 0, 0, 0, |
| 85 | 0, /* write retry count */ |
| 86 | 0, 0, 0 |
| 87 | }; |
| 88 | |
| 89 | static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = { |
| 90 | CACHE_MPAGE, |
| 91 | CACHE_MPAGE_LEN - 2, |
| 92 | 0, /* contains WCE, needs to be 0 for logic */ |
| 93 | 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 94 | 0, /* contains DRA, needs to be 0 for logic */ |
| 95 | 0, 0, 0, 0, 0, 0, 0 |
| 96 | }; |
| 97 | |
| 98 | static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = { |
| 99 | CONTROL_MPAGE, |
| 100 | CONTROL_MPAGE_LEN - 2, |
| 101 | 2, /* DSENSE=0, GLTSD=1 */ |
| 102 | 0, /* [QAM+QERR may be 1, see 05-359r1] */ |
| 103 | 0, 0, 0, 0, 0xff, 0xff, |
| 104 | 0, 30 /* extended self test time, see 05-359r1 */ |
| 105 | }; |
| 106 | |
Tejun Heo | c93b263 | 2010-09-01 17:50:04 +0200 | [diff] [blame] | 107 | static const char *ata_lpm_policy_names[] = { |
| 108 | [ATA_LPM_UNKNOWN] = "max_performance", |
| 109 | [ATA_LPM_MAX_POWER] = "max_performance", |
| 110 | [ATA_LPM_MED_POWER] = "medium_power", |
| 111 | [ATA_LPM_MIN_POWER] = "min_power", |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 112 | }; |
| 113 | |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 114 | static ssize_t ata_scsi_lpm_store(struct device *device, |
Tejun Heo | c93b263 | 2010-09-01 17:50:04 +0200 | [diff] [blame] | 115 | struct device_attribute *attr, |
| 116 | const char *buf, size_t count) |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 117 | { |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 118 | struct Scsi_Host *shost = class_to_shost(device); |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 119 | struct ata_port *ap = ata_shost_to_port(shost); |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 120 | struct ata_link *link; |
| 121 | struct ata_device *dev; |
Tejun Heo | c93b263 | 2010-09-01 17:50:04 +0200 | [diff] [blame] | 122 | enum ata_lpm_policy policy; |
Tejun Heo | 6b7ae95 | 2010-09-01 17:50:06 +0200 | [diff] [blame] | 123 | unsigned long flags; |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 124 | |
Tejun Heo | c93b263 | 2010-09-01 17:50:04 +0200 | [diff] [blame] | 125 | /* UNKNOWN is internal state, iterate from MAX_POWER */ |
| 126 | for (policy = ATA_LPM_MAX_POWER; |
| 127 | policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) { |
| 128 | const char *name = ata_lpm_policy_names[policy]; |
| 129 | |
| 130 | if (strncmp(name, buf, strlen(name)) == 0) |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 131 | break; |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 132 | } |
Tejun Heo | c93b263 | 2010-09-01 17:50:04 +0200 | [diff] [blame] | 133 | if (policy == ARRAY_SIZE(ata_lpm_policy_names)) |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 134 | return -EINVAL; |
| 135 | |
Tejun Heo | 6b7ae95 | 2010-09-01 17:50:06 +0200 | [diff] [blame] | 136 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 137 | |
| 138 | ata_for_each_link(link, ap, EDGE) { |
| 139 | ata_for_each_dev(dev, &ap->link, ENABLED) { |
| 140 | if (dev->horkage & ATA_HORKAGE_NOLPM) { |
| 141 | count = -EOPNOTSUPP; |
| 142 | goto out_unlock; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Tejun Heo | 6b7ae95 | 2010-09-01 17:50:06 +0200 | [diff] [blame] | 147 | ap->target_lpm_policy = policy; |
| 148 | ata_port_schedule_eh(ap); |
Tejun Heo | ecd75ad | 2014-01-16 09:47:17 -0500 | [diff] [blame] | 149 | out_unlock: |
Tejun Heo | 6b7ae95 | 2010-09-01 17:50:06 +0200 | [diff] [blame] | 150 | spin_unlock_irqrestore(ap->lock, flags); |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 151 | return count; |
| 152 | } |
| 153 | |
Tejun Heo | c93b263 | 2010-09-01 17:50:04 +0200 | [diff] [blame] | 154 | static ssize_t ata_scsi_lpm_show(struct device *dev, |
| 155 | struct device_attribute *attr, char *buf) |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 156 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 157 | struct Scsi_Host *shost = class_to_shost(dev); |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 158 | struct ata_port *ap = ata_shost_to_port(shost); |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 159 | |
Tejun Heo | 6b7ae95 | 2010-09-01 17:50:06 +0200 | [diff] [blame] | 160 | if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names)) |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 161 | return -EINVAL; |
| 162 | |
Tejun Heo | c93b263 | 2010-09-01 17:50:04 +0200 | [diff] [blame] | 163 | return snprintf(buf, PAGE_SIZE, "%s\n", |
Tejun Heo | 6b7ae95 | 2010-09-01 17:50:06 +0200 | [diff] [blame] | 164 | ata_lpm_policy_names[ap->target_lpm_policy]); |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 165 | } |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 166 | DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR, |
Tejun Heo | c93b263 | 2010-09-01 17:50:04 +0200 | [diff] [blame] | 167 | ata_scsi_lpm_show, ata_scsi_lpm_store); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 168 | EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy); |
Kristen Carlson Accardi | ca77329 | 2007-10-25 00:58:59 -0400 | [diff] [blame] | 169 | |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 170 | static ssize_t ata_scsi_park_show(struct device *device, |
| 171 | struct device_attribute *attr, char *buf) |
| 172 | { |
| 173 | struct scsi_device *sdev = to_scsi_device(device); |
| 174 | struct ata_port *ap; |
| 175 | struct ata_link *link; |
| 176 | struct ata_device *dev; |
Tejun Heo | 3948b6f | 2016-02-18 11:50:37 -0500 | [diff] [blame] | 177 | unsigned long now; |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 178 | unsigned int uninitialized_var(msecs); |
| 179 | int rc = 0; |
| 180 | |
| 181 | ap = ata_shost_to_port(sdev->host); |
| 182 | |
Tejun Heo | 3948b6f | 2016-02-18 11:50:37 -0500 | [diff] [blame] | 183 | spin_lock_irq(ap->lock); |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 184 | dev = ata_scsi_find_dev(ap, sdev); |
| 185 | if (!dev) { |
| 186 | rc = -ENODEV; |
| 187 | goto unlock; |
| 188 | } |
| 189 | if (dev->flags & ATA_DFLAG_NO_UNLOAD) { |
| 190 | rc = -EOPNOTSUPP; |
| 191 | goto unlock; |
| 192 | } |
| 193 | |
| 194 | link = dev->link; |
Elias Oltmanns | a464189 | 2008-11-03 19:01:08 +0900 | [diff] [blame] | 195 | now = jiffies; |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 196 | if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS && |
| 197 | link->eh_context.unloaded_mask & (1 << dev->devno) && |
Elias Oltmanns | a464189 | 2008-11-03 19:01:08 +0900 | [diff] [blame] | 198 | time_after(dev->unpark_deadline, now)) |
| 199 | msecs = jiffies_to_msecs(dev->unpark_deadline - now); |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 200 | else |
| 201 | msecs = 0; |
| 202 | |
| 203 | unlock: |
| 204 | spin_unlock_irq(ap->lock); |
| 205 | |
| 206 | return rc ? rc : snprintf(buf, 20, "%u\n", msecs); |
| 207 | } |
| 208 | |
| 209 | static ssize_t ata_scsi_park_store(struct device *device, |
| 210 | struct device_attribute *attr, |
| 211 | const char *buf, size_t len) |
| 212 | { |
| 213 | struct scsi_device *sdev = to_scsi_device(device); |
| 214 | struct ata_port *ap; |
| 215 | struct ata_device *dev; |
| 216 | long int input; |
| 217 | unsigned long flags; |
| 218 | int rc; |
| 219 | |
Jingoo Han | 42b9ab7 | 2013-07-19 15:56:41 +0900 | [diff] [blame] | 220 | rc = kstrtol(buf, 10, &input); |
| 221 | if (rc) |
| 222 | return rc; |
| 223 | if (input < -2) |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 224 | return -EINVAL; |
| 225 | if (input > ATA_TMOUT_MAX_PARK) { |
| 226 | rc = -EOVERFLOW; |
| 227 | input = ATA_TMOUT_MAX_PARK; |
| 228 | } |
| 229 | |
| 230 | ap = ata_shost_to_port(sdev->host); |
| 231 | |
| 232 | spin_lock_irqsave(ap->lock, flags); |
| 233 | dev = ata_scsi_find_dev(ap, sdev); |
| 234 | if (unlikely(!dev)) { |
| 235 | rc = -ENODEV; |
| 236 | goto unlock; |
| 237 | } |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 238 | if (dev->class != ATA_DEV_ATA && |
| 239 | dev->class != ATA_DEV_ZAC) { |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 240 | rc = -EOPNOTSUPP; |
| 241 | goto unlock; |
| 242 | } |
| 243 | |
| 244 | if (input >= 0) { |
| 245 | if (dev->flags & ATA_DFLAG_NO_UNLOAD) { |
| 246 | rc = -EOPNOTSUPP; |
| 247 | goto unlock; |
| 248 | } |
| 249 | |
| 250 | dev->unpark_deadline = ata_deadline(jiffies, input); |
| 251 | dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK; |
| 252 | ata_port_schedule_eh(ap); |
| 253 | complete(&ap->park_req_pending); |
| 254 | } else { |
| 255 | switch (input) { |
| 256 | case -1: |
| 257 | dev->flags &= ~ATA_DFLAG_NO_UNLOAD; |
| 258 | break; |
| 259 | case -2: |
| 260 | dev->flags |= ATA_DFLAG_NO_UNLOAD; |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | unlock: |
| 265 | spin_unlock_irqrestore(ap->lock, flags); |
| 266 | |
| 267 | return rc ? rc : len; |
| 268 | } |
| 269 | DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR, |
| 270 | ata_scsi_park_show, ata_scsi_park_store); |
| 271 | EXPORT_SYMBOL_GPL(dev_attr_unload_heads); |
| 272 | |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 273 | void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd, |
| 274 | u8 sk, u8 asc, u8 ascq) |
Tejun Heo | f0761be | 2008-04-28 17:16:52 +0900 | [diff] [blame] | 275 | { |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 276 | bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE); |
| 277 | |
Hannes Reinecke | 5b01e4b | 2016-04-04 11:43:54 +0200 | [diff] [blame] | 278 | if (!cmd) |
| 279 | return; |
| 280 | |
Tejun Heo | f0761be | 2008-04-28 17:16:52 +0900 | [diff] [blame] | 281 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
| 282 | |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 283 | scsi_build_sense_buffer(d_sense, cmd->sense_buffer, sk, asc, ascq); |
Tejun Heo | f0761be | 2008-04-28 17:16:52 +0900 | [diff] [blame] | 284 | } |
| 285 | |
Hannes Reinecke | 492bf62 | 2016-04-04 11:43:59 +0200 | [diff] [blame] | 286 | void ata_scsi_set_sense_information(struct ata_device *dev, |
| 287 | struct scsi_cmnd *cmd, |
| 288 | const struct ata_taskfile *tf) |
| 289 | { |
| 290 | u64 information; |
| 291 | |
| 292 | if (!cmd) |
| 293 | return; |
| 294 | |
| 295 | information = ata_tf_read_block(tf, dev); |
| 296 | if (information == U64_MAX) |
| 297 | return; |
| 298 | |
| 299 | scsi_set_sense_information(cmd->sense_buffer, |
| 300 | SCSI_SENSE_BUFFERSIZE, information); |
| 301 | } |
| 302 | |
Kristen Carlson Accardi | 18f7ba4 | 2008-06-03 10:33:55 -0700 | [diff] [blame] | 303 | static ssize_t |
| 304 | ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr, |
| 305 | const char *buf, size_t count) |
| 306 | { |
| 307 | struct Scsi_Host *shost = class_to_shost(dev); |
| 308 | struct ata_port *ap = ata_shost_to_port(shost); |
| 309 | if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM)) |
| 310 | return ap->ops->em_store(ap, buf, count); |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | |
| 314 | static ssize_t |
| 315 | ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr, |
| 316 | char *buf) |
| 317 | { |
| 318 | struct Scsi_Host *shost = class_to_shost(dev); |
| 319 | struct ata_port *ap = ata_shost_to_port(shost); |
| 320 | |
| 321 | if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM)) |
| 322 | return ap->ops->em_show(ap, buf); |
| 323 | return -EINVAL; |
| 324 | } |
Vitaly Mayatskikh | ea7a5ed | 2009-05-04 15:48:45 +0200 | [diff] [blame] | 325 | DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR, |
Kristen Carlson Accardi | 18f7ba4 | 2008-06-03 10:33:55 -0700 | [diff] [blame] | 326 | ata_scsi_em_message_show, ata_scsi_em_message_store); |
| 327 | EXPORT_SYMBOL_GPL(dev_attr_em_message); |
| 328 | |
| 329 | static ssize_t |
| 330 | ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr, |
| 331 | char *buf) |
| 332 | { |
| 333 | struct Scsi_Host *shost = class_to_shost(dev); |
| 334 | struct ata_port *ap = ata_shost_to_port(shost); |
| 335 | |
| 336 | return snprintf(buf, 23, "%d\n", ap->em_message_type); |
| 337 | } |
| 338 | DEVICE_ATTR(em_message_type, S_IRUGO, |
| 339 | ata_scsi_em_message_type_show, NULL); |
| 340 | EXPORT_SYMBOL_GPL(dev_attr_em_message_type); |
| 341 | |
| 342 | static ssize_t |
| 343 | ata_scsi_activity_show(struct device *dev, struct device_attribute *attr, |
| 344 | char *buf) |
| 345 | { |
| 346 | struct scsi_device *sdev = to_scsi_device(dev); |
| 347 | struct ata_port *ap = ata_shost_to_port(sdev->host); |
| 348 | struct ata_device *atadev = ata_scsi_find_dev(ap, sdev); |
| 349 | |
Xiaotian Feng | 26cd4d6 | 2012-12-13 16:12:18 +0800 | [diff] [blame] | 350 | if (atadev && ap->ops->sw_activity_show && |
| 351 | (ap->flags & ATA_FLAG_SW_ACTIVITY)) |
Kristen Carlson Accardi | 18f7ba4 | 2008-06-03 10:33:55 -0700 | [diff] [blame] | 352 | return ap->ops->sw_activity_show(atadev, buf); |
| 353 | return -EINVAL; |
| 354 | } |
| 355 | |
| 356 | static ssize_t |
| 357 | ata_scsi_activity_store(struct device *dev, struct device_attribute *attr, |
| 358 | const char *buf, size_t count) |
| 359 | { |
| 360 | struct scsi_device *sdev = to_scsi_device(dev); |
| 361 | struct ata_port *ap = ata_shost_to_port(sdev->host); |
| 362 | struct ata_device *atadev = ata_scsi_find_dev(ap, sdev); |
| 363 | enum sw_activity val; |
| 364 | int rc; |
| 365 | |
Xiaotian Feng | 26cd4d6 | 2012-12-13 16:12:18 +0800 | [diff] [blame] | 366 | if (atadev && ap->ops->sw_activity_store && |
| 367 | (ap->flags & ATA_FLAG_SW_ACTIVITY)) { |
Kristen Carlson Accardi | 18f7ba4 | 2008-06-03 10:33:55 -0700 | [diff] [blame] | 368 | val = simple_strtoul(buf, NULL, 0); |
| 369 | switch (val) { |
| 370 | case OFF: case BLINK_ON: case BLINK_OFF: |
| 371 | rc = ap->ops->sw_activity_store(atadev, val); |
| 372 | if (!rc) |
| 373 | return count; |
| 374 | else |
| 375 | return rc; |
| 376 | } |
| 377 | } |
| 378 | return -EINVAL; |
| 379 | } |
Vitaly Mayatskikh | ea7a5ed | 2009-05-04 15:48:45 +0200 | [diff] [blame] | 380 | DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show, |
Kristen Carlson Accardi | 18f7ba4 | 2008-06-03 10:33:55 -0700 | [diff] [blame] | 381 | ata_scsi_activity_store); |
| 382 | EXPORT_SYMBOL_GPL(dev_attr_sw_activity); |
| 383 | |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 384 | struct device_attribute *ata_common_sdev_attrs[] = { |
| 385 | &dev_attr_unload_heads, |
| 386 | NULL |
| 387 | }; |
| 388 | EXPORT_SYMBOL_GPL(ata_common_sdev_attrs); |
| 389 | |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 390 | static void ata_scsi_invalid_field(struct ata_device *dev, |
| 391 | struct scsi_cmnd *cmd) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 392 | { |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 393 | ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 394 | /* "Invalid field in cbd" */ |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 395 | cmd->scsi_done(cmd); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 396 | } |
| 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | /** |
| 399 | * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd. |
| 400 | * @sdev: SCSI device for which BIOS geometry is to be determined |
| 401 | * @bdev: block device associated with @sdev |
| 402 | * @capacity: capacity of SCSI device |
| 403 | * @geom: location to which geometry will be output |
| 404 | * |
| 405 | * Generic bios head/sector/cylinder calculator |
| 406 | * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS) |
| 407 | * mapping. Some situations may arise where the disk is not |
| 408 | * bootable if this is not used. |
| 409 | * |
| 410 | * LOCKING: |
| 411 | * Defined by the SCSI layer. We don't really care. |
| 412 | * |
| 413 | * RETURNS: |
| 414 | * Zero. |
| 415 | */ |
| 416 | int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev, |
| 417 | sector_t capacity, int geom[]) |
| 418 | { |
| 419 | geom[0] = 255; |
| 420 | geom[1] = 63; |
| 421 | sector_div(capacity, 255*63); |
| 422 | geom[2] = capacity; |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 427 | /** |
Tejun Heo | d8d9129 | 2010-05-15 20:09:34 +0200 | [diff] [blame] | 428 | * ata_scsi_unlock_native_capacity - unlock native capacity |
| 429 | * @sdev: SCSI device to adjust device capacity for |
| 430 | * |
| 431 | * This function is called if a partition on @sdev extends beyond |
| 432 | * the end of the device. It requests EH to unlock HPA. |
| 433 | * |
| 434 | * LOCKING: |
| 435 | * Defined by the SCSI layer. Might sleep. |
| 436 | */ |
| 437 | void ata_scsi_unlock_native_capacity(struct scsi_device *sdev) |
| 438 | { |
| 439 | struct ata_port *ap = ata_shost_to_port(sdev->host); |
| 440 | struct ata_device *dev; |
| 441 | unsigned long flags; |
| 442 | |
| 443 | spin_lock_irqsave(ap->lock, flags); |
| 444 | |
| 445 | dev = ata_scsi_find_dev(ap, sdev); |
| 446 | if (dev && dev->n_sectors < dev->n_native_sectors) { |
| 447 | dev->flags |= ATA_DFLAG_UNLOCK_HPA; |
| 448 | dev->link->eh_info.action |= ATA_EH_RESET; |
| 449 | ata_port_schedule_eh(ap); |
| 450 | } |
| 451 | |
| 452 | spin_unlock_irqrestore(ap->lock, flags); |
| 453 | ata_port_wait_eh(ap); |
| 454 | } |
| 455 | |
| 456 | /** |
Tejun Heo | 5924b74 | 2007-01-02 20:20:07 +0900 | [diff] [blame] | 457 | * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl |
Randy Dunlap | 5eb66fe | 2009-01-20 16:28:59 -0800 | [diff] [blame] | 458 | * @ap: target port |
Tejun Heo | 5924b74 | 2007-01-02 20:20:07 +0900 | [diff] [blame] | 459 | * @sdev: SCSI device to get identify data for |
| 460 | * @arg: User buffer area for identify data |
| 461 | * |
| 462 | * LOCKING: |
| 463 | * Defined by the SCSI layer. We don't really care. |
| 464 | * |
| 465 | * RETURNS: |
| 466 | * Zero on success, negative errno on error. |
| 467 | */ |
Jeff Garzik | 94be9a5 | 2009-01-16 10:17:09 -0500 | [diff] [blame] | 468 | static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev, |
| 469 | void __user *arg) |
Tejun Heo | 5924b74 | 2007-01-02 20:20:07 +0900 | [diff] [blame] | 470 | { |
Tejun Heo | 5924b74 | 2007-01-02 20:20:07 +0900 | [diff] [blame] | 471 | struct ata_device *dev = ata_scsi_find_dev(ap, sdev); |
| 472 | u16 __user *dst = arg; |
| 473 | char buf[40]; |
| 474 | |
| 475 | if (!dev) |
| 476 | return -ENOMSG; |
| 477 | |
| 478 | if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16))) |
| 479 | return -EFAULT; |
| 480 | |
| 481 | ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN); |
| 482 | if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN)) |
| 483 | return -EFAULT; |
| 484 | |
| 485 | ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN); |
| 486 | if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN)) |
| 487 | return -EFAULT; |
| 488 | |
| 489 | ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN); |
| 490 | if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN)) |
| 491 | return -EFAULT; |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | /** |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 497 | * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl |
Randy Dunlap | 8e8b77d | 2005-11-01 21:29:27 -0800 | [diff] [blame] | 498 | * @scsidev: Device to which we are issuing command |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 499 | * @arg: User provided data for issuing command |
| 500 | * |
| 501 | * LOCKING: |
| 502 | * Defined by the SCSI layer. We don't really care. |
| 503 | * |
| 504 | * RETURNS: |
| 505 | * Zero on success, negative errno on error. |
| 506 | */ |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 507 | int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg) |
| 508 | { |
| 509 | int rc = 0; |
| 510 | u8 scsi_cmd[MAX_COMMAND_SIZE]; |
Eran Tromer | bbe1fe7 | 2006-10-10 14:29:25 -0700 | [diff] [blame] | 511 | u8 args[4], *argbuf = NULL, *sensebuf = NULL; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 512 | int argsize = 0; |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 513 | enum dma_data_direction data_dir; |
Eran Tromer | bbe1fe7 | 2006-10-10 14:29:25 -0700 | [diff] [blame] | 514 | int cmd_result; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 515 | |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 516 | if (arg == NULL) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 517 | return -EINVAL; |
| 518 | |
| 519 | if (copy_from_user(args, arg, sizeof(args))) |
| 520 | return -EFAULT; |
| 521 | |
Eran Tromer | bbe1fe7 | 2006-10-10 14:29:25 -0700 | [diff] [blame] | 522 | sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO); |
| 523 | if (!sensebuf) |
| 524 | return -ENOMEM; |
| 525 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 526 | memset(scsi_cmd, 0, sizeof(scsi_cmd)); |
| 527 | |
| 528 | if (args[3]) { |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 529 | argsize = ATA_SECT_SIZE * args[3]; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 530 | argbuf = kmalloc(argsize, GFP_KERNEL); |
Jeff Raubitschek | 54dac83 | 2005-10-04 10:21:19 -0400 | [diff] [blame] | 531 | if (argbuf == NULL) { |
| 532 | rc = -ENOMEM; |
| 533 | goto error; |
| 534 | } |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 535 | |
| 536 | scsi_cmd[1] = (4 << 1); /* PIO Data-in */ |
| 537 | scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev, |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 538 | block count in sector count field */ |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 539 | data_dir = DMA_FROM_DEVICE; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 540 | } else { |
| 541 | scsi_cmd[1] = (3 << 1); /* Non-data */ |
Eran Tromer | bbe1fe7 | 2006-10-10 14:29:25 -0700 | [diff] [blame] | 542 | scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */ |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 543 | data_dir = DMA_NONE; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | scsi_cmd[0] = ATA_16; |
| 547 | |
| 548 | scsi_cmd[4] = args[2]; |
Bartlomiej Zolnierkiewicz | a4f1904 | 2008-10-10 22:39:20 +0200 | [diff] [blame] | 549 | if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */ |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 550 | scsi_cmd[6] = args[3]; |
| 551 | scsi_cmd[8] = args[1]; |
| 552 | scsi_cmd[10] = 0x4f; |
| 553 | scsi_cmd[12] = 0xc2; |
| 554 | } else { |
| 555 | scsi_cmd[6] = args[1]; |
| 556 | } |
| 557 | scsi_cmd[14] = args[0]; |
| 558 | |
| 559 | /* Good values for timeout and retries? Values below |
| 560 | from scsi_ioctl_send_command() for default case... */ |
Eran Tromer | bbe1fe7 | 2006-10-10 14:29:25 -0700 | [diff] [blame] | 561 | cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 562 | sensebuf, (10*HZ), 5, 0, NULL); |
Eran Tromer | bbe1fe7 | 2006-10-10 14:29:25 -0700 | [diff] [blame] | 563 | |
| 564 | if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */ |
| 565 | u8 *desc = sensebuf + 8; |
| 566 | cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */ |
| 567 | |
| 568 | /* If we set cc then ATA pass-through will cause a |
| 569 | * check condition even if no error. Filter that. */ |
| 570 | if (cmd_result & SAM_STAT_CHECK_CONDITION) { |
| 571 | struct scsi_sense_hdr sshdr; |
| 572 | scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 573 | &sshdr); |
Krzysztof Mazur | 6d3bfc7 | 2013-03-27 13:51:14 +0100 | [diff] [blame] | 574 | if (sshdr.sense_key == RECOVERED_ERROR && |
| 575 | sshdr.asc == 0 && sshdr.ascq == 0x1d) |
Eran Tromer | bbe1fe7 | 2006-10-10 14:29:25 -0700 | [diff] [blame] | 576 | cmd_result &= ~SAM_STAT_CHECK_CONDITION; |
| 577 | } |
| 578 | |
| 579 | /* Send userspace a few ATA registers (same as drivers/ide) */ |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 580 | if (sensebuf[0] == 0x72 && /* format is "descriptor" */ |
| 581 | desc[0] == 0x09) { /* code is "ATA Descriptor" */ |
| 582 | args[0] = desc[13]; /* status */ |
| 583 | args[1] = desc[3]; /* error */ |
| 584 | args[2] = desc[5]; /* sector count (0:7) */ |
Eran Tromer | bbe1fe7 | 2006-10-10 14:29:25 -0700 | [diff] [blame] | 585 | if (copy_to_user(arg, args, sizeof(args))) |
| 586 | rc = -EFAULT; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | |
| 591 | if (cmd_result) { |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 592 | rc = -EIO; |
| 593 | goto error; |
| 594 | } |
| 595 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 596 | if ((argbuf) |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 597 | && copy_to_user(arg + sizeof(args), argbuf, argsize)) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 598 | rc = -EFAULT; |
| 599 | error: |
Eran Tromer | bbe1fe7 | 2006-10-10 14:29:25 -0700 | [diff] [blame] | 600 | kfree(sensebuf); |
Jesper Juhl | 8f76078 | 2006-06-27 02:55:06 -0700 | [diff] [blame] | 601 | kfree(argbuf); |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 602 | return rc; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl |
Randy Dunlap | 8e8b77d | 2005-11-01 21:29:27 -0800 | [diff] [blame] | 607 | * @scsidev: Device to which we are issuing command |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 608 | * @arg: User provided data for issuing command |
| 609 | * |
| 610 | * LOCKING: |
| 611 | * Defined by the SCSI layer. We don't really care. |
| 612 | * |
| 613 | * RETURNS: |
| 614 | * Zero on success, negative errno on error. |
| 615 | */ |
| 616 | int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg) |
| 617 | { |
| 618 | int rc = 0; |
| 619 | u8 scsi_cmd[MAX_COMMAND_SIZE]; |
David Milburn | af068bd | 2007-01-30 00:59:15 -0800 | [diff] [blame] | 620 | u8 args[7], *sensebuf = NULL; |
| 621 | int cmd_result; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 622 | |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 623 | if (arg == NULL) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 624 | return -EINVAL; |
| 625 | |
| 626 | if (copy_from_user(args, arg, sizeof(args))) |
| 627 | return -EFAULT; |
| 628 | |
David Milburn | af068bd | 2007-01-30 00:59:15 -0800 | [diff] [blame] | 629 | sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO); |
| 630 | if (!sensebuf) |
| 631 | return -ENOMEM; |
| 632 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 633 | memset(scsi_cmd, 0, sizeof(scsi_cmd)); |
| 634 | scsi_cmd[0] = ATA_16; |
| 635 | scsi_cmd[1] = (3 << 1); /* Non-data */ |
David Milburn | af068bd | 2007-01-30 00:59:15 -0800 | [diff] [blame] | 636 | scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */ |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 637 | scsi_cmd[4] = args[1]; |
| 638 | scsi_cmd[6] = args[2]; |
| 639 | scsi_cmd[8] = args[3]; |
| 640 | scsi_cmd[10] = args[4]; |
| 641 | scsi_cmd[12] = args[5]; |
Mark Lord | 277239f | 2007-03-30 17:45:52 -0400 | [diff] [blame] | 642 | scsi_cmd[13] = args[6] & 0x4f; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 643 | scsi_cmd[14] = args[0]; |
| 644 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 645 | /* Good values for timeout and retries? Values below |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 646 | from scsi_ioctl_send_command() for default case... */ |
David Milburn | af068bd | 2007-01-30 00:59:15 -0800 | [diff] [blame] | 647 | cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 648 | sensebuf, (10*HZ), 5, 0, NULL); |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 649 | |
David Milburn | af068bd | 2007-01-30 00:59:15 -0800 | [diff] [blame] | 650 | if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */ |
| 651 | u8 *desc = sensebuf + 8; |
| 652 | cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */ |
| 653 | |
| 654 | /* If we set cc then ATA pass-through will cause a |
| 655 | * check condition even if no error. Filter that. */ |
| 656 | if (cmd_result & SAM_STAT_CHECK_CONDITION) { |
| 657 | struct scsi_sense_hdr sshdr; |
| 658 | scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, |
| 659 | &sshdr); |
Krzysztof Mazur | 6d3bfc7 | 2013-03-27 13:51:14 +0100 | [diff] [blame] | 660 | if (sshdr.sense_key == RECOVERED_ERROR && |
| 661 | sshdr.asc == 0 && sshdr.ascq == 0x1d) |
David Milburn | af068bd | 2007-01-30 00:59:15 -0800 | [diff] [blame] | 662 | cmd_result &= ~SAM_STAT_CHECK_CONDITION; |
| 663 | } |
| 664 | |
| 665 | /* Send userspace ATA registers */ |
| 666 | if (sensebuf[0] == 0x72 && /* format is "descriptor" */ |
| 667 | desc[0] == 0x09) {/* code is "ATA Descriptor" */ |
| 668 | args[0] = desc[13]; /* status */ |
| 669 | args[1] = desc[3]; /* error */ |
| 670 | args[2] = desc[5]; /* sector count (0:7) */ |
| 671 | args[3] = desc[7]; /* lbal */ |
| 672 | args[4] = desc[9]; /* lbam */ |
| 673 | args[5] = desc[11]; /* lbah */ |
| 674 | args[6] = desc[12]; /* select */ |
| 675 | if (copy_to_user(arg, args, sizeof(args))) |
| 676 | rc = -EFAULT; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | if (cmd_result) { |
| 681 | rc = -EIO; |
| 682 | goto error; |
| 683 | } |
| 684 | |
| 685 | error: |
| 686 | kfree(sensebuf); |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 687 | return rc; |
| 688 | } |
| 689 | |
Alan Cox | e3cf95d | 2009-04-09 17:31:17 +0100 | [diff] [blame] | 690 | static int ata_ioc32(struct ata_port *ap) |
| 691 | { |
| 692 | if (ap->flags & ATA_FLAG_PIO_DMA) |
| 693 | return 1; |
| 694 | if (ap->pflags & ATA_PFLAG_PIO32) |
| 695 | return 1; |
| 696 | return 0; |
| 697 | } |
| 698 | |
Jeff Garzik | 94be9a5 | 2009-01-16 10:17:09 -0500 | [diff] [blame] | 699 | int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev, |
| 700 | int cmd, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | { |
Arnd Bergmann | 287e661 | 2016-02-11 14:16:27 +0100 | [diff] [blame] | 702 | unsigned long val; |
| 703 | int rc = -EINVAL; |
Alan Cox | e3cf95d | 2009-04-09 17:31:17 +0100 | [diff] [blame] | 704 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | switch (cmd) { |
Arnd Bergmann | 287e661 | 2016-02-11 14:16:27 +0100 | [diff] [blame] | 707 | case HDIO_GET_32BIT: |
Alan Cox | e3cf95d | 2009-04-09 17:31:17 +0100 | [diff] [blame] | 708 | spin_lock_irqsave(ap->lock, flags); |
| 709 | val = ata_ioc32(ap); |
| 710 | spin_unlock_irqrestore(ap->lock, flags); |
Arnd Bergmann | 287e661 | 2016-02-11 14:16:27 +0100 | [diff] [blame] | 711 | return put_user(val, (unsigned long __user *)arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | |
Arnd Bergmann | 287e661 | 2016-02-11 14:16:27 +0100 | [diff] [blame] | 713 | case HDIO_SET_32BIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | val = (unsigned long) arg; |
Alan Cox | e3cf95d | 2009-04-09 17:31:17 +0100 | [diff] [blame] | 715 | rc = 0; |
| 716 | spin_lock_irqsave(ap->lock, flags); |
| 717 | if (ap->pflags & ATA_PFLAG_PIO32CHANGE) { |
| 718 | if (val) |
| 719 | ap->pflags |= ATA_PFLAG_PIO32; |
| 720 | else |
| 721 | ap->pflags &= ~ATA_PFLAG_PIO32; |
| 722 | } else { |
| 723 | if (val != ata_ioc32(ap)) |
| 724 | rc = -EINVAL; |
| 725 | } |
| 726 | spin_unlock_irqrestore(ap->lock, flags); |
| 727 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Tejun Heo | 5924b74 | 2007-01-02 20:20:07 +0900 | [diff] [blame] | 729 | case HDIO_GET_IDENTITY: |
Jeff Garzik | 94be9a5 | 2009-01-16 10:17:09 -0500 | [diff] [blame] | 730 | return ata_get_identity(ap, scsidev, arg); |
Tejun Heo | 5924b74 | 2007-01-02 20:20:07 +0900 | [diff] [blame] | 731 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 732 | case HDIO_DRIVE_CMD: |
| 733 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 734 | return -EACCES; |
| 735 | return ata_cmd_ioctl(scsidev, arg); |
| 736 | |
| 737 | case HDIO_DRIVE_TASK: |
| 738 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 739 | return -EACCES; |
| 740 | return ata_task_ioctl(scsidev, arg); |
| 741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | default: |
| 743 | rc = -ENOTTY; |
| 744 | break; |
| 745 | } |
| 746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | return rc; |
| 748 | } |
Jeff Garzik | 94be9a5 | 2009-01-16 10:17:09 -0500 | [diff] [blame] | 749 | EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl); |
| 750 | |
| 751 | int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg) |
| 752 | { |
| 753 | return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host), |
| 754 | scsidev, cmd, arg); |
| 755 | } |
| 756 | EXPORT_SYMBOL_GPL(ata_scsi_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
| 758 | /** |
| 759 | * ata_scsi_qc_new - acquire new ata_queued_cmd reference |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | * @dev: ATA device to which the new command is attached |
| 761 | * @cmd: SCSI command that originated this ATA command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | * |
| 763 | * Obtain a reference to an unused ata_queued_cmd structure, |
| 764 | * which is the basic libata structure representing a single |
| 765 | * ATA command sent to the hardware. |
| 766 | * |
| 767 | * If a command was available, fill in the SCSI-specific |
| 768 | * portions of the structure with information on the |
| 769 | * current command. |
| 770 | * |
| 771 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 772 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | * |
| 774 | * RETURNS: |
| 775 | * Command allocated, or %NULL if none available. |
| 776 | */ |
Adrian Bunk | 7102d23 | 2007-01-04 00:09:36 +0100 | [diff] [blame] | 777 | static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev, |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 778 | struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | { |
| 780 | struct ata_queued_cmd *qc; |
| 781 | |
Shaohua Li | 12cb5ce | 2015-01-15 17:32:27 -0800 | [diff] [blame] | 782 | qc = ata_qc_new_init(dev, cmd->request->tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | if (qc) { |
| 784 | qc->scsicmd = cmd; |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 785 | qc->scsidone = cmd->scsi_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
Tejun Heo | ff2aeb1 | 2007-12-05 16:43:11 +0900 | [diff] [blame] | 787 | qc->sg = scsi_sglist(cmd); |
Boaz Harrosh | 7120165 | 2007-09-18 17:48:50 +0200 | [diff] [blame] | 788 | qc->n_elem = scsi_sg_count(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } else { |
| 790 | cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1); |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 791 | cmd->scsi_done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | return qc; |
| 795 | } |
| 796 | |
Tejun Heo | aacda37 | 2008-03-18 17:47:43 +0900 | [diff] [blame] | 797 | static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc) |
| 798 | { |
| 799 | struct scsi_cmnd *scmd = qc->scsicmd; |
| 800 | |
| 801 | qc->extrabytes = scmd->request->extra_len; |
| 802 | qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes; |
| 803 | } |
| 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | /** |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 806 | * ata_dump_status - user friendly display of error info |
| 807 | * @id: id of the port in question |
| 808 | * @tf: ptr to filled out taskfile |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | * |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 810 | * Decode and dump the ATA error/status registers for the user so |
| 811 | * that they have some idea what really happened at the non |
| 812 | * make-believe layer. |
| 813 | * |
| 814 | * LOCKING: |
| 815 | * inherited from caller |
| 816 | */ |
Adrian Bunk | 7102d23 | 2007-01-04 00:09:36 +0100 | [diff] [blame] | 817 | static void ata_dump_status(unsigned id, struct ata_taskfile *tf) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 818 | { |
| 819 | u8 stat = tf->command, err = tf->feature; |
| 820 | |
| 821 | printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat); |
| 822 | if (stat & ATA_BUSY) { |
| 823 | printk("Busy }\n"); /* Data is not valid in this case */ |
| 824 | } else { |
Hannes Reinecke | 1308d7f | 2015-03-27 16:46:34 +0100 | [diff] [blame] | 825 | if (stat & ATA_DRDY) printk("DriveReady "); |
| 826 | if (stat & ATA_DF) printk("DeviceFault "); |
| 827 | if (stat & ATA_DSC) printk("SeekComplete "); |
| 828 | if (stat & ATA_DRQ) printk("DataRequest "); |
| 829 | if (stat & ATA_CORR) printk("CorrectedError "); |
| 830 | if (stat & ATA_SENSE) printk("Sense "); |
| 831 | if (stat & ATA_ERR) printk("Error "); |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 832 | printk("}\n"); |
| 833 | |
| 834 | if (err) { |
| 835 | printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err); |
Hannes Reinecke | 1308d7f | 2015-03-27 16:46:34 +0100 | [diff] [blame] | 836 | if (err & ATA_ABORTED) printk("DriveStatusError "); |
| 837 | if (err & ATA_ICRC) { |
| 838 | if (err & ATA_ABORTED) |
| 839 | printk("BadCRC "); |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 840 | else printk("Sector "); |
| 841 | } |
Hannes Reinecke | 1308d7f | 2015-03-27 16:46:34 +0100 | [diff] [blame] | 842 | if (err & ATA_UNC) printk("UncorrectableError "); |
| 843 | if (err & ATA_IDNF) printk("SectorIdNotFound "); |
| 844 | if (err & ATA_TRK0NF) printk("TrackZeroNotFound "); |
| 845 | if (err & ATA_AMNF) printk("AddrMarkNotFound "); |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 846 | printk("}\n"); |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * ata_to_sense_error - convert ATA error to SCSI error |
Randy Dunlap | 8e8b77d | 2005-11-01 21:29:27 -0800 | [diff] [blame] | 853 | * @id: ATA device number |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 854 | * @drv_stat: value contained in ATA status register |
| 855 | * @drv_err: value contained in ATA error register |
| 856 | * @sk: the sense key we'll fill out |
| 857 | * @asc: the additional sense code we'll fill out |
| 858 | * @ascq: the additional sense code qualifier we'll fill out |
Tejun Heo | 246619d | 2006-05-15 20:58:16 +0900 | [diff] [blame] | 859 | * @verbose: be verbose |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 860 | * |
| 861 | * Converts an ATA error into a SCSI error. Fill out pointers to |
| 862 | * SK, ASC, and ASCQ bytes for later use in fixed or descriptor |
| 863 | * format sense blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | * |
| 865 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 866 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | */ |
Adrian Bunk | 7102d23 | 2007-01-04 00:09:36 +0100 | [diff] [blame] | 868 | static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, |
| 869 | u8 *asc, u8 *ascq, int verbose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | { |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 871 | int i; |
Jeff Garzik | ffe75ef | 2005-10-09 10:40:44 -0400 | [diff] [blame] | 872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | /* Based on the 3ware driver translation table */ |
Arjan van de Ven | 98ac62d | 2005-11-28 10:06:23 +0100 | [diff] [blame] | 874 | static const unsigned char sense_table[][4] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | /* BBD|ECC|ID|MAR */ |
Hannes Reinecke | 8ae7204 | 2015-03-27 16:46:32 +0100 | [diff] [blame] | 876 | {0xd1, ABORTED_COMMAND, 0x00, 0x00}, |
| 877 | // Device busy Aborted command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | /* BBD|ECC|ID */ |
Hannes Reinecke | 8ae7204 | 2015-03-27 16:46:32 +0100 | [diff] [blame] | 879 | {0xd0, ABORTED_COMMAND, 0x00, 0x00}, |
| 880 | // Device busy Aborted command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | /* ECC|MC|MARK */ |
Hannes Reinecke | 8ae7204 | 2015-03-27 16:46:32 +0100 | [diff] [blame] | 882 | {0x61, HARDWARE_ERROR, 0x00, 0x00}, |
| 883 | // Device fault Hardware error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */ |
Hannes Reinecke | 8ae7204 | 2015-03-27 16:46:32 +0100 | [diff] [blame] | 885 | {0x84, ABORTED_COMMAND, 0x47, 0x00}, |
| 886 | // Data CRC error SCSI parity error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | /* MC|ID|ABRT|TRK0|MARK */ |
Hannes Reinecke | 8ae7204 | 2015-03-27 16:46:32 +0100 | [diff] [blame] | 888 | {0x37, NOT_READY, 0x04, 0x00}, |
| 889 | // Unit offline Not ready |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | /* MCR|MARK */ |
Hannes Reinecke | 8ae7204 | 2015-03-27 16:46:32 +0100 | [diff] [blame] | 891 | {0x09, NOT_READY, 0x04, 0x00}, |
| 892 | // Unrecovered disk error Not ready |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | /* Bad address mark */ |
Hannes Reinecke | 8ae7204 | 2015-03-27 16:46:32 +0100 | [diff] [blame] | 894 | {0x01, MEDIUM_ERROR, 0x13, 0x00}, |
| 895 | // Address mark not found for data field |
| 896 | /* TRK0 - Track 0 not found */ |
| 897 | {0x02, HARDWARE_ERROR, 0x00, 0x00}, |
| 898 | // Hardware error |
Gwendal Grignou | 78062c5 | 2013-06-18 10:54:48 -0700 | [diff] [blame] | 899 | /* Abort: 0x04 is not translated here, see below */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | /* Media change request */ |
Hannes Reinecke | 8ae7204 | 2015-03-27 16:46:32 +0100 | [diff] [blame] | 901 | {0x08, NOT_READY, 0x04, 0x00}, |
| 902 | // FIXME: faking offline |
| 903 | /* SRV/IDNF - ID not found */ |
| 904 | {0x10, ILLEGAL_REQUEST, 0x21, 0x00}, |
| 905 | // Logical address out of range |
| 906 | /* MC - Media Changed */ |
| 907 | {0x20, UNIT_ATTENTION, 0x28, 0x00}, |
| 908 | // Not ready to ready change, medium may have changed |
| 909 | /* ECC - Uncorrectable ECC error */ |
| 910 | {0x40, MEDIUM_ERROR, 0x11, 0x04}, |
| 911 | // Unrecovered read error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /* BBD - block marked bad */ |
Hannes Reinecke | 8ae7204 | 2015-03-27 16:46:32 +0100 | [diff] [blame] | 913 | {0x80, MEDIUM_ERROR, 0x11, 0x04}, |
| 914 | // Block marked bad Medium error, unrecovered read error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark |
| 916 | }; |
Arjan van de Ven | 98ac62d | 2005-11-28 10:06:23 +0100 | [diff] [blame] | 917 | static const unsigned char stat_table[][4] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | /* Must be first because BUSY means no other bits valid */ |
Hannes Reinecke | 8ae7204 | 2015-03-27 16:46:32 +0100 | [diff] [blame] | 919 | {0x80, ABORTED_COMMAND, 0x47, 0x00}, |
| 920 | // Busy, fake parity for now |
| 921 | {0x40, ILLEGAL_REQUEST, 0x21, 0x04}, |
| 922 | // Device ready, unaligned write command |
| 923 | {0x20, HARDWARE_ERROR, 0x44, 0x00}, |
| 924 | // Device fault, internal target failure |
| 925 | {0x08, ABORTED_COMMAND, 0x47, 0x00}, |
| 926 | // Timed out in xfer, fake parity for now |
| 927 | {0x04, RECOVERED_ERROR, 0x11, 0x00}, |
| 928 | // Recovered ECC error Medium error, recovered |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark |
| 930 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | /* |
| 933 | * Is this an error we can process/parse |
| 934 | */ |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 935 | if (drv_stat & ATA_BUSY) { |
| 936 | drv_err = 0; /* Ignore the err bits, they're invalid */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 939 | if (drv_err) { |
| 940 | /* Look for drv_err */ |
| 941 | for (i = 0; sense_table[i][0] != 0xFF; i++) { |
| 942 | /* Look for best matches first */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 943 | if ((sense_table[i][0] & drv_err) == |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 944 | sense_table[i][0]) { |
| 945 | *sk = sense_table[i][1]; |
| 946 | *asc = sense_table[i][2]; |
| 947 | *ascq = sense_table[i][3]; |
| 948 | goto translate_done; |
| 949 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Gwendal Grignou | 78062c5 | 2013-06-18 10:54:48 -0700 | [diff] [blame] | 953 | /* |
| 954 | * Fall back to interpreting status bits. Note that if the drv_err |
| 955 | * has only the ABRT bit set, we decode drv_stat. ABRT by itself |
| 956 | * is not descriptive enough. |
| 957 | */ |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 958 | for (i = 0; stat_table[i][0] != 0xFF; i++) { |
| 959 | if (stat_table[i][0] & drv_stat) { |
| 960 | *sk = stat_table[i][1]; |
| 961 | *asc = stat_table[i][2]; |
| 962 | *ascq = stat_table[i][3]; |
| 963 | goto translate_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
Gwendal Grignou | 78062c5 | 2013-06-18 10:54:48 -0700 | [diff] [blame] | 967 | /* |
| 968 | * We need a sensible error return here, which is tricky, and one |
| 969 | * that won't cause people to do things like return a disk wrongly. |
| 970 | */ |
Alan Cox | 2d20202 | 2006-03-21 15:53:46 +0000 | [diff] [blame] | 971 | *sk = ABORTED_COMMAND; |
| 972 | *asc = 0x00; |
| 973 | *ascq = 0x00; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 974 | |
| 975 | translate_done: |
Tejun Heo | 246619d | 2006-05-15 20:58:16 +0900 | [diff] [blame] | 976 | if (verbose) |
| 977 | printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x " |
| 978 | "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", |
| 979 | id, drv_stat, drv_err, *sk, *asc, *ascq); |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 980 | return; |
| 981 | } |
| 982 | |
| 983 | /* |
Tejun Heo | 750426a | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 984 | * ata_gen_passthru_sense - Generate check condition sense block. |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 985 | * @qc: Command that completed. |
| 986 | * |
| 987 | * This function is specific to the ATA descriptor format sense |
| 988 | * block specified for the ATA pass through commands. Regardless |
| 989 | * of whether the command errored or not, return a sense |
| 990 | * block. Copy all controller registers into the sense |
Gwendal Grignou | 84a9a8c | 2013-01-18 10:56:43 -0800 | [diff] [blame] | 991 | * block. If there was no error, we get the request from an ATA |
| 992 | * passthrough command, so we use the following sense data: |
| 993 | * sk = RECOVERED ERROR |
| 994 | * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE |
| 995 | * |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 996 | * |
| 997 | * LOCKING: |
Tejun Heo | 750426a | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 998 | * None. |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 999 | */ |
Tejun Heo | 750426a | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 1000 | static void ata_gen_passthru_sense(struct ata_queued_cmd *qc) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1001 | { |
| 1002 | struct scsi_cmnd *cmd = qc->scsicmd; |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 1003 | struct ata_taskfile *tf = &qc->result_tf; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1004 | unsigned char *sb = cmd->sense_buffer; |
| 1005 | unsigned char *desc = sb + 8; |
Tejun Heo | 246619d | 2006-05-15 20:58:16 +0900 | [diff] [blame] | 1006 | int verbose = qc->ap->ops->error_handler == NULL; |
Hannes Reinecke | b525e77 | 2016-04-04 11:44:00 +0200 | [diff] [blame] | 1007 | u8 sense_key, asc, ascq; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1008 | |
| 1009 | memset(sb, 0, SCSI_SENSE_BUFFERSIZE); |
| 1010 | |
Jeff Garzik | 0e5dec4 | 2005-10-06 09:40:20 -0400 | [diff] [blame] | 1011 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1012 | |
| 1013 | /* |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1014 | * Use ata_to_sense_error() to map status register bits |
| 1015 | * onto sense key, asc & ascq. |
| 1016 | */ |
Tejun Heo | 058e55e | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1017 | if (qc->err_mask || |
| 1018 | tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) { |
Tejun Heo | 44877b4 | 2007-02-21 01:06:51 +0900 | [diff] [blame] | 1019 | ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature, |
Hannes Reinecke | b525e77 | 2016-04-04 11:44:00 +0200 | [diff] [blame] | 1020 | &sense_key, &asc, &ascq, verbose); |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 1021 | ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq); |
Gwendal Grignou | 84a9a8c | 2013-01-18 10:56:43 -0800 | [diff] [blame] | 1022 | } else { |
Hannes Reinecke | 11093cb | 2016-04-04 11:44:02 +0200 | [diff] [blame] | 1023 | /* |
| 1024 | * ATA PASS-THROUGH INFORMATION AVAILABLE |
| 1025 | * Always in descriptor format sense. |
| 1026 | */ |
| 1027 | scsi_build_sense_buffer(1, cmd->sense_buffer, |
| 1028 | RECOVERED_ERROR, 0, 0x1D); |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1029 | } |
| 1030 | |
Hannes Reinecke | 11093cb | 2016-04-04 11:44:02 +0200 | [diff] [blame] | 1031 | if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) { |
| 1032 | u8 len; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1033 | |
Hannes Reinecke | 11093cb | 2016-04-04 11:44:02 +0200 | [diff] [blame] | 1034 | /* descriptor format */ |
| 1035 | len = sb[7]; |
| 1036 | desc = (char *)scsi_sense_desc_find(sb, len + 8, 9); |
| 1037 | if (!desc) { |
| 1038 | if (SCSI_SENSE_BUFFERSIZE < len + 14) |
| 1039 | return; |
| 1040 | sb[7] = len + 14; |
| 1041 | desc = sb + 8 + len; |
| 1042 | } |
| 1043 | desc[0] = 9; |
| 1044 | desc[1] = 12; |
| 1045 | /* |
| 1046 | * Copy registers into sense buffer. |
| 1047 | */ |
| 1048 | desc[2] = 0x00; |
| 1049 | desc[3] = tf->feature; /* == error reg */ |
| 1050 | desc[5] = tf->nsect; |
| 1051 | desc[7] = tf->lbal; |
| 1052 | desc[9] = tf->lbam; |
| 1053 | desc[11] = tf->lbah; |
| 1054 | desc[12] = tf->device; |
| 1055 | desc[13] = tf->command; /* == status reg */ |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1056 | |
Hannes Reinecke | 11093cb | 2016-04-04 11:44:02 +0200 | [diff] [blame] | 1057 | /* |
| 1058 | * Fill in Extend bit, and the high order bytes |
| 1059 | * if applicable. |
| 1060 | */ |
| 1061 | if (tf->flags & ATA_TFLAG_LBA48) { |
| 1062 | desc[2] |= 0x01; |
| 1063 | desc[4] = tf->hob_nsect; |
| 1064 | desc[6] = tf->hob_lbal; |
| 1065 | desc[8] = tf->hob_lbam; |
| 1066 | desc[10] = tf->hob_lbah; |
| 1067 | } |
| 1068 | } else { |
| 1069 | /* Fixed sense format */ |
| 1070 | desc[0] = tf->feature; |
| 1071 | desc[1] = tf->command; /* status */ |
| 1072 | desc[2] = tf->device; |
| 1073 | desc[3] = tf->nsect; |
| 1074 | desc[0] = 0; |
| 1075 | if (tf->flags & ATA_TFLAG_LBA48) { |
| 1076 | desc[8] |= 0x80; |
| 1077 | if (tf->hob_nsect) |
| 1078 | desc[8] |= 0x40; |
| 1079 | if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah) |
| 1080 | desc[8] |= 0x20; |
| 1081 | } |
| 1082 | desc[9] = tf->lbal; |
| 1083 | desc[10] = tf->lbam; |
| 1084 | desc[11] = tf->lbah; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /** |
Tejun Heo | 750426a | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 1089 | * ata_gen_ata_sense - generate a SCSI fixed sense block |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1090 | * @qc: Command that we are erroring out |
| 1091 | * |
Tejun Heo | d25614b | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 1092 | * Generate sense block for a failed ATA command @qc. Descriptor |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1093 | * format is used to accommodate LBA48 block address. |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1094 | * |
| 1095 | * LOCKING: |
Tejun Heo | 750426a | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 1096 | * None. |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1097 | */ |
Tejun Heo | 750426a | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 1098 | static void ata_gen_ata_sense(struct ata_queued_cmd *qc) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1099 | { |
Tejun Heo | d25614b | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 1100 | struct ata_device *dev = qc->dev; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1101 | struct scsi_cmnd *cmd = qc->scsicmd; |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 1102 | struct ata_taskfile *tf = &qc->result_tf; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1103 | unsigned char *sb = cmd->sense_buffer; |
Tejun Heo | 246619d | 2006-05-15 20:58:16 +0900 | [diff] [blame] | 1104 | int verbose = qc->ap->ops->error_handler == NULL; |
Tejun Heo | d25614b | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 1105 | u64 block; |
Hannes Reinecke | b525e77 | 2016-04-04 11:44:00 +0200 | [diff] [blame] | 1106 | u8 sense_key, asc, ascq; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1107 | |
| 1108 | memset(sb, 0, SCSI_SENSE_BUFFERSIZE); |
| 1109 | |
Jeff Garzik | 0e5dec4 | 2005-10-06 09:40:20 -0400 | [diff] [blame] | 1110 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1111 | |
Tejun Heo | d25614b | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 1112 | /* Use ata_to_sense_error() to map status register bits |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1113 | * onto sense key, asc & ascq. |
| 1114 | */ |
Tejun Heo | 058e55e | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1115 | if (qc->err_mask || |
| 1116 | tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) { |
Tejun Heo | 44877b4 | 2007-02-21 01:06:51 +0900 | [diff] [blame] | 1117 | ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature, |
Hannes Reinecke | b525e77 | 2016-04-04 11:44:00 +0200 | [diff] [blame] | 1118 | &sense_key, &asc, &ascq, verbose); |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 1119 | ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq); |
Hannes Reinecke | 5e6acd1 | 2016-04-04 11:43:56 +0200 | [diff] [blame] | 1120 | } else { |
| 1121 | /* Could not decode error */ |
| 1122 | ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n", |
| 1123 | tf->command, qc->err_mask); |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 1124 | ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0); |
Hannes Reinecke | 5e6acd1 | 2016-04-04 11:43:56 +0200 | [diff] [blame] | 1125 | return; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
Tejun Heo | d25614b | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 1128 | block = ata_tf_read_block(&qc->result_tf, dev); |
Hannes Reinecke | cffd1ee | 2016-04-04 11:43:57 +0200 | [diff] [blame] | 1129 | if (block == U64_MAX) |
| 1130 | return; |
Jeff Garzik | 0274aa2 | 2005-06-22 13:50:56 -0400 | [diff] [blame] | 1131 | |
Hannes Reinecke | cf8b49b | 2016-04-04 11:43:58 +0200 | [diff] [blame] | 1132 | scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Brian King | a6cce2a | 2006-03-17 17:04:15 -0600 | [diff] [blame] | 1135 | static void ata_scsi_sdev_config(struct scsi_device *sdev) |
| 1136 | { |
| 1137 | sdev->use_10_for_rw = 1; |
| 1138 | sdev->use_10_for_ms = 1; |
Martin K. Petersen | 3c6bdae | 2012-09-18 12:19:30 -0400 | [diff] [blame] | 1139 | sdev->no_report_opcodes = 1; |
Martin K. Petersen | 5db4486 | 2012-09-18 12:19:32 -0400 | [diff] [blame] | 1140 | sdev->no_write_same = 1; |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1141 | |
| 1142 | /* Schedule policy is determined by ->qc_defer() callback and |
| 1143 | * it needs to see every deferred qc. Set dev_blocked to 1 to |
| 1144 | * prevent SCSI midlayer from automatically deferring |
| 1145 | * requests. |
| 1146 | */ |
| 1147 | sdev->max_device_blocked = 1; |
Brian King | a6cce2a | 2006-03-17 17:04:15 -0600 | [diff] [blame] | 1148 | } |
| 1149 | |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1150 | /** |
| 1151 | * atapi_drain_needed - Check whether data transfer may overflow |
Randy Dunlap | 73fd8b6 | 2008-02-19 13:43:21 -0800 | [diff] [blame] | 1152 | * @rq: request to be checked |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1153 | * |
| 1154 | * ATAPI commands which transfer variable length data to host |
| 1155 | * might overflow due to application error or hardare bug. This |
| 1156 | * function checks whether overflow should be drained and ignored |
| 1157 | * for @request. |
| 1158 | * |
| 1159 | * LOCKING: |
| 1160 | * None. |
| 1161 | * |
| 1162 | * RETURNS: |
| 1163 | * 1 if ; otherwise, 0. |
| 1164 | */ |
| 1165 | static int atapi_drain_needed(struct request *rq) |
| 1166 | { |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 1167 | if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC)) |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1168 | return 0; |
| 1169 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1170 | if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE)) |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1171 | return 0; |
| 1172 | |
| 1173 | return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC; |
| 1174 | } |
| 1175 | |
| 1176 | static int ata_scsi_dev_config(struct scsi_device *sdev, |
| 1177 | struct ata_device *dev) |
Brian King | a6cce2a | 2006-03-17 17:04:15 -0600 | [diff] [blame] | 1178 | { |
shaohua.li@intel.com | 900e599 | 2011-05-06 11:35:31 -0600 | [diff] [blame] | 1179 | struct request_queue *q = sdev->request_queue; |
| 1180 | |
Elias Oltmanns | 45fabbb | 2008-09-21 11:54:08 +0200 | [diff] [blame] | 1181 | if (!ata_id_has_unload(dev->id)) |
| 1182 | dev->flags |= ATA_DFLAG_NO_UNLOAD; |
| 1183 | |
Tejun Heo | 914ed35 | 2006-11-01 18:39:55 +0900 | [diff] [blame] | 1184 | /* configure max sectors */ |
shaohua.li@intel.com | 900e599 | 2011-05-06 11:35:31 -0600 | [diff] [blame] | 1185 | blk_queue_max_hw_sectors(q, dev->max_sectors); |
Brian King | a6cce2a | 2006-03-17 17:04:15 -0600 | [diff] [blame] | 1186 | |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1187 | if (dev->class == ATA_DEV_ATAPI) { |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1188 | void *buf; |
| 1189 | |
Tejun Heo | 729a6a3 | 2011-01-20 13:59:06 +0100 | [diff] [blame] | 1190 | sdev->sector_size = ATA_SECT_SIZE; |
| 1191 | |
| 1192 | /* set DMA padding */ |
shaohua.li@intel.com | 900e599 | 2011-05-06 11:35:31 -0600 | [diff] [blame] | 1193 | blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1); |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1194 | |
| 1195 | /* configure draining */ |
| 1196 | buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL); |
| 1197 | if (!buf) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1198 | ata_dev_err(dev, "drain buffer allocation failed\n"); |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1199 | return -ENOMEM; |
| 1200 | } |
| 1201 | |
| 1202 | blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN); |
| 1203 | } else { |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 1204 | sdev->sector_size = ata_id_logical_sector_size(dev->id); |
Tejun Heo | d8cf538 | 2008-01-15 08:46:59 +0900 | [diff] [blame] | 1205 | sdev->manage_start_stop = 1; |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 1206 | } |
Tejun Heo | d8cf538 | 2008-01-15 08:46:59 +0900 | [diff] [blame] | 1207 | |
Tejun Heo | 729a6a3 | 2011-01-20 13:59:06 +0100 | [diff] [blame] | 1208 | /* |
| 1209 | * ata_pio_sectors() expects buffer for each sector to not cross |
| 1210 | * page boundary. Enforce it by requiring buffers to be sector |
| 1211 | * aligned, which works iff sector_size is not larger than |
| 1212 | * PAGE_SIZE. ATAPI devices also need the alignment as |
| 1213 | * IDENTIFY_PACKET is executed as ATA_PROT_PIO. |
| 1214 | */ |
| 1215 | if (sdev->sector_size > PAGE_SIZE) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1216 | ata_dev_warn(dev, |
Tejun Heo | 729a6a3 | 2011-01-20 13:59:06 +0100 | [diff] [blame] | 1217 | "sector_size=%u > PAGE_SIZE, PIO may malfunction\n", |
| 1218 | sdev->sector_size); |
| 1219 | |
shaohua.li@intel.com | 900e599 | 2011-05-06 11:35:31 -0600 | [diff] [blame] | 1220 | blk_queue_update_dma_alignment(q, sdev->sector_size - 1); |
Tejun Heo | 729a6a3 | 2011-01-20 13:59:06 +0100 | [diff] [blame] | 1221 | |
Jeff Garzik | f26792d | 2007-10-29 17:18:39 -0400 | [diff] [blame] | 1222 | if (dev->flags & ATA_DFLAG_AN) |
| 1223 | set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events); |
| 1224 | |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 1225 | if (dev->flags & ATA_DFLAG_NCQ) { |
| 1226 | int depth; |
| 1227 | |
| 1228 | depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id)); |
| 1229 | depth = min(ATA_MAX_QUEUE - 1, depth); |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 1230 | scsi_change_queue_depth(sdev, depth); |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 1231 | } |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1232 | |
shaohua.li@intel.com | 900e599 | 2011-05-06 11:35:31 -0600 | [diff] [blame] | 1233 | blk_queue_flush_queueable(q, false); |
| 1234 | |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 1235 | dev->sdev = sdev; |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1236 | return 0; |
Brian King | a6cce2a | 2006-03-17 17:04:15 -0600 | [diff] [blame] | 1237 | } |
| 1238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | /** |
| 1240 | * ata_scsi_slave_config - Set SCSI device attributes |
| 1241 | * @sdev: SCSI device to examine |
| 1242 | * |
| 1243 | * This is called before we actually start reading |
| 1244 | * and writing to the device, to configure certain |
| 1245 | * SCSI mid-layer behaviors. |
| 1246 | * |
| 1247 | * LOCKING: |
| 1248 | * Defined by SCSI layer. We don't really care. |
| 1249 | */ |
| 1250 | |
| 1251 | int ata_scsi_slave_config(struct scsi_device *sdev) |
| 1252 | { |
Tejun Heo | 3153436 | 2006-05-31 18:27:36 +0900 | [diff] [blame] | 1253 | struct ata_port *ap = ata_shost_to_port(sdev->host); |
| 1254 | struct ata_device *dev = __ata_scsi_find_dev(ap, sdev); |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1255 | int rc = 0; |
Tejun Heo | 3153436 | 2006-05-31 18:27:36 +0900 | [diff] [blame] | 1256 | |
Brian King | a6cce2a | 2006-03-17 17:04:15 -0600 | [diff] [blame] | 1257 | ata_scsi_sdev_config(sdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
Tejun Heo | 3153436 | 2006-05-31 18:27:36 +0900 | [diff] [blame] | 1259 | if (dev) |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1260 | rc = ata_scsi_dev_config(sdev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1262 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /** |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 1266 | * ata_scsi_slave_destroy - SCSI device is about to be destroyed |
| 1267 | * @sdev: SCSI device to be destroyed |
| 1268 | * |
| 1269 | * @sdev is about to be destroyed for hot/warm unplugging. If |
| 1270 | * this unplugging was initiated by libata as indicated by NULL |
| 1271 | * dev->sdev, this function doesn't have to do anything. |
| 1272 | * Otherwise, SCSI layer initiated warm-unplug is in progress. |
| 1273 | * Clear dev->sdev, schedule the device for ATA detach and invoke |
| 1274 | * EH. |
| 1275 | * |
| 1276 | * LOCKING: |
| 1277 | * Defined by SCSI layer. We don't really care. |
| 1278 | */ |
| 1279 | void ata_scsi_slave_destroy(struct scsi_device *sdev) |
| 1280 | { |
| 1281 | struct ata_port *ap = ata_shost_to_port(sdev->host); |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1282 | struct request_queue *q = sdev->request_queue; |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 1283 | unsigned long flags; |
| 1284 | struct ata_device *dev; |
| 1285 | |
| 1286 | if (!ap->ops->error_handler) |
| 1287 | return; |
| 1288 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1289 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 1290 | dev = __ata_scsi_find_dev(ap, sdev); |
| 1291 | if (dev && dev->sdev) { |
| 1292 | /* SCSI device already in CANCEL state, no need to offline it */ |
| 1293 | dev->sdev = NULL; |
| 1294 | dev->flags |= ATA_DFLAG_DETACH; |
| 1295 | ata_port_schedule_eh(ap); |
| 1296 | } |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1297 | spin_unlock_irqrestore(ap->lock, flags); |
James Bottomley | fa2fc7f | 2008-02-19 11:36:57 +0100 | [diff] [blame] | 1298 | |
| 1299 | kfree(q->dma_drain_buffer); |
| 1300 | q->dma_drain_buffer = NULL; |
| 1301 | q->dma_drain_size = 0; |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /** |
Dan Williams | f6e6703 | 2011-09-20 15:10:33 -0700 | [diff] [blame] | 1305 | * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth |
Marcos Paulo de Souza | 6d2dd05 | 2011-10-31 23:50:16 -0200 | [diff] [blame] | 1306 | * @ap: ATA port to which the device change the queue depth |
| 1307 | * @sdev: SCSI device to configure queue depth for |
| 1308 | * @queue_depth: new queue depth |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 1309 | * |
Dan Williams | f6e6703 | 2011-09-20 15:10:33 -0700 | [diff] [blame] | 1310 | * libsas and libata have different approaches for associating a sdev to |
| 1311 | * its ata_port. |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 1312 | * |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 1313 | */ |
Dan Williams | f6e6703 | 2011-09-20 15:10:33 -0700 | [diff] [blame] | 1314 | int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev, |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 1315 | int queue_depth) |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 1316 | { |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 1317 | struct ata_device *dev; |
Tejun Heo | 360f654 | 2006-09-30 19:45:00 +0900 | [diff] [blame] | 1318 | unsigned long flags; |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 1319 | |
Tejun Heo | c3c70c4 | 2007-02-20 23:31:22 +0900 | [diff] [blame] | 1320 | if (queue_depth < 1 || queue_depth == sdev->queue_depth) |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 1321 | return sdev->queue_depth; |
| 1322 | |
| 1323 | dev = ata_scsi_find_dev(ap, sdev); |
| 1324 | if (!dev || !ata_dev_enabled(dev)) |
| 1325 | return sdev->queue_depth; |
| 1326 | |
Tejun Heo | c3c70c4 | 2007-02-20 23:31:22 +0900 | [diff] [blame] | 1327 | /* NCQ enabled? */ |
Tejun Heo | 360f654 | 2006-09-30 19:45:00 +0900 | [diff] [blame] | 1328 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | c3c70c4 | 2007-02-20 23:31:22 +0900 | [diff] [blame] | 1329 | dev->flags &= ~ATA_DFLAG_NCQ_OFF; |
| 1330 | if (queue_depth == 1 || !ata_ncq_enabled(dev)) { |
Tejun Heo | 360f654 | 2006-09-30 19:45:00 +0900 | [diff] [blame] | 1331 | dev->flags |= ATA_DFLAG_NCQ_OFF; |
Tejun Heo | c3c70c4 | 2007-02-20 23:31:22 +0900 | [diff] [blame] | 1332 | queue_depth = 1; |
| 1333 | } |
Tejun Heo | 360f654 | 2006-09-30 19:45:00 +0900 | [diff] [blame] | 1334 | spin_unlock_irqrestore(ap->lock, flags); |
| 1335 | |
Tejun Heo | c3c70c4 | 2007-02-20 23:31:22 +0900 | [diff] [blame] | 1336 | /* limit and apply queue depth */ |
| 1337 | queue_depth = min(queue_depth, sdev->host->can_queue); |
| 1338 | queue_depth = min(queue_depth, ata_id_queue_depth(dev->id)); |
| 1339 | queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1); |
| 1340 | |
| 1341 | if (sdev->queue_depth == queue_depth) |
| 1342 | return -EINVAL; |
| 1343 | |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 1344 | return scsi_change_queue_depth(sdev, queue_depth); |
Tejun Heo | a6e6ce8 | 2006-05-15 21:03:48 +0900 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | /** |
Dan Williams | f6e6703 | 2011-09-20 15:10:33 -0700 | [diff] [blame] | 1348 | * ata_scsi_change_queue_depth - SCSI callback for queue depth config |
| 1349 | * @sdev: SCSI device to configure queue depth for |
| 1350 | * @queue_depth: new queue depth |
Dan Williams | f6e6703 | 2011-09-20 15:10:33 -0700 | [diff] [blame] | 1351 | * |
| 1352 | * This is libata standard hostt->change_queue_depth callback. |
| 1353 | * SCSI will call into this callback when user tries to set queue |
| 1354 | * depth via sysfs. |
| 1355 | * |
| 1356 | * LOCKING: |
| 1357 | * SCSI layer (we don't care) |
| 1358 | * |
| 1359 | * RETURNS: |
| 1360 | * Newly configured queue depth. |
| 1361 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 1362 | int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth) |
Dan Williams | f6e6703 | 2011-09-20 15:10:33 -0700 | [diff] [blame] | 1363 | { |
| 1364 | struct ata_port *ap = ata_shost_to_port(sdev->host); |
| 1365 | |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 1366 | return __ata_change_queue_depth(ap, sdev, queue_depth); |
Dan Williams | f6e6703 | 2011-09-20 15:10:33 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | /** |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1370 | * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command |
| 1371 | * @qc: Storage for translated ATA taskfile |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1372 | * |
| 1373 | * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY |
| 1374 | * (to start). Perhaps these commands should be preceded by |
| 1375 | * CHECK POWER MODE to see what power mode the device is already in. |
| 1376 | * [See SAT revision 5 at www.t10.org] |
| 1377 | * |
| 1378 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 1379 | * spin_lock_irqsave(host lock) |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1380 | * |
| 1381 | * RETURNS: |
| 1382 | * Zero on success, non-zero on error. |
| 1383 | */ |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 1384 | static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc) |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1385 | { |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1386 | struct scsi_cmnd *scmd = qc->scsicmd; |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1387 | struct ata_taskfile *tf = &qc->tf; |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 1388 | const u8 *cdb = scmd->cmnd; |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1389 | |
Tejun Heo | 2e5704f | 2006-12-17 10:46:33 +0900 | [diff] [blame] | 1390 | if (scmd->cmd_len < 5) |
| 1391 | goto invalid_fld; |
| 1392 | |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1393 | tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
| 1394 | tf->protocol = ATA_PROT_NODATA; |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1395 | if (cdb[1] & 0x1) { |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1396 | ; /* ignore IMMED bit, violates sat-r05 */ |
| 1397 | } |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1398 | if (cdb[4] & 0x2) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1399 | goto invalid_fld; /* LOEJ bit set not supported */ |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1400 | if (((cdb[4] >> 4) & 0xf) != 0) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1401 | goto invalid_fld; /* power conditions not supported */ |
Tejun Heo | e31e8531 | 2007-09-23 13:14:13 +0900 | [diff] [blame] | 1402 | |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1403 | if (cdb[4] & 0x1) { |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1404 | tf->nsect = 1; /* 1 sector, lba=0 */ |
Albert Lee | 9d5b130 | 2005-10-04 08:48:17 -0400 | [diff] [blame] | 1405 | |
| 1406 | if (qc->dev->flags & ATA_DFLAG_LBA) { |
Tejun Heo | c44078c | 2006-05-15 20:57:21 +0900 | [diff] [blame] | 1407 | tf->flags |= ATA_TFLAG_LBA; |
Albert Lee | 9d5b130 | 2005-10-04 08:48:17 -0400 | [diff] [blame] | 1408 | |
| 1409 | tf->lbah = 0x0; |
| 1410 | tf->lbam = 0x0; |
| 1411 | tf->lbal = 0x0; |
| 1412 | tf->device |= ATA_LBA; |
| 1413 | } else { |
| 1414 | /* CHS */ |
| 1415 | tf->lbal = 0x1; /* sect */ |
| 1416 | tf->lbam = 0x0; /* cyl low */ |
| 1417 | tf->lbah = 0x0; /* cyl high */ |
| 1418 | } |
| 1419 | |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1420 | tf->command = ATA_CMD_VERIFY; /* READ VERIFY */ |
Tejun Heo | 920a4b1 | 2007-05-04 21:28:48 +0200 | [diff] [blame] | 1421 | } else { |
Rafael J. Wysocki | 2a6e58d | 2009-01-19 20:56:43 +0100 | [diff] [blame] | 1422 | /* Some odd clown BIOSen issue spindown on power off (ACPI S4 |
| 1423 | * or S5) causing some drives to spin up and down again. |
| 1424 | */ |
| 1425 | if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) && |
| 1426 | system_state == SYSTEM_POWER_OFF) |
| 1427 | goto skip; |
| 1428 | |
| 1429 | if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) && |
| 1430 | system_entering_hibernation()) |
| 1431 | goto skip; |
| 1432 | |
Robert Hancock | 78981a7 | 2007-01-30 00:59:18 -0800 | [diff] [blame] | 1433 | /* Issue ATA STANDBY IMMEDIATE command */ |
| 1434 | tf->command = ATA_CMD_STANDBYNOW1; |
Tejun Heo | 920a4b1 | 2007-05-04 21:28:48 +0200 | [diff] [blame] | 1435 | } |
Robert Hancock | 78981a7 | 2007-01-30 00:59:18 -0800 | [diff] [blame] | 1436 | |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1437 | /* |
| 1438 | * Standby and Idle condition timers could be implemented but that |
| 1439 | * would require libata to implement the Power condition mode page |
| 1440 | * and allow the user to change it. Changing mode pages requires |
| 1441 | * MODE SELECT to be implemented. |
| 1442 | */ |
| 1443 | |
| 1444 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1445 | |
Rafael J. Wysocki | 2a6e58d | 2009-01-19 20:56:43 +0100 | [diff] [blame] | 1446 | invalid_fld: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 1447 | ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x24, 0x0); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1448 | /* "Invalid field in cbd" */ |
| 1449 | return 1; |
Rafael J. Wysocki | 2a6e58d | 2009-01-19 20:56:43 +0100 | [diff] [blame] | 1450 | skip: |
| 1451 | scmd->result = SAM_STAT_GOOD; |
| 1452 | return 1; |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | |
| 1456 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command |
| 1458 | * @qc: Storage for translated ATA taskfile |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | * |
| 1460 | * Sets up an ATA taskfile to issue FLUSH CACHE or |
| 1461 | * FLUSH CACHE EXT. |
| 1462 | * |
| 1463 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 1464 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | * |
| 1466 | * RETURNS: |
| 1467 | * Zero on success, non-zero on error. |
| 1468 | */ |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 1469 | static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | { |
| 1471 | struct ata_taskfile *tf = &qc->tf; |
| 1472 | |
| 1473 | tf->flags |= ATA_TFLAG_DEVICE; |
| 1474 | tf->protocol = ATA_PROT_NODATA; |
| 1475 | |
Tejun Heo | 6fc49ad | 2006-11-11 20:10:45 +0900 | [diff] [blame] | 1476 | if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | tf->command = ATA_CMD_FLUSH_EXT; |
| 1478 | else |
| 1479 | tf->command = ATA_CMD_FLUSH; |
| 1480 | |
Tejun Heo | b666da3 | 2007-10-26 15:53:59 +0900 | [diff] [blame] | 1481 | /* flush is critical for IO integrity, consider it an IO command */ |
| 1482 | qc->flags |= ATA_QCFLAG_IO; |
| 1483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | /** |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1488 | * scsi_6_lba_len - Get LBA and transfer length |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1489 | * @cdb: SCSI command to translate |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1490 | * |
| 1491 | * Calculate LBA and transfer length for 6-byte commands. |
| 1492 | * |
| 1493 | * RETURNS: |
| 1494 | * @plba: the LBA |
| 1495 | * @plen: the transfer length |
| 1496 | */ |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1497 | static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen) |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1498 | { |
| 1499 | u64 lba = 0; |
Jeff Garzik | 6c7b7d2 | 2007-05-25 04:39:39 -0400 | [diff] [blame] | 1500 | u32 len; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1501 | |
| 1502 | VPRINTK("six-byte command\n"); |
| 1503 | |
Jeff Garzik | 6c7b7d2 | 2007-05-25 04:39:39 -0400 | [diff] [blame] | 1504 | lba |= ((u64)(cdb[1] & 0x1f)) << 16; |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1505 | lba |= ((u64)cdb[2]) << 8; |
| 1506 | lba |= ((u64)cdb[3]); |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1507 | |
Jeff Garzik | 6c7b7d2 | 2007-05-25 04:39:39 -0400 | [diff] [blame] | 1508 | len = cdb[4]; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1509 | |
| 1510 | *plba = lba; |
| 1511 | *plen = len; |
| 1512 | } |
| 1513 | |
| 1514 | /** |
| 1515 | * scsi_10_lba_len - Get LBA and transfer length |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1516 | * @cdb: SCSI command to translate |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1517 | * |
| 1518 | * Calculate LBA and transfer length for 10-byte commands. |
| 1519 | * |
| 1520 | * RETURNS: |
| 1521 | * @plba: the LBA |
| 1522 | * @plen: the transfer length |
| 1523 | */ |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1524 | static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen) |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1525 | { |
| 1526 | u64 lba = 0; |
| 1527 | u32 len = 0; |
| 1528 | |
| 1529 | VPRINTK("ten-byte command\n"); |
| 1530 | |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1531 | lba |= ((u64)cdb[2]) << 24; |
| 1532 | lba |= ((u64)cdb[3]) << 16; |
| 1533 | lba |= ((u64)cdb[4]) << 8; |
| 1534 | lba |= ((u64)cdb[5]); |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1535 | |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1536 | len |= ((u32)cdb[7]) << 8; |
| 1537 | len |= ((u32)cdb[8]); |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1538 | |
| 1539 | *plba = lba; |
| 1540 | *plen = len; |
| 1541 | } |
| 1542 | |
| 1543 | /** |
| 1544 | * scsi_16_lba_len - Get LBA and transfer length |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1545 | * @cdb: SCSI command to translate |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1546 | * |
| 1547 | * Calculate LBA and transfer length for 16-byte commands. |
| 1548 | * |
| 1549 | * RETURNS: |
| 1550 | * @plba: the LBA |
| 1551 | * @plen: the transfer length |
| 1552 | */ |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1553 | static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen) |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1554 | { |
| 1555 | u64 lba = 0; |
| 1556 | u32 len = 0; |
| 1557 | |
| 1558 | VPRINTK("sixteen-byte command\n"); |
| 1559 | |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1560 | lba |= ((u64)cdb[2]) << 56; |
| 1561 | lba |= ((u64)cdb[3]) << 48; |
| 1562 | lba |= ((u64)cdb[4]) << 40; |
| 1563 | lba |= ((u64)cdb[5]) << 32; |
| 1564 | lba |= ((u64)cdb[6]) << 24; |
| 1565 | lba |= ((u64)cdb[7]) << 16; |
| 1566 | lba |= ((u64)cdb[8]) << 8; |
| 1567 | lba |= ((u64)cdb[9]); |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1568 | |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1569 | len |= ((u32)cdb[10]) << 24; |
| 1570 | len |= ((u32)cdb[11]) << 16; |
| 1571 | len |= ((u32)cdb[12]) << 8; |
| 1572 | len |= ((u32)cdb[13]); |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1573 | |
| 1574 | *plba = lba; |
| 1575 | *plen = len; |
| 1576 | } |
| 1577 | |
| 1578 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one |
| 1580 | * @qc: Storage for translated ATA taskfile |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | * |
| 1582 | * Converts SCSI VERIFY command to an ATA READ VERIFY command. |
| 1583 | * |
| 1584 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 1585 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | * |
| 1587 | * RETURNS: |
| 1588 | * Zero on success, non-zero on error. |
| 1589 | */ |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 1590 | static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | { |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1592 | struct scsi_cmnd *scmd = qc->scsicmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | struct ata_taskfile *tf = &qc->tf; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1594 | struct ata_device *dev = qc->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | u64 dev_sectors = qc->dev->n_sectors; |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 1596 | const u8 *cdb = scmd->cmnd; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1597 | u64 block; |
| 1598 | u32 n_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | |
| 1600 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 1601 | tf->protocol = ATA_PROT_NODATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
Tejun Heo | 2e5704f | 2006-12-17 10:46:33 +0900 | [diff] [blame] | 1603 | if (cdb[0] == VERIFY) { |
| 1604 | if (scmd->cmd_len < 10) |
| 1605 | goto invalid_fld; |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1606 | scsi_10_lba_len(cdb, &block, &n_block); |
Tejun Heo | 2e5704f | 2006-12-17 10:46:33 +0900 | [diff] [blame] | 1607 | } else if (cdb[0] == VERIFY_16) { |
| 1608 | if (scmd->cmd_len < 16) |
| 1609 | goto invalid_fld; |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1610 | scsi_16_lba_len(cdb, &block, &n_block); |
Tejun Heo | 2e5704f | 2006-12-17 10:46:33 +0900 | [diff] [blame] | 1611 | } else |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1612 | goto invalid_fld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1614 | if (!n_block) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1615 | goto nothing_to_do; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1616 | if (block >= dev_sectors) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1617 | goto out_of_range; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1618 | if ((block + n_block) > dev_sectors) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1619 | goto out_of_range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame] | 1621 | if (dev->flags & ATA_DFLAG_LBA) { |
| 1622 | tf->flags |= ATA_TFLAG_LBA; |
| 1623 | |
Albert Lee | c6a33e2 | 2005-10-12 15:12:26 +0800 | [diff] [blame] | 1624 | if (lba_28_ok(block, n_block)) { |
| 1625 | /* use LBA28 */ |
| 1626 | tf->command = ATA_CMD_VERIFY; |
| 1627 | tf->device |= (block >> 24) & 0xf; |
| 1628 | } else if (lba_48_ok(block, n_block)) { |
| 1629 | if (!(dev->flags & ATA_DFLAG_LBA48)) |
| 1630 | goto out_of_range; |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame] | 1631 | |
| 1632 | /* use LBA48 */ |
| 1633 | tf->flags |= ATA_TFLAG_LBA48; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1634 | tf->command = ATA_CMD_VERIFY_EXT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1636 | tf->hob_nsect = (n_block >> 8) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1638 | tf->hob_lbah = (block >> 40) & 0xff; |
| 1639 | tf->hob_lbam = (block >> 32) & 0xff; |
| 1640 | tf->hob_lbal = (block >> 24) & 0xff; |
Albert Lee | c6a33e2 | 2005-10-12 15:12:26 +0800 | [diff] [blame] | 1641 | } else |
| 1642 | /* request too large even for LBA48 */ |
| 1643 | goto out_of_range; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1644 | |
| 1645 | tf->nsect = n_block & 0xff; |
| 1646 | |
| 1647 | tf->lbah = (block >> 16) & 0xff; |
| 1648 | tf->lbam = (block >> 8) & 0xff; |
| 1649 | tf->lbal = block & 0xff; |
| 1650 | |
| 1651 | tf->device |= ATA_LBA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | } else { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1653 | /* CHS */ |
| 1654 | u32 sect, head, cyl, track; |
| 1655 | |
Albert Lee | c6a33e2 | 2005-10-12 15:12:26 +0800 | [diff] [blame] | 1656 | if (!lba_28_ok(block, n_block)) |
| 1657 | goto out_of_range; |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame] | 1658 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1659 | /* Convert LBA to CHS */ |
| 1660 | track = (u32)block / dev->sectors; |
| 1661 | cyl = track / dev->heads; |
| 1662 | head = track % dev->heads; |
| 1663 | sect = (u32)block % dev->sectors + 1; |
| 1664 | |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1665 | DPRINTK("block %u track %u cyl %u head %u sect %u\n", |
| 1666 | (u32)block, track, cyl, head, sect); |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1667 | |
| 1668 | /* Check whether the converted CHS can fit. |
| 1669 | Cylinder: 0-65535 |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1670 | Head: 0-15 |
| 1671 | Sector: 1-255*/ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1672 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1673 | goto out_of_range; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | tf->command = ATA_CMD_VERIFY; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1676 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ |
| 1677 | tf->lbal = sect; |
| 1678 | tf->lbam = cyl; |
| 1679 | tf->lbah = cyl >> 8; |
| 1680 | tf->device |= head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1684 | |
| 1685 | invalid_fld: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 1686 | ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x24, 0x0); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1687 | /* "Invalid field in cbd" */ |
| 1688 | return 1; |
| 1689 | |
| 1690 | out_of_range: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 1691 | ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1692 | /* "Logical Block Address out of range" */ |
| 1693 | return 1; |
| 1694 | |
| 1695 | nothing_to_do: |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1696 | scmd->result = SAM_STAT_GOOD; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1697 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /** |
| 1701 | * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one |
| 1702 | * @qc: Storage for translated ATA taskfile |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | * |
| 1704 | * Converts any of six SCSI read/write commands into the |
| 1705 | * ATA counterpart, including starting sector (LBA), |
| 1706 | * sector count, and taking into account the device's LBA48 |
| 1707 | * support. |
| 1708 | * |
| 1709 | * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and |
| 1710 | * %WRITE_16 are currently supported. |
| 1711 | * |
| 1712 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 1713 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | * |
| 1715 | * RETURNS: |
| 1716 | * Zero on success, non-zero on error. |
| 1717 | */ |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 1718 | static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | { |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1720 | struct scsi_cmnd *scmd = qc->scsicmd; |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 1721 | const u8 *cdb = scmd->cmnd; |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 1722 | unsigned int tf_flags = 0; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1723 | u64 block; |
| 1724 | u32 n_block; |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 1725 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1727 | if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16) |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 1728 | tf_flags |= ATA_TFLAG_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1730 | /* Calculate the SCSI LBA, transfer length and FUA. */ |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1731 | switch (cdb[0]) { |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1732 | case READ_10: |
| 1733 | case WRITE_10: |
Tejun Heo | 2e5704f | 2006-12-17 10:46:33 +0900 | [diff] [blame] | 1734 | if (unlikely(scmd->cmd_len < 10)) |
| 1735 | goto invalid_fld; |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1736 | scsi_10_lba_len(cdb, &block, &n_block); |
Jeff Garzik | 3e451a4 | 2012-08-17 14:04:50 -0400 | [diff] [blame] | 1737 | if (cdb[1] & (1 << 3)) |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 1738 | tf_flags |= ATA_TFLAG_FUA; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1739 | break; |
| 1740 | case READ_6: |
| 1741 | case WRITE_6: |
Tejun Heo | 2e5704f | 2006-12-17 10:46:33 +0900 | [diff] [blame] | 1742 | if (unlikely(scmd->cmd_len < 6)) |
| 1743 | goto invalid_fld; |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1744 | scsi_6_lba_len(cdb, &block, &n_block); |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1745 | |
| 1746 | /* for 6-byte r/w commands, transfer length 0 |
| 1747 | * means 256 blocks of data, not 0 block. |
| 1748 | */ |
Jeff Garzik | 76b2bf9 | 2005-08-29 19:24:43 -0400 | [diff] [blame] | 1749 | if (!n_block) |
| 1750 | n_block = 256; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1751 | break; |
| 1752 | case READ_16: |
| 1753 | case WRITE_16: |
Tejun Heo | 2e5704f | 2006-12-17 10:46:33 +0900 | [diff] [blame] | 1754 | if (unlikely(scmd->cmd_len < 16)) |
| 1755 | goto invalid_fld; |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1756 | scsi_16_lba_len(cdb, &block, &n_block); |
Jeff Garzik | 3e451a4 | 2012-08-17 14:04:50 -0400 | [diff] [blame] | 1757 | if (cdb[1] & (1 << 3)) |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 1758 | tf_flags |= ATA_TFLAG_FUA; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1759 | break; |
| 1760 | default: |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1761 | DPRINTK("no-byte command\n"); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1762 | goto invalid_fld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1765 | /* Check and compose ATA command */ |
| 1766 | if (!n_block) |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1767 | /* For 10-byte and 16-byte SCSI R/W commands, transfer |
| 1768 | * length 0 means transfer 0 block of data. |
| 1769 | * However, for ATA R/W commands, sector count 0 means |
| 1770 | * 256 or 65536 sectors, not 0 sectors as in SCSI. |
Alan Cox | f51750d | 2005-11-07 17:06:33 +0000 | [diff] [blame] | 1771 | * |
| 1772 | * WARNING: one or two older ATA drives treat 0 as 0... |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1773 | */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1774 | goto nothing_to_do; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1775 | |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 1776 | qc->flags |= ATA_QCFLAG_IO; |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 1777 | qc->nbytes = n_block * scmd->device->sector_size; |
Tejun Heo | 3dc1d88 | 2006-05-15 21:03:45 +0900 | [diff] [blame] | 1778 | |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 1779 | rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags, |
| 1780 | qc->tag); |
| 1781 | if (likely(rc == 0)) |
| 1782 | return 0; |
Tejun Heo | 3dc1d88 | 2006-05-15 21:03:45 +0900 | [diff] [blame] | 1783 | |
Tejun Heo | bd056d7 | 2006-11-14 22:47:10 +0900 | [diff] [blame] | 1784 | if (rc == -ERANGE) |
| 1785 | goto out_of_range; |
| 1786 | /* treat all other errors as -EINVAL, fall through */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1787 | invalid_fld: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 1788 | ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x24, 0x0); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1789 | /* "Invalid field in cbd" */ |
| 1790 | return 1; |
| 1791 | |
| 1792 | out_of_range: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 1793 | ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1794 | /* "Logical Block Address out of range" */ |
| 1795 | return 1; |
| 1796 | |
| 1797 | nothing_to_do: |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 1798 | scmd->result = SAM_STAT_GOOD; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1799 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | } |
| 1801 | |
Christoph Hellwig | 2aa8f5d | 2015-10-08 10:25:41 +0200 | [diff] [blame] | 1802 | static void ata_qc_done(struct ata_queued_cmd *qc) |
| 1803 | { |
| 1804 | struct scsi_cmnd *cmd = qc->scsicmd; |
| 1805 | void (*done)(struct scsi_cmnd *) = qc->scsidone; |
| 1806 | |
| 1807 | ata_qc_free(qc); |
| 1808 | done(cmd); |
| 1809 | } |
| 1810 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1811 | static void ata_scsi_qc_complete(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | { |
Tejun Heo | c31f571 | 2006-11-22 12:39:43 +0900 | [diff] [blame] | 1813 | struct ata_port *ap = qc->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | struct scsi_cmnd *cmd = qc->scsicmd; |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 1815 | u8 *cdb = cmd->cmnd; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1816 | int need_sense = (qc->err_mask != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1818 | /* For ATA pass thru (SAT) commands, generate a sense block if |
| 1819 | * user mandated it or if there's an error. Note that if we |
Gwendal Grignou | 84a9a8c | 2013-01-18 10:56:43 -0800 | [diff] [blame] | 1820 | * generate because the user forced us to [CK_COND =1], a check |
| 1821 | * condition is generated and the ATA register values are returned |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1822 | * whether the command completed successfully or not. If there |
Gwendal Grignou | 84a9a8c | 2013-01-18 10:56:43 -0800 | [diff] [blame] | 1823 | * was no error, we use the following sense data: |
| 1824 | * sk = RECOVERED ERROR |
| 1825 | * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1826 | */ |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 1827 | if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) && |
Christoph Hellwig | 25c7ce7 | 2015-10-03 19:21:11 +0200 | [diff] [blame] | 1828 | ((cdb[2] & 0x20) || need_sense)) |
Tejun Heo | 750426a | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 1829 | ata_gen_passthru_sense(qc); |
Hannes Reinecke | 5b01e4b | 2016-04-04 11:43:54 +0200 | [diff] [blame] | 1830 | else if (qc->flags & ATA_QCFLAG_SENSE_VALID) |
| 1831 | cmd->result = SAM_STAT_CHECK_CONDITION; |
Christoph Hellwig | 25c7ce7 | 2015-10-03 19:21:11 +0200 | [diff] [blame] | 1832 | else if (need_sense) |
| 1833 | ata_gen_ata_sense(qc); |
| 1834 | else |
| 1835 | cmd->result = SAM_STAT_GOOD; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1836 | |
Tejun Heo | c31f571 | 2006-11-22 12:39:43 +0900 | [diff] [blame] | 1837 | if (need_sense && !ap->ops->error_handler) |
Tejun Heo | 44877b4 | 2007-02-21 01:06:51 +0900 | [diff] [blame] | 1838 | ata_dump_status(ap->print_id, &qc->result_tf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | |
Christoph Hellwig | 2aa8f5d | 2015-10-08 10:25:41 +0200 | [diff] [blame] | 1840 | ata_qc_done(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * ata_scsi_translate - Translate then issue SCSI command to ATA device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | * @dev: ATA device to which the command is addressed |
| 1846 | * @cmd: SCSI command to execute |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | * @xlat_func: Actor which translates @cmd to an ATA taskfile |
| 1848 | * |
| 1849 | * Our ->queuecommand() function has decided that the SCSI |
| 1850 | * command issued can be directly translated into an ATA |
| 1851 | * command, rather than handled internally. |
| 1852 | * |
| 1853 | * This function sets up an ata_queued_cmd structure for the |
| 1854 | * SCSI command, and sends that ata_queued_cmd to the hardware. |
| 1855 | * |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1856 | * The xlat_func argument (actor) returns 0 if ready to execute |
| 1857 | * ATA command, else 1 to finish translation. If 1 is returned |
| 1858 | * then cmd->result (and possibly cmd->sense_buffer) are assumed |
| 1859 | * to be set reflecting an error condition or clean (early) |
| 1860 | * termination. |
| 1861 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 1863 | * spin_lock_irqsave(host lock) |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 1864 | * |
| 1865 | * RETURNS: |
| 1866 | * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command |
| 1867 | * needs to be deferred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | */ |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 1869 | static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd, |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 1870 | ata_xlat_func_t xlat_func) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | { |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1872 | struct ata_port *ap = dev->link->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | struct ata_queued_cmd *qc; |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1874 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | |
| 1876 | VPRINTK("ENTER\n"); |
| 1877 | |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 1878 | qc = ata_scsi_qc_new(dev, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | if (!qc) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1880 | goto err_mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | |
| 1882 | /* data is present; dma-map it */ |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 1883 | if (cmd->sc_data_direction == DMA_FROM_DEVICE || |
| 1884 | cmd->sc_data_direction == DMA_TO_DEVICE) { |
Boaz Harrosh | 7120165 | 2007-09-18 17:48:50 +0200 | [diff] [blame] | 1885 | if (unlikely(scsi_bufflen(cmd) < 1)) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 1886 | ata_dev_warn(dev, "WARNING: zero len r/w req\n"); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1887 | goto err_did; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
Boaz Harrosh | 7120165 | 2007-09-18 17:48:50 +0200 | [diff] [blame] | 1890 | ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | |
| 1892 | qc->dma_dir = cmd->sc_data_direction; |
| 1893 | } |
| 1894 | |
| 1895 | qc->complete_fn = ata_scsi_qc_complete; |
| 1896 | |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 1897 | if (xlat_func(qc)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1898 | goto early_finish; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1900 | if (ap->ops->qc_defer) { |
| 1901 | if ((rc = ap->ops->qc_defer(qc))) |
| 1902 | goto defer; |
| 1903 | } |
| 1904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | /* select device, send command to hardware */ |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 1906 | ata_qc_issue(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | |
| 1908 | VPRINTK("EXIT\n"); |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 1909 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1911 | early_finish: |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 1912 | ata_qc_free(qc); |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 1913 | cmd->scsi_done(cmd); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1914 | DPRINTK("EXIT - early finish (good or error)\n"); |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 1915 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1916 | |
| 1917 | err_did: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | ata_qc_free(qc); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1919 | cmd->result = (DID_ERROR << 16); |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 1920 | cmd->scsi_done(cmd); |
Darrick J. Wong | 253b92e | 2006-11-14 09:55:41 -0500 | [diff] [blame] | 1921 | err_mem: |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1922 | DPRINTK("EXIT - internal\n"); |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 1923 | return 0; |
Tejun Heo | 3dc1d88 | 2006-05-15 21:03:45 +0900 | [diff] [blame] | 1924 | |
| 1925 | defer: |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1926 | ata_qc_free(qc); |
Tejun Heo | 3dc1d88 | 2006-05-15 21:03:45 +0900 | [diff] [blame] | 1927 | DPRINTK("EXIT - defer\n"); |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 1928 | if (rc == ATA_DEFER_LINK) |
| 1929 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 1930 | else |
| 1931 | return SCSI_MLQUEUE_HOST_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | /** |
| 1935 | * ata_scsi_rbuf_get - Map response buffer. |
Randy Dunlap | ec2a20e | 2008-04-30 12:57:00 -0700 | [diff] [blame] | 1936 | * @cmd: SCSI command containing buffer to be mapped. |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1937 | * @flags: unsigned long variable to store irq enable status |
| 1938 | * @copy_in: copy in from user buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | * |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1940 | * Prepare buffer for simulated SCSI commands. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | * |
| 1942 | * LOCKING: |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1943 | * spin_lock_irqsave(ata_scsi_rbuf_lock) on success |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | * |
| 1945 | * RETURNS: |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1946 | * Pointer to response buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1948 | static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in, |
| 1949 | unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | { |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1951 | spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1953 | memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE); |
| 1954 | if (copy_in) |
| 1955 | sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), |
| 1956 | ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE); |
| 1957 | return ata_scsi_rbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | /** |
| 1961 | * ata_scsi_rbuf_put - Unmap response buffer. |
| 1962 | * @cmd: SCSI command containing buffer to be unmapped. |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1963 | * @copy_out: copy out result |
| 1964 | * @flags: @flags passed to ata_scsi_rbuf_get() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | * |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1966 | * Returns rbuf buffer. The result is copied to @cmd's buffer if |
| 1967 | * @copy_back is true. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | * |
| 1969 | * LOCKING: |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1970 | * Unlocks ata_scsi_rbuf_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1972 | static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out, |
| 1973 | unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | { |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1975 | if (copy_out) |
| 1976 | sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), |
| 1977 | ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE); |
| 1978 | spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * ata_scsi_rbuf_fill - wrapper for SCSI command simulators |
| 1983 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1984 | * @actor: Callback hook for desired SCSI command simulator |
| 1985 | * |
| 1986 | * Takes care of the hard work of simulating a SCSI command... |
| 1987 | * Mapping the response buffer, calling the command's handler, |
| 1988 | * and handling the handler's return value. This return value |
| 1989 | * indicates whether the handler wishes the SCSI command to be |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1990 | * completed successfully (0), or not (in which case cmd->result |
| 1991 | * and sense buffer are assumed to be set). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | * |
| 1993 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 1994 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | */ |
Tejun Heo | f0761be | 2008-04-28 17:16:52 +0900 | [diff] [blame] | 1996 | static void ata_scsi_rbuf_fill(struct ata_scsi_args *args, |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 1997 | unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | { |
| 1999 | u8 *rbuf; |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2000 | unsigned int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | struct scsi_cmnd *cmd = args->cmd; |
Jeff Garzik | b445c56 | 2008-02-29 19:10:51 -0500 | [diff] [blame] | 2002 | unsigned long flags; |
| 2003 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2004 | rbuf = ata_scsi_rbuf_get(cmd, false, &flags); |
| 2005 | rc = actor(args, rbuf); |
| 2006 | ata_scsi_rbuf_put(cmd, rc == 0, &flags); |
Jeff Garzik | b445c56 | 2008-02-29 19:10:51 -0500 | [diff] [blame] | 2007 | |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2008 | if (rc == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | cmd->result = SAM_STAT_GOOD; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2010 | args->done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | /** |
| 2014 | * ata_scsiop_inq_std - Simulate INQUIRY command |
| 2015 | * @args: device IDENTIFY data / SCSI command of interest. |
| 2016 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | * |
| 2018 | * Returns standard device identification data associated |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2019 | * with non-VPD INQUIRY command output. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | * |
| 2021 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 2022 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2024 | static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | { |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2026 | const u8 versions[] = { |
Hannes Reinecke | f9ca5ab | 2014-11-05 13:08:22 +0100 | [diff] [blame] | 2027 | 0x00, |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2028 | 0x60, /* SAM-3 (no version claimed) */ |
| 2029 | |
| 2030 | 0x03, |
| 2031 | 0x20, /* SBC-2 (no version claimed) */ |
| 2032 | |
| 2033 | 0x02, |
| 2034 | 0x60 /* SPC-3 (no version claimed) */ |
| 2035 | }; |
Hannes Reinecke | f9ca5ab | 2014-11-05 13:08:22 +0100 | [diff] [blame] | 2036 | const u8 versions_zbc[] = { |
| 2037 | 0x00, |
| 2038 | 0xA0, /* SAM-5 (no version claimed) */ |
| 2039 | |
| 2040 | 0x04, |
| 2041 | 0xC0, /* SBC-3 (no version claimed) */ |
| 2042 | |
| 2043 | 0x04, |
| 2044 | 0x60, /* SPC-4 (no version claimed) */ |
| 2045 | |
| 2046 | 0x60, |
| 2047 | 0x20, /* ZBC (no version claimed) */ |
| 2048 | }; |
| 2049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | u8 hdr[] = { |
| 2051 | TYPE_DISK, |
| 2052 | 0, |
| 2053 | 0x5, /* claim SPC-3 version compatibility */ |
| 2054 | 2, |
| 2055 | 95 - 4 |
| 2056 | }; |
| 2057 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2058 | VPRINTK("ENTER\n"); |
| 2059 | |
Manuel Lauss | 8a3e33c | 2015-09-30 21:10:25 +0200 | [diff] [blame] | 2060 | /* set scsi removable (RMB) bit per ata bit, or if the |
| 2061 | * AHCI port says it's external (Hotplug-capable, eSATA). |
| 2062 | */ |
| 2063 | if (ata_id_removable(args->id) || |
| 2064 | (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | hdr[1] |= (1 << 7); |
| 2066 | |
Hannes Reinecke | f9ca5ab | 2014-11-05 13:08:22 +0100 | [diff] [blame] | 2067 | if (args->dev->class == ATA_DEV_ZAC) { |
| 2068 | hdr[0] = TYPE_ZBC; |
| 2069 | hdr[2] = 0x6; /* ZBC is defined in SPC-4 */ |
| 2070 | } |
| 2071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | memcpy(rbuf, hdr, sizeof(hdr)); |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2073 | memcpy(&rbuf[8], "ATA ", 8); |
| 2074 | ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16); |
Keith Busch | c49a6bf | 2014-05-01 11:12:03 -0600 | [diff] [blame] | 2075 | |
| 2076 | /* From SAT, use last 2 words from fw rev unless they are spaces */ |
| 2077 | ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4); |
| 2078 | if (strncmp(&rbuf[32], " ", 4) == 0) |
| 2079 | ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2081 | if (rbuf[32] == 0 || rbuf[32] == ' ') |
| 2082 | memcpy(&rbuf[32], "n/a ", 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | |
Hannes Reinecke | f9ca5ab | 2014-11-05 13:08:22 +0100 | [diff] [blame] | 2084 | if (args->dev->class == ATA_DEV_ZAC) |
| 2085 | memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc)); |
| 2086 | else |
| 2087 | memcpy(rbuf + 58, versions, sizeof(versions)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
| 2089 | return 0; |
| 2090 | } |
| 2091 | |
| 2092 | /** |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2093 | * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | * @args: device IDENTIFY data / SCSI command of interest. |
| 2095 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | * |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2097 | * Returns list of inquiry VPD pages available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | * |
| 2099 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 2100 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2102 | static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | { |
| 2104 | const u8 pages[] = { |
| 2105 | 0x00, /* page 0x00, this page */ |
| 2106 | 0x80, /* page 0x80, unit serial no page */ |
Matthew Wilcox | 1e9dbc9 | 2008-06-19 13:13:38 -0600 | [diff] [blame] | 2107 | 0x83, /* page 0x83, device ident page */ |
| 2108 | 0x89, /* page 0x89, ata info page */ |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 2109 | 0xb0, /* page 0xb0, block limits page */ |
Matthew Wilcox | 1e9dbc9 | 2008-06-19 13:13:38 -0600 | [diff] [blame] | 2110 | 0xb1, /* page 0xb1, block device characteristics page */ |
Martin K. Petersen | 02e0a60 | 2010-09-10 01:23:18 -0400 | [diff] [blame] | 2111 | 0xb2, /* page 0xb2, thin provisioning page */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | }; |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2113 | |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2114 | rbuf[3] = sizeof(pages); /* number of supported VPD pages */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2115 | memcpy(rbuf + 4, pages, sizeof(pages)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | return 0; |
| 2117 | } |
| 2118 | |
| 2119 | /** |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2120 | * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | * @args: device IDENTIFY data / SCSI command of interest. |
| 2122 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | * |
| 2124 | * Returns ATA device serial number. |
| 2125 | * |
| 2126 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 2127 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2129 | static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | { |
| 2131 | const u8 hdr[] = { |
| 2132 | 0, |
| 2133 | 0x80, /* this page code */ |
| 2134 | 0, |
Tejun Heo | a0cf733 | 2007-01-02 20:18:49 +0900 | [diff] [blame] | 2135 | ATA_ID_SERNO_LEN, /* page len */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | }; |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | memcpy(rbuf, hdr, sizeof(hdr)); |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2139 | ata_id_string(args->id, (unsigned char *) &rbuf[4], |
| 2140 | ATA_ID_SERNO, ATA_ID_SERNO_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | return 0; |
| 2142 | } |
| 2143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | /** |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2145 | * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | * @args: device IDENTIFY data / SCSI command of interest. |
| 2147 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | * |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2149 | * Yields two logical unit device identification designators: |
| 2150 | * - vendor specific ASCII containing the ATA serial number |
| 2151 | * - SAT defined "t10 vendor id based" containing ASCII vendor |
| 2152 | * name ("ATA "), model and serial numbers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | * |
| 2154 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 2155 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2157 | static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | { |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2159 | const int sat_model_serial_desc_len = 68; |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2160 | int num; |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | rbuf[1] = 0x83; /* this page code */ |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2163 | num = 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2165 | /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */ |
| 2166 | rbuf[num + 0] = 2; |
| 2167 | rbuf[num + 3] = ATA_ID_SERNO_LEN; |
| 2168 | num += 4; |
| 2169 | ata_id_string(args->id, (unsigned char *) rbuf + num, |
| 2170 | ATA_ID_SERNO, ATA_ID_SERNO_LEN); |
| 2171 | num += ATA_ID_SERNO_LEN; |
| 2172 | |
| 2173 | /* SAT defined lu model and serial numbers descriptor */ |
| 2174 | /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */ |
| 2175 | rbuf[num + 0] = 2; |
| 2176 | rbuf[num + 1] = 1; |
| 2177 | rbuf[num + 3] = sat_model_serial_desc_len; |
| 2178 | num += 4; |
| 2179 | memcpy(rbuf + num, "ATA ", 8); |
| 2180 | num += 8; |
| 2181 | ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD, |
| 2182 | ATA_ID_PROD_LEN); |
| 2183 | num += ATA_ID_PROD_LEN; |
| 2184 | ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO, |
| 2185 | ATA_ID_SERNO_LEN); |
| 2186 | num += ATA_ID_SERNO_LEN; |
| 2187 | |
Hannes Reinecke | 6b3b9d7 | 2011-03-07 08:56:44 +0100 | [diff] [blame] | 2188 | if (ata_id_has_wwn(args->id)) { |
| 2189 | /* SAT defined lu world wide name */ |
| 2190 | /* piv=0, assoc=lu, code_set=binary, designator=NAA */ |
| 2191 | rbuf[num + 0] = 1; |
| 2192 | rbuf[num + 1] = 3; |
| 2193 | rbuf[num + 3] = ATA_ID_WWN_LEN; |
| 2194 | num += 4; |
| 2195 | ata_id_string(args->id, (unsigned char *) rbuf + num, |
| 2196 | ATA_ID_WWN, ATA_ID_WWN_LEN); |
| 2197 | num += ATA_ID_WWN_LEN; |
| 2198 | } |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 2199 | rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | return 0; |
| 2201 | } |
| 2202 | |
| 2203 | /** |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2204 | * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info |
| 2205 | * @args: device IDENTIFY data / SCSI command of interest. |
| 2206 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2207 | * |
| 2208 | * Yields SAT-specified ATA VPD page. |
| 2209 | * |
| 2210 | * LOCKING: |
| 2211 | * spin_lock_irqsave(host lock) |
| 2212 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2213 | static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf) |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2214 | { |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2215 | struct ata_taskfile tf; |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2216 | |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2217 | memset(&tf, 0, sizeof(tf)); |
| 2218 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2219 | rbuf[1] = 0x89; /* our page code */ |
| 2220 | rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */ |
| 2221 | rbuf[3] = (0x238 & 0xff); |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2222 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2223 | memcpy(&rbuf[8], "linux ", 8); |
| 2224 | memcpy(&rbuf[16], "libata ", 16); |
| 2225 | memcpy(&rbuf[32], DRV_VERSION, 4); |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2226 | |
| 2227 | /* we don't store the ATA device signature, so we fake it */ |
| 2228 | |
| 2229 | tf.command = ATA_DRDY; /* really, this is Status reg */ |
| 2230 | tf.lbal = 0x1; |
| 2231 | tf.nsect = 0x1; |
| 2232 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2233 | ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */ |
| 2234 | rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */ |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2235 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2236 | rbuf[56] = ATA_CMD_ID_ATA; |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2237 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2238 | memcpy(&rbuf[60], &args->id[0], 512); |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2239 | return 0; |
| 2240 | } |
| 2241 | |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 2242 | static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf) |
| 2243 | { |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 2244 | u16 min_io_sectors; |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 2245 | |
| 2246 | rbuf[1] = 0xb0; |
| 2247 | rbuf[3] = 0x3c; /* required VPD size with unmap support */ |
| 2248 | |
| 2249 | /* |
| 2250 | * Optimal transfer length granularity. |
| 2251 | * |
| 2252 | * This is always one physical block, but for disks with a smaller |
| 2253 | * logical than physical sector size we need to figure out what the |
| 2254 | * latter is. |
| 2255 | */ |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 2256 | min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id); |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 2257 | put_unaligned_be16(min_io_sectors, &rbuf[6]); |
| 2258 | |
| 2259 | /* |
| 2260 | * Optimal unmap granularity. |
| 2261 | * |
| 2262 | * The ATA spec doesn't even know about a granularity or alignment |
| 2263 | * for the TRIM command. We can leave away most of the unmap related |
| 2264 | * VPD page entries, but we have specifify a granularity to signal |
| 2265 | * that we support some form of unmap - in thise case via WRITE SAME |
| 2266 | * with the unmap bit set. |
| 2267 | */ |
Martin K. Petersen | e78db4d | 2009-11-26 22:46:03 -0500 | [diff] [blame] | 2268 | if (ata_id_has_trim(args->id)) { |
Martin K. Petersen | 5f4e206 | 2011-05-17 22:13:23 -0400 | [diff] [blame] | 2269 | put_unaligned_be64(65535 * 512 / 8, &rbuf[36]); |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 2270 | put_unaligned_be32(1, &rbuf[28]); |
Martin K. Petersen | e78db4d | 2009-11-26 22:46:03 -0500 | [diff] [blame] | 2271 | } |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 2272 | |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
Matthew Wilcox | 1e9dbc9 | 2008-06-19 13:13:38 -0600 | [diff] [blame] | 2276 | static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf) |
| 2277 | { |
Martin K. Petersen | 4bca328 | 2009-05-15 00:40:35 -0400 | [diff] [blame] | 2278 | int form_factor = ata_id_form_factor(args->id); |
| 2279 | int media_rotation_rate = ata_id_rotation_rate(args->id); |
| 2280 | |
Matthew Wilcox | 1e9dbc9 | 2008-06-19 13:13:38 -0600 | [diff] [blame] | 2281 | rbuf[1] = 0xb1; |
| 2282 | rbuf[3] = 0x3c; |
Martin K. Petersen | 4bca328 | 2009-05-15 00:40:35 -0400 | [diff] [blame] | 2283 | rbuf[4] = media_rotation_rate >> 8; |
| 2284 | rbuf[5] = media_rotation_rate; |
| 2285 | rbuf[7] = form_factor; |
Matthew Wilcox | 1e9dbc9 | 2008-06-19 13:13:38 -0600 | [diff] [blame] | 2286 | |
| 2287 | return 0; |
| 2288 | } |
| 2289 | |
Martin K. Petersen | 02e0a60 | 2010-09-10 01:23:18 -0400 | [diff] [blame] | 2290 | static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf) |
| 2291 | { |
| 2292 | /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */ |
| 2293 | rbuf[1] = 0xb2; |
| 2294 | rbuf[3] = 0x4; |
| 2295 | rbuf[5] = 1 << 6; /* TPWS */ |
| 2296 | |
| 2297 | return 0; |
| 2298 | } |
| 2299 | |
Jeff Garzik | ad355b4 | 2007-09-21 20:38:03 -0400 | [diff] [blame] | 2300 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 2301 | * ata_scsiop_noop - Command handler that simply returns success. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | * @args: device IDENTIFY data / SCSI command of interest. |
| 2303 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | * |
| 2305 | * No operation. Simply returns success to caller, to indicate |
| 2306 | * that the caller should successfully complete this SCSI command. |
| 2307 | * |
| 2308 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 2309 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2311 | static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | { |
| 2313 | VPRINTK("ENTER\n"); |
| 2314 | return 0; |
| 2315 | } |
| 2316 | |
| 2317 | /** |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2318 | * modecpy - Prepare response for MODE SENSE |
| 2319 | * @dest: output buffer |
| 2320 | * @src: data being copied |
| 2321 | * @n: length of mode page |
| 2322 | * @changeable: whether changeable parameters are requested |
| 2323 | * |
| 2324 | * Generate a generic MODE SENSE page for either current or changeable |
| 2325 | * parameters. |
| 2326 | * |
| 2327 | * LOCKING: |
| 2328 | * None. |
| 2329 | */ |
| 2330 | static void modecpy(u8 *dest, const u8 *src, int n, bool changeable) |
| 2331 | { |
| 2332 | if (changeable) { |
| 2333 | memcpy(dest, src, 2); |
| 2334 | memset(dest + 2, 0, n - 2); |
| 2335 | } else { |
| 2336 | memcpy(dest, src, n); |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | * ata_msense_caching - Simulate MODE SENSE caching info page |
| 2342 | * @id: device IDENTIFY data |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2343 | * @buf: output buffer |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2344 | * @changeable: whether changeable parameters are requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | * |
| 2346 | * Generate a caching info page, which conditionally indicates |
| 2347 | * write caching to the SCSI layer, depending on device |
| 2348 | * capabilities. |
| 2349 | * |
| 2350 | * LOCKING: |
| 2351 | * None. |
| 2352 | */ |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2353 | static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | { |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2355 | modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable); |
Paolo Bonzini | 1b26d29 | 2012-07-05 14:18:21 +0200 | [diff] [blame] | 2356 | if (changeable || ata_id_wcache_enabled(id)) |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2357 | buf[2] |= (1 << 2); /* write cache enable */ |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2358 | if (!changeable && !ata_id_rahead_enabled(id)) |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2359 | buf[12] |= (1 << 5); /* disable read ahead */ |
| 2360 | return sizeof(def_cache_mpage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | /** |
| 2364 | * ata_msense_ctl_mode - Simulate MODE SENSE control mode page |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 2365 | * @dev: ATA device of interest |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2366 | * @buf: output buffer |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2367 | * @changeable: whether changeable parameters are requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | * |
| 2369 | * Generate a generic MODE SENSE control mode page. |
| 2370 | * |
| 2371 | * LOCKING: |
| 2372 | * None. |
| 2373 | */ |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 2374 | static unsigned int ata_msense_ctl_mode(struct ata_device *dev, u8 *buf, |
| 2375 | bool changeable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | { |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2377 | modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable); |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 2378 | if (changeable && (dev->flags & ATA_DFLAG_D_SENSE)) |
| 2379 | buf[2] |= (1 << 2); /* Descriptor sense requested */ |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2380 | return sizeof(def_control_mpage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | /** |
| 2384 | * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page |
Randy Dunlap | ec2a20e | 2008-04-30 12:57:00 -0700 | [diff] [blame] | 2385 | * @buf: output buffer |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2386 | * @changeable: whether changeable parameters are requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | * |
| 2388 | * Generate a generic MODE SENSE r/w error recovery page. |
| 2389 | * |
| 2390 | * LOCKING: |
| 2391 | * None. |
| 2392 | */ |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2393 | static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | { |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2395 | modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage), |
| 2396 | changeable); |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2397 | return sizeof(def_rw_recovery_mpage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | } |
| 2399 | |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 2400 | /* |
| 2401 | * We can turn this into a real blacklist if it's needed, for now just |
| 2402 | * blacklist any Maxtor BANC1G10 revision firmware |
| 2403 | */ |
| 2404 | static int ata_dev_supports_fua(u16 *id) |
| 2405 | { |
Tejun Heo | a0cf733 | 2007-01-02 20:18:49 +0900 | [diff] [blame] | 2406 | unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1]; |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 2407 | |
Jeff Garzik | c3c013a | 2006-02-27 22:31:19 -0500 | [diff] [blame] | 2408 | if (!libata_fua) |
| 2409 | return 0; |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 2410 | if (!ata_id_has_fua(id)) |
| 2411 | return 0; |
| 2412 | |
Tejun Heo | a0cf733 | 2007-01-02 20:18:49 +0900 | [diff] [blame] | 2413 | ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model)); |
| 2414 | ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw)); |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 2415 | |
Tejun Heo | 2e02671 | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 2416 | if (strcmp(model, "Maxtor")) |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 2417 | return 1; |
Tejun Heo | 2e02671 | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 2418 | if (strcmp(fw, "BANC1G10")) |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 2419 | return 1; |
| 2420 | |
| 2421 | return 0; /* blacklisted */ |
| 2422 | } |
| 2423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | /** |
| 2425 | * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands |
| 2426 | * @args: device IDENTIFY data / SCSI command of interest. |
| 2427 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | * |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2429 | * Simulate MODE SENSE commands. Assume this is invoked for direct |
| 2430 | * access devices (e.g. disks) only. There should be no block |
| 2431 | * descriptor for other device types. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | * |
| 2433 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 2434 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2436 | static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | { |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 2438 | struct ata_device *dev = args->dev; |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2439 | u8 *scsicmd = args->cmd->cmnd, *p = rbuf; |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2440 | const u8 sat_blk_desc[] = { |
| 2441 | 0, 0, 0, 0, /* number of blocks: sat unspecified */ |
| 2442 | 0, |
| 2443 | 0, 0x2, 0x0 /* block length: 512 bytes */ |
| 2444 | }; |
| 2445 | u8 pg, spg; |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2446 | unsigned int ebd, page_control, six_byte; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 2447 | u8 dpofua; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | |
| 2449 | VPRINTK("ENTER\n"); |
| 2450 | |
| 2451 | six_byte = (scsicmd[0] == MODE_SENSE); |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2452 | ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */ |
| 2453 | /* |
| 2454 | * LLBA bit in msense(10) ignored (compliant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | */ |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | page_control = scsicmd[2] >> 6; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2458 | switch (page_control) { |
| 2459 | case 0: /* current */ |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2460 | case 1: /* changeable */ |
| 2461 | case 2: /* defaults */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2462 | break; /* supported */ |
| 2463 | case 3: /* saved */ |
| 2464 | goto saving_not_supp; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2465 | default: |
| 2466 | goto invalid_fld; |
| 2467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2469 | if (six_byte) |
| 2470 | p += 4 + (ebd ? 8 : 0); |
| 2471 | else |
| 2472 | p += 8 + (ebd ? 8 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2474 | pg = scsicmd[2] & 0x3f; |
| 2475 | spg = scsicmd[3]; |
| 2476 | /* |
| 2477 | * No mode subpages supported (yet) but asking for _all_ |
| 2478 | * subpages may be valid |
| 2479 | */ |
| 2480 | if (spg && (spg != ALL_SUB_MPAGES)) |
| 2481 | goto invalid_fld; |
| 2482 | |
| 2483 | switch(pg) { |
| 2484 | case RW_RECOVERY_MPAGE: |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2485 | p += ata_msense_rw_recovery(p, page_control == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | break; |
| 2487 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2488 | case CACHE_MPAGE: |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2489 | p += ata_msense_caching(args->id, p, page_control == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | break; |
| 2491 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2492 | case CONTROL_MPAGE: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 2493 | p += ata_msense_ctl_mode(args->dev, p, page_control == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2496 | case ALL_MPAGES: |
Paolo Bonzini | 6ca8e79 | 2012-07-05 14:18:20 +0200 | [diff] [blame] | 2497 | p += ata_msense_rw_recovery(p, page_control == 1); |
| 2498 | p += ata_msense_caching(args->id, p, page_control == 1); |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 2499 | p += ata_msense_ctl_mode(args->dev, p, page_control == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | break; |
| 2501 | |
| 2502 | default: /* invalid page code */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2503 | goto invalid_fld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | } |
| 2505 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 2506 | dpofua = 0; |
Alan Cox | f79d409 | 2006-05-22 16:55:11 +0100 | [diff] [blame] | 2507 | if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) && |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 2508 | (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count)) |
| 2509 | dpofua = 1 << 4; |
| 2510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | if (six_byte) { |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2512 | rbuf[0] = p - rbuf - 1; |
| 2513 | rbuf[2] |= dpofua; |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2514 | if (ebd) { |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2515 | rbuf[3] = sizeof(sat_blk_desc); |
| 2516 | memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc)); |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | } else { |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2519 | unsigned int output_len = p - rbuf - 2; |
| 2520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | rbuf[0] = output_len >> 8; |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2522 | rbuf[1] = output_len; |
| 2523 | rbuf[3] |= dpofua; |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2524 | if (ebd) { |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2525 | rbuf[7] = sizeof(sat_blk_desc); |
| 2526 | memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc)); |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2530 | |
| 2531 | invalid_fld: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 2532 | ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x24, 0x0); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2533 | /* "Invalid field in cbd" */ |
| 2534 | return 1; |
| 2535 | |
| 2536 | saving_not_supp: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 2537 | ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2538 | /* "Saving parameters not supported" */ |
| 2539 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | /** |
| 2543 | * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands |
| 2544 | * @args: device IDENTIFY data / SCSI command of interest. |
| 2545 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | * |
| 2547 | * Simulate READ CAPACITY commands. |
| 2548 | * |
| 2549 | * LOCKING: |
Tejun Heo | 6a36261 | 2006-11-20 11:15:47 +0900 | [diff] [blame] | 2550 | * None. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2552 | static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | { |
Martin K. Petersen | 61d79a8 | 2009-05-15 00:40:34 -0400 | [diff] [blame] | 2554 | struct ata_device *dev = args->dev; |
| 2555 | u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */ |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 2556 | u32 sector_size; /* physical sector size in bytes */ |
| 2557 | u8 log2_per_phys; |
| 2558 | u16 lowest_aligned; |
Martin K. Petersen | 61d79a8 | 2009-05-15 00:40:34 -0400 | [diff] [blame] | 2559 | |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 2560 | sector_size = ata_id_logical_sector_size(dev->id); |
| 2561 | log2_per_phys = ata_id_log2_per_physical_sector(dev->id); |
| 2562 | lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | |
| 2564 | VPRINTK("ENTER\n"); |
| 2565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | if (args->cmd->cmnd[0] == READ_CAPACITY) { |
Tejun Heo | 6a36261 | 2006-11-20 11:15:47 +0900 | [diff] [blame] | 2567 | if (last_lba >= 0xffffffffULL) |
| 2568 | last_lba = 0xffffffff; |
Philip Pokorny | 0c144d0 | 2005-05-28 01:24:47 -0700 | [diff] [blame] | 2569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | /* sector count, 32-bit */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2571 | rbuf[0] = last_lba >> (8 * 3); |
| 2572 | rbuf[1] = last_lba >> (8 * 2); |
| 2573 | rbuf[2] = last_lba >> (8 * 1); |
| 2574 | rbuf[3] = last_lba; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | |
| 2576 | /* sector size */ |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 2577 | rbuf[4] = sector_size >> (8 * 3); |
| 2578 | rbuf[5] = sector_size >> (8 * 2); |
| 2579 | rbuf[6] = sector_size >> (8 * 1); |
| 2580 | rbuf[7] = sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | } else { |
| 2582 | /* sector count, 64-bit */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2583 | rbuf[0] = last_lba >> (8 * 7); |
| 2584 | rbuf[1] = last_lba >> (8 * 6); |
| 2585 | rbuf[2] = last_lba >> (8 * 5); |
| 2586 | rbuf[3] = last_lba >> (8 * 4); |
| 2587 | rbuf[4] = last_lba >> (8 * 3); |
| 2588 | rbuf[5] = last_lba >> (8 * 2); |
| 2589 | rbuf[6] = last_lba >> (8 * 1); |
| 2590 | rbuf[7] = last_lba; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | |
| 2592 | /* sector size */ |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 2593 | rbuf[ 8] = sector_size >> (8 * 3); |
| 2594 | rbuf[ 9] = sector_size >> (8 * 2); |
| 2595 | rbuf[10] = sector_size >> (8 * 1); |
| 2596 | rbuf[11] = sector_size; |
Martin K. Petersen | 61d79a8 | 2009-05-15 00:40:34 -0400 | [diff] [blame] | 2597 | |
| 2598 | rbuf[12] = 0; |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 2599 | rbuf[13] = log2_per_phys; |
Martin K. Petersen | 61d79a8 | 2009-05-15 00:40:34 -0400 | [diff] [blame] | 2600 | rbuf[14] = (lowest_aligned >> 8) & 0x3f; |
| 2601 | rbuf[15] = lowest_aligned; |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 2602 | |
Arne Fitzenreiter | 71d126f | 2015-07-15 13:54:36 +0200 | [diff] [blame] | 2603 | if (ata_id_has_trim(args->id) && |
| 2604 | !(dev->horkage & ATA_HORKAGE_NOTRIM)) { |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 2605 | rbuf[14] |= 0x80; /* LBPME */ |
Martin K. Petersen | e78db4d | 2009-11-26 22:46:03 -0500 | [diff] [blame] | 2606 | |
Martin K. Petersen | e61f7d1 | 2015-01-08 10:34:27 -0500 | [diff] [blame] | 2607 | if (ata_id_has_zero_after_trim(args->id) && |
| 2608 | dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) { |
| 2609 | ata_dev_info(dev, "Enabling discard_zeroes_data\n"); |
| 2610 | rbuf[14] |= 0x40; /* LBPRZ */ |
| 2611 | } |
Martin K. Petersen | e78db4d | 2009-11-26 22:46:03 -0500 | [diff] [blame] | 2612 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | return 0; |
| 2615 | } |
| 2616 | |
| 2617 | /** |
| 2618 | * ata_scsiop_report_luns - Simulate REPORT LUNS command |
| 2619 | * @args: device IDENTIFY data / SCSI command of interest. |
| 2620 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | * |
| 2622 | * Simulate REPORT LUNS command. |
| 2623 | * |
| 2624 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 2625 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | */ |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2627 | static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | { |
| 2629 | VPRINTK("ENTER\n"); |
| 2630 | rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */ |
| 2631 | |
| 2632 | return 0; |
| 2633 | } |
| 2634 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 2635 | static void atapi_sense_complete(struct ata_queued_cmd *qc) |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2636 | { |
Tejun Heo | 74e6c8c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 2637 | if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) { |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2638 | /* FIXME: not quite right; we don't want the |
| 2639 | * translation of taskfile registers into |
| 2640 | * a sense descriptors, since that's only |
| 2641 | * correct for ATA, not ATAPI |
| 2642 | */ |
Tejun Heo | 750426a | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 2643 | ata_gen_passthru_sense(qc); |
Tejun Heo | 74e6c8c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 2644 | } |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2645 | |
Christoph Hellwig | 2aa8f5d | 2015-10-08 10:25:41 +0200 | [diff] [blame] | 2646 | ata_qc_done(qc); |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | /* is it pointless to prefer PIO for "safety reasons"? */ |
| 2650 | static inline int ata_pio_use_silly(struct ata_port *ap) |
| 2651 | { |
| 2652 | return (ap->flags & ATA_FLAG_PIO_DMA); |
| 2653 | } |
| 2654 | |
| 2655 | static void atapi_request_sense(struct ata_queued_cmd *qc) |
| 2656 | { |
| 2657 | struct ata_port *ap = qc->ap; |
| 2658 | struct scsi_cmnd *cmd = qc->scsicmd; |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2659 | |
| 2660 | DPRINTK("ATAPI request sense\n"); |
| 2661 | |
James Bottomley | 7ccd720 | 2008-01-17 11:56:24 -0600 | [diff] [blame] | 2662 | memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2663 | |
Tejun Heo | 127102a | 2008-04-07 22:47:21 +0900 | [diff] [blame] | 2664 | #ifdef CONFIG_ATA_SFF |
James Bottomley | 855d854 | 2008-04-18 13:18:48 -0500 | [diff] [blame] | 2665 | if (ap->ops->sff_tf_read) |
| 2666 | ap->ops->sff_tf_read(ap, &qc->tf); |
Tejun Heo | 127102a | 2008-04-07 22:47:21 +0900 | [diff] [blame] | 2667 | #endif |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2668 | |
| 2669 | /* fill these in, for the case where they are -not- overwritten */ |
| 2670 | cmd->sense_buffer[0] = 0x70; |
| 2671 | cmd->sense_buffer[2] = qc->tf.feature >> 4; |
| 2672 | |
| 2673 | ata_qc_reinit(qc); |
| 2674 | |
Tejun Heo | 93f8fec | 2007-12-05 16:43:01 +0900 | [diff] [blame] | 2675 | /* setup sg table and init transfer direction */ |
akpm@linux-foundation.org | cadb734 | 2008-01-15 16:01:52 -0800 | [diff] [blame] | 2676 | sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE); |
Tejun Heo | 93f8fec | 2007-12-05 16:43:01 +0900 | [diff] [blame] | 2677 | ata_sg_init(qc, &qc->sgent, 1); |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2678 | qc->dma_dir = DMA_FROM_DEVICE; |
| 2679 | |
Tejun Heo | 6e7846e | 2006-02-12 23:32:58 +0900 | [diff] [blame] | 2680 | memset(&qc->cdb, 0, qc->dev->cdb_len); |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2681 | qc->cdb[0] = REQUEST_SENSE; |
| 2682 | qc->cdb[4] = SCSI_SENSE_BUFFERSIZE; |
| 2683 | |
| 2684 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 2685 | qc->tf.command = ATA_CMD_PACKET; |
| 2686 | |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2687 | if (ata_pio_use_silly(ap)) { |
Tejun Heo | 0dc3688 | 2007-12-18 16:34:43 -0500 | [diff] [blame] | 2688 | qc->tf.protocol = ATAPI_PROT_DMA; |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2689 | qc->tf.feature |= ATAPI_PKT_DMA; |
| 2690 | } else { |
Tejun Heo | 0dc3688 | 2007-12-18 16:34:43 -0500 | [diff] [blame] | 2691 | qc->tf.protocol = ATAPI_PROT_PIO; |
Alan Cox | 2db78dd | 2007-10-02 13:53:04 -0700 | [diff] [blame] | 2692 | qc->tf.lbam = SCSI_SENSE_BUFFERSIZE; |
| 2693 | qc->tf.lbah = 0; |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2694 | } |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2695 | qc->nbytes = SCSI_SENSE_BUFFERSIZE; |
| 2696 | |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2697 | qc->complete_fn = atapi_sense_complete; |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2698 | |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 2699 | ata_qc_issue(qc); |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2700 | |
| 2701 | DPRINTK("EXIT\n"); |
| 2702 | } |
| 2703 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 2704 | static void atapi_qc_complete(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | { |
| 2706 | struct scsi_cmnd *cmd = qc->scsicmd; |
Albert Lee | a22e2eb | 2005-12-05 15:38:02 +0800 | [diff] [blame] | 2707 | unsigned int err_mask = qc->err_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 2709 | VPRINTK("ENTER, err_mask 0x%X\n", err_mask); |
Jeff Garzik | e12669e | 2005-10-05 18:39:23 -0400 | [diff] [blame] | 2710 | |
Tejun Heo | 246619d | 2006-05-15 20:58:16 +0900 | [diff] [blame] | 2711 | /* handle completion from new EH */ |
| 2712 | if (unlikely(qc->ap->ops->error_handler && |
| 2713 | (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) { |
| 2714 | |
| 2715 | if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) { |
| 2716 | /* FIXME: not quite right; we don't want the |
| 2717 | * translation of taskfile registers into a |
| 2718 | * sense descriptors, since that's only |
| 2719 | * correct for ATA, not ATAPI |
| 2720 | */ |
Tejun Heo | 750426a | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 2721 | ata_gen_passthru_sense(qc); |
Tejun Heo | 246619d | 2006-05-15 20:58:16 +0900 | [diff] [blame] | 2722 | } |
| 2723 | |
Tejun Heo | 22aac08 | 2006-08-08 14:08:59 +0900 | [diff] [blame] | 2724 | /* SCSI EH automatically locks door if sdev->locked is |
| 2725 | * set. Sometimes door lock request continues to |
| 2726 | * fail, for example, when no media is present. This |
| 2727 | * creates a loop - SCSI EH issues door lock which |
| 2728 | * fails and gets invoked again to acquire sense data |
| 2729 | * for the failed command. |
| 2730 | * |
| 2731 | * If door lock fails, always clear sdev->locked to |
| 2732 | * avoid this infinite loop. |
Tejun Heo | 2a5f07b | 2010-11-01 11:39:19 +0100 | [diff] [blame] | 2733 | * |
| 2734 | * This may happen before SCSI scan is complete. Make |
| 2735 | * sure qc->dev->sdev isn't NULL before dereferencing. |
Tejun Heo | 22aac08 | 2006-08-08 14:08:59 +0900 | [diff] [blame] | 2736 | */ |
Tejun Heo | 2a5f07b | 2010-11-01 11:39:19 +0100 | [diff] [blame] | 2737 | if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev) |
Tejun Heo | 22aac08 | 2006-08-08 14:08:59 +0900 | [diff] [blame] | 2738 | qc->dev->sdev->locked = 0; |
| 2739 | |
Tejun Heo | 246619d | 2006-05-15 20:58:16 +0900 | [diff] [blame] | 2740 | qc->scsicmd->result = SAM_STAT_CHECK_CONDITION; |
Christoph Hellwig | 2aa8f5d | 2015-10-08 10:25:41 +0200 | [diff] [blame] | 2741 | ata_qc_done(qc); |
Tejun Heo | 246619d | 2006-05-15 20:58:16 +0900 | [diff] [blame] | 2742 | return; |
| 2743 | } |
| 2744 | |
| 2745 | /* successful completion or old EH failure path */ |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 2746 | if (unlikely(err_mask & AC_ERR_DEV)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | cmd->result = SAM_STAT_CHECK_CONDITION; |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2748 | atapi_request_sense(qc); |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 2749 | return; |
Tejun Heo | 74e6c8c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 2750 | } else if (unlikely(err_mask)) { |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 2751 | /* FIXME: not quite right; we don't want the |
| 2752 | * translation of taskfile registers into |
| 2753 | * a sense descriptors, since that's only |
| 2754 | * correct for ATA, not ATAPI |
| 2755 | */ |
Tejun Heo | 750426a | 2006-11-14 22:37:35 +0900 | [diff] [blame] | 2756 | ata_gen_passthru_sense(qc); |
Tejun Heo | 74e6c8c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 2757 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | u8 *scsicmd = cmd->cmnd; |
| 2759 | |
Tony Battersby | fd71da4 | 2005-12-21 16:35:44 -0500 | [diff] [blame] | 2760 | if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) { |
Jeff Garzik | b445c56 | 2008-02-29 19:10:51 -0500 | [diff] [blame] | 2761 | unsigned long flags; |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2762 | u8 *buf; |
Jeff Garzik | b445c56 | 2008-02-29 19:10:51 -0500 | [diff] [blame] | 2763 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2764 | buf = ata_scsi_rbuf_get(cmd, true, &flags); |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 2765 | |
| 2766 | /* ATAPI devices typically report zero for their SCSI version, |
| 2767 | * and sometimes deviate from the spec WRT response data |
| 2768 | * format. If SCSI version is reported as zero like normal, |
| 2769 | * then we make the following fixups: 1) Fake MMC-5 version, |
| 2770 | * to indicate to the Linux scsi midlayer this is a modern |
| 2771 | * device. 2) Ensure response data format / ATAPI information |
| 2772 | * are always correct. |
| 2773 | */ |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 2774 | if (buf[2] == 0) { |
| 2775 | buf[2] = 0x5; |
| 2776 | buf[3] = 0x32; |
| 2777 | } |
| 2778 | |
Tejun Heo | 87340e9 | 2008-04-28 17:48:51 +0900 | [diff] [blame] | 2779 | ata_scsi_rbuf_put(cmd, true, &flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | } |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 2781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | cmd->result = SAM_STAT_GOOD; |
| 2783 | } |
| 2784 | |
Christoph Hellwig | 2aa8f5d | 2015-10-08 10:25:41 +0200 | [diff] [blame] | 2785 | ata_qc_done(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | } |
| 2787 | /** |
| 2788 | * atapi_xlat - Initialize PACKET taskfile |
| 2789 | * @qc: command structure to be initialized |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | * |
| 2791 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 2792 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | * |
| 2794 | * RETURNS: |
| 2795 | * Zero on success, non-zero on failure. |
| 2796 | */ |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 2797 | static unsigned int atapi_xlat(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | { |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 2799 | struct scsi_cmnd *scmd = qc->scsicmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | struct ata_device *dev = qc->dev; |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 2801 | int nodata = (scmd->sc_data_direction == DMA_NONE); |
Tejun Heo | 5895ef9 | 2008-06-17 12:36:26 +0900 | [diff] [blame] | 2802 | int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO); |
Alan Cox | 2db78dd | 2007-10-02 13:53:04 -0700 | [diff] [blame] | 2803 | unsigned int nbytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | |
Tejun Heo | 2e5704f | 2006-12-17 10:46:33 +0900 | [diff] [blame] | 2805 | memset(qc->cdb, 0, dev->cdb_len); |
| 2806 | memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | |
| 2808 | qc->complete_fn = atapi_qc_complete; |
| 2809 | |
| 2810 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 2811 | if (scmd->sc_data_direction == DMA_TO_DEVICE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | qc->tf.flags |= ATA_TFLAG_WRITE; |
| 2813 | DPRINTK("direction: write\n"); |
| 2814 | } |
| 2815 | |
| 2816 | qc->tf.command = ATA_CMD_PACKET; |
Tejun Heo | aacda37 | 2008-03-18 17:47:43 +0900 | [diff] [blame] | 2817 | ata_qc_set_pc_nbytes(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | |
Tejun Heo | e00f1ff | 2007-06-27 02:47:35 +0900 | [diff] [blame] | 2819 | /* check whether ATAPI DMA is safe */ |
Tejun Heo | 5895ef9 | 2008-06-17 12:36:26 +0900 | [diff] [blame] | 2820 | if (!nodata && !using_pio && atapi_check_dma(qc)) |
Tejun Heo | e00f1ff | 2007-06-27 02:47:35 +0900 | [diff] [blame] | 2821 | using_pio = 1; |
| 2822 | |
Tejun Heo | e190222 | 2007-11-26 20:58:02 +0900 | [diff] [blame] | 2823 | /* Some controller variants snoop this value for Packet |
| 2824 | * transfers to do state machine and FIFO management. Thus we |
| 2825 | * want to set it properly, and for DMA where it is |
| 2826 | * effectively meaningless. |
| 2827 | */ |
Tejun Heo | aacda37 | 2008-03-18 17:47:43 +0900 | [diff] [blame] | 2828 | nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024); |
Alan Cox | 2db78dd | 2007-10-02 13:53:04 -0700 | [diff] [blame] | 2829 | |
Tejun Heo | e190222 | 2007-11-26 20:58:02 +0900 | [diff] [blame] | 2830 | /* Most ATAPI devices which honor transfer chunk size don't |
| 2831 | * behave according to the spec when odd chunk size which |
| 2832 | * matches the transfer length is specified. If the number of |
| 2833 | * bytes to transfer is 2n+1. According to the spec, what |
| 2834 | * should happen is to indicate that 2n+1 is going to be |
| 2835 | * transferred and transfer 2n+2 bytes where the last byte is |
| 2836 | * padding. |
| 2837 | * |
| 2838 | * In practice, this doesn't happen. ATAPI devices first |
| 2839 | * indicate and transfer 2n bytes and then indicate and |
| 2840 | * transfer 2 bytes where the last byte is padding. |
| 2841 | * |
| 2842 | * This inconsistency confuses several controllers which |
| 2843 | * perform PIO using DMA such as Intel AHCIs and sil3124/32. |
| 2844 | * These controllers use actual number of transferred bytes to |
| 2845 | * update DMA poitner and transfer of 4n+2 bytes make those |
| 2846 | * controller push DMA pointer by 4n+4 bytes because SATA data |
| 2847 | * FISes are aligned to 4 bytes. This causes data corruption |
| 2848 | * and buffer overrun. |
| 2849 | * |
| 2850 | * Always setting nbytes to even number solves this problem |
| 2851 | * because then ATAPI devices don't have to split data at 2n |
| 2852 | * boundaries. |
| 2853 | */ |
| 2854 | if (nbytes & 0x1) |
| 2855 | nbytes++; |
| 2856 | |
Alan Cox | 2db78dd | 2007-10-02 13:53:04 -0700 | [diff] [blame] | 2857 | qc->tf.lbam = (nbytes & 0xFF); |
| 2858 | qc->tf.lbah = (nbytes >> 8); |
| 2859 | |
Tejun Heo | 5895ef9 | 2008-06-17 12:36:26 +0900 | [diff] [blame] | 2860 | if (nodata) |
| 2861 | qc->tf.protocol = ATAPI_PROT_NODATA; |
| 2862 | else if (using_pio) |
| 2863 | qc->tf.protocol = ATAPI_PROT_PIO; |
| 2864 | else { |
Tejun Heo | e00f1ff | 2007-06-27 02:47:35 +0900 | [diff] [blame] | 2865 | /* DMA data xfer */ |
Tejun Heo | 0dc3688 | 2007-12-18 16:34:43 -0500 | [diff] [blame] | 2866 | qc->tf.protocol = ATAPI_PROT_DMA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | qc->tf.feature |= ATAPI_PKT_DMA; |
| 2868 | |
Tejun Heo | 9116300 | 2008-02-21 13:25:50 +0900 | [diff] [blame] | 2869 | if ((dev->flags & ATA_DFLAG_DMADIR) && |
| 2870 | (scmd->sc_data_direction != DMA_TO_DEVICE)) |
Albert Lee | 95de719 | 2006-04-04 10:57:18 +0800 | [diff] [blame] | 2871 | /* some SATA bridges need us to indicate data xfer direction */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | qc->tf.feature |= ATAPI_DMADIR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | } |
| 2874 | |
Alan Cox | 2db78dd | 2007-10-02 13:53:04 -0700 | [diff] [blame] | 2875 | |
| 2876 | /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE |
| 2877 | as ATAPI tape drives don't get this right otherwise */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | return 0; |
| 2879 | } |
| 2880 | |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 2881 | static struct ata_device *ata_find_dev(struct ata_port *ap, int devno) |
Tejun Heo | ab5b3a5 | 2006-05-31 18:27:34 +0900 | [diff] [blame] | 2882 | { |
Tejun Heo | 071f44b | 2008-04-07 22:47:22 +0900 | [diff] [blame] | 2883 | if (!sata_pmp_attached(ap)) { |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2884 | if (likely(devno < ata_link_max_devices(&ap->link))) |
| 2885 | return &ap->link.device[devno]; |
| 2886 | } else { |
| 2887 | if (likely(devno < ap->nr_pmp_links)) |
| 2888 | return &ap->pmp_link[devno].device[0]; |
| 2889 | } |
| 2890 | |
Tejun Heo | ab5b3a5 | 2006-05-31 18:27:34 +0900 | [diff] [blame] | 2891 | return NULL; |
| 2892 | } |
| 2893 | |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 2894 | static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap, |
| 2895 | const struct scsi_device *scsidev) |
Tejun Heo | ab5b3a5 | 2006-05-31 18:27:34 +0900 | [diff] [blame] | 2896 | { |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2897 | int devno; |
Tejun Heo | ab5b3a5 | 2006-05-31 18:27:34 +0900 | [diff] [blame] | 2898 | |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2899 | /* skip commands not addressed to targets we simulate */ |
Tejun Heo | 071f44b | 2008-04-07 22:47:22 +0900 | [diff] [blame] | 2900 | if (!sata_pmp_attached(ap)) { |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2901 | if (unlikely(scsidev->channel || scsidev->lun)) |
| 2902 | return NULL; |
| 2903 | devno = scsidev->id; |
| 2904 | } else { |
| 2905 | if (unlikely(scsidev->id || scsidev->lun)) |
| 2906 | return NULL; |
| 2907 | devno = scsidev->channel; |
| 2908 | } |
| 2909 | |
| 2910 | return ata_find_dev(ap, devno); |
Tejun Heo | ab5b3a5 | 2006-05-31 18:27:34 +0900 | [diff] [blame] | 2911 | } |
| 2912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | /** |
| 2914 | * ata_scsi_find_dev - lookup ata_device from scsi_cmnd |
| 2915 | * @ap: ATA port to which the device is attached |
| 2916 | * @scsidev: SCSI device from which we derive the ATA device |
| 2917 | * |
| 2918 | * Given various information provided in struct scsi_cmnd, |
| 2919 | * map that onto an ATA bus, and using that mapping |
| 2920 | * determine which ata_device is associated with the |
| 2921 | * SCSI command to be sent. |
| 2922 | * |
| 2923 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 2924 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | * |
| 2926 | * RETURNS: |
| 2927 | * Associated ATA device, or %NULL if not found. |
| 2928 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | static struct ata_device * |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 2930 | ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | { |
Tejun Heo | ab5b3a5 | 2006-05-31 18:27:34 +0900 | [diff] [blame] | 2932 | struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | |
Tejun Heo | 2486fa5 | 2008-07-31 07:52:40 +0900 | [diff] [blame] | 2934 | if (unlikely(!dev || !ata_dev_enabled(dev))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | return NULL; |
| 2936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | return dev; |
| 2938 | } |
| 2939 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2940 | /* |
| 2941 | * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value. |
| 2942 | * @byte1: Byte 1 from pass-thru CDB. |
| 2943 | * |
| 2944 | * RETURNS: |
| 2945 | * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise. |
| 2946 | */ |
| 2947 | static u8 |
| 2948 | ata_scsi_map_proto(u8 byte1) |
| 2949 | { |
| 2950 | switch((byte1 & 0x1e) >> 1) { |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 2951 | case 3: /* Non-data */ |
| 2952 | return ATA_PROT_NODATA; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2953 | |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 2954 | case 6: /* DMA */ |
| 2955 | case 10: /* UDMA Data-in */ |
| 2956 | case 11: /* UDMA Data-Out */ |
| 2957 | return ATA_PROT_DMA; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2958 | |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 2959 | case 4: /* PIO Data-in */ |
| 2960 | case 5: /* PIO Data-out */ |
| 2961 | return ATA_PROT_PIO; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2962 | |
Vinayak Kale | ee7fb33 | 2015-10-27 11:19:15 +0530 | [diff] [blame] | 2963 | case 12: /* FPDMA */ |
| 2964 | return ATA_PROT_NCQ; |
| 2965 | |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 2966 | case 0: /* Hard Reset */ |
| 2967 | case 1: /* SRST */ |
| 2968 | case 8: /* Device Diagnostic */ |
| 2969 | case 9: /* Device Reset */ |
| 2970 | case 7: /* DMA Queued */ |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 2971 | case 15: /* Return Response Info */ |
| 2972 | default: /* Reserved */ |
| 2973 | break; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | return ATA_PROT_UNKNOWN; |
| 2977 | } |
| 2978 | |
| 2979 | /** |
| 2980 | * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile |
| 2981 | * @qc: command structure to be initialized |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2982 | * |
| 2983 | * Handles either 12 or 16-byte versions of the CDB. |
| 2984 | * |
| 2985 | * RETURNS: |
| 2986 | * Zero on success, non-zero on failure. |
| 2987 | */ |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 2988 | static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2989 | { |
| 2990 | struct ata_taskfile *tf = &(qc->tf); |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 2991 | struct scsi_cmnd *scmd = qc->scsicmd; |
Alan Cox | f79d409 | 2006-05-22 16:55:11 +0100 | [diff] [blame] | 2992 | struct ata_device *dev = qc->dev; |
Tejun Heo | ad70699 | 2006-12-17 10:45:57 +0900 | [diff] [blame] | 2993 | const u8 *cdb = scmd->cmnd; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2994 | |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 2995 | if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN) |
Tejun Heo | 9a40525 | 2005-12-02 11:49:11 +0900 | [diff] [blame] | 2996 | goto invalid_fld; |
Jeff Garzik | 8190bdb | 2006-05-24 01:53:39 -0400 | [diff] [blame] | 2997 | |
Vinayak Kale | 5f8e7f1 | 2015-10-27 13:11:46 +0530 | [diff] [blame] | 2998 | /* enable LBA */ |
| 2999 | tf->flags |= ATA_TFLAG_LBA; |
| 3000 | |
Alan Cox | ae8d4ee | 2007-11-04 22:05:49 -0500 | [diff] [blame] | 3001 | /* |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3002 | * 12 and 16 byte CDBs use different offsets to |
| 3003 | * provide the various register values. |
| 3004 | */ |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 3005 | if (cdb[0] == ATA_16) { |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3006 | /* |
| 3007 | * 16-byte CDB - may contain extended commands. |
| 3008 | * |
| 3009 | * If that is the case, copy the upper byte register values. |
| 3010 | */ |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 3011 | if (cdb[1] & 0x01) { |
| 3012 | tf->hob_feature = cdb[3]; |
| 3013 | tf->hob_nsect = cdb[5]; |
| 3014 | tf->hob_lbal = cdb[7]; |
| 3015 | tf->hob_lbam = cdb[9]; |
| 3016 | tf->hob_lbah = cdb[11]; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3017 | tf->flags |= ATA_TFLAG_LBA48; |
| 3018 | } else |
| 3019 | tf->flags &= ~ATA_TFLAG_LBA48; |
| 3020 | |
| 3021 | /* |
| 3022 | * Always copy low byte, device and command registers. |
| 3023 | */ |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 3024 | tf->feature = cdb[4]; |
| 3025 | tf->nsect = cdb[6]; |
| 3026 | tf->lbal = cdb[8]; |
| 3027 | tf->lbam = cdb[10]; |
| 3028 | tf->lbah = cdb[12]; |
| 3029 | tf->device = cdb[13]; |
| 3030 | tf->command = cdb[14]; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3031 | } else { |
| 3032 | /* |
| 3033 | * 12-byte CDB - incapable of extended commands. |
| 3034 | */ |
| 3035 | tf->flags &= ~ATA_TFLAG_LBA48; |
| 3036 | |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 3037 | tf->feature = cdb[3]; |
| 3038 | tf->nsect = cdb[4]; |
| 3039 | tf->lbal = cdb[5]; |
| 3040 | tf->lbam = cdb[6]; |
| 3041 | tf->lbah = cdb[7]; |
| 3042 | tf->device = cdb[8]; |
| 3043 | tf->command = cdb[9]; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3044 | } |
Albert Lee | fa4453c | 2007-06-07 15:52:07 +0800 | [diff] [blame] | 3045 | |
Vinayak Kale | ee7fb33 | 2015-10-27 11:19:15 +0530 | [diff] [blame] | 3046 | /* For NCQ commands with FPDMA protocol, copy the tag value */ |
| 3047 | if (tf->protocol == ATA_PROT_NCQ) |
| 3048 | tf->nsect = qc->tag << 3; |
| 3049 | |
Albert Lee | fa4453c | 2007-06-07 15:52:07 +0800 | [diff] [blame] | 3050 | /* enforce correct master/slave bit */ |
| 3051 | tf->device = dev->devno ? |
| 3052 | tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3053 | |
Tejun Heo | bd30add | 2009-09-03 16:08:11 +0900 | [diff] [blame] | 3054 | switch (tf->command) { |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 3055 | /* READ/WRITE LONG use a non-standard sect_size */ |
Tejun Heo | bd30add | 2009-09-03 16:08:11 +0900 | [diff] [blame] | 3056 | case ATA_CMD_READ_LONG: |
| 3057 | case ATA_CMD_READ_LONG_ONCE: |
| 3058 | case ATA_CMD_WRITE_LONG: |
| 3059 | case ATA_CMD_WRITE_LONG_ONCE: |
| 3060 | if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) |
| 3061 | goto invalid_fld; |
| 3062 | qc->sect_size = scsi_bufflen(scmd); |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 3063 | break; |
| 3064 | |
| 3065 | /* commands using reported Logical Block size (e.g. 512 or 4K) */ |
| 3066 | case ATA_CMD_CFA_WRITE_NE: |
| 3067 | case ATA_CMD_CFA_TRANS_SECT: |
| 3068 | case ATA_CMD_CFA_WRITE_MULT_NE: |
| 3069 | /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */ |
| 3070 | case ATA_CMD_READ: |
| 3071 | case ATA_CMD_READ_EXT: |
| 3072 | case ATA_CMD_READ_QUEUED: |
| 3073 | /* XXX: case ATA_CMD_READ_QUEUED_EXT: */ |
| 3074 | case ATA_CMD_FPDMA_READ: |
| 3075 | case ATA_CMD_READ_MULTI: |
| 3076 | case ATA_CMD_READ_MULTI_EXT: |
| 3077 | case ATA_CMD_PIO_READ: |
| 3078 | case ATA_CMD_PIO_READ_EXT: |
| 3079 | case ATA_CMD_READ_STREAM_DMA_EXT: |
| 3080 | case ATA_CMD_READ_STREAM_EXT: |
| 3081 | case ATA_CMD_VERIFY: |
| 3082 | case ATA_CMD_VERIFY_EXT: |
| 3083 | case ATA_CMD_WRITE: |
| 3084 | case ATA_CMD_WRITE_EXT: |
| 3085 | case ATA_CMD_WRITE_FUA_EXT: |
| 3086 | case ATA_CMD_WRITE_QUEUED: |
| 3087 | case ATA_CMD_WRITE_QUEUED_FUA_EXT: |
| 3088 | case ATA_CMD_FPDMA_WRITE: |
| 3089 | case ATA_CMD_WRITE_MULTI: |
| 3090 | case ATA_CMD_WRITE_MULTI_EXT: |
| 3091 | case ATA_CMD_WRITE_MULTI_FUA_EXT: |
| 3092 | case ATA_CMD_PIO_WRITE: |
| 3093 | case ATA_CMD_PIO_WRITE_EXT: |
| 3094 | case ATA_CMD_WRITE_STREAM_DMA_EXT: |
| 3095 | case ATA_CMD_WRITE_STREAM_EXT: |
| 3096 | qc->sect_size = scmd->device->sector_size; |
| 3097 | break; |
| 3098 | |
| 3099 | /* Everything else uses 512 byte "sectors" */ |
| 3100 | default: |
| 3101 | qc->sect_size = ATA_SECT_SIZE; |
Tejun Heo | bd30add | 2009-09-03 16:08:11 +0900 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | /* |
| 3105 | * Set flags so that all registers will be written, pass on |
| 3106 | * write indication (used for PIO/DMA setup), result TF is |
| 3107 | * copied back and we don't whine too much about its failure. |
| 3108 | */ |
Douglas Gilbert | bc496ed | 2010-02-01 13:11:38 -0500 | [diff] [blame] | 3109 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
Tejun Heo | bd30add | 2009-09-03 16:08:11 +0900 | [diff] [blame] | 3110 | if (scmd->sc_data_direction == DMA_TO_DEVICE) |
| 3111 | tf->flags |= ATA_TFLAG_WRITE; |
| 3112 | |
| 3113 | qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET; |
| 3114 | |
| 3115 | /* |
| 3116 | * Set transfer length. |
| 3117 | * |
| 3118 | * TODO: find out if we need to do more here to |
| 3119 | * cover scatter/gather case. |
| 3120 | */ |
| 3121 | ata_qc_set_pc_nbytes(qc); |
| 3122 | |
| 3123 | /* We may not issue DMA commands if no DMA mode is set */ |
| 3124 | if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) |
| 3125 | goto invalid_fld; |
| 3126 | |
Albert Lee | 1dce589 | 2007-06-07 15:49:22 +0800 | [diff] [blame] | 3127 | /* sanity check for pio multi commands */ |
| 3128 | if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) |
| 3129 | goto invalid_fld; |
| 3130 | |
| 3131 | if (is_multi_taskfile(tf)) { |
| 3132 | unsigned int multi_count = 1 << (cdb[1] >> 5); |
| 3133 | |
| 3134 | /* compare the passed through multi_count |
| 3135 | * with the cached multi_count of libata |
| 3136 | */ |
| 3137 | if (multi_count != dev->multi_count) |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3138 | ata_dev_warn(dev, "invalid multi_count %u ignored\n", |
| 3139 | multi_count); |
Alan Cox | d26fc95 | 2007-07-06 19:13:52 -0400 | [diff] [blame] | 3140 | } |
Albert Lee | 1dce589 | 2007-06-07 15:49:22 +0800 | [diff] [blame] | 3141 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3142 | /* |
| 3143 | * Filter SET_FEATURES - XFER MODE command -- otherwise, |
| 3144 | * SET_FEATURES - XFER MODE must be preceded/succeeded |
| 3145 | * by an update to hardware-specific registers for each |
| 3146 | * controller (i.e. the reason for ->set_piomode(), |
| 3147 | * ->set_dmamode(), and ->post_set_mode() hooks). |
| 3148 | */ |
Tejun Heo | bd30add | 2009-09-03 16:08:11 +0900 | [diff] [blame] | 3149 | if (tf->command == ATA_CMD_SET_FEATURES && |
| 3150 | tf->feature == SETFEATURES_XFER) |
Tejun Heo | 9a40525 | 2005-12-02 11:49:11 +0900 | [diff] [blame] | 3151 | goto invalid_fld; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3152 | |
| 3153 | /* |
Tejun Heo | bd30add | 2009-09-03 16:08:11 +0900 | [diff] [blame] | 3154 | * Filter TPM commands by default. These provide an |
| 3155 | * essentially uncontrolled encrypted "back door" between |
| 3156 | * applications and the disk. Set libata.allow_tpm=1 if you |
| 3157 | * have a real reason for wanting to use them. This ensures |
| 3158 | * that installed software cannot easily mess stuff up without |
| 3159 | * user intent. DVR type users will probably ship with this enabled |
| 3160 | * for movie content management. |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3161 | * |
Tejun Heo | bd30add | 2009-09-03 16:08:11 +0900 | [diff] [blame] | 3162 | * Note that for ATA8 we can issue a DCS change and DCS freeze lock |
| 3163 | * for this and should do in future but that it is not sufficient as |
| 3164 | * DCS is an optional feature set. Thus we also do the software filter |
| 3165 | * so that we comply with the TC consortium stated goal that the user |
| 3166 | * can turn off TC features of their system. |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3167 | */ |
Tejun Heo | bd30add | 2009-09-03 16:08:11 +0900 | [diff] [blame] | 3168 | if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) |
| 3169 | goto invalid_fld; |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 3170 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3171 | return 0; |
Tejun Heo | 9a40525 | 2005-12-02 11:49:11 +0900 | [diff] [blame] | 3172 | |
| 3173 | invalid_fld: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3174 | ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x24, 0x00); |
Tejun Heo | 9a40525 | 2005-12-02 11:49:11 +0900 | [diff] [blame] | 3175 | /* "Invalid field in cdb" */ |
| 3176 | return 1; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3177 | } |
| 3178 | |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 3179 | static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc) |
| 3180 | { |
| 3181 | struct ata_taskfile *tf = &qc->tf; |
| 3182 | struct scsi_cmnd *scmd = qc->scsicmd; |
| 3183 | struct ata_device *dev = qc->dev; |
| 3184 | const u8 *cdb = scmd->cmnd; |
| 3185 | u64 block; |
| 3186 | u32 n_block; |
| 3187 | u32 size; |
| 3188 | void *buf; |
| 3189 | |
| 3190 | /* we may not issue DMA commands if no DMA mode is set */ |
| 3191 | if (unlikely(!dev->dma_mode)) |
| 3192 | goto invalid_fld; |
| 3193 | |
| 3194 | if (unlikely(scmd->cmd_len < 16)) |
| 3195 | goto invalid_fld; |
| 3196 | scsi_16_lba_len(cdb, &block, &n_block); |
| 3197 | |
| 3198 | /* for now we only support WRITE SAME with the unmap bit set */ |
| 3199 | if (unlikely(!(cdb[1] & 0x8))) |
| 3200 | goto invalid_fld; |
| 3201 | |
| 3202 | /* |
| 3203 | * WRITE SAME always has a sector sized buffer as payload, this |
| 3204 | * should never be a multiple entry S/G list. |
| 3205 | */ |
| 3206 | if (!scsi_sg_count(scmd)) |
| 3207 | goto invalid_fld; |
| 3208 | |
| 3209 | buf = page_address(sg_page(scsi_sglist(scmd))); |
Martin K. Petersen | d0634c4 | 2009-11-26 12:00:43 -0500 | [diff] [blame] | 3210 | size = ata_set_lba_range_entries(buf, 512, block, n_block); |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 3211 | |
Marc Carino | 87fb6c3 | 2013-08-24 23:22:51 -0700 | [diff] [blame] | 3212 | if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) { |
| 3213 | /* Newer devices support queued TRIM commands */ |
| 3214 | tf->protocol = ATA_PROT_NCQ; |
| 3215 | tf->command = ATA_CMD_FPDMA_SEND; |
| 3216 | tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f; |
| 3217 | tf->nsect = qc->tag << 3; |
| 3218 | tf->hob_feature = (size / 512) >> 8; |
| 3219 | tf->feature = size / 512; |
| 3220 | |
| 3221 | tf->auxiliary = 1; |
| 3222 | } else { |
| 3223 | tf->protocol = ATA_PROT_DMA; |
| 3224 | tf->hob_feature = 0; |
| 3225 | tf->feature = ATA_DSM_TRIM; |
| 3226 | tf->hob_nsect = (size / 512) >> 8; |
| 3227 | tf->nsect = size / 512; |
| 3228 | tf->command = ATA_CMD_DSM; |
| 3229 | } |
| 3230 | |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 3231 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 | |
| 3232 | ATA_TFLAG_WRITE; |
| 3233 | |
| 3234 | ata_qc_set_pc_nbytes(qc); |
| 3235 | |
| 3236 | return 0; |
| 3237 | |
| 3238 | invalid_fld: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3239 | ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x24, 0x00); |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 3240 | /* "Invalid field in cdb" */ |
| 3241 | return 1; |
| 3242 | } |
| 3243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | /** |
Paolo Bonzini | 1b26d29 | 2012-07-05 14:18:21 +0200 | [diff] [blame] | 3245 | * ata_mselect_caching - Simulate MODE SELECT for caching info page |
| 3246 | * @qc: Storage for translated ATA taskfile |
| 3247 | * @buf: input buffer |
| 3248 | * @len: number of valid bytes in the input buffer |
| 3249 | * |
| 3250 | * Prepare a taskfile to modify caching information for the device. |
| 3251 | * |
| 3252 | * LOCKING: |
| 3253 | * None. |
| 3254 | */ |
| 3255 | static int ata_mselect_caching(struct ata_queued_cmd *qc, |
| 3256 | const u8 *buf, int len) |
| 3257 | { |
| 3258 | struct ata_taskfile *tf = &qc->tf; |
| 3259 | struct ata_device *dev = qc->dev; |
| 3260 | char mpage[CACHE_MPAGE_LEN]; |
| 3261 | u8 wce; |
| 3262 | |
| 3263 | /* |
| 3264 | * The first two bytes of def_cache_mpage are a header, so offsets |
| 3265 | * in mpage are off by 2 compared to buf. Same for len. |
| 3266 | */ |
| 3267 | |
| 3268 | if (len != CACHE_MPAGE_LEN - 2) |
| 3269 | return -EINVAL; |
| 3270 | |
| 3271 | wce = buf[0] & (1 << 2); |
| 3272 | |
| 3273 | /* |
| 3274 | * Check that read-only bits are not modified. |
| 3275 | */ |
| 3276 | ata_msense_caching(dev->id, mpage, false); |
| 3277 | mpage[2] &= ~(1 << 2); |
| 3278 | mpage[2] |= wce; |
| 3279 | if (memcmp(mpage + 2, buf, CACHE_MPAGE_LEN - 2) != 0) |
| 3280 | return -EINVAL; |
| 3281 | |
| 3282 | tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
| 3283 | tf->protocol = ATA_PROT_NODATA; |
| 3284 | tf->nsect = 0; |
| 3285 | tf->command = ATA_CMD_SET_FEATURES; |
| 3286 | tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF; |
| 3287 | return 0; |
| 3288 | } |
| 3289 | |
| 3290 | /** |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3291 | * ata_mselect_control - Simulate MODE SELECT for control page |
| 3292 | * @qc: Storage for translated ATA taskfile |
| 3293 | * @buf: input buffer |
| 3294 | * @len: number of valid bytes in the input buffer |
| 3295 | * |
| 3296 | * Prepare a taskfile to modify caching information for the device. |
| 3297 | * |
| 3298 | * LOCKING: |
| 3299 | * None. |
| 3300 | */ |
| 3301 | static int ata_mselect_control(struct ata_queued_cmd *qc, |
| 3302 | const u8 *buf, int len) |
| 3303 | { |
| 3304 | struct ata_device *dev = qc->dev; |
| 3305 | char mpage[CONTROL_MPAGE_LEN]; |
| 3306 | u8 d_sense; |
| 3307 | |
| 3308 | /* |
| 3309 | * The first two bytes of def_control_mpage are a header, so offsets |
| 3310 | * in mpage are off by 2 compared to buf. Same for len. |
| 3311 | */ |
| 3312 | |
| 3313 | if (len != CONTROL_MPAGE_LEN - 2) |
| 3314 | return -EINVAL; |
| 3315 | |
| 3316 | d_sense = buf[0] & (1 << 2); |
| 3317 | |
| 3318 | /* |
| 3319 | * Check that read-only bits are not modified. |
| 3320 | */ |
| 3321 | ata_msense_ctl_mode(dev, mpage, false); |
| 3322 | mpage[2] &= ~(1 << 2); |
| 3323 | mpage[2] |= d_sense; |
| 3324 | if (memcmp(mpage + 2, buf, CONTROL_MPAGE_LEN - 2) != 0) |
| 3325 | return -EINVAL; |
| 3326 | if (d_sense & (1 << 2)) |
| 3327 | dev->flags |= ATA_DFLAG_D_SENSE; |
| 3328 | else |
| 3329 | dev->flags &= ~ATA_DFLAG_D_SENSE; |
| 3330 | qc->scsicmd->result = SAM_STAT_GOOD; |
| 3331 | qc->scsicmd->scsi_done(qc->scsicmd); |
| 3332 | return 0; |
| 3333 | } |
| 3334 | |
| 3335 | /** |
Paolo Bonzini | 1b26d29 | 2012-07-05 14:18:21 +0200 | [diff] [blame] | 3336 | * ata_scsiop_mode_select - Simulate MODE SELECT 6, 10 commands |
| 3337 | * @qc: Storage for translated ATA taskfile |
| 3338 | * |
| 3339 | * Converts a MODE SELECT command to an ATA SET FEATURES taskfile. |
| 3340 | * Assume this is invoked for direct access devices (e.g. disks) only. |
| 3341 | * There should be no block descriptor for other device types. |
| 3342 | * |
| 3343 | * LOCKING: |
| 3344 | * spin_lock_irqsave(host lock) |
| 3345 | */ |
| 3346 | static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc) |
| 3347 | { |
| 3348 | struct scsi_cmnd *scmd = qc->scsicmd; |
| 3349 | const u8 *cdb = scmd->cmnd; |
| 3350 | const u8 *p; |
| 3351 | u8 pg, spg; |
| 3352 | unsigned six_byte, pg_len, hdr_len, bd_len; |
| 3353 | int len; |
| 3354 | |
| 3355 | VPRINTK("ENTER\n"); |
| 3356 | |
| 3357 | six_byte = (cdb[0] == MODE_SELECT); |
| 3358 | if (six_byte) { |
| 3359 | if (scmd->cmd_len < 5) |
| 3360 | goto invalid_fld; |
| 3361 | |
| 3362 | len = cdb[4]; |
| 3363 | hdr_len = 4; |
| 3364 | } else { |
| 3365 | if (scmd->cmd_len < 9) |
| 3366 | goto invalid_fld; |
| 3367 | |
| 3368 | len = (cdb[7] << 8) + cdb[8]; |
| 3369 | hdr_len = 8; |
| 3370 | } |
| 3371 | |
| 3372 | /* We only support PF=1, SP=0. */ |
| 3373 | if ((cdb[1] & 0x11) != 0x10) |
| 3374 | goto invalid_fld; |
| 3375 | |
| 3376 | /* Test early for possible overrun. */ |
| 3377 | if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len) |
| 3378 | goto invalid_param_len; |
| 3379 | |
| 3380 | p = page_address(sg_page(scsi_sglist(scmd))); |
| 3381 | |
| 3382 | /* Move past header and block descriptors. */ |
| 3383 | if (len < hdr_len) |
| 3384 | goto invalid_param_len; |
| 3385 | |
| 3386 | if (six_byte) |
| 3387 | bd_len = p[3]; |
| 3388 | else |
| 3389 | bd_len = (p[6] << 8) + p[7]; |
| 3390 | |
| 3391 | len -= hdr_len; |
| 3392 | p += hdr_len; |
| 3393 | if (len < bd_len) |
| 3394 | goto invalid_param_len; |
| 3395 | if (bd_len != 0 && bd_len != 8) |
| 3396 | goto invalid_param; |
| 3397 | |
| 3398 | len -= bd_len; |
| 3399 | p += bd_len; |
| 3400 | if (len == 0) |
| 3401 | goto skip; |
| 3402 | |
| 3403 | /* Parse both possible formats for the mode page headers. */ |
| 3404 | pg = p[0] & 0x3f; |
| 3405 | if (p[0] & 0x40) { |
| 3406 | if (len < 4) |
| 3407 | goto invalid_param_len; |
| 3408 | |
| 3409 | spg = p[1]; |
| 3410 | pg_len = (p[2] << 8) | p[3]; |
| 3411 | p += 4; |
| 3412 | len -= 4; |
| 3413 | } else { |
| 3414 | if (len < 2) |
| 3415 | goto invalid_param_len; |
| 3416 | |
| 3417 | spg = 0; |
| 3418 | pg_len = p[1]; |
| 3419 | p += 2; |
| 3420 | len -= 2; |
| 3421 | } |
| 3422 | |
| 3423 | /* |
| 3424 | * No mode subpages supported (yet) but asking for _all_ |
| 3425 | * subpages may be valid |
| 3426 | */ |
| 3427 | if (spg && (spg != ALL_SUB_MPAGES)) |
| 3428 | goto invalid_param; |
| 3429 | if (pg_len > len) |
| 3430 | goto invalid_param_len; |
| 3431 | |
| 3432 | switch (pg) { |
| 3433 | case CACHE_MPAGE: |
| 3434 | if (ata_mselect_caching(qc, p, pg_len) < 0) |
| 3435 | goto invalid_param; |
| 3436 | break; |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3437 | case CONTROL_MPAGE: |
| 3438 | if (ata_mselect_control(qc, p, pg_len) < 0) |
| 3439 | goto invalid_param; |
| 3440 | break; |
Paolo Bonzini | 1b26d29 | 2012-07-05 14:18:21 +0200 | [diff] [blame] | 3441 | default: /* invalid page code */ |
| 3442 | goto invalid_param; |
| 3443 | } |
| 3444 | |
| 3445 | /* |
| 3446 | * Only one page has changeable data, so we only support setting one |
| 3447 | * page at a time. |
| 3448 | */ |
| 3449 | if (len > pg_len) |
| 3450 | goto invalid_param; |
| 3451 | |
| 3452 | return 0; |
| 3453 | |
| 3454 | invalid_fld: |
| 3455 | /* "Invalid field in CDB" */ |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3456 | ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x24, 0x0); |
Paolo Bonzini | 1b26d29 | 2012-07-05 14:18:21 +0200 | [diff] [blame] | 3457 | return 1; |
| 3458 | |
| 3459 | invalid_param: |
| 3460 | /* "Invalid field in parameter list" */ |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3461 | ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x26, 0x0); |
Paolo Bonzini | 1b26d29 | 2012-07-05 14:18:21 +0200 | [diff] [blame] | 3462 | return 1; |
| 3463 | |
| 3464 | invalid_param_len: |
| 3465 | /* "Parameter list length error" */ |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3466 | ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0); |
Paolo Bonzini | 1b26d29 | 2012-07-05 14:18:21 +0200 | [diff] [blame] | 3467 | return 1; |
| 3468 | |
| 3469 | skip: |
| 3470 | scmd->result = SAM_STAT_GOOD; |
| 3471 | return 1; |
| 3472 | } |
| 3473 | |
| 3474 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | * ata_get_xlat_func - check if SCSI to ATA translation is possible |
| 3476 | * @dev: ATA device |
| 3477 | * @cmd: SCSI command opcode to consider |
| 3478 | * |
| 3479 | * Look up the SCSI command given, and determine whether the |
| 3480 | * SCSI command is to be translated or simulated. |
| 3481 | * |
| 3482 | * RETURNS: |
| 3483 | * Pointer to translation function if possible, %NULL if not. |
| 3484 | */ |
| 3485 | |
| 3486 | static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd) |
| 3487 | { |
| 3488 | switch (cmd) { |
| 3489 | case READ_6: |
| 3490 | case READ_10: |
| 3491 | case READ_16: |
| 3492 | |
| 3493 | case WRITE_6: |
| 3494 | case WRITE_10: |
| 3495 | case WRITE_16: |
| 3496 | return ata_scsi_rw_xlat; |
| 3497 | |
Christoph Hellwig | 0cdd6eb | 2009-12-10 10:36:01 +0100 | [diff] [blame] | 3498 | case WRITE_SAME_16: |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 3499 | return ata_scsi_write_same_xlat; |
| 3500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | case SYNCHRONIZE_CACHE: |
| 3502 | if (ata_try_flush_cache(dev)) |
| 3503 | return ata_scsi_flush_xlat; |
| 3504 | break; |
| 3505 | |
| 3506 | case VERIFY: |
| 3507 | case VERIFY_16: |
| 3508 | return ata_scsi_verify_xlat; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 3509 | |
| 3510 | case ATA_12: |
| 3511 | case ATA_16: |
| 3512 | return ata_scsi_pass_thru; |
Jeff Garzik | da61396 | 2005-08-29 19:01:43 -0400 | [diff] [blame] | 3513 | |
Paolo Bonzini | 1b26d29 | 2012-07-05 14:18:21 +0200 | [diff] [blame] | 3514 | case MODE_SELECT: |
| 3515 | case MODE_SELECT_10: |
| 3516 | return ata_scsi_mode_select_xlat; |
| 3517 | break; |
| 3518 | |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 3519 | case START_STOP: |
| 3520 | return ata_scsi_start_stop_xlat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | return NULL; |
| 3524 | } |
| 3525 | |
| 3526 | /** |
| 3527 | * ata_scsi_dump_cdb - dump SCSI command contents to dmesg |
| 3528 | * @ap: ATA port to which the command was being sent |
| 3529 | * @cmd: SCSI command to dump |
| 3530 | * |
| 3531 | * Prints the contents of a SCSI command via printk(). |
| 3532 | */ |
| 3533 | |
| 3534 | static inline void ata_scsi_dump_cdb(struct ata_port *ap, |
| 3535 | struct scsi_cmnd *cmd) |
| 3536 | { |
| 3537 | #ifdef ATA_DEBUG |
| 3538 | struct scsi_device *scsidev = cmd->device; |
| 3539 | u8 *scsicmd = cmd->cmnd; |
| 3540 | |
| 3541 | DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", |
Tejun Heo | 44877b4 | 2007-02-21 01:06:51 +0900 | [diff] [blame] | 3542 | ap->print_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | scsidev->channel, scsidev->id, scsidev->lun, |
| 3544 | scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3], |
| 3545 | scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7], |
| 3546 | scsicmd[8]); |
| 3547 | #endif |
| 3548 | } |
| 3549 | |
Tejun Heo | 542b144 | 2006-12-17 10:45:08 +0900 | [diff] [blame] | 3550 | static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd, |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 3551 | struct ata_device *dev) |
Brian King | eb3f0f9 | 2006-03-23 17:30:02 -0600 | [diff] [blame] | 3552 | { |
Mark Lord | baf4fdf | 2007-08-08 01:08:45 +0900 | [diff] [blame] | 3553 | u8 scsi_op = scmd->cmnd[0]; |
| 3554 | ata_xlat_func_t xlat_func; |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 3555 | int rc = 0; |
| 3556 | |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 3557 | if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) { |
Mark Lord | baf4fdf | 2007-08-08 01:08:45 +0900 | [diff] [blame] | 3558 | if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len)) |
| 3559 | goto bad_cdb_len; |
| 3560 | |
| 3561 | xlat_func = ata_get_xlat_func(dev, scsi_op); |
| 3562 | } else { |
| 3563 | if (unlikely(!scmd->cmd_len)) |
| 3564 | goto bad_cdb_len; |
| 3565 | |
| 3566 | xlat_func = NULL; |
| 3567 | if (likely((scsi_op != ATA_16) || !atapi_passthru16)) { |
| 3568 | /* relay SCSI command to ATAPI device */ |
Mark Lord | 607126c | 2007-11-15 13:13:59 +0900 | [diff] [blame] | 3569 | int len = COMMAND_SIZE(scsi_op); |
| 3570 | if (unlikely(len > scmd->cmd_len || len > dev->cdb_len)) |
Mark Lord | baf4fdf | 2007-08-08 01:08:45 +0900 | [diff] [blame] | 3571 | goto bad_cdb_len; |
| 3572 | |
| 3573 | xlat_func = atapi_xlat; |
| 3574 | } else { |
| 3575 | /* ATA_16 passthru, treat as an ATA command */ |
| 3576 | if (unlikely(scmd->cmd_len > 16)) |
| 3577 | goto bad_cdb_len; |
| 3578 | |
| 3579 | xlat_func = ata_get_xlat_func(dev, scsi_op); |
| 3580 | } |
Tejun Heo | 2e5704f | 2006-12-17 10:46:33 +0900 | [diff] [blame] | 3581 | } |
| 3582 | |
Mark Lord | baf4fdf | 2007-08-08 01:08:45 +0900 | [diff] [blame] | 3583 | if (xlat_func) |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 3584 | rc = ata_scsi_translate(dev, scmd, xlat_func); |
Mark Lord | baf4fdf | 2007-08-08 01:08:45 +0900 | [diff] [blame] | 3585 | else |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 3586 | ata_scsi_simulate(dev, scmd); |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 3587 | |
| 3588 | return rc; |
Mark Lord | baf4fdf | 2007-08-08 01:08:45 +0900 | [diff] [blame] | 3589 | |
| 3590 | bad_cdb_len: |
| 3591 | DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n", |
| 3592 | scmd->cmd_len, scsi_op, dev->cdb_len); |
| 3593 | scmd->result = DID_ERROR << 16; |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 3594 | scmd->scsi_done(scmd); |
Mark Lord | baf4fdf | 2007-08-08 01:08:45 +0900 | [diff] [blame] | 3595 | return 0; |
Brian King | eb3f0f9 | 2006-03-23 17:30:02 -0600 | [diff] [blame] | 3596 | } |
| 3597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | /** |
| 3599 | * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device |
Jeff Garzik | 23e701e | 2010-11-17 12:03:58 -0500 | [diff] [blame] | 3600 | * @shost: SCSI host of command to be sent |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3601 | * @cmd: SCSI command to be sent |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | * |
| 3603 | * In some cases, this function translates SCSI commands into |
| 3604 | * ATA taskfiles, and queues the taskfiles to be sent to |
| 3605 | * hardware. In other cases, this function simulates a |
| 3606 | * SCSI device by evaluating and responding to certain |
| 3607 | * SCSI commands. This creates the overall effect of |
| 3608 | * ATA and ATAPI devices appearing as SCSI devices. |
| 3609 | * |
| 3610 | * LOCKING: |
Jeff Garzik | 23e701e | 2010-11-17 12:03:58 -0500 | [diff] [blame] | 3611 | * ATA host lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | * |
| 3613 | * RETURNS: |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 3614 | * Return value from __ata_scsi_queuecmd() if @cmd can be queued, |
| 3615 | * 0 otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | */ |
Jeff Garzik | 23e701e | 2010-11-17 12:03:58 -0500 | [diff] [blame] | 3617 | int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | { |
| 3619 | struct ata_port *ap; |
| 3620 | struct ata_device *dev; |
| 3621 | struct scsi_device *scsidev = cmd->device; |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 3622 | int rc = 0; |
Jeff Garzik | 23e701e | 2010-11-17 12:03:58 -0500 | [diff] [blame] | 3623 | unsigned long irq_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | |
Jeff Garzik | 35bb94b | 2006-04-11 13:12:34 -0400 | [diff] [blame] | 3625 | ap = ata_shost_to_port(shost); |
Jeff Garzik | 005a5a0 | 2005-10-30 23:31:48 -0500 | [diff] [blame] | 3626 | |
Jeff Garzik | 23e701e | 2010-11-17 12:03:58 -0500 | [diff] [blame] | 3627 | spin_lock_irqsave(ap->lock, irq_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | |
| 3629 | ata_scsi_dump_cdb(ap, cmd); |
| 3630 | |
| 3631 | dev = ata_scsi_find_dev(ap, scsidev); |
Brian King | eb3f0f9 | 2006-03-23 17:30:02 -0600 | [diff] [blame] | 3632 | if (likely(dev)) |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 3633 | rc = __ata_scsi_queuecmd(cmd, dev); |
Brian King | eb3f0f9 | 2006-03-23 17:30:02 -0600 | [diff] [blame] | 3634 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | cmd->result = (DID_BAD_TARGET << 16); |
Jeff Garzik | 23e701e | 2010-11-17 12:03:58 -0500 | [diff] [blame] | 3636 | cmd->scsi_done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | } |
| 3638 | |
Jeff Garzik | 23e701e | 2010-11-17 12:03:58 -0500 | [diff] [blame] | 3639 | spin_unlock_irqrestore(ap->lock, irq_flags); |
| 3640 | |
Tejun Heo | 2115ea9 | 2006-05-15 21:03:39 +0900 | [diff] [blame] | 3641 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | /** |
| 3645 | * ata_scsi_simulate - simulate SCSI command on ATA device |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 3646 | * @dev: the target device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | * @cmd: SCSI command being sent to device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | * |
| 3649 | * Interprets and directly executes a select list of SCSI commands |
| 3650 | * that can be handled internally. |
| 3651 | * |
| 3652 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 3653 | * spin_lock_irqsave(host lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | */ |
| 3655 | |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 3656 | void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | { |
| 3658 | struct ata_scsi_args args; |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 3659 | const u8 *scsicmd = cmd->cmnd; |
Jeff Garzik | 4539414 | 2007-09-21 06:23:42 -0400 | [diff] [blame] | 3660 | u8 tmp8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 3662 | args.dev = dev; |
| 3663 | args.id = dev->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3664 | args.cmd = cmd; |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 3665 | args.done = cmd->scsi_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | |
| 3667 | switch(scsicmd[0]) { |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3668 | /* TODO: worth improving? */ |
| 3669 | case FORMAT_UNIT: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3670 | ata_scsi_invalid_field(dev, cmd); |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3671 | break; |
| 3672 | |
| 3673 | case INQUIRY: |
| 3674 | if (scsicmd[1] & 2) /* is CmdDt set? */ |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3675 | ata_scsi_invalid_field(dev, cmd); |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3676 | else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */ |
| 3677 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std); |
| 3678 | else switch (scsicmd[2]) { |
| 3679 | case 0x00: |
| 3680 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | break; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3682 | case 0x80: |
| 3683 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | break; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3685 | case 0x83: |
| 3686 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3687 | break; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3688 | case 0x89: |
| 3689 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3690 | break; |
Christoph Hellwig | 18f0f97 | 2009-11-17 10:00:47 -0500 | [diff] [blame] | 3691 | case 0xb0: |
| 3692 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0); |
| 3693 | break; |
Matthew Wilcox | 1e9dbc9 | 2008-06-19 13:13:38 -0600 | [diff] [blame] | 3694 | case 0xb1: |
| 3695 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1); |
| 3696 | break; |
Martin K. Petersen | 02e0a60 | 2010-09-10 01:23:18 -0400 | [diff] [blame] | 3697 | case 0xb2: |
| 3698 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2); |
| 3699 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | default: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3701 | ata_scsi_invalid_field(dev, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | break; |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3703 | } |
| 3704 | break; |
| 3705 | |
| 3706 | case MODE_SENSE: |
| 3707 | case MODE_SENSE_10: |
| 3708 | ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense); |
| 3709 | break; |
| 3710 | |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3711 | case READ_CAPACITY: |
| 3712 | ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); |
| 3713 | break; |
| 3714 | |
Hannes Reinecke | eb846d9 | 2014-11-17 14:25:19 +0100 | [diff] [blame] | 3715 | case SERVICE_ACTION_IN_16: |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3716 | if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16) |
| 3717 | ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); |
| 3718 | else |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3719 | ata_scsi_invalid_field(dev, cmd); |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3720 | break; |
| 3721 | |
| 3722 | case REPORT_LUNS: |
| 3723 | ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns); |
| 3724 | break; |
| 3725 | |
| 3726 | case REQUEST_SENSE: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3727 | ata_scsi_set_sense(dev, cmd, 0, 0, 0); |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3728 | cmd->result = (DRIVER_SENSE << 24); |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 3729 | cmd->scsi_done(cmd); |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3730 | break; |
| 3731 | |
| 3732 | /* if we reach this, then writeback caching is disabled, |
| 3733 | * turning this into a no-op. |
| 3734 | */ |
| 3735 | case SYNCHRONIZE_CACHE: |
| 3736 | /* fall through */ |
| 3737 | |
| 3738 | /* no-op's, complete with success */ |
| 3739 | case REZERO_UNIT: |
| 3740 | case SEEK_6: |
| 3741 | case SEEK_10: |
| 3742 | case TEST_UNIT_READY: |
| 3743 | ata_scsi_rbuf_fill(&args, ata_scsiop_noop); |
| 3744 | break; |
| 3745 | |
| 3746 | case SEND_DIAGNOSTIC: |
| 3747 | tmp8 = scsicmd[1] & ~(1 << 3); |
| 3748 | if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4])) |
| 3749 | ata_scsi_rbuf_fill(&args, ata_scsiop_noop); |
| 3750 | else |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3751 | ata_scsi_invalid_field(dev, cmd); |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3752 | break; |
| 3753 | |
| 3754 | /* all other commands */ |
| 3755 | default: |
Hannes Reinecke | 06dbde5 | 2016-04-04 11:44:03 +0200 | [diff] [blame^] | 3756 | ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0); |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3757 | /* "Invalid command operation code" */ |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 3758 | cmd->scsi_done(cmd); |
Jeff Garzik | 2dcb407 | 2007-10-19 06:42:56 -0400 | [diff] [blame] | 3759 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | } |
| 3761 | } |
| 3762 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 3763 | int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht) |
| 3764 | { |
| 3765 | int i, rc; |
| 3766 | |
| 3767 | for (i = 0; i < host->n_ports; i++) { |
| 3768 | struct ata_port *ap = host->ports[i]; |
| 3769 | struct Scsi_Host *shost; |
| 3770 | |
| 3771 | rc = -ENOMEM; |
| 3772 | shost = scsi_host_alloc(sht, sizeof(struct ata_port *)); |
| 3773 | if (!shost) |
| 3774 | goto err_alloc; |
| 3775 | |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 3776 | shost->eh_noresume = 1; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 3777 | *(struct ata_port **)&shost->hostdata[0] = ap; |
| 3778 | ap->scsi_host = shost; |
| 3779 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 3780 | shost->transportt = ata_scsi_transport_template; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 3781 | shost->unique_id = ap->print_id; |
| 3782 | shost->max_id = 16; |
| 3783 | shost->max_lun = 1; |
| 3784 | shost->max_channel = 1; |
| 3785 | shost->max_cmd_len = 16; |
Martin K. Petersen | 54b2b50 | 2013-10-23 06:25:40 -0400 | [diff] [blame] | 3786 | shost->no_write_same = 1; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 3787 | |
Tejun Heo | 31cc23b | 2007-09-23 13:14:12 +0900 | [diff] [blame] | 3788 | /* Schedule policy is determined by ->qc_defer() |
| 3789 | * callback and it needs to see every deferred qc. |
| 3790 | * Set host_blocked to 1 to prevent SCSI midlayer from |
| 3791 | * automatically deferring requests. |
| 3792 | */ |
| 3793 | shost->max_host_blocked = 1; |
| 3794 | |
Lin Ming | f8fc75d | 2012-04-12 13:50:39 +0800 | [diff] [blame] | 3795 | rc = scsi_add_host_with_dma(ap->scsi_host, |
| 3796 | &ap->tdev, ap->host->dev); |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 3797 | if (rc) |
| 3798 | goto err_add; |
| 3799 | } |
| 3800 | |
| 3801 | return 0; |
| 3802 | |
| 3803 | err_add: |
| 3804 | scsi_host_put(host->ports[i]->scsi_host); |
| 3805 | err_alloc: |
| 3806 | while (--i >= 0) { |
| 3807 | struct Scsi_Host *shost = host->ports[i]->scsi_host; |
| 3808 | |
| 3809 | scsi_remove_host(shost); |
| 3810 | scsi_host_put(shost); |
| 3811 | } |
| 3812 | return rc; |
| 3813 | } |
| 3814 | |
Tejun Heo | 1ae4631 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3815 | void ata_scsi_scan_host(struct ata_port *ap, int sync) |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 3816 | { |
Tejun Heo | 1ae4631 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3817 | int tries = 5; |
| 3818 | struct ata_device *last_failed_dev = NULL; |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 3819 | struct ata_link *link; |
Tejun Heo | 1ae4631 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3820 | struct ata_device *dev; |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 3821 | |
Tejun Heo | 1ae4631 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3822 | repeat: |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3823 | ata_for_each_link(link, ap, EDGE) { |
| 3824 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 3825 | struct scsi_device *sdev; |
| 3826 | int channel = 0, id = 0; |
Jeff Garzik | 3f19ee8 | 2005-10-03 21:36:41 -0400 | [diff] [blame] | 3827 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3828 | if (dev->sdev) |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 3829 | continue; |
Tejun Heo | 3edebac | 2006-05-31 18:27:40 +0900 | [diff] [blame] | 3830 | |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 3831 | if (ata_is_host_link(link)) |
| 3832 | id = dev->devno; |
| 3833 | else |
| 3834 | channel = link->pmp; |
| 3835 | |
| 3836 | sdev = __scsi_add_device(ap->scsi_host, channel, id, 0, |
| 3837 | NULL); |
| 3838 | if (!IS_ERR(sdev)) { |
| 3839 | dev->sdev = sdev; |
| 3840 | scsi_device_put(sdev); |
Grant Grundler | 295124d | 2010-08-17 10:56:53 -0700 | [diff] [blame] | 3841 | } else { |
| 3842 | dev->sdev = NULL; |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 3843 | } |
Tejun Heo | 3edebac | 2006-05-31 18:27:40 +0900 | [diff] [blame] | 3844 | } |
Jeff Garzik | 3f19ee8 | 2005-10-03 21:36:41 -0400 | [diff] [blame] | 3845 | } |
Tejun Heo | 1ae4631 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3846 | |
| 3847 | /* If we scanned while EH was in progress or allocation |
| 3848 | * failure occurred, scan would have failed silently. Check |
| 3849 | * whether all devices are attached. |
| 3850 | */ |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3851 | ata_for_each_link(link, ap, EDGE) { |
| 3852 | ata_for_each_dev(dev, link, ENABLED) { |
| 3853 | if (!dev->sdev) |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 3854 | goto exit_loop; |
| 3855 | } |
Tejun Heo | 1ae4631 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3856 | } |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 3857 | exit_loop: |
| 3858 | if (!link) |
Tejun Heo | 1ae4631 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3859 | return; |
| 3860 | |
| 3861 | /* we're missing some SCSI devices */ |
| 3862 | if (sync) { |
| 3863 | /* If caller requested synchrnous scan && we've made |
| 3864 | * any progress, sleep briefly and repeat. |
| 3865 | */ |
| 3866 | if (dev != last_failed_dev) { |
| 3867 | msleep(100); |
| 3868 | last_failed_dev = dev; |
| 3869 | goto repeat; |
| 3870 | } |
| 3871 | |
| 3872 | /* We might be failing to detect boot device, give it |
| 3873 | * a few more chances. |
| 3874 | */ |
| 3875 | if (--tries) { |
| 3876 | msleep(100); |
| 3877 | goto repeat; |
| 3878 | } |
| 3879 | |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3880 | ata_port_err(ap, |
| 3881 | "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n"); |
Tejun Heo | 1ae4631 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3882 | } |
| 3883 | |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 3884 | queue_delayed_work(system_long_wq, &ap->hotplug_task, |
Tejun Heo | 1ae4631 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 3885 | round_jiffies_relative(HZ)); |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 3886 | } |
Tejun Heo | 0ea035a | 2006-05-31 18:28:01 +0900 | [diff] [blame] | 3887 | |
| 3888 | /** |
| 3889 | * ata_scsi_offline_dev - offline attached SCSI device |
| 3890 | * @dev: ATA device to offline attached SCSI device for |
| 3891 | * |
| 3892 | * This function is called from ata_eh_hotplug() and responsible |
| 3893 | * for taking the SCSI device attached to @dev offline. This |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 3894 | * function is called with host lock which protects dev->sdev |
Tejun Heo | 0ea035a | 2006-05-31 18:28:01 +0900 | [diff] [blame] | 3895 | * against clearing. |
| 3896 | * |
| 3897 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 3898 | * spin_lock_irqsave(host lock) |
Tejun Heo | 0ea035a | 2006-05-31 18:28:01 +0900 | [diff] [blame] | 3899 | * |
| 3900 | * RETURNS: |
| 3901 | * 1 if attached SCSI device exists, 0 otherwise. |
| 3902 | */ |
| 3903 | int ata_scsi_offline_dev(struct ata_device *dev) |
| 3904 | { |
| 3905 | if (dev->sdev) { |
| 3906 | scsi_device_set_state(dev->sdev, SDEV_OFFLINE); |
| 3907 | return 1; |
| 3908 | } |
| 3909 | return 0; |
| 3910 | } |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 3911 | |
| 3912 | /** |
| 3913 | * ata_scsi_remove_dev - remove attached SCSI device |
| 3914 | * @dev: ATA device to remove attached SCSI device for |
| 3915 | * |
| 3916 | * This function is called from ata_eh_scsi_hotplug() and |
| 3917 | * responsible for removing the SCSI device attached to @dev. |
| 3918 | * |
| 3919 | * LOCKING: |
| 3920 | * Kernel thread context (may sleep). |
| 3921 | */ |
| 3922 | static void ata_scsi_remove_dev(struct ata_device *dev) |
| 3923 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 3924 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 3925 | struct scsi_device *sdev; |
| 3926 | unsigned long flags; |
| 3927 | |
| 3928 | /* Alas, we need to grab scan_mutex to ensure SCSI device |
| 3929 | * state doesn't change underneath us and thus |
| 3930 | * scsi_device_get() always succeeds. The mutex locking can |
| 3931 | * be removed if there is __scsi_device_get() interface which |
| 3932 | * increments reference counts regardless of device state. |
| 3933 | */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 3934 | mutex_lock(&ap->scsi_host->scan_mutex); |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 3935 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 3936 | |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 3937 | /* clearing dev->sdev is protected by host lock */ |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 3938 | sdev = dev->sdev; |
| 3939 | dev->sdev = NULL; |
| 3940 | |
| 3941 | if (sdev) { |
| 3942 | /* If user initiated unplug races with us, sdev can go |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 3943 | * away underneath us after the host lock and |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 3944 | * scan_mutex are released. Hold onto it. |
| 3945 | */ |
| 3946 | if (scsi_device_get(sdev) == 0) { |
| 3947 | /* The following ensures the attached sdev is |
| 3948 | * offline on return from ata_scsi_offline_dev() |
| 3949 | * regardless it wins or loses the race |
| 3950 | * against this function. |
| 3951 | */ |
| 3952 | scsi_device_set_state(sdev, SDEV_OFFLINE); |
| 3953 | } else { |
| 3954 | WARN_ON(1); |
| 3955 | sdev = NULL; |
| 3956 | } |
| 3957 | } |
| 3958 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 3959 | spin_unlock_irqrestore(ap->lock, flags); |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 3960 | mutex_unlock(&ap->scsi_host->scan_mutex); |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 3961 | |
| 3962 | if (sdev) { |
Joe Perches | a9a79df | 2011-04-15 15:51:59 -0700 | [diff] [blame] | 3963 | ata_dev_info(dev, "detaching (SCSI %s)\n", |
| 3964 | dev_name(&sdev->sdev_gendev)); |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 3965 | |
| 3966 | scsi_remove_device(sdev); |
| 3967 | scsi_device_put(sdev); |
| 3968 | } |
| 3969 | } |
| 3970 | |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 3971 | static void ata_scsi_handle_link_detach(struct ata_link *link) |
| 3972 | { |
| 3973 | struct ata_port *ap = link->ap; |
| 3974 | struct ata_device *dev; |
| 3975 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 3976 | ata_for_each_dev(dev, link, ALL) { |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 3977 | unsigned long flags; |
| 3978 | |
| 3979 | if (!(dev->flags & ATA_DFLAG_DETACHED)) |
| 3980 | continue; |
| 3981 | |
| 3982 | spin_lock_irqsave(ap->lock, flags); |
| 3983 | dev->flags &= ~ATA_DFLAG_DETACHED; |
| 3984 | spin_unlock_irqrestore(ap->lock, flags); |
| 3985 | |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 3986 | if (zpodd_dev_enabled(dev)) |
| 3987 | zpodd_exit(dev); |
| 3988 | |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 3989 | ata_scsi_remove_dev(dev); |
| 3990 | } |
| 3991 | } |
| 3992 | |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 3993 | /** |
Kristen Carlson Accardi | 2f29496 | 2007-08-15 04:11:25 -0400 | [diff] [blame] | 3994 | * ata_scsi_media_change_notify - send media change event |
Randy Dunlap | c5d0e6a | 2007-10-15 17:29:46 -0700 | [diff] [blame] | 3995 | * @dev: Pointer to the disk device with media change event |
Kristen Carlson Accardi | 2f29496 | 2007-08-15 04:11:25 -0400 | [diff] [blame] | 3996 | * |
| 3997 | * Tell the block layer to send a media change notification |
| 3998 | * event. |
| 3999 | * |
| 4000 | * LOCKING: |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 4001 | * spin_lock_irqsave(host lock) |
Kristen Carlson Accardi | 2f29496 | 2007-08-15 04:11:25 -0400 | [diff] [blame] | 4002 | */ |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 4003 | void ata_scsi_media_change_notify(struct ata_device *dev) |
Kristen Carlson Accardi | 2f29496 | 2007-08-15 04:11:25 -0400 | [diff] [blame] | 4004 | { |
Tejun Heo | 854c73a | 2007-09-23 13:14:11 +0900 | [diff] [blame] | 4005 | if (dev->sdev) |
Jeff Garzik | f26792d | 2007-10-29 17:18:39 -0400 | [diff] [blame] | 4006 | sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE, |
| 4007 | GFP_ATOMIC); |
Kristen Carlson Accardi | 2f29496 | 2007-08-15 04:11:25 -0400 | [diff] [blame] | 4008 | } |
Kristen Carlson Accardi | 2f29496 | 2007-08-15 04:11:25 -0400 | [diff] [blame] | 4009 | |
| 4010 | /** |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 4011 | * ata_scsi_hotplug - SCSI part of hotplug |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 4012 | * @work: Pointer to ATA port to perform SCSI hotplug on |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 4013 | * |
| 4014 | * Perform SCSI part of hotplug. It's executed from a separate |
| 4015 | * workqueue after EH completes. This is necessary because SCSI |
| 4016 | * hot plugging requires working EH and hot unplugging is |
| 4017 | * synchronized with hot plugging with a mutex. |
| 4018 | * |
| 4019 | * LOCKING: |
| 4020 | * Kernel thread context (may sleep). |
| 4021 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 4022 | void ata_scsi_hotplug(struct work_struct *work) |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 4023 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 4024 | struct ata_port *ap = |
| 4025 | container_of(work, struct ata_port, hotplug_task.work); |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4026 | int i; |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 4027 | |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 4028 | if (ap->pflags & ATA_PFLAG_UNLOADING) { |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 4029 | DPRINTK("ENTER/EXIT - unloading\n"); |
| 4030 | return; |
| 4031 | } |
| 4032 | |
Tejun Heo | 85fbd72 | 2013-12-18 07:07:32 -0500 | [diff] [blame] | 4033 | /* |
| 4034 | * XXX - UGLY HACK |
| 4035 | * |
| 4036 | * The block layer suspend/resume path is fundamentally broken due |
| 4037 | * to freezable kthreads and workqueue and may deadlock if a block |
| 4038 | * device gets removed while resume is in progress. I don't know |
| 4039 | * what the solution is short of removing freezable kthreads and |
| 4040 | * workqueues altogether. |
| 4041 | * |
| 4042 | * The following is an ugly hack to avoid kicking off device |
| 4043 | * removal while freezer is active. This is a joke but does avoid |
| 4044 | * this particular deadlock scenario. |
| 4045 | * |
| 4046 | * https://bugzilla.kernel.org/show_bug.cgi?id=62801 |
| 4047 | * http://marc.info/?l=linux-kernel&m=138695698516487 |
| 4048 | */ |
| 4049 | #ifdef CONFIG_FREEZER |
| 4050 | while (pm_freezing) |
| 4051 | msleep(10); |
| 4052 | #endif |
| 4053 | |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 4054 | DPRINTK("ENTER\n"); |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 4055 | mutex_lock(&ap->scsi_scan_mutex); |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 4056 | |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4057 | /* Unplug detached devices. We cannot use link iterator here |
| 4058 | * because PMP links have to be scanned even if PMP is |
| 4059 | * currently not attached. Iterate manually. |
| 4060 | */ |
| 4061 | ata_scsi_handle_link_detach(&ap->link); |
| 4062 | if (ap->pmp_link) |
| 4063 | for (i = 0; i < SATA_PMP_MAX_PORTS; i++) |
| 4064 | ata_scsi_handle_link_detach(&ap->pmp_link[i]); |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 4065 | |
| 4066 | /* scan for new ones */ |
Tejun Heo | 1ae4631 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 4067 | ata_scsi_scan_host(ap, 0); |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 4068 | |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 4069 | mutex_unlock(&ap->scsi_scan_mutex); |
Tejun Heo | 580b2102 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 4070 | DPRINTK("EXIT\n"); |
| 4071 | } |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 4072 | |
| 4073 | /** |
| 4074 | * ata_scsi_user_scan - indication for user-initiated bus scan |
| 4075 | * @shost: SCSI host to scan |
| 4076 | * @channel: Channel to scan |
| 4077 | * @id: ID to scan |
| 4078 | * @lun: LUN to scan |
| 4079 | * |
| 4080 | * This function is called when user explicitly requests bus |
| 4081 | * scan. Set probe pending flag and invoke EH. |
| 4082 | * |
| 4083 | * LOCKING: |
| 4084 | * SCSI layer (we don't care) |
| 4085 | * |
| 4086 | * RETURNS: |
| 4087 | * Zero. |
| 4088 | */ |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 4089 | int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel, |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 4090 | unsigned int id, u64 lun) |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 4091 | { |
| 4092 | struct ata_port *ap = ata_shost_to_port(shost); |
| 4093 | unsigned long flags; |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4094 | int devno, rc = 0; |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 4095 | |
| 4096 | if (!ap->ops->error_handler) |
| 4097 | return -EOPNOTSUPP; |
| 4098 | |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4099 | if (lun != SCAN_WILD_CARD && lun) |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 4100 | return -EINVAL; |
| 4101 | |
Tejun Heo | 071f44b | 2008-04-07 22:47:22 +0900 | [diff] [blame] | 4102 | if (!sata_pmp_attached(ap)) { |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4103 | if (channel != SCAN_WILD_CARD && channel) |
| 4104 | return -EINVAL; |
| 4105 | devno = id; |
| 4106 | } else { |
| 4107 | if (id != SCAN_WILD_CARD && id) |
| 4108 | return -EINVAL; |
| 4109 | devno = channel; |
| 4110 | } |
| 4111 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 4112 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 4113 | |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4114 | if (devno == SCAN_WILD_CARD) { |
| 4115 | struct ata_link *link; |
| 4116 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 4117 | ata_for_each_link(link, ap, EDGE) { |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4118 | struct ata_eh_info *ehi = &link->eh_info; |
Tejun Heo | b558edd | 2008-01-24 00:05:14 +0900 | [diff] [blame] | 4119 | ehi->probe_mask |= ATA_ALL_DEVICES; |
Tejun Heo | cf48062 | 2008-01-24 00:05:14 +0900 | [diff] [blame] | 4120 | ehi->action |= ATA_EH_RESET; |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4121 | } |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 4122 | } else { |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4123 | struct ata_device *dev = ata_find_dev(ap, devno); |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 4124 | |
| 4125 | if (dev) { |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4126 | struct ata_eh_info *ehi = &dev->link->eh_info; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4127 | ehi->probe_mask |= 1 << dev->devno; |
Tejun Heo | cf48062 | 2008-01-24 00:05:14 +0900 | [diff] [blame] | 4128 | ehi->action |= ATA_EH_RESET; |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 4129 | } else |
| 4130 | rc = -EINVAL; |
| 4131 | } |
| 4132 | |
Tejun Heo | 309afcb | 2006-09-30 18:07:17 +0900 | [diff] [blame] | 4133 | if (rc == 0) { |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 4134 | ata_port_schedule_eh(ap); |
Tejun Heo | 309afcb | 2006-09-30 18:07:17 +0900 | [diff] [blame] | 4135 | spin_unlock_irqrestore(ap->lock, flags); |
| 4136 | ata_port_wait_eh(ap); |
| 4137 | } else |
| 4138 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 83c47bc | 2006-05-31 18:28:07 +0900 | [diff] [blame] | 4139 | |
| 4140 | return rc; |
| 4141 | } |
zhao, forrest | 3057ac3 | 2006-06-12 12:01:34 +0800 | [diff] [blame] | 4142 | |
| 4143 | /** |
Tejun Heo | d017126 | 2006-06-12 22:51:14 +0900 | [diff] [blame] | 4144 | * ata_scsi_dev_rescan - initiate scsi_rescan_device() |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 4145 | * @work: Pointer to ATA port to perform scsi_rescan_device() |
zhao, forrest | 3057ac3 | 2006-06-12 12:01:34 +0800 | [diff] [blame] | 4146 | * |
Tejun Heo | d017126 | 2006-06-12 22:51:14 +0900 | [diff] [blame] | 4147 | * After ATA pass thru (SAT) commands are executed successfully, |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 4148 | * libata need to propagate the changes to SCSI layer. |
zhao, forrest | 3057ac3 | 2006-06-12 12:01:34 +0800 | [diff] [blame] | 4149 | * |
Tejun Heo | d017126 | 2006-06-12 22:51:14 +0900 | [diff] [blame] | 4150 | * LOCKING: |
| 4151 | * Kernel thread context (may sleep). |
zhao, forrest | 3057ac3 | 2006-06-12 12:01:34 +0800 | [diff] [blame] | 4152 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 4153 | void ata_scsi_dev_rescan(struct work_struct *work) |
zhao, forrest | 3057ac3 | 2006-06-12 12:01:34 +0800 | [diff] [blame] | 4154 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 4155 | struct ata_port *ap = |
| 4156 | container_of(work, struct ata_port, scsi_rescan_task); |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4157 | struct ata_link *link; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 4158 | struct ata_device *dev; |
Tejun Heo | f84e7e4 | 2006-11-22 11:21:31 +0900 | [diff] [blame] | 4159 | unsigned long flags; |
zhao, forrest | 3057ac3 | 2006-06-12 12:01:34 +0800 | [diff] [blame] | 4160 | |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 4161 | mutex_lock(&ap->scsi_scan_mutex); |
Tejun Heo | f84e7e4 | 2006-11-22 11:21:31 +0900 | [diff] [blame] | 4162 | spin_lock_irqsave(ap->lock, flags); |
zhao, forrest | 3057ac3 | 2006-06-12 12:01:34 +0800 | [diff] [blame] | 4163 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 4164 | ata_for_each_link(link, ap, EDGE) { |
| 4165 | ata_for_each_dev(dev, link, ENABLED) { |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4166 | struct scsi_device *sdev = dev->sdev; |
Tejun Heo | f84e7e4 | 2006-11-22 11:21:31 +0900 | [diff] [blame] | 4167 | |
Tejun Heo | 1eca436 | 2008-11-03 20:03:17 +0900 | [diff] [blame] | 4168 | if (!sdev) |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4169 | continue; |
| 4170 | if (scsi_device_get(sdev)) |
| 4171 | continue; |
Tejun Heo | f84e7e4 | 2006-11-22 11:21:31 +0900 | [diff] [blame] | 4172 | |
Tejun Heo | 41bda9c | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 4173 | spin_unlock_irqrestore(ap->lock, flags); |
| 4174 | scsi_rescan_device(&(sdev->sdev_gendev)); |
| 4175 | scsi_device_put(sdev); |
| 4176 | spin_lock_irqsave(ap->lock, flags); |
| 4177 | } |
zhao, forrest | 3057ac3 | 2006-06-12 12:01:34 +0800 | [diff] [blame] | 4178 | } |
Tejun Heo | f84e7e4 | 2006-11-22 11:21:31 +0900 | [diff] [blame] | 4179 | |
| 4180 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | ad72cf9 | 2010-07-02 10:03:52 +0200 | [diff] [blame] | 4181 | mutex_unlock(&ap->scsi_scan_mutex); |
zhao, forrest | 3057ac3 | 2006-06-12 12:01:34 +0800 | [diff] [blame] | 4182 | } |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4183 | |
| 4184 | /** |
| 4185 | * ata_sas_port_alloc - Allocate port for a SAS attached SATA device |
Jeff Garzik | 4f93137 | 2006-09-29 05:07:25 -0400 | [diff] [blame] | 4186 | * @host: ATA host container for all SAS ports |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4187 | * @port_info: Information from low-level host driver |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4188 | * @shost: SCSI host that the scsi device is attached to |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4189 | * |
| 4190 | * LOCKING: |
| 4191 | * PCI/etc. bus probe sem. |
| 4192 | * |
| 4193 | * RETURNS: |
| 4194 | * ata_port pointer on success / NULL on failure. |
| 4195 | */ |
| 4196 | |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4197 | struct ata_port *ata_sas_port_alloc(struct ata_host *host, |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4198 | struct ata_port_info *port_info, |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 4199 | struct Scsi_Host *shost) |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4200 | { |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 4201 | struct ata_port *ap; |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4202 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 4203 | ap = ata_port_alloc(host); |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4204 | if (!ap) |
| 4205 | return NULL; |
| 4206 | |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 4207 | ap->port_no = 0; |
James Bottomley | a29b5da | 2011-01-23 08:30:00 -0600 | [diff] [blame] | 4208 | ap->lock = &host->lock; |
Tejun Heo | f318719 | 2007-04-17 23:44:07 +0900 | [diff] [blame] | 4209 | ap->pio_mask = port_info->pio_mask; |
| 4210 | ap->mwdma_mask = port_info->mwdma_mask; |
| 4211 | ap->udma_mask = port_info->udma_mask; |
| 4212 | ap->flags |= port_info->flags; |
| 4213 | ap->ops = port_info->port_ops; |
| 4214 | ap->cbl = ATA_CBL_SATA; |
| 4215 | |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4216 | return ap; |
| 4217 | } |
| 4218 | EXPORT_SYMBOL_GPL(ata_sas_port_alloc); |
| 4219 | |
| 4220 | /** |
| 4221 | * ata_sas_port_start - Set port up for dma. |
| 4222 | * @ap: Port to initialize |
| 4223 | * |
| 4224 | * Called just after data structures for each port are |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4225 | * initialized. |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4226 | * |
| 4227 | * May be used as the port_start() entry in ata_port_operations. |
| 4228 | * |
| 4229 | * LOCKING: |
| 4230 | * Inherited from caller. |
| 4231 | */ |
| 4232 | int ata_sas_port_start(struct ata_port *ap) |
| 4233 | { |
Nishanth Aravamudan | 3f1e046 | 2011-06-16 08:28:36 -0700 | [diff] [blame] | 4234 | /* |
| 4235 | * the port is marked as frozen at allocation time, but if we don't |
| 4236 | * have new eh, we won't thaw it |
| 4237 | */ |
| 4238 | if (!ap->ops->error_handler) |
| 4239 | ap->pflags &= ~ATA_PFLAG_FROZEN; |
James Bottomley | dde2020 | 2008-02-19 11:36:56 +0100 | [diff] [blame] | 4240 | return 0; |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4241 | } |
| 4242 | EXPORT_SYMBOL_GPL(ata_sas_port_start); |
| 4243 | |
| 4244 | /** |
| 4245 | * ata_port_stop - Undo ata_sas_port_start() |
| 4246 | * @ap: Port to shut down |
| 4247 | * |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4248 | * May be used as the port_stop() entry in ata_port_operations. |
| 4249 | * |
| 4250 | * LOCKING: |
| 4251 | * Inherited from caller. |
| 4252 | */ |
| 4253 | |
| 4254 | void ata_sas_port_stop(struct ata_port *ap) |
| 4255 | { |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4256 | } |
| 4257 | EXPORT_SYMBOL_GPL(ata_sas_port_stop); |
| 4258 | |
Dan Williams | b202445 | 2012-03-21 21:09:07 -0700 | [diff] [blame] | 4259 | /** |
| 4260 | * ata_sas_async_probe - simply schedule probing and return |
| 4261 | * @ap: Port to probe |
| 4262 | * |
| 4263 | * For batch scheduling of probe for sas attached ata devices, assumes |
| 4264 | * the port has already been through ata_sas_port_init() |
| 4265 | */ |
| 4266 | void ata_sas_async_probe(struct ata_port *ap) |
Dan Williams | 9508a66 | 2012-01-18 20:47:01 -0800 | [diff] [blame] | 4267 | { |
Dan Williams | b202445 | 2012-03-21 21:09:07 -0700 | [diff] [blame] | 4268 | __ata_port_probe(ap); |
Dan Williams | 9508a66 | 2012-01-18 20:47:01 -0800 | [diff] [blame] | 4269 | } |
Dan Williams | b202445 | 2012-03-21 21:09:07 -0700 | [diff] [blame] | 4270 | EXPORT_SYMBOL_GPL(ata_sas_async_probe); |
| 4271 | |
| 4272 | int ata_sas_sync_probe(struct ata_port *ap) |
| 4273 | { |
| 4274 | return ata_port_probe(ap); |
| 4275 | } |
| 4276 | EXPORT_SYMBOL_GPL(ata_sas_sync_probe); |
| 4277 | |
Dan Williams | 9508a66 | 2012-01-18 20:47:01 -0800 | [diff] [blame] | 4278 | |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4279 | /** |
| 4280 | * ata_sas_port_init - Initialize a SATA device |
| 4281 | * @ap: SATA port to initialize |
| 4282 | * |
| 4283 | * LOCKING: |
| 4284 | * PCI/etc. bus probe sem. |
| 4285 | * |
| 4286 | * RETURNS: |
| 4287 | * Zero on success, non-zero on error. |
| 4288 | */ |
| 4289 | |
| 4290 | int ata_sas_port_init(struct ata_port *ap) |
| 4291 | { |
| 4292 | int rc = ap->ops->port_start(ap); |
| 4293 | |
Dan Williams | b202445 | 2012-03-21 21:09:07 -0700 | [diff] [blame] | 4294 | if (rc) |
| 4295 | return rc; |
| 4296 | ap->print_id = atomic_inc_return(&ata_print_id); |
| 4297 | return 0; |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4298 | } |
| 4299 | EXPORT_SYMBOL_GPL(ata_sas_port_init); |
| 4300 | |
| 4301 | /** |
| 4302 | * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc |
| 4303 | * @ap: SATA port to destroy |
| 4304 | * |
| 4305 | */ |
| 4306 | |
| 4307 | void ata_sas_port_destroy(struct ata_port *ap) |
| 4308 | { |
Tejun Heo | f0d36ef | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 4309 | if (ap->ops->port_stop) |
| 4310 | ap->ops->port_stop(ap); |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4311 | kfree(ap); |
| 4312 | } |
| 4313 | EXPORT_SYMBOL_GPL(ata_sas_port_destroy); |
| 4314 | |
| 4315 | /** |
| 4316 | * ata_sas_slave_configure - Default slave_config routine for libata devices |
| 4317 | * @sdev: SCSI device to configure |
| 4318 | * @ap: ATA port to which SCSI device is attached |
| 4319 | * |
| 4320 | * RETURNS: |
| 4321 | * Zero. |
| 4322 | */ |
| 4323 | |
| 4324 | int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap) |
| 4325 | { |
| 4326 | ata_scsi_sdev_config(sdev); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 4327 | ata_scsi_dev_config(sdev, ap->link.device); |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4328 | return 0; |
| 4329 | } |
| 4330 | EXPORT_SYMBOL_GPL(ata_sas_slave_configure); |
| 4331 | |
| 4332 | /** |
| 4333 | * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device |
| 4334 | * @cmd: SCSI command to be sent |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4335 | * @ap: ATA port to which the command is being sent |
| 4336 | * |
| 4337 | * RETURNS: |
Brian King | 08475a1 | 2006-11-20 13:51:56 -0600 | [diff] [blame] | 4338 | * Return value from __ata_scsi_queuecmd() if @cmd can be queued, |
| 4339 | * 0 otherwise. |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4340 | */ |
| 4341 | |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 4342 | int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap) |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4343 | { |
Brian King | 08475a1 | 2006-11-20 13:51:56 -0600 | [diff] [blame] | 4344 | int rc = 0; |
| 4345 | |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4346 | ata_scsi_dump_cdb(ap, cmd); |
| 4347 | |
Tejun Heo | 2486fa5 | 2008-07-31 07:52:40 +0900 | [diff] [blame] | 4348 | if (likely(ata_dev_enabled(ap->link.device))) |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 4349 | rc = __ata_scsi_queuecmd(cmd, ap->link.device); |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4350 | else { |
| 4351 | cmd->result = (DID_BAD_TARGET << 16); |
Jeff Garzik | b27dcfb | 2010-11-17 22:56:48 -0500 | [diff] [blame] | 4352 | cmd->scsi_done(cmd); |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4353 | } |
Brian King | 08475a1 | 2006-11-20 13:51:56 -0600 | [diff] [blame] | 4354 | return rc; |
Brian King | 8028916 | 2006-08-07 14:27:31 -0500 | [diff] [blame] | 4355 | } |
| 4356 | EXPORT_SYMBOL_GPL(ata_sas_queuecmd); |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4357 | |
| 4358 | int ata_sas_allocate_tag(struct ata_port *ap) |
| 4359 | { |
| 4360 | unsigned int max_queue = ap->host->n_tags; |
| 4361 | unsigned int i, tag; |
| 4362 | |
| 4363 | for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) { |
Tejun Heo | 3a02824 | 2015-03-24 14:14:18 -0400 | [diff] [blame] | 4364 | tag = tag < max_queue ? tag : 0; |
Shaohua Li | 98bd4be | 2015-01-23 19:52:07 -0800 | [diff] [blame] | 4365 | |
| 4366 | /* the last tag is reserved for internal command. */ |
| 4367 | if (tag == ATA_TAG_INTERNAL) |
| 4368 | continue; |
| 4369 | |
| 4370 | if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) { |
| 4371 | ap->sas_last_tag = tag; |
| 4372 | return tag; |
| 4373 | } |
| 4374 | } |
| 4375 | return -1; |
| 4376 | } |
| 4377 | |
| 4378 | void ata_sas_free_tag(unsigned int tag, struct ata_port *ap) |
| 4379 | { |
| 4380 | clear_bit(tag, &ap->sas_tag_allocated); |
| 4381 | } |