blob: 062cb2eee8dea62031e6a5034b591f5633a0ef74 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "libata.h"
Gwendal Grignoud9027472010-05-25 12:31:38 -070055#include "libata-transport.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Tejun Heo87340e92008-04-28 17:48:51 +090057#define ATA_SCSI_RBUF_SIZE 4096
58
59static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
60static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
Jeff Garzikb0955182005-05-12 15:45:22 -040061
Tejun Heoad706992006-12-17 10:45:57 +090062typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
Tejun Heoab5b3a52006-05-31 18:27:34 +090063
Jeff Garzik2dcb4072007-10-19 06:42:56 -040064static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090065 const struct scsi_device *scsidev);
Jeff Garzik2dcb4072007-10-19 06:42:56 -040066static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090067 const struct scsi_device *scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040069#define RW_RECOVERY_MPAGE 0x1
70#define RW_RECOVERY_MPAGE_LEN 12
71#define CACHE_MPAGE 0x8
72#define CACHE_MPAGE_LEN 20
73#define CONTROL_MPAGE 0xa
74#define CONTROL_MPAGE_LEN 12
75#define ALL_MPAGES 0x3f
76#define ALL_SUB_MPAGES 0xff
77
78
Jeff Garzik24f75682007-09-21 07:54:49 -040079static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040080 RW_RECOVERY_MPAGE,
81 RW_RECOVERY_MPAGE_LEN - 2,
Jeff Garzik24f75682007-09-21 07:54:49 -040082 (1 << 7), /* AWRE */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040083 0, /* read retry count */
84 0, 0, 0, 0,
85 0, /* write retry count */
86 0, 0, 0
87};
88
89static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
90 CACHE_MPAGE,
91 CACHE_MPAGE_LEN - 2,
92 0, /* contains WCE, needs to be 0 for logic */
93 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, /* contains DRA, needs to be 0 for logic */
95 0, 0, 0, 0, 0, 0, 0
96};
97
98static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
99 CONTROL_MPAGE,
100 CONTROL_MPAGE_LEN - 2,
101 2, /* DSENSE=0, GLTSD=1 */
102 0, /* [QAM+QERR may be 1, see 05-359r1] */
103 0, 0, 0, 0, 0xff, 0xff,
104 0, 30 /* extended self test time, see 05-359r1 */
105};
106
Tejun Heoc93b2632010-09-01 17:50:04 +0200107static const char *ata_lpm_policy_names[] = {
108 [ATA_LPM_UNKNOWN] = "max_performance",
109 [ATA_LPM_MAX_POWER] = "max_performance",
110 [ATA_LPM_MED_POWER] = "medium_power",
111 [ATA_LPM_MIN_POWER] = "min_power",
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400112};
113
Tejun Heoecd75ad2014-01-16 09:47:17 -0500114static ssize_t ata_scsi_lpm_store(struct device *device,
Tejun Heoc93b2632010-09-01 17:50:04 +0200115 struct device_attribute *attr,
116 const char *buf, size_t count)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400117{
Tejun Heoecd75ad2014-01-16 09:47:17 -0500118 struct Scsi_Host *shost = class_to_shost(device);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400119 struct ata_port *ap = ata_shost_to_port(shost);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500120 struct ata_link *link;
121 struct ata_device *dev;
Tejun Heoc93b2632010-09-01 17:50:04 +0200122 enum ata_lpm_policy policy;
Tejun Heo6b7ae952010-09-01 17:50:06 +0200123 unsigned long flags;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400124
Tejun Heoc93b2632010-09-01 17:50:04 +0200125 /* UNKNOWN is internal state, iterate from MAX_POWER */
126 for (policy = ATA_LPM_MAX_POWER;
127 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
128 const char *name = ata_lpm_policy_names[policy];
129
130 if (strncmp(name, buf, strlen(name)) == 0)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400131 break;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400132 }
Tejun Heoc93b2632010-09-01 17:50:04 +0200133 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400134 return -EINVAL;
135
Tejun Heo6b7ae952010-09-01 17:50:06 +0200136 spin_lock_irqsave(ap->lock, flags);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500137
138 ata_for_each_link(link, ap, EDGE) {
139 ata_for_each_dev(dev, &ap->link, ENABLED) {
140 if (dev->horkage & ATA_HORKAGE_NOLPM) {
141 count = -EOPNOTSUPP;
142 goto out_unlock;
143 }
144 }
145 }
146
Tejun Heo6b7ae952010-09-01 17:50:06 +0200147 ap->target_lpm_policy = policy;
148 ata_port_schedule_eh(ap);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500149out_unlock:
Tejun Heo6b7ae952010-09-01 17:50:06 +0200150 spin_unlock_irqrestore(ap->lock, flags);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400151 return count;
152}
153
Tejun Heoc93b2632010-09-01 17:50:04 +0200154static ssize_t ata_scsi_lpm_show(struct device *dev,
155 struct device_attribute *attr, char *buf)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400156{
Tony Jonesee959b02008-02-22 00:13:36 +0100157 struct Scsi_Host *shost = class_to_shost(dev);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400158 struct ata_port *ap = ata_shost_to_port(shost);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400159
Tejun Heo6b7ae952010-09-01 17:50:06 +0200160 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400161 return -EINVAL;
162
Tejun Heoc93b2632010-09-01 17:50:04 +0200163 return snprintf(buf, PAGE_SIZE, "%s\n",
Tejun Heo6b7ae952010-09-01 17:50:06 +0200164 ata_lpm_policy_names[ap->target_lpm_policy]);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400165}
Tony Jonesee959b02008-02-22 00:13:36 +0100166DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
Tejun Heoc93b2632010-09-01 17:50:04 +0200167 ata_scsi_lpm_show, ata_scsi_lpm_store);
Tony Jonesee959b02008-02-22 00:13:36 +0100168EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400169
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200170static ssize_t ata_scsi_park_show(struct device *device,
171 struct device_attribute *attr, char *buf)
172{
173 struct scsi_device *sdev = to_scsi_device(device);
174 struct ata_port *ap;
175 struct ata_link *link;
176 struct ata_device *dev;
Tejun Heo3948b6f2016-02-18 11:50:37 -0500177 unsigned long now;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200178 unsigned int uninitialized_var(msecs);
179 int rc = 0;
180
181 ap = ata_shost_to_port(sdev->host);
182
Tejun Heo3948b6f2016-02-18 11:50:37 -0500183 spin_lock_irq(ap->lock);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200184 dev = ata_scsi_find_dev(ap, sdev);
185 if (!dev) {
186 rc = -ENODEV;
187 goto unlock;
188 }
189 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
190 rc = -EOPNOTSUPP;
191 goto unlock;
192 }
193
194 link = dev->link;
Elias Oltmannsa4641892008-11-03 19:01:08 +0900195 now = jiffies;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200196 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
197 link->eh_context.unloaded_mask & (1 << dev->devno) &&
Elias Oltmannsa4641892008-11-03 19:01:08 +0900198 time_after(dev->unpark_deadline, now))
199 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200200 else
201 msecs = 0;
202
203unlock:
204 spin_unlock_irq(ap->lock);
205
206 return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
207}
208
209static ssize_t ata_scsi_park_store(struct device *device,
210 struct device_attribute *attr,
211 const char *buf, size_t len)
212{
213 struct scsi_device *sdev = to_scsi_device(device);
214 struct ata_port *ap;
215 struct ata_device *dev;
216 long int input;
217 unsigned long flags;
218 int rc;
219
Jingoo Han42b9ab72013-07-19 15:56:41 +0900220 rc = kstrtol(buf, 10, &input);
221 if (rc)
222 return rc;
223 if (input < -2)
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200224 return -EINVAL;
225 if (input > ATA_TMOUT_MAX_PARK) {
226 rc = -EOVERFLOW;
227 input = ATA_TMOUT_MAX_PARK;
228 }
229
230 ap = ata_shost_to_port(sdev->host);
231
232 spin_lock_irqsave(ap->lock, flags);
233 dev = ata_scsi_find_dev(ap, sdev);
234 if (unlikely(!dev)) {
235 rc = -ENODEV;
236 goto unlock;
237 }
Hannes Reinecke9162c652014-11-05 13:08:21 +0100238 if (dev->class != ATA_DEV_ATA &&
239 dev->class != ATA_DEV_ZAC) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200240 rc = -EOPNOTSUPP;
241 goto unlock;
242 }
243
244 if (input >= 0) {
245 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
246 rc = -EOPNOTSUPP;
247 goto unlock;
248 }
249
250 dev->unpark_deadline = ata_deadline(jiffies, input);
251 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
252 ata_port_schedule_eh(ap);
253 complete(&ap->park_req_pending);
254 } else {
255 switch (input) {
256 case -1:
257 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
258 break;
259 case -2:
260 dev->flags |= ATA_DFLAG_NO_UNLOAD;
261 break;
262 }
263 }
264unlock:
265 spin_unlock_irqrestore(ap->lock, flags);
266
267 return rc ? rc : len;
268}
269DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
270 ata_scsi_park_show, ata_scsi_park_store);
271EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
272
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200273void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
274 u8 sk, u8 asc, u8 ascq)
Tejun Heof0761be2008-04-28 17:16:52 +0900275{
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200276 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
277
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +0200278 if (!cmd)
279 return;
280
Tejun Heof0761be2008-04-28 17:16:52 +0900281 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
282
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200283 scsi_build_sense_buffer(d_sense, cmd->sense_buffer, sk, asc, ascq);
Tejun Heof0761be2008-04-28 17:16:52 +0900284}
285
Hannes Reinecke492bf622016-04-04 11:43:59 +0200286void ata_scsi_set_sense_information(struct ata_device *dev,
287 struct scsi_cmnd *cmd,
288 const struct ata_taskfile *tf)
289{
290 u64 information;
291
292 if (!cmd)
293 return;
294
295 information = ata_tf_read_block(tf, dev);
296 if (information == U64_MAX)
297 return;
298
299 scsi_set_sense_information(cmd->sense_buffer,
300 SCSI_SENSE_BUFFERSIZE, information);
301}
302
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200303static void ata_scsi_set_invalid_field(struct ata_device *dev,
304 struct scsi_cmnd *cmd, u16 field)
305{
306 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
307 /* "Invalid field in cbd" */
308 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
309 field, 0xff, 1);
310}
311
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700312static ssize_t
313ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
314 const char *buf, size_t count)
315{
316 struct Scsi_Host *shost = class_to_shost(dev);
317 struct ata_port *ap = ata_shost_to_port(shost);
318 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
319 return ap->ops->em_store(ap, buf, count);
320 return -EINVAL;
321}
322
323static ssize_t
324ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
325 char *buf)
326{
327 struct Scsi_Host *shost = class_to_shost(dev);
328 struct ata_port *ap = ata_shost_to_port(shost);
329
330 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
331 return ap->ops->em_show(ap, buf);
332 return -EINVAL;
333}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200334DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700335 ata_scsi_em_message_show, ata_scsi_em_message_store);
336EXPORT_SYMBOL_GPL(dev_attr_em_message);
337
338static ssize_t
339ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
340 char *buf)
341{
342 struct Scsi_Host *shost = class_to_shost(dev);
343 struct ata_port *ap = ata_shost_to_port(shost);
344
345 return snprintf(buf, 23, "%d\n", ap->em_message_type);
346}
347DEVICE_ATTR(em_message_type, S_IRUGO,
348 ata_scsi_em_message_type_show, NULL);
349EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
350
351static ssize_t
352ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
353 char *buf)
354{
355 struct scsi_device *sdev = to_scsi_device(dev);
356 struct ata_port *ap = ata_shost_to_port(sdev->host);
357 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
358
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800359 if (atadev && ap->ops->sw_activity_show &&
360 (ap->flags & ATA_FLAG_SW_ACTIVITY))
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700361 return ap->ops->sw_activity_show(atadev, buf);
362 return -EINVAL;
363}
364
365static ssize_t
366ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
367 const char *buf, size_t count)
368{
369 struct scsi_device *sdev = to_scsi_device(dev);
370 struct ata_port *ap = ata_shost_to_port(sdev->host);
371 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
372 enum sw_activity val;
373 int rc;
374
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800375 if (atadev && ap->ops->sw_activity_store &&
376 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700377 val = simple_strtoul(buf, NULL, 0);
378 switch (val) {
379 case OFF: case BLINK_ON: case BLINK_OFF:
380 rc = ap->ops->sw_activity_store(atadev, val);
381 if (!rc)
382 return count;
383 else
384 return rc;
385 }
386 }
387 return -EINVAL;
388}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200389DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700390 ata_scsi_activity_store);
391EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
392
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200393struct device_attribute *ata_common_sdev_attrs[] = {
394 &dev_attr_unload_heads,
395 NULL
396};
397EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
398
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200399static void ata_scsi_invalid_field(struct ata_device *dev,
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200400 struct scsi_cmnd *cmd, u16 field)
Douglas Gilbertae006512005-10-09 09:09:35 -0400401{
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200402 ata_scsi_set_invalid_field(dev, cmd, field);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500403 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -0400404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/**
407 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
408 * @sdev: SCSI device for which BIOS geometry is to be determined
409 * @bdev: block device associated with @sdev
410 * @capacity: capacity of SCSI device
411 * @geom: location to which geometry will be output
412 *
413 * Generic bios head/sector/cylinder calculator
414 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
415 * mapping. Some situations may arise where the disk is not
416 * bootable if this is not used.
417 *
418 * LOCKING:
419 * Defined by the SCSI layer. We don't really care.
420 *
421 * RETURNS:
422 * Zero.
423 */
424int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
425 sector_t capacity, int geom[])
426{
427 geom[0] = 255;
428 geom[1] = 63;
429 sector_div(capacity, 255*63);
430 geom[2] = capacity;
431
432 return 0;
433}
434
Jeff Garzikb0955182005-05-12 15:45:22 -0400435/**
Tejun Heod8d91292010-05-15 20:09:34 +0200436 * ata_scsi_unlock_native_capacity - unlock native capacity
437 * @sdev: SCSI device to adjust device capacity for
438 *
439 * This function is called if a partition on @sdev extends beyond
440 * the end of the device. It requests EH to unlock HPA.
441 *
442 * LOCKING:
443 * Defined by the SCSI layer. Might sleep.
444 */
445void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
446{
447 struct ata_port *ap = ata_shost_to_port(sdev->host);
448 struct ata_device *dev;
449 unsigned long flags;
450
451 spin_lock_irqsave(ap->lock, flags);
452
453 dev = ata_scsi_find_dev(ap, sdev);
454 if (dev && dev->n_sectors < dev->n_native_sectors) {
455 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
456 dev->link->eh_info.action |= ATA_EH_RESET;
457 ata_port_schedule_eh(ap);
458 }
459
460 spin_unlock_irqrestore(ap->lock, flags);
461 ata_port_wait_eh(ap);
462}
463
464/**
Tejun Heo5924b742007-01-02 20:20:07 +0900465 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
Randy Dunlap5eb66fe2009-01-20 16:28:59 -0800466 * @ap: target port
Tejun Heo5924b742007-01-02 20:20:07 +0900467 * @sdev: SCSI device to get identify data for
468 * @arg: User buffer area for identify data
469 *
470 * LOCKING:
471 * Defined by the SCSI layer. We don't really care.
472 *
473 * RETURNS:
474 * Zero on success, negative errno on error.
475 */
Jeff Garzik94be9a52009-01-16 10:17:09 -0500476static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
477 void __user *arg)
Tejun Heo5924b742007-01-02 20:20:07 +0900478{
Tejun Heo5924b742007-01-02 20:20:07 +0900479 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
480 u16 __user *dst = arg;
481 char buf[40];
482
483 if (!dev)
484 return -ENOMSG;
485
486 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
487 return -EFAULT;
488
489 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
490 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
491 return -EFAULT;
492
493 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
494 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
495 return -EFAULT;
496
497 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
498 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
499 return -EFAULT;
500
501 return 0;
502}
503
504/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400505 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800506 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400507 * @arg: User provided data for issuing command
508 *
509 * LOCKING:
510 * Defined by the SCSI layer. We don't really care.
511 *
512 * RETURNS:
513 * Zero on success, negative errno on error.
514 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400515int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
516{
517 int rc = 0;
518 u8 scsi_cmd[MAX_COMMAND_SIZE];
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700519 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400520 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600521 enum dma_data_direction data_dir;
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700522 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400523
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500524 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400525 return -EINVAL;
526
527 if (copy_from_user(args, arg, sizeof(args)))
528 return -EFAULT;
529
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700530 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
531 if (!sensebuf)
532 return -ENOMEM;
533
Jeff Garzikb0955182005-05-12 15:45:22 -0400534 memset(scsi_cmd, 0, sizeof(scsi_cmd));
535
536 if (args[3]) {
Grant Grundler295124d2010-08-17 10:56:53 -0700537 argsize = ATA_SECT_SIZE * args[3];
Jeff Garzikb0955182005-05-12 15:45:22 -0400538 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400539 if (argbuf == NULL) {
540 rc = -ENOMEM;
541 goto error;
542 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400543
544 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
545 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400546 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600547 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400548 } else {
549 scsi_cmd[1] = (3 << 1); /* Non-data */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700550 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600551 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400552 }
553
554 scsi_cmd[0] = ATA_16;
555
556 scsi_cmd[4] = args[2];
Bartlomiej Zolnierkiewicza4f19042008-10-10 22:39:20 +0200557 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
Jeff Garzikb0955182005-05-12 15:45:22 -0400558 scsi_cmd[6] = args[3];
559 scsi_cmd[8] = args[1];
560 scsi_cmd[10] = 0x4f;
561 scsi_cmd[12] = 0xc2;
562 } else {
563 scsi_cmd[6] = args[1];
564 }
565 scsi_cmd[14] = args[0];
566
567 /* Good values for timeout and retries? Values below
568 from scsi_ioctl_send_command() for default case... */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700569 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900570 sensebuf, (10*HZ), 5, 0, NULL);
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700571
572 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
573 u8 *desc = sensebuf + 8;
574 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
575
576 /* If we set cc then ATA pass-through will cause a
577 * check condition even if no error. Filter that. */
578 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
579 struct scsi_sense_hdr sshdr;
580 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400581 &sshdr);
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100582 if (sshdr.sense_key == RECOVERED_ERROR &&
583 sshdr.asc == 0 && sshdr.ascq == 0x1d)
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700584 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
585 }
586
587 /* Send userspace a few ATA registers (same as drivers/ide) */
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400588 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
589 desc[0] == 0x09) { /* code is "ATA Descriptor" */
590 args[0] = desc[13]; /* status */
591 args[1] = desc[3]; /* error */
592 args[2] = desc[5]; /* sector count (0:7) */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700593 if (copy_to_user(arg, args, sizeof(args)))
594 rc = -EFAULT;
595 }
596 }
597
598
599 if (cmd_result) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400600 rc = -EIO;
601 goto error;
602 }
603
Jeff Garzikb0955182005-05-12 15:45:22 -0400604 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500605 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400606 rc = -EFAULT;
607error:
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700608 kfree(sensebuf);
Jesper Juhl8f760782006-06-27 02:55:06 -0700609 kfree(argbuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400610 return rc;
611}
612
613/**
614 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800615 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400616 * @arg: User provided data for issuing command
617 *
618 * LOCKING:
619 * Defined by the SCSI layer. We don't really care.
620 *
621 * RETURNS:
622 * Zero on success, negative errno on error.
623 */
624int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
625{
626 int rc = 0;
627 u8 scsi_cmd[MAX_COMMAND_SIZE];
David Milburnaf068bd2007-01-30 00:59:15 -0800628 u8 args[7], *sensebuf = NULL;
629 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400630
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500631 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400632 return -EINVAL;
633
634 if (copy_from_user(args, arg, sizeof(args)))
635 return -EFAULT;
636
David Milburnaf068bd2007-01-30 00:59:15 -0800637 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
638 if (!sensebuf)
639 return -ENOMEM;
640
Jeff Garzikb0955182005-05-12 15:45:22 -0400641 memset(scsi_cmd, 0, sizeof(scsi_cmd));
642 scsi_cmd[0] = ATA_16;
643 scsi_cmd[1] = (3 << 1); /* Non-data */
David Milburnaf068bd2007-01-30 00:59:15 -0800644 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Jeff Garzikb0955182005-05-12 15:45:22 -0400645 scsi_cmd[4] = args[1];
646 scsi_cmd[6] = args[2];
647 scsi_cmd[8] = args[3];
648 scsi_cmd[10] = args[4];
649 scsi_cmd[12] = args[5];
Mark Lord277239f2007-03-30 17:45:52 -0400650 scsi_cmd[13] = args[6] & 0x4f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400651 scsi_cmd[14] = args[0];
652
Jeff Garzikb0955182005-05-12 15:45:22 -0400653 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500654 from scsi_ioctl_send_command() for default case... */
David Milburnaf068bd2007-01-30 00:59:15 -0800655 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900656 sensebuf, (10*HZ), 5, 0, NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400657
David Milburnaf068bd2007-01-30 00:59:15 -0800658 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
659 u8 *desc = sensebuf + 8;
660 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
661
662 /* If we set cc then ATA pass-through will cause a
663 * check condition even if no error. Filter that. */
664 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
665 struct scsi_sense_hdr sshdr;
666 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
667 &sshdr);
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100668 if (sshdr.sense_key == RECOVERED_ERROR &&
669 sshdr.asc == 0 && sshdr.ascq == 0x1d)
David Milburnaf068bd2007-01-30 00:59:15 -0800670 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
671 }
672
673 /* Send userspace ATA registers */
674 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
675 desc[0] == 0x09) {/* code is "ATA Descriptor" */
676 args[0] = desc[13]; /* status */
677 args[1] = desc[3]; /* error */
678 args[2] = desc[5]; /* sector count (0:7) */
679 args[3] = desc[7]; /* lbal */
680 args[4] = desc[9]; /* lbam */
681 args[5] = desc[11]; /* lbah */
682 args[6] = desc[12]; /* select */
683 if (copy_to_user(arg, args, sizeof(args)))
684 rc = -EFAULT;
685 }
686 }
687
688 if (cmd_result) {
689 rc = -EIO;
690 goto error;
691 }
692
693 error:
694 kfree(sensebuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400695 return rc;
696}
697
Alan Coxe3cf95d2009-04-09 17:31:17 +0100698static int ata_ioc32(struct ata_port *ap)
699{
700 if (ap->flags & ATA_FLAG_PIO_DMA)
701 return 1;
702 if (ap->pflags & ATA_PFLAG_PIO32)
703 return 1;
704 return 0;
705}
706
Jeff Garzik94be9a52009-01-16 10:17:09 -0500707int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
708 int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Arnd Bergmann287e6612016-02-11 14:16:27 +0100710 unsigned long val;
711 int rc = -EINVAL;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100712 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 switch (cmd) {
Arnd Bergmann287e6612016-02-11 14:16:27 +0100715 case HDIO_GET_32BIT:
Alan Coxe3cf95d2009-04-09 17:31:17 +0100716 spin_lock_irqsave(ap->lock, flags);
717 val = ata_ioc32(ap);
718 spin_unlock_irqrestore(ap->lock, flags);
Arnd Bergmann287e6612016-02-11 14:16:27 +0100719 return put_user(val, (unsigned long __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Arnd Bergmann287e6612016-02-11 14:16:27 +0100721 case HDIO_SET_32BIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 val = (unsigned long) arg;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100723 rc = 0;
724 spin_lock_irqsave(ap->lock, flags);
725 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
726 if (val)
727 ap->pflags |= ATA_PFLAG_PIO32;
728 else
729 ap->pflags &= ~ATA_PFLAG_PIO32;
730 } else {
731 if (val != ata_ioc32(ap))
732 rc = -EINVAL;
733 }
734 spin_unlock_irqrestore(ap->lock, flags);
735 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Tejun Heo5924b742007-01-02 20:20:07 +0900737 case HDIO_GET_IDENTITY:
Jeff Garzik94be9a52009-01-16 10:17:09 -0500738 return ata_get_identity(ap, scsidev, arg);
Tejun Heo5924b742007-01-02 20:20:07 +0900739
Jeff Garzikb0955182005-05-12 15:45:22 -0400740 case HDIO_DRIVE_CMD:
741 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
742 return -EACCES;
743 return ata_cmd_ioctl(scsidev, arg);
744
745 case HDIO_DRIVE_TASK:
746 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
747 return -EACCES;
748 return ata_task_ioctl(scsidev, arg);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 default:
751 rc = -ENOTTY;
752 break;
753 }
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return rc;
756}
Jeff Garzik94be9a52009-01-16 10:17:09 -0500757EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
758
759int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
760{
761 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
762 scsidev, cmd, arg);
763}
764EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766/**
767 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * @dev: ATA device to which the new command is attached
769 * @cmd: SCSI command that originated this ATA command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 *
771 * Obtain a reference to an unused ata_queued_cmd structure,
772 * which is the basic libata structure representing a single
773 * ATA command sent to the hardware.
774 *
775 * If a command was available, fill in the SCSI-specific
776 * portions of the structure with information on the
777 * current command.
778 *
779 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400780 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 *
782 * RETURNS:
783 * Command allocated, or %NULL if none available.
784 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100785static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500786 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct ata_queued_cmd *qc;
789
Shaohua Li12cb5ce2015-01-15 17:32:27 -0800790 qc = ata_qc_new_init(dev, cmd->request->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (qc) {
792 qc->scsicmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500793 qc->scsidone = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Tejun Heoff2aeb12007-12-05 16:43:11 +0900795 qc->sg = scsi_sglist(cmd);
Boaz Harrosh71201652007-09-18 17:48:50 +0200796 qc->n_elem = scsi_sg_count(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 } else {
798 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500799 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
802 return qc;
803}
804
Tejun Heoaacda372008-03-18 17:47:43 +0900805static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
806{
807 struct scsi_cmnd *scmd = qc->scsicmd;
808
809 qc->extrabytes = scmd->request->extra_len;
810 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400814 * ata_dump_status - user friendly display of error info
815 * @id: id of the port in question
816 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400818 * Decode and dump the ATA error/status registers for the user so
819 * that they have some idea what really happened at the non
820 * make-believe layer.
821 *
822 * LOCKING:
823 * inherited from caller
824 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100825static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
Jeff Garzikb0955182005-05-12 15:45:22 -0400826{
827 u8 stat = tf->command, err = tf->feature;
828
829 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
830 if (stat & ATA_BUSY) {
831 printk("Busy }\n"); /* Data is not valid in this case */
832 } else {
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100833 if (stat & ATA_DRDY) printk("DriveReady ");
834 if (stat & ATA_DF) printk("DeviceFault ");
835 if (stat & ATA_DSC) printk("SeekComplete ");
836 if (stat & ATA_DRQ) printk("DataRequest ");
837 if (stat & ATA_CORR) printk("CorrectedError ");
838 if (stat & ATA_SENSE) printk("Sense ");
839 if (stat & ATA_ERR) printk("Error ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400840 printk("}\n");
841
842 if (err) {
843 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100844 if (err & ATA_ABORTED) printk("DriveStatusError ");
845 if (err & ATA_ICRC) {
846 if (err & ATA_ABORTED)
847 printk("BadCRC ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400848 else printk("Sector ");
849 }
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100850 if (err & ATA_UNC) printk("UncorrectableError ");
851 if (err & ATA_IDNF) printk("SectorIdNotFound ");
852 if (err & ATA_TRK0NF) printk("TrackZeroNotFound ");
853 if (err & ATA_AMNF) printk("AddrMarkNotFound ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400854 printk("}\n");
855 }
856 }
857}
858
859/**
860 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800861 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400862 * @drv_stat: value contained in ATA status register
863 * @drv_err: value contained in ATA error register
864 * @sk: the sense key we'll fill out
865 * @asc: the additional sense code we'll fill out
866 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900867 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400868 *
869 * Converts an ATA error into a SCSI error. Fill out pointers to
870 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
871 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 *
873 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400874 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100876static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
877 u8 *asc, u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Jeff Garzikb0955182005-05-12 15:45:22 -0400879 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100882 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /* BBD|ECC|ID|MAR */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100884 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
885 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* BBD|ECC|ID */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100887 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
888 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /* ECC|MC|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100890 {0x61, HARDWARE_ERROR, 0x00, 0x00},
891 // Device fault Hardware error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100893 {0x84, ABORTED_COMMAND, 0x47, 0x00},
894 // Data CRC error SCSI parity error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* MC|ID|ABRT|TRK0|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100896 {0x37, NOT_READY, 0x04, 0x00},
897 // Unit offline Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /* MCR|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100899 {0x09, NOT_READY, 0x04, 0x00},
900 // Unrecovered disk error Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /* Bad address mark */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100902 {0x01, MEDIUM_ERROR, 0x13, 0x00},
903 // Address mark not found for data field
904 /* TRK0 - Track 0 not found */
905 {0x02, HARDWARE_ERROR, 0x00, 0x00},
906 // Hardware error
Gwendal Grignou78062c52013-06-18 10:54:48 -0700907 /* Abort: 0x04 is not translated here, see below */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* Media change request */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100909 {0x08, NOT_READY, 0x04, 0x00},
910 // FIXME: faking offline
911 /* SRV/IDNF - ID not found */
912 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
913 // Logical address out of range
914 /* MC - Media Changed */
915 {0x20, UNIT_ATTENTION, 0x28, 0x00},
916 // Not ready to ready change, medium may have changed
917 /* ECC - Uncorrectable ECC error */
918 {0x40, MEDIUM_ERROR, 0x11, 0x04},
919 // Unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* BBD - block marked bad */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100921 {0x80, MEDIUM_ERROR, 0x11, 0x04},
922 // Block marked bad Medium error, unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
924 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100925 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /* Must be first because BUSY means no other bits valid */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100927 {0x80, ABORTED_COMMAND, 0x47, 0x00},
928 // Busy, fake parity for now
929 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
930 // Device ready, unaligned write command
931 {0x20, HARDWARE_ERROR, 0x44, 0x00},
932 // Device fault, internal target failure
933 {0x08, ABORTED_COMMAND, 0x47, 0x00},
934 // Timed out in xfer, fake parity for now
935 {0x04, RECOVERED_ERROR, 0x11, 0x00},
936 // Recovered ECC error Medium error, recovered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
938 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /*
941 * Is this an error we can process/parse
942 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400943 if (drv_stat & ATA_BUSY) {
944 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Jeff Garzikb0955182005-05-12 15:45:22 -0400947 if (drv_err) {
948 /* Look for drv_err */
949 for (i = 0; sense_table[i][0] != 0xFF; i++) {
950 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500951 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -0400952 sense_table[i][0]) {
953 *sk = sense_table[i][1];
954 *asc = sense_table[i][2];
955 *ascq = sense_table[i][3];
956 goto translate_done;
957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
Gwendal Grignou78062c52013-06-18 10:54:48 -0700961 /*
962 * Fall back to interpreting status bits. Note that if the drv_err
963 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
964 * is not descriptive enough.
965 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400966 for (i = 0; stat_table[i][0] != 0xFF; i++) {
967 if (stat_table[i][0] & drv_stat) {
968 *sk = stat_table[i][1];
969 *asc = stat_table[i][2];
970 *ascq = stat_table[i][3];
971 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Gwendal Grignou78062c52013-06-18 10:54:48 -0700975 /*
976 * We need a sensible error return here, which is tricky, and one
977 * that won't cause people to do things like return a disk wrongly.
978 */
Alan Cox2d202022006-03-21 15:53:46 +0000979 *sk = ABORTED_COMMAND;
980 *asc = 0x00;
981 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -0400982
983 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +0900984 if (verbose)
985 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
986 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
987 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -0400988 return;
989}
990
991/*
Tejun Heo750426a2006-11-14 22:37:35 +0900992 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -0400993 * @qc: Command that completed.
994 *
995 * This function is specific to the ATA descriptor format sense
996 * block specified for the ATA pass through commands. Regardless
997 * of whether the command errored or not, return a sense
998 * block. Copy all controller registers into the sense
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -0800999 * block. If there was no error, we get the request from an ATA
1000 * passthrough command, so we use the following sense data:
1001 * sk = RECOVERED ERROR
1002 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
1003 *
Jeff Garzikb0955182005-05-12 15:45:22 -04001004 *
1005 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001006 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001007 */
Tejun Heo750426a2006-11-14 22:37:35 +09001008static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001009{
1010 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001011 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001012 unsigned char *sb = cmd->sense_buffer;
1013 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +09001014 int verbose = qc->ap->ops->error_handler == NULL;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001015 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001016
1017 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1018
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001019 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001020
1021 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04001022 * Use ata_to_sense_error() to map status register bits
1023 * onto sense key, asc & ascq.
1024 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001025 if (qc->err_mask ||
1026 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001027 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001028 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001029 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001030 } else {
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001031 /*
1032 * ATA PASS-THROUGH INFORMATION AVAILABLE
1033 * Always in descriptor format sense.
1034 */
1035 scsi_build_sense_buffer(1, cmd->sense_buffer,
1036 RECOVERED_ERROR, 0, 0x1D);
Jeff Garzikb0955182005-05-12 15:45:22 -04001037 }
1038
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001039 if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
1040 u8 len;
Jeff Garzikb0955182005-05-12 15:45:22 -04001041
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001042 /* descriptor format */
1043 len = sb[7];
1044 desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
1045 if (!desc) {
1046 if (SCSI_SENSE_BUFFERSIZE < len + 14)
1047 return;
1048 sb[7] = len + 14;
1049 desc = sb + 8 + len;
1050 }
1051 desc[0] = 9;
1052 desc[1] = 12;
1053 /*
1054 * Copy registers into sense buffer.
1055 */
1056 desc[2] = 0x00;
1057 desc[3] = tf->feature; /* == error reg */
1058 desc[5] = tf->nsect;
1059 desc[7] = tf->lbal;
1060 desc[9] = tf->lbam;
1061 desc[11] = tf->lbah;
1062 desc[12] = tf->device;
1063 desc[13] = tf->command; /* == status reg */
Jeff Garzikb0955182005-05-12 15:45:22 -04001064
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001065 /*
1066 * Fill in Extend bit, and the high order bytes
1067 * if applicable.
1068 */
1069 if (tf->flags & ATA_TFLAG_LBA48) {
1070 desc[2] |= 0x01;
1071 desc[4] = tf->hob_nsect;
1072 desc[6] = tf->hob_lbal;
1073 desc[8] = tf->hob_lbam;
1074 desc[10] = tf->hob_lbah;
1075 }
1076 } else {
1077 /* Fixed sense format */
1078 desc[0] = tf->feature;
1079 desc[1] = tf->command; /* status */
1080 desc[2] = tf->device;
1081 desc[3] = tf->nsect;
1082 desc[0] = 0;
1083 if (tf->flags & ATA_TFLAG_LBA48) {
1084 desc[8] |= 0x80;
1085 if (tf->hob_nsect)
1086 desc[8] |= 0x40;
1087 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
1088 desc[8] |= 0x20;
1089 }
1090 desc[9] = tf->lbal;
1091 desc[10] = tf->lbam;
1092 desc[11] = tf->lbah;
Jeff Garzikb0955182005-05-12 15:45:22 -04001093 }
1094}
1095
1096/**
Tejun Heo750426a2006-11-14 22:37:35 +09001097 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -04001098 * @qc: Command that we are erroring out
1099 *
Tejun Heod25614b2006-11-14 22:37:35 +09001100 * Generate sense block for a failed ATA command @qc. Descriptor
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001101 * format is used to accommodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -04001102 *
1103 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001104 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001105 */
Tejun Heo750426a2006-11-14 22:37:35 +09001106static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001107{
Tejun Heod25614b2006-11-14 22:37:35 +09001108 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -04001109 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001110 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001111 unsigned char *sb = cmd->sense_buffer;
Tejun Heo246619d2006-05-15 20:58:16 +09001112 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +09001113 u64 block;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001114 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001115
1116 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1117
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001118 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001119
Tejun Heod25614b2006-11-14 22:37:35 +09001120 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -04001121 * onto sense key, asc & ascq.
1122 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001123 if (qc->err_mask ||
1124 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001125 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001126 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001127 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001128 } else {
1129 /* Could not decode error */
1130 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
1131 tf->command, qc->err_mask);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001132 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001133 return;
Jeff Garzikb0955182005-05-12 15:45:22 -04001134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Tejun Heod25614b2006-11-14 22:37:35 +09001136 block = ata_tf_read_block(&qc->result_tf, dev);
Hannes Reineckecffd1ee2016-04-04 11:43:57 +02001137 if (block == U64_MAX)
1138 return;
Jeff Garzik0274aa22005-06-22 13:50:56 -04001139
Hannes Reineckecf8b49b2016-04-04 11:43:58 +02001140 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Brian Kinga6cce2a2006-03-17 17:04:15 -06001143static void ata_scsi_sdev_config(struct scsi_device *sdev)
1144{
1145 sdev->use_10_for_rw = 1;
1146 sdev->use_10_for_ms = 1;
Martin K. Petersen3c6bdae2012-09-18 12:19:30 -04001147 sdev->no_report_opcodes = 1;
Martin K. Petersen5db44862012-09-18 12:19:32 -04001148 sdev->no_write_same = 1;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001149
1150 /* Schedule policy is determined by ->qc_defer() callback and
1151 * it needs to see every deferred qc. Set dev_blocked to 1 to
1152 * prevent SCSI midlayer from automatically deferring
1153 * requests.
1154 */
1155 sdev->max_device_blocked = 1;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001156}
1157
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001158/**
1159 * atapi_drain_needed - Check whether data transfer may overflow
Randy Dunlap73fd8b62008-02-19 13:43:21 -08001160 * @rq: request to be checked
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001161 *
1162 * ATAPI commands which transfer variable length data to host
1163 * might overflow due to application error or hardare bug. This
1164 * function checks whether overflow should be drained and ignored
1165 * for @request.
1166 *
1167 * LOCKING:
1168 * None.
1169 *
1170 * RETURNS:
1171 * 1 if ; otherwise, 0.
1172 */
1173static int atapi_drain_needed(struct request *rq)
1174{
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001175 if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001176 return 0;
1177
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001178 if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001179 return 0;
1180
1181 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
1182}
1183
1184static int ata_scsi_dev_config(struct scsi_device *sdev,
1185 struct ata_device *dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -06001186{
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001187 struct request_queue *q = sdev->request_queue;
1188
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02001189 if (!ata_id_has_unload(dev->id))
1190 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1191
Tejun Heo914ed352006-11-01 18:39:55 +09001192 /* configure max sectors */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001193 blk_queue_max_hw_sectors(q, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -06001194
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001195 if (dev->class == ATA_DEV_ATAPI) {
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001196 void *buf;
1197
Tejun Heo729a6a32011-01-20 13:59:06 +01001198 sdev->sector_size = ATA_SECT_SIZE;
1199
1200 /* set DMA padding */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001201 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001202
1203 /* configure draining */
1204 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1205 if (!buf) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001206 ata_dev_err(dev, "drain buffer allocation failed\n");
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001207 return -ENOMEM;
1208 }
1209
1210 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1211 } else {
Grant Grundler295124d2010-08-17 10:56:53 -07001212 sdev->sector_size = ata_id_logical_sector_size(dev->id);
Tejun Heod8cf5382008-01-15 08:46:59 +09001213 sdev->manage_start_stop = 1;
James Bottomleydde20202008-02-19 11:36:56 +01001214 }
Tejun Heod8cf5382008-01-15 08:46:59 +09001215
Tejun Heo729a6a32011-01-20 13:59:06 +01001216 /*
1217 * ata_pio_sectors() expects buffer for each sector to not cross
1218 * page boundary. Enforce it by requiring buffers to be sector
1219 * aligned, which works iff sector_size is not larger than
1220 * PAGE_SIZE. ATAPI devices also need the alignment as
1221 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1222 */
1223 if (sdev->sector_size > PAGE_SIZE)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001224 ata_dev_warn(dev,
Tejun Heo729a6a32011-01-20 13:59:06 +01001225 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1226 sdev->sector_size);
1227
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001228 blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
Tejun Heo729a6a32011-01-20 13:59:06 +01001229
Jeff Garzikf26792d2007-10-29 17:18:39 -04001230 if (dev->flags & ATA_DFLAG_AN)
1231 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1232
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001233 if (dev->flags & ATA_DFLAG_NCQ) {
1234 int depth;
1235
1236 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1237 depth = min(ATA_MAX_QUEUE - 1, depth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001238 scsi_change_queue_depth(sdev, depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001239 }
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001240
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001241 blk_queue_flush_queueable(q, false);
1242
Grant Grundler295124d2010-08-17 10:56:53 -07001243 dev->sdev = sdev;
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001244 return 0;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001245}
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247/**
1248 * ata_scsi_slave_config - Set SCSI device attributes
1249 * @sdev: SCSI device to examine
1250 *
1251 * This is called before we actually start reading
1252 * and writing to the device, to configure certain
1253 * SCSI mid-layer behaviors.
1254 *
1255 * LOCKING:
1256 * Defined by SCSI layer. We don't really care.
1257 */
1258
1259int ata_scsi_slave_config(struct scsi_device *sdev)
1260{
Tejun Heo31534362006-05-31 18:27:36 +09001261 struct ata_port *ap = ata_shost_to_port(sdev->host);
1262 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001263 int rc = 0;
Tejun Heo31534362006-05-31 18:27:36 +09001264
Brian Kinga6cce2a2006-03-17 17:04:15 -06001265 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Tejun Heo31534362006-05-31 18:27:36 +09001267 if (dev)
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001268 rc = ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001270 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273/**
Tejun Heo83c47bc2006-05-31 18:28:07 +09001274 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1275 * @sdev: SCSI device to be destroyed
1276 *
1277 * @sdev is about to be destroyed for hot/warm unplugging. If
1278 * this unplugging was initiated by libata as indicated by NULL
1279 * dev->sdev, this function doesn't have to do anything.
1280 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1281 * Clear dev->sdev, schedule the device for ATA detach and invoke
1282 * EH.
1283 *
1284 * LOCKING:
1285 * Defined by SCSI layer. We don't really care.
1286 */
1287void ata_scsi_slave_destroy(struct scsi_device *sdev)
1288{
1289 struct ata_port *ap = ata_shost_to_port(sdev->host);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001290 struct request_queue *q = sdev->request_queue;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001291 unsigned long flags;
1292 struct ata_device *dev;
1293
1294 if (!ap->ops->error_handler)
1295 return;
1296
Jeff Garzikba6a1302006-06-22 23:46:10 -04001297 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09001298 dev = __ata_scsi_find_dev(ap, sdev);
1299 if (dev && dev->sdev) {
1300 /* SCSI device already in CANCEL state, no need to offline it */
1301 dev->sdev = NULL;
1302 dev->flags |= ATA_DFLAG_DETACH;
1303 ata_port_schedule_eh(ap);
1304 }
Jeff Garzikba6a1302006-06-22 23:46:10 -04001305 spin_unlock_irqrestore(ap->lock, flags);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001306
1307 kfree(q->dma_drain_buffer);
1308 q->dma_drain_buffer = NULL;
1309 q->dma_drain_size = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001310}
1311
1312/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001313 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
Marcos Paulo de Souza6d2dd052011-10-31 23:50:16 -02001314 * @ap: ATA port to which the device change the queue depth
1315 * @sdev: SCSI device to configure queue depth for
1316 * @queue_depth: new queue depth
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001317 *
Dan Williamsf6e67032011-09-20 15:10:33 -07001318 * libsas and libata have different approaches for associating a sdev to
1319 * its ata_port.
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001320 *
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001321 */
Dan Williamsf6e67032011-09-20 15:10:33 -07001322int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001323 int queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001324{
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001325 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +09001326 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001327
Tejun Heoc3c70c42007-02-20 23:31:22 +09001328 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001329 return sdev->queue_depth;
1330
1331 dev = ata_scsi_find_dev(ap, sdev);
1332 if (!dev || !ata_dev_enabled(dev))
1333 return sdev->queue_depth;
1334
Tejun Heoc3c70c42007-02-20 23:31:22 +09001335 /* NCQ enabled? */
Tejun Heo360f6542006-09-30 19:45:00 +09001336 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001337 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1338 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
Tejun Heo360f6542006-09-30 19:45:00 +09001339 dev->flags |= ATA_DFLAG_NCQ_OFF;
Tejun Heoc3c70c42007-02-20 23:31:22 +09001340 queue_depth = 1;
1341 }
Tejun Heo360f6542006-09-30 19:45:00 +09001342 spin_unlock_irqrestore(ap->lock, flags);
1343
Tejun Heoc3c70c42007-02-20 23:31:22 +09001344 /* limit and apply queue depth */
1345 queue_depth = min(queue_depth, sdev->host->can_queue);
1346 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1347 queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
1348
1349 if (sdev->queue_depth == queue_depth)
1350 return -EINVAL;
1351
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001352 return scsi_change_queue_depth(sdev, queue_depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001353}
1354
1355/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001356 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1357 * @sdev: SCSI device to configure queue depth for
1358 * @queue_depth: new queue depth
Dan Williamsf6e67032011-09-20 15:10:33 -07001359 *
1360 * This is libata standard hostt->change_queue_depth callback.
1361 * SCSI will call into this callback when user tries to set queue
1362 * depth via sysfs.
1363 *
1364 * LOCKING:
1365 * SCSI layer (we don't care)
1366 *
1367 * RETURNS:
1368 * Newly configured queue depth.
1369 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001370int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
Dan Williamsf6e67032011-09-20 15:10:33 -07001371{
1372 struct ata_port *ap = ata_shost_to_port(sdev->host);
1373
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001374 return __ata_change_queue_depth(ap, sdev, queue_depth);
Dan Williamsf6e67032011-09-20 15:10:33 -07001375}
1376
1377/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001378 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1379 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001380 *
1381 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1382 * (to start). Perhaps these commands should be preceded by
1383 * CHECK POWER MODE to see what power mode the device is already in.
1384 * [See SAT revision 5 at www.t10.org]
1385 *
1386 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001387 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001388 *
1389 * RETURNS:
1390 * Zero on success, non-zero on error.
1391 */
Tejun Heoad706992006-12-17 10:45:57 +09001392static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001393{
Tejun Heo542b1442006-12-17 10:45:08 +09001394 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001395 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +09001396 const u8 *cdb = scmd->cmnd;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001397 u16 fp;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001398
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001399 if (scmd->cmd_len < 5) {
1400 fp = 4;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001401 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001402 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09001403
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001404 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1405 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +09001406 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001407 ; /* ignore IMMED bit, violates sat-r05 */
1408 }
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001409 if (cdb[4] & 0x2) {
1410 fp = 4;
Douglas Gilbertae006512005-10-09 09:09:35 -04001411 goto invalid_fld; /* LOEJ bit set not supported */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001412 }
1413 if (((cdb[4] >> 4) & 0xf) != 0) {
1414 fp = 4;
Douglas Gilbertae006512005-10-09 09:09:35 -04001415 goto invalid_fld; /* power conditions not supported */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001416 }
Tejun Heoe31e8532007-09-23 13:14:13 +09001417
Tejun Heo542b1442006-12-17 10:45:08 +09001418 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001419 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -04001420
1421 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +09001422 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -04001423
1424 tf->lbah = 0x0;
1425 tf->lbam = 0x0;
1426 tf->lbal = 0x0;
1427 tf->device |= ATA_LBA;
1428 } else {
1429 /* CHS */
1430 tf->lbal = 0x1; /* sect */
1431 tf->lbam = 0x0; /* cyl low */
1432 tf->lbah = 0x0; /* cyl high */
1433 }
1434
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001435 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
Tejun Heo920a4b12007-05-04 21:28:48 +02001436 } else {
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001437 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1438 * or S5) causing some drives to spin up and down again.
1439 */
1440 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1441 system_state == SYSTEM_POWER_OFF)
1442 goto skip;
1443
1444 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1445 system_entering_hibernation())
1446 goto skip;
1447
Robert Hancock78981a72007-01-30 00:59:18 -08001448 /* Issue ATA STANDBY IMMEDIATE command */
1449 tf->command = ATA_CMD_STANDBYNOW1;
Tejun Heo920a4b12007-05-04 21:28:48 +02001450 }
Robert Hancock78981a72007-01-30 00:59:18 -08001451
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001452 /*
1453 * Standby and Idle condition timers could be implemented but that
1454 * would require libata to implement the Power condition mode page
1455 * and allow the user to change it. Changing mode pages requires
1456 * MODE SELECT to be implemented.
1457 */
1458
1459 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001460
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001461 invalid_fld:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001462 ata_scsi_set_invalid_field(qc->dev, scmd, fp);
Douglas Gilbertae006512005-10-09 09:09:35 -04001463 return 1;
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001464 skip:
1465 scmd->result = SAM_STAT_GOOD;
1466 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001467}
1468
1469
1470/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1472 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 *
1474 * Sets up an ATA taskfile to issue FLUSH CACHE or
1475 * FLUSH CACHE EXT.
1476 *
1477 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001478 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 *
1480 * RETURNS:
1481 * Zero on success, non-zero on error.
1482 */
Tejun Heoad706992006-12-17 10:45:57 +09001483static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 struct ata_taskfile *tf = &qc->tf;
1486
1487 tf->flags |= ATA_TFLAG_DEVICE;
1488 tf->protocol = ATA_PROT_NODATA;
1489
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001490 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 tf->command = ATA_CMD_FLUSH_EXT;
1492 else
1493 tf->command = ATA_CMD_FLUSH;
1494
Tejun Heob666da32007-10-26 15:53:59 +09001495 /* flush is critical for IO integrity, consider it an IO command */
1496 qc->flags |= ATA_QCFLAG_IO;
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return 0;
1499}
1500
1501/**
Albert Lee3aef5232005-10-04 08:47:43 -04001502 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001503 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001504 *
1505 * Calculate LBA and transfer length for 6-byte commands.
1506 *
1507 * RETURNS:
1508 * @plba: the LBA
1509 * @plen: the transfer length
1510 */
Tejun Heo542b1442006-12-17 10:45:08 +09001511static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001512{
1513 u64 lba = 0;
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001514 u32 len;
Albert Lee3aef5232005-10-04 08:47:43 -04001515
1516 VPRINTK("six-byte command\n");
1517
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001518 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
Tejun Heo542b1442006-12-17 10:45:08 +09001519 lba |= ((u64)cdb[2]) << 8;
1520 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001521
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001522 len = cdb[4];
Albert Lee3aef5232005-10-04 08:47:43 -04001523
1524 *plba = lba;
1525 *plen = len;
1526}
1527
1528/**
1529 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001530 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001531 *
1532 * Calculate LBA and transfer length for 10-byte commands.
1533 *
1534 * RETURNS:
1535 * @plba: the LBA
1536 * @plen: the transfer length
1537 */
Tejun Heo542b1442006-12-17 10:45:08 +09001538static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001539{
1540 u64 lba = 0;
1541 u32 len = 0;
1542
1543 VPRINTK("ten-byte command\n");
1544
Tejun Heo542b1442006-12-17 10:45:08 +09001545 lba |= ((u64)cdb[2]) << 24;
1546 lba |= ((u64)cdb[3]) << 16;
1547 lba |= ((u64)cdb[4]) << 8;
1548 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001549
Tejun Heo542b1442006-12-17 10:45:08 +09001550 len |= ((u32)cdb[7]) << 8;
1551 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001552
1553 *plba = lba;
1554 *plen = len;
1555}
1556
1557/**
1558 * scsi_16_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001559 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001560 *
1561 * Calculate LBA and transfer length for 16-byte commands.
1562 *
1563 * RETURNS:
1564 * @plba: the LBA
1565 * @plen: the transfer length
1566 */
Tejun Heo542b1442006-12-17 10:45:08 +09001567static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001568{
1569 u64 lba = 0;
1570 u32 len = 0;
1571
1572 VPRINTK("sixteen-byte command\n");
1573
Tejun Heo542b1442006-12-17 10:45:08 +09001574 lba |= ((u64)cdb[2]) << 56;
1575 lba |= ((u64)cdb[3]) << 48;
1576 lba |= ((u64)cdb[4]) << 40;
1577 lba |= ((u64)cdb[5]) << 32;
1578 lba |= ((u64)cdb[6]) << 24;
1579 lba |= ((u64)cdb[7]) << 16;
1580 lba |= ((u64)cdb[8]) << 8;
1581 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001582
Tejun Heo542b1442006-12-17 10:45:08 +09001583 len |= ((u32)cdb[10]) << 24;
1584 len |= ((u32)cdb[11]) << 16;
1585 len |= ((u32)cdb[12]) << 8;
1586 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001587
1588 *plba = lba;
1589 *plen = len;
1590}
1591
1592/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1594 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 *
1596 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1597 *
1598 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001599 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 *
1601 * RETURNS:
1602 * Zero on success, non-zero on error.
1603 */
Tejun Heoad706992006-12-17 10:45:57 +09001604static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Tejun Heo542b1442006-12-17 10:45:08 +09001606 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001608 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001610 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001611 u64 block;
1612 u32 n_block;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001613 u16 fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1616 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Tejun Heo2e5704f2006-12-17 10:46:33 +09001618 if (cdb[0] == VERIFY) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001619 if (scmd->cmd_len < 10) {
1620 fp = 9;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001621 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001622 }
Tejun Heo542b1442006-12-17 10:45:08 +09001623 scsi_10_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001624 } else if (cdb[0] == VERIFY_16) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001625 if (scmd->cmd_len < 16) {
1626 fp = 15;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001627 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001628 }
Tejun Heo542b1442006-12-17 10:45:08 +09001629 scsi_16_lba_len(cdb, &block, &n_block);
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001630 } else {
1631 fp = 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001632 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Albert Lee8bf62ece2005-05-12 15:29:42 -04001635 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001636 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001637 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001638 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001639 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001640 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Albert Lee07506692005-10-12 15:04:18 +08001642 if (dev->flags & ATA_DFLAG_LBA) {
1643 tf->flags |= ATA_TFLAG_LBA;
1644
Albert Leec6a33e22005-10-12 15:12:26 +08001645 if (lba_28_ok(block, n_block)) {
1646 /* use LBA28 */
1647 tf->command = ATA_CMD_VERIFY;
1648 tf->device |= (block >> 24) & 0xf;
1649 } else if (lba_48_ok(block, n_block)) {
1650 if (!(dev->flags & ATA_DFLAG_LBA48))
1651 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001652
1653 /* use LBA48 */
1654 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001655 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Albert Lee8bf62ece2005-05-12 15:29:42 -04001657 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Albert Lee8bf62ece2005-05-12 15:29:42 -04001659 tf->hob_lbah = (block >> 40) & 0xff;
1660 tf->hob_lbam = (block >> 32) & 0xff;
1661 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001662 } else
1663 /* request too large even for LBA48 */
1664 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001665
1666 tf->nsect = n_block & 0xff;
1667
1668 tf->lbah = (block >> 16) & 0xff;
1669 tf->lbam = (block >> 8) & 0xff;
1670 tf->lbal = block & 0xff;
1671
1672 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001674 /* CHS */
1675 u32 sect, head, cyl, track;
1676
Albert Leec6a33e22005-10-12 15:12:26 +08001677 if (!lba_28_ok(block, n_block))
1678 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001679
Albert Lee8bf62ece2005-05-12 15:29:42 -04001680 /* Convert LBA to CHS */
1681 track = (u32)block / dev->sectors;
1682 cyl = track / dev->heads;
1683 head = track % dev->heads;
1684 sect = (u32)block % dev->sectors + 1;
1685
Albert Leec187c4b2005-10-04 08:46:51 -04001686 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1687 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001688
1689 /* Check whether the converted CHS can fit.
1690 Cylinder: 0-65535
Albert Lee8bf62ece2005-05-12 15:29:42 -04001691 Head: 0-15
1692 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001693 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001694 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001697 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1698 tf->lbal = sect;
1699 tf->lbam = cyl;
1700 tf->lbah = cyl >> 8;
1701 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001705
1706invalid_fld:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001707 ata_scsi_set_invalid_field(qc->dev, scmd, fp);
Douglas Gilbertae006512005-10-09 09:09:35 -04001708 return 1;
1709
1710out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001711 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001712 /* "Logical Block Address out of range" */
1713 return 1;
1714
1715nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001716 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001717 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720/**
1721 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1722 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 *
1724 * Converts any of six SCSI read/write commands into the
1725 * ATA counterpart, including starting sector (LBA),
1726 * sector count, and taking into account the device's LBA48
1727 * support.
1728 *
1729 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1730 * %WRITE_16 are currently supported.
1731 *
1732 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001733 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 *
1735 * RETURNS:
1736 * Zero on success, non-zero on error.
1737 */
Tejun Heoad706992006-12-17 10:45:57 +09001738static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Tejun Heo542b1442006-12-17 10:45:08 +09001740 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001741 const u8 *cdb = scmd->cmnd;
Tejun Heobd056d72006-11-14 22:47:10 +09001742 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001743 u64 block;
1744 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001745 int rc;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001746 u16 fp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Tejun Heo542b1442006-12-17 10:45:08 +09001748 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001749 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001751 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001752 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001753 case READ_10:
1754 case WRITE_10:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001755 if (unlikely(scmd->cmd_len < 10)) {
1756 fp = 9;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001757 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001758 }
Tejun Heo542b1442006-12-17 10:45:08 +09001759 scsi_10_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001760 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001761 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001762 break;
1763 case READ_6:
1764 case WRITE_6:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001765 if (unlikely(scmd->cmd_len < 6)) {
1766 fp = 5;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001767 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001768 }
Tejun Heo542b1442006-12-17 10:45:08 +09001769 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001770
1771 /* for 6-byte r/w commands, transfer length 0
1772 * means 256 blocks of data, not 0 block.
1773 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001774 if (!n_block)
1775 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001776 break;
1777 case READ_16:
1778 case WRITE_16:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001779 if (unlikely(scmd->cmd_len < 16)) {
1780 fp = 15;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001781 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001782 }
Tejun Heo542b1442006-12-17 10:45:08 +09001783 scsi_16_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001784 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001785 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001786 break;
1787 default:
Albert Lee8bf62ece2005-05-12 15:29:42 -04001788 DPRINTK("no-byte command\n");
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001789 fp = 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001790 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792
Albert Lee8bf62ece2005-05-12 15:29:42 -04001793 /* Check and compose ATA command */
1794 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001795 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1796 * length 0 means transfer 0 block of data.
1797 * However, for ATA R/W commands, sector count 0 means
1798 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001799 *
1800 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001801 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001802 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001803
Tejun Heobd056d72006-11-14 22:47:10 +09001804 qc->flags |= ATA_QCFLAG_IO;
Grant Grundler295124d2010-08-17 10:56:53 -07001805 qc->nbytes = n_block * scmd->device->sector_size;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001806
Tejun Heobd056d72006-11-14 22:47:10 +09001807 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1808 qc->tag);
1809 if (likely(rc == 0))
1810 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001811
Tejun Heobd056d72006-11-14 22:47:10 +09001812 if (rc == -ERANGE)
1813 goto out_of_range;
1814 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001815invalid_fld:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001816 ata_scsi_set_invalid_field(qc->dev, scmd, fp);
Douglas Gilbertae006512005-10-09 09:09:35 -04001817 return 1;
1818
1819out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001820 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001821 /* "Logical Block Address out of range" */
1822 return 1;
1823
1824nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001825 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001826 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001829static void ata_qc_done(struct ata_queued_cmd *qc)
1830{
1831 struct scsi_cmnd *cmd = qc->scsicmd;
1832 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1833
1834 ata_qc_free(qc);
1835 done(cmd);
1836}
1837
Tejun Heo77853bf2006-01-23 13:09:36 +09001838static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Tejun Heoc31f5712006-11-22 12:39:43 +09001840 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001842 u8 *cdb = cmd->cmnd;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001843 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Jeff Garzikb0955182005-05-12 15:45:22 -04001845 /* For ATA pass thru (SAT) commands, generate a sense block if
1846 * user mandated it or if there's an error. Note that if we
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001847 * generate because the user forced us to [CK_COND =1], a check
1848 * condition is generated and the ATA register values are returned
Jeff Garzikb0955182005-05-12 15:45:22 -04001849 * whether the command completed successfully or not. If there
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001850 * was no error, we use the following sense data:
1851 * sk = RECOVERED ERROR
1852 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
Jeff Garzikb0955182005-05-12 15:45:22 -04001853 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001854 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001855 ((cdb[2] & 0x20) || need_sense))
Tejun Heo750426a2006-11-14 22:37:35 +09001856 ata_gen_passthru_sense(qc);
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +02001857 else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1858 cmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001859 else if (need_sense)
1860 ata_gen_ata_sense(qc);
1861 else
1862 cmd->result = SAM_STAT_GOOD;
Jeff Garzikb0955182005-05-12 15:45:22 -04001863
Tejun Heoc31f5712006-11-22 12:39:43 +09001864 if (need_sense && !ap->ops->error_handler)
Tejun Heo44877b42007-02-21 01:06:51 +09001865 ata_dump_status(ap->print_id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001867 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
1870/**
1871 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * @dev: ATA device to which the command is addressed
1873 * @cmd: SCSI command to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1875 *
1876 * Our ->queuecommand() function has decided that the SCSI
1877 * command issued can be directly translated into an ATA
1878 * command, rather than handled internally.
1879 *
1880 * This function sets up an ata_queued_cmd structure for the
1881 * SCSI command, and sends that ata_queued_cmd to the hardware.
1882 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001883 * The xlat_func argument (actor) returns 0 if ready to execute
1884 * ATA command, else 1 to finish translation. If 1 is returned
1885 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1886 * to be set reflecting an error condition or clean (early)
1887 * termination.
1888 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001890 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09001891 *
1892 * RETURNS:
1893 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1894 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 */
Tejun Heo2115ea92006-05-15 21:03:39 +09001896static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09001897 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Tejun Heo31cc23b2007-09-23 13:14:12 +09001899 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 struct ata_queued_cmd *qc;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001901 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 VPRINTK("ENTER\n");
1904
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05001905 qc = ata_scsi_qc_new(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04001907 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05001910 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1911 cmd->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harrosh71201652007-09-18 17:48:50 +02001912 if (unlikely(scsi_bufflen(cmd) < 1)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001913 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001914 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916
Boaz Harrosh71201652007-09-18 17:48:50 +02001917 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 qc->dma_dir = cmd->sc_data_direction;
1920 }
1921
1922 qc->complete_fn = ata_scsi_qc_complete;
1923
Tejun Heoad706992006-12-17 10:45:57 +09001924 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04001925 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Tejun Heo31cc23b2007-09-23 13:14:12 +09001927 if (ap->ops->qc_defer) {
1928 if ((rc = ap->ops->qc_defer(qc)))
1929 goto defer;
1930 }
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09001933 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Douglas Gilbertae006512005-10-09 09:09:35 -04001938early_finish:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001939 ata_qc_free(qc);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05001940 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -04001941 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001942 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001943
1944err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04001946 cmd->result = (DID_ERROR << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05001947 cmd->scsi_done(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05001948err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04001949 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001950 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001951
1952defer:
Tejun Heo31cc23b2007-09-23 13:14:12 +09001953 ata_qc_free(qc);
Tejun Heo3dc1d882006-05-15 21:03:45 +09001954 DPRINTK("EXIT - defer\n");
Tejun Heo31cc23b2007-09-23 13:14:12 +09001955 if (rc == ATA_DEFER_LINK)
1956 return SCSI_MLQUEUE_DEVICE_BUSY;
1957 else
1958 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
1961/**
1962 * ata_scsi_rbuf_get - Map response buffer.
Randy Dunlapec2a20e2008-04-30 12:57:00 -07001963 * @cmd: SCSI command containing buffer to be mapped.
Tejun Heo87340e92008-04-28 17:48:51 +09001964 * @flags: unsigned long variable to store irq enable status
1965 * @copy_in: copy in from user buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 *
Tejun Heo87340e92008-04-28 17:48:51 +09001967 * Prepare buffer for simulated SCSI commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 *
1969 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09001970 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 *
1972 * RETURNS:
Tejun Heo87340e92008-04-28 17:48:51 +09001973 * Pointer to response buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 */
Tejun Heo87340e92008-04-28 17:48:51 +09001975static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
1976 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Tejun Heo87340e92008-04-28 17:48:51 +09001978 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Tejun Heo87340e92008-04-28 17:48:51 +09001980 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
1981 if (copy_in)
1982 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1983 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1984 return ata_scsi_rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
1987/**
1988 * ata_scsi_rbuf_put - Unmap response buffer.
1989 * @cmd: SCSI command containing buffer to be unmapped.
Tejun Heo87340e92008-04-28 17:48:51 +09001990 * @copy_out: copy out result
1991 * @flags: @flags passed to ata_scsi_rbuf_get()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 *
Tejun Heo87340e92008-04-28 17:48:51 +09001993 * Returns rbuf buffer. The result is copied to @cmd's buffer if
1994 * @copy_back is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 *
1996 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09001997 * Unlocks ata_scsi_rbuf_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 */
Tejun Heo87340e92008-04-28 17:48:51 +09001999static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
2000 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
Tejun Heo87340e92008-04-28 17:48:51 +09002002 if (copy_out)
2003 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2004 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2005 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
2008/**
2009 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
2010 * @args: device IDENTIFY data / SCSI command of interest.
2011 * @actor: Callback hook for desired SCSI command simulator
2012 *
2013 * Takes care of the hard work of simulating a SCSI command...
2014 * Mapping the response buffer, calling the command's handler,
2015 * and handling the handler's return value. This return value
2016 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04002017 * completed successfully (0), or not (in which case cmd->result
2018 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 *
2020 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002021 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 */
Tejun Heof0761be2008-04-28 17:16:52 +09002023static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
Tejun Heo87340e92008-04-28 17:48:51 +09002024 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
2026 u8 *rbuf;
Tejun Heo87340e92008-04-28 17:48:51 +09002027 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 struct scsi_cmnd *cmd = args->cmd;
Jeff Garzikb445c562008-02-29 19:10:51 -05002029 unsigned long flags;
2030
Tejun Heo87340e92008-04-28 17:48:51 +09002031 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
2032 rc = actor(args, rbuf);
2033 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
Jeff Garzikb445c562008-02-29 19:10:51 -05002034
Douglas Gilbertae006512005-10-09 09:09:35 -04002035 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04002037 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
2040/**
2041 * ata_scsiop_inq_std - Simulate INQUIRY command
2042 * @args: device IDENTIFY data / SCSI command of interest.
2043 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 *
2045 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05002046 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 *
2048 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002049 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 */
Tejun Heo87340e92008-04-28 17:48:51 +09002051static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Tejun Heo87340e92008-04-28 17:48:51 +09002053 const u8 versions[] = {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002054 0x00,
Tejun Heo87340e92008-04-28 17:48:51 +09002055 0x60, /* SAM-3 (no version claimed) */
2056
2057 0x03,
2058 0x20, /* SBC-2 (no version claimed) */
2059
2060 0x02,
2061 0x60 /* SPC-3 (no version claimed) */
2062 };
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002063 const u8 versions_zbc[] = {
2064 0x00,
2065 0xA0, /* SAM-5 (no version claimed) */
2066
2067 0x04,
2068 0xC0, /* SBC-3 (no version claimed) */
2069
2070 0x04,
2071 0x60, /* SPC-4 (no version claimed) */
2072
2073 0x60,
2074 0x20, /* ZBC (no version claimed) */
2075 };
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 u8 hdr[] = {
2078 TYPE_DISK,
2079 0,
2080 0x5, /* claim SPC-3 version compatibility */
2081 2,
2082 95 - 4
2083 };
2084
Tejun Heo87340e92008-04-28 17:48:51 +09002085 VPRINTK("ENTER\n");
2086
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02002087 /* set scsi removable (RMB) bit per ata bit, or if the
2088 * AHCI port says it's external (Hotplug-capable, eSATA).
2089 */
2090 if (ata_id_removable(args->id) ||
2091 (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 hdr[1] |= (1 << 7);
2093
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002094 if (args->dev->class == ATA_DEV_ZAC) {
2095 hdr[0] = TYPE_ZBC;
2096 hdr[2] = 0x6; /* ZBC is defined in SPC-4 */
2097 }
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002100 memcpy(&rbuf[8], "ATA ", 8);
2101 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
Keith Buschc49a6bf2014-05-01 11:12:03 -06002102
2103 /* From SAT, use last 2 words from fw rev unless they are spaces */
2104 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
2105 if (strncmp(&rbuf[32], " ", 4) == 0)
2106 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Tejun Heo87340e92008-04-28 17:48:51 +09002108 if (rbuf[32] == 0 || rbuf[32] == ' ')
2109 memcpy(&rbuf[32], "n/a ", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002111 if (args->dev->class == ATA_DEV_ZAC)
2112 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
2113 else
2114 memcpy(rbuf + 58, versions, sizeof(versions));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 return 0;
2117}
2118
2119/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002120 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 * @args: device IDENTIFY data / SCSI command of interest.
2122 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002124 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 *
2126 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002127 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 */
Tejun Heo87340e92008-04-28 17:48:51 +09002129static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 const u8 pages[] = {
2132 0x00, /* page 0x00, this page */
2133 0x80, /* page 0x80, unit serial no page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002134 0x83, /* page 0x83, device ident page */
2135 0x89, /* page 0x89, ata info page */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002136 0xb0, /* page 0xb0, block limits page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002137 0xb1, /* page 0xb1, block device characteristics page */
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002138 0xb2, /* page 0xb2, thin provisioning page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 };
Tejun Heo87340e92008-04-28 17:48:51 +09002140
Jeff Garzikb142eb62006-03-21 20:37:47 -05002141 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
Tejun Heo87340e92008-04-28 17:48:51 +09002142 memcpy(rbuf + 4, pages, sizeof(pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return 0;
2144}
2145
2146/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002147 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 * @args: device IDENTIFY data / SCSI command of interest.
2149 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 *
2151 * Returns ATA device serial number.
2152 *
2153 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002154 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 */
Tejun Heo87340e92008-04-28 17:48:51 +09002156static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 const u8 hdr[] = {
2159 0,
2160 0x80, /* this page code */
2161 0,
Tejun Heoa0cf7332007-01-02 20:18:49 +09002162 ATA_ID_SERNO_LEN, /* page len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 };
Tejun Heo87340e92008-04-28 17:48:51 +09002164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002166 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2167 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return 0;
2169}
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002172 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 * @args: device IDENTIFY data / SCSI command of interest.
2174 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002176 * Yields two logical unit device identification designators:
2177 * - vendor specific ASCII containing the ATA serial number
2178 * - SAT defined "t10 vendor id based" containing ASCII vendor
2179 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 *
2181 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002182 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 */
Tejun Heo87340e92008-04-28 17:48:51 +09002184static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Jeff Garzikb142eb62006-03-21 20:37:47 -05002186 const int sat_model_serial_desc_len = 68;
Tejun Heo87340e92008-04-28 17:48:51 +09002187 int num;
Jeff Garzikb142eb62006-03-21 20:37:47 -05002188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05002190 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Tejun Heo87340e92008-04-28 17:48:51 +09002192 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2193 rbuf[num + 0] = 2;
2194 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2195 num += 4;
2196 ata_id_string(args->id, (unsigned char *) rbuf + num,
2197 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2198 num += ATA_ID_SERNO_LEN;
2199
2200 /* SAT defined lu model and serial numbers descriptor */
2201 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2202 rbuf[num + 0] = 2;
2203 rbuf[num + 1] = 1;
2204 rbuf[num + 3] = sat_model_serial_desc_len;
2205 num += 4;
2206 memcpy(rbuf + num, "ATA ", 8);
2207 num += 8;
2208 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2209 ATA_ID_PROD_LEN);
2210 num += ATA_ID_PROD_LEN;
2211 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2212 ATA_ID_SERNO_LEN);
2213 num += ATA_ID_SERNO_LEN;
2214
Hannes Reinecke6b3b9d72011-03-07 08:56:44 +01002215 if (ata_id_has_wwn(args->id)) {
2216 /* SAT defined lu world wide name */
2217 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2218 rbuf[num + 0] = 1;
2219 rbuf[num + 1] = 3;
2220 rbuf[num + 3] = ATA_ID_WWN_LEN;
2221 num += 4;
2222 ata_id_string(args->id, (unsigned char *) rbuf + num,
2223 ATA_ID_WWN, ATA_ID_WWN_LEN);
2224 num += ATA_ID_WWN_LEN;
2225 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05002226 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return 0;
2228}
2229
2230/**
Jeff Garzikad355b42007-09-21 20:38:03 -04002231 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2232 * @args: device IDENTIFY data / SCSI command of interest.
2233 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Jeff Garzikad355b42007-09-21 20:38:03 -04002234 *
2235 * Yields SAT-specified ATA VPD page.
2236 *
2237 * LOCKING:
2238 * spin_lock_irqsave(host lock)
2239 */
Tejun Heo87340e92008-04-28 17:48:51 +09002240static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
Jeff Garzikad355b42007-09-21 20:38:03 -04002241{
Jeff Garzikad355b42007-09-21 20:38:03 -04002242 struct ata_taskfile tf;
Jeff Garzikad355b42007-09-21 20:38:03 -04002243
Jeff Garzikad355b42007-09-21 20:38:03 -04002244 memset(&tf, 0, sizeof(tf));
2245
Tejun Heo87340e92008-04-28 17:48:51 +09002246 rbuf[1] = 0x89; /* our page code */
2247 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2248 rbuf[3] = (0x238 & 0xff);
Jeff Garzikad355b42007-09-21 20:38:03 -04002249
Tejun Heo87340e92008-04-28 17:48:51 +09002250 memcpy(&rbuf[8], "linux ", 8);
2251 memcpy(&rbuf[16], "libata ", 16);
2252 memcpy(&rbuf[32], DRV_VERSION, 4);
Jeff Garzikad355b42007-09-21 20:38:03 -04002253
2254 /* we don't store the ATA device signature, so we fake it */
2255
2256 tf.command = ATA_DRDY; /* really, this is Status reg */
2257 tf.lbal = 0x1;
2258 tf.nsect = 0x1;
2259
Tejun Heo87340e92008-04-28 17:48:51 +09002260 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
2261 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
Jeff Garzikad355b42007-09-21 20:38:03 -04002262
Tejun Heo87340e92008-04-28 17:48:51 +09002263 rbuf[56] = ATA_CMD_ID_ATA;
Jeff Garzikad355b42007-09-21 20:38:03 -04002264
Tejun Heo87340e92008-04-28 17:48:51 +09002265 memcpy(&rbuf[60], &args->id[0], 512);
Jeff Garzikad355b42007-09-21 20:38:03 -04002266 return 0;
2267}
2268
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002269static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2270{
Grant Grundler295124d2010-08-17 10:56:53 -07002271 u16 min_io_sectors;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002272
2273 rbuf[1] = 0xb0;
2274 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2275
2276 /*
2277 * Optimal transfer length granularity.
2278 *
2279 * This is always one physical block, but for disks with a smaller
2280 * logical than physical sector size we need to figure out what the
2281 * latter is.
2282 */
Grant Grundler295124d2010-08-17 10:56:53 -07002283 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002284 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2285
2286 /*
2287 * Optimal unmap granularity.
2288 *
2289 * The ATA spec doesn't even know about a granularity or alignment
2290 * for the TRIM command. We can leave away most of the unmap related
2291 * VPD page entries, but we have specifify a granularity to signal
2292 * that we support some form of unmap - in thise case via WRITE SAME
2293 * with the unmap bit set.
2294 */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002295 if (ata_id_has_trim(args->id)) {
Martin K. Petersen5f4e2062011-05-17 22:13:23 -04002296 put_unaligned_be64(65535 * 512 / 8, &rbuf[36]);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002297 put_unaligned_be32(1, &rbuf[28]);
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002298 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002299
2300 return 0;
2301}
2302
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002303static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2304{
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002305 int form_factor = ata_id_form_factor(args->id);
2306 int media_rotation_rate = ata_id_rotation_rate(args->id);
2307
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002308 rbuf[1] = 0xb1;
2309 rbuf[3] = 0x3c;
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002310 rbuf[4] = media_rotation_rate >> 8;
2311 rbuf[5] = media_rotation_rate;
2312 rbuf[7] = form_factor;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002313
2314 return 0;
2315}
2316
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002317static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
2318{
2319 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2320 rbuf[1] = 0xb2;
2321 rbuf[3] = 0x4;
2322 rbuf[5] = 1 << 6; /* TPWS */
2323
2324 return 0;
2325}
2326
Jeff Garzikad355b42007-09-21 20:38:03 -04002327/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002328 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 * @args: device IDENTIFY data / SCSI command of interest.
2330 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 *
2332 * No operation. Simply returns success to caller, to indicate
2333 * that the caller should successfully complete this SCSI command.
2334 *
2335 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002336 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 */
Tejun Heo87340e92008-04-28 17:48:51 +09002338static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
2340 VPRINTK("ENTER\n");
2341 return 0;
2342}
2343
2344/**
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002345 * modecpy - Prepare response for MODE SENSE
2346 * @dest: output buffer
2347 * @src: data being copied
2348 * @n: length of mode page
2349 * @changeable: whether changeable parameters are requested
2350 *
2351 * Generate a generic MODE SENSE page for either current or changeable
2352 * parameters.
2353 *
2354 * LOCKING:
2355 * None.
2356 */
2357static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2358{
2359 if (changeable) {
2360 memcpy(dest, src, 2);
2361 memset(dest + 2, 0, n - 2);
2362 } else {
2363 memcpy(dest, src, n);
2364 }
2365}
2366
2367/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 * ata_msense_caching - Simulate MODE SENSE caching info page
2369 * @id: device IDENTIFY data
Tejun Heo87340e92008-04-28 17:48:51 +09002370 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002371 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 *
2373 * Generate a caching info page, which conditionally indicates
2374 * write caching to the SCSI layer, depending on device
2375 * capabilities.
2376 *
2377 * LOCKING:
2378 * None.
2379 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002380static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002382 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02002383 if (changeable || ata_id_wcache_enabled(id))
Tejun Heo87340e92008-04-28 17:48:51 +09002384 buf[2] |= (1 << 2); /* write cache enable */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002385 if (!changeable && !ata_id_rahead_enabled(id))
Tejun Heo87340e92008-04-28 17:48:51 +09002386 buf[12] |= (1 << 5); /* disable read ahead */
2387 return sizeof(def_cache_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
2390/**
2391 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002392 * @dev: ATA device of interest
Tejun Heo87340e92008-04-28 17:48:51 +09002393 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002394 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 *
2396 * Generate a generic MODE SENSE control mode page.
2397 *
2398 * LOCKING:
2399 * None.
2400 */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002401static unsigned int ata_msense_ctl_mode(struct ata_device *dev, u8 *buf,
2402 bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002404 modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002405 if (changeable && (dev->flags & ATA_DFLAG_D_SENSE))
2406 buf[2] |= (1 << 2); /* Descriptor sense requested */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002407 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
2410/**
2411 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002412 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002413 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 *
2415 * Generate a generic MODE SENSE r/w error recovery page.
2416 *
2417 * LOCKING:
2418 * None.
2419 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002420static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002422 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2423 changeable);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002424 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002427/*
2428 * We can turn this into a real blacklist if it's needed, for now just
2429 * blacklist any Maxtor BANC1G10 revision firmware
2430 */
2431static int ata_dev_supports_fua(u16 *id)
2432{
Tejun Heoa0cf7332007-01-02 20:18:49 +09002433 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002434
Jeff Garzikc3c013a2006-02-27 22:31:19 -05002435 if (!libata_fua)
2436 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002437 if (!ata_id_has_fua(id))
2438 return 0;
2439
Tejun Heoa0cf7332007-01-02 20:18:49 +09002440 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2441 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002442
Tejun Heo2e026712006-02-12 22:47:04 +09002443 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002444 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09002445 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002446 return 1;
2447
2448 return 0; /* blacklisted */
2449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/**
2452 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2453 * @args: device IDENTIFY data / SCSI command of interest.
2454 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002456 * Simulate MODE SENSE commands. Assume this is invoked for direct
2457 * access devices (e.g. disks) only. There should be no block
2458 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 *
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_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002465 struct ata_device *dev = args->dev;
Tejun Heo87340e92008-04-28 17:48:51 +09002466 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002467 const u8 sat_blk_desc[] = {
2468 0, 0, 0, 0, /* number of blocks: sat unspecified */
2469 0,
2470 0, 0x2, 0x0 /* block length: 512 bytes */
2471 };
2472 u8 pg, spg;
Tejun Heo87340e92008-04-28 17:48:51 +09002473 unsigned int ebd, page_control, six_byte;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002474 u8 dpofua;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002475 u16 fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 VPRINTK("ENTER\n");
2478
2479 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002480 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2481 /*
2482 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002486 switch (page_control) {
2487 case 0: /* current */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002488 case 1: /* changeable */
2489 case 2: /* defaults */
Douglas Gilbertae006512005-10-09 09:09:35 -04002490 break; /* supported */
2491 case 3: /* saved */
2492 goto saving_not_supp;
Douglas Gilbertae006512005-10-09 09:09:35 -04002493 default:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002494 fp = 2;
Douglas Gilbertae006512005-10-09 09:09:35 -04002495 goto invalid_fld;
2496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Tejun Heo87340e92008-04-28 17:48:51 +09002498 if (six_byte)
2499 p += 4 + (ebd ? 8 : 0);
2500 else
2501 p += 8 + (ebd ? 8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002503 pg = scsicmd[2] & 0x3f;
2504 spg = scsicmd[3];
2505 /*
2506 * No mode subpages supported (yet) but asking for _all_
2507 * subpages may be valid
2508 */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002509 if (spg && (spg != ALL_SUB_MPAGES)) {
2510 fp = 3;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002511 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002512 }
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002513
2514 switch(pg) {
2515 case RW_RECOVERY_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002516 p += ata_msense_rw_recovery(p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 break;
2518
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002519 case CACHE_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002520 p += ata_msense_caching(args->id, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 break;
2522
Tejun Heo87340e92008-04-28 17:48:51 +09002523 case CONTROL_MPAGE:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002524 p += ata_msense_ctl_mode(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002527 case ALL_MPAGES:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002528 p += ata_msense_rw_recovery(p, page_control == 1);
2529 p += ata_msense_caching(args->id, p, page_control == 1);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002530 p += ata_msense_ctl_mode(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 break;
2532
2533 default: /* invalid page code */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002534 fp = 2;
Douglas Gilbertae006512005-10-09 09:09:35 -04002535 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
2537
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002538 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002539 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002540 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2541 dpofua = 1 << 4;
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (six_byte) {
Tejun Heo87340e92008-04-28 17:48:51 +09002544 rbuf[0] = p - rbuf - 1;
2545 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002546 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002547 rbuf[3] = sizeof(sat_blk_desc);
2548 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 } else {
Tejun Heo87340e92008-04-28 17:48:51 +09002551 unsigned int output_len = p - rbuf - 2;
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 rbuf[0] = output_len >> 8;
Tejun Heo87340e92008-04-28 17:48:51 +09002554 rbuf[1] = output_len;
2555 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002556 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002557 rbuf[7] = sizeof(sat_blk_desc);
2558 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002562
2563invalid_fld:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002564 ata_scsi_set_invalid_field(dev, args->cmd, fp);
Douglas Gilbertae006512005-10-09 09:09:35 -04002565 return 1;
2566
2567saving_not_supp:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002568 ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04002569 /* "Saving parameters not supported" */
2570 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
2573/**
2574 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2575 * @args: device IDENTIFY data / SCSI command of interest.
2576 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 *
2578 * Simulate READ CAPACITY commands.
2579 *
2580 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002581 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 */
Tejun Heo87340e92008-04-28 17:48:51 +09002583static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002585 struct ata_device *dev = args->dev;
2586 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
Grant Grundler295124d2010-08-17 10:56:53 -07002587 u32 sector_size; /* physical sector size in bytes */
2588 u8 log2_per_phys;
2589 u16 lowest_aligned;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002590
Grant Grundler295124d2010-08-17 10:56:53 -07002591 sector_size = ata_id_logical_sector_size(dev->id);
2592 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2593 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 VPRINTK("ENTER\n");
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002598 if (last_lba >= 0xffffffffULL)
2599 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 /* sector count, 32-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002602 rbuf[0] = last_lba >> (8 * 3);
2603 rbuf[1] = last_lba >> (8 * 2);
2604 rbuf[2] = last_lba >> (8 * 1);
2605 rbuf[3] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002608 rbuf[4] = sector_size >> (8 * 3);
2609 rbuf[5] = sector_size >> (8 * 2);
2610 rbuf[6] = sector_size >> (8 * 1);
2611 rbuf[7] = sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 } else {
2613 /* sector count, 64-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002614 rbuf[0] = last_lba >> (8 * 7);
2615 rbuf[1] = last_lba >> (8 * 6);
2616 rbuf[2] = last_lba >> (8 * 5);
2617 rbuf[3] = last_lba >> (8 * 4);
2618 rbuf[4] = last_lba >> (8 * 3);
2619 rbuf[5] = last_lba >> (8 * 2);
2620 rbuf[6] = last_lba >> (8 * 1);
2621 rbuf[7] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002624 rbuf[ 8] = sector_size >> (8 * 3);
2625 rbuf[ 9] = sector_size >> (8 * 2);
2626 rbuf[10] = sector_size >> (8 * 1);
2627 rbuf[11] = sector_size;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002628
2629 rbuf[12] = 0;
Grant Grundler295124d2010-08-17 10:56:53 -07002630 rbuf[13] = log2_per_phys;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002631 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2632 rbuf[15] = lowest_aligned;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002633
Arne Fitzenreiter71d126f2015-07-15 13:54:36 +02002634 if (ata_id_has_trim(args->id) &&
2635 !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002636 rbuf[14] |= 0x80; /* LBPME */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002637
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002638 if (ata_id_has_zero_after_trim(args->id) &&
2639 dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
2640 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2641 rbuf[14] |= 0x40; /* LBPRZ */
2642 }
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return 0;
2646}
2647
2648/**
2649 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2650 * @args: device IDENTIFY data / SCSI command of interest.
2651 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 *
2653 * Simulate REPORT LUNS command.
2654 *
2655 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002656 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 */
Tejun Heo87340e92008-04-28 17:48:51 +09002658static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 VPRINTK("ENTER\n");
2661 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2662
2663 return 0;
2664}
2665
Tejun Heo77853bf2006-01-23 13:09:36 +09002666static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002667{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002668 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002669 /* FIXME: not quite right; we don't want the
2670 * translation of taskfile registers into
2671 * a sense descriptors, since that's only
2672 * correct for ATA, not ATAPI
2673 */
Tejun Heo750426a2006-11-14 22:37:35 +09002674 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002675 }
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002676
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002677 ata_qc_done(qc);
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002678}
2679
2680/* is it pointless to prefer PIO for "safety reasons"? */
2681static inline int ata_pio_use_silly(struct ata_port *ap)
2682{
2683 return (ap->flags & ATA_FLAG_PIO_DMA);
2684}
2685
2686static void atapi_request_sense(struct ata_queued_cmd *qc)
2687{
2688 struct ata_port *ap = qc->ap;
2689 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002690
2691 DPRINTK("ATAPI request sense\n");
2692
James Bottomley7ccd7202008-01-17 11:56:24 -06002693 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Jeff Garzika939c962005-10-05 17:09:16 -04002694
Tejun Heo127102a2008-04-07 22:47:21 +09002695#ifdef CONFIG_ATA_SFF
James Bottomley855d8542008-04-18 13:18:48 -05002696 if (ap->ops->sff_tf_read)
2697 ap->ops->sff_tf_read(ap, &qc->tf);
Tejun Heo127102a2008-04-07 22:47:21 +09002698#endif
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002699
2700 /* fill these in, for the case where they are -not- overwritten */
2701 cmd->sense_buffer[0] = 0x70;
2702 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2703
2704 ata_qc_reinit(qc);
2705
Tejun Heo93f8fec2007-12-05 16:43:01 +09002706 /* setup sg table and init transfer direction */
akpm@linux-foundation.orgcadb7342008-01-15 16:01:52 -08002707 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
Tejun Heo93f8fec2007-12-05 16:43:01 +09002708 ata_sg_init(qc, &qc->sgent, 1);
Jeff Garzika939c962005-10-05 17:09:16 -04002709 qc->dma_dir = DMA_FROM_DEVICE;
2710
Tejun Heo6e7846e2006-02-12 23:32:58 +09002711 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002712 qc->cdb[0] = REQUEST_SENSE;
2713 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2714
2715 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2716 qc->tf.command = ATA_CMD_PACKET;
2717
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002718 if (ata_pio_use_silly(ap)) {
Tejun Heo0dc36882007-12-18 16:34:43 -05002719 qc->tf.protocol = ATAPI_PROT_DMA;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002720 qc->tf.feature |= ATAPI_PKT_DMA;
2721 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05002722 qc->tf.protocol = ATAPI_PROT_PIO;
Alan Cox2db78dd2007-10-02 13:53:04 -07002723 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2724 qc->tf.lbah = 0;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002725 }
Jeff Garzika939c962005-10-05 17:09:16 -04002726 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2727
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002728 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002729
Tejun Heo8e0e6942006-03-31 20:41:11 +09002730 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002731
2732 DPRINTK("EXIT\n");
2733}
2734
Tejun Heo77853bf2006-01-23 13:09:36 +09002735static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
2737 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002738 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Jeff Garzika7dac442005-10-30 04:44:42 -05002740 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002741
Tejun Heo246619d2006-05-15 20:58:16 +09002742 /* handle completion from new EH */
2743 if (unlikely(qc->ap->ops->error_handler &&
2744 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2745
2746 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2747 /* FIXME: not quite right; we don't want the
2748 * translation of taskfile registers into a
2749 * sense descriptors, since that's only
2750 * correct for ATA, not ATAPI
2751 */
Tejun Heo750426a2006-11-14 22:37:35 +09002752 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002753 }
2754
Tejun Heo22aac082006-08-08 14:08:59 +09002755 /* SCSI EH automatically locks door if sdev->locked is
2756 * set. Sometimes door lock request continues to
2757 * fail, for example, when no media is present. This
2758 * creates a loop - SCSI EH issues door lock which
2759 * fails and gets invoked again to acquire sense data
2760 * for the failed command.
2761 *
2762 * If door lock fails, always clear sdev->locked to
2763 * avoid this infinite loop.
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002764 *
2765 * This may happen before SCSI scan is complete. Make
2766 * sure qc->dev->sdev isn't NULL before dereferencing.
Tejun Heo22aac082006-08-08 14:08:59 +09002767 */
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002768 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
Tejun Heo22aac082006-08-08 14:08:59 +09002769 qc->dev->sdev->locked = 0;
2770
Tejun Heo246619d2006-05-15 20:58:16 +09002771 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002772 ata_qc_done(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002773 return;
2774 }
2775
2776 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002777 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002779 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002780 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002781 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002782 /* FIXME: not quite right; we don't want the
2783 * translation of taskfile registers into
2784 * a sense descriptors, since that's only
2785 * correct for ATA, not ATAPI
2786 */
Tejun Heo750426a2006-11-14 22:37:35 +09002787 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002788 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 u8 *scsicmd = cmd->cmnd;
2790
Tony Battersbyfd71da42005-12-21 16:35:44 -05002791 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Jeff Garzikb445c562008-02-29 19:10:51 -05002792 unsigned long flags;
Tejun Heo87340e92008-04-28 17:48:51 +09002793 u8 *buf;
Jeff Garzikb445c562008-02-29 19:10:51 -05002794
Tejun Heo87340e92008-04-28 17:48:51 +09002795 buf = ata_scsi_rbuf_get(cmd, true, &flags);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002796
2797 /* ATAPI devices typically report zero for their SCSI version,
2798 * and sometimes deviate from the spec WRT response data
2799 * format. If SCSI version is reported as zero like normal,
2800 * then we make the following fixups: 1) Fake MMC-5 version,
2801 * to indicate to the Linux scsi midlayer this is a modern
2802 * device. 2) Ensure response data format / ATAPI information
2803 * are always correct.
2804 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002805 if (buf[2] == 0) {
2806 buf[2] = 0x5;
2807 buf[3] = 0x32;
2808 }
2809
Tejun Heo87340e92008-04-28 17:48:51 +09002810 ata_scsi_rbuf_put(cmd, true, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 cmd->result = SAM_STAT_GOOD;
2814 }
2815
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002816 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817}
2818/**
2819 * atapi_xlat - Initialize PACKET taskfile
2820 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 *
2822 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002823 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 *
2825 * RETURNS:
2826 * Zero on success, non-zero on failure.
2827 */
Tejun Heoad706992006-12-17 10:45:57 +09002828static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Tejun Heo542b1442006-12-17 10:45:08 +09002830 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 struct ata_device *dev = qc->dev;
Tejun Heo542b1442006-12-17 10:45:08 +09002832 int nodata = (scmd->sc_data_direction == DMA_NONE);
Tejun Heo5895ef92008-06-17 12:36:26 +09002833 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
Alan Cox2db78dd2007-10-02 13:53:04 -07002834 unsigned int nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Tejun Heo2e5704f2006-12-17 10:46:33 +09002836 memset(qc->cdb, 0, dev->cdb_len);
2837 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
2839 qc->complete_fn = atapi_qc_complete;
2840
2841 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002842 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 qc->tf.flags |= ATA_TFLAG_WRITE;
2844 DPRINTK("direction: write\n");
2845 }
2846
2847 qc->tf.command = ATA_CMD_PACKET;
Tejun Heoaacda372008-03-18 17:47:43 +09002848 ata_qc_set_pc_nbytes(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002850 /* check whether ATAPI DMA is safe */
Tejun Heo5895ef92008-06-17 12:36:26 +09002851 if (!nodata && !using_pio && atapi_check_dma(qc))
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002852 using_pio = 1;
2853
Tejun Heoe1902222007-11-26 20:58:02 +09002854 /* Some controller variants snoop this value for Packet
2855 * transfers to do state machine and FIFO management. Thus we
2856 * want to set it properly, and for DMA where it is
2857 * effectively meaningless.
2858 */
Tejun Heoaacda372008-03-18 17:47:43 +09002859 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
Alan Cox2db78dd2007-10-02 13:53:04 -07002860
Tejun Heoe1902222007-11-26 20:58:02 +09002861 /* Most ATAPI devices which honor transfer chunk size don't
2862 * behave according to the spec when odd chunk size which
2863 * matches the transfer length is specified. If the number of
2864 * bytes to transfer is 2n+1. According to the spec, what
2865 * should happen is to indicate that 2n+1 is going to be
2866 * transferred and transfer 2n+2 bytes where the last byte is
2867 * padding.
2868 *
2869 * In practice, this doesn't happen. ATAPI devices first
2870 * indicate and transfer 2n bytes and then indicate and
2871 * transfer 2 bytes where the last byte is padding.
2872 *
2873 * This inconsistency confuses several controllers which
2874 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2875 * These controllers use actual number of transferred bytes to
2876 * update DMA poitner and transfer of 4n+2 bytes make those
2877 * controller push DMA pointer by 4n+4 bytes because SATA data
2878 * FISes are aligned to 4 bytes. This causes data corruption
2879 * and buffer overrun.
2880 *
2881 * Always setting nbytes to even number solves this problem
2882 * because then ATAPI devices don't have to split data at 2n
2883 * boundaries.
2884 */
2885 if (nbytes & 0x1)
2886 nbytes++;
2887
Alan Cox2db78dd2007-10-02 13:53:04 -07002888 qc->tf.lbam = (nbytes & 0xFF);
2889 qc->tf.lbah = (nbytes >> 8);
2890
Tejun Heo5895ef92008-06-17 12:36:26 +09002891 if (nodata)
2892 qc->tf.protocol = ATAPI_PROT_NODATA;
2893 else if (using_pio)
2894 qc->tf.protocol = ATAPI_PROT_PIO;
2895 else {
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002896 /* DMA data xfer */
Tejun Heo0dc36882007-12-18 16:34:43 -05002897 qc->tf.protocol = ATAPI_PROT_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 qc->tf.feature |= ATAPI_PKT_DMA;
2899
Tejun Heo91163002008-02-21 13:25:50 +09002900 if ((dev->flags & ATA_DFLAG_DMADIR) &&
2901 (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08002902 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905
Alan Cox2db78dd2007-10-02 13:53:04 -07002906
2907 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2908 as ATAPI tape drives don't get this right otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 return 0;
2910}
2911
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002912static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
Tejun Heoab5b3a52006-05-31 18:27:34 +09002913{
Tejun Heo071f44b2008-04-07 22:47:22 +09002914 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09002915 if (likely(devno < ata_link_max_devices(&ap->link)))
2916 return &ap->link.device[devno];
2917 } else {
2918 if (likely(devno < ap->nr_pmp_links))
2919 return &ap->pmp_link[devno].device[0];
2920 }
2921
Tejun Heoab5b3a52006-05-31 18:27:34 +09002922 return NULL;
2923}
2924
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002925static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
2926 const struct scsi_device *scsidev)
Tejun Heoab5b3a52006-05-31 18:27:34 +09002927{
Tejun Heo41bda9c2007-08-06 18:36:24 +09002928 int devno;
Tejun Heoab5b3a52006-05-31 18:27:34 +09002929
Tejun Heo41bda9c2007-08-06 18:36:24 +09002930 /* skip commands not addressed to targets we simulate */
Tejun Heo071f44b2008-04-07 22:47:22 +09002931 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09002932 if (unlikely(scsidev->channel || scsidev->lun))
2933 return NULL;
2934 devno = scsidev->id;
2935 } else {
2936 if (unlikely(scsidev->id || scsidev->lun))
2937 return NULL;
2938 devno = scsidev->channel;
2939 }
2940
2941 return ata_find_dev(ap, devno);
Tejun Heoab5b3a52006-05-31 18:27:34 +09002942}
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944/**
2945 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2946 * @ap: ATA port to which the device is attached
2947 * @scsidev: SCSI device from which we derive the ATA device
2948 *
2949 * Given various information provided in struct scsi_cmnd,
2950 * map that onto an ATA bus, and using that mapping
2951 * determine which ata_device is associated with the
2952 * SCSI command to be sent.
2953 *
2954 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002955 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 *
2957 * RETURNS:
2958 * Associated ATA device, or %NULL if not found.
2959 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04002961ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Tejun Heoab5b3a52006-05-31 18:27:34 +09002963 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Tejun Heo2486fa52008-07-31 07:52:40 +09002965 if (unlikely(!dev || !ata_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return NULL;
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 return dev;
2969}
2970
Jeff Garzikb0955182005-05-12 15:45:22 -04002971/*
2972 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2973 * @byte1: Byte 1 from pass-thru CDB.
2974 *
2975 * RETURNS:
2976 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2977 */
2978static u8
2979ata_scsi_map_proto(u8 byte1)
2980{
2981 switch((byte1 & 0x1e) >> 1) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002982 case 3: /* Non-data */
2983 return ATA_PROT_NODATA;
Jeff Garzikb0955182005-05-12 15:45:22 -04002984
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002985 case 6: /* DMA */
2986 case 10: /* UDMA Data-in */
2987 case 11: /* UDMA Data-Out */
2988 return ATA_PROT_DMA;
Jeff Garzikb0955182005-05-12 15:45:22 -04002989
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002990 case 4: /* PIO Data-in */
2991 case 5: /* PIO Data-out */
2992 return ATA_PROT_PIO;
Jeff Garzikb0955182005-05-12 15:45:22 -04002993
Vinayak Kaleee7fb332015-10-27 11:19:15 +05302994 case 12: /* FPDMA */
2995 return ATA_PROT_NCQ;
2996
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002997 case 0: /* Hard Reset */
2998 case 1: /* SRST */
2999 case 8: /* Device Diagnostic */
3000 case 9: /* Device Reset */
3001 case 7: /* DMA Queued */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003002 case 15: /* Return Response Info */
3003 default: /* Reserved */
3004 break;
Jeff Garzikb0955182005-05-12 15:45:22 -04003005 }
3006
3007 return ATA_PROT_UNKNOWN;
3008}
3009
3010/**
3011 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
3012 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04003013 *
3014 * Handles either 12 or 16-byte versions of the CDB.
3015 *
3016 * RETURNS:
3017 * Zero on success, non-zero on failure.
3018 */
Tejun Heoad706992006-12-17 10:45:57 +09003019static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04003020{
3021 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09003022 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01003023 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09003024 const u8 *cdb = scmd->cmnd;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003025 u16 fp;
Jeff Garzikb0955182005-05-12 15:45:22 -04003026
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003027 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN) {
3028 fp = 1;
Tejun Heo9a405252005-12-02 11:49:11 +09003029 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003030 }
Jeff Garzik8190bdb2006-05-24 01:53:39 -04003031
Vinayak Kale5f8e7f12015-10-27 13:11:46 +05303032 /* enable LBA */
3033 tf->flags |= ATA_TFLAG_LBA;
3034
Alan Coxae8d4ee2007-11-04 22:05:49 -05003035 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04003036 * 12 and 16 byte CDBs use different offsets to
3037 * provide the various register values.
3038 */
Tejun Heo542b1442006-12-17 10:45:08 +09003039 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04003040 /*
3041 * 16-byte CDB - may contain extended commands.
3042 *
3043 * If that is the case, copy the upper byte register values.
3044 */
Tejun Heo542b1442006-12-17 10:45:08 +09003045 if (cdb[1] & 0x01) {
3046 tf->hob_feature = cdb[3];
3047 tf->hob_nsect = cdb[5];
3048 tf->hob_lbal = cdb[7];
3049 tf->hob_lbam = cdb[9];
3050 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04003051 tf->flags |= ATA_TFLAG_LBA48;
3052 } else
3053 tf->flags &= ~ATA_TFLAG_LBA48;
3054
3055 /*
3056 * Always copy low byte, device and command registers.
3057 */
Tejun Heo542b1442006-12-17 10:45:08 +09003058 tf->feature = cdb[4];
3059 tf->nsect = cdb[6];
3060 tf->lbal = cdb[8];
3061 tf->lbam = cdb[10];
3062 tf->lbah = cdb[12];
3063 tf->device = cdb[13];
3064 tf->command = cdb[14];
Jeff Garzikb0955182005-05-12 15:45:22 -04003065 } else {
3066 /*
3067 * 12-byte CDB - incapable of extended commands.
3068 */
3069 tf->flags &= ~ATA_TFLAG_LBA48;
3070
Tejun Heo542b1442006-12-17 10:45:08 +09003071 tf->feature = cdb[3];
3072 tf->nsect = cdb[4];
3073 tf->lbal = cdb[5];
3074 tf->lbam = cdb[6];
3075 tf->lbah = cdb[7];
3076 tf->device = cdb[8];
3077 tf->command = cdb[9];
Jeff Garzikb0955182005-05-12 15:45:22 -04003078 }
Albert Leefa4453c2007-06-07 15:52:07 +08003079
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303080 /* For NCQ commands with FPDMA protocol, copy the tag value */
3081 if (tf->protocol == ATA_PROT_NCQ)
3082 tf->nsect = qc->tag << 3;
3083
Albert Leefa4453c2007-06-07 15:52:07 +08003084 /* enforce correct master/slave bit */
3085 tf->device = dev->devno ?
3086 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003087
Tejun Heobd30add2009-09-03 16:08:11 +09003088 switch (tf->command) {
Grant Grundler295124d2010-08-17 10:56:53 -07003089 /* READ/WRITE LONG use a non-standard sect_size */
Tejun Heobd30add2009-09-03 16:08:11 +09003090 case ATA_CMD_READ_LONG:
3091 case ATA_CMD_READ_LONG_ONCE:
3092 case ATA_CMD_WRITE_LONG:
3093 case ATA_CMD_WRITE_LONG_ONCE:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003094 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
3095 fp = 1;
Tejun Heobd30add2009-09-03 16:08:11 +09003096 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003097 }
Tejun Heobd30add2009-09-03 16:08:11 +09003098 qc->sect_size = scsi_bufflen(scmd);
Grant Grundler295124d2010-08-17 10:56:53 -07003099 break;
3100
3101 /* commands using reported Logical Block size (e.g. 512 or 4K) */
3102 case ATA_CMD_CFA_WRITE_NE:
3103 case ATA_CMD_CFA_TRANS_SECT:
3104 case ATA_CMD_CFA_WRITE_MULT_NE:
3105 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
3106 case ATA_CMD_READ:
3107 case ATA_CMD_READ_EXT:
3108 case ATA_CMD_READ_QUEUED:
3109 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
3110 case ATA_CMD_FPDMA_READ:
3111 case ATA_CMD_READ_MULTI:
3112 case ATA_CMD_READ_MULTI_EXT:
3113 case ATA_CMD_PIO_READ:
3114 case ATA_CMD_PIO_READ_EXT:
3115 case ATA_CMD_READ_STREAM_DMA_EXT:
3116 case ATA_CMD_READ_STREAM_EXT:
3117 case ATA_CMD_VERIFY:
3118 case ATA_CMD_VERIFY_EXT:
3119 case ATA_CMD_WRITE:
3120 case ATA_CMD_WRITE_EXT:
3121 case ATA_CMD_WRITE_FUA_EXT:
3122 case ATA_CMD_WRITE_QUEUED:
3123 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3124 case ATA_CMD_FPDMA_WRITE:
3125 case ATA_CMD_WRITE_MULTI:
3126 case ATA_CMD_WRITE_MULTI_EXT:
3127 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3128 case ATA_CMD_PIO_WRITE:
3129 case ATA_CMD_PIO_WRITE_EXT:
3130 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3131 case ATA_CMD_WRITE_STREAM_EXT:
3132 qc->sect_size = scmd->device->sector_size;
3133 break;
3134
3135 /* Everything else uses 512 byte "sectors" */
3136 default:
3137 qc->sect_size = ATA_SECT_SIZE;
Tejun Heobd30add2009-09-03 16:08:11 +09003138 }
3139
3140 /*
3141 * Set flags so that all registers will be written, pass on
3142 * write indication (used for PIO/DMA setup), result TF is
3143 * copied back and we don't whine too much about its failure.
3144 */
Douglas Gilbertbc496ed2010-02-01 13:11:38 -05003145 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heobd30add2009-09-03 16:08:11 +09003146 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3147 tf->flags |= ATA_TFLAG_WRITE;
3148
3149 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3150
3151 /*
3152 * Set transfer length.
3153 *
3154 * TODO: find out if we need to do more here to
3155 * cover scatter/gather case.
3156 */
3157 ata_qc_set_pc_nbytes(qc);
3158
3159 /* We may not issue DMA commands if no DMA mode is set */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003160 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) {
3161 fp = 1;
Tejun Heobd30add2009-09-03 16:08:11 +09003162 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003163 }
Tejun Heobd30add2009-09-03 16:08:11 +09003164
Albert Lee1dce5892007-06-07 15:49:22 +08003165 /* sanity check for pio multi commands */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003166 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
3167 fp = 1;
Albert Lee1dce5892007-06-07 15:49:22 +08003168 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003169 }
Albert Lee1dce5892007-06-07 15:49:22 +08003170
3171 if (is_multi_taskfile(tf)) {
3172 unsigned int multi_count = 1 << (cdb[1] >> 5);
3173
3174 /* compare the passed through multi_count
3175 * with the cached multi_count of libata
3176 */
3177 if (multi_count != dev->multi_count)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003178 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3179 multi_count);
Alan Coxd26fc952007-07-06 19:13:52 -04003180 }
Albert Lee1dce5892007-06-07 15:49:22 +08003181
Jeff Garzikb0955182005-05-12 15:45:22 -04003182 /*
3183 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3184 * SET_FEATURES - XFER MODE must be preceded/succeeded
3185 * by an update to hardware-specific registers for each
3186 * controller (i.e. the reason for ->set_piomode(),
3187 * ->set_dmamode(), and ->post_set_mode() hooks).
3188 */
Tejun Heobd30add2009-09-03 16:08:11 +09003189 if (tf->command == ATA_CMD_SET_FEATURES &&
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003190 tf->feature == SETFEATURES_XFER) {
3191 fp = (cdb[0] == ATA_16) ? 4 : 3;
Tejun Heo9a405252005-12-02 11:49:11 +09003192 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003193 }
Jeff Garzikb0955182005-05-12 15:45:22 -04003194
3195 /*
Tejun Heobd30add2009-09-03 16:08:11 +09003196 * Filter TPM commands by default. These provide an
3197 * essentially uncontrolled encrypted "back door" between
3198 * applications and the disk. Set libata.allow_tpm=1 if you
3199 * have a real reason for wanting to use them. This ensures
3200 * that installed software cannot easily mess stuff up without
3201 * user intent. DVR type users will probably ship with this enabled
3202 * for movie content management.
Jeff Garzikb0955182005-05-12 15:45:22 -04003203 *
Tejun Heobd30add2009-09-03 16:08:11 +09003204 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3205 * for this and should do in future but that it is not sufficient as
3206 * DCS is an optional feature set. Thus we also do the software filter
3207 * so that we comply with the TC consortium stated goal that the user
3208 * can turn off TC features of their system.
Jeff Garzikb0955182005-05-12 15:45:22 -04003209 */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003210 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
3211 fp = (cdb[0] == ATA_16) ? 14 : 9;
Tejun Heobd30add2009-09-03 16:08:11 +09003212 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003213 }
Tejun Heoe61e0672006-05-15 20:57:40 +09003214
Jeff Garzikb0955182005-05-12 15:45:22 -04003215 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09003216
3217 invalid_fld:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003218 ata_scsi_set_invalid_field(dev, scmd, fp);
Tejun Heo9a405252005-12-02 11:49:11 +09003219 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003220}
3221
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003222static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3223{
3224 struct ata_taskfile *tf = &qc->tf;
3225 struct scsi_cmnd *scmd = qc->scsicmd;
3226 struct ata_device *dev = qc->dev;
3227 const u8 *cdb = scmd->cmnd;
3228 u64 block;
3229 u32 n_block;
3230 u32 size;
3231 void *buf;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003232 u16 fp;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003233
3234 /* we may not issue DMA commands if no DMA mode is set */
3235 if (unlikely(!dev->dma_mode))
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003236 goto invalid_opcode;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003237
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003238 if (unlikely(scmd->cmd_len < 16)) {
3239 fp = 15;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003240 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003241 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003242 scsi_16_lba_len(cdb, &block, &n_block);
3243
3244 /* for now we only support WRITE SAME with the unmap bit set */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003245 if (unlikely(!(cdb[1] & 0x8))) {
3246 fp = 1;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003247 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003248 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003249
3250 /*
3251 * WRITE SAME always has a sector sized buffer as payload, this
3252 * should never be a multiple entry S/G list.
3253 */
3254 if (!scsi_sg_count(scmd))
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003255 goto invalid_param_len;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003256
3257 buf = page_address(sg_page(scsi_sglist(scmd)));
Martin K. Petersend0634c42009-11-26 12:00:43 -05003258 size = ata_set_lba_range_entries(buf, 512, block, n_block);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003259
Marc Carino87fb6c32013-08-24 23:22:51 -07003260 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3261 /* Newer devices support queued TRIM commands */
3262 tf->protocol = ATA_PROT_NCQ;
3263 tf->command = ATA_CMD_FPDMA_SEND;
3264 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
3265 tf->nsect = qc->tag << 3;
3266 tf->hob_feature = (size / 512) >> 8;
3267 tf->feature = size / 512;
3268
3269 tf->auxiliary = 1;
3270 } else {
3271 tf->protocol = ATA_PROT_DMA;
3272 tf->hob_feature = 0;
3273 tf->feature = ATA_DSM_TRIM;
3274 tf->hob_nsect = (size / 512) >> 8;
3275 tf->nsect = size / 512;
3276 tf->command = ATA_CMD_DSM;
3277 }
3278
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003279 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3280 ATA_TFLAG_WRITE;
3281
3282 ata_qc_set_pc_nbytes(qc);
3283
3284 return 0;
3285
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003286invalid_fld:
3287 ata_scsi_set_invalid_field(dev, scmd, fp);
3288 return 1;
3289invalid_param_len:
3290 /* "Parameter list length error" */
3291 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3292 return 1;
3293invalid_opcode:
3294 /* "Invalid command operation code" */
3295 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003296 return 1;
3297}
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003300 * ata_mselect_caching - Simulate MODE SELECT for caching info page
3301 * @qc: Storage for translated ATA taskfile
3302 * @buf: input buffer
3303 * @len: number of valid bytes in the input buffer
3304 *
3305 * Prepare a taskfile to modify caching information for the device.
3306 *
3307 * LOCKING:
3308 * None.
3309 */
3310static int ata_mselect_caching(struct ata_queued_cmd *qc,
3311 const u8 *buf, int len)
3312{
3313 struct ata_taskfile *tf = &qc->tf;
3314 struct ata_device *dev = qc->dev;
3315 char mpage[CACHE_MPAGE_LEN];
3316 u8 wce;
3317
3318 /*
3319 * The first two bytes of def_cache_mpage are a header, so offsets
3320 * in mpage are off by 2 compared to buf. Same for len.
3321 */
3322
3323 if (len != CACHE_MPAGE_LEN - 2)
3324 return -EINVAL;
3325
3326 wce = buf[0] & (1 << 2);
3327
3328 /*
3329 * Check that read-only bits are not modified.
3330 */
3331 ata_msense_caching(dev->id, mpage, false);
3332 mpage[2] &= ~(1 << 2);
3333 mpage[2] |= wce;
3334 if (memcmp(mpage + 2, buf, CACHE_MPAGE_LEN - 2) != 0)
3335 return -EINVAL;
3336
3337 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3338 tf->protocol = ATA_PROT_NODATA;
3339 tf->nsect = 0;
3340 tf->command = ATA_CMD_SET_FEATURES;
3341 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3342 return 0;
3343}
3344
3345/**
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003346 * ata_mselect_control - Simulate MODE SELECT for control page
3347 * @qc: Storage for translated ATA taskfile
3348 * @buf: input buffer
3349 * @len: number of valid bytes in the input buffer
3350 *
3351 * Prepare a taskfile to modify caching information for the device.
3352 *
3353 * LOCKING:
3354 * None.
3355 */
3356static int ata_mselect_control(struct ata_queued_cmd *qc,
3357 const u8 *buf, int len)
3358{
3359 struct ata_device *dev = qc->dev;
3360 char mpage[CONTROL_MPAGE_LEN];
3361 u8 d_sense;
3362
3363 /*
3364 * The first two bytes of def_control_mpage are a header, so offsets
3365 * in mpage are off by 2 compared to buf. Same for len.
3366 */
3367
3368 if (len != CONTROL_MPAGE_LEN - 2)
3369 return -EINVAL;
3370
3371 d_sense = buf[0] & (1 << 2);
3372
3373 /*
3374 * Check that read-only bits are not modified.
3375 */
3376 ata_msense_ctl_mode(dev, mpage, false);
3377 mpage[2] &= ~(1 << 2);
3378 mpage[2] |= d_sense;
3379 if (memcmp(mpage + 2, buf, CONTROL_MPAGE_LEN - 2) != 0)
3380 return -EINVAL;
3381 if (d_sense & (1 << 2))
3382 dev->flags |= ATA_DFLAG_D_SENSE;
3383 else
3384 dev->flags &= ~ATA_DFLAG_D_SENSE;
3385 qc->scsicmd->result = SAM_STAT_GOOD;
3386 qc->scsicmd->scsi_done(qc->scsicmd);
3387 return 0;
3388}
3389
3390/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003391 * ata_scsiop_mode_select - Simulate MODE SELECT 6, 10 commands
3392 * @qc: Storage for translated ATA taskfile
3393 *
3394 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
3395 * Assume this is invoked for direct access devices (e.g. disks) only.
3396 * There should be no block descriptor for other device types.
3397 *
3398 * LOCKING:
3399 * spin_lock_irqsave(host lock)
3400 */
3401static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
3402{
3403 struct scsi_cmnd *scmd = qc->scsicmd;
3404 const u8 *cdb = scmd->cmnd;
3405 const u8 *p;
3406 u8 pg, spg;
3407 unsigned six_byte, pg_len, hdr_len, bd_len;
3408 int len;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003409 u16 fp;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003410
3411 VPRINTK("ENTER\n");
3412
3413 six_byte = (cdb[0] == MODE_SELECT);
3414 if (six_byte) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003415 if (scmd->cmd_len < 5) {
3416 fp = 4;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003417 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003418 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003419
3420 len = cdb[4];
3421 hdr_len = 4;
3422 } else {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003423 if (scmd->cmd_len < 9) {
3424 fp = 8;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003425 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003426 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003427
3428 len = (cdb[7] << 8) + cdb[8];
3429 hdr_len = 8;
3430 }
3431
3432 /* We only support PF=1, SP=0. */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003433 if ((cdb[1] & 0x11) != 0x10) {
3434 fp = 1;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003435 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003436 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003437
3438 /* Test early for possible overrun. */
3439 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
3440 goto invalid_param_len;
3441
3442 p = page_address(sg_page(scsi_sglist(scmd)));
3443
3444 /* Move past header and block descriptors. */
3445 if (len < hdr_len)
3446 goto invalid_param_len;
3447
3448 if (six_byte)
3449 bd_len = p[3];
3450 else
3451 bd_len = (p[6] << 8) + p[7];
3452
3453 len -= hdr_len;
3454 p += hdr_len;
3455 if (len < bd_len)
3456 goto invalid_param_len;
3457 if (bd_len != 0 && bd_len != 8)
3458 goto invalid_param;
3459
3460 len -= bd_len;
3461 p += bd_len;
3462 if (len == 0)
3463 goto skip;
3464
3465 /* Parse both possible formats for the mode page headers. */
3466 pg = p[0] & 0x3f;
3467 if (p[0] & 0x40) {
3468 if (len < 4)
3469 goto invalid_param_len;
3470
3471 spg = p[1];
3472 pg_len = (p[2] << 8) | p[3];
3473 p += 4;
3474 len -= 4;
3475 } else {
3476 if (len < 2)
3477 goto invalid_param_len;
3478
3479 spg = 0;
3480 pg_len = p[1];
3481 p += 2;
3482 len -= 2;
3483 }
3484
3485 /*
3486 * No mode subpages supported (yet) but asking for _all_
3487 * subpages may be valid
3488 */
3489 if (spg && (spg != ALL_SUB_MPAGES))
3490 goto invalid_param;
3491 if (pg_len > len)
3492 goto invalid_param_len;
3493
3494 switch (pg) {
3495 case CACHE_MPAGE:
3496 if (ata_mselect_caching(qc, p, pg_len) < 0)
3497 goto invalid_param;
3498 break;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003499 case CONTROL_MPAGE:
3500 if (ata_mselect_control(qc, p, pg_len) < 0)
3501 goto invalid_param;
3502 break;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003503 default: /* invalid page code */
3504 goto invalid_param;
3505 }
3506
3507 /*
3508 * Only one page has changeable data, so we only support setting one
3509 * page at a time.
3510 */
3511 if (len > pg_len)
3512 goto invalid_param;
3513
3514 return 0;
3515
3516 invalid_fld:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003517 ata_scsi_set_invalid_field(qc->dev, scmd, fp);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003518 return 1;
3519
3520 invalid_param:
3521 /* "Invalid field in parameter list" */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003522 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x26, 0x0);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003523 return 1;
3524
3525 invalid_param_len:
3526 /* "Parameter list length error" */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003527 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003528 return 1;
3529
3530 skip:
3531 scmd->result = SAM_STAT_GOOD;
3532 return 1;
3533}
3534
3535/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 * ata_get_xlat_func - check if SCSI to ATA translation is possible
3537 * @dev: ATA device
3538 * @cmd: SCSI command opcode to consider
3539 *
3540 * Look up the SCSI command given, and determine whether the
3541 * SCSI command is to be translated or simulated.
3542 *
3543 * RETURNS:
3544 * Pointer to translation function if possible, %NULL if not.
3545 */
3546
3547static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
3548{
3549 switch (cmd) {
3550 case READ_6:
3551 case READ_10:
3552 case READ_16:
3553
3554 case WRITE_6:
3555 case WRITE_10:
3556 case WRITE_16:
3557 return ata_scsi_rw_xlat;
3558
Christoph Hellwig0cdd6eb2009-12-10 10:36:01 +01003559 case WRITE_SAME_16:
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003560 return ata_scsi_write_same_xlat;
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 case SYNCHRONIZE_CACHE:
3563 if (ata_try_flush_cache(dev))
3564 return ata_scsi_flush_xlat;
3565 break;
3566
3567 case VERIFY:
3568 case VERIFY_16:
3569 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04003570
3571 case ATA_12:
3572 case ATA_16:
3573 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04003574
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003575 case MODE_SELECT:
3576 case MODE_SELECT_10:
3577 return ata_scsi_mode_select_xlat;
3578 break;
3579
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04003580 case START_STOP:
3581 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 }
3583
3584 return NULL;
3585}
3586
3587/**
3588 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
3589 * @ap: ATA port to which the command was being sent
3590 * @cmd: SCSI command to dump
3591 *
3592 * Prints the contents of a SCSI command via printk().
3593 */
3594
3595static inline void ata_scsi_dump_cdb(struct ata_port *ap,
3596 struct scsi_cmnd *cmd)
3597{
3598#ifdef ATA_DEBUG
3599 struct scsi_device *scsidev = cmd->device;
3600 u8 *scsicmd = cmd->cmnd;
3601
3602 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09003603 ap->print_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 scsidev->channel, scsidev->id, scsidev->lun,
3605 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
3606 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
3607 scsicmd[8]);
3608#endif
3609}
3610
Tejun Heo542b1442006-12-17 10:45:08 +09003611static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09003612 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06003613{
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003614 u8 scsi_op = scmd->cmnd[0];
3615 ata_xlat_func_t xlat_func;
Tejun Heo2115ea92006-05-15 21:03:39 +09003616 int rc = 0;
3617
Hannes Reinecke9162c652014-11-05 13:08:21 +01003618 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003619 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
3620 goto bad_cdb_len;
3621
3622 xlat_func = ata_get_xlat_func(dev, scsi_op);
3623 } else {
3624 if (unlikely(!scmd->cmd_len))
3625 goto bad_cdb_len;
3626
3627 xlat_func = NULL;
3628 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
3629 /* relay SCSI command to ATAPI device */
Mark Lord607126c2007-11-15 13:13:59 +09003630 int len = COMMAND_SIZE(scsi_op);
3631 if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003632 goto bad_cdb_len;
3633
3634 xlat_func = atapi_xlat;
3635 } else {
3636 /* ATA_16 passthru, treat as an ATA command */
3637 if (unlikely(scmd->cmd_len > 16))
3638 goto bad_cdb_len;
3639
3640 xlat_func = ata_get_xlat_func(dev, scsi_op);
3641 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09003642 }
3643
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003644 if (xlat_func)
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003645 rc = ata_scsi_translate(dev, scmd, xlat_func);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003646 else
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003647 ata_scsi_simulate(dev, scmd);
Tejun Heo2115ea92006-05-15 21:03:39 +09003648
3649 return rc;
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003650
3651 bad_cdb_len:
3652 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
3653 scmd->cmd_len, scsi_op, dev->cdb_len);
3654 scmd->result = DID_ERROR << 16;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003655 scmd->scsi_done(scmd);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003656 return 0;
Brian Kingeb3f0f92006-03-23 17:30:02 -06003657}
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659/**
3660 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
Jeff Garzik23e701e2010-11-17 12:03:58 -05003661 * @shost: SCSI host of command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 * @cmd: SCSI command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 *
3664 * In some cases, this function translates SCSI commands into
3665 * ATA taskfiles, and queues the taskfiles to be sent to
3666 * hardware. In other cases, this function simulates a
3667 * SCSI device by evaluating and responding to certain
3668 * SCSI commands. This creates the overall effect of
3669 * ATA and ATAPI devices appearing as SCSI devices.
3670 *
3671 * LOCKING:
Jeff Garzik23e701e2010-11-17 12:03:58 -05003672 * ATA host lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 *
3674 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09003675 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3676 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 */
Jeff Garzik23e701e2010-11-17 12:03:58 -05003678int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
3680 struct ata_port *ap;
3681 struct ata_device *dev;
3682 struct scsi_device *scsidev = cmd->device;
Tejun Heo2115ea92006-05-15 21:03:39 +09003683 int rc = 0;
Jeff Garzik23e701e2010-11-17 12:03:58 -05003684 unsigned long irq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Jeff Garzik35bb94b2006-04-11 13:12:34 -04003686 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05003687
Jeff Garzik23e701e2010-11-17 12:03:58 -05003688 spin_lock_irqsave(ap->lock, irq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
3690 ata_scsi_dump_cdb(ap, cmd);
3691
3692 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06003693 if (likely(dev))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003694 rc = __ata_scsi_queuecmd(cmd, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06003695 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzik23e701e2010-11-17 12:03:58 -05003697 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
3699
Jeff Garzik23e701e2010-11-17 12:03:58 -05003700 spin_unlock_irqrestore(ap->lock, irq_flags);
3701
Tejun Heo2115ea92006-05-15 21:03:39 +09003702 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703}
3704
3705/**
3706 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003707 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 * @cmd: SCSI command being sent to device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 *
3710 * Interprets and directly executes a select list of SCSI commands
3711 * that can be handled internally.
3712 *
3713 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003714 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 */
3716
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003717void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
3719 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04003720 const u8 *scsicmd = cmd->cmnd;
Jeff Garzik45394142007-09-21 06:23:42 -04003721 u8 tmp8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Tejun Heo9a3dccc2006-01-06 09:56:18 +01003723 args.dev = dev;
3724 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 args.cmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003726 args.done = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
3728 switch(scsicmd[0]) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003729 /* TODO: worth improving? */
3730 case FORMAT_UNIT:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003731 ata_scsi_invalid_field(dev, cmd, 0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003732 break;
3733
3734 case INQUIRY:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003735 if (scsicmd[1] & 2) /* is CmdDt set? */
3736 ata_scsi_invalid_field(dev, cmd, 1);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003737 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
3738 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
3739 else switch (scsicmd[2]) {
3740 case 0x00:
3741 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003743 case 0x80:
3744 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003746 case 0x83:
3747 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003749 case 0x89:
3750 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 break;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003752 case 0xb0:
3753 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
3754 break;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06003755 case 0xb1:
3756 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
3757 break;
Martin K. Petersen02e0a602010-09-10 01:23:18 -04003758 case 0xb2:
3759 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
3760 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 default:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003762 ata_scsi_invalid_field(dev, cmd, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003764 }
3765 break;
3766
3767 case MODE_SENSE:
3768 case MODE_SENSE_10:
3769 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
3770 break;
3771
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003772 case READ_CAPACITY:
3773 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3774 break;
3775
Hannes Reineckeeb846d92014-11-17 14:25:19 +01003776 case SERVICE_ACTION_IN_16:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003777 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
3778 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3779 else
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003780 ata_scsi_invalid_field(dev, cmd, 1);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003781 break;
3782
3783 case REPORT_LUNS:
3784 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
3785 break;
3786
3787 case REQUEST_SENSE:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003788 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003789 cmd->result = (DRIVER_SENSE << 24);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003790 cmd->scsi_done(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003791 break;
3792
3793 /* if we reach this, then writeback caching is disabled,
3794 * turning this into a no-op.
3795 */
3796 case SYNCHRONIZE_CACHE:
3797 /* fall through */
3798
3799 /* no-op's, complete with success */
3800 case REZERO_UNIT:
3801 case SEEK_6:
3802 case SEEK_10:
3803 case TEST_UNIT_READY:
3804 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3805 break;
3806
3807 case SEND_DIAGNOSTIC:
3808 tmp8 = scsicmd[1] & ~(1 << 3);
3809 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
3810 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3811 else
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003812 ata_scsi_invalid_field(dev, cmd, 1);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003813 break;
3814
3815 /* all other commands */
3816 default:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003817 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003818 /* "Invalid command operation code" */
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003819 cmd->scsi_done(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 }
3822}
3823
Tejun Heof3187192007-04-17 23:44:07 +09003824int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
3825{
3826 int i, rc;
3827
3828 for (i = 0; i < host->n_ports; i++) {
3829 struct ata_port *ap = host->ports[i];
3830 struct Scsi_Host *shost;
3831
3832 rc = -ENOMEM;
3833 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
3834 if (!shost)
3835 goto err_alloc;
3836
Lin Ming9ee4f392011-12-05 09:20:28 +08003837 shost->eh_noresume = 1;
Tejun Heof3187192007-04-17 23:44:07 +09003838 *(struct ata_port **)&shost->hostdata[0] = ap;
3839 ap->scsi_host = shost;
3840
Gwendal Grignoud9027472010-05-25 12:31:38 -07003841 shost->transportt = ata_scsi_transport_template;
Tejun Heof3187192007-04-17 23:44:07 +09003842 shost->unique_id = ap->print_id;
3843 shost->max_id = 16;
3844 shost->max_lun = 1;
3845 shost->max_channel = 1;
3846 shost->max_cmd_len = 16;
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003847 shost->no_write_same = 1;
Tejun Heof3187192007-04-17 23:44:07 +09003848
Tejun Heo31cc23b2007-09-23 13:14:12 +09003849 /* Schedule policy is determined by ->qc_defer()
3850 * callback and it needs to see every deferred qc.
3851 * Set host_blocked to 1 to prevent SCSI midlayer from
3852 * automatically deferring requests.
3853 */
3854 shost->max_host_blocked = 1;
3855
Lin Mingf8fc75d2012-04-12 13:50:39 +08003856 rc = scsi_add_host_with_dma(ap->scsi_host,
3857 &ap->tdev, ap->host->dev);
Tejun Heof3187192007-04-17 23:44:07 +09003858 if (rc)
3859 goto err_add;
3860 }
3861
3862 return 0;
3863
3864 err_add:
3865 scsi_host_put(host->ports[i]->scsi_host);
3866 err_alloc:
3867 while (--i >= 0) {
3868 struct Scsi_Host *shost = host->ports[i]->scsi_host;
3869
3870 scsi_remove_host(shost);
3871 scsi_host_put(shost);
3872 }
3873 return rc;
3874}
3875
Tejun Heo1ae46312007-07-16 14:29:40 +09003876void ata_scsi_scan_host(struct ata_port *ap, int sync)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003877{
Tejun Heo1ae46312007-07-16 14:29:40 +09003878 int tries = 5;
3879 struct ata_device *last_failed_dev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003880 struct ata_link *link;
Tejun Heo1ae46312007-07-16 14:29:40 +09003881 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003882
Tejun Heo1ae46312007-07-16 14:29:40 +09003883 repeat:
Tejun Heo1eca4362008-11-03 20:03:17 +09003884 ata_for_each_link(link, ap, EDGE) {
3885 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003886 struct scsi_device *sdev;
3887 int channel = 0, id = 0;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04003888
Tejun Heo1eca4362008-11-03 20:03:17 +09003889 if (dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003890 continue;
Tejun Heo3edebac2006-05-31 18:27:40 +09003891
Tejun Heo41bda9c2007-08-06 18:36:24 +09003892 if (ata_is_host_link(link))
3893 id = dev->devno;
3894 else
3895 channel = link->pmp;
3896
3897 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
3898 NULL);
3899 if (!IS_ERR(sdev)) {
3900 dev->sdev = sdev;
3901 scsi_device_put(sdev);
Grant Grundler295124d2010-08-17 10:56:53 -07003902 } else {
3903 dev->sdev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003904 }
Tejun Heo3edebac2006-05-31 18:27:40 +09003905 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04003906 }
Tejun Heo1ae46312007-07-16 14:29:40 +09003907
3908 /* If we scanned while EH was in progress or allocation
3909 * failure occurred, scan would have failed silently. Check
3910 * whether all devices are attached.
3911 */
Tejun Heo1eca4362008-11-03 20:03:17 +09003912 ata_for_each_link(link, ap, EDGE) {
3913 ata_for_each_dev(dev, link, ENABLED) {
3914 if (!dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003915 goto exit_loop;
3916 }
Tejun Heo1ae46312007-07-16 14:29:40 +09003917 }
Tejun Heo41bda9c2007-08-06 18:36:24 +09003918 exit_loop:
3919 if (!link)
Tejun Heo1ae46312007-07-16 14:29:40 +09003920 return;
3921
3922 /* we're missing some SCSI devices */
3923 if (sync) {
3924 /* If caller requested synchrnous scan && we've made
3925 * any progress, sleep briefly and repeat.
3926 */
3927 if (dev != last_failed_dev) {
3928 msleep(100);
3929 last_failed_dev = dev;
3930 goto repeat;
3931 }
3932
3933 /* We might be failing to detect boot device, give it
3934 * a few more chances.
3935 */
3936 if (--tries) {
3937 msleep(100);
3938 goto repeat;
3939 }
3940
Joe Perchesa9a79df2011-04-15 15:51:59 -07003941 ata_port_err(ap,
3942 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
Tejun Heo1ae46312007-07-16 14:29:40 +09003943 }
3944
Tejun Heoad72cf92010-07-02 10:03:52 +02003945 queue_delayed_work(system_long_wq, &ap->hotplug_task,
Tejun Heo1ae46312007-07-16 14:29:40 +09003946 round_jiffies_relative(HZ));
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003947}
Tejun Heo0ea035a2006-05-31 18:28:01 +09003948
3949/**
3950 * ata_scsi_offline_dev - offline attached SCSI device
3951 * @dev: ATA device to offline attached SCSI device for
3952 *
3953 * This function is called from ata_eh_hotplug() and responsible
3954 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04003955 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09003956 * against clearing.
3957 *
3958 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003959 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09003960 *
3961 * RETURNS:
3962 * 1 if attached SCSI device exists, 0 otherwise.
3963 */
3964int ata_scsi_offline_dev(struct ata_device *dev)
3965{
3966 if (dev->sdev) {
3967 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
3968 return 1;
3969 }
3970 return 0;
3971}
Tejun Heo580b21022006-05-31 18:28:05 +09003972
3973/**
3974 * ata_scsi_remove_dev - remove attached SCSI device
3975 * @dev: ATA device to remove attached SCSI device for
3976 *
3977 * This function is called from ata_eh_scsi_hotplug() and
3978 * responsible for removing the SCSI device attached to @dev.
3979 *
3980 * LOCKING:
3981 * Kernel thread context (may sleep).
3982 */
3983static void ata_scsi_remove_dev(struct ata_device *dev)
3984{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003985 struct ata_port *ap = dev->link->ap;
Tejun Heo580b21022006-05-31 18:28:05 +09003986 struct scsi_device *sdev;
3987 unsigned long flags;
3988
3989 /* Alas, we need to grab scan_mutex to ensure SCSI device
3990 * state doesn't change underneath us and thus
3991 * scsi_device_get() always succeeds. The mutex locking can
3992 * be removed if there is __scsi_device_get() interface which
3993 * increments reference counts regardless of device state.
3994 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003995 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04003996 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b21022006-05-31 18:28:05 +09003997
Jeff Garzikcca39742006-08-24 03:19:22 -04003998 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b21022006-05-31 18:28:05 +09003999 sdev = dev->sdev;
4000 dev->sdev = NULL;
4001
4002 if (sdev) {
4003 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04004004 * away underneath us after the host lock and
Tejun Heo580b21022006-05-31 18:28:05 +09004005 * scan_mutex are released. Hold onto it.
4006 */
4007 if (scsi_device_get(sdev) == 0) {
4008 /* The following ensures the attached sdev is
4009 * offline on return from ata_scsi_offline_dev()
4010 * regardless it wins or loses the race
4011 * against this function.
4012 */
4013 scsi_device_set_state(sdev, SDEV_OFFLINE);
4014 } else {
4015 WARN_ON(1);
4016 sdev = NULL;
4017 }
4018 }
4019
Jeff Garzikba6a1302006-06-22 23:46:10 -04004020 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04004021 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004022
4023 if (sdev) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004024 ata_dev_info(dev, "detaching (SCSI %s)\n",
4025 dev_name(&sdev->sdev_gendev));
Tejun Heo580b21022006-05-31 18:28:05 +09004026
4027 scsi_remove_device(sdev);
4028 scsi_device_put(sdev);
4029 }
4030}
4031
Tejun Heo41bda9c2007-08-06 18:36:24 +09004032static void ata_scsi_handle_link_detach(struct ata_link *link)
4033{
4034 struct ata_port *ap = link->ap;
4035 struct ata_device *dev;
4036
Tejun Heo1eca4362008-11-03 20:03:17 +09004037 ata_for_each_dev(dev, link, ALL) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004038 unsigned long flags;
4039
4040 if (!(dev->flags & ATA_DFLAG_DETACHED))
4041 continue;
4042
4043 spin_lock_irqsave(ap->lock, flags);
4044 dev->flags &= ~ATA_DFLAG_DETACHED;
4045 spin_unlock_irqrestore(ap->lock, flags);
4046
Aaron Luf1bc1e42013-08-23 10:17:54 +08004047 if (zpodd_dev_enabled(dev))
4048 zpodd_exit(dev);
4049
Tejun Heo41bda9c2007-08-06 18:36:24 +09004050 ata_scsi_remove_dev(dev);
4051 }
4052}
4053
Tejun Heo580b21022006-05-31 18:28:05 +09004054/**
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004055 * ata_scsi_media_change_notify - send media change event
Randy Dunlapc5d0e6a2007-10-15 17:29:46 -07004056 * @dev: Pointer to the disk device with media change event
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004057 *
4058 * Tell the block layer to send a media change notification
4059 * event.
4060 *
4061 * LOCKING:
Tejun Heo854c73a2007-09-23 13:14:11 +09004062 * spin_lock_irqsave(host lock)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004063 */
Tejun Heo854c73a2007-09-23 13:14:11 +09004064void ata_scsi_media_change_notify(struct ata_device *dev)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004065{
Tejun Heo854c73a2007-09-23 13:14:11 +09004066 if (dev->sdev)
Jeff Garzikf26792d2007-10-29 17:18:39 -04004067 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
4068 GFP_ATOMIC);
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004069}
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004070
4071/**
Tejun Heo580b21022006-05-31 18:28:05 +09004072 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00004073 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b21022006-05-31 18:28:05 +09004074 *
4075 * Perform SCSI part of hotplug. It's executed from a separate
4076 * workqueue after EH completes. This is necessary because SCSI
4077 * hot plugging requires working EH and hot unplugging is
4078 * synchronized with hot plugging with a mutex.
4079 *
4080 * LOCKING:
4081 * Kernel thread context (may sleep).
4082 */
David Howells65f27f32006-11-22 14:55:48 +00004083void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b21022006-05-31 18:28:05 +09004084{
David Howells65f27f32006-11-22 14:55:48 +00004085 struct ata_port *ap =
4086 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004087 int i;
Tejun Heo580b21022006-05-31 18:28:05 +09004088
Tejun Heob51e9e52006-06-29 01:29:30 +09004089 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b21022006-05-31 18:28:05 +09004090 DPRINTK("ENTER/EXIT - unloading\n");
4091 return;
4092 }
4093
Tejun Heo85fbd722013-12-18 07:07:32 -05004094 /*
4095 * XXX - UGLY HACK
4096 *
4097 * The block layer suspend/resume path is fundamentally broken due
4098 * to freezable kthreads and workqueue and may deadlock if a block
4099 * device gets removed while resume is in progress. I don't know
4100 * what the solution is short of removing freezable kthreads and
4101 * workqueues altogether.
4102 *
4103 * The following is an ugly hack to avoid kicking off device
4104 * removal while freezer is active. This is a joke but does avoid
4105 * this particular deadlock scenario.
4106 *
4107 * https://bugzilla.kernel.org/show_bug.cgi?id=62801
4108 * http://marc.info/?l=linux-kernel&m=138695698516487
4109 */
4110#ifdef CONFIG_FREEZER
4111 while (pm_freezing)
4112 msleep(10);
4113#endif
4114
Tejun Heo580b21022006-05-31 18:28:05 +09004115 DPRINTK("ENTER\n");
Tejun Heoad72cf92010-07-02 10:03:52 +02004116 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004117
Tejun Heo41bda9c2007-08-06 18:36:24 +09004118 /* Unplug detached devices. We cannot use link iterator here
4119 * because PMP links have to be scanned even if PMP is
4120 * currently not attached. Iterate manually.
4121 */
4122 ata_scsi_handle_link_detach(&ap->link);
4123 if (ap->pmp_link)
4124 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
4125 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
Tejun Heo580b21022006-05-31 18:28:05 +09004126
4127 /* scan for new ones */
Tejun Heo1ae46312007-07-16 14:29:40 +09004128 ata_scsi_scan_host(ap, 0);
Tejun Heo580b21022006-05-31 18:28:05 +09004129
Tejun Heoad72cf92010-07-02 10:03:52 +02004130 mutex_unlock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004131 DPRINTK("EXIT\n");
4132}
Tejun Heo83c47bc2006-05-31 18:28:07 +09004133
4134/**
4135 * ata_scsi_user_scan - indication for user-initiated bus scan
4136 * @shost: SCSI host to scan
4137 * @channel: Channel to scan
4138 * @id: ID to scan
4139 * @lun: LUN to scan
4140 *
4141 * This function is called when user explicitly requests bus
4142 * scan. Set probe pending flag and invoke EH.
4143 *
4144 * LOCKING:
4145 * SCSI layer (we don't care)
4146 *
4147 * RETURNS:
4148 * Zero.
4149 */
Gwendal Grignoud9027472010-05-25 12:31:38 -07004150int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004151 unsigned int id, u64 lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004152{
4153 struct ata_port *ap = ata_shost_to_port(shost);
4154 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004155 int devno, rc = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004156
4157 if (!ap->ops->error_handler)
4158 return -EOPNOTSUPP;
4159
Tejun Heo41bda9c2007-08-06 18:36:24 +09004160 if (lun != SCAN_WILD_CARD && lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004161 return -EINVAL;
4162
Tejun Heo071f44b2008-04-07 22:47:22 +09004163 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004164 if (channel != SCAN_WILD_CARD && channel)
4165 return -EINVAL;
4166 devno = id;
4167 } else {
4168 if (id != SCAN_WILD_CARD && id)
4169 return -EINVAL;
4170 devno = channel;
4171 }
4172
Jeff Garzikba6a1302006-06-22 23:46:10 -04004173 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004174
Tejun Heo41bda9c2007-08-06 18:36:24 +09004175 if (devno == SCAN_WILD_CARD) {
4176 struct ata_link *link;
4177
Tejun Heo1eca4362008-11-03 20:03:17 +09004178 ata_for_each_link(link, ap, EDGE) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004179 struct ata_eh_info *ehi = &link->eh_info;
Tejun Heob558edd2008-01-24 00:05:14 +09004180 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09004181 ehi->action |= ATA_EH_RESET;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004182 }
Tejun Heo83c47bc2006-05-31 18:28:07 +09004183 } else {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004184 struct ata_device *dev = ata_find_dev(ap, devno);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004185
4186 if (dev) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004187 struct ata_eh_info *ehi = &dev->link->eh_info;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004188 ehi->probe_mask |= 1 << dev->devno;
Tejun Heocf480622008-01-24 00:05:14 +09004189 ehi->action |= ATA_EH_RESET;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004190 } else
4191 rc = -EINVAL;
4192 }
4193
Tejun Heo309afcb2006-09-30 18:07:17 +09004194 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09004195 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09004196 spin_unlock_irqrestore(ap->lock, flags);
4197 ata_port_wait_eh(ap);
4198 } else
4199 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004200
4201 return rc;
4202}
zhao, forrest3057ac32006-06-12 12:01:34 +08004203
4204/**
Tejun Heod0171262006-06-12 22:51:14 +09004205 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00004206 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08004207 *
Tejun Heod0171262006-06-12 22:51:14 +09004208 * After ATA pass thru (SAT) commands are executed successfully,
Tejun Heoad72cf92010-07-02 10:03:52 +02004209 * libata need to propagate the changes to SCSI layer.
zhao, forrest3057ac32006-06-12 12:01:34 +08004210 *
Tejun Heod0171262006-06-12 22:51:14 +09004211 * LOCKING:
4212 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08004213 */
David Howells65f27f32006-11-22 14:55:48 +00004214void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08004215{
David Howells65f27f32006-11-22 14:55:48 +00004216 struct ata_port *ap =
4217 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004218 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09004219 struct ata_device *dev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004220 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08004221
Tejun Heoad72cf92010-07-02 10:03:52 +02004222 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heof84e7e42006-11-22 11:21:31 +09004223 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08004224
Tejun Heo1eca4362008-11-03 20:03:17 +09004225 ata_for_each_link(link, ap, EDGE) {
4226 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004227 struct scsi_device *sdev = dev->sdev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004228
Tejun Heo1eca4362008-11-03 20:03:17 +09004229 if (!sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004230 continue;
4231 if (scsi_device_get(sdev))
4232 continue;
Tejun Heof84e7e42006-11-22 11:21:31 +09004233
Tejun Heo41bda9c2007-08-06 18:36:24 +09004234 spin_unlock_irqrestore(ap->lock, flags);
4235 scsi_rescan_device(&(sdev->sdev_gendev));
4236 scsi_device_put(sdev);
4237 spin_lock_irqsave(ap->lock, flags);
4238 }
zhao, forrest3057ac32006-06-12 12:01:34 +08004239 }
Tejun Heof84e7e42006-11-22 11:21:31 +09004240
4241 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad72cf92010-07-02 10:03:52 +02004242 mutex_unlock(&ap->scsi_scan_mutex);
zhao, forrest3057ac32006-06-12 12:01:34 +08004243}
Brian King80289162006-08-07 14:27:31 -05004244
4245/**
4246 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04004247 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05004248 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04004249 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05004250 *
4251 * LOCKING:
4252 * PCI/etc. bus probe sem.
4253 *
4254 * RETURNS:
4255 * ata_port pointer on success / NULL on failure.
4256 */
4257
Jeff Garzikcca39742006-08-24 03:19:22 -04004258struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05004259 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04004260 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05004261{
Tejun Heof3187192007-04-17 23:44:07 +09004262 struct ata_port *ap;
Brian King80289162006-08-07 14:27:31 -05004263
Tejun Heof3187192007-04-17 23:44:07 +09004264 ap = ata_port_alloc(host);
Brian King80289162006-08-07 14:27:31 -05004265 if (!ap)
4266 return NULL;
4267
Tejun Heof3187192007-04-17 23:44:07 +09004268 ap->port_no = 0;
James Bottomleya29b5da2011-01-23 08:30:00 -06004269 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09004270 ap->pio_mask = port_info->pio_mask;
4271 ap->mwdma_mask = port_info->mwdma_mask;
4272 ap->udma_mask = port_info->udma_mask;
4273 ap->flags |= port_info->flags;
4274 ap->ops = port_info->port_ops;
4275 ap->cbl = ATA_CBL_SATA;
4276
Brian King80289162006-08-07 14:27:31 -05004277 return ap;
4278}
4279EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
4280
4281/**
4282 * ata_sas_port_start - Set port up for dma.
4283 * @ap: Port to initialize
4284 *
4285 * Called just after data structures for each port are
James Bottomleydde20202008-02-19 11:36:56 +01004286 * initialized.
Brian King80289162006-08-07 14:27:31 -05004287 *
4288 * May be used as the port_start() entry in ata_port_operations.
4289 *
4290 * LOCKING:
4291 * Inherited from caller.
4292 */
4293int ata_sas_port_start(struct ata_port *ap)
4294{
Nishanth Aravamudan3f1e0462011-06-16 08:28:36 -07004295 /*
4296 * the port is marked as frozen at allocation time, but if we don't
4297 * have new eh, we won't thaw it
4298 */
4299 if (!ap->ops->error_handler)
4300 ap->pflags &= ~ATA_PFLAG_FROZEN;
James Bottomleydde20202008-02-19 11:36:56 +01004301 return 0;
Brian King80289162006-08-07 14:27:31 -05004302}
4303EXPORT_SYMBOL_GPL(ata_sas_port_start);
4304
4305/**
4306 * ata_port_stop - Undo ata_sas_port_start()
4307 * @ap: Port to shut down
4308 *
Brian King80289162006-08-07 14:27:31 -05004309 * May be used as the port_stop() entry in ata_port_operations.
4310 *
4311 * LOCKING:
4312 * Inherited from caller.
4313 */
4314
4315void ata_sas_port_stop(struct ata_port *ap)
4316{
Brian King80289162006-08-07 14:27:31 -05004317}
4318EXPORT_SYMBOL_GPL(ata_sas_port_stop);
4319
Dan Williamsb2024452012-03-21 21:09:07 -07004320/**
4321 * ata_sas_async_probe - simply schedule probing and return
4322 * @ap: Port to probe
4323 *
4324 * For batch scheduling of probe for sas attached ata devices, assumes
4325 * the port has already been through ata_sas_port_init()
4326 */
4327void ata_sas_async_probe(struct ata_port *ap)
Dan Williams9508a662012-01-18 20:47:01 -08004328{
Dan Williamsb2024452012-03-21 21:09:07 -07004329 __ata_port_probe(ap);
Dan Williams9508a662012-01-18 20:47:01 -08004330}
Dan Williamsb2024452012-03-21 21:09:07 -07004331EXPORT_SYMBOL_GPL(ata_sas_async_probe);
4332
4333int ata_sas_sync_probe(struct ata_port *ap)
4334{
4335 return ata_port_probe(ap);
4336}
4337EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
4338
Dan Williams9508a662012-01-18 20:47:01 -08004339
Brian King80289162006-08-07 14:27:31 -05004340/**
4341 * ata_sas_port_init - Initialize a SATA device
4342 * @ap: SATA port to initialize
4343 *
4344 * LOCKING:
4345 * PCI/etc. bus probe sem.
4346 *
4347 * RETURNS:
4348 * Zero on success, non-zero on error.
4349 */
4350
4351int ata_sas_port_init(struct ata_port *ap)
4352{
4353 int rc = ap->ops->port_start(ap);
4354
Dan Williamsb2024452012-03-21 21:09:07 -07004355 if (rc)
4356 return rc;
4357 ap->print_id = atomic_inc_return(&ata_print_id);
4358 return 0;
Brian King80289162006-08-07 14:27:31 -05004359}
4360EXPORT_SYMBOL_GPL(ata_sas_port_init);
4361
4362/**
4363 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
4364 * @ap: SATA port to destroy
4365 *
4366 */
4367
4368void ata_sas_port_destroy(struct ata_port *ap)
4369{
Tejun Heof0d36ef2007-01-20 16:00:28 +09004370 if (ap->ops->port_stop)
4371 ap->ops->port_stop(ap);
Brian King80289162006-08-07 14:27:31 -05004372 kfree(ap);
4373}
4374EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
4375
4376/**
4377 * ata_sas_slave_configure - Default slave_config routine for libata devices
4378 * @sdev: SCSI device to configure
4379 * @ap: ATA port to which SCSI device is attached
4380 *
4381 * RETURNS:
4382 * Zero.
4383 */
4384
4385int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
4386{
4387 ata_scsi_sdev_config(sdev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004388 ata_scsi_dev_config(sdev, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05004389 return 0;
4390}
4391EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
4392
4393/**
4394 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
4395 * @cmd: SCSI command to be sent
Brian King80289162006-08-07 14:27:31 -05004396 * @ap: ATA port to which the command is being sent
4397 *
4398 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06004399 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
4400 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05004401 */
4402
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004403int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
Brian King80289162006-08-07 14:27:31 -05004404{
Brian King08475a12006-11-20 13:51:56 -06004405 int rc = 0;
4406
Brian King80289162006-08-07 14:27:31 -05004407 ata_scsi_dump_cdb(ap, cmd);
4408
Tejun Heo2486fa52008-07-31 07:52:40 +09004409 if (likely(ata_dev_enabled(ap->link.device)))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004410 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05004411 else {
4412 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004413 cmd->scsi_done(cmd);
Brian King80289162006-08-07 14:27:31 -05004414 }
Brian King08475a12006-11-20 13:51:56 -06004415 return rc;
Brian King80289162006-08-07 14:27:31 -05004416}
4417EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
Shaohua Li98bd4be2015-01-23 19:52:07 -08004418
4419int ata_sas_allocate_tag(struct ata_port *ap)
4420{
4421 unsigned int max_queue = ap->host->n_tags;
4422 unsigned int i, tag;
4423
4424 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
Tejun Heo3a028242015-03-24 14:14:18 -04004425 tag = tag < max_queue ? tag : 0;
Shaohua Li98bd4be2015-01-23 19:52:07 -08004426
4427 /* the last tag is reserved for internal command. */
4428 if (tag == ATA_TAG_INTERNAL)
4429 continue;
4430
4431 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
4432 ap->sas_last_tag = tag;
4433 return tag;
4434 }
4435 }
4436 return -1;
4437}
4438
4439void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
4440{
4441 clear_bit(tag, &ap->sas_tag_allocated);
4442}