blob: 3c33f32c04c49e676f857bf0fcbec4b9cfa45234 [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 Reinecke5b01e4b2016-04-04 11:43:54 +0200273void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
Tejun Heof0761be2008-04-28 17:16:52 +0900274{
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +0200275 if (!cmd)
276 return;
277
Tejun Heof0761be2008-04-28 17:16:52 +0900278 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
279
280 scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq);
281}
282
Hannes Reinecke492bf622016-04-04 11:43:59 +0200283void ata_scsi_set_sense_information(struct ata_device *dev,
284 struct scsi_cmnd *cmd,
285 const struct ata_taskfile *tf)
286{
287 u64 information;
288
289 if (!cmd)
290 return;
291
292 information = ata_tf_read_block(tf, dev);
293 if (information == U64_MAX)
294 return;
295
296 scsi_set_sense_information(cmd->sense_buffer,
297 SCSI_SENSE_BUFFERSIZE, information);
298}
299
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700300static ssize_t
301ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
302 const char *buf, size_t count)
303{
304 struct Scsi_Host *shost = class_to_shost(dev);
305 struct ata_port *ap = ata_shost_to_port(shost);
306 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
307 return ap->ops->em_store(ap, buf, count);
308 return -EINVAL;
309}
310
311static ssize_t
312ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
313 char *buf)
314{
315 struct Scsi_Host *shost = class_to_shost(dev);
316 struct ata_port *ap = ata_shost_to_port(shost);
317
318 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
319 return ap->ops->em_show(ap, buf);
320 return -EINVAL;
321}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200322DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700323 ata_scsi_em_message_show, ata_scsi_em_message_store);
324EXPORT_SYMBOL_GPL(dev_attr_em_message);
325
326static ssize_t
327ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
328 char *buf)
329{
330 struct Scsi_Host *shost = class_to_shost(dev);
331 struct ata_port *ap = ata_shost_to_port(shost);
332
333 return snprintf(buf, 23, "%d\n", ap->em_message_type);
334}
335DEVICE_ATTR(em_message_type, S_IRUGO,
336 ata_scsi_em_message_type_show, NULL);
337EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
338
339static ssize_t
340ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
341 char *buf)
342{
343 struct scsi_device *sdev = to_scsi_device(dev);
344 struct ata_port *ap = ata_shost_to_port(sdev->host);
345 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
346
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800347 if (atadev && ap->ops->sw_activity_show &&
348 (ap->flags & ATA_FLAG_SW_ACTIVITY))
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700349 return ap->ops->sw_activity_show(atadev, buf);
350 return -EINVAL;
351}
352
353static ssize_t
354ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
355 const char *buf, size_t count)
356{
357 struct scsi_device *sdev = to_scsi_device(dev);
358 struct ata_port *ap = ata_shost_to_port(sdev->host);
359 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
360 enum sw_activity val;
361 int rc;
362
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800363 if (atadev && ap->ops->sw_activity_store &&
364 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700365 val = simple_strtoul(buf, NULL, 0);
366 switch (val) {
367 case OFF: case BLINK_ON: case BLINK_OFF:
368 rc = ap->ops->sw_activity_store(atadev, val);
369 if (!rc)
370 return count;
371 else
372 return rc;
373 }
374 }
375 return -EINVAL;
376}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200377DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700378 ata_scsi_activity_store);
379EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
380
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200381struct device_attribute *ata_common_sdev_attrs[] = {
382 &dev_attr_unload_heads,
383 NULL
384};
385EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
386
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500387static void ata_scsi_invalid_field(struct scsi_cmnd *cmd)
Douglas Gilbertae006512005-10-09 09:09:35 -0400388{
389 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
390 /* "Invalid field in cbd" */
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500391 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -0400392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/**
395 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
396 * @sdev: SCSI device for which BIOS geometry is to be determined
397 * @bdev: block device associated with @sdev
398 * @capacity: capacity of SCSI device
399 * @geom: location to which geometry will be output
400 *
401 * Generic bios head/sector/cylinder calculator
402 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
403 * mapping. Some situations may arise where the disk is not
404 * bootable if this is not used.
405 *
406 * LOCKING:
407 * Defined by the SCSI layer. We don't really care.
408 *
409 * RETURNS:
410 * Zero.
411 */
412int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
413 sector_t capacity, int geom[])
414{
415 geom[0] = 255;
416 geom[1] = 63;
417 sector_div(capacity, 255*63);
418 geom[2] = capacity;
419
420 return 0;
421}
422
Jeff Garzikb0955182005-05-12 15:45:22 -0400423/**
Tejun Heod8d91292010-05-15 20:09:34 +0200424 * ata_scsi_unlock_native_capacity - unlock native capacity
425 * @sdev: SCSI device to adjust device capacity for
426 *
427 * This function is called if a partition on @sdev extends beyond
428 * the end of the device. It requests EH to unlock HPA.
429 *
430 * LOCKING:
431 * Defined by the SCSI layer. Might sleep.
432 */
433void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
434{
435 struct ata_port *ap = ata_shost_to_port(sdev->host);
436 struct ata_device *dev;
437 unsigned long flags;
438
439 spin_lock_irqsave(ap->lock, flags);
440
441 dev = ata_scsi_find_dev(ap, sdev);
442 if (dev && dev->n_sectors < dev->n_native_sectors) {
443 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
444 dev->link->eh_info.action |= ATA_EH_RESET;
445 ata_port_schedule_eh(ap);
446 }
447
448 spin_unlock_irqrestore(ap->lock, flags);
449 ata_port_wait_eh(ap);
450}
451
452/**
Tejun Heo5924b742007-01-02 20:20:07 +0900453 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
Randy Dunlap5eb66fe2009-01-20 16:28:59 -0800454 * @ap: target port
Tejun Heo5924b742007-01-02 20:20:07 +0900455 * @sdev: SCSI device to get identify data for
456 * @arg: User buffer area for identify data
457 *
458 * LOCKING:
459 * Defined by the SCSI layer. We don't really care.
460 *
461 * RETURNS:
462 * Zero on success, negative errno on error.
463 */
Jeff Garzik94be9a52009-01-16 10:17:09 -0500464static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
465 void __user *arg)
Tejun Heo5924b742007-01-02 20:20:07 +0900466{
Tejun Heo5924b742007-01-02 20:20:07 +0900467 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
468 u16 __user *dst = arg;
469 char buf[40];
470
471 if (!dev)
472 return -ENOMSG;
473
474 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
475 return -EFAULT;
476
477 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
478 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
479 return -EFAULT;
480
481 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
482 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
483 return -EFAULT;
484
485 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
486 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
487 return -EFAULT;
488
489 return 0;
490}
491
492/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400493 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800494 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400495 * @arg: User provided data for issuing command
496 *
497 * LOCKING:
498 * Defined by the SCSI layer. We don't really care.
499 *
500 * RETURNS:
501 * Zero on success, negative errno on error.
502 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400503int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
504{
505 int rc = 0;
506 u8 scsi_cmd[MAX_COMMAND_SIZE];
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700507 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400508 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600509 enum dma_data_direction data_dir;
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700510 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400511
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500512 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400513 return -EINVAL;
514
515 if (copy_from_user(args, arg, sizeof(args)))
516 return -EFAULT;
517
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700518 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
519 if (!sensebuf)
520 return -ENOMEM;
521
Jeff Garzikb0955182005-05-12 15:45:22 -0400522 memset(scsi_cmd, 0, sizeof(scsi_cmd));
523
524 if (args[3]) {
Grant Grundler295124d2010-08-17 10:56:53 -0700525 argsize = ATA_SECT_SIZE * args[3];
Jeff Garzikb0955182005-05-12 15:45:22 -0400526 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400527 if (argbuf == NULL) {
528 rc = -ENOMEM;
529 goto error;
530 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400531
532 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
533 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400534 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600535 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400536 } else {
537 scsi_cmd[1] = (3 << 1); /* Non-data */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700538 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600539 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400540 }
541
542 scsi_cmd[0] = ATA_16;
543
544 scsi_cmd[4] = args[2];
Bartlomiej Zolnierkiewicza4f19042008-10-10 22:39:20 +0200545 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
Jeff Garzikb0955182005-05-12 15:45:22 -0400546 scsi_cmd[6] = args[3];
547 scsi_cmd[8] = args[1];
548 scsi_cmd[10] = 0x4f;
549 scsi_cmd[12] = 0xc2;
550 } else {
551 scsi_cmd[6] = args[1];
552 }
553 scsi_cmd[14] = args[0];
554
555 /* Good values for timeout and retries? Values below
556 from scsi_ioctl_send_command() for default case... */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700557 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900558 sensebuf, (10*HZ), 5, 0, NULL);
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700559
560 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
561 u8 *desc = sensebuf + 8;
562 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
563
564 /* If we set cc then ATA pass-through will cause a
565 * check condition even if no error. Filter that. */
566 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
567 struct scsi_sense_hdr sshdr;
568 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400569 &sshdr);
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100570 if (sshdr.sense_key == RECOVERED_ERROR &&
571 sshdr.asc == 0 && sshdr.ascq == 0x1d)
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700572 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
573 }
574
575 /* Send userspace a few ATA registers (same as drivers/ide) */
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400576 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
577 desc[0] == 0x09) { /* code is "ATA Descriptor" */
578 args[0] = desc[13]; /* status */
579 args[1] = desc[3]; /* error */
580 args[2] = desc[5]; /* sector count (0:7) */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700581 if (copy_to_user(arg, args, sizeof(args)))
582 rc = -EFAULT;
583 }
584 }
585
586
587 if (cmd_result) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400588 rc = -EIO;
589 goto error;
590 }
591
Jeff Garzikb0955182005-05-12 15:45:22 -0400592 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500593 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400594 rc = -EFAULT;
595error:
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700596 kfree(sensebuf);
Jesper Juhl8f760782006-06-27 02:55:06 -0700597 kfree(argbuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400598 return rc;
599}
600
601/**
602 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800603 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400604 * @arg: User provided data for issuing command
605 *
606 * LOCKING:
607 * Defined by the SCSI layer. We don't really care.
608 *
609 * RETURNS:
610 * Zero on success, negative errno on error.
611 */
612int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
613{
614 int rc = 0;
615 u8 scsi_cmd[MAX_COMMAND_SIZE];
David Milburnaf068bd2007-01-30 00:59:15 -0800616 u8 args[7], *sensebuf = NULL;
617 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400618
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500619 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400620 return -EINVAL;
621
622 if (copy_from_user(args, arg, sizeof(args)))
623 return -EFAULT;
624
David Milburnaf068bd2007-01-30 00:59:15 -0800625 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
626 if (!sensebuf)
627 return -ENOMEM;
628
Jeff Garzikb0955182005-05-12 15:45:22 -0400629 memset(scsi_cmd, 0, sizeof(scsi_cmd));
630 scsi_cmd[0] = ATA_16;
631 scsi_cmd[1] = (3 << 1); /* Non-data */
David Milburnaf068bd2007-01-30 00:59:15 -0800632 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Jeff Garzikb0955182005-05-12 15:45:22 -0400633 scsi_cmd[4] = args[1];
634 scsi_cmd[6] = args[2];
635 scsi_cmd[8] = args[3];
636 scsi_cmd[10] = args[4];
637 scsi_cmd[12] = args[5];
Mark Lord277239f2007-03-30 17:45:52 -0400638 scsi_cmd[13] = args[6] & 0x4f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400639 scsi_cmd[14] = args[0];
640
Jeff Garzikb0955182005-05-12 15:45:22 -0400641 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500642 from scsi_ioctl_send_command() for default case... */
David Milburnaf068bd2007-01-30 00:59:15 -0800643 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900644 sensebuf, (10*HZ), 5, 0, NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400645
David Milburnaf068bd2007-01-30 00:59:15 -0800646 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
647 u8 *desc = sensebuf + 8;
648 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
649
650 /* If we set cc then ATA pass-through will cause a
651 * check condition even if no error. Filter that. */
652 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
653 struct scsi_sense_hdr sshdr;
654 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
655 &sshdr);
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100656 if (sshdr.sense_key == RECOVERED_ERROR &&
657 sshdr.asc == 0 && sshdr.ascq == 0x1d)
David Milburnaf068bd2007-01-30 00:59:15 -0800658 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
659 }
660
661 /* Send userspace ATA registers */
662 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
663 desc[0] == 0x09) {/* code is "ATA Descriptor" */
664 args[0] = desc[13]; /* status */
665 args[1] = desc[3]; /* error */
666 args[2] = desc[5]; /* sector count (0:7) */
667 args[3] = desc[7]; /* lbal */
668 args[4] = desc[9]; /* lbam */
669 args[5] = desc[11]; /* lbah */
670 args[6] = desc[12]; /* select */
671 if (copy_to_user(arg, args, sizeof(args)))
672 rc = -EFAULT;
673 }
674 }
675
676 if (cmd_result) {
677 rc = -EIO;
678 goto error;
679 }
680
681 error:
682 kfree(sensebuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400683 return rc;
684}
685
Alan Coxe3cf95d2009-04-09 17:31:17 +0100686static int ata_ioc32(struct ata_port *ap)
687{
688 if (ap->flags & ATA_FLAG_PIO_DMA)
689 return 1;
690 if (ap->pflags & ATA_PFLAG_PIO32)
691 return 1;
692 return 0;
693}
694
Jeff Garzik94be9a52009-01-16 10:17:09 -0500695int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
696 int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Arnd Bergmann287e6612016-02-11 14:16:27 +0100698 unsigned long val;
699 int rc = -EINVAL;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100700 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 switch (cmd) {
Arnd Bergmann287e6612016-02-11 14:16:27 +0100703 case HDIO_GET_32BIT:
Alan Coxe3cf95d2009-04-09 17:31:17 +0100704 spin_lock_irqsave(ap->lock, flags);
705 val = ata_ioc32(ap);
706 spin_unlock_irqrestore(ap->lock, flags);
Arnd Bergmann287e6612016-02-11 14:16:27 +0100707 return put_user(val, (unsigned long __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Arnd Bergmann287e6612016-02-11 14:16:27 +0100709 case HDIO_SET_32BIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 val = (unsigned long) arg;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100711 rc = 0;
712 spin_lock_irqsave(ap->lock, flags);
713 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
714 if (val)
715 ap->pflags |= ATA_PFLAG_PIO32;
716 else
717 ap->pflags &= ~ATA_PFLAG_PIO32;
718 } else {
719 if (val != ata_ioc32(ap))
720 rc = -EINVAL;
721 }
722 spin_unlock_irqrestore(ap->lock, flags);
723 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Tejun Heo5924b742007-01-02 20:20:07 +0900725 case HDIO_GET_IDENTITY:
Jeff Garzik94be9a52009-01-16 10:17:09 -0500726 return ata_get_identity(ap, scsidev, arg);
Tejun Heo5924b742007-01-02 20:20:07 +0900727
Jeff Garzikb0955182005-05-12 15:45:22 -0400728 case HDIO_DRIVE_CMD:
729 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
730 return -EACCES;
731 return ata_cmd_ioctl(scsidev, arg);
732
733 case HDIO_DRIVE_TASK:
734 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
735 return -EACCES;
736 return ata_task_ioctl(scsidev, arg);
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 default:
739 rc = -ENOTTY;
740 break;
741 }
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return rc;
744}
Jeff Garzik94be9a52009-01-16 10:17:09 -0500745EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
746
747int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
748{
749 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
750 scsidev, cmd, arg);
751}
752EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754/**
755 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * @dev: ATA device to which the new command is attached
757 * @cmd: SCSI command that originated this ATA command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *
759 * Obtain a reference to an unused ata_queued_cmd structure,
760 * which is the basic libata structure representing a single
761 * ATA command sent to the hardware.
762 *
763 * If a command was available, fill in the SCSI-specific
764 * portions of the structure with information on the
765 * current command.
766 *
767 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400768 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 *
770 * RETURNS:
771 * Command allocated, or %NULL if none available.
772 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100773static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500774 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 struct ata_queued_cmd *qc;
777
Shaohua Li12cb5ce2015-01-15 17:32:27 -0800778 qc = ata_qc_new_init(dev, cmd->request->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (qc) {
780 qc->scsicmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500781 qc->scsidone = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Tejun Heoff2aeb12007-12-05 16:43:11 +0900783 qc->sg = scsi_sglist(cmd);
Boaz Harrosh71201652007-09-18 17:48:50 +0200784 qc->n_elem = scsi_sg_count(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 } else {
786 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500787 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
790 return qc;
791}
792
Tejun Heoaacda372008-03-18 17:47:43 +0900793static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
794{
795 struct scsi_cmnd *scmd = qc->scsicmd;
796
797 qc->extrabytes = scmd->request->extra_len;
798 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400802 * ata_dump_status - user friendly display of error info
803 * @id: id of the port in question
804 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400806 * Decode and dump the ATA error/status registers for the user so
807 * that they have some idea what really happened at the non
808 * make-believe layer.
809 *
810 * LOCKING:
811 * inherited from caller
812 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100813static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
Jeff Garzikb0955182005-05-12 15:45:22 -0400814{
815 u8 stat = tf->command, err = tf->feature;
816
817 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
818 if (stat & ATA_BUSY) {
819 printk("Busy }\n"); /* Data is not valid in this case */
820 } else {
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100821 if (stat & ATA_DRDY) printk("DriveReady ");
822 if (stat & ATA_DF) printk("DeviceFault ");
823 if (stat & ATA_DSC) printk("SeekComplete ");
824 if (stat & ATA_DRQ) printk("DataRequest ");
825 if (stat & ATA_CORR) printk("CorrectedError ");
826 if (stat & ATA_SENSE) printk("Sense ");
827 if (stat & ATA_ERR) printk("Error ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400828 printk("}\n");
829
830 if (err) {
831 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100832 if (err & ATA_ABORTED) printk("DriveStatusError ");
833 if (err & ATA_ICRC) {
834 if (err & ATA_ABORTED)
835 printk("BadCRC ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400836 else printk("Sector ");
837 }
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100838 if (err & ATA_UNC) printk("UncorrectableError ");
839 if (err & ATA_IDNF) printk("SectorIdNotFound ");
840 if (err & ATA_TRK0NF) printk("TrackZeroNotFound ");
841 if (err & ATA_AMNF) printk("AddrMarkNotFound ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400842 printk("}\n");
843 }
844 }
845}
846
847/**
848 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800849 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400850 * @drv_stat: value contained in ATA status register
851 * @drv_err: value contained in ATA error register
852 * @sk: the sense key we'll fill out
853 * @asc: the additional sense code we'll fill out
854 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900855 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400856 *
857 * Converts an ATA error into a SCSI error. Fill out pointers to
858 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
859 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 *
861 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400862 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100864static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
865 u8 *asc, u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Jeff Garzikb0955182005-05-12 15:45:22 -0400867 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100870 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /* BBD|ECC|ID|MAR */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100872 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
873 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /* BBD|ECC|ID */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100875 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
876 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /* ECC|MC|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100878 {0x61, HARDWARE_ERROR, 0x00, 0x00},
879 // Device fault Hardware error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100881 {0x84, ABORTED_COMMAND, 0x47, 0x00},
882 // Data CRC error SCSI parity error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /* MC|ID|ABRT|TRK0|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100884 {0x37, NOT_READY, 0x04, 0x00},
885 // Unit offline Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* MCR|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100887 {0x09, NOT_READY, 0x04, 0x00},
888 // Unrecovered disk error Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /* Bad address mark */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100890 {0x01, MEDIUM_ERROR, 0x13, 0x00},
891 // Address mark not found for data field
892 /* TRK0 - Track 0 not found */
893 {0x02, HARDWARE_ERROR, 0x00, 0x00},
894 // Hardware error
Gwendal Grignou78062c52013-06-18 10:54:48 -0700895 /* Abort: 0x04 is not translated here, see below */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /* Media change request */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100897 {0x08, NOT_READY, 0x04, 0x00},
898 // FIXME: faking offline
899 /* SRV/IDNF - ID not found */
900 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
901 // Logical address out of range
902 /* MC - Media Changed */
903 {0x20, UNIT_ATTENTION, 0x28, 0x00},
904 // Not ready to ready change, medium may have changed
905 /* ECC - Uncorrectable ECC error */
906 {0x40, MEDIUM_ERROR, 0x11, 0x04},
907 // Unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* BBD - block marked bad */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100909 {0x80, MEDIUM_ERROR, 0x11, 0x04},
910 // Block marked bad Medium error, unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
912 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100913 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* Must be first because BUSY means no other bits valid */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100915 {0x80, ABORTED_COMMAND, 0x47, 0x00},
916 // Busy, fake parity for now
917 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
918 // Device ready, unaligned write command
919 {0x20, HARDWARE_ERROR, 0x44, 0x00},
920 // Device fault, internal target failure
921 {0x08, ABORTED_COMMAND, 0x47, 0x00},
922 // Timed out in xfer, fake parity for now
923 {0x04, RECOVERED_ERROR, 0x11, 0x00},
924 // Recovered ECC error Medium error, recovered
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
926 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /*
929 * Is this an error we can process/parse
930 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400931 if (drv_stat & ATA_BUSY) {
932 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Jeff Garzikb0955182005-05-12 15:45:22 -0400935 if (drv_err) {
936 /* Look for drv_err */
937 for (i = 0; sense_table[i][0] != 0xFF; i++) {
938 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500939 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -0400940 sense_table[i][0]) {
941 *sk = sense_table[i][1];
942 *asc = sense_table[i][2];
943 *ascq = sense_table[i][3];
944 goto translate_done;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
Gwendal Grignou78062c52013-06-18 10:54:48 -0700949 /*
950 * Fall back to interpreting status bits. Note that if the drv_err
951 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
952 * is not descriptive enough.
953 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400954 for (i = 0; stat_table[i][0] != 0xFF; i++) {
955 if (stat_table[i][0] & drv_stat) {
956 *sk = stat_table[i][1];
957 *asc = stat_table[i][2];
958 *ascq = stat_table[i][3];
959 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Gwendal Grignou78062c52013-06-18 10:54:48 -0700963 /*
964 * We need a sensible error return here, which is tricky, and one
965 * that won't cause people to do things like return a disk wrongly.
966 */
Alan Cox2d202022006-03-21 15:53:46 +0000967 *sk = ABORTED_COMMAND;
968 *asc = 0x00;
969 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -0400970
971 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +0900972 if (verbose)
973 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
974 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
975 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -0400976 return;
977}
978
979/*
Tejun Heo750426a2006-11-14 22:37:35 +0900980 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -0400981 * @qc: Command that completed.
982 *
983 * This function is specific to the ATA descriptor format sense
984 * block specified for the ATA pass through commands. Regardless
985 * of whether the command errored or not, return a sense
986 * block. Copy all controller registers into the sense
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -0800987 * block. If there was no error, we get the request from an ATA
988 * passthrough command, so we use the following sense data:
989 * sk = RECOVERED ERROR
990 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
991 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400992 *
993 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +0900994 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -0400995 */
Tejun Heo750426a2006-11-14 22:37:35 +0900996static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -0400997{
998 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +0900999 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001000 unsigned char *sb = cmd->sense_buffer;
1001 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +09001002 int verbose = qc->ap->ops->error_handler == NULL;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001003 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001004
1005 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1006
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001007 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001008
1009 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04001010 * Use ata_to_sense_error() to map status register bits
1011 * onto sense key, asc & ascq.
1012 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001013 if (qc->err_mask ||
1014 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001015 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001016 &sense_key, &asc, &ascq, verbose);
1017 ata_scsi_set_sense(cmd, sense_key, asc, ascq);
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001018 } else {
Hannes Reineckeb525e772016-04-04 11:44:00 +02001019 /* ATA PASS-THROUGH INFORMATION AVAILABLE */
1020 ata_scsi_set_sense(cmd, RECOVERED_ERROR, 0, 0x1D);
Jeff Garzikb0955182005-05-12 15:45:22 -04001021 }
1022
1023 /*
1024 * Sense data is current and format is descriptor.
1025 */
1026 sb[0] = 0x72;
1027
1028 desc[0] = 0x09;
1029
Tejun Heof38621b2006-11-14 22:37:35 +09001030 /* set length of additional sense data */
1031 sb[7] = 14;
1032 desc[1] = 12;
Jeff Garzikb0955182005-05-12 15:45:22 -04001033
1034 /*
1035 * Copy registers into sense buffer.
1036 */
1037 desc[2] = 0x00;
1038 desc[3] = tf->feature; /* == error reg */
1039 desc[5] = tf->nsect;
1040 desc[7] = tf->lbal;
1041 desc[9] = tf->lbam;
1042 desc[11] = tf->lbah;
1043 desc[12] = tf->device;
1044 desc[13] = tf->command; /* == status reg */
1045
1046 /*
1047 * Fill in Extend bit, and the high order bytes
1048 * if applicable.
1049 */
1050 if (tf->flags & ATA_TFLAG_LBA48) {
1051 desc[2] |= 0x01;
1052 desc[4] = tf->hob_nsect;
1053 desc[6] = tf->hob_lbal;
1054 desc[8] = tf->hob_lbam;
1055 desc[10] = tf->hob_lbah;
1056 }
1057}
1058
1059/**
Tejun Heo750426a2006-11-14 22:37:35 +09001060 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -04001061 * @qc: Command that we are erroring out
1062 *
Tejun Heod25614b2006-11-14 22:37:35 +09001063 * Generate sense block for a failed ATA command @qc. Descriptor
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001064 * format is used to accommodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -04001065 *
1066 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001067 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001068 */
Tejun Heo750426a2006-11-14 22:37:35 +09001069static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001070{
Tejun Heod25614b2006-11-14 22:37:35 +09001071 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -04001072 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001073 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001074 unsigned char *sb = cmd->sense_buffer;
Tejun Heo246619d2006-05-15 20:58:16 +09001075 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +09001076 u64 block;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001077 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001078
1079 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1080
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001081 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001082
Tejun Heod25614b2006-11-14 22:37:35 +09001083 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -04001084 * onto sense key, asc & ascq.
1085 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001086 if (qc->err_mask ||
1087 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001088 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001089 &sense_key, &asc, &ascq, verbose);
1090 ata_scsi_set_sense(cmd, sense_key, asc, ascq);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001091 } else {
1092 /* Could not decode error */
1093 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
1094 tf->command, qc->err_mask);
1095 ata_scsi_set_sense(cmd, ABORTED_COMMAND, 0, 0);
1096 return;
Jeff Garzikb0955182005-05-12 15:45:22 -04001097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Tejun Heod25614b2006-11-14 22:37:35 +09001099 block = ata_tf_read_block(&qc->result_tf, dev);
Hannes Reineckecffd1ee2016-04-04 11:43:57 +02001100 if (block == U64_MAX)
1101 return;
Jeff Garzik0274aa22005-06-22 13:50:56 -04001102
Hannes Reineckecf8b49b2016-04-04 11:43:58 +02001103 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Brian Kinga6cce2a2006-03-17 17:04:15 -06001106static void ata_scsi_sdev_config(struct scsi_device *sdev)
1107{
1108 sdev->use_10_for_rw = 1;
1109 sdev->use_10_for_ms = 1;
Martin K. Petersen3c6bdae2012-09-18 12:19:30 -04001110 sdev->no_report_opcodes = 1;
Martin K. Petersen5db44862012-09-18 12:19:32 -04001111 sdev->no_write_same = 1;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001112
1113 /* Schedule policy is determined by ->qc_defer() callback and
1114 * it needs to see every deferred qc. Set dev_blocked to 1 to
1115 * prevent SCSI midlayer from automatically deferring
1116 * requests.
1117 */
1118 sdev->max_device_blocked = 1;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001119}
1120
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001121/**
1122 * atapi_drain_needed - Check whether data transfer may overflow
Randy Dunlap73fd8b62008-02-19 13:43:21 -08001123 * @rq: request to be checked
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001124 *
1125 * ATAPI commands which transfer variable length data to host
1126 * might overflow due to application error or hardare bug. This
1127 * function checks whether overflow should be drained and ignored
1128 * for @request.
1129 *
1130 * LOCKING:
1131 * None.
1132 *
1133 * RETURNS:
1134 * 1 if ; otherwise, 0.
1135 */
1136static int atapi_drain_needed(struct request *rq)
1137{
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001138 if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001139 return 0;
1140
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001141 if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001142 return 0;
1143
1144 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
1145}
1146
1147static int ata_scsi_dev_config(struct scsi_device *sdev,
1148 struct ata_device *dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -06001149{
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001150 struct request_queue *q = sdev->request_queue;
1151
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02001152 if (!ata_id_has_unload(dev->id))
1153 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1154
Tejun Heo914ed352006-11-01 18:39:55 +09001155 /* configure max sectors */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001156 blk_queue_max_hw_sectors(q, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -06001157
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001158 if (dev->class == ATA_DEV_ATAPI) {
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001159 void *buf;
1160
Tejun Heo729a6a32011-01-20 13:59:06 +01001161 sdev->sector_size = ATA_SECT_SIZE;
1162
1163 /* set DMA padding */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001164 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001165
1166 /* configure draining */
1167 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1168 if (!buf) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001169 ata_dev_err(dev, "drain buffer allocation failed\n");
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001170 return -ENOMEM;
1171 }
1172
1173 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1174 } else {
Grant Grundler295124d2010-08-17 10:56:53 -07001175 sdev->sector_size = ata_id_logical_sector_size(dev->id);
Tejun Heod8cf5382008-01-15 08:46:59 +09001176 sdev->manage_start_stop = 1;
James Bottomleydde20202008-02-19 11:36:56 +01001177 }
Tejun Heod8cf5382008-01-15 08:46:59 +09001178
Tejun Heo729a6a32011-01-20 13:59:06 +01001179 /*
1180 * ata_pio_sectors() expects buffer for each sector to not cross
1181 * page boundary. Enforce it by requiring buffers to be sector
1182 * aligned, which works iff sector_size is not larger than
1183 * PAGE_SIZE. ATAPI devices also need the alignment as
1184 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1185 */
1186 if (sdev->sector_size > PAGE_SIZE)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001187 ata_dev_warn(dev,
Tejun Heo729a6a32011-01-20 13:59:06 +01001188 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1189 sdev->sector_size);
1190
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001191 blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
Tejun Heo729a6a32011-01-20 13:59:06 +01001192
Jeff Garzikf26792d2007-10-29 17:18:39 -04001193 if (dev->flags & ATA_DFLAG_AN)
1194 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1195
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001196 if (dev->flags & ATA_DFLAG_NCQ) {
1197 int depth;
1198
1199 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1200 depth = min(ATA_MAX_QUEUE - 1, depth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001201 scsi_change_queue_depth(sdev, depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001202 }
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001203
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001204 blk_queue_flush_queueable(q, false);
1205
Grant Grundler295124d2010-08-17 10:56:53 -07001206 dev->sdev = sdev;
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001207 return 0;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/**
1211 * ata_scsi_slave_config - Set SCSI device attributes
1212 * @sdev: SCSI device to examine
1213 *
1214 * This is called before we actually start reading
1215 * and writing to the device, to configure certain
1216 * SCSI mid-layer behaviors.
1217 *
1218 * LOCKING:
1219 * Defined by SCSI layer. We don't really care.
1220 */
1221
1222int ata_scsi_slave_config(struct scsi_device *sdev)
1223{
Tejun Heo31534362006-05-31 18:27:36 +09001224 struct ata_port *ap = ata_shost_to_port(sdev->host);
1225 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001226 int rc = 0;
Tejun Heo31534362006-05-31 18:27:36 +09001227
Brian Kinga6cce2a2006-03-17 17:04:15 -06001228 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Tejun Heo31534362006-05-31 18:27:36 +09001230 if (dev)
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001231 rc = ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001233 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236/**
Tejun Heo83c47bc2006-05-31 18:28:07 +09001237 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1238 * @sdev: SCSI device to be destroyed
1239 *
1240 * @sdev is about to be destroyed for hot/warm unplugging. If
1241 * this unplugging was initiated by libata as indicated by NULL
1242 * dev->sdev, this function doesn't have to do anything.
1243 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1244 * Clear dev->sdev, schedule the device for ATA detach and invoke
1245 * EH.
1246 *
1247 * LOCKING:
1248 * Defined by SCSI layer. We don't really care.
1249 */
1250void ata_scsi_slave_destroy(struct scsi_device *sdev)
1251{
1252 struct ata_port *ap = ata_shost_to_port(sdev->host);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001253 struct request_queue *q = sdev->request_queue;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001254 unsigned long flags;
1255 struct ata_device *dev;
1256
1257 if (!ap->ops->error_handler)
1258 return;
1259
Jeff Garzikba6a1302006-06-22 23:46:10 -04001260 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09001261 dev = __ata_scsi_find_dev(ap, sdev);
1262 if (dev && dev->sdev) {
1263 /* SCSI device already in CANCEL state, no need to offline it */
1264 dev->sdev = NULL;
1265 dev->flags |= ATA_DFLAG_DETACH;
1266 ata_port_schedule_eh(ap);
1267 }
Jeff Garzikba6a1302006-06-22 23:46:10 -04001268 spin_unlock_irqrestore(ap->lock, flags);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001269
1270 kfree(q->dma_drain_buffer);
1271 q->dma_drain_buffer = NULL;
1272 q->dma_drain_size = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001273}
1274
1275/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001276 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
Marcos Paulo de Souza6d2dd052011-10-31 23:50:16 -02001277 * @ap: ATA port to which the device change the queue depth
1278 * @sdev: SCSI device to configure queue depth for
1279 * @queue_depth: new queue depth
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001280 *
Dan Williamsf6e67032011-09-20 15:10:33 -07001281 * libsas and libata have different approaches for associating a sdev to
1282 * its ata_port.
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001283 *
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001284 */
Dan Williamsf6e67032011-09-20 15:10:33 -07001285int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001286 int queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001287{
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001288 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +09001289 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001290
Tejun Heoc3c70c42007-02-20 23:31:22 +09001291 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001292 return sdev->queue_depth;
1293
1294 dev = ata_scsi_find_dev(ap, sdev);
1295 if (!dev || !ata_dev_enabled(dev))
1296 return sdev->queue_depth;
1297
Tejun Heoc3c70c42007-02-20 23:31:22 +09001298 /* NCQ enabled? */
Tejun Heo360f6542006-09-30 19:45:00 +09001299 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001300 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1301 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
Tejun Heo360f6542006-09-30 19:45:00 +09001302 dev->flags |= ATA_DFLAG_NCQ_OFF;
Tejun Heoc3c70c42007-02-20 23:31:22 +09001303 queue_depth = 1;
1304 }
Tejun Heo360f6542006-09-30 19:45:00 +09001305 spin_unlock_irqrestore(ap->lock, flags);
1306
Tejun Heoc3c70c42007-02-20 23:31:22 +09001307 /* limit and apply queue depth */
1308 queue_depth = min(queue_depth, sdev->host->can_queue);
1309 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1310 queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
1311
1312 if (sdev->queue_depth == queue_depth)
1313 return -EINVAL;
1314
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001315 return scsi_change_queue_depth(sdev, queue_depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001316}
1317
1318/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001319 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1320 * @sdev: SCSI device to configure queue depth for
1321 * @queue_depth: new queue depth
Dan Williamsf6e67032011-09-20 15:10:33 -07001322 *
1323 * This is libata standard hostt->change_queue_depth callback.
1324 * SCSI will call into this callback when user tries to set queue
1325 * depth via sysfs.
1326 *
1327 * LOCKING:
1328 * SCSI layer (we don't care)
1329 *
1330 * RETURNS:
1331 * Newly configured queue depth.
1332 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001333int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
Dan Williamsf6e67032011-09-20 15:10:33 -07001334{
1335 struct ata_port *ap = ata_shost_to_port(sdev->host);
1336
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001337 return __ata_change_queue_depth(ap, sdev, queue_depth);
Dan Williamsf6e67032011-09-20 15:10:33 -07001338}
1339
1340/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001341 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1342 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001343 *
1344 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1345 * (to start). Perhaps these commands should be preceded by
1346 * CHECK POWER MODE to see what power mode the device is already in.
1347 * [See SAT revision 5 at www.t10.org]
1348 *
1349 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001350 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001351 *
1352 * RETURNS:
1353 * Zero on success, non-zero on error.
1354 */
Tejun Heoad706992006-12-17 10:45:57 +09001355static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001356{
Tejun Heo542b1442006-12-17 10:45:08 +09001357 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001358 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +09001359 const u8 *cdb = scmd->cmnd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001360
Tejun Heo2e5704f2006-12-17 10:46:33 +09001361 if (scmd->cmd_len < 5)
1362 goto invalid_fld;
1363
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001364 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1365 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +09001366 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001367 ; /* ignore IMMED bit, violates sat-r05 */
1368 }
Tejun Heo542b1442006-12-17 10:45:08 +09001369 if (cdb[4] & 0x2)
Douglas Gilbertae006512005-10-09 09:09:35 -04001370 goto invalid_fld; /* LOEJ bit set not supported */
Tejun Heo542b1442006-12-17 10:45:08 +09001371 if (((cdb[4] >> 4) & 0xf) != 0)
Douglas Gilbertae006512005-10-09 09:09:35 -04001372 goto invalid_fld; /* power conditions not supported */
Tejun Heoe31e85312007-09-23 13:14:13 +09001373
Tejun Heo542b1442006-12-17 10:45:08 +09001374 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001375 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -04001376
1377 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +09001378 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -04001379
1380 tf->lbah = 0x0;
1381 tf->lbam = 0x0;
1382 tf->lbal = 0x0;
1383 tf->device |= ATA_LBA;
1384 } else {
1385 /* CHS */
1386 tf->lbal = 0x1; /* sect */
1387 tf->lbam = 0x0; /* cyl low */
1388 tf->lbah = 0x0; /* cyl high */
1389 }
1390
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001391 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
Tejun Heo920a4b12007-05-04 21:28:48 +02001392 } else {
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001393 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1394 * or S5) causing some drives to spin up and down again.
1395 */
1396 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1397 system_state == SYSTEM_POWER_OFF)
1398 goto skip;
1399
1400 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1401 system_entering_hibernation())
1402 goto skip;
1403
Robert Hancock78981a72007-01-30 00:59:18 -08001404 /* Issue ATA STANDBY IMMEDIATE command */
1405 tf->command = ATA_CMD_STANDBYNOW1;
Tejun Heo920a4b12007-05-04 21:28:48 +02001406 }
Robert Hancock78981a72007-01-30 00:59:18 -08001407
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001408 /*
1409 * Standby and Idle condition timers could be implemented but that
1410 * would require libata to implement the Power condition mode page
1411 * and allow the user to change it. Changing mode pages requires
1412 * MODE SELECT to be implemented.
1413 */
1414
1415 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001416
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001417 invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001418 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001419 /* "Invalid field in cbd" */
1420 return 1;
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001421 skip:
1422 scmd->result = SAM_STAT_GOOD;
1423 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001424}
1425
1426
1427/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1429 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 *
1431 * Sets up an ATA taskfile to issue FLUSH CACHE or
1432 * FLUSH CACHE EXT.
1433 *
1434 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001435 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 *
1437 * RETURNS:
1438 * Zero on success, non-zero on error.
1439 */
Tejun Heoad706992006-12-17 10:45:57 +09001440static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct ata_taskfile *tf = &qc->tf;
1443
1444 tf->flags |= ATA_TFLAG_DEVICE;
1445 tf->protocol = ATA_PROT_NODATA;
1446
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001447 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 tf->command = ATA_CMD_FLUSH_EXT;
1449 else
1450 tf->command = ATA_CMD_FLUSH;
1451
Tejun Heob666da32007-10-26 15:53:59 +09001452 /* flush is critical for IO integrity, consider it an IO command */
1453 qc->flags |= ATA_QCFLAG_IO;
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return 0;
1456}
1457
1458/**
Albert Lee3aef5232005-10-04 08:47:43 -04001459 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001460 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001461 *
1462 * Calculate LBA and transfer length for 6-byte commands.
1463 *
1464 * RETURNS:
1465 * @plba: the LBA
1466 * @plen: the transfer length
1467 */
Tejun Heo542b1442006-12-17 10:45:08 +09001468static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001469{
1470 u64 lba = 0;
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001471 u32 len;
Albert Lee3aef5232005-10-04 08:47:43 -04001472
1473 VPRINTK("six-byte command\n");
1474
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001475 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
Tejun Heo542b1442006-12-17 10:45:08 +09001476 lba |= ((u64)cdb[2]) << 8;
1477 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001478
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001479 len = cdb[4];
Albert Lee3aef5232005-10-04 08:47:43 -04001480
1481 *plba = lba;
1482 *plen = len;
1483}
1484
1485/**
1486 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001487 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001488 *
1489 * Calculate LBA and transfer length for 10-byte commands.
1490 *
1491 * RETURNS:
1492 * @plba: the LBA
1493 * @plen: the transfer length
1494 */
Tejun Heo542b1442006-12-17 10:45:08 +09001495static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001496{
1497 u64 lba = 0;
1498 u32 len = 0;
1499
1500 VPRINTK("ten-byte command\n");
1501
Tejun Heo542b1442006-12-17 10:45:08 +09001502 lba |= ((u64)cdb[2]) << 24;
1503 lba |= ((u64)cdb[3]) << 16;
1504 lba |= ((u64)cdb[4]) << 8;
1505 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001506
Tejun Heo542b1442006-12-17 10:45:08 +09001507 len |= ((u32)cdb[7]) << 8;
1508 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001509
1510 *plba = lba;
1511 *plen = len;
1512}
1513
1514/**
1515 * scsi_16_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 16-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_16_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("sixteen-byte command\n");
1530
Tejun Heo542b1442006-12-17 10:45:08 +09001531 lba |= ((u64)cdb[2]) << 56;
1532 lba |= ((u64)cdb[3]) << 48;
1533 lba |= ((u64)cdb[4]) << 40;
1534 lba |= ((u64)cdb[5]) << 32;
1535 lba |= ((u64)cdb[6]) << 24;
1536 lba |= ((u64)cdb[7]) << 16;
1537 lba |= ((u64)cdb[8]) << 8;
1538 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001539
Tejun Heo542b1442006-12-17 10:45:08 +09001540 len |= ((u32)cdb[10]) << 24;
1541 len |= ((u32)cdb[11]) << 16;
1542 len |= ((u32)cdb[12]) << 8;
1543 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001544
1545 *plba = lba;
1546 *plen = len;
1547}
1548
1549/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1551 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 *
1553 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1554 *
1555 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001556 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 *
1558 * RETURNS:
1559 * Zero on success, non-zero on error.
1560 */
Tejun Heoad706992006-12-17 10:45:57 +09001561static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Tejun Heo542b1442006-12-17 10:45:08 +09001563 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001565 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001567 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001568 u64 block;
1569 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1572 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Tejun Heo2e5704f2006-12-17 10:46:33 +09001574 if (cdb[0] == VERIFY) {
1575 if (scmd->cmd_len < 10)
1576 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001577 scsi_10_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001578 } else if (cdb[0] == VERIFY_16) {
1579 if (scmd->cmd_len < 16)
1580 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001581 scsi_16_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001582 } else
Douglas Gilbertae006512005-10-09 09:09:35 -04001583 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Albert Lee8bf62ece2005-05-12 15:29:42 -04001585 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001586 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001587 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001588 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001589 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001590 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Albert Lee07506692005-10-12 15:04:18 +08001592 if (dev->flags & ATA_DFLAG_LBA) {
1593 tf->flags |= ATA_TFLAG_LBA;
1594
Albert Leec6a33e22005-10-12 15:12:26 +08001595 if (lba_28_ok(block, n_block)) {
1596 /* use LBA28 */
1597 tf->command = ATA_CMD_VERIFY;
1598 tf->device |= (block >> 24) & 0xf;
1599 } else if (lba_48_ok(block, n_block)) {
1600 if (!(dev->flags & ATA_DFLAG_LBA48))
1601 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001602
1603 /* use LBA48 */
1604 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001605 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Albert Lee8bf62ece2005-05-12 15:29:42 -04001607 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Albert Lee8bf62ece2005-05-12 15:29:42 -04001609 tf->hob_lbah = (block >> 40) & 0xff;
1610 tf->hob_lbam = (block >> 32) & 0xff;
1611 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001612 } else
1613 /* request too large even for LBA48 */
1614 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001615
1616 tf->nsect = n_block & 0xff;
1617
1618 tf->lbah = (block >> 16) & 0xff;
1619 tf->lbam = (block >> 8) & 0xff;
1620 tf->lbal = block & 0xff;
1621
1622 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001624 /* CHS */
1625 u32 sect, head, cyl, track;
1626
Albert Leec6a33e22005-10-12 15:12:26 +08001627 if (!lba_28_ok(block, n_block))
1628 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001629
Albert Lee8bf62ece2005-05-12 15:29:42 -04001630 /* Convert LBA to CHS */
1631 track = (u32)block / dev->sectors;
1632 cyl = track / dev->heads;
1633 head = track % dev->heads;
1634 sect = (u32)block % dev->sectors + 1;
1635
Albert Leec187c4b2005-10-04 08:46:51 -04001636 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1637 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001638
1639 /* Check whether the converted CHS can fit.
1640 Cylinder: 0-65535
Albert Lee8bf62ece2005-05-12 15:29:42 -04001641 Head: 0-15
1642 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001643 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001644 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001647 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1648 tf->lbal = sect;
1649 tf->lbam = cyl;
1650 tf->lbah = cyl >> 8;
1651 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001655
1656invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001657 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001658 /* "Invalid field in cbd" */
1659 return 1;
1660
1661out_of_range:
Tejun Heo542b1442006-12-17 10:45:08 +09001662 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001663 /* "Logical Block Address out of range" */
1664 return 1;
1665
1666nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001667 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001668 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
1671/**
1672 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1673 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 *
1675 * Converts any of six SCSI read/write commands into the
1676 * ATA counterpart, including starting sector (LBA),
1677 * sector count, and taking into account the device's LBA48
1678 * support.
1679 *
1680 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1681 * %WRITE_16 are currently supported.
1682 *
1683 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001684 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 *
1686 * RETURNS:
1687 * Zero on success, non-zero on error.
1688 */
Tejun Heoad706992006-12-17 10:45:57 +09001689static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Tejun Heo542b1442006-12-17 10:45:08 +09001691 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001692 const u8 *cdb = scmd->cmnd;
Tejun Heobd056d72006-11-14 22:47:10 +09001693 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001694 u64 block;
1695 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001696 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Tejun Heo542b1442006-12-17 10:45:08 +09001698 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001699 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001701 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001702 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001703 case READ_10:
1704 case WRITE_10:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001705 if (unlikely(scmd->cmd_len < 10))
1706 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001707 scsi_10_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001708 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001709 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001710 break;
1711 case READ_6:
1712 case WRITE_6:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001713 if (unlikely(scmd->cmd_len < 6))
1714 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001715 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001716
1717 /* for 6-byte r/w commands, transfer length 0
1718 * means 256 blocks of data, not 0 block.
1719 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001720 if (!n_block)
1721 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001722 break;
1723 case READ_16:
1724 case WRITE_16:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001725 if (unlikely(scmd->cmd_len < 16))
1726 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001727 scsi_16_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001728 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001729 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001730 break;
1731 default:
Albert Lee8bf62ece2005-05-12 15:29:42 -04001732 DPRINTK("no-byte command\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001733 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735
Albert Lee8bf62ece2005-05-12 15:29:42 -04001736 /* Check and compose ATA command */
1737 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001738 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1739 * length 0 means transfer 0 block of data.
1740 * However, for ATA R/W commands, sector count 0 means
1741 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001742 *
1743 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001744 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001745 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001746
Tejun Heobd056d72006-11-14 22:47:10 +09001747 qc->flags |= ATA_QCFLAG_IO;
Grant Grundler295124d2010-08-17 10:56:53 -07001748 qc->nbytes = n_block * scmd->device->sector_size;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001749
Tejun Heobd056d72006-11-14 22:47:10 +09001750 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1751 qc->tag);
1752 if (likely(rc == 0))
1753 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001754
Tejun Heobd056d72006-11-14 22:47:10 +09001755 if (rc == -ERANGE)
1756 goto out_of_range;
1757 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001758invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001759 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001760 /* "Invalid field in cbd" */
1761 return 1;
1762
1763out_of_range:
Tejun Heo542b1442006-12-17 10:45:08 +09001764 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001765 /* "Logical Block Address out of range" */
1766 return 1;
1767
1768nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001769 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001770 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001773static void ata_qc_done(struct ata_queued_cmd *qc)
1774{
1775 struct scsi_cmnd *cmd = qc->scsicmd;
1776 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1777
1778 ata_qc_free(qc);
1779 done(cmd);
1780}
1781
Tejun Heo77853bf2006-01-23 13:09:36 +09001782static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Tejun Heoc31f5712006-11-22 12:39:43 +09001784 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001786 u8 *cdb = cmd->cmnd;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001787 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Jeff Garzikb0955182005-05-12 15:45:22 -04001789 /* For ATA pass thru (SAT) commands, generate a sense block if
1790 * user mandated it or if there's an error. Note that if we
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001791 * generate because the user forced us to [CK_COND =1], a check
1792 * condition is generated and the ATA register values are returned
Jeff Garzikb0955182005-05-12 15:45:22 -04001793 * whether the command completed successfully or not. If there
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001794 * was no error, we use the following sense data:
1795 * sk = RECOVERED ERROR
1796 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
Jeff Garzikb0955182005-05-12 15:45:22 -04001797 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001798 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001799 ((cdb[2] & 0x20) || need_sense))
Tejun Heo750426a2006-11-14 22:37:35 +09001800 ata_gen_passthru_sense(qc);
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +02001801 else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1802 cmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001803 else if (need_sense)
1804 ata_gen_ata_sense(qc);
1805 else
1806 cmd->result = SAM_STAT_GOOD;
Jeff Garzikb0955182005-05-12 15:45:22 -04001807
Tejun Heoc31f5712006-11-22 12:39:43 +09001808 if (need_sense && !ap->ops->error_handler)
Tejun Heo44877b42007-02-21 01:06:51 +09001809 ata_dump_status(ap->print_id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001811 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
1814/**
1815 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 * @dev: ATA device to which the command is addressed
1817 * @cmd: SCSI command to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1819 *
1820 * Our ->queuecommand() function has decided that the SCSI
1821 * command issued can be directly translated into an ATA
1822 * command, rather than handled internally.
1823 *
1824 * This function sets up an ata_queued_cmd structure for the
1825 * SCSI command, and sends that ata_queued_cmd to the hardware.
1826 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001827 * The xlat_func argument (actor) returns 0 if ready to execute
1828 * ATA command, else 1 to finish translation. If 1 is returned
1829 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1830 * to be set reflecting an error condition or clean (early)
1831 * termination.
1832 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001834 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09001835 *
1836 * RETURNS:
1837 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1838 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 */
Tejun Heo2115ea92006-05-15 21:03:39 +09001840static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09001841 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Tejun Heo31cc23b2007-09-23 13:14:12 +09001843 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 struct ata_queued_cmd *qc;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001845 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 VPRINTK("ENTER\n");
1848
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05001849 qc = ata_scsi_qc_new(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04001851 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05001854 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1855 cmd->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harrosh71201652007-09-18 17:48:50 +02001856 if (unlikely(scsi_bufflen(cmd) < 1)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001857 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001858 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860
Boaz Harrosh71201652007-09-18 17:48:50 +02001861 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 qc->dma_dir = cmd->sc_data_direction;
1864 }
1865
1866 qc->complete_fn = ata_scsi_qc_complete;
1867
Tejun Heoad706992006-12-17 10:45:57 +09001868 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04001869 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Tejun Heo31cc23b2007-09-23 13:14:12 +09001871 if (ap->ops->qc_defer) {
1872 if ((rc = ap->ops->qc_defer(qc)))
1873 goto defer;
1874 }
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09001877 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Douglas Gilbertae006512005-10-09 09:09:35 -04001882early_finish:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001883 ata_qc_free(qc);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05001884 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -04001885 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001886 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001887
1888err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04001890 cmd->result = (DID_ERROR << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05001891 cmd->scsi_done(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05001892err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04001893 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001894 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001895
1896defer:
Tejun Heo31cc23b2007-09-23 13:14:12 +09001897 ata_qc_free(qc);
Tejun Heo3dc1d882006-05-15 21:03:45 +09001898 DPRINTK("EXIT - defer\n");
Tejun Heo31cc23b2007-09-23 13:14:12 +09001899 if (rc == ATA_DEFER_LINK)
1900 return SCSI_MLQUEUE_DEVICE_BUSY;
1901 else
1902 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
1905/**
1906 * ata_scsi_rbuf_get - Map response buffer.
Randy Dunlapec2a20e2008-04-30 12:57:00 -07001907 * @cmd: SCSI command containing buffer to be mapped.
Tejun Heo87340e92008-04-28 17:48:51 +09001908 * @flags: unsigned long variable to store irq enable status
1909 * @copy_in: copy in from user buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 *
Tejun Heo87340e92008-04-28 17:48:51 +09001911 * Prepare buffer for simulated SCSI commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 *
1913 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09001914 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 *
1916 * RETURNS:
Tejun Heo87340e92008-04-28 17:48:51 +09001917 * Pointer to response buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 */
Tejun Heo87340e92008-04-28 17:48:51 +09001919static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
1920 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Tejun Heo87340e92008-04-28 17:48:51 +09001922 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Tejun Heo87340e92008-04-28 17:48:51 +09001924 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
1925 if (copy_in)
1926 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1927 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1928 return ata_scsi_rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
1931/**
1932 * ata_scsi_rbuf_put - Unmap response buffer.
1933 * @cmd: SCSI command containing buffer to be unmapped.
Tejun Heo87340e92008-04-28 17:48:51 +09001934 * @copy_out: copy out result
1935 * @flags: @flags passed to ata_scsi_rbuf_get()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 *
Tejun Heo87340e92008-04-28 17:48:51 +09001937 * Returns rbuf buffer. The result is copied to @cmd's buffer if
1938 * @copy_back is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 *
1940 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09001941 * Unlocks ata_scsi_rbuf_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 */
Tejun Heo87340e92008-04-28 17:48:51 +09001943static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
1944 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Tejun Heo87340e92008-04-28 17:48:51 +09001946 if (copy_out)
1947 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1948 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1949 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
1952/**
1953 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1954 * @args: device IDENTIFY data / SCSI command of interest.
1955 * @actor: Callback hook for desired SCSI command simulator
1956 *
1957 * Takes care of the hard work of simulating a SCSI command...
1958 * Mapping the response buffer, calling the command's handler,
1959 * and handling the handler's return value. This return value
1960 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04001961 * completed successfully (0), or not (in which case cmd->result
1962 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 *
1964 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001965 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 */
Tejun Heof0761be2008-04-28 17:16:52 +09001967static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
Tejun Heo87340e92008-04-28 17:48:51 +09001968 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 u8 *rbuf;
Tejun Heo87340e92008-04-28 17:48:51 +09001971 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 struct scsi_cmnd *cmd = args->cmd;
Jeff Garzikb445c562008-02-29 19:10:51 -05001973 unsigned long flags;
1974
Tejun Heo87340e92008-04-28 17:48:51 +09001975 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
1976 rc = actor(args, rbuf);
1977 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
Jeff Garzikb445c562008-02-29 19:10:51 -05001978
Douglas Gilbertae006512005-10-09 09:09:35 -04001979 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001981 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
1984/**
1985 * ata_scsiop_inq_std - Simulate INQUIRY command
1986 * @args: device IDENTIFY data / SCSI command of interest.
1987 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 *
1989 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05001990 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 *
1992 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001993 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 */
Tejun Heo87340e92008-04-28 17:48:51 +09001995static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Tejun Heo87340e92008-04-28 17:48:51 +09001997 const u8 versions[] = {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01001998 0x00,
Tejun Heo87340e92008-04-28 17:48:51 +09001999 0x60, /* SAM-3 (no version claimed) */
2000
2001 0x03,
2002 0x20, /* SBC-2 (no version claimed) */
2003
2004 0x02,
2005 0x60 /* SPC-3 (no version claimed) */
2006 };
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002007 const u8 versions_zbc[] = {
2008 0x00,
2009 0xA0, /* SAM-5 (no version claimed) */
2010
2011 0x04,
2012 0xC0, /* SBC-3 (no version claimed) */
2013
2014 0x04,
2015 0x60, /* SPC-4 (no version claimed) */
2016
2017 0x60,
2018 0x20, /* ZBC (no version claimed) */
2019 };
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 u8 hdr[] = {
2022 TYPE_DISK,
2023 0,
2024 0x5, /* claim SPC-3 version compatibility */
2025 2,
2026 95 - 4
2027 };
2028
Tejun Heo87340e92008-04-28 17:48:51 +09002029 VPRINTK("ENTER\n");
2030
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02002031 /* set scsi removable (RMB) bit per ata bit, or if the
2032 * AHCI port says it's external (Hotplug-capable, eSATA).
2033 */
2034 if (ata_id_removable(args->id) ||
2035 (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 hdr[1] |= (1 << 7);
2037
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002038 if (args->dev->class == ATA_DEV_ZAC) {
2039 hdr[0] = TYPE_ZBC;
2040 hdr[2] = 0x6; /* ZBC is defined in SPC-4 */
2041 }
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002044 memcpy(&rbuf[8], "ATA ", 8);
2045 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
Keith Buschc49a6bf2014-05-01 11:12:03 -06002046
2047 /* From SAT, use last 2 words from fw rev unless they are spaces */
2048 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
2049 if (strncmp(&rbuf[32], " ", 4) == 0)
2050 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Tejun Heo87340e92008-04-28 17:48:51 +09002052 if (rbuf[32] == 0 || rbuf[32] == ' ')
2053 memcpy(&rbuf[32], "n/a ", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002055 if (args->dev->class == ATA_DEV_ZAC)
2056 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
2057 else
2058 memcpy(rbuf + 58, versions, sizeof(versions));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 return 0;
2061}
2062
2063/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002064 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 * @args: device IDENTIFY data / SCSI command of interest.
2066 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002068 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 *
2070 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002071 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 */
Tejun Heo87340e92008-04-28 17:48:51 +09002073static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 const u8 pages[] = {
2076 0x00, /* page 0x00, this page */
2077 0x80, /* page 0x80, unit serial no page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002078 0x83, /* page 0x83, device ident page */
2079 0x89, /* page 0x89, ata info page */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002080 0xb0, /* page 0xb0, block limits page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002081 0xb1, /* page 0xb1, block device characteristics page */
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002082 0xb2, /* page 0xb2, thin provisioning page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 };
Tejun Heo87340e92008-04-28 17:48:51 +09002084
Jeff Garzikb142eb62006-03-21 20:37:47 -05002085 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
Tejun Heo87340e92008-04-28 17:48:51 +09002086 memcpy(rbuf + 4, pages, sizeof(pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return 0;
2088}
2089
2090/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002091 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 * @args: device IDENTIFY data / SCSI command of interest.
2093 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 *
2095 * Returns ATA device serial number.
2096 *
2097 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002098 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 */
Tejun Heo87340e92008-04-28 17:48:51 +09002100static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 const u8 hdr[] = {
2103 0,
2104 0x80, /* this page code */
2105 0,
Tejun Heoa0cf7332007-01-02 20:18:49 +09002106 ATA_ID_SERNO_LEN, /* page len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 };
Tejun Heo87340e92008-04-28 17:48:51 +09002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002110 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2111 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return 0;
2113}
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002116 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 * @args: device IDENTIFY data / SCSI command of interest.
2118 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002120 * Yields two logical unit device identification designators:
2121 * - vendor specific ASCII containing the ATA serial number
2122 * - SAT defined "t10 vendor id based" containing ASCII vendor
2123 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 *
2125 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002126 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 */
Tejun Heo87340e92008-04-28 17:48:51 +09002128static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Jeff Garzikb142eb62006-03-21 20:37:47 -05002130 const int sat_model_serial_desc_len = 68;
Tejun Heo87340e92008-04-28 17:48:51 +09002131 int num;
Jeff Garzikb142eb62006-03-21 20:37:47 -05002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05002134 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Tejun Heo87340e92008-04-28 17:48:51 +09002136 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2137 rbuf[num + 0] = 2;
2138 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2139 num += 4;
2140 ata_id_string(args->id, (unsigned char *) rbuf + num,
2141 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2142 num += ATA_ID_SERNO_LEN;
2143
2144 /* SAT defined lu model and serial numbers descriptor */
2145 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2146 rbuf[num + 0] = 2;
2147 rbuf[num + 1] = 1;
2148 rbuf[num + 3] = sat_model_serial_desc_len;
2149 num += 4;
2150 memcpy(rbuf + num, "ATA ", 8);
2151 num += 8;
2152 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2153 ATA_ID_PROD_LEN);
2154 num += ATA_ID_PROD_LEN;
2155 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2156 ATA_ID_SERNO_LEN);
2157 num += ATA_ID_SERNO_LEN;
2158
Hannes Reinecke6b3b9d72011-03-07 08:56:44 +01002159 if (ata_id_has_wwn(args->id)) {
2160 /* SAT defined lu world wide name */
2161 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2162 rbuf[num + 0] = 1;
2163 rbuf[num + 1] = 3;
2164 rbuf[num + 3] = ATA_ID_WWN_LEN;
2165 num += 4;
2166 ata_id_string(args->id, (unsigned char *) rbuf + num,
2167 ATA_ID_WWN, ATA_ID_WWN_LEN);
2168 num += ATA_ID_WWN_LEN;
2169 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05002170 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return 0;
2172}
2173
2174/**
Jeff Garzikad355b42007-09-21 20:38:03 -04002175 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2176 * @args: device IDENTIFY data / SCSI command of interest.
2177 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Jeff Garzikad355b42007-09-21 20:38:03 -04002178 *
2179 * Yields SAT-specified ATA VPD page.
2180 *
2181 * LOCKING:
2182 * spin_lock_irqsave(host lock)
2183 */
Tejun Heo87340e92008-04-28 17:48:51 +09002184static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
Jeff Garzikad355b42007-09-21 20:38:03 -04002185{
Jeff Garzikad355b42007-09-21 20:38:03 -04002186 struct ata_taskfile tf;
Jeff Garzikad355b42007-09-21 20:38:03 -04002187
Jeff Garzikad355b42007-09-21 20:38:03 -04002188 memset(&tf, 0, sizeof(tf));
2189
Tejun Heo87340e92008-04-28 17:48:51 +09002190 rbuf[1] = 0x89; /* our page code */
2191 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2192 rbuf[3] = (0x238 & 0xff);
Jeff Garzikad355b42007-09-21 20:38:03 -04002193
Tejun Heo87340e92008-04-28 17:48:51 +09002194 memcpy(&rbuf[8], "linux ", 8);
2195 memcpy(&rbuf[16], "libata ", 16);
2196 memcpy(&rbuf[32], DRV_VERSION, 4);
Jeff Garzikad355b42007-09-21 20:38:03 -04002197
2198 /* we don't store the ATA device signature, so we fake it */
2199
2200 tf.command = ATA_DRDY; /* really, this is Status reg */
2201 tf.lbal = 0x1;
2202 tf.nsect = 0x1;
2203
Tejun Heo87340e92008-04-28 17:48:51 +09002204 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
2205 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
Jeff Garzikad355b42007-09-21 20:38:03 -04002206
Tejun Heo87340e92008-04-28 17:48:51 +09002207 rbuf[56] = ATA_CMD_ID_ATA;
Jeff Garzikad355b42007-09-21 20:38:03 -04002208
Tejun Heo87340e92008-04-28 17:48:51 +09002209 memcpy(&rbuf[60], &args->id[0], 512);
Jeff Garzikad355b42007-09-21 20:38:03 -04002210 return 0;
2211}
2212
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002213static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2214{
Grant Grundler295124d2010-08-17 10:56:53 -07002215 u16 min_io_sectors;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002216
2217 rbuf[1] = 0xb0;
2218 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2219
2220 /*
2221 * Optimal transfer length granularity.
2222 *
2223 * This is always one physical block, but for disks with a smaller
2224 * logical than physical sector size we need to figure out what the
2225 * latter is.
2226 */
Grant Grundler295124d2010-08-17 10:56:53 -07002227 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002228 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2229
2230 /*
2231 * Optimal unmap granularity.
2232 *
2233 * The ATA spec doesn't even know about a granularity or alignment
2234 * for the TRIM command. We can leave away most of the unmap related
2235 * VPD page entries, but we have specifify a granularity to signal
2236 * that we support some form of unmap - in thise case via WRITE SAME
2237 * with the unmap bit set.
2238 */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002239 if (ata_id_has_trim(args->id)) {
Martin K. Petersen5f4e2062011-05-17 22:13:23 -04002240 put_unaligned_be64(65535 * 512 / 8, &rbuf[36]);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002241 put_unaligned_be32(1, &rbuf[28]);
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002242 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002243
2244 return 0;
2245}
2246
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002247static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2248{
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002249 int form_factor = ata_id_form_factor(args->id);
2250 int media_rotation_rate = ata_id_rotation_rate(args->id);
2251
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002252 rbuf[1] = 0xb1;
2253 rbuf[3] = 0x3c;
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002254 rbuf[4] = media_rotation_rate >> 8;
2255 rbuf[5] = media_rotation_rate;
2256 rbuf[7] = form_factor;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002257
2258 return 0;
2259}
2260
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002261static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
2262{
2263 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2264 rbuf[1] = 0xb2;
2265 rbuf[3] = 0x4;
2266 rbuf[5] = 1 << 6; /* TPWS */
2267
2268 return 0;
2269}
2270
Jeff Garzikad355b42007-09-21 20:38:03 -04002271/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002272 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 * @args: device IDENTIFY data / SCSI command of interest.
2274 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 *
2276 * No operation. Simply returns success to caller, to indicate
2277 * that the caller should successfully complete this SCSI command.
2278 *
2279 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002280 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 */
Tejun Heo87340e92008-04-28 17:48:51 +09002282static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
2284 VPRINTK("ENTER\n");
2285 return 0;
2286}
2287
2288/**
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002289 * modecpy - Prepare response for MODE SENSE
2290 * @dest: output buffer
2291 * @src: data being copied
2292 * @n: length of mode page
2293 * @changeable: whether changeable parameters are requested
2294 *
2295 * Generate a generic MODE SENSE page for either current or changeable
2296 * parameters.
2297 *
2298 * LOCKING:
2299 * None.
2300 */
2301static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2302{
2303 if (changeable) {
2304 memcpy(dest, src, 2);
2305 memset(dest + 2, 0, n - 2);
2306 } else {
2307 memcpy(dest, src, n);
2308 }
2309}
2310
2311/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 * ata_msense_caching - Simulate MODE SENSE caching info page
2313 * @id: device IDENTIFY data
Tejun Heo87340e92008-04-28 17:48:51 +09002314 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002315 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 *
2317 * Generate a caching info page, which conditionally indicates
2318 * write caching to the SCSI layer, depending on device
2319 * capabilities.
2320 *
2321 * LOCKING:
2322 * None.
2323 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002324static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002326 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02002327 if (changeable || ata_id_wcache_enabled(id))
Tejun Heo87340e92008-04-28 17:48:51 +09002328 buf[2] |= (1 << 2); /* write cache enable */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002329 if (!changeable && !ata_id_rahead_enabled(id))
Tejun Heo87340e92008-04-28 17:48:51 +09002330 buf[12] |= (1 << 5); /* disable read ahead */
2331 return sizeof(def_cache_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
2334/**
2335 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
Tejun Heo87340e92008-04-28 17:48:51 +09002336 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002337 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 *
2339 * Generate a generic MODE SENSE control mode page.
2340 *
2341 * LOCKING:
2342 * None.
2343 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002344static unsigned int ata_msense_ctl_mode(u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002346 modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002347 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
2350/**
2351 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002352 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002353 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 *
2355 * Generate a generic MODE SENSE r/w error recovery page.
2356 *
2357 * LOCKING:
2358 * None.
2359 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002360static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002362 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2363 changeable);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002364 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002367/*
2368 * We can turn this into a real blacklist if it's needed, for now just
2369 * blacklist any Maxtor BANC1G10 revision firmware
2370 */
2371static int ata_dev_supports_fua(u16 *id)
2372{
Tejun Heoa0cf7332007-01-02 20:18:49 +09002373 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002374
Jeff Garzikc3c013a2006-02-27 22:31:19 -05002375 if (!libata_fua)
2376 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002377 if (!ata_id_has_fua(id))
2378 return 0;
2379
Tejun Heoa0cf7332007-01-02 20:18:49 +09002380 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2381 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002382
Tejun Heo2e026712006-02-12 22:47:04 +09002383 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002384 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09002385 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002386 return 1;
2387
2388 return 0; /* blacklisted */
2389}
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391/**
2392 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2393 * @args: device IDENTIFY data / SCSI command of interest.
2394 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002396 * Simulate MODE SENSE commands. Assume this is invoked for direct
2397 * access devices (e.g. disks) only. There should be no block
2398 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 *
2400 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002401 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 */
Tejun Heo87340e92008-04-28 17:48:51 +09002403static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002405 struct ata_device *dev = args->dev;
Tejun Heo87340e92008-04-28 17:48:51 +09002406 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002407 const u8 sat_blk_desc[] = {
2408 0, 0, 0, 0, /* number of blocks: sat unspecified */
2409 0,
2410 0, 0x2, 0x0 /* block length: 512 bytes */
2411 };
2412 u8 pg, spg;
Tejun Heo87340e92008-04-28 17:48:51 +09002413 unsigned int ebd, page_control, six_byte;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002414 u8 dpofua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 VPRINTK("ENTER\n");
2417
2418 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002419 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2420 /*
2421 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002425 switch (page_control) {
2426 case 0: /* current */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002427 case 1: /* changeable */
2428 case 2: /* defaults */
Douglas Gilbertae006512005-10-09 09:09:35 -04002429 break; /* supported */
2430 case 3: /* saved */
2431 goto saving_not_supp;
Douglas Gilbertae006512005-10-09 09:09:35 -04002432 default:
2433 goto invalid_fld;
2434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Tejun Heo87340e92008-04-28 17:48:51 +09002436 if (six_byte)
2437 p += 4 + (ebd ? 8 : 0);
2438 else
2439 p += 8 + (ebd ? 8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002441 pg = scsicmd[2] & 0x3f;
2442 spg = scsicmd[3];
2443 /*
2444 * No mode subpages supported (yet) but asking for _all_
2445 * subpages may be valid
2446 */
2447 if (spg && (spg != ALL_SUB_MPAGES))
2448 goto invalid_fld;
2449
2450 switch(pg) {
2451 case RW_RECOVERY_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002452 p += ata_msense_rw_recovery(p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 break;
2454
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002455 case CACHE_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002456 p += ata_msense_caching(args->id, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 break;
2458
Tejun Heo87340e92008-04-28 17:48:51 +09002459 case CONTROL_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002460 p += ata_msense_ctl_mode(p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002463 case ALL_MPAGES:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002464 p += ata_msense_rw_recovery(p, page_control == 1);
2465 p += ata_msense_caching(args->id, p, page_control == 1);
2466 p += ata_msense_ctl_mode(p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 break;
2468
2469 default: /* invalid page code */
Douglas Gilbertae006512005-10-09 09:09:35 -04002470 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002473 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002474 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002475 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2476 dpofua = 1 << 4;
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (six_byte) {
Tejun Heo87340e92008-04-28 17:48:51 +09002479 rbuf[0] = p - rbuf - 1;
2480 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002481 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002482 rbuf[3] = sizeof(sat_blk_desc);
2483 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 } else {
Tejun Heo87340e92008-04-28 17:48:51 +09002486 unsigned int output_len = p - rbuf - 2;
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 rbuf[0] = output_len >> 8;
Tejun Heo87340e92008-04-28 17:48:51 +09002489 rbuf[1] = output_len;
2490 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002491 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002492 rbuf[7] = sizeof(sat_blk_desc);
2493 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002497
2498invalid_fld:
2499 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
2500 /* "Invalid field in cbd" */
2501 return 1;
2502
2503saving_not_supp:
2504 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2505 /* "Saving parameters not supported" */
2506 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
2509/**
2510 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2511 * @args: device IDENTIFY data / SCSI command of interest.
2512 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 *
2514 * Simulate READ CAPACITY commands.
2515 *
2516 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002517 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 */
Tejun Heo87340e92008-04-28 17:48:51 +09002519static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002521 struct ata_device *dev = args->dev;
2522 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
Grant Grundler295124d2010-08-17 10:56:53 -07002523 u32 sector_size; /* physical sector size in bytes */
2524 u8 log2_per_phys;
2525 u16 lowest_aligned;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002526
Grant Grundler295124d2010-08-17 10:56:53 -07002527 sector_size = ata_id_logical_sector_size(dev->id);
2528 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2529 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 VPRINTK("ENTER\n");
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002534 if (last_lba >= 0xffffffffULL)
2535 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /* sector count, 32-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002538 rbuf[0] = last_lba >> (8 * 3);
2539 rbuf[1] = last_lba >> (8 * 2);
2540 rbuf[2] = last_lba >> (8 * 1);
2541 rbuf[3] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002544 rbuf[4] = sector_size >> (8 * 3);
2545 rbuf[5] = sector_size >> (8 * 2);
2546 rbuf[6] = sector_size >> (8 * 1);
2547 rbuf[7] = sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 } else {
2549 /* sector count, 64-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002550 rbuf[0] = last_lba >> (8 * 7);
2551 rbuf[1] = last_lba >> (8 * 6);
2552 rbuf[2] = last_lba >> (8 * 5);
2553 rbuf[3] = last_lba >> (8 * 4);
2554 rbuf[4] = last_lba >> (8 * 3);
2555 rbuf[5] = last_lba >> (8 * 2);
2556 rbuf[6] = last_lba >> (8 * 1);
2557 rbuf[7] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002560 rbuf[ 8] = sector_size >> (8 * 3);
2561 rbuf[ 9] = sector_size >> (8 * 2);
2562 rbuf[10] = sector_size >> (8 * 1);
2563 rbuf[11] = sector_size;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002564
2565 rbuf[12] = 0;
Grant Grundler295124d2010-08-17 10:56:53 -07002566 rbuf[13] = log2_per_phys;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002567 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2568 rbuf[15] = lowest_aligned;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002569
Arne Fitzenreiter71d126f2015-07-15 13:54:36 +02002570 if (ata_id_has_trim(args->id) &&
2571 !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002572 rbuf[14] |= 0x80; /* LBPME */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002573
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002574 if (ata_id_has_zero_after_trim(args->id) &&
2575 dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
2576 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2577 rbuf[14] |= 0x40; /* LBPRZ */
2578 }
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return 0;
2582}
2583
2584/**
2585 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2586 * @args: device IDENTIFY data / SCSI command of interest.
2587 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 *
2589 * Simulate REPORT LUNS command.
2590 *
2591 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002592 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 */
Tejun Heo87340e92008-04-28 17:48:51 +09002594static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
2596 VPRINTK("ENTER\n");
2597 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2598
2599 return 0;
2600}
2601
Tejun Heo77853bf2006-01-23 13:09:36 +09002602static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002603{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002604 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002605 /* FIXME: not quite right; we don't want the
2606 * translation of taskfile registers into
2607 * a sense descriptors, since that's only
2608 * correct for ATA, not ATAPI
2609 */
Tejun Heo750426a2006-11-14 22:37:35 +09002610 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002611 }
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002612
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002613 ata_qc_done(qc);
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002614}
2615
2616/* is it pointless to prefer PIO for "safety reasons"? */
2617static inline int ata_pio_use_silly(struct ata_port *ap)
2618{
2619 return (ap->flags & ATA_FLAG_PIO_DMA);
2620}
2621
2622static void atapi_request_sense(struct ata_queued_cmd *qc)
2623{
2624 struct ata_port *ap = qc->ap;
2625 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002626
2627 DPRINTK("ATAPI request sense\n");
2628
James Bottomley7ccd7202008-01-17 11:56:24 -06002629 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Jeff Garzika939c962005-10-05 17:09:16 -04002630
Tejun Heo127102a2008-04-07 22:47:21 +09002631#ifdef CONFIG_ATA_SFF
James Bottomley855d8542008-04-18 13:18:48 -05002632 if (ap->ops->sff_tf_read)
2633 ap->ops->sff_tf_read(ap, &qc->tf);
Tejun Heo127102a2008-04-07 22:47:21 +09002634#endif
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002635
2636 /* fill these in, for the case where they are -not- overwritten */
2637 cmd->sense_buffer[0] = 0x70;
2638 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2639
2640 ata_qc_reinit(qc);
2641
Tejun Heo93f8fec2007-12-05 16:43:01 +09002642 /* setup sg table and init transfer direction */
akpm@linux-foundation.orgcadb7342008-01-15 16:01:52 -08002643 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
Tejun Heo93f8fec2007-12-05 16:43:01 +09002644 ata_sg_init(qc, &qc->sgent, 1);
Jeff Garzika939c962005-10-05 17:09:16 -04002645 qc->dma_dir = DMA_FROM_DEVICE;
2646
Tejun Heo6e7846e2006-02-12 23:32:58 +09002647 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002648 qc->cdb[0] = REQUEST_SENSE;
2649 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2650
2651 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2652 qc->tf.command = ATA_CMD_PACKET;
2653
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002654 if (ata_pio_use_silly(ap)) {
Tejun Heo0dc36882007-12-18 16:34:43 -05002655 qc->tf.protocol = ATAPI_PROT_DMA;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002656 qc->tf.feature |= ATAPI_PKT_DMA;
2657 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05002658 qc->tf.protocol = ATAPI_PROT_PIO;
Alan Cox2db78dd2007-10-02 13:53:04 -07002659 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2660 qc->tf.lbah = 0;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002661 }
Jeff Garzika939c962005-10-05 17:09:16 -04002662 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2663
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002664 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002665
Tejun Heo8e0e6942006-03-31 20:41:11 +09002666 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002667
2668 DPRINTK("EXIT\n");
2669}
2670
Tejun Heo77853bf2006-01-23 13:09:36 +09002671static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002674 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Jeff Garzika7dac442005-10-30 04:44:42 -05002676 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002677
Tejun Heo246619d2006-05-15 20:58:16 +09002678 /* handle completion from new EH */
2679 if (unlikely(qc->ap->ops->error_handler &&
2680 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2681
2682 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2683 /* FIXME: not quite right; we don't want the
2684 * translation of taskfile registers into a
2685 * sense descriptors, since that's only
2686 * correct for ATA, not ATAPI
2687 */
Tejun Heo750426a2006-11-14 22:37:35 +09002688 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002689 }
2690
Tejun Heo22aac082006-08-08 14:08:59 +09002691 /* SCSI EH automatically locks door if sdev->locked is
2692 * set. Sometimes door lock request continues to
2693 * fail, for example, when no media is present. This
2694 * creates a loop - SCSI EH issues door lock which
2695 * fails and gets invoked again to acquire sense data
2696 * for the failed command.
2697 *
2698 * If door lock fails, always clear sdev->locked to
2699 * avoid this infinite loop.
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002700 *
2701 * This may happen before SCSI scan is complete. Make
2702 * sure qc->dev->sdev isn't NULL before dereferencing.
Tejun Heo22aac082006-08-08 14:08:59 +09002703 */
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002704 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
Tejun Heo22aac082006-08-08 14:08:59 +09002705 qc->dev->sdev->locked = 0;
2706
Tejun Heo246619d2006-05-15 20:58:16 +09002707 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002708 ata_qc_done(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002709 return;
2710 }
2711
2712 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002713 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002715 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002716 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002717 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002718 /* FIXME: not quite right; we don't want the
2719 * translation of taskfile registers into
2720 * a sense descriptors, since that's only
2721 * correct for ATA, not ATAPI
2722 */
Tejun Heo750426a2006-11-14 22:37:35 +09002723 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002724 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 u8 *scsicmd = cmd->cmnd;
2726
Tony Battersbyfd71da42005-12-21 16:35:44 -05002727 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Jeff Garzikb445c562008-02-29 19:10:51 -05002728 unsigned long flags;
Tejun Heo87340e92008-04-28 17:48:51 +09002729 u8 *buf;
Jeff Garzikb445c562008-02-29 19:10:51 -05002730
Tejun Heo87340e92008-04-28 17:48:51 +09002731 buf = ata_scsi_rbuf_get(cmd, true, &flags);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002732
2733 /* ATAPI devices typically report zero for their SCSI version,
2734 * and sometimes deviate from the spec WRT response data
2735 * format. If SCSI version is reported as zero like normal,
2736 * then we make the following fixups: 1) Fake MMC-5 version,
2737 * to indicate to the Linux scsi midlayer this is a modern
2738 * device. 2) Ensure response data format / ATAPI information
2739 * are always correct.
2740 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002741 if (buf[2] == 0) {
2742 buf[2] = 0x5;
2743 buf[3] = 0x32;
2744 }
2745
Tejun Heo87340e92008-04-28 17:48:51 +09002746 ata_scsi_rbuf_put(cmd, true, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 cmd->result = SAM_STAT_GOOD;
2750 }
2751
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002752 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754/**
2755 * atapi_xlat - Initialize PACKET taskfile
2756 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 *
2758 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002759 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 *
2761 * RETURNS:
2762 * Zero on success, non-zero on failure.
2763 */
Tejun Heoad706992006-12-17 10:45:57 +09002764static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Tejun Heo542b1442006-12-17 10:45:08 +09002766 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 struct ata_device *dev = qc->dev;
Tejun Heo542b1442006-12-17 10:45:08 +09002768 int nodata = (scmd->sc_data_direction == DMA_NONE);
Tejun Heo5895ef92008-06-17 12:36:26 +09002769 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
Alan Cox2db78dd2007-10-02 13:53:04 -07002770 unsigned int nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Tejun Heo2e5704f2006-12-17 10:46:33 +09002772 memset(qc->cdb, 0, dev->cdb_len);
2773 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 qc->complete_fn = atapi_qc_complete;
2776
2777 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002778 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 qc->tf.flags |= ATA_TFLAG_WRITE;
2780 DPRINTK("direction: write\n");
2781 }
2782
2783 qc->tf.command = ATA_CMD_PACKET;
Tejun Heoaacda372008-03-18 17:47:43 +09002784 ata_qc_set_pc_nbytes(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002786 /* check whether ATAPI DMA is safe */
Tejun Heo5895ef92008-06-17 12:36:26 +09002787 if (!nodata && !using_pio && atapi_check_dma(qc))
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002788 using_pio = 1;
2789
Tejun Heoe1902222007-11-26 20:58:02 +09002790 /* Some controller variants snoop this value for Packet
2791 * transfers to do state machine and FIFO management. Thus we
2792 * want to set it properly, and for DMA where it is
2793 * effectively meaningless.
2794 */
Tejun Heoaacda372008-03-18 17:47:43 +09002795 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
Alan Cox2db78dd2007-10-02 13:53:04 -07002796
Tejun Heoe1902222007-11-26 20:58:02 +09002797 /* Most ATAPI devices which honor transfer chunk size don't
2798 * behave according to the spec when odd chunk size which
2799 * matches the transfer length is specified. If the number of
2800 * bytes to transfer is 2n+1. According to the spec, what
2801 * should happen is to indicate that 2n+1 is going to be
2802 * transferred and transfer 2n+2 bytes where the last byte is
2803 * padding.
2804 *
2805 * In practice, this doesn't happen. ATAPI devices first
2806 * indicate and transfer 2n bytes and then indicate and
2807 * transfer 2 bytes where the last byte is padding.
2808 *
2809 * This inconsistency confuses several controllers which
2810 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2811 * These controllers use actual number of transferred bytes to
2812 * update DMA poitner and transfer of 4n+2 bytes make those
2813 * controller push DMA pointer by 4n+4 bytes because SATA data
2814 * FISes are aligned to 4 bytes. This causes data corruption
2815 * and buffer overrun.
2816 *
2817 * Always setting nbytes to even number solves this problem
2818 * because then ATAPI devices don't have to split data at 2n
2819 * boundaries.
2820 */
2821 if (nbytes & 0x1)
2822 nbytes++;
2823
Alan Cox2db78dd2007-10-02 13:53:04 -07002824 qc->tf.lbam = (nbytes & 0xFF);
2825 qc->tf.lbah = (nbytes >> 8);
2826
Tejun Heo5895ef92008-06-17 12:36:26 +09002827 if (nodata)
2828 qc->tf.protocol = ATAPI_PROT_NODATA;
2829 else if (using_pio)
2830 qc->tf.protocol = ATAPI_PROT_PIO;
2831 else {
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002832 /* DMA data xfer */
Tejun Heo0dc36882007-12-18 16:34:43 -05002833 qc->tf.protocol = ATAPI_PROT_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 qc->tf.feature |= ATAPI_PKT_DMA;
2835
Tejun Heo91163002008-02-21 13:25:50 +09002836 if ((dev->flags & ATA_DFLAG_DMADIR) &&
2837 (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08002838 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
2841
Alan Cox2db78dd2007-10-02 13:53:04 -07002842
2843 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2844 as ATAPI tape drives don't get this right otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return 0;
2846}
2847
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002848static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
Tejun Heoab5b3a52006-05-31 18:27:34 +09002849{
Tejun Heo071f44b2008-04-07 22:47:22 +09002850 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09002851 if (likely(devno < ata_link_max_devices(&ap->link)))
2852 return &ap->link.device[devno];
2853 } else {
2854 if (likely(devno < ap->nr_pmp_links))
2855 return &ap->pmp_link[devno].device[0];
2856 }
2857
Tejun Heoab5b3a52006-05-31 18:27:34 +09002858 return NULL;
2859}
2860
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002861static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
2862 const struct scsi_device *scsidev)
Tejun Heoab5b3a52006-05-31 18:27:34 +09002863{
Tejun Heo41bda9c2007-08-06 18:36:24 +09002864 int devno;
Tejun Heoab5b3a52006-05-31 18:27:34 +09002865
Tejun Heo41bda9c2007-08-06 18:36:24 +09002866 /* skip commands not addressed to targets we simulate */
Tejun Heo071f44b2008-04-07 22:47:22 +09002867 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09002868 if (unlikely(scsidev->channel || scsidev->lun))
2869 return NULL;
2870 devno = scsidev->id;
2871 } else {
2872 if (unlikely(scsidev->id || scsidev->lun))
2873 return NULL;
2874 devno = scsidev->channel;
2875 }
2876
2877 return ata_find_dev(ap, devno);
Tejun Heoab5b3a52006-05-31 18:27:34 +09002878}
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880/**
2881 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2882 * @ap: ATA port to which the device is attached
2883 * @scsidev: SCSI device from which we derive the ATA device
2884 *
2885 * Given various information provided in struct scsi_cmnd,
2886 * map that onto an ATA bus, and using that mapping
2887 * determine which ata_device is associated with the
2888 * SCSI command to be sent.
2889 *
2890 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002891 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 *
2893 * RETURNS:
2894 * Associated ATA device, or %NULL if not found.
2895 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04002897ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
Tejun Heoab5b3a52006-05-31 18:27:34 +09002899 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Tejun Heo2486fa52008-07-31 07:52:40 +09002901 if (unlikely(!dev || !ata_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return NULL;
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return dev;
2905}
2906
Jeff Garzikb0955182005-05-12 15:45:22 -04002907/*
2908 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2909 * @byte1: Byte 1 from pass-thru CDB.
2910 *
2911 * RETURNS:
2912 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2913 */
2914static u8
2915ata_scsi_map_proto(u8 byte1)
2916{
2917 switch((byte1 & 0x1e) >> 1) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002918 case 3: /* Non-data */
2919 return ATA_PROT_NODATA;
Jeff Garzikb0955182005-05-12 15:45:22 -04002920
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002921 case 6: /* DMA */
2922 case 10: /* UDMA Data-in */
2923 case 11: /* UDMA Data-Out */
2924 return ATA_PROT_DMA;
Jeff Garzikb0955182005-05-12 15:45:22 -04002925
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002926 case 4: /* PIO Data-in */
2927 case 5: /* PIO Data-out */
2928 return ATA_PROT_PIO;
Jeff Garzikb0955182005-05-12 15:45:22 -04002929
Vinayak Kaleee7fb332015-10-27 11:19:15 +05302930 case 12: /* FPDMA */
2931 return ATA_PROT_NCQ;
2932
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002933 case 0: /* Hard Reset */
2934 case 1: /* SRST */
2935 case 8: /* Device Diagnostic */
2936 case 9: /* Device Reset */
2937 case 7: /* DMA Queued */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002938 case 15: /* Return Response Info */
2939 default: /* Reserved */
2940 break;
Jeff Garzikb0955182005-05-12 15:45:22 -04002941 }
2942
2943 return ATA_PROT_UNKNOWN;
2944}
2945
2946/**
2947 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2948 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04002949 *
2950 * Handles either 12 or 16-byte versions of the CDB.
2951 *
2952 * RETURNS:
2953 * Zero on success, non-zero on failure.
2954 */
Tejun Heoad706992006-12-17 10:45:57 +09002955static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04002956{
2957 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09002958 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01002959 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09002960 const u8 *cdb = scmd->cmnd;
Jeff Garzikb0955182005-05-12 15:45:22 -04002961
Tejun Heo542b1442006-12-17 10:45:08 +09002962 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
Tejun Heo9a405252005-12-02 11:49:11 +09002963 goto invalid_fld;
Jeff Garzik8190bdb2006-05-24 01:53:39 -04002964
Vinayak Kale5f8e7f12015-10-27 13:11:46 +05302965 /* enable LBA */
2966 tf->flags |= ATA_TFLAG_LBA;
2967
Alan Coxae8d4ee2007-11-04 22:05:49 -05002968 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04002969 * 12 and 16 byte CDBs use different offsets to
2970 * provide the various register values.
2971 */
Tejun Heo542b1442006-12-17 10:45:08 +09002972 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04002973 /*
2974 * 16-byte CDB - may contain extended commands.
2975 *
2976 * If that is the case, copy the upper byte register values.
2977 */
Tejun Heo542b1442006-12-17 10:45:08 +09002978 if (cdb[1] & 0x01) {
2979 tf->hob_feature = cdb[3];
2980 tf->hob_nsect = cdb[5];
2981 tf->hob_lbal = cdb[7];
2982 tf->hob_lbam = cdb[9];
2983 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04002984 tf->flags |= ATA_TFLAG_LBA48;
2985 } else
2986 tf->flags &= ~ATA_TFLAG_LBA48;
2987
2988 /*
2989 * Always copy low byte, device and command registers.
2990 */
Tejun Heo542b1442006-12-17 10:45:08 +09002991 tf->feature = cdb[4];
2992 tf->nsect = cdb[6];
2993 tf->lbal = cdb[8];
2994 tf->lbam = cdb[10];
2995 tf->lbah = cdb[12];
2996 tf->device = cdb[13];
2997 tf->command = cdb[14];
Jeff Garzikb0955182005-05-12 15:45:22 -04002998 } else {
2999 /*
3000 * 12-byte CDB - incapable of extended commands.
3001 */
3002 tf->flags &= ~ATA_TFLAG_LBA48;
3003
Tejun Heo542b1442006-12-17 10:45:08 +09003004 tf->feature = cdb[3];
3005 tf->nsect = cdb[4];
3006 tf->lbal = cdb[5];
3007 tf->lbam = cdb[6];
3008 tf->lbah = cdb[7];
3009 tf->device = cdb[8];
3010 tf->command = cdb[9];
Jeff Garzikb0955182005-05-12 15:45:22 -04003011 }
Albert Leefa4453c2007-06-07 15:52:07 +08003012
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303013 /* For NCQ commands with FPDMA protocol, copy the tag value */
3014 if (tf->protocol == ATA_PROT_NCQ)
3015 tf->nsect = qc->tag << 3;
3016
Albert Leefa4453c2007-06-07 15:52:07 +08003017 /* enforce correct master/slave bit */
3018 tf->device = dev->devno ?
3019 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003020
Tejun Heobd30add2009-09-03 16:08:11 +09003021 switch (tf->command) {
Grant Grundler295124d2010-08-17 10:56:53 -07003022 /* READ/WRITE LONG use a non-standard sect_size */
Tejun Heobd30add2009-09-03 16:08:11 +09003023 case ATA_CMD_READ_LONG:
3024 case ATA_CMD_READ_LONG_ONCE:
3025 case ATA_CMD_WRITE_LONG:
3026 case ATA_CMD_WRITE_LONG_ONCE:
3027 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
3028 goto invalid_fld;
3029 qc->sect_size = scsi_bufflen(scmd);
Grant Grundler295124d2010-08-17 10:56:53 -07003030 break;
3031
3032 /* commands using reported Logical Block size (e.g. 512 or 4K) */
3033 case ATA_CMD_CFA_WRITE_NE:
3034 case ATA_CMD_CFA_TRANS_SECT:
3035 case ATA_CMD_CFA_WRITE_MULT_NE:
3036 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
3037 case ATA_CMD_READ:
3038 case ATA_CMD_READ_EXT:
3039 case ATA_CMD_READ_QUEUED:
3040 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
3041 case ATA_CMD_FPDMA_READ:
3042 case ATA_CMD_READ_MULTI:
3043 case ATA_CMD_READ_MULTI_EXT:
3044 case ATA_CMD_PIO_READ:
3045 case ATA_CMD_PIO_READ_EXT:
3046 case ATA_CMD_READ_STREAM_DMA_EXT:
3047 case ATA_CMD_READ_STREAM_EXT:
3048 case ATA_CMD_VERIFY:
3049 case ATA_CMD_VERIFY_EXT:
3050 case ATA_CMD_WRITE:
3051 case ATA_CMD_WRITE_EXT:
3052 case ATA_CMD_WRITE_FUA_EXT:
3053 case ATA_CMD_WRITE_QUEUED:
3054 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3055 case ATA_CMD_FPDMA_WRITE:
3056 case ATA_CMD_WRITE_MULTI:
3057 case ATA_CMD_WRITE_MULTI_EXT:
3058 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3059 case ATA_CMD_PIO_WRITE:
3060 case ATA_CMD_PIO_WRITE_EXT:
3061 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3062 case ATA_CMD_WRITE_STREAM_EXT:
3063 qc->sect_size = scmd->device->sector_size;
3064 break;
3065
3066 /* Everything else uses 512 byte "sectors" */
3067 default:
3068 qc->sect_size = ATA_SECT_SIZE;
Tejun Heobd30add2009-09-03 16:08:11 +09003069 }
3070
3071 /*
3072 * Set flags so that all registers will be written, pass on
3073 * write indication (used for PIO/DMA setup), result TF is
3074 * copied back and we don't whine too much about its failure.
3075 */
Douglas Gilbertbc496ed2010-02-01 13:11:38 -05003076 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heobd30add2009-09-03 16:08:11 +09003077 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3078 tf->flags |= ATA_TFLAG_WRITE;
3079
3080 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3081
3082 /*
3083 * Set transfer length.
3084 *
3085 * TODO: find out if we need to do more here to
3086 * cover scatter/gather case.
3087 */
3088 ata_qc_set_pc_nbytes(qc);
3089
3090 /* We may not issue DMA commands if no DMA mode is set */
3091 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
3092 goto invalid_fld;
3093
Albert Lee1dce5892007-06-07 15:49:22 +08003094 /* sanity check for pio multi commands */
3095 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
3096 goto invalid_fld;
3097
3098 if (is_multi_taskfile(tf)) {
3099 unsigned int multi_count = 1 << (cdb[1] >> 5);
3100
3101 /* compare the passed through multi_count
3102 * with the cached multi_count of libata
3103 */
3104 if (multi_count != dev->multi_count)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003105 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3106 multi_count);
Alan Coxd26fc952007-07-06 19:13:52 -04003107 }
Albert Lee1dce5892007-06-07 15:49:22 +08003108
Jeff Garzikb0955182005-05-12 15:45:22 -04003109 /*
3110 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3111 * SET_FEATURES - XFER MODE must be preceded/succeeded
3112 * by an update to hardware-specific registers for each
3113 * controller (i.e. the reason for ->set_piomode(),
3114 * ->set_dmamode(), and ->post_set_mode() hooks).
3115 */
Tejun Heobd30add2009-09-03 16:08:11 +09003116 if (tf->command == ATA_CMD_SET_FEATURES &&
3117 tf->feature == SETFEATURES_XFER)
Tejun Heo9a405252005-12-02 11:49:11 +09003118 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04003119
3120 /*
Tejun Heobd30add2009-09-03 16:08:11 +09003121 * Filter TPM commands by default. These provide an
3122 * essentially uncontrolled encrypted "back door" between
3123 * applications and the disk. Set libata.allow_tpm=1 if you
3124 * have a real reason for wanting to use them. This ensures
3125 * that installed software cannot easily mess stuff up without
3126 * user intent. DVR type users will probably ship with this enabled
3127 * for movie content management.
Jeff Garzikb0955182005-05-12 15:45:22 -04003128 *
Tejun Heobd30add2009-09-03 16:08:11 +09003129 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3130 * for this and should do in future but that it is not sufficient as
3131 * DCS is an optional feature set. Thus we also do the software filter
3132 * so that we comply with the TC consortium stated goal that the user
3133 * can turn off TC features of their system.
Jeff Garzikb0955182005-05-12 15:45:22 -04003134 */
Tejun Heobd30add2009-09-03 16:08:11 +09003135 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
3136 goto invalid_fld;
Tejun Heoe61e0672006-05-15 20:57:40 +09003137
Jeff Garzikb0955182005-05-12 15:45:22 -04003138 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09003139
3140 invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09003141 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
Tejun Heo9a405252005-12-02 11:49:11 +09003142 /* "Invalid field in cdb" */
3143 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003144}
3145
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003146static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3147{
3148 struct ata_taskfile *tf = &qc->tf;
3149 struct scsi_cmnd *scmd = qc->scsicmd;
3150 struct ata_device *dev = qc->dev;
3151 const u8 *cdb = scmd->cmnd;
3152 u64 block;
3153 u32 n_block;
3154 u32 size;
3155 void *buf;
3156
3157 /* we may not issue DMA commands if no DMA mode is set */
3158 if (unlikely(!dev->dma_mode))
3159 goto invalid_fld;
3160
3161 if (unlikely(scmd->cmd_len < 16))
3162 goto invalid_fld;
3163 scsi_16_lba_len(cdb, &block, &n_block);
3164
3165 /* for now we only support WRITE SAME with the unmap bit set */
3166 if (unlikely(!(cdb[1] & 0x8)))
3167 goto invalid_fld;
3168
3169 /*
3170 * WRITE SAME always has a sector sized buffer as payload, this
3171 * should never be a multiple entry S/G list.
3172 */
3173 if (!scsi_sg_count(scmd))
3174 goto invalid_fld;
3175
3176 buf = page_address(sg_page(scsi_sglist(scmd)));
Martin K. Petersend0634c42009-11-26 12:00:43 -05003177 size = ata_set_lba_range_entries(buf, 512, block, n_block);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003178
Marc Carino87fb6c32013-08-24 23:22:51 -07003179 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3180 /* Newer devices support queued TRIM commands */
3181 tf->protocol = ATA_PROT_NCQ;
3182 tf->command = ATA_CMD_FPDMA_SEND;
3183 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
3184 tf->nsect = qc->tag << 3;
3185 tf->hob_feature = (size / 512) >> 8;
3186 tf->feature = size / 512;
3187
3188 tf->auxiliary = 1;
3189 } else {
3190 tf->protocol = ATA_PROT_DMA;
3191 tf->hob_feature = 0;
3192 tf->feature = ATA_DSM_TRIM;
3193 tf->hob_nsect = (size / 512) >> 8;
3194 tf->nsect = size / 512;
3195 tf->command = ATA_CMD_DSM;
3196 }
3197
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003198 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3199 ATA_TFLAG_WRITE;
3200
3201 ata_qc_set_pc_nbytes(qc);
3202
3203 return 0;
3204
3205 invalid_fld:
3206 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
3207 /* "Invalid field in cdb" */
3208 return 1;
3209}
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003212 * ata_mselect_caching - Simulate MODE SELECT for caching info page
3213 * @qc: Storage for translated ATA taskfile
3214 * @buf: input buffer
3215 * @len: number of valid bytes in the input buffer
3216 *
3217 * Prepare a taskfile to modify caching information for the device.
3218 *
3219 * LOCKING:
3220 * None.
3221 */
3222static int ata_mselect_caching(struct ata_queued_cmd *qc,
3223 const u8 *buf, int len)
3224{
3225 struct ata_taskfile *tf = &qc->tf;
3226 struct ata_device *dev = qc->dev;
3227 char mpage[CACHE_MPAGE_LEN];
3228 u8 wce;
3229
3230 /*
3231 * The first two bytes of def_cache_mpage are a header, so offsets
3232 * in mpage are off by 2 compared to buf. Same for len.
3233 */
3234
3235 if (len != CACHE_MPAGE_LEN - 2)
3236 return -EINVAL;
3237
3238 wce = buf[0] & (1 << 2);
3239
3240 /*
3241 * Check that read-only bits are not modified.
3242 */
3243 ata_msense_caching(dev->id, mpage, false);
3244 mpage[2] &= ~(1 << 2);
3245 mpage[2] |= wce;
3246 if (memcmp(mpage + 2, buf, CACHE_MPAGE_LEN - 2) != 0)
3247 return -EINVAL;
3248
3249 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3250 tf->protocol = ATA_PROT_NODATA;
3251 tf->nsect = 0;
3252 tf->command = ATA_CMD_SET_FEATURES;
3253 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3254 return 0;
3255}
3256
3257/**
3258 * ata_scsiop_mode_select - Simulate MODE SELECT 6, 10 commands
3259 * @qc: Storage for translated ATA taskfile
3260 *
3261 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
3262 * Assume this is invoked for direct access devices (e.g. disks) only.
3263 * There should be no block descriptor for other device types.
3264 *
3265 * LOCKING:
3266 * spin_lock_irqsave(host lock)
3267 */
3268static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
3269{
3270 struct scsi_cmnd *scmd = qc->scsicmd;
3271 const u8 *cdb = scmd->cmnd;
3272 const u8 *p;
3273 u8 pg, spg;
3274 unsigned six_byte, pg_len, hdr_len, bd_len;
3275 int len;
3276
3277 VPRINTK("ENTER\n");
3278
3279 six_byte = (cdb[0] == MODE_SELECT);
3280 if (six_byte) {
3281 if (scmd->cmd_len < 5)
3282 goto invalid_fld;
3283
3284 len = cdb[4];
3285 hdr_len = 4;
3286 } else {
3287 if (scmd->cmd_len < 9)
3288 goto invalid_fld;
3289
3290 len = (cdb[7] << 8) + cdb[8];
3291 hdr_len = 8;
3292 }
3293
3294 /* We only support PF=1, SP=0. */
3295 if ((cdb[1] & 0x11) != 0x10)
3296 goto invalid_fld;
3297
3298 /* Test early for possible overrun. */
3299 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
3300 goto invalid_param_len;
3301
3302 p = page_address(sg_page(scsi_sglist(scmd)));
3303
3304 /* Move past header and block descriptors. */
3305 if (len < hdr_len)
3306 goto invalid_param_len;
3307
3308 if (six_byte)
3309 bd_len = p[3];
3310 else
3311 bd_len = (p[6] << 8) + p[7];
3312
3313 len -= hdr_len;
3314 p += hdr_len;
3315 if (len < bd_len)
3316 goto invalid_param_len;
3317 if (bd_len != 0 && bd_len != 8)
3318 goto invalid_param;
3319
3320 len -= bd_len;
3321 p += bd_len;
3322 if (len == 0)
3323 goto skip;
3324
3325 /* Parse both possible formats for the mode page headers. */
3326 pg = p[0] & 0x3f;
3327 if (p[0] & 0x40) {
3328 if (len < 4)
3329 goto invalid_param_len;
3330
3331 spg = p[1];
3332 pg_len = (p[2] << 8) | p[3];
3333 p += 4;
3334 len -= 4;
3335 } else {
3336 if (len < 2)
3337 goto invalid_param_len;
3338
3339 spg = 0;
3340 pg_len = p[1];
3341 p += 2;
3342 len -= 2;
3343 }
3344
3345 /*
3346 * No mode subpages supported (yet) but asking for _all_
3347 * subpages may be valid
3348 */
3349 if (spg && (spg != ALL_SUB_MPAGES))
3350 goto invalid_param;
3351 if (pg_len > len)
3352 goto invalid_param_len;
3353
3354 switch (pg) {
3355 case CACHE_MPAGE:
3356 if (ata_mselect_caching(qc, p, pg_len) < 0)
3357 goto invalid_param;
3358 break;
3359
3360 default: /* invalid page code */
3361 goto invalid_param;
3362 }
3363
3364 /*
3365 * Only one page has changeable data, so we only support setting one
3366 * page at a time.
3367 */
3368 if (len > pg_len)
3369 goto invalid_param;
3370
3371 return 0;
3372
3373 invalid_fld:
3374 /* "Invalid field in CDB" */
3375 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
3376 return 1;
3377
3378 invalid_param:
3379 /* "Invalid field in parameter list" */
3380 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x26, 0x0);
3381 return 1;
3382
3383 invalid_param_len:
3384 /* "Parameter list length error" */
3385 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3386 return 1;
3387
3388 skip:
3389 scmd->result = SAM_STAT_GOOD;
3390 return 1;
3391}
3392
3393/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 * ata_get_xlat_func - check if SCSI to ATA translation is possible
3395 * @dev: ATA device
3396 * @cmd: SCSI command opcode to consider
3397 *
3398 * Look up the SCSI command given, and determine whether the
3399 * SCSI command is to be translated or simulated.
3400 *
3401 * RETURNS:
3402 * Pointer to translation function if possible, %NULL if not.
3403 */
3404
3405static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
3406{
3407 switch (cmd) {
3408 case READ_6:
3409 case READ_10:
3410 case READ_16:
3411
3412 case WRITE_6:
3413 case WRITE_10:
3414 case WRITE_16:
3415 return ata_scsi_rw_xlat;
3416
Christoph Hellwig0cdd6eb2009-12-10 10:36:01 +01003417 case WRITE_SAME_16:
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003418 return ata_scsi_write_same_xlat;
3419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 case SYNCHRONIZE_CACHE:
3421 if (ata_try_flush_cache(dev))
3422 return ata_scsi_flush_xlat;
3423 break;
3424
3425 case VERIFY:
3426 case VERIFY_16:
3427 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04003428
3429 case ATA_12:
3430 case ATA_16:
3431 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04003432
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003433 case MODE_SELECT:
3434 case MODE_SELECT_10:
3435 return ata_scsi_mode_select_xlat;
3436 break;
3437
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04003438 case START_STOP:
3439 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
3441
3442 return NULL;
3443}
3444
3445/**
3446 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
3447 * @ap: ATA port to which the command was being sent
3448 * @cmd: SCSI command to dump
3449 *
3450 * Prints the contents of a SCSI command via printk().
3451 */
3452
3453static inline void ata_scsi_dump_cdb(struct ata_port *ap,
3454 struct scsi_cmnd *cmd)
3455{
3456#ifdef ATA_DEBUG
3457 struct scsi_device *scsidev = cmd->device;
3458 u8 *scsicmd = cmd->cmnd;
3459
3460 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09003461 ap->print_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 scsidev->channel, scsidev->id, scsidev->lun,
3463 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
3464 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
3465 scsicmd[8]);
3466#endif
3467}
3468
Tejun Heo542b1442006-12-17 10:45:08 +09003469static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09003470 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06003471{
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003472 u8 scsi_op = scmd->cmnd[0];
3473 ata_xlat_func_t xlat_func;
Tejun Heo2115ea92006-05-15 21:03:39 +09003474 int rc = 0;
3475
Hannes Reinecke9162c652014-11-05 13:08:21 +01003476 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003477 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
3478 goto bad_cdb_len;
3479
3480 xlat_func = ata_get_xlat_func(dev, scsi_op);
3481 } else {
3482 if (unlikely(!scmd->cmd_len))
3483 goto bad_cdb_len;
3484
3485 xlat_func = NULL;
3486 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
3487 /* relay SCSI command to ATAPI device */
Mark Lord607126c2007-11-15 13:13:59 +09003488 int len = COMMAND_SIZE(scsi_op);
3489 if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003490 goto bad_cdb_len;
3491
3492 xlat_func = atapi_xlat;
3493 } else {
3494 /* ATA_16 passthru, treat as an ATA command */
3495 if (unlikely(scmd->cmd_len > 16))
3496 goto bad_cdb_len;
3497
3498 xlat_func = ata_get_xlat_func(dev, scsi_op);
3499 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09003500 }
3501
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003502 if (xlat_func)
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003503 rc = ata_scsi_translate(dev, scmd, xlat_func);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003504 else
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003505 ata_scsi_simulate(dev, scmd);
Tejun Heo2115ea92006-05-15 21:03:39 +09003506
3507 return rc;
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003508
3509 bad_cdb_len:
3510 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
3511 scmd->cmd_len, scsi_op, dev->cdb_len);
3512 scmd->result = DID_ERROR << 16;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003513 scmd->scsi_done(scmd);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003514 return 0;
Brian Kingeb3f0f92006-03-23 17:30:02 -06003515}
3516
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517/**
3518 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
Jeff Garzik23e701e2010-11-17 12:03:58 -05003519 * @shost: SCSI host of command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 * @cmd: SCSI command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 *
3522 * In some cases, this function translates SCSI commands into
3523 * ATA taskfiles, and queues the taskfiles to be sent to
3524 * hardware. In other cases, this function simulates a
3525 * SCSI device by evaluating and responding to certain
3526 * SCSI commands. This creates the overall effect of
3527 * ATA and ATAPI devices appearing as SCSI devices.
3528 *
3529 * LOCKING:
Jeff Garzik23e701e2010-11-17 12:03:58 -05003530 * ATA host lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 *
3532 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09003533 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3534 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 */
Jeff Garzik23e701e2010-11-17 12:03:58 -05003536int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
3538 struct ata_port *ap;
3539 struct ata_device *dev;
3540 struct scsi_device *scsidev = cmd->device;
Tejun Heo2115ea92006-05-15 21:03:39 +09003541 int rc = 0;
Jeff Garzik23e701e2010-11-17 12:03:58 -05003542 unsigned long irq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Jeff Garzik35bb94b2006-04-11 13:12:34 -04003544 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05003545
Jeff Garzik23e701e2010-11-17 12:03:58 -05003546 spin_lock_irqsave(ap->lock, irq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
3548 ata_scsi_dump_cdb(ap, cmd);
3549
3550 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06003551 if (likely(dev))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003552 rc = __ata_scsi_queuecmd(cmd, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06003553 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzik23e701e2010-11-17 12:03:58 -05003555 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 }
3557
Jeff Garzik23e701e2010-11-17 12:03:58 -05003558 spin_unlock_irqrestore(ap->lock, irq_flags);
3559
Tejun Heo2115ea92006-05-15 21:03:39 +09003560 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
3563/**
3564 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003565 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 * @cmd: SCSI command being sent to device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 *
3568 * Interprets and directly executes a select list of SCSI commands
3569 * that can be handled internally.
3570 *
3571 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003572 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 */
3574
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003575void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576{
3577 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04003578 const u8 *scsicmd = cmd->cmnd;
Jeff Garzik45394142007-09-21 06:23:42 -04003579 u8 tmp8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Tejun Heo9a3dccc2006-01-06 09:56:18 +01003581 args.dev = dev;
3582 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 args.cmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003584 args.done = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
3586 switch(scsicmd[0]) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003587 /* TODO: worth improving? */
3588 case FORMAT_UNIT:
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003589 ata_scsi_invalid_field(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003590 break;
3591
3592 case INQUIRY:
3593 if (scsicmd[1] & 2) /* is CmdDt set? */
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003594 ata_scsi_invalid_field(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003595 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
3596 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
3597 else switch (scsicmd[2]) {
3598 case 0x00:
3599 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003601 case 0x80:
3602 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003604 case 0x83:
3605 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003607 case 0x89:
3608 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 break;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003610 case 0xb0:
3611 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
3612 break;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06003613 case 0xb1:
3614 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
3615 break;
Martin K. Petersen02e0a602010-09-10 01:23:18 -04003616 case 0xb2:
3617 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
3618 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 default:
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003620 ata_scsi_invalid_field(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003622 }
3623 break;
3624
3625 case MODE_SENSE:
3626 case MODE_SENSE_10:
3627 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
3628 break;
3629
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003630 case READ_CAPACITY:
3631 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3632 break;
3633
Hannes Reineckeeb846d92014-11-17 14:25:19 +01003634 case SERVICE_ACTION_IN_16:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003635 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
3636 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3637 else
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003638 ata_scsi_invalid_field(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003639 break;
3640
3641 case REPORT_LUNS:
3642 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
3643 break;
3644
3645 case REQUEST_SENSE:
3646 ata_scsi_set_sense(cmd, 0, 0, 0);
3647 cmd->result = (DRIVER_SENSE << 24);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003648 cmd->scsi_done(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003649 break;
3650
3651 /* if we reach this, then writeback caching is disabled,
3652 * turning this into a no-op.
3653 */
3654 case SYNCHRONIZE_CACHE:
3655 /* fall through */
3656
3657 /* no-op's, complete with success */
3658 case REZERO_UNIT:
3659 case SEEK_6:
3660 case SEEK_10:
3661 case TEST_UNIT_READY:
3662 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3663 break;
3664
3665 case SEND_DIAGNOSTIC:
3666 tmp8 = scsicmd[1] & ~(1 << 3);
3667 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
3668 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3669 else
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003670 ata_scsi_invalid_field(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003671 break;
3672
3673 /* all other commands */
3674 default:
3675 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
3676 /* "Invalid command operation code" */
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05003677 cmd->scsi_done(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003678 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 }
3680}
3681
Tejun Heof3187192007-04-17 23:44:07 +09003682int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
3683{
3684 int i, rc;
3685
3686 for (i = 0; i < host->n_ports; i++) {
3687 struct ata_port *ap = host->ports[i];
3688 struct Scsi_Host *shost;
3689
3690 rc = -ENOMEM;
3691 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
3692 if (!shost)
3693 goto err_alloc;
3694
Lin Ming9ee4f392011-12-05 09:20:28 +08003695 shost->eh_noresume = 1;
Tejun Heof3187192007-04-17 23:44:07 +09003696 *(struct ata_port **)&shost->hostdata[0] = ap;
3697 ap->scsi_host = shost;
3698
Gwendal Grignoud9027472010-05-25 12:31:38 -07003699 shost->transportt = ata_scsi_transport_template;
Tejun Heof3187192007-04-17 23:44:07 +09003700 shost->unique_id = ap->print_id;
3701 shost->max_id = 16;
3702 shost->max_lun = 1;
3703 shost->max_channel = 1;
3704 shost->max_cmd_len = 16;
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003705 shost->no_write_same = 1;
Tejun Heof3187192007-04-17 23:44:07 +09003706
Tejun Heo31cc23b2007-09-23 13:14:12 +09003707 /* Schedule policy is determined by ->qc_defer()
3708 * callback and it needs to see every deferred qc.
3709 * Set host_blocked to 1 to prevent SCSI midlayer from
3710 * automatically deferring requests.
3711 */
3712 shost->max_host_blocked = 1;
3713
Lin Mingf8fc75d2012-04-12 13:50:39 +08003714 rc = scsi_add_host_with_dma(ap->scsi_host,
3715 &ap->tdev, ap->host->dev);
Tejun Heof3187192007-04-17 23:44:07 +09003716 if (rc)
3717 goto err_add;
3718 }
3719
3720 return 0;
3721
3722 err_add:
3723 scsi_host_put(host->ports[i]->scsi_host);
3724 err_alloc:
3725 while (--i >= 0) {
3726 struct Scsi_Host *shost = host->ports[i]->scsi_host;
3727
3728 scsi_remove_host(shost);
3729 scsi_host_put(shost);
3730 }
3731 return rc;
3732}
3733
Tejun Heo1ae46312007-07-16 14:29:40 +09003734void ata_scsi_scan_host(struct ata_port *ap, int sync)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003735{
Tejun Heo1ae46312007-07-16 14:29:40 +09003736 int tries = 5;
3737 struct ata_device *last_failed_dev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003738 struct ata_link *link;
Tejun Heo1ae46312007-07-16 14:29:40 +09003739 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003740
Tejun Heo1ae46312007-07-16 14:29:40 +09003741 repeat:
Tejun Heo1eca4362008-11-03 20:03:17 +09003742 ata_for_each_link(link, ap, EDGE) {
3743 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003744 struct scsi_device *sdev;
3745 int channel = 0, id = 0;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04003746
Tejun Heo1eca4362008-11-03 20:03:17 +09003747 if (dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003748 continue;
Tejun Heo3edebac2006-05-31 18:27:40 +09003749
Tejun Heo41bda9c2007-08-06 18:36:24 +09003750 if (ata_is_host_link(link))
3751 id = dev->devno;
3752 else
3753 channel = link->pmp;
3754
3755 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
3756 NULL);
3757 if (!IS_ERR(sdev)) {
3758 dev->sdev = sdev;
3759 scsi_device_put(sdev);
Grant Grundler295124d2010-08-17 10:56:53 -07003760 } else {
3761 dev->sdev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003762 }
Tejun Heo3edebac2006-05-31 18:27:40 +09003763 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04003764 }
Tejun Heo1ae46312007-07-16 14:29:40 +09003765
3766 /* If we scanned while EH was in progress or allocation
3767 * failure occurred, scan would have failed silently. Check
3768 * whether all devices are attached.
3769 */
Tejun Heo1eca4362008-11-03 20:03:17 +09003770 ata_for_each_link(link, ap, EDGE) {
3771 ata_for_each_dev(dev, link, ENABLED) {
3772 if (!dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003773 goto exit_loop;
3774 }
Tejun Heo1ae46312007-07-16 14:29:40 +09003775 }
Tejun Heo41bda9c2007-08-06 18:36:24 +09003776 exit_loop:
3777 if (!link)
Tejun Heo1ae46312007-07-16 14:29:40 +09003778 return;
3779
3780 /* we're missing some SCSI devices */
3781 if (sync) {
3782 /* If caller requested synchrnous scan && we've made
3783 * any progress, sleep briefly and repeat.
3784 */
3785 if (dev != last_failed_dev) {
3786 msleep(100);
3787 last_failed_dev = dev;
3788 goto repeat;
3789 }
3790
3791 /* We might be failing to detect boot device, give it
3792 * a few more chances.
3793 */
3794 if (--tries) {
3795 msleep(100);
3796 goto repeat;
3797 }
3798
Joe Perchesa9a79df2011-04-15 15:51:59 -07003799 ata_port_err(ap,
3800 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
Tejun Heo1ae46312007-07-16 14:29:40 +09003801 }
3802
Tejun Heoad72cf92010-07-02 10:03:52 +02003803 queue_delayed_work(system_long_wq, &ap->hotplug_task,
Tejun Heo1ae46312007-07-16 14:29:40 +09003804 round_jiffies_relative(HZ));
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003805}
Tejun Heo0ea035a2006-05-31 18:28:01 +09003806
3807/**
3808 * ata_scsi_offline_dev - offline attached SCSI device
3809 * @dev: ATA device to offline attached SCSI device for
3810 *
3811 * This function is called from ata_eh_hotplug() and responsible
3812 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04003813 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09003814 * against clearing.
3815 *
3816 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003817 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09003818 *
3819 * RETURNS:
3820 * 1 if attached SCSI device exists, 0 otherwise.
3821 */
3822int ata_scsi_offline_dev(struct ata_device *dev)
3823{
3824 if (dev->sdev) {
3825 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
3826 return 1;
3827 }
3828 return 0;
3829}
Tejun Heo580b21022006-05-31 18:28:05 +09003830
3831/**
3832 * ata_scsi_remove_dev - remove attached SCSI device
3833 * @dev: ATA device to remove attached SCSI device for
3834 *
3835 * This function is called from ata_eh_scsi_hotplug() and
3836 * responsible for removing the SCSI device attached to @dev.
3837 *
3838 * LOCKING:
3839 * Kernel thread context (may sleep).
3840 */
3841static void ata_scsi_remove_dev(struct ata_device *dev)
3842{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003843 struct ata_port *ap = dev->link->ap;
Tejun Heo580b21022006-05-31 18:28:05 +09003844 struct scsi_device *sdev;
3845 unsigned long flags;
3846
3847 /* Alas, we need to grab scan_mutex to ensure SCSI device
3848 * state doesn't change underneath us and thus
3849 * scsi_device_get() always succeeds. The mutex locking can
3850 * be removed if there is __scsi_device_get() interface which
3851 * increments reference counts regardless of device state.
3852 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003853 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04003854 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b21022006-05-31 18:28:05 +09003855
Jeff Garzikcca39742006-08-24 03:19:22 -04003856 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b21022006-05-31 18:28:05 +09003857 sdev = dev->sdev;
3858 dev->sdev = NULL;
3859
3860 if (sdev) {
3861 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04003862 * away underneath us after the host lock and
Tejun Heo580b21022006-05-31 18:28:05 +09003863 * scan_mutex are released. Hold onto it.
3864 */
3865 if (scsi_device_get(sdev) == 0) {
3866 /* The following ensures the attached sdev is
3867 * offline on return from ata_scsi_offline_dev()
3868 * regardless it wins or loses the race
3869 * against this function.
3870 */
3871 scsi_device_set_state(sdev, SDEV_OFFLINE);
3872 } else {
3873 WARN_ON(1);
3874 sdev = NULL;
3875 }
3876 }
3877
Jeff Garzikba6a1302006-06-22 23:46:10 -04003878 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04003879 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09003880
3881 if (sdev) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003882 ata_dev_info(dev, "detaching (SCSI %s)\n",
3883 dev_name(&sdev->sdev_gendev));
Tejun Heo580b21022006-05-31 18:28:05 +09003884
3885 scsi_remove_device(sdev);
3886 scsi_device_put(sdev);
3887 }
3888}
3889
Tejun Heo41bda9c2007-08-06 18:36:24 +09003890static void ata_scsi_handle_link_detach(struct ata_link *link)
3891{
3892 struct ata_port *ap = link->ap;
3893 struct ata_device *dev;
3894
Tejun Heo1eca4362008-11-03 20:03:17 +09003895 ata_for_each_dev(dev, link, ALL) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003896 unsigned long flags;
3897
3898 if (!(dev->flags & ATA_DFLAG_DETACHED))
3899 continue;
3900
3901 spin_lock_irqsave(ap->lock, flags);
3902 dev->flags &= ~ATA_DFLAG_DETACHED;
3903 spin_unlock_irqrestore(ap->lock, flags);
3904
Aaron Luf1bc1e42013-08-23 10:17:54 +08003905 if (zpodd_dev_enabled(dev))
3906 zpodd_exit(dev);
3907
Tejun Heo41bda9c2007-08-06 18:36:24 +09003908 ata_scsi_remove_dev(dev);
3909 }
3910}
3911
Tejun Heo580b21022006-05-31 18:28:05 +09003912/**
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003913 * ata_scsi_media_change_notify - send media change event
Randy Dunlapc5d0e6a2007-10-15 17:29:46 -07003914 * @dev: Pointer to the disk device with media change event
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003915 *
3916 * Tell the block layer to send a media change notification
3917 * event.
3918 *
3919 * LOCKING:
Tejun Heo854c73a2007-09-23 13:14:11 +09003920 * spin_lock_irqsave(host lock)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003921 */
Tejun Heo854c73a2007-09-23 13:14:11 +09003922void ata_scsi_media_change_notify(struct ata_device *dev)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003923{
Tejun Heo854c73a2007-09-23 13:14:11 +09003924 if (dev->sdev)
Jeff Garzikf26792d2007-10-29 17:18:39 -04003925 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
3926 GFP_ATOMIC);
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003927}
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003928
3929/**
Tejun Heo580b21022006-05-31 18:28:05 +09003930 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00003931 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b21022006-05-31 18:28:05 +09003932 *
3933 * Perform SCSI part of hotplug. It's executed from a separate
3934 * workqueue after EH completes. This is necessary because SCSI
3935 * hot plugging requires working EH and hot unplugging is
3936 * synchronized with hot plugging with a mutex.
3937 *
3938 * LOCKING:
3939 * Kernel thread context (may sleep).
3940 */
David Howells65f27f32006-11-22 14:55:48 +00003941void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b21022006-05-31 18:28:05 +09003942{
David Howells65f27f32006-11-22 14:55:48 +00003943 struct ata_port *ap =
3944 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo41bda9c2007-08-06 18:36:24 +09003945 int i;
Tejun Heo580b21022006-05-31 18:28:05 +09003946
Tejun Heob51e9e52006-06-29 01:29:30 +09003947 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b21022006-05-31 18:28:05 +09003948 DPRINTK("ENTER/EXIT - unloading\n");
3949 return;
3950 }
3951
Tejun Heo85fbd722013-12-18 07:07:32 -05003952 /*
3953 * XXX - UGLY HACK
3954 *
3955 * The block layer suspend/resume path is fundamentally broken due
3956 * to freezable kthreads and workqueue and may deadlock if a block
3957 * device gets removed while resume is in progress. I don't know
3958 * what the solution is short of removing freezable kthreads and
3959 * workqueues altogether.
3960 *
3961 * The following is an ugly hack to avoid kicking off device
3962 * removal while freezer is active. This is a joke but does avoid
3963 * this particular deadlock scenario.
3964 *
3965 * https://bugzilla.kernel.org/show_bug.cgi?id=62801
3966 * http://marc.info/?l=linux-kernel&m=138695698516487
3967 */
3968#ifdef CONFIG_FREEZER
3969 while (pm_freezing)
3970 msleep(10);
3971#endif
3972
Tejun Heo580b21022006-05-31 18:28:05 +09003973 DPRINTK("ENTER\n");
Tejun Heoad72cf92010-07-02 10:03:52 +02003974 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09003975
Tejun Heo41bda9c2007-08-06 18:36:24 +09003976 /* Unplug detached devices. We cannot use link iterator here
3977 * because PMP links have to be scanned even if PMP is
3978 * currently not attached. Iterate manually.
3979 */
3980 ata_scsi_handle_link_detach(&ap->link);
3981 if (ap->pmp_link)
3982 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
3983 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
Tejun Heo580b21022006-05-31 18:28:05 +09003984
3985 /* scan for new ones */
Tejun Heo1ae46312007-07-16 14:29:40 +09003986 ata_scsi_scan_host(ap, 0);
Tejun Heo580b21022006-05-31 18:28:05 +09003987
Tejun Heoad72cf92010-07-02 10:03:52 +02003988 mutex_unlock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09003989 DPRINTK("EXIT\n");
3990}
Tejun Heo83c47bc2006-05-31 18:28:07 +09003991
3992/**
3993 * ata_scsi_user_scan - indication for user-initiated bus scan
3994 * @shost: SCSI host to scan
3995 * @channel: Channel to scan
3996 * @id: ID to scan
3997 * @lun: LUN to scan
3998 *
3999 * This function is called when user explicitly requests bus
4000 * scan. Set probe pending flag and invoke EH.
4001 *
4002 * LOCKING:
4003 * SCSI layer (we don't care)
4004 *
4005 * RETURNS:
4006 * Zero.
4007 */
Gwendal Grignoud9027472010-05-25 12:31:38 -07004008int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004009 unsigned int id, u64 lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004010{
4011 struct ata_port *ap = ata_shost_to_port(shost);
4012 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004013 int devno, rc = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004014
4015 if (!ap->ops->error_handler)
4016 return -EOPNOTSUPP;
4017
Tejun Heo41bda9c2007-08-06 18:36:24 +09004018 if (lun != SCAN_WILD_CARD && lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004019 return -EINVAL;
4020
Tejun Heo071f44b2008-04-07 22:47:22 +09004021 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004022 if (channel != SCAN_WILD_CARD && channel)
4023 return -EINVAL;
4024 devno = id;
4025 } else {
4026 if (id != SCAN_WILD_CARD && id)
4027 return -EINVAL;
4028 devno = channel;
4029 }
4030
Jeff Garzikba6a1302006-06-22 23:46:10 -04004031 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004032
Tejun Heo41bda9c2007-08-06 18:36:24 +09004033 if (devno == SCAN_WILD_CARD) {
4034 struct ata_link *link;
4035
Tejun Heo1eca4362008-11-03 20:03:17 +09004036 ata_for_each_link(link, ap, EDGE) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004037 struct ata_eh_info *ehi = &link->eh_info;
Tejun Heob558edd2008-01-24 00:05:14 +09004038 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09004039 ehi->action |= ATA_EH_RESET;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004040 }
Tejun Heo83c47bc2006-05-31 18:28:07 +09004041 } else {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004042 struct ata_device *dev = ata_find_dev(ap, devno);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004043
4044 if (dev) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004045 struct ata_eh_info *ehi = &dev->link->eh_info;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004046 ehi->probe_mask |= 1 << dev->devno;
Tejun Heocf480622008-01-24 00:05:14 +09004047 ehi->action |= ATA_EH_RESET;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004048 } else
4049 rc = -EINVAL;
4050 }
4051
Tejun Heo309afcb2006-09-30 18:07:17 +09004052 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09004053 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09004054 spin_unlock_irqrestore(ap->lock, flags);
4055 ata_port_wait_eh(ap);
4056 } else
4057 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004058
4059 return rc;
4060}
zhao, forrest3057ac32006-06-12 12:01:34 +08004061
4062/**
Tejun Heod0171262006-06-12 22:51:14 +09004063 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00004064 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08004065 *
Tejun Heod0171262006-06-12 22:51:14 +09004066 * After ATA pass thru (SAT) commands are executed successfully,
Tejun Heoad72cf92010-07-02 10:03:52 +02004067 * libata need to propagate the changes to SCSI layer.
zhao, forrest3057ac32006-06-12 12:01:34 +08004068 *
Tejun Heod0171262006-06-12 22:51:14 +09004069 * LOCKING:
4070 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08004071 */
David Howells65f27f32006-11-22 14:55:48 +00004072void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08004073{
David Howells65f27f32006-11-22 14:55:48 +00004074 struct ata_port *ap =
4075 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004076 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09004077 struct ata_device *dev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004078 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08004079
Tejun Heoad72cf92010-07-02 10:03:52 +02004080 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heof84e7e42006-11-22 11:21:31 +09004081 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08004082
Tejun Heo1eca4362008-11-03 20:03:17 +09004083 ata_for_each_link(link, ap, EDGE) {
4084 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004085 struct scsi_device *sdev = dev->sdev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004086
Tejun Heo1eca4362008-11-03 20:03:17 +09004087 if (!sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004088 continue;
4089 if (scsi_device_get(sdev))
4090 continue;
Tejun Heof84e7e42006-11-22 11:21:31 +09004091
Tejun Heo41bda9c2007-08-06 18:36:24 +09004092 spin_unlock_irqrestore(ap->lock, flags);
4093 scsi_rescan_device(&(sdev->sdev_gendev));
4094 scsi_device_put(sdev);
4095 spin_lock_irqsave(ap->lock, flags);
4096 }
zhao, forrest3057ac32006-06-12 12:01:34 +08004097 }
Tejun Heof84e7e42006-11-22 11:21:31 +09004098
4099 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad72cf92010-07-02 10:03:52 +02004100 mutex_unlock(&ap->scsi_scan_mutex);
zhao, forrest3057ac32006-06-12 12:01:34 +08004101}
Brian King80289162006-08-07 14:27:31 -05004102
4103/**
4104 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04004105 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05004106 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04004107 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05004108 *
4109 * LOCKING:
4110 * PCI/etc. bus probe sem.
4111 *
4112 * RETURNS:
4113 * ata_port pointer on success / NULL on failure.
4114 */
4115
Jeff Garzikcca39742006-08-24 03:19:22 -04004116struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05004117 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04004118 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05004119{
Tejun Heof3187192007-04-17 23:44:07 +09004120 struct ata_port *ap;
Brian King80289162006-08-07 14:27:31 -05004121
Tejun Heof3187192007-04-17 23:44:07 +09004122 ap = ata_port_alloc(host);
Brian King80289162006-08-07 14:27:31 -05004123 if (!ap)
4124 return NULL;
4125
Tejun Heof3187192007-04-17 23:44:07 +09004126 ap->port_no = 0;
James Bottomleya29b5da2011-01-23 08:30:00 -06004127 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09004128 ap->pio_mask = port_info->pio_mask;
4129 ap->mwdma_mask = port_info->mwdma_mask;
4130 ap->udma_mask = port_info->udma_mask;
4131 ap->flags |= port_info->flags;
4132 ap->ops = port_info->port_ops;
4133 ap->cbl = ATA_CBL_SATA;
4134
Brian King80289162006-08-07 14:27:31 -05004135 return ap;
4136}
4137EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
4138
4139/**
4140 * ata_sas_port_start - Set port up for dma.
4141 * @ap: Port to initialize
4142 *
4143 * Called just after data structures for each port are
James Bottomleydde20202008-02-19 11:36:56 +01004144 * initialized.
Brian King80289162006-08-07 14:27:31 -05004145 *
4146 * May be used as the port_start() entry in ata_port_operations.
4147 *
4148 * LOCKING:
4149 * Inherited from caller.
4150 */
4151int ata_sas_port_start(struct ata_port *ap)
4152{
Nishanth Aravamudan3f1e0462011-06-16 08:28:36 -07004153 /*
4154 * the port is marked as frozen at allocation time, but if we don't
4155 * have new eh, we won't thaw it
4156 */
4157 if (!ap->ops->error_handler)
4158 ap->pflags &= ~ATA_PFLAG_FROZEN;
James Bottomleydde20202008-02-19 11:36:56 +01004159 return 0;
Brian King80289162006-08-07 14:27:31 -05004160}
4161EXPORT_SYMBOL_GPL(ata_sas_port_start);
4162
4163/**
4164 * ata_port_stop - Undo ata_sas_port_start()
4165 * @ap: Port to shut down
4166 *
Brian King80289162006-08-07 14:27:31 -05004167 * May be used as the port_stop() entry in ata_port_operations.
4168 *
4169 * LOCKING:
4170 * Inherited from caller.
4171 */
4172
4173void ata_sas_port_stop(struct ata_port *ap)
4174{
Brian King80289162006-08-07 14:27:31 -05004175}
4176EXPORT_SYMBOL_GPL(ata_sas_port_stop);
4177
Dan Williamsb2024452012-03-21 21:09:07 -07004178/**
4179 * ata_sas_async_probe - simply schedule probing and return
4180 * @ap: Port to probe
4181 *
4182 * For batch scheduling of probe for sas attached ata devices, assumes
4183 * the port has already been through ata_sas_port_init()
4184 */
4185void ata_sas_async_probe(struct ata_port *ap)
Dan Williams9508a662012-01-18 20:47:01 -08004186{
Dan Williamsb2024452012-03-21 21:09:07 -07004187 __ata_port_probe(ap);
Dan Williams9508a662012-01-18 20:47:01 -08004188}
Dan Williamsb2024452012-03-21 21:09:07 -07004189EXPORT_SYMBOL_GPL(ata_sas_async_probe);
4190
4191int ata_sas_sync_probe(struct ata_port *ap)
4192{
4193 return ata_port_probe(ap);
4194}
4195EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
4196
Dan Williams9508a662012-01-18 20:47:01 -08004197
Brian King80289162006-08-07 14:27:31 -05004198/**
4199 * ata_sas_port_init - Initialize a SATA device
4200 * @ap: SATA port to initialize
4201 *
4202 * LOCKING:
4203 * PCI/etc. bus probe sem.
4204 *
4205 * RETURNS:
4206 * Zero on success, non-zero on error.
4207 */
4208
4209int ata_sas_port_init(struct ata_port *ap)
4210{
4211 int rc = ap->ops->port_start(ap);
4212
Dan Williamsb2024452012-03-21 21:09:07 -07004213 if (rc)
4214 return rc;
4215 ap->print_id = atomic_inc_return(&ata_print_id);
4216 return 0;
Brian King80289162006-08-07 14:27:31 -05004217}
4218EXPORT_SYMBOL_GPL(ata_sas_port_init);
4219
4220/**
4221 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
4222 * @ap: SATA port to destroy
4223 *
4224 */
4225
4226void ata_sas_port_destroy(struct ata_port *ap)
4227{
Tejun Heof0d36ef2007-01-20 16:00:28 +09004228 if (ap->ops->port_stop)
4229 ap->ops->port_stop(ap);
Brian King80289162006-08-07 14:27:31 -05004230 kfree(ap);
4231}
4232EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
4233
4234/**
4235 * ata_sas_slave_configure - Default slave_config routine for libata devices
4236 * @sdev: SCSI device to configure
4237 * @ap: ATA port to which SCSI device is attached
4238 *
4239 * RETURNS:
4240 * Zero.
4241 */
4242
4243int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
4244{
4245 ata_scsi_sdev_config(sdev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004246 ata_scsi_dev_config(sdev, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05004247 return 0;
4248}
4249EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
4250
4251/**
4252 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
4253 * @cmd: SCSI command to be sent
Brian King80289162006-08-07 14:27:31 -05004254 * @ap: ATA port to which the command is being sent
4255 *
4256 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06004257 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
4258 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05004259 */
4260
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004261int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
Brian King80289162006-08-07 14:27:31 -05004262{
Brian King08475a12006-11-20 13:51:56 -06004263 int rc = 0;
4264
Brian King80289162006-08-07 14:27:31 -05004265 ata_scsi_dump_cdb(ap, cmd);
4266
Tejun Heo2486fa52008-07-31 07:52:40 +09004267 if (likely(ata_dev_enabled(ap->link.device)))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004268 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05004269 else {
4270 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004271 cmd->scsi_done(cmd);
Brian King80289162006-08-07 14:27:31 -05004272 }
Brian King08475a12006-11-20 13:51:56 -06004273 return rc;
Brian King80289162006-08-07 14:27:31 -05004274}
4275EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
Shaohua Li98bd4be2015-01-23 19:52:07 -08004276
4277int ata_sas_allocate_tag(struct ata_port *ap)
4278{
4279 unsigned int max_queue = ap->host->n_tags;
4280 unsigned int i, tag;
4281
4282 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
Tejun Heo3a028242015-03-24 14:14:18 -04004283 tag = tag < max_queue ? tag : 0;
Shaohua Li98bd4be2015-01-23 19:52:07 -08004284
4285 /* the last tag is reserved for internal command. */
4286 if (tag == ATA_TAG_INTERNAL)
4287 continue;
4288
4289 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
4290 ap->sas_last_tag = tag;
4291 return tag;
4292 }
4293 }
4294 return -1;
4295}
4296
4297void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
4298{
4299 clear_bit(tag, &ap->sas_tag_allocated);
4300}