blob: 43694f5f2c89c5f32c883f73b54809a7a9f2e8a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-scsi.c - helper library for ATA
3 *
Tejun Heo8c3d3d42013-05-14 11:09:50 -07004 * Maintained by: Tejun Heo <tj@kernel.org>
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04005 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from
31 * - http://www.t10.org/
32 * - http://www.t13.org/
33 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/kernel.h>
38#include <linux/blkdev.h>
39#include <linux/spinlock.h>
Paul Gortmaker38789fd2011-07-17 15:33:58 -040040#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <scsi/scsi_host.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020043#include <scsi/scsi_cmnd.h>
Mike Christie85837eb2005-11-11 16:38:53 -060044#include <scsi/scsi_eh.h>
Jeff Garzik005a5a02005-10-30 23:31:48 -050045#include <scsi/scsi_device.h>
Tejun Heoa6e6ce82006-05-15 21:03:48 +090046#include <scsi/scsi_tcq.h>
Tejun Heo30afc842006-03-18 18:40:14 +090047#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/libata.h>
Jeff Garzikb0955182005-05-12 15:45:22 -040049#include <linux/hdreg.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040050#include <linux/uaccess.h>
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +010051#include <linux/suspend.h>
Christoph Hellwig18f0f972009-11-17 10:00:47 -050052#include <asm/unaligned.h>
Adam Manzanares8e061782016-10-17 11:27:29 -070053#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include "libata.h"
Gwendal Grignoud9027472010-05-25 12:31:38 -070056#include "libata-transport.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Tejun Heo87340e92008-04-28 17:48:51 +090058#define ATA_SCSI_RBUF_SIZE 4096
59
60static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
61static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
Jeff Garzikb0955182005-05-12 15:45:22 -040062
Tejun Heoad706992006-12-17 10:45:57 +090063typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
Tejun Heoab5b3a52006-05-31 18:27:34 +090064
Jeff Garzik2dcb4072007-10-19 06:42:56 -040065static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090066 const struct scsi_device *scsidev);
Jeff Garzik2dcb4072007-10-19 06:42:56 -040067static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090068 const struct scsi_device *scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040070#define RW_RECOVERY_MPAGE 0x1
71#define RW_RECOVERY_MPAGE_LEN 12
72#define CACHE_MPAGE 0x8
73#define CACHE_MPAGE_LEN 20
74#define CONTROL_MPAGE 0xa
75#define CONTROL_MPAGE_LEN 12
76#define ALL_MPAGES 0x3f
77#define ALL_SUB_MPAGES 0xff
78
79
Jeff Garzik24f75682007-09-21 07:54:49 -040080static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040081 RW_RECOVERY_MPAGE,
82 RW_RECOVERY_MPAGE_LEN - 2,
Jeff Garzik24f75682007-09-21 07:54:49 -040083 (1 << 7), /* AWRE */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040084 0, /* read retry count */
85 0, 0, 0, 0,
86 0, /* write retry count */
87 0, 0, 0
88};
89
90static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
91 CACHE_MPAGE,
92 CACHE_MPAGE_LEN - 2,
93 0, /* contains WCE, needs to be 0 for logic */
94 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, /* contains DRA, needs to be 0 for logic */
96 0, 0, 0, 0, 0, 0, 0
97};
98
99static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
100 CONTROL_MPAGE,
101 CONTROL_MPAGE_LEN - 2,
102 2, /* DSENSE=0, GLTSD=1 */
103 0, /* [QAM+QERR may be 1, see 05-359r1] */
104 0, 0, 0, 0, 0xff, 0xff,
105 0, 30 /* extended self test time, see 05-359r1 */
106};
107
Tejun Heoc93b2632010-09-01 17:50:04 +0200108static const char *ata_lpm_policy_names[] = {
109 [ATA_LPM_UNKNOWN] = "max_performance",
110 [ATA_LPM_MAX_POWER] = "max_performance",
111 [ATA_LPM_MED_POWER] = "medium_power",
112 [ATA_LPM_MIN_POWER] = "min_power",
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400113};
114
Tejun Heoecd75ad2014-01-16 09:47:17 -0500115static ssize_t ata_scsi_lpm_store(struct device *device,
Tejun Heoc93b2632010-09-01 17:50:04 +0200116 struct device_attribute *attr,
117 const char *buf, size_t count)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400118{
Tejun Heoecd75ad2014-01-16 09:47:17 -0500119 struct Scsi_Host *shost = class_to_shost(device);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400120 struct ata_port *ap = ata_shost_to_port(shost);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500121 struct ata_link *link;
122 struct ata_device *dev;
Tejun Heoc93b2632010-09-01 17:50:04 +0200123 enum ata_lpm_policy policy;
Tejun Heo6b7ae952010-09-01 17:50:06 +0200124 unsigned long flags;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400125
Tejun Heoc93b2632010-09-01 17:50:04 +0200126 /* UNKNOWN is internal state, iterate from MAX_POWER */
127 for (policy = ATA_LPM_MAX_POWER;
128 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
129 const char *name = ata_lpm_policy_names[policy];
130
131 if (strncmp(name, buf, strlen(name)) == 0)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400132 break;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400133 }
Tejun Heoc93b2632010-09-01 17:50:04 +0200134 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400135 return -EINVAL;
136
Tejun Heo6b7ae952010-09-01 17:50:06 +0200137 spin_lock_irqsave(ap->lock, flags);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500138
139 ata_for_each_link(link, ap, EDGE) {
140 ata_for_each_dev(dev, &ap->link, ENABLED) {
141 if (dev->horkage & ATA_HORKAGE_NOLPM) {
142 count = -EOPNOTSUPP;
143 goto out_unlock;
144 }
145 }
146 }
147
Tejun Heo6b7ae952010-09-01 17:50:06 +0200148 ap->target_lpm_policy = policy;
149 ata_port_schedule_eh(ap);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500150out_unlock:
Tejun Heo6b7ae952010-09-01 17:50:06 +0200151 spin_unlock_irqrestore(ap->lock, flags);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400152 return count;
153}
154
Tejun Heoc93b2632010-09-01 17:50:04 +0200155static ssize_t ata_scsi_lpm_show(struct device *dev,
156 struct device_attribute *attr, char *buf)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400157{
Tony Jonesee959b02008-02-22 00:13:36 +0100158 struct Scsi_Host *shost = class_to_shost(dev);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400159 struct ata_port *ap = ata_shost_to_port(shost);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400160
Tejun Heo6b7ae952010-09-01 17:50:06 +0200161 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400162 return -EINVAL;
163
Tejun Heoc93b2632010-09-01 17:50:04 +0200164 return snprintf(buf, PAGE_SIZE, "%s\n",
Tejun Heo6b7ae952010-09-01 17:50:06 +0200165 ata_lpm_policy_names[ap->target_lpm_policy]);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400166}
Tony Jonesee959b02008-02-22 00:13:36 +0100167DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
Tejun Heoc93b2632010-09-01 17:50:04 +0200168 ata_scsi_lpm_show, ata_scsi_lpm_store);
Tony Jonesee959b02008-02-22 00:13:36 +0100169EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400170
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200171static ssize_t ata_scsi_park_show(struct device *device,
172 struct device_attribute *attr, char *buf)
173{
174 struct scsi_device *sdev = to_scsi_device(device);
175 struct ata_port *ap;
176 struct ata_link *link;
177 struct ata_device *dev;
Tejun Heo3948b6f2016-02-18 11:50:37 -0500178 unsigned long now;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200179 unsigned int uninitialized_var(msecs);
180 int rc = 0;
181
182 ap = ata_shost_to_port(sdev->host);
183
Tejun Heo3948b6f2016-02-18 11:50:37 -0500184 spin_lock_irq(ap->lock);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200185 dev = ata_scsi_find_dev(ap, sdev);
186 if (!dev) {
187 rc = -ENODEV;
188 goto unlock;
189 }
190 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
191 rc = -EOPNOTSUPP;
192 goto unlock;
193 }
194
195 link = dev->link;
Elias Oltmannsa4641892008-11-03 19:01:08 +0900196 now = jiffies;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200197 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
198 link->eh_context.unloaded_mask & (1 << dev->devno) &&
Elias Oltmannsa4641892008-11-03 19:01:08 +0900199 time_after(dev->unpark_deadline, now))
200 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200201 else
202 msecs = 0;
203
204unlock:
205 spin_unlock_irq(ap->lock);
206
207 return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
208}
209
210static ssize_t ata_scsi_park_store(struct device *device,
211 struct device_attribute *attr,
212 const char *buf, size_t len)
213{
214 struct scsi_device *sdev = to_scsi_device(device);
215 struct ata_port *ap;
216 struct ata_device *dev;
217 long int input;
218 unsigned long flags;
219 int rc;
220
Jingoo Han42b9ab72013-07-19 15:56:41 +0900221 rc = kstrtol(buf, 10, &input);
222 if (rc)
223 return rc;
224 if (input < -2)
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200225 return -EINVAL;
226 if (input > ATA_TMOUT_MAX_PARK) {
227 rc = -EOVERFLOW;
228 input = ATA_TMOUT_MAX_PARK;
229 }
230
231 ap = ata_shost_to_port(sdev->host);
232
233 spin_lock_irqsave(ap->lock, flags);
234 dev = ata_scsi_find_dev(ap, sdev);
235 if (unlikely(!dev)) {
236 rc = -ENODEV;
237 goto unlock;
238 }
Hannes Reinecke9162c652014-11-05 13:08:21 +0100239 if (dev->class != ATA_DEV_ATA &&
240 dev->class != ATA_DEV_ZAC) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200241 rc = -EOPNOTSUPP;
242 goto unlock;
243 }
244
245 if (input >= 0) {
246 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
247 rc = -EOPNOTSUPP;
248 goto unlock;
249 }
250
251 dev->unpark_deadline = ata_deadline(jiffies, input);
252 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
253 ata_port_schedule_eh(ap);
254 complete(&ap->park_req_pending);
255 } else {
256 switch (input) {
257 case -1:
258 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
259 break;
260 case -2:
261 dev->flags |= ATA_DFLAG_NO_UNLOAD;
262 break;
263 }
264 }
265unlock:
266 spin_unlock_irqrestore(ap->lock, flags);
267
268 return rc ? rc : len;
269}
270DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
271 ata_scsi_park_show, ata_scsi_park_store);
272EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
273
Adam Manzanares84f95242016-10-17 11:27:30 -0700274static ssize_t ata_ncq_prio_enable_show(struct device *device,
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800275 struct device_attribute *attr,
276 char *buf)
Adam Manzanares84f95242016-10-17 11:27:30 -0700277{
278 struct scsi_device *sdev = to_scsi_device(device);
279 struct ata_port *ap;
280 struct ata_device *dev;
281 bool ncq_prio_enable;
282 int rc = 0;
283
284 ap = ata_shost_to_port(sdev->host);
285
286 spin_lock_irq(ap->lock);
287 dev = ata_scsi_find_dev(ap, sdev);
288 if (!dev) {
289 rc = -ENODEV;
290 goto unlock;
291 }
292
293 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
294
295unlock:
296 spin_unlock_irq(ap->lock);
297
298 return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable);
299}
300
301static ssize_t ata_ncq_prio_enable_store(struct device *device,
302 struct device_attribute *attr,
303 const char *buf, size_t len)
304{
305 struct scsi_device *sdev = to_scsi_device(device);
306 struct ata_port *ap;
307 struct ata_device *dev;
308 long int input;
Adam Manzanares84f95242016-10-17 11:27:30 -0700309 int rc;
310
311 rc = kstrtol(buf, 10, &input);
312 if (rc)
313 return rc;
314 if ((input < 0) || (input > 1))
315 return -EINVAL;
316
317 ap = ata_shost_to_port(sdev->host);
Adam Manzanares84f95242016-10-17 11:27:30 -0700318 dev = ata_scsi_find_dev(ap, sdev);
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800319 if (unlikely(!dev))
320 return -ENODEV;
321
322 spin_lock_irq(ap->lock);
323 if (input)
324 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
325 else
326 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
327
328 dev->link->eh_info.action |= ATA_EH_REVALIDATE;
329 dev->link->eh_info.flags |= ATA_EHI_QUIET;
330 ata_port_schedule_eh(ap);
331 spin_unlock_irq(ap->lock);
332
333 ata_port_wait_eh(ap);
Adam Manzanares84f95242016-10-17 11:27:30 -0700334
Adam Manzanares4e647d92016-10-19 20:40:19 -0700335 if (input) {
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800336 spin_lock_irq(ap->lock);
Adam Manzanares4e647d92016-10-19 20:40:19 -0700337 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800338 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
339 rc = -EIO;
Adam Manzanares4e647d92016-10-19 20:40:19 -0700340 }
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800341 spin_unlock_irq(ap->lock);
Adam Manzanares4e647d92016-10-19 20:40:19 -0700342 }
Adam Manzanares84f95242016-10-17 11:27:30 -0700343
Adam Manzanares84f95242016-10-17 11:27:30 -0700344 return rc ? rc : len;
345}
346
347DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
348 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
349EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
350
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200351void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
352 u8 sk, u8 asc, u8 ascq)
Tejun Heof0761be2008-04-28 17:16:52 +0900353{
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200354 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
355
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +0200356 if (!cmd)
357 return;
358
Tejun Heof0761be2008-04-28 17:16:52 +0900359 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
360
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200361 scsi_build_sense_buffer(d_sense, cmd->sense_buffer, sk, asc, ascq);
Tejun Heof0761be2008-04-28 17:16:52 +0900362}
363
Hannes Reinecke492bf622016-04-04 11:43:59 +0200364void ata_scsi_set_sense_information(struct ata_device *dev,
365 struct scsi_cmnd *cmd,
366 const struct ata_taskfile *tf)
367{
368 u64 information;
369
370 if (!cmd)
371 return;
372
373 information = ata_tf_read_block(tf, dev);
374 if (information == U64_MAX)
375 return;
376
377 scsi_set_sense_information(cmd->sense_buffer,
378 SCSI_SENSE_BUFFERSIZE, information);
379}
380
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200381static void ata_scsi_set_invalid_field(struct ata_device *dev,
Hannes Reinecke0df10b82016-04-04 11:44:06 +0200382 struct scsi_cmnd *cmd, u16 field, u8 bit)
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200383{
384 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
Tom Yan8554e5e2016-07-07 01:13:09 +0800385 /* "Invalid field in CDB" */
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200386 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
Hannes Reinecke0df10b82016-04-04 11:44:06 +0200387 field, bit, 1);
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200388}
389
Hannes Reinecke77800812016-04-04 11:44:07 +0200390static void ata_scsi_set_invalid_parameter(struct ata_device *dev,
391 struct scsi_cmnd *cmd, u16 field)
392{
393 /* "Invalid field in parameter list" */
394 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x26, 0x0);
395 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
396 field, 0xff, 0);
Tejun Heo5924b742007-01-02 20:20:07 +0900397}
398
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700399static ssize_t
400ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
401 const char *buf, size_t count)
402{
403 struct Scsi_Host *shost = class_to_shost(dev);
404 struct ata_port *ap = ata_shost_to_port(shost);
405 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
406 return ap->ops->em_store(ap, buf, count);
407 return -EINVAL;
408}
409
410static ssize_t
411ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
412 char *buf)
413{
414 struct Scsi_Host *shost = class_to_shost(dev);
415 struct ata_port *ap = ata_shost_to_port(shost);
416
417 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
418 return ap->ops->em_show(ap, buf);
419 return -EINVAL;
420}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200421DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700422 ata_scsi_em_message_show, ata_scsi_em_message_store);
423EXPORT_SYMBOL_GPL(dev_attr_em_message);
424
425static ssize_t
426ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
427 char *buf)
428{
429 struct Scsi_Host *shost = class_to_shost(dev);
430 struct ata_port *ap = ata_shost_to_port(shost);
431
432 return snprintf(buf, 23, "%d\n", ap->em_message_type);
433}
434DEVICE_ATTR(em_message_type, S_IRUGO,
435 ata_scsi_em_message_type_show, NULL);
436EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
437
438static ssize_t
439ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
440 char *buf)
441{
442 struct scsi_device *sdev = to_scsi_device(dev);
443 struct ata_port *ap = ata_shost_to_port(sdev->host);
444 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
445
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800446 if (atadev && ap->ops->sw_activity_show &&
447 (ap->flags & ATA_FLAG_SW_ACTIVITY))
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700448 return ap->ops->sw_activity_show(atadev, buf);
449 return -EINVAL;
450}
451
452static ssize_t
453ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
454 const char *buf, size_t count)
455{
456 struct scsi_device *sdev = to_scsi_device(dev);
457 struct ata_port *ap = ata_shost_to_port(sdev->host);
458 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
459 enum sw_activity val;
460 int rc;
461
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800462 if (atadev && ap->ops->sw_activity_store &&
463 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700464 val = simple_strtoul(buf, NULL, 0);
465 switch (val) {
466 case OFF: case BLINK_ON: case BLINK_OFF:
467 rc = ap->ops->sw_activity_store(atadev, val);
468 if (!rc)
469 return count;
470 else
471 return rc;
472 }
473 }
474 return -EINVAL;
475}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200476DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700477 ata_scsi_activity_store);
478EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
479
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200480struct device_attribute *ata_common_sdev_attrs[] = {
481 &dev_attr_unload_heads,
Adam Manzanares84f95242016-10-17 11:27:30 -0700482 &dev_attr_ncq_prio_enable,
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200483 NULL
484};
485EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
486
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200487static void ata_scsi_invalid_field(struct ata_device *dev,
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200488 struct scsi_cmnd *cmd, u16 field)
Douglas Gilbertae006512005-10-09 09:09:35 -0400489{
Hannes Reinecke0df10b82016-04-04 11:44:06 +0200490 ata_scsi_set_invalid_field(dev, cmd, field, 0xff);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500491 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -0400492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/**
495 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
496 * @sdev: SCSI device for which BIOS geometry is to be determined
497 * @bdev: block device associated with @sdev
498 * @capacity: capacity of SCSI device
499 * @geom: location to which geometry will be output
500 *
501 * Generic bios head/sector/cylinder calculator
502 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
503 * mapping. Some situations may arise where the disk is not
504 * bootable if this is not used.
505 *
506 * LOCKING:
507 * Defined by the SCSI layer. We don't really care.
508 *
509 * RETURNS:
510 * Zero.
511 */
512int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
513 sector_t capacity, int geom[])
514{
515 geom[0] = 255;
516 geom[1] = 63;
517 sector_div(capacity, 255*63);
518 geom[2] = capacity;
519
520 return 0;
521}
522
Jeff Garzikb0955182005-05-12 15:45:22 -0400523/**
Tejun Heod8d91292010-05-15 20:09:34 +0200524 * ata_scsi_unlock_native_capacity - unlock native capacity
525 * @sdev: SCSI device to adjust device capacity for
526 *
527 * This function is called if a partition on @sdev extends beyond
528 * the end of the device. It requests EH to unlock HPA.
529 *
530 * LOCKING:
531 * Defined by the SCSI layer. Might sleep.
532 */
533void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
534{
535 struct ata_port *ap = ata_shost_to_port(sdev->host);
536 struct ata_device *dev;
537 unsigned long flags;
538
539 spin_lock_irqsave(ap->lock, flags);
540
541 dev = ata_scsi_find_dev(ap, sdev);
542 if (dev && dev->n_sectors < dev->n_native_sectors) {
543 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
544 dev->link->eh_info.action |= ATA_EH_RESET;
545 ata_port_schedule_eh(ap);
546 }
547
548 spin_unlock_irqrestore(ap->lock, flags);
549 ata_port_wait_eh(ap);
550}
551
552/**
Tejun Heo5924b742007-01-02 20:20:07 +0900553 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
Randy Dunlap5eb66fe2009-01-20 16:28:59 -0800554 * @ap: target port
Tejun Heo5924b742007-01-02 20:20:07 +0900555 * @sdev: SCSI device to get identify data for
556 * @arg: User buffer area for identify data
557 *
558 * LOCKING:
559 * Defined by the SCSI layer. We don't really care.
560 *
561 * RETURNS:
562 * Zero on success, negative errno on error.
563 */
Jeff Garzik94be9a52009-01-16 10:17:09 -0500564static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
565 void __user *arg)
Tejun Heo5924b742007-01-02 20:20:07 +0900566{
Tejun Heo5924b742007-01-02 20:20:07 +0900567 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
568 u16 __user *dst = arg;
569 char buf[40];
570
571 if (!dev)
572 return -ENOMSG;
573
574 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
575 return -EFAULT;
576
577 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
578 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
579 return -EFAULT;
580
581 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
582 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
583 return -EFAULT;
584
585 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
586 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
587 return -EFAULT;
588
589 return 0;
590}
591
592/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400593 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800594 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400595 * @arg: User provided data for issuing command
596 *
597 * LOCKING:
598 * Defined by the SCSI layer. We don't really care.
599 *
600 * RETURNS:
601 * Zero on success, negative errno on error.
602 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400603int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
604{
605 int rc = 0;
606 u8 scsi_cmd[MAX_COMMAND_SIZE];
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700607 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400608 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600609 enum dma_data_direction data_dir;
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700610 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400611
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500612 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400613 return -EINVAL;
614
615 if (copy_from_user(args, arg, sizeof(args)))
616 return -EFAULT;
617
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700618 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
619 if (!sensebuf)
620 return -ENOMEM;
621
Jeff Garzikb0955182005-05-12 15:45:22 -0400622 memset(scsi_cmd, 0, sizeof(scsi_cmd));
623
624 if (args[3]) {
Grant Grundler295124d2010-08-17 10:56:53 -0700625 argsize = ATA_SECT_SIZE * args[3];
Jeff Garzikb0955182005-05-12 15:45:22 -0400626 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400627 if (argbuf == NULL) {
628 rc = -ENOMEM;
629 goto error;
630 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400631
632 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
633 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400634 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600635 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400636 } else {
637 scsi_cmd[1] = (3 << 1); /* Non-data */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700638 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600639 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400640 }
641
642 scsi_cmd[0] = ATA_16;
643
644 scsi_cmd[4] = args[2];
Bartlomiej Zolnierkiewicza4f19042008-10-10 22:39:20 +0200645 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
Jeff Garzikb0955182005-05-12 15:45:22 -0400646 scsi_cmd[6] = args[3];
647 scsi_cmd[8] = args[1];
648 scsi_cmd[10] = 0x4f;
649 scsi_cmd[12] = 0xc2;
650 } else {
651 scsi_cmd[6] = args[1];
652 }
653 scsi_cmd[14] = args[0];
654
655 /* Good values for timeout and retries? Values below
656 from scsi_ioctl_send_command() for default case... */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700657 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900658 sensebuf, (10*HZ), 5, 0, NULL);
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700659
660 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
661 u8 *desc = sensebuf + 8;
662 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
663
664 /* If we set cc then ATA pass-through will cause a
665 * check condition even if no error. Filter that. */
666 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
667 struct scsi_sense_hdr sshdr;
668 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400669 &sshdr);
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100670 if (sshdr.sense_key == RECOVERED_ERROR &&
671 sshdr.asc == 0 && sshdr.ascq == 0x1d)
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700672 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
673 }
674
675 /* Send userspace a few ATA registers (same as drivers/ide) */
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400676 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
677 desc[0] == 0x09) { /* code is "ATA Descriptor" */
678 args[0] = desc[13]; /* status */
679 args[1] = desc[3]; /* error */
680 args[2] = desc[5]; /* sector count (0:7) */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700681 if (copy_to_user(arg, args, sizeof(args)))
682 rc = -EFAULT;
683 }
684 }
685
686
687 if (cmd_result) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400688 rc = -EIO;
689 goto error;
690 }
691
Jeff Garzikb0955182005-05-12 15:45:22 -0400692 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500693 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400694 rc = -EFAULT;
695error:
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700696 kfree(sensebuf);
Jesper Juhl8f760782006-06-27 02:55:06 -0700697 kfree(argbuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400698 return rc;
699}
700
701/**
702 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800703 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400704 * @arg: User provided data for issuing command
705 *
706 * LOCKING:
707 * Defined by the SCSI layer. We don't really care.
708 *
709 * RETURNS:
710 * Zero on success, negative errno on error.
711 */
712int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
713{
714 int rc = 0;
715 u8 scsi_cmd[MAX_COMMAND_SIZE];
David Milburnaf068bd2007-01-30 00:59:15 -0800716 u8 args[7], *sensebuf = NULL;
717 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400718
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500719 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400720 return -EINVAL;
721
722 if (copy_from_user(args, arg, sizeof(args)))
723 return -EFAULT;
724
David Milburnaf068bd2007-01-30 00:59:15 -0800725 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
726 if (!sensebuf)
727 return -ENOMEM;
728
Jeff Garzikb0955182005-05-12 15:45:22 -0400729 memset(scsi_cmd, 0, sizeof(scsi_cmd));
730 scsi_cmd[0] = ATA_16;
731 scsi_cmd[1] = (3 << 1); /* Non-data */
David Milburnaf068bd2007-01-30 00:59:15 -0800732 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Jeff Garzikb0955182005-05-12 15:45:22 -0400733 scsi_cmd[4] = args[1];
734 scsi_cmd[6] = args[2];
735 scsi_cmd[8] = args[3];
736 scsi_cmd[10] = args[4];
737 scsi_cmd[12] = args[5];
Mark Lord277239f2007-03-30 17:45:52 -0400738 scsi_cmd[13] = args[6] & 0x4f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400739 scsi_cmd[14] = args[0];
740
Jeff Garzikb0955182005-05-12 15:45:22 -0400741 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500742 from scsi_ioctl_send_command() for default case... */
David Milburnaf068bd2007-01-30 00:59:15 -0800743 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900744 sensebuf, (10*HZ), 5, 0, NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400745
David Milburnaf068bd2007-01-30 00:59:15 -0800746 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
747 u8 *desc = sensebuf + 8;
748 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
749
750 /* If we set cc then ATA pass-through will cause a
751 * check condition even if no error. Filter that. */
752 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
753 struct scsi_sense_hdr sshdr;
754 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
755 &sshdr);
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100756 if (sshdr.sense_key == RECOVERED_ERROR &&
757 sshdr.asc == 0 && sshdr.ascq == 0x1d)
David Milburnaf068bd2007-01-30 00:59:15 -0800758 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
759 }
760
761 /* Send userspace ATA registers */
762 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
763 desc[0] == 0x09) {/* code is "ATA Descriptor" */
764 args[0] = desc[13]; /* status */
765 args[1] = desc[3]; /* error */
766 args[2] = desc[5]; /* sector count (0:7) */
767 args[3] = desc[7]; /* lbal */
768 args[4] = desc[9]; /* lbam */
769 args[5] = desc[11]; /* lbah */
770 args[6] = desc[12]; /* select */
771 if (copy_to_user(arg, args, sizeof(args)))
772 rc = -EFAULT;
773 }
774 }
775
776 if (cmd_result) {
777 rc = -EIO;
778 goto error;
779 }
780
781 error:
782 kfree(sensebuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400783 return rc;
784}
785
Alan Coxe3cf95d2009-04-09 17:31:17 +0100786static int ata_ioc32(struct ata_port *ap)
787{
788 if (ap->flags & ATA_FLAG_PIO_DMA)
789 return 1;
790 if (ap->pflags & ATA_PFLAG_PIO32)
791 return 1;
792 return 0;
793}
794
Jeff Garzik94be9a52009-01-16 10:17:09 -0500795int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
796 int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Arnd Bergmann287e6612016-02-11 14:16:27 +0100798 unsigned long val;
799 int rc = -EINVAL;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100800 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 switch (cmd) {
Arnd Bergmann287e6612016-02-11 14:16:27 +0100803 case HDIO_GET_32BIT:
Alan Coxe3cf95d2009-04-09 17:31:17 +0100804 spin_lock_irqsave(ap->lock, flags);
805 val = ata_ioc32(ap);
806 spin_unlock_irqrestore(ap->lock, flags);
Arnd Bergmann287e6612016-02-11 14:16:27 +0100807 return put_user(val, (unsigned long __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Arnd Bergmann287e6612016-02-11 14:16:27 +0100809 case HDIO_SET_32BIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 val = (unsigned long) arg;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100811 rc = 0;
812 spin_lock_irqsave(ap->lock, flags);
813 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
814 if (val)
815 ap->pflags |= ATA_PFLAG_PIO32;
816 else
817 ap->pflags &= ~ATA_PFLAG_PIO32;
818 } else {
819 if (val != ata_ioc32(ap))
820 rc = -EINVAL;
821 }
822 spin_unlock_irqrestore(ap->lock, flags);
823 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Tejun Heo5924b742007-01-02 20:20:07 +0900825 case HDIO_GET_IDENTITY:
Jeff Garzik94be9a52009-01-16 10:17:09 -0500826 return ata_get_identity(ap, scsidev, arg);
Tejun Heo5924b742007-01-02 20:20:07 +0900827
Jeff Garzikb0955182005-05-12 15:45:22 -0400828 case HDIO_DRIVE_CMD:
829 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
830 return -EACCES;
831 return ata_cmd_ioctl(scsidev, arg);
832
833 case HDIO_DRIVE_TASK:
834 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
835 return -EACCES;
836 return ata_task_ioctl(scsidev, arg);
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 default:
839 rc = -ENOTTY;
840 break;
841 }
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return rc;
844}
Jeff Garzik94be9a52009-01-16 10:17:09 -0500845EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
846
847int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
848{
849 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
850 scsidev, cmd, arg);
851}
852EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854/**
855 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * @dev: ATA device to which the new command is attached
857 * @cmd: SCSI command that originated this ATA command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 *
859 * Obtain a reference to an unused ata_queued_cmd structure,
860 * which is the basic libata structure representing a single
861 * ATA command sent to the hardware.
862 *
863 * If a command was available, fill in the SCSI-specific
864 * portions of the structure with information on the
865 * current command.
866 *
867 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400868 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 *
870 * RETURNS:
871 * Command allocated, or %NULL if none available.
872 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100873static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500874 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct ata_queued_cmd *qc;
877
Shaohua Li12cb5ce2015-01-15 17:32:27 -0800878 qc = ata_qc_new_init(dev, cmd->request->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (qc) {
880 qc->scsicmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500881 qc->scsidone = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Tejun Heoff2aeb12007-12-05 16:43:11 +0900883 qc->sg = scsi_sglist(cmd);
Boaz Harrosh71201652007-09-18 17:48:50 +0200884 qc->n_elem = scsi_sg_count(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 } else {
886 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500887 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889
890 return qc;
891}
892
Tejun Heoaacda372008-03-18 17:47:43 +0900893static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
894{
895 struct scsi_cmnd *scmd = qc->scsicmd;
896
897 qc->extrabytes = scmd->request->extra_len;
898 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400902 * ata_dump_status - user friendly display of error info
903 * @id: id of the port in question
904 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400906 * Decode and dump the ATA error/status registers for the user so
907 * that they have some idea what really happened at the non
908 * make-believe layer.
909 *
910 * LOCKING:
911 * inherited from caller
912 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100913static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
Jeff Garzikb0955182005-05-12 15:45:22 -0400914{
915 u8 stat = tf->command, err = tf->feature;
916
917 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
918 if (stat & ATA_BUSY) {
919 printk("Busy }\n"); /* Data is not valid in this case */
920 } else {
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100921 if (stat & ATA_DRDY) printk("DriveReady ");
922 if (stat & ATA_DF) printk("DeviceFault ");
923 if (stat & ATA_DSC) printk("SeekComplete ");
924 if (stat & ATA_DRQ) printk("DataRequest ");
925 if (stat & ATA_CORR) printk("CorrectedError ");
926 if (stat & ATA_SENSE) printk("Sense ");
927 if (stat & ATA_ERR) printk("Error ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400928 printk("}\n");
929
930 if (err) {
931 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100932 if (err & ATA_ABORTED) printk("DriveStatusError ");
933 if (err & ATA_ICRC) {
934 if (err & ATA_ABORTED)
935 printk("BadCRC ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400936 else printk("Sector ");
937 }
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100938 if (err & ATA_UNC) printk("UncorrectableError ");
939 if (err & ATA_IDNF) printk("SectorIdNotFound ");
940 if (err & ATA_TRK0NF) printk("TrackZeroNotFound ");
941 if (err & ATA_AMNF) printk("AddrMarkNotFound ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400942 printk("}\n");
943 }
944 }
945}
946
947/**
948 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800949 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400950 * @drv_stat: value contained in ATA status register
951 * @drv_err: value contained in ATA error register
952 * @sk: the sense key we'll fill out
953 * @asc: the additional sense code we'll fill out
954 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900955 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400956 *
957 * Converts an ATA error into a SCSI error. Fill out pointers to
958 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
959 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 *
961 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400962 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100964static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
965 u8 *asc, u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Jeff Garzikb0955182005-05-12 15:45:22 -0400967 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100970 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* BBD|ECC|ID|MAR */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100972 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
973 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* BBD|ECC|ID */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100975 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
976 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* ECC|MC|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100978 {0x61, HARDWARE_ERROR, 0x00, 0x00},
979 // Device fault Hardware error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100981 {0x84, ABORTED_COMMAND, 0x47, 0x00},
982 // Data CRC error SCSI parity error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* MC|ID|ABRT|TRK0|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100984 {0x37, NOT_READY, 0x04, 0x00},
985 // Unit offline Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* MCR|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100987 {0x09, NOT_READY, 0x04, 0x00},
988 // Unrecovered disk error Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* Bad address mark */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100990 {0x01, MEDIUM_ERROR, 0x13, 0x00},
991 // Address mark not found for data field
992 /* TRK0 - Track 0 not found */
993 {0x02, HARDWARE_ERROR, 0x00, 0x00},
994 // Hardware error
Gwendal Grignou78062c52013-06-18 10:54:48 -0700995 /* Abort: 0x04 is not translated here, see below */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /* Media change request */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100997 {0x08, NOT_READY, 0x04, 0x00},
998 // FIXME: faking offline
999 /* SRV/IDNF - ID not found */
1000 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
1001 // Logical address out of range
1002 /* MC - Media Changed */
1003 {0x20, UNIT_ATTENTION, 0x28, 0x00},
1004 // Not ready to ready change, medium may have changed
1005 /* ECC - Uncorrectable ECC error */
1006 {0x40, MEDIUM_ERROR, 0x11, 0x04},
1007 // Unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* BBD - block marked bad */
Hannes Reinecke8ae72042015-03-27 16:46:32 +01001009 {0x80, MEDIUM_ERROR, 0x11, 0x04},
1010 // Block marked bad Medium error, unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
1012 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01001013 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /* Must be first because BUSY means no other bits valid */
Hannes Reinecke8ae72042015-03-27 16:46:32 +01001015 {0x80, ABORTED_COMMAND, 0x47, 0x00},
1016 // Busy, fake parity for now
1017 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
1018 // Device ready, unaligned write command
1019 {0x20, HARDWARE_ERROR, 0x44, 0x00},
1020 // Device fault, internal target failure
1021 {0x08, ABORTED_COMMAND, 0x47, 0x00},
1022 // Timed out in xfer, fake parity for now
1023 {0x04, RECOVERED_ERROR, 0x11, 0x00},
1024 // Recovered ECC error Medium error, recovered
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
1026 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /*
1029 * Is this an error we can process/parse
1030 */
Jeff Garzikb0955182005-05-12 15:45:22 -04001031 if (drv_stat & ATA_BUSY) {
1032 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Jeff Garzikb0955182005-05-12 15:45:22 -04001035 if (drv_err) {
1036 /* Look for drv_err */
1037 for (i = 0; sense_table[i][0] != 0xFF; i++) {
1038 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001039 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -04001040 sense_table[i][0]) {
1041 *sk = sense_table[i][1];
1042 *asc = sense_table[i][2];
1043 *ascq = sense_table[i][3];
1044 goto translate_done;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
Gwendal Grignou78062c52013-06-18 10:54:48 -07001049 /*
1050 * Fall back to interpreting status bits. Note that if the drv_err
1051 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
1052 * is not descriptive enough.
1053 */
Jeff Garzikb0955182005-05-12 15:45:22 -04001054 for (i = 0; stat_table[i][0] != 0xFF; i++) {
1055 if (stat_table[i][0] & drv_stat) {
1056 *sk = stat_table[i][1];
1057 *asc = stat_table[i][2];
1058 *ascq = stat_table[i][3];
1059 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Gwendal Grignou78062c52013-06-18 10:54:48 -07001063 /*
1064 * We need a sensible error return here, which is tricky, and one
1065 * that won't cause people to do things like return a disk wrongly.
1066 */
Alan Cox2d202022006-03-21 15:53:46 +00001067 *sk = ABORTED_COMMAND;
1068 *asc = 0x00;
1069 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -04001070
1071 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +09001072 if (verbose)
1073 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
1074 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
1075 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -04001076 return;
1077}
1078
1079/*
Tejun Heo750426a2006-11-14 22:37:35 +09001080 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -04001081 * @qc: Command that completed.
1082 *
1083 * This function is specific to the ATA descriptor format sense
1084 * block specified for the ATA pass through commands. Regardless
1085 * of whether the command errored or not, return a sense
1086 * block. Copy all controller registers into the sense
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001087 * block. If there was no error, we get the request from an ATA
1088 * passthrough command, so we use the following sense data:
1089 * sk = RECOVERED ERROR
1090 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
1091 *
Jeff Garzikb0955182005-05-12 15:45:22 -04001092 *
1093 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001094 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001095 */
Tejun Heo750426a2006-11-14 22:37:35 +09001096static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001097{
1098 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001099 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001100 unsigned char *sb = cmd->sense_buffer;
1101 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +09001102 int verbose = qc->ap->ops->error_handler == NULL;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001103 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001104
1105 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1106
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001107 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001108
1109 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04001110 * Use ata_to_sense_error() to map status register bits
1111 * onto sense key, asc & ascq.
1112 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001113 if (qc->err_mask ||
1114 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001115 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001116 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001117 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001118 } else {
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001119 /*
1120 * ATA PASS-THROUGH INFORMATION AVAILABLE
1121 * Always in descriptor format sense.
1122 */
1123 scsi_build_sense_buffer(1, cmd->sense_buffer,
1124 RECOVERED_ERROR, 0, 0x1D);
Jeff Garzikb0955182005-05-12 15:45:22 -04001125 }
1126
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001127 if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
1128 u8 len;
Jeff Garzikb0955182005-05-12 15:45:22 -04001129
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001130 /* descriptor format */
1131 len = sb[7];
1132 desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
1133 if (!desc) {
1134 if (SCSI_SENSE_BUFFERSIZE < len + 14)
1135 return;
1136 sb[7] = len + 14;
1137 desc = sb + 8 + len;
1138 }
1139 desc[0] = 9;
1140 desc[1] = 12;
1141 /*
1142 * Copy registers into sense buffer.
1143 */
1144 desc[2] = 0x00;
1145 desc[3] = tf->feature; /* == error reg */
1146 desc[5] = tf->nsect;
1147 desc[7] = tf->lbal;
1148 desc[9] = tf->lbam;
1149 desc[11] = tf->lbah;
1150 desc[12] = tf->device;
1151 desc[13] = tf->command; /* == status reg */
Jeff Garzikb0955182005-05-12 15:45:22 -04001152
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001153 /*
1154 * Fill in Extend bit, and the high order bytes
1155 * if applicable.
1156 */
1157 if (tf->flags & ATA_TFLAG_LBA48) {
1158 desc[2] |= 0x01;
1159 desc[4] = tf->hob_nsect;
1160 desc[6] = tf->hob_lbal;
1161 desc[8] = tf->hob_lbam;
1162 desc[10] = tf->hob_lbah;
1163 }
1164 } else {
1165 /* Fixed sense format */
1166 desc[0] = tf->feature;
1167 desc[1] = tf->command; /* status */
1168 desc[2] = tf->device;
1169 desc[3] = tf->nsect;
Hannes Reineckee0029dc2016-10-31 21:06:58 +01001170 desc[7] = 0;
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001171 if (tf->flags & ATA_TFLAG_LBA48) {
1172 desc[8] |= 0x80;
1173 if (tf->hob_nsect)
1174 desc[8] |= 0x40;
1175 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
1176 desc[8] |= 0x20;
1177 }
1178 desc[9] = tf->lbal;
1179 desc[10] = tf->lbam;
1180 desc[11] = tf->lbah;
Jeff Garzikb0955182005-05-12 15:45:22 -04001181 }
1182}
1183
1184/**
Tejun Heo750426a2006-11-14 22:37:35 +09001185 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -04001186 * @qc: Command that we are erroring out
1187 *
Tejun Heod25614b2006-11-14 22:37:35 +09001188 * Generate sense block for a failed ATA command @qc. Descriptor
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001189 * format is used to accommodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -04001190 *
1191 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001192 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001193 */
Tejun Heo750426a2006-11-14 22:37:35 +09001194static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001195{
Tejun Heod25614b2006-11-14 22:37:35 +09001196 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -04001197 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001198 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001199 unsigned char *sb = cmd->sense_buffer;
Tejun Heo246619d2006-05-15 20:58:16 +09001200 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +09001201 u64 block;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001202 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001203
1204 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1205
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001206 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001207
Hannes Reinecke59f68472016-04-25 12:45:50 +02001208 if (ata_dev_disabled(dev)) {
1209 /* Device disabled after error recovery */
1210 /* LOGICAL UNIT NOT READY, HARD RESET REQUIRED */
1211 ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
1212 return;
1213 }
Tejun Heod25614b2006-11-14 22:37:35 +09001214 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -04001215 * onto sense key, asc & ascq.
1216 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001217 if (qc->err_mask ||
1218 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001219 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001220 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001221 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001222 } else {
1223 /* Could not decode error */
1224 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
1225 tf->command, qc->err_mask);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001226 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001227 return;
Jeff Garzikb0955182005-05-12 15:45:22 -04001228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Tejun Heod25614b2006-11-14 22:37:35 +09001230 block = ata_tf_read_block(&qc->result_tf, dev);
Hannes Reineckecffd1ee2016-04-04 11:43:57 +02001231 if (block == U64_MAX)
1232 return;
Jeff Garzik0274aa22005-06-22 13:50:56 -04001233
Hannes Reineckecf8b49b2016-04-04 11:43:58 +02001234 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Brian Kinga6cce2a2006-03-17 17:04:15 -06001237static void ata_scsi_sdev_config(struct scsi_device *sdev)
1238{
1239 sdev->use_10_for_rw = 1;
1240 sdev->use_10_for_ms = 1;
Nicolai Stangee1859342016-12-07 22:21:33 +01001241 sdev->no_write_same = 1;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001242
1243 /* Schedule policy is determined by ->qc_defer() callback and
1244 * it needs to see every deferred qc. Set dev_blocked to 1 to
1245 * prevent SCSI midlayer from automatically deferring
1246 * requests.
1247 */
1248 sdev->max_device_blocked = 1;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001249}
1250
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001251/**
1252 * atapi_drain_needed - Check whether data transfer may overflow
Randy Dunlap73fd8b62008-02-19 13:43:21 -08001253 * @rq: request to be checked
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001254 *
1255 * ATAPI commands which transfer variable length data to host
Masanari Iidac9b55602016-04-13 23:36:27 +09001256 * might overflow due to application error or hardware bug. This
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001257 * function checks whether overflow should be drained and ignored
1258 * for @request.
1259 *
1260 * LOCKING:
1261 * None.
1262 *
1263 * RETURNS:
1264 * 1 if ; otherwise, 0.
1265 */
1266static int atapi_drain_needed(struct request *rq)
1267{
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001268 if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001269 return 0;
1270
Mike Christiea8ebb052016-06-05 14:31:45 -05001271 if (!blk_rq_bytes(rq) || op_is_write(req_op(rq)))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001272 return 0;
1273
1274 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
1275}
1276
1277static int ata_scsi_dev_config(struct scsi_device *sdev,
1278 struct ata_device *dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -06001279{
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001280 struct request_queue *q = sdev->request_queue;
1281
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02001282 if (!ata_id_has_unload(dev->id))
1283 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1284
Tejun Heo914ed352006-11-01 18:39:55 +09001285 /* configure max sectors */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001286 blk_queue_max_hw_sectors(q, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -06001287
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001288 if (dev->class == ATA_DEV_ATAPI) {
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001289 void *buf;
1290
Tejun Heo729a6a32011-01-20 13:59:06 +01001291 sdev->sector_size = ATA_SECT_SIZE;
1292
1293 /* set DMA padding */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001294 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001295
1296 /* configure draining */
1297 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1298 if (!buf) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001299 ata_dev_err(dev, "drain buffer allocation failed\n");
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001300 return -ENOMEM;
1301 }
1302
1303 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1304 } else {
Grant Grundler295124d2010-08-17 10:56:53 -07001305 sdev->sector_size = ata_id_logical_sector_size(dev->id);
Tejun Heod8cf5382008-01-15 08:46:59 +09001306 sdev->manage_start_stop = 1;
James Bottomleydde20202008-02-19 11:36:56 +01001307 }
Tejun Heod8cf5382008-01-15 08:46:59 +09001308
Tejun Heo729a6a32011-01-20 13:59:06 +01001309 /*
1310 * ata_pio_sectors() expects buffer for each sector to not cross
1311 * page boundary. Enforce it by requiring buffers to be sector
1312 * aligned, which works iff sector_size is not larger than
1313 * PAGE_SIZE. ATAPI devices also need the alignment as
1314 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1315 */
1316 if (sdev->sector_size > PAGE_SIZE)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001317 ata_dev_warn(dev,
Tejun Heo729a6a32011-01-20 13:59:06 +01001318 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1319 sdev->sector_size);
1320
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001321 blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
Tejun Heo729a6a32011-01-20 13:59:06 +01001322
Jeff Garzikf26792d2007-10-29 17:18:39 -04001323 if (dev->flags & ATA_DFLAG_AN)
1324 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1325
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001326 if (dev->flags & ATA_DFLAG_NCQ) {
1327 int depth;
1328
1329 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1330 depth = min(ATA_MAX_QUEUE - 1, depth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001331 scsi_change_queue_depth(sdev, depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001332 }
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001333
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001334 blk_queue_flush_queueable(q, false);
1335
Grant Grundler295124d2010-08-17 10:56:53 -07001336 dev->sdev = sdev;
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001337 return 0;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340/**
1341 * ata_scsi_slave_config - Set SCSI device attributes
1342 * @sdev: SCSI device to examine
1343 *
1344 * This is called before we actually start reading
1345 * and writing to the device, to configure certain
1346 * SCSI mid-layer behaviors.
1347 *
1348 * LOCKING:
1349 * Defined by SCSI layer. We don't really care.
1350 */
1351
1352int ata_scsi_slave_config(struct scsi_device *sdev)
1353{
Tejun Heo31534362006-05-31 18:27:36 +09001354 struct ata_port *ap = ata_shost_to_port(sdev->host);
1355 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001356 int rc = 0;
Tejun Heo31534362006-05-31 18:27:36 +09001357
Brian Kinga6cce2a2006-03-17 17:04:15 -06001358 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Tejun Heo31534362006-05-31 18:27:36 +09001360 if (dev)
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001361 rc = ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001363 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
1366/**
Tejun Heo83c47bc2006-05-31 18:28:07 +09001367 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1368 * @sdev: SCSI device to be destroyed
1369 *
1370 * @sdev is about to be destroyed for hot/warm unplugging. If
1371 * this unplugging was initiated by libata as indicated by NULL
1372 * dev->sdev, this function doesn't have to do anything.
1373 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1374 * Clear dev->sdev, schedule the device for ATA detach and invoke
1375 * EH.
1376 *
1377 * LOCKING:
1378 * Defined by SCSI layer. We don't really care.
1379 */
1380void ata_scsi_slave_destroy(struct scsi_device *sdev)
1381{
1382 struct ata_port *ap = ata_shost_to_port(sdev->host);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001383 struct request_queue *q = sdev->request_queue;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001384 unsigned long flags;
1385 struct ata_device *dev;
1386
1387 if (!ap->ops->error_handler)
1388 return;
1389
Jeff Garzikba6a1302006-06-22 23:46:10 -04001390 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09001391 dev = __ata_scsi_find_dev(ap, sdev);
1392 if (dev && dev->sdev) {
1393 /* SCSI device already in CANCEL state, no need to offline it */
1394 dev->sdev = NULL;
1395 dev->flags |= ATA_DFLAG_DETACH;
1396 ata_port_schedule_eh(ap);
1397 }
Jeff Garzikba6a1302006-06-22 23:46:10 -04001398 spin_unlock_irqrestore(ap->lock, flags);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001399
1400 kfree(q->dma_drain_buffer);
1401 q->dma_drain_buffer = NULL;
1402 q->dma_drain_size = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001403}
1404
1405/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001406 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
Marcos Paulo de Souza6d2dd052011-10-31 23:50:16 -02001407 * @ap: ATA port to which the device change the queue depth
1408 * @sdev: SCSI device to configure queue depth for
1409 * @queue_depth: new queue depth
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001410 *
Dan Williamsf6e67032011-09-20 15:10:33 -07001411 * libsas and libata have different approaches for associating a sdev to
1412 * its ata_port.
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001413 *
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001414 */
Dan Williamsf6e67032011-09-20 15:10:33 -07001415int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001416 int queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001417{
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001418 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +09001419 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001420
Tejun Heoc3c70c42007-02-20 23:31:22 +09001421 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001422 return sdev->queue_depth;
1423
1424 dev = ata_scsi_find_dev(ap, sdev);
1425 if (!dev || !ata_dev_enabled(dev))
1426 return sdev->queue_depth;
1427
Tejun Heoc3c70c42007-02-20 23:31:22 +09001428 /* NCQ enabled? */
Tejun Heo360f6542006-09-30 19:45:00 +09001429 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001430 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1431 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
Tejun Heo360f6542006-09-30 19:45:00 +09001432 dev->flags |= ATA_DFLAG_NCQ_OFF;
Tejun Heoc3c70c42007-02-20 23:31:22 +09001433 queue_depth = 1;
1434 }
Tejun Heo360f6542006-09-30 19:45:00 +09001435 spin_unlock_irqrestore(ap->lock, flags);
1436
Tejun Heoc3c70c42007-02-20 23:31:22 +09001437 /* limit and apply queue depth */
1438 queue_depth = min(queue_depth, sdev->host->can_queue);
1439 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1440 queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
1441
1442 if (sdev->queue_depth == queue_depth)
1443 return -EINVAL;
1444
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001445 return scsi_change_queue_depth(sdev, queue_depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001446}
1447
1448/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001449 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1450 * @sdev: SCSI device to configure queue depth for
1451 * @queue_depth: new queue depth
Dan Williamsf6e67032011-09-20 15:10:33 -07001452 *
1453 * This is libata standard hostt->change_queue_depth callback.
1454 * SCSI will call into this callback when user tries to set queue
1455 * depth via sysfs.
1456 *
1457 * LOCKING:
1458 * SCSI layer (we don't care)
1459 *
1460 * RETURNS:
1461 * Newly configured queue depth.
1462 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001463int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
Dan Williamsf6e67032011-09-20 15:10:33 -07001464{
1465 struct ata_port *ap = ata_shost_to_port(sdev->host);
1466
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001467 return __ata_change_queue_depth(ap, sdev, queue_depth);
Dan Williamsf6e67032011-09-20 15:10:33 -07001468}
1469
1470/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001471 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1472 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001473 *
1474 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1475 * (to start). Perhaps these commands should be preceded by
1476 * CHECK POWER MODE to see what power mode the device is already in.
1477 * [See SAT revision 5 at www.t10.org]
1478 *
1479 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001480 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001481 *
1482 * RETURNS:
1483 * Zero on success, non-zero on error.
1484 */
Tejun Heoad706992006-12-17 10:45:57 +09001485static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001486{
Tejun Heo542b1442006-12-17 10:45:08 +09001487 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001488 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +09001489 const u8 *cdb = scmd->cmnd;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001490 u16 fp;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001491 u8 bp = 0xff;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001492
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001493 if (scmd->cmd_len < 5) {
1494 fp = 4;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001495 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001496 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09001497
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001498 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1499 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +09001500 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001501 ; /* ignore IMMED bit, violates sat-r05 */
1502 }
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001503 if (cdb[4] & 0x2) {
1504 fp = 4;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001505 bp = 1;
Douglas Gilbertae006512005-10-09 09:09:35 -04001506 goto invalid_fld; /* LOEJ bit set not supported */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001507 }
1508 if (((cdb[4] >> 4) & 0xf) != 0) {
1509 fp = 4;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001510 bp = 3;
Douglas Gilbertae006512005-10-09 09:09:35 -04001511 goto invalid_fld; /* power conditions not supported */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001512 }
Tejun Heoe31e85312007-09-23 13:14:13 +09001513
Tejun Heo542b1442006-12-17 10:45:08 +09001514 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001515 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -04001516
1517 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +09001518 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -04001519
1520 tf->lbah = 0x0;
1521 tf->lbam = 0x0;
1522 tf->lbal = 0x0;
1523 tf->device |= ATA_LBA;
1524 } else {
1525 /* CHS */
1526 tf->lbal = 0x1; /* sect */
1527 tf->lbam = 0x0; /* cyl low */
1528 tf->lbah = 0x0; /* cyl high */
1529 }
1530
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001531 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
Tejun Heo920a4b12007-05-04 21:28:48 +02001532 } else {
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001533 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1534 * or S5) causing some drives to spin up and down again.
1535 */
1536 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1537 system_state == SYSTEM_POWER_OFF)
1538 goto skip;
1539
1540 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1541 system_entering_hibernation())
1542 goto skip;
1543
Robert Hancock78981a72007-01-30 00:59:18 -08001544 /* Issue ATA STANDBY IMMEDIATE command */
1545 tf->command = ATA_CMD_STANDBYNOW1;
Tejun Heo920a4b12007-05-04 21:28:48 +02001546 }
Robert Hancock78981a72007-01-30 00:59:18 -08001547
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001548 /*
1549 * Standby and Idle condition timers could be implemented but that
1550 * would require libata to implement the Power condition mode page
1551 * and allow the user to change it. Changing mode pages requires
1552 * MODE SELECT to be implemented.
1553 */
1554
1555 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001556
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001557 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001558 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
Douglas Gilbertae006512005-10-09 09:09:35 -04001559 return 1;
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001560 skip:
1561 scmd->result = SAM_STAT_GOOD;
1562 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001563}
1564
1565
1566/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1568 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 *
1570 * Sets up an ATA taskfile to issue FLUSH CACHE or
1571 * FLUSH CACHE EXT.
1572 *
1573 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001574 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 *
1576 * RETURNS:
1577 * Zero on success, non-zero on error.
1578 */
Tejun Heoad706992006-12-17 10:45:57 +09001579static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 struct ata_taskfile *tf = &qc->tf;
1582
1583 tf->flags |= ATA_TFLAG_DEVICE;
1584 tf->protocol = ATA_PROT_NODATA;
1585
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001586 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 tf->command = ATA_CMD_FLUSH_EXT;
1588 else
1589 tf->command = ATA_CMD_FLUSH;
1590
Tejun Heob666da32007-10-26 15:53:59 +09001591 /* flush is critical for IO integrity, consider it an IO command */
1592 qc->flags |= ATA_QCFLAG_IO;
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return 0;
1595}
1596
1597/**
Albert Lee3aef5232005-10-04 08:47:43 -04001598 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001599 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001600 *
1601 * Calculate LBA and transfer length for 6-byte commands.
1602 *
1603 * RETURNS:
1604 * @plba: the LBA
1605 * @plen: the transfer length
1606 */
Tejun Heo542b1442006-12-17 10:45:08 +09001607static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001608{
1609 u64 lba = 0;
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001610 u32 len;
Albert Lee3aef5232005-10-04 08:47:43 -04001611
1612 VPRINTK("six-byte command\n");
1613
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001614 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
Tejun Heo542b1442006-12-17 10:45:08 +09001615 lba |= ((u64)cdb[2]) << 8;
1616 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001617
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001618 len = cdb[4];
Albert Lee3aef5232005-10-04 08:47:43 -04001619
1620 *plba = lba;
1621 *plen = len;
1622}
1623
1624/**
1625 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001626 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001627 *
1628 * Calculate LBA and transfer length for 10-byte commands.
1629 *
1630 * RETURNS:
1631 * @plba: the LBA
1632 * @plen: the transfer length
1633 */
Tejun Heo542b1442006-12-17 10:45:08 +09001634static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001635{
1636 u64 lba = 0;
1637 u32 len = 0;
1638
1639 VPRINTK("ten-byte command\n");
1640
Tejun Heo542b1442006-12-17 10:45:08 +09001641 lba |= ((u64)cdb[2]) << 24;
1642 lba |= ((u64)cdb[3]) << 16;
1643 lba |= ((u64)cdb[4]) << 8;
1644 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001645
Tejun Heo542b1442006-12-17 10:45:08 +09001646 len |= ((u32)cdb[7]) << 8;
1647 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001648
1649 *plba = lba;
1650 *plen = len;
1651}
1652
1653/**
1654 * scsi_16_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001655 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001656 *
1657 * Calculate LBA and transfer length for 16-byte commands.
1658 *
1659 * RETURNS:
1660 * @plba: the LBA
1661 * @plen: the transfer length
1662 */
Tejun Heo542b1442006-12-17 10:45:08 +09001663static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001664{
1665 u64 lba = 0;
1666 u32 len = 0;
1667
1668 VPRINTK("sixteen-byte command\n");
1669
Tejun Heo542b1442006-12-17 10:45:08 +09001670 lba |= ((u64)cdb[2]) << 56;
1671 lba |= ((u64)cdb[3]) << 48;
1672 lba |= ((u64)cdb[4]) << 40;
1673 lba |= ((u64)cdb[5]) << 32;
1674 lba |= ((u64)cdb[6]) << 24;
1675 lba |= ((u64)cdb[7]) << 16;
1676 lba |= ((u64)cdb[8]) << 8;
1677 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001678
Tejun Heo542b1442006-12-17 10:45:08 +09001679 len |= ((u32)cdb[10]) << 24;
1680 len |= ((u32)cdb[11]) << 16;
1681 len |= ((u32)cdb[12]) << 8;
1682 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001683
1684 *plba = lba;
1685 *plen = len;
1686}
1687
1688/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1690 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 *
1692 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1693 *
1694 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001695 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 *
1697 * RETURNS:
1698 * Zero on success, non-zero on error.
1699 */
Tejun Heoad706992006-12-17 10:45:57 +09001700static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Tejun Heo542b1442006-12-17 10:45:08 +09001702 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001704 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001706 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001707 u64 block;
1708 u32 n_block;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001709 u16 fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1712 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Tejun Heo2e5704f2006-12-17 10:46:33 +09001714 if (cdb[0] == VERIFY) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001715 if (scmd->cmd_len < 10) {
1716 fp = 9;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001717 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001718 }
Tejun Heo542b1442006-12-17 10:45:08 +09001719 scsi_10_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001720 } else if (cdb[0] == VERIFY_16) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001721 if (scmd->cmd_len < 16) {
1722 fp = 15;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001723 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001724 }
Tejun Heo542b1442006-12-17 10:45:08 +09001725 scsi_16_lba_len(cdb, &block, &n_block);
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001726 } else {
1727 fp = 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001728 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Albert Lee8bf62ece2005-05-12 15:29:42 -04001731 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001732 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001733 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001734 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001735 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001736 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Albert Lee07506692005-10-12 15:04:18 +08001738 if (dev->flags & ATA_DFLAG_LBA) {
1739 tf->flags |= ATA_TFLAG_LBA;
1740
Albert Leec6a33e22005-10-12 15:12:26 +08001741 if (lba_28_ok(block, n_block)) {
1742 /* use LBA28 */
1743 tf->command = ATA_CMD_VERIFY;
1744 tf->device |= (block >> 24) & 0xf;
1745 } else if (lba_48_ok(block, n_block)) {
1746 if (!(dev->flags & ATA_DFLAG_LBA48))
1747 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001748
1749 /* use LBA48 */
1750 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001751 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Albert Lee8bf62ece2005-05-12 15:29:42 -04001753 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Albert Lee8bf62ece2005-05-12 15:29:42 -04001755 tf->hob_lbah = (block >> 40) & 0xff;
1756 tf->hob_lbam = (block >> 32) & 0xff;
1757 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001758 } else
1759 /* request too large even for LBA48 */
1760 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001761
1762 tf->nsect = n_block & 0xff;
1763
1764 tf->lbah = (block >> 16) & 0xff;
1765 tf->lbam = (block >> 8) & 0xff;
1766 tf->lbal = block & 0xff;
1767
1768 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001770 /* CHS */
1771 u32 sect, head, cyl, track;
1772
Albert Leec6a33e22005-10-12 15:12:26 +08001773 if (!lba_28_ok(block, n_block))
1774 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001775
Albert Lee8bf62ece2005-05-12 15:29:42 -04001776 /* Convert LBA to CHS */
1777 track = (u32)block / dev->sectors;
1778 cyl = track / dev->heads;
1779 head = track % dev->heads;
1780 sect = (u32)block % dev->sectors + 1;
1781
Albert Leec187c4b2005-10-04 08:46:51 -04001782 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1783 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001784
1785 /* Check whether the converted CHS can fit.
1786 Cylinder: 0-65535
Albert Lee8bf62ece2005-05-12 15:29:42 -04001787 Head: 0-15
1788 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001789 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001790 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001793 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1794 tf->lbal = sect;
1795 tf->lbam = cyl;
1796 tf->lbah = cyl >> 8;
1797 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001801
1802invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001803 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
Douglas Gilbertae006512005-10-09 09:09:35 -04001804 return 1;
1805
1806out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001807 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001808 /* "Logical Block Address out of range" */
1809 return 1;
1810
1811nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001812 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001813 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
1816/**
1817 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1818 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 *
1820 * Converts any of six SCSI read/write commands into the
1821 * ATA counterpart, including starting sector (LBA),
1822 * sector count, and taking into account the device's LBA48
1823 * support.
1824 *
1825 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1826 * %WRITE_16 are currently supported.
1827 *
1828 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001829 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 *
1831 * RETURNS:
1832 * Zero on success, non-zero on error.
1833 */
Tejun Heoad706992006-12-17 10:45:57 +09001834static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Tejun Heo542b1442006-12-17 10:45:08 +09001836 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001837 const u8 *cdb = scmd->cmnd;
Adam Manzanares8e061782016-10-17 11:27:29 -07001838 struct request *rq = scmd->request;
1839 int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
Tejun Heobd056d72006-11-14 22:47:10 +09001840 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001841 u64 block;
1842 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001843 int rc;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001844 u16 fp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Tejun Heo542b1442006-12-17 10:45:08 +09001846 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001847 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001849 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001850 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001851 case READ_10:
1852 case WRITE_10:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001853 if (unlikely(scmd->cmd_len < 10)) {
1854 fp = 9;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001855 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001856 }
Tejun Heo542b1442006-12-17 10:45:08 +09001857 scsi_10_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001858 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001859 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001860 break;
1861 case READ_6:
1862 case WRITE_6:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001863 if (unlikely(scmd->cmd_len < 6)) {
1864 fp = 5;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001865 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001866 }
Tejun Heo542b1442006-12-17 10:45:08 +09001867 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001868
1869 /* for 6-byte r/w commands, transfer length 0
1870 * means 256 blocks of data, not 0 block.
1871 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001872 if (!n_block)
1873 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001874 break;
1875 case READ_16:
1876 case WRITE_16:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001877 if (unlikely(scmd->cmd_len < 16)) {
1878 fp = 15;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001879 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001880 }
Tejun Heo542b1442006-12-17 10:45:08 +09001881 scsi_16_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001882 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001883 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001884 break;
1885 default:
Albert Lee8bf62ece2005-05-12 15:29:42 -04001886 DPRINTK("no-byte command\n");
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001887 fp = 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001888 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890
Albert Lee8bf62ece2005-05-12 15:29:42 -04001891 /* Check and compose ATA command */
1892 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001893 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1894 * length 0 means transfer 0 block of data.
1895 * However, for ATA R/W commands, sector count 0 means
1896 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001897 *
1898 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001899 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001900 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001901
Tejun Heobd056d72006-11-14 22:47:10 +09001902 qc->flags |= ATA_QCFLAG_IO;
Grant Grundler295124d2010-08-17 10:56:53 -07001903 qc->nbytes = n_block * scmd->device->sector_size;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001904
Tejun Heobd056d72006-11-14 22:47:10 +09001905 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
Adam Manzanares8e061782016-10-17 11:27:29 -07001906 qc->tag, class);
1907
Tejun Heobd056d72006-11-14 22:47:10 +09001908 if (likely(rc == 0))
1909 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001910
Tejun Heobd056d72006-11-14 22:47:10 +09001911 if (rc == -ERANGE)
1912 goto out_of_range;
1913 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001914invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001915 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
Douglas Gilbertae006512005-10-09 09:09:35 -04001916 return 1;
1917
1918out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001919 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001920 /* "Logical Block Address out of range" */
1921 return 1;
1922
1923nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001924 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001925 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001928static void ata_qc_done(struct ata_queued_cmd *qc)
1929{
1930 struct scsi_cmnd *cmd = qc->scsicmd;
1931 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1932
1933 ata_qc_free(qc);
1934 done(cmd);
1935}
1936
Tejun Heo77853bf2006-01-23 13:09:36 +09001937static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Tejun Heoc31f5712006-11-22 12:39:43 +09001939 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001941 u8 *cdb = cmd->cmnd;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001942 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Jeff Garzikb0955182005-05-12 15:45:22 -04001944 /* For ATA pass thru (SAT) commands, generate a sense block if
1945 * user mandated it or if there's an error. Note that if we
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001946 * generate because the user forced us to [CK_COND =1], a check
1947 * condition is generated and the ATA register values are returned
Jeff Garzikb0955182005-05-12 15:45:22 -04001948 * whether the command completed successfully or not. If there
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001949 * was no error, we use the following sense data:
1950 * sk = RECOVERED ERROR
1951 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
Jeff Garzikb0955182005-05-12 15:45:22 -04001952 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001953 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001954 ((cdb[2] & 0x20) || need_sense))
Tejun Heo750426a2006-11-14 22:37:35 +09001955 ata_gen_passthru_sense(qc);
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +02001956 else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1957 cmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001958 else if (need_sense)
1959 ata_gen_ata_sense(qc);
1960 else
1961 cmd->result = SAM_STAT_GOOD;
Jeff Garzikb0955182005-05-12 15:45:22 -04001962
Tejun Heoc31f5712006-11-22 12:39:43 +09001963 if (need_sense && !ap->ops->error_handler)
Tejun Heo44877b42007-02-21 01:06:51 +09001964 ata_dump_status(ap->print_id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001966 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969/**
1970 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 * @dev: ATA device to which the command is addressed
1972 * @cmd: SCSI command to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1974 *
1975 * Our ->queuecommand() function has decided that the SCSI
1976 * command issued can be directly translated into an ATA
1977 * command, rather than handled internally.
1978 *
1979 * This function sets up an ata_queued_cmd structure for the
1980 * SCSI command, and sends that ata_queued_cmd to the hardware.
1981 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001982 * The xlat_func argument (actor) returns 0 if ready to execute
1983 * ATA command, else 1 to finish translation. If 1 is returned
1984 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1985 * to be set reflecting an error condition or clean (early)
1986 * termination.
1987 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001989 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09001990 *
1991 * RETURNS:
1992 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1993 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 */
Tejun Heo2115ea92006-05-15 21:03:39 +09001995static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09001996 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Tejun Heo31cc23b2007-09-23 13:14:12 +09001998 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 struct ata_queued_cmd *qc;
Tejun Heo31cc23b2007-09-23 13:14:12 +09002000 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 VPRINTK("ENTER\n");
2003
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002004 qc = ata_scsi_qc_new(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04002006 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05002009 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
2010 cmd->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harrosh71201652007-09-18 17:48:50 +02002011 if (unlikely(scsi_bufflen(cmd) < 1)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002012 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04002013 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015
Boaz Harrosh71201652007-09-18 17:48:50 +02002016 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 qc->dma_dir = cmd->sc_data_direction;
2019 }
2020
2021 qc->complete_fn = ata_scsi_qc_complete;
2022
Tejun Heoad706992006-12-17 10:45:57 +09002023 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04002024 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Tejun Heo31cc23b2007-09-23 13:14:12 +09002026 if (ap->ops->qc_defer) {
2027 if ((rc = ap->ops->qc_defer(qc)))
2028 goto defer;
2029 }
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09002032 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Douglas Gilbertae006512005-10-09 09:09:35 -04002037early_finish:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002038 ata_qc_free(qc);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002039 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -04002040 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002041 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002042
2043err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04002045 cmd->result = (DID_ERROR << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002046 cmd->scsi_done(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05002047err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04002048 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002049 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09002050
2051defer:
Tejun Heo31cc23b2007-09-23 13:14:12 +09002052 ata_qc_free(qc);
Tejun Heo3dc1d882006-05-15 21:03:45 +09002053 DPRINTK("EXIT - defer\n");
Tejun Heo31cc23b2007-09-23 13:14:12 +09002054 if (rc == ATA_DEFER_LINK)
2055 return SCSI_MLQUEUE_DEVICE_BUSY;
2056 else
2057 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
Christoph Hellwigf0a37d12017-01-10 09:41:44 +01002060struct ata_scsi_args {
2061 struct ata_device *dev;
2062 u16 *id;
2063 struct scsi_cmnd *cmd;
2064 void (*done)(struct scsi_cmnd *);
2065};
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067/**
2068 * ata_scsi_rbuf_get - Map response buffer.
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002069 * @cmd: SCSI command containing buffer to be mapped.
Tejun Heo87340e92008-04-28 17:48:51 +09002070 * @flags: unsigned long variable to store irq enable status
2071 * @copy_in: copy in from user buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 *
Tejun Heo87340e92008-04-28 17:48:51 +09002073 * Prepare buffer for simulated SCSI commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 *
2075 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09002076 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 *
2078 * RETURNS:
Tejun Heo87340e92008-04-28 17:48:51 +09002079 * Pointer to response buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 */
Tejun Heo87340e92008-04-28 17:48:51 +09002081static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
2082 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Tejun Heo87340e92008-04-28 17:48:51 +09002084 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Tejun Heo87340e92008-04-28 17:48:51 +09002086 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
2087 if (copy_in)
2088 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2089 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2090 return ata_scsi_rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
2093/**
2094 * ata_scsi_rbuf_put - Unmap response buffer.
2095 * @cmd: SCSI command containing buffer to be unmapped.
Tejun Heo87340e92008-04-28 17:48:51 +09002096 * @copy_out: copy out result
2097 * @flags: @flags passed to ata_scsi_rbuf_get()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 *
Tejun Heo87340e92008-04-28 17:48:51 +09002099 * Returns rbuf buffer. The result is copied to @cmd's buffer if
2100 * @copy_back is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 *
2102 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09002103 * Unlocks ata_scsi_rbuf_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 */
Tejun Heo87340e92008-04-28 17:48:51 +09002105static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
2106 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Tejun Heo87340e92008-04-28 17:48:51 +09002108 if (copy_out)
2109 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2110 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2111 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
2114/**
2115 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
2116 * @args: device IDENTIFY data / SCSI command of interest.
2117 * @actor: Callback hook for desired SCSI command simulator
2118 *
2119 * Takes care of the hard work of simulating a SCSI command...
2120 * Mapping the response buffer, calling the command's handler,
2121 * and handling the handler's return value. This return value
2122 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04002123 * completed successfully (0), or not (in which case cmd->result
2124 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 *
2126 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002127 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 */
Tejun Heof0761be2008-04-28 17:16:52 +09002129static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
Tejun Heo87340e92008-04-28 17:48:51 +09002130 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 u8 *rbuf;
Tejun Heo87340e92008-04-28 17:48:51 +09002133 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 struct scsi_cmnd *cmd = args->cmd;
Jeff Garzikb445c562008-02-29 19:10:51 -05002135 unsigned long flags;
2136
Tejun Heo87340e92008-04-28 17:48:51 +09002137 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
2138 rc = actor(args, rbuf);
2139 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
Jeff Garzikb445c562008-02-29 19:10:51 -05002140
Douglas Gilbertae006512005-10-09 09:09:35 -04002141 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04002143 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146/**
2147 * ata_scsiop_inq_std - Simulate INQUIRY command
2148 * @args: device IDENTIFY data / SCSI command of interest.
2149 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 *
2151 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05002152 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 *
2154 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002155 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 */
Tejun Heo87340e92008-04-28 17:48:51 +09002157static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Tejun Heo87340e92008-04-28 17:48:51 +09002159 const u8 versions[] = {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002160 0x00,
Tejun Heo87340e92008-04-28 17:48:51 +09002161 0x60, /* SAM-3 (no version claimed) */
2162
2163 0x03,
2164 0x20, /* SBC-2 (no version claimed) */
2165
Tom Yan29a37ea2016-07-12 21:29:35 +08002166 0x03,
2167 0x00 /* SPC-3 (no version claimed) */
Tejun Heo87340e92008-04-28 17:48:51 +09002168 };
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002169 const u8 versions_zbc[] = {
2170 0x00,
2171 0xA0, /* SAM-5 (no version claimed) */
2172
Hannes Reinecke856c4662016-04-25 12:45:55 +02002173 0x06,
2174 0x00, /* SBC-4 (no version claimed) */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002175
Hannes Reinecke856c4662016-04-25 12:45:55 +02002176 0x05,
2177 0xC0, /* SPC-5 (no version claimed) */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002178
2179 0x60,
Hannes Reinecke856c4662016-04-25 12:45:55 +02002180 0x24, /* ZBC r05 */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002181 };
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 u8 hdr[] = {
2184 TYPE_DISK,
2185 0,
2186 0x5, /* claim SPC-3 version compatibility */
2187 2,
Tom Yan415ffdd2016-07-12 21:29:34 +08002188 95 - 4,
2189 0,
2190 0,
2191 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 };
2193
Tejun Heo87340e92008-04-28 17:48:51 +09002194 VPRINTK("ENTER\n");
2195
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02002196 /* set scsi removable (RMB) bit per ata bit, or if the
2197 * AHCI port says it's external (Hotplug-capable, eSATA).
2198 */
2199 if (ata_id_removable(args->id) ||
2200 (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 hdr[1] |= (1 << 7);
2202
Tom Yan56b8cba2016-07-12 22:12:01 +08002203 if (args->dev->class == ATA_DEV_ZAC) {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002204 hdr[0] = TYPE_ZBC;
Tom Yan56b8cba2016-07-12 22:12:01 +08002205 hdr[2] = 0x7; /* claim SPC-5 version compatibility */
2206 }
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002209 memcpy(&rbuf[8], "ATA ", 8);
2210 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
Keith Buschc49a6bf2014-05-01 11:12:03 -06002211
2212 /* From SAT, use last 2 words from fw rev unless they are spaces */
2213 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
2214 if (strncmp(&rbuf[32], " ", 4) == 0)
2215 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Tejun Heo87340e92008-04-28 17:48:51 +09002217 if (rbuf[32] == 0 || rbuf[32] == ' ')
2218 memcpy(&rbuf[32], "n/a ", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Hannes Reinecke856c4662016-04-25 12:45:55 +02002220 if (ata_id_zoned_cap(args->id) || args->dev->class == ATA_DEV_ZAC)
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002221 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
2222 else
2223 memcpy(rbuf + 58, versions, sizeof(versions));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 return 0;
2226}
2227
2228/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002229 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 * @args: device IDENTIFY data / SCSI command of interest.
2231 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002233 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 *
2235 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002236 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 */
Tejun Heo87340e92008-04-28 17:48:51 +09002238static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002240 int num_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 const u8 pages[] = {
2242 0x00, /* page 0x00, this page */
2243 0x80, /* page 0x80, unit serial no page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002244 0x83, /* page 0x83, device ident page */
2245 0x89, /* page 0x89, ata info page */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002246 0xb0, /* page 0xb0, block limits page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002247 0xb1, /* page 0xb1, block device characteristics page */
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002248 0xb2, /* page 0xb2, thin provisioning page */
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002249 0xb6, /* page 0xb6, zoned block device characteristics */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 };
Tejun Heo87340e92008-04-28 17:48:51 +09002251
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002252 num_pages = sizeof(pages);
2253 if (!(args->dev->flags & ATA_DFLAG_ZAC))
2254 num_pages--;
2255 rbuf[3] = num_pages; /* number of supported VPD pages */
2256 memcpy(rbuf + 4, pages, num_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return 0;
2258}
2259
2260/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002261 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 * @args: device IDENTIFY data / SCSI command of interest.
2263 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 *
2265 * Returns ATA device serial number.
2266 *
2267 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002268 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 */
Tejun Heo87340e92008-04-28 17:48:51 +09002270static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 const u8 hdr[] = {
2273 0,
2274 0x80, /* this page code */
2275 0,
Tejun Heoa0cf7332007-01-02 20:18:49 +09002276 ATA_ID_SERNO_LEN, /* page len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 };
Tejun Heo87340e92008-04-28 17:48:51 +09002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002280 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2281 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return 0;
2283}
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002286 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 * @args: device IDENTIFY data / SCSI command of interest.
2288 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002290 * Yields two logical unit device identification designators:
2291 * - vendor specific ASCII containing the ATA serial number
2292 * - SAT defined "t10 vendor id based" containing ASCII vendor
2293 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 *
2295 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002296 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 */
Tejun Heo87340e92008-04-28 17:48:51 +09002298static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Jeff Garzikb142eb62006-03-21 20:37:47 -05002300 const int sat_model_serial_desc_len = 68;
Tejun Heo87340e92008-04-28 17:48:51 +09002301 int num;
Jeff Garzikb142eb62006-03-21 20:37:47 -05002302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05002304 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Tejun Heo87340e92008-04-28 17:48:51 +09002306 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2307 rbuf[num + 0] = 2;
2308 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2309 num += 4;
2310 ata_id_string(args->id, (unsigned char *) rbuf + num,
2311 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2312 num += ATA_ID_SERNO_LEN;
2313
2314 /* SAT defined lu model and serial numbers descriptor */
2315 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2316 rbuf[num + 0] = 2;
2317 rbuf[num + 1] = 1;
2318 rbuf[num + 3] = sat_model_serial_desc_len;
2319 num += 4;
2320 memcpy(rbuf + num, "ATA ", 8);
2321 num += 8;
2322 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2323 ATA_ID_PROD_LEN);
2324 num += ATA_ID_PROD_LEN;
2325 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2326 ATA_ID_SERNO_LEN);
2327 num += ATA_ID_SERNO_LEN;
2328
Hannes Reinecke6b3b9d72011-03-07 08:56:44 +01002329 if (ata_id_has_wwn(args->id)) {
2330 /* SAT defined lu world wide name */
2331 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2332 rbuf[num + 0] = 1;
2333 rbuf[num + 1] = 3;
2334 rbuf[num + 3] = ATA_ID_WWN_LEN;
2335 num += 4;
2336 ata_id_string(args->id, (unsigned char *) rbuf + num,
2337 ATA_ID_WWN, ATA_ID_WWN_LEN);
2338 num += ATA_ID_WWN_LEN;
2339 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05002340 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return 0;
2342}
2343
2344/**
Jeff Garzikad355b42007-09-21 20:38:03 -04002345 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2346 * @args: device IDENTIFY data / SCSI command of interest.
2347 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Jeff Garzikad355b42007-09-21 20:38:03 -04002348 *
2349 * Yields SAT-specified ATA VPD page.
2350 *
2351 * LOCKING:
2352 * spin_lock_irqsave(host lock)
2353 */
Tejun Heo87340e92008-04-28 17:48:51 +09002354static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
Jeff Garzikad355b42007-09-21 20:38:03 -04002355{
Jeff Garzikad355b42007-09-21 20:38:03 -04002356 struct ata_taskfile tf;
Jeff Garzikad355b42007-09-21 20:38:03 -04002357
Jeff Garzikad355b42007-09-21 20:38:03 -04002358 memset(&tf, 0, sizeof(tf));
2359
Tejun Heo87340e92008-04-28 17:48:51 +09002360 rbuf[1] = 0x89; /* our page code */
2361 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2362 rbuf[3] = (0x238 & 0xff);
Jeff Garzikad355b42007-09-21 20:38:03 -04002363
Tejun Heo87340e92008-04-28 17:48:51 +09002364 memcpy(&rbuf[8], "linux ", 8);
2365 memcpy(&rbuf[16], "libata ", 16);
2366 memcpy(&rbuf[32], DRV_VERSION, 4);
Jeff Garzikad355b42007-09-21 20:38:03 -04002367
2368 /* we don't store the ATA device signature, so we fake it */
2369
2370 tf.command = ATA_DRDY; /* really, this is Status reg */
2371 tf.lbal = 0x1;
2372 tf.nsect = 0x1;
2373
Tejun Heo87340e92008-04-28 17:48:51 +09002374 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
2375 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
Jeff Garzikad355b42007-09-21 20:38:03 -04002376
Tejun Heo87340e92008-04-28 17:48:51 +09002377 rbuf[56] = ATA_CMD_ID_ATA;
Jeff Garzikad355b42007-09-21 20:38:03 -04002378
Tejun Heo87340e92008-04-28 17:48:51 +09002379 memcpy(&rbuf[60], &args->id[0], 512);
Jeff Garzikad355b42007-09-21 20:38:03 -04002380 return 0;
2381}
2382
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002383static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2384{
Grant Grundler295124d2010-08-17 10:56:53 -07002385 u16 min_io_sectors;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002386
2387 rbuf[1] = 0xb0;
2388 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2389
2390 /*
2391 * Optimal transfer length granularity.
2392 *
2393 * This is always one physical block, but for disks with a smaller
2394 * logical than physical sector size we need to figure out what the
2395 * latter is.
2396 */
Grant Grundler295124d2010-08-17 10:56:53 -07002397 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002398 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2399
2400 /*
2401 * Optimal unmap granularity.
2402 *
2403 * The ATA spec doesn't even know about a granularity or alignment
2404 * for the TRIM command. We can leave away most of the unmap related
2405 * VPD page entries, but we have specifify a granularity to signal
2406 * that we support some form of unmap - in thise case via WRITE SAME
2407 * with the unmap bit set.
2408 */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002409 if (ata_id_has_trim(args->id)) {
Tom Yan29838602016-07-13 04:31:23 +08002410 put_unaligned_be64(65535 * ATA_MAX_TRIM_RNUM, &rbuf[36]);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002411 put_unaligned_be32(1, &rbuf[28]);
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002412 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002413
2414 return 0;
2415}
2416
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002417static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2418{
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002419 int form_factor = ata_id_form_factor(args->id);
2420 int media_rotation_rate = ata_id_rotation_rate(args->id);
Hannes Reinecke856c4662016-04-25 12:45:55 +02002421 u8 zoned = ata_id_zoned_cap(args->id);
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002422
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002423 rbuf[1] = 0xb1;
2424 rbuf[3] = 0x3c;
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002425 rbuf[4] = media_rotation_rate >> 8;
2426 rbuf[5] = media_rotation_rate;
2427 rbuf[7] = form_factor;
Hannes Reinecke856c4662016-04-25 12:45:55 +02002428 if (zoned)
2429 rbuf[8] = (zoned << 4);
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002430
2431 return 0;
2432}
2433
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002434static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
2435{
2436 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2437 rbuf[1] = 0xb2;
2438 rbuf[3] = 0x4;
2439 rbuf[5] = 1 << 6; /* TPWS */
2440
2441 return 0;
2442}
2443
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002444static unsigned int ata_scsiop_inq_b6(struct ata_scsi_args *args, u8 *rbuf)
2445{
2446 /*
2447 * zbc-r05 SCSI Zoned Block device characteristics VPD page
2448 */
2449 rbuf[1] = 0xb6;
2450 rbuf[3] = 0x3C;
2451
2452 /*
2453 * URSWRZ bit is only meaningful for host-managed ZAC drives
2454 */
2455 if (args->dev->zac_zoned_cap & 1)
2456 rbuf[4] |= 1;
2457 put_unaligned_be32(args->dev->zac_zones_optimal_open, &rbuf[8]);
2458 put_unaligned_be32(args->dev->zac_zones_optimal_nonseq, &rbuf[12]);
2459 put_unaligned_be32(args->dev->zac_zones_max_open, &rbuf[16]);
2460
2461 return 0;
2462}
2463
Jeff Garzikad355b42007-09-21 20:38:03 -04002464/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002465 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 * @args: device IDENTIFY data / SCSI command of interest.
2467 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 *
2469 * No operation. Simply returns success to caller, to indicate
2470 * that the caller should successfully complete this SCSI command.
2471 *
2472 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002473 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 */
Tejun Heo87340e92008-04-28 17:48:51 +09002475static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 VPRINTK("ENTER\n");
2478 return 0;
2479}
2480
2481/**
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002482 * modecpy - Prepare response for MODE SENSE
2483 * @dest: output buffer
2484 * @src: data being copied
2485 * @n: length of mode page
2486 * @changeable: whether changeable parameters are requested
2487 *
2488 * Generate a generic MODE SENSE page for either current or changeable
2489 * parameters.
2490 *
2491 * LOCKING:
2492 * None.
2493 */
2494static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2495{
2496 if (changeable) {
2497 memcpy(dest, src, 2);
2498 memset(dest + 2, 0, n - 2);
2499 } else {
2500 memcpy(dest, src, n);
2501 }
2502}
2503
2504/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 * ata_msense_caching - Simulate MODE SENSE caching info page
2506 * @id: device IDENTIFY data
Tejun Heo87340e92008-04-28 17:48:51 +09002507 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002508 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 *
2510 * Generate a caching info page, which conditionally indicates
2511 * write caching to the SCSI layer, depending on device
2512 * capabilities.
2513 *
2514 * LOCKING:
2515 * None.
2516 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002517static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002519 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
Tom Yan737bee92016-07-20 04:39:28 +08002520 if (changeable) {
2521 buf[2] |= (1 << 2); /* ata_mselect_caching() */
2522 } else {
2523 buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */
2524 buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */
2525 }
Tejun Heo87340e92008-04-28 17:48:51 +09002526 return sizeof(def_cache_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
2529/**
Tom Yanf086b742016-07-13 02:54:12 +08002530 * ata_msense_control - Simulate MODE SENSE control mode page
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002531 * @dev: ATA device of interest
Tejun Heo87340e92008-04-28 17:48:51 +09002532 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002533 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 *
2535 * Generate a generic MODE SENSE control mode page.
2536 *
2537 * LOCKING:
2538 * None.
2539 */
Tom Yanf086b742016-07-13 02:54:12 +08002540static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002541 bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002543 modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
Tom Yan737bee92016-07-20 04:39:28 +08002544 if (changeable) {
2545 buf[2] |= (1 << 2); /* ata_mselect_control() */
2546 } else {
2547 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
2548
2549 buf[2] |= (d_sense << 2); /* descriptor format sense data */
2550 }
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002551 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
2554/**
2555 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002556 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002557 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 *
2559 * Generate a generic MODE SENSE r/w error recovery page.
2560 *
2561 * LOCKING:
2562 * None.
2563 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002564static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002566 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2567 changeable);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002568 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002571/*
2572 * We can turn this into a real blacklist if it's needed, for now just
2573 * blacklist any Maxtor BANC1G10 revision firmware
2574 */
2575static int ata_dev_supports_fua(u16 *id)
2576{
Tejun Heoa0cf7332007-01-02 20:18:49 +09002577 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002578
Jeff Garzikc3c013a2006-02-27 22:31:19 -05002579 if (!libata_fua)
2580 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002581 if (!ata_id_has_fua(id))
2582 return 0;
2583
Tejun Heoa0cf7332007-01-02 20:18:49 +09002584 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2585 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002586
Tejun Heo2e026712006-02-12 22:47:04 +09002587 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002588 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09002589 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002590 return 1;
2591
2592 return 0; /* blacklisted */
2593}
2594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595/**
2596 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2597 * @args: device IDENTIFY data / SCSI command of interest.
2598 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002600 * Simulate MODE SENSE commands. Assume this is invoked for direct
2601 * access devices (e.g. disks) only. There should be no block
2602 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 *
2604 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002605 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 */
Tejun Heo87340e92008-04-28 17:48:51 +09002607static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002609 struct ata_device *dev = args->dev;
Tejun Heo87340e92008-04-28 17:48:51 +09002610 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002611 const u8 sat_blk_desc[] = {
2612 0, 0, 0, 0, /* number of blocks: sat unspecified */
2613 0,
2614 0, 0x2, 0x0 /* block length: 512 bytes */
2615 };
2616 u8 pg, spg;
Tejun Heo87340e92008-04-28 17:48:51 +09002617 unsigned int ebd, page_control, six_byte;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002618 u8 dpofua, bp = 0xff;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002619 u16 fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621 VPRINTK("ENTER\n");
2622
2623 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002624 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2625 /*
2626 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002630 switch (page_control) {
2631 case 0: /* current */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002632 case 1: /* changeable */
2633 case 2: /* defaults */
Douglas Gilbertae006512005-10-09 09:09:35 -04002634 break; /* supported */
2635 case 3: /* saved */
2636 goto saving_not_supp;
Douglas Gilbertae006512005-10-09 09:09:35 -04002637 default:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002638 fp = 2;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002639 bp = 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002640 goto invalid_fld;
2641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Tejun Heo87340e92008-04-28 17:48:51 +09002643 if (six_byte)
2644 p += 4 + (ebd ? 8 : 0);
2645 else
2646 p += 8 + (ebd ? 8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002648 pg = scsicmd[2] & 0x3f;
2649 spg = scsicmd[3];
2650 /*
2651 * No mode subpages supported (yet) but asking for _all_
2652 * subpages may be valid
2653 */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002654 if (spg && (spg != ALL_SUB_MPAGES)) {
2655 fp = 3;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002656 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002657 }
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002658
2659 switch(pg) {
2660 case RW_RECOVERY_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002661 p += ata_msense_rw_recovery(p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 break;
2663
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002664 case CACHE_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002665 p += ata_msense_caching(args->id, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 break;
2667
Tejun Heo87340e92008-04-28 17:48:51 +09002668 case CONTROL_MPAGE:
Tom Yanf086b742016-07-13 02:54:12 +08002669 p += ata_msense_control(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002672 case ALL_MPAGES:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002673 p += ata_msense_rw_recovery(p, page_control == 1);
2674 p += ata_msense_caching(args->id, p, page_control == 1);
Tom Yanf086b742016-07-13 02:54:12 +08002675 p += ata_msense_control(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 break;
2677
2678 default: /* invalid page code */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002679 fp = 2;
Douglas Gilbertae006512005-10-09 09:09:35 -04002680 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
2682
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002683 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002684 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002685 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2686 dpofua = 1 << 4;
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (six_byte) {
Tejun Heo87340e92008-04-28 17:48:51 +09002689 rbuf[0] = p - rbuf - 1;
2690 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002691 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002692 rbuf[3] = sizeof(sat_blk_desc);
2693 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 } else {
Tejun Heo87340e92008-04-28 17:48:51 +09002696 unsigned int output_len = p - rbuf - 2;
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 rbuf[0] = output_len >> 8;
Tejun Heo87340e92008-04-28 17:48:51 +09002699 rbuf[1] = output_len;
2700 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002701 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002702 rbuf[7] = sizeof(sat_blk_desc);
2703 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002707
2708invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002709 ata_scsi_set_invalid_field(dev, args->cmd, fp, bp);
Douglas Gilbertae006512005-10-09 09:09:35 -04002710 return 1;
2711
2712saving_not_supp:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002713 ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04002714 /* "Saving parameters not supported" */
2715 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
2718/**
2719 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2720 * @args: device IDENTIFY data / SCSI command of interest.
2721 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 *
2723 * Simulate READ CAPACITY commands.
2724 *
2725 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002726 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 */
Tejun Heo87340e92008-04-28 17:48:51 +09002728static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002730 struct ata_device *dev = args->dev;
2731 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
Grant Grundler295124d2010-08-17 10:56:53 -07002732 u32 sector_size; /* physical sector size in bytes */
2733 u8 log2_per_phys;
2734 u16 lowest_aligned;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002735
Grant Grundler295124d2010-08-17 10:56:53 -07002736 sector_size = ata_id_logical_sector_size(dev->id);
2737 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2738 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 VPRINTK("ENTER\n");
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002743 if (last_lba >= 0xffffffffULL)
2744 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 /* sector count, 32-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002747 rbuf[0] = last_lba >> (8 * 3);
2748 rbuf[1] = last_lba >> (8 * 2);
2749 rbuf[2] = last_lba >> (8 * 1);
2750 rbuf[3] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002753 rbuf[4] = sector_size >> (8 * 3);
2754 rbuf[5] = sector_size >> (8 * 2);
2755 rbuf[6] = sector_size >> (8 * 1);
2756 rbuf[7] = sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 } else {
2758 /* sector count, 64-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002759 rbuf[0] = last_lba >> (8 * 7);
2760 rbuf[1] = last_lba >> (8 * 6);
2761 rbuf[2] = last_lba >> (8 * 5);
2762 rbuf[3] = last_lba >> (8 * 4);
2763 rbuf[4] = last_lba >> (8 * 3);
2764 rbuf[5] = last_lba >> (8 * 2);
2765 rbuf[6] = last_lba >> (8 * 1);
2766 rbuf[7] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002769 rbuf[ 8] = sector_size >> (8 * 3);
2770 rbuf[ 9] = sector_size >> (8 * 2);
2771 rbuf[10] = sector_size >> (8 * 1);
2772 rbuf[11] = sector_size;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002773
2774 rbuf[12] = 0;
Grant Grundler295124d2010-08-17 10:56:53 -07002775 rbuf[13] = log2_per_phys;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002776 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2777 rbuf[15] = lowest_aligned;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002778
Arne Fitzenreiter71d126f2015-07-15 13:54:36 +02002779 if (ata_id_has_trim(args->id) &&
2780 !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002781 rbuf[14] |= 0x80; /* LBPME */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002782
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002783 if (ata_id_has_zero_after_trim(args->id) &&
2784 dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
2785 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2786 rbuf[14] |= 0x40; /* LBPRZ */
2787 }
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002788 }
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002789 if (ata_id_zoned_cap(args->id) ||
2790 args->dev->class == ATA_DEV_ZAC)
2791 rbuf[12] = (1 << 4); /* RC_BASIS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return 0;
2794}
2795
2796/**
2797 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2798 * @args: device IDENTIFY data / SCSI command of interest.
2799 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 *
2801 * Simulate REPORT LUNS command.
2802 *
2803 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002804 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 */
Tejun Heo87340e92008-04-28 17:48:51 +09002806static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
2808 VPRINTK("ENTER\n");
2809 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2810
2811 return 0;
2812}
2813
Tejun Heo77853bf2006-01-23 13:09:36 +09002814static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002815{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002816 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002817 /* FIXME: not quite right; we don't want the
2818 * translation of taskfile registers into
2819 * a sense descriptors, since that's only
2820 * correct for ATA, not ATAPI
2821 */
Tejun Heo750426a2006-11-14 22:37:35 +09002822 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002823 }
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002824
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002825 ata_qc_done(qc);
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002826}
2827
2828/* is it pointless to prefer PIO for "safety reasons"? */
2829static inline int ata_pio_use_silly(struct ata_port *ap)
2830{
2831 return (ap->flags & ATA_FLAG_PIO_DMA);
2832}
2833
2834static void atapi_request_sense(struct ata_queued_cmd *qc)
2835{
2836 struct ata_port *ap = qc->ap;
2837 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002838
2839 DPRINTK("ATAPI request sense\n");
2840
James Bottomley7ccd7202008-01-17 11:56:24 -06002841 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Jeff Garzika939c962005-10-05 17:09:16 -04002842
Tejun Heo127102a2008-04-07 22:47:21 +09002843#ifdef CONFIG_ATA_SFF
James Bottomley855d8542008-04-18 13:18:48 -05002844 if (ap->ops->sff_tf_read)
2845 ap->ops->sff_tf_read(ap, &qc->tf);
Tejun Heo127102a2008-04-07 22:47:21 +09002846#endif
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002847
2848 /* fill these in, for the case where they are -not- overwritten */
2849 cmd->sense_buffer[0] = 0x70;
2850 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2851
2852 ata_qc_reinit(qc);
2853
Tejun Heo93f8fec2007-12-05 16:43:01 +09002854 /* setup sg table and init transfer direction */
akpm@linux-foundation.orgcadb7342008-01-15 16:01:52 -08002855 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
Tejun Heo93f8fec2007-12-05 16:43:01 +09002856 ata_sg_init(qc, &qc->sgent, 1);
Jeff Garzika939c962005-10-05 17:09:16 -04002857 qc->dma_dir = DMA_FROM_DEVICE;
2858
Tejun Heo6e7846e2006-02-12 23:32:58 +09002859 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002860 qc->cdb[0] = REQUEST_SENSE;
2861 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2862
2863 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2864 qc->tf.command = ATA_CMD_PACKET;
2865
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002866 if (ata_pio_use_silly(ap)) {
Tejun Heo0dc36882007-12-18 16:34:43 -05002867 qc->tf.protocol = ATAPI_PROT_DMA;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002868 qc->tf.feature |= ATAPI_PKT_DMA;
2869 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05002870 qc->tf.protocol = ATAPI_PROT_PIO;
Alan Cox2db78dd2007-10-02 13:53:04 -07002871 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2872 qc->tf.lbah = 0;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002873 }
Jeff Garzika939c962005-10-05 17:09:16 -04002874 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2875
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002876 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002877
Tejun Heo8e0e6942006-03-31 20:41:11 +09002878 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002879
2880 DPRINTK("EXIT\n");
2881}
2882
Christoph Hellwigaa18da82017-01-10 09:41:43 +01002883/*
2884 * ATAPI devices typically report zero for their SCSI version, and sometimes
2885 * deviate from the spec WRT response data format. If SCSI version is
2886 * reported as zero like normal, then we make the following fixups:
2887 * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a
2888 * modern device.
2889 * 2) Ensure response data format / ATAPI information are always correct.
2890 */
2891static void atapi_fixup_inquiry(struct scsi_cmnd *cmd)
2892{
2893 u8 buf[4];
2894
2895 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2896 if (buf[2] == 0) {
2897 buf[2] = 0x5;
2898 buf[3] = 0x32;
2899 }
2900 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2901}
2902
Tejun Heo77853bf2006-01-23 13:09:36 +09002903static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
2905 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002906 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Jeff Garzika7dac442005-10-30 04:44:42 -05002908 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002909
Tejun Heo246619d2006-05-15 20:58:16 +09002910 /* handle completion from new EH */
2911 if (unlikely(qc->ap->ops->error_handler &&
2912 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2913
2914 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2915 /* FIXME: not quite right; we don't want the
2916 * translation of taskfile registers into a
2917 * sense descriptors, since that's only
2918 * correct for ATA, not ATAPI
2919 */
Tejun Heo750426a2006-11-14 22:37:35 +09002920 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002921 }
2922
Tejun Heo22aac082006-08-08 14:08:59 +09002923 /* SCSI EH automatically locks door if sdev->locked is
2924 * set. Sometimes door lock request continues to
2925 * fail, for example, when no media is present. This
2926 * creates a loop - SCSI EH issues door lock which
2927 * fails and gets invoked again to acquire sense data
2928 * for the failed command.
2929 *
2930 * If door lock fails, always clear sdev->locked to
2931 * avoid this infinite loop.
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002932 *
2933 * This may happen before SCSI scan is complete. Make
2934 * sure qc->dev->sdev isn't NULL before dereferencing.
Tejun Heo22aac082006-08-08 14:08:59 +09002935 */
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002936 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
Tejun Heo22aac082006-08-08 14:08:59 +09002937 qc->dev->sdev->locked = 0;
2938
Tejun Heo246619d2006-05-15 20:58:16 +09002939 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002940 ata_qc_done(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002941 return;
2942 }
2943
2944 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002945 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002947 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002948 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002949 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002950 /* FIXME: not quite right; we don't want the
2951 * translation of taskfile registers into
2952 * a sense descriptors, since that's only
2953 * correct for ATA, not ATAPI
2954 */
Tejun Heo750426a2006-11-14 22:37:35 +09002955 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002956 } else {
Christoph Hellwigaa18da82017-01-10 09:41:43 +01002957 if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0)
2958 atapi_fixup_inquiry(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 cmd->result = SAM_STAT_GOOD;
2960 }
2961
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002962 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964/**
2965 * atapi_xlat - Initialize PACKET taskfile
2966 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 *
2968 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002969 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 *
2971 * RETURNS:
2972 * Zero on success, non-zero on failure.
2973 */
Tejun Heoad706992006-12-17 10:45:57 +09002974static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Tejun Heo542b1442006-12-17 10:45:08 +09002976 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 struct ata_device *dev = qc->dev;
Tejun Heo542b1442006-12-17 10:45:08 +09002978 int nodata = (scmd->sc_data_direction == DMA_NONE);
Tejun Heo5895ef92008-06-17 12:36:26 +09002979 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
Alan Cox2db78dd2007-10-02 13:53:04 -07002980 unsigned int nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Tejun Heo2e5704f2006-12-17 10:46:33 +09002982 memset(qc->cdb, 0, dev->cdb_len);
2983 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 qc->complete_fn = atapi_qc_complete;
2986
2987 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002988 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 qc->tf.flags |= ATA_TFLAG_WRITE;
2990 DPRINTK("direction: write\n");
2991 }
2992
2993 qc->tf.command = ATA_CMD_PACKET;
Tejun Heoaacda372008-03-18 17:47:43 +09002994 ata_qc_set_pc_nbytes(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002996 /* check whether ATAPI DMA is safe */
Tejun Heo5895ef92008-06-17 12:36:26 +09002997 if (!nodata && !using_pio && atapi_check_dma(qc))
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002998 using_pio = 1;
2999
Tejun Heoe1902222007-11-26 20:58:02 +09003000 /* Some controller variants snoop this value for Packet
3001 * transfers to do state machine and FIFO management. Thus we
3002 * want to set it properly, and for DMA where it is
3003 * effectively meaningless.
3004 */
Tejun Heoaacda372008-03-18 17:47:43 +09003005 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
Alan Cox2db78dd2007-10-02 13:53:04 -07003006
Tejun Heoe1902222007-11-26 20:58:02 +09003007 /* Most ATAPI devices which honor transfer chunk size don't
3008 * behave according to the spec when odd chunk size which
3009 * matches the transfer length is specified. If the number of
3010 * bytes to transfer is 2n+1. According to the spec, what
3011 * should happen is to indicate that 2n+1 is going to be
3012 * transferred and transfer 2n+2 bytes where the last byte is
3013 * padding.
3014 *
3015 * In practice, this doesn't happen. ATAPI devices first
3016 * indicate and transfer 2n bytes and then indicate and
3017 * transfer 2 bytes where the last byte is padding.
3018 *
3019 * This inconsistency confuses several controllers which
3020 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
3021 * These controllers use actual number of transferred bytes to
3022 * update DMA poitner and transfer of 4n+2 bytes make those
3023 * controller push DMA pointer by 4n+4 bytes because SATA data
3024 * FISes are aligned to 4 bytes. This causes data corruption
3025 * and buffer overrun.
3026 *
3027 * Always setting nbytes to even number solves this problem
3028 * because then ATAPI devices don't have to split data at 2n
3029 * boundaries.
3030 */
3031 if (nbytes & 0x1)
3032 nbytes++;
3033
Alan Cox2db78dd2007-10-02 13:53:04 -07003034 qc->tf.lbam = (nbytes & 0xFF);
3035 qc->tf.lbah = (nbytes >> 8);
3036
Tejun Heo5895ef92008-06-17 12:36:26 +09003037 if (nodata)
3038 qc->tf.protocol = ATAPI_PROT_NODATA;
3039 else if (using_pio)
3040 qc->tf.protocol = ATAPI_PROT_PIO;
3041 else {
Tejun Heoe00f1ff2007-06-27 02:47:35 +09003042 /* DMA data xfer */
Tejun Heo0dc36882007-12-18 16:34:43 -05003043 qc->tf.protocol = ATAPI_PROT_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 qc->tf.feature |= ATAPI_PKT_DMA;
3045
Tejun Heo91163002008-02-21 13:25:50 +09003046 if ((dev->flags & ATA_DFLAG_DMADIR) &&
3047 (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08003048 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
3051
Alan Cox2db78dd2007-10-02 13:53:04 -07003052
3053 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
3054 as ATAPI tape drives don't get this right otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return 0;
3056}
3057
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003058static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
Tejun Heoab5b3a52006-05-31 18:27:34 +09003059{
Tejun Heo071f44b2008-04-07 22:47:22 +09003060 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003061 if (likely(devno < ata_link_max_devices(&ap->link)))
3062 return &ap->link.device[devno];
3063 } else {
3064 if (likely(devno < ap->nr_pmp_links))
3065 return &ap->pmp_link[devno].device[0];
3066 }
3067
Tejun Heoab5b3a52006-05-31 18:27:34 +09003068 return NULL;
3069}
3070
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003071static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
3072 const struct scsi_device *scsidev)
Tejun Heoab5b3a52006-05-31 18:27:34 +09003073{
Tejun Heo41bda9c2007-08-06 18:36:24 +09003074 int devno;
Tejun Heoab5b3a52006-05-31 18:27:34 +09003075
Tejun Heo41bda9c2007-08-06 18:36:24 +09003076 /* skip commands not addressed to targets we simulate */
Tejun Heo071f44b2008-04-07 22:47:22 +09003077 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003078 if (unlikely(scsidev->channel || scsidev->lun))
3079 return NULL;
3080 devno = scsidev->id;
3081 } else {
3082 if (unlikely(scsidev->id || scsidev->lun))
3083 return NULL;
3084 devno = scsidev->channel;
3085 }
3086
3087 return ata_find_dev(ap, devno);
Tejun Heoab5b3a52006-05-31 18:27:34 +09003088}
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090/**
3091 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
3092 * @ap: ATA port to which the device is attached
3093 * @scsidev: SCSI device from which we derive the ATA device
3094 *
3095 * Given various information provided in struct scsi_cmnd,
3096 * map that onto an ATA bus, and using that mapping
3097 * determine which ata_device is associated with the
3098 * SCSI command to be sent.
3099 *
3100 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003101 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 *
3103 * RETURNS:
3104 * Associated ATA device, or %NULL if not found.
3105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04003107ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
Tejun Heoab5b3a52006-05-31 18:27:34 +09003109 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
Tejun Heo2486fa52008-07-31 07:52:40 +09003111 if (unlikely(!dev || !ata_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 return NULL;
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return dev;
3115}
3116
Jeff Garzikb0955182005-05-12 15:45:22 -04003117/*
3118 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
3119 * @byte1: Byte 1 from pass-thru CDB.
3120 *
3121 * RETURNS:
3122 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
3123 */
3124static u8
3125ata_scsi_map_proto(u8 byte1)
3126{
3127 switch((byte1 & 0x1e) >> 1) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003128 case 3: /* Non-data */
3129 return ATA_PROT_NODATA;
Jeff Garzikb0955182005-05-12 15:45:22 -04003130
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003131 case 6: /* DMA */
3132 case 10: /* UDMA Data-in */
3133 case 11: /* UDMA Data-Out */
3134 return ATA_PROT_DMA;
Jeff Garzikb0955182005-05-12 15:45:22 -04003135
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003136 case 4: /* PIO Data-in */
3137 case 5: /* PIO Data-out */
3138 return ATA_PROT_PIO;
Jeff Garzikb0955182005-05-12 15:45:22 -04003139
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303140 case 12: /* FPDMA */
3141 return ATA_PROT_NCQ;
3142
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003143 case 0: /* Hard Reset */
3144 case 1: /* SRST */
3145 case 8: /* Device Diagnostic */
3146 case 9: /* Device Reset */
3147 case 7: /* DMA Queued */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003148 case 15: /* Return Response Info */
3149 default: /* Reserved */
3150 break;
Jeff Garzikb0955182005-05-12 15:45:22 -04003151 }
3152
3153 return ATA_PROT_UNKNOWN;
3154}
3155
3156/**
3157 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
3158 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04003159 *
3160 * Handles either 12 or 16-byte versions of the CDB.
3161 *
3162 * RETURNS:
3163 * Zero on success, non-zero on failure.
3164 */
Tejun Heoad706992006-12-17 10:45:57 +09003165static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04003166{
3167 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09003168 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01003169 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09003170 const u8 *cdb = scmd->cmnd;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003171 u16 fp;
Jeff Garzikb0955182005-05-12 15:45:22 -04003172
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003173 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN) {
3174 fp = 1;
Tejun Heo9a405252005-12-02 11:49:11 +09003175 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003176 }
Jeff Garzik8190bdb2006-05-24 01:53:39 -04003177
Hannes Reinecke5b844b62016-07-14 09:05:48 +09003178 if (ata_is_ncq(tf->protocol) && (cdb[2] & 0x3) == 0)
3179 tf->protocol = ATA_PROT_NCQ_NODATA;
3180
Vinayak Kale5f8e7f12015-10-27 13:11:46 +05303181 /* enable LBA */
3182 tf->flags |= ATA_TFLAG_LBA;
3183
Alan Coxae8d4ee2007-11-04 22:05:49 -05003184 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04003185 * 12 and 16 byte CDBs use different offsets to
3186 * provide the various register values.
3187 */
Tejun Heo542b1442006-12-17 10:45:08 +09003188 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04003189 /*
3190 * 16-byte CDB - may contain extended commands.
3191 *
3192 * If that is the case, copy the upper byte register values.
3193 */
Tejun Heo542b1442006-12-17 10:45:08 +09003194 if (cdb[1] & 0x01) {
3195 tf->hob_feature = cdb[3];
3196 tf->hob_nsect = cdb[5];
3197 tf->hob_lbal = cdb[7];
3198 tf->hob_lbam = cdb[9];
3199 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04003200 tf->flags |= ATA_TFLAG_LBA48;
3201 } else
3202 tf->flags &= ~ATA_TFLAG_LBA48;
3203
3204 /*
3205 * Always copy low byte, device and command registers.
3206 */
Tejun Heo542b1442006-12-17 10:45:08 +09003207 tf->feature = cdb[4];
3208 tf->nsect = cdb[6];
3209 tf->lbal = cdb[8];
3210 tf->lbam = cdb[10];
3211 tf->lbah = cdb[12];
3212 tf->device = cdb[13];
3213 tf->command = cdb[14];
Jeff Garzikb0955182005-05-12 15:45:22 -04003214 } else {
3215 /*
3216 * 12-byte CDB - incapable of extended commands.
3217 */
3218 tf->flags &= ~ATA_TFLAG_LBA48;
3219
Tejun Heo542b1442006-12-17 10:45:08 +09003220 tf->feature = cdb[3];
3221 tf->nsect = cdb[4];
3222 tf->lbal = cdb[5];
3223 tf->lbam = cdb[6];
3224 tf->lbah = cdb[7];
3225 tf->device = cdb[8];
3226 tf->command = cdb[9];
Jeff Garzikb0955182005-05-12 15:45:22 -04003227 }
Albert Leefa4453c2007-06-07 15:52:07 +08003228
Hannes Reinecke179b3102016-07-14 09:05:43 +09003229 /* For NCQ commands copy the tag value */
3230 if (ata_is_ncq(tf->protocol))
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303231 tf->nsect = qc->tag << 3;
3232
Albert Leefa4453c2007-06-07 15:52:07 +08003233 /* enforce correct master/slave bit */
3234 tf->device = dev->devno ?
3235 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003236
Tejun Heobd30add2009-09-03 16:08:11 +09003237 switch (tf->command) {
Grant Grundler295124d2010-08-17 10:56:53 -07003238 /* READ/WRITE LONG use a non-standard sect_size */
Tejun Heobd30add2009-09-03 16:08:11 +09003239 case ATA_CMD_READ_LONG:
3240 case ATA_CMD_READ_LONG_ONCE:
3241 case ATA_CMD_WRITE_LONG:
3242 case ATA_CMD_WRITE_LONG_ONCE:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003243 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
3244 fp = 1;
Tejun Heobd30add2009-09-03 16:08:11 +09003245 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003246 }
Tejun Heobd30add2009-09-03 16:08:11 +09003247 qc->sect_size = scsi_bufflen(scmd);
Grant Grundler295124d2010-08-17 10:56:53 -07003248 break;
3249
3250 /* commands using reported Logical Block size (e.g. 512 or 4K) */
3251 case ATA_CMD_CFA_WRITE_NE:
3252 case ATA_CMD_CFA_TRANS_SECT:
3253 case ATA_CMD_CFA_WRITE_MULT_NE:
3254 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
3255 case ATA_CMD_READ:
3256 case ATA_CMD_READ_EXT:
3257 case ATA_CMD_READ_QUEUED:
3258 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
3259 case ATA_CMD_FPDMA_READ:
3260 case ATA_CMD_READ_MULTI:
3261 case ATA_CMD_READ_MULTI_EXT:
3262 case ATA_CMD_PIO_READ:
3263 case ATA_CMD_PIO_READ_EXT:
3264 case ATA_CMD_READ_STREAM_DMA_EXT:
3265 case ATA_CMD_READ_STREAM_EXT:
3266 case ATA_CMD_VERIFY:
3267 case ATA_CMD_VERIFY_EXT:
3268 case ATA_CMD_WRITE:
3269 case ATA_CMD_WRITE_EXT:
3270 case ATA_CMD_WRITE_FUA_EXT:
3271 case ATA_CMD_WRITE_QUEUED:
3272 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3273 case ATA_CMD_FPDMA_WRITE:
3274 case ATA_CMD_WRITE_MULTI:
3275 case ATA_CMD_WRITE_MULTI_EXT:
3276 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3277 case ATA_CMD_PIO_WRITE:
3278 case ATA_CMD_PIO_WRITE_EXT:
3279 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3280 case ATA_CMD_WRITE_STREAM_EXT:
3281 qc->sect_size = scmd->device->sector_size;
3282 break;
3283
3284 /* Everything else uses 512 byte "sectors" */
3285 default:
3286 qc->sect_size = ATA_SECT_SIZE;
Tejun Heobd30add2009-09-03 16:08:11 +09003287 }
3288
3289 /*
3290 * Set flags so that all registers will be written, pass on
3291 * write indication (used for PIO/DMA setup), result TF is
3292 * copied back and we don't whine too much about its failure.
3293 */
Douglas Gilbertbc496ed2010-02-01 13:11:38 -05003294 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heobd30add2009-09-03 16:08:11 +09003295 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3296 tf->flags |= ATA_TFLAG_WRITE;
3297
3298 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3299
3300 /*
3301 * Set transfer length.
3302 *
3303 * TODO: find out if we need to do more here to
3304 * cover scatter/gather case.
3305 */
3306 ata_qc_set_pc_nbytes(qc);
3307
3308 /* We may not issue DMA commands if no DMA mode is set */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003309 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) {
3310 fp = 1;
Tejun Heobd30add2009-09-03 16:08:11 +09003311 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003312 }
Tejun Heobd30add2009-09-03 16:08:11 +09003313
Albert Lee1dce5892007-06-07 15:49:22 +08003314 /* sanity check for pio multi commands */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003315 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
3316 fp = 1;
Albert Lee1dce5892007-06-07 15:49:22 +08003317 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003318 }
Albert Lee1dce5892007-06-07 15:49:22 +08003319
3320 if (is_multi_taskfile(tf)) {
3321 unsigned int multi_count = 1 << (cdb[1] >> 5);
3322
3323 /* compare the passed through multi_count
3324 * with the cached multi_count of libata
3325 */
3326 if (multi_count != dev->multi_count)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003327 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3328 multi_count);
Alan Coxd26fc952007-07-06 19:13:52 -04003329 }
Albert Lee1dce5892007-06-07 15:49:22 +08003330
Jeff Garzikb0955182005-05-12 15:45:22 -04003331 /*
3332 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3333 * SET_FEATURES - XFER MODE must be preceded/succeeded
3334 * by an update to hardware-specific registers for each
3335 * controller (i.e. the reason for ->set_piomode(),
3336 * ->set_dmamode(), and ->post_set_mode() hooks).
3337 */
Tejun Heobd30add2009-09-03 16:08:11 +09003338 if (tf->command == ATA_CMD_SET_FEATURES &&
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003339 tf->feature == SETFEATURES_XFER) {
3340 fp = (cdb[0] == ATA_16) ? 4 : 3;
Tejun Heo9a405252005-12-02 11:49:11 +09003341 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003342 }
Jeff Garzikb0955182005-05-12 15:45:22 -04003343
3344 /*
Tejun Heobd30add2009-09-03 16:08:11 +09003345 * Filter TPM commands by default. These provide an
3346 * essentially uncontrolled encrypted "back door" between
3347 * applications and the disk. Set libata.allow_tpm=1 if you
3348 * have a real reason for wanting to use them. This ensures
3349 * that installed software cannot easily mess stuff up without
3350 * user intent. DVR type users will probably ship with this enabled
3351 * for movie content management.
Jeff Garzikb0955182005-05-12 15:45:22 -04003352 *
Tejun Heobd30add2009-09-03 16:08:11 +09003353 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3354 * for this and should do in future but that it is not sufficient as
3355 * DCS is an optional feature set. Thus we also do the software filter
3356 * so that we comply with the TC consortium stated goal that the user
3357 * can turn off TC features of their system.
Jeff Garzikb0955182005-05-12 15:45:22 -04003358 */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003359 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
3360 fp = (cdb[0] == ATA_16) ? 14 : 9;
Tejun Heobd30add2009-09-03 16:08:11 +09003361 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003362 }
Tejun Heoe61e0672006-05-15 20:57:40 +09003363
Jeff Garzikb0955182005-05-12 15:45:22 -04003364 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09003365
3366 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003367 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
Tejun Heo9a405252005-12-02 11:49:11 +09003368 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003369}
3370
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003371/**
3372 * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim
3373 * @cmd: SCSI command being translated
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003374 * @trmax: Maximum number of entries that will fit in sector_size bytes.
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003375 * @sector: Starting sector
Shaun Tancheff7b203092016-08-21 23:23:19 -05003376 * @count: Total Range of request in logical sectors
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003377 *
3378 * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted
3379 * descriptor.
3380 *
3381 * Upto 64 entries of the format:
3382 * 63:48 Range Length
3383 * 47:0 LBA
3384 *
3385 * Range Length of 0 is ignored.
3386 * LBA's should be sorted order and not overlap.
3387 *
3388 * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003389 *
3390 * Return: Number of bytes copied into sglist.
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003391 */
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003392static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax,
3393 u64 sector, u32 count)
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003394{
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003395 struct scsi_device *sdp = cmd->device;
3396 size_t len = sdp->sector_size;
3397 size_t r;
3398 __le64 *buf;
3399 u32 i = 0;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003400 unsigned long flags;
3401
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003402 WARN_ON(len > ATA_SCSI_RBUF_SIZE);
3403
3404 if (len > ATA_SCSI_RBUF_SIZE)
3405 len = ATA_SCSI_RBUF_SIZE;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003406
3407 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003408 buf = ((void *)ata_scsi_rbuf);
3409 memset(buf, 0, len);
3410 while (i < trmax) {
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003411 u64 entry = sector |
3412 ((u64)(count > 0xffff ? 0xffff : count) << 48);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003413 buf[i++] = __cpu_to_le64(entry);
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003414 if (count <= 0xffff)
3415 break;
3416 count -= 0xffff;
3417 sector += 0xffff;
3418 }
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003419 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003420 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
3421
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003422 return r;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003423}
3424
Shaun Tancheff7b203092016-08-21 23:23:19 -05003425/**
3426 * ata_format_dsm_trim_descr() - SATL Write Same to ATA SCT Write Same
3427 * @cmd: SCSI command being translated
3428 * @lba: Starting sector
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003429 * @num: Number of sectors to be zero'd.
Shaun Tancheff7b203092016-08-21 23:23:19 -05003430 *
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003431 * Rewrite the WRITE SAME payload to be an SCT Write Same formatted
Shaun Tancheff7b203092016-08-21 23:23:19 -05003432 * descriptor.
3433 * NOTE: Writes a pattern (0's) in the foreground.
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003434 *
3435 * Return: Number of bytes copied into sglist.
Shaun Tancheff7b203092016-08-21 23:23:19 -05003436 */
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003437static size_t ata_format_sct_write_same(struct scsi_cmnd *cmd, u64 lba, u64 num)
Shaun Tancheff7b203092016-08-21 23:23:19 -05003438{
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003439 struct scsi_device *sdp = cmd->device;
3440 size_t len = sdp->sector_size;
3441 size_t r;
3442 u16 *buf;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003443 unsigned long flags;
3444
3445 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003446 buf = ((void *)ata_scsi_rbuf);
Shaun Tancheff7b203092016-08-21 23:23:19 -05003447
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003448 put_unaligned_le16(0x0002, &buf[0]); /* SCT_ACT_WRITE_SAME */
3449 put_unaligned_le16(0x0101, &buf[1]); /* WRITE PTRN FG */
3450 put_unaligned_le64(lba, &buf[2]);
3451 put_unaligned_le64(num, &buf[6]);
3452 put_unaligned_le32(0u, &buf[10]); /* pattern */
Shaun Tancheff7b203092016-08-21 23:23:19 -05003453
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003454 WARN_ON(len > ATA_SCSI_RBUF_SIZE);
3455
3456 if (len > ATA_SCSI_RBUF_SIZE)
3457 len = ATA_SCSI_RBUF_SIZE;
3458
3459 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
Shaun Tancheff7b203092016-08-21 23:23:19 -05003460 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003461
3462 return r;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003463}
3464
3465/**
3466 * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same
3467 * @qc: Command to be translated
3468 *
3469 * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or
3470 * an SCT Write Same command.
3471 * Based on WRITE SAME has the UNMAP flag
3472 * When set translate to DSM TRIM
3473 * When clear translate to SCT Write Same
3474 */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003475static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3476{
3477 struct ata_taskfile *tf = &qc->tf;
3478 struct scsi_cmnd *scmd = qc->scsicmd;
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003479 struct scsi_device *sdp = scmd->device;
3480 size_t len = sdp->sector_size;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003481 struct ata_device *dev = qc->dev;
3482 const u8 *cdb = scmd->cmnd;
3483 u64 block;
3484 u32 n_block;
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003485 const u32 trmax = len >> 3;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003486 u32 size;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003487 u16 fp;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003488 u8 bp = 0xff;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003489 u8 unmap = cdb[1] & 0x8;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003490
3491 /* we may not issue DMA commands if no DMA mode is set */
3492 if (unlikely(!dev->dma_mode))
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003493 goto invalid_opcode;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003494
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003495 if (unlikely(scmd->cmd_len < 16)) {
3496 fp = 15;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003497 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003498 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003499 scsi_16_lba_len(cdb, &block, &n_block);
3500
Shaun Tancheff7b203092016-08-21 23:23:19 -05003501 if (unmap) {
3502 /* If trim is not enabled the cmd is invalid. */
3503 if ((dev->horkage & ATA_HORKAGE_NOTRIM) ||
3504 !ata_id_has_trim(dev->id)) {
3505 fp = 1;
3506 bp = 3;
3507 goto invalid_fld;
3508 }
3509 /* If the request is too large the cmd is invalid */
3510 if (n_block > 0xffff * trmax) {
3511 fp = 2;
3512 goto invalid_fld;
3513 }
3514 } else {
3515 /* If write same is not available the cmd is invalid */
3516 if (!ata_id_sct_write_same(dev->id)) {
3517 fp = 1;
3518 bp = 3;
3519 goto invalid_fld;
3520 }
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003521 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003522
3523 /*
3524 * WRITE SAME always has a sector sized buffer as payload, this
3525 * should never be a multiple entry S/G list.
3526 */
3527 if (!scsi_sg_count(scmd))
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003528 goto invalid_param_len;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003529
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003530 /*
3531 * size must match sector size in bytes
3532 * For DATA SET MANAGEMENT TRIM in ACS-2 nsect (aka count)
3533 * is defined as number of 512 byte blocks to be transferred.
3534 */
Shaun Tancheff7b203092016-08-21 23:23:19 -05003535 if (unmap) {
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003536 size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003537 if (size != len)
3538 goto invalid_param_len;
3539
Shaun Tancheff7b203092016-08-21 23:23:19 -05003540 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3541 /* Newer devices support queued TRIM commands */
3542 tf->protocol = ATA_PROT_NCQ;
3543 tf->command = ATA_CMD_FPDMA_SEND;
3544 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
3545 tf->nsect = qc->tag << 3;
3546 tf->hob_feature = (size / 512) >> 8;
3547 tf->feature = size / 512;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003548
Shaun Tancheff7b203092016-08-21 23:23:19 -05003549 tf->auxiliary = 1;
3550 } else {
3551 tf->protocol = ATA_PROT_DMA;
3552 tf->hob_feature = 0;
3553 tf->feature = ATA_DSM_TRIM;
3554 tf->hob_nsect = (size / 512) >> 8;
3555 tf->nsect = size / 512;
3556 tf->command = ATA_CMD_DSM;
3557 }
Marc Carino87fb6c32013-08-24 23:22:51 -07003558 } else {
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003559 size = ata_format_sct_write_same(scmd, block, n_block);
3560 if (size != len)
3561 goto invalid_param_len;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003562
Marc Carino87fb6c32013-08-24 23:22:51 -07003563 tf->hob_feature = 0;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003564 tf->feature = 0;
3565 tf->hob_nsect = 0;
3566 tf->nsect = 1;
3567 tf->lbah = 0;
3568 tf->lbam = 0;
3569 tf->lbal = ATA_CMD_STANDBYNOW1;
3570 tf->hob_lbah = 0;
3571 tf->hob_lbam = 0;
3572 tf->hob_lbal = 0;
3573 tf->device = ATA_CMD_STANDBYNOW1;
3574 tf->protocol = ATA_PROT_DMA;
3575 tf->command = ATA_CMD_WRITE_LOG_DMA_EXT;
Shaun Tancheff7d36dd02016-08-21 23:23:21 -05003576 if (unlikely(dev->flags & ATA_DFLAG_PIO))
3577 tf->command = ATA_CMD_WRITE_LOG_EXT;
Marc Carino87fb6c32013-08-24 23:22:51 -07003578 }
3579
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003580 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3581 ATA_TFLAG_WRITE;
3582
3583 ata_qc_set_pc_nbytes(qc);
3584
3585 return 0;
3586
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003587invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003588 ata_scsi_set_invalid_field(dev, scmd, fp, bp);
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003589 return 1;
3590invalid_param_len:
3591 /* "Parameter list length error" */
3592 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3593 return 1;
3594invalid_opcode:
3595 /* "Invalid command operation code" */
3596 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003597 return 1;
3598}
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600/**
Shaun Tancheff7b203092016-08-21 23:23:19 -05003601 * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN
3602 * @args: device MAINTENANCE_IN data / SCSI command of interest.
3603 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
3604 *
3605 * Yields a subset to satisfy scsi_report_opcode()
3606 *
3607 * LOCKING:
3608 * spin_lock_irqsave(host lock)
3609 */
3610static unsigned int ata_scsiop_maint_in(struct ata_scsi_args *args, u8 *rbuf)
3611{
3612 struct ata_device *dev = args->dev;
3613 u8 *cdb = args->cmd->cmnd;
3614 u8 supported = 0;
3615 unsigned int err = 0;
3616
3617 if (cdb[2] != 1) {
3618 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
3619 err = 2;
3620 goto out;
3621 }
3622 switch (cdb[3]) {
3623 case INQUIRY:
3624 case MODE_SENSE:
3625 case MODE_SENSE_10:
3626 case READ_CAPACITY:
3627 case SERVICE_ACTION_IN_16:
3628 case REPORT_LUNS:
3629 case REQUEST_SENSE:
3630 case SYNCHRONIZE_CACHE:
3631 case REZERO_UNIT:
3632 case SEEK_6:
3633 case SEEK_10:
3634 case TEST_UNIT_READY:
3635 case SEND_DIAGNOSTIC:
3636 case MAINTENANCE_IN:
3637 case READ_6:
3638 case READ_10:
3639 case READ_16:
3640 case WRITE_6:
3641 case WRITE_10:
3642 case WRITE_16:
3643 case ATA_12:
3644 case ATA_16:
3645 case VERIFY:
3646 case VERIFY_16:
3647 case MODE_SELECT:
3648 case MODE_SELECT_10:
3649 case START_STOP:
3650 supported = 3;
3651 break;
3652 case WRITE_SAME_16:
Shaun Tancheff0ce1b182016-09-09 11:44:19 -05003653 if (!ata_id_sct_write_same(dev->id))
3654 break;
3655 /* fallthrough: if SCT ... only enable for ZBC */
Shaun Tancheff7b203092016-08-21 23:23:19 -05003656 case ZBC_IN:
3657 case ZBC_OUT:
3658 if (ata_id_zoned_cap(dev->id) ||
3659 dev->class == ATA_DEV_ZAC)
3660 supported = 3;
3661 break;
3662 default:
3663 break;
3664 }
3665out:
3666 rbuf[1] = supported; /* supported */
3667 return err;
3668}
3669
3670/**
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003671 * ata_scsi_report_zones_complete - convert ATA output
3672 * @qc: command structure returning the data
3673 *
3674 * Convert T-13 little-endian field representation into
3675 * T-10 big-endian field representation.
3676 * What a mess.
3677 */
3678static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
3679{
3680 struct scsi_cmnd *scmd = qc->scsicmd;
3681 struct sg_mapping_iter miter;
3682 unsigned long flags;
3683 unsigned int bytes = 0;
3684
3685 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
3686 SG_MITER_TO_SG | SG_MITER_ATOMIC);
3687
3688 local_irq_save(flags);
3689 while (sg_miter_next(&miter)) {
3690 unsigned int offset = 0;
3691
3692 if (bytes == 0) {
3693 char *hdr;
3694 u32 list_length;
3695 u64 max_lba, opt_lba;
3696 u16 same;
3697
3698 /* Swizzle header */
3699 hdr = miter.addr;
3700 list_length = get_unaligned_le32(&hdr[0]);
3701 same = get_unaligned_le16(&hdr[4]);
3702 max_lba = get_unaligned_le64(&hdr[8]);
3703 opt_lba = get_unaligned_le64(&hdr[16]);
3704 put_unaligned_be32(list_length, &hdr[0]);
3705 hdr[4] = same & 0xf;
3706 put_unaligned_be64(max_lba, &hdr[8]);
3707 put_unaligned_be64(opt_lba, &hdr[16]);
3708 offset += 64;
3709 bytes += 64;
3710 }
3711 while (offset < miter.length) {
3712 char *rec;
3713 u8 cond, type, non_seq, reset;
3714 u64 size, start, wp;
3715
3716 /* Swizzle zone descriptor */
3717 rec = miter.addr + offset;
3718 type = rec[0] & 0xf;
3719 cond = (rec[1] >> 4) & 0xf;
3720 non_seq = (rec[1] & 2);
3721 reset = (rec[1] & 1);
3722 size = get_unaligned_le64(&rec[8]);
3723 start = get_unaligned_le64(&rec[16]);
3724 wp = get_unaligned_le64(&rec[24]);
3725 rec[0] = type;
3726 rec[1] = (cond << 4) | non_seq | reset;
3727 put_unaligned_be64(size, &rec[8]);
3728 put_unaligned_be64(start, &rec[16]);
3729 put_unaligned_be64(wp, &rec[24]);
3730 WARN_ON(offset + 64 > miter.length);
3731 offset += 64;
3732 bytes += 64;
3733 }
3734 }
3735 sg_miter_stop(&miter);
3736 local_irq_restore(flags);
3737
3738 ata_scsi_qc_complete(qc);
3739}
3740
3741static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
3742{
3743 struct ata_taskfile *tf = &qc->tf;
3744 struct scsi_cmnd *scmd = qc->scsicmd;
3745 const u8 *cdb = scmd->cmnd;
3746 u16 sect, fp = (u16)-1;
3747 u8 sa, options, bp = 0xff;
3748 u64 block;
3749 u32 n_block;
3750
3751 if (unlikely(scmd->cmd_len < 16)) {
3752 ata_dev_warn(qc->dev, "invalid cdb length %d\n",
3753 scmd->cmd_len);
3754 fp = 15;
3755 goto invalid_fld;
3756 }
3757 scsi_16_lba_len(cdb, &block, &n_block);
3758 if (n_block != scsi_bufflen(scmd)) {
3759 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
3760 n_block, scsi_bufflen(scmd));
3761 goto invalid_param_len;
3762 }
3763 sa = cdb[1] & 0x1f;
3764 if (sa != ZI_REPORT_ZONES) {
3765 ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
3766 fp = 1;
3767 goto invalid_fld;
3768 }
3769 /*
3770 * ZAC allows only for transfers in 512 byte blocks,
3771 * and uses a 16 bit value for the transfer count.
3772 */
3773 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
3774 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
3775 goto invalid_param_len;
3776 }
3777 sect = n_block / 512;
Damien Le Moal3f174222016-07-14 09:05:49 +09003778 options = cdb[14] & 0xbf;
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003779
3780 if (ata_ncq_enabled(qc->dev) &&
3781 ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
3782 tf->protocol = ATA_PROT_NCQ;
3783 tf->command = ATA_CMD_FPDMA_RECV;
3784 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
3785 tf->nsect = qc->tag << 3;
3786 tf->feature = sect & 0xff;
3787 tf->hob_feature = (sect >> 8) & 0xff;
Damien Le Moal3f174222016-07-14 09:05:49 +09003788 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003789 } else {
3790 tf->command = ATA_CMD_ZAC_MGMT_IN;
3791 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
3792 tf->protocol = ATA_PROT_DMA;
3793 tf->hob_feature = options;
3794 tf->hob_nsect = (sect >> 8) & 0xff;
3795 tf->nsect = sect & 0xff;
3796 }
3797 tf->device = ATA_LBA;
3798 tf->lbah = (block >> 16) & 0xff;
3799 tf->lbam = (block >> 8) & 0xff;
3800 tf->lbal = block & 0xff;
3801 tf->hob_lbah = (block >> 40) & 0xff;
3802 tf->hob_lbam = (block >> 32) & 0xff;
3803 tf->hob_lbal = (block >> 24) & 0xff;
3804
3805 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3806 qc->flags |= ATA_QCFLAG_RESULT_TF;
3807
3808 ata_qc_set_pc_nbytes(qc);
3809
3810 qc->complete_fn = ata_scsi_report_zones_complete;
3811
3812 return 0;
3813
3814invalid_fld:
3815 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
3816 return 1;
3817
3818invalid_param_len:
3819 /* "Parameter list length error" */
3820 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3821 return 1;
3822}
3823
Hannes Reinecke27708a92016-04-25 12:45:53 +02003824static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
3825{
3826 struct ata_taskfile *tf = &qc->tf;
3827 struct scsi_cmnd *scmd = qc->scsicmd;
3828 struct ata_device *dev = qc->dev;
3829 const u8 *cdb = scmd->cmnd;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003830 u8 all, sa;
Hannes Reinecke27708a92016-04-25 12:45:53 +02003831 u64 block;
3832 u32 n_block;
3833 u16 fp = (u16)-1;
3834
3835 if (unlikely(scmd->cmd_len < 16)) {
3836 fp = 15;
3837 goto invalid_fld;
3838 }
3839
3840 sa = cdb[1] & 0x1f;
3841 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
3842 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
3843 fp = 1;
3844 goto invalid_fld;
3845 }
3846
3847 scsi_16_lba_len(cdb, &block, &n_block);
3848 if (n_block) {
3849 /*
3850 * ZAC MANAGEMENT OUT doesn't define any length
3851 */
3852 goto invalid_param_len;
3853 }
3854 if (block > dev->n_sectors)
3855 goto out_of_range;
3856
Damien Le Moal2950cef2016-07-14 09:05:51 +09003857 all = cdb[14] & 0x1;
Hannes Reinecke27708a92016-04-25 12:45:53 +02003858
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003859 if (ata_ncq_enabled(qc->dev) &&
3860 ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
Hannes Reinecke5b844b62016-07-14 09:05:48 +09003861 tf->protocol = ATA_PROT_NCQ_NODATA;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003862 tf->command = ATA_CMD_NCQ_NON_DATA;
Damien Le Moalee194b72016-07-14 09:05:50 +09003863 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003864 tf->nsect = qc->tag << 3;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003865 tf->auxiliary = sa | ((u16)all << 8);
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003866 } else {
3867 tf->protocol = ATA_PROT_NODATA;
3868 tf->command = ATA_CMD_ZAC_MGMT_OUT;
3869 tf->feature = sa;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003870 tf->hob_feature = all;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003871 }
Hannes Reinecke27708a92016-04-25 12:45:53 +02003872 tf->lbah = (block >> 16) & 0xff;
3873 tf->lbam = (block >> 8) & 0xff;
3874 tf->lbal = block & 0xff;
3875 tf->hob_lbah = (block >> 40) & 0xff;
3876 tf->hob_lbam = (block >> 32) & 0xff;
3877 tf->hob_lbal = (block >> 24) & 0xff;
3878 tf->device = ATA_LBA;
3879 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3880
3881 return 0;
3882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 invalid_fld:
Hannes Reinecke27708a92016-04-25 12:45:53 +02003884 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
3885 return 1;
3886 out_of_range:
3887 /* "Logical Block Address out of range" */
3888 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x00);
3889 return 1;
3890invalid_param_len:
3891 /* "Parameter list length error" */
3892 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 return 1;
3894}
3895
3896/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003897 * ata_mselect_caching - Simulate MODE SELECT for caching info page
3898 * @qc: Storage for translated ATA taskfile
3899 * @buf: input buffer
3900 * @len: number of valid bytes in the input buffer
Hannes Reinecke77800812016-04-04 11:44:07 +02003901 * @fp: out parameter for the failed field on error
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003902 *
3903 * Prepare a taskfile to modify caching information for the device.
3904 *
3905 * LOCKING:
3906 * None.
3907 */
3908static int ata_mselect_caching(struct ata_queued_cmd *qc,
Hannes Reinecke77800812016-04-04 11:44:07 +02003909 const u8 *buf, int len, u16 *fp)
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003910{
3911 struct ata_taskfile *tf = &qc->tf;
3912 struct ata_device *dev = qc->dev;
Tom Yand7372cb2016-07-23 02:34:08 +08003913 u8 mpage[CACHE_MPAGE_LEN];
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003914 u8 wce;
Hannes Reinecke77800812016-04-04 11:44:07 +02003915 int i;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003916
3917 /*
3918 * The first two bytes of def_cache_mpage are a header, so offsets
3919 * in mpage are off by 2 compared to buf. Same for len.
3920 */
3921
Hannes Reinecke77800812016-04-04 11:44:07 +02003922 if (len != CACHE_MPAGE_LEN - 2) {
3923 if (len < CACHE_MPAGE_LEN - 2)
3924 *fp = len;
3925 else
3926 *fp = CACHE_MPAGE_LEN - 2;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003927 return -EINVAL;
Hannes Reinecke77800812016-04-04 11:44:07 +02003928 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003929
3930 wce = buf[0] & (1 << 2);
3931
3932 /*
3933 * Check that read-only bits are not modified.
3934 */
3935 ata_msense_caching(dev->id, mpage, false);
Hannes Reinecke77800812016-04-04 11:44:07 +02003936 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
3937 if (i == 0)
3938 continue;
3939 if (mpage[i + 2] != buf[i]) {
3940 *fp = i;
3941 return -EINVAL;
3942 }
3943 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003944
3945 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3946 tf->protocol = ATA_PROT_NODATA;
3947 tf->nsect = 0;
3948 tf->command = ATA_CMD_SET_FEATURES;
3949 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3950 return 0;
3951}
3952
3953/**
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003954 * ata_mselect_control - Simulate MODE SELECT for control page
3955 * @qc: Storage for translated ATA taskfile
3956 * @buf: input buffer
3957 * @len: number of valid bytes in the input buffer
Hannes Reinecke77800812016-04-04 11:44:07 +02003958 * @fp: out parameter for the failed field on error
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003959 *
3960 * Prepare a taskfile to modify caching information for the device.
3961 *
3962 * LOCKING:
3963 * None.
3964 */
3965static int ata_mselect_control(struct ata_queued_cmd *qc,
Hannes Reinecke77800812016-04-04 11:44:07 +02003966 const u8 *buf, int len, u16 *fp)
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003967{
3968 struct ata_device *dev = qc->dev;
Tom Yand7372cb2016-07-23 02:34:08 +08003969 u8 mpage[CONTROL_MPAGE_LEN];
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003970 u8 d_sense;
Hannes Reinecke77800812016-04-04 11:44:07 +02003971 int i;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003972
3973 /*
3974 * The first two bytes of def_control_mpage are a header, so offsets
3975 * in mpage are off by 2 compared to buf. Same for len.
3976 */
3977
Hannes Reinecke77800812016-04-04 11:44:07 +02003978 if (len != CONTROL_MPAGE_LEN - 2) {
3979 if (len < CONTROL_MPAGE_LEN - 2)
3980 *fp = len;
3981 else
3982 *fp = CONTROL_MPAGE_LEN - 2;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003983 return -EINVAL;
Hannes Reinecke77800812016-04-04 11:44:07 +02003984 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003985
3986 d_sense = buf[0] & (1 << 2);
3987
3988 /*
3989 * Check that read-only bits are not modified.
3990 */
Tom Yanf086b742016-07-13 02:54:12 +08003991 ata_msense_control(dev, mpage, false);
Hannes Reinecke77800812016-04-04 11:44:07 +02003992 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
3993 if (i == 0)
3994 continue;
3995 if (mpage[2 + i] != buf[i]) {
3996 *fp = i;
3997 return -EINVAL;
3998 }
3999 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004000 if (d_sense & (1 << 2))
4001 dev->flags |= ATA_DFLAG_D_SENSE;
4002 else
4003 dev->flags &= ~ATA_DFLAG_D_SENSE;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004004 return 0;
4005}
4006
4007/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004008 * ata_scsiop_mode_select - Simulate MODE SELECT 6, 10 commands
4009 * @qc: Storage for translated ATA taskfile
4010 *
4011 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
4012 * Assume this is invoked for direct access devices (e.g. disks) only.
4013 * There should be no block descriptor for other device types.
4014 *
4015 * LOCKING:
4016 * spin_lock_irqsave(host lock)
4017 */
4018static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
4019{
4020 struct scsi_cmnd *scmd = qc->scsicmd;
4021 const u8 *cdb = scmd->cmnd;
4022 const u8 *p;
4023 u8 pg, spg;
4024 unsigned six_byte, pg_len, hdr_len, bd_len;
4025 int len;
Hannes Reinecke77800812016-04-04 11:44:07 +02004026 u16 fp = (u16)-1;
4027 u8 bp = 0xff;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004028
4029 VPRINTK("ENTER\n");
4030
4031 six_byte = (cdb[0] == MODE_SELECT);
4032 if (six_byte) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004033 if (scmd->cmd_len < 5) {
4034 fp = 4;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004035 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004036 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004037
4038 len = cdb[4];
4039 hdr_len = 4;
4040 } else {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004041 if (scmd->cmd_len < 9) {
4042 fp = 8;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004043 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004044 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004045
4046 len = (cdb[7] << 8) + cdb[8];
4047 hdr_len = 8;
4048 }
4049
4050 /* We only support PF=1, SP=0. */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004051 if ((cdb[1] & 0x11) != 0x10) {
4052 fp = 1;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02004053 bp = (cdb[1] & 0x01) ? 1 : 5;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004054 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004055 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004056
4057 /* Test early for possible overrun. */
4058 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
4059 goto invalid_param_len;
4060
4061 p = page_address(sg_page(scsi_sglist(scmd)));
4062
4063 /* Move past header and block descriptors. */
4064 if (len < hdr_len)
4065 goto invalid_param_len;
4066
4067 if (six_byte)
4068 bd_len = p[3];
4069 else
4070 bd_len = (p[6] << 8) + p[7];
4071
4072 len -= hdr_len;
4073 p += hdr_len;
4074 if (len < bd_len)
4075 goto invalid_param_len;
Hannes Reinecke77800812016-04-04 11:44:07 +02004076 if (bd_len != 0 && bd_len != 8) {
4077 fp = (six_byte) ? 3 : 6;
4078 fp += bd_len + hdr_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004079 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004080 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004081
4082 len -= bd_len;
4083 p += bd_len;
4084 if (len == 0)
4085 goto skip;
4086
4087 /* Parse both possible formats for the mode page headers. */
4088 pg = p[0] & 0x3f;
4089 if (p[0] & 0x40) {
4090 if (len < 4)
4091 goto invalid_param_len;
4092
4093 spg = p[1];
4094 pg_len = (p[2] << 8) | p[3];
4095 p += 4;
4096 len -= 4;
4097 } else {
4098 if (len < 2)
4099 goto invalid_param_len;
4100
4101 spg = 0;
4102 pg_len = p[1];
4103 p += 2;
4104 len -= 2;
4105 }
4106
4107 /*
4108 * No mode subpages supported (yet) but asking for _all_
4109 * subpages may be valid
4110 */
Hannes Reinecke77800812016-04-04 11:44:07 +02004111 if (spg && (spg != ALL_SUB_MPAGES)) {
4112 fp = (p[0] & 0x40) ? 1 : 0;
4113 fp += hdr_len + bd_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004114 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004115 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004116 if (pg_len > len)
4117 goto invalid_param_len;
4118
4119 switch (pg) {
4120 case CACHE_MPAGE:
Hannes Reinecke77800812016-04-04 11:44:07 +02004121 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
4122 fp += hdr_len + bd_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004123 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004124 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004125 break;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004126 case CONTROL_MPAGE:
Hannes Reinecke77800812016-04-04 11:44:07 +02004127 if (ata_mselect_control(qc, p, pg_len, &fp) < 0) {
4128 fp += hdr_len + bd_len;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004129 goto invalid_param;
Tom Yan535fd072016-07-22 02:41:54 +08004130 } else {
4131 goto skip; /* No ATA command to send */
Hannes Reinecke77800812016-04-04 11:44:07 +02004132 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004133 break;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004134 default: /* invalid page code */
Hannes Reinecke77800812016-04-04 11:44:07 +02004135 fp = bd_len + hdr_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004136 goto invalid_param;
4137 }
4138
4139 /*
4140 * Only one page has changeable data, so we only support setting one
4141 * page at a time.
4142 */
4143 if (len > pg_len)
4144 goto invalid_param;
4145
4146 return 0;
4147
4148 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02004149 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004150 return 1;
4151
4152 invalid_param:
Hannes Reinecke77800812016-04-04 11:44:07 +02004153 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004154 return 1;
4155
4156 invalid_param_len:
4157 /* "Parameter list length error" */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004158 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004159 return 1;
4160
4161 skip:
4162 scmd->result = SAM_STAT_GOOD;
4163 return 1;
4164}
4165
4166/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 * ata_get_xlat_func - check if SCSI to ATA translation is possible
4168 * @dev: ATA device
4169 * @cmd: SCSI command opcode to consider
4170 *
4171 * Look up the SCSI command given, and determine whether the
4172 * SCSI command is to be translated or simulated.
4173 *
4174 * RETURNS:
4175 * Pointer to translation function if possible, %NULL if not.
4176 */
4177
4178static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
4179{
4180 switch (cmd) {
4181 case READ_6:
4182 case READ_10:
4183 case READ_16:
4184
4185 case WRITE_6:
4186 case WRITE_10:
4187 case WRITE_16:
4188 return ata_scsi_rw_xlat;
4189
Christoph Hellwig0cdd6eb2009-12-10 10:36:01 +01004190 case WRITE_SAME_16:
Christoph Hellwig18f0f972009-11-17 10:00:47 -05004191 return ata_scsi_write_same_xlat;
4192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 case SYNCHRONIZE_CACHE:
4194 if (ata_try_flush_cache(dev))
4195 return ata_scsi_flush_xlat;
4196 break;
4197
4198 case VERIFY:
4199 case VERIFY_16:
4200 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04004201
4202 case ATA_12:
4203 case ATA_16:
4204 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04004205
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004206 case MODE_SELECT:
4207 case MODE_SELECT_10:
4208 return ata_scsi_mode_select_xlat;
4209 break;
4210
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02004211 case ZBC_IN:
4212 return ata_scsi_zbc_in_xlat;
4213
Hannes Reinecke27708a92016-04-25 12:45:53 +02004214 case ZBC_OUT:
4215 return ata_scsi_zbc_out_xlat;
4216
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04004217 case START_STOP:
4218 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
4220
4221 return NULL;
4222}
4223
4224/**
4225 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
4226 * @ap: ATA port to which the command was being sent
4227 * @cmd: SCSI command to dump
4228 *
4229 * Prints the contents of a SCSI command via printk().
4230 */
4231
4232static inline void ata_scsi_dump_cdb(struct ata_port *ap,
4233 struct scsi_cmnd *cmd)
4234{
4235#ifdef ATA_DEBUG
4236 struct scsi_device *scsidev = cmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Andy Shevchenko26f58832016-05-06 21:40:40 +03004238 DPRINTK("CDB (%u:%d,%d,%d) %9ph\n",
Tejun Heo44877b42007-02-21 01:06:51 +09004239 ap->print_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 scsidev->channel, scsidev->id, scsidev->lun,
Andy Shevchenko26f58832016-05-06 21:40:40 +03004241 cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242#endif
4243}
4244
Tejun Heo542b1442006-12-17 10:45:08 +09004245static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09004246 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06004247{
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004248 u8 scsi_op = scmd->cmnd[0];
4249 ata_xlat_func_t xlat_func;
Tejun Heo2115ea92006-05-15 21:03:39 +09004250 int rc = 0;
4251
Hannes Reinecke9162c652014-11-05 13:08:21 +01004252 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004253 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
4254 goto bad_cdb_len;
4255
4256 xlat_func = ata_get_xlat_func(dev, scsi_op);
4257 } else {
4258 if (unlikely(!scmd->cmd_len))
4259 goto bad_cdb_len;
4260
4261 xlat_func = NULL;
4262 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
4263 /* relay SCSI command to ATAPI device */
Mark Lord607126c2007-11-15 13:13:59 +09004264 int len = COMMAND_SIZE(scsi_op);
4265 if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004266 goto bad_cdb_len;
4267
4268 xlat_func = atapi_xlat;
4269 } else {
4270 /* ATA_16 passthru, treat as an ATA command */
4271 if (unlikely(scmd->cmd_len > 16))
4272 goto bad_cdb_len;
4273
4274 xlat_func = ata_get_xlat_func(dev, scsi_op);
4275 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09004276 }
4277
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004278 if (xlat_func)
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004279 rc = ata_scsi_translate(dev, scmd, xlat_func);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004280 else
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004281 ata_scsi_simulate(dev, scmd);
Tejun Heo2115ea92006-05-15 21:03:39 +09004282
4283 return rc;
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004284
4285 bad_cdb_len:
4286 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
4287 scmd->cmd_len, scsi_op, dev->cdb_len);
4288 scmd->result = DID_ERROR << 16;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004289 scmd->scsi_done(scmd);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004290 return 0;
Brian Kingeb3f0f92006-03-23 17:30:02 -06004291}
4292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293/**
4294 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
Jeff Garzik23e701e2010-11-17 12:03:58 -05004295 * @shost: SCSI host of command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 * @cmd: SCSI command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 *
4298 * In some cases, this function translates SCSI commands into
4299 * ATA taskfiles, and queues the taskfiles to be sent to
4300 * hardware. In other cases, this function simulates a
4301 * SCSI device by evaluating and responding to certain
4302 * SCSI commands. This creates the overall effect of
4303 * ATA and ATAPI devices appearing as SCSI devices.
4304 *
4305 * LOCKING:
Jeff Garzik23e701e2010-11-17 12:03:58 -05004306 * ATA host lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 *
4308 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09004309 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
4310 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 */
Jeff Garzik23e701e2010-11-17 12:03:58 -05004312int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
4314 struct ata_port *ap;
4315 struct ata_device *dev;
4316 struct scsi_device *scsidev = cmd->device;
Tejun Heo2115ea92006-05-15 21:03:39 +09004317 int rc = 0;
Jeff Garzik23e701e2010-11-17 12:03:58 -05004318 unsigned long irq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
Jeff Garzik35bb94b2006-04-11 13:12:34 -04004320 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05004321
Jeff Garzik23e701e2010-11-17 12:03:58 -05004322 spin_lock_irqsave(ap->lock, irq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
4324 ata_scsi_dump_cdb(ap, cmd);
4325
4326 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06004327 if (likely(dev))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004328 rc = __ata_scsi_queuecmd(cmd, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06004329 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzik23e701e2010-11-17 12:03:58 -05004331 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 }
4333
Jeff Garzik23e701e2010-11-17 12:03:58 -05004334 spin_unlock_irqrestore(ap->lock, irq_flags);
4335
Tejun Heo2115ea92006-05-15 21:03:39 +09004336 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337}
4338
4339/**
4340 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004341 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 * @cmd: SCSI command being sent to device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 *
4344 * Interprets and directly executes a select list of SCSI commands
4345 * that can be handled internally.
4346 *
4347 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004348 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 */
4350
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004351void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352{
4353 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04004354 const u8 *scsicmd = cmd->cmnd;
Jeff Garzik45394142007-09-21 06:23:42 -04004355 u8 tmp8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Tejun Heo9a3dccc2006-01-06 09:56:18 +01004357 args.dev = dev;
4358 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 args.cmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004360 args.done = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
4362 switch(scsicmd[0]) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004363 case INQUIRY:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004364 if (scsicmd[1] & 2) /* is CmdDt set? */
4365 ata_scsi_invalid_field(dev, cmd, 1);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004366 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
4367 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
4368 else switch (scsicmd[2]) {
4369 case 0x00:
4370 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004372 case 0x80:
4373 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004375 case 0x83:
4376 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004378 case 0x89:
4379 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 break;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05004381 case 0xb0:
4382 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
4383 break;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06004384 case 0xb1:
4385 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
4386 break;
Martin K. Petersen02e0a602010-09-10 01:23:18 -04004387 case 0xb2:
4388 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
4389 break;
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02004390 case 0xb6:
4391 if (dev->flags & ATA_DFLAG_ZAC) {
4392 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b6);
4393 break;
4394 }
4395 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 default:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004397 ata_scsi_invalid_field(dev, cmd, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004399 }
4400 break;
4401
4402 case MODE_SENSE:
4403 case MODE_SENSE_10:
4404 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
4405 break;
4406
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004407 case READ_CAPACITY:
4408 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4409 break;
4410
Hannes Reineckeeb846d92014-11-17 14:25:19 +01004411 case SERVICE_ACTION_IN_16:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004412 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
4413 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4414 else
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004415 ata_scsi_invalid_field(dev, cmd, 1);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004416 break;
4417
4418 case REPORT_LUNS:
4419 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
4420 break;
4421
4422 case REQUEST_SENSE:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004423 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004424 cmd->result = (DRIVER_SENSE << 24);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004425 cmd->scsi_done(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004426 break;
4427
4428 /* if we reach this, then writeback caching is disabled,
4429 * turning this into a no-op.
4430 */
4431 case SYNCHRONIZE_CACHE:
4432 /* fall through */
4433
4434 /* no-op's, complete with success */
4435 case REZERO_UNIT:
4436 case SEEK_6:
4437 case SEEK_10:
4438 case TEST_UNIT_READY:
4439 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
4440 break;
4441
4442 case SEND_DIAGNOSTIC:
4443 tmp8 = scsicmd[1] & ~(1 << 3);
4444 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
4445 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
4446 else
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004447 ata_scsi_invalid_field(dev, cmd, 1);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004448 break;
4449
Shaun Tancheff7b203092016-08-21 23:23:19 -05004450 case MAINTENANCE_IN:
4451 if (scsicmd[1] == MI_REPORT_SUPPORTED_OPERATION_CODES)
4452 ata_scsi_rbuf_fill(&args, ata_scsiop_maint_in);
4453 else
4454 ata_scsi_invalid_field(dev, cmd, 1);
4455 break;
4456
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004457 /* all other commands */
4458 default:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004459 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004460 /* "Invalid command operation code" */
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004461 cmd->scsi_done(cmd);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 }
4464}
4465
Tejun Heof3187192007-04-17 23:44:07 +09004466int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
4467{
4468 int i, rc;
4469
4470 for (i = 0; i < host->n_ports; i++) {
4471 struct ata_port *ap = host->ports[i];
4472 struct Scsi_Host *shost;
4473
4474 rc = -ENOMEM;
4475 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
4476 if (!shost)
4477 goto err_alloc;
4478
Lin Ming9ee4f392011-12-05 09:20:28 +08004479 shost->eh_noresume = 1;
Tejun Heof3187192007-04-17 23:44:07 +09004480 *(struct ata_port **)&shost->hostdata[0] = ap;
4481 ap->scsi_host = shost;
4482
Gwendal Grignoud9027472010-05-25 12:31:38 -07004483 shost->transportt = ata_scsi_transport_template;
Tejun Heof3187192007-04-17 23:44:07 +09004484 shost->unique_id = ap->print_id;
4485 shost->max_id = 16;
4486 shost->max_lun = 1;
4487 shost->max_channel = 1;
4488 shost->max_cmd_len = 16;
4489
Tejun Heo31cc23b2007-09-23 13:14:12 +09004490 /* Schedule policy is determined by ->qc_defer()
4491 * callback and it needs to see every deferred qc.
4492 * Set host_blocked to 1 to prevent SCSI midlayer from
4493 * automatically deferring requests.
4494 */
4495 shost->max_host_blocked = 1;
4496
Lin Mingf8fc75d2012-04-12 13:50:39 +08004497 rc = scsi_add_host_with_dma(ap->scsi_host,
4498 &ap->tdev, ap->host->dev);
Tejun Heof3187192007-04-17 23:44:07 +09004499 if (rc)
4500 goto err_add;
4501 }
4502
4503 return 0;
4504
4505 err_add:
4506 scsi_host_put(host->ports[i]->scsi_host);
4507 err_alloc:
4508 while (--i >= 0) {
4509 struct Scsi_Host *shost = host->ports[i]->scsi_host;
4510
4511 scsi_remove_host(shost);
4512 scsi_host_put(shost);
4513 }
4514 return rc;
4515}
4516
Tejun Heo1ae46312007-07-16 14:29:40 +09004517void ata_scsi_scan_host(struct ata_port *ap, int sync)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004518{
Tejun Heo1ae46312007-07-16 14:29:40 +09004519 int tries = 5;
4520 struct ata_device *last_failed_dev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004521 struct ata_link *link;
Tejun Heo1ae46312007-07-16 14:29:40 +09004522 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004523
Tejun Heo1ae46312007-07-16 14:29:40 +09004524 repeat:
Tejun Heo1eca4362008-11-03 20:03:17 +09004525 ata_for_each_link(link, ap, EDGE) {
4526 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004527 struct scsi_device *sdev;
4528 int channel = 0, id = 0;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04004529
Tejun Heo1eca4362008-11-03 20:03:17 +09004530 if (dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004531 continue;
Tejun Heo3edebac2006-05-31 18:27:40 +09004532
Tejun Heo41bda9c2007-08-06 18:36:24 +09004533 if (ata_is_host_link(link))
4534 id = dev->devno;
4535 else
4536 channel = link->pmp;
4537
4538 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
4539 NULL);
4540 if (!IS_ERR(sdev)) {
4541 dev->sdev = sdev;
4542 scsi_device_put(sdev);
Grant Grundler295124d2010-08-17 10:56:53 -07004543 } else {
4544 dev->sdev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004545 }
Tejun Heo3edebac2006-05-31 18:27:40 +09004546 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04004547 }
Tejun Heo1ae46312007-07-16 14:29:40 +09004548
4549 /* If we scanned while EH was in progress or allocation
4550 * failure occurred, scan would have failed silently. Check
4551 * whether all devices are attached.
4552 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004553 ata_for_each_link(link, ap, EDGE) {
4554 ata_for_each_dev(dev, link, ENABLED) {
4555 if (!dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004556 goto exit_loop;
4557 }
Tejun Heo1ae46312007-07-16 14:29:40 +09004558 }
Tejun Heo41bda9c2007-08-06 18:36:24 +09004559 exit_loop:
4560 if (!link)
Tejun Heo1ae46312007-07-16 14:29:40 +09004561 return;
4562
4563 /* we're missing some SCSI devices */
4564 if (sync) {
4565 /* If caller requested synchrnous scan && we've made
4566 * any progress, sleep briefly and repeat.
4567 */
4568 if (dev != last_failed_dev) {
4569 msleep(100);
4570 last_failed_dev = dev;
4571 goto repeat;
4572 }
4573
4574 /* We might be failing to detect boot device, give it
4575 * a few more chances.
4576 */
4577 if (--tries) {
4578 msleep(100);
4579 goto repeat;
4580 }
4581
Joe Perchesa9a79df2011-04-15 15:51:59 -07004582 ata_port_err(ap,
4583 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
Tejun Heo1ae46312007-07-16 14:29:40 +09004584 }
4585
Tejun Heoad72cf92010-07-02 10:03:52 +02004586 queue_delayed_work(system_long_wq, &ap->hotplug_task,
Tejun Heo1ae46312007-07-16 14:29:40 +09004587 round_jiffies_relative(HZ));
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004588}
Tejun Heo0ea035a2006-05-31 18:28:01 +09004589
4590/**
4591 * ata_scsi_offline_dev - offline attached SCSI device
4592 * @dev: ATA device to offline attached SCSI device for
4593 *
4594 * This function is called from ata_eh_hotplug() and responsible
4595 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04004596 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09004597 * against clearing.
4598 *
4599 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004600 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09004601 *
4602 * RETURNS:
4603 * 1 if attached SCSI device exists, 0 otherwise.
4604 */
4605int ata_scsi_offline_dev(struct ata_device *dev)
4606{
4607 if (dev->sdev) {
4608 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
4609 return 1;
4610 }
4611 return 0;
4612}
Tejun Heo580b21022006-05-31 18:28:05 +09004613
4614/**
4615 * ata_scsi_remove_dev - remove attached SCSI device
4616 * @dev: ATA device to remove attached SCSI device for
4617 *
4618 * This function is called from ata_eh_scsi_hotplug() and
4619 * responsible for removing the SCSI device attached to @dev.
4620 *
4621 * LOCKING:
4622 * Kernel thread context (may sleep).
4623 */
4624static void ata_scsi_remove_dev(struct ata_device *dev)
4625{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004626 struct ata_port *ap = dev->link->ap;
Tejun Heo580b21022006-05-31 18:28:05 +09004627 struct scsi_device *sdev;
4628 unsigned long flags;
4629
4630 /* Alas, we need to grab scan_mutex to ensure SCSI device
4631 * state doesn't change underneath us and thus
4632 * scsi_device_get() always succeeds. The mutex locking can
4633 * be removed if there is __scsi_device_get() interface which
4634 * increments reference counts regardless of device state.
4635 */
Jeff Garzikcca39742006-08-24 03:19:22 -04004636 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04004637 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b21022006-05-31 18:28:05 +09004638
Jeff Garzikcca39742006-08-24 03:19:22 -04004639 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b21022006-05-31 18:28:05 +09004640 sdev = dev->sdev;
4641 dev->sdev = NULL;
4642
4643 if (sdev) {
4644 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04004645 * away underneath us after the host lock and
Tejun Heo580b21022006-05-31 18:28:05 +09004646 * scan_mutex are released. Hold onto it.
4647 */
4648 if (scsi_device_get(sdev) == 0) {
4649 /* The following ensures the attached sdev is
4650 * offline on return from ata_scsi_offline_dev()
4651 * regardless it wins or loses the race
4652 * against this function.
4653 */
4654 scsi_device_set_state(sdev, SDEV_OFFLINE);
4655 } else {
4656 WARN_ON(1);
4657 sdev = NULL;
4658 }
4659 }
4660
Jeff Garzikba6a1302006-06-22 23:46:10 -04004661 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04004662 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004663
4664 if (sdev) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004665 ata_dev_info(dev, "detaching (SCSI %s)\n",
4666 dev_name(&sdev->sdev_gendev));
Tejun Heo580b21022006-05-31 18:28:05 +09004667
4668 scsi_remove_device(sdev);
4669 scsi_device_put(sdev);
4670 }
4671}
4672
Tejun Heo41bda9c2007-08-06 18:36:24 +09004673static void ata_scsi_handle_link_detach(struct ata_link *link)
4674{
4675 struct ata_port *ap = link->ap;
4676 struct ata_device *dev;
4677
Tejun Heo1eca4362008-11-03 20:03:17 +09004678 ata_for_each_dev(dev, link, ALL) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004679 unsigned long flags;
4680
4681 if (!(dev->flags & ATA_DFLAG_DETACHED))
4682 continue;
4683
4684 spin_lock_irqsave(ap->lock, flags);
4685 dev->flags &= ~ATA_DFLAG_DETACHED;
4686 spin_unlock_irqrestore(ap->lock, flags);
4687
Aaron Luf1bc1e42013-08-23 10:17:54 +08004688 if (zpodd_dev_enabled(dev))
4689 zpodd_exit(dev);
4690
Tejun Heo41bda9c2007-08-06 18:36:24 +09004691 ata_scsi_remove_dev(dev);
4692 }
4693}
4694
Tejun Heo580b21022006-05-31 18:28:05 +09004695/**
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004696 * ata_scsi_media_change_notify - send media change event
Randy Dunlapc5d0e6a2007-10-15 17:29:46 -07004697 * @dev: Pointer to the disk device with media change event
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004698 *
4699 * Tell the block layer to send a media change notification
4700 * event.
4701 *
4702 * LOCKING:
Tejun Heo854c73a2007-09-23 13:14:11 +09004703 * spin_lock_irqsave(host lock)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004704 */
Tejun Heo854c73a2007-09-23 13:14:11 +09004705void ata_scsi_media_change_notify(struct ata_device *dev)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004706{
Tejun Heo854c73a2007-09-23 13:14:11 +09004707 if (dev->sdev)
Jeff Garzikf26792d2007-10-29 17:18:39 -04004708 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
4709 GFP_ATOMIC);
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004710}
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004711
4712/**
Tejun Heo580b21022006-05-31 18:28:05 +09004713 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00004714 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b21022006-05-31 18:28:05 +09004715 *
4716 * Perform SCSI part of hotplug. It's executed from a separate
4717 * workqueue after EH completes. This is necessary because SCSI
4718 * hot plugging requires working EH and hot unplugging is
4719 * synchronized with hot plugging with a mutex.
4720 *
4721 * LOCKING:
4722 * Kernel thread context (may sleep).
4723 */
David Howells65f27f32006-11-22 14:55:48 +00004724void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b21022006-05-31 18:28:05 +09004725{
David Howells65f27f32006-11-22 14:55:48 +00004726 struct ata_port *ap =
4727 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004728 int i;
Tejun Heo580b21022006-05-31 18:28:05 +09004729
Tejun Heob51e9e52006-06-29 01:29:30 +09004730 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b21022006-05-31 18:28:05 +09004731 DPRINTK("ENTER/EXIT - unloading\n");
4732 return;
4733 }
4734
Tejun Heo85fbd722013-12-18 07:07:32 -05004735 /*
4736 * XXX - UGLY HACK
4737 *
4738 * The block layer suspend/resume path is fundamentally broken due
4739 * to freezable kthreads and workqueue and may deadlock if a block
4740 * device gets removed while resume is in progress. I don't know
4741 * what the solution is short of removing freezable kthreads and
4742 * workqueues altogether.
4743 *
4744 * The following is an ugly hack to avoid kicking off device
4745 * removal while freezer is active. This is a joke but does avoid
4746 * this particular deadlock scenario.
4747 *
4748 * https://bugzilla.kernel.org/show_bug.cgi?id=62801
4749 * http://marc.info/?l=linux-kernel&m=138695698516487
4750 */
4751#ifdef CONFIG_FREEZER
4752 while (pm_freezing)
4753 msleep(10);
4754#endif
4755
Tejun Heo580b21022006-05-31 18:28:05 +09004756 DPRINTK("ENTER\n");
Tejun Heoad72cf92010-07-02 10:03:52 +02004757 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004758
Tejun Heo41bda9c2007-08-06 18:36:24 +09004759 /* Unplug detached devices. We cannot use link iterator here
4760 * because PMP links have to be scanned even if PMP is
4761 * currently not attached. Iterate manually.
4762 */
4763 ata_scsi_handle_link_detach(&ap->link);
4764 if (ap->pmp_link)
4765 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
4766 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
Tejun Heo580b21022006-05-31 18:28:05 +09004767
4768 /* scan for new ones */
Tejun Heo1ae46312007-07-16 14:29:40 +09004769 ata_scsi_scan_host(ap, 0);
Tejun Heo580b21022006-05-31 18:28:05 +09004770
Tejun Heoad72cf92010-07-02 10:03:52 +02004771 mutex_unlock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004772 DPRINTK("EXIT\n");
4773}
Tejun Heo83c47bc2006-05-31 18:28:07 +09004774
4775/**
4776 * ata_scsi_user_scan - indication for user-initiated bus scan
4777 * @shost: SCSI host to scan
4778 * @channel: Channel to scan
4779 * @id: ID to scan
4780 * @lun: LUN to scan
4781 *
4782 * This function is called when user explicitly requests bus
4783 * scan. Set probe pending flag and invoke EH.
4784 *
4785 * LOCKING:
4786 * SCSI layer (we don't care)
4787 *
4788 * RETURNS:
4789 * Zero.
4790 */
Gwendal Grignoud9027472010-05-25 12:31:38 -07004791int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004792 unsigned int id, u64 lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004793{
4794 struct ata_port *ap = ata_shost_to_port(shost);
4795 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004796 int devno, rc = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004797
4798 if (!ap->ops->error_handler)
4799 return -EOPNOTSUPP;
4800
Tejun Heo41bda9c2007-08-06 18:36:24 +09004801 if (lun != SCAN_WILD_CARD && lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004802 return -EINVAL;
4803
Tejun Heo071f44b2008-04-07 22:47:22 +09004804 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004805 if (channel != SCAN_WILD_CARD && channel)
4806 return -EINVAL;
4807 devno = id;
4808 } else {
4809 if (id != SCAN_WILD_CARD && id)
4810 return -EINVAL;
4811 devno = channel;
4812 }
4813
Jeff Garzikba6a1302006-06-22 23:46:10 -04004814 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004815
Tejun Heo41bda9c2007-08-06 18:36:24 +09004816 if (devno == SCAN_WILD_CARD) {
4817 struct ata_link *link;
4818
Tejun Heo1eca4362008-11-03 20:03:17 +09004819 ata_for_each_link(link, ap, EDGE) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004820 struct ata_eh_info *ehi = &link->eh_info;
Tejun Heob558edd2008-01-24 00:05:14 +09004821 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09004822 ehi->action |= ATA_EH_RESET;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004823 }
Tejun Heo83c47bc2006-05-31 18:28:07 +09004824 } else {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004825 struct ata_device *dev = ata_find_dev(ap, devno);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004826
4827 if (dev) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004828 struct ata_eh_info *ehi = &dev->link->eh_info;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004829 ehi->probe_mask |= 1 << dev->devno;
Tejun Heocf480622008-01-24 00:05:14 +09004830 ehi->action |= ATA_EH_RESET;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004831 } else
4832 rc = -EINVAL;
4833 }
4834
Tejun Heo309afcb2006-09-30 18:07:17 +09004835 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09004836 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09004837 spin_unlock_irqrestore(ap->lock, flags);
4838 ata_port_wait_eh(ap);
4839 } else
4840 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004841
4842 return rc;
4843}
zhao, forrest3057ac32006-06-12 12:01:34 +08004844
4845/**
Tejun Heod0171262006-06-12 22:51:14 +09004846 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00004847 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08004848 *
Tejun Heod0171262006-06-12 22:51:14 +09004849 * After ATA pass thru (SAT) commands are executed successfully,
Tejun Heoad72cf92010-07-02 10:03:52 +02004850 * libata need to propagate the changes to SCSI layer.
zhao, forrest3057ac32006-06-12 12:01:34 +08004851 *
Tejun Heod0171262006-06-12 22:51:14 +09004852 * LOCKING:
4853 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08004854 */
David Howells65f27f32006-11-22 14:55:48 +00004855void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08004856{
David Howells65f27f32006-11-22 14:55:48 +00004857 struct ata_port *ap =
4858 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004859 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09004860 struct ata_device *dev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004861 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08004862
Tejun Heoad72cf92010-07-02 10:03:52 +02004863 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heof84e7e42006-11-22 11:21:31 +09004864 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08004865
Tejun Heo1eca4362008-11-03 20:03:17 +09004866 ata_for_each_link(link, ap, EDGE) {
4867 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004868 struct scsi_device *sdev = dev->sdev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004869
Tejun Heo1eca4362008-11-03 20:03:17 +09004870 if (!sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004871 continue;
4872 if (scsi_device_get(sdev))
4873 continue;
Tejun Heof84e7e42006-11-22 11:21:31 +09004874
Tejun Heo41bda9c2007-08-06 18:36:24 +09004875 spin_unlock_irqrestore(ap->lock, flags);
4876 scsi_rescan_device(&(sdev->sdev_gendev));
4877 scsi_device_put(sdev);
4878 spin_lock_irqsave(ap->lock, flags);
4879 }
zhao, forrest3057ac32006-06-12 12:01:34 +08004880 }
Tejun Heof84e7e42006-11-22 11:21:31 +09004881
4882 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad72cf92010-07-02 10:03:52 +02004883 mutex_unlock(&ap->scsi_scan_mutex);
zhao, forrest3057ac32006-06-12 12:01:34 +08004884}
Brian King80289162006-08-07 14:27:31 -05004885
4886/**
4887 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04004888 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05004889 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04004890 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05004891 *
4892 * LOCKING:
4893 * PCI/etc. bus probe sem.
4894 *
4895 * RETURNS:
4896 * ata_port pointer on success / NULL on failure.
4897 */
4898
Jeff Garzikcca39742006-08-24 03:19:22 -04004899struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05004900 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04004901 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05004902{
Tejun Heof3187192007-04-17 23:44:07 +09004903 struct ata_port *ap;
Brian King80289162006-08-07 14:27:31 -05004904
Tejun Heof3187192007-04-17 23:44:07 +09004905 ap = ata_port_alloc(host);
Brian King80289162006-08-07 14:27:31 -05004906 if (!ap)
4907 return NULL;
4908
Tejun Heof3187192007-04-17 23:44:07 +09004909 ap->port_no = 0;
James Bottomleya29b5da2011-01-23 08:30:00 -06004910 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09004911 ap->pio_mask = port_info->pio_mask;
4912 ap->mwdma_mask = port_info->mwdma_mask;
4913 ap->udma_mask = port_info->udma_mask;
4914 ap->flags |= port_info->flags;
4915 ap->ops = port_info->port_ops;
4916 ap->cbl = ATA_CBL_SATA;
4917
Brian King80289162006-08-07 14:27:31 -05004918 return ap;
4919}
4920EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
4921
4922/**
4923 * ata_sas_port_start - Set port up for dma.
4924 * @ap: Port to initialize
4925 *
4926 * Called just after data structures for each port are
James Bottomleydde20202008-02-19 11:36:56 +01004927 * initialized.
Brian King80289162006-08-07 14:27:31 -05004928 *
4929 * May be used as the port_start() entry in ata_port_operations.
4930 *
4931 * LOCKING:
4932 * Inherited from caller.
4933 */
4934int ata_sas_port_start(struct ata_port *ap)
4935{
Nishanth Aravamudan3f1e0462011-06-16 08:28:36 -07004936 /*
4937 * the port is marked as frozen at allocation time, but if we don't
4938 * have new eh, we won't thaw it
4939 */
4940 if (!ap->ops->error_handler)
4941 ap->pflags &= ~ATA_PFLAG_FROZEN;
James Bottomleydde20202008-02-19 11:36:56 +01004942 return 0;
Brian King80289162006-08-07 14:27:31 -05004943}
4944EXPORT_SYMBOL_GPL(ata_sas_port_start);
4945
4946/**
4947 * ata_port_stop - Undo ata_sas_port_start()
4948 * @ap: Port to shut down
4949 *
Brian King80289162006-08-07 14:27:31 -05004950 * May be used as the port_stop() entry in ata_port_operations.
4951 *
4952 * LOCKING:
4953 * Inherited from caller.
4954 */
4955
4956void ata_sas_port_stop(struct ata_port *ap)
4957{
Brian King80289162006-08-07 14:27:31 -05004958}
4959EXPORT_SYMBOL_GPL(ata_sas_port_stop);
4960
Dan Williamsb2024452012-03-21 21:09:07 -07004961/**
4962 * ata_sas_async_probe - simply schedule probing and return
4963 * @ap: Port to probe
4964 *
4965 * For batch scheduling of probe for sas attached ata devices, assumes
4966 * the port has already been through ata_sas_port_init()
4967 */
4968void ata_sas_async_probe(struct ata_port *ap)
Dan Williams9508a662012-01-18 20:47:01 -08004969{
Dan Williamsb2024452012-03-21 21:09:07 -07004970 __ata_port_probe(ap);
Dan Williams9508a662012-01-18 20:47:01 -08004971}
Dan Williamsb2024452012-03-21 21:09:07 -07004972EXPORT_SYMBOL_GPL(ata_sas_async_probe);
4973
4974int ata_sas_sync_probe(struct ata_port *ap)
4975{
4976 return ata_port_probe(ap);
4977}
4978EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
4979
Dan Williams9508a662012-01-18 20:47:01 -08004980
Brian King80289162006-08-07 14:27:31 -05004981/**
4982 * ata_sas_port_init - Initialize a SATA device
4983 * @ap: SATA port to initialize
4984 *
4985 * LOCKING:
4986 * PCI/etc. bus probe sem.
4987 *
4988 * RETURNS:
4989 * Zero on success, non-zero on error.
4990 */
4991
4992int ata_sas_port_init(struct ata_port *ap)
4993{
4994 int rc = ap->ops->port_start(ap);
4995
Dan Williamsb2024452012-03-21 21:09:07 -07004996 if (rc)
4997 return rc;
4998 ap->print_id = atomic_inc_return(&ata_print_id);
4999 return 0;
Brian King80289162006-08-07 14:27:31 -05005000}
5001EXPORT_SYMBOL_GPL(ata_sas_port_init);
5002
5003/**
5004 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
5005 * @ap: SATA port to destroy
5006 *
5007 */
5008
5009void ata_sas_port_destroy(struct ata_port *ap)
5010{
Tejun Heof0d36ef2007-01-20 16:00:28 +09005011 if (ap->ops->port_stop)
5012 ap->ops->port_stop(ap);
Brian King80289162006-08-07 14:27:31 -05005013 kfree(ap);
5014}
5015EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
5016
5017/**
5018 * ata_sas_slave_configure - Default slave_config routine for libata devices
5019 * @sdev: SCSI device to configure
5020 * @ap: ATA port to which SCSI device is attached
5021 *
5022 * RETURNS:
5023 * Zero.
5024 */
5025
5026int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
5027{
5028 ata_scsi_sdev_config(sdev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005029 ata_scsi_dev_config(sdev, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05005030 return 0;
5031}
5032EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
5033
5034/**
5035 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
5036 * @cmd: SCSI command to be sent
Brian King80289162006-08-07 14:27:31 -05005037 * @ap: ATA port to which the command is being sent
5038 *
5039 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06005040 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
5041 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05005042 */
5043
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005044int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
Brian King80289162006-08-07 14:27:31 -05005045{
Brian King08475a12006-11-20 13:51:56 -06005046 int rc = 0;
5047
Brian King80289162006-08-07 14:27:31 -05005048 ata_scsi_dump_cdb(ap, cmd);
5049
Tejun Heo2486fa52008-07-31 07:52:40 +09005050 if (likely(ata_dev_enabled(ap->link.device)))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005051 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05005052 else {
5053 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005054 cmd->scsi_done(cmd);
Brian King80289162006-08-07 14:27:31 -05005055 }
Brian King08475a12006-11-20 13:51:56 -06005056 return rc;
Brian King80289162006-08-07 14:27:31 -05005057}
5058EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
Shaohua Li98bd4be2015-01-23 19:52:07 -08005059
5060int ata_sas_allocate_tag(struct ata_port *ap)
5061{
5062 unsigned int max_queue = ap->host->n_tags;
5063 unsigned int i, tag;
5064
5065 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
Tejun Heo3a028242015-03-24 14:14:18 -04005066 tag = tag < max_queue ? tag : 0;
Shaohua Li98bd4be2015-01-23 19:52:07 -08005067
5068 /* the last tag is reserved for internal command. */
5069 if (tag == ATA_TAG_INTERNAL)
5070 continue;
5071
5072 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
5073 ap->sas_last_tag = tag;
5074 return tag;
5075 }
5076 }
5077 return -1;
5078}
5079
5080void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
5081{
5082 clear_bit(tag, &ap->sas_tag_allocated);
5083}