blob: 49c09d876358f788233556ef1c001a8ba2f50d0e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-scsi.c - helper library for ATA
3 *
Tejun Heo8c3d3d42013-05-14 11:09:50 -07004 * Maintained by: Tejun Heo <tj@kernel.org>
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04005 * 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 Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/kernel.h>
38#include <linux/blkdev.h>
39#include <linux/spinlock.h>
Paul Gortmaker38789fd2011-07-17 15:33:58 -040040#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <scsi/scsi_host.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020043#include <scsi/scsi_cmnd.h>
Mike Christie85837eb2005-11-11 16:38:53 -060044#include <scsi/scsi_eh.h>
Jeff Garzik005a5a02005-10-30 23:31:48 -050045#include <scsi/scsi_device.h>
Tejun Heoa6e6ce82006-05-15 21:03:48 +090046#include <scsi/scsi_tcq.h>
Tejun Heo30afc842006-03-18 18:40:14 +090047#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/libata.h>
Jeff Garzikb0955182005-05-12 15:45:22 -040049#include <linux/hdreg.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040050#include <linux/uaccess.h>
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +010051#include <linux/suspend.h>
Christoph Hellwig18f0f972009-11-17 10:00:47 -050052#include <asm/unaligned.h>
Adam Manzanares8e061782016-10-17 11:27:29 -070053#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include "libata.h"
Gwendal Grignoud9027472010-05-25 12:31:38 -070056#include "libata-transport.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Tejun Heo87340e92008-04-28 17:48:51 +090058#define ATA_SCSI_RBUF_SIZE 4096
59
60static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
61static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
Jeff Garzikb0955182005-05-12 15:45:22 -040062
Tejun Heoad706992006-12-17 10:45:57 +090063typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
Tejun Heoab5b3a52006-05-31 18:27:34 +090064
Jeff Garzik2dcb4072007-10-19 06:42:56 -040065static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090066 const struct scsi_device *scsidev);
Jeff Garzik2dcb4072007-10-19 06:42:56 -040067static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090068 const struct scsi_device *scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040070#define RW_RECOVERY_MPAGE 0x1
71#define RW_RECOVERY_MPAGE_LEN 12
72#define CACHE_MPAGE 0x8
73#define CACHE_MPAGE_LEN 20
74#define CONTROL_MPAGE 0xa
75#define CONTROL_MPAGE_LEN 12
76#define ALL_MPAGES 0x3f
77#define ALL_SUB_MPAGES 0xff
78
79
Jeff Garzik24f75682007-09-21 07:54:49 -040080static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040081 RW_RECOVERY_MPAGE,
82 RW_RECOVERY_MPAGE_LEN - 2,
Jeff Garzik24f75682007-09-21 07:54:49 -040083 (1 << 7), /* AWRE */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040084 0, /* read retry count */
85 0, 0, 0, 0,
86 0, /* write retry count */
87 0, 0, 0
88};
89
90static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
91 CACHE_MPAGE,
92 CACHE_MPAGE_LEN - 2,
93 0, /* contains WCE, needs to be 0 for logic */
94 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, /* contains DRA, needs to be 0 for logic */
96 0, 0, 0, 0, 0, 0, 0
97};
98
99static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
100 CONTROL_MPAGE,
101 CONTROL_MPAGE_LEN - 2,
102 2, /* DSENSE=0, GLTSD=1 */
103 0, /* [QAM+QERR may be 1, see 05-359r1] */
104 0, 0, 0, 0, 0xff, 0xff,
105 0, 30 /* extended self test time, see 05-359r1 */
106};
107
Tejun Heoc93b2632010-09-01 17:50:04 +0200108static const char *ata_lpm_policy_names[] = {
109 [ATA_LPM_UNKNOWN] = "max_performance",
110 [ATA_LPM_MAX_POWER] = "max_performance",
111 [ATA_LPM_MED_POWER] = "medium_power",
112 [ATA_LPM_MIN_POWER] = "min_power",
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400113};
114
Tejun Heoecd75ad2014-01-16 09:47:17 -0500115static ssize_t ata_scsi_lpm_store(struct device *device,
Tejun Heoc93b2632010-09-01 17:50:04 +0200116 struct device_attribute *attr,
117 const char *buf, size_t count)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400118{
Tejun Heoecd75ad2014-01-16 09:47:17 -0500119 struct Scsi_Host *shost = class_to_shost(device);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400120 struct ata_port *ap = ata_shost_to_port(shost);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500121 struct ata_link *link;
122 struct ata_device *dev;
Tejun Heoc93b2632010-09-01 17:50:04 +0200123 enum ata_lpm_policy policy;
Tejun Heo6b7ae952010-09-01 17:50:06 +0200124 unsigned long flags;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400125
Tejun Heoc93b2632010-09-01 17:50:04 +0200126 /* UNKNOWN is internal state, iterate from MAX_POWER */
127 for (policy = ATA_LPM_MAX_POWER;
128 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
129 const char *name = ata_lpm_policy_names[policy];
130
131 if (strncmp(name, buf, strlen(name)) == 0)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400132 break;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400133 }
Tejun Heoc93b2632010-09-01 17:50:04 +0200134 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400135 return -EINVAL;
136
Tejun Heo6b7ae952010-09-01 17:50:06 +0200137 spin_lock_irqsave(ap->lock, flags);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500138
139 ata_for_each_link(link, ap, EDGE) {
140 ata_for_each_dev(dev, &ap->link, ENABLED) {
141 if (dev->horkage & ATA_HORKAGE_NOLPM) {
142 count = -EOPNOTSUPP;
143 goto out_unlock;
144 }
145 }
146 }
147
Tejun Heo6b7ae952010-09-01 17:50:06 +0200148 ap->target_lpm_policy = policy;
149 ata_port_schedule_eh(ap);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500150out_unlock:
Tejun Heo6b7ae952010-09-01 17:50:06 +0200151 spin_unlock_irqrestore(ap->lock, flags);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400152 return count;
153}
154
Tejun Heoc93b2632010-09-01 17:50:04 +0200155static ssize_t ata_scsi_lpm_show(struct device *dev,
156 struct device_attribute *attr, char *buf)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400157{
Tony Jonesee959b02008-02-22 00:13:36 +0100158 struct Scsi_Host *shost = class_to_shost(dev);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400159 struct ata_port *ap = ata_shost_to_port(shost);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400160
Tejun Heo6b7ae952010-09-01 17:50:06 +0200161 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400162 return -EINVAL;
163
Tejun Heoc93b2632010-09-01 17:50:04 +0200164 return snprintf(buf, PAGE_SIZE, "%s\n",
Tejun Heo6b7ae952010-09-01 17:50:06 +0200165 ata_lpm_policy_names[ap->target_lpm_policy]);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400166}
Tony Jonesee959b02008-02-22 00:13:36 +0100167DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
Tejun Heoc93b2632010-09-01 17:50:04 +0200168 ata_scsi_lpm_show, ata_scsi_lpm_store);
Tony Jonesee959b02008-02-22 00:13:36 +0100169EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400170
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200171static ssize_t ata_scsi_park_show(struct device *device,
172 struct device_attribute *attr, char *buf)
173{
174 struct scsi_device *sdev = to_scsi_device(device);
175 struct ata_port *ap;
176 struct ata_link *link;
177 struct ata_device *dev;
Tejun Heo3948b6f2016-02-18 11:50:37 -0500178 unsigned long now;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200179 unsigned int uninitialized_var(msecs);
180 int rc = 0;
181
182 ap = ata_shost_to_port(sdev->host);
183
Tejun Heo3948b6f2016-02-18 11:50:37 -0500184 spin_lock_irq(ap->lock);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200185 dev = ata_scsi_find_dev(ap, sdev);
186 if (!dev) {
187 rc = -ENODEV;
188 goto unlock;
189 }
190 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
191 rc = -EOPNOTSUPP;
192 goto unlock;
193 }
194
195 link = dev->link;
Elias Oltmannsa4641892008-11-03 19:01:08 +0900196 now = jiffies;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200197 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
198 link->eh_context.unloaded_mask & (1 << dev->devno) &&
Elias Oltmannsa4641892008-11-03 19:01:08 +0900199 time_after(dev->unpark_deadline, now))
200 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200201 else
202 msecs = 0;
203
204unlock:
205 spin_unlock_irq(ap->lock);
206
207 return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
208}
209
210static ssize_t ata_scsi_park_store(struct device *device,
211 struct device_attribute *attr,
212 const char *buf, size_t len)
213{
214 struct scsi_device *sdev = to_scsi_device(device);
215 struct ata_port *ap;
216 struct ata_device *dev;
217 long int input;
218 unsigned long flags;
219 int rc;
220
Jingoo Han42b9ab72013-07-19 15:56:41 +0900221 rc = kstrtol(buf, 10, &input);
222 if (rc)
223 return rc;
224 if (input < -2)
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200225 return -EINVAL;
226 if (input > ATA_TMOUT_MAX_PARK) {
227 rc = -EOVERFLOW;
228 input = ATA_TMOUT_MAX_PARK;
229 }
230
231 ap = ata_shost_to_port(sdev->host);
232
233 spin_lock_irqsave(ap->lock, flags);
234 dev = ata_scsi_find_dev(ap, sdev);
235 if (unlikely(!dev)) {
236 rc = -ENODEV;
237 goto unlock;
238 }
Hannes Reinecke9162c652014-11-05 13:08:21 +0100239 if (dev->class != ATA_DEV_ATA &&
240 dev->class != ATA_DEV_ZAC) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200241 rc = -EOPNOTSUPP;
242 goto unlock;
243 }
244
245 if (input >= 0) {
246 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
247 rc = -EOPNOTSUPP;
248 goto unlock;
249 }
250
251 dev->unpark_deadline = ata_deadline(jiffies, input);
252 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
253 ata_port_schedule_eh(ap);
254 complete(&ap->park_req_pending);
255 } else {
256 switch (input) {
257 case -1:
258 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
259 break;
260 case -2:
261 dev->flags |= ATA_DFLAG_NO_UNLOAD;
262 break;
263 }
264 }
265unlock:
266 spin_unlock_irqrestore(ap->lock, flags);
267
268 return rc ? rc : len;
269}
270DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
271 ata_scsi_park_show, ata_scsi_park_store);
272EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
273
Adam Manzanares84f95242016-10-17 11:27:30 -0700274static ssize_t ata_ncq_prio_enable_show(struct device *device,
275 struct device_attribute *attr, char *buf)
276{
277 struct scsi_device *sdev = to_scsi_device(device);
278 struct ata_port *ap;
279 struct ata_device *dev;
280 bool ncq_prio_enable;
281 int rc = 0;
282
283 ap = ata_shost_to_port(sdev->host);
284
285 spin_lock_irq(ap->lock);
286 dev = ata_scsi_find_dev(ap, sdev);
287 if (!dev) {
288 rc = -ENODEV;
289 goto unlock;
290 }
291
292 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
293
294unlock:
295 spin_unlock_irq(ap->lock);
296
297 return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable);
298}
299
300static ssize_t ata_ncq_prio_enable_store(struct device *device,
301 struct device_attribute *attr,
302 const char *buf, size_t len)
303{
304 struct scsi_device *sdev = to_scsi_device(device);
305 struct ata_port *ap;
306 struct ata_device *dev;
307 long int input;
308 unsigned long flags;
309 int rc;
310
311 rc = kstrtol(buf, 10, &input);
312 if (rc)
313 return rc;
314 if ((input < 0) || (input > 1))
315 return -EINVAL;
316
317 ap = ata_shost_to_port(sdev->host);
318
319 spin_lock_irqsave(ap->lock, flags);
320 dev = ata_scsi_find_dev(ap, sdev);
321 if (unlikely(!dev)) {
322 rc = -ENODEV;
323 goto unlock;
324 }
325
Adam Manzanares4e647d92016-10-19 20:40:19 -0700326 if (input) {
327 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
328 rc = -EOPNOTSUPP;
329 goto unlock;
330 }
331
Adam Manzanares84f95242016-10-17 11:27:30 -0700332 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
Adam Manzanares4e647d92016-10-19 20:40:19 -0700333 } else {
Adam Manzanares84f95242016-10-17 11:27:30 -0700334 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
Adam Manzanares4e647d92016-10-19 20:40:19 -0700335 }
Adam Manzanares84f95242016-10-17 11:27:30 -0700336
337unlock:
338 spin_unlock_irqrestore(ap->lock, flags);
339
340 return rc ? rc : len;
341}
342
343DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
344 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
345EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
346
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200347void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
348 u8 sk, u8 asc, u8 ascq)
Tejun Heof0761be2008-04-28 17:16:52 +0900349{
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200350 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
351
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +0200352 if (!cmd)
353 return;
354
Tejun Heof0761be2008-04-28 17:16:52 +0900355 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
356
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200357 scsi_build_sense_buffer(d_sense, cmd->sense_buffer, sk, asc, ascq);
Tejun Heof0761be2008-04-28 17:16:52 +0900358}
359
Hannes Reinecke492bf622016-04-04 11:43:59 +0200360void ata_scsi_set_sense_information(struct ata_device *dev,
361 struct scsi_cmnd *cmd,
362 const struct ata_taskfile *tf)
363{
364 u64 information;
365
366 if (!cmd)
367 return;
368
369 information = ata_tf_read_block(tf, dev);
370 if (information == U64_MAX)
371 return;
372
373 scsi_set_sense_information(cmd->sense_buffer,
374 SCSI_SENSE_BUFFERSIZE, information);
375}
376
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200377static void ata_scsi_set_invalid_field(struct ata_device *dev,
Hannes Reinecke0df10b82016-04-04 11:44:06 +0200378 struct scsi_cmnd *cmd, u16 field, u8 bit)
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200379{
380 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
Tom Yan8554e5e2016-07-07 01:13:09 +0800381 /* "Invalid field in CDB" */
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200382 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
Hannes Reinecke0df10b82016-04-04 11:44:06 +0200383 field, bit, 1);
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200384}
385
Hannes Reinecke77800812016-04-04 11:44:07 +0200386static void ata_scsi_set_invalid_parameter(struct ata_device *dev,
387 struct scsi_cmnd *cmd, u16 field)
388{
389 /* "Invalid field in parameter list" */
390 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x26, 0x0);
391 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
392 field, 0xff, 0);
Tejun Heo5924b742007-01-02 20:20:07 +0900393}
394
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700395static ssize_t
396ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
397 const char *buf, size_t count)
398{
399 struct Scsi_Host *shost = class_to_shost(dev);
400 struct ata_port *ap = ata_shost_to_port(shost);
401 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
402 return ap->ops->em_store(ap, buf, count);
403 return -EINVAL;
404}
405
406static ssize_t
407ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
408 char *buf)
409{
410 struct Scsi_Host *shost = class_to_shost(dev);
411 struct ata_port *ap = ata_shost_to_port(shost);
412
413 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
414 return ap->ops->em_show(ap, buf);
415 return -EINVAL;
416}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200417DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700418 ata_scsi_em_message_show, ata_scsi_em_message_store);
419EXPORT_SYMBOL_GPL(dev_attr_em_message);
420
421static ssize_t
422ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
423 char *buf)
424{
425 struct Scsi_Host *shost = class_to_shost(dev);
426 struct ata_port *ap = ata_shost_to_port(shost);
427
428 return snprintf(buf, 23, "%d\n", ap->em_message_type);
429}
430DEVICE_ATTR(em_message_type, S_IRUGO,
431 ata_scsi_em_message_type_show, NULL);
432EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
433
434static ssize_t
435ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
436 char *buf)
437{
438 struct scsi_device *sdev = to_scsi_device(dev);
439 struct ata_port *ap = ata_shost_to_port(sdev->host);
440 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
441
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800442 if (atadev && ap->ops->sw_activity_show &&
443 (ap->flags & ATA_FLAG_SW_ACTIVITY))
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700444 return ap->ops->sw_activity_show(atadev, buf);
445 return -EINVAL;
446}
447
448static ssize_t
449ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
450 const char *buf, size_t count)
451{
452 struct scsi_device *sdev = to_scsi_device(dev);
453 struct ata_port *ap = ata_shost_to_port(sdev->host);
454 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
455 enum sw_activity val;
456 int rc;
457
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800458 if (atadev && ap->ops->sw_activity_store &&
459 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700460 val = simple_strtoul(buf, NULL, 0);
461 switch (val) {
462 case OFF: case BLINK_ON: case BLINK_OFF:
463 rc = ap->ops->sw_activity_store(atadev, val);
464 if (!rc)
465 return count;
466 else
467 return rc;
468 }
469 }
470 return -EINVAL;
471}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200472DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700473 ata_scsi_activity_store);
474EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
475
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200476struct device_attribute *ata_common_sdev_attrs[] = {
477 &dev_attr_unload_heads,
Adam Manzanares84f95242016-10-17 11:27:30 -0700478 &dev_attr_ncq_prio_enable,
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200479 NULL
480};
481EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
482
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200483static void ata_scsi_invalid_field(struct ata_device *dev,
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200484 struct scsi_cmnd *cmd, u16 field)
Douglas Gilbertae006512005-10-09 09:09:35 -0400485{
Hannes Reinecke0df10b82016-04-04 11:44:06 +0200486 ata_scsi_set_invalid_field(dev, cmd, field, 0xff);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500487 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -0400488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/**
491 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
492 * @sdev: SCSI device for which BIOS geometry is to be determined
493 * @bdev: block device associated with @sdev
494 * @capacity: capacity of SCSI device
495 * @geom: location to which geometry will be output
496 *
497 * Generic bios head/sector/cylinder calculator
498 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
499 * mapping. Some situations may arise where the disk is not
500 * bootable if this is not used.
501 *
502 * LOCKING:
503 * Defined by the SCSI layer. We don't really care.
504 *
505 * RETURNS:
506 * Zero.
507 */
508int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
509 sector_t capacity, int geom[])
510{
511 geom[0] = 255;
512 geom[1] = 63;
513 sector_div(capacity, 255*63);
514 geom[2] = capacity;
515
516 return 0;
517}
518
Jeff Garzikb0955182005-05-12 15:45:22 -0400519/**
Tejun Heod8d91292010-05-15 20:09:34 +0200520 * ata_scsi_unlock_native_capacity - unlock native capacity
521 * @sdev: SCSI device to adjust device capacity for
522 *
523 * This function is called if a partition on @sdev extends beyond
524 * the end of the device. It requests EH to unlock HPA.
525 *
526 * LOCKING:
527 * Defined by the SCSI layer. Might sleep.
528 */
529void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
530{
531 struct ata_port *ap = ata_shost_to_port(sdev->host);
532 struct ata_device *dev;
533 unsigned long flags;
534
535 spin_lock_irqsave(ap->lock, flags);
536
537 dev = ata_scsi_find_dev(ap, sdev);
538 if (dev && dev->n_sectors < dev->n_native_sectors) {
539 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
540 dev->link->eh_info.action |= ATA_EH_RESET;
541 ata_port_schedule_eh(ap);
542 }
543
544 spin_unlock_irqrestore(ap->lock, flags);
545 ata_port_wait_eh(ap);
546}
547
548/**
Tejun Heo5924b742007-01-02 20:20:07 +0900549 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
Randy Dunlap5eb66fe2009-01-20 16:28:59 -0800550 * @ap: target port
Tejun Heo5924b742007-01-02 20:20:07 +0900551 * @sdev: SCSI device to get identify data for
552 * @arg: User buffer area for identify data
553 *
554 * LOCKING:
555 * Defined by the SCSI layer. We don't really care.
556 *
557 * RETURNS:
558 * Zero on success, negative errno on error.
559 */
Jeff Garzik94be9a52009-01-16 10:17:09 -0500560static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
561 void __user *arg)
Tejun Heo5924b742007-01-02 20:20:07 +0900562{
Tejun Heo5924b742007-01-02 20:20:07 +0900563 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
564 u16 __user *dst = arg;
565 char buf[40];
566
567 if (!dev)
568 return -ENOMSG;
569
570 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
571 return -EFAULT;
572
573 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
574 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
575 return -EFAULT;
576
577 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
578 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
579 return -EFAULT;
580
581 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
582 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
583 return -EFAULT;
584
585 return 0;
586}
587
588/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400589 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800590 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400591 * @arg: User provided data for issuing command
592 *
593 * LOCKING:
594 * Defined by the SCSI layer. We don't really care.
595 *
596 * RETURNS:
597 * Zero on success, negative errno on error.
598 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400599int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
600{
601 int rc = 0;
602 u8 scsi_cmd[MAX_COMMAND_SIZE];
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700603 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400604 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600605 enum dma_data_direction data_dir;
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700606 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400607
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500608 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400609 return -EINVAL;
610
611 if (copy_from_user(args, arg, sizeof(args)))
612 return -EFAULT;
613
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700614 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
615 if (!sensebuf)
616 return -ENOMEM;
617
Jeff Garzikb0955182005-05-12 15:45:22 -0400618 memset(scsi_cmd, 0, sizeof(scsi_cmd));
619
620 if (args[3]) {
Grant Grundler295124d2010-08-17 10:56:53 -0700621 argsize = ATA_SECT_SIZE * args[3];
Jeff Garzikb0955182005-05-12 15:45:22 -0400622 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400623 if (argbuf == NULL) {
624 rc = -ENOMEM;
625 goto error;
626 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400627
628 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
629 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400630 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600631 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400632 } else {
633 scsi_cmd[1] = (3 << 1); /* Non-data */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700634 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600635 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400636 }
637
638 scsi_cmd[0] = ATA_16;
639
640 scsi_cmd[4] = args[2];
Bartlomiej Zolnierkiewicza4f19042008-10-10 22:39:20 +0200641 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
Jeff Garzikb0955182005-05-12 15:45:22 -0400642 scsi_cmd[6] = args[3];
643 scsi_cmd[8] = args[1];
644 scsi_cmd[10] = 0x4f;
645 scsi_cmd[12] = 0xc2;
646 } else {
647 scsi_cmd[6] = args[1];
648 }
649 scsi_cmd[14] = args[0];
650
651 /* Good values for timeout and retries? Values below
652 from scsi_ioctl_send_command() for default case... */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700653 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900654 sensebuf, (10*HZ), 5, 0, NULL);
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700655
656 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
657 u8 *desc = sensebuf + 8;
658 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
659
660 /* If we set cc then ATA pass-through will cause a
661 * check condition even if no error. Filter that. */
662 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
663 struct scsi_sense_hdr sshdr;
664 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400665 &sshdr);
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100666 if (sshdr.sense_key == RECOVERED_ERROR &&
667 sshdr.asc == 0 && sshdr.ascq == 0x1d)
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700668 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
669 }
670
671 /* Send userspace a few ATA registers (same as drivers/ide) */
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400672 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
673 desc[0] == 0x09) { /* code is "ATA Descriptor" */
674 args[0] = desc[13]; /* status */
675 args[1] = desc[3]; /* error */
676 args[2] = desc[5]; /* sector count (0:7) */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700677 if (copy_to_user(arg, args, sizeof(args)))
678 rc = -EFAULT;
679 }
680 }
681
682
683 if (cmd_result) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400684 rc = -EIO;
685 goto error;
686 }
687
Jeff Garzikb0955182005-05-12 15:45:22 -0400688 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500689 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400690 rc = -EFAULT;
691error:
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700692 kfree(sensebuf);
Jesper Juhl8f760782006-06-27 02:55:06 -0700693 kfree(argbuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400694 return rc;
695}
696
697/**
698 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800699 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400700 * @arg: User provided data for issuing command
701 *
702 * LOCKING:
703 * Defined by the SCSI layer. We don't really care.
704 *
705 * RETURNS:
706 * Zero on success, negative errno on error.
707 */
708int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
709{
710 int rc = 0;
711 u8 scsi_cmd[MAX_COMMAND_SIZE];
David Milburnaf068bd2007-01-30 00:59:15 -0800712 u8 args[7], *sensebuf = NULL;
713 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400714
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500715 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400716 return -EINVAL;
717
718 if (copy_from_user(args, arg, sizeof(args)))
719 return -EFAULT;
720
David Milburnaf068bd2007-01-30 00:59:15 -0800721 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
722 if (!sensebuf)
723 return -ENOMEM;
724
Jeff Garzikb0955182005-05-12 15:45:22 -0400725 memset(scsi_cmd, 0, sizeof(scsi_cmd));
726 scsi_cmd[0] = ATA_16;
727 scsi_cmd[1] = (3 << 1); /* Non-data */
David Milburnaf068bd2007-01-30 00:59:15 -0800728 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Jeff Garzikb0955182005-05-12 15:45:22 -0400729 scsi_cmd[4] = args[1];
730 scsi_cmd[6] = args[2];
731 scsi_cmd[8] = args[3];
732 scsi_cmd[10] = args[4];
733 scsi_cmd[12] = args[5];
Mark Lord277239f2007-03-30 17:45:52 -0400734 scsi_cmd[13] = args[6] & 0x4f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400735 scsi_cmd[14] = args[0];
736
Jeff Garzikb0955182005-05-12 15:45:22 -0400737 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500738 from scsi_ioctl_send_command() for default case... */
David Milburnaf068bd2007-01-30 00:59:15 -0800739 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900740 sensebuf, (10*HZ), 5, 0, NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400741
David Milburnaf068bd2007-01-30 00:59:15 -0800742 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
743 u8 *desc = sensebuf + 8;
744 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
745
746 /* If we set cc then ATA pass-through will cause a
747 * check condition even if no error. Filter that. */
748 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
749 struct scsi_sense_hdr sshdr;
750 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
751 &sshdr);
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100752 if (sshdr.sense_key == RECOVERED_ERROR &&
753 sshdr.asc == 0 && sshdr.ascq == 0x1d)
David Milburnaf068bd2007-01-30 00:59:15 -0800754 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
755 }
756
757 /* Send userspace ATA registers */
758 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
759 desc[0] == 0x09) {/* code is "ATA Descriptor" */
760 args[0] = desc[13]; /* status */
761 args[1] = desc[3]; /* error */
762 args[2] = desc[5]; /* sector count (0:7) */
763 args[3] = desc[7]; /* lbal */
764 args[4] = desc[9]; /* lbam */
765 args[5] = desc[11]; /* lbah */
766 args[6] = desc[12]; /* select */
767 if (copy_to_user(arg, args, sizeof(args)))
768 rc = -EFAULT;
769 }
770 }
771
772 if (cmd_result) {
773 rc = -EIO;
774 goto error;
775 }
776
777 error:
778 kfree(sensebuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400779 return rc;
780}
781
Alan Coxe3cf95d2009-04-09 17:31:17 +0100782static int ata_ioc32(struct ata_port *ap)
783{
784 if (ap->flags & ATA_FLAG_PIO_DMA)
785 return 1;
786 if (ap->pflags & ATA_PFLAG_PIO32)
787 return 1;
788 return 0;
789}
790
Jeff Garzik94be9a52009-01-16 10:17:09 -0500791int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
792 int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Arnd Bergmann287e6612016-02-11 14:16:27 +0100794 unsigned long val;
795 int rc = -EINVAL;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100796 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 switch (cmd) {
Arnd Bergmann287e6612016-02-11 14:16:27 +0100799 case HDIO_GET_32BIT:
Alan Coxe3cf95d2009-04-09 17:31:17 +0100800 spin_lock_irqsave(ap->lock, flags);
801 val = ata_ioc32(ap);
802 spin_unlock_irqrestore(ap->lock, flags);
Arnd Bergmann287e6612016-02-11 14:16:27 +0100803 return put_user(val, (unsigned long __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Arnd Bergmann287e6612016-02-11 14:16:27 +0100805 case HDIO_SET_32BIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 val = (unsigned long) arg;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100807 rc = 0;
808 spin_lock_irqsave(ap->lock, flags);
809 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
810 if (val)
811 ap->pflags |= ATA_PFLAG_PIO32;
812 else
813 ap->pflags &= ~ATA_PFLAG_PIO32;
814 } else {
815 if (val != ata_ioc32(ap))
816 rc = -EINVAL;
817 }
818 spin_unlock_irqrestore(ap->lock, flags);
819 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Tejun Heo5924b742007-01-02 20:20:07 +0900821 case HDIO_GET_IDENTITY:
Jeff Garzik94be9a52009-01-16 10:17:09 -0500822 return ata_get_identity(ap, scsidev, arg);
Tejun Heo5924b742007-01-02 20:20:07 +0900823
Jeff Garzikb0955182005-05-12 15:45:22 -0400824 case HDIO_DRIVE_CMD:
825 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
826 return -EACCES;
827 return ata_cmd_ioctl(scsidev, arg);
828
829 case HDIO_DRIVE_TASK:
830 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
831 return -EACCES;
832 return ata_task_ioctl(scsidev, arg);
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 default:
835 rc = -ENOTTY;
836 break;
837 }
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return rc;
840}
Jeff Garzik94be9a52009-01-16 10:17:09 -0500841EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
842
843int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
844{
845 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
846 scsidev, cmd, arg);
847}
848EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850/**
851 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * @dev: ATA device to which the new command is attached
853 * @cmd: SCSI command that originated this ATA command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 *
855 * Obtain a reference to an unused ata_queued_cmd structure,
856 * which is the basic libata structure representing a single
857 * ATA command sent to the hardware.
858 *
859 * If a command was available, fill in the SCSI-specific
860 * portions of the structure with information on the
861 * current command.
862 *
863 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400864 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *
866 * RETURNS:
867 * Command allocated, or %NULL if none available.
868 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100869static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500870 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct ata_queued_cmd *qc;
873
Shaohua Li12cb5ce2015-01-15 17:32:27 -0800874 qc = ata_qc_new_init(dev, cmd->request->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (qc) {
876 qc->scsicmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500877 qc->scsidone = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Tejun Heoff2aeb12007-12-05 16:43:11 +0900879 qc->sg = scsi_sglist(cmd);
Boaz Harrosh71201652007-09-18 17:48:50 +0200880 qc->n_elem = scsi_sg_count(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 } else {
882 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500883 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
886 return qc;
887}
888
Tejun Heoaacda372008-03-18 17:47:43 +0900889static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
890{
891 struct scsi_cmnd *scmd = qc->scsicmd;
892
893 qc->extrabytes = scmd->request->extra_len;
894 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400898 * ata_dump_status - user friendly display of error info
899 * @id: id of the port in question
900 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400902 * Decode and dump the ATA error/status registers for the user so
903 * that they have some idea what really happened at the non
904 * make-believe layer.
905 *
906 * LOCKING:
907 * inherited from caller
908 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100909static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
Jeff Garzikb0955182005-05-12 15:45:22 -0400910{
911 u8 stat = tf->command, err = tf->feature;
912
913 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
914 if (stat & ATA_BUSY) {
915 printk("Busy }\n"); /* Data is not valid in this case */
916 } else {
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100917 if (stat & ATA_DRDY) printk("DriveReady ");
918 if (stat & ATA_DF) printk("DeviceFault ");
919 if (stat & ATA_DSC) printk("SeekComplete ");
920 if (stat & ATA_DRQ) printk("DataRequest ");
921 if (stat & ATA_CORR) printk("CorrectedError ");
922 if (stat & ATA_SENSE) printk("Sense ");
923 if (stat & ATA_ERR) printk("Error ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400924 printk("}\n");
925
926 if (err) {
927 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100928 if (err & ATA_ABORTED) printk("DriveStatusError ");
929 if (err & ATA_ICRC) {
930 if (err & ATA_ABORTED)
931 printk("BadCRC ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400932 else printk("Sector ");
933 }
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100934 if (err & ATA_UNC) printk("UncorrectableError ");
935 if (err & ATA_IDNF) printk("SectorIdNotFound ");
936 if (err & ATA_TRK0NF) printk("TrackZeroNotFound ");
937 if (err & ATA_AMNF) printk("AddrMarkNotFound ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400938 printk("}\n");
939 }
940 }
941}
942
943/**
944 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800945 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400946 * @drv_stat: value contained in ATA status register
947 * @drv_err: value contained in ATA error register
948 * @sk: the sense key we'll fill out
949 * @asc: the additional sense code we'll fill out
950 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900951 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400952 *
953 * Converts an ATA error into a SCSI error. Fill out pointers to
954 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
955 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 *
957 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400958 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100960static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
961 u8 *asc, u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Jeff Garzikb0955182005-05-12 15:45:22 -0400963 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100966 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* BBD|ECC|ID|MAR */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100968 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
969 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* BBD|ECC|ID */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100971 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
972 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* ECC|MC|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100974 {0x61, HARDWARE_ERROR, 0x00, 0x00},
975 // Device fault Hardware error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100977 {0x84, ABORTED_COMMAND, 0x47, 0x00},
978 // Data CRC error SCSI parity error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* MC|ID|ABRT|TRK0|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100980 {0x37, NOT_READY, 0x04, 0x00},
981 // Unit offline Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* MCR|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100983 {0x09, NOT_READY, 0x04, 0x00},
984 // Unrecovered disk error Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* Bad address mark */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100986 {0x01, MEDIUM_ERROR, 0x13, 0x00},
987 // Address mark not found for data field
988 /* TRK0 - Track 0 not found */
989 {0x02, HARDWARE_ERROR, 0x00, 0x00},
990 // Hardware error
Gwendal Grignou78062c52013-06-18 10:54:48 -0700991 /* Abort: 0x04 is not translated here, see below */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* Media change request */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100993 {0x08, NOT_READY, 0x04, 0x00},
994 // FIXME: faking offline
995 /* SRV/IDNF - ID not found */
996 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
997 // Logical address out of range
998 /* MC - Media Changed */
999 {0x20, UNIT_ATTENTION, 0x28, 0x00},
1000 // Not ready to ready change, medium may have changed
1001 /* ECC - Uncorrectable ECC error */
1002 {0x40, MEDIUM_ERROR, 0x11, 0x04},
1003 // Unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* BBD - block marked bad */
Hannes Reinecke8ae72042015-03-27 16:46:32 +01001005 {0x80, MEDIUM_ERROR, 0x11, 0x04},
1006 // Block marked bad Medium error, unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
1008 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01001009 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* Must be first because BUSY means no other bits valid */
Hannes Reinecke8ae72042015-03-27 16:46:32 +01001011 {0x80, ABORTED_COMMAND, 0x47, 0x00},
1012 // Busy, fake parity for now
1013 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
1014 // Device ready, unaligned write command
1015 {0x20, HARDWARE_ERROR, 0x44, 0x00},
1016 // Device fault, internal target failure
1017 {0x08, ABORTED_COMMAND, 0x47, 0x00},
1018 // Timed out in xfer, fake parity for now
1019 {0x04, RECOVERED_ERROR, 0x11, 0x00},
1020 // Recovered ECC error Medium error, recovered
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
1022 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /*
1025 * Is this an error we can process/parse
1026 */
Jeff Garzikb0955182005-05-12 15:45:22 -04001027 if (drv_stat & ATA_BUSY) {
1028 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Jeff Garzikb0955182005-05-12 15:45:22 -04001031 if (drv_err) {
1032 /* Look for drv_err */
1033 for (i = 0; sense_table[i][0] != 0xFF; i++) {
1034 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001035 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -04001036 sense_table[i][0]) {
1037 *sk = sense_table[i][1];
1038 *asc = sense_table[i][2];
1039 *ascq = sense_table[i][3];
1040 goto translate_done;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
Gwendal Grignou78062c52013-06-18 10:54:48 -07001045 /*
1046 * Fall back to interpreting status bits. Note that if the drv_err
1047 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
1048 * is not descriptive enough.
1049 */
Jeff Garzikb0955182005-05-12 15:45:22 -04001050 for (i = 0; stat_table[i][0] != 0xFF; i++) {
1051 if (stat_table[i][0] & drv_stat) {
1052 *sk = stat_table[i][1];
1053 *asc = stat_table[i][2];
1054 *ascq = stat_table[i][3];
1055 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Gwendal Grignou78062c52013-06-18 10:54:48 -07001059 /*
1060 * We need a sensible error return here, which is tricky, and one
1061 * that won't cause people to do things like return a disk wrongly.
1062 */
Alan Cox2d202022006-03-21 15:53:46 +00001063 *sk = ABORTED_COMMAND;
1064 *asc = 0x00;
1065 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -04001066
1067 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +09001068 if (verbose)
1069 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
1070 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
1071 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -04001072 return;
1073}
1074
1075/*
Tejun Heo750426a2006-11-14 22:37:35 +09001076 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -04001077 * @qc: Command that completed.
1078 *
1079 * This function is specific to the ATA descriptor format sense
1080 * block specified for the ATA pass through commands. Regardless
1081 * of whether the command errored or not, return a sense
1082 * block. Copy all controller registers into the sense
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001083 * block. If there was no error, we get the request from an ATA
1084 * passthrough command, so we use the following sense data:
1085 * sk = RECOVERED ERROR
1086 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
1087 *
Jeff Garzikb0955182005-05-12 15:45:22 -04001088 *
1089 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001090 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001091 */
Tejun Heo750426a2006-11-14 22:37:35 +09001092static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001093{
1094 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001095 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001096 unsigned char *sb = cmd->sense_buffer;
1097 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +09001098 int verbose = qc->ap->ops->error_handler == NULL;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001099 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001100
1101 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1102
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001103 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001104
1105 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04001106 * Use ata_to_sense_error() to map status register bits
1107 * onto sense key, asc & ascq.
1108 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001109 if (qc->err_mask ||
1110 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001111 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001112 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001113 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001114 } else {
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001115 /*
1116 * ATA PASS-THROUGH INFORMATION AVAILABLE
1117 * Always in descriptor format sense.
1118 */
1119 scsi_build_sense_buffer(1, cmd->sense_buffer,
1120 RECOVERED_ERROR, 0, 0x1D);
Jeff Garzikb0955182005-05-12 15:45:22 -04001121 }
1122
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001123 if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
1124 u8 len;
Jeff Garzikb0955182005-05-12 15:45:22 -04001125
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001126 /* descriptor format */
1127 len = sb[7];
1128 desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
1129 if (!desc) {
1130 if (SCSI_SENSE_BUFFERSIZE < len + 14)
1131 return;
1132 sb[7] = len + 14;
1133 desc = sb + 8 + len;
1134 }
1135 desc[0] = 9;
1136 desc[1] = 12;
1137 /*
1138 * Copy registers into sense buffer.
1139 */
1140 desc[2] = 0x00;
1141 desc[3] = tf->feature; /* == error reg */
1142 desc[5] = tf->nsect;
1143 desc[7] = tf->lbal;
1144 desc[9] = tf->lbam;
1145 desc[11] = tf->lbah;
1146 desc[12] = tf->device;
1147 desc[13] = tf->command; /* == status reg */
Jeff Garzikb0955182005-05-12 15:45:22 -04001148
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001149 /*
1150 * Fill in Extend bit, and the high order bytes
1151 * if applicable.
1152 */
1153 if (tf->flags & ATA_TFLAG_LBA48) {
1154 desc[2] |= 0x01;
1155 desc[4] = tf->hob_nsect;
1156 desc[6] = tf->hob_lbal;
1157 desc[8] = tf->hob_lbam;
1158 desc[10] = tf->hob_lbah;
1159 }
1160 } else {
1161 /* Fixed sense format */
1162 desc[0] = tf->feature;
1163 desc[1] = tf->command; /* status */
1164 desc[2] = tf->device;
1165 desc[3] = tf->nsect;
1166 desc[0] = 0;
1167 if (tf->flags & ATA_TFLAG_LBA48) {
1168 desc[8] |= 0x80;
1169 if (tf->hob_nsect)
1170 desc[8] |= 0x40;
1171 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
1172 desc[8] |= 0x20;
1173 }
1174 desc[9] = tf->lbal;
1175 desc[10] = tf->lbam;
1176 desc[11] = tf->lbah;
Jeff Garzikb0955182005-05-12 15:45:22 -04001177 }
1178}
1179
1180/**
Tejun Heo750426a2006-11-14 22:37:35 +09001181 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -04001182 * @qc: Command that we are erroring out
1183 *
Tejun Heod25614b2006-11-14 22:37:35 +09001184 * Generate sense block for a failed ATA command @qc. Descriptor
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001185 * format is used to accommodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -04001186 *
1187 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001188 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001189 */
Tejun Heo750426a2006-11-14 22:37:35 +09001190static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001191{
Tejun Heod25614b2006-11-14 22:37:35 +09001192 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -04001193 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001194 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001195 unsigned char *sb = cmd->sense_buffer;
Tejun Heo246619d2006-05-15 20:58:16 +09001196 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +09001197 u64 block;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001198 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001199
1200 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1201
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001202 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001203
Hannes Reinecke59f68472016-04-25 12:45:50 +02001204 if (ata_dev_disabled(dev)) {
1205 /* Device disabled after error recovery */
1206 /* LOGICAL UNIT NOT READY, HARD RESET REQUIRED */
1207 ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
1208 return;
1209 }
Tejun Heod25614b2006-11-14 22:37:35 +09001210 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -04001211 * onto sense key, asc & ascq.
1212 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001213 if (qc->err_mask ||
1214 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001215 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001216 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001217 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001218 } else {
1219 /* Could not decode error */
1220 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
1221 tf->command, qc->err_mask);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001222 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001223 return;
Jeff Garzikb0955182005-05-12 15:45:22 -04001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Tejun Heod25614b2006-11-14 22:37:35 +09001226 block = ata_tf_read_block(&qc->result_tf, dev);
Hannes Reineckecffd1ee2016-04-04 11:43:57 +02001227 if (block == U64_MAX)
1228 return;
Jeff Garzik0274aa22005-06-22 13:50:56 -04001229
Hannes Reineckecf8b49b2016-04-04 11:43:58 +02001230 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Brian Kinga6cce2a2006-03-17 17:04:15 -06001233static void ata_scsi_sdev_config(struct scsi_device *sdev)
1234{
1235 sdev->use_10_for_rw = 1;
1236 sdev->use_10_for_ms = 1;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001237
1238 /* Schedule policy is determined by ->qc_defer() callback and
1239 * it needs to see every deferred qc. Set dev_blocked to 1 to
1240 * prevent SCSI midlayer from automatically deferring
1241 * requests.
1242 */
1243 sdev->max_device_blocked = 1;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001244}
1245
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001246/**
1247 * atapi_drain_needed - Check whether data transfer may overflow
Randy Dunlap73fd8b62008-02-19 13:43:21 -08001248 * @rq: request to be checked
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001249 *
1250 * ATAPI commands which transfer variable length data to host
Masanari Iidac9b55602016-04-13 23:36:27 +09001251 * might overflow due to application error or hardware bug. This
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001252 * function checks whether overflow should be drained and ignored
1253 * for @request.
1254 *
1255 * LOCKING:
1256 * None.
1257 *
1258 * RETURNS:
1259 * 1 if ; otherwise, 0.
1260 */
1261static int atapi_drain_needed(struct request *rq)
1262{
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001263 if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001264 return 0;
1265
Mike Christiea8ebb052016-06-05 14:31:45 -05001266 if (!blk_rq_bytes(rq) || op_is_write(req_op(rq)))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001267 return 0;
1268
1269 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
1270}
1271
1272static int ata_scsi_dev_config(struct scsi_device *sdev,
1273 struct ata_device *dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -06001274{
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001275 struct request_queue *q = sdev->request_queue;
1276
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02001277 if (!ata_id_has_unload(dev->id))
1278 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1279
Tejun Heo914ed352006-11-01 18:39:55 +09001280 /* configure max sectors */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001281 blk_queue_max_hw_sectors(q, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -06001282
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001283 if (dev->class == ATA_DEV_ATAPI) {
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001284 void *buf;
1285
Tejun Heo729a6a32011-01-20 13:59:06 +01001286 sdev->sector_size = ATA_SECT_SIZE;
1287
1288 /* set DMA padding */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001289 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001290
1291 /* configure draining */
1292 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1293 if (!buf) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001294 ata_dev_err(dev, "drain buffer allocation failed\n");
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001295 return -ENOMEM;
1296 }
1297
1298 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1299 } else {
Grant Grundler295124d2010-08-17 10:56:53 -07001300 sdev->sector_size = ata_id_logical_sector_size(dev->id);
Tejun Heod8cf5382008-01-15 08:46:59 +09001301 sdev->manage_start_stop = 1;
James Bottomleydde20202008-02-19 11:36:56 +01001302 }
Tejun Heod8cf5382008-01-15 08:46:59 +09001303
Tejun Heo729a6a32011-01-20 13:59:06 +01001304 /*
1305 * ata_pio_sectors() expects buffer for each sector to not cross
1306 * page boundary. Enforce it by requiring buffers to be sector
1307 * aligned, which works iff sector_size is not larger than
1308 * PAGE_SIZE. ATAPI devices also need the alignment as
1309 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1310 */
1311 if (sdev->sector_size > PAGE_SIZE)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001312 ata_dev_warn(dev,
Tejun Heo729a6a32011-01-20 13:59:06 +01001313 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1314 sdev->sector_size);
1315
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001316 blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
Tejun Heo729a6a32011-01-20 13:59:06 +01001317
Jeff Garzikf26792d2007-10-29 17:18:39 -04001318 if (dev->flags & ATA_DFLAG_AN)
1319 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1320
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001321 if (dev->flags & ATA_DFLAG_NCQ) {
1322 int depth;
1323
1324 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1325 depth = min(ATA_MAX_QUEUE - 1, depth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001326 scsi_change_queue_depth(sdev, depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001327 }
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001328
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001329 blk_queue_flush_queueable(q, false);
1330
Grant Grundler295124d2010-08-17 10:56:53 -07001331 dev->sdev = sdev;
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001332 return 0;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001333}
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335/**
1336 * ata_scsi_slave_config - Set SCSI device attributes
1337 * @sdev: SCSI device to examine
1338 *
1339 * This is called before we actually start reading
1340 * and writing to the device, to configure certain
1341 * SCSI mid-layer behaviors.
1342 *
1343 * LOCKING:
1344 * Defined by SCSI layer. We don't really care.
1345 */
1346
1347int ata_scsi_slave_config(struct scsi_device *sdev)
1348{
Tejun Heo31534362006-05-31 18:27:36 +09001349 struct ata_port *ap = ata_shost_to_port(sdev->host);
1350 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001351 int rc = 0;
Tejun Heo31534362006-05-31 18:27:36 +09001352
Brian Kinga6cce2a2006-03-17 17:04:15 -06001353 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Tejun Heo31534362006-05-31 18:27:36 +09001355 if (dev)
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001356 rc = ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001358 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
1361/**
Tejun Heo83c47bc2006-05-31 18:28:07 +09001362 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1363 * @sdev: SCSI device to be destroyed
1364 *
1365 * @sdev is about to be destroyed for hot/warm unplugging. If
1366 * this unplugging was initiated by libata as indicated by NULL
1367 * dev->sdev, this function doesn't have to do anything.
1368 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1369 * Clear dev->sdev, schedule the device for ATA detach and invoke
1370 * EH.
1371 *
1372 * LOCKING:
1373 * Defined by SCSI layer. We don't really care.
1374 */
1375void ata_scsi_slave_destroy(struct scsi_device *sdev)
1376{
1377 struct ata_port *ap = ata_shost_to_port(sdev->host);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001378 struct request_queue *q = sdev->request_queue;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001379 unsigned long flags;
1380 struct ata_device *dev;
1381
1382 if (!ap->ops->error_handler)
1383 return;
1384
Jeff Garzikba6a1302006-06-22 23:46:10 -04001385 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09001386 dev = __ata_scsi_find_dev(ap, sdev);
1387 if (dev && dev->sdev) {
1388 /* SCSI device already in CANCEL state, no need to offline it */
1389 dev->sdev = NULL;
1390 dev->flags |= ATA_DFLAG_DETACH;
1391 ata_port_schedule_eh(ap);
1392 }
Jeff Garzikba6a1302006-06-22 23:46:10 -04001393 spin_unlock_irqrestore(ap->lock, flags);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001394
1395 kfree(q->dma_drain_buffer);
1396 q->dma_drain_buffer = NULL;
1397 q->dma_drain_size = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001398}
1399
1400/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001401 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
Marcos Paulo de Souza6d2dd052011-10-31 23:50:16 -02001402 * @ap: ATA port to which the device change the queue depth
1403 * @sdev: SCSI device to configure queue depth for
1404 * @queue_depth: new queue depth
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001405 *
Dan Williamsf6e67032011-09-20 15:10:33 -07001406 * libsas and libata have different approaches for associating a sdev to
1407 * its ata_port.
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001408 *
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001409 */
Dan Williamsf6e67032011-09-20 15:10:33 -07001410int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001411 int queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001412{
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001413 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +09001414 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001415
Tejun Heoc3c70c42007-02-20 23:31:22 +09001416 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001417 return sdev->queue_depth;
1418
1419 dev = ata_scsi_find_dev(ap, sdev);
1420 if (!dev || !ata_dev_enabled(dev))
1421 return sdev->queue_depth;
1422
Tejun Heoc3c70c42007-02-20 23:31:22 +09001423 /* NCQ enabled? */
Tejun Heo360f6542006-09-30 19:45:00 +09001424 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001425 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1426 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
Tejun Heo360f6542006-09-30 19:45:00 +09001427 dev->flags |= ATA_DFLAG_NCQ_OFF;
Tejun Heoc3c70c42007-02-20 23:31:22 +09001428 queue_depth = 1;
1429 }
Tejun Heo360f6542006-09-30 19:45:00 +09001430 spin_unlock_irqrestore(ap->lock, flags);
1431
Tejun Heoc3c70c42007-02-20 23:31:22 +09001432 /* limit and apply queue depth */
1433 queue_depth = min(queue_depth, sdev->host->can_queue);
1434 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1435 queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
1436
1437 if (sdev->queue_depth == queue_depth)
1438 return -EINVAL;
1439
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001440 return scsi_change_queue_depth(sdev, queue_depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001441}
1442
1443/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001444 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1445 * @sdev: SCSI device to configure queue depth for
1446 * @queue_depth: new queue depth
Dan Williamsf6e67032011-09-20 15:10:33 -07001447 *
1448 * This is libata standard hostt->change_queue_depth callback.
1449 * SCSI will call into this callback when user tries to set queue
1450 * depth via sysfs.
1451 *
1452 * LOCKING:
1453 * SCSI layer (we don't care)
1454 *
1455 * RETURNS:
1456 * Newly configured queue depth.
1457 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001458int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
Dan Williamsf6e67032011-09-20 15:10:33 -07001459{
1460 struct ata_port *ap = ata_shost_to_port(sdev->host);
1461
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001462 return __ata_change_queue_depth(ap, sdev, queue_depth);
Dan Williamsf6e67032011-09-20 15:10:33 -07001463}
1464
1465/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001466 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1467 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001468 *
1469 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1470 * (to start). Perhaps these commands should be preceded by
1471 * CHECK POWER MODE to see what power mode the device is already in.
1472 * [See SAT revision 5 at www.t10.org]
1473 *
1474 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001475 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001476 *
1477 * RETURNS:
1478 * Zero on success, non-zero on error.
1479 */
Tejun Heoad706992006-12-17 10:45:57 +09001480static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001481{
Tejun Heo542b1442006-12-17 10:45:08 +09001482 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001483 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +09001484 const u8 *cdb = scmd->cmnd;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001485 u16 fp;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001486 u8 bp = 0xff;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001487
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001488 if (scmd->cmd_len < 5) {
1489 fp = 4;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001490 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001491 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09001492
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001493 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1494 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +09001495 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001496 ; /* ignore IMMED bit, violates sat-r05 */
1497 }
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001498 if (cdb[4] & 0x2) {
1499 fp = 4;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001500 bp = 1;
Douglas Gilbertae006512005-10-09 09:09:35 -04001501 goto invalid_fld; /* LOEJ bit set not supported */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001502 }
1503 if (((cdb[4] >> 4) & 0xf) != 0) {
1504 fp = 4;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001505 bp = 3;
Douglas Gilbertae006512005-10-09 09:09:35 -04001506 goto invalid_fld; /* power conditions not supported */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001507 }
Tejun Heoe31e85312007-09-23 13:14:13 +09001508
Tejun Heo542b1442006-12-17 10:45:08 +09001509 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001510 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -04001511
1512 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +09001513 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -04001514
1515 tf->lbah = 0x0;
1516 tf->lbam = 0x0;
1517 tf->lbal = 0x0;
1518 tf->device |= ATA_LBA;
1519 } else {
1520 /* CHS */
1521 tf->lbal = 0x1; /* sect */
1522 tf->lbam = 0x0; /* cyl low */
1523 tf->lbah = 0x0; /* cyl high */
1524 }
1525
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001526 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
Tejun Heo920a4b12007-05-04 21:28:48 +02001527 } else {
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001528 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1529 * or S5) causing some drives to spin up and down again.
1530 */
1531 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1532 system_state == SYSTEM_POWER_OFF)
1533 goto skip;
1534
1535 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1536 system_entering_hibernation())
1537 goto skip;
1538
Robert Hancock78981a72007-01-30 00:59:18 -08001539 /* Issue ATA STANDBY IMMEDIATE command */
1540 tf->command = ATA_CMD_STANDBYNOW1;
Tejun Heo920a4b12007-05-04 21:28:48 +02001541 }
Robert Hancock78981a72007-01-30 00:59:18 -08001542
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001543 /*
1544 * Standby and Idle condition timers could be implemented but that
1545 * would require libata to implement the Power condition mode page
1546 * and allow the user to change it. Changing mode pages requires
1547 * MODE SELECT to be implemented.
1548 */
1549
1550 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001551
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001552 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001553 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
Douglas Gilbertae006512005-10-09 09:09:35 -04001554 return 1;
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001555 skip:
1556 scmd->result = SAM_STAT_GOOD;
1557 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001558}
1559
1560
1561/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1563 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 *
1565 * Sets up an ATA taskfile to issue FLUSH CACHE or
1566 * FLUSH CACHE EXT.
1567 *
1568 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001569 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 *
1571 * RETURNS:
1572 * Zero on success, non-zero on error.
1573 */
Tejun Heoad706992006-12-17 10:45:57 +09001574static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 struct ata_taskfile *tf = &qc->tf;
1577
1578 tf->flags |= ATA_TFLAG_DEVICE;
1579 tf->protocol = ATA_PROT_NODATA;
1580
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001581 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 tf->command = ATA_CMD_FLUSH_EXT;
1583 else
1584 tf->command = ATA_CMD_FLUSH;
1585
Tejun Heob666da32007-10-26 15:53:59 +09001586 /* flush is critical for IO integrity, consider it an IO command */
1587 qc->flags |= ATA_QCFLAG_IO;
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return 0;
1590}
1591
1592/**
Albert Lee3aef5232005-10-04 08:47:43 -04001593 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001594 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001595 *
1596 * Calculate LBA and transfer length for 6-byte commands.
1597 *
1598 * RETURNS:
1599 * @plba: the LBA
1600 * @plen: the transfer length
1601 */
Tejun Heo542b1442006-12-17 10:45:08 +09001602static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001603{
1604 u64 lba = 0;
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001605 u32 len;
Albert Lee3aef5232005-10-04 08:47:43 -04001606
1607 VPRINTK("six-byte command\n");
1608
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001609 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
Tejun Heo542b1442006-12-17 10:45:08 +09001610 lba |= ((u64)cdb[2]) << 8;
1611 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001612
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001613 len = cdb[4];
Albert Lee3aef5232005-10-04 08:47:43 -04001614
1615 *plba = lba;
1616 *plen = len;
1617}
1618
1619/**
1620 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001621 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001622 *
1623 * Calculate LBA and transfer length for 10-byte commands.
1624 *
1625 * RETURNS:
1626 * @plba: the LBA
1627 * @plen: the transfer length
1628 */
Tejun Heo542b1442006-12-17 10:45:08 +09001629static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001630{
1631 u64 lba = 0;
1632 u32 len = 0;
1633
1634 VPRINTK("ten-byte command\n");
1635
Tejun Heo542b1442006-12-17 10:45:08 +09001636 lba |= ((u64)cdb[2]) << 24;
1637 lba |= ((u64)cdb[3]) << 16;
1638 lba |= ((u64)cdb[4]) << 8;
1639 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001640
Tejun Heo542b1442006-12-17 10:45:08 +09001641 len |= ((u32)cdb[7]) << 8;
1642 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001643
1644 *plba = lba;
1645 *plen = len;
1646}
1647
1648/**
1649 * scsi_16_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001650 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001651 *
1652 * Calculate LBA and transfer length for 16-byte commands.
1653 *
1654 * RETURNS:
1655 * @plba: the LBA
1656 * @plen: the transfer length
1657 */
Tejun Heo542b1442006-12-17 10:45:08 +09001658static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001659{
1660 u64 lba = 0;
1661 u32 len = 0;
1662
1663 VPRINTK("sixteen-byte command\n");
1664
Tejun Heo542b1442006-12-17 10:45:08 +09001665 lba |= ((u64)cdb[2]) << 56;
1666 lba |= ((u64)cdb[3]) << 48;
1667 lba |= ((u64)cdb[4]) << 40;
1668 lba |= ((u64)cdb[5]) << 32;
1669 lba |= ((u64)cdb[6]) << 24;
1670 lba |= ((u64)cdb[7]) << 16;
1671 lba |= ((u64)cdb[8]) << 8;
1672 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001673
Tejun Heo542b1442006-12-17 10:45:08 +09001674 len |= ((u32)cdb[10]) << 24;
1675 len |= ((u32)cdb[11]) << 16;
1676 len |= ((u32)cdb[12]) << 8;
1677 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001678
1679 *plba = lba;
1680 *plen = len;
1681}
1682
1683/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1685 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 *
1687 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1688 *
1689 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001690 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 *
1692 * RETURNS:
1693 * Zero on success, non-zero on error.
1694 */
Tejun Heoad706992006-12-17 10:45:57 +09001695static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Tejun Heo542b1442006-12-17 10:45:08 +09001697 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001699 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001701 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001702 u64 block;
1703 u32 n_block;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001704 u16 fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1707 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Tejun Heo2e5704f2006-12-17 10:46:33 +09001709 if (cdb[0] == VERIFY) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001710 if (scmd->cmd_len < 10) {
1711 fp = 9;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001712 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001713 }
Tejun Heo542b1442006-12-17 10:45:08 +09001714 scsi_10_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001715 } else if (cdb[0] == VERIFY_16) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001716 if (scmd->cmd_len < 16) {
1717 fp = 15;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001718 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001719 }
Tejun Heo542b1442006-12-17 10:45:08 +09001720 scsi_16_lba_len(cdb, &block, &n_block);
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001721 } else {
1722 fp = 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001723 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Albert Lee8bf62ece2005-05-12 15:29:42 -04001726 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001727 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001728 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001729 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001730 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001731 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Albert Lee07506692005-10-12 15:04:18 +08001733 if (dev->flags & ATA_DFLAG_LBA) {
1734 tf->flags |= ATA_TFLAG_LBA;
1735
Albert Leec6a33e22005-10-12 15:12:26 +08001736 if (lba_28_ok(block, n_block)) {
1737 /* use LBA28 */
1738 tf->command = ATA_CMD_VERIFY;
1739 tf->device |= (block >> 24) & 0xf;
1740 } else if (lba_48_ok(block, n_block)) {
1741 if (!(dev->flags & ATA_DFLAG_LBA48))
1742 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001743
1744 /* use LBA48 */
1745 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001746 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Albert Lee8bf62ece2005-05-12 15:29:42 -04001748 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Albert Lee8bf62ece2005-05-12 15:29:42 -04001750 tf->hob_lbah = (block >> 40) & 0xff;
1751 tf->hob_lbam = (block >> 32) & 0xff;
1752 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001753 } else
1754 /* request too large even for LBA48 */
1755 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001756
1757 tf->nsect = n_block & 0xff;
1758
1759 tf->lbah = (block >> 16) & 0xff;
1760 tf->lbam = (block >> 8) & 0xff;
1761 tf->lbal = block & 0xff;
1762
1763 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001765 /* CHS */
1766 u32 sect, head, cyl, track;
1767
Albert Leec6a33e22005-10-12 15:12:26 +08001768 if (!lba_28_ok(block, n_block))
1769 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001770
Albert Lee8bf62ece2005-05-12 15:29:42 -04001771 /* Convert LBA to CHS */
1772 track = (u32)block / dev->sectors;
1773 cyl = track / dev->heads;
1774 head = track % dev->heads;
1775 sect = (u32)block % dev->sectors + 1;
1776
Albert Leec187c4b2005-10-04 08:46:51 -04001777 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1778 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001779
1780 /* Check whether the converted CHS can fit.
1781 Cylinder: 0-65535
Albert Lee8bf62ece2005-05-12 15:29:42 -04001782 Head: 0-15
1783 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001784 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001785 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001788 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1789 tf->lbal = sect;
1790 tf->lbam = cyl;
1791 tf->lbah = cyl >> 8;
1792 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001796
1797invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001798 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
Douglas Gilbertae006512005-10-09 09:09:35 -04001799 return 1;
1800
1801out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001802 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001803 /* "Logical Block Address out of range" */
1804 return 1;
1805
1806nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001807 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001808 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
1811/**
1812 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1813 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 *
1815 * Converts any of six SCSI read/write commands into the
1816 * ATA counterpart, including starting sector (LBA),
1817 * sector count, and taking into account the device's LBA48
1818 * support.
1819 *
1820 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1821 * %WRITE_16 are currently supported.
1822 *
1823 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001824 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 *
1826 * RETURNS:
1827 * Zero on success, non-zero on error.
1828 */
Tejun Heoad706992006-12-17 10:45:57 +09001829static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Tejun Heo542b1442006-12-17 10:45:08 +09001831 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001832 const u8 *cdb = scmd->cmnd;
Adam Manzanares8e061782016-10-17 11:27:29 -07001833 struct request *rq = scmd->request;
1834 int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
Tejun Heobd056d72006-11-14 22:47:10 +09001835 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001836 u64 block;
1837 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001838 int rc;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001839 u16 fp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Tejun Heo542b1442006-12-17 10:45:08 +09001841 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001842 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001844 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001845 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001846 case READ_10:
1847 case WRITE_10:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001848 if (unlikely(scmd->cmd_len < 10)) {
1849 fp = 9;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001850 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001851 }
Tejun Heo542b1442006-12-17 10:45:08 +09001852 scsi_10_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001853 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001854 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001855 break;
1856 case READ_6:
1857 case WRITE_6:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001858 if (unlikely(scmd->cmd_len < 6)) {
1859 fp = 5;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001860 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001861 }
Tejun Heo542b1442006-12-17 10:45:08 +09001862 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001863
1864 /* for 6-byte r/w commands, transfer length 0
1865 * means 256 blocks of data, not 0 block.
1866 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001867 if (!n_block)
1868 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001869 break;
1870 case READ_16:
1871 case WRITE_16:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001872 if (unlikely(scmd->cmd_len < 16)) {
1873 fp = 15;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001874 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001875 }
Tejun Heo542b1442006-12-17 10:45:08 +09001876 scsi_16_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001877 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001878 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001879 break;
1880 default:
Albert Lee8bf62ece2005-05-12 15:29:42 -04001881 DPRINTK("no-byte command\n");
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001882 fp = 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001883 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885
Albert Lee8bf62ece2005-05-12 15:29:42 -04001886 /* Check and compose ATA command */
1887 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001888 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1889 * length 0 means transfer 0 block of data.
1890 * However, for ATA R/W commands, sector count 0 means
1891 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001892 *
1893 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001894 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001895 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001896
Tejun Heobd056d72006-11-14 22:47:10 +09001897 qc->flags |= ATA_QCFLAG_IO;
Grant Grundler295124d2010-08-17 10:56:53 -07001898 qc->nbytes = n_block * scmd->device->sector_size;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001899
Tejun Heobd056d72006-11-14 22:47:10 +09001900 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
Adam Manzanares8e061782016-10-17 11:27:29 -07001901 qc->tag, class);
1902
Tejun Heobd056d72006-11-14 22:47:10 +09001903 if (likely(rc == 0))
1904 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001905
Tejun Heobd056d72006-11-14 22:47:10 +09001906 if (rc == -ERANGE)
1907 goto out_of_range;
1908 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001909invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001910 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
Douglas Gilbertae006512005-10-09 09:09:35 -04001911 return 1;
1912
1913out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001914 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001915 /* "Logical Block Address out of range" */
1916 return 1;
1917
1918nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001919 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001920 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001923static void ata_qc_done(struct ata_queued_cmd *qc)
1924{
1925 struct scsi_cmnd *cmd = qc->scsicmd;
1926 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1927
1928 ata_qc_free(qc);
1929 done(cmd);
1930}
1931
Tejun Heo77853bf2006-01-23 13:09:36 +09001932static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Tejun Heoc31f5712006-11-22 12:39:43 +09001934 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001936 u8 *cdb = cmd->cmnd;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001937 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Jeff Garzikb0955182005-05-12 15:45:22 -04001939 /* For ATA pass thru (SAT) commands, generate a sense block if
1940 * user mandated it or if there's an error. Note that if we
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001941 * generate because the user forced us to [CK_COND =1], a check
1942 * condition is generated and the ATA register values are returned
Jeff Garzikb0955182005-05-12 15:45:22 -04001943 * whether the command completed successfully or not. If there
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001944 * was no error, we use the following sense data:
1945 * sk = RECOVERED ERROR
1946 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
Jeff Garzikb0955182005-05-12 15:45:22 -04001947 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001948 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001949 ((cdb[2] & 0x20) || need_sense))
Tejun Heo750426a2006-11-14 22:37:35 +09001950 ata_gen_passthru_sense(qc);
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +02001951 else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1952 cmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001953 else if (need_sense)
1954 ata_gen_ata_sense(qc);
1955 else
1956 cmd->result = SAM_STAT_GOOD;
Jeff Garzikb0955182005-05-12 15:45:22 -04001957
Tejun Heoc31f5712006-11-22 12:39:43 +09001958 if (need_sense && !ap->ops->error_handler)
Tejun Heo44877b42007-02-21 01:06:51 +09001959 ata_dump_status(ap->print_id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001961 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
1964/**
1965 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 * @dev: ATA device to which the command is addressed
1967 * @cmd: SCSI command to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1969 *
1970 * Our ->queuecommand() function has decided that the SCSI
1971 * command issued can be directly translated into an ATA
1972 * command, rather than handled internally.
1973 *
1974 * This function sets up an ata_queued_cmd structure for the
1975 * SCSI command, and sends that ata_queued_cmd to the hardware.
1976 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001977 * The xlat_func argument (actor) returns 0 if ready to execute
1978 * ATA command, else 1 to finish translation. If 1 is returned
1979 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1980 * to be set reflecting an error condition or clean (early)
1981 * termination.
1982 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001984 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09001985 *
1986 * RETURNS:
1987 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1988 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 */
Tejun Heo2115ea92006-05-15 21:03:39 +09001990static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09001991 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Tejun Heo31cc23b2007-09-23 13:14:12 +09001993 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 struct ata_queued_cmd *qc;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001995 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 VPRINTK("ENTER\n");
1998
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05001999 qc = ata_scsi_qc_new(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04002001 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05002004 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
2005 cmd->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harrosh71201652007-09-18 17:48:50 +02002006 if (unlikely(scsi_bufflen(cmd) < 1)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002007 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04002008 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010
Boaz Harrosh71201652007-09-18 17:48:50 +02002011 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 qc->dma_dir = cmd->sc_data_direction;
2014 }
2015
2016 qc->complete_fn = ata_scsi_qc_complete;
2017
Tejun Heoad706992006-12-17 10:45:57 +09002018 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04002019 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Tejun Heo31cc23b2007-09-23 13:14:12 +09002021 if (ap->ops->qc_defer) {
2022 if ((rc = ap->ops->qc_defer(qc)))
2023 goto defer;
2024 }
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09002027 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Douglas Gilbertae006512005-10-09 09:09:35 -04002032early_finish:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002033 ata_qc_free(qc);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002034 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -04002035 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002036 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002037
2038err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04002040 cmd->result = (DID_ERROR << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002041 cmd->scsi_done(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05002042err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04002043 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002044 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09002045
2046defer:
Tejun Heo31cc23b2007-09-23 13:14:12 +09002047 ata_qc_free(qc);
Tejun Heo3dc1d882006-05-15 21:03:45 +09002048 DPRINTK("EXIT - defer\n");
Tejun Heo31cc23b2007-09-23 13:14:12 +09002049 if (rc == ATA_DEFER_LINK)
2050 return SCSI_MLQUEUE_DEVICE_BUSY;
2051 else
2052 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
2055/**
2056 * ata_scsi_rbuf_get - Map response buffer.
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002057 * @cmd: SCSI command containing buffer to be mapped.
Tejun Heo87340e92008-04-28 17:48:51 +09002058 * @flags: unsigned long variable to store irq enable status
2059 * @copy_in: copy in from user buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 *
Tejun Heo87340e92008-04-28 17:48:51 +09002061 * Prepare buffer for simulated SCSI commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 *
2063 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09002064 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 *
2066 * RETURNS:
Tejun Heo87340e92008-04-28 17:48:51 +09002067 * Pointer to response buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 */
Tejun Heo87340e92008-04-28 17:48:51 +09002069static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
2070 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Tejun Heo87340e92008-04-28 17:48:51 +09002072 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Tejun Heo87340e92008-04-28 17:48:51 +09002074 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
2075 if (copy_in)
2076 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2077 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2078 return ata_scsi_rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
2081/**
2082 * ata_scsi_rbuf_put - Unmap response buffer.
2083 * @cmd: SCSI command containing buffer to be unmapped.
Tejun Heo87340e92008-04-28 17:48:51 +09002084 * @copy_out: copy out result
2085 * @flags: @flags passed to ata_scsi_rbuf_get()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 *
Tejun Heo87340e92008-04-28 17:48:51 +09002087 * Returns rbuf buffer. The result is copied to @cmd's buffer if
2088 * @copy_back is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 *
2090 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09002091 * Unlocks ata_scsi_rbuf_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 */
Tejun Heo87340e92008-04-28 17:48:51 +09002093static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
2094 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Tejun Heo87340e92008-04-28 17:48:51 +09002096 if (copy_out)
2097 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2098 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2099 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
2102/**
2103 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
2104 * @args: device IDENTIFY data / SCSI command of interest.
2105 * @actor: Callback hook for desired SCSI command simulator
2106 *
2107 * Takes care of the hard work of simulating a SCSI command...
2108 * Mapping the response buffer, calling the command's handler,
2109 * and handling the handler's return value. This return value
2110 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04002111 * completed successfully (0), or not (in which case cmd->result
2112 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 *
2114 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002115 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 */
Tejun Heof0761be2008-04-28 17:16:52 +09002117static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
Tejun Heo87340e92008-04-28 17:48:51 +09002118 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
2120 u8 *rbuf;
Tejun Heo87340e92008-04-28 17:48:51 +09002121 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 struct scsi_cmnd *cmd = args->cmd;
Jeff Garzikb445c562008-02-29 19:10:51 -05002123 unsigned long flags;
2124
Tejun Heo87340e92008-04-28 17:48:51 +09002125 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
2126 rc = actor(args, rbuf);
2127 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
Jeff Garzikb445c562008-02-29 19:10:51 -05002128
Douglas Gilbertae006512005-10-09 09:09:35 -04002129 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04002131 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
2134/**
2135 * ata_scsiop_inq_std - Simulate INQUIRY command
2136 * @args: device IDENTIFY data / SCSI command of interest.
2137 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 *
2139 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05002140 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 *
2142 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002143 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 */
Tejun Heo87340e92008-04-28 17:48:51 +09002145static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Tejun Heo87340e92008-04-28 17:48:51 +09002147 const u8 versions[] = {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002148 0x00,
Tejun Heo87340e92008-04-28 17:48:51 +09002149 0x60, /* SAM-3 (no version claimed) */
2150
2151 0x03,
2152 0x20, /* SBC-2 (no version claimed) */
2153
Tom Yan29a37ea2016-07-12 21:29:35 +08002154 0x03,
2155 0x00 /* SPC-3 (no version claimed) */
Tejun Heo87340e92008-04-28 17:48:51 +09002156 };
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002157 const u8 versions_zbc[] = {
2158 0x00,
2159 0xA0, /* SAM-5 (no version claimed) */
2160
Hannes Reinecke856c4662016-04-25 12:45:55 +02002161 0x06,
2162 0x00, /* SBC-4 (no version claimed) */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002163
Hannes Reinecke856c4662016-04-25 12:45:55 +02002164 0x05,
2165 0xC0, /* SPC-5 (no version claimed) */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002166
2167 0x60,
Hannes Reinecke856c4662016-04-25 12:45:55 +02002168 0x24, /* ZBC r05 */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002169 };
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 u8 hdr[] = {
2172 TYPE_DISK,
2173 0,
2174 0x5, /* claim SPC-3 version compatibility */
2175 2,
Tom Yan415ffdd2016-07-12 21:29:34 +08002176 95 - 4,
2177 0,
2178 0,
2179 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 };
2181
Tejun Heo87340e92008-04-28 17:48:51 +09002182 VPRINTK("ENTER\n");
2183
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02002184 /* set scsi removable (RMB) bit per ata bit, or if the
2185 * AHCI port says it's external (Hotplug-capable, eSATA).
2186 */
2187 if (ata_id_removable(args->id) ||
2188 (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 hdr[1] |= (1 << 7);
2190
Tom Yan56b8cba2016-07-12 22:12:01 +08002191 if (args->dev->class == ATA_DEV_ZAC) {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002192 hdr[0] = TYPE_ZBC;
Tom Yan56b8cba2016-07-12 22:12:01 +08002193 hdr[2] = 0x7; /* claim SPC-5 version compatibility */
2194 }
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002197 memcpy(&rbuf[8], "ATA ", 8);
2198 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
Keith Buschc49a6bf2014-05-01 11:12:03 -06002199
2200 /* From SAT, use last 2 words from fw rev unless they are spaces */
2201 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
2202 if (strncmp(&rbuf[32], " ", 4) == 0)
2203 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Tejun Heo87340e92008-04-28 17:48:51 +09002205 if (rbuf[32] == 0 || rbuf[32] == ' ')
2206 memcpy(&rbuf[32], "n/a ", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Hannes Reinecke856c4662016-04-25 12:45:55 +02002208 if (ata_id_zoned_cap(args->id) || args->dev->class == ATA_DEV_ZAC)
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002209 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
2210 else
2211 memcpy(rbuf + 58, versions, sizeof(versions));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 return 0;
2214}
2215
2216/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002217 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 * @args: device IDENTIFY data / SCSI command of interest.
2219 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002221 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 *
2223 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002224 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 */
Tejun Heo87340e92008-04-28 17:48:51 +09002226static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002228 int num_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 const u8 pages[] = {
2230 0x00, /* page 0x00, this page */
2231 0x80, /* page 0x80, unit serial no page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002232 0x83, /* page 0x83, device ident page */
2233 0x89, /* page 0x89, ata info page */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002234 0xb0, /* page 0xb0, block limits page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002235 0xb1, /* page 0xb1, block device characteristics page */
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002236 0xb2, /* page 0xb2, thin provisioning page */
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002237 0xb6, /* page 0xb6, zoned block device characteristics */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 };
Tejun Heo87340e92008-04-28 17:48:51 +09002239
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002240 num_pages = sizeof(pages);
2241 if (!(args->dev->flags & ATA_DFLAG_ZAC))
2242 num_pages--;
2243 rbuf[3] = num_pages; /* number of supported VPD pages */
2244 memcpy(rbuf + 4, pages, num_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return 0;
2246}
2247
2248/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002249 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 * @args: device IDENTIFY data / SCSI command of interest.
2251 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 *
2253 * Returns ATA device serial number.
2254 *
2255 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002256 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 */
Tejun Heo87340e92008-04-28 17:48:51 +09002258static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 const u8 hdr[] = {
2261 0,
2262 0x80, /* this page code */
2263 0,
Tejun Heoa0cf7332007-01-02 20:18:49 +09002264 ATA_ID_SERNO_LEN, /* page len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 };
Tejun Heo87340e92008-04-28 17:48:51 +09002266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002268 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2269 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return 0;
2271}
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002274 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * @args: device IDENTIFY data / SCSI command of interest.
2276 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002278 * Yields two logical unit device identification designators:
2279 * - vendor specific ASCII containing the ATA serial number
2280 * - SAT defined "t10 vendor id based" containing ASCII vendor
2281 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 *
2283 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002284 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 */
Tejun Heo87340e92008-04-28 17:48:51 +09002286static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Jeff Garzikb142eb62006-03-21 20:37:47 -05002288 const int sat_model_serial_desc_len = 68;
Tejun Heo87340e92008-04-28 17:48:51 +09002289 int num;
Jeff Garzikb142eb62006-03-21 20:37:47 -05002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05002292 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Tejun Heo87340e92008-04-28 17:48:51 +09002294 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2295 rbuf[num + 0] = 2;
2296 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2297 num += 4;
2298 ata_id_string(args->id, (unsigned char *) rbuf + num,
2299 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2300 num += ATA_ID_SERNO_LEN;
2301
2302 /* SAT defined lu model and serial numbers descriptor */
2303 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2304 rbuf[num + 0] = 2;
2305 rbuf[num + 1] = 1;
2306 rbuf[num + 3] = sat_model_serial_desc_len;
2307 num += 4;
2308 memcpy(rbuf + num, "ATA ", 8);
2309 num += 8;
2310 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2311 ATA_ID_PROD_LEN);
2312 num += ATA_ID_PROD_LEN;
2313 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2314 ATA_ID_SERNO_LEN);
2315 num += ATA_ID_SERNO_LEN;
2316
Hannes Reinecke6b3b9d72011-03-07 08:56:44 +01002317 if (ata_id_has_wwn(args->id)) {
2318 /* SAT defined lu world wide name */
2319 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2320 rbuf[num + 0] = 1;
2321 rbuf[num + 1] = 3;
2322 rbuf[num + 3] = ATA_ID_WWN_LEN;
2323 num += 4;
2324 ata_id_string(args->id, (unsigned char *) rbuf + num,
2325 ATA_ID_WWN, ATA_ID_WWN_LEN);
2326 num += ATA_ID_WWN_LEN;
2327 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05002328 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return 0;
2330}
2331
2332/**
Jeff Garzikad355b42007-09-21 20:38:03 -04002333 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2334 * @args: device IDENTIFY data / SCSI command of interest.
2335 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Jeff Garzikad355b42007-09-21 20:38:03 -04002336 *
2337 * Yields SAT-specified ATA VPD page.
2338 *
2339 * LOCKING:
2340 * spin_lock_irqsave(host lock)
2341 */
Tejun Heo87340e92008-04-28 17:48:51 +09002342static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
Jeff Garzikad355b42007-09-21 20:38:03 -04002343{
Jeff Garzikad355b42007-09-21 20:38:03 -04002344 struct ata_taskfile tf;
Jeff Garzikad355b42007-09-21 20:38:03 -04002345
Jeff Garzikad355b42007-09-21 20:38:03 -04002346 memset(&tf, 0, sizeof(tf));
2347
Tejun Heo87340e92008-04-28 17:48:51 +09002348 rbuf[1] = 0x89; /* our page code */
2349 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2350 rbuf[3] = (0x238 & 0xff);
Jeff Garzikad355b42007-09-21 20:38:03 -04002351
Tejun Heo87340e92008-04-28 17:48:51 +09002352 memcpy(&rbuf[8], "linux ", 8);
2353 memcpy(&rbuf[16], "libata ", 16);
2354 memcpy(&rbuf[32], DRV_VERSION, 4);
Jeff Garzikad355b42007-09-21 20:38:03 -04002355
2356 /* we don't store the ATA device signature, so we fake it */
2357
2358 tf.command = ATA_DRDY; /* really, this is Status reg */
2359 tf.lbal = 0x1;
2360 tf.nsect = 0x1;
2361
Tejun Heo87340e92008-04-28 17:48:51 +09002362 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
2363 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
Jeff Garzikad355b42007-09-21 20:38:03 -04002364
Tejun Heo87340e92008-04-28 17:48:51 +09002365 rbuf[56] = ATA_CMD_ID_ATA;
Jeff Garzikad355b42007-09-21 20:38:03 -04002366
Tejun Heo87340e92008-04-28 17:48:51 +09002367 memcpy(&rbuf[60], &args->id[0], 512);
Jeff Garzikad355b42007-09-21 20:38:03 -04002368 return 0;
2369}
2370
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002371static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2372{
Grant Grundler295124d2010-08-17 10:56:53 -07002373 u16 min_io_sectors;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002374
2375 rbuf[1] = 0xb0;
2376 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2377
2378 /*
2379 * Optimal transfer length granularity.
2380 *
2381 * This is always one physical block, but for disks with a smaller
2382 * logical than physical sector size we need to figure out what the
2383 * latter is.
2384 */
Grant Grundler295124d2010-08-17 10:56:53 -07002385 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002386 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2387
2388 /*
2389 * Optimal unmap granularity.
2390 *
2391 * The ATA spec doesn't even know about a granularity or alignment
2392 * for the TRIM command. We can leave away most of the unmap related
2393 * VPD page entries, but we have specifify a granularity to signal
2394 * that we support some form of unmap - in thise case via WRITE SAME
2395 * with the unmap bit set.
2396 */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002397 if (ata_id_has_trim(args->id)) {
Tom Yan29838602016-07-13 04:31:23 +08002398 put_unaligned_be64(65535 * ATA_MAX_TRIM_RNUM, &rbuf[36]);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002399 put_unaligned_be32(1, &rbuf[28]);
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002400 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002401
2402 return 0;
2403}
2404
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002405static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2406{
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002407 int form_factor = ata_id_form_factor(args->id);
2408 int media_rotation_rate = ata_id_rotation_rate(args->id);
Hannes Reinecke856c4662016-04-25 12:45:55 +02002409 u8 zoned = ata_id_zoned_cap(args->id);
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002410
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002411 rbuf[1] = 0xb1;
2412 rbuf[3] = 0x3c;
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002413 rbuf[4] = media_rotation_rate >> 8;
2414 rbuf[5] = media_rotation_rate;
2415 rbuf[7] = form_factor;
Hannes Reinecke856c4662016-04-25 12:45:55 +02002416 if (zoned)
2417 rbuf[8] = (zoned << 4);
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002418
2419 return 0;
2420}
2421
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002422static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
2423{
2424 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2425 rbuf[1] = 0xb2;
2426 rbuf[3] = 0x4;
2427 rbuf[5] = 1 << 6; /* TPWS */
2428
2429 return 0;
2430}
2431
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002432static unsigned int ata_scsiop_inq_b6(struct ata_scsi_args *args, u8 *rbuf)
2433{
2434 /*
2435 * zbc-r05 SCSI Zoned Block device characteristics VPD page
2436 */
2437 rbuf[1] = 0xb6;
2438 rbuf[3] = 0x3C;
2439
2440 /*
2441 * URSWRZ bit is only meaningful for host-managed ZAC drives
2442 */
2443 if (args->dev->zac_zoned_cap & 1)
2444 rbuf[4] |= 1;
2445 put_unaligned_be32(args->dev->zac_zones_optimal_open, &rbuf[8]);
2446 put_unaligned_be32(args->dev->zac_zones_optimal_nonseq, &rbuf[12]);
2447 put_unaligned_be32(args->dev->zac_zones_max_open, &rbuf[16]);
2448
2449 return 0;
2450}
2451
Jeff Garzikad355b42007-09-21 20:38:03 -04002452/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002453 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 * @args: device IDENTIFY data / SCSI command of interest.
2455 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 *
2457 * No operation. Simply returns success to caller, to indicate
2458 * that the caller should successfully complete this SCSI command.
2459 *
2460 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002461 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 */
Tejun Heo87340e92008-04-28 17:48:51 +09002463static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 VPRINTK("ENTER\n");
2466 return 0;
2467}
2468
2469/**
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002470 * modecpy - Prepare response for MODE SENSE
2471 * @dest: output buffer
2472 * @src: data being copied
2473 * @n: length of mode page
2474 * @changeable: whether changeable parameters are requested
2475 *
2476 * Generate a generic MODE SENSE page for either current or changeable
2477 * parameters.
2478 *
2479 * LOCKING:
2480 * None.
2481 */
2482static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2483{
2484 if (changeable) {
2485 memcpy(dest, src, 2);
2486 memset(dest + 2, 0, n - 2);
2487 } else {
2488 memcpy(dest, src, n);
2489 }
2490}
2491
2492/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 * ata_msense_caching - Simulate MODE SENSE caching info page
2494 * @id: device IDENTIFY data
Tejun Heo87340e92008-04-28 17:48:51 +09002495 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002496 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 *
2498 * Generate a caching info page, which conditionally indicates
2499 * write caching to the SCSI layer, depending on device
2500 * capabilities.
2501 *
2502 * LOCKING:
2503 * None.
2504 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002505static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002507 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
Tom Yan737bee92016-07-20 04:39:28 +08002508 if (changeable) {
2509 buf[2] |= (1 << 2); /* ata_mselect_caching() */
2510 } else {
2511 buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */
2512 buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */
2513 }
Tejun Heo87340e92008-04-28 17:48:51 +09002514 return sizeof(def_cache_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
2517/**
Tom Yanf086b742016-07-13 02:54:12 +08002518 * ata_msense_control - Simulate MODE SENSE control mode page
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002519 * @dev: ATA device of interest
Tejun Heo87340e92008-04-28 17:48:51 +09002520 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002521 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 *
2523 * Generate a generic MODE SENSE control mode page.
2524 *
2525 * LOCKING:
2526 * None.
2527 */
Tom Yanf086b742016-07-13 02:54:12 +08002528static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002529 bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002531 modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
Tom Yan737bee92016-07-20 04:39:28 +08002532 if (changeable) {
2533 buf[2] |= (1 << 2); /* ata_mselect_control() */
2534 } else {
2535 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
2536
2537 buf[2] |= (d_sense << 2); /* descriptor format sense data */
2538 }
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002539 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542/**
2543 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002544 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002545 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 *
2547 * Generate a generic MODE SENSE r/w error recovery page.
2548 *
2549 * LOCKING:
2550 * None.
2551 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002552static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002554 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2555 changeable);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002556 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002559/*
2560 * We can turn this into a real blacklist if it's needed, for now just
2561 * blacklist any Maxtor BANC1G10 revision firmware
2562 */
2563static int ata_dev_supports_fua(u16 *id)
2564{
Tejun Heoa0cf7332007-01-02 20:18:49 +09002565 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002566
Jeff Garzikc3c013a2006-02-27 22:31:19 -05002567 if (!libata_fua)
2568 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002569 if (!ata_id_has_fua(id))
2570 return 0;
2571
Tejun Heoa0cf7332007-01-02 20:18:49 +09002572 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2573 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002574
Tejun Heo2e026712006-02-12 22:47:04 +09002575 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002576 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09002577 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002578 return 1;
2579
2580 return 0; /* blacklisted */
2581}
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583/**
2584 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2585 * @args: device IDENTIFY data / SCSI command of interest.
2586 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002588 * Simulate MODE SENSE commands. Assume this is invoked for direct
2589 * access devices (e.g. disks) only. There should be no block
2590 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 *
2592 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002593 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 */
Tejun Heo87340e92008-04-28 17:48:51 +09002595static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002597 struct ata_device *dev = args->dev;
Tejun Heo87340e92008-04-28 17:48:51 +09002598 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002599 const u8 sat_blk_desc[] = {
2600 0, 0, 0, 0, /* number of blocks: sat unspecified */
2601 0,
2602 0, 0x2, 0x0 /* block length: 512 bytes */
2603 };
2604 u8 pg, spg;
Tejun Heo87340e92008-04-28 17:48:51 +09002605 unsigned int ebd, page_control, six_byte;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002606 u8 dpofua, bp = 0xff;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002607 u16 fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 VPRINTK("ENTER\n");
2610
2611 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002612 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2613 /*
2614 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002618 switch (page_control) {
2619 case 0: /* current */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002620 case 1: /* changeable */
2621 case 2: /* defaults */
Douglas Gilbertae006512005-10-09 09:09:35 -04002622 break; /* supported */
2623 case 3: /* saved */
2624 goto saving_not_supp;
Douglas Gilbertae006512005-10-09 09:09:35 -04002625 default:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002626 fp = 2;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002627 bp = 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002628 goto invalid_fld;
2629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Tejun Heo87340e92008-04-28 17:48:51 +09002631 if (six_byte)
2632 p += 4 + (ebd ? 8 : 0);
2633 else
2634 p += 8 + (ebd ? 8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002636 pg = scsicmd[2] & 0x3f;
2637 spg = scsicmd[3];
2638 /*
2639 * No mode subpages supported (yet) but asking for _all_
2640 * subpages may be valid
2641 */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002642 if (spg && (spg != ALL_SUB_MPAGES)) {
2643 fp = 3;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002644 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002645 }
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002646
2647 switch(pg) {
2648 case RW_RECOVERY_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002649 p += ata_msense_rw_recovery(p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 break;
2651
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002652 case CACHE_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002653 p += ata_msense_caching(args->id, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 break;
2655
Tejun Heo87340e92008-04-28 17:48:51 +09002656 case CONTROL_MPAGE:
Tom Yanf086b742016-07-13 02:54:12 +08002657 p += ata_msense_control(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002660 case ALL_MPAGES:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002661 p += ata_msense_rw_recovery(p, page_control == 1);
2662 p += ata_msense_caching(args->id, p, page_control == 1);
Tom Yanf086b742016-07-13 02:54:12 +08002663 p += ata_msense_control(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 break;
2665
2666 default: /* invalid page code */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002667 fp = 2;
Douglas Gilbertae006512005-10-09 09:09:35 -04002668 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002671 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002672 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002673 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2674 dpofua = 1 << 4;
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (six_byte) {
Tejun Heo87340e92008-04-28 17:48:51 +09002677 rbuf[0] = p - rbuf - 1;
2678 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002679 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002680 rbuf[3] = sizeof(sat_blk_desc);
2681 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 } else {
Tejun Heo87340e92008-04-28 17:48:51 +09002684 unsigned int output_len = p - rbuf - 2;
2685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 rbuf[0] = output_len >> 8;
Tejun Heo87340e92008-04-28 17:48:51 +09002687 rbuf[1] = output_len;
2688 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002689 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002690 rbuf[7] = sizeof(sat_blk_desc);
2691 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002695
2696invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002697 ata_scsi_set_invalid_field(dev, args->cmd, fp, bp);
Douglas Gilbertae006512005-10-09 09:09:35 -04002698 return 1;
2699
2700saving_not_supp:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002701 ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04002702 /* "Saving parameters not supported" */
2703 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
2705
2706/**
2707 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2708 * @args: device IDENTIFY data / SCSI command of interest.
2709 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 *
2711 * Simulate READ CAPACITY commands.
2712 *
2713 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002714 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 */
Tejun Heo87340e92008-04-28 17:48:51 +09002716static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002718 struct ata_device *dev = args->dev;
2719 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
Grant Grundler295124d2010-08-17 10:56:53 -07002720 u32 sector_size; /* physical sector size in bytes */
2721 u8 log2_per_phys;
2722 u16 lowest_aligned;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002723
Grant Grundler295124d2010-08-17 10:56:53 -07002724 sector_size = ata_id_logical_sector_size(dev->id);
2725 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2726 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 VPRINTK("ENTER\n");
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002731 if (last_lba >= 0xffffffffULL)
2732 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /* sector count, 32-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002735 rbuf[0] = last_lba >> (8 * 3);
2736 rbuf[1] = last_lba >> (8 * 2);
2737 rbuf[2] = last_lba >> (8 * 1);
2738 rbuf[3] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002741 rbuf[4] = sector_size >> (8 * 3);
2742 rbuf[5] = sector_size >> (8 * 2);
2743 rbuf[6] = sector_size >> (8 * 1);
2744 rbuf[7] = sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 } else {
2746 /* sector count, 64-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002747 rbuf[0] = last_lba >> (8 * 7);
2748 rbuf[1] = last_lba >> (8 * 6);
2749 rbuf[2] = last_lba >> (8 * 5);
2750 rbuf[3] = last_lba >> (8 * 4);
2751 rbuf[4] = last_lba >> (8 * 3);
2752 rbuf[5] = last_lba >> (8 * 2);
2753 rbuf[6] = last_lba >> (8 * 1);
2754 rbuf[7] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002757 rbuf[ 8] = sector_size >> (8 * 3);
2758 rbuf[ 9] = sector_size >> (8 * 2);
2759 rbuf[10] = sector_size >> (8 * 1);
2760 rbuf[11] = sector_size;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002761
2762 rbuf[12] = 0;
Grant Grundler295124d2010-08-17 10:56:53 -07002763 rbuf[13] = log2_per_phys;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002764 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2765 rbuf[15] = lowest_aligned;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002766
Arne Fitzenreiter71d126f2015-07-15 13:54:36 +02002767 if (ata_id_has_trim(args->id) &&
2768 !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002769 rbuf[14] |= 0x80; /* LBPME */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002770
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002771 if (ata_id_has_zero_after_trim(args->id) &&
2772 dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
2773 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2774 rbuf[14] |= 0x40; /* LBPRZ */
2775 }
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002776 }
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002777 if (ata_id_zoned_cap(args->id) ||
2778 args->dev->class == ATA_DEV_ZAC)
2779 rbuf[12] = (1 << 4); /* RC_BASIS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return 0;
2782}
2783
2784/**
2785 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2786 * @args: device IDENTIFY data / SCSI command of interest.
2787 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 *
2789 * Simulate REPORT LUNS command.
2790 *
2791 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002792 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 */
Tejun Heo87340e92008-04-28 17:48:51 +09002794static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
2796 VPRINTK("ENTER\n");
2797 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2798
2799 return 0;
2800}
2801
Tejun Heo77853bf2006-01-23 13:09:36 +09002802static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002803{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002804 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002805 /* FIXME: not quite right; we don't want the
2806 * translation of taskfile registers into
2807 * a sense descriptors, since that's only
2808 * correct for ATA, not ATAPI
2809 */
Tejun Heo750426a2006-11-14 22:37:35 +09002810 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002811 }
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002812
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002813 ata_qc_done(qc);
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002814}
2815
2816/* is it pointless to prefer PIO for "safety reasons"? */
2817static inline int ata_pio_use_silly(struct ata_port *ap)
2818{
2819 return (ap->flags & ATA_FLAG_PIO_DMA);
2820}
2821
2822static void atapi_request_sense(struct ata_queued_cmd *qc)
2823{
2824 struct ata_port *ap = qc->ap;
2825 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002826
2827 DPRINTK("ATAPI request sense\n");
2828
James Bottomley7ccd7202008-01-17 11:56:24 -06002829 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Jeff Garzika939c962005-10-05 17:09:16 -04002830
Tejun Heo127102a2008-04-07 22:47:21 +09002831#ifdef CONFIG_ATA_SFF
James Bottomley855d8542008-04-18 13:18:48 -05002832 if (ap->ops->sff_tf_read)
2833 ap->ops->sff_tf_read(ap, &qc->tf);
Tejun Heo127102a2008-04-07 22:47:21 +09002834#endif
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002835
2836 /* fill these in, for the case where they are -not- overwritten */
2837 cmd->sense_buffer[0] = 0x70;
2838 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2839
2840 ata_qc_reinit(qc);
2841
Tejun Heo93f8fec2007-12-05 16:43:01 +09002842 /* setup sg table and init transfer direction */
akpm@linux-foundation.orgcadb7342008-01-15 16:01:52 -08002843 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
Tejun Heo93f8fec2007-12-05 16:43:01 +09002844 ata_sg_init(qc, &qc->sgent, 1);
Jeff Garzika939c962005-10-05 17:09:16 -04002845 qc->dma_dir = DMA_FROM_DEVICE;
2846
Tejun Heo6e7846e2006-02-12 23:32:58 +09002847 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002848 qc->cdb[0] = REQUEST_SENSE;
2849 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2850
2851 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2852 qc->tf.command = ATA_CMD_PACKET;
2853
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002854 if (ata_pio_use_silly(ap)) {
Tejun Heo0dc36882007-12-18 16:34:43 -05002855 qc->tf.protocol = ATAPI_PROT_DMA;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002856 qc->tf.feature |= ATAPI_PKT_DMA;
2857 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05002858 qc->tf.protocol = ATAPI_PROT_PIO;
Alan Cox2db78dd2007-10-02 13:53:04 -07002859 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2860 qc->tf.lbah = 0;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002861 }
Jeff Garzika939c962005-10-05 17:09:16 -04002862 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2863
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002864 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002865
Tejun Heo8e0e6942006-03-31 20:41:11 +09002866 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002867
2868 DPRINTK("EXIT\n");
2869}
2870
Tejun Heo77853bf2006-01-23 13:09:36 +09002871static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
2873 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002874 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Jeff Garzika7dac442005-10-30 04:44:42 -05002876 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002877
Tejun Heo246619d2006-05-15 20:58:16 +09002878 /* handle completion from new EH */
2879 if (unlikely(qc->ap->ops->error_handler &&
2880 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2881
2882 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2883 /* FIXME: not quite right; we don't want the
2884 * translation of taskfile registers into a
2885 * sense descriptors, since that's only
2886 * correct for ATA, not ATAPI
2887 */
Tejun Heo750426a2006-11-14 22:37:35 +09002888 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002889 }
2890
Tejun Heo22aac082006-08-08 14:08:59 +09002891 /* SCSI EH automatically locks door if sdev->locked is
2892 * set. Sometimes door lock request continues to
2893 * fail, for example, when no media is present. This
2894 * creates a loop - SCSI EH issues door lock which
2895 * fails and gets invoked again to acquire sense data
2896 * for the failed command.
2897 *
2898 * If door lock fails, always clear sdev->locked to
2899 * avoid this infinite loop.
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002900 *
2901 * This may happen before SCSI scan is complete. Make
2902 * sure qc->dev->sdev isn't NULL before dereferencing.
Tejun Heo22aac082006-08-08 14:08:59 +09002903 */
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002904 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
Tejun Heo22aac082006-08-08 14:08:59 +09002905 qc->dev->sdev->locked = 0;
2906
Tejun Heo246619d2006-05-15 20:58:16 +09002907 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002908 ata_qc_done(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002909 return;
2910 }
2911
2912 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002913 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002915 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002916 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002917 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002918 /* FIXME: not quite right; we don't want the
2919 * translation of taskfile registers into
2920 * a sense descriptors, since that's only
2921 * correct for ATA, not ATAPI
2922 */
Tejun Heo750426a2006-11-14 22:37:35 +09002923 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002924 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 u8 *scsicmd = cmd->cmnd;
2926
Tony Battersbyfd71da42005-12-21 16:35:44 -05002927 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Jeff Garzikb445c562008-02-29 19:10:51 -05002928 unsigned long flags;
Tejun Heo87340e92008-04-28 17:48:51 +09002929 u8 *buf;
Jeff Garzikb445c562008-02-29 19:10:51 -05002930
Tejun Heo87340e92008-04-28 17:48:51 +09002931 buf = ata_scsi_rbuf_get(cmd, true, &flags);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002932
2933 /* ATAPI devices typically report zero for their SCSI version,
2934 * and sometimes deviate from the spec WRT response data
2935 * format. If SCSI version is reported as zero like normal,
2936 * then we make the following fixups: 1) Fake MMC-5 version,
2937 * to indicate to the Linux scsi midlayer this is a modern
2938 * device. 2) Ensure response data format / ATAPI information
2939 * are always correct.
2940 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002941 if (buf[2] == 0) {
2942 buf[2] = 0x5;
2943 buf[3] = 0x32;
2944 }
2945
Tejun Heo87340e92008-04-28 17:48:51 +09002946 ata_scsi_rbuf_put(cmd, true, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 cmd->result = SAM_STAT_GOOD;
2950 }
2951
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002952 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953}
2954/**
2955 * atapi_xlat - Initialize PACKET taskfile
2956 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 *
2958 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002959 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 *
2961 * RETURNS:
2962 * Zero on success, non-zero on failure.
2963 */
Tejun Heoad706992006-12-17 10:45:57 +09002964static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
Tejun Heo542b1442006-12-17 10:45:08 +09002966 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 struct ata_device *dev = qc->dev;
Tejun Heo542b1442006-12-17 10:45:08 +09002968 int nodata = (scmd->sc_data_direction == DMA_NONE);
Tejun Heo5895ef92008-06-17 12:36:26 +09002969 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
Alan Cox2db78dd2007-10-02 13:53:04 -07002970 unsigned int nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Tejun Heo2e5704f2006-12-17 10:46:33 +09002972 memset(qc->cdb, 0, dev->cdb_len);
2973 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 qc->complete_fn = atapi_qc_complete;
2976
2977 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002978 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 qc->tf.flags |= ATA_TFLAG_WRITE;
2980 DPRINTK("direction: write\n");
2981 }
2982
2983 qc->tf.command = ATA_CMD_PACKET;
Tejun Heoaacda372008-03-18 17:47:43 +09002984 ata_qc_set_pc_nbytes(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002986 /* check whether ATAPI DMA is safe */
Tejun Heo5895ef92008-06-17 12:36:26 +09002987 if (!nodata && !using_pio && atapi_check_dma(qc))
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002988 using_pio = 1;
2989
Tejun Heoe1902222007-11-26 20:58:02 +09002990 /* Some controller variants snoop this value for Packet
2991 * transfers to do state machine and FIFO management. Thus we
2992 * want to set it properly, and for DMA where it is
2993 * effectively meaningless.
2994 */
Tejun Heoaacda372008-03-18 17:47:43 +09002995 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
Alan Cox2db78dd2007-10-02 13:53:04 -07002996
Tejun Heoe1902222007-11-26 20:58:02 +09002997 /* Most ATAPI devices which honor transfer chunk size don't
2998 * behave according to the spec when odd chunk size which
2999 * matches the transfer length is specified. If the number of
3000 * bytes to transfer is 2n+1. According to the spec, what
3001 * should happen is to indicate that 2n+1 is going to be
3002 * transferred and transfer 2n+2 bytes where the last byte is
3003 * padding.
3004 *
3005 * In practice, this doesn't happen. ATAPI devices first
3006 * indicate and transfer 2n bytes and then indicate and
3007 * transfer 2 bytes where the last byte is padding.
3008 *
3009 * This inconsistency confuses several controllers which
3010 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
3011 * These controllers use actual number of transferred bytes to
3012 * update DMA poitner and transfer of 4n+2 bytes make those
3013 * controller push DMA pointer by 4n+4 bytes because SATA data
3014 * FISes are aligned to 4 bytes. This causes data corruption
3015 * and buffer overrun.
3016 *
3017 * Always setting nbytes to even number solves this problem
3018 * because then ATAPI devices don't have to split data at 2n
3019 * boundaries.
3020 */
3021 if (nbytes & 0x1)
3022 nbytes++;
3023
Alan Cox2db78dd2007-10-02 13:53:04 -07003024 qc->tf.lbam = (nbytes & 0xFF);
3025 qc->tf.lbah = (nbytes >> 8);
3026
Tejun Heo5895ef92008-06-17 12:36:26 +09003027 if (nodata)
3028 qc->tf.protocol = ATAPI_PROT_NODATA;
3029 else if (using_pio)
3030 qc->tf.protocol = ATAPI_PROT_PIO;
3031 else {
Tejun Heoe00f1ff2007-06-27 02:47:35 +09003032 /* DMA data xfer */
Tejun Heo0dc36882007-12-18 16:34:43 -05003033 qc->tf.protocol = ATAPI_PROT_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 qc->tf.feature |= ATAPI_PKT_DMA;
3035
Tejun Heo91163002008-02-21 13:25:50 +09003036 if ((dev->flags & ATA_DFLAG_DMADIR) &&
3037 (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08003038 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
3041
Alan Cox2db78dd2007-10-02 13:53:04 -07003042
3043 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
3044 as ATAPI tape drives don't get this right otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return 0;
3046}
3047
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003048static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
Tejun Heoab5b3a52006-05-31 18:27:34 +09003049{
Tejun Heo071f44b2008-04-07 22:47:22 +09003050 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003051 if (likely(devno < ata_link_max_devices(&ap->link)))
3052 return &ap->link.device[devno];
3053 } else {
3054 if (likely(devno < ap->nr_pmp_links))
3055 return &ap->pmp_link[devno].device[0];
3056 }
3057
Tejun Heoab5b3a52006-05-31 18:27:34 +09003058 return NULL;
3059}
3060
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003061static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
3062 const struct scsi_device *scsidev)
Tejun Heoab5b3a52006-05-31 18:27:34 +09003063{
Tejun Heo41bda9c2007-08-06 18:36:24 +09003064 int devno;
Tejun Heoab5b3a52006-05-31 18:27:34 +09003065
Tejun Heo41bda9c2007-08-06 18:36:24 +09003066 /* skip commands not addressed to targets we simulate */
Tejun Heo071f44b2008-04-07 22:47:22 +09003067 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003068 if (unlikely(scsidev->channel || scsidev->lun))
3069 return NULL;
3070 devno = scsidev->id;
3071 } else {
3072 if (unlikely(scsidev->id || scsidev->lun))
3073 return NULL;
3074 devno = scsidev->channel;
3075 }
3076
3077 return ata_find_dev(ap, devno);
Tejun Heoab5b3a52006-05-31 18:27:34 +09003078}
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080/**
3081 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
3082 * @ap: ATA port to which the device is attached
3083 * @scsidev: SCSI device from which we derive the ATA device
3084 *
3085 * Given various information provided in struct scsi_cmnd,
3086 * map that onto an ATA bus, and using that mapping
3087 * determine which ata_device is associated with the
3088 * SCSI command to be sent.
3089 *
3090 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003091 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 *
3093 * RETURNS:
3094 * Associated ATA device, or %NULL if not found.
3095 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04003097ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Tejun Heoab5b3a52006-05-31 18:27:34 +09003099 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Tejun Heo2486fa52008-07-31 07:52:40 +09003101 if (unlikely(!dev || !ata_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return NULL;
3103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return dev;
3105}
3106
Jeff Garzikb0955182005-05-12 15:45:22 -04003107/*
3108 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
3109 * @byte1: Byte 1 from pass-thru CDB.
3110 *
3111 * RETURNS:
3112 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
3113 */
3114static u8
3115ata_scsi_map_proto(u8 byte1)
3116{
3117 switch((byte1 & 0x1e) >> 1) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003118 case 3: /* Non-data */
3119 return ATA_PROT_NODATA;
Jeff Garzikb0955182005-05-12 15:45:22 -04003120
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003121 case 6: /* DMA */
3122 case 10: /* UDMA Data-in */
3123 case 11: /* UDMA Data-Out */
3124 return ATA_PROT_DMA;
Jeff Garzikb0955182005-05-12 15:45:22 -04003125
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003126 case 4: /* PIO Data-in */
3127 case 5: /* PIO Data-out */
3128 return ATA_PROT_PIO;
Jeff Garzikb0955182005-05-12 15:45:22 -04003129
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303130 case 12: /* FPDMA */
3131 return ATA_PROT_NCQ;
3132
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003133 case 0: /* Hard Reset */
3134 case 1: /* SRST */
3135 case 8: /* Device Diagnostic */
3136 case 9: /* Device Reset */
3137 case 7: /* DMA Queued */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003138 case 15: /* Return Response Info */
3139 default: /* Reserved */
3140 break;
Jeff Garzikb0955182005-05-12 15:45:22 -04003141 }
3142
3143 return ATA_PROT_UNKNOWN;
3144}
3145
3146/**
3147 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
3148 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04003149 *
3150 * Handles either 12 or 16-byte versions of the CDB.
3151 *
3152 * RETURNS:
3153 * Zero on success, non-zero on failure.
3154 */
Tejun Heoad706992006-12-17 10:45:57 +09003155static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04003156{
3157 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09003158 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01003159 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09003160 const u8 *cdb = scmd->cmnd;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003161 u16 fp;
Jeff Garzikb0955182005-05-12 15:45:22 -04003162
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003163 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN) {
3164 fp = 1;
Tejun Heo9a405252005-12-02 11:49:11 +09003165 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003166 }
Jeff Garzik8190bdb2006-05-24 01:53:39 -04003167
Hannes Reinecke5b844b62016-07-14 09:05:48 +09003168 if (ata_is_ncq(tf->protocol) && (cdb[2] & 0x3) == 0)
3169 tf->protocol = ATA_PROT_NCQ_NODATA;
3170
Vinayak Kale5f8e7f12015-10-27 13:11:46 +05303171 /* enable LBA */
3172 tf->flags |= ATA_TFLAG_LBA;
3173
Alan Coxae8d4ee2007-11-04 22:05:49 -05003174 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04003175 * 12 and 16 byte CDBs use different offsets to
3176 * provide the various register values.
3177 */
Tejun Heo542b1442006-12-17 10:45:08 +09003178 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04003179 /*
3180 * 16-byte CDB - may contain extended commands.
3181 *
3182 * If that is the case, copy the upper byte register values.
3183 */
Tejun Heo542b1442006-12-17 10:45:08 +09003184 if (cdb[1] & 0x01) {
3185 tf->hob_feature = cdb[3];
3186 tf->hob_nsect = cdb[5];
3187 tf->hob_lbal = cdb[7];
3188 tf->hob_lbam = cdb[9];
3189 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04003190 tf->flags |= ATA_TFLAG_LBA48;
3191 } else
3192 tf->flags &= ~ATA_TFLAG_LBA48;
3193
3194 /*
3195 * Always copy low byte, device and command registers.
3196 */
Tejun Heo542b1442006-12-17 10:45:08 +09003197 tf->feature = cdb[4];
3198 tf->nsect = cdb[6];
3199 tf->lbal = cdb[8];
3200 tf->lbam = cdb[10];
3201 tf->lbah = cdb[12];
3202 tf->device = cdb[13];
3203 tf->command = cdb[14];
Jeff Garzikb0955182005-05-12 15:45:22 -04003204 } else {
3205 /*
3206 * 12-byte CDB - incapable of extended commands.
3207 */
3208 tf->flags &= ~ATA_TFLAG_LBA48;
3209
Tejun Heo542b1442006-12-17 10:45:08 +09003210 tf->feature = cdb[3];
3211 tf->nsect = cdb[4];
3212 tf->lbal = cdb[5];
3213 tf->lbam = cdb[6];
3214 tf->lbah = cdb[7];
3215 tf->device = cdb[8];
3216 tf->command = cdb[9];
Jeff Garzikb0955182005-05-12 15:45:22 -04003217 }
Albert Leefa4453c2007-06-07 15:52:07 +08003218
Hannes Reinecke179b3102016-07-14 09:05:43 +09003219 /* For NCQ commands copy the tag value */
3220 if (ata_is_ncq(tf->protocol))
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303221 tf->nsect = qc->tag << 3;
3222
Albert Leefa4453c2007-06-07 15:52:07 +08003223 /* enforce correct master/slave bit */
3224 tf->device = dev->devno ?
3225 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003226
Tejun Heobd30add2009-09-03 16:08:11 +09003227 switch (tf->command) {
Grant Grundler295124d2010-08-17 10:56:53 -07003228 /* READ/WRITE LONG use a non-standard sect_size */
Tejun Heobd30add2009-09-03 16:08:11 +09003229 case ATA_CMD_READ_LONG:
3230 case ATA_CMD_READ_LONG_ONCE:
3231 case ATA_CMD_WRITE_LONG:
3232 case ATA_CMD_WRITE_LONG_ONCE:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003233 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
3234 fp = 1;
Tejun Heobd30add2009-09-03 16:08:11 +09003235 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003236 }
Tejun Heobd30add2009-09-03 16:08:11 +09003237 qc->sect_size = scsi_bufflen(scmd);
Grant Grundler295124d2010-08-17 10:56:53 -07003238 break;
3239
3240 /* commands using reported Logical Block size (e.g. 512 or 4K) */
3241 case ATA_CMD_CFA_WRITE_NE:
3242 case ATA_CMD_CFA_TRANS_SECT:
3243 case ATA_CMD_CFA_WRITE_MULT_NE:
3244 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
3245 case ATA_CMD_READ:
3246 case ATA_CMD_READ_EXT:
3247 case ATA_CMD_READ_QUEUED:
3248 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
3249 case ATA_CMD_FPDMA_READ:
3250 case ATA_CMD_READ_MULTI:
3251 case ATA_CMD_READ_MULTI_EXT:
3252 case ATA_CMD_PIO_READ:
3253 case ATA_CMD_PIO_READ_EXT:
3254 case ATA_CMD_READ_STREAM_DMA_EXT:
3255 case ATA_CMD_READ_STREAM_EXT:
3256 case ATA_CMD_VERIFY:
3257 case ATA_CMD_VERIFY_EXT:
3258 case ATA_CMD_WRITE:
3259 case ATA_CMD_WRITE_EXT:
3260 case ATA_CMD_WRITE_FUA_EXT:
3261 case ATA_CMD_WRITE_QUEUED:
3262 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3263 case ATA_CMD_FPDMA_WRITE:
3264 case ATA_CMD_WRITE_MULTI:
3265 case ATA_CMD_WRITE_MULTI_EXT:
3266 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3267 case ATA_CMD_PIO_WRITE:
3268 case ATA_CMD_PIO_WRITE_EXT:
3269 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3270 case ATA_CMD_WRITE_STREAM_EXT:
3271 qc->sect_size = scmd->device->sector_size;
3272 break;
3273
3274 /* Everything else uses 512 byte "sectors" */
3275 default:
3276 qc->sect_size = ATA_SECT_SIZE;
Tejun Heobd30add2009-09-03 16:08:11 +09003277 }
3278
3279 /*
3280 * Set flags so that all registers will be written, pass on
3281 * write indication (used for PIO/DMA setup), result TF is
3282 * copied back and we don't whine too much about its failure.
3283 */
Douglas Gilbertbc496ed2010-02-01 13:11:38 -05003284 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heobd30add2009-09-03 16:08:11 +09003285 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3286 tf->flags |= ATA_TFLAG_WRITE;
3287
3288 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3289
3290 /*
3291 * Set transfer length.
3292 *
3293 * TODO: find out if we need to do more here to
3294 * cover scatter/gather case.
3295 */
3296 ata_qc_set_pc_nbytes(qc);
3297
3298 /* We may not issue DMA commands if no DMA mode is set */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003299 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) {
3300 fp = 1;
Tejun Heobd30add2009-09-03 16:08:11 +09003301 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003302 }
Tejun Heobd30add2009-09-03 16:08:11 +09003303
Albert Lee1dce5892007-06-07 15:49:22 +08003304 /* sanity check for pio multi commands */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003305 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
3306 fp = 1;
Albert Lee1dce5892007-06-07 15:49:22 +08003307 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003308 }
Albert Lee1dce5892007-06-07 15:49:22 +08003309
3310 if (is_multi_taskfile(tf)) {
3311 unsigned int multi_count = 1 << (cdb[1] >> 5);
3312
3313 /* compare the passed through multi_count
3314 * with the cached multi_count of libata
3315 */
3316 if (multi_count != dev->multi_count)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003317 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3318 multi_count);
Alan Coxd26fc952007-07-06 19:13:52 -04003319 }
Albert Lee1dce5892007-06-07 15:49:22 +08003320
Jeff Garzikb0955182005-05-12 15:45:22 -04003321 /*
3322 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3323 * SET_FEATURES - XFER MODE must be preceded/succeeded
3324 * by an update to hardware-specific registers for each
3325 * controller (i.e. the reason for ->set_piomode(),
3326 * ->set_dmamode(), and ->post_set_mode() hooks).
3327 */
Tejun Heobd30add2009-09-03 16:08:11 +09003328 if (tf->command == ATA_CMD_SET_FEATURES &&
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003329 tf->feature == SETFEATURES_XFER) {
3330 fp = (cdb[0] == ATA_16) ? 4 : 3;
Tejun Heo9a405252005-12-02 11:49:11 +09003331 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003332 }
Jeff Garzikb0955182005-05-12 15:45:22 -04003333
3334 /*
Tejun Heobd30add2009-09-03 16:08:11 +09003335 * Filter TPM commands by default. These provide an
3336 * essentially uncontrolled encrypted "back door" between
3337 * applications and the disk. Set libata.allow_tpm=1 if you
3338 * have a real reason for wanting to use them. This ensures
3339 * that installed software cannot easily mess stuff up without
3340 * user intent. DVR type users will probably ship with this enabled
3341 * for movie content management.
Jeff Garzikb0955182005-05-12 15:45:22 -04003342 *
Tejun Heobd30add2009-09-03 16:08:11 +09003343 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3344 * for this and should do in future but that it is not sufficient as
3345 * DCS is an optional feature set. Thus we also do the software filter
3346 * so that we comply with the TC consortium stated goal that the user
3347 * can turn off TC features of their system.
Jeff Garzikb0955182005-05-12 15:45:22 -04003348 */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003349 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
3350 fp = (cdb[0] == ATA_16) ? 14 : 9;
Tejun Heobd30add2009-09-03 16:08:11 +09003351 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003352 }
Tejun Heoe61e0672006-05-15 20:57:40 +09003353
Jeff Garzikb0955182005-05-12 15:45:22 -04003354 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09003355
3356 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003357 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
Tejun Heo9a405252005-12-02 11:49:11 +09003358 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003359}
3360
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003361/**
3362 * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim
3363 * @cmd: SCSI command being translated
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003364 * @trmax: Maximum number of entries that will fit in sector_size bytes.
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003365 * @sector: Starting sector
Shaun Tancheff7b203092016-08-21 23:23:19 -05003366 * @count: Total Range of request in logical sectors
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003367 *
3368 * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted
3369 * descriptor.
3370 *
3371 * Upto 64 entries of the format:
3372 * 63:48 Range Length
3373 * 47:0 LBA
3374 *
3375 * Range Length of 0 is ignored.
3376 * LBA's should be sorted order and not overlap.
3377 *
3378 * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003379 *
3380 * Return: Number of bytes copied into sglist.
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003381 */
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003382static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax,
3383 u64 sector, u32 count)
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003384{
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003385 struct scsi_device *sdp = cmd->device;
3386 size_t len = sdp->sector_size;
3387 size_t r;
3388 __le64 *buf;
3389 u32 i = 0;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003390 unsigned long flags;
3391
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003392 WARN_ON(len > ATA_SCSI_RBUF_SIZE);
3393
3394 if (len > ATA_SCSI_RBUF_SIZE)
3395 len = ATA_SCSI_RBUF_SIZE;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003396
3397 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003398 buf = ((void *)ata_scsi_rbuf);
3399 memset(buf, 0, len);
3400 while (i < trmax) {
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003401 u64 entry = sector |
3402 ((u64)(count > 0xffff ? 0xffff : count) << 48);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003403 buf[i++] = __cpu_to_le64(entry);
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003404 if (count <= 0xffff)
3405 break;
3406 count -= 0xffff;
3407 sector += 0xffff;
3408 }
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003409 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003410 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
3411
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003412 return r;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003413}
3414
Shaun Tancheff7b203092016-08-21 23:23:19 -05003415/**
3416 * ata_format_dsm_trim_descr() - SATL Write Same to ATA SCT Write Same
3417 * @cmd: SCSI command being translated
3418 * @lba: Starting sector
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003419 * @num: Number of sectors to be zero'd.
Shaun Tancheff7b203092016-08-21 23:23:19 -05003420 *
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003421 * Rewrite the WRITE SAME payload to be an SCT Write Same formatted
Shaun Tancheff7b203092016-08-21 23:23:19 -05003422 * descriptor.
3423 * NOTE: Writes a pattern (0's) in the foreground.
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003424 *
3425 * Return: Number of bytes copied into sglist.
Shaun Tancheff7b203092016-08-21 23:23:19 -05003426 */
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003427static size_t ata_format_sct_write_same(struct scsi_cmnd *cmd, u64 lba, u64 num)
Shaun Tancheff7b203092016-08-21 23:23:19 -05003428{
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003429 struct scsi_device *sdp = cmd->device;
3430 size_t len = sdp->sector_size;
3431 size_t r;
3432 u16 *buf;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003433 unsigned long flags;
3434
3435 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003436 buf = ((void *)ata_scsi_rbuf);
Shaun Tancheff7b203092016-08-21 23:23:19 -05003437
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003438 put_unaligned_le16(0x0002, &buf[0]); /* SCT_ACT_WRITE_SAME */
3439 put_unaligned_le16(0x0101, &buf[1]); /* WRITE PTRN FG */
3440 put_unaligned_le64(lba, &buf[2]);
3441 put_unaligned_le64(num, &buf[6]);
3442 put_unaligned_le32(0u, &buf[10]); /* pattern */
Shaun Tancheff7b203092016-08-21 23:23:19 -05003443
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003444 WARN_ON(len > ATA_SCSI_RBUF_SIZE);
3445
3446 if (len > ATA_SCSI_RBUF_SIZE)
3447 len = ATA_SCSI_RBUF_SIZE;
3448
3449 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
Shaun Tancheff7b203092016-08-21 23:23:19 -05003450 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003451
3452 return r;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003453}
3454
3455/**
3456 * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same
3457 * @qc: Command to be translated
3458 *
3459 * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or
3460 * an SCT Write Same command.
3461 * Based on WRITE SAME has the UNMAP flag
3462 * When set translate to DSM TRIM
3463 * When clear translate to SCT Write Same
3464 */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003465static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3466{
3467 struct ata_taskfile *tf = &qc->tf;
3468 struct scsi_cmnd *scmd = qc->scsicmd;
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003469 struct scsi_device *sdp = scmd->device;
3470 size_t len = sdp->sector_size;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003471 struct ata_device *dev = qc->dev;
3472 const u8 *cdb = scmd->cmnd;
3473 u64 block;
3474 u32 n_block;
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003475 const u32 trmax = len >> 3;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003476 u32 size;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003477 u16 fp;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003478 u8 bp = 0xff;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003479 u8 unmap = cdb[1] & 0x8;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003480
3481 /* we may not issue DMA commands if no DMA mode is set */
3482 if (unlikely(!dev->dma_mode))
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003483 goto invalid_opcode;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003484
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003485 if (unlikely(scmd->cmd_len < 16)) {
3486 fp = 15;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003487 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003488 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003489 scsi_16_lba_len(cdb, &block, &n_block);
3490
Shaun Tancheff7b203092016-08-21 23:23:19 -05003491 if (unmap) {
3492 /* If trim is not enabled the cmd is invalid. */
3493 if ((dev->horkage & ATA_HORKAGE_NOTRIM) ||
3494 !ata_id_has_trim(dev->id)) {
3495 fp = 1;
3496 bp = 3;
3497 goto invalid_fld;
3498 }
3499 /* If the request is too large the cmd is invalid */
3500 if (n_block > 0xffff * trmax) {
3501 fp = 2;
3502 goto invalid_fld;
3503 }
3504 } else {
3505 /* If write same is not available the cmd is invalid */
3506 if (!ata_id_sct_write_same(dev->id)) {
3507 fp = 1;
3508 bp = 3;
3509 goto invalid_fld;
3510 }
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003511 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003512
3513 /*
3514 * WRITE SAME always has a sector sized buffer as payload, this
3515 * should never be a multiple entry S/G list.
3516 */
3517 if (!scsi_sg_count(scmd))
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003518 goto invalid_param_len;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003519
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003520 /*
3521 * size must match sector size in bytes
3522 * For DATA SET MANAGEMENT TRIM in ACS-2 nsect (aka count)
3523 * is defined as number of 512 byte blocks to be transferred.
3524 */
Shaun Tancheff7b203092016-08-21 23:23:19 -05003525 if (unmap) {
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003526 size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003527 if (size != len)
3528 goto invalid_param_len;
3529
Shaun Tancheff7b203092016-08-21 23:23:19 -05003530 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3531 /* Newer devices support queued TRIM commands */
3532 tf->protocol = ATA_PROT_NCQ;
3533 tf->command = ATA_CMD_FPDMA_SEND;
3534 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
3535 tf->nsect = qc->tag << 3;
3536 tf->hob_feature = (size / 512) >> 8;
3537 tf->feature = size / 512;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003538
Shaun Tancheff7b203092016-08-21 23:23:19 -05003539 tf->auxiliary = 1;
3540 } else {
3541 tf->protocol = ATA_PROT_DMA;
3542 tf->hob_feature = 0;
3543 tf->feature = ATA_DSM_TRIM;
3544 tf->hob_nsect = (size / 512) >> 8;
3545 tf->nsect = size / 512;
3546 tf->command = ATA_CMD_DSM;
3547 }
Marc Carino87fb6c32013-08-24 23:22:51 -07003548 } else {
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003549 size = ata_format_sct_write_same(scmd, block, n_block);
3550 if (size != len)
3551 goto invalid_param_len;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003552
Marc Carino87fb6c32013-08-24 23:22:51 -07003553 tf->hob_feature = 0;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003554 tf->feature = 0;
3555 tf->hob_nsect = 0;
3556 tf->nsect = 1;
3557 tf->lbah = 0;
3558 tf->lbam = 0;
3559 tf->lbal = ATA_CMD_STANDBYNOW1;
3560 tf->hob_lbah = 0;
3561 tf->hob_lbam = 0;
3562 tf->hob_lbal = 0;
3563 tf->device = ATA_CMD_STANDBYNOW1;
3564 tf->protocol = ATA_PROT_DMA;
3565 tf->command = ATA_CMD_WRITE_LOG_DMA_EXT;
Shaun Tancheff7d36dd02016-08-21 23:23:21 -05003566 if (unlikely(dev->flags & ATA_DFLAG_PIO))
3567 tf->command = ATA_CMD_WRITE_LOG_EXT;
Marc Carino87fb6c32013-08-24 23:22:51 -07003568 }
3569
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003570 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3571 ATA_TFLAG_WRITE;
3572
3573 ata_qc_set_pc_nbytes(qc);
3574
3575 return 0;
3576
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003577invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003578 ata_scsi_set_invalid_field(dev, scmd, fp, bp);
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003579 return 1;
3580invalid_param_len:
3581 /* "Parameter list length error" */
3582 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3583 return 1;
3584invalid_opcode:
3585 /* "Invalid command operation code" */
3586 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003587 return 1;
3588}
3589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590/**
Shaun Tancheff7b203092016-08-21 23:23:19 -05003591 * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN
3592 * @args: device MAINTENANCE_IN data / SCSI command of interest.
3593 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
3594 *
3595 * Yields a subset to satisfy scsi_report_opcode()
3596 *
3597 * LOCKING:
3598 * spin_lock_irqsave(host lock)
3599 */
3600static unsigned int ata_scsiop_maint_in(struct ata_scsi_args *args, u8 *rbuf)
3601{
3602 struct ata_device *dev = args->dev;
3603 u8 *cdb = args->cmd->cmnd;
3604 u8 supported = 0;
3605 unsigned int err = 0;
3606
3607 if (cdb[2] != 1) {
3608 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
3609 err = 2;
3610 goto out;
3611 }
3612 switch (cdb[3]) {
3613 case INQUIRY:
3614 case MODE_SENSE:
3615 case MODE_SENSE_10:
3616 case READ_CAPACITY:
3617 case SERVICE_ACTION_IN_16:
3618 case REPORT_LUNS:
3619 case REQUEST_SENSE:
3620 case SYNCHRONIZE_CACHE:
3621 case REZERO_UNIT:
3622 case SEEK_6:
3623 case SEEK_10:
3624 case TEST_UNIT_READY:
3625 case SEND_DIAGNOSTIC:
3626 case MAINTENANCE_IN:
3627 case READ_6:
3628 case READ_10:
3629 case READ_16:
3630 case WRITE_6:
3631 case WRITE_10:
3632 case WRITE_16:
3633 case ATA_12:
3634 case ATA_16:
3635 case VERIFY:
3636 case VERIFY_16:
3637 case MODE_SELECT:
3638 case MODE_SELECT_10:
3639 case START_STOP:
3640 supported = 3;
3641 break;
3642 case WRITE_SAME_16:
Shaun Tancheff0ce1b182016-09-09 11:44:19 -05003643 if (!ata_id_sct_write_same(dev->id))
3644 break;
3645 /* fallthrough: if SCT ... only enable for ZBC */
Shaun Tancheff7b203092016-08-21 23:23:19 -05003646 case ZBC_IN:
3647 case ZBC_OUT:
3648 if (ata_id_zoned_cap(dev->id) ||
3649 dev->class == ATA_DEV_ZAC)
3650 supported = 3;
3651 break;
3652 default:
3653 break;
3654 }
3655out:
3656 rbuf[1] = supported; /* supported */
3657 return err;
3658}
3659
3660/**
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003661 * ata_scsi_report_zones_complete - convert ATA output
3662 * @qc: command structure returning the data
3663 *
3664 * Convert T-13 little-endian field representation into
3665 * T-10 big-endian field representation.
3666 * What a mess.
3667 */
3668static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
3669{
3670 struct scsi_cmnd *scmd = qc->scsicmd;
3671 struct sg_mapping_iter miter;
3672 unsigned long flags;
3673 unsigned int bytes = 0;
3674
3675 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
3676 SG_MITER_TO_SG | SG_MITER_ATOMIC);
3677
3678 local_irq_save(flags);
3679 while (sg_miter_next(&miter)) {
3680 unsigned int offset = 0;
3681
3682 if (bytes == 0) {
3683 char *hdr;
3684 u32 list_length;
3685 u64 max_lba, opt_lba;
3686 u16 same;
3687
3688 /* Swizzle header */
3689 hdr = miter.addr;
3690 list_length = get_unaligned_le32(&hdr[0]);
3691 same = get_unaligned_le16(&hdr[4]);
3692 max_lba = get_unaligned_le64(&hdr[8]);
3693 opt_lba = get_unaligned_le64(&hdr[16]);
3694 put_unaligned_be32(list_length, &hdr[0]);
3695 hdr[4] = same & 0xf;
3696 put_unaligned_be64(max_lba, &hdr[8]);
3697 put_unaligned_be64(opt_lba, &hdr[16]);
3698 offset += 64;
3699 bytes += 64;
3700 }
3701 while (offset < miter.length) {
3702 char *rec;
3703 u8 cond, type, non_seq, reset;
3704 u64 size, start, wp;
3705
3706 /* Swizzle zone descriptor */
3707 rec = miter.addr + offset;
3708 type = rec[0] & 0xf;
3709 cond = (rec[1] >> 4) & 0xf;
3710 non_seq = (rec[1] & 2);
3711 reset = (rec[1] & 1);
3712 size = get_unaligned_le64(&rec[8]);
3713 start = get_unaligned_le64(&rec[16]);
3714 wp = get_unaligned_le64(&rec[24]);
3715 rec[0] = type;
3716 rec[1] = (cond << 4) | non_seq | reset;
3717 put_unaligned_be64(size, &rec[8]);
3718 put_unaligned_be64(start, &rec[16]);
3719 put_unaligned_be64(wp, &rec[24]);
3720 WARN_ON(offset + 64 > miter.length);
3721 offset += 64;
3722 bytes += 64;
3723 }
3724 }
3725 sg_miter_stop(&miter);
3726 local_irq_restore(flags);
3727
3728 ata_scsi_qc_complete(qc);
3729}
3730
3731static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
3732{
3733 struct ata_taskfile *tf = &qc->tf;
3734 struct scsi_cmnd *scmd = qc->scsicmd;
3735 const u8 *cdb = scmd->cmnd;
3736 u16 sect, fp = (u16)-1;
3737 u8 sa, options, bp = 0xff;
3738 u64 block;
3739 u32 n_block;
3740
3741 if (unlikely(scmd->cmd_len < 16)) {
3742 ata_dev_warn(qc->dev, "invalid cdb length %d\n",
3743 scmd->cmd_len);
3744 fp = 15;
3745 goto invalid_fld;
3746 }
3747 scsi_16_lba_len(cdb, &block, &n_block);
3748 if (n_block != scsi_bufflen(scmd)) {
3749 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
3750 n_block, scsi_bufflen(scmd));
3751 goto invalid_param_len;
3752 }
3753 sa = cdb[1] & 0x1f;
3754 if (sa != ZI_REPORT_ZONES) {
3755 ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
3756 fp = 1;
3757 goto invalid_fld;
3758 }
3759 /*
3760 * ZAC allows only for transfers in 512 byte blocks,
3761 * and uses a 16 bit value for the transfer count.
3762 */
3763 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
3764 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
3765 goto invalid_param_len;
3766 }
3767 sect = n_block / 512;
Damien Le Moal3f174222016-07-14 09:05:49 +09003768 options = cdb[14] & 0xbf;
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003769
3770 if (ata_ncq_enabled(qc->dev) &&
3771 ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
3772 tf->protocol = ATA_PROT_NCQ;
3773 tf->command = ATA_CMD_FPDMA_RECV;
3774 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
3775 tf->nsect = qc->tag << 3;
3776 tf->feature = sect & 0xff;
3777 tf->hob_feature = (sect >> 8) & 0xff;
Damien Le Moal3f174222016-07-14 09:05:49 +09003778 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003779 } else {
3780 tf->command = ATA_CMD_ZAC_MGMT_IN;
3781 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
3782 tf->protocol = ATA_PROT_DMA;
3783 tf->hob_feature = options;
3784 tf->hob_nsect = (sect >> 8) & 0xff;
3785 tf->nsect = sect & 0xff;
3786 }
3787 tf->device = ATA_LBA;
3788 tf->lbah = (block >> 16) & 0xff;
3789 tf->lbam = (block >> 8) & 0xff;
3790 tf->lbal = block & 0xff;
3791 tf->hob_lbah = (block >> 40) & 0xff;
3792 tf->hob_lbam = (block >> 32) & 0xff;
3793 tf->hob_lbal = (block >> 24) & 0xff;
3794
3795 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3796 qc->flags |= ATA_QCFLAG_RESULT_TF;
3797
3798 ata_qc_set_pc_nbytes(qc);
3799
3800 qc->complete_fn = ata_scsi_report_zones_complete;
3801
3802 return 0;
3803
3804invalid_fld:
3805 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
3806 return 1;
3807
3808invalid_param_len:
3809 /* "Parameter list length error" */
3810 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3811 return 1;
3812}
3813
Hannes Reinecke27708a92016-04-25 12:45:53 +02003814static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
3815{
3816 struct ata_taskfile *tf = &qc->tf;
3817 struct scsi_cmnd *scmd = qc->scsicmd;
3818 struct ata_device *dev = qc->dev;
3819 const u8 *cdb = scmd->cmnd;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003820 u8 all, sa;
Hannes Reinecke27708a92016-04-25 12:45:53 +02003821 u64 block;
3822 u32 n_block;
3823 u16 fp = (u16)-1;
3824
3825 if (unlikely(scmd->cmd_len < 16)) {
3826 fp = 15;
3827 goto invalid_fld;
3828 }
3829
3830 sa = cdb[1] & 0x1f;
3831 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
3832 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
3833 fp = 1;
3834 goto invalid_fld;
3835 }
3836
3837 scsi_16_lba_len(cdb, &block, &n_block);
3838 if (n_block) {
3839 /*
3840 * ZAC MANAGEMENT OUT doesn't define any length
3841 */
3842 goto invalid_param_len;
3843 }
3844 if (block > dev->n_sectors)
3845 goto out_of_range;
3846
Damien Le Moal2950cef2016-07-14 09:05:51 +09003847 all = cdb[14] & 0x1;
Hannes Reinecke27708a92016-04-25 12:45:53 +02003848
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003849 if (ata_ncq_enabled(qc->dev) &&
3850 ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
Hannes Reinecke5b844b62016-07-14 09:05:48 +09003851 tf->protocol = ATA_PROT_NCQ_NODATA;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003852 tf->command = ATA_CMD_NCQ_NON_DATA;
Damien Le Moalee194b72016-07-14 09:05:50 +09003853 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003854 tf->nsect = qc->tag << 3;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003855 tf->auxiliary = sa | ((u16)all << 8);
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003856 } else {
3857 tf->protocol = ATA_PROT_NODATA;
3858 tf->command = ATA_CMD_ZAC_MGMT_OUT;
3859 tf->feature = sa;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003860 tf->hob_feature = all;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003861 }
Hannes Reinecke27708a92016-04-25 12:45:53 +02003862 tf->lbah = (block >> 16) & 0xff;
3863 tf->lbam = (block >> 8) & 0xff;
3864 tf->lbal = block & 0xff;
3865 tf->hob_lbah = (block >> 40) & 0xff;
3866 tf->hob_lbam = (block >> 32) & 0xff;
3867 tf->hob_lbal = (block >> 24) & 0xff;
3868 tf->device = ATA_LBA;
3869 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3870
3871 return 0;
3872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 invalid_fld:
Hannes Reinecke27708a92016-04-25 12:45:53 +02003874 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
3875 return 1;
3876 out_of_range:
3877 /* "Logical Block Address out of range" */
3878 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x00);
3879 return 1;
3880invalid_param_len:
3881 /* "Parameter list length error" */
3882 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 return 1;
3884}
3885
3886/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003887 * ata_mselect_caching - Simulate MODE SELECT for caching info page
3888 * @qc: Storage for translated ATA taskfile
3889 * @buf: input buffer
3890 * @len: number of valid bytes in the input buffer
Hannes Reinecke77800812016-04-04 11:44:07 +02003891 * @fp: out parameter for the failed field on error
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003892 *
3893 * Prepare a taskfile to modify caching information for the device.
3894 *
3895 * LOCKING:
3896 * None.
3897 */
3898static int ata_mselect_caching(struct ata_queued_cmd *qc,
Hannes Reinecke77800812016-04-04 11:44:07 +02003899 const u8 *buf, int len, u16 *fp)
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003900{
3901 struct ata_taskfile *tf = &qc->tf;
3902 struct ata_device *dev = qc->dev;
Tom Yand7372cb2016-07-23 02:34:08 +08003903 u8 mpage[CACHE_MPAGE_LEN];
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003904 u8 wce;
Hannes Reinecke77800812016-04-04 11:44:07 +02003905 int i;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003906
3907 /*
3908 * The first two bytes of def_cache_mpage are a header, so offsets
3909 * in mpage are off by 2 compared to buf. Same for len.
3910 */
3911
Hannes Reinecke77800812016-04-04 11:44:07 +02003912 if (len != CACHE_MPAGE_LEN - 2) {
3913 if (len < CACHE_MPAGE_LEN - 2)
3914 *fp = len;
3915 else
3916 *fp = CACHE_MPAGE_LEN - 2;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003917 return -EINVAL;
Hannes Reinecke77800812016-04-04 11:44:07 +02003918 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003919
3920 wce = buf[0] & (1 << 2);
3921
3922 /*
3923 * Check that read-only bits are not modified.
3924 */
3925 ata_msense_caching(dev->id, mpage, false);
Hannes Reinecke77800812016-04-04 11:44:07 +02003926 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
3927 if (i == 0)
3928 continue;
3929 if (mpage[i + 2] != buf[i]) {
3930 *fp = i;
3931 return -EINVAL;
3932 }
3933 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003934
3935 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3936 tf->protocol = ATA_PROT_NODATA;
3937 tf->nsect = 0;
3938 tf->command = ATA_CMD_SET_FEATURES;
3939 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3940 return 0;
3941}
3942
3943/**
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003944 * ata_mselect_control - Simulate MODE SELECT for control page
3945 * @qc: Storage for translated ATA taskfile
3946 * @buf: input buffer
3947 * @len: number of valid bytes in the input buffer
Hannes Reinecke77800812016-04-04 11:44:07 +02003948 * @fp: out parameter for the failed field on error
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003949 *
3950 * Prepare a taskfile to modify caching information for the device.
3951 *
3952 * LOCKING:
3953 * None.
3954 */
3955static int ata_mselect_control(struct ata_queued_cmd *qc,
Hannes Reinecke77800812016-04-04 11:44:07 +02003956 const u8 *buf, int len, u16 *fp)
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003957{
3958 struct ata_device *dev = qc->dev;
Tom Yand7372cb2016-07-23 02:34:08 +08003959 u8 mpage[CONTROL_MPAGE_LEN];
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003960 u8 d_sense;
Hannes Reinecke77800812016-04-04 11:44:07 +02003961 int i;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003962
3963 /*
3964 * The first two bytes of def_control_mpage are a header, so offsets
3965 * in mpage are off by 2 compared to buf. Same for len.
3966 */
3967
Hannes Reinecke77800812016-04-04 11:44:07 +02003968 if (len != CONTROL_MPAGE_LEN - 2) {
3969 if (len < CONTROL_MPAGE_LEN - 2)
3970 *fp = len;
3971 else
3972 *fp = CONTROL_MPAGE_LEN - 2;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003973 return -EINVAL;
Hannes Reinecke77800812016-04-04 11:44:07 +02003974 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003975
3976 d_sense = buf[0] & (1 << 2);
3977
3978 /*
3979 * Check that read-only bits are not modified.
3980 */
Tom Yanf086b742016-07-13 02:54:12 +08003981 ata_msense_control(dev, mpage, false);
Hannes Reinecke77800812016-04-04 11:44:07 +02003982 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
3983 if (i == 0)
3984 continue;
3985 if (mpage[2 + i] != buf[i]) {
3986 *fp = i;
3987 return -EINVAL;
3988 }
3989 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003990 if (d_sense & (1 << 2))
3991 dev->flags |= ATA_DFLAG_D_SENSE;
3992 else
3993 dev->flags &= ~ATA_DFLAG_D_SENSE;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003994 return 0;
3995}
3996
3997/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003998 * ata_scsiop_mode_select - Simulate MODE SELECT 6, 10 commands
3999 * @qc: Storage for translated ATA taskfile
4000 *
4001 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
4002 * Assume this is invoked for direct access devices (e.g. disks) only.
4003 * There should be no block descriptor for other device types.
4004 *
4005 * LOCKING:
4006 * spin_lock_irqsave(host lock)
4007 */
4008static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
4009{
4010 struct scsi_cmnd *scmd = qc->scsicmd;
4011 const u8 *cdb = scmd->cmnd;
4012 const u8 *p;
4013 u8 pg, spg;
4014 unsigned six_byte, pg_len, hdr_len, bd_len;
4015 int len;
Hannes Reinecke77800812016-04-04 11:44:07 +02004016 u16 fp = (u16)-1;
4017 u8 bp = 0xff;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004018
4019 VPRINTK("ENTER\n");
4020
4021 six_byte = (cdb[0] == MODE_SELECT);
4022 if (six_byte) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004023 if (scmd->cmd_len < 5) {
4024 fp = 4;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004025 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004026 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004027
4028 len = cdb[4];
4029 hdr_len = 4;
4030 } else {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004031 if (scmd->cmd_len < 9) {
4032 fp = 8;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004033 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004034 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004035
4036 len = (cdb[7] << 8) + cdb[8];
4037 hdr_len = 8;
4038 }
4039
4040 /* We only support PF=1, SP=0. */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004041 if ((cdb[1] & 0x11) != 0x10) {
4042 fp = 1;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02004043 bp = (cdb[1] & 0x01) ? 1 : 5;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004044 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004045 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004046
4047 /* Test early for possible overrun. */
4048 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
4049 goto invalid_param_len;
4050
4051 p = page_address(sg_page(scsi_sglist(scmd)));
4052
4053 /* Move past header and block descriptors. */
4054 if (len < hdr_len)
4055 goto invalid_param_len;
4056
4057 if (six_byte)
4058 bd_len = p[3];
4059 else
4060 bd_len = (p[6] << 8) + p[7];
4061
4062 len -= hdr_len;
4063 p += hdr_len;
4064 if (len < bd_len)
4065 goto invalid_param_len;
Hannes Reinecke77800812016-04-04 11:44:07 +02004066 if (bd_len != 0 && bd_len != 8) {
4067 fp = (six_byte) ? 3 : 6;
4068 fp += bd_len + hdr_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004069 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004070 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004071
4072 len -= bd_len;
4073 p += bd_len;
4074 if (len == 0)
4075 goto skip;
4076
4077 /* Parse both possible formats for the mode page headers. */
4078 pg = p[0] & 0x3f;
4079 if (p[0] & 0x40) {
4080 if (len < 4)
4081 goto invalid_param_len;
4082
4083 spg = p[1];
4084 pg_len = (p[2] << 8) | p[3];
4085 p += 4;
4086 len -= 4;
4087 } else {
4088 if (len < 2)
4089 goto invalid_param_len;
4090
4091 spg = 0;
4092 pg_len = p[1];
4093 p += 2;
4094 len -= 2;
4095 }
4096
4097 /*
4098 * No mode subpages supported (yet) but asking for _all_
4099 * subpages may be valid
4100 */
Hannes Reinecke77800812016-04-04 11:44:07 +02004101 if (spg && (spg != ALL_SUB_MPAGES)) {
4102 fp = (p[0] & 0x40) ? 1 : 0;
4103 fp += hdr_len + bd_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004104 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004105 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004106 if (pg_len > len)
4107 goto invalid_param_len;
4108
4109 switch (pg) {
4110 case CACHE_MPAGE:
Hannes Reinecke77800812016-04-04 11:44:07 +02004111 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
4112 fp += hdr_len + bd_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004113 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004114 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004115 break;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004116 case CONTROL_MPAGE:
Hannes Reinecke77800812016-04-04 11:44:07 +02004117 if (ata_mselect_control(qc, p, pg_len, &fp) < 0) {
4118 fp += hdr_len + bd_len;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004119 goto invalid_param;
Tom Yan535fd072016-07-22 02:41:54 +08004120 } else {
4121 goto skip; /* No ATA command to send */
Hannes Reinecke77800812016-04-04 11:44:07 +02004122 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004123 break;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004124 default: /* invalid page code */
Hannes Reinecke77800812016-04-04 11:44:07 +02004125 fp = bd_len + hdr_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004126 goto invalid_param;
4127 }
4128
4129 /*
4130 * Only one page has changeable data, so we only support setting one
4131 * page at a time.
4132 */
4133 if (len > pg_len)
4134 goto invalid_param;
4135
4136 return 0;
4137
4138 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02004139 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004140 return 1;
4141
4142 invalid_param:
Hannes Reinecke77800812016-04-04 11:44:07 +02004143 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004144 return 1;
4145
4146 invalid_param_len:
4147 /* "Parameter list length error" */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004148 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004149 return 1;
4150
4151 skip:
4152 scmd->result = SAM_STAT_GOOD;
4153 return 1;
4154}
4155
4156/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 * ata_get_xlat_func - check if SCSI to ATA translation is possible
4158 * @dev: ATA device
4159 * @cmd: SCSI command opcode to consider
4160 *
4161 * Look up the SCSI command given, and determine whether the
4162 * SCSI command is to be translated or simulated.
4163 *
4164 * RETURNS:
4165 * Pointer to translation function if possible, %NULL if not.
4166 */
4167
4168static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
4169{
4170 switch (cmd) {
4171 case READ_6:
4172 case READ_10:
4173 case READ_16:
4174
4175 case WRITE_6:
4176 case WRITE_10:
4177 case WRITE_16:
4178 return ata_scsi_rw_xlat;
4179
Christoph Hellwig0cdd6eb2009-12-10 10:36:01 +01004180 case WRITE_SAME_16:
Christoph Hellwig18f0f972009-11-17 10:00:47 -05004181 return ata_scsi_write_same_xlat;
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 case SYNCHRONIZE_CACHE:
4184 if (ata_try_flush_cache(dev))
4185 return ata_scsi_flush_xlat;
4186 break;
4187
4188 case VERIFY:
4189 case VERIFY_16:
4190 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04004191
4192 case ATA_12:
4193 case ATA_16:
4194 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04004195
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004196 case MODE_SELECT:
4197 case MODE_SELECT_10:
4198 return ata_scsi_mode_select_xlat;
4199 break;
4200
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02004201 case ZBC_IN:
4202 return ata_scsi_zbc_in_xlat;
4203
Hannes Reinecke27708a92016-04-25 12:45:53 +02004204 case ZBC_OUT:
4205 return ata_scsi_zbc_out_xlat;
4206
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04004207 case START_STOP:
4208 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
4210
4211 return NULL;
4212}
4213
4214/**
4215 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
4216 * @ap: ATA port to which the command was being sent
4217 * @cmd: SCSI command to dump
4218 *
4219 * Prints the contents of a SCSI command via printk().
4220 */
4221
4222static inline void ata_scsi_dump_cdb(struct ata_port *ap,
4223 struct scsi_cmnd *cmd)
4224{
4225#ifdef ATA_DEBUG
4226 struct scsi_device *scsidev = cmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Andy Shevchenko26f58832016-05-06 21:40:40 +03004228 DPRINTK("CDB (%u:%d,%d,%d) %9ph\n",
Tejun Heo44877b42007-02-21 01:06:51 +09004229 ap->print_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 scsidev->channel, scsidev->id, scsidev->lun,
Andy Shevchenko26f58832016-05-06 21:40:40 +03004231 cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232#endif
4233}
4234
Tejun Heo542b1442006-12-17 10:45:08 +09004235static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09004236 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06004237{
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004238 u8 scsi_op = scmd->cmnd[0];
4239 ata_xlat_func_t xlat_func;
Tejun Heo2115ea92006-05-15 21:03:39 +09004240 int rc = 0;
4241
Hannes Reinecke9162c652014-11-05 13:08:21 +01004242 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004243 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
4244 goto bad_cdb_len;
4245
4246 xlat_func = ata_get_xlat_func(dev, scsi_op);
4247 } else {
4248 if (unlikely(!scmd->cmd_len))
4249 goto bad_cdb_len;
4250
4251 xlat_func = NULL;
4252 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
4253 /* relay SCSI command to ATAPI device */
Mark Lord607126c2007-11-15 13:13:59 +09004254 int len = COMMAND_SIZE(scsi_op);
4255 if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004256 goto bad_cdb_len;
4257
4258 xlat_func = atapi_xlat;
4259 } else {
4260 /* ATA_16 passthru, treat as an ATA command */
4261 if (unlikely(scmd->cmd_len > 16))
4262 goto bad_cdb_len;
4263
4264 xlat_func = ata_get_xlat_func(dev, scsi_op);
4265 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09004266 }
4267
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004268 if (xlat_func)
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004269 rc = ata_scsi_translate(dev, scmd, xlat_func);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004270 else
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004271 ata_scsi_simulate(dev, scmd);
Tejun Heo2115ea92006-05-15 21:03:39 +09004272
4273 return rc;
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004274
4275 bad_cdb_len:
4276 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
4277 scmd->cmd_len, scsi_op, dev->cdb_len);
4278 scmd->result = DID_ERROR << 16;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004279 scmd->scsi_done(scmd);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004280 return 0;
Brian Kingeb3f0f92006-03-23 17:30:02 -06004281}
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283/**
4284 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
Jeff Garzik23e701e2010-11-17 12:03:58 -05004285 * @shost: SCSI host of command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 * @cmd: SCSI command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 *
4288 * In some cases, this function translates SCSI commands into
4289 * ATA taskfiles, and queues the taskfiles to be sent to
4290 * hardware. In other cases, this function simulates a
4291 * SCSI device by evaluating and responding to certain
4292 * SCSI commands. This creates the overall effect of
4293 * ATA and ATAPI devices appearing as SCSI devices.
4294 *
4295 * LOCKING:
Jeff Garzik23e701e2010-11-17 12:03:58 -05004296 * ATA host lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 *
4298 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09004299 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
4300 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 */
Jeff Garzik23e701e2010-11-17 12:03:58 -05004302int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303{
4304 struct ata_port *ap;
4305 struct ata_device *dev;
4306 struct scsi_device *scsidev = cmd->device;
Tejun Heo2115ea92006-05-15 21:03:39 +09004307 int rc = 0;
Jeff Garzik23e701e2010-11-17 12:03:58 -05004308 unsigned long irq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Jeff Garzik35bb94b2006-04-11 13:12:34 -04004310 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05004311
Jeff Garzik23e701e2010-11-17 12:03:58 -05004312 spin_lock_irqsave(ap->lock, irq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
4314 ata_scsi_dump_cdb(ap, cmd);
4315
4316 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06004317 if (likely(dev))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004318 rc = __ata_scsi_queuecmd(cmd, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06004319 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzik23e701e2010-11-17 12:03:58 -05004321 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
4323
Jeff Garzik23e701e2010-11-17 12:03:58 -05004324 spin_unlock_irqrestore(ap->lock, irq_flags);
4325
Tejun Heo2115ea92006-05-15 21:03:39 +09004326 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327}
4328
4329/**
4330 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004331 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 * @cmd: SCSI command being sent to device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 *
4334 * Interprets and directly executes a select list of SCSI commands
4335 * that can be handled internally.
4336 *
4337 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004338 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 */
4340
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004341void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
4343 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04004344 const u8 *scsicmd = cmd->cmnd;
Jeff Garzik45394142007-09-21 06:23:42 -04004345 u8 tmp8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Tejun Heo9a3dccc2006-01-06 09:56:18 +01004347 args.dev = dev;
4348 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 args.cmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004350 args.done = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
4352 switch(scsicmd[0]) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004353 case INQUIRY:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004354 if (scsicmd[1] & 2) /* is CmdDt set? */
4355 ata_scsi_invalid_field(dev, cmd, 1);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004356 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
4357 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
4358 else switch (scsicmd[2]) {
4359 case 0x00:
4360 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004362 case 0x80:
4363 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004365 case 0x83:
4366 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004368 case 0x89:
4369 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 break;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05004371 case 0xb0:
4372 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
4373 break;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06004374 case 0xb1:
4375 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
4376 break;
Martin K. Petersen02e0a602010-09-10 01:23:18 -04004377 case 0xb2:
4378 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
4379 break;
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02004380 case 0xb6:
4381 if (dev->flags & ATA_DFLAG_ZAC) {
4382 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b6);
4383 break;
4384 }
4385 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 default:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004387 ata_scsi_invalid_field(dev, cmd, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004389 }
4390 break;
4391
4392 case MODE_SENSE:
4393 case MODE_SENSE_10:
4394 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
4395 break;
4396
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004397 case READ_CAPACITY:
4398 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4399 break;
4400
Hannes Reineckeeb846d92014-11-17 14:25:19 +01004401 case SERVICE_ACTION_IN_16:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004402 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
4403 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4404 else
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004405 ata_scsi_invalid_field(dev, cmd, 1);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004406 break;
4407
4408 case REPORT_LUNS:
4409 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
4410 break;
4411
4412 case REQUEST_SENSE:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004413 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004414 cmd->result = (DRIVER_SENSE << 24);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004415 cmd->scsi_done(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004416 break;
4417
4418 /* if we reach this, then writeback caching is disabled,
4419 * turning this into a no-op.
4420 */
4421 case SYNCHRONIZE_CACHE:
4422 /* fall through */
4423
4424 /* no-op's, complete with success */
4425 case REZERO_UNIT:
4426 case SEEK_6:
4427 case SEEK_10:
4428 case TEST_UNIT_READY:
4429 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
4430 break;
4431
4432 case SEND_DIAGNOSTIC:
4433 tmp8 = scsicmd[1] & ~(1 << 3);
4434 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
4435 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
4436 else
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004437 ata_scsi_invalid_field(dev, cmd, 1);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004438 break;
4439
Shaun Tancheff7b203092016-08-21 23:23:19 -05004440 case MAINTENANCE_IN:
4441 if (scsicmd[1] == MI_REPORT_SUPPORTED_OPERATION_CODES)
4442 ata_scsi_rbuf_fill(&args, ata_scsiop_maint_in);
4443 else
4444 ata_scsi_invalid_field(dev, cmd, 1);
4445 break;
4446
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004447 /* all other commands */
4448 default:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004449 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004450 /* "Invalid command operation code" */
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004451 cmd->scsi_done(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 }
4454}
4455
Tejun Heof3187192007-04-17 23:44:07 +09004456int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
4457{
4458 int i, rc;
4459
4460 for (i = 0; i < host->n_ports; i++) {
4461 struct ata_port *ap = host->ports[i];
4462 struct Scsi_Host *shost;
4463
4464 rc = -ENOMEM;
4465 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
4466 if (!shost)
4467 goto err_alloc;
4468
Lin Ming9ee4f392011-12-05 09:20:28 +08004469 shost->eh_noresume = 1;
Tejun Heof3187192007-04-17 23:44:07 +09004470 *(struct ata_port **)&shost->hostdata[0] = ap;
4471 ap->scsi_host = shost;
4472
Gwendal Grignoud9027472010-05-25 12:31:38 -07004473 shost->transportt = ata_scsi_transport_template;
Tejun Heof3187192007-04-17 23:44:07 +09004474 shost->unique_id = ap->print_id;
4475 shost->max_id = 16;
4476 shost->max_lun = 1;
4477 shost->max_channel = 1;
4478 shost->max_cmd_len = 16;
4479
Tejun Heo31cc23b2007-09-23 13:14:12 +09004480 /* Schedule policy is determined by ->qc_defer()
4481 * callback and it needs to see every deferred qc.
4482 * Set host_blocked to 1 to prevent SCSI midlayer from
4483 * automatically deferring requests.
4484 */
4485 shost->max_host_blocked = 1;
4486
Lin Mingf8fc75d2012-04-12 13:50:39 +08004487 rc = scsi_add_host_with_dma(ap->scsi_host,
4488 &ap->tdev, ap->host->dev);
Tejun Heof3187192007-04-17 23:44:07 +09004489 if (rc)
4490 goto err_add;
4491 }
4492
4493 return 0;
4494
4495 err_add:
4496 scsi_host_put(host->ports[i]->scsi_host);
4497 err_alloc:
4498 while (--i >= 0) {
4499 struct Scsi_Host *shost = host->ports[i]->scsi_host;
4500
4501 scsi_remove_host(shost);
4502 scsi_host_put(shost);
4503 }
4504 return rc;
4505}
4506
Tejun Heo1ae46312007-07-16 14:29:40 +09004507void ata_scsi_scan_host(struct ata_port *ap, int sync)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004508{
Tejun Heo1ae46312007-07-16 14:29:40 +09004509 int tries = 5;
4510 struct ata_device *last_failed_dev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004511 struct ata_link *link;
Tejun Heo1ae46312007-07-16 14:29:40 +09004512 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004513
Tejun Heo1ae46312007-07-16 14:29:40 +09004514 repeat:
Tejun Heo1eca4362008-11-03 20:03:17 +09004515 ata_for_each_link(link, ap, EDGE) {
4516 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004517 struct scsi_device *sdev;
4518 int channel = 0, id = 0;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04004519
Tejun Heo1eca4362008-11-03 20:03:17 +09004520 if (dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004521 continue;
Tejun Heo3edebac2006-05-31 18:27:40 +09004522
Tejun Heo41bda9c2007-08-06 18:36:24 +09004523 if (ata_is_host_link(link))
4524 id = dev->devno;
4525 else
4526 channel = link->pmp;
4527
4528 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
4529 NULL);
4530 if (!IS_ERR(sdev)) {
4531 dev->sdev = sdev;
4532 scsi_device_put(sdev);
Grant Grundler295124d2010-08-17 10:56:53 -07004533 } else {
4534 dev->sdev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004535 }
Tejun Heo3edebac2006-05-31 18:27:40 +09004536 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04004537 }
Tejun Heo1ae46312007-07-16 14:29:40 +09004538
4539 /* If we scanned while EH was in progress or allocation
4540 * failure occurred, scan would have failed silently. Check
4541 * whether all devices are attached.
4542 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004543 ata_for_each_link(link, ap, EDGE) {
4544 ata_for_each_dev(dev, link, ENABLED) {
4545 if (!dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004546 goto exit_loop;
4547 }
Tejun Heo1ae46312007-07-16 14:29:40 +09004548 }
Tejun Heo41bda9c2007-08-06 18:36:24 +09004549 exit_loop:
4550 if (!link)
Tejun Heo1ae46312007-07-16 14:29:40 +09004551 return;
4552
4553 /* we're missing some SCSI devices */
4554 if (sync) {
4555 /* If caller requested synchrnous scan && we've made
4556 * any progress, sleep briefly and repeat.
4557 */
4558 if (dev != last_failed_dev) {
4559 msleep(100);
4560 last_failed_dev = dev;
4561 goto repeat;
4562 }
4563
4564 /* We might be failing to detect boot device, give it
4565 * a few more chances.
4566 */
4567 if (--tries) {
4568 msleep(100);
4569 goto repeat;
4570 }
4571
Joe Perchesa9a79df2011-04-15 15:51:59 -07004572 ata_port_err(ap,
4573 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
Tejun Heo1ae46312007-07-16 14:29:40 +09004574 }
4575
Tejun Heoad72cf92010-07-02 10:03:52 +02004576 queue_delayed_work(system_long_wq, &ap->hotplug_task,
Tejun Heo1ae46312007-07-16 14:29:40 +09004577 round_jiffies_relative(HZ));
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004578}
Tejun Heo0ea035a2006-05-31 18:28:01 +09004579
4580/**
4581 * ata_scsi_offline_dev - offline attached SCSI device
4582 * @dev: ATA device to offline attached SCSI device for
4583 *
4584 * This function is called from ata_eh_hotplug() and responsible
4585 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04004586 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09004587 * against clearing.
4588 *
4589 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004590 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09004591 *
4592 * RETURNS:
4593 * 1 if attached SCSI device exists, 0 otherwise.
4594 */
4595int ata_scsi_offline_dev(struct ata_device *dev)
4596{
4597 if (dev->sdev) {
4598 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
4599 return 1;
4600 }
4601 return 0;
4602}
Tejun Heo580b21022006-05-31 18:28:05 +09004603
4604/**
4605 * ata_scsi_remove_dev - remove attached SCSI device
4606 * @dev: ATA device to remove attached SCSI device for
4607 *
4608 * This function is called from ata_eh_scsi_hotplug() and
4609 * responsible for removing the SCSI device attached to @dev.
4610 *
4611 * LOCKING:
4612 * Kernel thread context (may sleep).
4613 */
4614static void ata_scsi_remove_dev(struct ata_device *dev)
4615{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004616 struct ata_port *ap = dev->link->ap;
Tejun Heo580b21022006-05-31 18:28:05 +09004617 struct scsi_device *sdev;
4618 unsigned long flags;
4619
4620 /* Alas, we need to grab scan_mutex to ensure SCSI device
4621 * state doesn't change underneath us and thus
4622 * scsi_device_get() always succeeds. The mutex locking can
4623 * be removed if there is __scsi_device_get() interface which
4624 * increments reference counts regardless of device state.
4625 */
Jeff Garzikcca39742006-08-24 03:19:22 -04004626 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04004627 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b21022006-05-31 18:28:05 +09004628
Jeff Garzikcca39742006-08-24 03:19:22 -04004629 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b21022006-05-31 18:28:05 +09004630 sdev = dev->sdev;
4631 dev->sdev = NULL;
4632
4633 if (sdev) {
4634 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04004635 * away underneath us after the host lock and
Tejun Heo580b21022006-05-31 18:28:05 +09004636 * scan_mutex are released. Hold onto it.
4637 */
4638 if (scsi_device_get(sdev) == 0) {
4639 /* The following ensures the attached sdev is
4640 * offline on return from ata_scsi_offline_dev()
4641 * regardless it wins or loses the race
4642 * against this function.
4643 */
4644 scsi_device_set_state(sdev, SDEV_OFFLINE);
4645 } else {
4646 WARN_ON(1);
4647 sdev = NULL;
4648 }
4649 }
4650
Jeff Garzikba6a1302006-06-22 23:46:10 -04004651 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04004652 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004653
4654 if (sdev) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004655 ata_dev_info(dev, "detaching (SCSI %s)\n",
4656 dev_name(&sdev->sdev_gendev));
Tejun Heo580b21022006-05-31 18:28:05 +09004657
4658 scsi_remove_device(sdev);
4659 scsi_device_put(sdev);
4660 }
4661}
4662
Tejun Heo41bda9c2007-08-06 18:36:24 +09004663static void ata_scsi_handle_link_detach(struct ata_link *link)
4664{
4665 struct ata_port *ap = link->ap;
4666 struct ata_device *dev;
4667
Tejun Heo1eca4362008-11-03 20:03:17 +09004668 ata_for_each_dev(dev, link, ALL) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004669 unsigned long flags;
4670
4671 if (!(dev->flags & ATA_DFLAG_DETACHED))
4672 continue;
4673
4674 spin_lock_irqsave(ap->lock, flags);
4675 dev->flags &= ~ATA_DFLAG_DETACHED;
4676 spin_unlock_irqrestore(ap->lock, flags);
4677
Aaron Luf1bc1e42013-08-23 10:17:54 +08004678 if (zpodd_dev_enabled(dev))
4679 zpodd_exit(dev);
4680
Tejun Heo41bda9c2007-08-06 18:36:24 +09004681 ata_scsi_remove_dev(dev);
4682 }
4683}
4684
Tejun Heo580b21022006-05-31 18:28:05 +09004685/**
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004686 * ata_scsi_media_change_notify - send media change event
Randy Dunlapc5d0e6a2007-10-15 17:29:46 -07004687 * @dev: Pointer to the disk device with media change event
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004688 *
4689 * Tell the block layer to send a media change notification
4690 * event.
4691 *
4692 * LOCKING:
Tejun Heo854c73a2007-09-23 13:14:11 +09004693 * spin_lock_irqsave(host lock)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004694 */
Tejun Heo854c73a2007-09-23 13:14:11 +09004695void ata_scsi_media_change_notify(struct ata_device *dev)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004696{
Tejun Heo854c73a2007-09-23 13:14:11 +09004697 if (dev->sdev)
Jeff Garzikf26792d2007-10-29 17:18:39 -04004698 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
4699 GFP_ATOMIC);
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004700}
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004701
4702/**
Tejun Heo580b21022006-05-31 18:28:05 +09004703 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00004704 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b21022006-05-31 18:28:05 +09004705 *
4706 * Perform SCSI part of hotplug. It's executed from a separate
4707 * workqueue after EH completes. This is necessary because SCSI
4708 * hot plugging requires working EH and hot unplugging is
4709 * synchronized with hot plugging with a mutex.
4710 *
4711 * LOCKING:
4712 * Kernel thread context (may sleep).
4713 */
David Howells65f27f32006-11-22 14:55:48 +00004714void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b21022006-05-31 18:28:05 +09004715{
David Howells65f27f32006-11-22 14:55:48 +00004716 struct ata_port *ap =
4717 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004718 int i;
Tejun Heo580b21022006-05-31 18:28:05 +09004719
Tejun Heob51e9e52006-06-29 01:29:30 +09004720 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b21022006-05-31 18:28:05 +09004721 DPRINTK("ENTER/EXIT - unloading\n");
4722 return;
4723 }
4724
Tejun Heo85fbd722013-12-18 07:07:32 -05004725 /*
4726 * XXX - UGLY HACK
4727 *
4728 * The block layer suspend/resume path is fundamentally broken due
4729 * to freezable kthreads and workqueue and may deadlock if a block
4730 * device gets removed while resume is in progress. I don't know
4731 * what the solution is short of removing freezable kthreads and
4732 * workqueues altogether.
4733 *
4734 * The following is an ugly hack to avoid kicking off device
4735 * removal while freezer is active. This is a joke but does avoid
4736 * this particular deadlock scenario.
4737 *
4738 * https://bugzilla.kernel.org/show_bug.cgi?id=62801
4739 * http://marc.info/?l=linux-kernel&m=138695698516487
4740 */
4741#ifdef CONFIG_FREEZER
4742 while (pm_freezing)
4743 msleep(10);
4744#endif
4745
Tejun Heo580b21022006-05-31 18:28:05 +09004746 DPRINTK("ENTER\n");
Tejun Heoad72cf92010-07-02 10:03:52 +02004747 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004748
Tejun Heo41bda9c2007-08-06 18:36:24 +09004749 /* Unplug detached devices. We cannot use link iterator here
4750 * because PMP links have to be scanned even if PMP is
4751 * currently not attached. Iterate manually.
4752 */
4753 ata_scsi_handle_link_detach(&ap->link);
4754 if (ap->pmp_link)
4755 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
4756 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
Tejun Heo580b21022006-05-31 18:28:05 +09004757
4758 /* scan for new ones */
Tejun Heo1ae46312007-07-16 14:29:40 +09004759 ata_scsi_scan_host(ap, 0);
Tejun Heo580b21022006-05-31 18:28:05 +09004760
Tejun Heoad72cf92010-07-02 10:03:52 +02004761 mutex_unlock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004762 DPRINTK("EXIT\n");
4763}
Tejun Heo83c47bc2006-05-31 18:28:07 +09004764
4765/**
4766 * ata_scsi_user_scan - indication for user-initiated bus scan
4767 * @shost: SCSI host to scan
4768 * @channel: Channel to scan
4769 * @id: ID to scan
4770 * @lun: LUN to scan
4771 *
4772 * This function is called when user explicitly requests bus
4773 * scan. Set probe pending flag and invoke EH.
4774 *
4775 * LOCKING:
4776 * SCSI layer (we don't care)
4777 *
4778 * RETURNS:
4779 * Zero.
4780 */
Gwendal Grignoud9027472010-05-25 12:31:38 -07004781int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004782 unsigned int id, u64 lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004783{
4784 struct ata_port *ap = ata_shost_to_port(shost);
4785 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004786 int devno, rc = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004787
4788 if (!ap->ops->error_handler)
4789 return -EOPNOTSUPP;
4790
Tejun Heo41bda9c2007-08-06 18:36:24 +09004791 if (lun != SCAN_WILD_CARD && lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004792 return -EINVAL;
4793
Tejun Heo071f44b2008-04-07 22:47:22 +09004794 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004795 if (channel != SCAN_WILD_CARD && channel)
4796 return -EINVAL;
4797 devno = id;
4798 } else {
4799 if (id != SCAN_WILD_CARD && id)
4800 return -EINVAL;
4801 devno = channel;
4802 }
4803
Jeff Garzikba6a1302006-06-22 23:46:10 -04004804 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004805
Tejun Heo41bda9c2007-08-06 18:36:24 +09004806 if (devno == SCAN_WILD_CARD) {
4807 struct ata_link *link;
4808
Tejun Heo1eca4362008-11-03 20:03:17 +09004809 ata_for_each_link(link, ap, EDGE) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004810 struct ata_eh_info *ehi = &link->eh_info;
Tejun Heob558edd2008-01-24 00:05:14 +09004811 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09004812 ehi->action |= ATA_EH_RESET;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004813 }
Tejun Heo83c47bc2006-05-31 18:28:07 +09004814 } else {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004815 struct ata_device *dev = ata_find_dev(ap, devno);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004816
4817 if (dev) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004818 struct ata_eh_info *ehi = &dev->link->eh_info;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004819 ehi->probe_mask |= 1 << dev->devno;
Tejun Heocf480622008-01-24 00:05:14 +09004820 ehi->action |= ATA_EH_RESET;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004821 } else
4822 rc = -EINVAL;
4823 }
4824
Tejun Heo309afcb2006-09-30 18:07:17 +09004825 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09004826 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09004827 spin_unlock_irqrestore(ap->lock, flags);
4828 ata_port_wait_eh(ap);
4829 } else
4830 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004831
4832 return rc;
4833}
zhao, forrest3057ac32006-06-12 12:01:34 +08004834
4835/**
Tejun Heod0171262006-06-12 22:51:14 +09004836 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00004837 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08004838 *
Tejun Heod0171262006-06-12 22:51:14 +09004839 * After ATA pass thru (SAT) commands are executed successfully,
Tejun Heoad72cf92010-07-02 10:03:52 +02004840 * libata need to propagate the changes to SCSI layer.
zhao, forrest3057ac32006-06-12 12:01:34 +08004841 *
Tejun Heod0171262006-06-12 22:51:14 +09004842 * LOCKING:
4843 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08004844 */
David Howells65f27f32006-11-22 14:55:48 +00004845void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08004846{
David Howells65f27f32006-11-22 14:55:48 +00004847 struct ata_port *ap =
4848 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004849 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09004850 struct ata_device *dev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004851 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08004852
Tejun Heoad72cf92010-07-02 10:03:52 +02004853 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heof84e7e42006-11-22 11:21:31 +09004854 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08004855
Tejun Heo1eca4362008-11-03 20:03:17 +09004856 ata_for_each_link(link, ap, EDGE) {
4857 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004858 struct scsi_device *sdev = dev->sdev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004859
Tejun Heo1eca4362008-11-03 20:03:17 +09004860 if (!sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004861 continue;
4862 if (scsi_device_get(sdev))
4863 continue;
Tejun Heof84e7e42006-11-22 11:21:31 +09004864
Tejun Heo41bda9c2007-08-06 18:36:24 +09004865 spin_unlock_irqrestore(ap->lock, flags);
4866 scsi_rescan_device(&(sdev->sdev_gendev));
4867 scsi_device_put(sdev);
4868 spin_lock_irqsave(ap->lock, flags);
4869 }
zhao, forrest3057ac32006-06-12 12:01:34 +08004870 }
Tejun Heof84e7e42006-11-22 11:21:31 +09004871
4872 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad72cf92010-07-02 10:03:52 +02004873 mutex_unlock(&ap->scsi_scan_mutex);
zhao, forrest3057ac32006-06-12 12:01:34 +08004874}
Brian King80289162006-08-07 14:27:31 -05004875
4876/**
4877 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04004878 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05004879 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04004880 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05004881 *
4882 * LOCKING:
4883 * PCI/etc. bus probe sem.
4884 *
4885 * RETURNS:
4886 * ata_port pointer on success / NULL on failure.
4887 */
4888
Jeff Garzikcca39742006-08-24 03:19:22 -04004889struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05004890 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04004891 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05004892{
Tejun Heof3187192007-04-17 23:44:07 +09004893 struct ata_port *ap;
Brian King80289162006-08-07 14:27:31 -05004894
Tejun Heof3187192007-04-17 23:44:07 +09004895 ap = ata_port_alloc(host);
Brian King80289162006-08-07 14:27:31 -05004896 if (!ap)
4897 return NULL;
4898
Tejun Heof3187192007-04-17 23:44:07 +09004899 ap->port_no = 0;
James Bottomleya29b5da2011-01-23 08:30:00 -06004900 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09004901 ap->pio_mask = port_info->pio_mask;
4902 ap->mwdma_mask = port_info->mwdma_mask;
4903 ap->udma_mask = port_info->udma_mask;
4904 ap->flags |= port_info->flags;
4905 ap->ops = port_info->port_ops;
4906 ap->cbl = ATA_CBL_SATA;
4907
Brian King80289162006-08-07 14:27:31 -05004908 return ap;
4909}
4910EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
4911
4912/**
4913 * ata_sas_port_start - Set port up for dma.
4914 * @ap: Port to initialize
4915 *
4916 * Called just after data structures for each port are
James Bottomleydde20202008-02-19 11:36:56 +01004917 * initialized.
Brian King80289162006-08-07 14:27:31 -05004918 *
4919 * May be used as the port_start() entry in ata_port_operations.
4920 *
4921 * LOCKING:
4922 * Inherited from caller.
4923 */
4924int ata_sas_port_start(struct ata_port *ap)
4925{
Nishanth Aravamudan3f1e0462011-06-16 08:28:36 -07004926 /*
4927 * the port is marked as frozen at allocation time, but if we don't
4928 * have new eh, we won't thaw it
4929 */
4930 if (!ap->ops->error_handler)
4931 ap->pflags &= ~ATA_PFLAG_FROZEN;
James Bottomleydde20202008-02-19 11:36:56 +01004932 return 0;
Brian King80289162006-08-07 14:27:31 -05004933}
4934EXPORT_SYMBOL_GPL(ata_sas_port_start);
4935
4936/**
4937 * ata_port_stop - Undo ata_sas_port_start()
4938 * @ap: Port to shut down
4939 *
Brian King80289162006-08-07 14:27:31 -05004940 * May be used as the port_stop() entry in ata_port_operations.
4941 *
4942 * LOCKING:
4943 * Inherited from caller.
4944 */
4945
4946void ata_sas_port_stop(struct ata_port *ap)
4947{
Brian King80289162006-08-07 14:27:31 -05004948}
4949EXPORT_SYMBOL_GPL(ata_sas_port_stop);
4950
Dan Williamsb2024452012-03-21 21:09:07 -07004951/**
4952 * ata_sas_async_probe - simply schedule probing and return
4953 * @ap: Port to probe
4954 *
4955 * For batch scheduling of probe for sas attached ata devices, assumes
4956 * the port has already been through ata_sas_port_init()
4957 */
4958void ata_sas_async_probe(struct ata_port *ap)
Dan Williams9508a662012-01-18 20:47:01 -08004959{
Dan Williamsb2024452012-03-21 21:09:07 -07004960 __ata_port_probe(ap);
Dan Williams9508a662012-01-18 20:47:01 -08004961}
Dan Williamsb2024452012-03-21 21:09:07 -07004962EXPORT_SYMBOL_GPL(ata_sas_async_probe);
4963
4964int ata_sas_sync_probe(struct ata_port *ap)
4965{
4966 return ata_port_probe(ap);
4967}
4968EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
4969
Dan Williams9508a662012-01-18 20:47:01 -08004970
Brian King80289162006-08-07 14:27:31 -05004971/**
4972 * ata_sas_port_init - Initialize a SATA device
4973 * @ap: SATA port to initialize
4974 *
4975 * LOCKING:
4976 * PCI/etc. bus probe sem.
4977 *
4978 * RETURNS:
4979 * Zero on success, non-zero on error.
4980 */
4981
4982int ata_sas_port_init(struct ata_port *ap)
4983{
4984 int rc = ap->ops->port_start(ap);
4985
Dan Williamsb2024452012-03-21 21:09:07 -07004986 if (rc)
4987 return rc;
4988 ap->print_id = atomic_inc_return(&ata_print_id);
4989 return 0;
Brian King80289162006-08-07 14:27:31 -05004990}
4991EXPORT_SYMBOL_GPL(ata_sas_port_init);
4992
4993/**
4994 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
4995 * @ap: SATA port to destroy
4996 *
4997 */
4998
4999void ata_sas_port_destroy(struct ata_port *ap)
5000{
Tejun Heof0d36ef2007-01-20 16:00:28 +09005001 if (ap->ops->port_stop)
5002 ap->ops->port_stop(ap);
Brian King80289162006-08-07 14:27:31 -05005003 kfree(ap);
5004}
5005EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
5006
5007/**
5008 * ata_sas_slave_configure - Default slave_config routine for libata devices
5009 * @sdev: SCSI device to configure
5010 * @ap: ATA port to which SCSI device is attached
5011 *
5012 * RETURNS:
5013 * Zero.
5014 */
5015
5016int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
5017{
5018 ata_scsi_sdev_config(sdev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005019 ata_scsi_dev_config(sdev, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05005020 return 0;
5021}
5022EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
5023
5024/**
5025 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
5026 * @cmd: SCSI command to be sent
Brian King80289162006-08-07 14:27:31 -05005027 * @ap: ATA port to which the command is being sent
5028 *
5029 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06005030 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
5031 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05005032 */
5033
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005034int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
Brian King80289162006-08-07 14:27:31 -05005035{
Brian King08475a12006-11-20 13:51:56 -06005036 int rc = 0;
5037
Brian King80289162006-08-07 14:27:31 -05005038 ata_scsi_dump_cdb(ap, cmd);
5039
Tejun Heo2486fa52008-07-31 07:52:40 +09005040 if (likely(ata_dev_enabled(ap->link.device)))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005041 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05005042 else {
5043 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005044 cmd->scsi_done(cmd);
Brian King80289162006-08-07 14:27:31 -05005045 }
Brian King08475a12006-11-20 13:51:56 -06005046 return rc;
Brian King80289162006-08-07 14:27:31 -05005047}
5048EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
Shaohua Li98bd4be2015-01-23 19:52:07 -08005049
5050int ata_sas_allocate_tag(struct ata_port *ap)
5051{
5052 unsigned int max_queue = ap->host->n_tags;
5053 unsigned int i, tag;
5054
5055 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
Tejun Heo3a028242015-03-24 14:14:18 -04005056 tag = tag < max_queue ? tag : 0;
Shaohua Li98bd4be2015-01-23 19:52:07 -08005057
5058 /* the last tag is reserved for internal command. */
5059 if (tag == ATA_TAG_INTERNAL)
5060 continue;
5061
5062 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
5063 ap->sas_last_tag = tag;
5064 return tag;
5065 }
5066 }
5067 return -1;
5068}
5069
5070void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
5071{
5072 clear_bit(tag, &ap->sas_tag_allocated);
5073}