blob: 2389247bdf6f984ac5307659ce225c218787eb69 [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
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700303static ssize_t
304ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count)
306{
307 struct Scsi_Host *shost = class_to_shost(dev);
308 struct ata_port *ap = ata_shost_to_port(shost);
309 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
310 return ap->ops->em_store(ap, buf, count);
311 return -EINVAL;
312}
313
314static ssize_t
315ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
316 char *buf)
317{
318 struct Scsi_Host *shost = class_to_shost(dev);
319 struct ata_port *ap = ata_shost_to_port(shost);
320
321 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
322 return ap->ops->em_show(ap, buf);
323 return -EINVAL;
324}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200325DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700326 ata_scsi_em_message_show, ata_scsi_em_message_store);
327EXPORT_SYMBOL_GPL(dev_attr_em_message);
328
329static ssize_t
330ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
331 char *buf)
332{
333 struct Scsi_Host *shost = class_to_shost(dev);
334 struct ata_port *ap = ata_shost_to_port(shost);
335
336 return snprintf(buf, 23, "%d\n", ap->em_message_type);
337}
338DEVICE_ATTR(em_message_type, S_IRUGO,
339 ata_scsi_em_message_type_show, NULL);
340EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
341
342static ssize_t
343ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
344 char *buf)
345{
346 struct scsi_device *sdev = to_scsi_device(dev);
347 struct ata_port *ap = ata_shost_to_port(sdev->host);
348 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
349
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800350 if (atadev && ap->ops->sw_activity_show &&
351 (ap->flags & ATA_FLAG_SW_ACTIVITY))
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700352 return ap->ops->sw_activity_show(atadev, buf);
353 return -EINVAL;
354}
355
356static ssize_t
357ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
358 const char *buf, size_t count)
359{
360 struct scsi_device *sdev = to_scsi_device(dev);
361 struct ata_port *ap = ata_shost_to_port(sdev->host);
362 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
363 enum sw_activity val;
364 int rc;
365
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800366 if (atadev && ap->ops->sw_activity_store &&
367 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700368 val = simple_strtoul(buf, NULL, 0);
369 switch (val) {
370 case OFF: case BLINK_ON: case BLINK_OFF:
371 rc = ap->ops->sw_activity_store(atadev, val);
372 if (!rc)
373 return count;
374 else
375 return rc;
376 }
377 }
378 return -EINVAL;
379}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200380DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700381 ata_scsi_activity_store);
382EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
383
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200384struct device_attribute *ata_common_sdev_attrs[] = {
385 &dev_attr_unload_heads,
386 NULL
387};
388EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
389
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200390static void ata_scsi_invalid_field(struct ata_device *dev,
391 struct scsi_cmnd *cmd)
Douglas Gilbertae006512005-10-09 09:09:35 -0400392{
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200393 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -0400394 /* "Invalid field in cbd" */
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500395 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -0400396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/**
399 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
400 * @sdev: SCSI device for which BIOS geometry is to be determined
401 * @bdev: block device associated with @sdev
402 * @capacity: capacity of SCSI device
403 * @geom: location to which geometry will be output
404 *
405 * Generic bios head/sector/cylinder calculator
406 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
407 * mapping. Some situations may arise where the disk is not
408 * bootable if this is not used.
409 *
410 * LOCKING:
411 * Defined by the SCSI layer. We don't really care.
412 *
413 * RETURNS:
414 * Zero.
415 */
416int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
417 sector_t capacity, int geom[])
418{
419 geom[0] = 255;
420 geom[1] = 63;
421 sector_div(capacity, 255*63);
422 geom[2] = capacity;
423
424 return 0;
425}
426
Jeff Garzikb0955182005-05-12 15:45:22 -0400427/**
Tejun Heod8d91292010-05-15 20:09:34 +0200428 * ata_scsi_unlock_native_capacity - unlock native capacity
429 * @sdev: SCSI device to adjust device capacity for
430 *
431 * This function is called if a partition on @sdev extends beyond
432 * the end of the device. It requests EH to unlock HPA.
433 *
434 * LOCKING:
435 * Defined by the SCSI layer. Might sleep.
436 */
437void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
438{
439 struct ata_port *ap = ata_shost_to_port(sdev->host);
440 struct ata_device *dev;
441 unsigned long flags;
442
443 spin_lock_irqsave(ap->lock, flags);
444
445 dev = ata_scsi_find_dev(ap, sdev);
446 if (dev && dev->n_sectors < dev->n_native_sectors) {
447 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
448 dev->link->eh_info.action |= ATA_EH_RESET;
449 ata_port_schedule_eh(ap);
450 }
451
452 spin_unlock_irqrestore(ap->lock, flags);
453 ata_port_wait_eh(ap);
454}
455
456/**
Tejun Heo5924b742007-01-02 20:20:07 +0900457 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
Randy Dunlap5eb66fe2009-01-20 16:28:59 -0800458 * @ap: target port
Tejun Heo5924b742007-01-02 20:20:07 +0900459 * @sdev: SCSI device to get identify data for
460 * @arg: User buffer area for identify data
461 *
462 * LOCKING:
463 * Defined by the SCSI layer. We don't really care.
464 *
465 * RETURNS:
466 * Zero on success, negative errno on error.
467 */
Jeff Garzik94be9a52009-01-16 10:17:09 -0500468static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
469 void __user *arg)
Tejun Heo5924b742007-01-02 20:20:07 +0900470{
Tejun Heo5924b742007-01-02 20:20:07 +0900471 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
472 u16 __user *dst = arg;
473 char buf[40];
474
475 if (!dev)
476 return -ENOMSG;
477
478 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
479 return -EFAULT;
480
481 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
482 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
483 return -EFAULT;
484
485 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
486 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
487 return -EFAULT;
488
489 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
490 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
491 return -EFAULT;
492
493 return 0;
494}
495
496/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400497 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800498 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400499 * @arg: User provided data for issuing command
500 *
501 * LOCKING:
502 * Defined by the SCSI layer. We don't really care.
503 *
504 * RETURNS:
505 * Zero on success, negative errno on error.
506 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400507int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
508{
509 int rc = 0;
510 u8 scsi_cmd[MAX_COMMAND_SIZE];
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700511 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400512 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600513 enum dma_data_direction data_dir;
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700514 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400515
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500516 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400517 return -EINVAL;
518
519 if (copy_from_user(args, arg, sizeof(args)))
520 return -EFAULT;
521
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700522 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
523 if (!sensebuf)
524 return -ENOMEM;
525
Jeff Garzikb0955182005-05-12 15:45:22 -0400526 memset(scsi_cmd, 0, sizeof(scsi_cmd));
527
528 if (args[3]) {
Grant Grundler295124d2010-08-17 10:56:53 -0700529 argsize = ATA_SECT_SIZE * args[3];
Jeff Garzikb0955182005-05-12 15:45:22 -0400530 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400531 if (argbuf == NULL) {
532 rc = -ENOMEM;
533 goto error;
534 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400535
536 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
537 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400538 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600539 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400540 } else {
541 scsi_cmd[1] = (3 << 1); /* Non-data */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700542 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600543 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400544 }
545
546 scsi_cmd[0] = ATA_16;
547
548 scsi_cmd[4] = args[2];
Bartlomiej Zolnierkiewicza4f19042008-10-10 22:39:20 +0200549 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
Jeff Garzikb0955182005-05-12 15:45:22 -0400550 scsi_cmd[6] = args[3];
551 scsi_cmd[8] = args[1];
552 scsi_cmd[10] = 0x4f;
553 scsi_cmd[12] = 0xc2;
554 } else {
555 scsi_cmd[6] = args[1];
556 }
557 scsi_cmd[14] = args[0];
558
559 /* Good values for timeout and retries? Values below
560 from scsi_ioctl_send_command() for default case... */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700561 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900562 sensebuf, (10*HZ), 5, 0, NULL);
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700563
564 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
565 u8 *desc = sensebuf + 8;
566 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
567
568 /* If we set cc then ATA pass-through will cause a
569 * check condition even if no error. Filter that. */
570 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
571 struct scsi_sense_hdr sshdr;
572 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400573 &sshdr);
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100574 if (sshdr.sense_key == RECOVERED_ERROR &&
575 sshdr.asc == 0 && sshdr.ascq == 0x1d)
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700576 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
577 }
578
579 /* Send userspace a few ATA registers (same as drivers/ide) */
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400580 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
581 desc[0] == 0x09) { /* code is "ATA Descriptor" */
582 args[0] = desc[13]; /* status */
583 args[1] = desc[3]; /* error */
584 args[2] = desc[5]; /* sector count (0:7) */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700585 if (copy_to_user(arg, args, sizeof(args)))
586 rc = -EFAULT;
587 }
588 }
589
590
591 if (cmd_result) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400592 rc = -EIO;
593 goto error;
594 }
595
Jeff Garzikb0955182005-05-12 15:45:22 -0400596 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500597 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400598 rc = -EFAULT;
599error:
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700600 kfree(sensebuf);
Jesper Juhl8f760782006-06-27 02:55:06 -0700601 kfree(argbuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400602 return rc;
603}
604
605/**
606 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800607 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400608 * @arg: User provided data for issuing command
609 *
610 * LOCKING:
611 * Defined by the SCSI layer. We don't really care.
612 *
613 * RETURNS:
614 * Zero on success, negative errno on error.
615 */
616int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
617{
618 int rc = 0;
619 u8 scsi_cmd[MAX_COMMAND_SIZE];
David Milburnaf068bd2007-01-30 00:59:15 -0800620 u8 args[7], *sensebuf = NULL;
621 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400622
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500623 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400624 return -EINVAL;
625
626 if (copy_from_user(args, arg, sizeof(args)))
627 return -EFAULT;
628
David Milburnaf068bd2007-01-30 00:59:15 -0800629 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
630 if (!sensebuf)
631 return -ENOMEM;
632
Jeff Garzikb0955182005-05-12 15:45:22 -0400633 memset(scsi_cmd, 0, sizeof(scsi_cmd));
634 scsi_cmd[0] = ATA_16;
635 scsi_cmd[1] = (3 << 1); /* Non-data */
David Milburnaf068bd2007-01-30 00:59:15 -0800636 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Jeff Garzikb0955182005-05-12 15:45:22 -0400637 scsi_cmd[4] = args[1];
638 scsi_cmd[6] = args[2];
639 scsi_cmd[8] = args[3];
640 scsi_cmd[10] = args[4];
641 scsi_cmd[12] = args[5];
Mark Lord277239f2007-03-30 17:45:52 -0400642 scsi_cmd[13] = args[6] & 0x4f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400643 scsi_cmd[14] = args[0];
644
Jeff Garzikb0955182005-05-12 15:45:22 -0400645 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500646 from scsi_ioctl_send_command() for default case... */
David Milburnaf068bd2007-01-30 00:59:15 -0800647 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900648 sensebuf, (10*HZ), 5, 0, NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400649
David Milburnaf068bd2007-01-30 00:59:15 -0800650 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
651 u8 *desc = sensebuf + 8;
652 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
653
654 /* If we set cc then ATA pass-through will cause a
655 * check condition even if no error. Filter that. */
656 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
657 struct scsi_sense_hdr sshdr;
658 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
659 &sshdr);
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100660 if (sshdr.sense_key == RECOVERED_ERROR &&
661 sshdr.asc == 0 && sshdr.ascq == 0x1d)
David Milburnaf068bd2007-01-30 00:59:15 -0800662 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
663 }
664
665 /* Send userspace ATA registers */
666 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
667 desc[0] == 0x09) {/* code is "ATA Descriptor" */
668 args[0] = desc[13]; /* status */
669 args[1] = desc[3]; /* error */
670 args[2] = desc[5]; /* sector count (0:7) */
671 args[3] = desc[7]; /* lbal */
672 args[4] = desc[9]; /* lbam */
673 args[5] = desc[11]; /* lbah */
674 args[6] = desc[12]; /* select */
675 if (copy_to_user(arg, args, sizeof(args)))
676 rc = -EFAULT;
677 }
678 }
679
680 if (cmd_result) {
681 rc = -EIO;
682 goto error;
683 }
684
685 error:
686 kfree(sensebuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400687 return rc;
688}
689
Alan Coxe3cf95d2009-04-09 17:31:17 +0100690static int ata_ioc32(struct ata_port *ap)
691{
692 if (ap->flags & ATA_FLAG_PIO_DMA)
693 return 1;
694 if (ap->pflags & ATA_PFLAG_PIO32)
695 return 1;
696 return 0;
697}
698
Jeff Garzik94be9a52009-01-16 10:17:09 -0500699int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
700 int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Arnd Bergmann287e6612016-02-11 14:16:27 +0100702 unsigned long val;
703 int rc = -EINVAL;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100704 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 switch (cmd) {
Arnd Bergmann287e6612016-02-11 14:16:27 +0100707 case HDIO_GET_32BIT:
Alan Coxe3cf95d2009-04-09 17:31:17 +0100708 spin_lock_irqsave(ap->lock, flags);
709 val = ata_ioc32(ap);
710 spin_unlock_irqrestore(ap->lock, flags);
Arnd Bergmann287e6612016-02-11 14:16:27 +0100711 return put_user(val, (unsigned long __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Arnd Bergmann287e6612016-02-11 14:16:27 +0100713 case HDIO_SET_32BIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 val = (unsigned long) arg;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100715 rc = 0;
716 spin_lock_irqsave(ap->lock, flags);
717 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
718 if (val)
719 ap->pflags |= ATA_PFLAG_PIO32;
720 else
721 ap->pflags &= ~ATA_PFLAG_PIO32;
722 } else {
723 if (val != ata_ioc32(ap))
724 rc = -EINVAL;
725 }
726 spin_unlock_irqrestore(ap->lock, flags);
727 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Tejun Heo5924b742007-01-02 20:20:07 +0900729 case HDIO_GET_IDENTITY:
Jeff Garzik94be9a52009-01-16 10:17:09 -0500730 return ata_get_identity(ap, scsidev, arg);
Tejun Heo5924b742007-01-02 20:20:07 +0900731
Jeff Garzikb0955182005-05-12 15:45:22 -0400732 case HDIO_DRIVE_CMD:
733 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
734 return -EACCES;
735 return ata_cmd_ioctl(scsidev, arg);
736
737 case HDIO_DRIVE_TASK:
738 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
739 return -EACCES;
740 return ata_task_ioctl(scsidev, arg);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 default:
743 rc = -ENOTTY;
744 break;
745 }
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return rc;
748}
Jeff Garzik94be9a52009-01-16 10:17:09 -0500749EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
750
751int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
752{
753 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
754 scsidev, cmd, arg);
755}
756EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758/**
759 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * @dev: ATA device to which the new command is attached
761 * @cmd: SCSI command that originated this ATA command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
763 * Obtain a reference to an unused ata_queued_cmd structure,
764 * which is the basic libata structure representing a single
765 * ATA command sent to the hardware.
766 *
767 * If a command was available, fill in the SCSI-specific
768 * portions of the structure with information on the
769 * current command.
770 *
771 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400772 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *
774 * RETURNS:
775 * Command allocated, or %NULL if none available.
776 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100777static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500778 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct ata_queued_cmd *qc;
781
Shaohua Li12cb5ce2015-01-15 17:32:27 -0800782 qc = ata_qc_new_init(dev, cmd->request->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (qc) {
784 qc->scsicmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500785 qc->scsidone = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Tejun Heoff2aeb12007-12-05 16:43:11 +0900787 qc->sg = scsi_sglist(cmd);
Boaz Harrosh71201652007-09-18 17:48:50 +0200788 qc->n_elem = scsi_sg_count(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 } else {
790 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500791 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
794 return qc;
795}
796
Tejun Heoaacda372008-03-18 17:47:43 +0900797static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
798{
799 struct scsi_cmnd *scmd = qc->scsicmd;
800
801 qc->extrabytes = scmd->request->extra_len;
802 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400806 * ata_dump_status - user friendly display of error info
807 * @id: id of the port in question
808 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400810 * Decode and dump the ATA error/status registers for the user so
811 * that they have some idea what really happened at the non
812 * make-believe layer.
813 *
814 * LOCKING:
815 * inherited from caller
816 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100817static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
Jeff Garzikb0955182005-05-12 15:45:22 -0400818{
819 u8 stat = tf->command, err = tf->feature;
820
821 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
822 if (stat & ATA_BUSY) {
823 printk("Busy }\n"); /* Data is not valid in this case */
824 } else {
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100825 if (stat & ATA_DRDY) printk("DriveReady ");
826 if (stat & ATA_DF) printk("DeviceFault ");
827 if (stat & ATA_DSC) printk("SeekComplete ");
828 if (stat & ATA_DRQ) printk("DataRequest ");
829 if (stat & ATA_CORR) printk("CorrectedError ");
830 if (stat & ATA_SENSE) printk("Sense ");
831 if (stat & ATA_ERR) printk("Error ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400832 printk("}\n");
833
834 if (err) {
835 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100836 if (err & ATA_ABORTED) printk("DriveStatusError ");
837 if (err & ATA_ICRC) {
838 if (err & ATA_ABORTED)
839 printk("BadCRC ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400840 else printk("Sector ");
841 }
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100842 if (err & ATA_UNC) printk("UncorrectableError ");
843 if (err & ATA_IDNF) printk("SectorIdNotFound ");
844 if (err & ATA_TRK0NF) printk("TrackZeroNotFound ");
845 if (err & ATA_AMNF) printk("AddrMarkNotFound ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400846 printk("}\n");
847 }
848 }
849}
850
851/**
852 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800853 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400854 * @drv_stat: value contained in ATA status register
855 * @drv_err: value contained in ATA error register
856 * @sk: the sense key we'll fill out
857 * @asc: the additional sense code we'll fill out
858 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900859 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400860 *
861 * Converts an ATA error into a SCSI error. Fill out pointers to
862 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
863 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 *
865 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400866 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100868static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
869 u8 *asc, u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Jeff Garzikb0955182005-05-12 15:45:22 -0400871 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100874 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* BBD|ECC|ID|MAR */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100876 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
877 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* BBD|ECC|ID */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100879 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
880 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* ECC|MC|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100882 {0x61, HARDWARE_ERROR, 0x00, 0x00},
883 // Device fault Hardware error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100885 {0x84, ABORTED_COMMAND, 0x47, 0x00},
886 // Data CRC error SCSI parity error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* MC|ID|ABRT|TRK0|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100888 {0x37, NOT_READY, 0x04, 0x00},
889 // Unit offline Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* MCR|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100891 {0x09, NOT_READY, 0x04, 0x00},
892 // Unrecovered disk error Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* Bad address mark */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100894 {0x01, MEDIUM_ERROR, 0x13, 0x00},
895 // Address mark not found for data field
896 /* TRK0 - Track 0 not found */
897 {0x02, HARDWARE_ERROR, 0x00, 0x00},
898 // Hardware error
Gwendal Grignou78062c52013-06-18 10:54:48 -0700899 /* Abort: 0x04 is not translated here, see below */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /* Media change request */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100901 {0x08, NOT_READY, 0x04, 0x00},
902 // FIXME: faking offline
903 /* SRV/IDNF - ID not found */
904 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
905 // Logical address out of range
906 /* MC - Media Changed */
907 {0x20, UNIT_ATTENTION, 0x28, 0x00},
908 // Not ready to ready change, medium may have changed
909 /* ECC - Uncorrectable ECC error */
910 {0x40, MEDIUM_ERROR, 0x11, 0x04},
911 // Unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /* BBD - block marked bad */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100913 {0x80, MEDIUM_ERROR, 0x11, 0x04},
914 // Block marked bad Medium error, unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
916 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100917 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* Must be first because BUSY means no other bits valid */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100919 {0x80, ABORTED_COMMAND, 0x47, 0x00},
920 // Busy, fake parity for now
921 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
922 // Device ready, unaligned write command
923 {0x20, HARDWARE_ERROR, 0x44, 0x00},
924 // Device fault, internal target failure
925 {0x08, ABORTED_COMMAND, 0x47, 0x00},
926 // Timed out in xfer, fake parity for now
927 {0x04, RECOVERED_ERROR, 0x11, 0x00},
928 // Recovered ECC error Medium error, recovered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
930 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * Is this an error we can process/parse
934 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400935 if (drv_stat & ATA_BUSY) {
936 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Jeff Garzikb0955182005-05-12 15:45:22 -0400939 if (drv_err) {
940 /* Look for drv_err */
941 for (i = 0; sense_table[i][0] != 0xFF; i++) {
942 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500943 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -0400944 sense_table[i][0]) {
945 *sk = sense_table[i][1];
946 *asc = sense_table[i][2];
947 *ascq = sense_table[i][3];
948 goto translate_done;
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
Gwendal Grignou78062c52013-06-18 10:54:48 -0700953 /*
954 * Fall back to interpreting status bits. Note that if the drv_err
955 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
956 * is not descriptive enough.
957 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400958 for (i = 0; stat_table[i][0] != 0xFF; i++) {
959 if (stat_table[i][0] & drv_stat) {
960 *sk = stat_table[i][1];
961 *asc = stat_table[i][2];
962 *ascq = stat_table[i][3];
963 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Gwendal Grignou78062c52013-06-18 10:54:48 -0700967 /*
968 * We need a sensible error return here, which is tricky, and one
969 * that won't cause people to do things like return a disk wrongly.
970 */
Alan Cox2d202022006-03-21 15:53:46 +0000971 *sk = ABORTED_COMMAND;
972 *asc = 0x00;
973 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -0400974
975 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +0900976 if (verbose)
977 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
978 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
979 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -0400980 return;
981}
982
983/*
Tejun Heo750426a2006-11-14 22:37:35 +0900984 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -0400985 * @qc: Command that completed.
986 *
987 * This function is specific to the ATA descriptor format sense
988 * block specified for the ATA pass through commands. Regardless
989 * of whether the command errored or not, return a sense
990 * block. Copy all controller registers into the sense
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -0800991 * block. If there was no error, we get the request from an ATA
992 * passthrough command, so we use the following sense data:
993 * sk = RECOVERED ERROR
994 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
995 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400996 *
997 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +0900998 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -0400999 */
Tejun Heo750426a2006-11-14 22:37:35 +09001000static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001001{
1002 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001003 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001004 unsigned char *sb = cmd->sense_buffer;
1005 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +09001006 int verbose = qc->ap->ops->error_handler == NULL;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001007 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001008
1009 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1010
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001011 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001012
1013 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04001014 * Use ata_to_sense_error() to map status register bits
1015 * onto sense key, asc & ascq.
1016 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001017 if (qc->err_mask ||
1018 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001019 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001020 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001021 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001022 } else {
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001023 /*
1024 * ATA PASS-THROUGH INFORMATION AVAILABLE
1025 * Always in descriptor format sense.
1026 */
1027 scsi_build_sense_buffer(1, cmd->sense_buffer,
1028 RECOVERED_ERROR, 0, 0x1D);
Jeff Garzikb0955182005-05-12 15:45:22 -04001029 }
1030
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001031 if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
1032 u8 len;
Jeff Garzikb0955182005-05-12 15:45:22 -04001033
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001034 /* descriptor format */
1035 len = sb[7];
1036 desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
1037 if (!desc) {
1038 if (SCSI_SENSE_BUFFERSIZE < len + 14)
1039 return;
1040 sb[7] = len + 14;
1041 desc = sb + 8 + len;
1042 }
1043 desc[0] = 9;
1044 desc[1] = 12;
1045 /*
1046 * Copy registers into sense buffer.
1047 */
1048 desc[2] = 0x00;
1049 desc[3] = tf->feature; /* == error reg */
1050 desc[5] = tf->nsect;
1051 desc[7] = tf->lbal;
1052 desc[9] = tf->lbam;
1053 desc[11] = tf->lbah;
1054 desc[12] = tf->device;
1055 desc[13] = tf->command; /* == status reg */
Jeff Garzikb0955182005-05-12 15:45:22 -04001056
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001057 /*
1058 * Fill in Extend bit, and the high order bytes
1059 * if applicable.
1060 */
1061 if (tf->flags & ATA_TFLAG_LBA48) {
1062 desc[2] |= 0x01;
1063 desc[4] = tf->hob_nsect;
1064 desc[6] = tf->hob_lbal;
1065 desc[8] = tf->hob_lbam;
1066 desc[10] = tf->hob_lbah;
1067 }
1068 } else {
1069 /* Fixed sense format */
1070 desc[0] = tf->feature;
1071 desc[1] = tf->command; /* status */
1072 desc[2] = tf->device;
1073 desc[3] = tf->nsect;
1074 desc[0] = 0;
1075 if (tf->flags & ATA_TFLAG_LBA48) {
1076 desc[8] |= 0x80;
1077 if (tf->hob_nsect)
1078 desc[8] |= 0x40;
1079 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
1080 desc[8] |= 0x20;
1081 }
1082 desc[9] = tf->lbal;
1083 desc[10] = tf->lbam;
1084 desc[11] = tf->lbah;
Jeff Garzikb0955182005-05-12 15:45:22 -04001085 }
1086}
1087
1088/**
Tejun Heo750426a2006-11-14 22:37:35 +09001089 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -04001090 * @qc: Command that we are erroring out
1091 *
Tejun Heod25614b2006-11-14 22:37:35 +09001092 * Generate sense block for a failed ATA command @qc. Descriptor
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001093 * format is used to accommodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -04001094 *
1095 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001096 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001097 */
Tejun Heo750426a2006-11-14 22:37:35 +09001098static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001099{
Tejun Heod25614b2006-11-14 22:37:35 +09001100 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -04001101 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001102 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001103 unsigned char *sb = cmd->sense_buffer;
Tejun Heo246619d2006-05-15 20:58:16 +09001104 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +09001105 u64 block;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001106 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001107
1108 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1109
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001110 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001111
Tejun Heod25614b2006-11-14 22:37:35 +09001112 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -04001113 * onto sense key, asc & ascq.
1114 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001115 if (qc->err_mask ||
1116 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001117 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001118 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001119 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001120 } else {
1121 /* Could not decode error */
1122 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
1123 tf->command, qc->err_mask);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001124 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001125 return;
Jeff Garzikb0955182005-05-12 15:45:22 -04001126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Tejun Heod25614b2006-11-14 22:37:35 +09001128 block = ata_tf_read_block(&qc->result_tf, dev);
Hannes Reineckecffd1ee2016-04-04 11:43:57 +02001129 if (block == U64_MAX)
1130 return;
Jeff Garzik0274aa22005-06-22 13:50:56 -04001131
Hannes Reineckecf8b49b2016-04-04 11:43:58 +02001132 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
Brian Kinga6cce2a2006-03-17 17:04:15 -06001135static void ata_scsi_sdev_config(struct scsi_device *sdev)
1136{
1137 sdev->use_10_for_rw = 1;
1138 sdev->use_10_for_ms = 1;
Martin K. Petersen3c6bdae2012-09-18 12:19:30 -04001139 sdev->no_report_opcodes = 1;
Martin K. Petersen5db44862012-09-18 12:19:32 -04001140 sdev->no_write_same = 1;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001141
1142 /* Schedule policy is determined by ->qc_defer() callback and
1143 * it needs to see every deferred qc. Set dev_blocked to 1 to
1144 * prevent SCSI midlayer from automatically deferring
1145 * requests.
1146 */
1147 sdev->max_device_blocked = 1;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001148}
1149
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001150/**
1151 * atapi_drain_needed - Check whether data transfer may overflow
Randy Dunlap73fd8b62008-02-19 13:43:21 -08001152 * @rq: request to be checked
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001153 *
1154 * ATAPI commands which transfer variable length data to host
1155 * might overflow due to application error or hardare bug. This
1156 * function checks whether overflow should be drained and ignored
1157 * for @request.
1158 *
1159 * LOCKING:
1160 * None.
1161 *
1162 * RETURNS:
1163 * 1 if ; otherwise, 0.
1164 */
1165static int atapi_drain_needed(struct request *rq)
1166{
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001167 if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001168 return 0;
1169
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001170 if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001171 return 0;
1172
1173 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
1174}
1175
1176static int ata_scsi_dev_config(struct scsi_device *sdev,
1177 struct ata_device *dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -06001178{
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001179 struct request_queue *q = sdev->request_queue;
1180
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02001181 if (!ata_id_has_unload(dev->id))
1182 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1183
Tejun Heo914ed352006-11-01 18:39:55 +09001184 /* configure max sectors */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001185 blk_queue_max_hw_sectors(q, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -06001186
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001187 if (dev->class == ATA_DEV_ATAPI) {
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001188 void *buf;
1189
Tejun Heo729a6a32011-01-20 13:59:06 +01001190 sdev->sector_size = ATA_SECT_SIZE;
1191
1192 /* set DMA padding */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001193 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001194
1195 /* configure draining */
1196 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1197 if (!buf) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001198 ata_dev_err(dev, "drain buffer allocation failed\n");
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001199 return -ENOMEM;
1200 }
1201
1202 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1203 } else {
Grant Grundler295124d2010-08-17 10:56:53 -07001204 sdev->sector_size = ata_id_logical_sector_size(dev->id);
Tejun Heod8cf5382008-01-15 08:46:59 +09001205 sdev->manage_start_stop = 1;
James Bottomleydde20202008-02-19 11:36:56 +01001206 }
Tejun Heod8cf5382008-01-15 08:46:59 +09001207
Tejun Heo729a6a32011-01-20 13:59:06 +01001208 /*
1209 * ata_pio_sectors() expects buffer for each sector to not cross
1210 * page boundary. Enforce it by requiring buffers to be sector
1211 * aligned, which works iff sector_size is not larger than
1212 * PAGE_SIZE. ATAPI devices also need the alignment as
1213 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1214 */
1215 if (sdev->sector_size > PAGE_SIZE)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001216 ata_dev_warn(dev,
Tejun Heo729a6a32011-01-20 13:59:06 +01001217 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1218 sdev->sector_size);
1219
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001220 blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
Tejun Heo729a6a32011-01-20 13:59:06 +01001221
Jeff Garzikf26792d2007-10-29 17:18:39 -04001222 if (dev->flags & ATA_DFLAG_AN)
1223 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1224
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001225 if (dev->flags & ATA_DFLAG_NCQ) {
1226 int depth;
1227
1228 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1229 depth = min(ATA_MAX_QUEUE - 1, depth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001230 scsi_change_queue_depth(sdev, depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001231 }
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001232
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001233 blk_queue_flush_queueable(q, false);
1234
Grant Grundler295124d2010-08-17 10:56:53 -07001235 dev->sdev = sdev;
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001236 return 0;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/**
1240 * ata_scsi_slave_config - Set SCSI device attributes
1241 * @sdev: SCSI device to examine
1242 *
1243 * This is called before we actually start reading
1244 * and writing to the device, to configure certain
1245 * SCSI mid-layer behaviors.
1246 *
1247 * LOCKING:
1248 * Defined by SCSI layer. We don't really care.
1249 */
1250
1251int ata_scsi_slave_config(struct scsi_device *sdev)
1252{
Tejun Heo31534362006-05-31 18:27:36 +09001253 struct ata_port *ap = ata_shost_to_port(sdev->host);
1254 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001255 int rc = 0;
Tejun Heo31534362006-05-31 18:27:36 +09001256
Brian Kinga6cce2a2006-03-17 17:04:15 -06001257 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Tejun Heo31534362006-05-31 18:27:36 +09001259 if (dev)
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001260 rc = ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001262 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
1265/**
Tejun Heo83c47bc2006-05-31 18:28:07 +09001266 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1267 * @sdev: SCSI device to be destroyed
1268 *
1269 * @sdev is about to be destroyed for hot/warm unplugging. If
1270 * this unplugging was initiated by libata as indicated by NULL
1271 * dev->sdev, this function doesn't have to do anything.
1272 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1273 * Clear dev->sdev, schedule the device for ATA detach and invoke
1274 * EH.
1275 *
1276 * LOCKING:
1277 * Defined by SCSI layer. We don't really care.
1278 */
1279void ata_scsi_slave_destroy(struct scsi_device *sdev)
1280{
1281 struct ata_port *ap = ata_shost_to_port(sdev->host);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001282 struct request_queue *q = sdev->request_queue;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001283 unsigned long flags;
1284 struct ata_device *dev;
1285
1286 if (!ap->ops->error_handler)
1287 return;
1288
Jeff Garzikba6a1302006-06-22 23:46:10 -04001289 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09001290 dev = __ata_scsi_find_dev(ap, sdev);
1291 if (dev && dev->sdev) {
1292 /* SCSI device already in CANCEL state, no need to offline it */
1293 dev->sdev = NULL;
1294 dev->flags |= ATA_DFLAG_DETACH;
1295 ata_port_schedule_eh(ap);
1296 }
Jeff Garzikba6a1302006-06-22 23:46:10 -04001297 spin_unlock_irqrestore(ap->lock, flags);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001298
1299 kfree(q->dma_drain_buffer);
1300 q->dma_drain_buffer = NULL;
1301 q->dma_drain_size = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001302}
1303
1304/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001305 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
Marcos Paulo de Souza6d2dd052011-10-31 23:50:16 -02001306 * @ap: ATA port to which the device change the queue depth
1307 * @sdev: SCSI device to configure queue depth for
1308 * @queue_depth: new queue depth
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001309 *
Dan Williamsf6e67032011-09-20 15:10:33 -07001310 * libsas and libata have different approaches for associating a sdev to
1311 * its ata_port.
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001312 *
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001313 */
Dan Williamsf6e67032011-09-20 15:10:33 -07001314int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001315 int queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001316{
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001317 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +09001318 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001319
Tejun Heoc3c70c42007-02-20 23:31:22 +09001320 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001321 return sdev->queue_depth;
1322
1323 dev = ata_scsi_find_dev(ap, sdev);
1324 if (!dev || !ata_dev_enabled(dev))
1325 return sdev->queue_depth;
1326
Tejun Heoc3c70c42007-02-20 23:31:22 +09001327 /* NCQ enabled? */
Tejun Heo360f6542006-09-30 19:45:00 +09001328 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001329 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1330 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
Tejun Heo360f6542006-09-30 19:45:00 +09001331 dev->flags |= ATA_DFLAG_NCQ_OFF;
Tejun Heoc3c70c42007-02-20 23:31:22 +09001332 queue_depth = 1;
1333 }
Tejun Heo360f6542006-09-30 19:45:00 +09001334 spin_unlock_irqrestore(ap->lock, flags);
1335
Tejun Heoc3c70c42007-02-20 23:31:22 +09001336 /* limit and apply queue depth */
1337 queue_depth = min(queue_depth, sdev->host->can_queue);
1338 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1339 queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
1340
1341 if (sdev->queue_depth == queue_depth)
1342 return -EINVAL;
1343
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001344 return scsi_change_queue_depth(sdev, queue_depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001345}
1346
1347/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001348 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1349 * @sdev: SCSI device to configure queue depth for
1350 * @queue_depth: new queue depth
Dan Williamsf6e67032011-09-20 15:10:33 -07001351 *
1352 * This is libata standard hostt->change_queue_depth callback.
1353 * SCSI will call into this callback when user tries to set queue
1354 * depth via sysfs.
1355 *
1356 * LOCKING:
1357 * SCSI layer (we don't care)
1358 *
1359 * RETURNS:
1360 * Newly configured queue depth.
1361 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001362int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
Dan Williamsf6e67032011-09-20 15:10:33 -07001363{
1364 struct ata_port *ap = ata_shost_to_port(sdev->host);
1365
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001366 return __ata_change_queue_depth(ap, sdev, queue_depth);
Dan Williamsf6e67032011-09-20 15:10:33 -07001367}
1368
1369/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001370 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1371 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001372 *
1373 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1374 * (to start). Perhaps these commands should be preceded by
1375 * CHECK POWER MODE to see what power mode the device is already in.
1376 * [See SAT revision 5 at www.t10.org]
1377 *
1378 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001379 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001380 *
1381 * RETURNS:
1382 * Zero on success, non-zero on error.
1383 */
Tejun Heoad706992006-12-17 10:45:57 +09001384static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001385{
Tejun Heo542b1442006-12-17 10:45:08 +09001386 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001387 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +09001388 const u8 *cdb = scmd->cmnd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001389
Tejun Heo2e5704f2006-12-17 10:46:33 +09001390 if (scmd->cmd_len < 5)
1391 goto invalid_fld;
1392
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001393 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1394 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +09001395 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001396 ; /* ignore IMMED bit, violates sat-r05 */
1397 }
Tejun Heo542b1442006-12-17 10:45:08 +09001398 if (cdb[4] & 0x2)
Douglas Gilbertae006512005-10-09 09:09:35 -04001399 goto invalid_fld; /* LOEJ bit set not supported */
Tejun Heo542b1442006-12-17 10:45:08 +09001400 if (((cdb[4] >> 4) & 0xf) != 0)
Douglas Gilbertae006512005-10-09 09:09:35 -04001401 goto invalid_fld; /* power conditions not supported */
Tejun Heoe31e85312007-09-23 13:14:13 +09001402
Tejun Heo542b1442006-12-17 10:45:08 +09001403 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001404 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -04001405
1406 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +09001407 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -04001408
1409 tf->lbah = 0x0;
1410 tf->lbam = 0x0;
1411 tf->lbal = 0x0;
1412 tf->device |= ATA_LBA;
1413 } else {
1414 /* CHS */
1415 tf->lbal = 0x1; /* sect */
1416 tf->lbam = 0x0; /* cyl low */
1417 tf->lbah = 0x0; /* cyl high */
1418 }
1419
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001420 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
Tejun Heo920a4b12007-05-04 21:28:48 +02001421 } else {
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001422 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1423 * or S5) causing some drives to spin up and down again.
1424 */
1425 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1426 system_state == SYSTEM_POWER_OFF)
1427 goto skip;
1428
1429 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1430 system_entering_hibernation())
1431 goto skip;
1432
Robert Hancock78981a72007-01-30 00:59:18 -08001433 /* Issue ATA STANDBY IMMEDIATE command */
1434 tf->command = ATA_CMD_STANDBYNOW1;
Tejun Heo920a4b12007-05-04 21:28:48 +02001435 }
Robert Hancock78981a72007-01-30 00:59:18 -08001436
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001437 /*
1438 * Standby and Idle condition timers could be implemented but that
1439 * would require libata to implement the Power condition mode page
1440 * and allow the user to change it. Changing mode pages requires
1441 * MODE SELECT to be implemented.
1442 */
1443
1444 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001445
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001446 invalid_fld:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001447 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001448 /* "Invalid field in cbd" */
1449 return 1;
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001450 skip:
1451 scmd->result = SAM_STAT_GOOD;
1452 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001453}
1454
1455
1456/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1458 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 *
1460 * Sets up an ATA taskfile to issue FLUSH CACHE or
1461 * FLUSH CACHE EXT.
1462 *
1463 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001464 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 *
1466 * RETURNS:
1467 * Zero on success, non-zero on error.
1468 */
Tejun Heoad706992006-12-17 10:45:57 +09001469static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 struct ata_taskfile *tf = &qc->tf;
1472
1473 tf->flags |= ATA_TFLAG_DEVICE;
1474 tf->protocol = ATA_PROT_NODATA;
1475
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001476 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 tf->command = ATA_CMD_FLUSH_EXT;
1478 else
1479 tf->command = ATA_CMD_FLUSH;
1480
Tejun Heob666da32007-10-26 15:53:59 +09001481 /* flush is critical for IO integrity, consider it an IO command */
1482 qc->flags |= ATA_QCFLAG_IO;
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return 0;
1485}
1486
1487/**
Albert Lee3aef5232005-10-04 08:47:43 -04001488 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001489 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001490 *
1491 * Calculate LBA and transfer length for 6-byte commands.
1492 *
1493 * RETURNS:
1494 * @plba: the LBA
1495 * @plen: the transfer length
1496 */
Tejun Heo542b1442006-12-17 10:45:08 +09001497static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001498{
1499 u64 lba = 0;
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001500 u32 len;
Albert Lee3aef5232005-10-04 08:47:43 -04001501
1502 VPRINTK("six-byte command\n");
1503
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001504 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
Tejun Heo542b1442006-12-17 10:45:08 +09001505 lba |= ((u64)cdb[2]) << 8;
1506 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001507
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001508 len = cdb[4];
Albert Lee3aef5232005-10-04 08:47:43 -04001509
1510 *plba = lba;
1511 *plen = len;
1512}
1513
1514/**
1515 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001516 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001517 *
1518 * Calculate LBA and transfer length for 10-byte commands.
1519 *
1520 * RETURNS:
1521 * @plba: the LBA
1522 * @plen: the transfer length
1523 */
Tejun Heo542b1442006-12-17 10:45:08 +09001524static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001525{
1526 u64 lba = 0;
1527 u32 len = 0;
1528
1529 VPRINTK("ten-byte command\n");
1530
Tejun Heo542b1442006-12-17 10:45:08 +09001531 lba |= ((u64)cdb[2]) << 24;
1532 lba |= ((u64)cdb[3]) << 16;
1533 lba |= ((u64)cdb[4]) << 8;
1534 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001535
Tejun Heo542b1442006-12-17 10:45:08 +09001536 len |= ((u32)cdb[7]) << 8;
1537 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001538
1539 *plba = lba;
1540 *plen = len;
1541}
1542
1543/**
1544 * scsi_16_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001545 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001546 *
1547 * Calculate LBA and transfer length for 16-byte commands.
1548 *
1549 * RETURNS:
1550 * @plba: the LBA
1551 * @plen: the transfer length
1552 */
Tejun Heo542b1442006-12-17 10:45:08 +09001553static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001554{
1555 u64 lba = 0;
1556 u32 len = 0;
1557
1558 VPRINTK("sixteen-byte command\n");
1559
Tejun Heo542b1442006-12-17 10:45:08 +09001560 lba |= ((u64)cdb[2]) << 56;
1561 lba |= ((u64)cdb[3]) << 48;
1562 lba |= ((u64)cdb[4]) << 40;
1563 lba |= ((u64)cdb[5]) << 32;
1564 lba |= ((u64)cdb[6]) << 24;
1565 lba |= ((u64)cdb[7]) << 16;
1566 lba |= ((u64)cdb[8]) << 8;
1567 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001568
Tejun Heo542b1442006-12-17 10:45:08 +09001569 len |= ((u32)cdb[10]) << 24;
1570 len |= ((u32)cdb[11]) << 16;
1571 len |= ((u32)cdb[12]) << 8;
1572 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001573
1574 *plba = lba;
1575 *plen = len;
1576}
1577
1578/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1580 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 *
1582 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1583 *
1584 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001585 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 *
1587 * RETURNS:
1588 * Zero on success, non-zero on error.
1589 */
Tejun Heoad706992006-12-17 10:45:57 +09001590static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Tejun Heo542b1442006-12-17 10:45:08 +09001592 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001594 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001596 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001597 u64 block;
1598 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1601 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Tejun Heo2e5704f2006-12-17 10:46:33 +09001603 if (cdb[0] == VERIFY) {
1604 if (scmd->cmd_len < 10)
1605 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001606 scsi_10_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001607 } else if (cdb[0] == VERIFY_16) {
1608 if (scmd->cmd_len < 16)
1609 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001610 scsi_16_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001611 } else
Douglas Gilbertae006512005-10-09 09:09:35 -04001612 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Albert Lee8bf62ece2005-05-12 15:29:42 -04001614 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001615 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001616 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001617 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001618 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001619 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Albert Lee07506692005-10-12 15:04:18 +08001621 if (dev->flags & ATA_DFLAG_LBA) {
1622 tf->flags |= ATA_TFLAG_LBA;
1623
Albert Leec6a33e22005-10-12 15:12:26 +08001624 if (lba_28_ok(block, n_block)) {
1625 /* use LBA28 */
1626 tf->command = ATA_CMD_VERIFY;
1627 tf->device |= (block >> 24) & 0xf;
1628 } else if (lba_48_ok(block, n_block)) {
1629 if (!(dev->flags & ATA_DFLAG_LBA48))
1630 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001631
1632 /* use LBA48 */
1633 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001634 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Albert Lee8bf62ece2005-05-12 15:29:42 -04001636 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Albert Lee8bf62ece2005-05-12 15:29:42 -04001638 tf->hob_lbah = (block >> 40) & 0xff;
1639 tf->hob_lbam = (block >> 32) & 0xff;
1640 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001641 } else
1642 /* request too large even for LBA48 */
1643 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001644
1645 tf->nsect = n_block & 0xff;
1646
1647 tf->lbah = (block >> 16) & 0xff;
1648 tf->lbam = (block >> 8) & 0xff;
1649 tf->lbal = block & 0xff;
1650
1651 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001653 /* CHS */
1654 u32 sect, head, cyl, track;
1655
Albert Leec6a33e22005-10-12 15:12:26 +08001656 if (!lba_28_ok(block, n_block))
1657 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001658
Albert Lee8bf62ece2005-05-12 15:29:42 -04001659 /* Convert LBA to CHS */
1660 track = (u32)block / dev->sectors;
1661 cyl = track / dev->heads;
1662 head = track % dev->heads;
1663 sect = (u32)block % dev->sectors + 1;
1664
Albert Leec187c4b2005-10-04 08:46:51 -04001665 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1666 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001667
1668 /* Check whether the converted CHS can fit.
1669 Cylinder: 0-65535
Albert Lee8bf62ece2005-05-12 15:29:42 -04001670 Head: 0-15
1671 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001672 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001673 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001676 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1677 tf->lbal = sect;
1678 tf->lbam = cyl;
1679 tf->lbah = cyl >> 8;
1680 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001684
1685invalid_fld:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001686 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001687 /* "Invalid field in cbd" */
1688 return 1;
1689
1690out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001691 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001692 /* "Logical Block Address out of range" */
1693 return 1;
1694
1695nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001696 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001697 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
1700/**
1701 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1702 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 *
1704 * Converts any of six SCSI read/write commands into the
1705 * ATA counterpart, including starting sector (LBA),
1706 * sector count, and taking into account the device's LBA48
1707 * support.
1708 *
1709 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1710 * %WRITE_16 are currently supported.
1711 *
1712 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001713 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 *
1715 * RETURNS:
1716 * Zero on success, non-zero on error.
1717 */
Tejun Heoad706992006-12-17 10:45:57 +09001718static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Tejun Heo542b1442006-12-17 10:45:08 +09001720 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001721 const u8 *cdb = scmd->cmnd;
Tejun Heobd056d72006-11-14 22:47:10 +09001722 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001723 u64 block;
1724 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001725 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Tejun Heo542b1442006-12-17 10:45:08 +09001727 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001728 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001730 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001731 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001732 case READ_10:
1733 case WRITE_10:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001734 if (unlikely(scmd->cmd_len < 10))
1735 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001736 scsi_10_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001737 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001738 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001739 break;
1740 case READ_6:
1741 case WRITE_6:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001742 if (unlikely(scmd->cmd_len < 6))
1743 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001744 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001745
1746 /* for 6-byte r/w commands, transfer length 0
1747 * means 256 blocks of data, not 0 block.
1748 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001749 if (!n_block)
1750 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001751 break;
1752 case READ_16:
1753 case WRITE_16:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001754 if (unlikely(scmd->cmd_len < 16))
1755 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001756 scsi_16_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001757 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001758 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001759 break;
1760 default:
Albert Lee8bf62ece2005-05-12 15:29:42 -04001761 DPRINTK("no-byte command\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001762 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764
Albert Lee8bf62ece2005-05-12 15:29:42 -04001765 /* Check and compose ATA command */
1766 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001767 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1768 * length 0 means transfer 0 block of data.
1769 * However, for ATA R/W commands, sector count 0 means
1770 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001771 *
1772 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001773 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001774 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001775
Tejun Heobd056d72006-11-14 22:47:10 +09001776 qc->flags |= ATA_QCFLAG_IO;
Grant Grundler295124d2010-08-17 10:56:53 -07001777 qc->nbytes = n_block * scmd->device->sector_size;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001778
Tejun Heobd056d72006-11-14 22:47:10 +09001779 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1780 qc->tag);
1781 if (likely(rc == 0))
1782 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001783
Tejun Heobd056d72006-11-14 22:47:10 +09001784 if (rc == -ERANGE)
1785 goto out_of_range;
1786 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001787invalid_fld:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001788 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001789 /* "Invalid field in cbd" */
1790 return 1;
1791
1792out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001793 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001794 /* "Logical Block Address out of range" */
1795 return 1;
1796
1797nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001798 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001799 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001802static void ata_qc_done(struct ata_queued_cmd *qc)
1803{
1804 struct scsi_cmnd *cmd = qc->scsicmd;
1805 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1806
1807 ata_qc_free(qc);
1808 done(cmd);
1809}
1810
Tejun Heo77853bf2006-01-23 13:09:36 +09001811static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Tejun Heoc31f5712006-11-22 12:39:43 +09001813 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001815 u8 *cdb = cmd->cmnd;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001816 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jeff Garzikb0955182005-05-12 15:45:22 -04001818 /* For ATA pass thru (SAT) commands, generate a sense block if
1819 * user mandated it or if there's an error. Note that if we
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001820 * generate because the user forced us to [CK_COND =1], a check
1821 * condition is generated and the ATA register values are returned
Jeff Garzikb0955182005-05-12 15:45:22 -04001822 * whether the command completed successfully or not. If there
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001823 * was no error, we use the following sense data:
1824 * sk = RECOVERED ERROR
1825 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
Jeff Garzikb0955182005-05-12 15:45:22 -04001826 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001827 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001828 ((cdb[2] & 0x20) || need_sense))
Tejun Heo750426a2006-11-14 22:37:35 +09001829 ata_gen_passthru_sense(qc);
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +02001830 else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1831 cmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001832 else if (need_sense)
1833 ata_gen_ata_sense(qc);
1834 else
1835 cmd->result = SAM_STAT_GOOD;
Jeff Garzikb0955182005-05-12 15:45:22 -04001836
Tejun Heoc31f5712006-11-22 12:39:43 +09001837 if (need_sense && !ap->ops->error_handler)
Tejun Heo44877b42007-02-21 01:06:51 +09001838 ata_dump_status(ap->print_id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001840 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
1843/**
1844 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 * @dev: ATA device to which the command is addressed
1846 * @cmd: SCSI command to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1848 *
1849 * Our ->queuecommand() function has decided that the SCSI
1850 * command issued can be directly translated into an ATA
1851 * command, rather than handled internally.
1852 *
1853 * This function sets up an ata_queued_cmd structure for the
1854 * SCSI command, and sends that ata_queued_cmd to the hardware.
1855 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001856 * The xlat_func argument (actor) returns 0 if ready to execute
1857 * ATA command, else 1 to finish translation. If 1 is returned
1858 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1859 * to be set reflecting an error condition or clean (early)
1860 * termination.
1861 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001863 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09001864 *
1865 * RETURNS:
1866 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1867 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 */
Tejun Heo2115ea92006-05-15 21:03:39 +09001869static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09001870 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Tejun Heo31cc23b2007-09-23 13:14:12 +09001872 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 struct ata_queued_cmd *qc;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001874 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 VPRINTK("ENTER\n");
1877
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05001878 qc = ata_scsi_qc_new(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04001880 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05001883 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1884 cmd->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harrosh71201652007-09-18 17:48:50 +02001885 if (unlikely(scsi_bufflen(cmd) < 1)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001886 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001887 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889
Boaz Harrosh71201652007-09-18 17:48:50 +02001890 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 qc->dma_dir = cmd->sc_data_direction;
1893 }
1894
1895 qc->complete_fn = ata_scsi_qc_complete;
1896
Tejun Heoad706992006-12-17 10:45:57 +09001897 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04001898 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Tejun Heo31cc23b2007-09-23 13:14:12 +09001900 if (ap->ops->qc_defer) {
1901 if ((rc = ap->ops->qc_defer(qc)))
1902 goto defer;
1903 }
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09001906 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Douglas Gilbertae006512005-10-09 09:09:35 -04001911early_finish:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001912 ata_qc_free(qc);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05001913 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -04001914 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001915 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001916
1917err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04001919 cmd->result = (DID_ERROR << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05001920 cmd->scsi_done(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05001921err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04001922 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001923 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001924
1925defer:
Tejun Heo31cc23b2007-09-23 13:14:12 +09001926 ata_qc_free(qc);
Tejun Heo3dc1d882006-05-15 21:03:45 +09001927 DPRINTK("EXIT - defer\n");
Tejun Heo31cc23b2007-09-23 13:14:12 +09001928 if (rc == ATA_DEFER_LINK)
1929 return SCSI_MLQUEUE_DEVICE_BUSY;
1930 else
1931 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
1934/**
1935 * ata_scsi_rbuf_get - Map response buffer.
Randy Dunlapec2a20e2008-04-30 12:57:00 -07001936 * @cmd: SCSI command containing buffer to be mapped.
Tejun Heo87340e92008-04-28 17:48:51 +09001937 * @flags: unsigned long variable to store irq enable status
1938 * @copy_in: copy in from user buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 *
Tejun Heo87340e92008-04-28 17:48:51 +09001940 * Prepare buffer for simulated SCSI commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 *
1942 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09001943 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 *
1945 * RETURNS:
Tejun Heo87340e92008-04-28 17:48:51 +09001946 * Pointer to response buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
Tejun Heo87340e92008-04-28 17:48:51 +09001948static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
1949 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Tejun Heo87340e92008-04-28 17:48:51 +09001951 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Tejun Heo87340e92008-04-28 17:48:51 +09001953 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
1954 if (copy_in)
1955 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1956 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1957 return ata_scsi_rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960/**
1961 * ata_scsi_rbuf_put - Unmap response buffer.
1962 * @cmd: SCSI command containing buffer to be unmapped.
Tejun Heo87340e92008-04-28 17:48:51 +09001963 * @copy_out: copy out result
1964 * @flags: @flags passed to ata_scsi_rbuf_get()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 *
Tejun Heo87340e92008-04-28 17:48:51 +09001966 * Returns rbuf buffer. The result is copied to @cmd's buffer if
1967 * @copy_back is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 *
1969 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09001970 * Unlocks ata_scsi_rbuf_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 */
Tejun Heo87340e92008-04-28 17:48:51 +09001972static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
1973 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Tejun Heo87340e92008-04-28 17:48:51 +09001975 if (copy_out)
1976 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1977 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1978 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
1981/**
1982 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1983 * @args: device IDENTIFY data / SCSI command of interest.
1984 * @actor: Callback hook for desired SCSI command simulator
1985 *
1986 * Takes care of the hard work of simulating a SCSI command...
1987 * Mapping the response buffer, calling the command's handler,
1988 * and handling the handler's return value. This return value
1989 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04001990 * completed successfully (0), or not (in which case cmd->result
1991 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 *
1993 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001994 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
Tejun Heof0761be2008-04-28 17:16:52 +09001996static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
Tejun Heo87340e92008-04-28 17:48:51 +09001997 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
1999 u8 *rbuf;
Tejun Heo87340e92008-04-28 17:48:51 +09002000 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 struct scsi_cmnd *cmd = args->cmd;
Jeff Garzikb445c562008-02-29 19:10:51 -05002002 unsigned long flags;
2003
Tejun Heo87340e92008-04-28 17:48:51 +09002004 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
2005 rc = actor(args, rbuf);
2006 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
Jeff Garzikb445c562008-02-29 19:10:51 -05002007
Douglas Gilbertae006512005-10-09 09:09:35 -04002008 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04002010 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
2013/**
2014 * ata_scsiop_inq_std - Simulate INQUIRY command
2015 * @args: device IDENTIFY data / SCSI command of interest.
2016 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 *
2018 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05002019 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 *
2021 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002022 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 */
Tejun Heo87340e92008-04-28 17:48:51 +09002024static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Tejun Heo87340e92008-04-28 17:48:51 +09002026 const u8 versions[] = {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002027 0x00,
Tejun Heo87340e92008-04-28 17:48:51 +09002028 0x60, /* SAM-3 (no version claimed) */
2029
2030 0x03,
2031 0x20, /* SBC-2 (no version claimed) */
2032
2033 0x02,
2034 0x60 /* SPC-3 (no version claimed) */
2035 };
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002036 const u8 versions_zbc[] = {
2037 0x00,
2038 0xA0, /* SAM-5 (no version claimed) */
2039
2040 0x04,
2041 0xC0, /* SBC-3 (no version claimed) */
2042
2043 0x04,
2044 0x60, /* SPC-4 (no version claimed) */
2045
2046 0x60,
2047 0x20, /* ZBC (no version claimed) */
2048 };
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 u8 hdr[] = {
2051 TYPE_DISK,
2052 0,
2053 0x5, /* claim SPC-3 version compatibility */
2054 2,
2055 95 - 4
2056 };
2057
Tejun Heo87340e92008-04-28 17:48:51 +09002058 VPRINTK("ENTER\n");
2059
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02002060 /* set scsi removable (RMB) bit per ata bit, or if the
2061 * AHCI port says it's external (Hotplug-capable, eSATA).
2062 */
2063 if (ata_id_removable(args->id) ||
2064 (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 hdr[1] |= (1 << 7);
2066
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002067 if (args->dev->class == ATA_DEV_ZAC) {
2068 hdr[0] = TYPE_ZBC;
2069 hdr[2] = 0x6; /* ZBC is defined in SPC-4 */
2070 }
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002073 memcpy(&rbuf[8], "ATA ", 8);
2074 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
Keith Buschc49a6bf2014-05-01 11:12:03 -06002075
2076 /* From SAT, use last 2 words from fw rev unless they are spaces */
2077 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
2078 if (strncmp(&rbuf[32], " ", 4) == 0)
2079 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Tejun Heo87340e92008-04-28 17:48:51 +09002081 if (rbuf[32] == 0 || rbuf[32] == ' ')
2082 memcpy(&rbuf[32], "n/a ", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002084 if (args->dev->class == ATA_DEV_ZAC)
2085 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
2086 else
2087 memcpy(rbuf + 58, versions, sizeof(versions));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 return 0;
2090}
2091
2092/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002093 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 * @args: device IDENTIFY data / SCSI command of interest.
2095 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002097 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 *
2099 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002100 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 */
Tejun Heo87340e92008-04-28 17:48:51 +09002102static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 const u8 pages[] = {
2105 0x00, /* page 0x00, this page */
2106 0x80, /* page 0x80, unit serial no page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002107 0x83, /* page 0x83, device ident page */
2108 0x89, /* page 0x89, ata info page */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002109 0xb0, /* page 0xb0, block limits page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002110 0xb1, /* page 0xb1, block device characteristics page */
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002111 0xb2, /* page 0xb2, thin provisioning page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 };
Tejun Heo87340e92008-04-28 17:48:51 +09002113
Jeff Garzikb142eb62006-03-21 20:37:47 -05002114 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
Tejun Heo87340e92008-04-28 17:48:51 +09002115 memcpy(rbuf + 4, pages, sizeof(pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return 0;
2117}
2118
2119/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002120 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
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 *
2124 * Returns ATA device serial number.
2125 *
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_80(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 const u8 hdr[] = {
2132 0,
2133 0x80, /* this page code */
2134 0,
Tejun Heoa0cf7332007-01-02 20:18:49 +09002135 ATA_ID_SERNO_LEN, /* page len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 };
Tejun Heo87340e92008-04-28 17:48:51 +09002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002139 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2140 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return 0;
2142}
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002145 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 * @args: device IDENTIFY data / SCSI command of interest.
2147 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002149 * Yields two logical unit device identification designators:
2150 * - vendor specific ASCII containing the ATA serial number
2151 * - SAT defined "t10 vendor id based" containing ASCII vendor
2152 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 *
2154 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002155 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 */
Tejun Heo87340e92008-04-28 17:48:51 +09002157static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Jeff Garzikb142eb62006-03-21 20:37:47 -05002159 const int sat_model_serial_desc_len = 68;
Tejun Heo87340e92008-04-28 17:48:51 +09002160 int num;
Jeff Garzikb142eb62006-03-21 20:37:47 -05002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05002163 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Tejun Heo87340e92008-04-28 17:48:51 +09002165 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2166 rbuf[num + 0] = 2;
2167 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2168 num += 4;
2169 ata_id_string(args->id, (unsigned char *) rbuf + num,
2170 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2171 num += ATA_ID_SERNO_LEN;
2172
2173 /* SAT defined lu model and serial numbers descriptor */
2174 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2175 rbuf[num + 0] = 2;
2176 rbuf[num + 1] = 1;
2177 rbuf[num + 3] = sat_model_serial_desc_len;
2178 num += 4;
2179 memcpy(rbuf + num, "ATA ", 8);
2180 num += 8;
2181 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2182 ATA_ID_PROD_LEN);
2183 num += ATA_ID_PROD_LEN;
2184 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2185 ATA_ID_SERNO_LEN);
2186 num += ATA_ID_SERNO_LEN;
2187
Hannes Reinecke6b3b9d72011-03-07 08:56:44 +01002188 if (ata_id_has_wwn(args->id)) {
2189 /* SAT defined lu world wide name */
2190 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2191 rbuf[num + 0] = 1;
2192 rbuf[num + 1] = 3;
2193 rbuf[num + 3] = ATA_ID_WWN_LEN;
2194 num += 4;
2195 ata_id_string(args->id, (unsigned char *) rbuf + num,
2196 ATA_ID_WWN, ATA_ID_WWN_LEN);
2197 num += ATA_ID_WWN_LEN;
2198 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05002199 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return 0;
2201}
2202
2203/**
Jeff Garzikad355b42007-09-21 20:38:03 -04002204 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2205 * @args: device IDENTIFY data / SCSI command of interest.
2206 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Jeff Garzikad355b42007-09-21 20:38:03 -04002207 *
2208 * Yields SAT-specified ATA VPD page.
2209 *
2210 * LOCKING:
2211 * spin_lock_irqsave(host lock)
2212 */
Tejun Heo87340e92008-04-28 17:48:51 +09002213static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
Jeff Garzikad355b42007-09-21 20:38:03 -04002214{
Jeff Garzikad355b42007-09-21 20:38:03 -04002215 struct ata_taskfile tf;
Jeff Garzikad355b42007-09-21 20:38:03 -04002216
Jeff Garzikad355b42007-09-21 20:38:03 -04002217 memset(&tf, 0, sizeof(tf));
2218
Tejun Heo87340e92008-04-28 17:48:51 +09002219 rbuf[1] = 0x89; /* our page code */
2220 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2221 rbuf[3] = (0x238 & 0xff);
Jeff Garzikad355b42007-09-21 20:38:03 -04002222
Tejun Heo87340e92008-04-28 17:48:51 +09002223 memcpy(&rbuf[8], "linux ", 8);
2224 memcpy(&rbuf[16], "libata ", 16);
2225 memcpy(&rbuf[32], DRV_VERSION, 4);
Jeff Garzikad355b42007-09-21 20:38:03 -04002226
2227 /* we don't store the ATA device signature, so we fake it */
2228
2229 tf.command = ATA_DRDY; /* really, this is Status reg */
2230 tf.lbal = 0x1;
2231 tf.nsect = 0x1;
2232
Tejun Heo87340e92008-04-28 17:48:51 +09002233 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
2234 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
Jeff Garzikad355b42007-09-21 20:38:03 -04002235
Tejun Heo87340e92008-04-28 17:48:51 +09002236 rbuf[56] = ATA_CMD_ID_ATA;
Jeff Garzikad355b42007-09-21 20:38:03 -04002237
Tejun Heo87340e92008-04-28 17:48:51 +09002238 memcpy(&rbuf[60], &args->id[0], 512);
Jeff Garzikad355b42007-09-21 20:38:03 -04002239 return 0;
2240}
2241
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002242static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2243{
Grant Grundler295124d2010-08-17 10:56:53 -07002244 u16 min_io_sectors;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002245
2246 rbuf[1] = 0xb0;
2247 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2248
2249 /*
2250 * Optimal transfer length granularity.
2251 *
2252 * This is always one physical block, but for disks with a smaller
2253 * logical than physical sector size we need to figure out what the
2254 * latter is.
2255 */
Grant Grundler295124d2010-08-17 10:56:53 -07002256 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002257 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2258
2259 /*
2260 * Optimal unmap granularity.
2261 *
2262 * The ATA spec doesn't even know about a granularity or alignment
2263 * for the TRIM command. We can leave away most of the unmap related
2264 * VPD page entries, but we have specifify a granularity to signal
2265 * that we support some form of unmap - in thise case via WRITE SAME
2266 * with the unmap bit set.
2267 */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002268 if (ata_id_has_trim(args->id)) {
Martin K. Petersen5f4e2062011-05-17 22:13:23 -04002269 put_unaligned_be64(65535 * 512 / 8, &rbuf[36]);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002270 put_unaligned_be32(1, &rbuf[28]);
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002271 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002272
2273 return 0;
2274}
2275
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002276static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2277{
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002278 int form_factor = ata_id_form_factor(args->id);
2279 int media_rotation_rate = ata_id_rotation_rate(args->id);
2280
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002281 rbuf[1] = 0xb1;
2282 rbuf[3] = 0x3c;
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002283 rbuf[4] = media_rotation_rate >> 8;
2284 rbuf[5] = media_rotation_rate;
2285 rbuf[7] = form_factor;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002286
2287 return 0;
2288}
2289
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002290static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
2291{
2292 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2293 rbuf[1] = 0xb2;
2294 rbuf[3] = 0x4;
2295 rbuf[5] = 1 << 6; /* TPWS */
2296
2297 return 0;
2298}
2299
Jeff Garzikad355b42007-09-21 20:38:03 -04002300/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002301 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 * @args: device IDENTIFY data / SCSI command of interest.
2303 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 *
2305 * No operation. Simply returns success to caller, to indicate
2306 * that the caller should successfully complete this SCSI command.
2307 *
2308 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002309 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 */
Tejun Heo87340e92008-04-28 17:48:51 +09002311static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 VPRINTK("ENTER\n");
2314 return 0;
2315}
2316
2317/**
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002318 * modecpy - Prepare response for MODE SENSE
2319 * @dest: output buffer
2320 * @src: data being copied
2321 * @n: length of mode page
2322 * @changeable: whether changeable parameters are requested
2323 *
2324 * Generate a generic MODE SENSE page for either current or changeable
2325 * parameters.
2326 *
2327 * LOCKING:
2328 * None.
2329 */
2330static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2331{
2332 if (changeable) {
2333 memcpy(dest, src, 2);
2334 memset(dest + 2, 0, n - 2);
2335 } else {
2336 memcpy(dest, src, n);
2337 }
2338}
2339
2340/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 * ata_msense_caching - Simulate MODE SENSE caching info page
2342 * @id: device IDENTIFY data
Tejun Heo87340e92008-04-28 17:48:51 +09002343 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002344 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 *
2346 * Generate a caching info page, which conditionally indicates
2347 * write caching to the SCSI layer, depending on device
2348 * capabilities.
2349 *
2350 * LOCKING:
2351 * None.
2352 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002353static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002355 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02002356 if (changeable || ata_id_wcache_enabled(id))
Tejun Heo87340e92008-04-28 17:48:51 +09002357 buf[2] |= (1 << 2); /* write cache enable */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002358 if (!changeable && !ata_id_rahead_enabled(id))
Tejun Heo87340e92008-04-28 17:48:51 +09002359 buf[12] |= (1 << 5); /* disable read ahead */
2360 return sizeof(def_cache_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
2363/**
2364 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002365 * @dev: ATA device of interest
Tejun Heo87340e92008-04-28 17:48:51 +09002366 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002367 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 *
2369 * Generate a generic MODE SENSE control mode page.
2370 *
2371 * LOCKING:
2372 * None.
2373 */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002374static unsigned int ata_msense_ctl_mode(struct ata_device *dev, u8 *buf,
2375 bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002377 modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002378 if (changeable && (dev->flags & ATA_DFLAG_D_SENSE))
2379 buf[2] |= (1 << 2); /* Descriptor sense requested */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002380 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
2383/**
2384 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002385 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002386 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 *
2388 * Generate a generic MODE SENSE r/w error recovery page.
2389 *
2390 * LOCKING:
2391 * None.
2392 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002393static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002395 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2396 changeable);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002397 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002400/*
2401 * We can turn this into a real blacklist if it's needed, for now just
2402 * blacklist any Maxtor BANC1G10 revision firmware
2403 */
2404static int ata_dev_supports_fua(u16 *id)
2405{
Tejun Heoa0cf7332007-01-02 20:18:49 +09002406 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002407
Jeff Garzikc3c013a2006-02-27 22:31:19 -05002408 if (!libata_fua)
2409 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002410 if (!ata_id_has_fua(id))
2411 return 0;
2412
Tejun Heoa0cf7332007-01-02 20:18:49 +09002413 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2414 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002415
Tejun Heo2e026712006-02-12 22:47:04 +09002416 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002417 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09002418 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002419 return 1;
2420
2421 return 0; /* blacklisted */
2422}
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424/**
2425 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2426 * @args: device IDENTIFY data / SCSI command of interest.
2427 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002429 * Simulate MODE SENSE commands. Assume this is invoked for direct
2430 * access devices (e.g. disks) only. There should be no block
2431 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 *
2433 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002434 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 */
Tejun Heo87340e92008-04-28 17:48:51 +09002436static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002438 struct ata_device *dev = args->dev;
Tejun Heo87340e92008-04-28 17:48:51 +09002439 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002440 const u8 sat_blk_desc[] = {
2441 0, 0, 0, 0, /* number of blocks: sat unspecified */
2442 0,
2443 0, 0x2, 0x0 /* block length: 512 bytes */
2444 };
2445 u8 pg, spg;
Tejun Heo87340e92008-04-28 17:48:51 +09002446 unsigned int ebd, page_control, six_byte;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002447 u8 dpofua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 VPRINTK("ENTER\n");
2450
2451 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002452 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2453 /*
2454 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002458 switch (page_control) {
2459 case 0: /* current */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002460 case 1: /* changeable */
2461 case 2: /* defaults */
Douglas Gilbertae006512005-10-09 09:09:35 -04002462 break; /* supported */
2463 case 3: /* saved */
2464 goto saving_not_supp;
Douglas Gilbertae006512005-10-09 09:09:35 -04002465 default:
2466 goto invalid_fld;
2467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Tejun Heo87340e92008-04-28 17:48:51 +09002469 if (six_byte)
2470 p += 4 + (ebd ? 8 : 0);
2471 else
2472 p += 8 + (ebd ? 8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002474 pg = scsicmd[2] & 0x3f;
2475 spg = scsicmd[3];
2476 /*
2477 * No mode subpages supported (yet) but asking for _all_
2478 * subpages may be valid
2479 */
2480 if (spg && (spg != ALL_SUB_MPAGES))
2481 goto invalid_fld;
2482
2483 switch(pg) {
2484 case RW_RECOVERY_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002485 p += ata_msense_rw_recovery(p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
2487
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002488 case CACHE_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002489 p += ata_msense_caching(args->id, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 break;
2491
Tejun Heo87340e92008-04-28 17:48:51 +09002492 case CONTROL_MPAGE:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002493 p += ata_msense_ctl_mode(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002496 case ALL_MPAGES:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002497 p += ata_msense_rw_recovery(p, page_control == 1);
2498 p += ata_msense_caching(args->id, p, page_control == 1);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002499 p += ata_msense_ctl_mode(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 break;
2501
2502 default: /* invalid page code */
Douglas Gilbertae006512005-10-09 09:09:35 -04002503 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
2505
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002506 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002507 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002508 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2509 dpofua = 1 << 4;
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (six_byte) {
Tejun Heo87340e92008-04-28 17:48:51 +09002512 rbuf[0] = p - rbuf - 1;
2513 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002514 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002515 rbuf[3] = sizeof(sat_blk_desc);
2516 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 } else {
Tejun Heo87340e92008-04-28 17:48:51 +09002519 unsigned int output_len = p - rbuf - 2;
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 rbuf[0] = output_len >> 8;
Tejun Heo87340e92008-04-28 17:48:51 +09002522 rbuf[1] = output_len;
2523 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002524 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002525 rbuf[7] = sizeof(sat_blk_desc);
2526 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002530
2531invalid_fld:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002532 ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04002533 /* "Invalid field in cbd" */
2534 return 1;
2535
2536saving_not_supp:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002537 ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04002538 /* "Saving parameters not supported" */
2539 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542/**
2543 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2544 * @args: device IDENTIFY data / SCSI command of interest.
2545 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 *
2547 * Simulate READ CAPACITY commands.
2548 *
2549 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002550 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 */
Tejun Heo87340e92008-04-28 17:48:51 +09002552static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002554 struct ata_device *dev = args->dev;
2555 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
Grant Grundler295124d2010-08-17 10:56:53 -07002556 u32 sector_size; /* physical sector size in bytes */
2557 u8 log2_per_phys;
2558 u16 lowest_aligned;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002559
Grant Grundler295124d2010-08-17 10:56:53 -07002560 sector_size = ata_id_logical_sector_size(dev->id);
2561 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2562 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
2564 VPRINTK("ENTER\n");
2565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002567 if (last_lba >= 0xffffffffULL)
2568 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002569
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 /* sector count, 32-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002571 rbuf[0] = last_lba >> (8 * 3);
2572 rbuf[1] = last_lba >> (8 * 2);
2573 rbuf[2] = last_lba >> (8 * 1);
2574 rbuf[3] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002577 rbuf[4] = sector_size >> (8 * 3);
2578 rbuf[5] = sector_size >> (8 * 2);
2579 rbuf[6] = sector_size >> (8 * 1);
2580 rbuf[7] = sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 } else {
2582 /* sector count, 64-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002583 rbuf[0] = last_lba >> (8 * 7);
2584 rbuf[1] = last_lba >> (8 * 6);
2585 rbuf[2] = last_lba >> (8 * 5);
2586 rbuf[3] = last_lba >> (8 * 4);
2587 rbuf[4] = last_lba >> (8 * 3);
2588 rbuf[5] = last_lba >> (8 * 2);
2589 rbuf[6] = last_lba >> (8 * 1);
2590 rbuf[7] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002593 rbuf[ 8] = sector_size >> (8 * 3);
2594 rbuf[ 9] = sector_size >> (8 * 2);
2595 rbuf[10] = sector_size >> (8 * 1);
2596 rbuf[11] = sector_size;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002597
2598 rbuf[12] = 0;
Grant Grundler295124d2010-08-17 10:56:53 -07002599 rbuf[13] = log2_per_phys;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002600 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2601 rbuf[15] = lowest_aligned;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002602
Arne Fitzenreiter71d126f2015-07-15 13:54:36 +02002603 if (ata_id_has_trim(args->id) &&
2604 !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002605 rbuf[14] |= 0x80; /* LBPME */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002606
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002607 if (ata_id_has_zero_after_trim(args->id) &&
2608 dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
2609 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2610 rbuf[14] |= 0x40; /* LBPRZ */
2611 }
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return 0;
2615}
2616
2617/**
2618 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2619 * @args: device IDENTIFY data / SCSI command of interest.
2620 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 *
2622 * Simulate REPORT LUNS command.
2623 *
2624 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002625 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 */
Tejun Heo87340e92008-04-28 17:48:51 +09002627static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
2629 VPRINTK("ENTER\n");
2630 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2631
2632 return 0;
2633}
2634
Tejun Heo77853bf2006-01-23 13:09:36 +09002635static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002636{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002637 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002638 /* FIXME: not quite right; we don't want the
2639 * translation of taskfile registers into
2640 * a sense descriptors, since that's only
2641 * correct for ATA, not ATAPI
2642 */
Tejun Heo750426a2006-11-14 22:37:35 +09002643 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002644 }
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002645
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002646 ata_qc_done(qc);
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002647}
2648
2649/* is it pointless to prefer PIO for "safety reasons"? */
2650static inline int ata_pio_use_silly(struct ata_port *ap)
2651{
2652 return (ap->flags & ATA_FLAG_PIO_DMA);
2653}
2654
2655static void atapi_request_sense(struct ata_queued_cmd *qc)
2656{
2657 struct ata_port *ap = qc->ap;
2658 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002659
2660 DPRINTK("ATAPI request sense\n");
2661
James Bottomley7ccd7202008-01-17 11:56:24 -06002662 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Jeff Garzika939c962005-10-05 17:09:16 -04002663
Tejun Heo127102a2008-04-07 22:47:21 +09002664#ifdef CONFIG_ATA_SFF
James Bottomley855d8542008-04-18 13:18:48 -05002665 if (ap->ops->sff_tf_read)
2666 ap->ops->sff_tf_read(ap, &qc->tf);
Tejun Heo127102a2008-04-07 22:47:21 +09002667#endif
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002668
2669 /* fill these in, for the case where they are -not- overwritten */
2670 cmd->sense_buffer[0] = 0x70;
2671 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2672
2673 ata_qc_reinit(qc);
2674
Tejun Heo93f8fec2007-12-05 16:43:01 +09002675 /* setup sg table and init transfer direction */
akpm@linux-foundation.orgcadb7342008-01-15 16:01:52 -08002676 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
Tejun Heo93f8fec2007-12-05 16:43:01 +09002677 ata_sg_init(qc, &qc->sgent, 1);
Jeff Garzika939c962005-10-05 17:09:16 -04002678 qc->dma_dir = DMA_FROM_DEVICE;
2679
Tejun Heo6e7846e2006-02-12 23:32:58 +09002680 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002681 qc->cdb[0] = REQUEST_SENSE;
2682 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2683
2684 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2685 qc->tf.command = ATA_CMD_PACKET;
2686
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002687 if (ata_pio_use_silly(ap)) {
Tejun Heo0dc36882007-12-18 16:34:43 -05002688 qc->tf.protocol = ATAPI_PROT_DMA;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002689 qc->tf.feature |= ATAPI_PKT_DMA;
2690 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05002691 qc->tf.protocol = ATAPI_PROT_PIO;
Alan Cox2db78dd2007-10-02 13:53:04 -07002692 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2693 qc->tf.lbah = 0;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002694 }
Jeff Garzika939c962005-10-05 17:09:16 -04002695 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2696
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002697 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002698
Tejun Heo8e0e6942006-03-31 20:41:11 +09002699 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002700
2701 DPRINTK("EXIT\n");
2702}
2703
Tejun Heo77853bf2006-01-23 13:09:36 +09002704static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
2706 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002707 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Jeff Garzika7dac442005-10-30 04:44:42 -05002709 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002710
Tejun Heo246619d2006-05-15 20:58:16 +09002711 /* handle completion from new EH */
2712 if (unlikely(qc->ap->ops->error_handler &&
2713 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2714
2715 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2716 /* FIXME: not quite right; we don't want the
2717 * translation of taskfile registers into a
2718 * sense descriptors, since that's only
2719 * correct for ATA, not ATAPI
2720 */
Tejun Heo750426a2006-11-14 22:37:35 +09002721 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002722 }
2723
Tejun Heo22aac082006-08-08 14:08:59 +09002724 /* SCSI EH automatically locks door if sdev->locked is
2725 * set. Sometimes door lock request continues to
2726 * fail, for example, when no media is present. This
2727 * creates a loop - SCSI EH issues door lock which
2728 * fails and gets invoked again to acquire sense data
2729 * for the failed command.
2730 *
2731 * If door lock fails, always clear sdev->locked to
2732 * avoid this infinite loop.
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002733 *
2734 * This may happen before SCSI scan is complete. Make
2735 * sure qc->dev->sdev isn't NULL before dereferencing.
Tejun Heo22aac082006-08-08 14:08:59 +09002736 */
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002737 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
Tejun Heo22aac082006-08-08 14:08:59 +09002738 qc->dev->sdev->locked = 0;
2739
Tejun Heo246619d2006-05-15 20:58:16 +09002740 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002741 ata_qc_done(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002742 return;
2743 }
2744
2745 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002746 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002748 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002749 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002750 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002751 /* FIXME: not quite right; we don't want the
2752 * translation of taskfile registers into
2753 * a sense descriptors, since that's only
2754 * correct for ATA, not ATAPI
2755 */
Tejun Heo750426a2006-11-14 22:37:35 +09002756 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002757 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 u8 *scsicmd = cmd->cmnd;
2759
Tony Battersbyfd71da42005-12-21 16:35:44 -05002760 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Jeff Garzikb445c562008-02-29 19:10:51 -05002761 unsigned long flags;
Tejun Heo87340e92008-04-28 17:48:51 +09002762 u8 *buf;
Jeff Garzikb445c562008-02-29 19:10:51 -05002763
Tejun Heo87340e92008-04-28 17:48:51 +09002764 buf = ata_scsi_rbuf_get(cmd, true, &flags);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002765
2766 /* ATAPI devices typically report zero for their SCSI version,
2767 * and sometimes deviate from the spec WRT response data
2768 * format. If SCSI version is reported as zero like normal,
2769 * then we make the following fixups: 1) Fake MMC-5 version,
2770 * to indicate to the Linux scsi midlayer this is a modern
2771 * device. 2) Ensure response data format / ATAPI information
2772 * are always correct.
2773 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002774 if (buf[2] == 0) {
2775 buf[2] = 0x5;
2776 buf[3] = 0x32;
2777 }
2778
Tejun Heo87340e92008-04-28 17:48:51 +09002779 ata_scsi_rbuf_put(cmd, true, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 cmd->result = SAM_STAT_GOOD;
2783 }
2784
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002785 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786}
2787/**
2788 * atapi_xlat - Initialize PACKET taskfile
2789 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 *
2791 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002792 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 *
2794 * RETURNS:
2795 * Zero on success, non-zero on failure.
2796 */
Tejun Heoad706992006-12-17 10:45:57 +09002797static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Tejun Heo542b1442006-12-17 10:45:08 +09002799 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 struct ata_device *dev = qc->dev;
Tejun Heo542b1442006-12-17 10:45:08 +09002801 int nodata = (scmd->sc_data_direction == DMA_NONE);
Tejun Heo5895ef92008-06-17 12:36:26 +09002802 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
Alan Cox2db78dd2007-10-02 13:53:04 -07002803 unsigned int nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Tejun Heo2e5704f2006-12-17 10:46:33 +09002805 memset(qc->cdb, 0, dev->cdb_len);
2806 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 qc->complete_fn = atapi_qc_complete;
2809
2810 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002811 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 qc->tf.flags |= ATA_TFLAG_WRITE;
2813 DPRINTK("direction: write\n");
2814 }
2815
2816 qc->tf.command = ATA_CMD_PACKET;
Tejun Heoaacda372008-03-18 17:47:43 +09002817 ata_qc_set_pc_nbytes(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002819 /* check whether ATAPI DMA is safe */
Tejun Heo5895ef92008-06-17 12:36:26 +09002820 if (!nodata && !using_pio && atapi_check_dma(qc))
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002821 using_pio = 1;
2822
Tejun Heoe1902222007-11-26 20:58:02 +09002823 /* Some controller variants snoop this value for Packet
2824 * transfers to do state machine and FIFO management. Thus we
2825 * want to set it properly, and for DMA where it is
2826 * effectively meaningless.
2827 */
Tejun Heoaacda372008-03-18 17:47:43 +09002828 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
Alan Cox2db78dd2007-10-02 13:53:04 -07002829
Tejun Heoe1902222007-11-26 20:58:02 +09002830 /* Most ATAPI devices which honor transfer chunk size don't
2831 * behave according to the spec when odd chunk size which
2832 * matches the transfer length is specified. If the number of
2833 * bytes to transfer is 2n+1. According to the spec, what
2834 * should happen is to indicate that 2n+1 is going to be
2835 * transferred and transfer 2n+2 bytes where the last byte is
2836 * padding.
2837 *
2838 * In practice, this doesn't happen. ATAPI devices first
2839 * indicate and transfer 2n bytes and then indicate and
2840 * transfer 2 bytes where the last byte is padding.
2841 *
2842 * This inconsistency confuses several controllers which
2843 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2844 * These controllers use actual number of transferred bytes to
2845 * update DMA poitner and transfer of 4n+2 bytes make those
2846 * controller push DMA pointer by 4n+4 bytes because SATA data
2847 * FISes are aligned to 4 bytes. This causes data corruption
2848 * and buffer overrun.
2849 *
2850 * Always setting nbytes to even number solves this problem
2851 * because then ATAPI devices don't have to split data at 2n
2852 * boundaries.
2853 */
2854 if (nbytes & 0x1)
2855 nbytes++;
2856
Alan Cox2db78dd2007-10-02 13:53:04 -07002857 qc->tf.lbam = (nbytes & 0xFF);
2858 qc->tf.lbah = (nbytes >> 8);
2859
Tejun Heo5895ef92008-06-17 12:36:26 +09002860 if (nodata)
2861 qc->tf.protocol = ATAPI_PROT_NODATA;
2862 else if (using_pio)
2863 qc->tf.protocol = ATAPI_PROT_PIO;
2864 else {
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002865 /* DMA data xfer */
Tejun Heo0dc36882007-12-18 16:34:43 -05002866 qc->tf.protocol = ATAPI_PROT_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 qc->tf.feature |= ATAPI_PKT_DMA;
2868
Tejun Heo91163002008-02-21 13:25:50 +09002869 if ((dev->flags & ATA_DFLAG_DMADIR) &&
2870 (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08002871 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
2874
Alan Cox2db78dd2007-10-02 13:53:04 -07002875
2876 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2877 as ATAPI tape drives don't get this right otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return 0;
2879}
2880
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002881static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
Tejun Heoab5b3a52006-05-31 18:27:34 +09002882{
Tejun Heo071f44b2008-04-07 22:47:22 +09002883 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09002884 if (likely(devno < ata_link_max_devices(&ap->link)))
2885 return &ap->link.device[devno];
2886 } else {
2887 if (likely(devno < ap->nr_pmp_links))
2888 return &ap->pmp_link[devno].device[0];
2889 }
2890
Tejun Heoab5b3a52006-05-31 18:27:34 +09002891 return NULL;
2892}
2893
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002894static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
2895 const struct scsi_device *scsidev)
Tejun Heoab5b3a52006-05-31 18:27:34 +09002896{
Tejun Heo41bda9c2007-08-06 18:36:24 +09002897 int devno;
Tejun Heoab5b3a52006-05-31 18:27:34 +09002898
Tejun Heo41bda9c2007-08-06 18:36:24 +09002899 /* skip commands not addressed to targets we simulate */
Tejun Heo071f44b2008-04-07 22:47:22 +09002900 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09002901 if (unlikely(scsidev->channel || scsidev->lun))
2902 return NULL;
2903 devno = scsidev->id;
2904 } else {
2905 if (unlikely(scsidev->id || scsidev->lun))
2906 return NULL;
2907 devno = scsidev->channel;
2908 }
2909
2910 return ata_find_dev(ap, devno);
Tejun Heoab5b3a52006-05-31 18:27:34 +09002911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913/**
2914 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2915 * @ap: ATA port to which the device is attached
2916 * @scsidev: SCSI device from which we derive the ATA device
2917 *
2918 * Given various information provided in struct scsi_cmnd,
2919 * map that onto an ATA bus, and using that mapping
2920 * determine which ata_device is associated with the
2921 * SCSI command to be sent.
2922 *
2923 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002924 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 *
2926 * RETURNS:
2927 * Associated ATA device, or %NULL if not found.
2928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04002930ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Tejun Heoab5b3a52006-05-31 18:27:34 +09002932 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Tejun Heo2486fa52008-07-31 07:52:40 +09002934 if (unlikely(!dev || !ata_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return NULL;
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return dev;
2938}
2939
Jeff Garzikb0955182005-05-12 15:45:22 -04002940/*
2941 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2942 * @byte1: Byte 1 from pass-thru CDB.
2943 *
2944 * RETURNS:
2945 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2946 */
2947static u8
2948ata_scsi_map_proto(u8 byte1)
2949{
2950 switch((byte1 & 0x1e) >> 1) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002951 case 3: /* Non-data */
2952 return ATA_PROT_NODATA;
Jeff Garzikb0955182005-05-12 15:45:22 -04002953
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002954 case 6: /* DMA */
2955 case 10: /* UDMA Data-in */
2956 case 11: /* UDMA Data-Out */
2957 return ATA_PROT_DMA;
Jeff Garzikb0955182005-05-12 15:45:22 -04002958
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002959 case 4: /* PIO Data-in */
2960 case 5: /* PIO Data-out */
2961 return ATA_PROT_PIO;
Jeff Garzikb0955182005-05-12 15:45:22 -04002962
Vinayak Kaleee7fb332015-10-27 11:19:15 +05302963 case 12: /* FPDMA */
2964 return ATA_PROT_NCQ;
2965
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002966 case 0: /* Hard Reset */
2967 case 1: /* SRST */
2968 case 8: /* Device Diagnostic */
2969 case 9: /* Device Reset */
2970 case 7: /* DMA Queued */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002971 case 15: /* Return Response Info */
2972 default: /* Reserved */
2973 break;
Jeff Garzikb0955182005-05-12 15:45:22 -04002974 }
2975
2976 return ATA_PROT_UNKNOWN;
2977}
2978
2979/**
2980 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2981 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04002982 *
2983 * Handles either 12 or 16-byte versions of the CDB.
2984 *
2985 * RETURNS:
2986 * Zero on success, non-zero on failure.
2987 */
Tejun Heoad706992006-12-17 10:45:57 +09002988static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04002989{
2990 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09002991 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01002992 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09002993 const u8 *cdb = scmd->cmnd;
Jeff Garzikb0955182005-05-12 15:45:22 -04002994
Tejun Heo542b1442006-12-17 10:45:08 +09002995 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
Tejun Heo9a405252005-12-02 11:49:11 +09002996 goto invalid_fld;
Jeff Garzik8190bdb2006-05-24 01:53:39 -04002997
Vinayak Kale5f8e7f12015-10-27 13:11:46 +05302998 /* enable LBA */
2999 tf->flags |= ATA_TFLAG_LBA;
3000
Alan Coxae8d4ee2007-11-04 22:05:49 -05003001 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04003002 * 12 and 16 byte CDBs use different offsets to
3003 * provide the various register values.
3004 */
Tejun Heo542b1442006-12-17 10:45:08 +09003005 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04003006 /*
3007 * 16-byte CDB - may contain extended commands.
3008 *
3009 * If that is the case, copy the upper byte register values.
3010 */
Tejun Heo542b1442006-12-17 10:45:08 +09003011 if (cdb[1] & 0x01) {
3012 tf->hob_feature = cdb[3];
3013 tf->hob_nsect = cdb[5];
3014 tf->hob_lbal = cdb[7];
3015 tf->hob_lbam = cdb[9];
3016 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04003017 tf->flags |= ATA_TFLAG_LBA48;
3018 } else
3019 tf->flags &= ~ATA_TFLAG_LBA48;
3020
3021 /*
3022 * Always copy low byte, device and command registers.
3023 */
Tejun Heo542b1442006-12-17 10:45:08 +09003024 tf->feature = cdb[4];
3025 tf->nsect = cdb[6];
3026 tf->lbal = cdb[8];
3027 tf->lbam = cdb[10];
3028 tf->lbah = cdb[12];
3029 tf->device = cdb[13];
3030 tf->command = cdb[14];
Jeff Garzikb0955182005-05-12 15:45:22 -04003031 } else {
3032 /*
3033 * 12-byte CDB - incapable of extended commands.
3034 */
3035 tf->flags &= ~ATA_TFLAG_LBA48;
3036
Tejun Heo542b1442006-12-17 10:45:08 +09003037 tf->feature = cdb[3];
3038 tf->nsect = cdb[4];
3039 tf->lbal = cdb[5];
3040 tf->lbam = cdb[6];
3041 tf->lbah = cdb[7];
3042 tf->device = cdb[8];
3043 tf->command = cdb[9];
Jeff Garzikb0955182005-05-12 15:45:22 -04003044 }
Albert Leefa4453c2007-06-07 15:52:07 +08003045
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303046 /* For NCQ commands with FPDMA protocol, copy the tag value */
3047 if (tf->protocol == ATA_PROT_NCQ)
3048 tf->nsect = qc->tag << 3;
3049
Albert Leefa4453c2007-06-07 15:52:07 +08003050 /* enforce correct master/slave bit */
3051 tf->device = dev->devno ?
3052 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003053
Tejun Heobd30add2009-09-03 16:08:11 +09003054 switch (tf->command) {
Grant Grundler295124d2010-08-17 10:56:53 -07003055 /* READ/WRITE LONG use a non-standard sect_size */
Tejun Heobd30add2009-09-03 16:08:11 +09003056 case ATA_CMD_READ_LONG:
3057 case ATA_CMD_READ_LONG_ONCE:
3058 case ATA_CMD_WRITE_LONG:
3059 case ATA_CMD_WRITE_LONG_ONCE:
3060 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
3061 goto invalid_fld;
3062 qc->sect_size = scsi_bufflen(scmd);
Grant Grundler295124d2010-08-17 10:56:53 -07003063 break;
3064
3065 /* commands using reported Logical Block size (e.g. 512 or 4K) */
3066 case ATA_CMD_CFA_WRITE_NE:
3067 case ATA_CMD_CFA_TRANS_SECT:
3068 case ATA_CMD_CFA_WRITE_MULT_NE:
3069 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
3070 case ATA_CMD_READ:
3071 case ATA_CMD_READ_EXT:
3072 case ATA_CMD_READ_QUEUED:
3073 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
3074 case ATA_CMD_FPDMA_READ:
3075 case ATA_CMD_READ_MULTI:
3076 case ATA_CMD_READ_MULTI_EXT:
3077 case ATA_CMD_PIO_READ:
3078 case ATA_CMD_PIO_READ_EXT:
3079 case ATA_CMD_READ_STREAM_DMA_EXT:
3080 case ATA_CMD_READ_STREAM_EXT:
3081 case ATA_CMD_VERIFY:
3082 case ATA_CMD_VERIFY_EXT:
3083 case ATA_CMD_WRITE:
3084 case ATA_CMD_WRITE_EXT:
3085 case ATA_CMD_WRITE_FUA_EXT:
3086 case ATA_CMD_WRITE_QUEUED:
3087 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3088 case ATA_CMD_FPDMA_WRITE:
3089 case ATA_CMD_WRITE_MULTI:
3090 case ATA_CMD_WRITE_MULTI_EXT:
3091 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3092 case ATA_CMD_PIO_WRITE:
3093 case ATA_CMD_PIO_WRITE_EXT:
3094 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3095 case ATA_CMD_WRITE_STREAM_EXT:
3096 qc->sect_size = scmd->device->sector_size;
3097 break;
3098
3099 /* Everything else uses 512 byte "sectors" */
3100 default:
3101 qc->sect_size = ATA_SECT_SIZE;
Tejun Heobd30add2009-09-03 16:08:11 +09003102 }
3103
3104 /*
3105 * Set flags so that all registers will be written, pass on
3106 * write indication (used for PIO/DMA setup), result TF is
3107 * copied back and we don't whine too much about its failure.
3108 */
Douglas Gilbertbc496ed2010-02-01 13:11:38 -05003109 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heobd30add2009-09-03 16:08:11 +09003110 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3111 tf->flags |= ATA_TFLAG_WRITE;
3112
3113 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3114
3115 /*
3116 * Set transfer length.
3117 *
3118 * TODO: find out if we need to do more here to
3119 * cover scatter/gather case.
3120 */
3121 ata_qc_set_pc_nbytes(qc);
3122
3123 /* We may not issue DMA commands if no DMA mode is set */
3124 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
3125 goto invalid_fld;
3126
Albert Lee1dce5892007-06-07 15:49:22 +08003127 /* sanity check for pio multi commands */
3128 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
3129 goto invalid_fld;
3130
3131 if (is_multi_taskfile(tf)) {
3132 unsigned int multi_count = 1 << (cdb[1] >> 5);
3133
3134 /* compare the passed through multi_count
3135 * with the cached multi_count of libata
3136 */
3137 if (multi_count != dev->multi_count)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003138 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3139 multi_count);
Alan Coxd26fc952007-07-06 19:13:52 -04003140 }
Albert Lee1dce5892007-06-07 15:49:22 +08003141
Jeff Garzikb0955182005-05-12 15:45:22 -04003142 /*
3143 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3144 * SET_FEATURES - XFER MODE must be preceded/succeeded
3145 * by an update to hardware-specific registers for each
3146 * controller (i.e. the reason for ->set_piomode(),
3147 * ->set_dmamode(), and ->post_set_mode() hooks).
3148 */
Tejun Heobd30add2009-09-03 16:08:11 +09003149 if (tf->command == ATA_CMD_SET_FEATURES &&
3150 tf->feature == SETFEATURES_XFER)
Tejun Heo9a405252005-12-02 11:49:11 +09003151 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04003152
3153 /*
Tejun Heobd30add2009-09-03 16:08:11 +09003154 * Filter TPM commands by default. These provide an
3155 * essentially uncontrolled encrypted "back door" between
3156 * applications and the disk. Set libata.allow_tpm=1 if you
3157 * have a real reason for wanting to use them. This ensures
3158 * that installed software cannot easily mess stuff up without
3159 * user intent. DVR type users will probably ship with this enabled
3160 * for movie content management.
Jeff Garzikb0955182005-05-12 15:45:22 -04003161 *
Tejun Heobd30add2009-09-03 16:08:11 +09003162 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3163 * for this and should do in future but that it is not sufficient as
3164 * DCS is an optional feature set. Thus we also do the software filter
3165 * so that we comply with the TC consortium stated goal that the user
3166 * can turn off TC features of their system.
Jeff Garzikb0955182005-05-12 15:45:22 -04003167 */
Tejun Heobd30add2009-09-03 16:08:11 +09003168 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
3169 goto invalid_fld;
Tejun Heoe61e0672006-05-15 20:57:40 +09003170
Jeff Garzikb0955182005-05-12 15:45:22 -04003171 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09003172
3173 invalid_fld:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003174 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x24, 0x00);
Tejun Heo9a405252005-12-02 11:49:11 +09003175 /* "Invalid field in cdb" */
3176 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003177}
3178
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003179static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3180{
3181 struct ata_taskfile *tf = &qc->tf;
3182 struct scsi_cmnd *scmd = qc->scsicmd;
3183 struct ata_device *dev = qc->dev;
3184 const u8 *cdb = scmd->cmnd;
3185 u64 block;
3186 u32 n_block;
3187 u32 size;
3188 void *buf;
3189
3190 /* we may not issue DMA commands if no DMA mode is set */
3191 if (unlikely(!dev->dma_mode))
3192 goto invalid_fld;
3193
3194 if (unlikely(scmd->cmd_len < 16))
3195 goto invalid_fld;
3196 scsi_16_lba_len(cdb, &block, &n_block);
3197
3198 /* for now we only support WRITE SAME with the unmap bit set */
3199 if (unlikely(!(cdb[1] & 0x8)))
3200 goto invalid_fld;
3201
3202 /*
3203 * WRITE SAME always has a sector sized buffer as payload, this
3204 * should never be a multiple entry S/G list.
3205 */
3206 if (!scsi_sg_count(scmd))
3207 goto invalid_fld;
3208
3209 buf = page_address(sg_page(scsi_sglist(scmd)));
Martin K. Petersend0634c42009-11-26 12:00:43 -05003210 size = ata_set_lba_range_entries(buf, 512, block, n_block);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003211
Marc Carino87fb6c32013-08-24 23:22:51 -07003212 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3213 /* Newer devices support queued TRIM commands */
3214 tf->protocol = ATA_PROT_NCQ;
3215 tf->command = ATA_CMD_FPDMA_SEND;
3216 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
3217 tf->nsect = qc->tag << 3;
3218 tf->hob_feature = (size / 512) >> 8;
3219 tf->feature = size / 512;
3220
3221 tf->auxiliary = 1;
3222 } else {
3223 tf->protocol = ATA_PROT_DMA;
3224 tf->hob_feature = 0;
3225 tf->feature = ATA_DSM_TRIM;
3226 tf->hob_nsect = (size / 512) >> 8;
3227 tf->nsect = size / 512;
3228 tf->command = ATA_CMD_DSM;
3229 }
3230
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003231 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3232 ATA_TFLAG_WRITE;
3233
3234 ata_qc_set_pc_nbytes(qc);
3235
3236 return 0;
3237
3238 invalid_fld:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003239 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x24, 0x00);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003240 /* "Invalid field in cdb" */
3241 return 1;
3242}
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003245 * ata_mselect_caching - Simulate MODE SELECT for caching info page
3246 * @qc: Storage for translated ATA taskfile
3247 * @buf: input buffer
3248 * @len: number of valid bytes in the input buffer
3249 *
3250 * Prepare a taskfile to modify caching information for the device.
3251 *
3252 * LOCKING:
3253 * None.
3254 */
3255static int ata_mselect_caching(struct ata_queued_cmd *qc,
3256 const u8 *buf, int len)
3257{
3258 struct ata_taskfile *tf = &qc->tf;
3259 struct ata_device *dev = qc->dev;
3260 char mpage[CACHE_MPAGE_LEN];
3261 u8 wce;
3262
3263 /*
3264 * The first two bytes of def_cache_mpage are a header, so offsets
3265 * in mpage are off by 2 compared to buf. Same for len.
3266 */
3267
3268 if (len != CACHE_MPAGE_LEN - 2)
3269 return -EINVAL;
3270
3271 wce = buf[0] & (1 << 2);
3272
3273 /*
3274 * Check that read-only bits are not modified.
3275 */
3276 ata_msense_caching(dev->id, mpage, false);
3277 mpage[2] &= ~(1 << 2);
3278 mpage[2] |= wce;
3279 if (memcmp(mpage + 2, buf, CACHE_MPAGE_LEN - 2) != 0)
3280 return -EINVAL;
3281
3282 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3283 tf->protocol = ATA_PROT_NODATA;
3284 tf->nsect = 0;
3285 tf->command = ATA_CMD_SET_FEATURES;
3286 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3287 return 0;
3288}
3289
3290/**
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003291 * ata_mselect_control - Simulate MODE SELECT for control page
3292 * @qc: Storage for translated ATA taskfile
3293 * @buf: input buffer
3294 * @len: number of valid bytes in the input buffer
3295 *
3296 * Prepare a taskfile to modify caching information for the device.
3297 *
3298 * LOCKING:
3299 * None.
3300 */
3301static int ata_mselect_control(struct ata_queued_cmd *qc,
3302 const u8 *buf, int len)
3303{
3304 struct ata_device *dev = qc->dev;
3305 char mpage[CONTROL_MPAGE_LEN];
3306 u8 d_sense;
3307
3308 /*
3309 * The first two bytes of def_control_mpage are a header, so offsets
3310 * in mpage are off by 2 compared to buf. Same for len.
3311 */
3312
3313 if (len != CONTROL_MPAGE_LEN - 2)
3314 return -EINVAL;
3315
3316 d_sense = buf[0] & (1 << 2);
3317
3318 /*
3319 * Check that read-only bits are not modified.
3320 */
3321 ata_msense_ctl_mode(dev, mpage, false);
3322 mpage[2] &= ~(1 << 2);
3323 mpage[2] |= d_sense;
3324 if (memcmp(mpage + 2, buf, CONTROL_MPAGE_LEN - 2) != 0)
3325 return -EINVAL;
3326 if (d_sense & (1 << 2))
3327 dev->flags |= ATA_DFLAG_D_SENSE;
3328 else
3329 dev->flags &= ~ATA_DFLAG_D_SENSE;
3330 qc->scsicmd->result = SAM_STAT_GOOD;
3331 qc->scsicmd->scsi_done(qc->scsicmd);
3332 return 0;
3333}
3334
3335/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003336 * ata_scsiop_mode_select - Simulate MODE SELECT 6, 10 commands
3337 * @qc: Storage for translated ATA taskfile
3338 *
3339 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
3340 * Assume this is invoked for direct access devices (e.g. disks) only.
3341 * There should be no block descriptor for other device types.
3342 *
3343 * LOCKING:
3344 * spin_lock_irqsave(host lock)
3345 */
3346static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
3347{
3348 struct scsi_cmnd *scmd = qc->scsicmd;
3349 const u8 *cdb = scmd->cmnd;
3350 const u8 *p;
3351 u8 pg, spg;
3352 unsigned six_byte, pg_len, hdr_len, bd_len;
3353 int len;
3354
3355 VPRINTK("ENTER\n");
3356
3357 six_byte = (cdb[0] == MODE_SELECT);
3358 if (six_byte) {
3359 if (scmd->cmd_len < 5)
3360 goto invalid_fld;
3361
3362 len = cdb[4];
3363 hdr_len = 4;
3364 } else {
3365 if (scmd->cmd_len < 9)
3366 goto invalid_fld;
3367
3368 len = (cdb[7] << 8) + cdb[8];
3369 hdr_len = 8;
3370 }
3371
3372 /* We only support PF=1, SP=0. */
3373 if ((cdb[1] & 0x11) != 0x10)
3374 goto invalid_fld;
3375
3376 /* Test early for possible overrun. */
3377 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
3378 goto invalid_param_len;
3379
3380 p = page_address(sg_page(scsi_sglist(scmd)));
3381
3382 /* Move past header and block descriptors. */
3383 if (len < hdr_len)
3384 goto invalid_param_len;
3385
3386 if (six_byte)
3387 bd_len = p[3];
3388 else
3389 bd_len = (p[6] << 8) + p[7];
3390
3391 len -= hdr_len;
3392 p += hdr_len;
3393 if (len < bd_len)
3394 goto invalid_param_len;
3395 if (bd_len != 0 && bd_len != 8)
3396 goto invalid_param;
3397
3398 len -= bd_len;
3399 p += bd_len;
3400 if (len == 0)
3401 goto skip;
3402
3403 /* Parse both possible formats for the mode page headers. */
3404 pg = p[0] & 0x3f;
3405 if (p[0] & 0x40) {
3406 if (len < 4)
3407 goto invalid_param_len;
3408
3409 spg = p[1];
3410 pg_len = (p[2] << 8) | p[3];
3411 p += 4;
3412 len -= 4;
3413 } else {
3414 if (len < 2)
3415 goto invalid_param_len;
3416
3417 spg = 0;
3418 pg_len = p[1];
3419 p += 2;
3420 len -= 2;
3421 }
3422
3423 /*
3424 * No mode subpages supported (yet) but asking for _all_
3425 * subpages may be valid
3426 */
3427 if (spg && (spg != ALL_SUB_MPAGES))
3428 goto invalid_param;
3429 if (pg_len > len)
3430 goto invalid_param_len;
3431
3432 switch (pg) {
3433 case CACHE_MPAGE:
3434 if (ata_mselect_caching(qc, p, pg_len) < 0)
3435 goto invalid_param;
3436 break;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003437 case CONTROL_MPAGE:
3438 if (ata_mselect_control(qc, p, pg_len) < 0)
3439 goto invalid_param;
3440 break;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003441 default: /* invalid page code */
3442 goto invalid_param;
3443 }
3444
3445 /*
3446 * Only one page has changeable data, so we only support setting one
3447 * page at a time.
3448 */
3449 if (len > pg_len)
3450 goto invalid_param;
3451
3452 return 0;
3453
3454 invalid_fld:
3455 /* "Invalid field in CDB" */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003456 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003457 return 1;
3458
3459 invalid_param:
3460 /* "Invalid field in parameter list" */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003461 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x26, 0x0);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003462 return 1;
3463
3464 invalid_param_len:
3465 /* "Parameter list length error" */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003466 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003467 return 1;
3468
3469 skip:
3470 scmd->result = SAM_STAT_GOOD;
3471 return 1;
3472}
3473
3474/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 * ata_get_xlat_func - check if SCSI to ATA translation is possible
3476 * @dev: ATA device
3477 * @cmd: SCSI command opcode to consider
3478 *
3479 * Look up the SCSI command given, and determine whether the
3480 * SCSI command is to be translated or simulated.
3481 *
3482 * RETURNS:
3483 * Pointer to translation function if possible, %NULL if not.
3484 */
3485
3486static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
3487{
3488 switch (cmd) {
3489 case READ_6:
3490 case READ_10:
3491 case READ_16:
3492
3493 case WRITE_6:
3494 case WRITE_10:
3495 case WRITE_16:
3496 return ata_scsi_rw_xlat;
3497
Christoph Hellwig0cdd6eb2009-12-10 10:36:01 +01003498 case WRITE_SAME_16:
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003499 return ata_scsi_write_same_xlat;
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 case SYNCHRONIZE_CACHE:
3502 if (ata_try_flush_cache(dev))
3503 return ata_scsi_flush_xlat;
3504 break;
3505
3506 case VERIFY:
3507 case VERIFY_16:
3508 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04003509
3510 case ATA_12:
3511 case ATA_16:
3512 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04003513
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003514 case MODE_SELECT:
3515 case MODE_SELECT_10:
3516 return ata_scsi_mode_select_xlat;
3517 break;
3518
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04003519 case START_STOP:
3520 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 }
3522
3523 return NULL;
3524}
3525
3526/**
3527 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
3528 * @ap: ATA port to which the command was being sent
3529 * @cmd: SCSI command to dump
3530 *
3531 * Prints the contents of a SCSI command via printk().
3532 */
3533
3534static inline void ata_scsi_dump_cdb(struct ata_port *ap,
3535 struct scsi_cmnd *cmd)
3536{
3537#ifdef ATA_DEBUG
3538 struct scsi_device *scsidev = cmd->device;
3539 u8 *scsicmd = cmd->cmnd;
3540
3541 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09003542 ap->print_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 scsidev->channel, scsidev->id, scsidev->lun,
3544 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
3545 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
3546 scsicmd[8]);
3547#endif
3548}
3549
Tejun Heo542b1442006-12-17 10:45:08 +09003550static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09003551 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06003552{
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003553 u8 scsi_op = scmd->cmnd[0];
3554 ata_xlat_func_t xlat_func;
Tejun Heo2115ea92006-05-15 21:03:39 +09003555 int rc = 0;
3556
Hannes Reinecke9162c652014-11-05 13:08:21 +01003557 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003558 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
3559 goto bad_cdb_len;
3560
3561 xlat_func = ata_get_xlat_func(dev, scsi_op);
3562 } else {
3563 if (unlikely(!scmd->cmd_len))
3564 goto bad_cdb_len;
3565
3566 xlat_func = NULL;
3567 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
3568 /* relay SCSI command to ATAPI device */
Mark Lord607126c2007-11-15 13:13:59 +09003569 int len = COMMAND_SIZE(scsi_op);
3570 if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003571 goto bad_cdb_len;
3572
3573 xlat_func = atapi_xlat;
3574 } else {
3575 /* ATA_16 passthru, treat as an ATA command */
3576 if (unlikely(scmd->cmd_len > 16))
3577 goto bad_cdb_len;
3578
3579 xlat_func = ata_get_xlat_func(dev, scsi_op);
3580 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09003581 }
3582
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003583 if (xlat_func)
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003584 rc = ata_scsi_translate(dev, scmd, xlat_func);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003585 else
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003586 ata_scsi_simulate(dev, scmd);
Tejun Heo2115ea92006-05-15 21:03:39 +09003587
3588 return rc;
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003589
3590 bad_cdb_len:
3591 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
3592 scmd->cmd_len, scsi_op, dev->cdb_len);
3593 scmd->result = DID_ERROR << 16;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003594 scmd->scsi_done(scmd);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003595 return 0;
Brian Kingeb3f0f92006-03-23 17:30:02 -06003596}
3597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598/**
3599 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
Jeff Garzik23e701e2010-11-17 12:03:58 -05003600 * @shost: SCSI host of command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 * @cmd: SCSI command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 *
3603 * In some cases, this function translates SCSI commands into
3604 * ATA taskfiles, and queues the taskfiles to be sent to
3605 * hardware. In other cases, this function simulates a
3606 * SCSI device by evaluating and responding to certain
3607 * SCSI commands. This creates the overall effect of
3608 * ATA and ATAPI devices appearing as SCSI devices.
3609 *
3610 * LOCKING:
Jeff Garzik23e701e2010-11-17 12:03:58 -05003611 * ATA host lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 *
3613 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09003614 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3615 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 */
Jeff Garzik23e701e2010-11-17 12:03:58 -05003617int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
3619 struct ata_port *ap;
3620 struct ata_device *dev;
3621 struct scsi_device *scsidev = cmd->device;
Tejun Heo2115ea92006-05-15 21:03:39 +09003622 int rc = 0;
Jeff Garzik23e701e2010-11-17 12:03:58 -05003623 unsigned long irq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Jeff Garzik35bb94b2006-04-11 13:12:34 -04003625 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05003626
Jeff Garzik23e701e2010-11-17 12:03:58 -05003627 spin_lock_irqsave(ap->lock, irq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
3629 ata_scsi_dump_cdb(ap, cmd);
3630
3631 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06003632 if (likely(dev))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003633 rc = __ata_scsi_queuecmd(cmd, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06003634 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzik23e701e2010-11-17 12:03:58 -05003636 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 }
3638
Jeff Garzik23e701e2010-11-17 12:03:58 -05003639 spin_unlock_irqrestore(ap->lock, irq_flags);
3640
Tejun Heo2115ea92006-05-15 21:03:39 +09003641 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642}
3643
3644/**
3645 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003646 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 * @cmd: SCSI command being sent to device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 *
3649 * Interprets and directly executes a select list of SCSI commands
3650 * that can be handled internally.
3651 *
3652 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003653 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 */
3655
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003656void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
3658 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04003659 const u8 *scsicmd = cmd->cmnd;
Jeff Garzik45394142007-09-21 06:23:42 -04003660 u8 tmp8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Tejun Heo9a3dccc2006-01-06 09:56:18 +01003662 args.dev = dev;
3663 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 args.cmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003665 args.done = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
3667 switch(scsicmd[0]) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003668 /* TODO: worth improving? */
3669 case FORMAT_UNIT:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003670 ata_scsi_invalid_field(dev, cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003671 break;
3672
3673 case INQUIRY:
3674 if (scsicmd[1] & 2) /* is CmdDt set? */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003675 ata_scsi_invalid_field(dev, cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003676 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
3677 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
3678 else switch (scsicmd[2]) {
3679 case 0x00:
3680 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003682 case 0x80:
3683 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003685 case 0x83:
3686 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003688 case 0x89:
3689 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 break;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003691 case 0xb0:
3692 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
3693 break;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06003694 case 0xb1:
3695 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
3696 break;
Martin K. Petersen02e0a602010-09-10 01:23:18 -04003697 case 0xb2:
3698 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
3699 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 default:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003701 ata_scsi_invalid_field(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003703 }
3704 break;
3705
3706 case MODE_SENSE:
3707 case MODE_SENSE_10:
3708 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
3709 break;
3710
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003711 case READ_CAPACITY:
3712 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3713 break;
3714
Hannes Reineckeeb846d92014-11-17 14:25:19 +01003715 case SERVICE_ACTION_IN_16:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003716 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
3717 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3718 else
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003719 ata_scsi_invalid_field(dev, cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003720 break;
3721
3722 case REPORT_LUNS:
3723 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
3724 break;
3725
3726 case REQUEST_SENSE:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003727 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003728 cmd->result = (DRIVER_SENSE << 24);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003729 cmd->scsi_done(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003730 break;
3731
3732 /* if we reach this, then writeback caching is disabled,
3733 * turning this into a no-op.
3734 */
3735 case SYNCHRONIZE_CACHE:
3736 /* fall through */
3737
3738 /* no-op's, complete with success */
3739 case REZERO_UNIT:
3740 case SEEK_6:
3741 case SEEK_10:
3742 case TEST_UNIT_READY:
3743 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3744 break;
3745
3746 case SEND_DIAGNOSTIC:
3747 tmp8 = scsicmd[1] & ~(1 << 3);
3748 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
3749 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3750 else
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003751 ata_scsi_invalid_field(dev, cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003752 break;
3753
3754 /* all other commands */
3755 default:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003756 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003757 /* "Invalid command operation code" */
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003758 cmd->scsi_done(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 }
3761}
3762
Tejun Heof3187192007-04-17 23:44:07 +09003763int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
3764{
3765 int i, rc;
3766
3767 for (i = 0; i < host->n_ports; i++) {
3768 struct ata_port *ap = host->ports[i];
3769 struct Scsi_Host *shost;
3770
3771 rc = -ENOMEM;
3772 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
3773 if (!shost)
3774 goto err_alloc;
3775
Lin Ming9ee4f392011-12-05 09:20:28 +08003776 shost->eh_noresume = 1;
Tejun Heof3187192007-04-17 23:44:07 +09003777 *(struct ata_port **)&shost->hostdata[0] = ap;
3778 ap->scsi_host = shost;
3779
Gwendal Grignoud9027472010-05-25 12:31:38 -07003780 shost->transportt = ata_scsi_transport_template;
Tejun Heof3187192007-04-17 23:44:07 +09003781 shost->unique_id = ap->print_id;
3782 shost->max_id = 16;
3783 shost->max_lun = 1;
3784 shost->max_channel = 1;
3785 shost->max_cmd_len = 16;
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003786 shost->no_write_same = 1;
Tejun Heof3187192007-04-17 23:44:07 +09003787
Tejun Heo31cc23b2007-09-23 13:14:12 +09003788 /* Schedule policy is determined by ->qc_defer()
3789 * callback and it needs to see every deferred qc.
3790 * Set host_blocked to 1 to prevent SCSI midlayer from
3791 * automatically deferring requests.
3792 */
3793 shost->max_host_blocked = 1;
3794
Lin Mingf8fc75d2012-04-12 13:50:39 +08003795 rc = scsi_add_host_with_dma(ap->scsi_host,
3796 &ap->tdev, ap->host->dev);
Tejun Heof3187192007-04-17 23:44:07 +09003797 if (rc)
3798 goto err_add;
3799 }
3800
3801 return 0;
3802
3803 err_add:
3804 scsi_host_put(host->ports[i]->scsi_host);
3805 err_alloc:
3806 while (--i >= 0) {
3807 struct Scsi_Host *shost = host->ports[i]->scsi_host;
3808
3809 scsi_remove_host(shost);
3810 scsi_host_put(shost);
3811 }
3812 return rc;
3813}
3814
Tejun Heo1ae46312007-07-16 14:29:40 +09003815void ata_scsi_scan_host(struct ata_port *ap, int sync)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003816{
Tejun Heo1ae46312007-07-16 14:29:40 +09003817 int tries = 5;
3818 struct ata_device *last_failed_dev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003819 struct ata_link *link;
Tejun Heo1ae46312007-07-16 14:29:40 +09003820 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003821
Tejun Heo1ae46312007-07-16 14:29:40 +09003822 repeat:
Tejun Heo1eca4362008-11-03 20:03:17 +09003823 ata_for_each_link(link, ap, EDGE) {
3824 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003825 struct scsi_device *sdev;
3826 int channel = 0, id = 0;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04003827
Tejun Heo1eca4362008-11-03 20:03:17 +09003828 if (dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003829 continue;
Tejun Heo3edebac2006-05-31 18:27:40 +09003830
Tejun Heo41bda9c2007-08-06 18:36:24 +09003831 if (ata_is_host_link(link))
3832 id = dev->devno;
3833 else
3834 channel = link->pmp;
3835
3836 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
3837 NULL);
3838 if (!IS_ERR(sdev)) {
3839 dev->sdev = sdev;
3840 scsi_device_put(sdev);
Grant Grundler295124d2010-08-17 10:56:53 -07003841 } else {
3842 dev->sdev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003843 }
Tejun Heo3edebac2006-05-31 18:27:40 +09003844 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04003845 }
Tejun Heo1ae46312007-07-16 14:29:40 +09003846
3847 /* If we scanned while EH was in progress or allocation
3848 * failure occurred, scan would have failed silently. Check
3849 * whether all devices are attached.
3850 */
Tejun Heo1eca4362008-11-03 20:03:17 +09003851 ata_for_each_link(link, ap, EDGE) {
3852 ata_for_each_dev(dev, link, ENABLED) {
3853 if (!dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003854 goto exit_loop;
3855 }
Tejun Heo1ae46312007-07-16 14:29:40 +09003856 }
Tejun Heo41bda9c2007-08-06 18:36:24 +09003857 exit_loop:
3858 if (!link)
Tejun Heo1ae46312007-07-16 14:29:40 +09003859 return;
3860
3861 /* we're missing some SCSI devices */
3862 if (sync) {
3863 /* If caller requested synchrnous scan && we've made
3864 * any progress, sleep briefly and repeat.
3865 */
3866 if (dev != last_failed_dev) {
3867 msleep(100);
3868 last_failed_dev = dev;
3869 goto repeat;
3870 }
3871
3872 /* We might be failing to detect boot device, give it
3873 * a few more chances.
3874 */
3875 if (--tries) {
3876 msleep(100);
3877 goto repeat;
3878 }
3879
Joe Perchesa9a79df2011-04-15 15:51:59 -07003880 ata_port_err(ap,
3881 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
Tejun Heo1ae46312007-07-16 14:29:40 +09003882 }
3883
Tejun Heoad72cf92010-07-02 10:03:52 +02003884 queue_delayed_work(system_long_wq, &ap->hotplug_task,
Tejun Heo1ae46312007-07-16 14:29:40 +09003885 round_jiffies_relative(HZ));
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003886}
Tejun Heo0ea035a2006-05-31 18:28:01 +09003887
3888/**
3889 * ata_scsi_offline_dev - offline attached SCSI device
3890 * @dev: ATA device to offline attached SCSI device for
3891 *
3892 * This function is called from ata_eh_hotplug() and responsible
3893 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04003894 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09003895 * against clearing.
3896 *
3897 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003898 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09003899 *
3900 * RETURNS:
3901 * 1 if attached SCSI device exists, 0 otherwise.
3902 */
3903int ata_scsi_offline_dev(struct ata_device *dev)
3904{
3905 if (dev->sdev) {
3906 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
3907 return 1;
3908 }
3909 return 0;
3910}
Tejun Heo580b21022006-05-31 18:28:05 +09003911
3912/**
3913 * ata_scsi_remove_dev - remove attached SCSI device
3914 * @dev: ATA device to remove attached SCSI device for
3915 *
3916 * This function is called from ata_eh_scsi_hotplug() and
3917 * responsible for removing the SCSI device attached to @dev.
3918 *
3919 * LOCKING:
3920 * Kernel thread context (may sleep).
3921 */
3922static void ata_scsi_remove_dev(struct ata_device *dev)
3923{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003924 struct ata_port *ap = dev->link->ap;
Tejun Heo580b21022006-05-31 18:28:05 +09003925 struct scsi_device *sdev;
3926 unsigned long flags;
3927
3928 /* Alas, we need to grab scan_mutex to ensure SCSI device
3929 * state doesn't change underneath us and thus
3930 * scsi_device_get() always succeeds. The mutex locking can
3931 * be removed if there is __scsi_device_get() interface which
3932 * increments reference counts regardless of device state.
3933 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003934 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04003935 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b21022006-05-31 18:28:05 +09003936
Jeff Garzikcca39742006-08-24 03:19:22 -04003937 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b21022006-05-31 18:28:05 +09003938 sdev = dev->sdev;
3939 dev->sdev = NULL;
3940
3941 if (sdev) {
3942 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04003943 * away underneath us after the host lock and
Tejun Heo580b21022006-05-31 18:28:05 +09003944 * scan_mutex are released. Hold onto it.
3945 */
3946 if (scsi_device_get(sdev) == 0) {
3947 /* The following ensures the attached sdev is
3948 * offline on return from ata_scsi_offline_dev()
3949 * regardless it wins or loses the race
3950 * against this function.
3951 */
3952 scsi_device_set_state(sdev, SDEV_OFFLINE);
3953 } else {
3954 WARN_ON(1);
3955 sdev = NULL;
3956 }
3957 }
3958
Jeff Garzikba6a1302006-06-22 23:46:10 -04003959 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04003960 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09003961
3962 if (sdev) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003963 ata_dev_info(dev, "detaching (SCSI %s)\n",
3964 dev_name(&sdev->sdev_gendev));
Tejun Heo580b21022006-05-31 18:28:05 +09003965
3966 scsi_remove_device(sdev);
3967 scsi_device_put(sdev);
3968 }
3969}
3970
Tejun Heo41bda9c2007-08-06 18:36:24 +09003971static void ata_scsi_handle_link_detach(struct ata_link *link)
3972{
3973 struct ata_port *ap = link->ap;
3974 struct ata_device *dev;
3975
Tejun Heo1eca4362008-11-03 20:03:17 +09003976 ata_for_each_dev(dev, link, ALL) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003977 unsigned long flags;
3978
3979 if (!(dev->flags & ATA_DFLAG_DETACHED))
3980 continue;
3981
3982 spin_lock_irqsave(ap->lock, flags);
3983 dev->flags &= ~ATA_DFLAG_DETACHED;
3984 spin_unlock_irqrestore(ap->lock, flags);
3985
Aaron Luf1bc1e42013-08-23 10:17:54 +08003986 if (zpodd_dev_enabled(dev))
3987 zpodd_exit(dev);
3988
Tejun Heo41bda9c2007-08-06 18:36:24 +09003989 ata_scsi_remove_dev(dev);
3990 }
3991}
3992
Tejun Heo580b21022006-05-31 18:28:05 +09003993/**
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003994 * ata_scsi_media_change_notify - send media change event
Randy Dunlapc5d0e6a2007-10-15 17:29:46 -07003995 * @dev: Pointer to the disk device with media change event
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003996 *
3997 * Tell the block layer to send a media change notification
3998 * event.
3999 *
4000 * LOCKING:
Tejun Heo854c73a2007-09-23 13:14:11 +09004001 * spin_lock_irqsave(host lock)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004002 */
Tejun Heo854c73a2007-09-23 13:14:11 +09004003void ata_scsi_media_change_notify(struct ata_device *dev)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004004{
Tejun Heo854c73a2007-09-23 13:14:11 +09004005 if (dev->sdev)
Jeff Garzikf26792d2007-10-29 17:18:39 -04004006 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
4007 GFP_ATOMIC);
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004008}
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004009
4010/**
Tejun Heo580b21022006-05-31 18:28:05 +09004011 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00004012 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b21022006-05-31 18:28:05 +09004013 *
4014 * Perform SCSI part of hotplug. It's executed from a separate
4015 * workqueue after EH completes. This is necessary because SCSI
4016 * hot plugging requires working EH and hot unplugging is
4017 * synchronized with hot plugging with a mutex.
4018 *
4019 * LOCKING:
4020 * Kernel thread context (may sleep).
4021 */
David Howells65f27f32006-11-22 14:55:48 +00004022void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b21022006-05-31 18:28:05 +09004023{
David Howells65f27f32006-11-22 14:55:48 +00004024 struct ata_port *ap =
4025 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004026 int i;
Tejun Heo580b21022006-05-31 18:28:05 +09004027
Tejun Heob51e9e52006-06-29 01:29:30 +09004028 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b21022006-05-31 18:28:05 +09004029 DPRINTK("ENTER/EXIT - unloading\n");
4030 return;
4031 }
4032
Tejun Heo85fbd722013-12-18 07:07:32 -05004033 /*
4034 * XXX - UGLY HACK
4035 *
4036 * The block layer suspend/resume path is fundamentally broken due
4037 * to freezable kthreads and workqueue and may deadlock if a block
4038 * device gets removed while resume is in progress. I don't know
4039 * what the solution is short of removing freezable kthreads and
4040 * workqueues altogether.
4041 *
4042 * The following is an ugly hack to avoid kicking off device
4043 * removal while freezer is active. This is a joke but does avoid
4044 * this particular deadlock scenario.
4045 *
4046 * https://bugzilla.kernel.org/show_bug.cgi?id=62801
4047 * http://marc.info/?l=linux-kernel&m=138695698516487
4048 */
4049#ifdef CONFIG_FREEZER
4050 while (pm_freezing)
4051 msleep(10);
4052#endif
4053
Tejun Heo580b21022006-05-31 18:28:05 +09004054 DPRINTK("ENTER\n");
Tejun Heoad72cf92010-07-02 10:03:52 +02004055 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004056
Tejun Heo41bda9c2007-08-06 18:36:24 +09004057 /* Unplug detached devices. We cannot use link iterator here
4058 * because PMP links have to be scanned even if PMP is
4059 * currently not attached. Iterate manually.
4060 */
4061 ata_scsi_handle_link_detach(&ap->link);
4062 if (ap->pmp_link)
4063 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
4064 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
Tejun Heo580b21022006-05-31 18:28:05 +09004065
4066 /* scan for new ones */
Tejun Heo1ae46312007-07-16 14:29:40 +09004067 ata_scsi_scan_host(ap, 0);
Tejun Heo580b21022006-05-31 18:28:05 +09004068
Tejun Heoad72cf92010-07-02 10:03:52 +02004069 mutex_unlock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004070 DPRINTK("EXIT\n");
4071}
Tejun Heo83c47bc2006-05-31 18:28:07 +09004072
4073/**
4074 * ata_scsi_user_scan - indication for user-initiated bus scan
4075 * @shost: SCSI host to scan
4076 * @channel: Channel to scan
4077 * @id: ID to scan
4078 * @lun: LUN to scan
4079 *
4080 * This function is called when user explicitly requests bus
4081 * scan. Set probe pending flag and invoke EH.
4082 *
4083 * LOCKING:
4084 * SCSI layer (we don't care)
4085 *
4086 * RETURNS:
4087 * Zero.
4088 */
Gwendal Grignoud9027472010-05-25 12:31:38 -07004089int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004090 unsigned int id, u64 lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004091{
4092 struct ata_port *ap = ata_shost_to_port(shost);
4093 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004094 int devno, rc = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004095
4096 if (!ap->ops->error_handler)
4097 return -EOPNOTSUPP;
4098
Tejun Heo41bda9c2007-08-06 18:36:24 +09004099 if (lun != SCAN_WILD_CARD && lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004100 return -EINVAL;
4101
Tejun Heo071f44b2008-04-07 22:47:22 +09004102 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004103 if (channel != SCAN_WILD_CARD && channel)
4104 return -EINVAL;
4105 devno = id;
4106 } else {
4107 if (id != SCAN_WILD_CARD && id)
4108 return -EINVAL;
4109 devno = channel;
4110 }
4111
Jeff Garzikba6a1302006-06-22 23:46:10 -04004112 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004113
Tejun Heo41bda9c2007-08-06 18:36:24 +09004114 if (devno == SCAN_WILD_CARD) {
4115 struct ata_link *link;
4116
Tejun Heo1eca4362008-11-03 20:03:17 +09004117 ata_for_each_link(link, ap, EDGE) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004118 struct ata_eh_info *ehi = &link->eh_info;
Tejun Heob558edd2008-01-24 00:05:14 +09004119 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09004120 ehi->action |= ATA_EH_RESET;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004121 }
Tejun Heo83c47bc2006-05-31 18:28:07 +09004122 } else {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004123 struct ata_device *dev = ata_find_dev(ap, devno);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004124
4125 if (dev) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004126 struct ata_eh_info *ehi = &dev->link->eh_info;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004127 ehi->probe_mask |= 1 << dev->devno;
Tejun Heocf480622008-01-24 00:05:14 +09004128 ehi->action |= ATA_EH_RESET;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004129 } else
4130 rc = -EINVAL;
4131 }
4132
Tejun Heo309afcb2006-09-30 18:07:17 +09004133 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09004134 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09004135 spin_unlock_irqrestore(ap->lock, flags);
4136 ata_port_wait_eh(ap);
4137 } else
4138 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004139
4140 return rc;
4141}
zhao, forrest3057ac32006-06-12 12:01:34 +08004142
4143/**
Tejun Heod0171262006-06-12 22:51:14 +09004144 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00004145 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08004146 *
Tejun Heod0171262006-06-12 22:51:14 +09004147 * After ATA pass thru (SAT) commands are executed successfully,
Tejun Heoad72cf92010-07-02 10:03:52 +02004148 * libata need to propagate the changes to SCSI layer.
zhao, forrest3057ac32006-06-12 12:01:34 +08004149 *
Tejun Heod0171262006-06-12 22:51:14 +09004150 * LOCKING:
4151 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08004152 */
David Howells65f27f32006-11-22 14:55:48 +00004153void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08004154{
David Howells65f27f32006-11-22 14:55:48 +00004155 struct ata_port *ap =
4156 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004157 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09004158 struct ata_device *dev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004159 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08004160
Tejun Heoad72cf92010-07-02 10:03:52 +02004161 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heof84e7e42006-11-22 11:21:31 +09004162 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08004163
Tejun Heo1eca4362008-11-03 20:03:17 +09004164 ata_for_each_link(link, ap, EDGE) {
4165 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004166 struct scsi_device *sdev = dev->sdev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004167
Tejun Heo1eca4362008-11-03 20:03:17 +09004168 if (!sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004169 continue;
4170 if (scsi_device_get(sdev))
4171 continue;
Tejun Heof84e7e42006-11-22 11:21:31 +09004172
Tejun Heo41bda9c2007-08-06 18:36:24 +09004173 spin_unlock_irqrestore(ap->lock, flags);
4174 scsi_rescan_device(&(sdev->sdev_gendev));
4175 scsi_device_put(sdev);
4176 spin_lock_irqsave(ap->lock, flags);
4177 }
zhao, forrest3057ac32006-06-12 12:01:34 +08004178 }
Tejun Heof84e7e42006-11-22 11:21:31 +09004179
4180 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad72cf92010-07-02 10:03:52 +02004181 mutex_unlock(&ap->scsi_scan_mutex);
zhao, forrest3057ac32006-06-12 12:01:34 +08004182}
Brian King80289162006-08-07 14:27:31 -05004183
4184/**
4185 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04004186 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05004187 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04004188 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05004189 *
4190 * LOCKING:
4191 * PCI/etc. bus probe sem.
4192 *
4193 * RETURNS:
4194 * ata_port pointer on success / NULL on failure.
4195 */
4196
Jeff Garzikcca39742006-08-24 03:19:22 -04004197struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05004198 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04004199 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05004200{
Tejun Heof3187192007-04-17 23:44:07 +09004201 struct ata_port *ap;
Brian King80289162006-08-07 14:27:31 -05004202
Tejun Heof3187192007-04-17 23:44:07 +09004203 ap = ata_port_alloc(host);
Brian King80289162006-08-07 14:27:31 -05004204 if (!ap)
4205 return NULL;
4206
Tejun Heof3187192007-04-17 23:44:07 +09004207 ap->port_no = 0;
James Bottomleya29b5da2011-01-23 08:30:00 -06004208 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09004209 ap->pio_mask = port_info->pio_mask;
4210 ap->mwdma_mask = port_info->mwdma_mask;
4211 ap->udma_mask = port_info->udma_mask;
4212 ap->flags |= port_info->flags;
4213 ap->ops = port_info->port_ops;
4214 ap->cbl = ATA_CBL_SATA;
4215
Brian King80289162006-08-07 14:27:31 -05004216 return ap;
4217}
4218EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
4219
4220/**
4221 * ata_sas_port_start - Set port up for dma.
4222 * @ap: Port to initialize
4223 *
4224 * Called just after data structures for each port are
James Bottomleydde20202008-02-19 11:36:56 +01004225 * initialized.
Brian King80289162006-08-07 14:27:31 -05004226 *
4227 * May be used as the port_start() entry in ata_port_operations.
4228 *
4229 * LOCKING:
4230 * Inherited from caller.
4231 */
4232int ata_sas_port_start(struct ata_port *ap)
4233{
Nishanth Aravamudan3f1e0462011-06-16 08:28:36 -07004234 /*
4235 * the port is marked as frozen at allocation time, but if we don't
4236 * have new eh, we won't thaw it
4237 */
4238 if (!ap->ops->error_handler)
4239 ap->pflags &= ~ATA_PFLAG_FROZEN;
James Bottomleydde20202008-02-19 11:36:56 +01004240 return 0;
Brian King80289162006-08-07 14:27:31 -05004241}
4242EXPORT_SYMBOL_GPL(ata_sas_port_start);
4243
4244/**
4245 * ata_port_stop - Undo ata_sas_port_start()
4246 * @ap: Port to shut down
4247 *
Brian King80289162006-08-07 14:27:31 -05004248 * May be used as the port_stop() entry in ata_port_operations.
4249 *
4250 * LOCKING:
4251 * Inherited from caller.
4252 */
4253
4254void ata_sas_port_stop(struct ata_port *ap)
4255{
Brian King80289162006-08-07 14:27:31 -05004256}
4257EXPORT_SYMBOL_GPL(ata_sas_port_stop);
4258
Dan Williamsb2024452012-03-21 21:09:07 -07004259/**
4260 * ata_sas_async_probe - simply schedule probing and return
4261 * @ap: Port to probe
4262 *
4263 * For batch scheduling of probe for sas attached ata devices, assumes
4264 * the port has already been through ata_sas_port_init()
4265 */
4266void ata_sas_async_probe(struct ata_port *ap)
Dan Williams9508a662012-01-18 20:47:01 -08004267{
Dan Williamsb2024452012-03-21 21:09:07 -07004268 __ata_port_probe(ap);
Dan Williams9508a662012-01-18 20:47:01 -08004269}
Dan Williamsb2024452012-03-21 21:09:07 -07004270EXPORT_SYMBOL_GPL(ata_sas_async_probe);
4271
4272int ata_sas_sync_probe(struct ata_port *ap)
4273{
4274 return ata_port_probe(ap);
4275}
4276EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
4277
Dan Williams9508a662012-01-18 20:47:01 -08004278
Brian King80289162006-08-07 14:27:31 -05004279/**
4280 * ata_sas_port_init - Initialize a SATA device
4281 * @ap: SATA port to initialize
4282 *
4283 * LOCKING:
4284 * PCI/etc. bus probe sem.
4285 *
4286 * RETURNS:
4287 * Zero on success, non-zero on error.
4288 */
4289
4290int ata_sas_port_init(struct ata_port *ap)
4291{
4292 int rc = ap->ops->port_start(ap);
4293
Dan Williamsb2024452012-03-21 21:09:07 -07004294 if (rc)
4295 return rc;
4296 ap->print_id = atomic_inc_return(&ata_print_id);
4297 return 0;
Brian King80289162006-08-07 14:27:31 -05004298}
4299EXPORT_SYMBOL_GPL(ata_sas_port_init);
4300
4301/**
4302 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
4303 * @ap: SATA port to destroy
4304 *
4305 */
4306
4307void ata_sas_port_destroy(struct ata_port *ap)
4308{
Tejun Heof0d36ef2007-01-20 16:00:28 +09004309 if (ap->ops->port_stop)
4310 ap->ops->port_stop(ap);
Brian King80289162006-08-07 14:27:31 -05004311 kfree(ap);
4312}
4313EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
4314
4315/**
4316 * ata_sas_slave_configure - Default slave_config routine for libata devices
4317 * @sdev: SCSI device to configure
4318 * @ap: ATA port to which SCSI device is attached
4319 *
4320 * RETURNS:
4321 * Zero.
4322 */
4323
4324int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
4325{
4326 ata_scsi_sdev_config(sdev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004327 ata_scsi_dev_config(sdev, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05004328 return 0;
4329}
4330EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
4331
4332/**
4333 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
4334 * @cmd: SCSI command to be sent
Brian King80289162006-08-07 14:27:31 -05004335 * @ap: ATA port to which the command is being sent
4336 *
4337 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06004338 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
4339 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05004340 */
4341
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004342int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
Brian King80289162006-08-07 14:27:31 -05004343{
Brian King08475a12006-11-20 13:51:56 -06004344 int rc = 0;
4345
Brian King80289162006-08-07 14:27:31 -05004346 ata_scsi_dump_cdb(ap, cmd);
4347
Tejun Heo2486fa52008-07-31 07:52:40 +09004348 if (likely(ata_dev_enabled(ap->link.device)))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004349 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05004350 else {
4351 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004352 cmd->scsi_done(cmd);
Brian King80289162006-08-07 14:27:31 -05004353 }
Brian King08475a12006-11-20 13:51:56 -06004354 return rc;
Brian King80289162006-08-07 14:27:31 -05004355}
4356EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
Shaohua Li98bd4be2015-01-23 19:52:07 -08004357
4358int ata_sas_allocate_tag(struct ata_port *ap)
4359{
4360 unsigned int max_queue = ap->host->n_tags;
4361 unsigned int i, tag;
4362
4363 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
Tejun Heo3a028242015-03-24 14:14:18 -04004364 tag = tag < max_queue ? tag : 0;
Shaohua Li98bd4be2015-01-23 19:52:07 -08004365
4366 /* the last tag is reserved for internal command. */
4367 if (tag == ATA_TAG_INTERNAL)
4368 continue;
4369
4370 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
4371 ap->sas_last_tag = tag;
4372 return tag;
4373 }
4374 }
4375 return -1;
4376}
4377
4378void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
4379{
4380 clear_bit(tag, &ap->sas_tag_allocated);
4381}