blob: 63c25f1499c31aece37fcc46acfa01edebe3bf4b [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',
Mauro Carvalho Chehab9bb9a392017-05-16 09:16:37 -030028 * as Documentation/driver-api/libata.rst
Jeff Garzikaf36d7f2005-08-28 20:18:39 -040029 *
30 * Hardware documentation available from
31 * - http://www.t10.org/
32 * - http://www.t13.org/
33 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/kernel.h>
38#include <linux/blkdev.h>
39#include <linux/spinlock.h>
Paul Gortmaker38789fd2011-07-17 15:33:58 -040040#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <scsi/scsi_host.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020043#include <scsi/scsi_cmnd.h>
Mike Christie85837eb2005-11-11 16:38:53 -060044#include <scsi/scsi_eh.h>
Jeff Garzik005a5a02005-10-30 23:31:48 -050045#include <scsi/scsi_device.h>
Tejun Heoa6e6ce82006-05-15 21:03:48 +090046#include <scsi/scsi_tcq.h>
Tejun Heo30afc842006-03-18 18:40:14 +090047#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/libata.h>
Jeff Garzikb0955182005-05-12 15:45:22 -040049#include <linux/hdreg.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040050#include <linux/uaccess.h>
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +010051#include <linux/suspend.h>
Christoph Hellwig18f0f972009-11-17 10:00:47 -050052#include <asm/unaligned.h>
Adam Manzanares8e061782016-10-17 11:27:29 -070053#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include "libata.h"
Gwendal Grignoud9027472010-05-25 12:31:38 -070056#include "libata-transport.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Tejun Heo87340e92008-04-28 17:48:51 +090058#define ATA_SCSI_RBUF_SIZE 4096
59
60static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
61static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
Jeff Garzikb0955182005-05-12 15:45:22 -040062
Tejun Heoad706992006-12-17 10:45:57 +090063typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
Tejun Heoab5b3a52006-05-31 18:27:34 +090064
Jeff Garzik2dcb4072007-10-19 06:42:56 -040065static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090066 const struct scsi_device *scsidev);
Jeff Garzik2dcb4072007-10-19 06:42:56 -040067static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090068 const struct scsi_device *scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040070#define RW_RECOVERY_MPAGE 0x1
71#define RW_RECOVERY_MPAGE_LEN 12
72#define CACHE_MPAGE 0x8
73#define CACHE_MPAGE_LEN 20
74#define CONTROL_MPAGE 0xa
75#define CONTROL_MPAGE_LEN 12
76#define ALL_MPAGES 0x3f
77#define ALL_SUB_MPAGES 0xff
78
79
Jeff Garzik24f75682007-09-21 07:54:49 -040080static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040081 RW_RECOVERY_MPAGE,
82 RW_RECOVERY_MPAGE_LEN - 2,
Jeff Garzik24f75682007-09-21 07:54:49 -040083 (1 << 7), /* AWRE */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040084 0, /* read retry count */
85 0, 0, 0, 0,
86 0, /* write retry count */
87 0, 0, 0
88};
89
90static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
91 CACHE_MPAGE,
92 CACHE_MPAGE_LEN - 2,
93 0, /* contains WCE, needs to be 0 for logic */
94 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, /* contains DRA, needs to be 0 for logic */
96 0, 0, 0, 0, 0, 0, 0
97};
98
99static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
100 CONTROL_MPAGE,
101 CONTROL_MPAGE_LEN - 2,
102 2, /* DSENSE=0, GLTSD=1 */
103 0, /* [QAM+QERR may be 1, see 05-359r1] */
104 0, 0, 0, 0, 0xff, 0xff,
105 0, 30 /* extended self test time, see 05-359r1 */
106};
107
Tejun Heoc93b2632010-09-01 17:50:04 +0200108static const char *ata_lpm_policy_names[] = {
Hans de Goedef4ac6472017-09-14 12:35:36 +0200109 [ATA_LPM_UNKNOWN] = "max_performance",
110 [ATA_LPM_MAX_POWER] = "max_performance",
111 [ATA_LPM_MED_POWER] = "medium_power",
112 [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
Srinivas Pandruvadaa5ec5a7b2018-07-27 13:47:02 -0700113 [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
Hans de Goedef4ac6472017-09-14 12:35:36 +0200114 [ATA_LPM_MIN_POWER] = "min_power",
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400115};
116
Tejun Heoecd75ad2014-01-16 09:47:17 -0500117static ssize_t ata_scsi_lpm_store(struct device *device,
Tejun Heoc93b2632010-09-01 17:50:04 +0200118 struct device_attribute *attr,
119 const char *buf, size_t count)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400120{
Tejun Heoecd75ad2014-01-16 09:47:17 -0500121 struct Scsi_Host *shost = class_to_shost(device);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400122 struct ata_port *ap = ata_shost_to_port(shost);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500123 struct ata_link *link;
124 struct ata_device *dev;
Tejun Heoc93b2632010-09-01 17:50:04 +0200125 enum ata_lpm_policy policy;
Tejun Heo6b7ae952010-09-01 17:50:06 +0200126 unsigned long flags;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400127
Tejun Heoc93b2632010-09-01 17:50:04 +0200128 /* UNKNOWN is internal state, iterate from MAX_POWER */
129 for (policy = ATA_LPM_MAX_POWER;
130 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
131 const char *name = ata_lpm_policy_names[policy];
132
133 if (strncmp(name, buf, strlen(name)) == 0)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400134 break;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400135 }
Tejun Heoc93b2632010-09-01 17:50:04 +0200136 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400137 return -EINVAL;
138
Tejun Heo6b7ae952010-09-01 17:50:06 +0200139 spin_lock_irqsave(ap->lock, flags);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500140
141 ata_for_each_link(link, ap, EDGE) {
142 ata_for_each_dev(dev, &ap->link, ENABLED) {
143 if (dev->horkage & ATA_HORKAGE_NOLPM) {
144 count = -EOPNOTSUPP;
145 goto out_unlock;
146 }
147 }
148 }
149
Tejun Heo6b7ae952010-09-01 17:50:06 +0200150 ap->target_lpm_policy = policy;
151 ata_port_schedule_eh(ap);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500152out_unlock:
Tejun Heo6b7ae952010-09-01 17:50:06 +0200153 spin_unlock_irqrestore(ap->lock, flags);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400154 return count;
155}
156
Tejun Heoc93b2632010-09-01 17:50:04 +0200157static ssize_t ata_scsi_lpm_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400159{
Tony Jonesee959b02008-02-22 00:13:36 +0100160 struct Scsi_Host *shost = class_to_shost(dev);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400161 struct ata_port *ap = ata_shost_to_port(shost);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400162
Tejun Heo6b7ae952010-09-01 17:50:06 +0200163 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400164 return -EINVAL;
165
Tejun Heoc93b2632010-09-01 17:50:04 +0200166 return snprintf(buf, PAGE_SIZE, "%s\n",
Tejun Heo6b7ae952010-09-01 17:50:06 +0200167 ata_lpm_policy_names[ap->target_lpm_policy]);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400168}
Tony Jonesee959b02008-02-22 00:13:36 +0100169DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
Tejun Heoc93b2632010-09-01 17:50:04 +0200170 ata_scsi_lpm_show, ata_scsi_lpm_store);
Tony Jonesee959b02008-02-22 00:13:36 +0100171EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400172
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200173static ssize_t ata_scsi_park_show(struct device *device,
174 struct device_attribute *attr, char *buf)
175{
176 struct scsi_device *sdev = to_scsi_device(device);
177 struct ata_port *ap;
178 struct ata_link *link;
179 struct ata_device *dev;
Tejun Heo3948b6f2016-02-18 11:50:37 -0500180 unsigned long now;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200181 unsigned int uninitialized_var(msecs);
182 int rc = 0;
183
184 ap = ata_shost_to_port(sdev->host);
185
Tejun Heo3948b6f2016-02-18 11:50:37 -0500186 spin_lock_irq(ap->lock);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200187 dev = ata_scsi_find_dev(ap, sdev);
188 if (!dev) {
189 rc = -ENODEV;
190 goto unlock;
191 }
192 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
193 rc = -EOPNOTSUPP;
194 goto unlock;
195 }
196
197 link = dev->link;
Elias Oltmannsa4641892008-11-03 19:01:08 +0900198 now = jiffies;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200199 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
200 link->eh_context.unloaded_mask & (1 << dev->devno) &&
Elias Oltmannsa4641892008-11-03 19:01:08 +0900201 time_after(dev->unpark_deadline, now))
202 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200203 else
204 msecs = 0;
205
206unlock:
207 spin_unlock_irq(ap->lock);
208
209 return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
210}
211
212static ssize_t ata_scsi_park_store(struct device *device,
213 struct device_attribute *attr,
214 const char *buf, size_t len)
215{
216 struct scsi_device *sdev = to_scsi_device(device);
217 struct ata_port *ap;
218 struct ata_device *dev;
219 long int input;
220 unsigned long flags;
221 int rc;
222
Jingoo Han42b9ab72013-07-19 15:56:41 +0900223 rc = kstrtol(buf, 10, &input);
224 if (rc)
225 return rc;
226 if (input < -2)
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200227 return -EINVAL;
228 if (input > ATA_TMOUT_MAX_PARK) {
229 rc = -EOVERFLOW;
230 input = ATA_TMOUT_MAX_PARK;
231 }
232
233 ap = ata_shost_to_port(sdev->host);
234
235 spin_lock_irqsave(ap->lock, flags);
236 dev = ata_scsi_find_dev(ap, sdev);
237 if (unlikely(!dev)) {
238 rc = -ENODEV;
239 goto unlock;
240 }
Hannes Reinecke9162c652014-11-05 13:08:21 +0100241 if (dev->class != ATA_DEV_ATA &&
242 dev->class != ATA_DEV_ZAC) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200243 rc = -EOPNOTSUPP;
244 goto unlock;
245 }
246
247 if (input >= 0) {
248 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
249 rc = -EOPNOTSUPP;
250 goto unlock;
251 }
252
253 dev->unpark_deadline = ata_deadline(jiffies, input);
254 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
255 ata_port_schedule_eh(ap);
256 complete(&ap->park_req_pending);
257 } else {
258 switch (input) {
259 case -1:
260 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
261 break;
262 case -2:
263 dev->flags |= ATA_DFLAG_NO_UNLOAD;
264 break;
265 }
266 }
267unlock:
268 spin_unlock_irqrestore(ap->lock, flags);
269
270 return rc ? rc : len;
271}
272DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
273 ata_scsi_park_show, ata_scsi_park_store);
274EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
275
Adam Manzanares84f95242016-10-17 11:27:30 -0700276static ssize_t ata_ncq_prio_enable_show(struct device *device,
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800277 struct device_attribute *attr,
278 char *buf)
Adam Manzanares84f95242016-10-17 11:27:30 -0700279{
280 struct scsi_device *sdev = to_scsi_device(device);
281 struct ata_port *ap;
282 struct ata_device *dev;
283 bool ncq_prio_enable;
284 int rc = 0;
285
286 ap = ata_shost_to_port(sdev->host);
287
288 spin_lock_irq(ap->lock);
289 dev = ata_scsi_find_dev(ap, sdev);
290 if (!dev) {
291 rc = -ENODEV;
292 goto unlock;
293 }
294
295 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
296
297unlock:
298 spin_unlock_irq(ap->lock);
299
300 return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable);
301}
302
303static ssize_t ata_ncq_prio_enable_store(struct device *device,
304 struct device_attribute *attr,
305 const char *buf, size_t len)
306{
307 struct scsi_device *sdev = to_scsi_device(device);
308 struct ata_port *ap;
309 struct ata_device *dev;
310 long int input;
Adam Manzanares84f95242016-10-17 11:27:30 -0700311 int rc;
312
313 rc = kstrtol(buf, 10, &input);
314 if (rc)
315 return rc;
316 if ((input < 0) || (input > 1))
317 return -EINVAL;
318
319 ap = ata_shost_to_port(sdev->host);
Adam Manzanares84f95242016-10-17 11:27:30 -0700320 dev = ata_scsi_find_dev(ap, sdev);
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800321 if (unlikely(!dev))
322 return -ENODEV;
323
324 spin_lock_irq(ap->lock);
325 if (input)
326 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
327 else
328 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
329
330 dev->link->eh_info.action |= ATA_EH_REVALIDATE;
331 dev->link->eh_info.flags |= ATA_EHI_QUIET;
332 ata_port_schedule_eh(ap);
333 spin_unlock_irq(ap->lock);
334
335 ata_port_wait_eh(ap);
Adam Manzanares84f95242016-10-17 11:27:30 -0700336
Adam Manzanares4e647d92016-10-19 20:40:19 -0700337 if (input) {
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800338 spin_lock_irq(ap->lock);
Adam Manzanares4e647d92016-10-19 20:40:19 -0700339 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800340 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
341 rc = -EIO;
Adam Manzanares4e647d92016-10-19 20:40:19 -0700342 }
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800343 spin_unlock_irq(ap->lock);
Adam Manzanares4e647d92016-10-19 20:40:19 -0700344 }
Adam Manzanares84f95242016-10-17 11:27:30 -0700345
Adam Manzanares84f95242016-10-17 11:27:30 -0700346 return rc ? rc : len;
347}
348
349DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
350 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
351EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
352
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200353void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
354 u8 sk, u8 asc, u8 ascq)
Tejun Heof0761be2008-04-28 17:16:52 +0900355{
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200356 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
357
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +0200358 if (!cmd)
359 return;
360
Tejun Heof0761be2008-04-28 17:16:52 +0900361 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
362
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200363 scsi_build_sense_buffer(d_sense, cmd->sense_buffer, sk, asc, ascq);
Tejun Heof0761be2008-04-28 17:16:52 +0900364}
365
Hannes Reinecke492bf622016-04-04 11:43:59 +0200366void ata_scsi_set_sense_information(struct ata_device *dev,
367 struct scsi_cmnd *cmd,
368 const struct ata_taskfile *tf)
369{
370 u64 information;
371
372 if (!cmd)
373 return;
374
375 information = ata_tf_read_block(tf, dev);
376 if (information == U64_MAX)
377 return;
378
379 scsi_set_sense_information(cmd->sense_buffer,
380 SCSI_SENSE_BUFFERSIZE, information);
381}
382
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200383static void ata_scsi_set_invalid_field(struct ata_device *dev,
Hannes Reinecke0df10b82016-04-04 11:44:06 +0200384 struct scsi_cmnd *cmd, u16 field, u8 bit)
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200385{
386 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
Tom Yan8554e5e2016-07-07 01:13:09 +0800387 /* "Invalid field in CDB" */
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200388 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
Hannes Reinecke0df10b82016-04-04 11:44:06 +0200389 field, bit, 1);
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200390}
391
Hannes Reinecke77800812016-04-04 11:44:07 +0200392static void ata_scsi_set_invalid_parameter(struct ata_device *dev,
393 struct scsi_cmnd *cmd, u16 field)
394{
395 /* "Invalid field in parameter list" */
396 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x26, 0x0);
397 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
398 field, 0xff, 0);
Tejun Heo5924b742007-01-02 20:20:07 +0900399}
400
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700401static ssize_t
402ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
403 const char *buf, size_t count)
404{
405 struct Scsi_Host *shost = class_to_shost(dev);
406 struct ata_port *ap = ata_shost_to_port(shost);
407 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
408 return ap->ops->em_store(ap, buf, count);
409 return -EINVAL;
410}
411
412static ssize_t
413ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
414 char *buf)
415{
416 struct Scsi_Host *shost = class_to_shost(dev);
417 struct ata_port *ap = ata_shost_to_port(shost);
418
419 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
420 return ap->ops->em_show(ap, buf);
421 return -EINVAL;
422}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200423DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700424 ata_scsi_em_message_show, ata_scsi_em_message_store);
425EXPORT_SYMBOL_GPL(dev_attr_em_message);
426
427static ssize_t
428ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
429 char *buf)
430{
431 struct Scsi_Host *shost = class_to_shost(dev);
432 struct ata_port *ap = ata_shost_to_port(shost);
433
434 return snprintf(buf, 23, "%d\n", ap->em_message_type);
435}
436DEVICE_ATTR(em_message_type, S_IRUGO,
437 ata_scsi_em_message_type_show, NULL);
438EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
439
440static ssize_t
441ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
442 char *buf)
443{
444 struct scsi_device *sdev = to_scsi_device(dev);
445 struct ata_port *ap = ata_shost_to_port(sdev->host);
446 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
447
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800448 if (atadev && ap->ops->sw_activity_show &&
449 (ap->flags & ATA_FLAG_SW_ACTIVITY))
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700450 return ap->ops->sw_activity_show(atadev, buf);
451 return -EINVAL;
452}
453
454static ssize_t
455ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
456 const char *buf, size_t count)
457{
458 struct scsi_device *sdev = to_scsi_device(dev);
459 struct ata_port *ap = ata_shost_to_port(sdev->host);
460 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
461 enum sw_activity val;
462 int rc;
463
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800464 if (atadev && ap->ops->sw_activity_store &&
465 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700466 val = simple_strtoul(buf, NULL, 0);
467 switch (val) {
468 case OFF: case BLINK_ON: case BLINK_OFF:
469 rc = ap->ops->sw_activity_store(atadev, val);
470 if (!rc)
471 return count;
472 else
473 return rc;
474 }
475 }
476 return -EINVAL;
477}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200478DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700479 ata_scsi_activity_store);
480EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
481
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200482struct device_attribute *ata_common_sdev_attrs[] = {
483 &dev_attr_unload_heads,
Adam Manzanares84f95242016-10-17 11:27:30 -0700484 &dev_attr_ncq_prio_enable,
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200485 NULL
486};
487EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/**
490 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
491 * @sdev: SCSI device for which BIOS geometry is to be determined
492 * @bdev: block device associated with @sdev
493 * @capacity: capacity of SCSI device
494 * @geom: location to which geometry will be output
495 *
496 * Generic bios head/sector/cylinder calculator
497 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
498 * mapping. Some situations may arise where the disk is not
499 * bootable if this is not used.
500 *
501 * LOCKING:
502 * Defined by the SCSI layer. We don't really care.
503 *
504 * RETURNS:
505 * Zero.
506 */
507int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
508 sector_t capacity, int geom[])
509{
510 geom[0] = 255;
511 geom[1] = 63;
512 sector_div(capacity, 255*63);
513 geom[2] = capacity;
514
515 return 0;
516}
517
Jeff Garzikb0955182005-05-12 15:45:22 -0400518/**
Tejun Heod8d91292010-05-15 20:09:34 +0200519 * ata_scsi_unlock_native_capacity - unlock native capacity
520 * @sdev: SCSI device to adjust device capacity for
521 *
522 * This function is called if a partition on @sdev extends beyond
523 * the end of the device. It requests EH to unlock HPA.
524 *
525 * LOCKING:
526 * Defined by the SCSI layer. Might sleep.
527 */
528void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
529{
530 struct ata_port *ap = ata_shost_to_port(sdev->host);
531 struct ata_device *dev;
532 unsigned long flags;
533
534 spin_lock_irqsave(ap->lock, flags);
535
536 dev = ata_scsi_find_dev(ap, sdev);
537 if (dev && dev->n_sectors < dev->n_native_sectors) {
538 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
539 dev->link->eh_info.action |= ATA_EH_RESET;
540 ata_port_schedule_eh(ap);
541 }
542
543 spin_unlock_irqrestore(ap->lock, flags);
544 ata_port_wait_eh(ap);
545}
546
547/**
Tejun Heo5924b742007-01-02 20:20:07 +0900548 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
Randy Dunlap5eb66fe2009-01-20 16:28:59 -0800549 * @ap: target port
Tejun Heo5924b742007-01-02 20:20:07 +0900550 * @sdev: SCSI device to get identify data for
551 * @arg: User buffer area for identify data
552 *
553 * LOCKING:
554 * Defined by the SCSI layer. We don't really care.
555 *
556 * RETURNS:
557 * Zero on success, negative errno on error.
558 */
Jeff Garzik94be9a52009-01-16 10:17:09 -0500559static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
560 void __user *arg)
Tejun Heo5924b742007-01-02 20:20:07 +0900561{
Tejun Heo5924b742007-01-02 20:20:07 +0900562 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
563 u16 __user *dst = arg;
564 char buf[40];
565
566 if (!dev)
567 return -ENOMSG;
568
569 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
570 return -EFAULT;
571
572 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
573 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
574 return -EFAULT;
575
576 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
577 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
578 return -EFAULT;
579
580 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
581 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
582 return -EFAULT;
583
584 return 0;
585}
586
587/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400588 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800589 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400590 * @arg: User provided data for issuing command
591 *
592 * LOCKING:
593 * Defined by the SCSI layer. We don't really care.
594 *
595 * RETURNS:
596 * Zero on success, negative errno on error.
597 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400598int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
599{
600 int rc = 0;
601 u8 scsi_cmd[MAX_COMMAND_SIZE];
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700602 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400603 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600604 enum dma_data_direction data_dir;
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100605 struct scsi_sense_hdr sshdr;
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700606 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400607
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500608 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400609 return -EINVAL;
610
611 if (copy_from_user(args, arg, sizeof(args)))
612 return -EFAULT;
613
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700614 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
615 if (!sensebuf)
616 return -ENOMEM;
617
Jeff Garzikb0955182005-05-12 15:45:22 -0400618 memset(scsi_cmd, 0, sizeof(scsi_cmd));
619
620 if (args[3]) {
Grant Grundler295124d2010-08-17 10:56:53 -0700621 argsize = ATA_SECT_SIZE * args[3];
Jeff Garzikb0955182005-05-12 15:45:22 -0400622 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400623 if (argbuf == NULL) {
624 rc = -ENOMEM;
625 goto error;
626 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400627
628 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
629 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400630 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600631 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400632 } else {
633 scsi_cmd[1] = (3 << 1); /* Non-data */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700634 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600635 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400636 }
637
638 scsi_cmd[0] = ATA_16;
639
640 scsi_cmd[4] = args[2];
Bartlomiej Zolnierkiewicza4f19042008-10-10 22:39:20 +0200641 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
Jeff Garzikb0955182005-05-12 15:45:22 -0400642 scsi_cmd[6] = args[3];
643 scsi_cmd[8] = args[1];
644 scsi_cmd[10] = 0x4f;
645 scsi_cmd[12] = 0xc2;
646 } else {
647 scsi_cmd[6] = args[1];
648 }
649 scsi_cmd[14] = args[0];
650
651 /* Good values for timeout and retries? Values below
652 from scsi_ioctl_send_command() for default case... */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700653 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100654 sensebuf, &sshdr, (10*HZ), 5, 0, 0, NULL);
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700655
656 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
657 u8 *desc = sensebuf + 8;
658 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
659
660 /* If we set cc then ATA pass-through will cause a
661 * check condition even if no error. Filter that. */
662 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100663 if (sshdr.sense_key == RECOVERED_ERROR &&
664 sshdr.asc == 0 && sshdr.ascq == 0x1d)
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700665 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
666 }
667
668 /* Send userspace a few ATA registers (same as drivers/ide) */
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400669 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
670 desc[0] == 0x09) { /* code is "ATA Descriptor" */
671 args[0] = desc[13]; /* status */
672 args[1] = desc[3]; /* error */
673 args[2] = desc[5]; /* sector count (0:7) */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700674 if (copy_to_user(arg, args, sizeof(args)))
675 rc = -EFAULT;
676 }
677 }
678
679
680 if (cmd_result) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400681 rc = -EIO;
682 goto error;
683 }
684
Jeff Garzikb0955182005-05-12 15:45:22 -0400685 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500686 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400687 rc = -EFAULT;
688error:
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700689 kfree(sensebuf);
Jesper Juhl8f760782006-06-27 02:55:06 -0700690 kfree(argbuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400691 return rc;
692}
693
694/**
695 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800696 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400697 * @arg: User provided data for issuing command
698 *
699 * LOCKING:
700 * Defined by the SCSI layer. We don't really care.
701 *
702 * RETURNS:
703 * Zero on success, negative errno on error.
704 */
705int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
706{
707 int rc = 0;
708 u8 scsi_cmd[MAX_COMMAND_SIZE];
David Milburnaf068bd2007-01-30 00:59:15 -0800709 u8 args[7], *sensebuf = NULL;
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100710 struct scsi_sense_hdr sshdr;
David Milburnaf068bd2007-01-30 00:59:15 -0800711 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400712
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500713 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400714 return -EINVAL;
715
716 if (copy_from_user(args, arg, sizeof(args)))
717 return -EFAULT;
718
David Milburnaf068bd2007-01-30 00:59:15 -0800719 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
720 if (!sensebuf)
721 return -ENOMEM;
722
Jeff Garzikb0955182005-05-12 15:45:22 -0400723 memset(scsi_cmd, 0, sizeof(scsi_cmd));
724 scsi_cmd[0] = ATA_16;
725 scsi_cmd[1] = (3 << 1); /* Non-data */
David Milburnaf068bd2007-01-30 00:59:15 -0800726 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Jeff Garzikb0955182005-05-12 15:45:22 -0400727 scsi_cmd[4] = args[1];
728 scsi_cmd[6] = args[2];
729 scsi_cmd[8] = args[3];
730 scsi_cmd[10] = args[4];
731 scsi_cmd[12] = args[5];
Mark Lord277239f2007-03-30 17:45:52 -0400732 scsi_cmd[13] = args[6] & 0x4f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400733 scsi_cmd[14] = args[0];
734
Jeff Garzikb0955182005-05-12 15:45:22 -0400735 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500736 from scsi_ioctl_send_command() for default case... */
David Milburnaf068bd2007-01-30 00:59:15 -0800737 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100738 sensebuf, &sshdr, (10*HZ), 5, 0, 0, NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400739
David Milburnaf068bd2007-01-30 00:59:15 -0800740 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
741 u8 *desc = sensebuf + 8;
742 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
743
744 /* If we set cc then ATA pass-through will cause a
745 * check condition even if no error. Filter that. */
746 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100747 if (sshdr.sense_key == RECOVERED_ERROR &&
748 sshdr.asc == 0 && sshdr.ascq == 0x1d)
David Milburnaf068bd2007-01-30 00:59:15 -0800749 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
750 }
751
752 /* Send userspace ATA registers */
753 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
754 desc[0] == 0x09) {/* code is "ATA Descriptor" */
755 args[0] = desc[13]; /* status */
756 args[1] = desc[3]; /* error */
757 args[2] = desc[5]; /* sector count (0:7) */
758 args[3] = desc[7]; /* lbal */
759 args[4] = desc[9]; /* lbam */
760 args[5] = desc[11]; /* lbah */
761 args[6] = desc[12]; /* select */
762 if (copy_to_user(arg, args, sizeof(args)))
763 rc = -EFAULT;
764 }
765 }
766
767 if (cmd_result) {
768 rc = -EIO;
769 goto error;
770 }
771
772 error:
773 kfree(sensebuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400774 return rc;
775}
776
Alan Coxe3cf95d2009-04-09 17:31:17 +0100777static int ata_ioc32(struct ata_port *ap)
778{
779 if (ap->flags & ATA_FLAG_PIO_DMA)
780 return 1;
781 if (ap->pflags & ATA_PFLAG_PIO32)
782 return 1;
783 return 0;
784}
785
Jeff Garzik94be9a52009-01-16 10:17:09 -0500786int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
787 int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Arnd Bergmann287e6612016-02-11 14:16:27 +0100789 unsigned long val;
790 int rc = -EINVAL;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100791 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 switch (cmd) {
Arnd Bergmann287e6612016-02-11 14:16:27 +0100794 case HDIO_GET_32BIT:
Alan Coxe3cf95d2009-04-09 17:31:17 +0100795 spin_lock_irqsave(ap->lock, flags);
796 val = ata_ioc32(ap);
797 spin_unlock_irqrestore(ap->lock, flags);
Arnd Bergmann287e6612016-02-11 14:16:27 +0100798 return put_user(val, (unsigned long __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Arnd Bergmann287e6612016-02-11 14:16:27 +0100800 case HDIO_SET_32BIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 val = (unsigned long) arg;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100802 rc = 0;
803 spin_lock_irqsave(ap->lock, flags);
804 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
805 if (val)
806 ap->pflags |= ATA_PFLAG_PIO32;
807 else
808 ap->pflags &= ~ATA_PFLAG_PIO32;
809 } else {
810 if (val != ata_ioc32(ap))
811 rc = -EINVAL;
812 }
813 spin_unlock_irqrestore(ap->lock, flags);
814 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Tejun Heo5924b742007-01-02 20:20:07 +0900816 case HDIO_GET_IDENTITY:
Jeff Garzik94be9a52009-01-16 10:17:09 -0500817 return ata_get_identity(ap, scsidev, arg);
Tejun Heo5924b742007-01-02 20:20:07 +0900818
Jeff Garzikb0955182005-05-12 15:45:22 -0400819 case HDIO_DRIVE_CMD:
820 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
821 return -EACCES;
822 return ata_cmd_ioctl(scsidev, arg);
823
824 case HDIO_DRIVE_TASK:
825 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
826 return -EACCES;
827 return ata_task_ioctl(scsidev, arg);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 default:
830 rc = -ENOTTY;
831 break;
832 }
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return rc;
835}
Jeff Garzik94be9a52009-01-16 10:17:09 -0500836EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
837
838int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
839{
840 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
841 scsidev, cmd, arg);
842}
843EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845/**
846 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 * @dev: ATA device to which the new command is attached
848 * @cmd: SCSI command that originated this ATA command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 *
850 * Obtain a reference to an unused ata_queued_cmd structure,
851 * which is the basic libata structure representing a single
852 * ATA command sent to the hardware.
853 *
854 * If a command was available, fill in the SCSI-specific
855 * portions of the structure with information on the
856 * current command.
857 *
858 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400859 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 *
861 * RETURNS:
862 * Command allocated, or %NULL if none available.
863 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100864static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500865 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct ata_queued_cmd *qc;
868
Shaohua Li12cb5ce2015-01-15 17:32:27 -0800869 qc = ata_qc_new_init(dev, cmd->request->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (qc) {
871 qc->scsicmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500872 qc->scsidone = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Tejun Heoff2aeb12007-12-05 16:43:11 +0900874 qc->sg = scsi_sglist(cmd);
Boaz Harrosh71201652007-09-18 17:48:50 +0200875 qc->n_elem = scsi_sg_count(cmd);
Damien Le Moal7eb49502018-05-09 09:28:11 +0900876
877 if (cmd->request->rq_flags & RQF_QUIET)
878 qc->flags |= ATA_QCFLAG_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 } else {
880 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500881 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
884 return qc;
885}
886
Tejun Heoaacda372008-03-18 17:47:43 +0900887static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
888{
889 struct scsi_cmnd *scmd = qc->scsicmd;
890
891 qc->extrabytes = scmd->request->extra_len;
892 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400896 * ata_dump_status - user friendly display of error info
897 * @id: id of the port in question
898 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400900 * Decode and dump the ATA error/status registers for the user so
901 * that they have some idea what really happened at the non
902 * make-believe layer.
903 *
904 * LOCKING:
905 * inherited from caller
906 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100907static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
Jeff Garzikb0955182005-05-12 15:45:22 -0400908{
909 u8 stat = tf->command, err = tf->feature;
910
Joe Perches6ef56322017-06-12 12:17:39 -0700911 pr_warn("ata%u: status=0x%02x { ", id, stat);
Jeff Garzikb0955182005-05-12 15:45:22 -0400912 if (stat & ATA_BUSY) {
Joe Perches6ef56322017-06-12 12:17:39 -0700913 pr_cont("Busy }\n"); /* Data is not valid in this case */
Jeff Garzikb0955182005-05-12 15:45:22 -0400914 } else {
Joe Perches6ef56322017-06-12 12:17:39 -0700915 if (stat & ATA_DRDY) pr_cont("DriveReady ");
916 if (stat & ATA_DF) pr_cont("DeviceFault ");
917 if (stat & ATA_DSC) pr_cont("SeekComplete ");
918 if (stat & ATA_DRQ) pr_cont("DataRequest ");
919 if (stat & ATA_CORR) pr_cont("CorrectedError ");
920 if (stat & ATA_SENSE) pr_cont("Sense ");
921 if (stat & ATA_ERR) pr_cont("Error ");
922 pr_cont("}\n");
Jeff Garzikb0955182005-05-12 15:45:22 -0400923
924 if (err) {
Joe Perches6ef56322017-06-12 12:17:39 -0700925 pr_warn("ata%u: error=0x%02x { ", id, err);
926 if (err & ATA_ABORTED) pr_cont("DriveStatusError ");
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100927 if (err & ATA_ICRC) {
928 if (err & ATA_ABORTED)
Joe Perches6ef56322017-06-12 12:17:39 -0700929 pr_cont("BadCRC ");
930 else pr_cont("Sector ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400931 }
Joe Perches6ef56322017-06-12 12:17:39 -0700932 if (err & ATA_UNC) pr_cont("UncorrectableError ");
933 if (err & ATA_IDNF) pr_cont("SectorIdNotFound ");
934 if (err & ATA_TRK0NF) pr_cont("TrackZeroNotFound ");
935 if (err & ATA_AMNF) pr_cont("AddrMarkNotFound ");
936 pr_cont("}\n");
Jeff Garzikb0955182005-05-12 15:45:22 -0400937 }
938 }
939}
940
941/**
942 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800943 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400944 * @drv_stat: value contained in ATA status register
945 * @drv_err: value contained in ATA error register
946 * @sk: the sense key we'll fill out
947 * @asc: the additional sense code we'll fill out
948 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900949 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400950 *
951 * Converts an ATA error into a SCSI error. Fill out pointers to
952 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
953 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 *
955 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400956 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100958static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
959 u8 *asc, u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jeff Garzikb0955182005-05-12 15:45:22 -0400961 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100964 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* BBD|ECC|ID|MAR */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100966 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
967 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* BBD|ECC|ID */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100969 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
970 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* ECC|MC|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100972 {0x61, HARDWARE_ERROR, 0x00, 0x00},
973 // Device fault Hardware error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100975 {0x84, ABORTED_COMMAND, 0x47, 0x00},
976 // Data CRC error SCSI parity error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* MC|ID|ABRT|TRK0|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100978 {0x37, NOT_READY, 0x04, 0x00},
979 // Unit offline Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* MCR|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100981 {0x09, NOT_READY, 0x04, 0x00},
982 // Unrecovered disk error Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* Bad address mark */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100984 {0x01, MEDIUM_ERROR, 0x13, 0x00},
985 // Address mark not found for data field
986 /* TRK0 - Track 0 not found */
987 {0x02, HARDWARE_ERROR, 0x00, 0x00},
988 // Hardware error
Gwendal Grignou78062c52013-06-18 10:54:48 -0700989 /* Abort: 0x04 is not translated here, see below */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* Media change request */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100991 {0x08, NOT_READY, 0x04, 0x00},
992 // FIXME: faking offline
993 /* SRV/IDNF - ID not found */
994 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
995 // Logical address out of range
996 /* MC - Media Changed */
997 {0x20, UNIT_ATTENTION, 0x28, 0x00},
998 // Not ready to ready change, medium may have changed
999 /* ECC - Uncorrectable ECC error */
1000 {0x40, MEDIUM_ERROR, 0x11, 0x04},
1001 // Unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* BBD - block marked bad */
Hannes Reinecke8ae72042015-03-27 16:46:32 +01001003 {0x80, MEDIUM_ERROR, 0x11, 0x04},
1004 // Block marked bad Medium error, unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
1006 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01001007 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* Must be first because BUSY means no other bits valid */
Hannes Reinecke8ae72042015-03-27 16:46:32 +01001009 {0x80, ABORTED_COMMAND, 0x47, 0x00},
1010 // Busy, fake parity for now
1011 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
1012 // Device ready, unaligned write command
1013 {0x20, HARDWARE_ERROR, 0x44, 0x00},
1014 // Device fault, internal target failure
1015 {0x08, ABORTED_COMMAND, 0x47, 0x00},
1016 // Timed out in xfer, fake parity for now
1017 {0x04, RECOVERED_ERROR, 0x11, 0x00},
1018 // Recovered ECC error Medium error, recovered
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
1020 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /*
1023 * Is this an error we can process/parse
1024 */
Jeff Garzikb0955182005-05-12 15:45:22 -04001025 if (drv_stat & ATA_BUSY) {
1026 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Jeff Garzikb0955182005-05-12 15:45:22 -04001029 if (drv_err) {
1030 /* Look for drv_err */
1031 for (i = 0; sense_table[i][0] != 0xFF; i++) {
1032 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001033 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -04001034 sense_table[i][0]) {
1035 *sk = sense_table[i][1];
1036 *asc = sense_table[i][2];
1037 *ascq = sense_table[i][3];
1038 goto translate_done;
1039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
Gwendal Grignou78062c52013-06-18 10:54:48 -07001043 /*
1044 * Fall back to interpreting status bits. Note that if the drv_err
1045 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
1046 * is not descriptive enough.
1047 */
Jeff Garzikb0955182005-05-12 15:45:22 -04001048 for (i = 0; stat_table[i][0] != 0xFF; i++) {
1049 if (stat_table[i][0] & drv_stat) {
1050 *sk = stat_table[i][1];
1051 *asc = stat_table[i][2];
1052 *ascq = stat_table[i][3];
1053 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Gwendal Grignou78062c52013-06-18 10:54:48 -07001057 /*
1058 * We need a sensible error return here, which is tricky, and one
1059 * that won't cause people to do things like return a disk wrongly.
1060 */
Alan Cox2d202022006-03-21 15:53:46 +00001061 *sk = ABORTED_COMMAND;
1062 *asc = 0x00;
1063 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -04001064
1065 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +09001066 if (verbose)
Joe Perches6ef56322017-06-12 12:17:39 -07001067 pr_err("ata%u: translated ATA stat/err 0x%02x/%02x to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
Tejun Heo246619d2006-05-15 20:58:16 +09001068 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -04001069 return;
1070}
1071
1072/*
Tejun Heo750426a2006-11-14 22:37:35 +09001073 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -04001074 * @qc: Command that completed.
1075 *
1076 * This function is specific to the ATA descriptor format sense
1077 * block specified for the ATA pass through commands. Regardless
1078 * of whether the command errored or not, return a sense
1079 * block. Copy all controller registers into the sense
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001080 * block. If there was no error, we get the request from an ATA
1081 * passthrough command, so we use the following sense data:
1082 * sk = RECOVERED ERROR
1083 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
1084 *
Jeff Garzikb0955182005-05-12 15:45:22 -04001085 *
1086 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001087 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001088 */
Tejun Heo750426a2006-11-14 22:37:35 +09001089static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001090{
1091 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001092 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001093 unsigned char *sb = cmd->sense_buffer;
1094 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +09001095 int verbose = qc->ap->ops->error_handler == NULL;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001096 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001097
1098 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1099
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001100 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001101
1102 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04001103 * Use ata_to_sense_error() to map status register bits
1104 * onto sense key, asc & ascq.
1105 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001106 if (qc->err_mask ||
1107 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001108 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001109 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001110 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001111 } else {
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001112 /*
1113 * ATA PASS-THROUGH INFORMATION AVAILABLE
1114 * Always in descriptor format sense.
1115 */
1116 scsi_build_sense_buffer(1, cmd->sense_buffer,
1117 RECOVERED_ERROR, 0, 0x1D);
Jeff Garzikb0955182005-05-12 15:45:22 -04001118 }
1119
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001120 if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
1121 u8 len;
Jeff Garzikb0955182005-05-12 15:45:22 -04001122
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001123 /* descriptor format */
1124 len = sb[7];
1125 desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
1126 if (!desc) {
1127 if (SCSI_SENSE_BUFFERSIZE < len + 14)
1128 return;
1129 sb[7] = len + 14;
1130 desc = sb + 8 + len;
1131 }
1132 desc[0] = 9;
1133 desc[1] = 12;
1134 /*
1135 * Copy registers into sense buffer.
1136 */
1137 desc[2] = 0x00;
1138 desc[3] = tf->feature; /* == error reg */
1139 desc[5] = tf->nsect;
1140 desc[7] = tf->lbal;
1141 desc[9] = tf->lbam;
1142 desc[11] = tf->lbah;
1143 desc[12] = tf->device;
1144 desc[13] = tf->command; /* == status reg */
Jeff Garzikb0955182005-05-12 15:45:22 -04001145
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001146 /*
1147 * Fill in Extend bit, and the high order bytes
1148 * if applicable.
1149 */
1150 if (tf->flags & ATA_TFLAG_LBA48) {
1151 desc[2] |= 0x01;
1152 desc[4] = tf->hob_nsect;
1153 desc[6] = tf->hob_lbal;
1154 desc[8] = tf->hob_lbam;
1155 desc[10] = tf->hob_lbah;
1156 }
1157 } else {
1158 /* Fixed sense format */
1159 desc[0] = tf->feature;
1160 desc[1] = tf->command; /* status */
1161 desc[2] = tf->device;
1162 desc[3] = tf->nsect;
Hannes Reineckee0029dc2016-10-31 21:06:58 +01001163 desc[7] = 0;
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001164 if (tf->flags & ATA_TFLAG_LBA48) {
1165 desc[8] |= 0x80;
1166 if (tf->hob_nsect)
1167 desc[8] |= 0x40;
1168 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
1169 desc[8] |= 0x20;
1170 }
1171 desc[9] = tf->lbal;
1172 desc[10] = tf->lbam;
1173 desc[11] = tf->lbah;
Jeff Garzikb0955182005-05-12 15:45:22 -04001174 }
1175}
1176
1177/**
Tejun Heo750426a2006-11-14 22:37:35 +09001178 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -04001179 * @qc: Command that we are erroring out
1180 *
Tejun Heod25614b2006-11-14 22:37:35 +09001181 * Generate sense block for a failed ATA command @qc. Descriptor
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001182 * format is used to accommodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -04001183 *
1184 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001185 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001186 */
Tejun Heo750426a2006-11-14 22:37:35 +09001187static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001188{
Tejun Heod25614b2006-11-14 22:37:35 +09001189 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -04001190 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001191 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001192 unsigned char *sb = cmd->sense_buffer;
Tejun Heo246619d2006-05-15 20:58:16 +09001193 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +09001194 u64 block;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001195 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001196
1197 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1198
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001199 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001200
Hannes Reinecke59f68472016-04-25 12:45:50 +02001201 if (ata_dev_disabled(dev)) {
1202 /* Device disabled after error recovery */
1203 /* LOGICAL UNIT NOT READY, HARD RESET REQUIRED */
1204 ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
1205 return;
1206 }
Tejun Heod25614b2006-11-14 22:37:35 +09001207 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -04001208 * onto sense key, asc & ascq.
1209 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001210 if (qc->err_mask ||
1211 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001212 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001213 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001214 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001215 } else {
1216 /* Could not decode error */
1217 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
1218 tf->command, qc->err_mask);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001219 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001220 return;
Jeff Garzikb0955182005-05-12 15:45:22 -04001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Tejun Heod25614b2006-11-14 22:37:35 +09001223 block = ata_tf_read_block(&qc->result_tf, dev);
Hannes Reineckecffd1ee2016-04-04 11:43:57 +02001224 if (block == U64_MAX)
1225 return;
Jeff Garzik0274aa22005-06-22 13:50:56 -04001226
Hannes Reineckecf8b49b2016-04-04 11:43:58 +02001227 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Brian Kinga6cce2a2006-03-17 17:04:15 -06001230static void ata_scsi_sdev_config(struct scsi_device *sdev)
1231{
1232 sdev->use_10_for_rw = 1;
1233 sdev->use_10_for_ms = 1;
Nicolai Stangee1859342016-12-07 22:21:33 +01001234 sdev->no_write_same = 1;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001235
1236 /* Schedule policy is determined by ->qc_defer() callback and
1237 * it needs to see every deferred qc. Set dev_blocked to 1 to
1238 * prevent SCSI midlayer from automatically deferring
1239 * requests.
1240 */
1241 sdev->max_device_blocked = 1;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001242}
1243
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001244/**
1245 * atapi_drain_needed - Check whether data transfer may overflow
Randy Dunlap73fd8b62008-02-19 13:43:21 -08001246 * @rq: request to be checked
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001247 *
1248 * ATAPI commands which transfer variable length data to host
Masanari Iidac9b55602016-04-13 23:36:27 +09001249 * might overflow due to application error or hardware bug. This
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001250 * function checks whether overflow should be drained and ignored
1251 * for @request.
1252 *
1253 * LOCKING:
1254 * None.
1255 *
1256 * RETURNS:
1257 * 1 if ; otherwise, 0.
1258 */
1259static int atapi_drain_needed(struct request *rq)
1260{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001261 if (likely(!blk_rq_is_passthrough(rq)))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001262 return 0;
1263
Mike Christiea8ebb052016-06-05 14:31:45 -05001264 if (!blk_rq_bytes(rq) || op_is_write(req_op(rq)))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001265 return 0;
1266
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001267 return atapi_cmd_type(scsi_req(rq)->cmd[0]) == ATAPI_MISC;
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001268}
1269
1270static int ata_scsi_dev_config(struct scsi_device *sdev,
1271 struct ata_device *dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -06001272{
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001273 struct request_queue *q = sdev->request_queue;
1274
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02001275 if (!ata_id_has_unload(dev->id))
1276 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1277
Tejun Heo914ed352006-11-01 18:39:55 +09001278 /* configure max sectors */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001279 blk_queue_max_hw_sectors(q, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -06001280
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001281 if (dev->class == ATA_DEV_ATAPI) {
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001282 void *buf;
1283
Tejun Heo729a6a32011-01-20 13:59:06 +01001284 sdev->sector_size = ATA_SECT_SIZE;
1285
1286 /* set DMA padding */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001287 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001288
1289 /* configure draining */
1290 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1291 if (!buf) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001292 ata_dev_err(dev, "drain buffer allocation failed\n");
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001293 return -ENOMEM;
1294 }
1295
1296 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1297 } else {
Grant Grundler295124d2010-08-17 10:56:53 -07001298 sdev->sector_size = ata_id_logical_sector_size(dev->id);
Tejun Heod8cf5382008-01-15 08:46:59 +09001299 sdev->manage_start_stop = 1;
James Bottomleydde20202008-02-19 11:36:56 +01001300 }
Tejun Heod8cf5382008-01-15 08:46:59 +09001301
Tejun Heo729a6a32011-01-20 13:59:06 +01001302 /*
1303 * ata_pio_sectors() expects buffer for each sector to not cross
1304 * page boundary. Enforce it by requiring buffers to be sector
1305 * aligned, which works iff sector_size is not larger than
1306 * PAGE_SIZE. ATAPI devices also need the alignment as
1307 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1308 */
1309 if (sdev->sector_size > PAGE_SIZE)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001310 ata_dev_warn(dev,
Tejun Heo729a6a32011-01-20 13:59:06 +01001311 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1312 sdev->sector_size);
1313
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001314 blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
Tejun Heo729a6a32011-01-20 13:59:06 +01001315
Jeff Garzikf26792d2007-10-29 17:18:39 -04001316 if (dev->flags & ATA_DFLAG_AN)
1317 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1318
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001319 if (dev->flags & ATA_DFLAG_NCQ) {
1320 int depth;
1321
1322 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
Jens Axboe69278f72018-05-11 12:51:10 -06001323 depth = min(ATA_MAX_QUEUE, depth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001324 scsi_change_queue_depth(sdev, depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001325 }
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001326
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001327 blk_queue_flush_queueable(q, false);
1328
Christoph Hellwigd80210f2017-06-19 14:26:46 +02001329 if (dev->flags & ATA_DFLAG_TRUSTED)
1330 sdev->security_supported = 1;
1331
Grant Grundler295124d2010-08-17 10:56:53 -07001332 dev->sdev = sdev;
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001333 return 0;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001334}
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336/**
1337 * ata_scsi_slave_config - Set SCSI device attributes
1338 * @sdev: SCSI device to examine
1339 *
1340 * This is called before we actually start reading
1341 * and writing to the device, to configure certain
1342 * SCSI mid-layer behaviors.
1343 *
1344 * LOCKING:
1345 * Defined by SCSI layer. We don't really care.
1346 */
1347
1348int ata_scsi_slave_config(struct scsi_device *sdev)
1349{
Tejun Heo31534362006-05-31 18:27:36 +09001350 struct ata_port *ap = ata_shost_to_port(sdev->host);
1351 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001352 int rc = 0;
Tejun Heo31534362006-05-31 18:27:36 +09001353
Brian Kinga6cce2a2006-03-17 17:04:15 -06001354 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Tejun Heo31534362006-05-31 18:27:36 +09001356 if (dev)
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001357 rc = ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001359 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362/**
Tejun Heo83c47bc2006-05-31 18:28:07 +09001363 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1364 * @sdev: SCSI device to be destroyed
1365 *
1366 * @sdev is about to be destroyed for hot/warm unplugging. If
1367 * this unplugging was initiated by libata as indicated by NULL
1368 * dev->sdev, this function doesn't have to do anything.
1369 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1370 * Clear dev->sdev, schedule the device for ATA detach and invoke
1371 * EH.
1372 *
1373 * LOCKING:
1374 * Defined by SCSI layer. We don't really care.
1375 */
1376void ata_scsi_slave_destroy(struct scsi_device *sdev)
1377{
1378 struct ata_port *ap = ata_shost_to_port(sdev->host);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001379 struct request_queue *q = sdev->request_queue;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001380 unsigned long flags;
1381 struct ata_device *dev;
1382
1383 if (!ap->ops->error_handler)
1384 return;
1385
Jeff Garzikba6a1302006-06-22 23:46:10 -04001386 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09001387 dev = __ata_scsi_find_dev(ap, sdev);
1388 if (dev && dev->sdev) {
1389 /* SCSI device already in CANCEL state, no need to offline it */
1390 dev->sdev = NULL;
1391 dev->flags |= ATA_DFLAG_DETACH;
1392 ata_port_schedule_eh(ap);
1393 }
Jeff Garzikba6a1302006-06-22 23:46:10 -04001394 spin_unlock_irqrestore(ap->lock, flags);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001395
1396 kfree(q->dma_drain_buffer);
1397 q->dma_drain_buffer = NULL;
1398 q->dma_drain_size = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001399}
1400
1401/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001402 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
Marcos Paulo de Souza6d2dd052011-10-31 23:50:16 -02001403 * @ap: ATA port to which the device change the queue depth
1404 * @sdev: SCSI device to configure queue depth for
1405 * @queue_depth: new queue depth
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001406 *
Dan Williamsf6e67032011-09-20 15:10:33 -07001407 * libsas and libata have different approaches for associating a sdev to
1408 * its ata_port.
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001409 *
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001410 */
Dan Williamsf6e67032011-09-20 15:10:33 -07001411int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001412 int queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001413{
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001414 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +09001415 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001416
Tejun Heoc3c70c42007-02-20 23:31:22 +09001417 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001418 return sdev->queue_depth;
1419
1420 dev = ata_scsi_find_dev(ap, sdev);
1421 if (!dev || !ata_dev_enabled(dev))
1422 return sdev->queue_depth;
1423
Tejun Heoc3c70c42007-02-20 23:31:22 +09001424 /* NCQ enabled? */
Tejun Heo360f6542006-09-30 19:45:00 +09001425 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001426 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1427 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
Tejun Heo360f6542006-09-30 19:45:00 +09001428 dev->flags |= ATA_DFLAG_NCQ_OFF;
Tejun Heoc3c70c42007-02-20 23:31:22 +09001429 queue_depth = 1;
1430 }
Tejun Heo360f6542006-09-30 19:45:00 +09001431 spin_unlock_irqrestore(ap->lock, flags);
1432
Tejun Heoc3c70c42007-02-20 23:31:22 +09001433 /* limit and apply queue depth */
1434 queue_depth = min(queue_depth, sdev->host->can_queue);
1435 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
Jens Axboe69278f72018-05-11 12:51:10 -06001436 queue_depth = min(queue_depth, ATA_MAX_QUEUE);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001437
1438 if (sdev->queue_depth == queue_depth)
1439 return -EINVAL;
1440
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001441 return scsi_change_queue_depth(sdev, queue_depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001442}
1443
1444/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001445 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1446 * @sdev: SCSI device to configure queue depth for
1447 * @queue_depth: new queue depth
Dan Williamsf6e67032011-09-20 15:10:33 -07001448 *
1449 * This is libata standard hostt->change_queue_depth callback.
1450 * SCSI will call into this callback when user tries to set queue
1451 * depth via sysfs.
1452 *
1453 * LOCKING:
1454 * SCSI layer (we don't care)
1455 *
1456 * RETURNS:
1457 * Newly configured queue depth.
1458 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001459int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
Dan Williamsf6e67032011-09-20 15:10:33 -07001460{
1461 struct ata_port *ap = ata_shost_to_port(sdev->host);
1462
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001463 return __ata_change_queue_depth(ap, sdev, queue_depth);
Dan Williamsf6e67032011-09-20 15:10:33 -07001464}
1465
1466/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001467 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1468 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001469 *
1470 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1471 * (to start). Perhaps these commands should be preceded by
1472 * CHECK POWER MODE to see what power mode the device is already in.
1473 * [See SAT revision 5 at www.t10.org]
1474 *
1475 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001476 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001477 *
1478 * RETURNS:
1479 * Zero on success, non-zero on error.
1480 */
Tejun Heoad706992006-12-17 10:45:57 +09001481static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001482{
Tejun Heo542b1442006-12-17 10:45:08 +09001483 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001484 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +09001485 const u8 *cdb = scmd->cmnd;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001486 u16 fp;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001487 u8 bp = 0xff;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001488
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001489 if (scmd->cmd_len < 5) {
1490 fp = 4;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001491 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001492 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09001493
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001494 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1495 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +09001496 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001497 ; /* ignore IMMED bit, violates sat-r05 */
1498 }
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001499 if (cdb[4] & 0x2) {
1500 fp = 4;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001501 bp = 1;
Douglas Gilbertae006512005-10-09 09:09:35 -04001502 goto invalid_fld; /* LOEJ bit set not supported */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001503 }
1504 if (((cdb[4] >> 4) & 0xf) != 0) {
1505 fp = 4;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001506 bp = 3;
Douglas Gilbertae006512005-10-09 09:09:35 -04001507 goto invalid_fld; /* power conditions not supported */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001508 }
Tejun Heoe31e85312007-09-23 13:14:13 +09001509
Tejun Heo542b1442006-12-17 10:45:08 +09001510 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001511 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -04001512
1513 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +09001514 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -04001515
1516 tf->lbah = 0x0;
1517 tf->lbam = 0x0;
1518 tf->lbal = 0x0;
1519 tf->device |= ATA_LBA;
1520 } else {
1521 /* CHS */
1522 tf->lbal = 0x1; /* sect */
1523 tf->lbam = 0x0; /* cyl low */
1524 tf->lbah = 0x0; /* cyl high */
1525 }
1526
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001527 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
Tejun Heo920a4b12007-05-04 21:28:48 +02001528 } else {
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001529 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1530 * or S5) causing some drives to spin up and down again.
1531 */
1532 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1533 system_state == SYSTEM_POWER_OFF)
1534 goto skip;
1535
1536 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1537 system_entering_hibernation())
1538 goto skip;
1539
Robert Hancock78981a72007-01-30 00:59:18 -08001540 /* Issue ATA STANDBY IMMEDIATE command */
1541 tf->command = ATA_CMD_STANDBYNOW1;
Tejun Heo920a4b12007-05-04 21:28:48 +02001542 }
Robert Hancock78981a72007-01-30 00:59:18 -08001543
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001544 /*
1545 * Standby and Idle condition timers could be implemented but that
1546 * would require libata to implement the Power condition mode page
1547 * and allow the user to change it. Changing mode pages requires
1548 * MODE SELECT to be implemented.
1549 */
1550
1551 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001552
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001553 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001554 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
Douglas Gilbertae006512005-10-09 09:09:35 -04001555 return 1;
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001556 skip:
1557 scmd->result = SAM_STAT_GOOD;
1558 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001559}
1560
1561
1562/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1564 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 *
1566 * Sets up an ATA taskfile to issue FLUSH CACHE or
1567 * FLUSH CACHE EXT.
1568 *
1569 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001570 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 *
1572 * RETURNS:
1573 * Zero on success, non-zero on error.
1574 */
Tejun Heoad706992006-12-17 10:45:57 +09001575static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 struct ata_taskfile *tf = &qc->tf;
1578
1579 tf->flags |= ATA_TFLAG_DEVICE;
1580 tf->protocol = ATA_PROT_NODATA;
1581
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001582 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 tf->command = ATA_CMD_FLUSH_EXT;
1584 else
1585 tf->command = ATA_CMD_FLUSH;
1586
Tejun Heob666da32007-10-26 15:53:59 +09001587 /* flush is critical for IO integrity, consider it an IO command */
1588 qc->flags |= ATA_QCFLAG_IO;
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return 0;
1591}
1592
1593/**
Albert Lee3aef5232005-10-04 08:47:43 -04001594 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001595 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001596 *
1597 * Calculate LBA and transfer length for 6-byte commands.
1598 *
1599 * RETURNS:
1600 * @plba: the LBA
1601 * @plen: the transfer length
1602 */
Tejun Heo542b1442006-12-17 10:45:08 +09001603static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001604{
1605 u64 lba = 0;
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001606 u32 len;
Albert Lee3aef5232005-10-04 08:47:43 -04001607
1608 VPRINTK("six-byte command\n");
1609
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001610 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
Tejun Heo542b1442006-12-17 10:45:08 +09001611 lba |= ((u64)cdb[2]) << 8;
1612 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001613
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001614 len = cdb[4];
Albert Lee3aef5232005-10-04 08:47:43 -04001615
1616 *plba = lba;
1617 *plen = len;
1618}
1619
1620/**
1621 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001622 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001623 *
1624 * Calculate LBA and transfer length for 10-byte commands.
1625 *
1626 * RETURNS:
1627 * @plba: the LBA
1628 * @plen: the transfer length
1629 */
Tejun Heo542b1442006-12-17 10:45:08 +09001630static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001631{
1632 u64 lba = 0;
1633 u32 len = 0;
1634
1635 VPRINTK("ten-byte command\n");
1636
Tejun Heo542b1442006-12-17 10:45:08 +09001637 lba |= ((u64)cdb[2]) << 24;
1638 lba |= ((u64)cdb[3]) << 16;
1639 lba |= ((u64)cdb[4]) << 8;
1640 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001641
Tejun Heo542b1442006-12-17 10:45:08 +09001642 len |= ((u32)cdb[7]) << 8;
1643 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001644
1645 *plba = lba;
1646 *plen = len;
1647}
1648
1649/**
1650 * scsi_16_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001651 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001652 *
1653 * Calculate LBA and transfer length for 16-byte commands.
1654 *
1655 * RETURNS:
1656 * @plba: the LBA
1657 * @plen: the transfer length
1658 */
Tejun Heo542b1442006-12-17 10:45:08 +09001659static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001660{
1661 u64 lba = 0;
1662 u32 len = 0;
1663
1664 VPRINTK("sixteen-byte command\n");
1665
Tejun Heo542b1442006-12-17 10:45:08 +09001666 lba |= ((u64)cdb[2]) << 56;
1667 lba |= ((u64)cdb[3]) << 48;
1668 lba |= ((u64)cdb[4]) << 40;
1669 lba |= ((u64)cdb[5]) << 32;
1670 lba |= ((u64)cdb[6]) << 24;
1671 lba |= ((u64)cdb[7]) << 16;
1672 lba |= ((u64)cdb[8]) << 8;
1673 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001674
Tejun Heo542b1442006-12-17 10:45:08 +09001675 len |= ((u32)cdb[10]) << 24;
1676 len |= ((u32)cdb[11]) << 16;
1677 len |= ((u32)cdb[12]) << 8;
1678 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001679
1680 *plba = lba;
1681 *plen = len;
1682}
1683
1684/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1686 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 *
1688 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1689 *
1690 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001691 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 *
1693 * RETURNS:
1694 * Zero on success, non-zero on error.
1695 */
Tejun Heoad706992006-12-17 10:45:57 +09001696static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Tejun Heo542b1442006-12-17 10:45:08 +09001698 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001700 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001702 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001703 u64 block;
1704 u32 n_block;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001705 u16 fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1708 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Tejun Heo2e5704f2006-12-17 10:46:33 +09001710 if (cdb[0] == VERIFY) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001711 if (scmd->cmd_len < 10) {
1712 fp = 9;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001713 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001714 }
Tejun Heo542b1442006-12-17 10:45:08 +09001715 scsi_10_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001716 } else if (cdb[0] == VERIFY_16) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001717 if (scmd->cmd_len < 16) {
1718 fp = 15;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001719 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001720 }
Tejun Heo542b1442006-12-17 10:45:08 +09001721 scsi_16_lba_len(cdb, &block, &n_block);
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001722 } else {
1723 fp = 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001724 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Albert Lee8bf62ece2005-05-12 15:29:42 -04001727 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001728 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001729 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001730 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001731 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001732 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Albert Lee07506692005-10-12 15:04:18 +08001734 if (dev->flags & ATA_DFLAG_LBA) {
1735 tf->flags |= ATA_TFLAG_LBA;
1736
Albert Leec6a33e22005-10-12 15:12:26 +08001737 if (lba_28_ok(block, n_block)) {
1738 /* use LBA28 */
1739 tf->command = ATA_CMD_VERIFY;
1740 tf->device |= (block >> 24) & 0xf;
1741 } else if (lba_48_ok(block, n_block)) {
1742 if (!(dev->flags & ATA_DFLAG_LBA48))
1743 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001744
1745 /* use LBA48 */
1746 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001747 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Albert Lee8bf62ece2005-05-12 15:29:42 -04001749 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Albert Lee8bf62ece2005-05-12 15:29:42 -04001751 tf->hob_lbah = (block >> 40) & 0xff;
1752 tf->hob_lbam = (block >> 32) & 0xff;
1753 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001754 } else
1755 /* request too large even for LBA48 */
1756 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001757
1758 tf->nsect = n_block & 0xff;
1759
1760 tf->lbah = (block >> 16) & 0xff;
1761 tf->lbam = (block >> 8) & 0xff;
1762 tf->lbal = block & 0xff;
1763
1764 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001766 /* CHS */
1767 u32 sect, head, cyl, track;
1768
Albert Leec6a33e22005-10-12 15:12:26 +08001769 if (!lba_28_ok(block, n_block))
1770 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001771
Albert Lee8bf62ece2005-05-12 15:29:42 -04001772 /* Convert LBA to CHS */
1773 track = (u32)block / dev->sectors;
1774 cyl = track / dev->heads;
1775 head = track % dev->heads;
1776 sect = (u32)block % dev->sectors + 1;
1777
Albert Leec187c4b2005-10-04 08:46:51 -04001778 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1779 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001780
1781 /* Check whether the converted CHS can fit.
1782 Cylinder: 0-65535
Albert Lee8bf62ece2005-05-12 15:29:42 -04001783 Head: 0-15
1784 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001785 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001786 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001789 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1790 tf->lbal = sect;
1791 tf->lbam = cyl;
1792 tf->lbah = cyl >> 8;
1793 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001797
1798invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001799 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
Douglas Gilbertae006512005-10-09 09:09:35 -04001800 return 1;
1801
1802out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001803 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001804 /* "Logical Block Address out of range" */
1805 return 1;
1806
1807nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001808 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001809 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
1812/**
1813 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1814 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 *
1816 * Converts any of six SCSI read/write commands into the
1817 * ATA counterpart, including starting sector (LBA),
1818 * sector count, and taking into account the device's LBA48
1819 * support.
1820 *
1821 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1822 * %WRITE_16 are currently supported.
1823 *
1824 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001825 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 *
1827 * RETURNS:
1828 * Zero on success, non-zero on error.
1829 */
Tejun Heoad706992006-12-17 10:45:57 +09001830static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Tejun Heo542b1442006-12-17 10:45:08 +09001832 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001833 const u8 *cdb = scmd->cmnd;
Adam Manzanares8e061782016-10-17 11:27:29 -07001834 struct request *rq = scmd->request;
1835 int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
Tejun Heobd056d72006-11-14 22:47:10 +09001836 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001837 u64 block;
1838 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001839 int rc;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001840 u16 fp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Tejun Heo542b1442006-12-17 10:45:08 +09001842 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001843 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001845 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001846 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001847 case READ_10:
1848 case WRITE_10:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001849 if (unlikely(scmd->cmd_len < 10)) {
1850 fp = 9;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001851 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001852 }
Tejun Heo542b1442006-12-17 10:45:08 +09001853 scsi_10_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001854 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001855 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001856 break;
1857 case READ_6:
1858 case WRITE_6:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001859 if (unlikely(scmd->cmd_len < 6)) {
1860 fp = 5;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001861 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001862 }
Tejun Heo542b1442006-12-17 10:45:08 +09001863 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001864
1865 /* for 6-byte r/w commands, transfer length 0
1866 * means 256 blocks of data, not 0 block.
1867 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001868 if (!n_block)
1869 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001870 break;
1871 case READ_16:
1872 case WRITE_16:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001873 if (unlikely(scmd->cmd_len < 16)) {
1874 fp = 15;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001875 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001876 }
Tejun Heo542b1442006-12-17 10:45:08 +09001877 scsi_16_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001878 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001879 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001880 break;
1881 default:
Albert Lee8bf62ece2005-05-12 15:29:42 -04001882 DPRINTK("no-byte command\n");
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001883 fp = 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001884 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886
Albert Lee8bf62ece2005-05-12 15:29:42 -04001887 /* Check and compose ATA command */
1888 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001889 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1890 * length 0 means transfer 0 block of data.
1891 * However, for ATA R/W commands, sector count 0 means
1892 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001893 *
1894 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001895 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001896 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001897
Tejun Heobd056d72006-11-14 22:47:10 +09001898 qc->flags |= ATA_QCFLAG_IO;
Grant Grundler295124d2010-08-17 10:56:53 -07001899 qc->nbytes = n_block * scmd->device->sector_size;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001900
Tejun Heobd056d72006-11-14 22:47:10 +09001901 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
Jens Axboe4e5b6262018-05-11 12:51:04 -06001902 qc->hw_tag, class);
Adam Manzanares8e061782016-10-17 11:27:29 -07001903
Tejun Heobd056d72006-11-14 22:47:10 +09001904 if (likely(rc == 0))
1905 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001906
Tejun Heobd056d72006-11-14 22:47:10 +09001907 if (rc == -ERANGE)
1908 goto out_of_range;
1909 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001910invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001911 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
Douglas Gilbertae006512005-10-09 09:09:35 -04001912 return 1;
1913
1914out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001915 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001916 /* "Logical Block Address out of range" */
1917 return 1;
1918
1919nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001920 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001921 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001924static void ata_qc_done(struct ata_queued_cmd *qc)
1925{
1926 struct scsi_cmnd *cmd = qc->scsicmd;
1927 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1928
1929 ata_qc_free(qc);
1930 done(cmd);
1931}
1932
Tejun Heo77853bf2006-01-23 13:09:36 +09001933static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Tejun Heoc31f5712006-11-22 12:39:43 +09001935 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001937 u8 *cdb = cmd->cmnd;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001938 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Jeff Garzikb0955182005-05-12 15:45:22 -04001940 /* For ATA pass thru (SAT) commands, generate a sense block if
1941 * user mandated it or if there's an error. Note that if we
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001942 * generate because the user forced us to [CK_COND =1], a check
1943 * condition is generated and the ATA register values are returned
Jeff Garzikb0955182005-05-12 15:45:22 -04001944 * whether the command completed successfully or not. If there
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001945 * was no error, we use the following sense data:
1946 * sk = RECOVERED ERROR
1947 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
Jeff Garzikb0955182005-05-12 15:45:22 -04001948 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001949 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001950 ((cdb[2] & 0x20) || need_sense))
Tejun Heo750426a2006-11-14 22:37:35 +09001951 ata_gen_passthru_sense(qc);
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +02001952 else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1953 cmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001954 else if (need_sense)
1955 ata_gen_ata_sense(qc);
1956 else
1957 cmd->result = SAM_STAT_GOOD;
Jeff Garzikb0955182005-05-12 15:45:22 -04001958
Tejun Heoc31f5712006-11-22 12:39:43 +09001959 if (need_sense && !ap->ops->error_handler)
Tejun Heo44877b42007-02-21 01:06:51 +09001960 ata_dump_status(ap->print_id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001962 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
1965/**
1966 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 * @dev: ATA device to which the command is addressed
1968 * @cmd: SCSI command to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1970 *
1971 * Our ->queuecommand() function has decided that the SCSI
1972 * command issued can be directly translated into an ATA
1973 * command, rather than handled internally.
1974 *
1975 * This function sets up an ata_queued_cmd structure for the
1976 * SCSI command, and sends that ata_queued_cmd to the hardware.
1977 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001978 * The xlat_func argument (actor) returns 0 if ready to execute
1979 * ATA command, else 1 to finish translation. If 1 is returned
1980 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1981 * to be set reflecting an error condition or clean (early)
1982 * termination.
1983 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001985 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09001986 *
1987 * RETURNS:
1988 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1989 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 */
Tejun Heo2115ea92006-05-15 21:03:39 +09001991static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09001992 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Tejun Heo31cc23b2007-09-23 13:14:12 +09001994 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct ata_queued_cmd *qc;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001996 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 VPRINTK("ENTER\n");
1999
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002000 qc = ata_scsi_qc_new(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04002002 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05002005 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
2006 cmd->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harrosh71201652007-09-18 17:48:50 +02002007 if (unlikely(scsi_bufflen(cmd) < 1)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002008 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04002009 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011
Boaz Harrosh71201652007-09-18 17:48:50 +02002012 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 qc->dma_dir = cmd->sc_data_direction;
2015 }
2016
2017 qc->complete_fn = ata_scsi_qc_complete;
2018
Tejun Heoad706992006-12-17 10:45:57 +09002019 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04002020 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Tejun Heo31cc23b2007-09-23 13:14:12 +09002022 if (ap->ops->qc_defer) {
2023 if ((rc = ap->ops->qc_defer(qc)))
2024 goto defer;
2025 }
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09002028 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Douglas Gilbertae006512005-10-09 09:09:35 -04002033early_finish:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002034 ata_qc_free(qc);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002035 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -04002036 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002037 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002038
2039err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04002041 cmd->result = (DID_ERROR << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002042 cmd->scsi_done(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05002043err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04002044 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002045 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09002046
2047defer:
Tejun Heo31cc23b2007-09-23 13:14:12 +09002048 ata_qc_free(qc);
Tejun Heo3dc1d882006-05-15 21:03:45 +09002049 DPRINTK("EXIT - defer\n");
Tejun Heo31cc23b2007-09-23 13:14:12 +09002050 if (rc == ATA_DEFER_LINK)
2051 return SCSI_MLQUEUE_DEVICE_BUSY;
2052 else
2053 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Christoph Hellwigf0a37d12017-01-10 09:41:44 +01002056struct ata_scsi_args {
2057 struct ata_device *dev;
2058 u16 *id;
2059 struct scsi_cmnd *cmd;
Christoph Hellwigf0a37d12017-01-10 09:41:44 +01002060};
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062/**
2063 * ata_scsi_rbuf_get - Map response buffer.
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002064 * @cmd: SCSI command containing buffer to be mapped.
Tejun Heo87340e92008-04-28 17:48:51 +09002065 * @flags: unsigned long variable to store irq enable status
2066 * @copy_in: copy in from user buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 *
Tejun Heo87340e92008-04-28 17:48:51 +09002068 * Prepare buffer for simulated SCSI commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 *
2070 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09002071 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 *
2073 * RETURNS:
Tejun Heo87340e92008-04-28 17:48:51 +09002074 * Pointer to response buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 */
Tejun Heo87340e92008-04-28 17:48:51 +09002076static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
2077 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Tejun Heo87340e92008-04-28 17:48:51 +09002079 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Tejun Heo87340e92008-04-28 17:48:51 +09002081 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
2082 if (copy_in)
2083 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2084 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2085 return ata_scsi_rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
2088/**
2089 * ata_scsi_rbuf_put - Unmap response buffer.
2090 * @cmd: SCSI command containing buffer to be unmapped.
Tejun Heo87340e92008-04-28 17:48:51 +09002091 * @copy_out: copy out result
2092 * @flags: @flags passed to ata_scsi_rbuf_get()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 *
Tejun Heo87340e92008-04-28 17:48:51 +09002094 * Returns rbuf buffer. The result is copied to @cmd's buffer if
2095 * @copy_back is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 *
2097 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09002098 * Unlocks ata_scsi_rbuf_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 */
Tejun Heo87340e92008-04-28 17:48:51 +09002100static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
2101 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Tejun Heo87340e92008-04-28 17:48:51 +09002103 if (copy_out)
2104 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2105 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2106 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
2109/**
2110 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
2111 * @args: device IDENTIFY data / SCSI command of interest.
2112 * @actor: Callback hook for desired SCSI command simulator
2113 *
2114 * Takes care of the hard work of simulating a SCSI command...
2115 * Mapping the response buffer, calling the command's handler,
2116 * and handling the handler's return value. This return value
2117 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04002118 * completed successfully (0), or not (in which case cmd->result
2119 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 *
2121 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002122 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 */
Tejun Heof0761be2008-04-28 17:16:52 +09002124static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
Tejun Heo87340e92008-04-28 17:48:51 +09002125 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 u8 *rbuf;
Tejun Heo87340e92008-04-28 17:48:51 +09002128 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct scsi_cmnd *cmd = args->cmd;
Jeff Garzikb445c562008-02-29 19:10:51 -05002130 unsigned long flags;
2131
Tejun Heo87340e92008-04-28 17:48:51 +09002132 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
2133 rc = actor(args, rbuf);
2134 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
Jeff Garzikb445c562008-02-29 19:10:51 -05002135
Douglas Gilbertae006512005-10-09 09:09:35 -04002136 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 cmd->result = SAM_STAT_GOOD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
2140/**
2141 * ata_scsiop_inq_std - Simulate INQUIRY command
2142 * @args: device IDENTIFY data / SCSI command of interest.
2143 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 *
2145 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05002146 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 *
2148 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002149 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 */
Tejun Heo87340e92008-04-28 17:48:51 +09002151static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Colin Ian Kinge94f79142017-09-19 09:39:52 +01002153 static const u8 versions[] = {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002154 0x00,
Tejun Heo87340e92008-04-28 17:48:51 +09002155 0x60, /* SAM-3 (no version claimed) */
2156
2157 0x03,
2158 0x20, /* SBC-2 (no version claimed) */
2159
Tom Yan29a37ea2016-07-12 21:29:35 +08002160 0x03,
2161 0x00 /* SPC-3 (no version claimed) */
Tejun Heo87340e92008-04-28 17:48:51 +09002162 };
Colin Ian Kinge94f79142017-09-19 09:39:52 +01002163 static const u8 versions_zbc[] = {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002164 0x00,
2165 0xA0, /* SAM-5 (no version claimed) */
2166
Hannes Reinecke856c4662016-04-25 12:45:55 +02002167 0x06,
2168 0x00, /* SBC-4 (no version claimed) */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002169
Hannes Reinecke856c4662016-04-25 12:45:55 +02002170 0x05,
2171 0xC0, /* SPC-5 (no version claimed) */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002172
2173 0x60,
Hannes Reinecke856c4662016-04-25 12:45:55 +02002174 0x24, /* ZBC r05 */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002175 };
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 u8 hdr[] = {
2178 TYPE_DISK,
2179 0,
2180 0x5, /* claim SPC-3 version compatibility */
2181 2,
Tom Yan415ffdd2016-07-12 21:29:34 +08002182 95 - 4,
2183 0,
2184 0,
2185 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 };
2187
Tejun Heo87340e92008-04-28 17:48:51 +09002188 VPRINTK("ENTER\n");
2189
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02002190 /* set scsi removable (RMB) bit per ata bit, or if the
2191 * AHCI port says it's external (Hotplug-capable, eSATA).
2192 */
2193 if (ata_id_removable(args->id) ||
2194 (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 hdr[1] |= (1 << 7);
2196
Tom Yan56b8cba2016-07-12 22:12:01 +08002197 if (args->dev->class == ATA_DEV_ZAC) {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002198 hdr[0] = TYPE_ZBC;
Tom Yan56b8cba2016-07-12 22:12:01 +08002199 hdr[2] = 0x7; /* claim SPC-5 version compatibility */
2200 }
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002203 memcpy(&rbuf[8], "ATA ", 8);
2204 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
Keith Buschc49a6bf2014-05-01 11:12:03 -06002205
2206 /* From SAT, use last 2 words from fw rev unless they are spaces */
2207 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
2208 if (strncmp(&rbuf[32], " ", 4) == 0)
2209 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Tejun Heo87340e92008-04-28 17:48:51 +09002211 if (rbuf[32] == 0 || rbuf[32] == ' ')
2212 memcpy(&rbuf[32], "n/a ", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Hannes Reinecke856c4662016-04-25 12:45:55 +02002214 if (ata_id_zoned_cap(args->id) || args->dev->class == ATA_DEV_ZAC)
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002215 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
2216 else
2217 memcpy(rbuf + 58, versions, sizeof(versions));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 return 0;
2220}
2221
2222/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002223 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 * @args: device IDENTIFY data / SCSI command of interest.
2225 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002227 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 *
2229 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002230 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 */
Tejun Heo87340e92008-04-28 17:48:51 +09002232static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002234 int num_pages;
Colin Ian Kinge94f79142017-09-19 09:39:52 +01002235 static const u8 pages[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 0x00, /* page 0x00, this page */
2237 0x80, /* page 0x80, unit serial no page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002238 0x83, /* page 0x83, device ident page */
2239 0x89, /* page 0x89, ata info page */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002240 0xb0, /* page 0xb0, block limits page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002241 0xb1, /* page 0xb1, block device characteristics page */
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002242 0xb2, /* page 0xb2, thin provisioning page */
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002243 0xb6, /* page 0xb6, zoned block device characteristics */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 };
Tejun Heo87340e92008-04-28 17:48:51 +09002245
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002246 num_pages = sizeof(pages);
2247 if (!(args->dev->flags & ATA_DFLAG_ZAC))
2248 num_pages--;
2249 rbuf[3] = num_pages; /* number of supported VPD pages */
2250 memcpy(rbuf + 4, pages, num_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return 0;
2252}
2253
2254/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002255 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 * @args: device IDENTIFY data / SCSI command of interest.
2257 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 *
2259 * Returns ATA device serial number.
2260 *
2261 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002262 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 */
Tejun Heo87340e92008-04-28 17:48:51 +09002264static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Colin Ian Kinge94f79142017-09-19 09:39:52 +01002266 static const u8 hdr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 0,
2268 0x80, /* this page code */
2269 0,
Tejun Heoa0cf7332007-01-02 20:18:49 +09002270 ATA_ID_SERNO_LEN, /* page len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 };
Tejun Heo87340e92008-04-28 17:48:51 +09002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002274 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2275 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return 0;
2277}
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002280 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 * @args: device IDENTIFY data / SCSI command of interest.
2282 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002284 * Yields two logical unit device identification designators:
2285 * - vendor specific ASCII containing the ATA serial number
2286 * - SAT defined "t10 vendor id based" containing ASCII vendor
2287 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 *
2289 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002290 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 */
Tejun Heo87340e92008-04-28 17:48:51 +09002292static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
Jeff Garzikb142eb62006-03-21 20:37:47 -05002294 const int sat_model_serial_desc_len = 68;
Tejun Heo87340e92008-04-28 17:48:51 +09002295 int num;
Jeff Garzikb142eb62006-03-21 20:37:47 -05002296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05002298 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Tejun Heo87340e92008-04-28 17:48:51 +09002300 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2301 rbuf[num + 0] = 2;
2302 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2303 num += 4;
2304 ata_id_string(args->id, (unsigned char *) rbuf + num,
2305 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2306 num += ATA_ID_SERNO_LEN;
2307
2308 /* SAT defined lu model and serial numbers descriptor */
2309 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2310 rbuf[num + 0] = 2;
2311 rbuf[num + 1] = 1;
2312 rbuf[num + 3] = sat_model_serial_desc_len;
2313 num += 4;
2314 memcpy(rbuf + num, "ATA ", 8);
2315 num += 8;
2316 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2317 ATA_ID_PROD_LEN);
2318 num += ATA_ID_PROD_LEN;
2319 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2320 ATA_ID_SERNO_LEN);
2321 num += ATA_ID_SERNO_LEN;
2322
Hannes Reinecke6b3b9d72011-03-07 08:56:44 +01002323 if (ata_id_has_wwn(args->id)) {
2324 /* SAT defined lu world wide name */
2325 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2326 rbuf[num + 0] = 1;
2327 rbuf[num + 1] = 3;
2328 rbuf[num + 3] = ATA_ID_WWN_LEN;
2329 num += 4;
2330 ata_id_string(args->id, (unsigned char *) rbuf + num,
2331 ATA_ID_WWN, ATA_ID_WWN_LEN);
2332 num += ATA_ID_WWN_LEN;
2333 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05002334 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return 0;
2336}
2337
2338/**
Jeff Garzikad355b42007-09-21 20:38:03 -04002339 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2340 * @args: device IDENTIFY data / SCSI command of interest.
2341 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Jeff Garzikad355b42007-09-21 20:38:03 -04002342 *
2343 * Yields SAT-specified ATA VPD page.
2344 *
2345 * LOCKING:
2346 * spin_lock_irqsave(host lock)
2347 */
Tejun Heo87340e92008-04-28 17:48:51 +09002348static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
Jeff Garzikad355b42007-09-21 20:38:03 -04002349{
Jeff Garzikad355b42007-09-21 20:38:03 -04002350 struct ata_taskfile tf;
Jeff Garzikad355b42007-09-21 20:38:03 -04002351
Jeff Garzikad355b42007-09-21 20:38:03 -04002352 memset(&tf, 0, sizeof(tf));
2353
Tejun Heo87340e92008-04-28 17:48:51 +09002354 rbuf[1] = 0x89; /* our page code */
2355 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2356 rbuf[3] = (0x238 & 0xff);
Jeff Garzikad355b42007-09-21 20:38:03 -04002357
Tejun Heo87340e92008-04-28 17:48:51 +09002358 memcpy(&rbuf[8], "linux ", 8);
2359 memcpy(&rbuf[16], "libata ", 16);
2360 memcpy(&rbuf[32], DRV_VERSION, 4);
Jeff Garzikad355b42007-09-21 20:38:03 -04002361
2362 /* we don't store the ATA device signature, so we fake it */
2363
2364 tf.command = ATA_DRDY; /* really, this is Status reg */
2365 tf.lbal = 0x1;
2366 tf.nsect = 0x1;
2367
Tejun Heo87340e92008-04-28 17:48:51 +09002368 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
2369 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
Jeff Garzikad355b42007-09-21 20:38:03 -04002370
Tejun Heo87340e92008-04-28 17:48:51 +09002371 rbuf[56] = ATA_CMD_ID_ATA;
Jeff Garzikad355b42007-09-21 20:38:03 -04002372
Tejun Heo87340e92008-04-28 17:48:51 +09002373 memcpy(&rbuf[60], &args->id[0], 512);
Jeff Garzikad355b42007-09-21 20:38:03 -04002374 return 0;
2375}
2376
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002377static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2378{
Grant Grundler295124d2010-08-17 10:56:53 -07002379 u16 min_io_sectors;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002380
2381 rbuf[1] = 0xb0;
2382 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2383
2384 /*
2385 * Optimal transfer length granularity.
2386 *
2387 * This is always one physical block, but for disks with a smaller
2388 * logical than physical sector size we need to figure out what the
2389 * latter is.
2390 */
Grant Grundler295124d2010-08-17 10:56:53 -07002391 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002392 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2393
2394 /*
2395 * Optimal unmap granularity.
2396 *
2397 * The ATA spec doesn't even know about a granularity or alignment
2398 * for the TRIM command. We can leave away most of the unmap related
2399 * VPD page entries, but we have specifify a granularity to signal
2400 * that we support some form of unmap - in thise case via WRITE SAME
2401 * with the unmap bit set.
2402 */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002403 if (ata_id_has_trim(args->id)) {
Tom Yan29838602016-07-13 04:31:23 +08002404 put_unaligned_be64(65535 * ATA_MAX_TRIM_RNUM, &rbuf[36]);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002405 put_unaligned_be32(1, &rbuf[28]);
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002406 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002407
2408 return 0;
2409}
2410
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002411static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2412{
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002413 int form_factor = ata_id_form_factor(args->id);
2414 int media_rotation_rate = ata_id_rotation_rate(args->id);
Hannes Reinecke856c4662016-04-25 12:45:55 +02002415 u8 zoned = ata_id_zoned_cap(args->id);
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002416
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002417 rbuf[1] = 0xb1;
2418 rbuf[3] = 0x3c;
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002419 rbuf[4] = media_rotation_rate >> 8;
2420 rbuf[5] = media_rotation_rate;
2421 rbuf[7] = form_factor;
Hannes Reinecke856c4662016-04-25 12:45:55 +02002422 if (zoned)
2423 rbuf[8] = (zoned << 4);
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002424
2425 return 0;
2426}
2427
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002428static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
2429{
2430 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2431 rbuf[1] = 0xb2;
2432 rbuf[3] = 0x4;
2433 rbuf[5] = 1 << 6; /* TPWS */
2434
2435 return 0;
2436}
2437
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002438static unsigned int ata_scsiop_inq_b6(struct ata_scsi_args *args, u8 *rbuf)
2439{
2440 /*
2441 * zbc-r05 SCSI Zoned Block device characteristics VPD page
2442 */
2443 rbuf[1] = 0xb6;
2444 rbuf[3] = 0x3C;
2445
2446 /*
2447 * URSWRZ bit is only meaningful for host-managed ZAC drives
2448 */
2449 if (args->dev->zac_zoned_cap & 1)
2450 rbuf[4] |= 1;
2451 put_unaligned_be32(args->dev->zac_zones_optimal_open, &rbuf[8]);
2452 put_unaligned_be32(args->dev->zac_zones_optimal_nonseq, &rbuf[12]);
2453 put_unaligned_be32(args->dev->zac_zones_max_open, &rbuf[16]);
2454
2455 return 0;
2456}
2457
Jeff Garzikad355b42007-09-21 20:38:03 -04002458/**
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002459 * modecpy - Prepare response for MODE SENSE
2460 * @dest: output buffer
2461 * @src: data being copied
2462 * @n: length of mode page
2463 * @changeable: whether changeable parameters are requested
2464 *
2465 * Generate a generic MODE SENSE page for either current or changeable
2466 * parameters.
2467 *
2468 * LOCKING:
2469 * None.
2470 */
2471static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2472{
2473 if (changeable) {
2474 memcpy(dest, src, 2);
2475 memset(dest + 2, 0, n - 2);
2476 } else {
2477 memcpy(dest, src, n);
2478 }
2479}
2480
2481/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 * ata_msense_caching - Simulate MODE SENSE caching info page
2483 * @id: device IDENTIFY data
Tejun Heo87340e92008-04-28 17:48:51 +09002484 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002485 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 *
2487 * Generate a caching info page, which conditionally indicates
2488 * write caching to the SCSI layer, depending on device
2489 * capabilities.
2490 *
2491 * LOCKING:
2492 * None.
2493 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002494static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002496 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
Tom Yan737bee92016-07-20 04:39:28 +08002497 if (changeable) {
2498 buf[2] |= (1 << 2); /* ata_mselect_caching() */
2499 } else {
2500 buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */
2501 buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */
2502 }
Tejun Heo87340e92008-04-28 17:48:51 +09002503 return sizeof(def_cache_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
2506/**
Tom Yanf086b742016-07-13 02:54:12 +08002507 * ata_msense_control - Simulate MODE SENSE control mode page
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002508 * @dev: ATA device of interest
Tejun Heo87340e92008-04-28 17:48:51 +09002509 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002510 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 *
2512 * Generate a generic MODE SENSE control mode page.
2513 *
2514 * LOCKING:
2515 * None.
2516 */
Tom Yanf086b742016-07-13 02:54:12 +08002517static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002518 bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002520 modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
Tom Yan737bee92016-07-20 04:39:28 +08002521 if (changeable) {
2522 buf[2] |= (1 << 2); /* ata_mselect_control() */
2523 } else {
2524 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
2525
2526 buf[2] |= (d_sense << 2); /* descriptor format sense data */
2527 }
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002528 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
2531/**
2532 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002533 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002534 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 *
2536 * Generate a generic MODE SENSE r/w error recovery page.
2537 *
2538 * LOCKING:
2539 * None.
2540 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002541static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002543 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2544 changeable);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002545 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002548/*
2549 * We can turn this into a real blacklist if it's needed, for now just
2550 * blacklist any Maxtor BANC1G10 revision firmware
2551 */
2552static int ata_dev_supports_fua(u16 *id)
2553{
Tejun Heoa0cf7332007-01-02 20:18:49 +09002554 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002555
Jeff Garzikc3c013a2006-02-27 22:31:19 -05002556 if (!libata_fua)
2557 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002558 if (!ata_id_has_fua(id))
2559 return 0;
2560
Tejun Heoa0cf7332007-01-02 20:18:49 +09002561 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2562 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002563
Tejun Heo2e026712006-02-12 22:47:04 +09002564 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002565 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09002566 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002567 return 1;
2568
2569 return 0; /* blacklisted */
2570}
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572/**
2573 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2574 * @args: device IDENTIFY data / SCSI command of interest.
2575 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002577 * Simulate MODE SENSE commands. Assume this is invoked for direct
2578 * access devices (e.g. disks) only. There should be no block
2579 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 *
2581 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002582 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 */
Tejun Heo87340e92008-04-28 17:48:51 +09002584static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002586 struct ata_device *dev = args->dev;
Tejun Heo87340e92008-04-28 17:48:51 +09002587 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
Colin Ian Kinge94f79142017-09-19 09:39:52 +01002588 static const u8 sat_blk_desc[] = {
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002589 0, 0, 0, 0, /* number of blocks: sat unspecified */
2590 0,
2591 0, 0x2, 0x0 /* block length: 512 bytes */
2592 };
2593 u8 pg, spg;
Tejun Heo87340e92008-04-28 17:48:51 +09002594 unsigned int ebd, page_control, six_byte;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002595 u8 dpofua, bp = 0xff;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002596 u16 fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 VPRINTK("ENTER\n");
2599
2600 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002601 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2602 /*
2603 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002607 switch (page_control) {
2608 case 0: /* current */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002609 case 1: /* changeable */
2610 case 2: /* defaults */
Douglas Gilbertae006512005-10-09 09:09:35 -04002611 break; /* supported */
2612 case 3: /* saved */
2613 goto saving_not_supp;
Douglas Gilbertae006512005-10-09 09:09:35 -04002614 default:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002615 fp = 2;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002616 bp = 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002617 goto invalid_fld;
2618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Tejun Heo87340e92008-04-28 17:48:51 +09002620 if (six_byte)
2621 p += 4 + (ebd ? 8 : 0);
2622 else
2623 p += 8 + (ebd ? 8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002625 pg = scsicmd[2] & 0x3f;
2626 spg = scsicmd[3];
2627 /*
2628 * No mode subpages supported (yet) but asking for _all_
2629 * subpages may be valid
2630 */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002631 if (spg && (spg != ALL_SUB_MPAGES)) {
2632 fp = 3;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002633 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002634 }
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002635
2636 switch(pg) {
2637 case RW_RECOVERY_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002638 p += ata_msense_rw_recovery(p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 break;
2640
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002641 case CACHE_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002642 p += ata_msense_caching(args->id, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 break;
2644
Tejun Heo87340e92008-04-28 17:48:51 +09002645 case CONTROL_MPAGE:
Tom Yanf086b742016-07-13 02:54:12 +08002646 p += ata_msense_control(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002649 case ALL_MPAGES:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002650 p += ata_msense_rw_recovery(p, page_control == 1);
2651 p += ata_msense_caching(args->id, p, page_control == 1);
Tom Yanf086b742016-07-13 02:54:12 +08002652 p += ata_msense_control(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 break;
2654
2655 default: /* invalid page code */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002656 fp = 2;
Douglas Gilbertae006512005-10-09 09:09:35 -04002657 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 }
2659
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002660 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002661 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002662 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2663 dpofua = 1 << 4;
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (six_byte) {
Tejun Heo87340e92008-04-28 17:48:51 +09002666 rbuf[0] = p - rbuf - 1;
2667 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002668 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002669 rbuf[3] = sizeof(sat_blk_desc);
2670 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 } else {
Tejun Heo87340e92008-04-28 17:48:51 +09002673 unsigned int output_len = p - rbuf - 2;
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 rbuf[0] = output_len >> 8;
Tejun Heo87340e92008-04-28 17:48:51 +09002676 rbuf[1] = output_len;
2677 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002678 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002679 rbuf[7] = sizeof(sat_blk_desc);
2680 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002684
2685invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002686 ata_scsi_set_invalid_field(dev, args->cmd, fp, bp);
Douglas Gilbertae006512005-10-09 09:09:35 -04002687 return 1;
2688
2689saving_not_supp:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002690 ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04002691 /* "Saving parameters not supported" */
2692 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
2695/**
2696 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2697 * @args: device IDENTIFY data / SCSI command of interest.
2698 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 *
2700 * Simulate READ CAPACITY commands.
2701 *
2702 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002703 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 */
Tejun Heo87340e92008-04-28 17:48:51 +09002705static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002707 struct ata_device *dev = args->dev;
2708 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
Grant Grundler295124d2010-08-17 10:56:53 -07002709 u32 sector_size; /* physical sector size in bytes */
2710 u8 log2_per_phys;
2711 u16 lowest_aligned;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002712
Grant Grundler295124d2010-08-17 10:56:53 -07002713 sector_size = ata_id_logical_sector_size(dev->id);
2714 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2715 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 VPRINTK("ENTER\n");
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002720 if (last_lba >= 0xffffffffULL)
2721 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 /* sector count, 32-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002724 rbuf[0] = last_lba >> (8 * 3);
2725 rbuf[1] = last_lba >> (8 * 2);
2726 rbuf[2] = last_lba >> (8 * 1);
2727 rbuf[3] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002730 rbuf[4] = sector_size >> (8 * 3);
2731 rbuf[5] = sector_size >> (8 * 2);
2732 rbuf[6] = sector_size >> (8 * 1);
2733 rbuf[7] = sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 } else {
2735 /* sector count, 64-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002736 rbuf[0] = last_lba >> (8 * 7);
2737 rbuf[1] = last_lba >> (8 * 6);
2738 rbuf[2] = last_lba >> (8 * 5);
2739 rbuf[3] = last_lba >> (8 * 4);
2740 rbuf[4] = last_lba >> (8 * 3);
2741 rbuf[5] = last_lba >> (8 * 2);
2742 rbuf[6] = last_lba >> (8 * 1);
2743 rbuf[7] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002746 rbuf[ 8] = sector_size >> (8 * 3);
2747 rbuf[ 9] = sector_size >> (8 * 2);
2748 rbuf[10] = sector_size >> (8 * 1);
2749 rbuf[11] = sector_size;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002750
2751 rbuf[12] = 0;
Grant Grundler295124d2010-08-17 10:56:53 -07002752 rbuf[13] = log2_per_phys;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002753 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2754 rbuf[15] = lowest_aligned;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002755
Arne Fitzenreiter71d126f2015-07-15 13:54:36 +02002756 if (ata_id_has_trim(args->id) &&
2757 !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002758 rbuf[14] |= 0x80; /* LBPME */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002759
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002760 if (ata_id_has_zero_after_trim(args->id) &&
2761 dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
2762 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2763 rbuf[14] |= 0x40; /* LBPRZ */
2764 }
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002765 }
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002766 if (ata_id_zoned_cap(args->id) ||
2767 args->dev->class == ATA_DEV_ZAC)
2768 rbuf[12] = (1 << 4); /* RC_BASIS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return 0;
2771}
2772
2773/**
2774 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2775 * @args: device IDENTIFY data / SCSI command of interest.
2776 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 *
2778 * Simulate REPORT LUNS command.
2779 *
2780 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002781 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 */
Tejun Heo87340e92008-04-28 17:48:51 +09002783static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
2785 VPRINTK("ENTER\n");
2786 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2787
2788 return 0;
2789}
2790
Tejun Heo77853bf2006-01-23 13:09:36 +09002791static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002792{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002793 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002794 /* FIXME: not quite right; we don't want the
2795 * translation of taskfile registers into
2796 * a sense descriptors, since that's only
2797 * correct for ATA, not ATAPI
2798 */
Tejun Heo750426a2006-11-14 22:37:35 +09002799 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002800 }
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002801
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002802 ata_qc_done(qc);
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002803}
2804
2805/* is it pointless to prefer PIO for "safety reasons"? */
2806static inline int ata_pio_use_silly(struct ata_port *ap)
2807{
2808 return (ap->flags & ATA_FLAG_PIO_DMA);
2809}
2810
2811static void atapi_request_sense(struct ata_queued_cmd *qc)
2812{
2813 struct ata_port *ap = qc->ap;
2814 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002815
2816 DPRINTK("ATAPI request sense\n");
2817
James Bottomley7ccd7202008-01-17 11:56:24 -06002818 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Jeff Garzika939c962005-10-05 17:09:16 -04002819
Tejun Heo127102a2008-04-07 22:47:21 +09002820#ifdef CONFIG_ATA_SFF
James Bottomley855d8542008-04-18 13:18:48 -05002821 if (ap->ops->sff_tf_read)
2822 ap->ops->sff_tf_read(ap, &qc->tf);
Tejun Heo127102a2008-04-07 22:47:21 +09002823#endif
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002824
2825 /* fill these in, for the case where they are -not- overwritten */
2826 cmd->sense_buffer[0] = 0x70;
2827 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2828
2829 ata_qc_reinit(qc);
2830
Tejun Heo93f8fec2007-12-05 16:43:01 +09002831 /* setup sg table and init transfer direction */
akpm@linux-foundation.orgcadb7342008-01-15 16:01:52 -08002832 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
Tejun Heo93f8fec2007-12-05 16:43:01 +09002833 ata_sg_init(qc, &qc->sgent, 1);
Jeff Garzika939c962005-10-05 17:09:16 -04002834 qc->dma_dir = DMA_FROM_DEVICE;
2835
Tejun Heo6e7846e2006-02-12 23:32:58 +09002836 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002837 qc->cdb[0] = REQUEST_SENSE;
2838 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2839
2840 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2841 qc->tf.command = ATA_CMD_PACKET;
2842
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002843 if (ata_pio_use_silly(ap)) {
Tejun Heo0dc36882007-12-18 16:34:43 -05002844 qc->tf.protocol = ATAPI_PROT_DMA;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002845 qc->tf.feature |= ATAPI_PKT_DMA;
2846 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05002847 qc->tf.protocol = ATAPI_PROT_PIO;
Alan Cox2db78dd2007-10-02 13:53:04 -07002848 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2849 qc->tf.lbah = 0;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002850 }
Jeff Garzika939c962005-10-05 17:09:16 -04002851 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2852
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002853 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002854
Tejun Heo8e0e6942006-03-31 20:41:11 +09002855 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002856
2857 DPRINTK("EXIT\n");
2858}
2859
Christoph Hellwigaa18da82017-01-10 09:41:43 +01002860/*
2861 * ATAPI devices typically report zero for their SCSI version, and sometimes
2862 * deviate from the spec WRT response data format. If SCSI version is
2863 * reported as zero like normal, then we make the following fixups:
2864 * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a
2865 * modern device.
2866 * 2) Ensure response data format / ATAPI information are always correct.
2867 */
2868static void atapi_fixup_inquiry(struct scsi_cmnd *cmd)
2869{
2870 u8 buf[4];
2871
2872 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2873 if (buf[2] == 0) {
2874 buf[2] = 0x5;
2875 buf[3] = 0x32;
2876 }
2877 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2878}
2879
Tejun Heo77853bf2006-01-23 13:09:36 +09002880static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
2882 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002883 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Jeff Garzika7dac442005-10-30 04:44:42 -05002885 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002886
Tejun Heo246619d2006-05-15 20:58:16 +09002887 /* handle completion from new EH */
2888 if (unlikely(qc->ap->ops->error_handler &&
2889 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2890
2891 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2892 /* FIXME: not quite right; we don't want the
2893 * translation of taskfile registers into a
2894 * sense descriptors, since that's only
2895 * correct for ATA, not ATAPI
2896 */
Tejun Heo750426a2006-11-14 22:37:35 +09002897 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002898 }
2899
Tejun Heo22aac082006-08-08 14:08:59 +09002900 /* SCSI EH automatically locks door if sdev->locked is
2901 * set. Sometimes door lock request continues to
2902 * fail, for example, when no media is present. This
2903 * creates a loop - SCSI EH issues door lock which
2904 * fails and gets invoked again to acquire sense data
2905 * for the failed command.
2906 *
2907 * If door lock fails, always clear sdev->locked to
2908 * avoid this infinite loop.
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002909 *
2910 * This may happen before SCSI scan is complete. Make
2911 * sure qc->dev->sdev isn't NULL before dereferencing.
Tejun Heo22aac082006-08-08 14:08:59 +09002912 */
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002913 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
Tejun Heo22aac082006-08-08 14:08:59 +09002914 qc->dev->sdev->locked = 0;
2915
Tejun Heo246619d2006-05-15 20:58:16 +09002916 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002917 ata_qc_done(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002918 return;
2919 }
2920
2921 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002922 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002924 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002925 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002926 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002927 /* FIXME: not quite right; we don't want the
2928 * translation of taskfile registers into
2929 * a sense descriptors, since that's only
2930 * correct for ATA, not ATAPI
2931 */
Tejun Heo750426a2006-11-14 22:37:35 +09002932 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002933 } else {
Christoph Hellwigaa18da82017-01-10 09:41:43 +01002934 if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0)
2935 atapi_fixup_inquiry(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 cmd->result = SAM_STAT_GOOD;
2937 }
2938
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002939 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941/**
2942 * atapi_xlat - Initialize PACKET taskfile
2943 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 *
2945 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002946 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 *
2948 * RETURNS:
2949 * Zero on success, non-zero on failure.
2950 */
Tejun Heoad706992006-12-17 10:45:57 +09002951static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
Tejun Heo542b1442006-12-17 10:45:08 +09002953 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 struct ata_device *dev = qc->dev;
Tejun Heo542b1442006-12-17 10:45:08 +09002955 int nodata = (scmd->sc_data_direction == DMA_NONE);
Tejun Heo5895ef92008-06-17 12:36:26 +09002956 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
Alan Cox2db78dd2007-10-02 13:53:04 -07002957 unsigned int nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Tejun Heo2e5704f2006-12-17 10:46:33 +09002959 memset(qc->cdb, 0, dev->cdb_len);
2960 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962 qc->complete_fn = atapi_qc_complete;
2963
2964 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002965 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 qc->tf.flags |= ATA_TFLAG_WRITE;
2967 DPRINTK("direction: write\n");
2968 }
2969
2970 qc->tf.command = ATA_CMD_PACKET;
Tejun Heoaacda372008-03-18 17:47:43 +09002971 ata_qc_set_pc_nbytes(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002973 /* check whether ATAPI DMA is safe */
Tejun Heo5895ef92008-06-17 12:36:26 +09002974 if (!nodata && !using_pio && atapi_check_dma(qc))
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002975 using_pio = 1;
2976
Tejun Heoe1902222007-11-26 20:58:02 +09002977 /* Some controller variants snoop this value for Packet
2978 * transfers to do state machine and FIFO management. Thus we
2979 * want to set it properly, and for DMA where it is
2980 * effectively meaningless.
2981 */
Tejun Heoaacda372008-03-18 17:47:43 +09002982 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
Alan Cox2db78dd2007-10-02 13:53:04 -07002983
Tejun Heoe1902222007-11-26 20:58:02 +09002984 /* Most ATAPI devices which honor transfer chunk size don't
2985 * behave according to the spec when odd chunk size which
2986 * matches the transfer length is specified. If the number of
2987 * bytes to transfer is 2n+1. According to the spec, what
2988 * should happen is to indicate that 2n+1 is going to be
2989 * transferred and transfer 2n+2 bytes where the last byte is
2990 * padding.
2991 *
2992 * In practice, this doesn't happen. ATAPI devices first
2993 * indicate and transfer 2n bytes and then indicate and
2994 * transfer 2 bytes where the last byte is padding.
2995 *
2996 * This inconsistency confuses several controllers which
2997 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2998 * These controllers use actual number of transferred bytes to
2999 * update DMA poitner and transfer of 4n+2 bytes make those
3000 * controller push DMA pointer by 4n+4 bytes because SATA data
3001 * FISes are aligned to 4 bytes. This causes data corruption
3002 * and buffer overrun.
3003 *
3004 * Always setting nbytes to even number solves this problem
3005 * because then ATAPI devices don't have to split data at 2n
3006 * boundaries.
3007 */
3008 if (nbytes & 0x1)
3009 nbytes++;
3010
Alan Cox2db78dd2007-10-02 13:53:04 -07003011 qc->tf.lbam = (nbytes & 0xFF);
3012 qc->tf.lbah = (nbytes >> 8);
3013
Tejun Heo5895ef92008-06-17 12:36:26 +09003014 if (nodata)
3015 qc->tf.protocol = ATAPI_PROT_NODATA;
3016 else if (using_pio)
3017 qc->tf.protocol = ATAPI_PROT_PIO;
3018 else {
Tejun Heoe00f1ff2007-06-27 02:47:35 +09003019 /* DMA data xfer */
Tejun Heo0dc36882007-12-18 16:34:43 -05003020 qc->tf.protocol = ATAPI_PROT_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 qc->tf.feature |= ATAPI_PKT_DMA;
3022
Tejun Heo91163002008-02-21 13:25:50 +09003023 if ((dev->flags & ATA_DFLAG_DMADIR) &&
3024 (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08003025 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028
Alan Cox2db78dd2007-10-02 13:53:04 -07003029
3030 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
3031 as ATAPI tape drives don't get this right otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return 0;
3033}
3034
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003035static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
Tejun Heoab5b3a52006-05-31 18:27:34 +09003036{
Tejun Heo071f44b2008-04-07 22:47:22 +09003037 if (!sata_pmp_attached(ap)) {
Dan Carpenter59a5e262017-07-19 13:06:41 +03003038 if (likely(devno >= 0 &&
3039 devno < ata_link_max_devices(&ap->link)))
Tejun Heo41bda9c2007-08-06 18:36:24 +09003040 return &ap->link.device[devno];
3041 } else {
Dan Carpenter59a5e262017-07-19 13:06:41 +03003042 if (likely(devno >= 0 &&
3043 devno < ap->nr_pmp_links))
Tejun Heo41bda9c2007-08-06 18:36:24 +09003044 return &ap->pmp_link[devno].device[0];
3045 }
3046
Tejun Heoab5b3a52006-05-31 18:27:34 +09003047 return NULL;
3048}
3049
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003050static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
3051 const struct scsi_device *scsidev)
Tejun Heoab5b3a52006-05-31 18:27:34 +09003052{
Tejun Heo41bda9c2007-08-06 18:36:24 +09003053 int devno;
Tejun Heoab5b3a52006-05-31 18:27:34 +09003054
Tejun Heo41bda9c2007-08-06 18:36:24 +09003055 /* skip commands not addressed to targets we simulate */
Tejun Heo071f44b2008-04-07 22:47:22 +09003056 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003057 if (unlikely(scsidev->channel || scsidev->lun))
3058 return NULL;
3059 devno = scsidev->id;
3060 } else {
3061 if (unlikely(scsidev->id || scsidev->lun))
3062 return NULL;
3063 devno = scsidev->channel;
3064 }
3065
3066 return ata_find_dev(ap, devno);
Tejun Heoab5b3a52006-05-31 18:27:34 +09003067}
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069/**
3070 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
3071 * @ap: ATA port to which the device is attached
3072 * @scsidev: SCSI device from which we derive the ATA device
3073 *
3074 * Given various information provided in struct scsi_cmnd,
3075 * map that onto an ATA bus, and using that mapping
3076 * determine which ata_device is associated with the
3077 * SCSI command to be sent.
3078 *
3079 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003080 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 *
3082 * RETURNS:
3083 * Associated ATA device, or %NULL if not found.
3084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04003086ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Tejun Heoab5b3a52006-05-31 18:27:34 +09003088 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Tejun Heo2486fa52008-07-31 07:52:40 +09003090 if (unlikely(!dev || !ata_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return NULL;
3092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return dev;
3094}
3095
Jeff Garzikb0955182005-05-12 15:45:22 -04003096/*
3097 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
3098 * @byte1: Byte 1 from pass-thru CDB.
3099 *
3100 * RETURNS:
3101 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
3102 */
3103static u8
3104ata_scsi_map_proto(u8 byte1)
3105{
3106 switch((byte1 & 0x1e) >> 1) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003107 case 3: /* Non-data */
3108 return ATA_PROT_NODATA;
Jeff Garzikb0955182005-05-12 15:45:22 -04003109
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003110 case 6: /* DMA */
3111 case 10: /* UDMA Data-in */
3112 case 11: /* UDMA Data-Out */
3113 return ATA_PROT_DMA;
Jeff Garzikb0955182005-05-12 15:45:22 -04003114
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003115 case 4: /* PIO Data-in */
3116 case 5: /* PIO Data-out */
3117 return ATA_PROT_PIO;
Jeff Garzikb0955182005-05-12 15:45:22 -04003118
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303119 case 12: /* FPDMA */
3120 return ATA_PROT_NCQ;
3121
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003122 case 0: /* Hard Reset */
3123 case 1: /* SRST */
3124 case 8: /* Device Diagnostic */
3125 case 9: /* Device Reset */
3126 case 7: /* DMA Queued */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003127 case 15: /* Return Response Info */
3128 default: /* Reserved */
3129 break;
Jeff Garzikb0955182005-05-12 15:45:22 -04003130 }
3131
3132 return ATA_PROT_UNKNOWN;
3133}
3134
3135/**
3136 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
3137 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04003138 *
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003139 * Handles either 12, 16, or 32-byte versions of the CDB.
Jeff Garzikb0955182005-05-12 15:45:22 -04003140 *
3141 * RETURNS:
3142 * Zero on success, non-zero on failure.
3143 */
Tejun Heoad706992006-12-17 10:45:57 +09003144static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04003145{
3146 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09003147 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01003148 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09003149 const u8 *cdb = scmd->cmnd;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003150 u16 fp;
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003151 u16 cdb_offset = 0;
Jeff Garzikb0955182005-05-12 15:45:22 -04003152
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003153 /* 7Fh variable length cmd means a ata pass-thru(32) */
3154 if (cdb[0] == VARIABLE_LENGTH_CMD)
3155 cdb_offset = 9;
3156
3157 tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]);
3158 if (tf->protocol == ATA_PROT_UNKNOWN) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003159 fp = 1;
Tejun Heo9a405252005-12-02 11:49:11 +09003160 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003161 }
Jeff Garzik8190bdb2006-05-24 01:53:39 -04003162
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003163 if (ata_is_ncq(tf->protocol) && (cdb[2 + cdb_offset] & 0x3) == 0)
Hannes Reinecke5b844b62016-07-14 09:05:48 +09003164 tf->protocol = ATA_PROT_NCQ_NODATA;
3165
Vinayak Kale5f8e7f12015-10-27 13:11:46 +05303166 /* enable LBA */
3167 tf->flags |= ATA_TFLAG_LBA;
3168
Alan Coxae8d4ee2007-11-04 22:05:49 -05003169 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04003170 * 12 and 16 byte CDBs use different offsets to
3171 * provide the various register values.
3172 */
Tejun Heo542b1442006-12-17 10:45:08 +09003173 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04003174 /*
3175 * 16-byte CDB - may contain extended commands.
3176 *
3177 * If that is the case, copy the upper byte register values.
3178 */
Tejun Heo542b1442006-12-17 10:45:08 +09003179 if (cdb[1] & 0x01) {
3180 tf->hob_feature = cdb[3];
3181 tf->hob_nsect = cdb[5];
3182 tf->hob_lbal = cdb[7];
3183 tf->hob_lbam = cdb[9];
3184 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04003185 tf->flags |= ATA_TFLAG_LBA48;
3186 } else
3187 tf->flags &= ~ATA_TFLAG_LBA48;
3188
3189 /*
3190 * Always copy low byte, device and command registers.
3191 */
Tejun Heo542b1442006-12-17 10:45:08 +09003192 tf->feature = cdb[4];
3193 tf->nsect = cdb[6];
3194 tf->lbal = cdb[8];
3195 tf->lbam = cdb[10];
3196 tf->lbah = cdb[12];
3197 tf->device = cdb[13];
3198 tf->command = cdb[14];
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003199 } else if (cdb[0] == ATA_12) {
Jeff Garzikb0955182005-05-12 15:45:22 -04003200 /*
3201 * 12-byte CDB - incapable of extended commands.
3202 */
3203 tf->flags &= ~ATA_TFLAG_LBA48;
3204
Tejun Heo542b1442006-12-17 10:45:08 +09003205 tf->feature = cdb[3];
3206 tf->nsect = cdb[4];
3207 tf->lbal = cdb[5];
3208 tf->lbam = cdb[6];
3209 tf->lbah = cdb[7];
3210 tf->device = cdb[8];
3211 tf->command = cdb[9];
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003212 } else {
3213 /*
3214 * 32-byte CDB - may contain extended command fields.
3215 *
3216 * If that is the case, copy the upper byte register values.
3217 */
3218 if (cdb[10] & 0x01) {
3219 tf->hob_feature = cdb[20];
3220 tf->hob_nsect = cdb[22];
3221 tf->hob_lbal = cdb[16];
3222 tf->hob_lbam = cdb[15];
3223 tf->hob_lbah = cdb[14];
3224 tf->flags |= ATA_TFLAG_LBA48;
3225 } else
3226 tf->flags &= ~ATA_TFLAG_LBA48;
3227
3228 tf->feature = cdb[21];
3229 tf->nsect = cdb[23];
3230 tf->lbal = cdb[19];
3231 tf->lbam = cdb[18];
3232 tf->lbah = cdb[17];
3233 tf->device = cdb[24];
3234 tf->command = cdb[25];
3235 tf->auxiliary = get_unaligned_be32(&cdb[28]);
Jeff Garzikb0955182005-05-12 15:45:22 -04003236 }
Albert Leefa4453c2007-06-07 15:52:07 +08003237
Hannes Reinecke179b3102016-07-14 09:05:43 +09003238 /* For NCQ commands copy the tag value */
3239 if (ata_is_ncq(tf->protocol))
Jens Axboe4e5b6262018-05-11 12:51:04 -06003240 tf->nsect = qc->hw_tag << 3;
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303241
Albert Leefa4453c2007-06-07 15:52:07 +08003242 /* enforce correct master/slave bit */
3243 tf->device = dev->devno ?
3244 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003245
Tejun Heobd30add2009-09-03 16:08:11 +09003246 switch (tf->command) {
Grant Grundler295124d2010-08-17 10:56:53 -07003247 /* READ/WRITE LONG use a non-standard sect_size */
Tejun Heobd30add2009-09-03 16:08:11 +09003248 case ATA_CMD_READ_LONG:
3249 case ATA_CMD_READ_LONG_ONCE:
3250 case ATA_CMD_WRITE_LONG:
3251 case ATA_CMD_WRITE_LONG_ONCE:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003252 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
3253 fp = 1;
Tejun Heobd30add2009-09-03 16:08:11 +09003254 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003255 }
Tejun Heobd30add2009-09-03 16:08:11 +09003256 qc->sect_size = scsi_bufflen(scmd);
Grant Grundler295124d2010-08-17 10:56:53 -07003257 break;
3258
3259 /* commands using reported Logical Block size (e.g. 512 or 4K) */
3260 case ATA_CMD_CFA_WRITE_NE:
3261 case ATA_CMD_CFA_TRANS_SECT:
3262 case ATA_CMD_CFA_WRITE_MULT_NE:
3263 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
3264 case ATA_CMD_READ:
3265 case ATA_CMD_READ_EXT:
3266 case ATA_CMD_READ_QUEUED:
3267 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
3268 case ATA_CMD_FPDMA_READ:
3269 case ATA_CMD_READ_MULTI:
3270 case ATA_CMD_READ_MULTI_EXT:
3271 case ATA_CMD_PIO_READ:
3272 case ATA_CMD_PIO_READ_EXT:
3273 case ATA_CMD_READ_STREAM_DMA_EXT:
3274 case ATA_CMD_READ_STREAM_EXT:
3275 case ATA_CMD_VERIFY:
3276 case ATA_CMD_VERIFY_EXT:
3277 case ATA_CMD_WRITE:
3278 case ATA_CMD_WRITE_EXT:
3279 case ATA_CMD_WRITE_FUA_EXT:
3280 case ATA_CMD_WRITE_QUEUED:
3281 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3282 case ATA_CMD_FPDMA_WRITE:
3283 case ATA_CMD_WRITE_MULTI:
3284 case ATA_CMD_WRITE_MULTI_EXT:
3285 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3286 case ATA_CMD_PIO_WRITE:
3287 case ATA_CMD_PIO_WRITE_EXT:
3288 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3289 case ATA_CMD_WRITE_STREAM_EXT:
3290 qc->sect_size = scmd->device->sector_size;
3291 break;
3292
3293 /* Everything else uses 512 byte "sectors" */
3294 default:
3295 qc->sect_size = ATA_SECT_SIZE;
Tejun Heobd30add2009-09-03 16:08:11 +09003296 }
3297
3298 /*
3299 * Set flags so that all registers will be written, pass on
3300 * write indication (used for PIO/DMA setup), result TF is
3301 * copied back and we don't whine too much about its failure.
3302 */
Douglas Gilbertbc496ed2010-02-01 13:11:38 -05003303 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heobd30add2009-09-03 16:08:11 +09003304 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3305 tf->flags |= ATA_TFLAG_WRITE;
3306
3307 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3308
3309 /*
3310 * Set transfer length.
3311 *
3312 * TODO: find out if we need to do more here to
3313 * cover scatter/gather case.
3314 */
3315 ata_qc_set_pc_nbytes(qc);
3316
3317 /* We may not issue DMA commands if no DMA mode is set */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003318 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) {
3319 fp = 1;
Tejun Heobd30add2009-09-03 16:08:11 +09003320 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003321 }
Tejun Heobd30add2009-09-03 16:08:11 +09003322
Eric Biggers2c1ec6f2018-02-03 20:33:51 -08003323 /* We may not issue NCQ commands to devices not supporting NCQ */
3324 if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) {
3325 fp = 1;
3326 goto invalid_fld;
3327 }
3328
Albert Lee1dce5892007-06-07 15:49:22 +08003329 /* sanity check for pio multi commands */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003330 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
3331 fp = 1;
Albert Lee1dce5892007-06-07 15:49:22 +08003332 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003333 }
Albert Lee1dce5892007-06-07 15:49:22 +08003334
3335 if (is_multi_taskfile(tf)) {
3336 unsigned int multi_count = 1 << (cdb[1] >> 5);
3337
3338 /* compare the passed through multi_count
3339 * with the cached multi_count of libata
3340 */
3341 if (multi_count != dev->multi_count)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003342 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3343 multi_count);
Alan Coxd26fc952007-07-06 19:13:52 -04003344 }
Albert Lee1dce5892007-06-07 15:49:22 +08003345
Jeff Garzikb0955182005-05-12 15:45:22 -04003346 /*
3347 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3348 * SET_FEATURES - XFER MODE must be preceded/succeeded
3349 * by an update to hardware-specific registers for each
3350 * controller (i.e. the reason for ->set_piomode(),
3351 * ->set_dmamode(), and ->post_set_mode() hooks).
3352 */
Tejun Heobd30add2009-09-03 16:08:11 +09003353 if (tf->command == ATA_CMD_SET_FEATURES &&
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003354 tf->feature == SETFEATURES_XFER) {
3355 fp = (cdb[0] == ATA_16) ? 4 : 3;
Tejun Heo9a405252005-12-02 11:49:11 +09003356 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003357 }
Jeff Garzikb0955182005-05-12 15:45:22 -04003358
3359 /*
Tejun Heobd30add2009-09-03 16:08:11 +09003360 * Filter TPM commands by default. These provide an
3361 * essentially uncontrolled encrypted "back door" between
3362 * applications and the disk. Set libata.allow_tpm=1 if you
3363 * have a real reason for wanting to use them. This ensures
3364 * that installed software cannot easily mess stuff up without
3365 * user intent. DVR type users will probably ship with this enabled
3366 * for movie content management.
Jeff Garzikb0955182005-05-12 15:45:22 -04003367 *
Tejun Heobd30add2009-09-03 16:08:11 +09003368 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3369 * for this and should do in future but that it is not sufficient as
3370 * DCS is an optional feature set. Thus we also do the software filter
3371 * so that we comply with the TC consortium stated goal that the user
3372 * can turn off TC features of their system.
Jeff Garzikb0955182005-05-12 15:45:22 -04003373 */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003374 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
3375 fp = (cdb[0] == ATA_16) ? 14 : 9;
Tejun Heobd30add2009-09-03 16:08:11 +09003376 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003377 }
Tejun Heoe61e0672006-05-15 20:57:40 +09003378
Jeff Garzikb0955182005-05-12 15:45:22 -04003379 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09003380
3381 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003382 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
Tejun Heo9a405252005-12-02 11:49:11 +09003383 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003384}
3385
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003386/**
3387 * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim
3388 * @cmd: SCSI command being translated
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003389 * @trmax: Maximum number of entries that will fit in sector_size bytes.
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003390 * @sector: Starting sector
Shaun Tancheff7b203092016-08-21 23:23:19 -05003391 * @count: Total Range of request in logical sectors
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003392 *
3393 * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted
3394 * descriptor.
3395 *
3396 * Upto 64 entries of the format:
3397 * 63:48 Range Length
3398 * 47:0 LBA
3399 *
3400 * Range Length of 0 is ignored.
3401 * LBA's should be sorted order and not overlap.
3402 *
3403 * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003404 *
3405 * Return: Number of bytes copied into sglist.
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003406 */
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003407static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax,
3408 u64 sector, u32 count)
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003409{
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003410 struct scsi_device *sdp = cmd->device;
3411 size_t len = sdp->sector_size;
3412 size_t r;
3413 __le64 *buf;
3414 u32 i = 0;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003415 unsigned long flags;
3416
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003417 WARN_ON(len > ATA_SCSI_RBUF_SIZE);
3418
3419 if (len > ATA_SCSI_RBUF_SIZE)
3420 len = ATA_SCSI_RBUF_SIZE;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003421
3422 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003423 buf = ((void *)ata_scsi_rbuf);
3424 memset(buf, 0, len);
3425 while (i < trmax) {
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003426 u64 entry = sector |
3427 ((u64)(count > 0xffff ? 0xffff : count) << 48);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003428 buf[i++] = __cpu_to_le64(entry);
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003429 if (count <= 0xffff)
3430 break;
3431 count -= 0xffff;
3432 sector += 0xffff;
3433 }
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003434 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003435 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
3436
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003437 return r;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003438}
3439
Shaun Tancheff7b203092016-08-21 23:23:19 -05003440/**
Shaun Tancheff7b203092016-08-21 23:23:19 -05003441 * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same
3442 * @qc: Command to be translated
3443 *
3444 * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or
3445 * an SCT Write Same command.
Mauro Carvalho Chehab6baf20b2017-05-16 09:16:18 -03003446 * Based on WRITE SAME has the UNMAP flag:
3447 *
3448 * - When set translate to DSM TRIM
3449 * - When clear translate to SCT Write Same
Shaun Tancheff7b203092016-08-21 23:23:19 -05003450 */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003451static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3452{
3453 struct ata_taskfile *tf = &qc->tf;
3454 struct scsi_cmnd *scmd = qc->scsicmd;
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003455 struct scsi_device *sdp = scmd->device;
3456 size_t len = sdp->sector_size;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003457 struct ata_device *dev = qc->dev;
3458 const u8 *cdb = scmd->cmnd;
3459 u64 block;
3460 u32 n_block;
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003461 const u32 trmax = len >> 3;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003462 u32 size;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003463 u16 fp;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003464 u8 bp = 0xff;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003465 u8 unmap = cdb[1] & 0x8;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003466
3467 /* we may not issue DMA commands if no DMA mode is set */
3468 if (unlikely(!dev->dma_mode))
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003469 goto invalid_opcode;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003470
Christoph Hellwigc6ade202017-04-25 13:39:54 +02003471 /*
3472 * We only allow sending this command through the block layer,
3473 * as it modifies the DATA OUT buffer, which would corrupt user
3474 * memory for SG_IO commands.
3475 */
3476 if (unlikely(blk_rq_is_passthrough(scmd->request)))
3477 goto invalid_opcode;
3478
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003479 if (unlikely(scmd->cmd_len < 16)) {
3480 fp = 15;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003481 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003482 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003483 scsi_16_lba_len(cdb, &block, &n_block);
3484
Christoph Hellwig63ccc192017-04-25 14:26:52 +02003485 if (!unmap ||
3486 (dev->horkage & ATA_HORKAGE_NOTRIM) ||
3487 !ata_id_has_trim(dev->id)) {
3488 fp = 1;
3489 bp = 3;
3490 goto invalid_fld;
3491 }
3492 /* If the request is too large the cmd is invalid */
3493 if (n_block > 0xffff * trmax) {
3494 fp = 2;
3495 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003496 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003497
3498 /*
3499 * WRITE SAME always has a sector sized buffer as payload, this
3500 * should never be a multiple entry S/G list.
3501 */
3502 if (!scsi_sg_count(scmd))
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003503 goto invalid_param_len;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003504
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003505 /*
3506 * size must match sector size in bytes
3507 * For DATA SET MANAGEMENT TRIM in ACS-2 nsect (aka count)
3508 * is defined as number of 512 byte blocks to be transferred.
3509 */
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003510
Christoph Hellwig63ccc192017-04-25 14:26:52 +02003511 size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block);
3512 if (size != len)
3513 goto invalid_param_len;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003514
Christoph Hellwig63ccc192017-04-25 14:26:52 +02003515 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3516 /* Newer devices support queued TRIM commands */
3517 tf->protocol = ATA_PROT_NCQ;
3518 tf->command = ATA_CMD_FPDMA_SEND;
3519 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
Jens Axboe4e5b6262018-05-11 12:51:04 -06003520 tf->nsect = qc->hw_tag << 3;
Christoph Hellwig63ccc192017-04-25 14:26:52 +02003521 tf->hob_feature = (size / 512) >> 8;
3522 tf->feature = size / 512;
3523
3524 tf->auxiliary = 1;
Marc Carino87fb6c32013-08-24 23:22:51 -07003525 } else {
Shaun Tancheff7b203092016-08-21 23:23:19 -05003526 tf->protocol = ATA_PROT_DMA;
Christoph Hellwig63ccc192017-04-25 14:26:52 +02003527 tf->hob_feature = 0;
3528 tf->feature = ATA_DSM_TRIM;
3529 tf->hob_nsect = (size / 512) >> 8;
3530 tf->nsect = size / 512;
3531 tf->command = ATA_CMD_DSM;
Marc Carino87fb6c32013-08-24 23:22:51 -07003532 }
3533
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003534 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3535 ATA_TFLAG_WRITE;
3536
3537 ata_qc_set_pc_nbytes(qc);
3538
3539 return 0;
3540
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003541invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003542 ata_scsi_set_invalid_field(dev, scmd, fp, bp);
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003543 return 1;
3544invalid_param_len:
3545 /* "Parameter list length error" */
3546 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3547 return 1;
3548invalid_opcode:
3549 /* "Invalid command operation code" */
3550 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003551 return 1;
3552}
3553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554/**
Shaun Tancheff7b203092016-08-21 23:23:19 -05003555 * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN
3556 * @args: device MAINTENANCE_IN data / SCSI command of interest.
3557 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
3558 *
3559 * Yields a subset to satisfy scsi_report_opcode()
3560 *
3561 * LOCKING:
3562 * spin_lock_irqsave(host lock)
3563 */
3564static unsigned int ata_scsiop_maint_in(struct ata_scsi_args *args, u8 *rbuf)
3565{
3566 struct ata_device *dev = args->dev;
3567 u8 *cdb = args->cmd->cmnd;
3568 u8 supported = 0;
3569 unsigned int err = 0;
3570
3571 if (cdb[2] != 1) {
3572 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
3573 err = 2;
3574 goto out;
3575 }
3576 switch (cdb[3]) {
3577 case INQUIRY:
3578 case MODE_SENSE:
3579 case MODE_SENSE_10:
3580 case READ_CAPACITY:
3581 case SERVICE_ACTION_IN_16:
3582 case REPORT_LUNS:
3583 case REQUEST_SENSE:
3584 case SYNCHRONIZE_CACHE:
3585 case REZERO_UNIT:
3586 case SEEK_6:
3587 case SEEK_10:
3588 case TEST_UNIT_READY:
3589 case SEND_DIAGNOSTIC:
3590 case MAINTENANCE_IN:
3591 case READ_6:
3592 case READ_10:
3593 case READ_16:
3594 case WRITE_6:
3595 case WRITE_10:
3596 case WRITE_16:
3597 case ATA_12:
3598 case ATA_16:
3599 case VERIFY:
3600 case VERIFY_16:
3601 case MODE_SELECT:
3602 case MODE_SELECT_10:
3603 case START_STOP:
3604 supported = 3;
3605 break;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003606 case ZBC_IN:
3607 case ZBC_OUT:
3608 if (ata_id_zoned_cap(dev->id) ||
3609 dev->class == ATA_DEV_ZAC)
3610 supported = 3;
3611 break;
Christoph Hellwig818831c2017-06-04 14:42:24 +02003612 case SECURITY_PROTOCOL_IN:
3613 case SECURITY_PROTOCOL_OUT:
3614 if (dev->flags & ATA_DFLAG_TRUSTED)
3615 supported = 3;
3616 break;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003617 default:
3618 break;
3619 }
3620out:
3621 rbuf[1] = supported; /* supported */
3622 return err;
3623}
3624
3625/**
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003626 * ata_scsi_report_zones_complete - convert ATA output
3627 * @qc: command structure returning the data
3628 *
3629 * Convert T-13 little-endian field representation into
3630 * T-10 big-endian field representation.
3631 * What a mess.
3632 */
3633static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
3634{
3635 struct scsi_cmnd *scmd = qc->scsicmd;
3636 struct sg_mapping_iter miter;
3637 unsigned long flags;
3638 unsigned int bytes = 0;
3639
3640 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
3641 SG_MITER_TO_SG | SG_MITER_ATOMIC);
3642
3643 local_irq_save(flags);
3644 while (sg_miter_next(&miter)) {
3645 unsigned int offset = 0;
3646
3647 if (bytes == 0) {
3648 char *hdr;
3649 u32 list_length;
3650 u64 max_lba, opt_lba;
3651 u16 same;
3652
3653 /* Swizzle header */
3654 hdr = miter.addr;
3655 list_length = get_unaligned_le32(&hdr[0]);
3656 same = get_unaligned_le16(&hdr[4]);
3657 max_lba = get_unaligned_le64(&hdr[8]);
3658 opt_lba = get_unaligned_le64(&hdr[16]);
3659 put_unaligned_be32(list_length, &hdr[0]);
3660 hdr[4] = same & 0xf;
3661 put_unaligned_be64(max_lba, &hdr[8]);
3662 put_unaligned_be64(opt_lba, &hdr[16]);
3663 offset += 64;
3664 bytes += 64;
3665 }
3666 while (offset < miter.length) {
3667 char *rec;
3668 u8 cond, type, non_seq, reset;
3669 u64 size, start, wp;
3670
3671 /* Swizzle zone descriptor */
3672 rec = miter.addr + offset;
3673 type = rec[0] & 0xf;
3674 cond = (rec[1] >> 4) & 0xf;
3675 non_seq = (rec[1] & 2);
3676 reset = (rec[1] & 1);
3677 size = get_unaligned_le64(&rec[8]);
3678 start = get_unaligned_le64(&rec[16]);
3679 wp = get_unaligned_le64(&rec[24]);
3680 rec[0] = type;
3681 rec[1] = (cond << 4) | non_seq | reset;
3682 put_unaligned_be64(size, &rec[8]);
3683 put_unaligned_be64(start, &rec[16]);
3684 put_unaligned_be64(wp, &rec[24]);
3685 WARN_ON(offset + 64 > miter.length);
3686 offset += 64;
3687 bytes += 64;
3688 }
3689 }
3690 sg_miter_stop(&miter);
3691 local_irq_restore(flags);
3692
3693 ata_scsi_qc_complete(qc);
3694}
3695
3696static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
3697{
3698 struct ata_taskfile *tf = &qc->tf;
3699 struct scsi_cmnd *scmd = qc->scsicmd;
3700 const u8 *cdb = scmd->cmnd;
3701 u16 sect, fp = (u16)-1;
3702 u8 sa, options, bp = 0xff;
3703 u64 block;
3704 u32 n_block;
3705
3706 if (unlikely(scmd->cmd_len < 16)) {
3707 ata_dev_warn(qc->dev, "invalid cdb length %d\n",
3708 scmd->cmd_len);
3709 fp = 15;
3710 goto invalid_fld;
3711 }
3712 scsi_16_lba_len(cdb, &block, &n_block);
3713 if (n_block != scsi_bufflen(scmd)) {
3714 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
3715 n_block, scsi_bufflen(scmd));
3716 goto invalid_param_len;
3717 }
3718 sa = cdb[1] & 0x1f;
3719 if (sa != ZI_REPORT_ZONES) {
3720 ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
3721 fp = 1;
3722 goto invalid_fld;
3723 }
3724 /*
3725 * ZAC allows only for transfers in 512 byte blocks,
3726 * and uses a 16 bit value for the transfer count.
3727 */
3728 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
3729 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
3730 goto invalid_param_len;
3731 }
3732 sect = n_block / 512;
Damien Le Moal3f174222016-07-14 09:05:49 +09003733 options = cdb[14] & 0xbf;
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003734
3735 if (ata_ncq_enabled(qc->dev) &&
3736 ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
3737 tf->protocol = ATA_PROT_NCQ;
3738 tf->command = ATA_CMD_FPDMA_RECV;
3739 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
Jens Axboe4e5b6262018-05-11 12:51:04 -06003740 tf->nsect = qc->hw_tag << 3;
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003741 tf->feature = sect & 0xff;
3742 tf->hob_feature = (sect >> 8) & 0xff;
Damien Le Moal3f174222016-07-14 09:05:49 +09003743 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003744 } else {
3745 tf->command = ATA_CMD_ZAC_MGMT_IN;
3746 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
3747 tf->protocol = ATA_PROT_DMA;
3748 tf->hob_feature = options;
3749 tf->hob_nsect = (sect >> 8) & 0xff;
3750 tf->nsect = sect & 0xff;
3751 }
3752 tf->device = ATA_LBA;
3753 tf->lbah = (block >> 16) & 0xff;
3754 tf->lbam = (block >> 8) & 0xff;
3755 tf->lbal = block & 0xff;
3756 tf->hob_lbah = (block >> 40) & 0xff;
3757 tf->hob_lbam = (block >> 32) & 0xff;
3758 tf->hob_lbal = (block >> 24) & 0xff;
3759
3760 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3761 qc->flags |= ATA_QCFLAG_RESULT_TF;
3762
3763 ata_qc_set_pc_nbytes(qc);
3764
3765 qc->complete_fn = ata_scsi_report_zones_complete;
3766
3767 return 0;
3768
3769invalid_fld:
3770 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
3771 return 1;
3772
3773invalid_param_len:
3774 /* "Parameter list length error" */
3775 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3776 return 1;
3777}
3778
Hannes Reinecke27708a92016-04-25 12:45:53 +02003779static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
3780{
3781 struct ata_taskfile *tf = &qc->tf;
3782 struct scsi_cmnd *scmd = qc->scsicmd;
3783 struct ata_device *dev = qc->dev;
3784 const u8 *cdb = scmd->cmnd;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003785 u8 all, sa;
Hannes Reinecke27708a92016-04-25 12:45:53 +02003786 u64 block;
3787 u32 n_block;
3788 u16 fp = (u16)-1;
3789
3790 if (unlikely(scmd->cmd_len < 16)) {
3791 fp = 15;
3792 goto invalid_fld;
3793 }
3794
3795 sa = cdb[1] & 0x1f;
3796 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
3797 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
3798 fp = 1;
3799 goto invalid_fld;
3800 }
3801
3802 scsi_16_lba_len(cdb, &block, &n_block);
3803 if (n_block) {
3804 /*
3805 * ZAC MANAGEMENT OUT doesn't define any length
3806 */
3807 goto invalid_param_len;
3808 }
3809 if (block > dev->n_sectors)
3810 goto out_of_range;
3811
Damien Le Moal2950cef2016-07-14 09:05:51 +09003812 all = cdb[14] & 0x1;
Hannes Reinecke27708a92016-04-25 12:45:53 +02003813
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003814 if (ata_ncq_enabled(qc->dev) &&
3815 ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
Hannes Reinecke5b844b62016-07-14 09:05:48 +09003816 tf->protocol = ATA_PROT_NCQ_NODATA;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003817 tf->command = ATA_CMD_NCQ_NON_DATA;
Damien Le Moalee194b72016-07-14 09:05:50 +09003818 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
Jens Axboe4e5b6262018-05-11 12:51:04 -06003819 tf->nsect = qc->hw_tag << 3;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003820 tf->auxiliary = sa | ((u16)all << 8);
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003821 } else {
3822 tf->protocol = ATA_PROT_NODATA;
3823 tf->command = ATA_CMD_ZAC_MGMT_OUT;
3824 tf->feature = sa;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003825 tf->hob_feature = all;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003826 }
Hannes Reinecke27708a92016-04-25 12:45:53 +02003827 tf->lbah = (block >> 16) & 0xff;
3828 tf->lbam = (block >> 8) & 0xff;
3829 tf->lbal = block & 0xff;
3830 tf->hob_lbah = (block >> 40) & 0xff;
3831 tf->hob_lbam = (block >> 32) & 0xff;
3832 tf->hob_lbal = (block >> 24) & 0xff;
3833 tf->device = ATA_LBA;
3834 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3835
3836 return 0;
3837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 invalid_fld:
Hannes Reinecke27708a92016-04-25 12:45:53 +02003839 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
3840 return 1;
3841 out_of_range:
3842 /* "Logical Block Address out of range" */
3843 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x00);
3844 return 1;
3845invalid_param_len:
3846 /* "Parameter list length error" */
3847 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 return 1;
3849}
3850
3851/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003852 * ata_mselect_caching - Simulate MODE SELECT for caching info page
3853 * @qc: Storage for translated ATA taskfile
3854 * @buf: input buffer
3855 * @len: number of valid bytes in the input buffer
Hannes Reinecke77800812016-04-04 11:44:07 +02003856 * @fp: out parameter for the failed field on error
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003857 *
3858 * Prepare a taskfile to modify caching information for the device.
3859 *
3860 * LOCKING:
3861 * None.
3862 */
3863static int ata_mselect_caching(struct ata_queued_cmd *qc,
Hannes Reinecke77800812016-04-04 11:44:07 +02003864 const u8 *buf, int len, u16 *fp)
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003865{
3866 struct ata_taskfile *tf = &qc->tf;
3867 struct ata_device *dev = qc->dev;
Tom Yand7372cb2016-07-23 02:34:08 +08003868 u8 mpage[CACHE_MPAGE_LEN];
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003869 u8 wce;
Hannes Reinecke77800812016-04-04 11:44:07 +02003870 int i;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003871
3872 /*
3873 * The first two bytes of def_cache_mpage are a header, so offsets
3874 * in mpage are off by 2 compared to buf. Same for len.
3875 */
3876
Hannes Reinecke77800812016-04-04 11:44:07 +02003877 if (len != CACHE_MPAGE_LEN - 2) {
3878 if (len < CACHE_MPAGE_LEN - 2)
3879 *fp = len;
3880 else
3881 *fp = CACHE_MPAGE_LEN - 2;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003882 return -EINVAL;
Hannes Reinecke77800812016-04-04 11:44:07 +02003883 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003884
3885 wce = buf[0] & (1 << 2);
3886
3887 /*
3888 * Check that read-only bits are not modified.
3889 */
3890 ata_msense_caching(dev->id, mpage, false);
Hannes Reinecke77800812016-04-04 11:44:07 +02003891 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
3892 if (i == 0)
3893 continue;
3894 if (mpage[i + 2] != buf[i]) {
3895 *fp = i;
3896 return -EINVAL;
3897 }
3898 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003899
3900 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3901 tf->protocol = ATA_PROT_NODATA;
3902 tf->nsect = 0;
3903 tf->command = ATA_CMD_SET_FEATURES;
3904 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3905 return 0;
3906}
3907
3908/**
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003909 * ata_mselect_control - Simulate MODE SELECT for control page
3910 * @qc: Storage for translated ATA taskfile
3911 * @buf: input buffer
3912 * @len: number of valid bytes in the input buffer
Hannes Reinecke77800812016-04-04 11:44:07 +02003913 * @fp: out parameter for the failed field on error
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003914 *
3915 * Prepare a taskfile to modify caching information for the device.
3916 *
3917 * LOCKING:
3918 * None.
3919 */
3920static int ata_mselect_control(struct ata_queued_cmd *qc,
Hannes Reinecke77800812016-04-04 11:44:07 +02003921 const u8 *buf, int len, u16 *fp)
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003922{
3923 struct ata_device *dev = qc->dev;
Tom Yand7372cb2016-07-23 02:34:08 +08003924 u8 mpage[CONTROL_MPAGE_LEN];
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003925 u8 d_sense;
Hannes Reinecke77800812016-04-04 11:44:07 +02003926 int i;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003927
3928 /*
3929 * The first two bytes of def_control_mpage are a header, so offsets
3930 * in mpage are off by 2 compared to buf. Same for len.
3931 */
3932
Hannes Reinecke77800812016-04-04 11:44:07 +02003933 if (len != CONTROL_MPAGE_LEN - 2) {
3934 if (len < CONTROL_MPAGE_LEN - 2)
3935 *fp = len;
3936 else
3937 *fp = CONTROL_MPAGE_LEN - 2;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003938 return -EINVAL;
Hannes Reinecke77800812016-04-04 11:44:07 +02003939 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003940
3941 d_sense = buf[0] & (1 << 2);
3942
3943 /*
3944 * Check that read-only bits are not modified.
3945 */
Tom Yanf086b742016-07-13 02:54:12 +08003946 ata_msense_control(dev, mpage, false);
Hannes Reinecke77800812016-04-04 11:44:07 +02003947 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
3948 if (i == 0)
3949 continue;
3950 if (mpage[2 + i] != buf[i]) {
3951 *fp = i;
3952 return -EINVAL;
3953 }
3954 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003955 if (d_sense & (1 << 2))
3956 dev->flags |= ATA_DFLAG_D_SENSE;
3957 else
3958 dev->flags &= ~ATA_DFLAG_D_SENSE;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003959 return 0;
3960}
3961
3962/**
Minwoo Im01efd602017-06-20 19:21:30 +09003963 * ata_scsi_mode_select_xlat - Simulate MODE SELECT 6, 10 commands
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003964 * @qc: Storage for translated ATA taskfile
3965 *
3966 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
3967 * Assume this is invoked for direct access devices (e.g. disks) only.
3968 * There should be no block descriptor for other device types.
3969 *
3970 * LOCKING:
3971 * spin_lock_irqsave(host lock)
3972 */
3973static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
3974{
3975 struct scsi_cmnd *scmd = qc->scsicmd;
3976 const u8 *cdb = scmd->cmnd;
3977 const u8 *p;
3978 u8 pg, spg;
3979 unsigned six_byte, pg_len, hdr_len, bd_len;
3980 int len;
Hannes Reinecke77800812016-04-04 11:44:07 +02003981 u16 fp = (u16)-1;
3982 u8 bp = 0xff;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003983
3984 VPRINTK("ENTER\n");
3985
3986 six_byte = (cdb[0] == MODE_SELECT);
3987 if (six_byte) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003988 if (scmd->cmd_len < 5) {
3989 fp = 4;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003990 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003991 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003992
3993 len = cdb[4];
3994 hdr_len = 4;
3995 } else {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003996 if (scmd->cmd_len < 9) {
3997 fp = 8;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003998 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003999 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004000
4001 len = (cdb[7] << 8) + cdb[8];
4002 hdr_len = 8;
4003 }
4004
4005 /* We only support PF=1, SP=0. */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004006 if ((cdb[1] & 0x11) != 0x10) {
4007 fp = 1;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02004008 bp = (cdb[1] & 0x01) ? 1 : 5;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004009 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004010 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004011
4012 /* Test early for possible overrun. */
4013 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
4014 goto invalid_param_len;
4015
4016 p = page_address(sg_page(scsi_sglist(scmd)));
4017
4018 /* Move past header and block descriptors. */
4019 if (len < hdr_len)
4020 goto invalid_param_len;
4021
4022 if (six_byte)
4023 bd_len = p[3];
4024 else
4025 bd_len = (p[6] << 8) + p[7];
4026
4027 len -= hdr_len;
4028 p += hdr_len;
4029 if (len < bd_len)
4030 goto invalid_param_len;
Hannes Reinecke77800812016-04-04 11:44:07 +02004031 if (bd_len != 0 && bd_len != 8) {
4032 fp = (six_byte) ? 3 : 6;
4033 fp += bd_len + hdr_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004034 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004035 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004036
4037 len -= bd_len;
4038 p += bd_len;
4039 if (len == 0)
4040 goto skip;
4041
4042 /* Parse both possible formats for the mode page headers. */
4043 pg = p[0] & 0x3f;
4044 if (p[0] & 0x40) {
4045 if (len < 4)
4046 goto invalid_param_len;
4047
4048 spg = p[1];
4049 pg_len = (p[2] << 8) | p[3];
4050 p += 4;
4051 len -= 4;
4052 } else {
4053 if (len < 2)
4054 goto invalid_param_len;
4055
4056 spg = 0;
4057 pg_len = p[1];
4058 p += 2;
4059 len -= 2;
4060 }
4061
4062 /*
4063 * No mode subpages supported (yet) but asking for _all_
4064 * subpages may be valid
4065 */
Hannes Reinecke77800812016-04-04 11:44:07 +02004066 if (spg && (spg != ALL_SUB_MPAGES)) {
4067 fp = (p[0] & 0x40) ? 1 : 0;
4068 fp += hdr_len + bd_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004069 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004070 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004071 if (pg_len > len)
4072 goto invalid_param_len;
4073
4074 switch (pg) {
4075 case CACHE_MPAGE:
Hannes Reinecke77800812016-04-04 11:44:07 +02004076 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
4077 fp += hdr_len + bd_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004078 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004079 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004080 break;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004081 case CONTROL_MPAGE:
Hannes Reinecke77800812016-04-04 11:44:07 +02004082 if (ata_mselect_control(qc, p, pg_len, &fp) < 0) {
4083 fp += hdr_len + bd_len;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004084 goto invalid_param;
Tom Yan535fd072016-07-22 02:41:54 +08004085 } else {
4086 goto skip; /* No ATA command to send */
Hannes Reinecke77800812016-04-04 11:44:07 +02004087 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004088 break;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004089 default: /* invalid page code */
Hannes Reinecke77800812016-04-04 11:44:07 +02004090 fp = bd_len + hdr_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004091 goto invalid_param;
4092 }
4093
4094 /*
4095 * Only one page has changeable data, so we only support setting one
4096 * page at a time.
4097 */
4098 if (len > pg_len)
4099 goto invalid_param;
4100
4101 return 0;
4102
4103 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02004104 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004105 return 1;
4106
4107 invalid_param:
Hannes Reinecke77800812016-04-04 11:44:07 +02004108 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004109 return 1;
4110
4111 invalid_param_len:
4112 /* "Parameter list length error" */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004113 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004114 return 1;
4115
4116 skip:
4117 scmd->result = SAM_STAT_GOOD;
4118 return 1;
4119}
4120
Christoph Hellwig818831c2017-06-04 14:42:24 +02004121static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma)
4122{
4123 if (len == 0)
4124 return ATA_CMD_TRUSTED_NONDATA;
4125 else if (send)
4126 return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND;
4127 else
4128 return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV;
4129}
4130
4131static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc)
4132{
4133 struct scsi_cmnd *scmd = qc->scsicmd;
4134 const u8 *cdb = scmd->cmnd;
4135 struct ata_taskfile *tf = &qc->tf;
4136 u8 secp = cdb[1];
4137 bool send = (cdb[0] == SECURITY_PROTOCOL_OUT);
4138 u16 spsp = get_unaligned_be16(&cdb[2]);
4139 u32 len = get_unaligned_be32(&cdb[6]);
4140 bool dma = !(qc->dev->flags & ATA_DFLAG_PIO);
4141
4142 /*
4143 * We don't support the ATA "security" protocol.
4144 */
4145 if (secp == 0xef) {
4146 ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0);
4147 return 1;
4148 }
4149
4150 if (cdb[4] & 7) { /* INC_512 */
4151 if (len > 0xffff) {
4152 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4153 return 1;
4154 }
4155 } else {
4156 if (len > 0x01fffe00) {
4157 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4158 return 1;
4159 }
4160
4161 /* convert to the sector-based ATA addressing */
4162 len = (len + 511) / 512;
4163 }
4164
4165 tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO;
4166 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA;
4167 if (send)
4168 tf->flags |= ATA_TFLAG_WRITE;
4169 tf->command = ata_scsi_trusted_op(len, send, dma);
4170 tf->feature = secp;
4171 tf->lbam = spsp & 0xff;
4172 tf->lbah = spsp >> 8;
4173
4174 if (len) {
4175 tf->nsect = len & 0xff;
4176 tf->lbal = len >> 8;
4177 } else {
4178 if (!send)
4179 tf->lbah = (1 << 7);
4180 }
4181
4182 ata_qc_set_pc_nbytes(qc);
4183 return 0;
4184}
4185
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004186/**
Minwoo Imb1ffbf82017-06-24 03:41:10 +09004187 * ata_scsi_var_len_cdb_xlat - SATL variable length CDB to Handler
4188 * @qc: Command to be translated
4189 *
4190 * Translate a SCSI variable length CDB to specified commands.
4191 * It checks a service action value in CDB to call corresponding handler.
4192 *
4193 * RETURNS:
4194 * Zero on success, non-zero on failure
4195 *
4196 */
4197static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc)
4198{
4199 struct scsi_cmnd *scmd = qc->scsicmd;
4200 const u8 *cdb = scmd->cmnd;
4201 const u16 sa = get_unaligned_be16(&cdb[8]);
4202
4203 /*
4204 * if service action represents a ata pass-thru(32) command,
4205 * then pass it to ata_scsi_pass_thru handler.
4206 */
4207 if (sa == ATA_32)
4208 return ata_scsi_pass_thru(qc);
4209
Minwoo Imb1ffbf82017-06-24 03:41:10 +09004210 /* unsupported service action */
4211 return 1;
4212}
4213
4214/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 * ata_get_xlat_func - check if SCSI to ATA translation is possible
4216 * @dev: ATA device
4217 * @cmd: SCSI command opcode to consider
4218 *
4219 * Look up the SCSI command given, and determine whether the
4220 * SCSI command is to be translated or simulated.
4221 *
4222 * RETURNS:
4223 * Pointer to translation function if possible, %NULL if not.
4224 */
4225
4226static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
4227{
4228 switch (cmd) {
4229 case READ_6:
4230 case READ_10:
4231 case READ_16:
4232
4233 case WRITE_6:
4234 case WRITE_10:
4235 case WRITE_16:
4236 return ata_scsi_rw_xlat;
4237
Christoph Hellwig0cdd6eb2009-12-10 10:36:01 +01004238 case WRITE_SAME_16:
Christoph Hellwig18f0f972009-11-17 10:00:47 -05004239 return ata_scsi_write_same_xlat;
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 case SYNCHRONIZE_CACHE:
4242 if (ata_try_flush_cache(dev))
4243 return ata_scsi_flush_xlat;
4244 break;
4245
4246 case VERIFY:
4247 case VERIFY_16:
4248 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04004249
4250 case ATA_12:
4251 case ATA_16:
4252 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04004253
Minwoo Imb1ffbf82017-06-24 03:41:10 +09004254 case VARIABLE_LENGTH_CMD:
4255 return ata_scsi_var_len_cdb_xlat;
4256
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004257 case MODE_SELECT:
4258 case MODE_SELECT_10:
4259 return ata_scsi_mode_select_xlat;
4260 break;
4261
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02004262 case ZBC_IN:
4263 return ata_scsi_zbc_in_xlat;
4264
Hannes Reinecke27708a92016-04-25 12:45:53 +02004265 case ZBC_OUT:
4266 return ata_scsi_zbc_out_xlat;
4267
Christoph Hellwig818831c2017-06-04 14:42:24 +02004268 case SECURITY_PROTOCOL_IN:
4269 case SECURITY_PROTOCOL_OUT:
4270 if (!(dev->flags & ATA_DFLAG_TRUSTED))
4271 break;
4272 return ata_scsi_security_inout_xlat;
4273
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04004274 case START_STOP:
4275 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 }
4277
4278 return NULL;
4279}
4280
4281/**
4282 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
4283 * @ap: ATA port to which the command was being sent
4284 * @cmd: SCSI command to dump
4285 *
4286 * Prints the contents of a SCSI command via printk().
4287 */
4288
4289static inline void ata_scsi_dump_cdb(struct ata_port *ap,
4290 struct scsi_cmnd *cmd)
4291{
Paul Menzel32a53422018-07-08 09:11:34 +02004292#ifdef ATA_VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 struct scsi_device *scsidev = cmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Paul Menzel32a53422018-07-08 09:11:34 +02004295 VPRINTK("CDB (%u:%d,%d,%lld) %9ph\n",
Tejun Heo44877b42007-02-21 01:06:51 +09004296 ap->print_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 scsidev->channel, scsidev->id, scsidev->lun,
Andy Shevchenko26f58832016-05-06 21:40:40 +03004298 cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299#endif
4300}
4301
Tejun Heo542b1442006-12-17 10:45:08 +09004302static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09004303 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06004304{
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004305 u8 scsi_op = scmd->cmnd[0];
4306 ata_xlat_func_t xlat_func;
Tejun Heo2115ea92006-05-15 21:03:39 +09004307 int rc = 0;
4308
Hannes Reinecke9162c652014-11-05 13:08:21 +01004309 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004310 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
4311 goto bad_cdb_len;
4312
4313 xlat_func = ata_get_xlat_func(dev, scsi_op);
4314 } else {
4315 if (unlikely(!scmd->cmd_len))
4316 goto bad_cdb_len;
4317
4318 xlat_func = NULL;
4319 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
4320 /* relay SCSI command to ATAPI device */
Mark Lord607126c2007-11-15 13:13:59 +09004321 int len = COMMAND_SIZE(scsi_op);
Eric Biggers058f58e2018-02-03 20:30:56 -08004322 if (unlikely(len > scmd->cmd_len ||
4323 len > dev->cdb_len ||
4324 scmd->cmd_len > ATAPI_CDB_LEN))
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004325 goto bad_cdb_len;
4326
4327 xlat_func = atapi_xlat;
4328 } else {
4329 /* ATA_16 passthru, treat as an ATA command */
4330 if (unlikely(scmd->cmd_len > 16))
4331 goto bad_cdb_len;
4332
4333 xlat_func = ata_get_xlat_func(dev, scsi_op);
4334 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09004335 }
4336
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004337 if (xlat_func)
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004338 rc = ata_scsi_translate(dev, scmd, xlat_func);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004339 else
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004340 ata_scsi_simulate(dev, scmd);
Tejun Heo2115ea92006-05-15 21:03:39 +09004341
4342 return rc;
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004343
4344 bad_cdb_len:
4345 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
4346 scmd->cmd_len, scsi_op, dev->cdb_len);
4347 scmd->result = DID_ERROR << 16;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004348 scmd->scsi_done(scmd);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004349 return 0;
Brian Kingeb3f0f92006-03-23 17:30:02 -06004350}
4351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352/**
4353 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
Jeff Garzik23e701e2010-11-17 12:03:58 -05004354 * @shost: SCSI host of command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 * @cmd: SCSI command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 *
4357 * In some cases, this function translates SCSI commands into
4358 * ATA taskfiles, and queues the taskfiles to be sent to
4359 * hardware. In other cases, this function simulates a
4360 * SCSI device by evaluating and responding to certain
4361 * SCSI commands. This creates the overall effect of
4362 * ATA and ATAPI devices appearing as SCSI devices.
4363 *
4364 * LOCKING:
Jeff Garzik23e701e2010-11-17 12:03:58 -05004365 * ATA host lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 *
4367 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09004368 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
4369 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 */
Jeff Garzik23e701e2010-11-17 12:03:58 -05004371int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372{
4373 struct ata_port *ap;
4374 struct ata_device *dev;
4375 struct scsi_device *scsidev = cmd->device;
Tejun Heo2115ea92006-05-15 21:03:39 +09004376 int rc = 0;
Jeff Garzik23e701e2010-11-17 12:03:58 -05004377 unsigned long irq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Jeff Garzik35bb94b2006-04-11 13:12:34 -04004379 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05004380
Jeff Garzik23e701e2010-11-17 12:03:58 -05004381 spin_lock_irqsave(ap->lock, irq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
4383 ata_scsi_dump_cdb(ap, cmd);
4384
4385 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06004386 if (likely(dev))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004387 rc = __ata_scsi_queuecmd(cmd, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06004388 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzik23e701e2010-11-17 12:03:58 -05004390 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 }
4392
Jeff Garzik23e701e2010-11-17 12:03:58 -05004393 spin_unlock_irqrestore(ap->lock, irq_flags);
4394
Tejun Heo2115ea92006-05-15 21:03:39 +09004395 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396}
4397
4398/**
4399 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004400 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 * @cmd: SCSI command being sent to device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 *
4403 * Interprets and directly executes a select list of SCSI commands
4404 * that can be handled internally.
4405 *
4406 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004407 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 */
4409
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004410void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411{
4412 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04004413 const u8 *scsicmd = cmd->cmnd;
Jeff Garzik45394142007-09-21 06:23:42 -04004414 u8 tmp8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Tejun Heo9a3dccc2006-01-06 09:56:18 +01004416 args.dev = dev;
4417 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 args.cmd = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
4420 switch(scsicmd[0]) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004421 case INQUIRY:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004422 if (scsicmd[1] & 2) /* is CmdDt set? */
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004423 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004424 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
4425 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
4426 else switch (scsicmd[2]) {
4427 case 0x00:
4428 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004430 case 0x80:
4431 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004433 case 0x83:
4434 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004436 case 0x89:
4437 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 break;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05004439 case 0xb0:
4440 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
4441 break;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06004442 case 0xb1:
4443 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
4444 break;
Martin K. Petersen02e0a602010-09-10 01:23:18 -04004445 case 0xb2:
4446 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
4447 break;
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02004448 case 0xb6:
4449 if (dev->flags & ATA_DFLAG_ZAC) {
4450 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b6);
4451 break;
4452 }
4453 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 default:
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004455 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004457 }
4458 break;
4459
4460 case MODE_SENSE:
4461 case MODE_SENSE_10:
4462 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
4463 break;
4464
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004465 case READ_CAPACITY:
4466 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4467 break;
4468
Hannes Reineckeeb846d92014-11-17 14:25:19 +01004469 case SERVICE_ACTION_IN_16:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004470 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
4471 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4472 else
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004473 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004474 break;
4475
4476 case REPORT_LUNS:
4477 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
4478 break;
4479
4480 case REQUEST_SENSE:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004481 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004482 cmd->result = (DRIVER_SENSE << 24);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004483 break;
4484
4485 /* if we reach this, then writeback caching is disabled,
4486 * turning this into a no-op.
4487 */
4488 case SYNCHRONIZE_CACHE:
4489 /* fall through */
4490
4491 /* no-op's, complete with success */
4492 case REZERO_UNIT:
4493 case SEEK_6:
4494 case SEEK_10:
4495 case TEST_UNIT_READY:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004496 break;
4497
4498 case SEND_DIAGNOSTIC:
4499 tmp8 = scsicmd[1] & ~(1 << 3);
Christoph Hellwiga0c0b0e2017-01-10 09:41:47 +01004500 if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4])
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004501 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004502 break;
4503
Shaun Tancheff7b203092016-08-21 23:23:19 -05004504 case MAINTENANCE_IN:
4505 if (scsicmd[1] == MI_REPORT_SUPPORTED_OPERATION_CODES)
4506 ata_scsi_rbuf_fill(&args, ata_scsiop_maint_in);
4507 else
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004508 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
Shaun Tancheff7b203092016-08-21 23:23:19 -05004509 break;
4510
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004511 /* all other commands */
4512 default:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004513 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004514 /* "Invalid command operation code" */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 }
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004517
4518 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519}
4520
Tejun Heof3187192007-04-17 23:44:07 +09004521int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
4522{
4523 int i, rc;
4524
4525 for (i = 0; i < host->n_ports; i++) {
4526 struct ata_port *ap = host->ports[i];
4527 struct Scsi_Host *shost;
4528
4529 rc = -ENOMEM;
4530 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
4531 if (!shost)
4532 goto err_alloc;
4533
Lin Ming9ee4f392011-12-05 09:20:28 +08004534 shost->eh_noresume = 1;
Tejun Heof3187192007-04-17 23:44:07 +09004535 *(struct ata_port **)&shost->hostdata[0] = ap;
4536 ap->scsi_host = shost;
4537
Gwendal Grignoud9027472010-05-25 12:31:38 -07004538 shost->transportt = ata_scsi_transport_template;
Tejun Heof3187192007-04-17 23:44:07 +09004539 shost->unique_id = ap->print_id;
4540 shost->max_id = 16;
4541 shost->max_lun = 1;
4542 shost->max_channel = 1;
Minwoo Imb1ffbf82017-06-24 03:41:10 +09004543 shost->max_cmd_len = 32;
Tejun Heof3187192007-04-17 23:44:07 +09004544
Tejun Heo31cc23b2007-09-23 13:14:12 +09004545 /* Schedule policy is determined by ->qc_defer()
4546 * callback and it needs to see every deferred qc.
4547 * Set host_blocked to 1 to prevent SCSI midlayer from
4548 * automatically deferring requests.
4549 */
4550 shost->max_host_blocked = 1;
4551
Lin Mingf8fc75d2012-04-12 13:50:39 +08004552 rc = scsi_add_host_with_dma(ap->scsi_host,
4553 &ap->tdev, ap->host->dev);
Tejun Heof3187192007-04-17 23:44:07 +09004554 if (rc)
4555 goto err_add;
4556 }
4557
4558 return 0;
4559
4560 err_add:
4561 scsi_host_put(host->ports[i]->scsi_host);
4562 err_alloc:
4563 while (--i >= 0) {
4564 struct Scsi_Host *shost = host->ports[i]->scsi_host;
4565
4566 scsi_remove_host(shost);
4567 scsi_host_put(shost);
4568 }
4569 return rc;
4570}
4571
Tejun Heo1ae46312007-07-16 14:29:40 +09004572void ata_scsi_scan_host(struct ata_port *ap, int sync)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004573{
Tejun Heo1ae46312007-07-16 14:29:40 +09004574 int tries = 5;
4575 struct ata_device *last_failed_dev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004576 struct ata_link *link;
Tejun Heo1ae46312007-07-16 14:29:40 +09004577 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004578
Tejun Heo1ae46312007-07-16 14:29:40 +09004579 repeat:
Tejun Heo1eca4362008-11-03 20:03:17 +09004580 ata_for_each_link(link, ap, EDGE) {
4581 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004582 struct scsi_device *sdev;
4583 int channel = 0, id = 0;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04004584
Tejun Heo1eca4362008-11-03 20:03:17 +09004585 if (dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004586 continue;
Tejun Heo3edebac2006-05-31 18:27:40 +09004587
Tejun Heo41bda9c2007-08-06 18:36:24 +09004588 if (ata_is_host_link(link))
4589 id = dev->devno;
4590 else
4591 channel = link->pmp;
4592
4593 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
4594 NULL);
4595 if (!IS_ERR(sdev)) {
4596 dev->sdev = sdev;
4597 scsi_device_put(sdev);
Grant Grundler295124d2010-08-17 10:56:53 -07004598 } else {
4599 dev->sdev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004600 }
Tejun Heo3edebac2006-05-31 18:27:40 +09004601 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04004602 }
Tejun Heo1ae46312007-07-16 14:29:40 +09004603
4604 /* If we scanned while EH was in progress or allocation
4605 * failure occurred, scan would have failed silently. Check
4606 * whether all devices are attached.
4607 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004608 ata_for_each_link(link, ap, EDGE) {
4609 ata_for_each_dev(dev, link, ENABLED) {
4610 if (!dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004611 goto exit_loop;
4612 }
Tejun Heo1ae46312007-07-16 14:29:40 +09004613 }
Tejun Heo41bda9c2007-08-06 18:36:24 +09004614 exit_loop:
4615 if (!link)
Tejun Heo1ae46312007-07-16 14:29:40 +09004616 return;
4617
4618 /* we're missing some SCSI devices */
4619 if (sync) {
4620 /* If caller requested synchrnous scan && we've made
4621 * any progress, sleep briefly and repeat.
4622 */
4623 if (dev != last_failed_dev) {
4624 msleep(100);
4625 last_failed_dev = dev;
4626 goto repeat;
4627 }
4628
4629 /* We might be failing to detect boot device, give it
4630 * a few more chances.
4631 */
4632 if (--tries) {
4633 msleep(100);
4634 goto repeat;
4635 }
4636
Joe Perchesa9a79df2011-04-15 15:51:59 -07004637 ata_port_err(ap,
4638 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
Tejun Heo1ae46312007-07-16 14:29:40 +09004639 }
4640
Tejun Heoad72cf92010-07-02 10:03:52 +02004641 queue_delayed_work(system_long_wq, &ap->hotplug_task,
Tejun Heo1ae46312007-07-16 14:29:40 +09004642 round_jiffies_relative(HZ));
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004643}
Tejun Heo0ea035a2006-05-31 18:28:01 +09004644
4645/**
4646 * ata_scsi_offline_dev - offline attached SCSI device
4647 * @dev: ATA device to offline attached SCSI device for
4648 *
4649 * This function is called from ata_eh_hotplug() and responsible
4650 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04004651 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09004652 * against clearing.
4653 *
4654 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004655 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09004656 *
4657 * RETURNS:
4658 * 1 if attached SCSI device exists, 0 otherwise.
4659 */
4660int ata_scsi_offline_dev(struct ata_device *dev)
4661{
4662 if (dev->sdev) {
4663 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
4664 return 1;
4665 }
4666 return 0;
4667}
Tejun Heo580b21022006-05-31 18:28:05 +09004668
4669/**
4670 * ata_scsi_remove_dev - remove attached SCSI device
4671 * @dev: ATA device to remove attached SCSI device for
4672 *
4673 * This function is called from ata_eh_scsi_hotplug() and
4674 * responsible for removing the SCSI device attached to @dev.
4675 *
4676 * LOCKING:
4677 * Kernel thread context (may sleep).
4678 */
4679static void ata_scsi_remove_dev(struct ata_device *dev)
4680{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004681 struct ata_port *ap = dev->link->ap;
Tejun Heo580b21022006-05-31 18:28:05 +09004682 struct scsi_device *sdev;
4683 unsigned long flags;
4684
4685 /* Alas, we need to grab scan_mutex to ensure SCSI device
4686 * state doesn't change underneath us and thus
4687 * scsi_device_get() always succeeds. The mutex locking can
4688 * be removed if there is __scsi_device_get() interface which
4689 * increments reference counts regardless of device state.
4690 */
Jeff Garzikcca39742006-08-24 03:19:22 -04004691 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04004692 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b21022006-05-31 18:28:05 +09004693
Jeff Garzikcca39742006-08-24 03:19:22 -04004694 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b21022006-05-31 18:28:05 +09004695 sdev = dev->sdev;
4696 dev->sdev = NULL;
4697
4698 if (sdev) {
4699 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04004700 * away underneath us after the host lock and
Tejun Heo580b21022006-05-31 18:28:05 +09004701 * scan_mutex are released. Hold onto it.
4702 */
4703 if (scsi_device_get(sdev) == 0) {
4704 /* The following ensures the attached sdev is
4705 * offline on return from ata_scsi_offline_dev()
4706 * regardless it wins or loses the race
4707 * against this function.
4708 */
4709 scsi_device_set_state(sdev, SDEV_OFFLINE);
4710 } else {
4711 WARN_ON(1);
4712 sdev = NULL;
4713 }
4714 }
4715
Jeff Garzikba6a1302006-06-22 23:46:10 -04004716 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04004717 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004718
4719 if (sdev) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004720 ata_dev_info(dev, "detaching (SCSI %s)\n",
4721 dev_name(&sdev->sdev_gendev));
Tejun Heo580b21022006-05-31 18:28:05 +09004722
4723 scsi_remove_device(sdev);
4724 scsi_device_put(sdev);
4725 }
4726}
4727
Tejun Heo41bda9c2007-08-06 18:36:24 +09004728static void ata_scsi_handle_link_detach(struct ata_link *link)
4729{
4730 struct ata_port *ap = link->ap;
4731 struct ata_device *dev;
4732
Tejun Heo1eca4362008-11-03 20:03:17 +09004733 ata_for_each_dev(dev, link, ALL) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004734 unsigned long flags;
4735
4736 if (!(dev->flags & ATA_DFLAG_DETACHED))
4737 continue;
4738
4739 spin_lock_irqsave(ap->lock, flags);
4740 dev->flags &= ~ATA_DFLAG_DETACHED;
4741 spin_unlock_irqrestore(ap->lock, flags);
4742
Aaron Luf1bc1e42013-08-23 10:17:54 +08004743 if (zpodd_dev_enabled(dev))
4744 zpodd_exit(dev);
4745
Tejun Heo41bda9c2007-08-06 18:36:24 +09004746 ata_scsi_remove_dev(dev);
4747 }
4748}
4749
Tejun Heo580b21022006-05-31 18:28:05 +09004750/**
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004751 * ata_scsi_media_change_notify - send media change event
Randy Dunlapc5d0e6a2007-10-15 17:29:46 -07004752 * @dev: Pointer to the disk device with media change event
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004753 *
4754 * Tell the block layer to send a media change notification
4755 * event.
4756 *
4757 * LOCKING:
Tejun Heo854c73a2007-09-23 13:14:11 +09004758 * spin_lock_irqsave(host lock)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004759 */
Tejun Heo854c73a2007-09-23 13:14:11 +09004760void ata_scsi_media_change_notify(struct ata_device *dev)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004761{
Tejun Heo854c73a2007-09-23 13:14:11 +09004762 if (dev->sdev)
Jeff Garzikf26792d2007-10-29 17:18:39 -04004763 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
4764 GFP_ATOMIC);
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004765}
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004766
4767/**
Tejun Heo580b21022006-05-31 18:28:05 +09004768 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00004769 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b21022006-05-31 18:28:05 +09004770 *
4771 * Perform SCSI part of hotplug. It's executed from a separate
4772 * workqueue after EH completes. This is necessary because SCSI
4773 * hot plugging requires working EH and hot unplugging is
4774 * synchronized with hot plugging with a mutex.
4775 *
4776 * LOCKING:
4777 * Kernel thread context (may sleep).
4778 */
David Howells65f27f32006-11-22 14:55:48 +00004779void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b21022006-05-31 18:28:05 +09004780{
David Howells65f27f32006-11-22 14:55:48 +00004781 struct ata_port *ap =
4782 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004783 int i;
Tejun Heo580b21022006-05-31 18:28:05 +09004784
Tejun Heob51e9e52006-06-29 01:29:30 +09004785 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b21022006-05-31 18:28:05 +09004786 DPRINTK("ENTER/EXIT - unloading\n");
4787 return;
4788 }
4789
Tejun Heo85fbd722013-12-18 07:07:32 -05004790 /*
4791 * XXX - UGLY HACK
4792 *
4793 * The block layer suspend/resume path is fundamentally broken due
4794 * to freezable kthreads and workqueue and may deadlock if a block
4795 * device gets removed while resume is in progress. I don't know
4796 * what the solution is short of removing freezable kthreads and
4797 * workqueues altogether.
4798 *
4799 * The following is an ugly hack to avoid kicking off device
4800 * removal while freezer is active. This is a joke but does avoid
4801 * this particular deadlock scenario.
4802 *
4803 * https://bugzilla.kernel.org/show_bug.cgi?id=62801
4804 * http://marc.info/?l=linux-kernel&m=138695698516487
4805 */
4806#ifdef CONFIG_FREEZER
4807 while (pm_freezing)
4808 msleep(10);
4809#endif
4810
Tejun Heo580b21022006-05-31 18:28:05 +09004811 DPRINTK("ENTER\n");
Tejun Heoad72cf92010-07-02 10:03:52 +02004812 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004813
Tejun Heo41bda9c2007-08-06 18:36:24 +09004814 /* Unplug detached devices. We cannot use link iterator here
4815 * because PMP links have to be scanned even if PMP is
4816 * currently not attached. Iterate manually.
4817 */
4818 ata_scsi_handle_link_detach(&ap->link);
4819 if (ap->pmp_link)
4820 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
4821 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
Tejun Heo580b21022006-05-31 18:28:05 +09004822
4823 /* scan for new ones */
Tejun Heo1ae46312007-07-16 14:29:40 +09004824 ata_scsi_scan_host(ap, 0);
Tejun Heo580b21022006-05-31 18:28:05 +09004825
Tejun Heoad72cf92010-07-02 10:03:52 +02004826 mutex_unlock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004827 DPRINTK("EXIT\n");
4828}
Tejun Heo83c47bc2006-05-31 18:28:07 +09004829
4830/**
4831 * ata_scsi_user_scan - indication for user-initiated bus scan
4832 * @shost: SCSI host to scan
4833 * @channel: Channel to scan
4834 * @id: ID to scan
4835 * @lun: LUN to scan
4836 *
4837 * This function is called when user explicitly requests bus
4838 * scan. Set probe pending flag and invoke EH.
4839 *
4840 * LOCKING:
4841 * SCSI layer (we don't care)
4842 *
4843 * RETURNS:
4844 * Zero.
4845 */
Gwendal Grignoud9027472010-05-25 12:31:38 -07004846int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004847 unsigned int id, u64 lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004848{
4849 struct ata_port *ap = ata_shost_to_port(shost);
4850 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004851 int devno, rc = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004852
4853 if (!ap->ops->error_handler)
4854 return -EOPNOTSUPP;
4855
Tejun Heo41bda9c2007-08-06 18:36:24 +09004856 if (lun != SCAN_WILD_CARD && lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004857 return -EINVAL;
4858
Tejun Heo071f44b2008-04-07 22:47:22 +09004859 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004860 if (channel != SCAN_WILD_CARD && channel)
4861 return -EINVAL;
4862 devno = id;
4863 } else {
4864 if (id != SCAN_WILD_CARD && id)
4865 return -EINVAL;
4866 devno = channel;
4867 }
4868
Jeff Garzikba6a1302006-06-22 23:46:10 -04004869 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004870
Tejun Heo41bda9c2007-08-06 18:36:24 +09004871 if (devno == SCAN_WILD_CARD) {
4872 struct ata_link *link;
4873
Tejun Heo1eca4362008-11-03 20:03:17 +09004874 ata_for_each_link(link, ap, EDGE) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004875 struct ata_eh_info *ehi = &link->eh_info;
Tejun Heob558edd2008-01-24 00:05:14 +09004876 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09004877 ehi->action |= ATA_EH_RESET;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004878 }
Tejun Heo83c47bc2006-05-31 18:28:07 +09004879 } else {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004880 struct ata_device *dev = ata_find_dev(ap, devno);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004881
4882 if (dev) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004883 struct ata_eh_info *ehi = &dev->link->eh_info;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004884 ehi->probe_mask |= 1 << dev->devno;
Tejun Heocf480622008-01-24 00:05:14 +09004885 ehi->action |= ATA_EH_RESET;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004886 } else
4887 rc = -EINVAL;
4888 }
4889
Tejun Heo309afcb2006-09-30 18:07:17 +09004890 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09004891 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09004892 spin_unlock_irqrestore(ap->lock, flags);
4893 ata_port_wait_eh(ap);
4894 } else
4895 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004896
4897 return rc;
4898}
zhao, forrest3057ac32006-06-12 12:01:34 +08004899
4900/**
Tejun Heod0171262006-06-12 22:51:14 +09004901 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00004902 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08004903 *
Tejun Heod0171262006-06-12 22:51:14 +09004904 * After ATA pass thru (SAT) commands are executed successfully,
Tejun Heoad72cf92010-07-02 10:03:52 +02004905 * libata need to propagate the changes to SCSI layer.
zhao, forrest3057ac32006-06-12 12:01:34 +08004906 *
Tejun Heod0171262006-06-12 22:51:14 +09004907 * LOCKING:
4908 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08004909 */
David Howells65f27f32006-11-22 14:55:48 +00004910void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08004911{
David Howells65f27f32006-11-22 14:55:48 +00004912 struct ata_port *ap =
4913 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004914 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09004915 struct ata_device *dev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004916 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08004917
Tejun Heoad72cf92010-07-02 10:03:52 +02004918 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heof84e7e42006-11-22 11:21:31 +09004919 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08004920
Tejun Heo1eca4362008-11-03 20:03:17 +09004921 ata_for_each_link(link, ap, EDGE) {
4922 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004923 struct scsi_device *sdev = dev->sdev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004924
Tejun Heo1eca4362008-11-03 20:03:17 +09004925 if (!sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004926 continue;
4927 if (scsi_device_get(sdev))
4928 continue;
Tejun Heof84e7e42006-11-22 11:21:31 +09004929
Tejun Heo41bda9c2007-08-06 18:36:24 +09004930 spin_unlock_irqrestore(ap->lock, flags);
4931 scsi_rescan_device(&(sdev->sdev_gendev));
4932 scsi_device_put(sdev);
4933 spin_lock_irqsave(ap->lock, flags);
4934 }
zhao, forrest3057ac32006-06-12 12:01:34 +08004935 }
Tejun Heof84e7e42006-11-22 11:21:31 +09004936
4937 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad72cf92010-07-02 10:03:52 +02004938 mutex_unlock(&ap->scsi_scan_mutex);
zhao, forrest3057ac32006-06-12 12:01:34 +08004939}
Brian King80289162006-08-07 14:27:31 -05004940
4941/**
4942 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04004943 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05004944 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04004945 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05004946 *
4947 * LOCKING:
4948 * PCI/etc. bus probe sem.
4949 *
4950 * RETURNS:
4951 * ata_port pointer on success / NULL on failure.
4952 */
4953
Jeff Garzikcca39742006-08-24 03:19:22 -04004954struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05004955 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04004956 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05004957{
Tejun Heof3187192007-04-17 23:44:07 +09004958 struct ata_port *ap;
Brian King80289162006-08-07 14:27:31 -05004959
Tejun Heof3187192007-04-17 23:44:07 +09004960 ap = ata_port_alloc(host);
Brian King80289162006-08-07 14:27:31 -05004961 if (!ap)
4962 return NULL;
4963
Tejun Heof3187192007-04-17 23:44:07 +09004964 ap->port_no = 0;
James Bottomleya29b5da2011-01-23 08:30:00 -06004965 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09004966 ap->pio_mask = port_info->pio_mask;
4967 ap->mwdma_mask = port_info->mwdma_mask;
4968 ap->udma_mask = port_info->udma_mask;
4969 ap->flags |= port_info->flags;
4970 ap->ops = port_info->port_ops;
4971 ap->cbl = ATA_CBL_SATA;
4972
Brian King80289162006-08-07 14:27:31 -05004973 return ap;
4974}
4975EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
4976
4977/**
4978 * ata_sas_port_start - Set port up for dma.
4979 * @ap: Port to initialize
4980 *
4981 * Called just after data structures for each port are
James Bottomleydde20202008-02-19 11:36:56 +01004982 * initialized.
Brian King80289162006-08-07 14:27:31 -05004983 *
4984 * May be used as the port_start() entry in ata_port_operations.
4985 *
4986 * LOCKING:
4987 * Inherited from caller.
4988 */
4989int ata_sas_port_start(struct ata_port *ap)
4990{
Nishanth Aravamudan3f1e0462011-06-16 08:28:36 -07004991 /*
4992 * the port is marked as frozen at allocation time, but if we don't
4993 * have new eh, we won't thaw it
4994 */
4995 if (!ap->ops->error_handler)
4996 ap->pflags &= ~ATA_PFLAG_FROZEN;
James Bottomleydde20202008-02-19 11:36:56 +01004997 return 0;
Brian King80289162006-08-07 14:27:31 -05004998}
4999EXPORT_SYMBOL_GPL(ata_sas_port_start);
5000
5001/**
5002 * ata_port_stop - Undo ata_sas_port_start()
5003 * @ap: Port to shut down
5004 *
Brian King80289162006-08-07 14:27:31 -05005005 * May be used as the port_stop() entry in ata_port_operations.
5006 *
5007 * LOCKING:
5008 * Inherited from caller.
5009 */
5010
5011void ata_sas_port_stop(struct ata_port *ap)
5012{
Brian King80289162006-08-07 14:27:31 -05005013}
5014EXPORT_SYMBOL_GPL(ata_sas_port_stop);
5015
Dan Williamsb2024452012-03-21 21:09:07 -07005016/**
5017 * ata_sas_async_probe - simply schedule probing and return
5018 * @ap: Port to probe
5019 *
5020 * For batch scheduling of probe for sas attached ata devices, assumes
5021 * the port has already been through ata_sas_port_init()
5022 */
5023void ata_sas_async_probe(struct ata_port *ap)
Dan Williams9508a662012-01-18 20:47:01 -08005024{
Dan Williamsb2024452012-03-21 21:09:07 -07005025 __ata_port_probe(ap);
Dan Williams9508a662012-01-18 20:47:01 -08005026}
Dan Williamsb2024452012-03-21 21:09:07 -07005027EXPORT_SYMBOL_GPL(ata_sas_async_probe);
5028
5029int ata_sas_sync_probe(struct ata_port *ap)
5030{
5031 return ata_port_probe(ap);
5032}
5033EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
5034
Dan Williams9508a662012-01-18 20:47:01 -08005035
Brian King80289162006-08-07 14:27:31 -05005036/**
5037 * ata_sas_port_init - Initialize a SATA device
5038 * @ap: SATA port to initialize
5039 *
5040 * LOCKING:
5041 * PCI/etc. bus probe sem.
5042 *
5043 * RETURNS:
5044 * Zero on success, non-zero on error.
5045 */
5046
5047int ata_sas_port_init(struct ata_port *ap)
5048{
5049 int rc = ap->ops->port_start(ap);
5050
Dan Williamsb2024452012-03-21 21:09:07 -07005051 if (rc)
5052 return rc;
5053 ap->print_id = atomic_inc_return(&ata_print_id);
5054 return 0;
Brian King80289162006-08-07 14:27:31 -05005055}
5056EXPORT_SYMBOL_GPL(ata_sas_port_init);
5057
Jason Yanb6240a42018-03-26 17:27:41 +08005058int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
5059{
5060 return ata_tport_add(parent, ap);
5061}
5062EXPORT_SYMBOL_GPL(ata_sas_tport_add);
5063
5064void ata_sas_tport_delete(struct ata_port *ap)
5065{
5066 ata_tport_delete(ap);
5067}
5068EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
5069
Brian King80289162006-08-07 14:27:31 -05005070/**
5071 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
5072 * @ap: SATA port to destroy
5073 *
5074 */
5075
5076void ata_sas_port_destroy(struct ata_port *ap)
5077{
Tejun Heof0d36ef2007-01-20 16:00:28 +09005078 if (ap->ops->port_stop)
5079 ap->ops->port_stop(ap);
Brian King80289162006-08-07 14:27:31 -05005080 kfree(ap);
5081}
5082EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
5083
5084/**
5085 * ata_sas_slave_configure - Default slave_config routine for libata devices
5086 * @sdev: SCSI device to configure
5087 * @ap: ATA port to which SCSI device is attached
5088 *
5089 * RETURNS:
5090 * Zero.
5091 */
5092
5093int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
5094{
5095 ata_scsi_sdev_config(sdev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005096 ata_scsi_dev_config(sdev, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05005097 return 0;
5098}
5099EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
5100
5101/**
5102 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
5103 * @cmd: SCSI command to be sent
Brian King80289162006-08-07 14:27:31 -05005104 * @ap: ATA port to which the command is being sent
5105 *
5106 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06005107 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
5108 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05005109 */
5110
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005111int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
Brian King80289162006-08-07 14:27:31 -05005112{
Brian King08475a12006-11-20 13:51:56 -06005113 int rc = 0;
5114
Brian King80289162006-08-07 14:27:31 -05005115 ata_scsi_dump_cdb(ap, cmd);
5116
Tejun Heo2486fa52008-07-31 07:52:40 +09005117 if (likely(ata_dev_enabled(ap->link.device)))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005118 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05005119 else {
5120 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005121 cmd->scsi_done(cmd);
Brian King80289162006-08-07 14:27:31 -05005122 }
Brian King08475a12006-11-20 13:51:56 -06005123 return rc;
Brian King80289162006-08-07 14:27:31 -05005124}
5125EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
Shaohua Li98bd4be2015-01-23 19:52:07 -08005126
5127int ata_sas_allocate_tag(struct ata_port *ap)
5128{
5129 unsigned int max_queue = ap->host->n_tags;
5130 unsigned int i, tag;
5131
5132 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
Tejun Heo3a028242015-03-24 14:14:18 -04005133 tag = tag < max_queue ? tag : 0;
Shaohua Li98bd4be2015-01-23 19:52:07 -08005134
5135 /* the last tag is reserved for internal command. */
Jens Axboe2e2cc672018-05-11 12:51:06 -06005136 if (ata_tag_internal(tag))
Shaohua Li98bd4be2015-01-23 19:52:07 -08005137 continue;
5138
5139 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
5140 ap->sas_last_tag = tag;
5141 return tag;
5142 }
5143 }
5144 return -1;
5145}
5146
5147void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
5148{
5149 clear_bit(tag, &ap->sas_tag_allocated);
5150}