blob: 24024e7282967577705dd34d48219a957474a4c2 [file] [log] [blame]
Thomas Gleixnerc82ee6d2019-05-19 15:51:48 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04003 * libata-scsi.c - helper library for ATA
4 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04005 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
7 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04008 * libata documentation is available via 'make {ps|pdf}docs',
Mauro Carvalho Chehab9bb9a392017-05-16 09:16:37 -03009 * as Documentation/driver-api/libata.rst
Jeff Garzikaf36d7f2005-08-28 20:18:39 -040010 *
11 * Hardware documentation available from
12 * - http://www.t10.org/
13 * - http://www.t13.org/
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Arnd Bergmann75c0b0e2019-11-30 20:28:12 +010016#include <linux/compat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/blkdev.h>
20#include <linux/spinlock.h>
Paul Gortmaker38789fd2011-07-17 15:33:58 -040021#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <scsi/scsi_host.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020024#include <scsi/scsi_cmnd.h>
Mike Christie85837eb2005-11-11 16:38:53 -060025#include <scsi/scsi_eh.h>
Jeff Garzik005a5a02005-10-30 23:31:48 -050026#include <scsi/scsi_device.h>
Tejun Heoa6e6ce82006-05-15 21:03:48 +090027#include <scsi/scsi_tcq.h>
Tejun Heo30afc842006-03-18 18:40:14 +090028#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/libata.h>
Jeff Garzikb0955182005-05-12 15:45:22 -040030#include <linux/hdreg.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040031#include <linux/uaccess.h>
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +010032#include <linux/suspend.h>
Christoph Hellwig18f0f972009-11-17 10:00:47 -050033#include <asm/unaligned.h>
Adam Manzanares8e061782016-10-17 11:27:29 -070034#include <linux/ioprio.h>
Linus Walleij45b80842020-02-22 12:20:57 +010035#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include "libata.h"
Gwendal Grignoud9027472010-05-25 12:31:38 -070038#include "libata-transport.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Bartlomiej Zolnierkiewiczc8fa4f82020-03-26 16:58:03 +010040#define ATA_SCSI_RBUF_SIZE 576
Tejun Heo87340e92008-04-28 17:48:51 +090041
42static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
43static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
Jeff Garzikb0955182005-05-12 15:45:22 -040044
Tejun Heoad706992006-12-17 10:45:57 +090045typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
Tejun Heoab5b3a52006-05-31 18:27:34 +090046
Jeff Garzik2dcb4072007-10-19 06:42:56 -040047static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090048 const struct scsi_device *scsidev);
Jeff Garzik2dcb4072007-10-19 06:42:56 -040049static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090050 const struct scsi_device *scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040052#define RW_RECOVERY_MPAGE 0x1
53#define RW_RECOVERY_MPAGE_LEN 12
54#define CACHE_MPAGE 0x8
55#define CACHE_MPAGE_LEN 20
56#define CONTROL_MPAGE 0xa
57#define CONTROL_MPAGE_LEN 12
58#define ALL_MPAGES 0x3f
59#define ALL_SUB_MPAGES 0xff
60
61
Jeff Garzik24f75682007-09-21 07:54:49 -040062static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040063 RW_RECOVERY_MPAGE,
64 RW_RECOVERY_MPAGE_LEN - 2,
Jeff Garzik24f75682007-09-21 07:54:49 -040065 (1 << 7), /* AWRE */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040066 0, /* read retry count */
67 0, 0, 0, 0,
68 0, /* write retry count */
69 0, 0, 0
70};
71
72static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
73 CACHE_MPAGE,
74 CACHE_MPAGE_LEN - 2,
75 0, /* contains WCE, needs to be 0 for logic */
76 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, /* contains DRA, needs to be 0 for logic */
78 0, 0, 0, 0, 0, 0, 0
79};
80
81static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
82 CONTROL_MPAGE,
83 CONTROL_MPAGE_LEN - 2,
84 2, /* DSENSE=0, GLTSD=1 */
85 0, /* [QAM+QERR may be 1, see 05-359r1] */
86 0, 0, 0, 0, 0xff, 0xff,
87 0, 30 /* extended self test time, see 05-359r1 */
88};
89
Tejun Heoc93b2632010-09-01 17:50:04 +020090static const char *ata_lpm_policy_names[] = {
Hans de Goedef4ac6472017-09-14 12:35:36 +020091 [ATA_LPM_UNKNOWN] = "max_performance",
92 [ATA_LPM_MAX_POWER] = "max_performance",
93 [ATA_LPM_MED_POWER] = "medium_power",
94 [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
Srinivas Pandruvadaa5ec5a7b2018-07-27 13:47:02 -070095 [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
Hans de Goedef4ac6472017-09-14 12:35:36 +020096 [ATA_LPM_MIN_POWER] = "min_power",
Kristen Carlson Accardica773292007-10-25 00:58:59 -040097};
98
Tejun Heoecd75ad2014-01-16 09:47:17 -050099static ssize_t ata_scsi_lpm_store(struct device *device,
Tejun Heoc93b2632010-09-01 17:50:04 +0200100 struct device_attribute *attr,
101 const char *buf, size_t count)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400102{
Tejun Heoecd75ad2014-01-16 09:47:17 -0500103 struct Scsi_Host *shost = class_to_shost(device);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400104 struct ata_port *ap = ata_shost_to_port(shost);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500105 struct ata_link *link;
106 struct ata_device *dev;
Tejun Heoc93b2632010-09-01 17:50:04 +0200107 enum ata_lpm_policy policy;
Tejun Heo6b7ae952010-09-01 17:50:06 +0200108 unsigned long flags;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400109
Tejun Heoc93b2632010-09-01 17:50:04 +0200110 /* UNKNOWN is internal state, iterate from MAX_POWER */
111 for (policy = ATA_LPM_MAX_POWER;
112 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
113 const char *name = ata_lpm_policy_names[policy];
114
115 if (strncmp(name, buf, strlen(name)) == 0)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400116 break;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400117 }
Tejun Heoc93b2632010-09-01 17:50:04 +0200118 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400119 return -EINVAL;
120
Tejun Heo6b7ae952010-09-01 17:50:06 +0200121 spin_lock_irqsave(ap->lock, flags);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500122
123 ata_for_each_link(link, ap, EDGE) {
124 ata_for_each_dev(dev, &ap->link, ENABLED) {
125 if (dev->horkage & ATA_HORKAGE_NOLPM) {
126 count = -EOPNOTSUPP;
127 goto out_unlock;
128 }
129 }
130 }
131
Tejun Heo6b7ae952010-09-01 17:50:06 +0200132 ap->target_lpm_policy = policy;
133 ata_port_schedule_eh(ap);
Tejun Heoecd75ad2014-01-16 09:47:17 -0500134out_unlock:
Tejun Heo6b7ae952010-09-01 17:50:06 +0200135 spin_unlock_irqrestore(ap->lock, flags);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400136 return count;
137}
138
Tejun Heoc93b2632010-09-01 17:50:04 +0200139static ssize_t ata_scsi_lpm_show(struct device *dev,
140 struct device_attribute *attr, char *buf)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400141{
Tony Jonesee959b02008-02-22 00:13:36 +0100142 struct Scsi_Host *shost = class_to_shost(dev);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400143 struct ata_port *ap = ata_shost_to_port(shost);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400144
Tejun Heo6b7ae952010-09-01 17:50:06 +0200145 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400146 return -EINVAL;
147
Tejun Heoc93b2632010-09-01 17:50:04 +0200148 return snprintf(buf, PAGE_SIZE, "%s\n",
Tejun Heo6b7ae952010-09-01 17:50:06 +0200149 ata_lpm_policy_names[ap->target_lpm_policy]);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400150}
Tony Jonesee959b02008-02-22 00:13:36 +0100151DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
Tejun Heoc93b2632010-09-01 17:50:04 +0200152 ata_scsi_lpm_show, ata_scsi_lpm_store);
Tony Jonesee959b02008-02-22 00:13:36 +0100153EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400154
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200155static ssize_t ata_scsi_park_show(struct device *device,
156 struct device_attribute *attr, char *buf)
157{
158 struct scsi_device *sdev = to_scsi_device(device);
159 struct ata_port *ap;
160 struct ata_link *link;
161 struct ata_device *dev;
Tejun Heo3948b6f2016-02-18 11:50:37 -0500162 unsigned long now;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200163 unsigned int uninitialized_var(msecs);
164 int rc = 0;
165
166 ap = ata_shost_to_port(sdev->host);
167
Tejun Heo3948b6f2016-02-18 11:50:37 -0500168 spin_lock_irq(ap->lock);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200169 dev = ata_scsi_find_dev(ap, sdev);
170 if (!dev) {
171 rc = -ENODEV;
172 goto unlock;
173 }
174 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
175 rc = -EOPNOTSUPP;
176 goto unlock;
177 }
178
179 link = dev->link;
Elias Oltmannsa4641892008-11-03 19:01:08 +0900180 now = jiffies;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200181 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
182 link->eh_context.unloaded_mask & (1 << dev->devno) &&
Elias Oltmannsa4641892008-11-03 19:01:08 +0900183 time_after(dev->unpark_deadline, now))
184 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200185 else
186 msecs = 0;
187
188unlock:
189 spin_unlock_irq(ap->lock);
190
191 return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
192}
193
194static ssize_t ata_scsi_park_store(struct device *device,
195 struct device_attribute *attr,
196 const char *buf, size_t len)
197{
198 struct scsi_device *sdev = to_scsi_device(device);
199 struct ata_port *ap;
200 struct ata_device *dev;
201 long int input;
202 unsigned long flags;
203 int rc;
204
Jingoo Han42b9ab72013-07-19 15:56:41 +0900205 rc = kstrtol(buf, 10, &input);
206 if (rc)
207 return rc;
208 if (input < -2)
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200209 return -EINVAL;
210 if (input > ATA_TMOUT_MAX_PARK) {
211 rc = -EOVERFLOW;
212 input = ATA_TMOUT_MAX_PARK;
213 }
214
215 ap = ata_shost_to_port(sdev->host);
216
217 spin_lock_irqsave(ap->lock, flags);
218 dev = ata_scsi_find_dev(ap, sdev);
219 if (unlikely(!dev)) {
220 rc = -ENODEV;
221 goto unlock;
222 }
Hannes Reinecke9162c652014-11-05 13:08:21 +0100223 if (dev->class != ATA_DEV_ATA &&
224 dev->class != ATA_DEV_ZAC) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200225 rc = -EOPNOTSUPP;
226 goto unlock;
227 }
228
229 if (input >= 0) {
230 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
231 rc = -EOPNOTSUPP;
232 goto unlock;
233 }
234
235 dev->unpark_deadline = ata_deadline(jiffies, input);
236 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
237 ata_port_schedule_eh(ap);
238 complete(&ap->park_req_pending);
239 } else {
240 switch (input) {
241 case -1:
242 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
243 break;
244 case -2:
245 dev->flags |= ATA_DFLAG_NO_UNLOAD;
246 break;
247 }
248 }
249unlock:
250 spin_unlock_irqrestore(ap->lock, flags);
251
252 return rc ? rc : len;
253}
254DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
255 ata_scsi_park_show, ata_scsi_park_store);
256EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
257
Adam Manzanares84f95242016-10-17 11:27:30 -0700258static ssize_t ata_ncq_prio_enable_show(struct device *device,
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800259 struct device_attribute *attr,
260 char *buf)
Adam Manzanares84f95242016-10-17 11:27:30 -0700261{
262 struct scsi_device *sdev = to_scsi_device(device);
263 struct ata_port *ap;
264 struct ata_device *dev;
265 bool ncq_prio_enable;
266 int rc = 0;
267
268 ap = ata_shost_to_port(sdev->host);
269
270 spin_lock_irq(ap->lock);
271 dev = ata_scsi_find_dev(ap, sdev);
272 if (!dev) {
273 rc = -ENODEV;
274 goto unlock;
275 }
276
277 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
278
279unlock:
280 spin_unlock_irq(ap->lock);
281
282 return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable);
283}
284
285static ssize_t ata_ncq_prio_enable_store(struct device *device,
286 struct device_attribute *attr,
287 const char *buf, size_t len)
288{
289 struct scsi_device *sdev = to_scsi_device(device);
290 struct ata_port *ap;
291 struct ata_device *dev;
292 long int input;
Adam Manzanares84f95242016-10-17 11:27:30 -0700293 int rc;
294
295 rc = kstrtol(buf, 10, &input);
296 if (rc)
297 return rc;
298 if ((input < 0) || (input > 1))
299 return -EINVAL;
300
301 ap = ata_shost_to_port(sdev->host);
Adam Manzanares84f95242016-10-17 11:27:30 -0700302 dev = ata_scsi_find_dev(ap, sdev);
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800303 if (unlikely(!dev))
304 return -ENODEV;
305
306 spin_lock_irq(ap->lock);
307 if (input)
308 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
309 else
310 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
311
312 dev->link->eh_info.action |= ATA_EH_REVALIDATE;
313 dev->link->eh_info.flags |= ATA_EHI_QUIET;
314 ata_port_schedule_eh(ap);
315 spin_unlock_irq(ap->lock);
316
317 ata_port_wait_eh(ap);
Adam Manzanares84f95242016-10-17 11:27:30 -0700318
Adam Manzanares4e647d92016-10-19 20:40:19 -0700319 if (input) {
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800320 spin_lock_irq(ap->lock);
Adam Manzanares4e647d92016-10-19 20:40:19 -0700321 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800322 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
323 rc = -EIO;
Adam Manzanares4e647d92016-10-19 20:40:19 -0700324 }
Adam Manzanares9f56eca2016-12-13 12:00:05 -0800325 spin_unlock_irq(ap->lock);
Adam Manzanares4e647d92016-10-19 20:40:19 -0700326 }
Adam Manzanares84f95242016-10-17 11:27:30 -0700327
Adam Manzanares84f95242016-10-17 11:27:30 -0700328 return rc ? rc : len;
329}
330
331DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
332 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
333EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
334
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200335void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
336 u8 sk, u8 asc, u8 ascq)
Tejun Heof0761be2008-04-28 17:16:52 +0900337{
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200338 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
339
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +0200340 if (!cmd)
341 return;
342
Tejun Heof0761be2008-04-28 17:16:52 +0900343 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
344
Hannes Reinecke06dbde52016-04-04 11:44:03 +0200345 scsi_build_sense_buffer(d_sense, cmd->sense_buffer, sk, asc, ascq);
Tejun Heof0761be2008-04-28 17:16:52 +0900346}
347
Hannes Reinecke492bf622016-04-04 11:43:59 +0200348void ata_scsi_set_sense_information(struct ata_device *dev,
349 struct scsi_cmnd *cmd,
350 const struct ata_taskfile *tf)
351{
352 u64 information;
353
354 if (!cmd)
355 return;
356
357 information = ata_tf_read_block(tf, dev);
358 if (information == U64_MAX)
359 return;
360
361 scsi_set_sense_information(cmd->sense_buffer,
362 SCSI_SENSE_BUFFERSIZE, information);
363}
364
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200365static void ata_scsi_set_invalid_field(struct ata_device *dev,
Hannes Reinecke0df10b82016-04-04 11:44:06 +0200366 struct scsi_cmnd *cmd, u16 field, u8 bit)
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200367{
368 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
Tom Yan8554e5e2016-07-07 01:13:09 +0800369 /* "Invalid field in CDB" */
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200370 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
Hannes Reinecke0df10b82016-04-04 11:44:06 +0200371 field, bit, 1);
Hannes Reineckebcfc8672016-04-04 11:44:05 +0200372}
373
Hannes Reinecke77800812016-04-04 11:44:07 +0200374static void ata_scsi_set_invalid_parameter(struct ata_device *dev,
375 struct scsi_cmnd *cmd, u16 field)
376{
377 /* "Invalid field in parameter list" */
378 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x26, 0x0);
379 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
380 field, 0xff, 0);
Tejun Heo5924b742007-01-02 20:20:07 +0900381}
382
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700383static ssize_t
384ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
385 const char *buf, size_t count)
386{
387 struct Scsi_Host *shost = class_to_shost(dev);
388 struct ata_port *ap = ata_shost_to_port(shost);
389 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
390 return ap->ops->em_store(ap, buf, count);
391 return -EINVAL;
392}
393
394static ssize_t
395ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
396 char *buf)
397{
398 struct Scsi_Host *shost = class_to_shost(dev);
399 struct ata_port *ap = ata_shost_to_port(shost);
400
401 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
402 return ap->ops->em_show(ap, buf);
403 return -EINVAL;
404}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200405DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700406 ata_scsi_em_message_show, ata_scsi_em_message_store);
407EXPORT_SYMBOL_GPL(dev_attr_em_message);
408
409static ssize_t
410ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
411 char *buf)
412{
413 struct Scsi_Host *shost = class_to_shost(dev);
414 struct ata_port *ap = ata_shost_to_port(shost);
415
416 return snprintf(buf, 23, "%d\n", ap->em_message_type);
417}
418DEVICE_ATTR(em_message_type, S_IRUGO,
419 ata_scsi_em_message_type_show, NULL);
420EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
421
422static ssize_t
423ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
424 char *buf)
425{
426 struct scsi_device *sdev = to_scsi_device(dev);
427 struct ata_port *ap = ata_shost_to_port(sdev->host);
428 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
429
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800430 if (atadev && ap->ops->sw_activity_show &&
431 (ap->flags & ATA_FLAG_SW_ACTIVITY))
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700432 return ap->ops->sw_activity_show(atadev, buf);
433 return -EINVAL;
434}
435
436static ssize_t
437ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
438 const char *buf, size_t count)
439{
440 struct scsi_device *sdev = to_scsi_device(dev);
441 struct ata_port *ap = ata_shost_to_port(sdev->host);
442 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
443 enum sw_activity val;
444 int rc;
445
Xiaotian Feng26cd4d62012-12-13 16:12:18 +0800446 if (atadev && ap->ops->sw_activity_store &&
447 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700448 val = simple_strtoul(buf, NULL, 0);
449 switch (val) {
450 case OFF: case BLINK_ON: case BLINK_OFF:
451 rc = ap->ops->sw_activity_store(atadev, val);
452 if (!rc)
453 return count;
454 else
455 return rc;
456 }
457 }
458 return -EINVAL;
459}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200460DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700461 ata_scsi_activity_store);
462EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
463
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200464struct device_attribute *ata_common_sdev_attrs[] = {
465 &dev_attr_unload_heads,
466 NULL
467};
468EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
469
Bartlomiej Zolnierkiewicz6f09eae2020-03-26 16:57:57 +0100470struct device_attribute *ata_ncq_sdev_attrs[] = {
471 &dev_attr_unload_heads,
472 &dev_attr_ncq_prio_enable,
473 NULL
474};
475EXPORT_SYMBOL_GPL(ata_ncq_sdev_attrs);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/**
478 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
479 * @sdev: SCSI device for which BIOS geometry is to be determined
480 * @bdev: block device associated with @sdev
481 * @capacity: capacity of SCSI device
482 * @geom: location to which geometry will be output
483 *
484 * Generic bios head/sector/cylinder calculator
485 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
486 * mapping. Some situations may arise where the disk is not
487 * bootable if this is not used.
488 *
489 * LOCKING:
490 * Defined by the SCSI layer. We don't really care.
491 *
492 * RETURNS:
493 * Zero.
494 */
495int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
496 sector_t capacity, int geom[])
497{
498 geom[0] = 255;
499 geom[1] = 63;
500 sector_div(capacity, 255*63);
501 geom[2] = capacity;
502
503 return 0;
504}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100505EXPORT_SYMBOL_GPL(ata_std_bios_param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Jeff Garzikb0955182005-05-12 15:45:22 -0400507/**
Tejun Heod8d91292010-05-15 20:09:34 +0200508 * ata_scsi_unlock_native_capacity - unlock native capacity
509 * @sdev: SCSI device to adjust device capacity for
510 *
511 * This function is called if a partition on @sdev extends beyond
512 * the end of the device. It requests EH to unlock HPA.
513 *
514 * LOCKING:
515 * Defined by the SCSI layer. Might sleep.
516 */
517void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
518{
519 struct ata_port *ap = ata_shost_to_port(sdev->host);
520 struct ata_device *dev;
521 unsigned long flags;
522
523 spin_lock_irqsave(ap->lock, flags);
524
525 dev = ata_scsi_find_dev(ap, sdev);
526 if (dev && dev->n_sectors < dev->n_native_sectors) {
527 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
528 dev->link->eh_info.action |= ATA_EH_RESET;
529 ata_port_schedule_eh(ap);
530 }
531
532 spin_unlock_irqrestore(ap->lock, flags);
533 ata_port_wait_eh(ap);
534}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100535EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
Tejun Heod8d91292010-05-15 20:09:34 +0200536
537/**
Tejun Heo5924b742007-01-02 20:20:07 +0900538 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
Randy Dunlap5eb66fe2009-01-20 16:28:59 -0800539 * @ap: target port
Tejun Heo5924b742007-01-02 20:20:07 +0900540 * @sdev: SCSI device to get identify data for
541 * @arg: User buffer area for identify data
542 *
543 * LOCKING:
544 * Defined by the SCSI layer. We don't really care.
545 *
546 * RETURNS:
547 * Zero on success, negative errno on error.
548 */
Jeff Garzik94be9a52009-01-16 10:17:09 -0500549static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
550 void __user *arg)
Tejun Heo5924b742007-01-02 20:20:07 +0900551{
Tejun Heo5924b742007-01-02 20:20:07 +0900552 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
553 u16 __user *dst = arg;
554 char buf[40];
555
556 if (!dev)
557 return -ENOMSG;
558
559 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
560 return -EFAULT;
561
562 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
563 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
564 return -EFAULT;
565
566 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
567 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
568 return -EFAULT;
569
570 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
571 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
572 return -EFAULT;
573
574 return 0;
575}
576
577/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400578 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800579 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400580 * @arg: User provided data for issuing command
581 *
582 * LOCKING:
583 * Defined by the SCSI layer. We don't really care.
584 *
585 * RETURNS:
586 * Zero on success, negative errno on error.
587 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400588int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
589{
590 int rc = 0;
Kees Cook429296c2018-07-31 12:51:53 -0700591 u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
Jeff Garzikb0955182005-05-12 15:45:22 -0400592 u8 scsi_cmd[MAX_COMMAND_SIZE];
Kees Cook429296c2018-07-31 12:51:53 -0700593 u8 args[4], *argbuf = NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400594 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600595 enum dma_data_direction data_dir;
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100596 struct scsi_sense_hdr sshdr;
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700597 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400598
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500599 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400600 return -EINVAL;
601
602 if (copy_from_user(args, arg, sizeof(args)))
603 return -EFAULT;
604
Kees Cook429296c2018-07-31 12:51:53 -0700605 memset(sensebuf, 0, sizeof(sensebuf));
Jeff Garzikb0955182005-05-12 15:45:22 -0400606 memset(scsi_cmd, 0, sizeof(scsi_cmd));
607
608 if (args[3]) {
Grant Grundler295124d2010-08-17 10:56:53 -0700609 argsize = ATA_SECT_SIZE * args[3];
Jeff Garzikb0955182005-05-12 15:45:22 -0400610 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400611 if (argbuf == NULL) {
612 rc = -ENOMEM;
613 goto error;
614 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400615
616 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
617 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400618 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600619 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400620 } else {
621 scsi_cmd[1] = (3 << 1); /* Non-data */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700622 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600623 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400624 }
625
626 scsi_cmd[0] = ATA_16;
627
628 scsi_cmd[4] = args[2];
Bartlomiej Zolnierkiewicza4f19042008-10-10 22:39:20 +0200629 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
Jeff Garzikb0955182005-05-12 15:45:22 -0400630 scsi_cmd[6] = args[3];
631 scsi_cmd[8] = args[1];
Linus Walleijf355eec2018-08-30 11:06:36 +0200632 scsi_cmd[10] = ATA_SMART_LBAM_PASS;
633 scsi_cmd[12] = ATA_SMART_LBAH_PASS;
Jeff Garzikb0955182005-05-12 15:45:22 -0400634 } else {
635 scsi_cmd[6] = args[1];
636 }
637 scsi_cmd[14] = args[0];
638
639 /* Good values for timeout and retries? Values below
640 from scsi_ioctl_send_command() for default case... */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700641 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100642 sensebuf, &sshdr, (10*HZ), 5, 0, 0, NULL);
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700643
644 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
645 u8 *desc = sensebuf + 8;
646 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
647
648 /* If we set cc then ATA pass-through will cause a
649 * check condition even if no error. Filter that. */
650 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100651 if (sshdr.sense_key == RECOVERED_ERROR &&
652 sshdr.asc == 0 && sshdr.ascq == 0x1d)
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700653 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
654 }
655
656 /* Send userspace a few ATA registers (same as drivers/ide) */
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400657 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
658 desc[0] == 0x09) { /* code is "ATA Descriptor" */
659 args[0] = desc[13]; /* status */
660 args[1] = desc[3]; /* error */
661 args[2] = desc[5]; /* sector count (0:7) */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700662 if (copy_to_user(arg, args, sizeof(args)))
663 rc = -EFAULT;
664 }
665 }
666
667
668 if (cmd_result) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400669 rc = -EIO;
670 goto error;
671 }
672
Jeff Garzikb0955182005-05-12 15:45:22 -0400673 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500674 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400675 rc = -EFAULT;
676error:
Jesper Juhl8f760782006-06-27 02:55:06 -0700677 kfree(argbuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400678 return rc;
679}
680
681/**
682 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800683 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400684 * @arg: User provided data for issuing command
685 *
686 * LOCKING:
687 * Defined by the SCSI layer. We don't really care.
688 *
689 * RETURNS:
690 * Zero on success, negative errno on error.
691 */
692int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
693{
694 int rc = 0;
Kees Cook429296c2018-07-31 12:51:53 -0700695 u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
Jeff Garzikb0955182005-05-12 15:45:22 -0400696 u8 scsi_cmd[MAX_COMMAND_SIZE];
Kees Cook429296c2018-07-31 12:51:53 -0700697 u8 args[7];
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100698 struct scsi_sense_hdr sshdr;
David Milburnaf068bd2007-01-30 00:59:15 -0800699 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400700
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500701 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400702 return -EINVAL;
703
704 if (copy_from_user(args, arg, sizeof(args)))
705 return -EFAULT;
706
Kees Cook429296c2018-07-31 12:51:53 -0700707 memset(sensebuf, 0, sizeof(sensebuf));
Jeff Garzikb0955182005-05-12 15:45:22 -0400708 memset(scsi_cmd, 0, sizeof(scsi_cmd));
709 scsi_cmd[0] = ATA_16;
710 scsi_cmd[1] = (3 << 1); /* Non-data */
David Milburnaf068bd2007-01-30 00:59:15 -0800711 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Jeff Garzikb0955182005-05-12 15:45:22 -0400712 scsi_cmd[4] = args[1];
713 scsi_cmd[6] = args[2];
714 scsi_cmd[8] = args[3];
715 scsi_cmd[10] = args[4];
716 scsi_cmd[12] = args[5];
Mark Lord277239f2007-03-30 17:45:52 -0400717 scsi_cmd[13] = args[6] & 0x4f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400718 scsi_cmd[14] = args[0];
719
Jeff Garzikb0955182005-05-12 15:45:22 -0400720 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500721 from scsi_ioctl_send_command() for default case... */
David Milburnaf068bd2007-01-30 00:59:15 -0800722 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
Christoph Hellwig76aaf872017-02-23 16:02:36 +0100723 sensebuf, &sshdr, (10*HZ), 5, 0, 0, NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400724
David Milburnaf068bd2007-01-30 00:59:15 -0800725 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
726 u8 *desc = sensebuf + 8;
727 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
728
729 /* If we set cc then ATA pass-through will cause a
730 * check condition even if no error. Filter that. */
731 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
Krzysztof Mazur6d3bfc72013-03-27 13:51:14 +0100732 if (sshdr.sense_key == RECOVERED_ERROR &&
733 sshdr.asc == 0 && sshdr.ascq == 0x1d)
David Milburnaf068bd2007-01-30 00:59:15 -0800734 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
735 }
736
737 /* Send userspace ATA registers */
738 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
739 desc[0] == 0x09) {/* code is "ATA Descriptor" */
740 args[0] = desc[13]; /* status */
741 args[1] = desc[3]; /* error */
742 args[2] = desc[5]; /* sector count (0:7) */
743 args[3] = desc[7]; /* lbal */
744 args[4] = desc[9]; /* lbam */
745 args[5] = desc[11]; /* lbah */
746 args[6] = desc[12]; /* select */
747 if (copy_to_user(arg, args, sizeof(args)))
748 rc = -EFAULT;
749 }
750 }
751
752 if (cmd_result) {
753 rc = -EIO;
754 goto error;
755 }
756
757 error:
Jeff Garzikb0955182005-05-12 15:45:22 -0400758 return rc;
759}
760
Alan Coxe3cf95d2009-04-09 17:31:17 +0100761static int ata_ioc32(struct ata_port *ap)
762{
763 if (ap->flags & ATA_FLAG_PIO_DMA)
764 return 1;
765 if (ap->pflags & ATA_PFLAG_PIO32)
766 return 1;
767 return 0;
768}
769
Arnd Bergmann75c0b0e2019-11-30 20:28:12 +0100770/*
771 * This handles both native and compat commands, so anything added
772 * here must have a compatible argument, or check in_compat_syscall()
773 */
Jeff Garzik94be9a52009-01-16 10:17:09 -0500774int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
Nathan Chancellor6f4e6262019-02-07 09:07:20 -0700775 unsigned int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Arnd Bergmann287e6612016-02-11 14:16:27 +0100777 unsigned long val;
778 int rc = -EINVAL;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100779 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 switch (cmd) {
Arnd Bergmann287e6612016-02-11 14:16:27 +0100782 case HDIO_GET_32BIT:
Alan Coxe3cf95d2009-04-09 17:31:17 +0100783 spin_lock_irqsave(ap->lock, flags);
784 val = ata_ioc32(ap);
785 spin_unlock_irqrestore(ap->lock, flags);
Arnd Bergmann75c0b0e2019-11-30 20:28:12 +0100786#ifdef CONFIG_COMPAT
787 if (in_compat_syscall())
788 return put_user(val, (compat_ulong_t __user *)arg);
789#endif
Arnd Bergmann287e6612016-02-11 14:16:27 +0100790 return put_user(val, (unsigned long __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Arnd Bergmann287e6612016-02-11 14:16:27 +0100792 case HDIO_SET_32BIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 val = (unsigned long) arg;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100794 rc = 0;
795 spin_lock_irqsave(ap->lock, flags);
796 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
797 if (val)
798 ap->pflags |= ATA_PFLAG_PIO32;
799 else
800 ap->pflags &= ~ATA_PFLAG_PIO32;
801 } else {
802 if (val != ata_ioc32(ap))
803 rc = -EINVAL;
804 }
805 spin_unlock_irqrestore(ap->lock, flags);
806 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Tejun Heo5924b742007-01-02 20:20:07 +0900808 case HDIO_GET_IDENTITY:
Jeff Garzik94be9a52009-01-16 10:17:09 -0500809 return ata_get_identity(ap, scsidev, arg);
Tejun Heo5924b742007-01-02 20:20:07 +0900810
Jeff Garzikb0955182005-05-12 15:45:22 -0400811 case HDIO_DRIVE_CMD:
812 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
813 return -EACCES;
814 return ata_cmd_ioctl(scsidev, arg);
815
816 case HDIO_DRIVE_TASK:
817 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
818 return -EACCES;
819 return ata_task_ioctl(scsidev, arg);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 default:
822 rc = -ENOTTY;
823 break;
824 }
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return rc;
827}
Jeff Garzik94be9a52009-01-16 10:17:09 -0500828EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
829
Nathan Chancellor6f4e6262019-02-07 09:07:20 -0700830int ata_scsi_ioctl(struct scsi_device *scsidev, unsigned int cmd,
831 void __user *arg)
Jeff Garzik94be9a52009-01-16 10:17:09 -0500832{
833 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
834 scsidev, cmd, arg);
835}
836EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838/**
839 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * @dev: ATA device to which the new command is attached
841 * @cmd: SCSI command that originated this ATA command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 *
843 * Obtain a reference to an unused ata_queued_cmd structure,
844 * which is the basic libata structure representing a single
845 * ATA command sent to the hardware.
846 *
847 * If a command was available, fill in the SCSI-specific
848 * portions of the structure with information on the
849 * current command.
850 *
851 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400852 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 *
854 * RETURNS:
855 * Command allocated, or %NULL if none available.
856 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100857static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500858 struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct ata_queued_cmd *qc;
861
Shaohua Li12cb5ce2015-01-15 17:32:27 -0800862 qc = ata_qc_new_init(dev, cmd->request->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (qc) {
864 qc->scsicmd = cmd;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500865 qc->scsidone = cmd->scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Tejun Heoff2aeb12007-12-05 16:43:11 +0900867 qc->sg = scsi_sglist(cmd);
Boaz Harrosh71201652007-09-18 17:48:50 +0200868 qc->n_elem = scsi_sg_count(cmd);
Damien Le Moal7eb49502018-05-09 09:28:11 +0900869
870 if (cmd->request->rq_flags & RQF_QUIET)
871 qc->flags |= ATA_QCFLAG_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 } else {
873 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -0500874 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
877 return qc;
878}
879
Tejun Heoaacda372008-03-18 17:47:43 +0900880static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
881{
882 struct scsi_cmnd *scmd = qc->scsicmd;
883
884 qc->extrabytes = scmd->request->extra_len;
885 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400889 * ata_dump_status - user friendly display of error info
890 * @id: id of the port in question
891 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400893 * Decode and dump the ATA error/status registers for the user so
894 * that they have some idea what really happened at the non
895 * make-believe layer.
896 *
897 * LOCKING:
898 * inherited from caller
899 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100900static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
Jeff Garzikb0955182005-05-12 15:45:22 -0400901{
902 u8 stat = tf->command, err = tf->feature;
903
Joe Perches6ef56322017-06-12 12:17:39 -0700904 pr_warn("ata%u: status=0x%02x { ", id, stat);
Jeff Garzikb0955182005-05-12 15:45:22 -0400905 if (stat & ATA_BUSY) {
Joe Perches6ef56322017-06-12 12:17:39 -0700906 pr_cont("Busy }\n"); /* Data is not valid in this case */
Jeff Garzikb0955182005-05-12 15:45:22 -0400907 } else {
Joe Perches6ef56322017-06-12 12:17:39 -0700908 if (stat & ATA_DRDY) pr_cont("DriveReady ");
909 if (stat & ATA_DF) pr_cont("DeviceFault ");
910 if (stat & ATA_DSC) pr_cont("SeekComplete ");
911 if (stat & ATA_DRQ) pr_cont("DataRequest ");
912 if (stat & ATA_CORR) pr_cont("CorrectedError ");
913 if (stat & ATA_SENSE) pr_cont("Sense ");
914 if (stat & ATA_ERR) pr_cont("Error ");
915 pr_cont("}\n");
Jeff Garzikb0955182005-05-12 15:45:22 -0400916
917 if (err) {
Joe Perches6ef56322017-06-12 12:17:39 -0700918 pr_warn("ata%u: error=0x%02x { ", id, err);
919 if (err & ATA_ABORTED) pr_cont("DriveStatusError ");
Hannes Reinecke1308d7f2015-03-27 16:46:34 +0100920 if (err & ATA_ICRC) {
921 if (err & ATA_ABORTED)
Joe Perches6ef56322017-06-12 12:17:39 -0700922 pr_cont("BadCRC ");
923 else pr_cont("Sector ");
Jeff Garzikb0955182005-05-12 15:45:22 -0400924 }
Joe Perches6ef56322017-06-12 12:17:39 -0700925 if (err & ATA_UNC) pr_cont("UncorrectableError ");
926 if (err & ATA_IDNF) pr_cont("SectorIdNotFound ");
927 if (err & ATA_TRK0NF) pr_cont("TrackZeroNotFound ");
928 if (err & ATA_AMNF) pr_cont("AddrMarkNotFound ");
929 pr_cont("}\n");
Jeff Garzikb0955182005-05-12 15:45:22 -0400930 }
931 }
932}
933
934/**
935 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800936 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400937 * @drv_stat: value contained in ATA status register
938 * @drv_err: value contained in ATA error register
939 * @sk: the sense key we'll fill out
940 * @asc: the additional sense code we'll fill out
941 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900942 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400943 *
944 * Converts an ATA error into a SCSI error. Fill out pointers to
945 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
946 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 *
948 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400949 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100951static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
952 u8 *asc, u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Jeff Garzikb0955182005-05-12 15:45:22 -0400954 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100957 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* BBD|ECC|ID|MAR */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100959 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
960 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /* BBD|ECC|ID */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100962 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
963 // Device busy Aborted command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /* ECC|MC|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100965 {0x61, HARDWARE_ERROR, 0x00, 0x00},
966 // Device fault Hardware error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100968 {0x84, ABORTED_COMMAND, 0x47, 0x00},
969 // Data CRC error SCSI parity error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* MC|ID|ABRT|TRK0|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100971 {0x37, NOT_READY, 0x04, 0x00},
972 // Unit offline Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* MCR|MARK */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100974 {0x09, NOT_READY, 0x04, 0x00},
975 // Unrecovered disk error Not ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Bad address mark */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100977 {0x01, MEDIUM_ERROR, 0x13, 0x00},
978 // Address mark not found for data field
979 /* TRK0 - Track 0 not found */
980 {0x02, HARDWARE_ERROR, 0x00, 0x00},
981 // Hardware error
Gwendal Grignou78062c52013-06-18 10:54:48 -0700982 /* Abort: 0x04 is not translated here, see below */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* Media change request */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100984 {0x08, NOT_READY, 0x04, 0x00},
985 // FIXME: faking offline
986 /* SRV/IDNF - ID not found */
987 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
988 // Logical address out of range
989 /* MC - Media Changed */
990 {0x20, UNIT_ATTENTION, 0x28, 0x00},
991 // Not ready to ready change, medium may have changed
992 /* ECC - Uncorrectable ECC error */
993 {0x40, MEDIUM_ERROR, 0x11, 0x04},
994 // Unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /* BBD - block marked bad */
Hannes Reinecke8ae72042015-03-27 16:46:32 +0100996 {0x80, MEDIUM_ERROR, 0x11, 0x04},
997 // Block marked bad Medium error, unrecovered read error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
999 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01001000 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* Must be first because BUSY means no other bits valid */
Hannes Reinecke8ae72042015-03-27 16:46:32 +01001002 {0x80, ABORTED_COMMAND, 0x47, 0x00},
1003 // Busy, fake parity for now
1004 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
1005 // Device ready, unaligned write command
1006 {0x20, HARDWARE_ERROR, 0x44, 0x00},
1007 // Device fault, internal target failure
1008 {0x08, ABORTED_COMMAND, 0x47, 0x00},
1009 // Timed out in xfer, fake parity for now
1010 {0x04, RECOVERED_ERROR, 0x11, 0x00},
1011 // Recovered ECC error Medium error, recovered
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
1013 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /*
1016 * Is this an error we can process/parse
1017 */
Jeff Garzikb0955182005-05-12 15:45:22 -04001018 if (drv_stat & ATA_BUSY) {
1019 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Jeff Garzikb0955182005-05-12 15:45:22 -04001022 if (drv_err) {
1023 /* Look for drv_err */
1024 for (i = 0; sense_table[i][0] != 0xFF; i++) {
1025 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001026 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -04001027 sense_table[i][0]) {
1028 *sk = sense_table[i][1];
1029 *asc = sense_table[i][2];
1030 *ascq = sense_table[i][3];
1031 goto translate_done;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
Gwendal Grignou78062c52013-06-18 10:54:48 -07001036 /*
1037 * Fall back to interpreting status bits. Note that if the drv_err
1038 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
1039 * is not descriptive enough.
1040 */
Jeff Garzikb0955182005-05-12 15:45:22 -04001041 for (i = 0; stat_table[i][0] != 0xFF; i++) {
1042 if (stat_table[i][0] & drv_stat) {
1043 *sk = stat_table[i][1];
1044 *asc = stat_table[i][2];
1045 *ascq = stat_table[i][3];
1046 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Gwendal Grignou78062c52013-06-18 10:54:48 -07001050 /*
1051 * We need a sensible error return here, which is tricky, and one
1052 * that won't cause people to do things like return a disk wrongly.
1053 */
Alan Cox2d202022006-03-21 15:53:46 +00001054 *sk = ABORTED_COMMAND;
1055 *asc = 0x00;
1056 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -04001057
1058 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +09001059 if (verbose)
Joe Perches6ef56322017-06-12 12:17:39 -07001060 pr_err("ata%u: translated ATA stat/err 0x%02x/%02x to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
Tejun Heo246619d2006-05-15 20:58:16 +09001061 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -04001062 return;
1063}
1064
1065/*
Tejun Heo750426a2006-11-14 22:37:35 +09001066 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -04001067 * @qc: Command that completed.
1068 *
1069 * This function is specific to the ATA descriptor format sense
1070 * block specified for the ATA pass through commands. Regardless
1071 * of whether the command errored or not, return a sense
1072 * block. Copy all controller registers into the sense
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001073 * block. If there was no error, we get the request from an ATA
1074 * passthrough command, so we use the following sense data:
1075 * sk = RECOVERED ERROR
1076 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
1077 *
Jeff Garzikb0955182005-05-12 15:45:22 -04001078 *
1079 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001080 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001081 */
Tejun Heo750426a2006-11-14 22:37:35 +09001082static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001083{
1084 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001085 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001086 unsigned char *sb = cmd->sense_buffer;
1087 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +09001088 int verbose = qc->ap->ops->error_handler == NULL;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001089 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001090
1091 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1092
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001093 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001094
1095 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04001096 * Use ata_to_sense_error() to map status register bits
1097 * onto sense key, asc & ascq.
1098 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001099 if (qc->err_mask ||
1100 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001101 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001102 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001103 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001104 } else {
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001105 /*
1106 * ATA PASS-THROUGH INFORMATION AVAILABLE
1107 * Always in descriptor format sense.
1108 */
1109 scsi_build_sense_buffer(1, cmd->sense_buffer,
1110 RECOVERED_ERROR, 0, 0x1D);
Jeff Garzikb0955182005-05-12 15:45:22 -04001111 }
1112
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001113 if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
1114 u8 len;
Jeff Garzikb0955182005-05-12 15:45:22 -04001115
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001116 /* descriptor format */
1117 len = sb[7];
1118 desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
1119 if (!desc) {
1120 if (SCSI_SENSE_BUFFERSIZE < len + 14)
1121 return;
1122 sb[7] = len + 14;
1123 desc = sb + 8 + len;
1124 }
1125 desc[0] = 9;
1126 desc[1] = 12;
1127 /*
1128 * Copy registers into sense buffer.
1129 */
1130 desc[2] = 0x00;
1131 desc[3] = tf->feature; /* == error reg */
1132 desc[5] = tf->nsect;
1133 desc[7] = tf->lbal;
1134 desc[9] = tf->lbam;
1135 desc[11] = tf->lbah;
1136 desc[12] = tf->device;
1137 desc[13] = tf->command; /* == status reg */
Jeff Garzikb0955182005-05-12 15:45:22 -04001138
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001139 /*
1140 * Fill in Extend bit, and the high order bytes
1141 * if applicable.
1142 */
1143 if (tf->flags & ATA_TFLAG_LBA48) {
1144 desc[2] |= 0x01;
1145 desc[4] = tf->hob_nsect;
1146 desc[6] = tf->hob_lbal;
1147 desc[8] = tf->hob_lbam;
1148 desc[10] = tf->hob_lbah;
1149 }
1150 } else {
1151 /* Fixed sense format */
1152 desc[0] = tf->feature;
1153 desc[1] = tf->command; /* status */
1154 desc[2] = tf->device;
1155 desc[3] = tf->nsect;
Hannes Reineckee0029dc2016-10-31 21:06:58 +01001156 desc[7] = 0;
Hannes Reinecke11093cb2016-04-04 11:44:02 +02001157 if (tf->flags & ATA_TFLAG_LBA48) {
1158 desc[8] |= 0x80;
1159 if (tf->hob_nsect)
1160 desc[8] |= 0x40;
1161 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
1162 desc[8] |= 0x20;
1163 }
1164 desc[9] = tf->lbal;
1165 desc[10] = tf->lbam;
1166 desc[11] = tf->lbah;
Jeff Garzikb0955182005-05-12 15:45:22 -04001167 }
1168}
1169
1170/**
Tejun Heo750426a2006-11-14 22:37:35 +09001171 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -04001172 * @qc: Command that we are erroring out
1173 *
Tejun Heod25614b2006-11-14 22:37:35 +09001174 * Generate sense block for a failed ATA command @qc. Descriptor
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001175 * format is used to accommodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -04001176 *
1177 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001178 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001179 */
Tejun Heo750426a2006-11-14 22:37:35 +09001180static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001181{
Tejun Heod25614b2006-11-14 22:37:35 +09001182 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -04001183 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001184 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001185 unsigned char *sb = cmd->sense_buffer;
Tejun Heo246619d2006-05-15 20:58:16 +09001186 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +09001187 u64 block;
Hannes Reineckeb525e772016-04-04 11:44:00 +02001188 u8 sense_key, asc, ascq;
Jeff Garzikb0955182005-05-12 15:45:22 -04001189
1190 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1191
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001192 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001193
Hannes Reinecke59f68472016-04-25 12:45:50 +02001194 if (ata_dev_disabled(dev)) {
1195 /* Device disabled after error recovery */
1196 /* LOGICAL UNIT NOT READY, HARD RESET REQUIRED */
1197 ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
1198 return;
1199 }
Tejun Heod25614b2006-11-14 22:37:35 +09001200 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -04001201 * onto sense key, asc & ascq.
1202 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001203 if (qc->err_mask ||
1204 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001205 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Hannes Reineckeb525e772016-04-04 11:44:00 +02001206 &sense_key, &asc, &ascq, verbose);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001207 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001208 } else {
1209 /* Could not decode error */
1210 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
1211 tf->command, qc->err_mask);
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001212 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
Hannes Reinecke5e6acd12016-04-04 11:43:56 +02001213 return;
Jeff Garzikb0955182005-05-12 15:45:22 -04001214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Tejun Heod25614b2006-11-14 22:37:35 +09001216 block = ata_tf_read_block(&qc->result_tf, dev);
Hannes Reineckecffd1ee2016-04-04 11:43:57 +02001217 if (block == U64_MAX)
1218 return;
Jeff Garzik0274aa22005-06-22 13:50:56 -04001219
Hannes Reineckecf8b49b2016-04-04 11:43:58 +02001220 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Brian Kinga6cce2a2006-03-17 17:04:15 -06001223static void ata_scsi_sdev_config(struct scsi_device *sdev)
1224{
1225 sdev->use_10_for_rw = 1;
1226 sdev->use_10_for_ms = 1;
Nicolai Stangee1859342016-12-07 22:21:33 +01001227 sdev->no_write_same = 1;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001228
1229 /* Schedule policy is determined by ->qc_defer() callback and
1230 * it needs to see every deferred qc. Set dev_blocked to 1 to
1231 * prevent SCSI midlayer from automatically deferring
1232 * requests.
1233 */
1234 sdev->max_device_blocked = 1;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001235}
1236
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001237/**
1238 * atapi_drain_needed - Check whether data transfer may overflow
Randy Dunlap73fd8b62008-02-19 13:43:21 -08001239 * @rq: request to be checked
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001240 *
1241 * ATAPI commands which transfer variable length data to host
Masanari Iidac9b55602016-04-13 23:36:27 +09001242 * might overflow due to application error or hardware bug. This
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001243 * function checks whether overflow should be drained and ignored
1244 * for @request.
1245 *
1246 * LOCKING:
1247 * None.
1248 *
1249 * RETURNS:
1250 * 1 if ; otherwise, 0.
1251 */
1252static int atapi_drain_needed(struct request *rq)
1253{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001254 if (likely(!blk_rq_is_passthrough(rq)))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001255 return 0;
1256
Mike Christiea8ebb052016-06-05 14:31:45 -05001257 if (!blk_rq_bytes(rq) || op_is_write(req_op(rq)))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001258 return 0;
1259
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001260 return atapi_cmd_type(scsi_req(rq)->cmd[0]) == ATAPI_MISC;
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001261}
1262
1263static int ata_scsi_dev_config(struct scsi_device *sdev,
1264 struct ata_device *dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -06001265{
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001266 struct request_queue *q = sdev->request_queue;
1267
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02001268 if (!ata_id_has_unload(dev->id))
1269 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1270
Tejun Heo914ed352006-11-01 18:39:55 +09001271 /* configure max sectors */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001272 blk_queue_max_hw_sectors(q, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -06001273
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001274 if (dev->class == ATA_DEV_ATAPI) {
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001275 void *buf;
1276
Tejun Heo729a6a32011-01-20 13:59:06 +01001277 sdev->sector_size = ATA_SECT_SIZE;
1278
1279 /* set DMA padding */
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001280 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001281
1282 /* configure draining */
1283 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1284 if (!buf) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07001285 ata_dev_err(dev, "drain buffer allocation failed\n");
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001286 return -ENOMEM;
1287 }
1288
1289 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1290 } else {
Grant Grundler295124d2010-08-17 10:56:53 -07001291 sdev->sector_size = ata_id_logical_sector_size(dev->id);
Tejun Heod8cf5382008-01-15 08:46:59 +09001292 sdev->manage_start_stop = 1;
James Bottomleydde20202008-02-19 11:36:56 +01001293 }
Tejun Heod8cf5382008-01-15 08:46:59 +09001294
Tejun Heo729a6a32011-01-20 13:59:06 +01001295 /*
1296 * ata_pio_sectors() expects buffer for each sector to not cross
1297 * page boundary. Enforce it by requiring buffers to be sector
1298 * aligned, which works iff sector_size is not larger than
1299 * PAGE_SIZE. ATAPI devices also need the alignment as
1300 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1301 */
1302 if (sdev->sector_size > PAGE_SIZE)
Joe Perchesa9a79df2011-04-15 15:51:59 -07001303 ata_dev_warn(dev,
Tejun Heo729a6a32011-01-20 13:59:06 +01001304 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1305 sdev->sector_size);
1306
shaohua.li@intel.com900e5992011-05-06 11:35:31 -06001307 blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
Tejun Heo729a6a32011-01-20 13:59:06 +01001308
Jeff Garzikf26792d2007-10-29 17:18:39 -04001309 if (dev->flags & ATA_DFLAG_AN)
1310 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1311
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001312 if (dev->flags & ATA_DFLAG_NCQ) {
1313 int depth;
1314
1315 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
Jens Axboe69278f72018-05-11 12:51:10 -06001316 depth = min(ATA_MAX_QUEUE, depth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001317 scsi_change_queue_depth(sdev, depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001318 }
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001319
Christoph Hellwigd80210f2017-06-19 14:26:46 +02001320 if (dev->flags & ATA_DFLAG_TRUSTED)
1321 sdev->security_supported = 1;
1322
Grant Grundler295124d2010-08-17 10:56:53 -07001323 dev->sdev = sdev;
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001324 return 0;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/**
1328 * ata_scsi_slave_config - Set SCSI device attributes
1329 * @sdev: SCSI device to examine
1330 *
1331 * This is called before we actually start reading
1332 * and writing to the device, to configure certain
1333 * SCSI mid-layer behaviors.
1334 *
1335 * LOCKING:
1336 * Defined by SCSI layer. We don't really care.
1337 */
1338
1339int ata_scsi_slave_config(struct scsi_device *sdev)
1340{
Tejun Heo31534362006-05-31 18:27:36 +09001341 struct ata_port *ap = ata_shost_to_port(sdev->host);
1342 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001343 int rc = 0;
Tejun Heo31534362006-05-31 18:27:36 +09001344
Brian Kinga6cce2a2006-03-17 17:04:15 -06001345 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Tejun Heo31534362006-05-31 18:27:36 +09001347 if (dev)
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001348 rc = ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001350 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001352EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354/**
Tejun Heo83c47bc2006-05-31 18:28:07 +09001355 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1356 * @sdev: SCSI device to be destroyed
1357 *
1358 * @sdev is about to be destroyed for hot/warm unplugging. If
1359 * this unplugging was initiated by libata as indicated by NULL
1360 * dev->sdev, this function doesn't have to do anything.
1361 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1362 * Clear dev->sdev, schedule the device for ATA detach and invoke
1363 * EH.
1364 *
1365 * LOCKING:
1366 * Defined by SCSI layer. We don't really care.
1367 */
1368void ata_scsi_slave_destroy(struct scsi_device *sdev)
1369{
1370 struct ata_port *ap = ata_shost_to_port(sdev->host);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001371 struct request_queue *q = sdev->request_queue;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001372 unsigned long flags;
1373 struct ata_device *dev;
1374
1375 if (!ap->ops->error_handler)
1376 return;
1377
Jeff Garzikba6a1302006-06-22 23:46:10 -04001378 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09001379 dev = __ata_scsi_find_dev(ap, sdev);
1380 if (dev && dev->sdev) {
1381 /* SCSI device already in CANCEL state, no need to offline it */
1382 dev->sdev = NULL;
1383 dev->flags |= ATA_DFLAG_DETACH;
1384 ata_port_schedule_eh(ap);
1385 }
Jeff Garzikba6a1302006-06-22 23:46:10 -04001386 spin_unlock_irqrestore(ap->lock, flags);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001387
1388 kfree(q->dma_drain_buffer);
1389 q->dma_drain_buffer = NULL;
1390 q->dma_drain_size = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001391}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001392EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heo83c47bc2006-05-31 18:28:07 +09001393
1394/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001395 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
Marcos Paulo de Souza6d2dd052011-10-31 23:50:16 -02001396 * @ap: ATA port to which the device change the queue depth
1397 * @sdev: SCSI device to configure queue depth for
1398 * @queue_depth: new queue depth
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001399 *
Dan Williamsf6e67032011-09-20 15:10:33 -07001400 * libsas and libata have different approaches for associating a sdev to
1401 * its ata_port.
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001402 *
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001403 */
Dan Williamsf6e67032011-09-20 15:10:33 -07001404int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001405 int queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001406{
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001407 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +09001408 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001409
Tejun Heoc3c70c42007-02-20 23:31:22 +09001410 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001411 return sdev->queue_depth;
1412
1413 dev = ata_scsi_find_dev(ap, sdev);
1414 if (!dev || !ata_dev_enabled(dev))
1415 return sdev->queue_depth;
1416
Tejun Heoc3c70c42007-02-20 23:31:22 +09001417 /* NCQ enabled? */
Tejun Heo360f6542006-09-30 19:45:00 +09001418 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001419 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1420 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
Tejun Heo360f6542006-09-30 19:45:00 +09001421 dev->flags |= ATA_DFLAG_NCQ_OFF;
Tejun Heoc3c70c42007-02-20 23:31:22 +09001422 queue_depth = 1;
1423 }
Tejun Heo360f6542006-09-30 19:45:00 +09001424 spin_unlock_irqrestore(ap->lock, flags);
1425
Tejun Heoc3c70c42007-02-20 23:31:22 +09001426 /* limit and apply queue depth */
1427 queue_depth = min(queue_depth, sdev->host->can_queue);
1428 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
Jens Axboe69278f72018-05-11 12:51:10 -06001429 queue_depth = min(queue_depth, ATA_MAX_QUEUE);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001430
1431 if (sdev->queue_depth == queue_depth)
1432 return -EINVAL;
1433
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001434 return scsi_change_queue_depth(sdev, queue_depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001435}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001436EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001437
1438/**
Dan Williamsf6e67032011-09-20 15:10:33 -07001439 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1440 * @sdev: SCSI device to configure queue depth for
1441 * @queue_depth: new queue depth
Dan Williamsf6e67032011-09-20 15:10:33 -07001442 *
1443 * This is libata standard hostt->change_queue_depth callback.
1444 * SCSI will call into this callback when user tries to set queue
1445 * depth via sysfs.
1446 *
1447 * LOCKING:
1448 * SCSI layer (we don't care)
1449 *
1450 * RETURNS:
1451 * Newly configured queue depth.
1452 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001453int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
Dan Williamsf6e67032011-09-20 15:10:33 -07001454{
1455 struct ata_port *ap = ata_shost_to_port(sdev->host);
1456
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001457 return __ata_change_queue_depth(ap, sdev, queue_depth);
Dan Williamsf6e67032011-09-20 15:10:33 -07001458}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001459EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Dan Williamsf6e67032011-09-20 15:10:33 -07001460
1461/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001462 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1463 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001464 *
1465 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1466 * (to start). Perhaps these commands should be preceded by
1467 * CHECK POWER MODE to see what power mode the device is already in.
1468 * [See SAT revision 5 at www.t10.org]
1469 *
1470 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001471 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001472 *
1473 * RETURNS:
1474 * Zero on success, non-zero on error.
1475 */
Tejun Heoad706992006-12-17 10:45:57 +09001476static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001477{
Tejun Heo542b1442006-12-17 10:45:08 +09001478 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001479 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +09001480 const u8 *cdb = scmd->cmnd;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001481 u16 fp;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001482 u8 bp = 0xff;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001483
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001484 if (scmd->cmd_len < 5) {
1485 fp = 4;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001486 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001487 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09001488
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001489 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1490 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +09001491 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001492 ; /* ignore IMMED bit, violates sat-r05 */
1493 }
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001494 if (cdb[4] & 0x2) {
1495 fp = 4;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001496 bp = 1;
Douglas Gilbertae006512005-10-09 09:09:35 -04001497 goto invalid_fld; /* LOEJ bit set not supported */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001498 }
1499 if (((cdb[4] >> 4) & 0xf) != 0) {
1500 fp = 4;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001501 bp = 3;
Douglas Gilbertae006512005-10-09 09:09:35 -04001502 goto invalid_fld; /* power conditions not supported */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001503 }
Tejun Heoe31e85312007-09-23 13:14:13 +09001504
Tejun Heo542b1442006-12-17 10:45:08 +09001505 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001506 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -04001507
1508 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +09001509 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -04001510
1511 tf->lbah = 0x0;
1512 tf->lbam = 0x0;
1513 tf->lbal = 0x0;
1514 tf->device |= ATA_LBA;
1515 } else {
1516 /* CHS */
1517 tf->lbal = 0x1; /* sect */
1518 tf->lbam = 0x0; /* cyl low */
1519 tf->lbah = 0x0; /* cyl high */
1520 }
1521
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001522 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
Tejun Heo920a4b12007-05-04 21:28:48 +02001523 } else {
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001524 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1525 * or S5) causing some drives to spin up and down again.
1526 */
1527 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1528 system_state == SYSTEM_POWER_OFF)
1529 goto skip;
1530
1531 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1532 system_entering_hibernation())
1533 goto skip;
1534
Robert Hancock78981a72007-01-30 00:59:18 -08001535 /* Issue ATA STANDBY IMMEDIATE command */
1536 tf->command = ATA_CMD_STANDBYNOW1;
Tejun Heo920a4b12007-05-04 21:28:48 +02001537 }
Robert Hancock78981a72007-01-30 00:59:18 -08001538
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001539 /*
1540 * Standby and Idle condition timers could be implemented but that
1541 * would require libata to implement the Power condition mode page
1542 * and allow the user to change it. Changing mode pages requires
1543 * MODE SELECT to be implemented.
1544 */
1545
1546 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001547
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001548 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001549 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
Douglas Gilbertae006512005-10-09 09:09:35 -04001550 return 1;
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001551 skip:
1552 scmd->result = SAM_STAT_GOOD;
1553 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001554}
1555
1556
1557/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1559 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 *
1561 * Sets up an ATA taskfile to issue FLUSH CACHE or
1562 * FLUSH CACHE EXT.
1563 *
1564 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001565 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 *
1567 * RETURNS:
1568 * Zero on success, non-zero on error.
1569 */
Tejun Heoad706992006-12-17 10:45:57 +09001570static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 struct ata_taskfile *tf = &qc->tf;
1573
1574 tf->flags |= ATA_TFLAG_DEVICE;
1575 tf->protocol = ATA_PROT_NODATA;
1576
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001577 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 tf->command = ATA_CMD_FLUSH_EXT;
1579 else
1580 tf->command = ATA_CMD_FLUSH;
1581
Tejun Heob666da32007-10-26 15:53:59 +09001582 /* flush is critical for IO integrity, consider it an IO command */
1583 qc->flags |= ATA_QCFLAG_IO;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return 0;
1586}
1587
1588/**
Albert Lee3aef5232005-10-04 08:47:43 -04001589 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001590 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001591 *
1592 * Calculate LBA and transfer length for 6-byte commands.
1593 *
1594 * RETURNS:
1595 * @plba: the LBA
1596 * @plen: the transfer length
1597 */
Tejun Heo542b1442006-12-17 10:45:08 +09001598static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001599{
1600 u64 lba = 0;
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001601 u32 len;
Albert Lee3aef5232005-10-04 08:47:43 -04001602
1603 VPRINTK("six-byte command\n");
1604
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001605 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
Tejun Heo542b1442006-12-17 10:45:08 +09001606 lba |= ((u64)cdb[2]) << 8;
1607 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001608
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001609 len = cdb[4];
Albert Lee3aef5232005-10-04 08:47:43 -04001610
1611 *plba = lba;
1612 *plen = len;
1613}
1614
1615/**
1616 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001617 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001618 *
1619 * Calculate LBA and transfer length for 10-byte commands.
1620 *
1621 * RETURNS:
1622 * @plba: the LBA
1623 * @plen: the transfer length
1624 */
Tejun Heo542b1442006-12-17 10:45:08 +09001625static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001626{
1627 u64 lba = 0;
1628 u32 len = 0;
1629
1630 VPRINTK("ten-byte command\n");
1631
Tejun Heo542b1442006-12-17 10:45:08 +09001632 lba |= ((u64)cdb[2]) << 24;
1633 lba |= ((u64)cdb[3]) << 16;
1634 lba |= ((u64)cdb[4]) << 8;
1635 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001636
Tejun Heo542b1442006-12-17 10:45:08 +09001637 len |= ((u32)cdb[7]) << 8;
1638 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001639
1640 *plba = lba;
1641 *plen = len;
1642}
1643
1644/**
1645 * scsi_16_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001646 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001647 *
1648 * Calculate LBA and transfer length for 16-byte commands.
1649 *
1650 * RETURNS:
1651 * @plba: the LBA
1652 * @plen: the transfer length
1653 */
Tejun Heo542b1442006-12-17 10:45:08 +09001654static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001655{
1656 u64 lba = 0;
1657 u32 len = 0;
1658
1659 VPRINTK("sixteen-byte command\n");
1660
Tejun Heo542b1442006-12-17 10:45:08 +09001661 lba |= ((u64)cdb[2]) << 56;
1662 lba |= ((u64)cdb[3]) << 48;
1663 lba |= ((u64)cdb[4]) << 40;
1664 lba |= ((u64)cdb[5]) << 32;
1665 lba |= ((u64)cdb[6]) << 24;
1666 lba |= ((u64)cdb[7]) << 16;
1667 lba |= ((u64)cdb[8]) << 8;
1668 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001669
Tejun Heo542b1442006-12-17 10:45:08 +09001670 len |= ((u32)cdb[10]) << 24;
1671 len |= ((u32)cdb[11]) << 16;
1672 len |= ((u32)cdb[12]) << 8;
1673 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001674
1675 *plba = lba;
1676 *plen = len;
1677}
1678
1679/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1681 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 *
1683 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1684 *
1685 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001686 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 *
1688 * RETURNS:
1689 * Zero on success, non-zero on error.
1690 */
Tejun Heoad706992006-12-17 10:45:57 +09001691static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Tejun Heo542b1442006-12-17 10:45:08 +09001693 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001695 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001697 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001698 u64 block;
1699 u32 n_block;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001700 u16 fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1703 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Tejun Heo2e5704f2006-12-17 10:46:33 +09001705 if (cdb[0] == VERIFY) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001706 if (scmd->cmd_len < 10) {
1707 fp = 9;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001708 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001709 }
Tejun Heo542b1442006-12-17 10:45:08 +09001710 scsi_10_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001711 } else if (cdb[0] == VERIFY_16) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001712 if (scmd->cmd_len < 16) {
1713 fp = 15;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001714 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001715 }
Tejun Heo542b1442006-12-17 10:45:08 +09001716 scsi_16_lba_len(cdb, &block, &n_block);
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001717 } else {
1718 fp = 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001719 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Albert Lee8bf62ece2005-05-12 15:29:42 -04001722 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001723 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001724 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001725 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001726 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001727 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Albert Lee07506692005-10-12 15:04:18 +08001729 if (dev->flags & ATA_DFLAG_LBA) {
1730 tf->flags |= ATA_TFLAG_LBA;
1731
Albert Leec6a33e22005-10-12 15:12:26 +08001732 if (lba_28_ok(block, n_block)) {
1733 /* use LBA28 */
1734 tf->command = ATA_CMD_VERIFY;
1735 tf->device |= (block >> 24) & 0xf;
1736 } else if (lba_48_ok(block, n_block)) {
1737 if (!(dev->flags & ATA_DFLAG_LBA48))
1738 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001739
1740 /* use LBA48 */
1741 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001742 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Albert Lee8bf62ece2005-05-12 15:29:42 -04001744 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Albert Lee8bf62ece2005-05-12 15:29:42 -04001746 tf->hob_lbah = (block >> 40) & 0xff;
1747 tf->hob_lbam = (block >> 32) & 0xff;
1748 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001749 } else
1750 /* request too large even for LBA48 */
1751 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001752
1753 tf->nsect = n_block & 0xff;
1754
1755 tf->lbah = (block >> 16) & 0xff;
1756 tf->lbam = (block >> 8) & 0xff;
1757 tf->lbal = block & 0xff;
1758
1759 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001761 /* CHS */
1762 u32 sect, head, cyl, track;
1763
Albert Leec6a33e22005-10-12 15:12:26 +08001764 if (!lba_28_ok(block, n_block))
1765 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001766
Albert Lee8bf62ece2005-05-12 15:29:42 -04001767 /* Convert LBA to CHS */
1768 track = (u32)block / dev->sectors;
1769 cyl = track / dev->heads;
1770 head = track % dev->heads;
1771 sect = (u32)block % dev->sectors + 1;
1772
Albert Leec187c4b2005-10-04 08:46:51 -04001773 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1774 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001775
1776 /* Check whether the converted CHS can fit.
1777 Cylinder: 0-65535
Albert Lee8bf62ece2005-05-12 15:29:42 -04001778 Head: 0-15
1779 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001780 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001781 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001784 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1785 tf->lbal = sect;
1786 tf->lbam = cyl;
1787 tf->lbah = cyl >> 8;
1788 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001792
1793invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001794 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
Douglas Gilbertae006512005-10-09 09:09:35 -04001795 return 1;
1796
1797out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001798 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001799 /* "Logical Block Address out of range" */
1800 return 1;
1801
1802nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001803 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001804 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Jens Axboe2d727152019-08-07 12:20:52 -06001807static bool ata_check_nblocks(struct scsi_cmnd *scmd, u32 n_blocks)
1808{
1809 struct request *rq = scmd->request;
1810 u32 req_blocks;
1811
1812 if (!blk_rq_is_passthrough(rq))
1813 return true;
1814
1815 req_blocks = blk_rq_bytes(rq) / scmd->device->sector_size;
1816 if (n_blocks > req_blocks)
1817 return false;
1818
1819 return true;
1820}
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822/**
1823 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1824 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 *
1826 * Converts any of six SCSI read/write commands into the
1827 * ATA counterpart, including starting sector (LBA),
1828 * sector count, and taking into account the device's LBA48
1829 * support.
1830 *
1831 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1832 * %WRITE_16 are currently supported.
1833 *
1834 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001835 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 *
1837 * RETURNS:
1838 * Zero on success, non-zero on error.
1839 */
Tejun Heoad706992006-12-17 10:45:57 +09001840static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Tejun Heo542b1442006-12-17 10:45:08 +09001842 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001843 const u8 *cdb = scmd->cmnd;
Adam Manzanares8e061782016-10-17 11:27:29 -07001844 struct request *rq = scmd->request;
1845 int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
Tejun Heobd056d72006-11-14 22:47:10 +09001846 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001847 u64 block;
1848 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001849 int rc;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001850 u16 fp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Tejun Heo542b1442006-12-17 10:45:08 +09001852 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001853 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001855 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001856 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001857 case READ_10:
1858 case WRITE_10:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001859 if (unlikely(scmd->cmd_len < 10)) {
1860 fp = 9;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001861 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001862 }
Tejun Heo542b1442006-12-17 10:45:08 +09001863 scsi_10_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001864 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001865 tf_flags |= ATA_TFLAG_FUA;
Jens Axboe2d727152019-08-07 12:20:52 -06001866 if (!ata_check_nblocks(scmd, n_block))
1867 goto invalid_fld;
Albert Lee3aef5232005-10-04 08:47:43 -04001868 break;
1869 case READ_6:
1870 case WRITE_6:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001871 if (unlikely(scmd->cmd_len < 6)) {
1872 fp = 5;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001873 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001874 }
Tejun Heo542b1442006-12-17 10:45:08 +09001875 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001876
1877 /* for 6-byte r/w commands, transfer length 0
1878 * means 256 blocks of data, not 0 block.
1879 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001880 if (!n_block)
1881 n_block = 256;
Jens Axboe2d727152019-08-07 12:20:52 -06001882 if (!ata_check_nblocks(scmd, n_block))
1883 goto invalid_fld;
Albert Lee3aef5232005-10-04 08:47:43 -04001884 break;
1885 case READ_16:
1886 case WRITE_16:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001887 if (unlikely(scmd->cmd_len < 16)) {
1888 fp = 15;
Tejun Heo2e5704f2006-12-17 10:46:33 +09001889 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001890 }
Tejun Heo542b1442006-12-17 10:45:08 +09001891 scsi_16_lba_len(cdb, &block, &n_block);
Jeff Garzik3e451a42012-08-17 14:04:50 -04001892 if (cdb[1] & (1 << 3))
Tejun Heobd056d72006-11-14 22:47:10 +09001893 tf_flags |= ATA_TFLAG_FUA;
Jens Axboe2d727152019-08-07 12:20:52 -06001894 if (!ata_check_nblocks(scmd, n_block))
1895 goto invalid_fld;
Albert Lee3aef5232005-10-04 08:47:43 -04001896 break;
1897 default:
Albert Lee8bf62ece2005-05-12 15:29:42 -04001898 DPRINTK("no-byte command\n");
Hannes Reineckebcfc8672016-04-04 11:44:05 +02001899 fp = 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001900 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902
Albert Lee8bf62ece2005-05-12 15:29:42 -04001903 /* Check and compose ATA command */
1904 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001905 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1906 * length 0 means transfer 0 block of data.
1907 * However, for ATA R/W commands, sector count 0 means
1908 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001909 *
1910 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001911 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001912 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001913
Tejun Heobd056d72006-11-14 22:47:10 +09001914 qc->flags |= ATA_QCFLAG_IO;
Grant Grundler295124d2010-08-17 10:56:53 -07001915 qc->nbytes = n_block * scmd->device->sector_size;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001916
Tejun Heobd056d72006-11-14 22:47:10 +09001917 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
Jens Axboe4e5b6262018-05-11 12:51:04 -06001918 qc->hw_tag, class);
Adam Manzanares8e061782016-10-17 11:27:29 -07001919
Tejun Heobd056d72006-11-14 22:47:10 +09001920 if (likely(rc == 0))
1921 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001922
Tejun Heobd056d72006-11-14 22:47:10 +09001923 if (rc == -ERANGE)
1924 goto out_of_range;
1925 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001926invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02001927 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
Douglas Gilbertae006512005-10-09 09:09:35 -04001928 return 1;
1929
1930out_of_range:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001931 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001932 /* "Logical Block Address out of range" */
1933 return 1;
1934
1935nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001936 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001937 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
1939
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001940static void ata_qc_done(struct ata_queued_cmd *qc)
1941{
1942 struct scsi_cmnd *cmd = qc->scsicmd;
1943 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1944
1945 ata_qc_free(qc);
1946 done(cmd);
1947}
1948
Tejun Heo77853bf2006-01-23 13:09:36 +09001949static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Tejun Heoc31f5712006-11-22 12:39:43 +09001951 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001953 u8 *cdb = cmd->cmnd;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001954 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Jeff Garzikb0955182005-05-12 15:45:22 -04001956 /* For ATA pass thru (SAT) commands, generate a sense block if
1957 * user mandated it or if there's an error. Note that if we
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001958 * generate because the user forced us to [CK_COND =1], a check
1959 * condition is generated and the ATA register values are returned
Jeff Garzikb0955182005-05-12 15:45:22 -04001960 * whether the command completed successfully or not. If there
Gwendal Grignou84a9a8c2013-01-18 10:56:43 -08001961 * was no error, we use the following sense data:
1962 * sk = RECOVERED ERROR
1963 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
Jeff Garzikb0955182005-05-12 15:45:22 -04001964 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001965 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001966 ((cdb[2] & 0x20) || need_sense))
Tejun Heo750426a2006-11-14 22:37:35 +09001967 ata_gen_passthru_sense(qc);
Hannes Reinecke5b01e4b2016-04-04 11:43:54 +02001968 else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1969 cmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig25c7ce72015-10-03 19:21:11 +02001970 else if (need_sense)
1971 ata_gen_ata_sense(qc);
1972 else
1973 cmd->result = SAM_STAT_GOOD;
Jeff Garzikb0955182005-05-12 15:45:22 -04001974
Tejun Heoc31f5712006-11-22 12:39:43 +09001975 if (need_sense && !ap->ops->error_handler)
Tejun Heo44877b42007-02-21 01:06:51 +09001976 ata_dump_status(ap->print_id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02001978 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
1981/**
1982 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 * @dev: ATA device to which the command is addressed
1984 * @cmd: SCSI command to execute
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1986 *
1987 * Our ->queuecommand() function has decided that the SCSI
1988 * command issued can be directly translated into an ATA
1989 * command, rather than handled internally.
1990 *
1991 * This function sets up an ata_queued_cmd structure for the
1992 * SCSI command, and sends that ata_queued_cmd to the hardware.
1993 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001994 * The xlat_func argument (actor) returns 0 if ready to execute
1995 * ATA command, else 1 to finish translation. If 1 is returned
1996 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1997 * to be set reflecting an error condition or clean (early)
1998 * termination.
1999 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002001 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09002002 *
2003 * RETURNS:
2004 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
2005 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 */
Tejun Heo2115ea92006-05-15 21:03:39 +09002007static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09002008 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Tejun Heo31cc23b2007-09-23 13:14:12 +09002010 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 struct ata_queued_cmd *qc;
Tejun Heo31cc23b2007-09-23 13:14:12 +09002012 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 VPRINTK("ENTER\n");
2015
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002016 qc = ata_scsi_qc_new(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04002018 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05002021 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
2022 cmd->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harrosh71201652007-09-18 17:48:50 +02002023 if (unlikely(scsi_bufflen(cmd) < 1)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002024 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04002025 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027
Boaz Harrosh71201652007-09-18 17:48:50 +02002028 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 qc->dma_dir = cmd->sc_data_direction;
2031 }
2032
2033 qc->complete_fn = ata_scsi_qc_complete;
2034
Tejun Heoad706992006-12-17 10:45:57 +09002035 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04002036 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Tejun Heo31cc23b2007-09-23 13:14:12 +09002038 if (ap->ops->qc_defer) {
2039 if ((rc = ap->ops->qc_defer(qc)))
2040 goto defer;
2041 }
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09002044 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Douglas Gilbertae006512005-10-09 09:09:35 -04002049early_finish:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002050 ata_qc_free(qc);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002051 cmd->scsi_done(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -04002052 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002053 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002054
2055err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04002057 cmd->result = (DID_ERROR << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05002058 cmd->scsi_done(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05002059err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04002060 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09002061 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09002062
2063defer:
Tejun Heo31cc23b2007-09-23 13:14:12 +09002064 ata_qc_free(qc);
Tejun Heo3dc1d882006-05-15 21:03:45 +09002065 DPRINTK("EXIT - defer\n");
Tejun Heo31cc23b2007-09-23 13:14:12 +09002066 if (rc == ATA_DEFER_LINK)
2067 return SCSI_MLQUEUE_DEVICE_BUSY;
2068 else
2069 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Christoph Hellwigf0a37d12017-01-10 09:41:44 +01002072struct ata_scsi_args {
2073 struct ata_device *dev;
2074 u16 *id;
2075 struct scsi_cmnd *cmd;
Christoph Hellwigf0a37d12017-01-10 09:41:44 +01002076};
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078/**
2079 * ata_scsi_rbuf_get - Map response buffer.
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002080 * @cmd: SCSI command containing buffer to be mapped.
Tejun Heo87340e92008-04-28 17:48:51 +09002081 * @flags: unsigned long variable to store irq enable status
2082 * @copy_in: copy in from user buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 *
Tejun Heo87340e92008-04-28 17:48:51 +09002084 * Prepare buffer for simulated SCSI commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 *
2086 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09002087 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 *
2089 * RETURNS:
Tejun Heo87340e92008-04-28 17:48:51 +09002090 * Pointer to response buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 */
Tejun Heo87340e92008-04-28 17:48:51 +09002092static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
2093 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Tejun Heo87340e92008-04-28 17:48:51 +09002095 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Tejun Heo87340e92008-04-28 17:48:51 +09002097 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
2098 if (copy_in)
2099 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2100 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2101 return ata_scsi_rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
2104/**
2105 * ata_scsi_rbuf_put - Unmap response buffer.
2106 * @cmd: SCSI command containing buffer to be unmapped.
Tejun Heo87340e92008-04-28 17:48:51 +09002107 * @copy_out: copy out result
2108 * @flags: @flags passed to ata_scsi_rbuf_get()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 *
Tejun Heo87340e92008-04-28 17:48:51 +09002110 * Returns rbuf buffer. The result is copied to @cmd's buffer if
2111 * @copy_back is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 *
2113 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09002114 * Unlocks ata_scsi_rbuf_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 */
Tejun Heo87340e92008-04-28 17:48:51 +09002116static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
2117 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Tejun Heo87340e92008-04-28 17:48:51 +09002119 if (copy_out)
2120 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2121 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2122 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
2125/**
2126 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
2127 * @args: device IDENTIFY data / SCSI command of interest.
2128 * @actor: Callback hook for desired SCSI command simulator
2129 *
2130 * Takes care of the hard work of simulating a SCSI command...
2131 * Mapping the response buffer, calling the command's handler,
2132 * and handling the handler's return value. This return value
2133 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04002134 * completed successfully (0), or not (in which case cmd->result
2135 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 *
2137 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002138 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 */
Tejun Heof0761be2008-04-28 17:16:52 +09002140static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
Tejun Heo87340e92008-04-28 17:48:51 +09002141 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 u8 *rbuf;
Tejun Heo87340e92008-04-28 17:48:51 +09002144 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 struct scsi_cmnd *cmd = args->cmd;
Jeff Garzikb445c562008-02-29 19:10:51 -05002146 unsigned long flags;
2147
Tejun Heo87340e92008-04-28 17:48:51 +09002148 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
2149 rc = actor(args, rbuf);
2150 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
Jeff Garzikb445c562008-02-29 19:10:51 -05002151
Douglas Gilbertae006512005-10-09 09:09:35 -04002152 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 cmd->result = SAM_STAT_GOOD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
2156/**
2157 * ata_scsiop_inq_std - Simulate INQUIRY command
2158 * @args: device IDENTIFY data / SCSI command of interest.
2159 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 *
2161 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05002162 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 *
2164 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002165 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 */
Tejun Heo87340e92008-04-28 17:48:51 +09002167static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Colin Ian Kinge94f79142017-09-19 09:39:52 +01002169 static const u8 versions[] = {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002170 0x00,
Tejun Heo87340e92008-04-28 17:48:51 +09002171 0x60, /* SAM-3 (no version claimed) */
2172
2173 0x03,
2174 0x20, /* SBC-2 (no version claimed) */
2175
Tom Yan29a37ea2016-07-12 21:29:35 +08002176 0x03,
2177 0x00 /* SPC-3 (no version claimed) */
Tejun Heo87340e92008-04-28 17:48:51 +09002178 };
Colin Ian Kinge94f79142017-09-19 09:39:52 +01002179 static const u8 versions_zbc[] = {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002180 0x00,
2181 0xA0, /* SAM-5 (no version claimed) */
2182
Hannes Reinecke856c4662016-04-25 12:45:55 +02002183 0x06,
2184 0x00, /* SBC-4 (no version claimed) */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002185
Hannes Reinecke856c4662016-04-25 12:45:55 +02002186 0x05,
2187 0xC0, /* SPC-5 (no version claimed) */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002188
2189 0x60,
Hannes Reinecke856c4662016-04-25 12:45:55 +02002190 0x24, /* ZBC r05 */
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002191 };
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 u8 hdr[] = {
2194 TYPE_DISK,
2195 0,
2196 0x5, /* claim SPC-3 version compatibility */
2197 2,
Tom Yan415ffdd2016-07-12 21:29:34 +08002198 95 - 4,
2199 0,
2200 0,
2201 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 };
2203
Tejun Heo87340e92008-04-28 17:48:51 +09002204 VPRINTK("ENTER\n");
2205
Manuel Lauss8a3e33c2015-09-30 21:10:25 +02002206 /* set scsi removable (RMB) bit per ata bit, or if the
2207 * AHCI port says it's external (Hotplug-capable, eSATA).
2208 */
2209 if (ata_id_removable(args->id) ||
2210 (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 hdr[1] |= (1 << 7);
2212
Tom Yan56b8cba2016-07-12 22:12:01 +08002213 if (args->dev->class == ATA_DEV_ZAC) {
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002214 hdr[0] = TYPE_ZBC;
Tom Yan56b8cba2016-07-12 22:12:01 +08002215 hdr[2] = 0x7; /* claim SPC-5 version compatibility */
2216 }
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002219 memcpy(&rbuf[8], "ATA ", 8);
2220 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
Keith Buschc49a6bf2014-05-01 11:12:03 -06002221
2222 /* From SAT, use last 2 words from fw rev unless they are spaces */
2223 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
2224 if (strncmp(&rbuf[32], " ", 4) == 0)
2225 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Tejun Heo87340e92008-04-28 17:48:51 +09002227 if (rbuf[32] == 0 || rbuf[32] == ' ')
2228 memcpy(&rbuf[32], "n/a ", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Hannes Reinecke856c4662016-04-25 12:45:55 +02002230 if (ata_id_zoned_cap(args->id) || args->dev->class == ATA_DEV_ZAC)
Hannes Reineckef9ca5ab2014-11-05 13:08:22 +01002231 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
2232 else
2233 memcpy(rbuf + 58, versions, sizeof(versions));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 return 0;
2236}
2237
2238/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002239 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 * @args: device IDENTIFY data / SCSI command of interest.
2241 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002243 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 *
2245 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002246 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 */
Tejun Heo87340e92008-04-28 17:48:51 +09002248static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002250 int num_pages;
Colin Ian Kinge94f79142017-09-19 09:39:52 +01002251 static const u8 pages[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 0x00, /* page 0x00, this page */
2253 0x80, /* page 0x80, unit serial no page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002254 0x83, /* page 0x83, device ident page */
2255 0x89, /* page 0x89, ata info page */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002256 0xb0, /* page 0xb0, block limits page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002257 0xb1, /* page 0xb1, block device characteristics page */
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002258 0xb2, /* page 0xb2, thin provisioning page */
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002259 0xb6, /* page 0xb6, zoned block device characteristics */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 };
Tejun Heo87340e92008-04-28 17:48:51 +09002261
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002262 num_pages = sizeof(pages);
2263 if (!(args->dev->flags & ATA_DFLAG_ZAC))
2264 num_pages--;
2265 rbuf[3] = num_pages; /* number of supported VPD pages */
2266 memcpy(rbuf + 4, pages, num_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return 0;
2268}
2269
2270/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002271 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 * @args: device IDENTIFY data / SCSI command of interest.
2273 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 *
2275 * Returns ATA device serial number.
2276 *
2277 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002278 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 */
Tejun Heo87340e92008-04-28 17:48:51 +09002280static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Colin Ian Kinge94f79142017-09-19 09:39:52 +01002282 static const u8 hdr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 0,
2284 0x80, /* this page code */
2285 0,
Tejun Heoa0cf7332007-01-02 20:18:49 +09002286 ATA_ID_SERNO_LEN, /* page len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 };
Tejun Heo87340e92008-04-28 17:48:51 +09002288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002290 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2291 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return 0;
2293}
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002296 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 * @args: device IDENTIFY data / SCSI command of interest.
2298 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002300 * Yields two logical unit device identification designators:
2301 * - vendor specific ASCII containing the ATA serial number
2302 * - SAT defined "t10 vendor id based" containing ASCII vendor
2303 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 *
2305 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002306 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 */
Tejun Heo87340e92008-04-28 17:48:51 +09002308static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Jeff Garzikb142eb62006-03-21 20:37:47 -05002310 const int sat_model_serial_desc_len = 68;
Tejun Heo87340e92008-04-28 17:48:51 +09002311 int num;
Jeff Garzikb142eb62006-03-21 20:37:47 -05002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05002314 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Tejun Heo87340e92008-04-28 17:48:51 +09002316 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2317 rbuf[num + 0] = 2;
2318 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2319 num += 4;
2320 ata_id_string(args->id, (unsigned char *) rbuf + num,
2321 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2322 num += ATA_ID_SERNO_LEN;
2323
2324 /* SAT defined lu model and serial numbers descriptor */
2325 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2326 rbuf[num + 0] = 2;
2327 rbuf[num + 1] = 1;
2328 rbuf[num + 3] = sat_model_serial_desc_len;
2329 num += 4;
2330 memcpy(rbuf + num, "ATA ", 8);
2331 num += 8;
2332 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2333 ATA_ID_PROD_LEN);
2334 num += ATA_ID_PROD_LEN;
2335 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2336 ATA_ID_SERNO_LEN);
2337 num += ATA_ID_SERNO_LEN;
2338
Hannes Reinecke6b3b9d72011-03-07 08:56:44 +01002339 if (ata_id_has_wwn(args->id)) {
2340 /* SAT defined lu world wide name */
2341 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2342 rbuf[num + 0] = 1;
2343 rbuf[num + 1] = 3;
2344 rbuf[num + 3] = ATA_ID_WWN_LEN;
2345 num += 4;
2346 ata_id_string(args->id, (unsigned char *) rbuf + num,
2347 ATA_ID_WWN, ATA_ID_WWN_LEN);
2348 num += ATA_ID_WWN_LEN;
2349 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05002350 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return 0;
2352}
2353
2354/**
Jeff Garzikad355b42007-09-21 20:38:03 -04002355 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2356 * @args: device IDENTIFY data / SCSI command of interest.
2357 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Jeff Garzikad355b42007-09-21 20:38:03 -04002358 *
2359 * Yields SAT-specified ATA VPD page.
2360 *
2361 * LOCKING:
2362 * spin_lock_irqsave(host lock)
2363 */
Tejun Heo87340e92008-04-28 17:48:51 +09002364static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
Jeff Garzikad355b42007-09-21 20:38:03 -04002365{
Tejun Heo87340e92008-04-28 17:48:51 +09002366 rbuf[1] = 0x89; /* our page code */
2367 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2368 rbuf[3] = (0x238 & 0xff);
Jeff Garzikad355b42007-09-21 20:38:03 -04002369
Tejun Heo87340e92008-04-28 17:48:51 +09002370 memcpy(&rbuf[8], "linux ", 8);
2371 memcpy(&rbuf[16], "libata ", 16);
2372 memcpy(&rbuf[32], DRV_VERSION, 4);
Jeff Garzikad355b42007-09-21 20:38:03 -04002373
Tejun Heo87340e92008-04-28 17:48:51 +09002374 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
Bartlomiej Zolnierkiewicz33531002020-03-26 16:58:00 +01002375 rbuf[37] = (1 << 7); /* bit 7 indicates Command FIS */
2376 /* TODO: PMP? */
2377
2378 /* we don't store the ATA device signature, so we fake it */
2379 rbuf[38] = ATA_DRDY; /* really, this is Status reg */
2380 rbuf[40] = 0x1;
2381 rbuf[48] = 0x1;
Jeff Garzikad355b42007-09-21 20:38:03 -04002382
Tejun Heo87340e92008-04-28 17:48:51 +09002383 rbuf[56] = ATA_CMD_ID_ATA;
Jeff Garzikad355b42007-09-21 20:38:03 -04002384
Tejun Heo87340e92008-04-28 17:48:51 +09002385 memcpy(&rbuf[60], &args->id[0], 512);
Jeff Garzikad355b42007-09-21 20:38:03 -04002386 return 0;
2387}
2388
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002389static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2390{
Grant Grundler295124d2010-08-17 10:56:53 -07002391 u16 min_io_sectors;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002392
2393 rbuf[1] = 0xb0;
2394 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2395
2396 /*
2397 * Optimal transfer length granularity.
2398 *
2399 * This is always one physical block, but for disks with a smaller
2400 * logical than physical sector size we need to figure out what the
2401 * latter is.
2402 */
Grant Grundler295124d2010-08-17 10:56:53 -07002403 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002404 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2405
2406 /*
2407 * Optimal unmap granularity.
2408 *
2409 * The ATA spec doesn't even know about a granularity or alignment
2410 * for the TRIM command. We can leave away most of the unmap related
2411 * VPD page entries, but we have specifify a granularity to signal
2412 * that we support some form of unmap - in thise case via WRITE SAME
2413 * with the unmap bit set.
2414 */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002415 if (ata_id_has_trim(args->id)) {
Tom Yan29838602016-07-13 04:31:23 +08002416 put_unaligned_be64(65535 * ATA_MAX_TRIM_RNUM, &rbuf[36]);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002417 put_unaligned_be32(1, &rbuf[28]);
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002418 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002419
2420 return 0;
2421}
2422
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002423static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2424{
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002425 int form_factor = ata_id_form_factor(args->id);
2426 int media_rotation_rate = ata_id_rotation_rate(args->id);
Hannes Reinecke856c4662016-04-25 12:45:55 +02002427 u8 zoned = ata_id_zoned_cap(args->id);
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002428
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002429 rbuf[1] = 0xb1;
2430 rbuf[3] = 0x3c;
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002431 rbuf[4] = media_rotation_rate >> 8;
2432 rbuf[5] = media_rotation_rate;
2433 rbuf[7] = form_factor;
Hannes Reinecke856c4662016-04-25 12:45:55 +02002434 if (zoned)
2435 rbuf[8] = (zoned << 4);
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002436
2437 return 0;
2438}
2439
Martin K. Petersen02e0a602010-09-10 01:23:18 -04002440static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
2441{
2442 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2443 rbuf[1] = 0xb2;
2444 rbuf[3] = 0x4;
2445 rbuf[5] = 1 << 6; /* TPWS */
2446
2447 return 0;
2448}
2449
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002450static unsigned int ata_scsiop_inq_b6(struct ata_scsi_args *args, u8 *rbuf)
2451{
2452 /*
2453 * zbc-r05 SCSI Zoned Block device characteristics VPD page
2454 */
2455 rbuf[1] = 0xb6;
2456 rbuf[3] = 0x3C;
2457
2458 /*
2459 * URSWRZ bit is only meaningful for host-managed ZAC drives
2460 */
2461 if (args->dev->zac_zoned_cap & 1)
2462 rbuf[4] |= 1;
2463 put_unaligned_be32(args->dev->zac_zones_optimal_open, &rbuf[8]);
2464 put_unaligned_be32(args->dev->zac_zones_optimal_nonseq, &rbuf[12]);
2465 put_unaligned_be32(args->dev->zac_zones_max_open, &rbuf[16]);
2466
2467 return 0;
2468}
2469
Jeff Garzikad355b42007-09-21 20:38:03 -04002470/**
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002471 * modecpy - Prepare response for MODE SENSE
2472 * @dest: output buffer
2473 * @src: data being copied
2474 * @n: length of mode page
2475 * @changeable: whether changeable parameters are requested
2476 *
2477 * Generate a generic MODE SENSE page for either current or changeable
2478 * parameters.
2479 *
2480 * LOCKING:
2481 * None.
2482 */
2483static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2484{
2485 if (changeable) {
2486 memcpy(dest, src, 2);
2487 memset(dest + 2, 0, n - 2);
2488 } else {
2489 memcpy(dest, src, n);
2490 }
2491}
2492
2493/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 * ata_msense_caching - Simulate MODE SENSE caching info page
2495 * @id: device IDENTIFY data
Tejun Heo87340e92008-04-28 17:48:51 +09002496 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002497 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 *
2499 * Generate a caching info page, which conditionally indicates
2500 * write caching to the SCSI layer, depending on device
2501 * capabilities.
2502 *
2503 * LOCKING:
2504 * None.
2505 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002506static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002508 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
Tom Yan737bee92016-07-20 04:39:28 +08002509 if (changeable) {
2510 buf[2] |= (1 << 2); /* ata_mselect_caching() */
2511 } else {
2512 buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */
2513 buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */
2514 }
Tejun Heo87340e92008-04-28 17:48:51 +09002515 return sizeof(def_cache_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
2518/**
Tom Yanf086b742016-07-13 02:54:12 +08002519 * ata_msense_control - Simulate MODE SENSE control mode page
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002520 * @dev: ATA device of interest
Tejun Heo87340e92008-04-28 17:48:51 +09002521 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002522 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 *
2524 * Generate a generic MODE SENSE control mode page.
2525 *
2526 * LOCKING:
2527 * None.
2528 */
Tom Yanf086b742016-07-13 02:54:12 +08002529static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002530 bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002532 modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
Tom Yan737bee92016-07-20 04:39:28 +08002533 if (changeable) {
2534 buf[2] |= (1 << 2); /* ata_mselect_control() */
2535 } else {
2536 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
2537
2538 buf[2] |= (d_sense << 2); /* descriptor format sense data */
2539 }
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002540 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542
2543/**
2544 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002545 * @buf: output buffer
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002546 * @changeable: whether changeable parameters are requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 *
2548 * Generate a generic MODE SENSE r/w error recovery page.
2549 *
2550 * LOCKING:
2551 * None.
2552 */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002553static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002555 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2556 changeable);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002557 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002560/*
2561 * We can turn this into a real blacklist if it's needed, for now just
2562 * blacklist any Maxtor BANC1G10 revision firmware
2563 */
2564static int ata_dev_supports_fua(u16 *id)
2565{
Tejun Heoa0cf7332007-01-02 20:18:49 +09002566 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002567
Jeff Garzikc3c013a2006-02-27 22:31:19 -05002568 if (!libata_fua)
2569 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002570 if (!ata_id_has_fua(id))
2571 return 0;
2572
Tejun Heoa0cf7332007-01-02 20:18:49 +09002573 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2574 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002575
Tejun Heo2e026712006-02-12 22:47:04 +09002576 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002577 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09002578 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002579 return 1;
2580
2581 return 0; /* blacklisted */
2582}
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584/**
2585 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2586 * @args: device IDENTIFY data / SCSI command of interest.
2587 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002589 * Simulate MODE SENSE commands. Assume this is invoked for direct
2590 * access devices (e.g. disks) only. There should be no block
2591 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 *
2593 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002594 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 */
Tejun Heo87340e92008-04-28 17:48:51 +09002596static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002598 struct ata_device *dev = args->dev;
Tejun Heo87340e92008-04-28 17:48:51 +09002599 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
Colin Ian Kinge94f79142017-09-19 09:39:52 +01002600 static const u8 sat_blk_desc[] = {
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002601 0, 0, 0, 0, /* number of blocks: sat unspecified */
2602 0,
2603 0, 0x2, 0x0 /* block length: 512 bytes */
2604 };
2605 u8 pg, spg;
Tejun Heo87340e92008-04-28 17:48:51 +09002606 unsigned int ebd, page_control, six_byte;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002607 u8 dpofua, bp = 0xff;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002608 u16 fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 VPRINTK("ENTER\n");
2611
2612 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002613 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2614 /*
2615 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002619 switch (page_control) {
2620 case 0: /* current */
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002621 case 1: /* changeable */
2622 case 2: /* defaults */
Douglas Gilbertae006512005-10-09 09:09:35 -04002623 break; /* supported */
2624 case 3: /* saved */
2625 goto saving_not_supp;
Douglas Gilbertae006512005-10-09 09:09:35 -04002626 default:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002627 fp = 2;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002628 bp = 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002629 goto invalid_fld;
2630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Tejun Heo87340e92008-04-28 17:48:51 +09002632 if (six_byte)
2633 p += 4 + (ebd ? 8 : 0);
2634 else
2635 p += 8 + (ebd ? 8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002637 pg = scsicmd[2] & 0x3f;
2638 spg = scsicmd[3];
2639 /*
2640 * No mode subpages supported (yet) but asking for _all_
2641 * subpages may be valid
2642 */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002643 if (spg && (spg != ALL_SUB_MPAGES)) {
2644 fp = 3;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002645 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002646 }
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002647
2648 switch(pg) {
2649 case RW_RECOVERY_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002650 p += ata_msense_rw_recovery(p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 break;
2652
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002653 case CACHE_MPAGE:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002654 p += ata_msense_caching(args->id, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 break;
2656
Tejun Heo87340e92008-04-28 17:48:51 +09002657 case CONTROL_MPAGE:
Tom Yanf086b742016-07-13 02:54:12 +08002658 p += ata_msense_control(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002661 case ALL_MPAGES:
Paolo Bonzini6ca8e792012-07-05 14:18:20 +02002662 p += ata_msense_rw_recovery(p, page_control == 1);
2663 p += ata_msense_caching(args->id, p, page_control == 1);
Tom Yanf086b742016-07-13 02:54:12 +08002664 p += ata_msense_control(args->dev, p, page_control == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 break;
2666
2667 default: /* invalid page code */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02002668 fp = 2;
Douglas Gilbertae006512005-10-09 09:09:35 -04002669 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
2671
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002672 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002673 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002674 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2675 dpofua = 1 << 4;
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 if (six_byte) {
Tejun Heo87340e92008-04-28 17:48:51 +09002678 rbuf[0] = p - rbuf - 1;
2679 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002680 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002681 rbuf[3] = sizeof(sat_blk_desc);
2682 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 } else {
Tejun Heo87340e92008-04-28 17:48:51 +09002685 unsigned int output_len = p - rbuf - 2;
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 rbuf[0] = output_len >> 8;
Tejun Heo87340e92008-04-28 17:48:51 +09002688 rbuf[1] = output_len;
2689 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002690 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002691 rbuf[7] = sizeof(sat_blk_desc);
2692 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002696
2697invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02002698 ata_scsi_set_invalid_field(dev, args->cmd, fp, bp);
Douglas Gilbertae006512005-10-09 09:09:35 -04002699 return 1;
2700
2701saving_not_supp:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02002702 ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04002703 /* "Saving parameters not supported" */
2704 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
2707/**
2708 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2709 * @args: device IDENTIFY data / SCSI command of interest.
2710 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 *
2712 * Simulate READ CAPACITY commands.
2713 *
2714 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002715 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 */
Tejun Heo87340e92008-04-28 17:48:51 +09002717static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002719 struct ata_device *dev = args->dev;
2720 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
Grant Grundler295124d2010-08-17 10:56:53 -07002721 u32 sector_size; /* physical sector size in bytes */
2722 u8 log2_per_phys;
2723 u16 lowest_aligned;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002724
Grant Grundler295124d2010-08-17 10:56:53 -07002725 sector_size = ata_id_logical_sector_size(dev->id);
2726 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2727 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 VPRINTK("ENTER\n");
2730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002732 if (last_lba >= 0xffffffffULL)
2733 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 /* sector count, 32-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002736 rbuf[0] = last_lba >> (8 * 3);
2737 rbuf[1] = last_lba >> (8 * 2);
2738 rbuf[2] = last_lba >> (8 * 1);
2739 rbuf[3] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002742 rbuf[4] = sector_size >> (8 * 3);
2743 rbuf[5] = sector_size >> (8 * 2);
2744 rbuf[6] = sector_size >> (8 * 1);
2745 rbuf[7] = sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 } else {
2747 /* sector count, 64-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002748 rbuf[0] = last_lba >> (8 * 7);
2749 rbuf[1] = last_lba >> (8 * 6);
2750 rbuf[2] = last_lba >> (8 * 5);
2751 rbuf[3] = last_lba >> (8 * 4);
2752 rbuf[4] = last_lba >> (8 * 3);
2753 rbuf[5] = last_lba >> (8 * 2);
2754 rbuf[6] = last_lba >> (8 * 1);
2755 rbuf[7] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002758 rbuf[ 8] = sector_size >> (8 * 3);
2759 rbuf[ 9] = sector_size >> (8 * 2);
2760 rbuf[10] = sector_size >> (8 * 1);
2761 rbuf[11] = sector_size;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002762
2763 rbuf[12] = 0;
Grant Grundler295124d2010-08-17 10:56:53 -07002764 rbuf[13] = log2_per_phys;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002765 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2766 rbuf[15] = lowest_aligned;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002767
Arne Fitzenreiter71d126f2015-07-15 13:54:36 +02002768 if (ata_id_has_trim(args->id) &&
2769 !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002770 rbuf[14] |= 0x80; /* LBPME */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002771
Martin K. Petersene61f7d12015-01-08 10:34:27 -05002772 if (ata_id_has_zero_after_trim(args->id) &&
2773 dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
2774 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2775 rbuf[14] |= 0x40; /* LBPRZ */
2776 }
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002777 }
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02002778 if (ata_id_zoned_cap(args->id) ||
2779 args->dev->class == ATA_DEV_ZAC)
2780 rbuf[12] = (1 << 4); /* RC_BASIS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return 0;
2783}
2784
2785/**
2786 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2787 * @args: device IDENTIFY data / SCSI command of interest.
2788 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 *
2790 * Simulate REPORT LUNS command.
2791 *
2792 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002793 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 */
Tejun Heo87340e92008-04-28 17:48:51 +09002795static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
2797 VPRINTK("ENTER\n");
2798 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2799
2800 return 0;
2801}
2802
Tejun Heo77853bf2006-01-23 13:09:36 +09002803static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002804{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002805 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002806 /* FIXME: not quite right; we don't want the
2807 * translation of taskfile registers into
2808 * a sense descriptors, since that's only
2809 * correct for ATA, not ATAPI
2810 */
Tejun Heo750426a2006-11-14 22:37:35 +09002811 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002812 }
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002813
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002814 ata_qc_done(qc);
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002815}
2816
2817/* is it pointless to prefer PIO for "safety reasons"? */
2818static inline int ata_pio_use_silly(struct ata_port *ap)
2819{
2820 return (ap->flags & ATA_FLAG_PIO_DMA);
2821}
2822
2823static void atapi_request_sense(struct ata_queued_cmd *qc)
2824{
2825 struct ata_port *ap = qc->ap;
2826 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002827
2828 DPRINTK("ATAPI request sense\n");
2829
James Bottomley7ccd7202008-01-17 11:56:24 -06002830 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Jeff Garzika939c962005-10-05 17:09:16 -04002831
Tejun Heo127102a2008-04-07 22:47:21 +09002832#ifdef CONFIG_ATA_SFF
James Bottomley855d8542008-04-18 13:18:48 -05002833 if (ap->ops->sff_tf_read)
2834 ap->ops->sff_tf_read(ap, &qc->tf);
Tejun Heo127102a2008-04-07 22:47:21 +09002835#endif
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002836
2837 /* fill these in, for the case where they are -not- overwritten */
2838 cmd->sense_buffer[0] = 0x70;
2839 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2840
2841 ata_qc_reinit(qc);
2842
Tejun Heo93f8fec2007-12-05 16:43:01 +09002843 /* setup sg table and init transfer direction */
akpm@linux-foundation.orgcadb7342008-01-15 16:01:52 -08002844 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
Tejun Heo93f8fec2007-12-05 16:43:01 +09002845 ata_sg_init(qc, &qc->sgent, 1);
Jeff Garzika939c962005-10-05 17:09:16 -04002846 qc->dma_dir = DMA_FROM_DEVICE;
2847
Tejun Heo6e7846e2006-02-12 23:32:58 +09002848 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002849 qc->cdb[0] = REQUEST_SENSE;
2850 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2851
2852 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2853 qc->tf.command = ATA_CMD_PACKET;
2854
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002855 if (ata_pio_use_silly(ap)) {
Tejun Heo0dc36882007-12-18 16:34:43 -05002856 qc->tf.protocol = ATAPI_PROT_DMA;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002857 qc->tf.feature |= ATAPI_PKT_DMA;
2858 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05002859 qc->tf.protocol = ATAPI_PROT_PIO;
Alan Cox2db78dd2007-10-02 13:53:04 -07002860 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2861 qc->tf.lbah = 0;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002862 }
Jeff Garzika939c962005-10-05 17:09:16 -04002863 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2864
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002865 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002866
Tejun Heo8e0e6942006-03-31 20:41:11 +09002867 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002868
2869 DPRINTK("EXIT\n");
2870}
2871
Christoph Hellwigaa18da82017-01-10 09:41:43 +01002872/*
2873 * ATAPI devices typically report zero for their SCSI version, and sometimes
2874 * deviate from the spec WRT response data format. If SCSI version is
2875 * reported as zero like normal, then we make the following fixups:
2876 * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a
2877 * modern device.
2878 * 2) Ensure response data format / ATAPI information are always correct.
2879 */
2880static void atapi_fixup_inquiry(struct scsi_cmnd *cmd)
2881{
2882 u8 buf[4];
2883
2884 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2885 if (buf[2] == 0) {
2886 buf[2] = 0x5;
2887 buf[3] = 0x32;
2888 }
2889 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2890}
2891
Tejun Heo77853bf2006-01-23 13:09:36 +09002892static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
2894 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002895 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Jeff Garzika7dac442005-10-30 04:44:42 -05002897 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002898
Tejun Heo246619d2006-05-15 20:58:16 +09002899 /* handle completion from new EH */
2900 if (unlikely(qc->ap->ops->error_handler &&
2901 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2902
2903 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2904 /* FIXME: not quite right; we don't want the
2905 * translation of taskfile registers into a
2906 * sense descriptors, since that's only
2907 * correct for ATA, not ATAPI
2908 */
Tejun Heo750426a2006-11-14 22:37:35 +09002909 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002910 }
2911
Tejun Heo22aac082006-08-08 14:08:59 +09002912 /* SCSI EH automatically locks door if sdev->locked is
2913 * set. Sometimes door lock request continues to
2914 * fail, for example, when no media is present. This
2915 * creates a loop - SCSI EH issues door lock which
2916 * fails and gets invoked again to acquire sense data
2917 * for the failed command.
2918 *
2919 * If door lock fails, always clear sdev->locked to
2920 * avoid this infinite loop.
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002921 *
2922 * This may happen before SCSI scan is complete. Make
2923 * sure qc->dev->sdev isn't NULL before dereferencing.
Tejun Heo22aac082006-08-08 14:08:59 +09002924 */
Tejun Heo2a5f07b2010-11-01 11:39:19 +01002925 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
Tejun Heo22aac082006-08-08 14:08:59 +09002926 qc->dev->sdev->locked = 0;
2927
Tejun Heo246619d2006-05-15 20:58:16 +09002928 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002929 ata_qc_done(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002930 return;
2931 }
2932
2933 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002934 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002936 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002937 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002938 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002939 /* FIXME: not quite right; we don't want the
2940 * translation of taskfile registers into
2941 * a sense descriptors, since that's only
2942 * correct for ATA, not ATAPI
2943 */
Tejun Heo750426a2006-11-14 22:37:35 +09002944 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002945 } else {
Christoph Hellwigaa18da82017-01-10 09:41:43 +01002946 if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0)
2947 atapi_fixup_inquiry(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 cmd->result = SAM_STAT_GOOD;
2949 }
2950
Christoph Hellwig2aa8f5d2015-10-08 10:25:41 +02002951 ata_qc_done(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953/**
2954 * atapi_xlat - Initialize PACKET taskfile
2955 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 *
2957 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002958 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 *
2960 * RETURNS:
2961 * Zero on success, non-zero on failure.
2962 */
Tejun Heoad706992006-12-17 10:45:57 +09002963static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Tejun Heo542b1442006-12-17 10:45:08 +09002965 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 struct ata_device *dev = qc->dev;
Tejun Heo542b1442006-12-17 10:45:08 +09002967 int nodata = (scmd->sc_data_direction == DMA_NONE);
Tejun Heo5895ef92008-06-17 12:36:26 +09002968 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
Alan Cox2db78dd2007-10-02 13:53:04 -07002969 unsigned int nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Tejun Heo2e5704f2006-12-17 10:46:33 +09002971 memset(qc->cdb, 0, dev->cdb_len);
2972 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 qc->complete_fn = atapi_qc_complete;
2975
2976 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002977 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 qc->tf.flags |= ATA_TFLAG_WRITE;
2979 DPRINTK("direction: write\n");
2980 }
2981
2982 qc->tf.command = ATA_CMD_PACKET;
Tejun Heoaacda372008-03-18 17:47:43 +09002983 ata_qc_set_pc_nbytes(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002985 /* check whether ATAPI DMA is safe */
Tejun Heo5895ef92008-06-17 12:36:26 +09002986 if (!nodata && !using_pio && atapi_check_dma(qc))
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002987 using_pio = 1;
2988
Tejun Heoe1902222007-11-26 20:58:02 +09002989 /* Some controller variants snoop this value for Packet
2990 * transfers to do state machine and FIFO management. Thus we
2991 * want to set it properly, and for DMA where it is
2992 * effectively meaningless.
2993 */
Tejun Heoaacda372008-03-18 17:47:43 +09002994 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
Alan Cox2db78dd2007-10-02 13:53:04 -07002995
Tejun Heoe1902222007-11-26 20:58:02 +09002996 /* Most ATAPI devices which honor transfer chunk size don't
2997 * behave according to the spec when odd chunk size which
2998 * matches the transfer length is specified. If the number of
2999 * bytes to transfer is 2n+1. According to the spec, what
3000 * should happen is to indicate that 2n+1 is going to be
3001 * transferred and transfer 2n+2 bytes where the last byte is
3002 * padding.
3003 *
3004 * In practice, this doesn't happen. ATAPI devices first
3005 * indicate and transfer 2n bytes and then indicate and
3006 * transfer 2 bytes where the last byte is padding.
3007 *
3008 * This inconsistency confuses several controllers which
3009 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
3010 * These controllers use actual number of transferred bytes to
Chengguang Xu8d1e02d2019-02-10 23:19:01 +08003011 * update DMA pointer and transfer of 4n+2 bytes make those
Tejun Heoe1902222007-11-26 20:58:02 +09003012 * controller push DMA pointer by 4n+4 bytes because SATA data
3013 * FISes are aligned to 4 bytes. This causes data corruption
3014 * and buffer overrun.
3015 *
3016 * Always setting nbytes to even number solves this problem
3017 * because then ATAPI devices don't have to split data at 2n
3018 * boundaries.
3019 */
3020 if (nbytes & 0x1)
3021 nbytes++;
3022
Alan Cox2db78dd2007-10-02 13:53:04 -07003023 qc->tf.lbam = (nbytes & 0xFF);
3024 qc->tf.lbah = (nbytes >> 8);
3025
Tejun Heo5895ef92008-06-17 12:36:26 +09003026 if (nodata)
3027 qc->tf.protocol = ATAPI_PROT_NODATA;
3028 else if (using_pio)
3029 qc->tf.protocol = ATAPI_PROT_PIO;
3030 else {
Tejun Heoe00f1ff2007-06-27 02:47:35 +09003031 /* DMA data xfer */
Tejun Heo0dc36882007-12-18 16:34:43 -05003032 qc->tf.protocol = ATAPI_PROT_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 qc->tf.feature |= ATAPI_PKT_DMA;
3034
Tejun Heo91163002008-02-21 13:25:50 +09003035 if ((dev->flags & ATA_DFLAG_DMADIR) &&
3036 (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08003037 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 }
3040
Alan Cox2db78dd2007-10-02 13:53:04 -07003041
3042 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
3043 as ATAPI tape drives don't get this right otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return 0;
3045}
3046
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003047static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
Tejun Heoab5b3a52006-05-31 18:27:34 +09003048{
Tejun Heo071f44b2008-04-07 22:47:22 +09003049 if (!sata_pmp_attached(ap)) {
Dan Carpenter59a5e262017-07-19 13:06:41 +03003050 if (likely(devno >= 0 &&
3051 devno < ata_link_max_devices(&ap->link)))
Tejun Heo41bda9c2007-08-06 18:36:24 +09003052 return &ap->link.device[devno];
3053 } else {
Dan Carpenter59a5e262017-07-19 13:06:41 +03003054 if (likely(devno >= 0 &&
3055 devno < ap->nr_pmp_links))
Tejun Heo41bda9c2007-08-06 18:36:24 +09003056 return &ap->pmp_link[devno].device[0];
3057 }
3058
Tejun Heoab5b3a52006-05-31 18:27:34 +09003059 return NULL;
3060}
3061
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003062static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
3063 const struct scsi_device *scsidev)
Tejun Heoab5b3a52006-05-31 18:27:34 +09003064{
Tejun Heo41bda9c2007-08-06 18:36:24 +09003065 int devno;
Tejun Heoab5b3a52006-05-31 18:27:34 +09003066
Tejun Heo41bda9c2007-08-06 18:36:24 +09003067 /* skip commands not addressed to targets we simulate */
Tejun Heo071f44b2008-04-07 22:47:22 +09003068 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003069 if (unlikely(scsidev->channel || scsidev->lun))
3070 return NULL;
3071 devno = scsidev->id;
3072 } else {
3073 if (unlikely(scsidev->id || scsidev->lun))
3074 return NULL;
3075 devno = scsidev->channel;
3076 }
3077
3078 return ata_find_dev(ap, devno);
Tejun Heoab5b3a52006-05-31 18:27:34 +09003079}
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081/**
3082 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
3083 * @ap: ATA port to which the device is attached
3084 * @scsidev: SCSI device from which we derive the ATA device
3085 *
3086 * Given various information provided in struct scsi_cmnd,
3087 * map that onto an ATA bus, and using that mapping
3088 * determine which ata_device is associated with the
3089 * SCSI command to be sent.
3090 *
3091 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003092 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 *
3094 * RETURNS:
3095 * Associated ATA device, or %NULL if not found.
3096 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04003098ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Tejun Heoab5b3a52006-05-31 18:27:34 +09003100 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Tejun Heo2486fa52008-07-31 07:52:40 +09003102 if (unlikely(!dev || !ata_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return NULL;
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 return dev;
3106}
3107
Jeff Garzikb0955182005-05-12 15:45:22 -04003108/*
3109 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
3110 * @byte1: Byte 1 from pass-thru CDB.
3111 *
3112 * RETURNS:
3113 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
3114 */
3115static u8
3116ata_scsi_map_proto(u8 byte1)
3117{
3118 switch((byte1 & 0x1e) >> 1) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003119 case 3: /* Non-data */
3120 return ATA_PROT_NODATA;
Jeff Garzikb0955182005-05-12 15:45:22 -04003121
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003122 case 6: /* DMA */
3123 case 10: /* UDMA Data-in */
3124 case 11: /* UDMA Data-Out */
3125 return ATA_PROT_DMA;
Jeff Garzikb0955182005-05-12 15:45:22 -04003126
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003127 case 4: /* PIO Data-in */
3128 case 5: /* PIO Data-out */
3129 return ATA_PROT_PIO;
Jeff Garzikb0955182005-05-12 15:45:22 -04003130
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303131 case 12: /* FPDMA */
3132 return ATA_PROT_NCQ;
3133
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003134 case 0: /* Hard Reset */
3135 case 1: /* SRST */
3136 case 8: /* Device Diagnostic */
3137 case 9: /* Device Reset */
3138 case 7: /* DMA Queued */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003139 case 15: /* Return Response Info */
3140 default: /* Reserved */
3141 break;
Jeff Garzikb0955182005-05-12 15:45:22 -04003142 }
3143
3144 return ATA_PROT_UNKNOWN;
3145}
3146
3147/**
3148 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
3149 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04003150 *
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003151 * Handles either 12, 16, or 32-byte versions of the CDB.
Jeff Garzikb0955182005-05-12 15:45:22 -04003152 *
3153 * RETURNS:
3154 * Zero on success, non-zero on failure.
3155 */
Tejun Heoad706992006-12-17 10:45:57 +09003156static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04003157{
3158 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09003159 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01003160 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09003161 const u8 *cdb = scmd->cmnd;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003162 u16 fp;
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003163 u16 cdb_offset = 0;
Jeff Garzikb0955182005-05-12 15:45:22 -04003164
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003165 /* 7Fh variable length cmd means a ata pass-thru(32) */
3166 if (cdb[0] == VARIABLE_LENGTH_CMD)
3167 cdb_offset = 9;
3168
3169 tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]);
3170 if (tf->protocol == ATA_PROT_UNKNOWN) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003171 fp = 1;
Tejun Heo9a405252005-12-02 11:49:11 +09003172 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003173 }
Jeff Garzik8190bdb2006-05-24 01:53:39 -04003174
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003175 if (ata_is_ncq(tf->protocol) && (cdb[2 + cdb_offset] & 0x3) == 0)
Hannes Reinecke5b844b62016-07-14 09:05:48 +09003176 tf->protocol = ATA_PROT_NCQ_NODATA;
3177
Vinayak Kale5f8e7f12015-10-27 13:11:46 +05303178 /* enable LBA */
3179 tf->flags |= ATA_TFLAG_LBA;
3180
Alan Coxae8d4ee2007-11-04 22:05:49 -05003181 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04003182 * 12 and 16 byte CDBs use different offsets to
3183 * provide the various register values.
3184 */
Tejun Heo542b1442006-12-17 10:45:08 +09003185 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04003186 /*
3187 * 16-byte CDB - may contain extended commands.
3188 *
3189 * If that is the case, copy the upper byte register values.
3190 */
Tejun Heo542b1442006-12-17 10:45:08 +09003191 if (cdb[1] & 0x01) {
3192 tf->hob_feature = cdb[3];
3193 tf->hob_nsect = cdb[5];
3194 tf->hob_lbal = cdb[7];
3195 tf->hob_lbam = cdb[9];
3196 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04003197 tf->flags |= ATA_TFLAG_LBA48;
3198 } else
3199 tf->flags &= ~ATA_TFLAG_LBA48;
3200
3201 /*
3202 * Always copy low byte, device and command registers.
3203 */
Tejun Heo542b1442006-12-17 10:45:08 +09003204 tf->feature = cdb[4];
3205 tf->nsect = cdb[6];
3206 tf->lbal = cdb[8];
3207 tf->lbam = cdb[10];
3208 tf->lbah = cdb[12];
3209 tf->device = cdb[13];
3210 tf->command = cdb[14];
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003211 } else if (cdb[0] == ATA_12) {
Jeff Garzikb0955182005-05-12 15:45:22 -04003212 /*
3213 * 12-byte CDB - incapable of extended commands.
3214 */
3215 tf->flags &= ~ATA_TFLAG_LBA48;
3216
Tejun Heo542b1442006-12-17 10:45:08 +09003217 tf->feature = cdb[3];
3218 tf->nsect = cdb[4];
3219 tf->lbal = cdb[5];
3220 tf->lbam = cdb[6];
3221 tf->lbah = cdb[7];
3222 tf->device = cdb[8];
3223 tf->command = cdb[9];
Minwoo Imb1ffbf82017-06-24 03:41:10 +09003224 } else {
3225 /*
3226 * 32-byte CDB - may contain extended command fields.
3227 *
3228 * If that is the case, copy the upper byte register values.
3229 */
3230 if (cdb[10] & 0x01) {
3231 tf->hob_feature = cdb[20];
3232 tf->hob_nsect = cdb[22];
3233 tf->hob_lbal = cdb[16];
3234 tf->hob_lbam = cdb[15];
3235 tf->hob_lbah = cdb[14];
3236 tf->flags |= ATA_TFLAG_LBA48;
3237 } else
3238 tf->flags &= ~ATA_TFLAG_LBA48;
3239
3240 tf->feature = cdb[21];
3241 tf->nsect = cdb[23];
3242 tf->lbal = cdb[19];
3243 tf->lbam = cdb[18];
3244 tf->lbah = cdb[17];
3245 tf->device = cdb[24];
3246 tf->command = cdb[25];
3247 tf->auxiliary = get_unaligned_be32(&cdb[28]);
Jeff Garzikb0955182005-05-12 15:45:22 -04003248 }
Albert Leefa4453c2007-06-07 15:52:07 +08003249
Hannes Reinecke179b3102016-07-14 09:05:43 +09003250 /* For NCQ commands copy the tag value */
3251 if (ata_is_ncq(tf->protocol))
Jens Axboe4e5b6262018-05-11 12:51:04 -06003252 tf->nsect = qc->hw_tag << 3;
Vinayak Kaleee7fb332015-10-27 11:19:15 +05303253
Albert Leefa4453c2007-06-07 15:52:07 +08003254 /* enforce correct master/slave bit */
3255 tf->device = dev->devno ?
3256 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003257
Tejun Heobd30add2009-09-03 16:08:11 +09003258 switch (tf->command) {
Grant Grundler295124d2010-08-17 10:56:53 -07003259 /* READ/WRITE LONG use a non-standard sect_size */
Tejun Heobd30add2009-09-03 16:08:11 +09003260 case ATA_CMD_READ_LONG:
3261 case ATA_CMD_READ_LONG_ONCE:
3262 case ATA_CMD_WRITE_LONG:
3263 case ATA_CMD_WRITE_LONG_ONCE:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003264 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
3265 fp = 1;
Tejun Heobd30add2009-09-03 16:08:11 +09003266 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003267 }
Tejun Heobd30add2009-09-03 16:08:11 +09003268 qc->sect_size = scsi_bufflen(scmd);
Grant Grundler295124d2010-08-17 10:56:53 -07003269 break;
3270
3271 /* commands using reported Logical Block size (e.g. 512 or 4K) */
3272 case ATA_CMD_CFA_WRITE_NE:
3273 case ATA_CMD_CFA_TRANS_SECT:
3274 case ATA_CMD_CFA_WRITE_MULT_NE:
3275 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
3276 case ATA_CMD_READ:
3277 case ATA_CMD_READ_EXT:
3278 case ATA_CMD_READ_QUEUED:
3279 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
3280 case ATA_CMD_FPDMA_READ:
3281 case ATA_CMD_READ_MULTI:
3282 case ATA_CMD_READ_MULTI_EXT:
3283 case ATA_CMD_PIO_READ:
3284 case ATA_CMD_PIO_READ_EXT:
3285 case ATA_CMD_READ_STREAM_DMA_EXT:
3286 case ATA_CMD_READ_STREAM_EXT:
3287 case ATA_CMD_VERIFY:
3288 case ATA_CMD_VERIFY_EXT:
3289 case ATA_CMD_WRITE:
3290 case ATA_CMD_WRITE_EXT:
3291 case ATA_CMD_WRITE_FUA_EXT:
3292 case ATA_CMD_WRITE_QUEUED:
3293 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3294 case ATA_CMD_FPDMA_WRITE:
3295 case ATA_CMD_WRITE_MULTI:
3296 case ATA_CMD_WRITE_MULTI_EXT:
3297 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3298 case ATA_CMD_PIO_WRITE:
3299 case ATA_CMD_PIO_WRITE_EXT:
3300 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3301 case ATA_CMD_WRITE_STREAM_EXT:
3302 qc->sect_size = scmd->device->sector_size;
3303 break;
3304
3305 /* Everything else uses 512 byte "sectors" */
3306 default:
3307 qc->sect_size = ATA_SECT_SIZE;
Tejun Heobd30add2009-09-03 16:08:11 +09003308 }
3309
3310 /*
3311 * Set flags so that all registers will be written, pass on
3312 * write indication (used for PIO/DMA setup), result TF is
3313 * copied back and we don't whine too much about its failure.
3314 */
Douglas Gilbertbc496ed2010-02-01 13:11:38 -05003315 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heobd30add2009-09-03 16:08:11 +09003316 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3317 tf->flags |= ATA_TFLAG_WRITE;
3318
3319 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3320
3321 /*
3322 * Set transfer length.
3323 *
3324 * TODO: find out if we need to do more here to
3325 * cover scatter/gather case.
3326 */
3327 ata_qc_set_pc_nbytes(qc);
3328
3329 /* We may not issue DMA commands if no DMA mode is set */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003330 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) {
3331 fp = 1;
Tejun Heobd30add2009-09-03 16:08:11 +09003332 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003333 }
Tejun Heobd30add2009-09-03 16:08:11 +09003334
Eric Biggers2c1ec6f2018-02-03 20:33:51 -08003335 /* We may not issue NCQ commands to devices not supporting NCQ */
3336 if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) {
3337 fp = 1;
3338 goto invalid_fld;
3339 }
3340
Albert Lee1dce5892007-06-07 15:49:22 +08003341 /* sanity check for pio multi commands */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003342 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
3343 fp = 1;
Albert Lee1dce5892007-06-07 15:49:22 +08003344 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003345 }
Albert Lee1dce5892007-06-07 15:49:22 +08003346
3347 if (is_multi_taskfile(tf)) {
3348 unsigned int multi_count = 1 << (cdb[1] >> 5);
3349
3350 /* compare the passed through multi_count
3351 * with the cached multi_count of libata
3352 */
3353 if (multi_count != dev->multi_count)
Joe Perchesa9a79df2011-04-15 15:51:59 -07003354 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3355 multi_count);
Alan Coxd26fc952007-07-06 19:13:52 -04003356 }
Albert Lee1dce5892007-06-07 15:49:22 +08003357
Jeff Garzikb0955182005-05-12 15:45:22 -04003358 /*
3359 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3360 * SET_FEATURES - XFER MODE must be preceded/succeeded
3361 * by an update to hardware-specific registers for each
3362 * controller (i.e. the reason for ->set_piomode(),
3363 * ->set_dmamode(), and ->post_set_mode() hooks).
3364 */
Tejun Heobd30add2009-09-03 16:08:11 +09003365 if (tf->command == ATA_CMD_SET_FEATURES &&
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003366 tf->feature == SETFEATURES_XFER) {
3367 fp = (cdb[0] == ATA_16) ? 4 : 3;
Tejun Heo9a405252005-12-02 11:49:11 +09003368 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003369 }
Jeff Garzikb0955182005-05-12 15:45:22 -04003370
3371 /*
Tejun Heobd30add2009-09-03 16:08:11 +09003372 * Filter TPM commands by default. These provide an
3373 * essentially uncontrolled encrypted "back door" between
3374 * applications and the disk. Set libata.allow_tpm=1 if you
3375 * have a real reason for wanting to use them. This ensures
3376 * that installed software cannot easily mess stuff up without
3377 * user intent. DVR type users will probably ship with this enabled
3378 * for movie content management.
Jeff Garzikb0955182005-05-12 15:45:22 -04003379 *
Tejun Heobd30add2009-09-03 16:08:11 +09003380 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3381 * for this and should do in future but that it is not sufficient as
3382 * DCS is an optional feature set. Thus we also do the software filter
3383 * so that we comply with the TC consortium stated goal that the user
3384 * can turn off TC features of their system.
Jeff Garzikb0955182005-05-12 15:45:22 -04003385 */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003386 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
3387 fp = (cdb[0] == ATA_16) ? 14 : 9;
Tejun Heobd30add2009-09-03 16:08:11 +09003388 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003389 }
Tejun Heoe61e0672006-05-15 20:57:40 +09003390
Jeff Garzikb0955182005-05-12 15:45:22 -04003391 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09003392
3393 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003394 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
Tejun Heo9a405252005-12-02 11:49:11 +09003395 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04003396}
3397
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003398/**
3399 * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim
3400 * @cmd: SCSI command being translated
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003401 * @trmax: Maximum number of entries that will fit in sector_size bytes.
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003402 * @sector: Starting sector
Shaun Tancheff7b203092016-08-21 23:23:19 -05003403 * @count: Total Range of request in logical sectors
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003404 *
3405 * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted
3406 * descriptor.
3407 *
3408 * Upto 64 entries of the format:
3409 * 63:48 Range Length
3410 * 47:0 LBA
3411 *
3412 * Range Length of 0 is ignored.
3413 * LBA's should be sorted order and not overlap.
3414 *
3415 * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003416 *
3417 * Return: Number of bytes copied into sglist.
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003418 */
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003419static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax,
3420 u64 sector, u32 count)
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003421{
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003422 struct scsi_device *sdp = cmd->device;
3423 size_t len = sdp->sector_size;
3424 size_t r;
3425 __le64 *buf;
3426 u32 i = 0;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003427 unsigned long flags;
3428
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003429 WARN_ON(len > ATA_SCSI_RBUF_SIZE);
3430
3431 if (len > ATA_SCSI_RBUF_SIZE)
3432 len = ATA_SCSI_RBUF_SIZE;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003433
3434 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003435 buf = ((void *)ata_scsi_rbuf);
3436 memset(buf, 0, len);
3437 while (i < trmax) {
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003438 u64 entry = sector |
3439 ((u64)(count > 0xffff ? 0xffff : count) << 48);
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003440 buf[i++] = __cpu_to_le64(entry);
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003441 if (count <= 0xffff)
3442 break;
3443 count -= 0xffff;
3444 sector += 0xffff;
3445 }
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003446 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003447 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
3448
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003449 return r;
Shaun Tancheff9379e6b2016-08-21 23:23:18 -05003450}
3451
Shaun Tancheff7b203092016-08-21 23:23:19 -05003452/**
Shaun Tancheff7b203092016-08-21 23:23:19 -05003453 * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same
3454 * @qc: Command to be translated
3455 *
3456 * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or
3457 * an SCT Write Same command.
Mauro Carvalho Chehab6baf20b2017-05-16 09:16:18 -03003458 * Based on WRITE SAME has the UNMAP flag:
3459 *
3460 * - When set translate to DSM TRIM
3461 * - When clear translate to SCT Write Same
Shaun Tancheff7b203092016-08-21 23:23:19 -05003462 */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003463static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3464{
3465 struct ata_taskfile *tf = &qc->tf;
3466 struct scsi_cmnd *scmd = qc->scsicmd;
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003467 struct scsi_device *sdp = scmd->device;
3468 size_t len = sdp->sector_size;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003469 struct ata_device *dev = qc->dev;
3470 const u8 *cdb = scmd->cmnd;
3471 u64 block;
3472 u32 n_block;
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003473 const u32 trmax = len >> 3;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003474 u32 size;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003475 u16 fp;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003476 u8 bp = 0xff;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003477 u8 unmap = cdb[1] & 0x8;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003478
3479 /* we may not issue DMA commands if no DMA mode is set */
3480 if (unlikely(!dev->dma_mode))
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003481 goto invalid_opcode;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003482
Christoph Hellwigc6ade202017-04-25 13:39:54 +02003483 /*
3484 * We only allow sending this command through the block layer,
3485 * as it modifies the DATA OUT buffer, which would corrupt user
3486 * memory for SG_IO commands.
3487 */
3488 if (unlikely(blk_rq_is_passthrough(scmd->request)))
3489 goto invalid_opcode;
3490
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003491 if (unlikely(scmd->cmd_len < 16)) {
3492 fp = 15;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003493 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003494 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003495 scsi_16_lba_len(cdb, &block, &n_block);
3496
Christoph Hellwig63ccc192017-04-25 14:26:52 +02003497 if (!unmap ||
3498 (dev->horkage & ATA_HORKAGE_NOTRIM) ||
3499 !ata_id_has_trim(dev->id)) {
3500 fp = 1;
3501 bp = 3;
3502 goto invalid_fld;
3503 }
3504 /* If the request is too large the cmd is invalid */
3505 if (n_block > 0xffff * trmax) {
3506 fp = 2;
3507 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003508 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003509
3510 /*
3511 * WRITE SAME always has a sector sized buffer as payload, this
3512 * should never be a multiple entry S/G list.
3513 */
3514 if (!scsi_sg_count(scmd))
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003515 goto invalid_param_len;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003516
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003517 /*
3518 * size must match sector size in bytes
3519 * For DATA SET MANAGEMENT TRIM in ACS-2 nsect (aka count)
3520 * is defined as number of 512 byte blocks to be transferred.
3521 */
Shaun Tancheffef2d7392016-08-24 13:08:14 -05003522
Christoph Hellwig63ccc192017-04-25 14:26:52 +02003523 size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block);
3524 if (size != len)
3525 goto invalid_param_len;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003526
Christoph Hellwig63ccc192017-04-25 14:26:52 +02003527 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3528 /* Newer devices support queued TRIM commands */
3529 tf->protocol = ATA_PROT_NCQ;
3530 tf->command = ATA_CMD_FPDMA_SEND;
3531 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
Jens Axboe4e5b6262018-05-11 12:51:04 -06003532 tf->nsect = qc->hw_tag << 3;
Christoph Hellwig63ccc192017-04-25 14:26:52 +02003533 tf->hob_feature = (size / 512) >> 8;
3534 tf->feature = size / 512;
3535
3536 tf->auxiliary = 1;
Marc Carino87fb6c32013-08-24 23:22:51 -07003537 } else {
Shaun Tancheff7b203092016-08-21 23:23:19 -05003538 tf->protocol = ATA_PROT_DMA;
Christoph Hellwig63ccc192017-04-25 14:26:52 +02003539 tf->hob_feature = 0;
3540 tf->feature = ATA_DSM_TRIM;
3541 tf->hob_nsect = (size / 512) >> 8;
3542 tf->nsect = size / 512;
3543 tf->command = ATA_CMD_DSM;
Marc Carino87fb6c32013-08-24 23:22:51 -07003544 }
3545
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003546 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3547 ATA_TFLAG_WRITE;
3548
3549 ata_qc_set_pc_nbytes(qc);
3550
3551 return 0;
3552
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003553invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02003554 ata_scsi_set_invalid_field(dev, scmd, fp, bp);
Hannes Reineckebcfc8672016-04-04 11:44:05 +02003555 return 1;
3556invalid_param_len:
3557 /* "Parameter list length error" */
3558 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3559 return 1;
3560invalid_opcode:
3561 /* "Invalid command operation code" */
3562 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003563 return 1;
3564}
3565
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566/**
Shaun Tancheff7b203092016-08-21 23:23:19 -05003567 * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN
3568 * @args: device MAINTENANCE_IN data / SCSI command of interest.
3569 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
3570 *
3571 * Yields a subset to satisfy scsi_report_opcode()
3572 *
3573 * LOCKING:
3574 * spin_lock_irqsave(host lock)
3575 */
3576static unsigned int ata_scsiop_maint_in(struct ata_scsi_args *args, u8 *rbuf)
3577{
3578 struct ata_device *dev = args->dev;
3579 u8 *cdb = args->cmd->cmnd;
3580 u8 supported = 0;
3581 unsigned int err = 0;
3582
3583 if (cdb[2] != 1) {
3584 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
3585 err = 2;
3586 goto out;
3587 }
3588 switch (cdb[3]) {
3589 case INQUIRY:
3590 case MODE_SENSE:
3591 case MODE_SENSE_10:
3592 case READ_CAPACITY:
3593 case SERVICE_ACTION_IN_16:
3594 case REPORT_LUNS:
3595 case REQUEST_SENSE:
3596 case SYNCHRONIZE_CACHE:
3597 case REZERO_UNIT:
3598 case SEEK_6:
3599 case SEEK_10:
3600 case TEST_UNIT_READY:
3601 case SEND_DIAGNOSTIC:
3602 case MAINTENANCE_IN:
3603 case READ_6:
3604 case READ_10:
3605 case READ_16:
3606 case WRITE_6:
3607 case WRITE_10:
3608 case WRITE_16:
3609 case ATA_12:
3610 case ATA_16:
3611 case VERIFY:
3612 case VERIFY_16:
3613 case MODE_SELECT:
3614 case MODE_SELECT_10:
3615 case START_STOP:
3616 supported = 3;
3617 break;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003618 case ZBC_IN:
3619 case ZBC_OUT:
3620 if (ata_id_zoned_cap(dev->id) ||
3621 dev->class == ATA_DEV_ZAC)
3622 supported = 3;
3623 break;
Christoph Hellwig818831c2017-06-04 14:42:24 +02003624 case SECURITY_PROTOCOL_IN:
3625 case SECURITY_PROTOCOL_OUT:
3626 if (dev->flags & ATA_DFLAG_TRUSTED)
3627 supported = 3;
3628 break;
Shaun Tancheff7b203092016-08-21 23:23:19 -05003629 default:
3630 break;
3631 }
3632out:
3633 rbuf[1] = supported; /* supported */
3634 return err;
3635}
3636
3637/**
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003638 * ata_scsi_report_zones_complete - convert ATA output
3639 * @qc: command structure returning the data
3640 *
3641 * Convert T-13 little-endian field representation into
3642 * T-10 big-endian field representation.
3643 * What a mess.
3644 */
3645static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
3646{
3647 struct scsi_cmnd *scmd = qc->scsicmd;
3648 struct sg_mapping_iter miter;
3649 unsigned long flags;
3650 unsigned int bytes = 0;
3651
3652 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
3653 SG_MITER_TO_SG | SG_MITER_ATOMIC);
3654
3655 local_irq_save(flags);
3656 while (sg_miter_next(&miter)) {
3657 unsigned int offset = 0;
3658
3659 if (bytes == 0) {
3660 char *hdr;
3661 u32 list_length;
3662 u64 max_lba, opt_lba;
3663 u16 same;
3664
3665 /* Swizzle header */
3666 hdr = miter.addr;
3667 list_length = get_unaligned_le32(&hdr[0]);
3668 same = get_unaligned_le16(&hdr[4]);
3669 max_lba = get_unaligned_le64(&hdr[8]);
3670 opt_lba = get_unaligned_le64(&hdr[16]);
3671 put_unaligned_be32(list_length, &hdr[0]);
3672 hdr[4] = same & 0xf;
3673 put_unaligned_be64(max_lba, &hdr[8]);
3674 put_unaligned_be64(opt_lba, &hdr[16]);
3675 offset += 64;
3676 bytes += 64;
3677 }
3678 while (offset < miter.length) {
3679 char *rec;
3680 u8 cond, type, non_seq, reset;
3681 u64 size, start, wp;
3682
3683 /* Swizzle zone descriptor */
3684 rec = miter.addr + offset;
3685 type = rec[0] & 0xf;
3686 cond = (rec[1] >> 4) & 0xf;
3687 non_seq = (rec[1] & 2);
3688 reset = (rec[1] & 1);
3689 size = get_unaligned_le64(&rec[8]);
3690 start = get_unaligned_le64(&rec[16]);
3691 wp = get_unaligned_le64(&rec[24]);
3692 rec[0] = type;
3693 rec[1] = (cond << 4) | non_seq | reset;
3694 put_unaligned_be64(size, &rec[8]);
3695 put_unaligned_be64(start, &rec[16]);
3696 put_unaligned_be64(wp, &rec[24]);
3697 WARN_ON(offset + 64 > miter.length);
3698 offset += 64;
3699 bytes += 64;
3700 }
3701 }
3702 sg_miter_stop(&miter);
3703 local_irq_restore(flags);
3704
3705 ata_scsi_qc_complete(qc);
3706}
3707
3708static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
3709{
3710 struct ata_taskfile *tf = &qc->tf;
3711 struct scsi_cmnd *scmd = qc->scsicmd;
3712 const u8 *cdb = scmd->cmnd;
3713 u16 sect, fp = (u16)-1;
3714 u8 sa, options, bp = 0xff;
3715 u64 block;
3716 u32 n_block;
3717
3718 if (unlikely(scmd->cmd_len < 16)) {
3719 ata_dev_warn(qc->dev, "invalid cdb length %d\n",
3720 scmd->cmd_len);
3721 fp = 15;
3722 goto invalid_fld;
3723 }
3724 scsi_16_lba_len(cdb, &block, &n_block);
3725 if (n_block != scsi_bufflen(scmd)) {
3726 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
3727 n_block, scsi_bufflen(scmd));
3728 goto invalid_param_len;
3729 }
3730 sa = cdb[1] & 0x1f;
3731 if (sa != ZI_REPORT_ZONES) {
3732 ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
3733 fp = 1;
3734 goto invalid_fld;
3735 }
3736 /*
3737 * ZAC allows only for transfers in 512 byte blocks,
3738 * and uses a 16 bit value for the transfer count.
3739 */
3740 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
3741 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
3742 goto invalid_param_len;
3743 }
3744 sect = n_block / 512;
Damien Le Moal3f174222016-07-14 09:05:49 +09003745 options = cdb[14] & 0xbf;
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003746
3747 if (ata_ncq_enabled(qc->dev) &&
3748 ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
3749 tf->protocol = ATA_PROT_NCQ;
3750 tf->command = ATA_CMD_FPDMA_RECV;
3751 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
Jens Axboe4e5b6262018-05-11 12:51:04 -06003752 tf->nsect = qc->hw_tag << 3;
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003753 tf->feature = sect & 0xff;
3754 tf->hob_feature = (sect >> 8) & 0xff;
Damien Le Moal3f174222016-07-14 09:05:49 +09003755 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02003756 } else {
3757 tf->command = ATA_CMD_ZAC_MGMT_IN;
3758 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
3759 tf->protocol = ATA_PROT_DMA;
3760 tf->hob_feature = options;
3761 tf->hob_nsect = (sect >> 8) & 0xff;
3762 tf->nsect = sect & 0xff;
3763 }
3764 tf->device = ATA_LBA;
3765 tf->lbah = (block >> 16) & 0xff;
3766 tf->lbam = (block >> 8) & 0xff;
3767 tf->lbal = block & 0xff;
3768 tf->hob_lbah = (block >> 40) & 0xff;
3769 tf->hob_lbam = (block >> 32) & 0xff;
3770 tf->hob_lbal = (block >> 24) & 0xff;
3771
3772 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3773 qc->flags |= ATA_QCFLAG_RESULT_TF;
3774
3775 ata_qc_set_pc_nbytes(qc);
3776
3777 qc->complete_fn = ata_scsi_report_zones_complete;
3778
3779 return 0;
3780
3781invalid_fld:
3782 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
3783 return 1;
3784
3785invalid_param_len:
3786 /* "Parameter list length error" */
3787 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3788 return 1;
3789}
3790
Hannes Reinecke27708a92016-04-25 12:45:53 +02003791static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
3792{
3793 struct ata_taskfile *tf = &qc->tf;
3794 struct scsi_cmnd *scmd = qc->scsicmd;
3795 struct ata_device *dev = qc->dev;
3796 const u8 *cdb = scmd->cmnd;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003797 u8 all, sa;
Hannes Reinecke27708a92016-04-25 12:45:53 +02003798 u64 block;
3799 u32 n_block;
3800 u16 fp = (u16)-1;
3801
3802 if (unlikely(scmd->cmd_len < 16)) {
3803 fp = 15;
3804 goto invalid_fld;
3805 }
3806
3807 sa = cdb[1] & 0x1f;
3808 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
3809 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
3810 fp = 1;
3811 goto invalid_fld;
3812 }
3813
3814 scsi_16_lba_len(cdb, &block, &n_block);
3815 if (n_block) {
3816 /*
3817 * ZAC MANAGEMENT OUT doesn't define any length
3818 */
3819 goto invalid_param_len;
3820 }
Hannes Reinecke27708a92016-04-25 12:45:53 +02003821
Damien Le Moal2950cef2016-07-14 09:05:51 +09003822 all = cdb[14] & 0x1;
Damien Le Moal6edf1d42018-06-26 20:56:55 +09003823 if (all) {
3824 /*
3825 * Ignore the block address (zone ID) as defined by ZBC.
3826 */
3827 block = 0;
3828 } else if (block >= dev->n_sectors) {
Damien Le Moalb320a0a2018-06-26 20:56:54 +09003829 /*
3830 * Block must be a valid zone ID (a zone start LBA).
3831 */
3832 fp = 2;
3833 goto invalid_fld;
3834 }
Hannes Reinecke27708a92016-04-25 12:45:53 +02003835
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003836 if (ata_ncq_enabled(qc->dev) &&
3837 ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
Hannes Reinecke5b844b62016-07-14 09:05:48 +09003838 tf->protocol = ATA_PROT_NCQ_NODATA;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003839 tf->command = ATA_CMD_NCQ_NON_DATA;
Damien Le Moalee194b72016-07-14 09:05:50 +09003840 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
Jens Axboe4e5b6262018-05-11 12:51:04 -06003841 tf->nsect = qc->hw_tag << 3;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003842 tf->auxiliary = sa | ((u16)all << 8);
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003843 } else {
3844 tf->protocol = ATA_PROT_NODATA;
3845 tf->command = ATA_CMD_ZAC_MGMT_OUT;
3846 tf->feature = sa;
Damien Le Moal2950cef2016-07-14 09:05:51 +09003847 tf->hob_feature = all;
Hannes Reinecke284b3b72016-04-25 12:45:54 +02003848 }
Hannes Reinecke27708a92016-04-25 12:45:53 +02003849 tf->lbah = (block >> 16) & 0xff;
3850 tf->lbam = (block >> 8) & 0xff;
3851 tf->lbal = block & 0xff;
3852 tf->hob_lbah = (block >> 40) & 0xff;
3853 tf->hob_lbam = (block >> 32) & 0xff;
3854 tf->hob_lbal = (block >> 24) & 0xff;
3855 tf->device = ATA_LBA;
3856 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3857
3858 return 0;
3859
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 invalid_fld:
Hannes Reinecke27708a92016-04-25 12:45:53 +02003861 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
3862 return 1;
Hannes Reinecke27708a92016-04-25 12:45:53 +02003863invalid_param_len:
3864 /* "Parameter list length error" */
3865 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return 1;
3867}
3868
3869/**
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003870 * ata_mselect_caching - Simulate MODE SELECT for caching info page
3871 * @qc: Storage for translated ATA taskfile
3872 * @buf: input buffer
3873 * @len: number of valid bytes in the input buffer
Hannes Reinecke77800812016-04-04 11:44:07 +02003874 * @fp: out parameter for the failed field on error
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003875 *
3876 * Prepare a taskfile to modify caching information for the device.
3877 *
3878 * LOCKING:
3879 * None.
3880 */
3881static int ata_mselect_caching(struct ata_queued_cmd *qc,
Hannes Reinecke77800812016-04-04 11:44:07 +02003882 const u8 *buf, int len, u16 *fp)
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003883{
3884 struct ata_taskfile *tf = &qc->tf;
3885 struct ata_device *dev = qc->dev;
Tom Yand7372cb2016-07-23 02:34:08 +08003886 u8 mpage[CACHE_MPAGE_LEN];
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003887 u8 wce;
Hannes Reinecke77800812016-04-04 11:44:07 +02003888 int i;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003889
3890 /*
3891 * The first two bytes of def_cache_mpage are a header, so offsets
3892 * in mpage are off by 2 compared to buf. Same for len.
3893 */
3894
Hannes Reinecke77800812016-04-04 11:44:07 +02003895 if (len != CACHE_MPAGE_LEN - 2) {
3896 if (len < CACHE_MPAGE_LEN - 2)
3897 *fp = len;
3898 else
3899 *fp = CACHE_MPAGE_LEN - 2;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003900 return -EINVAL;
Hannes Reinecke77800812016-04-04 11:44:07 +02003901 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003902
3903 wce = buf[0] & (1 << 2);
3904
3905 /*
3906 * Check that read-only bits are not modified.
3907 */
3908 ata_msense_caching(dev->id, mpage, false);
Hannes Reinecke77800812016-04-04 11:44:07 +02003909 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
3910 if (i == 0)
3911 continue;
3912 if (mpage[i + 2] != buf[i]) {
3913 *fp = i;
3914 return -EINVAL;
3915 }
3916 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003917
3918 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3919 tf->protocol = ATA_PROT_NODATA;
3920 tf->nsect = 0;
3921 tf->command = ATA_CMD_SET_FEATURES;
3922 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3923 return 0;
3924}
3925
3926/**
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003927 * ata_mselect_control - Simulate MODE SELECT for control page
3928 * @qc: Storage for translated ATA taskfile
3929 * @buf: input buffer
3930 * @len: number of valid bytes in the input buffer
Hannes Reinecke77800812016-04-04 11:44:07 +02003931 * @fp: out parameter for the failed field on error
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003932 *
3933 * Prepare a taskfile to modify caching information for the device.
3934 *
3935 * LOCKING:
3936 * None.
3937 */
3938static int ata_mselect_control(struct ata_queued_cmd *qc,
Hannes Reinecke77800812016-04-04 11:44:07 +02003939 const u8 *buf, int len, u16 *fp)
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003940{
3941 struct ata_device *dev = qc->dev;
Tom Yand7372cb2016-07-23 02:34:08 +08003942 u8 mpage[CONTROL_MPAGE_LEN];
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003943 u8 d_sense;
Hannes Reinecke77800812016-04-04 11:44:07 +02003944 int i;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003945
3946 /*
3947 * The first two bytes of def_control_mpage are a header, so offsets
3948 * in mpage are off by 2 compared to buf. Same for len.
3949 */
3950
Hannes Reinecke77800812016-04-04 11:44:07 +02003951 if (len != CONTROL_MPAGE_LEN - 2) {
3952 if (len < CONTROL_MPAGE_LEN - 2)
3953 *fp = len;
3954 else
3955 *fp = CONTROL_MPAGE_LEN - 2;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003956 return -EINVAL;
Hannes Reinecke77800812016-04-04 11:44:07 +02003957 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003958
3959 d_sense = buf[0] & (1 << 2);
3960
3961 /*
3962 * Check that read-only bits are not modified.
3963 */
Tom Yanf086b742016-07-13 02:54:12 +08003964 ata_msense_control(dev, mpage, false);
Hannes Reinecke77800812016-04-04 11:44:07 +02003965 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
3966 if (i == 0)
3967 continue;
3968 if (mpage[2 + i] != buf[i]) {
3969 *fp = i;
3970 return -EINVAL;
3971 }
3972 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003973 if (d_sense & (1 << 2))
3974 dev->flags |= ATA_DFLAG_D_SENSE;
3975 else
3976 dev->flags &= ~ATA_DFLAG_D_SENSE;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02003977 return 0;
3978}
3979
3980/**
Minwoo Im01efd602017-06-20 19:21:30 +09003981 * ata_scsi_mode_select_xlat - Simulate MODE SELECT 6, 10 commands
Paolo Bonzini1b26d292012-07-05 14:18:21 +02003982 * @qc: Storage for translated ATA taskfile
3983 *
3984 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
3985 * Assume this is invoked for direct access devices (e.g. disks) only.
3986 * There should be no block descriptor for other device types.
3987 *
3988 * LOCKING:
3989 * spin_lock_irqsave(host lock)
3990 */
3991static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
3992{
3993 struct scsi_cmnd *scmd = qc->scsicmd;
3994 const u8 *cdb = scmd->cmnd;
3995 const u8 *p;
3996 u8 pg, spg;
3997 unsigned six_byte, pg_len, hdr_len, bd_len;
3998 int len;
Hannes Reinecke77800812016-04-04 11:44:07 +02003999 u16 fp = (u16)-1;
4000 u8 bp = 0xff;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004001
4002 VPRINTK("ENTER\n");
4003
4004 six_byte = (cdb[0] == MODE_SELECT);
4005 if (six_byte) {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004006 if (scmd->cmd_len < 5) {
4007 fp = 4;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004008 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004009 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004010
4011 len = cdb[4];
4012 hdr_len = 4;
4013 } else {
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004014 if (scmd->cmd_len < 9) {
4015 fp = 8;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004016 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004017 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004018
4019 len = (cdb[7] << 8) + cdb[8];
4020 hdr_len = 8;
4021 }
4022
4023 /* We only support PF=1, SP=0. */
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004024 if ((cdb[1] & 0x11) != 0x10) {
4025 fp = 1;
Hannes Reinecke0df10b82016-04-04 11:44:06 +02004026 bp = (cdb[1] & 0x01) ? 1 : 5;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004027 goto invalid_fld;
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004028 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004029
4030 /* Test early for possible overrun. */
4031 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
4032 goto invalid_param_len;
4033
4034 p = page_address(sg_page(scsi_sglist(scmd)));
4035
4036 /* Move past header and block descriptors. */
4037 if (len < hdr_len)
4038 goto invalid_param_len;
4039
4040 if (six_byte)
4041 bd_len = p[3];
4042 else
4043 bd_len = (p[6] << 8) + p[7];
4044
4045 len -= hdr_len;
4046 p += hdr_len;
4047 if (len < bd_len)
4048 goto invalid_param_len;
Hannes Reinecke77800812016-04-04 11:44:07 +02004049 if (bd_len != 0 && bd_len != 8) {
4050 fp = (six_byte) ? 3 : 6;
4051 fp += bd_len + hdr_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004052 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004053 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004054
4055 len -= bd_len;
4056 p += bd_len;
4057 if (len == 0)
4058 goto skip;
4059
4060 /* Parse both possible formats for the mode page headers. */
4061 pg = p[0] & 0x3f;
4062 if (p[0] & 0x40) {
4063 if (len < 4)
4064 goto invalid_param_len;
4065
4066 spg = p[1];
4067 pg_len = (p[2] << 8) | p[3];
4068 p += 4;
4069 len -= 4;
4070 } else {
4071 if (len < 2)
4072 goto invalid_param_len;
4073
4074 spg = 0;
4075 pg_len = p[1];
4076 p += 2;
4077 len -= 2;
4078 }
4079
4080 /*
4081 * No mode subpages supported (yet) but asking for _all_
4082 * subpages may be valid
4083 */
Hannes Reinecke77800812016-04-04 11:44:07 +02004084 if (spg && (spg != ALL_SUB_MPAGES)) {
4085 fp = (p[0] & 0x40) ? 1 : 0;
4086 fp += hdr_len + bd_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004087 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004088 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004089 if (pg_len > len)
4090 goto invalid_param_len;
4091
4092 switch (pg) {
4093 case CACHE_MPAGE:
Hannes Reinecke77800812016-04-04 11:44:07 +02004094 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
4095 fp += hdr_len + bd_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004096 goto invalid_param;
Hannes Reinecke77800812016-04-04 11:44:07 +02004097 }
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004098 break;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004099 case CONTROL_MPAGE:
Hannes Reinecke77800812016-04-04 11:44:07 +02004100 if (ata_mselect_control(qc, p, pg_len, &fp) < 0) {
4101 fp += hdr_len + bd_len;
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004102 goto invalid_param;
Tom Yan535fd072016-07-22 02:41:54 +08004103 } else {
4104 goto skip; /* No ATA command to send */
Hannes Reinecke77800812016-04-04 11:44:07 +02004105 }
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004106 break;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004107 default: /* invalid page code */
Hannes Reinecke77800812016-04-04 11:44:07 +02004108 fp = bd_len + hdr_len;
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004109 goto invalid_param;
4110 }
4111
4112 /*
4113 * Only one page has changeable data, so we only support setting one
4114 * page at a time.
4115 */
4116 if (len > pg_len)
4117 goto invalid_param;
4118
4119 return 0;
4120
4121 invalid_fld:
Hannes Reinecke0df10b82016-04-04 11:44:06 +02004122 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004123 return 1;
4124
4125 invalid_param:
Hannes Reinecke77800812016-04-04 11:44:07 +02004126 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004127 return 1;
4128
4129 invalid_param_len:
4130 /* "Parameter list length error" */
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004131 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004132 return 1;
4133
4134 skip:
4135 scmd->result = SAM_STAT_GOOD;
4136 return 1;
4137}
4138
Christoph Hellwig818831c2017-06-04 14:42:24 +02004139static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma)
4140{
4141 if (len == 0)
4142 return ATA_CMD_TRUSTED_NONDATA;
4143 else if (send)
4144 return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND;
4145 else
4146 return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV;
4147}
4148
4149static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc)
4150{
4151 struct scsi_cmnd *scmd = qc->scsicmd;
4152 const u8 *cdb = scmd->cmnd;
4153 struct ata_taskfile *tf = &qc->tf;
4154 u8 secp = cdb[1];
4155 bool send = (cdb[0] == SECURITY_PROTOCOL_OUT);
4156 u16 spsp = get_unaligned_be16(&cdb[2]);
4157 u32 len = get_unaligned_be32(&cdb[6]);
4158 bool dma = !(qc->dev->flags & ATA_DFLAG_PIO);
4159
4160 /*
4161 * We don't support the ATA "security" protocol.
4162 */
4163 if (secp == 0xef) {
4164 ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0);
4165 return 1;
4166 }
4167
4168 if (cdb[4] & 7) { /* INC_512 */
4169 if (len > 0xffff) {
4170 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4171 return 1;
4172 }
4173 } else {
4174 if (len > 0x01fffe00) {
4175 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4176 return 1;
4177 }
4178
4179 /* convert to the sector-based ATA addressing */
4180 len = (len + 511) / 512;
4181 }
4182
4183 tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO;
4184 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA;
4185 if (send)
4186 tf->flags |= ATA_TFLAG_WRITE;
4187 tf->command = ata_scsi_trusted_op(len, send, dma);
4188 tf->feature = secp;
4189 tf->lbam = spsp & 0xff;
4190 tf->lbah = spsp >> 8;
4191
4192 if (len) {
4193 tf->nsect = len & 0xff;
4194 tf->lbal = len >> 8;
4195 } else {
4196 if (!send)
4197 tf->lbah = (1 << 7);
4198 }
4199
4200 ata_qc_set_pc_nbytes(qc);
4201 return 0;
4202}
4203
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004204/**
Minwoo Imb1ffbf82017-06-24 03:41:10 +09004205 * ata_scsi_var_len_cdb_xlat - SATL variable length CDB to Handler
4206 * @qc: Command to be translated
4207 *
4208 * Translate a SCSI variable length CDB to specified commands.
4209 * It checks a service action value in CDB to call corresponding handler.
4210 *
4211 * RETURNS:
4212 * Zero on success, non-zero on failure
4213 *
4214 */
4215static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc)
4216{
4217 struct scsi_cmnd *scmd = qc->scsicmd;
4218 const u8 *cdb = scmd->cmnd;
4219 const u16 sa = get_unaligned_be16(&cdb[8]);
4220
4221 /*
4222 * if service action represents a ata pass-thru(32) command,
4223 * then pass it to ata_scsi_pass_thru handler.
4224 */
4225 if (sa == ATA_32)
4226 return ata_scsi_pass_thru(qc);
4227
Minwoo Imb1ffbf82017-06-24 03:41:10 +09004228 /* unsupported service action */
4229 return 1;
4230}
4231
4232/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 * ata_get_xlat_func - check if SCSI to ATA translation is possible
4234 * @dev: ATA device
4235 * @cmd: SCSI command opcode to consider
4236 *
4237 * Look up the SCSI command given, and determine whether the
4238 * SCSI command is to be translated or simulated.
4239 *
4240 * RETURNS:
4241 * Pointer to translation function if possible, %NULL if not.
4242 */
4243
4244static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
4245{
4246 switch (cmd) {
4247 case READ_6:
4248 case READ_10:
4249 case READ_16:
4250
4251 case WRITE_6:
4252 case WRITE_10:
4253 case WRITE_16:
4254 return ata_scsi_rw_xlat;
4255
Christoph Hellwig0cdd6eb2009-12-10 10:36:01 +01004256 case WRITE_SAME_16:
Christoph Hellwig18f0f972009-11-17 10:00:47 -05004257 return ata_scsi_write_same_xlat;
4258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 case SYNCHRONIZE_CACHE:
4260 if (ata_try_flush_cache(dev))
4261 return ata_scsi_flush_xlat;
4262 break;
4263
4264 case VERIFY:
4265 case VERIFY_16:
4266 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04004267
4268 case ATA_12:
4269 case ATA_16:
4270 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04004271
Minwoo Imb1ffbf82017-06-24 03:41:10 +09004272 case VARIABLE_LENGTH_CMD:
4273 return ata_scsi_var_len_cdb_xlat;
4274
Paolo Bonzini1b26d292012-07-05 14:18:21 +02004275 case MODE_SELECT:
4276 case MODE_SELECT_10:
4277 return ata_scsi_mode_select_xlat;
4278 break;
4279
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02004280 case ZBC_IN:
4281 return ata_scsi_zbc_in_xlat;
4282
Hannes Reinecke27708a92016-04-25 12:45:53 +02004283 case ZBC_OUT:
4284 return ata_scsi_zbc_out_xlat;
4285
Christoph Hellwig818831c2017-06-04 14:42:24 +02004286 case SECURITY_PROTOCOL_IN:
4287 case SECURITY_PROTOCOL_OUT:
4288 if (!(dev->flags & ATA_DFLAG_TRUSTED))
4289 break;
4290 return ata_scsi_security_inout_xlat;
4291
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04004292 case START_STOP:
4293 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 }
4295
4296 return NULL;
4297}
4298
4299/**
4300 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
4301 * @ap: ATA port to which the command was being sent
4302 * @cmd: SCSI command to dump
4303 *
4304 * Prints the contents of a SCSI command via printk().
4305 */
4306
4307static inline void ata_scsi_dump_cdb(struct ata_port *ap,
4308 struct scsi_cmnd *cmd)
4309{
Paul Menzel32a53422018-07-08 09:11:34 +02004310#ifdef ATA_VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 struct scsi_device *scsidev = cmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Paul Menzel32a53422018-07-08 09:11:34 +02004313 VPRINTK("CDB (%u:%d,%d,%lld) %9ph\n",
Tejun Heo44877b42007-02-21 01:06:51 +09004314 ap->print_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 scsidev->channel, scsidev->id, scsidev->lun,
Andy Shevchenko26f58832016-05-06 21:40:40 +03004316 cmd->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317#endif
4318}
4319
Tejun Heo542b1442006-12-17 10:45:08 +09004320static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09004321 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06004322{
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004323 u8 scsi_op = scmd->cmnd[0];
4324 ata_xlat_func_t xlat_func;
Tejun Heo2115ea92006-05-15 21:03:39 +09004325 int rc = 0;
4326
Hannes Reinecke9162c652014-11-05 13:08:21 +01004327 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004328 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
4329 goto bad_cdb_len;
4330
4331 xlat_func = ata_get_xlat_func(dev, scsi_op);
4332 } else {
4333 if (unlikely(!scmd->cmd_len))
4334 goto bad_cdb_len;
4335
4336 xlat_func = NULL;
4337 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
4338 /* relay SCSI command to ATAPI device */
Mark Lord607126c2007-11-15 13:13:59 +09004339 int len = COMMAND_SIZE(scsi_op);
Eric Biggers058f58e2018-02-03 20:30:56 -08004340 if (unlikely(len > scmd->cmd_len ||
4341 len > dev->cdb_len ||
4342 scmd->cmd_len > ATAPI_CDB_LEN))
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004343 goto bad_cdb_len;
4344
4345 xlat_func = atapi_xlat;
4346 } else {
4347 /* ATA_16 passthru, treat as an ATA command */
4348 if (unlikely(scmd->cmd_len > 16))
4349 goto bad_cdb_len;
4350
4351 xlat_func = ata_get_xlat_func(dev, scsi_op);
4352 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09004353 }
4354
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004355 if (xlat_func)
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004356 rc = ata_scsi_translate(dev, scmd, xlat_func);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004357 else
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004358 ata_scsi_simulate(dev, scmd);
Tejun Heo2115ea92006-05-15 21:03:39 +09004359
4360 return rc;
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004361
4362 bad_cdb_len:
4363 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
4364 scmd->cmd_len, scsi_op, dev->cdb_len);
4365 scmd->result = DID_ERROR << 16;
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004366 scmd->scsi_done(scmd);
Mark Lordbaf4fdf2007-08-08 01:08:45 +09004367 return 0;
Brian Kingeb3f0f92006-03-23 17:30:02 -06004368}
4369
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370/**
4371 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
Jeff Garzik23e701e2010-11-17 12:03:58 -05004372 * @shost: SCSI host of command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 * @cmd: SCSI command to be sent
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 *
4375 * In some cases, this function translates SCSI commands into
4376 * ATA taskfiles, and queues the taskfiles to be sent to
4377 * hardware. In other cases, this function simulates a
4378 * SCSI device by evaluating and responding to certain
4379 * SCSI commands. This creates the overall effect of
4380 * ATA and ATAPI devices appearing as SCSI devices.
4381 *
4382 * LOCKING:
Jeff Garzik23e701e2010-11-17 12:03:58 -05004383 * ATA host lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 *
4385 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09004386 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
4387 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 */
Jeff Garzik23e701e2010-11-17 12:03:58 -05004389int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
4391 struct ata_port *ap;
4392 struct ata_device *dev;
4393 struct scsi_device *scsidev = cmd->device;
Tejun Heo2115ea92006-05-15 21:03:39 +09004394 int rc = 0;
Jeff Garzik23e701e2010-11-17 12:03:58 -05004395 unsigned long irq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Jeff Garzik35bb94b2006-04-11 13:12:34 -04004397 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05004398
Jeff Garzik23e701e2010-11-17 12:03:58 -05004399 spin_lock_irqsave(ap->lock, irq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
4401 ata_scsi_dump_cdb(ap, cmd);
4402
4403 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06004404 if (likely(dev))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004405 rc = __ata_scsi_queuecmd(cmd, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06004406 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzik23e701e2010-11-17 12:03:58 -05004408 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
4410
Jeff Garzik23e701e2010-11-17 12:03:58 -05004411 spin_unlock_irqrestore(ap->lock, irq_flags);
4412
Tejun Heo2115ea92006-05-15 21:03:39 +09004413 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004415EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
4417/**
4418 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004419 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 * @cmd: SCSI command being sent to device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 *
4422 * Interprets and directly executes a select list of SCSI commands
4423 * that can be handled internally.
4424 *
4425 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004426 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 */
4428
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05004429void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
4431 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04004432 const u8 *scsicmd = cmd->cmnd;
Jeff Garzik45394142007-09-21 06:23:42 -04004433 u8 tmp8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Tejun Heo9a3dccc2006-01-06 09:56:18 +01004435 args.dev = dev;
4436 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 args.cmd = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
4439 switch(scsicmd[0]) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004440 case INQUIRY:
Hannes Reineckebcfc8672016-04-04 11:44:05 +02004441 if (scsicmd[1] & 2) /* is CmdDt set? */
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004442 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004443 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
4444 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
4445 else switch (scsicmd[2]) {
4446 case 0x00:
4447 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004449 case 0x80:
4450 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004452 case 0x83:
4453 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004455 case 0x89:
4456 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 break;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05004458 case 0xb0:
4459 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
4460 break;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06004461 case 0xb1:
4462 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
4463 break;
Martin K. Petersen02e0a602010-09-10 01:23:18 -04004464 case 0xb2:
4465 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
4466 break;
Hannes Reinecke6d1003a2016-04-25 12:45:56 +02004467 case 0xb6:
4468 if (dev->flags & ATA_DFLAG_ZAC) {
4469 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b6);
4470 break;
4471 }
4472 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 default:
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004474 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004476 }
4477 break;
4478
4479 case MODE_SENSE:
4480 case MODE_SENSE_10:
4481 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
4482 break;
4483
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004484 case READ_CAPACITY:
4485 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4486 break;
4487
Hannes Reineckeeb846d92014-11-17 14:25:19 +01004488 case SERVICE_ACTION_IN_16:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004489 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
4490 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4491 else
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004492 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004493 break;
4494
4495 case REPORT_LUNS:
4496 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
4497 break;
4498
4499 case REQUEST_SENSE:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004500 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004501 cmd->result = (DRIVER_SENSE << 24);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004502 break;
4503
4504 /* if we reach this, then writeback caching is disabled,
4505 * turning this into a no-op.
4506 */
4507 case SYNCHRONIZE_CACHE:
4508 /* fall through */
4509
4510 /* no-op's, complete with success */
4511 case REZERO_UNIT:
4512 case SEEK_6:
4513 case SEEK_10:
4514 case TEST_UNIT_READY:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004515 break;
4516
4517 case SEND_DIAGNOSTIC:
4518 tmp8 = scsicmd[1] & ~(1 << 3);
Christoph Hellwiga0c0b0e2017-01-10 09:41:47 +01004519 if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4])
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004520 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004521 break;
4522
Shaun Tancheff7b203092016-08-21 23:23:19 -05004523 case MAINTENANCE_IN:
4524 if (scsicmd[1] == MI_REPORT_SUPPORTED_OPERATION_CODES)
4525 ata_scsi_rbuf_fill(&args, ata_scsiop_maint_in);
4526 else
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004527 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
Shaun Tancheff7b203092016-08-21 23:23:19 -05004528 break;
4529
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004530 /* all other commands */
4531 default:
Hannes Reinecke06dbde52016-04-04 11:44:03 +02004532 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004533 /* "Invalid command operation code" */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
Christoph Hellwig8fc6c062017-01-10 09:41:46 +01004536
4537 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01004539EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
Tejun Heof3187192007-04-17 23:44:07 +09004541int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
4542{
4543 int i, rc;
4544
4545 for (i = 0; i < host->n_ports; i++) {
4546 struct ata_port *ap = host->ports[i];
4547 struct Scsi_Host *shost;
4548
4549 rc = -ENOMEM;
4550 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
4551 if (!shost)
4552 goto err_alloc;
4553
Lin Ming9ee4f392011-12-05 09:20:28 +08004554 shost->eh_noresume = 1;
Tejun Heof3187192007-04-17 23:44:07 +09004555 *(struct ata_port **)&shost->hostdata[0] = ap;
4556 ap->scsi_host = shost;
4557
Gwendal Grignoud9027472010-05-25 12:31:38 -07004558 shost->transportt = ata_scsi_transport_template;
Tejun Heof3187192007-04-17 23:44:07 +09004559 shost->unique_id = ap->print_id;
4560 shost->max_id = 16;
4561 shost->max_lun = 1;
4562 shost->max_channel = 1;
Minwoo Imb1ffbf82017-06-24 03:41:10 +09004563 shost->max_cmd_len = 32;
Tejun Heof3187192007-04-17 23:44:07 +09004564
Tejun Heo31cc23b2007-09-23 13:14:12 +09004565 /* Schedule policy is determined by ->qc_defer()
4566 * callback and it needs to see every deferred qc.
4567 * Set host_blocked to 1 to prevent SCSI midlayer from
4568 * automatically deferring requests.
4569 */
4570 shost->max_host_blocked = 1;
4571
John Garry1d72f7a2020-02-28 19:33:35 +08004572 rc = scsi_add_host_with_dma(shost, &ap->tdev, ap->host->dev);
Tejun Heof3187192007-04-17 23:44:07 +09004573 if (rc)
John Garry1d72f7a2020-02-28 19:33:35 +08004574 goto err_alloc;
Tejun Heof3187192007-04-17 23:44:07 +09004575 }
4576
4577 return 0;
4578
Tejun Heof3187192007-04-17 23:44:07 +09004579 err_alloc:
4580 while (--i >= 0) {
4581 struct Scsi_Host *shost = host->ports[i]->scsi_host;
4582
John Garry1d72f7a2020-02-28 19:33:35 +08004583 /* scsi_host_put() is in ata_devres_release() */
Tejun Heof3187192007-04-17 23:44:07 +09004584 scsi_remove_host(shost);
Tejun Heof3187192007-04-17 23:44:07 +09004585 }
4586 return rc;
4587}
4588
Linus Walleij45b80842020-02-22 12:20:57 +01004589#ifdef CONFIG_OF
4590static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
4591{
4592 struct scsi_device *sdev = dev->sdev;
4593 struct device *d = ap->host->dev;
4594 struct device_node *np = d->of_node;
4595 struct device_node *child;
4596
4597 for_each_available_child_of_node(np, child) {
4598 int ret;
4599 u32 val;
4600
4601 ret = of_property_read_u32(child, "reg", &val);
4602 if (ret)
4603 continue;
4604 if (val == dev->devno) {
4605 dev_dbg(d, "found matching device node\n");
4606 sdev->sdev_gendev.of_node = child;
4607 return;
4608 }
4609 }
4610}
4611#else
4612static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
4613{
4614}
4615#endif
4616
Tejun Heo1ae46312007-07-16 14:29:40 +09004617void ata_scsi_scan_host(struct ata_port *ap, int sync)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004618{
Tejun Heo1ae46312007-07-16 14:29:40 +09004619 int tries = 5;
4620 struct ata_device *last_failed_dev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004621 struct ata_link *link;
Tejun Heo1ae46312007-07-16 14:29:40 +09004622 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004623
Tejun Heo1ae46312007-07-16 14:29:40 +09004624 repeat:
Tejun Heo1eca4362008-11-03 20:03:17 +09004625 ata_for_each_link(link, ap, EDGE) {
4626 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004627 struct scsi_device *sdev;
4628 int channel = 0, id = 0;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04004629
Tejun Heo1eca4362008-11-03 20:03:17 +09004630 if (dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004631 continue;
Tejun Heo3edebac2006-05-31 18:27:40 +09004632
Tejun Heo41bda9c2007-08-06 18:36:24 +09004633 if (ata_is_host_link(link))
4634 id = dev->devno;
4635 else
4636 channel = link->pmp;
4637
4638 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
4639 NULL);
4640 if (!IS_ERR(sdev)) {
4641 dev->sdev = sdev;
Linus Walleij45b80842020-02-22 12:20:57 +01004642 ata_scsi_assign_ofnode(dev, ap);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004643 scsi_device_put(sdev);
Grant Grundler295124d2010-08-17 10:56:53 -07004644 } else {
4645 dev->sdev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004646 }
Tejun Heo3edebac2006-05-31 18:27:40 +09004647 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04004648 }
Tejun Heo1ae46312007-07-16 14:29:40 +09004649
4650 /* If we scanned while EH was in progress or allocation
4651 * failure occurred, scan would have failed silently. Check
4652 * whether all devices are attached.
4653 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004654 ata_for_each_link(link, ap, EDGE) {
4655 ata_for_each_dev(dev, link, ENABLED) {
4656 if (!dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004657 goto exit_loop;
4658 }
Tejun Heo1ae46312007-07-16 14:29:40 +09004659 }
Tejun Heo41bda9c2007-08-06 18:36:24 +09004660 exit_loop:
4661 if (!link)
Tejun Heo1ae46312007-07-16 14:29:40 +09004662 return;
4663
4664 /* we're missing some SCSI devices */
4665 if (sync) {
4666 /* If caller requested synchrnous scan && we've made
4667 * any progress, sleep briefly and repeat.
4668 */
4669 if (dev != last_failed_dev) {
4670 msleep(100);
4671 last_failed_dev = dev;
4672 goto repeat;
4673 }
4674
4675 /* We might be failing to detect boot device, give it
4676 * a few more chances.
4677 */
4678 if (--tries) {
4679 msleep(100);
4680 goto repeat;
4681 }
4682
Joe Perchesa9a79df2011-04-15 15:51:59 -07004683 ata_port_err(ap,
4684 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
Tejun Heo1ae46312007-07-16 14:29:40 +09004685 }
4686
Tejun Heoad72cf92010-07-02 10:03:52 +02004687 queue_delayed_work(system_long_wq, &ap->hotplug_task,
Tejun Heo1ae46312007-07-16 14:29:40 +09004688 round_jiffies_relative(HZ));
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004689}
Tejun Heo0ea035a2006-05-31 18:28:01 +09004690
4691/**
4692 * ata_scsi_offline_dev - offline attached SCSI device
4693 * @dev: ATA device to offline attached SCSI device for
4694 *
4695 * This function is called from ata_eh_hotplug() and responsible
4696 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04004697 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09004698 * against clearing.
4699 *
4700 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004701 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09004702 *
4703 * RETURNS:
4704 * 1 if attached SCSI device exists, 0 otherwise.
4705 */
4706int ata_scsi_offline_dev(struct ata_device *dev)
4707{
4708 if (dev->sdev) {
4709 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
4710 return 1;
4711 }
4712 return 0;
4713}
Tejun Heo580b21022006-05-31 18:28:05 +09004714
4715/**
4716 * ata_scsi_remove_dev - remove attached SCSI device
4717 * @dev: ATA device to remove attached SCSI device for
4718 *
4719 * This function is called from ata_eh_scsi_hotplug() and
4720 * responsible for removing the SCSI device attached to @dev.
4721 *
4722 * LOCKING:
4723 * Kernel thread context (may sleep).
4724 */
4725static void ata_scsi_remove_dev(struct ata_device *dev)
4726{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004727 struct ata_port *ap = dev->link->ap;
Tejun Heo580b21022006-05-31 18:28:05 +09004728 struct scsi_device *sdev;
4729 unsigned long flags;
4730
4731 /* Alas, we need to grab scan_mutex to ensure SCSI device
4732 * state doesn't change underneath us and thus
4733 * scsi_device_get() always succeeds. The mutex locking can
4734 * be removed if there is __scsi_device_get() interface which
4735 * increments reference counts regardless of device state.
4736 */
Jeff Garzikcca39742006-08-24 03:19:22 -04004737 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04004738 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b21022006-05-31 18:28:05 +09004739
Jeff Garzikcca39742006-08-24 03:19:22 -04004740 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b21022006-05-31 18:28:05 +09004741 sdev = dev->sdev;
4742 dev->sdev = NULL;
4743
4744 if (sdev) {
4745 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04004746 * away underneath us after the host lock and
Tejun Heo580b21022006-05-31 18:28:05 +09004747 * scan_mutex are released. Hold onto it.
4748 */
4749 if (scsi_device_get(sdev) == 0) {
4750 /* The following ensures the attached sdev is
4751 * offline on return from ata_scsi_offline_dev()
4752 * regardless it wins or loses the race
4753 * against this function.
4754 */
4755 scsi_device_set_state(sdev, SDEV_OFFLINE);
4756 } else {
4757 WARN_ON(1);
4758 sdev = NULL;
4759 }
4760 }
4761
Jeff Garzikba6a1302006-06-22 23:46:10 -04004762 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04004763 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004764
4765 if (sdev) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07004766 ata_dev_info(dev, "detaching (SCSI %s)\n",
4767 dev_name(&sdev->sdev_gendev));
Tejun Heo580b21022006-05-31 18:28:05 +09004768
4769 scsi_remove_device(sdev);
4770 scsi_device_put(sdev);
4771 }
4772}
4773
Tejun Heo41bda9c2007-08-06 18:36:24 +09004774static void ata_scsi_handle_link_detach(struct ata_link *link)
4775{
4776 struct ata_port *ap = link->ap;
4777 struct ata_device *dev;
4778
Tejun Heo1eca4362008-11-03 20:03:17 +09004779 ata_for_each_dev(dev, link, ALL) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004780 unsigned long flags;
4781
4782 if (!(dev->flags & ATA_DFLAG_DETACHED))
4783 continue;
4784
4785 spin_lock_irqsave(ap->lock, flags);
4786 dev->flags &= ~ATA_DFLAG_DETACHED;
4787 spin_unlock_irqrestore(ap->lock, flags);
4788
Aaron Luf1bc1e42013-08-23 10:17:54 +08004789 if (zpodd_dev_enabled(dev))
4790 zpodd_exit(dev);
4791
Tejun Heo41bda9c2007-08-06 18:36:24 +09004792 ata_scsi_remove_dev(dev);
4793 }
4794}
4795
Tejun Heo580b21022006-05-31 18:28:05 +09004796/**
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004797 * ata_scsi_media_change_notify - send media change event
Randy Dunlapc5d0e6a2007-10-15 17:29:46 -07004798 * @dev: Pointer to the disk device with media change event
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004799 *
4800 * Tell the block layer to send a media change notification
4801 * event.
4802 *
4803 * LOCKING:
Tejun Heo854c73a2007-09-23 13:14:11 +09004804 * spin_lock_irqsave(host lock)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004805 */
Tejun Heo854c73a2007-09-23 13:14:11 +09004806void ata_scsi_media_change_notify(struct ata_device *dev)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004807{
Tejun Heo854c73a2007-09-23 13:14:11 +09004808 if (dev->sdev)
Jeff Garzikf26792d2007-10-29 17:18:39 -04004809 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
4810 GFP_ATOMIC);
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004811}
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04004812
4813/**
Tejun Heo580b21022006-05-31 18:28:05 +09004814 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00004815 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b21022006-05-31 18:28:05 +09004816 *
4817 * Perform SCSI part of hotplug. It's executed from a separate
4818 * workqueue after EH completes. This is necessary because SCSI
4819 * hot plugging requires working EH and hot unplugging is
4820 * synchronized with hot plugging with a mutex.
4821 *
4822 * LOCKING:
4823 * Kernel thread context (may sleep).
4824 */
David Howells65f27f32006-11-22 14:55:48 +00004825void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b21022006-05-31 18:28:05 +09004826{
David Howells65f27f32006-11-22 14:55:48 +00004827 struct ata_port *ap =
4828 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004829 int i;
Tejun Heo580b21022006-05-31 18:28:05 +09004830
Tejun Heob51e9e52006-06-29 01:29:30 +09004831 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b21022006-05-31 18:28:05 +09004832 DPRINTK("ENTER/EXIT - unloading\n");
4833 return;
4834 }
4835
4836 DPRINTK("ENTER\n");
Tejun Heoad72cf92010-07-02 10:03:52 +02004837 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004838
Tejun Heo41bda9c2007-08-06 18:36:24 +09004839 /* Unplug detached devices. We cannot use link iterator here
4840 * because PMP links have to be scanned even if PMP is
4841 * currently not attached. Iterate manually.
4842 */
4843 ata_scsi_handle_link_detach(&ap->link);
4844 if (ap->pmp_link)
4845 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
4846 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
Tejun Heo580b21022006-05-31 18:28:05 +09004847
4848 /* scan for new ones */
Tejun Heo1ae46312007-07-16 14:29:40 +09004849 ata_scsi_scan_host(ap, 0);
Tejun Heo580b21022006-05-31 18:28:05 +09004850
Tejun Heoad72cf92010-07-02 10:03:52 +02004851 mutex_unlock(&ap->scsi_scan_mutex);
Tejun Heo580b21022006-05-31 18:28:05 +09004852 DPRINTK("EXIT\n");
4853}
Tejun Heo83c47bc2006-05-31 18:28:07 +09004854
4855/**
4856 * ata_scsi_user_scan - indication for user-initiated bus scan
4857 * @shost: SCSI host to scan
4858 * @channel: Channel to scan
4859 * @id: ID to scan
4860 * @lun: LUN to scan
4861 *
4862 * This function is called when user explicitly requests bus
4863 * scan. Set probe pending flag and invoke EH.
4864 *
4865 * LOCKING:
4866 * SCSI layer (we don't care)
4867 *
4868 * RETURNS:
4869 * Zero.
4870 */
Gwendal Grignoud9027472010-05-25 12:31:38 -07004871int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02004872 unsigned int id, u64 lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004873{
4874 struct ata_port *ap = ata_shost_to_port(shost);
4875 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004876 int devno, rc = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004877
4878 if (!ap->ops->error_handler)
4879 return -EOPNOTSUPP;
4880
Tejun Heo41bda9c2007-08-06 18:36:24 +09004881 if (lun != SCAN_WILD_CARD && lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09004882 return -EINVAL;
4883
Tejun Heo071f44b2008-04-07 22:47:22 +09004884 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004885 if (channel != SCAN_WILD_CARD && channel)
4886 return -EINVAL;
4887 devno = id;
4888 } else {
4889 if (id != SCAN_WILD_CARD && id)
4890 return -EINVAL;
4891 devno = channel;
4892 }
4893
Jeff Garzikba6a1302006-06-22 23:46:10 -04004894 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004895
Tejun Heo41bda9c2007-08-06 18:36:24 +09004896 if (devno == SCAN_WILD_CARD) {
4897 struct ata_link *link;
4898
Tejun Heo1eca4362008-11-03 20:03:17 +09004899 ata_for_each_link(link, ap, EDGE) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004900 struct ata_eh_info *ehi = &link->eh_info;
Tejun Heob558edd2008-01-24 00:05:14 +09004901 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09004902 ehi->action |= ATA_EH_RESET;
Tejun Heo41bda9c2007-08-06 18:36:24 +09004903 }
Tejun Heo83c47bc2006-05-31 18:28:07 +09004904 } else {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004905 struct ata_device *dev = ata_find_dev(ap, devno);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004906
4907 if (dev) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004908 struct ata_eh_info *ehi = &dev->link->eh_info;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004909 ehi->probe_mask |= 1 << dev->devno;
Tejun Heocf480622008-01-24 00:05:14 +09004910 ehi->action |= ATA_EH_RESET;
Tejun Heo83c47bc2006-05-31 18:28:07 +09004911 } else
4912 rc = -EINVAL;
4913 }
4914
Tejun Heo309afcb2006-09-30 18:07:17 +09004915 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09004916 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09004917 spin_unlock_irqrestore(ap->lock, flags);
4918 ata_port_wait_eh(ap);
4919 } else
4920 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09004921
4922 return rc;
4923}
zhao, forrest3057ac32006-06-12 12:01:34 +08004924
4925/**
Tejun Heod0171262006-06-12 22:51:14 +09004926 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00004927 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08004928 *
Tejun Heod0171262006-06-12 22:51:14 +09004929 * After ATA pass thru (SAT) commands are executed successfully,
Tejun Heoad72cf92010-07-02 10:03:52 +02004930 * libata need to propagate the changes to SCSI layer.
zhao, forrest3057ac32006-06-12 12:01:34 +08004931 *
Tejun Heod0171262006-06-12 22:51:14 +09004932 * LOCKING:
4933 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08004934 */
David Howells65f27f32006-11-22 14:55:48 +00004935void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08004936{
David Howells65f27f32006-11-22 14:55:48 +00004937 struct ata_port *ap =
4938 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heo41bda9c2007-08-06 18:36:24 +09004939 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09004940 struct ata_device *dev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004941 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08004942
Tejun Heoad72cf92010-07-02 10:03:52 +02004943 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heof84e7e42006-11-22 11:21:31 +09004944 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08004945
Tejun Heo1eca4362008-11-03 20:03:17 +09004946 ata_for_each_link(link, ap, EDGE) {
4947 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09004948 struct scsi_device *sdev = dev->sdev;
Tejun Heof84e7e42006-11-22 11:21:31 +09004949
Tejun Heo1eca4362008-11-03 20:03:17 +09004950 if (!sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09004951 continue;
4952 if (scsi_device_get(sdev))
4953 continue;
Tejun Heof84e7e42006-11-22 11:21:31 +09004954
Tejun Heo41bda9c2007-08-06 18:36:24 +09004955 spin_unlock_irqrestore(ap->lock, flags);
4956 scsi_rescan_device(&(sdev->sdev_gendev));
4957 scsi_device_put(sdev);
4958 spin_lock_irqsave(ap->lock, flags);
4959 }
zhao, forrest3057ac32006-06-12 12:01:34 +08004960 }
Tejun Heof84e7e42006-11-22 11:21:31 +09004961
4962 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad72cf92010-07-02 10:03:52 +02004963 mutex_unlock(&ap->scsi_scan_mutex);
zhao, forrest3057ac32006-06-12 12:01:34 +08004964}
Brian King80289162006-08-07 14:27:31 -05004965
4966/**
4967 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04004968 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05004969 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04004970 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05004971 *
4972 * LOCKING:
4973 * PCI/etc. bus probe sem.
4974 *
4975 * RETURNS:
4976 * ata_port pointer on success / NULL on failure.
4977 */
4978
Jeff Garzikcca39742006-08-24 03:19:22 -04004979struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05004980 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04004981 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05004982{
Tejun Heof3187192007-04-17 23:44:07 +09004983 struct ata_port *ap;
Brian King80289162006-08-07 14:27:31 -05004984
Tejun Heof3187192007-04-17 23:44:07 +09004985 ap = ata_port_alloc(host);
Brian King80289162006-08-07 14:27:31 -05004986 if (!ap)
4987 return NULL;
4988
Tejun Heof3187192007-04-17 23:44:07 +09004989 ap->port_no = 0;
James Bottomleya29b5da2011-01-23 08:30:00 -06004990 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09004991 ap->pio_mask = port_info->pio_mask;
4992 ap->mwdma_mask = port_info->mwdma_mask;
4993 ap->udma_mask = port_info->udma_mask;
4994 ap->flags |= port_info->flags;
4995 ap->ops = port_info->port_ops;
4996 ap->cbl = ATA_CBL_SATA;
4997
Brian King80289162006-08-07 14:27:31 -05004998 return ap;
4999}
5000EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
5001
5002/**
5003 * ata_sas_port_start - Set port up for dma.
5004 * @ap: Port to initialize
5005 *
5006 * Called just after data structures for each port are
James Bottomleydde20202008-02-19 11:36:56 +01005007 * initialized.
Brian King80289162006-08-07 14:27:31 -05005008 *
5009 * May be used as the port_start() entry in ata_port_operations.
5010 *
5011 * LOCKING:
5012 * Inherited from caller.
5013 */
5014int ata_sas_port_start(struct ata_port *ap)
5015{
Nishanth Aravamudan3f1e0462011-06-16 08:28:36 -07005016 /*
5017 * the port is marked as frozen at allocation time, but if we don't
5018 * have new eh, we won't thaw it
5019 */
5020 if (!ap->ops->error_handler)
5021 ap->pflags &= ~ATA_PFLAG_FROZEN;
James Bottomleydde20202008-02-19 11:36:56 +01005022 return 0;
Brian King80289162006-08-07 14:27:31 -05005023}
5024EXPORT_SYMBOL_GPL(ata_sas_port_start);
5025
5026/**
5027 * ata_port_stop - Undo ata_sas_port_start()
5028 * @ap: Port to shut down
5029 *
Brian King80289162006-08-07 14:27:31 -05005030 * May be used as the port_stop() entry in ata_port_operations.
5031 *
5032 * LOCKING:
5033 * Inherited from caller.
5034 */
5035
5036void ata_sas_port_stop(struct ata_port *ap)
5037{
Brian King80289162006-08-07 14:27:31 -05005038}
5039EXPORT_SYMBOL_GPL(ata_sas_port_stop);
5040
Dan Williamsb2024452012-03-21 21:09:07 -07005041/**
5042 * ata_sas_async_probe - simply schedule probing and return
5043 * @ap: Port to probe
5044 *
5045 * For batch scheduling of probe for sas attached ata devices, assumes
5046 * the port has already been through ata_sas_port_init()
5047 */
5048void ata_sas_async_probe(struct ata_port *ap)
Dan Williams9508a662012-01-18 20:47:01 -08005049{
Dan Williamsb2024452012-03-21 21:09:07 -07005050 __ata_port_probe(ap);
Dan Williams9508a662012-01-18 20:47:01 -08005051}
Dan Williamsb2024452012-03-21 21:09:07 -07005052EXPORT_SYMBOL_GPL(ata_sas_async_probe);
5053
5054int ata_sas_sync_probe(struct ata_port *ap)
5055{
5056 return ata_port_probe(ap);
5057}
5058EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
5059
Dan Williams9508a662012-01-18 20:47:01 -08005060
Brian King80289162006-08-07 14:27:31 -05005061/**
5062 * ata_sas_port_init - Initialize a SATA device
5063 * @ap: SATA port to initialize
5064 *
5065 * LOCKING:
5066 * PCI/etc. bus probe sem.
5067 *
5068 * RETURNS:
5069 * Zero on success, non-zero on error.
5070 */
5071
5072int ata_sas_port_init(struct ata_port *ap)
5073{
5074 int rc = ap->ops->port_start(ap);
5075
Dan Williamsb2024452012-03-21 21:09:07 -07005076 if (rc)
5077 return rc;
5078 ap->print_id = atomic_inc_return(&ata_print_id);
5079 return 0;
Brian King80289162006-08-07 14:27:31 -05005080}
5081EXPORT_SYMBOL_GPL(ata_sas_port_init);
5082
Jason Yanb6240a42018-03-26 17:27:41 +08005083int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
5084{
5085 return ata_tport_add(parent, ap);
5086}
5087EXPORT_SYMBOL_GPL(ata_sas_tport_add);
5088
5089void ata_sas_tport_delete(struct ata_port *ap)
5090{
5091 ata_tport_delete(ap);
5092}
5093EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
5094
Brian King80289162006-08-07 14:27:31 -05005095/**
5096 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
5097 * @ap: SATA port to destroy
5098 *
5099 */
5100
5101void ata_sas_port_destroy(struct ata_port *ap)
5102{
Tejun Heof0d36ef2007-01-20 16:00:28 +09005103 if (ap->ops->port_stop)
5104 ap->ops->port_stop(ap);
Brian King80289162006-08-07 14:27:31 -05005105 kfree(ap);
5106}
5107EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
5108
5109/**
5110 * ata_sas_slave_configure - Default slave_config routine for libata devices
5111 * @sdev: SCSI device to configure
5112 * @ap: ATA port to which SCSI device is attached
5113 *
5114 * RETURNS:
5115 * Zero.
5116 */
5117
5118int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
5119{
5120 ata_scsi_sdev_config(sdev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005121 ata_scsi_dev_config(sdev, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05005122 return 0;
5123}
5124EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
5125
5126/**
5127 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
5128 * @cmd: SCSI command to be sent
Brian King80289162006-08-07 14:27:31 -05005129 * @ap: ATA port to which the command is being sent
5130 *
5131 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06005132 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
5133 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05005134 */
5135
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005136int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
Brian King80289162006-08-07 14:27:31 -05005137{
Brian King08475a12006-11-20 13:51:56 -06005138 int rc = 0;
5139
Brian King80289162006-08-07 14:27:31 -05005140 ata_scsi_dump_cdb(ap, cmd);
5141
Tejun Heo2486fa52008-07-31 07:52:40 +09005142 if (likely(ata_dev_enabled(ap->link.device)))
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005143 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05005144 else {
5145 cmd->result = (DID_BAD_TARGET << 16);
Jeff Garzikb27dcfb2010-11-17 22:56:48 -05005146 cmd->scsi_done(cmd);
Brian King80289162006-08-07 14:27:31 -05005147 }
Brian King08475a12006-11-20 13:51:56 -06005148 return rc;
Brian King80289162006-08-07 14:27:31 -05005149}
5150EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
Shaohua Li98bd4be2015-01-23 19:52:07 -08005151
5152int ata_sas_allocate_tag(struct ata_port *ap)
5153{
5154 unsigned int max_queue = ap->host->n_tags;
5155 unsigned int i, tag;
5156
5157 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
Tejun Heo3a028242015-03-24 14:14:18 -04005158 tag = tag < max_queue ? tag : 0;
Shaohua Li98bd4be2015-01-23 19:52:07 -08005159
5160 /* the last tag is reserved for internal command. */
Jens Axboe2e2cc672018-05-11 12:51:06 -06005161 if (ata_tag_internal(tag))
Shaohua Li98bd4be2015-01-23 19:52:07 -08005162 continue;
5163
5164 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
5165 ap->sas_last_tag = tag;
5166 return tag;
5167 }
5168 }
5169 return -1;
5170}
5171
5172void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
5173{
5174 clear_bit(tag, &ap->sas_tag_allocated);
5175}