Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / drivers / ata / libata-scsi.c
CommitLineData
c82ee6d3 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
af36d7f0
JG
3 * libata-scsi.c - helper library for ATA
4 *
af36d7f0
JG
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
7 *
af36d7f0 8 * libata documentation is available via 'make {ps|pdf}docs',
9bb9a39c 9 * as Documentation/driver-api/libata.rst
af36d7f0
JG
10 *
11 * Hardware documentation available from
12 * - http://www.t10.org/
13 * - http://www.t13.org/
1da177e4
LT
14 */
15
75c0b0e1 16#include <linux/compat.h>
5a0e3ad6 17#include <linux/slab.h>
1da177e4
LT
18#include <linux/kernel.h>
19#include <linux/blkdev.h>
20#include <linux/spinlock.h>
38789fda 21#include <linux/export.h>
1da177e4 22#include <scsi/scsi.h>
1da177e4 23#include <scsi/scsi_host.h>
beb40487 24#include <scsi/scsi_cmnd.h>
85837ebd 25#include <scsi/scsi_eh.h>
005a5a06 26#include <scsi/scsi_device.h>
a6e6ce8e 27#include <scsi/scsi_tcq.h>
30afc84c 28#include <scsi/scsi_transport.h>
1da177e4 29#include <linux/libata.h>
b095518e 30#include <linux/hdreg.h>
2dcb407e 31#include <linux/uaccess.h>
2a6e58d2 32#include <linux/suspend.h>
5f60d5f6 33#include <linux/unaligned.h>
8e061784 34#include <linux/ioprio.h>
45b8084f 35#include <linux/of.h>
1da177e4
LT
36
37#include "libata.h"
d9027470 38#include "libata-transport.h"
1da177e4 39
673b2fe6 40#define ATA_SCSI_RBUF_SIZE 2048
87340e98
TH
41
42static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
43static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
b095518e 44
ad706991 45typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
ab5b3a5b 46
2dcb407e 47static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
ab5b3a5b 48 const struct scsi_device *scsidev);
1da177e4 49
62e4a60e
DLM
50#define RW_RECOVERY_MPAGE 0x1
51#define RW_RECOVERY_MPAGE_LEN 12
52#define CACHE_MPAGE 0x8
53#define CACHE_MPAGE_LEN 20
54#define CONTROL_MPAGE 0xa
55#define CONTROL_MPAGE_LEN 12
56#define ALL_MPAGES 0x3f
57#define ALL_SUB_MPAGES 0xff
673b2fe6
DLM
58#define CDL_T2A_SUB_MPAGE 0x07
59#define CDL_T2B_SUB_MPAGE 0x08
60#define CDL_T2_SUB_MPAGE_LEN 232
df60f9c6
DLM
61#define ATA_FEATURE_SUB_MPAGE 0xf2
62#define ATA_FEATURE_SUB_MPAGE_LEN 16
00ac37f5 63
24f75686 64static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
00ac37f5
DG
65 RW_RECOVERY_MPAGE,
66 RW_RECOVERY_MPAGE_LEN - 2,
24f75686 67 (1 << 7), /* AWRE */
00ac37f5
DG
68 0, /* read retry count */
69 0, 0, 0, 0,
70 0, /* write retry count */
71 0, 0, 0
72};
73
74static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
75 CACHE_MPAGE,
76 CACHE_MPAGE_LEN - 2,
77 0, /* contains WCE, needs to be 0 for logic */
78 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0, /* contains DRA, needs to be 0 for logic */
80 0, 0, 0, 0, 0, 0, 0
81};
82
83static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
84 CONTROL_MPAGE,
85 CONTROL_MPAGE_LEN - 2,
86 2, /* DSENSE=0, GLTSD=1 */
87 0, /* [QAM+QERR may be 1, see 05-359r1] */
88 0, 0, 0, 0, 0xff, 0xff,
89 0, 30 /* extended self test time, see 05-359r1 */
90};
91
45fabbb7
EO
92static ssize_t ata_scsi_park_show(struct device *device,
93 struct device_attribute *attr, char *buf)
94{
95 struct scsi_device *sdev = to_scsi_device(device);
96 struct ata_port *ap;
97 struct ata_link *link;
98 struct ata_device *dev;
3948b6f2 99 unsigned long now;
3f649ab7 100 unsigned int msecs;
45fabbb7
EO
101 int rc = 0;
102
103 ap = ata_shost_to_port(sdev->host);
104
3948b6f2 105 spin_lock_irq(ap->lock);
45fabbb7
EO
106 dev = ata_scsi_find_dev(ap, sdev);
107 if (!dev) {
108 rc = -ENODEV;
109 goto unlock;
110 }
111 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
112 rc = -EOPNOTSUPP;
113 goto unlock;
114 }
115
116 link = dev->link;
a464189d 117 now = jiffies;
45fabbb7
EO
118 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
119 link->eh_context.unloaded_mask & (1 << dev->devno) &&
a464189d
EO
120 time_after(dev->unpark_deadline, now))
121 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
45fabbb7
EO
122 else
123 msecs = 0;
124
125unlock:
126 spin_unlock_irq(ap->lock);
127
0667391e 128 return rc ? rc : sysfs_emit(buf, "%u\n", msecs);
45fabbb7
EO
129}
130
131static ssize_t ata_scsi_park_store(struct device *device,
132 struct device_attribute *attr,
133 const char *buf, size_t len)
134{
135 struct scsi_device *sdev = to_scsi_device(device);
136 struct ata_port *ap;
137 struct ata_device *dev;
8c125363 138 int input;
45fabbb7
EO
139 unsigned long flags;
140 int rc;
141
8c125363 142 rc = kstrtoint(buf, 10, &input);
42b9ab7a
JH
143 if (rc)
144 return rc;
145 if (input < -2)
45fabbb7
EO
146 return -EINVAL;
147 if (input > ATA_TMOUT_MAX_PARK) {
148 rc = -EOVERFLOW;
149 input = ATA_TMOUT_MAX_PARK;
150 }
151
152 ap = ata_shost_to_port(sdev->host);
153
154 spin_lock_irqsave(ap->lock, flags);
155 dev = ata_scsi_find_dev(ap, sdev);
156 if (unlikely(!dev)) {
157 rc = -ENODEV;
158 goto unlock;
159 }
9162c657
HR
160 if (dev->class != ATA_DEV_ATA &&
161 dev->class != ATA_DEV_ZAC) {
45fabbb7
EO
162 rc = -EOPNOTSUPP;
163 goto unlock;
164 }
165
166 if (input >= 0) {
167 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
168 rc = -EOPNOTSUPP;
169 goto unlock;
170 }
171
172 dev->unpark_deadline = ata_deadline(jiffies, input);
173 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
174 ata_port_schedule_eh(ap);
175 complete(&ap->park_req_pending);
176 } else {
177 switch (input) {
178 case -1:
179 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
180 break;
181 case -2:
182 dev->flags |= ATA_DFLAG_NO_UNLOAD;
183 break;
184 }
185 }
186unlock:
187 spin_unlock_irqrestore(ap->lock, flags);
188
189 return rc ? rc : len;
190}
191DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
192 ata_scsi_park_show, ata_scsi_park_store);
193EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
194
4b89ad8e
NC
195bool ata_scsi_sense_is_valid(u8 sk, u8 asc, u8 ascq)
196{
197 /*
198 * If sk == NO_SENSE, and asc + ascq == NO ADDITIONAL SENSE INFORMATION,
199 * then there is no sense data to add.
200 */
201 if (sk == 0 && asc == 0 && ascq == 0)
202 return false;
203
204 /* If sk > COMPLETED, sense data is bogus. */
205 if (sk > COMPLETED)
206 return false;
207
208 return true;
209}
210
06dbde5f
HR
211void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
212 u8 sk, u8 asc, u8 ascq)
f0761be3 213{
06dbde5f
HR
214 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
215
f2b1e9c6 216 scsi_build_sense(cmd, d_sense, sk, asc, ascq);
f0761be3
TH
217}
218
11533932 219static void ata_scsi_set_sense_information(struct ata_queued_cmd *qc)
492bf621
HR
220{
221 u64 information;
222
11533932
IP
223 if (!(qc->flags & ATA_QCFLAG_RTF_FILLED)) {
224 ata_dev_dbg(qc->dev,
225 "missing result TF: can't set INFORMATION sense field\n");
226 return;
227 }
228
229 information = ata_tf_read_block(&qc->result_tf, qc->dev);
492bf621
HR
230 if (information == U64_MAX)
231 return;
232
11533932 233 scsi_set_sense_information(qc->scsicmd->sense_buffer,
492bf621
HR
234 SCSI_SENSE_BUFFERSIZE, information);
235}
236
97981926
IP
237/**
238 * ata_scsi_set_passthru_sense_fields - Set ATA fields in sense buffer
239 * @qc: ATA PASS-THROUGH command.
240 *
241 * Populates "ATA Status Return sense data descriptor" / "Fixed format
242 * sense data" with ATA taskfile fields.
243 *
244 * LOCKING:
245 * None.
246 */
247static void ata_scsi_set_passthru_sense_fields(struct ata_queued_cmd *qc)
248{
816be86c 249 struct ata_device *dev = qc->dev;
97981926
IP
250 struct scsi_cmnd *cmd = qc->scsicmd;
251 struct ata_taskfile *tf = &qc->result_tf;
252 unsigned char *sb = cmd->sense_buffer;
253
816be86c
IP
254 if (!(qc->flags & ATA_QCFLAG_RTF_FILLED)) {
255 ata_dev_dbg(dev,
256 "missing result TF: can't set ATA PT sense fields\n");
257 return;
258 }
259
97981926
IP
260 if ((sb[0] & 0x7f) >= 0x72) {
261 unsigned char *desc;
262 u8 len;
263
264 /* descriptor format */
265 len = sb[7];
266 desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
267 if (!desc) {
268 if (SCSI_SENSE_BUFFERSIZE < len + 14)
269 return;
270 sb[7] = len + 14;
271 desc = sb + 8 + len;
272 }
273 desc[0] = 9;
274 desc[1] = 12;
275 /*
276 * Copy registers into sense buffer.
277 */
278 desc[2] = 0x00;
279 desc[3] = tf->error;
280 desc[5] = tf->nsect;
281 desc[7] = tf->lbal;
282 desc[9] = tf->lbam;
283 desc[11] = tf->lbah;
284 desc[12] = tf->device;
285 desc[13] = tf->status;
286
287 /*
288 * Fill in Extend bit, and the high order bytes
289 * if applicable.
290 */
291 if (tf->flags & ATA_TFLAG_LBA48) {
292 desc[2] |= 0x01;
293 desc[4] = tf->hob_nsect;
294 desc[6] = tf->hob_lbal;
295 desc[8] = tf->hob_lbam;
296 desc[10] = tf->hob_lbah;
297 }
298 } else {
299 /* Fixed sense format */
300 sb[0] |= 0x80;
301 sb[3] = tf->error;
302 sb[4] = tf->status;
303 sb[5] = tf->device;
304 sb[6] = tf->nsect;
305 if (tf->flags & ATA_TFLAG_LBA48) {
306 sb[8] |= 0x80;
307 if (tf->hob_nsect)
308 sb[8] |= 0x40;
309 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
310 sb[8] |= 0x20;
311 }
312 sb[9] = tf->lbal;
313 sb[10] = tf->lbam;
314 sb[11] = tf->lbah;
315 }
316}
317
bcfc867d 318static void ata_scsi_set_invalid_field(struct ata_device *dev,
0df10b84 319 struct scsi_cmnd *cmd, u16 field, u8 bit)
bcfc867d
HR
320{
321 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
8554e5e1 322 /* "Invalid field in CDB" */
bcfc867d 323 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
0df10b84 324 field, bit, 1);
bcfc867d
HR
325}
326
7780081c
HR
327static void ata_scsi_set_invalid_parameter(struct ata_device *dev,
328 struct scsi_cmnd *cmd, u16 field)
329{
330 /* "Invalid field in parameter list" */
331 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x26, 0x0);
332 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
333 field, 0xff, 0);
f0761be3
TH
334}
335
c3f69c7f
BVA
336static struct attribute *ata_common_sdev_attrs[] = {
337 &dev_attr_unload_heads.attr,
45fabbb7
EO
338 NULL
339};
c3f69c7f
BVA
340
341static const struct attribute_group ata_common_sdev_attr_group = {
342 .attrs = ata_common_sdev_attrs
343};
344
345const struct attribute_group *ata_common_sdev_groups[] = {
346 &ata_common_sdev_attr_group,
347 NULL
348};
349EXPORT_SYMBOL_GPL(ata_common_sdev_groups);
45fabbb7 350
1da177e4
LT
351/**
352 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
353 * @sdev: SCSI device for which BIOS geometry is to be determined
354 * @bdev: block device associated with @sdev
355 * @capacity: capacity of SCSI device
356 * @geom: location to which geometry will be output
357 *
358 * Generic bios head/sector/cylinder calculator
359 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
360 * mapping. Some situations may arise where the disk is not
361 * bootable if this is not used.
362 *
363 * LOCKING:
364 * Defined by the SCSI layer. We don't really care.
365 *
366 * RETURNS:
367 * Zero.
368 */
369int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
370 sector_t capacity, int geom[])
371{
372 geom[0] = 255;
373 geom[1] = 63;
374 sector_div(capacity, 255*63);
375 geom[2] = capacity;
376
377 return 0;
378}
a52fbcfc 379EXPORT_SYMBOL_GPL(ata_std_bios_param);
1da177e4 380
d8d9129e
TH
381/**
382 * ata_scsi_unlock_native_capacity - unlock native capacity
383 * @sdev: SCSI device to adjust device capacity for
384 *
385 * This function is called if a partition on @sdev extends beyond
386 * the end of the device. It requests EH to unlock HPA.
387 *
388 * LOCKING:
389 * Defined by the SCSI layer. Might sleep.
390 */
391void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
392{
393 struct ata_port *ap = ata_shost_to_port(sdev->host);
394 struct ata_device *dev;
395 unsigned long flags;
396
397 spin_lock_irqsave(ap->lock, flags);
398
399 dev = ata_scsi_find_dev(ap, sdev);
400 if (dev && dev->n_sectors < dev->n_native_sectors) {
401 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
402 dev->link->eh_info.action |= ATA_EH_RESET;
403 ata_port_schedule_eh(ap);
404 }
405
406 spin_unlock_irqrestore(ap->lock, flags);
407 ata_port_wait_eh(ap);
408}
a52fbcfc 409EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
d8d9129e 410
5924b74c
TH
411/**
412 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
5eb66fe0 413 * @ap: target port
5924b74c
TH
414 * @sdev: SCSI device to get identify data for
415 * @arg: User buffer area for identify data
416 *
417 * LOCKING:
418 * Defined by the SCSI layer. We don't really care.
419 *
420 * RETURNS:
421 * Zero on success, negative errno on error.
422 */
94be9a58
JG
423static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
424 void __user *arg)
5924b74c 425{
5924b74c
TH
426 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
427 u16 __user *dst = arg;
428 char buf[40];
429
430 if (!dev)
431 return -ENOMSG;
432
433 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
434 return -EFAULT;
435
436 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
437 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
438 return -EFAULT;
439
440 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
441 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
442 return -EFAULT;
443
444 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
445 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
446 return -EFAULT;
447
448 return 0;
449}
450
b095518e
JG
451/**
452 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
8e8b77dd 453 * @scsidev: Device to which we are issuing command
b095518e
JG
454 * @arg: User provided data for issuing command
455 *
456 * LOCKING:
457 * Defined by the SCSI layer. We don't really care.
458 *
459 * RETURNS:
460 * Zero on success, negative errno on error.
461 */
b095518e
JG
462int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
463{
464 int rc = 0;
429296cc 465 u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
b095518e 466 u8 scsi_cmd[MAX_COMMAND_SIZE];
429296cc 467 u8 args[4], *argbuf = NULL;
b095518e 468 int argsize = 0;
76aaf87b 469 struct scsi_sense_hdr sshdr;
5fa7b072
MC
470 const struct scsi_exec_args exec_args = {
471 .sshdr = &sshdr,
472 .sense = sensebuf,
473 .sense_len = sizeof(sensebuf),
474 };
bbe1fe7e 475 int cmd_result;
b095518e 476
c893a3ae 477 if (arg == NULL)
b095518e
JG
478 return -EINVAL;
479
480 if (copy_from_user(args, arg, sizeof(args)))
481 return -EFAULT;
482
429296cc 483 memset(sensebuf, 0, sizeof(sensebuf));
b095518e
JG
484 memset(scsi_cmd, 0, sizeof(scsi_cmd));
485
486 if (args[3]) {
295124dc 487 argsize = ATA_SECT_SIZE * args[3];
b095518e 488 argbuf = kmalloc(argsize, GFP_KERNEL);
54dac83c
JR
489 if (argbuf == NULL) {
490 rc = -ENOMEM;
491 goto error;
492 }
b095518e
JG
493
494 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
495 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
2dcb407e 496 block count in sector count field */
b095518e
JG
497 } else {
498 scsi_cmd[1] = (3 << 1); /* Non-data */
bbe1fe7e 499 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
b095518e
JG
500 }
501
502 scsi_cmd[0] = ATA_16;
503
504 scsi_cmd[4] = args[2];
a4f19040 505 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
b095518e
JG
506 scsi_cmd[6] = args[3];
507 scsi_cmd[8] = args[1];
f355eece
LW
508 scsi_cmd[10] = ATA_SMART_LBAM_PASS;
509 scsi_cmd[12] = ATA_SMART_LBAH_PASS;
b095518e
JG
510 } else {
511 scsi_cmd[6] = args[1];
512 }
513 scsi_cmd[14] = args[0];
514
515 /* Good values for timeout and retries? Values below
516 from scsi_ioctl_send_command() for default case... */
5fa7b072
MC
517 cmd_result = scsi_execute_cmd(scsidev, scsi_cmd, REQ_OP_DRV_IN, argbuf,
518 argsize, 10 * HZ, 5, &exec_args);
ced202f7
HR
519 if (cmd_result < 0) {
520 rc = cmd_result;
521 goto error;
522 }
464a00c9 523 if (scsi_sense_valid(&sshdr)) {/* sense data available */
bbe1fe7e 524 u8 *desc = sensebuf + 8;
bbe1fe7e
ET
525
526 /* If we set cc then ATA pass-through will cause a
527 * check condition even if no error. Filter that. */
464a00c9 528 if (scsi_status_is_check_condition(cmd_result)) {
6d3bfc7b
KM
529 if (sshdr.sense_key == RECOVERED_ERROR &&
530 sshdr.asc == 0 && sshdr.ascq == 0x1d)
bbe1fe7e
ET
531 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
532 }
533
534 /* Send userspace a few ATA registers (same as drivers/ide) */
2dcb407e
JG
535 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
536 desc[0] == 0x09) { /* code is "ATA Descriptor" */
537 args[0] = desc[13]; /* status */
538 args[1] = desc[3]; /* error */
539 args[2] = desc[5]; /* sector count (0:7) */
bbe1fe7e
ET
540 if (copy_to_user(arg, args, sizeof(args)))
541 rc = -EFAULT;
542 }
543 }
544
545
546 if (cmd_result) {
b095518e
JG
547 rc = -EIO;
548 goto error;
549 }
550
b095518e 551 if ((argbuf)
c893a3ae 552 && copy_to_user(arg + sizeof(args), argbuf, argsize))
b095518e
JG
553 rc = -EFAULT;
554error:
8f760780 555 kfree(argbuf);
b095518e
JG
556 return rc;
557}
558
559/**
560 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
8e8b77dd 561 * @scsidev: Device to which we are issuing command
b095518e
JG
562 * @arg: User provided data for issuing command
563 *
564 * LOCKING:
565 * Defined by the SCSI layer. We don't really care.
566 *
567 * RETURNS:
568 * Zero on success, negative errno on error.
569 */
570int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
571{
572 int rc = 0;
429296cc 573 u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
b095518e 574 u8 scsi_cmd[MAX_COMMAND_SIZE];
429296cc 575 u8 args[7];
76aaf87b 576 struct scsi_sense_hdr sshdr;
af068bd1 577 int cmd_result;
5fa7b072
MC
578 const struct scsi_exec_args exec_args = {
579 .sshdr = &sshdr,
580 .sense = sensebuf,
581 .sense_len = sizeof(sensebuf),
582 };
b095518e 583
c893a3ae 584 if (arg == NULL)
b095518e
JG
585 return -EINVAL;
586
587 if (copy_from_user(args, arg, sizeof(args)))
588 return -EFAULT;
589
429296cc 590 memset(sensebuf, 0, sizeof(sensebuf));
b095518e
JG
591 memset(scsi_cmd, 0, sizeof(scsi_cmd));
592 scsi_cmd[0] = ATA_16;
593 scsi_cmd[1] = (3 << 1); /* Non-data */
af068bd1 594 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
b095518e
JG
595 scsi_cmd[4] = args[1];
596 scsi_cmd[6] = args[2];
597 scsi_cmd[8] = args[3];
598 scsi_cmd[10] = args[4];
599 scsi_cmd[12] = args[5];
277239f2 600 scsi_cmd[13] = args[6] & 0x4f;
b095518e
JG
601 scsi_cmd[14] = args[0];
602
b095518e 603 /* Good values for timeout and retries? Values below
2e9edbf8 604 from scsi_ioctl_send_command() for default case... */
5fa7b072
MC
605 cmd_result = scsi_execute_cmd(scsidev, scsi_cmd, REQ_OP_DRV_IN, NULL,
606 0, 10 * HZ, 5, &exec_args);
ced202f7
HR
607 if (cmd_result < 0) {
608 rc = cmd_result;
609 goto error;
610 }
464a00c9 611 if (scsi_sense_valid(&sshdr)) {/* sense data available */
af068bd1 612 u8 *desc = sensebuf + 8;
af068bd1
DM
613
614 /* If we set cc then ATA pass-through will cause a
615 * check condition even if no error. Filter that. */
616 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
6d3bfc7b
KM
617 if (sshdr.sense_key == RECOVERED_ERROR &&
618 sshdr.asc == 0 && sshdr.ascq == 0x1d)
af068bd1
DM
619 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
620 }
621
622 /* Send userspace ATA registers */
623 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
624 desc[0] == 0x09) {/* code is "ATA Descriptor" */
625 args[0] = desc[13]; /* status */
626 args[1] = desc[3]; /* error */
627 args[2] = desc[5]; /* sector count (0:7) */
628 args[3] = desc[7]; /* lbal */
629 args[4] = desc[9]; /* lbam */
630 args[5] = desc[11]; /* lbah */
631 args[6] = desc[12]; /* select */
632 if (copy_to_user(arg, args, sizeof(args)))
633 rc = -EFAULT;
634 }
635 }
636
637 if (cmd_result) {
b095518e 638 rc = -EIO;
af068bd1
DM
639 goto error;
640 }
b095518e 641
af068bd1 642 error:
b095518e
JG
643 return rc;
644}
645
0184898d 646static bool ata_ioc32(struct ata_port *ap)
e3cf95dd
AC
647{
648 if (ap->flags & ATA_FLAG_PIO_DMA)
0184898d 649 return true;
e3cf95dd 650 if (ap->pflags & ATA_PFLAG_PIO32)
0184898d
SS
651 return true;
652 return false;
e3cf95dd
AC
653}
654
75c0b0e1
AB
655/*
656 * This handles both native and compat commands, so anything added
657 * here must have a compatible argument, or check in_compat_syscall()
658 */
94be9a58 659int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
6f4e626f 660 unsigned int cmd, void __user *arg)
1da177e4 661{
287e6611
AB
662 unsigned long val;
663 int rc = -EINVAL;
e3cf95dd 664 unsigned long flags;
1da177e4 665
1da177e4 666 switch (cmd) {
287e6611 667 case HDIO_GET_32BIT:
e3cf95dd
AC
668 spin_lock_irqsave(ap->lock, flags);
669 val = ata_ioc32(ap);
670 spin_unlock_irqrestore(ap->lock, flags);
75c0b0e1
AB
671#ifdef CONFIG_COMPAT
672 if (in_compat_syscall())
673 return put_user(val, (compat_ulong_t __user *)arg);
674#endif
287e6611 675 return put_user(val, (unsigned long __user *)arg);
1da177e4 676
287e6611 677 case HDIO_SET_32BIT:
1da177e4 678 val = (unsigned long) arg;
e3cf95dd
AC
679 rc = 0;
680 spin_lock_irqsave(ap->lock, flags);
681 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
682 if (val)
683 ap->pflags |= ATA_PFLAG_PIO32;
684 else
685 ap->pflags &= ~ATA_PFLAG_PIO32;
686 } else {
687 if (val != ata_ioc32(ap))
688 rc = -EINVAL;
689 }
690 spin_unlock_irqrestore(ap->lock, flags);
691 return rc;
1da177e4 692
5924b74c 693 case HDIO_GET_IDENTITY:
94be9a58 694 return ata_get_identity(ap, scsidev, arg);
5924b74c 695
b095518e
JG
696 case HDIO_DRIVE_CMD:
697 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
698 return -EACCES;
699 return ata_cmd_ioctl(scsidev, arg);
700
701 case HDIO_DRIVE_TASK:
702 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
703 return -EACCES;
704 return ata_task_ioctl(scsidev, arg);
705
1da177e4
LT
706 default:
707 rc = -ENOTTY;
708 break;
709 }
710
1da177e4
LT
711 return rc;
712}
94be9a58
JG
713EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
714
6f4e626f
NC
715int ata_scsi_ioctl(struct scsi_device *scsidev, unsigned int cmd,
716 void __user *arg)
94be9a58
JG
717{
718 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
719 scsidev, cmd, arg);
720}
721EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
1da177e4
LT
722
723/**
724 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
1da177e4
LT
725 * @dev: ATA device to which the new command is attached
726 * @cmd: SCSI command that originated this ATA command
1da177e4
LT
727 *
728 * Obtain a reference to an unused ata_queued_cmd structure,
729 * which is the basic libata structure representing a single
730 * ATA command sent to the hardware.
731 *
732 * If a command was available, fill in the SCSI-specific
733 * portions of the structure with information on the
734 * current command.
735 *
736 * LOCKING:
cca3974e 737 * spin_lock_irqsave(host lock)
1da177e4
LT
738 *
739 * RETURNS:
740 * Command allocated, or %NULL if none available.
741 */
7102d230 742static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
b27dcfb0 743 struct scsi_cmnd *cmd)
1da177e4 744{
4f1a22ee 745 struct ata_port *ap = dev->link->ap;
1da177e4 746 struct ata_queued_cmd *qc;
4f1a22ee
JG
747 int tag;
748
4cb7c6f1 749 if (unlikely(ata_port_is_frozen(ap)))
4f1a22ee
JG
750 goto fail;
751
752 if (ap->flags & ATA_FLAG_SAS_HOST) {
753 /*
754 * SAS hosts may queue > ATA_MAX_QUEUE commands so use
755 * unique per-device budget token as a tag.
756 */
757 if (WARN_ON_ONCE(cmd->budget_token >= ATA_MAX_QUEUE))
758 goto fail;
759 tag = cmd->budget_token;
760 } else {
761 tag = scsi_cmd_to_rq(cmd)->tag;
762 }
1da177e4 763
4f1a22ee
JG
764 qc = __ata_qc_from_tag(ap, tag);
765 qc->tag = qc->hw_tag = tag;
766 qc->ap = ap;
767 qc->dev = dev;
1da177e4 768
4f1a22ee 769 ata_qc_reinit(qc);
7eb49509 770
4f1a22ee
JG
771 qc->scsicmd = cmd;
772 qc->scsidone = scsi_done;
773
774 qc->sg = scsi_sglist(cmd);
775 qc->n_elem = scsi_sg_count(cmd);
776
777 if (scsi_cmd_to_rq(cmd)->rq_flags & RQF_QUIET)
778 qc->flags |= ATA_QCFLAG_QUIET;
1da177e4
LT
779
780 return qc;
4f1a22ee
JG
781
782fail:
783 set_host_byte(cmd, DID_OK);
784 set_status_byte(cmd, SAM_STAT_TASK_SET_FULL);
785 scsi_done(cmd);
786 return NULL;
1da177e4
LT
787}
788
aacda375
TH
789static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
790{
791 struct scsi_cmnd *scmd = qc->scsicmd;
792
bdf8710d 793 qc->extrabytes = scmd->extra_len;
aacda375
TH
794 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
795}
796
1da177e4
LT
797/**
798 * ata_to_sense_error - convert ATA error to SCSI error
1da177e4 799 * @drv_stat: value contained in ATA status register
b095518e
JG
800 * @drv_err: value contained in ATA error register
801 * @sk: the sense key we'll fill out
802 * @asc: the additional sense code we'll fill out
803 * @ascq: the additional sense code qualifier we'll fill out
1da177e4 804 *
b095518e
JG
805 * Converts an ATA error into a SCSI error. Fill out pointers to
806 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
807 * format sense blocks.
1da177e4
LT
808 *
809 * LOCKING:
cca3974e 810 * spin_lock_irqsave(host lock)
1da177e4 811 */
ea3b26a9
IP
812static void ata_to_sense_error(u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
813 u8 *ascq)
1da177e4 814{
b095518e 815 int i;
ffe75ef6 816
1da177e4 817 /* Based on the 3ware driver translation table */
98ac62de 818 static const unsigned char sense_table[][4] = {
1da177e4 819 /* BBD|ECC|ID|MAR */
8ae72044
HR
820 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
821 // Device busy Aborted command
1da177e4 822 /* BBD|ECC|ID */
8ae72044
HR
823 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
824 // Device busy Aborted command
1da177e4 825 /* ECC|MC|MARK */
8ae72044
HR
826 {0x61, HARDWARE_ERROR, 0x00, 0x00},
827 // Device fault Hardware error
1da177e4 828 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
8ae72044
HR
829 {0x84, ABORTED_COMMAND, 0x47, 0x00},
830 // Data CRC error SCSI parity error
1da177e4 831 /* MC|ID|ABRT|TRK0|MARK */
8ae72044
HR
832 {0x37, NOT_READY, 0x04, 0x00},
833 // Unit offline Not ready
1da177e4 834 /* MCR|MARK */
8ae72044
HR
835 {0x09, NOT_READY, 0x04, 0x00},
836 // Unrecovered disk error Not ready
1da177e4 837 /* Bad address mark */
8ae72044
HR
838 {0x01, MEDIUM_ERROR, 0x13, 0x00},
839 // Address mark not found for data field
840 /* TRK0 - Track 0 not found */
841 {0x02, HARDWARE_ERROR, 0x00, 0x00},
842 // Hardware error
78062c50 843 /* Abort: 0x04 is not translated here, see below */
1da177e4 844 /* Media change request */
8ae72044
HR
845 {0x08, NOT_READY, 0x04, 0x00},
846 // FIXME: faking offline
847 /* SRV/IDNF - ID not found */
848 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
849 // Logical address out of range
850 /* MC - Media Changed */
851 {0x20, UNIT_ATTENTION, 0x28, 0x00},
852 // Not ready to ready change, medium may have changed
853 /* ECC - Uncorrectable ECC error */
854 {0x40, MEDIUM_ERROR, 0x11, 0x04},
855 // Unrecovered read error
1da177e4 856 /* BBD - block marked bad */
8ae72044
HR
857 {0x80, MEDIUM_ERROR, 0x11, 0x04},
858 // Block marked bad Medium error, unrecovered read error
1da177e4
LT
859 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
860 };
98ac62de 861 static const unsigned char stat_table[][4] = {
1da177e4 862 /* Must be first because BUSY means no other bits valid */
8ae72044
HR
863 {0x80, ABORTED_COMMAND, 0x47, 0x00},
864 // Busy, fake parity for now
865 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
866 // Device ready, unaligned write command
867 {0x20, HARDWARE_ERROR, 0x44, 0x00},
868 // Device fault, internal target failure
869 {0x08, ABORTED_COMMAND, 0x47, 0x00},
870 // Timed out in xfer, fake parity for now
871 {0x04, RECOVERED_ERROR, 0x11, 0x00},
872 // Recovered ECC error Medium error, recovered
1da177e4
LT
873 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
874 };
1da177e4 875
1da177e4
LT
876 /*
877 * Is this an error we can process/parse
878 */
b095518e
JG
879 if (drv_stat & ATA_BUSY) {
880 drv_err = 0; /* Ignore the err bits, they're invalid */
1da177e4 881 }
b095518e
JG
882
883 if (drv_err) {
884 /* Look for drv_err */
885 for (i = 0; sense_table[i][0] != 0xFF; i++) {
886 /* Look for best matches first */
2e9edbf8 887 if ((sense_table[i][0] & drv_err) ==
b095518e
JG
888 sense_table[i][0]) {
889 *sk = sense_table[i][1];
890 *asc = sense_table[i][2];
891 *ascq = sense_table[i][3];
ff8072d5 892 return;
b095518e
JG
893 }
894 }
1da177e4 895 }
b095518e 896
78062c50
GG
897 /*
898 * Fall back to interpreting status bits. Note that if the drv_err
899 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
900 * is not descriptive enough.
901 */
b095518e
JG
902 for (i = 0; stat_table[i][0] != 0xFF; i++) {
903 if (stat_table[i][0] & drv_stat) {
904 *sk = stat_table[i][1];
905 *asc = stat_table[i][2];
906 *ascq = stat_table[i][3];
ff8072d5 907 return;
1da177e4 908 }
b095518e 909 }
b095518e 910
78062c50
GG
911 /*
912 * We need a sensible error return here, which is tricky, and one
913 * that won't cause people to do things like return a disk wrongly.
914 */
2d202024
AC
915 *sk = ABORTED_COMMAND;
916 *asc = 0x00;
917 *ascq = 0x00;
b095518e
JG
918}
919
920/*
750426aa 921 * ata_gen_passthru_sense - Generate check condition sense block.
b095518e
JG
922 * @qc: Command that completed.
923 *
97981926
IP
924 * This function is specific to the ATA pass through commands.
925 * Regardless of whether the command errored or not, return a sense
84a9a8cd
GG
926 * block. If there was no error, we get the request from an ATA
927 * passthrough command, so we use the following sense data:
928 * sk = RECOVERED ERROR
929 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
930 *
b095518e
JG
931 *
932 * LOCKING:
750426aa 933 * None.
b095518e 934 */
750426aa 935static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
b095518e 936{
816be86c 937 struct ata_device *dev = qc->dev;
b095518e 938 struct scsi_cmnd *cmd = qc->scsicmd;
e61e0672 939 struct ata_taskfile *tf = &qc->result_tf;
b525e773 940 u8 sense_key, asc, ascq;
b095518e 941
816be86c
IP
942 if (!(qc->flags & ATA_QCFLAG_RTF_FILLED)) {
943 ata_dev_dbg(dev,
944 "missing result TF: can't generate ATA PT sense data\n");
945 return;
946 }
b095518e 947
b095518e
JG
948 /*
949 * Use ata_to_sense_error() to map status register bits
950 * onto sense key, asc & ascq.
951 */
058e55e1 952 if (qc->err_mask ||
efcef265 953 tf->status & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
ea3b26a9 954 ata_to_sense_error(tf->status, tf->error,
ff8072d5 955 &sense_key, &asc, &ascq);
06dbde5f 956 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
84a9a8cd 957 } else {
fa0db8e5
NC
958 /*
959 * ATA PASS-THROUGH INFORMATION AVAILABLE
960 *
961 * Note: we are supposed to call ata_scsi_set_sense(), which
962 * respects the D_SENSE bit, instead of unconditionally
963 * generating the sense data in descriptor format. However,
964 * because hdparm, hddtemp, and udisks incorrectly assume sense
965 * data in descriptor format, without even looking at the
966 * RESPONSE CODE field in the returned sense data (to see which
967 * format the returned sense data is in), we are stuck with
968 * being bug compatible with older kernels.
969 */
970 scsi_build_sense(cmd, 1, RECOVERED_ERROR, 0, 0x1D);
1da177e4 971 }
b095518e 972}
1da177e4 973
b095518e 974/**
750426aa 975 * ata_gen_ata_sense - generate a SCSI fixed sense block
b095518e
JG
976 * @qc: Command that we are erroring out
977 *
11533932 978 * Generate sense block for a failed ATA command @qc.
b095518e
JG
979 *
980 * LOCKING:
750426aa 981 * None.
b095518e 982 */
750426aa 983static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
b095518e 984{
d25614ba 985 struct ata_device *dev = qc->dev;
b095518e 986 struct scsi_cmnd *cmd = qc->scsicmd;
e61e0672 987 struct ata_taskfile *tf = &qc->result_tf;
b525e773 988 u8 sense_key, asc, ascq;
b095518e 989
59f68474
HR
990 if (ata_dev_disabled(dev)) {
991 /* Device disabled after error recovery */
992 /* LOGICAL UNIT NOT READY, HARD RESET REQUIRED */
993 ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
994 return;
995 }
816be86c
IP
996
997 if (!(qc->flags & ATA_QCFLAG_RTF_FILLED)) {
998 ata_dev_dbg(dev,
999 "missing result TF: can't generate sense data\n");
1000 return;
1001 }
1002
d25614ba 1003 /* Use ata_to_sense_error() to map status register bits
b095518e
JG
1004 * onto sense key, asc & ascq.
1005 */
058e55e1 1006 if (qc->err_mask ||
efcef265 1007 tf->status & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
ea3b26a9 1008 ata_to_sense_error(tf->status, tf->error,
ff8072d5 1009 &sense_key, &asc, &ascq);
06dbde5f 1010 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
5e6acd1c
HR
1011 } else {
1012 /* Could not decode error */
1013 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
efcef265 1014 tf->status, qc->err_mask);
06dbde5f 1015 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
5e6acd1c 1016 return;
1da177e4 1017 }
1da177e4
LT
1018}
1019
15964ff7 1020void ata_scsi_sdev_config(struct scsi_device *sdev)
a6cce2a7
BK
1021{
1022 sdev->use_10_for_rw = 1;
1023 sdev->use_10_for_ms = 1;
e185934f 1024 sdev->no_write_same = 1;
31cc23b3
TH
1025
1026 /* Schedule policy is determined by ->qc_defer() callback and
1027 * it needs to see every deferred qc. Set dev_blocked to 1 to
1028 * prevent SCSI midlayer from automatically deferring
1029 * requests.
1030 */
1031 sdev->max_device_blocked = 1;
a6cce2a7
BK
1032}
1033
fa2fc7f4 1034/**
94bd5719 1035 * ata_scsi_dma_need_drain - Check whether data transfer may overflow
73fd8b6d 1036 * @rq: request to be checked
fa2fc7f4
JB
1037 *
1038 * ATAPI commands which transfer variable length data to host
c9b5560a 1039 * might overflow due to application error or hardware bug. This
fa2fc7f4
JB
1040 * function checks whether overflow should be drained and ignored
1041 * for @request.
1042 *
1043 * LOCKING:
1044 * None.
1045 *
1046 * RETURNS:
1047 * 1 if ; otherwise, 0.
1048 */
cc97923a 1049bool ata_scsi_dma_need_drain(struct request *rq)
fa2fc7f4 1050{
ce70fd9a
CH
1051 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
1052
1053 return atapi_cmd_type(scmd->cmnd[0]) == ATAPI_MISC;
fa2fc7f4 1054}
cc97923a 1055EXPORT_SYMBOL_GPL(ata_scsi_dma_need_drain);
fa2fc7f4 1056
a25a9c85
CH
1057int ata_scsi_dev_config(struct scsi_device *sdev, struct queue_limits *lim,
1058 struct ata_device *dev)
a6cce2a7 1059{
6a8438de 1060 int depth = 1;
900e599e 1061
45fabbb7
EO
1062 if (!ata_id_has_unload(dev->id))
1063 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1064
914ed354 1065 /* configure max sectors */
0568e612 1066 dev->max_sectors = min(dev->max_sectors, sdev->host->max_sectors);
a25a9c85 1067 lim->max_hw_sectors = dev->max_sectors;
a6cce2a7 1068
fa2fc7f4 1069 if (dev->class == ATA_DEV_ATAPI) {
729a6a30
TH
1070 sdev->sector_size = ATA_SECT_SIZE;
1071
1072 /* set DMA padding */
e94b45d0 1073 lim->dma_pad_mask = ATA_DMA_PAD_SZ - 1;
fa2fc7f4 1074
cc97923a 1075 /* make room for appending the drain */
a25a9c85 1076 lim->max_segments--;
cc97923a
CH
1077
1078 sdev->dma_drain_len = ATAPI_MAX_DRAIN;
ce288e05 1079 sdev->dma_drain_buf = kmalloc(sdev->dma_drain_len, GFP_NOIO);
cc97923a 1080 if (!sdev->dma_drain_buf) {
a9a79dfe 1081 ata_dev_err(dev, "drain buffer allocation failed\n");
fa2fc7f4
JB
1082 return -ENOMEM;
1083 }
fa2fc7f4 1084 } else {
295124dc 1085 sdev->sector_size = ata_id_logical_sector_size(dev->id);
aa3998db 1086
0a858905 1087 /*
aa3998db 1088 * Ask the sd driver to issue START STOP UNIT on runtime suspend
24eca2dc
DLM
1089 * and resume and shutdown only. For system level suspend/resume,
1090 * devices power state is handled directly by libata EH.
b09d7f8f
DLM
1091 * Given that disks are always spun up on system resume, also
1092 * make sure that the sd driver forces runtime suspended disks
1093 * to be resumed to correctly reflect the power state of the
1094 * device.
0a858905 1095 */
6371be7a
DLM
1096 sdev->manage_runtime_start_stop = 1;
1097 sdev->manage_shutdown = 1;
b09d7f8f 1098 sdev->force_runtime_start_on_system_start = 1;
dde20207 1099 }
d8cf5389 1100
729a6a30
TH
1101 /*
1102 * ata_pio_sectors() expects buffer for each sector to not cross
1103 * page boundary. Enforce it by requiring buffers to be sector
1104 * aligned, which works iff sector_size is not larger than
1105 * PAGE_SIZE. ATAPI devices also need the alignment as
1106 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1107 */
1108 if (sdev->sector_size > PAGE_SIZE)
a9a79dfe 1109 ata_dev_warn(dev,
729a6a30
TH
1110 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1111 sdev->sector_size);
1112
a25a9c85 1113 lim->dma_alignment = sdev->sector_size - 1;
729a6a30 1114
f26792d5
JG
1115 if (dev->flags & ATA_DFLAG_AN)
1116 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1117
43cff7d9 1118 if (ata_ncq_supported(dev))
a6e6ce8e 1119 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
6a8438de
DLM
1120 depth = min(ATA_MAX_QUEUE, depth);
1121 scsi_change_queue_depth(sdev, depth);
fa2fc7f4 1122
d80210f2
CH
1123 if (dev->flags & ATA_DFLAG_TRUSTED)
1124 sdev->security_supported = 1;
1125
295124dc 1126 dev->sdev = sdev;
fa2fc7f4 1127 return 0;
a6cce2a7
BK
1128}
1129
fb99ef17 1130/**
ed638918 1131 * ata_scsi_sdev_init - Early setup of SCSI device
fb99ef17
DLM
1132 * @sdev: SCSI device to examine
1133 *
1134 * This is called from scsi_alloc_sdev() when the scsi device
1135 * associated with an ATA device is scanned on a port.
1136 *
1137 * LOCKING:
1138 * Defined by SCSI layer. We don't really care.
1139 */
1140
ed638918 1141int ata_scsi_sdev_init(struct scsi_device *sdev)
fb99ef17
DLM
1142{
1143 struct ata_port *ap = ata_shost_to_port(sdev->host);
1144 struct device_link *link;
1145
1146 ata_scsi_sdev_config(sdev);
1147
1148 /*
1149 * Create a link from the ata_port device to the scsi device to ensure
1150 * that PM does suspend/resume in the correct order: the scsi device is
1151 * consumer (child) and the ata port the supplier (parent).
1152 */
1153 link = device_link_add(&sdev->sdev_gendev, &ap->tdev,
1154 DL_FLAG_STATELESS |
1155 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE);
1156 if (!link) {
1157 ata_port_err(ap, "Failed to create link to scsi device %s\n",
1158 dev_name(&sdev->sdev_gendev));
1159 return -ENODEV;
1160 }
1161
1162 return 0;
1163}
ed638918 1164EXPORT_SYMBOL_GPL(ata_scsi_sdev_init);
fb99ef17 1165
1da177e4 1166/**
47c2e30a 1167 * ata_scsi_sdev_configure - Set SCSI device attributes
1da177e4 1168 * @sdev: SCSI device to examine
a25a9c85 1169 * @lim: queue limits
1da177e4
LT
1170 *
1171 * This is called before we actually start reading
1172 * and writing to the device, to configure certain
1173 * SCSI mid-layer behaviors.
1174 *
1175 * LOCKING:
1176 * Defined by SCSI layer. We don't really care.
1177 */
1178
47c2e30a 1179int ata_scsi_sdev_configure(struct scsi_device *sdev, struct queue_limits *lim)
1da177e4 1180{
31534363
TH
1181 struct ata_port *ap = ata_shost_to_port(sdev->host);
1182 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
1da177e4 1183
31534363 1184 if (dev)
a25a9c85 1185 return ata_scsi_dev_config(sdev, lim, dev);
1da177e4 1186
fb99ef17 1187 return 0;
1da177e4 1188}
47c2e30a 1189EXPORT_SYMBOL_GPL(ata_scsi_sdev_configure);
1da177e4 1190
83c47bcb 1191/**
ed638918 1192 * ata_scsi_sdev_destroy - SCSI device is about to be destroyed
83c47bcb
TH
1193 * @sdev: SCSI device to be destroyed
1194 *
1195 * @sdev is about to be destroyed for hot/warm unplugging. If
1196 * this unplugging was initiated by libata as indicated by NULL
1197 * dev->sdev, this function doesn't have to do anything.
1198 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1199 * Clear dev->sdev, schedule the device for ATA detach and invoke
1200 * EH.
1201 *
1202 * LOCKING:
1203 * Defined by SCSI layer. We don't really care.
1204 */
ed638918 1205void ata_scsi_sdev_destroy(struct scsi_device *sdev)
83c47bcb
TH
1206{
1207 struct ata_port *ap = ata_shost_to_port(sdev->host);
1208 unsigned long flags;
1209 struct ata_device *dev;
1210
fb99ef17
DLM
1211 device_link_remove(&sdev->sdev_gendev, &ap->tdev);
1212
ba6a1308 1213 spin_lock_irqsave(ap->lock, flags);
83c47bcb
TH
1214 dev = __ata_scsi_find_dev(ap, sdev);
1215 if (dev && dev->sdev) {
1216 /* SCSI device already in CANCEL state, no need to offline it */
1217 dev->sdev = NULL;
1218 dev->flags |= ATA_DFLAG_DETACH;
1219 ata_port_schedule_eh(ap);
1220 }
ba6a1308 1221 spin_unlock_irqrestore(ap->lock, flags);
fa2fc7f4 1222
cc97923a 1223 kfree(sdev->dma_drain_buf);
83c47bcb 1224}
ed638918 1225EXPORT_SYMBOL_GPL(ata_scsi_sdev_destroy);
83c47bcb 1226
972dcafb
DG
1227/**
1228 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1229 * @qc: Storage for translated ATA taskfile
972dcafb
DG
1230 *
1231 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1232 * (to start). Perhaps these commands should be preceded by
1233 * CHECK POWER MODE to see what power mode the device is already in.
1234 * [See SAT revision 5 at www.t10.org]
1235 *
1236 * LOCKING:
cca3974e 1237 * spin_lock_irqsave(host lock)
972dcafb
DG
1238 *
1239 * RETURNS:
1240 * Zero on success, non-zero on error.
1241 */
ad706991 1242static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
972dcafb 1243{
542b1444 1244 struct scsi_cmnd *scmd = qc->scsicmd;
ad706991 1245 const u8 *cdb = scmd->cmnd;
bcfc867d 1246 u16 fp;
0df10b84 1247 u8 bp = 0xff;
972dcafb 1248
bcfc867d
HR
1249 if (scmd->cmd_len < 5) {
1250 fp = 4;
2e5704f6 1251 goto invalid_fld;
bcfc867d 1252 }
2e5704f6 1253
8c1f0817 1254 /* LOEJ bit set not supported */
bcfc867d
HR
1255 if (cdb[4] & 0x2) {
1256 fp = 4;
0df10b84 1257 bp = 1;
8c1f0817 1258 goto invalid_fld;
bcfc867d 1259 }
8c1f0817
DLM
1260
1261 /* Power conditions not supported */
bcfc867d
HR
1262 if (((cdb[4] >> 4) & 0xf) != 0) {
1263 fp = 4;
0df10b84 1264 bp = 3;
8c1f0817 1265 goto invalid_fld;
bcfc867d 1266 }
e31e8531 1267
8c1f0817
DLM
1268 /* Ignore IMMED bit (cdb[1] & 0x1), violates sat-r05 */
1269 if (!ata_dev_power_init_tf(qc->dev, &qc->tf, cdb[4] & 0x1)) {
1270 ata_scsi_set_sense(qc->dev, scmd, ABORTED_COMMAND, 0, 0);
1271 return 1;
920a4b10 1272 }
78981a7c 1273
972dcafb
DG
1274 /*
1275 * Standby and Idle condition timers could be implemented but that
1276 * would require libata to implement the Power condition mode page
1277 * and allow the user to change it. Changing mode pages requires
1278 * MODE SELECT to be implemented.
1279 */
1280
1281 return 0;
ae006510 1282
2a6e58d2 1283 invalid_fld:
0df10b84 1284 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
ae006510 1285 return 1;
972dcafb
DG
1286}
1287
1da177e4
LT
1288/**
1289 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1290 * @qc: Storage for translated ATA taskfile
1da177e4
LT
1291 *
1292 * Sets up an ATA taskfile to issue FLUSH CACHE or
1293 * FLUSH CACHE EXT.
1294 *
1295 * LOCKING:
cca3974e 1296 * spin_lock_irqsave(host lock)
1da177e4
LT
1297 *
1298 * RETURNS:
1299 * Zero on success, non-zero on error.
1300 */
ad706991 1301static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
1da177e4
LT
1302{
1303 struct ata_taskfile *tf = &qc->tf;
1304
1305 tf->flags |= ATA_TFLAG_DEVICE;
1306 tf->protocol = ATA_PROT_NODATA;
1307
6fc49adb 1308 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
1da177e4
LT
1309 tf->command = ATA_CMD_FLUSH_EXT;
1310 else
1311 tf->command = ATA_CMD_FLUSH;
1312
b666da35
TH
1313 /* flush is critical for IO integrity, consider it an IO command */
1314 qc->flags |= ATA_QCFLAG_IO;
1315
1da177e4
LT
1316 return 0;
1317}
1318
3aef5231
AL
1319/**
1320 * scsi_6_lba_len - Get LBA and transfer length
542b1444 1321 * @cdb: SCSI command to translate
3aef5231
AL
1322 *
1323 * Calculate LBA and transfer length for 6-byte commands.
1324 *
1325 * RETURNS:
1326 * @plba: the LBA
1327 * @plen: the transfer length
1328 */
542b1444 1329static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
3aef5231 1330{
08b64eee
AS
1331 *plba = get_unaligned_be24(&cdb[1]) & 0x1fffff;
1332 *plen = cdb[4];
3aef5231
AL
1333}
1334
1335/**
1336 * scsi_10_lba_len - Get LBA and transfer length
542b1444 1337 * @cdb: SCSI command to translate
3aef5231
AL
1338 *
1339 * Calculate LBA and transfer length for 10-byte commands.
1340 *
1341 * RETURNS:
1342 * @plba: the LBA
1343 * @plen: the transfer length
1344 */
6e163f9b 1345static inline void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
3aef5231 1346{
6e163f9b
DLM
1347 *plba = get_unaligned_be32(&cdb[2]);
1348 *plen = get_unaligned_be16(&cdb[7]);
3aef5231
AL
1349}
1350
1351/**
1352 * scsi_16_lba_len - Get LBA and transfer length
542b1444 1353 * @cdb: SCSI command to translate
3aef5231
AL
1354 *
1355 * Calculate LBA and transfer length for 16-byte commands.
1356 *
1357 * RETURNS:
1358 * @plba: the LBA
1359 * @plen: the transfer length
1360 */
6e163f9b 1361static inline void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
3aef5231 1362{
6e163f9b
DLM
1363 *plba = get_unaligned_be64(&cdb[2]);
1364 *plen = get_unaligned_be32(&cdb[10]);
3aef5231
AL
1365}
1366
eafe804b
DLM
1367/**
1368 * scsi_dld - Get duration limit descriptor index
1369 * @cdb: SCSI command to translate
1370 *
1371 * Returns the dld bits indicating the index of a command duration limit
1372 * descriptor.
1373 */
1374static inline int scsi_dld(const u8 *cdb)
1375{
1376 return ((cdb[1] & 0x01) << 2) | ((cdb[14] >> 6) & 0x03);
1377}
1378
1da177e4
LT
1379/**
1380 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1381 * @qc: Storage for translated ATA taskfile
1da177e4
LT
1382 *
1383 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1384 *
1385 * LOCKING:
cca3974e 1386 * spin_lock_irqsave(host lock)
1da177e4
LT
1387 *
1388 * RETURNS:
1389 * Zero on success, non-zero on error.
1390 */
ad706991 1391static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
1da177e4 1392{
542b1444 1393 struct scsi_cmnd *scmd = qc->scsicmd;
1da177e4 1394 struct ata_taskfile *tf = &qc->tf;
8bf62ece 1395 struct ata_device *dev = qc->dev;
1da177e4 1396 u64 dev_sectors = qc->dev->n_sectors;
ad706991 1397 const u8 *cdb = scmd->cmnd;
3aef5231
AL
1398 u64 block;
1399 u32 n_block;
bcfc867d 1400 u16 fp;
1da177e4
LT
1401
1402 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1403 tf->protocol = ATA_PROT_NODATA;
1da177e4 1404
59b00404
SS
1405 switch (cdb[0]) {
1406 case VERIFY:
bcfc867d
HR
1407 if (scmd->cmd_len < 10) {
1408 fp = 9;
2e5704f6 1409 goto invalid_fld;
bcfc867d 1410 }
542b1444 1411 scsi_10_lba_len(cdb, &block, &n_block);
59b00404
SS
1412 break;
1413 case VERIFY_16:
bcfc867d
HR
1414 if (scmd->cmd_len < 16) {
1415 fp = 15;
2e5704f6 1416 goto invalid_fld;
bcfc867d 1417 }
542b1444 1418 scsi_16_lba_len(cdb, &block, &n_block);
59b00404
SS
1419 break;
1420 default:
bcfc867d 1421 fp = 0;
ae006510 1422 goto invalid_fld;
bcfc867d 1423 }
1da177e4 1424
8bf62ece 1425 if (!n_block)
ae006510 1426 goto nothing_to_do;
8bf62ece 1427 if (block >= dev_sectors)
ae006510 1428 goto out_of_range;
8bf62ece 1429 if ((block + n_block) > dev_sectors)
ae006510 1430 goto out_of_range;
1da177e4 1431
07506697
AL
1432 if (dev->flags & ATA_DFLAG_LBA) {
1433 tf->flags |= ATA_TFLAG_LBA;
1434
c6a33e24
AL
1435 if (lba_28_ok(block, n_block)) {
1436 /* use LBA28 */
1437 tf->command = ATA_CMD_VERIFY;
1438 tf->device |= (block >> 24) & 0xf;
1439 } else if (lba_48_ok(block, n_block)) {
1440 if (!(dev->flags & ATA_DFLAG_LBA48))
1441 goto out_of_range;
07506697
AL
1442
1443 /* use LBA48 */
1444 tf->flags |= ATA_TFLAG_LBA48;
8bf62ece 1445 tf->command = ATA_CMD_VERIFY_EXT;
1da177e4 1446
8bf62ece 1447 tf->hob_nsect = (n_block >> 8) & 0xff;
1da177e4 1448
8bf62ece
AL
1449 tf->hob_lbah = (block >> 40) & 0xff;
1450 tf->hob_lbam = (block >> 32) & 0xff;
1451 tf->hob_lbal = (block >> 24) & 0xff;
c6a33e24
AL
1452 } else
1453 /* request too large even for LBA48 */
1454 goto out_of_range;
8bf62ece
AL
1455
1456 tf->nsect = n_block & 0xff;
1da177e4 1457
8bf62ece
AL
1458 tf->lbah = (block >> 16) & 0xff;
1459 tf->lbam = (block >> 8) & 0xff;
1460 tf->lbal = block & 0xff;
1da177e4 1461
8bf62ece
AL
1462 tf->device |= ATA_LBA;
1463 } else {
1464 /* CHS */
1465 u32 sect, head, cyl, track;
1466
c6a33e24
AL
1467 if (!lba_28_ok(block, n_block))
1468 goto out_of_range;
07506697 1469
8bf62ece
AL
1470 /* Convert LBA to CHS */
1471 track = (u32)block / dev->sectors;
1472 cyl = track / dev->heads;
1473 head = track % dev->heads;
1474 sect = (u32)block % dev->sectors + 1;
1475
2e9edbf8
JG
1476 /* Check whether the converted CHS can fit.
1477 Cylinder: 0-65535
8bf62ece
AL
1478 Head: 0-15
1479 Sector: 1-255*/
2e9edbf8 1480 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
ae006510 1481 goto out_of_range;
2e9edbf8 1482
8bf62ece
AL
1483 tf->command = ATA_CMD_VERIFY;
1484 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1485 tf->lbal = sect;
1486 tf->lbam = cyl;
1487 tf->lbah = cyl >> 8;
1488 tf->device |= head;
1489 }
1da177e4
LT
1490
1491 return 0;
ae006510
DG
1492
1493invalid_fld:
0df10b84 1494 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
ae006510
DG
1495 return 1;
1496
1497out_of_range:
06dbde5f 1498 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
ae006510
DG
1499 /* "Logical Block Address out of range" */
1500 return 1;
1501
1502nothing_to_do:
542b1444 1503 scmd->result = SAM_STAT_GOOD;
ae006510 1504 return 1;
1da177e4
LT
1505}
1506
2d727150
JA
1507static bool ata_check_nblocks(struct scsi_cmnd *scmd, u32 n_blocks)
1508{
c8329cd5 1509 struct request *rq = scsi_cmd_to_rq(scmd);
2d727150
JA
1510 u32 req_blocks;
1511
1512 if (!blk_rq_is_passthrough(rq))
1513 return true;
1514
1515 req_blocks = blk_rq_bytes(rq) / scmd->device->sector_size;
1516 if (n_blocks > req_blocks)
1517 return false;
1518
1519 return true;
1520}
1521
1da177e4
LT
1522/**
1523 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1524 * @qc: Storage for translated ATA taskfile
1da177e4
LT
1525 *
1526 * Converts any of six SCSI read/write commands into the
1527 * ATA counterpart, including starting sector (LBA),
1528 * sector count, and taking into account the device's LBA48
1529 * support.
1530 *
1531 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1532 * %WRITE_16 are currently supported.
1533 *
1534 * LOCKING:
cca3974e 1535 * spin_lock_irqsave(host lock)
1da177e4
LT
1536 *
1537 * RETURNS:
1538 * Zero on success, non-zero on error.
1539 */
ad706991 1540static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
1da177e4 1541{
542b1444 1542 struct scsi_cmnd *scmd = qc->scsicmd;
ad706991 1543 const u8 *cdb = scmd->cmnd;
c8329cd5 1544 struct request *rq = scsi_cmd_to_rq(scmd);
8e061784 1545 int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
bd056d7e 1546 unsigned int tf_flags = 0;
eafe804b 1547 int dld = 0;
3aef5231
AL
1548 u64 block;
1549 u32 n_block;
bd056d7e 1550 int rc;
bcfc867d 1551 u16 fp = 0;
1da177e4 1552
59b00404
SS
1553 switch (cdb[0]) {
1554 case WRITE_6:
1555 case WRITE_10:
1556 case WRITE_16:
bd056d7e 1557 tf_flags |= ATA_TFLAG_WRITE;
59b00404
SS
1558 break;
1559 }
1da177e4 1560
9a3dccc4 1561 /* Calculate the SCSI LBA, transfer length and FUA. */
542b1444 1562 switch (cdb[0]) {
3aef5231
AL
1563 case READ_10:
1564 case WRITE_10:
bcfc867d
HR
1565 if (unlikely(scmd->cmd_len < 10)) {
1566 fp = 9;
2e5704f6 1567 goto invalid_fld;
bcfc867d 1568 }
542b1444 1569 scsi_10_lba_len(cdb, &block, &n_block);
3e451a49 1570 if (cdb[1] & (1 << 3))
bd056d7e 1571 tf_flags |= ATA_TFLAG_FUA;
2d727150
JA
1572 if (!ata_check_nblocks(scmd, n_block))
1573 goto invalid_fld;
3aef5231
AL
1574 break;
1575 case READ_6:
1576 case WRITE_6:
bcfc867d
HR
1577 if (unlikely(scmd->cmd_len < 6)) {
1578 fp = 5;
2e5704f6 1579 goto invalid_fld;
bcfc867d 1580 }
542b1444 1581 scsi_6_lba_len(cdb, &block, &n_block);
c187c4b5
AL
1582
1583 /* for 6-byte r/w commands, transfer length 0
1584 * means 256 blocks of data, not 0 block.
1585 */
76b2bf9b
JG
1586 if (!n_block)
1587 n_block = 256;
2d727150
JA
1588 if (!ata_check_nblocks(scmd, n_block))
1589 goto invalid_fld;
3aef5231
AL
1590 break;
1591 case READ_16:
1592 case WRITE_16:
bcfc867d
HR
1593 if (unlikely(scmd->cmd_len < 16)) {
1594 fp = 15;
2e5704f6 1595 goto invalid_fld;
bcfc867d 1596 }
542b1444 1597 scsi_16_lba_len(cdb, &block, &n_block);
eafe804b 1598 dld = scsi_dld(cdb);
3e451a49 1599 if (cdb[1] & (1 << 3))
bd056d7e 1600 tf_flags |= ATA_TFLAG_FUA;
2d727150
JA
1601 if (!ata_check_nblocks(scmd, n_block))
1602 goto invalid_fld;
3aef5231
AL
1603 break;
1604 default:
bcfc867d 1605 fp = 0;
ae006510 1606 goto invalid_fld;
1da177e4
LT
1607 }
1608
8bf62ece
AL
1609 /* Check and compose ATA command */
1610 if (!n_block)
c187c4b5
AL
1611 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1612 * length 0 means transfer 0 block of data.
1613 * However, for ATA R/W commands, sector count 0 means
1614 * 256 or 65536 sectors, not 0 sectors as in SCSI.
f51750d5
AC
1615 *
1616 * WARNING: one or two older ATA drives treat 0 as 0...
c187c4b5 1617 */
ae006510 1618 goto nothing_to_do;
1da177e4 1619
bd056d7e 1620 qc->flags |= ATA_QCFLAG_IO;
295124dc 1621 qc->nbytes = n_block * scmd->device->sector_size;
1da177e4 1622
eafe804b 1623 rc = ata_build_rw_tf(qc, block, n_block, tf_flags, dld, class);
bd056d7e
TH
1624 if (likely(rc == 0))
1625 return 0;
ae006510 1626
bd056d7e
TH
1627 if (rc == -ERANGE)
1628 goto out_of_range;
1629 /* treat all other errors as -EINVAL, fall through */
ae006510 1630invalid_fld:
0df10b84 1631 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
ae006510
DG
1632 return 1;
1633
1634out_of_range:
06dbde5f 1635 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
ae006510
DG
1636 /* "Logical Block Address out of range" */
1637 return 1;
1638
1639nothing_to_do:
542b1444 1640 scmd->result = SAM_STAT_GOOD;
ae006510 1641 return 1;
1da177e4
LT
1642}
1643
2aa8f5d6
CH
1644static void ata_qc_done(struct ata_queued_cmd *qc)
1645{
1646 struct scsi_cmnd *cmd = qc->scsicmd;
1647 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1648
1649 ata_qc_free(qc);
1650 done(cmd);
1651}
1652
77853bf2 1653static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1da177e4
LT
1654{
1655 struct scsi_cmnd *cmd = qc->scsicmd;
a7dac447 1656 u8 *cdb = cmd->cmnd;
97981926
IP
1657 bool have_sense = qc->flags & ATA_QCFLAG_SENSE_VALID;
1658 bool is_ata_passthru = cdb[0] == ATA_16 || cdb[0] == ATA_12;
1659 bool is_ck_cond_request = cdb[2] & 0x20;
1660 bool is_error = qc->err_mask != 0;
b095518e
JG
1661
1662 /* For ATA pass thru (SAT) commands, generate a sense block if
1663 * user mandated it or if there's an error. Note that if we
97981926 1664 * generate because the user forced us to [CK_COND=1], a check
84a9a8cd 1665 * condition is generated and the ATA register values are returned
b095518e 1666 * whether the command completed successfully or not. If there
97981926 1667 * was no error, and CK_COND=1, we use the following sense data:
84a9a8cd
GG
1668 * sk = RECOVERED ERROR
1669 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
b095518e 1670 */
97981926
IP
1671 if (is_ata_passthru && (is_ck_cond_request || is_error || have_sense)) {
1672 if (!have_sense)
1673 ata_gen_passthru_sense(qc);
1674 ata_scsi_set_passthru_sense_fields(qc);
1675 if (is_ck_cond_request)
1676 set_status_byte(qc->scsicmd, SAM_STAT_CHECK_CONDITION);
11533932
IP
1677 } else if (is_error) {
1678 if (!have_sense)
1679 ata_gen_ata_sense(qc);
1680 ata_scsi_set_sense_information(qc);
97981926 1681 }
1da177e4 1682
2aa8f5d6 1683 ata_qc_done(qc);
1da177e4
LT
1684}
1685
1686/**
1687 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1da177e4
LT
1688 * @dev: ATA device to which the command is addressed
1689 * @cmd: SCSI command to execute
1da177e4
LT
1690 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1691 *
1692 * Our ->queuecommand() function has decided that the SCSI
1693 * command issued can be directly translated into an ATA
1694 * command, rather than handled internally.
1695 *
1696 * This function sets up an ata_queued_cmd structure for the
1697 * SCSI command, and sends that ata_queued_cmd to the hardware.
1698 *
ae006510
DG
1699 * The xlat_func argument (actor) returns 0 if ready to execute
1700 * ATA command, else 1 to finish translation. If 1 is returned
1701 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1702 * to be set reflecting an error condition or clean (early)
1703 * termination.
1704 *
1da177e4 1705 * LOCKING:
cca3974e 1706 * spin_lock_irqsave(host lock)
2115ea94
TH
1707 *
1708 * RETURNS:
1709 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1710 * needs to be deferred.
1da177e4 1711 */
2115ea94 1712static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
2115ea94 1713 ata_xlat_func_t xlat_func)
1da177e4 1714{
31cc23b3 1715 struct ata_port *ap = dev->link->ap;
1da177e4 1716 struct ata_queued_cmd *qc;
31cc23b3 1717 int rc;
1da177e4 1718
b27dcfb0 1719 qc = ata_scsi_qc_new(dev, cmd);
1da177e4 1720 if (!qc)
ae006510 1721 goto err_mem;
1da177e4
LT
1722
1723 /* data is present; dma-map it */
be7db055 1724 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1725 cmd->sc_data_direction == DMA_TO_DEVICE) {
7120165c 1726 if (unlikely(scsi_bufflen(cmd) < 1)) {
a9a79dfe 1727 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
ae006510 1728 goto err_did;
1da177e4
LT
1729 }
1730
7120165c 1731 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
1da177e4
LT
1732
1733 qc->dma_dir = cmd->sc_data_direction;
1734 }
1735
1736 qc->complete_fn = ata_scsi_qc_complete;
1737
ad706991 1738 if (xlat_func(qc))
ae006510 1739 goto early_finish;
1da177e4 1740
31cc23b3
TH
1741 if (ap->ops->qc_defer) {
1742 if ((rc = ap->ops->qc_defer(qc)))
1743 goto defer;
1744 }
1745
1da177e4 1746 /* select device, send command to hardware */
8e0e694a 1747 ata_qc_issue(qc);
1da177e4 1748
2115ea94 1749 return 0;
1da177e4 1750
ae006510 1751early_finish:
2dcb407e 1752 ata_qc_free(qc);
58bf201d 1753 scsi_done(cmd);
2115ea94 1754 return 0;
ae006510
DG
1755
1756err_did:
1da177e4 1757 ata_qc_free(qc);
ae006510 1758 cmd->result = (DID_ERROR << 16);
58bf201d 1759 scsi_done(cmd);
253b92ec 1760err_mem:
2115ea94 1761 return 0;
3dc1d881
TH
1762
1763defer:
31cc23b3 1764 ata_qc_free(qc);
31cc23b3
TH
1765 if (rc == ATA_DEFER_LINK)
1766 return SCSI_MLQUEUE_DEVICE_BUSY;
1767 else
1768 return SCSI_MLQUEUE_HOST_BUSY;
1da177e4
LT
1769}
1770
1da177e4
LT
1771/**
1772 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
2365278e
DLM
1773 * @dev: Target device.
1774 * @cmd: SCSI command of interest.
1da177e4
LT
1775 * @actor: Callback hook for desired SCSI command simulator
1776 *
1777 * Takes care of the hard work of simulating a SCSI command...
1778 * Mapping the response buffer, calling the command's handler,
1779 * and handling the handler's return value. This return value
1780 * indicates whether the handler wishes the SCSI command to be
ae006510
DG
1781 * completed successfully (0), or not (in which case cmd->result
1782 * and sense buffer are assumed to be set).
1da177e4
LT
1783 *
1784 * LOCKING:
cca3974e 1785 * spin_lock_irqsave(host lock)
1da177e4 1786 */
2365278e
DLM
1787static void ata_scsi_rbuf_fill(struct ata_device *dev, struct scsi_cmnd *cmd,
1788 unsigned int (*actor)(struct ata_device *dev,
1789 struct scsi_cmnd *cmd, u8 *rbuf))
1da177e4 1790{
b445c568 1791 unsigned long flags;
5251ae22 1792 unsigned int len;
b445c568 1793
56b4f06c
DLM
1794 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
1795
1796 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
5251ae22
DLM
1797 len = actor(dev, cmd, ata_scsi_rbuf);
1798 if (len) {
56b4f06c
DLM
1799 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1800 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2365278e 1801 cmd->result = SAM_STAT_GOOD;
5251ae22
DLM
1802 if (scsi_bufflen(cmd) > len)
1803 scsi_set_resid(cmd, scsi_bufflen(cmd) - len);
2365278e 1804 }
56b4f06c
DLM
1805
1806 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
1da177e4
LT
1807}
1808
1809/**
b055e3be 1810 * ata_scsiop_inq_std - Simulate standard INQUIRY command
2365278e
DLM
1811 * @dev: Target device.
1812 * @cmd: SCSI command of interest.
1da177e4 1813 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1da177e4
LT
1814 *
1815 * Returns standard device identification data associated
b142eb65 1816 * with non-VPD INQUIRY command output.
1da177e4
LT
1817 *
1818 * LOCKING:
cca3974e 1819 * spin_lock_irqsave(host lock)
1da177e4 1820 */
2365278e
DLM
1821static unsigned int ata_scsiop_inq_std(struct ata_device *dev,
1822 struct scsi_cmnd *cmd, u8 *rbuf)
1da177e4 1823{
e94f7914 1824 static const u8 versions[] = {
f9ca5ab8 1825 0x00,
87340e98
TH
1826 0x60, /* SAM-3 (no version claimed) */
1827
1828 0x03,
1829 0x20, /* SBC-2 (no version claimed) */
1830
29a37ea4
TY
1831 0x03,
1832 0x00 /* SPC-3 (no version claimed) */
87340e98 1833 };
e94f7914 1834 static const u8 versions_zbc[] = {
f9ca5ab8
HR
1835 0x00,
1836 0xA0, /* SAM-5 (no version claimed) */
1837
856c4663
HR
1838 0x06,
1839 0x00, /* SBC-4 (no version claimed) */
f9ca5ab8 1840
856c4663
HR
1841 0x05,
1842 0xC0, /* SPC-5 (no version claimed) */
f9ca5ab8
HR
1843
1844 0x60,
856c4663 1845 0x24, /* ZBC r05 */
f9ca5ab8
HR
1846 };
1847
1da177e4
LT
1848 u8 hdr[] = {
1849 TYPE_DISK,
1850 0,
1851 0x5, /* claim SPC-3 version compatibility */
1852 2,
415ffdde
TY
1853 95 - 4,
1854 0,
1855 0,
1856 2
1da177e4
LT
1857 };
1858
a6a75edc
DLM
1859 /*
1860 * Set the SCSI Removable Media Bit (RMB) if the ATA removable media
1861 * device bit (obsolete since ATA-8 ACS) is set.
8a3e33cf 1862 */
2365278e 1863 if (ata_id_removable(dev->id))
1da177e4
LT
1864 hdr[1] |= (1 << 7);
1865
2365278e 1866 if (dev->class == ATA_DEV_ZAC) {
f9ca5ab8 1867 hdr[0] = TYPE_ZBC;
56b8cbab
TY
1868 hdr[2] = 0x7; /* claim SPC-5 version compatibility */
1869 }
f9ca5ab8 1870
2365278e 1871 if (dev->flags & ATA_DFLAG_CDL)
2132df16
DLM
1872 hdr[2] = 0xd; /* claim SPC-6 version compatibility */
1873
1da177e4 1874 memcpy(rbuf, hdr, sizeof(hdr));
87340e98 1875 memcpy(&rbuf[8], "ATA ", 8);
2365278e 1876 ata_id_string(dev->id, &rbuf[16], ATA_ID_PROD, 16);
c49a6bf5
KB
1877
1878 /* From SAT, use last 2 words from fw rev unless they are spaces */
2365278e 1879 ata_id_string(dev->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
c49a6bf5 1880 if (strncmp(&rbuf[32], " ", 4) == 0)
2365278e 1881 ata_id_string(dev->id, &rbuf[32], ATA_ID_FW_REV, 4);
1da177e4 1882
87340e98
TH
1883 if (rbuf[32] == 0 || rbuf[32] == ' ')
1884 memcpy(&rbuf[32], "n/a ", 4);
1da177e4 1885
2365278e 1886 if (ata_id_zoned_cap(dev->id) || dev->class == ATA_DEV_ZAC)
f9ca5ab8
HR
1887 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
1888 else
1889 memcpy(rbuf + 58, versions, sizeof(versions));
1da177e4 1890
5251ae22
DLM
1891 /*
1892 * Include all 8 possible version descriptors, even if not all of
1893 * them are popoulated.
1894 */
1895 return 96;
1da177e4
LT
1896}
1897
1898/**
b142eb65 1899 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
2365278e
DLM
1900 * @dev: Target device.
1901 * @cmd: SCSI command of interest.
1da177e4 1902 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1da177e4 1903 *
b142eb65 1904 * Returns list of inquiry VPD pages available.
1da177e4
LT
1905 *
1906 * LOCKING:
cca3974e 1907 * spin_lock_irqsave(host lock)
1da177e4 1908 */
2365278e
DLM
1909static unsigned int ata_scsiop_inq_00(struct ata_device *dev,
1910 struct scsi_cmnd *cmd, u8 *rbuf)
1da177e4 1911{
fe22e1c2 1912 int i, num_pages = 0;
e94f7914 1913 static const u8 pages[] = {
1da177e4
LT
1914 0x00, /* page 0x00, this page */
1915 0x80, /* page 0x80, unit serial no page */
1e9dbc92
MW
1916 0x83, /* page 0x83, device ident page */
1917 0x89, /* page 0x89, ata info page */
18f0f978 1918 0xb0, /* page 0xb0, block limits page */
1e9dbc92 1919 0xb1, /* page 0xb1, block device characteristics page */
02e0a604 1920 0xb2, /* page 0xb2, thin provisioning page */
6d1003ae 1921 0xb6, /* page 0xb6, zoned block device characteristics */
fe22e1c2 1922 0xb9, /* page 0xb9, concurrent positioning ranges */
1da177e4 1923 };
1da177e4 1924
fe22e1c2
DLM
1925 for (i = 0; i < sizeof(pages); i++) {
1926 if (pages[i] == 0xb6 &&
2365278e 1927 !(dev->flags & ATA_DFLAG_ZAC))
fe22e1c2
DLM
1928 continue;
1929 rbuf[num_pages + 4] = pages[i];
1930 num_pages++;
1931 }
6d1003ae 1932 rbuf[3] = num_pages; /* number of supported VPD pages */
5251ae22
DLM
1933
1934 return get_unaligned_be16(&rbuf[2]) + 4;
1da177e4
LT
1935}
1936
1937/**
b142eb65 1938 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
2365278e
DLM
1939 * @dev: Target device.
1940 * @cmd: SCSI command of interest.
1da177e4 1941 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1da177e4
LT
1942 *
1943 * Returns ATA device serial number.
1944 *
1945 * LOCKING:
cca3974e 1946 * spin_lock_irqsave(host lock)
1da177e4 1947 */
2365278e
DLM
1948static unsigned int ata_scsiop_inq_80(struct ata_device *dev,
1949 struct scsi_cmnd *cmd, u8 *rbuf)
1da177e4 1950{
e94f7914 1951 static const u8 hdr[] = {
1da177e4
LT
1952 0,
1953 0x80, /* this page code */
1954 0,
a0cf733b 1955 ATA_ID_SERNO_LEN, /* page len */
1da177e4 1956 };
1da177e4 1957
87340e98 1958 memcpy(rbuf, hdr, sizeof(hdr));
2365278e 1959 ata_id_string(dev->id, (unsigned char *) &rbuf[4],
87340e98 1960 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
5251ae22
DLM
1961
1962 return get_unaligned_be16(&rbuf[2]) + 4;
1da177e4
LT
1963}
1964
1da177e4 1965/**
b142eb65 1966 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
2365278e
DLM
1967 * @dev: Target device.
1968 * @cmd: SCSI command of interest.
1da177e4 1969 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1da177e4 1970 *
b142eb65
JG
1971 * Yields two logical unit device identification designators:
1972 * - vendor specific ASCII containing the ATA serial number
1973 * - SAT defined "t10 vendor id based" containing ASCII vendor
1974 * name ("ATA "), model and serial numbers.
1da177e4
LT
1975 *
1976 * LOCKING:
cca3974e 1977 * spin_lock_irqsave(host lock)
1da177e4 1978 */
2365278e
DLM
1979static unsigned int ata_scsiop_inq_83(struct ata_device *dev,
1980 struct scsi_cmnd *cmd, u8 *rbuf)
1da177e4 1981{
b142eb65 1982 const int sat_model_serial_desc_len = 68;
87340e98 1983 int num;
1da177e4 1984
b142eb65
JG
1985 rbuf[1] = 0x83; /* this page code */
1986 num = 4;
1987
87340e98
TH
1988 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
1989 rbuf[num + 0] = 2;
1990 rbuf[num + 3] = ATA_ID_SERNO_LEN;
1991 num += 4;
2365278e 1992 ata_id_string(dev->id, (unsigned char *) rbuf + num,
87340e98
TH
1993 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
1994 num += ATA_ID_SERNO_LEN;
1995
1996 /* SAT defined lu model and serial numbers descriptor */
1997 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
1998 rbuf[num + 0] = 2;
1999 rbuf[num + 1] = 1;
2000 rbuf[num + 3] = sat_model_serial_desc_len;
2001 num += 4;
2002 memcpy(rbuf + num, "ATA ", 8);
2003 num += 8;
2365278e 2004 ata_id_string(dev->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
87340e98
TH
2005 ATA_ID_PROD_LEN);
2006 num += ATA_ID_PROD_LEN;
2365278e 2007 ata_id_string(dev->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
87340e98
TH
2008 ATA_ID_SERNO_LEN);
2009 num += ATA_ID_SERNO_LEN;
2010
2365278e 2011 if (ata_id_has_wwn(dev->id)) {
6b3b9d73
HR
2012 /* SAT defined lu world wide name */
2013 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2014 rbuf[num + 0] = 1;
2015 rbuf[num + 1] = 3;
2016 rbuf[num + 3] = ATA_ID_WWN_LEN;
2017 num += 4;
2365278e 2018 ata_id_string(dev->id, (unsigned char *) rbuf + num,
6b3b9d73
HR
2019 ATA_ID_WWN, ATA_ID_WWN_LEN);
2020 num += ATA_ID_WWN_LEN;
2021 }
b142eb65 2022 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
5251ae22
DLM
2023
2024 return get_unaligned_be16(&rbuf[2]) + 4;
1da177e4
LT
2025}
2026
ad355b46
JG
2027/**
2028 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2365278e
DLM
2029 * @dev: Target device.
2030 * @cmd: SCSI command of interest.
ad355b46 2031 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
ad355b46
JG
2032 *
2033 * Yields SAT-specified ATA VPD page.
2034 *
2035 * LOCKING:
2036 * spin_lock_irqsave(host lock)
2037 */
2365278e
DLM
2038static unsigned int ata_scsiop_inq_89(struct ata_device *dev,
2039 struct scsi_cmnd *cmd, u8 *rbuf)
ad355b46 2040{
87340e98
TH
2041 rbuf[1] = 0x89; /* our page code */
2042 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2043 rbuf[3] = (0x238 & 0xff);
ad355b46 2044
87340e98
TH
2045 memcpy(&rbuf[8], "linux ", 8);
2046 memcpy(&rbuf[16], "libata ", 16);
2047 memcpy(&rbuf[32], DRV_VERSION, 4);
ad355b46 2048
87340e98 2049 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
33531001
BZ
2050 rbuf[37] = (1 << 7); /* bit 7 indicates Command FIS */
2051 /* TODO: PMP? */
2052
2053 /* we don't store the ATA device signature, so we fake it */
2054 rbuf[38] = ATA_DRDY; /* really, this is Status reg */
2055 rbuf[40] = 0x1;
2056 rbuf[48] = 0x1;
ad355b46 2057
87340e98 2058 rbuf[56] = ATA_CMD_ID_ATA;
ad355b46 2059
2365278e 2060 memcpy(&rbuf[60], &dev->id[0], 512);
5251ae22
DLM
2061
2062 return get_unaligned_be16(&rbuf[2]) + 4;
ad355b46
JG
2063}
2064
47000e84
DLM
2065/**
2066 * ata_scsiop_inq_b0 - Simulate INQUIRY VPD page B0, Block Limits
2365278e
DLM
2067 * @dev: Target device.
2068 * @cmd: SCSI command of interest.
47000e84
DLM
2069 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2070 *
2071 * Return data for the VPD page B0h (Block Limits).
2072 *
2073 * LOCKING:
2074 * spin_lock_irqsave(host lock)
2075 */
2365278e
DLM
2076static unsigned int ata_scsiop_inq_b0(struct ata_device *dev,
2077 struct scsi_cmnd *cmd, u8 *rbuf)
18f0f978 2078{
295124dc 2079 u16 min_io_sectors;
18f0f978
CH
2080
2081 rbuf[1] = 0xb0;
2082 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2083
2084 /*
2085 * Optimal transfer length granularity.
2086 *
2087 * This is always one physical block, but for disks with a smaller
2088 * logical than physical sector size we need to figure out what the
2089 * latter is.
2090 */
2365278e 2091 min_io_sectors = 1 << ata_id_log2_per_physical_sector(dev->id);
18f0f978
CH
2092 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2093
2094 /*
2095 * Optimal unmap granularity.
2096 *
2097 * The ATA spec doesn't even know about a granularity or alignment
2098 * for the TRIM command. We can leave away most of the unmap related
2099 * VPD page entries, but we have specifify a granularity to signal
2100 * that we support some form of unmap - in thise case via WRITE SAME
2101 * with the unmap bit set.
2102 */
2365278e 2103 if (ata_id_has_trim(dev->id)) {
3b545563
TH
2104 u64 max_blocks = 65535 * ATA_MAX_TRIM_RNUM;
2105
7ebd8c5a 2106 if (dev->quirks & ATA_QUIRK_MAX_TRIM_128M)
3b545563
TH
2107 max_blocks = 128 << (20 - SECTOR_SHIFT);
2108
2109 put_unaligned_be64(max_blocks, &rbuf[36]);
18f0f978 2110 put_unaligned_be32(1, &rbuf[28]);
e78db4df 2111 }
18f0f978 2112
5251ae22 2113 return get_unaligned_be16(&rbuf[2]) + 4;
18f0f978
CH
2114}
2115
47000e84
DLM
2116/**
2117 * ata_scsiop_inq_b1 - Simulate INQUIRY VPD page B1, Block Device
2118 * Characteristics
2365278e
DLM
2119 * @dev: Target device.
2120 * @cmd: SCSI command of interest.
47000e84
DLM
2121 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2122 *
2123 * Return data for the VPD page B1h (Block Device Characteristics).
2124 *
2125 * LOCKING:
2126 * spin_lock_irqsave(host lock)
2127 */
2365278e
DLM
2128static unsigned int ata_scsiop_inq_b1(struct ata_device *dev,
2129 struct scsi_cmnd *cmd, u8 *rbuf)
1e9dbc92 2130{
2365278e
DLM
2131 int form_factor = ata_id_form_factor(dev->id);
2132 int media_rotation_rate = ata_id_rotation_rate(dev->id);
2133 u8 zoned = ata_id_zoned_cap(dev->id);
4bca3286 2134
1e9dbc92
MW
2135 rbuf[1] = 0xb1;
2136 rbuf[3] = 0x3c;
4bca3286
MP
2137 rbuf[4] = media_rotation_rate >> 8;
2138 rbuf[5] = media_rotation_rate;
2139 rbuf[7] = form_factor;
856c4663
HR
2140 if (zoned)
2141 rbuf[8] = (zoned << 4);
1e9dbc92 2142
5251ae22 2143 return get_unaligned_be16(&rbuf[2]) + 4;
1e9dbc92
MW
2144}
2145
47000e84
DLM
2146/**
2147 * ata_scsiop_inq_b2 - Simulate INQUIRY VPD page B2, Logical Block
2148 * Provisioning
2365278e
DLM
2149 * @dev: Target device.
2150 * @cmd: SCSI command of interest.
47000e84
DLM
2151 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2152 *
2153 * Return data for the VPD page B2h (Logical Block Provisioning).
2154 *
2155 * LOCKING:
2156 * spin_lock_irqsave(host lock)
2157 */
2365278e
DLM
2158static unsigned int ata_scsiop_inq_b2(struct ata_device *dev,
2159 struct scsi_cmnd *cmd, u8 *rbuf)
02e0a604
MP
2160{
2161 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2162 rbuf[1] = 0xb2;
2163 rbuf[3] = 0x4;
2164 rbuf[5] = 1 << 6; /* TPWS */
2165
5251ae22 2166 return get_unaligned_be16(&rbuf[2]) + 4;
02e0a604
MP
2167}
2168
47000e84
DLM
2169/**
2170 * ata_scsiop_inq_b6 - Simulate INQUIRY VPD page B6, Zoned Block Device
2171 * Characteristics
2365278e
DLM
2172 * @dev: Target device.
2173 * @cmd: SCSI command of interest.
47000e84
DLM
2174 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2175 *
2176 * Return data for the VPD page B2h (Zoned Block Device Characteristics).
2177 *
2178 * LOCKING:
2179 * spin_lock_irqsave(host lock)
2180 */
2365278e
DLM
2181static unsigned int ata_scsiop_inq_b6(struct ata_device *dev,
2182 struct scsi_cmnd *cmd, u8 *rbuf)
6d1003ae 2183{
2365278e
DLM
2184 if (!(dev->flags & ATA_DFLAG_ZAC)) {
2185 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
5251ae22 2186 return 0;
b055e3be
DLM
2187 }
2188
6d1003ae
HR
2189 /*
2190 * zbc-r05 SCSI Zoned Block device characteristics VPD page
2191 */
2192 rbuf[1] = 0xb6;
2193 rbuf[3] = 0x3C;
2194
2195 /*
2196 * URSWRZ bit is only meaningful for host-managed ZAC drives
2197 */
2365278e 2198 if (dev->zac_zoned_cap & 1)
6d1003ae 2199 rbuf[4] |= 1;
2365278e
DLM
2200 put_unaligned_be32(dev->zac_zones_optimal_open, &rbuf[8]);
2201 put_unaligned_be32(dev->zac_zones_optimal_nonseq, &rbuf[12]);
2202 put_unaligned_be32(dev->zac_zones_max_open, &rbuf[16]);
6d1003ae 2203
5251ae22 2204 return get_unaligned_be16(&rbuf[2]) + 4;
6d1003ae
HR
2205}
2206
47000e84
DLM
2207/**
2208 * ata_scsiop_inq_b9 - Simulate INQUIRY VPD page B9, Concurrent Positioning
2209 * Ranges
2365278e
DLM
2210 * @dev: Target device.
2211 * @cmd: SCSI command of interest.
47000e84
DLM
2212 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2213 *
2214 * Return data for the VPD page B9h (Concurrent Positioning Ranges).
2215 *
2216 * LOCKING:
2217 * spin_lock_irqsave(host lock)
2218 */
2365278e
DLM
2219static unsigned int ata_scsiop_inq_b9(struct ata_device *dev,
2220 struct scsi_cmnd *cmd, u8 *rbuf)
fe22e1c2 2221{
2365278e 2222 struct ata_cpr_log *cpr_log = dev->cpr_log;
fe22e1c2
DLM
2223 u8 *desc = &rbuf[64];
2224 int i;
2225
b055e3be 2226 if (!cpr_log) {
2365278e 2227 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
5251ae22 2228 return 0;
b055e3be
DLM
2229 }
2230
fe22e1c2
DLM
2231 /* SCSI Concurrent Positioning Ranges VPD page: SBC-5 rev 1 or later */
2232 rbuf[1] = 0xb9;
6d11acd4 2233 put_unaligned_be16(64 + (int)cpr_log->nr_cpr * 32 - 4, &rbuf[2]);
fe22e1c2
DLM
2234
2235 for (i = 0; i < cpr_log->nr_cpr; i++, desc += 32) {
2236 desc[0] = cpr_log->cpr[i].num;
2237 desc[1] = cpr_log->cpr[i].num_storage_elements;
2238 put_unaligned_be64(cpr_log->cpr[i].start_lba, &desc[8]);
2239 put_unaligned_be64(cpr_log->cpr[i].num_lbas, &desc[16]);
2240 }
2241
5251ae22 2242 return get_unaligned_be16(&rbuf[2]) + 4;
fe22e1c2
DLM
2243}
2244
b055e3be
DLM
2245/**
2246 * ata_scsiop_inquiry - Simulate INQUIRY command
2365278e
DLM
2247 * @dev: Target device.
2248 * @cmd: SCSI command of interest.
b055e3be
DLM
2249 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2250 *
2251 * Returns data associated with an INQUIRY command output.
2252 *
2253 * LOCKING:
2254 * spin_lock_irqsave(host lock)
2255 */
2365278e
DLM
2256static unsigned int ata_scsiop_inquiry(struct ata_device *dev,
2257 struct scsi_cmnd *cmd, u8 *rbuf)
b055e3be 2258{
b055e3be
DLM
2259 const u8 *scsicmd = cmd->cmnd;
2260
2261 /* is CmdDt set? */
2262 if (scsicmd[1] & 2) {
2263 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
5251ae22 2264 return 0;
b055e3be
DLM
2265 }
2266
2267 /* Is EVPD clear? */
2268 if ((scsicmd[1] & 1) == 0)
2365278e 2269 return ata_scsiop_inq_std(dev, cmd, rbuf);
b055e3be
DLM
2270
2271 switch (scsicmd[2]) {
2272 case 0x00:
2365278e 2273 return ata_scsiop_inq_00(dev, cmd, rbuf);
b055e3be 2274 case 0x80:
2365278e 2275 return ata_scsiop_inq_80(dev, cmd, rbuf);
b055e3be 2276 case 0x83:
2365278e 2277 return ata_scsiop_inq_83(dev, cmd, rbuf);
b055e3be 2278 case 0x89:
2365278e 2279 return ata_scsiop_inq_89(dev, cmd, rbuf);
b055e3be 2280 case 0xb0:
2365278e 2281 return ata_scsiop_inq_b0(dev, cmd, rbuf);
b055e3be 2282 case 0xb1:
2365278e 2283 return ata_scsiop_inq_b1(dev, cmd, rbuf);
b055e3be 2284 case 0xb2:
2365278e 2285 return ata_scsiop_inq_b2(dev, cmd, rbuf);
b055e3be 2286 case 0xb6:
2365278e 2287 return ata_scsiop_inq_b6(dev, cmd, rbuf);
b055e3be 2288 case 0xb9:
2365278e 2289 return ata_scsiop_inq_b9(dev, cmd, rbuf);
b055e3be
DLM
2290 default:
2291 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
5251ae22 2292 return 0;
b055e3be 2293 }
fe22e1c2
DLM
2294}
2295
6ca8e794
PB
2296/**
2297 * modecpy - Prepare response for MODE SENSE
2298 * @dest: output buffer
2299 * @src: data being copied
2300 * @n: length of mode page
2301 * @changeable: whether changeable parameters are requested
2302 *
2303 * Generate a generic MODE SENSE page for either current or changeable
2304 * parameters.
2305 *
2306 * LOCKING:
2307 * None.
2308 */
2309static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2310{
2311 if (changeable) {
2312 memcpy(dest, src, 2);
2313 memset(dest + 2, 0, n - 2);
2314 } else {
2315 memcpy(dest, src, n);
2316 }
2317}
2318
1da177e4
LT
2319/**
2320 * ata_msense_caching - Simulate MODE SENSE caching info page
2321 * @id: device IDENTIFY data
87340e98 2322 * @buf: output buffer
6ca8e794 2323 * @changeable: whether changeable parameters are requested
1da177e4
LT
2324 *
2325 * Generate a caching info page, which conditionally indicates
2326 * write caching to the SCSI layer, depending on device
2327 * capabilities.
2328 *
2329 * LOCKING:
2330 * None.
2331 */
6ca8e794 2332static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
1da177e4 2333{
6ca8e794 2334 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
737bee93
TY
2335 if (changeable) {
2336 buf[2] |= (1 << 2); /* ata_mselect_caching() */
2337 } else {
2338 buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */
2339 buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */
2340 }
87340e98 2341 return sizeof(def_cache_mpage);
1da177e4
LT
2342}
2343
673b2fe6
DLM
2344/*
2345 * Simulate MODE SENSE control mode page, sub-page 0.
2346 */
2347static unsigned int ata_msense_control_spg0(struct ata_device *dev, u8 *buf,
2348 bool changeable)
2349{
2350 modecpy(buf, def_control_mpage,
2351 sizeof(def_control_mpage), changeable);
2352 if (changeable) {
2353 /* ata_mselect_control() */
2354 buf[2] |= (1 << 2);
2355 } else {
2356 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
2357
2358 /* descriptor format sense data */
2359 buf[2] |= (d_sense << 2);
2360 }
2361
2362 return sizeof(def_control_mpage);
2363}
2364
2365/*
2366 * Translate an ATA duration limit in microseconds to a SCSI duration limit
2367 * using the t2cdlunits 0xa (10ms). Since the SCSI duration limits are 2-bytes
2368 * only, take care of overflows.
2369 */
2370static inline u16 ata_xlat_cdl_limit(u8 *buf)
2371{
2372 u32 limit = get_unaligned_le32(buf);
2373
2374 return min_t(u32, limit / 10000, 65535);
2375}
2376
2377/*
2378 * Simulate MODE SENSE control mode page, sub-pages 07h and 08h
2379 * (command duration limits T2A and T2B mode pages).
2380 */
2381static unsigned int ata_msense_control_spgt2(struct ata_device *dev, u8 *buf,
2382 u8 spg)
2383{
03a9cfc1 2384 u8 *b, *cdl, *desc;
673b2fe6
DLM
2385 u32 policy;
2386 int i;
2387
03a9cfc1
DLM
2388 if (!(dev->flags & ATA_DFLAG_CDL) || !dev->cdl)
2389 return 0;
2390
2391 cdl = dev->cdl->desc_log_buf;
2392
673b2fe6
DLM
2393 /*
2394 * Fill the subpage. The first four bytes of the T2A/T2B mode pages
2395 * are a header. The PAGE LENGTH field is the size of the page
2396 * excluding the header.
2397 */
2398 buf[0] = CONTROL_MPAGE;
2399 buf[1] = spg;
2400 put_unaligned_be16(CDL_T2_SUB_MPAGE_LEN - 4, &buf[2]);
2401 if (spg == CDL_T2A_SUB_MPAGE) {
2402 /*
2403 * Read descriptors map to the T2A page:
2404 * set perf_vs_duration_guidleine.
2405 */
2406 buf[7] = (cdl[0] & 0x03) << 4;
2407 desc = cdl + 64;
2408 } else {
2409 /* Write descriptors map to the T2B page */
2410 desc = cdl + 288;
2411 }
2412
2413 /* Fill the T2 page descriptors */
2414 b = &buf[8];
2415 policy = get_unaligned_le32(&cdl[0]);
2416 for (i = 0; i < 7; i++, b += 32, desc += 32) {
2417 /* t2cdlunits: fixed to 10ms */
2418 b[0] = 0x0a;
2419
2420 /* Max inactive time and its policy */
2421 put_unaligned_be16(ata_xlat_cdl_limit(&desc[8]), &b[2]);
2422 b[6] = ((policy >> 8) & 0x0f) << 4;
2423
2424 /* Max active time and its policy */
2425 put_unaligned_be16(ata_xlat_cdl_limit(&desc[4]), &b[4]);
2426 b[6] |= (policy >> 4) & 0x0f;
2427
2428 /* Command duration guideline and its policy */
2429 put_unaligned_be16(ata_xlat_cdl_limit(&desc[16]), &b[10]);
2430 b[14] = policy & 0x0f;
2431 }
2432
2433 return CDL_T2_SUB_MPAGE_LEN;
2434}
2435
df60f9c6
DLM
2436/*
2437 * Simulate MODE SENSE control mode page, sub-page f2h
2438 * (ATA feature control mode page).
2439 */
2440static unsigned int ata_msense_control_ata_feature(struct ata_device *dev,
2441 u8 *buf)
2442{
2443 /* PS=0, SPF=1 */
2444 buf[0] = CONTROL_MPAGE | (1 << 6);
2445 buf[1] = ATA_FEATURE_SUB_MPAGE;
2446
2447 /*
2448 * The first four bytes of ATA Feature Control mode page are a header.
2449 * The PAGE LENGTH field is the size of the page excluding the header.
2450 */
2451 put_unaligned_be16(ATA_FEATURE_SUB_MPAGE_LEN - 4, &buf[2]);
2452
88474ad7
DLM
2453 if (dev->flags & ATA_DFLAG_CDL_ENABLED)
2454 buf[4] = 0x02; /* T2A and T2B pages enabled */
df60f9c6
DLM
2455 else
2456 buf[4] = 0;
2457
2458 return ATA_FEATURE_SUB_MPAGE_LEN;
2459}
2460
1da177e4 2461/**
f086b748 2462 * ata_msense_control - Simulate MODE SENSE control mode page
06dbde5f 2463 * @dev: ATA device of interest
87340e98 2464 * @buf: output buffer
673b2fe6 2465 * @spg: sub-page code
6ca8e794 2466 * @changeable: whether changeable parameters are requested
1da177e4
LT
2467 *
2468 * Generate a generic MODE SENSE control mode page.
2469 *
2470 * LOCKING:
2471 * None.
2472 */
f086b748 2473static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
673b2fe6
DLM
2474 u8 spg, bool changeable)
2475{
2476 unsigned int n;
2477
2478 switch (spg) {
2479 case 0:
2480 return ata_msense_control_spg0(dev, buf, changeable);
2481 case CDL_T2A_SUB_MPAGE:
2482 case CDL_T2B_SUB_MPAGE:
2483 return ata_msense_control_spgt2(dev, buf, spg);
df60f9c6
DLM
2484 case ATA_FEATURE_SUB_MPAGE:
2485 return ata_msense_control_ata_feature(dev, buf);
673b2fe6
DLM
2486 case ALL_SUB_MPAGES:
2487 n = ata_msense_control_spg0(dev, buf, changeable);
2488 n += ata_msense_control_spgt2(dev, buf + n, CDL_T2A_SUB_MPAGE);
0e9a2990 2489 n += ata_msense_control_spgt2(dev, buf + n, CDL_T2B_SUB_MPAGE);
df60f9c6 2490 n += ata_msense_control_ata_feature(dev, buf + n);
673b2fe6
DLM
2491 return n;
2492 default:
2493 return 0;
737bee93 2494 }
1da177e4
LT
2495}
2496
2497/**
2498 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
ec2a20e6 2499 * @buf: output buffer
6ca8e794 2500 * @changeable: whether changeable parameters are requested
1da177e4
LT
2501 *
2502 * Generate a generic MODE SENSE r/w error recovery page.
2503 *
2504 * LOCKING:
2505 * None.
2506 */
6ca8e794 2507static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
1da177e4 2508{
6ca8e794
PB
2509 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2510 changeable);
00ac37f5 2511 return sizeof(def_rw_recovery_mpage);
1da177e4
LT
2512}
2513
2514/**
2515 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2365278e
DLM
2516 * @dev: Target device.
2517 * @cmd: SCSI command of interest.
1da177e4 2518 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1da177e4 2519 *
00ac37f5
DG
2520 * Simulate MODE SENSE commands. Assume this is invoked for direct
2521 * access devices (e.g. disks) only. There should be no block
2522 * descriptor for other device types.
1da177e4
LT
2523 *
2524 * LOCKING:
cca3974e 2525 * spin_lock_irqsave(host lock)
1da177e4 2526 */
2365278e
DLM
2527static unsigned int ata_scsiop_mode_sense(struct ata_device *dev,
2528 struct scsi_cmnd *cmd, u8 *rbuf)
1da177e4 2529{
2365278e 2530 u8 *scsicmd = cmd->cmnd, *p = rbuf;
e94f7914 2531 static const u8 sat_blk_desc[] = {
00ac37f5
DG
2532 0, 0, 0, 0, /* number of blocks: sat unspecified */
2533 0,
2534 0, 0x2, 0x0 /* block length: 512 bytes */
2535 };
2536 u8 pg, spg;
87340e98 2537 unsigned int ebd, page_control, six_byte;
4d2e4980 2538 u8 dpofua = 0, bp = 0xff;
bcfc867d 2539 u16 fp;
1da177e4 2540
1da177e4 2541 six_byte = (scsicmd[0] == MODE_SENSE);
00ac37f5
DG
2542 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2543 /*
2544 * LLBA bit in msense(10) ignored (compliant)
1da177e4 2545 */
00ac37f5 2546
1da177e4 2547 page_control = scsicmd[2] >> 6;
ae006510
DG
2548 switch (page_control) {
2549 case 0: /* current */
6ca8e794
PB
2550 case 1: /* changeable */
2551 case 2: /* defaults */
ae006510
DG
2552 break; /* supported */
2553 case 3: /* saved */
2554 goto saving_not_supp;
ae006510 2555 default:
bcfc867d 2556 fp = 2;
0df10b84 2557 bp = 6;
ae006510
DG
2558 goto invalid_fld;
2559 }
1da177e4 2560
87340e98
TH
2561 if (six_byte)
2562 p += 4 + (ebd ? 8 : 0);
2563 else
2564 p += 8 + (ebd ? 8 : 0);
1da177e4 2565
00ac37f5
DG
2566 pg = scsicmd[2] & 0x3f;
2567 spg = scsicmd[3];
673b2fe6 2568
00ac37f5 2569 /*
df60f9c6 2570 * Supported subpages: all subpages and sub-pages 07h, 08h and f2h of
673b2fe6 2571 * the control page.
00ac37f5 2572 */
673b2fe6
DLM
2573 if (spg) {
2574 switch (spg) {
2575 case ALL_SUB_MPAGES:
2576 break;
2577 case CDL_T2A_SUB_MPAGE:
2578 case CDL_T2B_SUB_MPAGE:
df60f9c6 2579 case ATA_FEATURE_SUB_MPAGE:
673b2fe6
DLM
2580 if (dev->flags & ATA_DFLAG_CDL && pg == CONTROL_MPAGE)
2581 break;
2582 fallthrough;
2583 default:
2584 fp = 3;
2585 goto invalid_fld;
2586 }
bcfc867d 2587 }
00ac37f5
DG
2588
2589 switch(pg) {
2590 case RW_RECOVERY_MPAGE:
6ca8e794 2591 p += ata_msense_rw_recovery(p, page_control == 1);
1da177e4
LT
2592 break;
2593
00ac37f5 2594 case CACHE_MPAGE:
2365278e 2595 p += ata_msense_caching(dev->id, p, page_control == 1);
1da177e4
LT
2596 break;
2597
87340e98 2598 case CONTROL_MPAGE:
2365278e 2599 p += ata_msense_control(dev, p, spg, page_control == 1);
1da177e4 2600 break;
1da177e4 2601
00ac37f5 2602 case ALL_MPAGES:
6ca8e794 2603 p += ata_msense_rw_recovery(p, page_control == 1);
2365278e
DLM
2604 p += ata_msense_caching(dev->id, p, page_control == 1);
2605 p += ata_msense_control(dev, p, spg, page_control == 1);
1da177e4
LT
2606 break;
2607
2608 default: /* invalid page code */
bcfc867d 2609 fp = 2;
ae006510 2610 goto invalid_fld;
1da177e4
LT
2611 }
2612
4d2e4980 2613 if (dev->flags & ATA_DFLAG_FUA)
9a3dccc4
TH
2614 dpofua = 1 << 4;
2615
1da177e4 2616 if (six_byte) {
87340e98
TH
2617 rbuf[0] = p - rbuf - 1;
2618 rbuf[2] |= dpofua;
00ac37f5 2619 if (ebd) {
87340e98
TH
2620 rbuf[3] = sizeof(sat_blk_desc);
2621 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
00ac37f5 2622 }
5251ae22
DLM
2623
2624 return rbuf[0] + 1;
1da177e4 2625 }
5251ae22
DLM
2626
2627 put_unaligned_be16(p - rbuf - 2, &rbuf[0]);
2628 rbuf[3] |= dpofua;
2629 if (ebd) {
2630 rbuf[7] = sizeof(sat_blk_desc);
2631 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
2632 }
2633
2634 return get_unaligned_be16(&rbuf[0]) + 2;
ae006510
DG
2635
2636invalid_fld:
2365278e 2637 ata_scsi_set_invalid_field(dev, cmd, fp, bp);
5251ae22 2638 return 0;
ae006510
DG
2639
2640saving_not_supp:
2365278e 2641 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x39, 0x0);
ae006510 2642 /* "Saving parameters not supported" */
5251ae22 2643 return 0;
1da177e4
LT
2644}
2645
2646/**
2647 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2365278e
DLM
2648 * @dev: Target device.
2649 * @cmd: SCSI command of interest.
1da177e4 2650 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1da177e4
LT
2651 *
2652 * Simulate READ CAPACITY commands.
2653 *
2654 * LOCKING:
6a36261e 2655 * None.
1da177e4 2656 */
2365278e
DLM
2657static unsigned int ata_scsiop_read_cap(struct ata_device *dev,
2658 struct scsi_cmnd *cmd, u8 *rbuf)
1da177e4 2659{
2365278e 2660 u8 *scsicmd = cmd->cmnd;
61d79a8e 2661 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
295124dc
GG
2662 u32 sector_size; /* physical sector size in bytes */
2663 u8 log2_per_phys;
2664 u16 lowest_aligned;
2665
2666 sector_size = ata_id_logical_sector_size(dev->id);
2667 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2668 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
1da177e4 2669
44bdde15 2670 if (scsicmd[0] == READ_CAPACITY) {
6a36261e
TH
2671 if (last_lba >= 0xffffffffULL)
2672 last_lba = 0xffffffff;
0c144d0d 2673
1da177e4 2674 /* sector count, 32-bit */
87340e98
TH
2675 rbuf[0] = last_lba >> (8 * 3);
2676 rbuf[1] = last_lba >> (8 * 2);
2677 rbuf[2] = last_lba >> (8 * 1);
2678 rbuf[3] = last_lba;
1da177e4
LT
2679
2680 /* sector size */
295124dc
GG
2681 rbuf[4] = sector_size >> (8 * 3);
2682 rbuf[5] = sector_size >> (8 * 2);
2683 rbuf[6] = sector_size >> (8 * 1);
2684 rbuf[7] = sector_size;
1da177e4 2685
5251ae22 2686 return 8;
44bdde15
DLM
2687 }
2688
2689 /*
2690 * READ CAPACITY 16 command is defined as a service action
2691 * (SERVICE_ACTION_IN_16 command).
2692 */
2693 if (scsicmd[0] != SERVICE_ACTION_IN_16 ||
2694 (scsicmd[1] & 0x1f) != SAI_READ_CAPACITY_16) {
2365278e 2695 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
5251ae22 2696 return 0;
44bdde15
DLM
2697 }
2698
2699 /* sector count, 64-bit */
2700 rbuf[0] = last_lba >> (8 * 7);
2701 rbuf[1] = last_lba >> (8 * 6);
2702 rbuf[2] = last_lba >> (8 * 5);
2703 rbuf[3] = last_lba >> (8 * 4);
2704 rbuf[4] = last_lba >> (8 * 3);
2705 rbuf[5] = last_lba >> (8 * 2);
2706 rbuf[6] = last_lba >> (8 * 1);
2707 rbuf[7] = last_lba;
2708
2709 /* sector size */
2710 rbuf[ 8] = sector_size >> (8 * 3);
2711 rbuf[ 9] = sector_size >> (8 * 2);
2712 rbuf[10] = sector_size >> (8 * 1);
2713 rbuf[11] = sector_size;
2714
2365278e 2715 if (ata_id_zoned_cap(dev->id) || dev->class == ATA_DEV_ZAC)
44bdde15
DLM
2716 rbuf[12] = (1 << 4); /* RC_BASIS */
2717 rbuf[13] = log2_per_phys;
2718 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2719 rbuf[15] = lowest_aligned;
2720
2365278e 2721 if (ata_id_has_trim(dev->id) && !(dev->quirks & ATA_QUIRK_NOTRIM)) {
44bdde15
DLM
2722 rbuf[14] |= 0x80; /* LBPME */
2723
2365278e 2724 if (ata_id_has_zero_after_trim(dev->id) &&
44bdde15
DLM
2725 dev->quirks & ATA_QUIRK_ZERO_AFTER_TRIM) {
2726 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2727 rbuf[14] |= 0x40; /* LBPRZ */
e78db4df 2728 }
1da177e4 2729 }
44bdde15 2730
5251ae22 2731 return 16;
1da177e4
LT
2732}
2733
2734/**
2735 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2365278e
DLM
2736 * @dev: Target device.
2737 * @cmd: SCSI command of interest.
1da177e4 2738 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1da177e4
LT
2739 *
2740 * Simulate REPORT LUNS command.
2741 *
2742 * LOCKING:
cca3974e 2743 * spin_lock_irqsave(host lock)
1da177e4 2744 */
2365278e
DLM
2745static unsigned int ata_scsiop_report_luns(struct ata_device *dev,
2746 struct scsi_cmnd *cmd, u8 *rbuf)
1da177e4 2747{
1da177e4
LT
2748 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2749
5251ae22 2750 return 16;
1da177e4
LT
2751}
2752
aa18da8b
CH
2753/*
2754 * ATAPI devices typically report zero for their SCSI version, and sometimes
2755 * deviate from the spec WRT response data format. If SCSI version is
2756 * reported as zero like normal, then we make the following fixups:
2757 * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a
2758 * modern device.
2759 * 2) Ensure response data format / ATAPI information are always correct.
2760 */
2761static void atapi_fixup_inquiry(struct scsi_cmnd *cmd)
2762{
2763 u8 buf[4];
2764
2765 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2766 if (buf[2] == 0) {
2767 buf[2] = 0x5;
2768 buf[3] = 0x32;
2769 }
2770 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2771}
2772
77853bf2 2773static void atapi_qc_complete(struct ata_queued_cmd *qc)
1da177e4
LT
2774{
2775 struct scsi_cmnd *cmd = qc->scsicmd;
a22e2eb0 2776 unsigned int err_mask = qc->err_mask;
1da177e4 2777
ff8072d5
HR
2778 /* handle completion from EH */
2779 if (unlikely(err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID)) {
246619da 2780
97981926 2781 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID))
750426aa 2782 ata_gen_passthru_sense(qc);
246619da 2783
22aac089
TH
2784 /* SCSI EH automatically locks door if sdev->locked is
2785 * set. Sometimes door lock request continues to
2786 * fail, for example, when no media is present. This
2787 * creates a loop - SCSI EH issues door lock which
2788 * fails and gets invoked again to acquire sense data
2789 * for the failed command.
2790 *
2791 * If door lock fails, always clear sdev->locked to
2792 * avoid this infinite loop.
2a5f07b5
TH
2793 *
2794 * This may happen before SCSI scan is complete. Make
2795 * sure qc->dev->sdev isn't NULL before dereferencing.
22aac089 2796 */
2a5f07b5 2797 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
22aac089
TH
2798 qc->dev->sdev->locked = 0;
2799
246619da 2800 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2aa8f5d6 2801 ata_qc_done(qc);
246619da
TH
2802 return;
2803 }
2804
ff8072d5
HR
2805 /* successful completion path */
2806 if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0)
2807 atapi_fixup_inquiry(cmd);
2808 cmd->result = SAM_STAT_GOOD;
1da177e4 2809
2aa8f5d6 2810 ata_qc_done(qc);
1da177e4
LT
2811}
2812/**
2813 * atapi_xlat - Initialize PACKET taskfile
2814 * @qc: command structure to be initialized
1da177e4
LT
2815 *
2816 * LOCKING:
cca3974e 2817 * spin_lock_irqsave(host lock)
1da177e4
LT
2818 *
2819 * RETURNS:
2820 * Zero on success, non-zero on failure.
2821 */
ad706991 2822static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
1da177e4 2823{
542b1444 2824 struct scsi_cmnd *scmd = qc->scsicmd;
1da177e4 2825 struct ata_device *dev = qc->dev;
542b1444 2826 int nodata = (scmd->sc_data_direction == DMA_NONE);
5895ef9a 2827 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
2db78dd3 2828 unsigned int nbytes;
1da177e4 2829
2e5704f6
TH
2830 memset(qc->cdb, 0, dev->cdb_len);
2831 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
1da177e4
LT
2832
2833 qc->complete_fn = atapi_qc_complete;
2834
2835 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
542b1444 2836 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1da177e4 2837 qc->tf.flags |= ATA_TFLAG_WRITE;
1da177e4
LT
2838 }
2839
2840 qc->tf.command = ATA_CMD_PACKET;
aacda375 2841 ata_qc_set_pc_nbytes(qc);
e00f1ff3
TH
2842
2843 /* check whether ATAPI DMA is safe */
5895ef9a 2844 if (!nodata && !using_pio && atapi_check_dma(qc))
e00f1ff3 2845 using_pio = 1;
1da177e4 2846
e190222d
TH
2847 /* Some controller variants snoop this value for Packet
2848 * transfers to do state machine and FIFO management. Thus we
2849 * want to set it properly, and for DMA where it is
2850 * effectively meaningless.
2851 */
aacda375 2852 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
2db78dd3 2853
e190222d
TH
2854 /* Most ATAPI devices which honor transfer chunk size don't
2855 * behave according to the spec when odd chunk size which
2856 * matches the transfer length is specified. If the number of
2857 * bytes to transfer is 2n+1. According to the spec, what
2858 * should happen is to indicate that 2n+1 is going to be
2859 * transferred and transfer 2n+2 bytes where the last byte is
2860 * padding.
2861 *
2862 * In practice, this doesn't happen. ATAPI devices first
2863 * indicate and transfer 2n bytes and then indicate and
2864 * transfer 2 bytes where the last byte is padding.
2865 *
2866 * This inconsistency confuses several controllers which
2867 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2868 * These controllers use actual number of transferred bytes to
8d1e02d1 2869 * update DMA pointer and transfer of 4n+2 bytes make those
e190222d
TH
2870 * controller push DMA pointer by 4n+4 bytes because SATA data
2871 * FISes are aligned to 4 bytes. This causes data corruption
2872 * and buffer overrun.
2873 *
2874 * Always setting nbytes to even number solves this problem
2875 * because then ATAPI devices don't have to split data at 2n
2876 * boundaries.
2877 */
2878 if (nbytes & 0x1)
2879 nbytes++;
2880
2db78dd3
AC
2881 qc->tf.lbam = (nbytes & 0xFF);
2882 qc->tf.lbah = (nbytes >> 8);
2883
5895ef9a
TH
2884 if (nodata)
2885 qc->tf.protocol = ATAPI_PROT_NODATA;
2886 else if (using_pio)
2887 qc->tf.protocol = ATAPI_PROT_PIO;
2888 else {
e00f1ff3 2889 /* DMA data xfer */
0dc36888 2890 qc->tf.protocol = ATAPI_PROT_DMA;
1da177e4
LT
2891 qc->tf.feature |= ATAPI_PKT_DMA;
2892
91163006
TH
2893 if ((dev->flags & ATA_DFLAG_DMADIR) &&
2894 (scmd->sc_data_direction != DMA_TO_DEVICE))
95de719a 2895 /* some SATA bridges need us to indicate data xfer direction */
1da177e4 2896 qc->tf.feature |= ATAPI_DMADIR;
1da177e4
LT
2897 }
2898
2db78dd3
AC
2899
2900 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2901 as ATAPI tape drives don't get this right otherwise */
1da177e4
LT
2902 return 0;
2903}
2904
7f875850 2905static struct ata_device *ata_find_dev(struct ata_port *ap, unsigned int devno)
ab5b3a5b 2906{
7f875850
DLM
2907 /*
2908 * For the non-PMP case, ata_link_max_devices() returns 1 (SATA case),
2909 * or 2 (IDE master + slave case). However, the former case includes
2910 * libsas hosted devices which are numbered per scsi host, leading
2911 * to devno potentially being larger than 0 but with each struct
2912 * ata_device having its own struct ata_port and struct ata_link.
2913 * To accommodate these, ignore devno and always use device number 0.
2914 */
2915 if (likely(!sata_pmp_attached(ap))) {
2916 int link_max_devices = ata_link_max_devices(&ap->link);
2917
2918 if (link_max_devices == 1)
2919 return &ap->link.device[0];
2920
2921 if (devno < link_max_devices)
41bda9c9 2922 return &ap->link.device[devno];
7f875850
DLM
2923
2924 return NULL;
41bda9c9
TH
2925 }
2926
7f875850
DLM
2927 /*
2928 * For PMP-attached devices, the device number corresponds to C
2929 * (channel) of SCSI [H:C:I:L], indicating the port pmp link
2930 * for the device.
2931 */
2932 if (devno < ap->nr_pmp_links)
2933 return &ap->pmp_link[devno].device[0];
2934
ab5b3a5b
TH
2935 return NULL;
2936}
2937
2dcb407e
JG
2938static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
2939 const struct scsi_device *scsidev)
ab5b3a5b 2940{
41bda9c9
TH
2941 int devno;
2942
ab5b3a5b 2943 /* skip commands not addressed to targets we simulate */
071f44b1 2944 if (!sata_pmp_attached(ap)) {
41bda9c9
TH
2945 if (unlikely(scsidev->channel || scsidev->lun))
2946 return NULL;
2947 devno = scsidev->id;
2948 } else {
2949 if (unlikely(scsidev->id || scsidev->lun))
2950 return NULL;
2951 devno = scsidev->channel;
2952 }
ab5b3a5b 2953
41bda9c9 2954 return ata_find_dev(ap, devno);
ab5b3a5b
TH
2955}
2956
1da177e4
LT
2957/**
2958 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2959 * @ap: ATA port to which the device is attached
2960 * @scsidev: SCSI device from which we derive the ATA device
2961 *
2962 * Given various information provided in struct scsi_cmnd,
2963 * map that onto an ATA bus, and using that mapping
2964 * determine which ata_device is associated with the
2965 * SCSI command to be sent.
2966 *
2967 * LOCKING:
cca3974e 2968 * spin_lock_irqsave(host lock)
1da177e4
LT
2969 *
2970 * RETURNS:
2971 * Associated ATA device, or %NULL if not found.
2972 */
ec811a94 2973struct ata_device *
057ace5e 2974ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
1da177e4 2975{
ab5b3a5b 2976 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
1da177e4 2977
2486fa56 2978 if (unlikely(!dev || !ata_dev_enabled(dev)))
1da177e4
LT
2979 return NULL;
2980
1da177e4
LT
2981 return dev;
2982}
2983
b095518e
JG
2984/*
2985 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2986 * @byte1: Byte 1 from pass-thru CDB.
2987 *
2988 * RETURNS:
2989 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2990 */
2991static u8
2992ata_scsi_map_proto(u8 byte1)
2993{
2994 switch((byte1 & 0x1e) >> 1) {
2dcb407e
JG
2995 case 3: /* Non-data */
2996 return ATA_PROT_NODATA;
2997
2998 case 6: /* DMA */
2999 case 10: /* UDMA Data-in */
3000 case 11: /* UDMA Data-Out */
3001 return ATA_PROT_DMA;
3002
3003 case 4: /* PIO Data-in */
3004 case 5: /* PIO Data-out */
3005 return ATA_PROT_PIO;
3006
ee7fb331
VK
3007 case 12: /* FPDMA */
3008 return ATA_PROT_NCQ;
3009
2dcb407e
JG
3010 case 0: /* Hard Reset */
3011 case 1: /* SRST */
3012 case 8: /* Device Diagnostic */
3013 case 9: /* Device Reset */
3014 case 7: /* DMA Queued */
2dcb407e
JG
3015 case 15: /* Return Response Info */
3016 default: /* Reserved */
3017 break;
b095518e
JG
3018 }
3019
3020 return ATA_PROT_UNKNOWN;
3021}
3022
3023/**
3024 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
3025 * @qc: command structure to be initialized
b095518e 3026 *
b1ffbf85 3027 * Handles either 12, 16, or 32-byte versions of the CDB.
b095518e
JG
3028 *
3029 * RETURNS:
3030 * Zero on success, non-zero on failure.
3031 */
ad706991 3032static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
b095518e
JG
3033{
3034 struct ata_taskfile *tf = &(qc->tf);
542b1444 3035 struct scsi_cmnd *scmd = qc->scsicmd;
f79d409f 3036 struct ata_device *dev = qc->dev;
ad706991 3037 const u8 *cdb = scmd->cmnd;
bcfc867d 3038 u16 fp;
b1ffbf85 3039 u16 cdb_offset = 0;
b095518e 3040
b1ffbf85
MI
3041 /* 7Fh variable length cmd means a ata pass-thru(32) */
3042 if (cdb[0] == VARIABLE_LENGTH_CMD)
3043 cdb_offset = 9;
3044
3045 tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]);
3046 if (tf->protocol == ATA_PROT_UNKNOWN) {
bcfc867d 3047 fp = 1;
9a405257 3048 goto invalid_fld;
bcfc867d 3049 }
8190bdb9 3050
5da5231b
GK
3051 if ((cdb[2 + cdb_offset] & 0x3) == 0) {
3052 /*
3053 * When T_LENGTH is zero (No data is transferred), dir should
3054 * be DMA_NONE.
3055 */
3056 if (scmd->sc_data_direction != DMA_NONE) {
3057 fp = 2 + cdb_offset;
3058 goto invalid_fld;
3059 }
3060
3061 if (ata_is_ncq(tf->protocol))
3062 tf->protocol = ATA_PROT_NCQ_NODATA;
3063 }
5b844b63 3064
5f8e7f17
VK
3065 /* enable LBA */
3066 tf->flags |= ATA_TFLAG_LBA;
3067
b095518e
JG
3068 /*
3069 * 12 and 16 byte CDBs use different offsets to
3070 * provide the various register values.
3071 */
59b00404
SS
3072 switch (cdb[0]) {
3073 case ATA_16:
b095518e
JG
3074 /*
3075 * 16-byte CDB - may contain extended commands.
3076 *
3077 * If that is the case, copy the upper byte register values.
3078 */
542b1444
TH
3079 if (cdb[1] & 0x01) {
3080 tf->hob_feature = cdb[3];
3081 tf->hob_nsect = cdb[5];
3082 tf->hob_lbal = cdb[7];
3083 tf->hob_lbam = cdb[9];
3084 tf->hob_lbah = cdb[11];
b095518e
JG
3085 tf->flags |= ATA_TFLAG_LBA48;
3086 } else
3087 tf->flags &= ~ATA_TFLAG_LBA48;
3088
3089 /*
3090 * Always copy low byte, device and command registers.
3091 */
542b1444
TH
3092 tf->feature = cdb[4];
3093 tf->nsect = cdb[6];
3094 tf->lbal = cdb[8];
3095 tf->lbam = cdb[10];
3096 tf->lbah = cdb[12];
3097 tf->device = cdb[13];
3098 tf->command = cdb[14];
59b00404
SS
3099 break;
3100 case ATA_12:
b095518e
JG
3101 /*
3102 * 12-byte CDB - incapable of extended commands.
3103 */
3104 tf->flags &= ~ATA_TFLAG_LBA48;
3105
542b1444
TH
3106 tf->feature = cdb[3];
3107 tf->nsect = cdb[4];
3108 tf->lbal = cdb[5];
3109 tf->lbam = cdb[6];
3110 tf->lbah = cdb[7];
3111 tf->device = cdb[8];
3112 tf->command = cdb[9];
59b00404
SS
3113 break;
3114 default:
b1ffbf85
MI
3115 /*
3116 * 32-byte CDB - may contain extended command fields.
3117 *
3118 * If that is the case, copy the upper byte register values.
3119 */
3120 if (cdb[10] & 0x01) {
3121 tf->hob_feature = cdb[20];
3122 tf->hob_nsect = cdb[22];
3123 tf->hob_lbal = cdb[16];
3124 tf->hob_lbam = cdb[15];
3125 tf->hob_lbah = cdb[14];
3126 tf->flags |= ATA_TFLAG_LBA48;
3127 } else
3128 tf->flags &= ~ATA_TFLAG_LBA48;
3129
3130 tf->feature = cdb[21];
3131 tf->nsect = cdb[23];
3132 tf->lbal = cdb[19];
3133 tf->lbam = cdb[18];
3134 tf->lbah = cdb[17];
3135 tf->device = cdb[24];
3136 tf->command = cdb[25];
3137 tf->auxiliary = get_unaligned_be32(&cdb[28]);
59b00404 3138 break;
b095518e 3139 }
fa4453c4 3140
179b310a
HR
3141 /* For NCQ commands copy the tag value */
3142 if (ata_is_ncq(tf->protocol))
4e5b6260 3143 tf->nsect = qc->hw_tag << 3;
ee7fb331 3144
fa4453c4
AL
3145 /* enforce correct master/slave bit */
3146 tf->device = dev->devno ?
3147 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
b095518e 3148
bd30add8 3149 switch (tf->command) {
295124dc 3150 /* READ/WRITE LONG use a non-standard sect_size */
bd30add8
TH
3151 case ATA_CMD_READ_LONG:
3152 case ATA_CMD_READ_LONG_ONCE:
3153 case ATA_CMD_WRITE_LONG:
3154 case ATA_CMD_WRITE_LONG_ONCE:
bcfc867d
HR
3155 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
3156 fp = 1;
bd30add8 3157 goto invalid_fld;
bcfc867d 3158 }
bd30add8 3159 qc->sect_size = scsi_bufflen(scmd);
295124dc
GG
3160 break;
3161
3162 /* commands using reported Logical Block size (e.g. 512 or 4K) */
3163 case ATA_CMD_CFA_WRITE_NE:
3164 case ATA_CMD_CFA_TRANS_SECT:
3165 case ATA_CMD_CFA_WRITE_MULT_NE:
3166 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
3167 case ATA_CMD_READ:
3168 case ATA_CMD_READ_EXT:
3169 case ATA_CMD_READ_QUEUED:
3170 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
3171 case ATA_CMD_FPDMA_READ:
3172 case ATA_CMD_READ_MULTI:
3173 case ATA_CMD_READ_MULTI_EXT:
3174 case ATA_CMD_PIO_READ:
3175 case ATA_CMD_PIO_READ_EXT:
3176 case ATA_CMD_READ_STREAM_DMA_EXT:
3177 case ATA_CMD_READ_STREAM_EXT:
3178 case ATA_CMD_VERIFY:
3179 case ATA_CMD_VERIFY_EXT:
3180 case ATA_CMD_WRITE:
3181 case ATA_CMD_WRITE_EXT:
3182 case ATA_CMD_WRITE_FUA_EXT:
3183 case ATA_CMD_WRITE_QUEUED:
3184 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3185 case ATA_CMD_FPDMA_WRITE:
3186 case ATA_CMD_WRITE_MULTI:
3187 case ATA_CMD_WRITE_MULTI_EXT:
3188 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3189 case ATA_CMD_PIO_WRITE:
3190 case ATA_CMD_PIO_WRITE_EXT:
3191 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3192 case ATA_CMD_WRITE_STREAM_EXT:
3193 qc->sect_size = scmd->device->sector_size;
3194 break;
3195
3196 /* Everything else uses 512 byte "sectors" */
3197 default:
3198 qc->sect_size = ATA_SECT_SIZE;
bd30add8
TH
3199 }
3200
3201 /*
3202 * Set flags so that all registers will be written, pass on
3203 * write indication (used for PIO/DMA setup), result TF is
3204 * copied back and we don't whine too much about its failure.
3205 */
bc496ed0 3206 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
bd30add8
TH
3207 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3208 tf->flags |= ATA_TFLAG_WRITE;
3209
3210 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3211
3212 /*
3213 * Set transfer length.
3214 *
3215 * TODO: find out if we need to do more here to
3216 * cover scatter/gather case.
3217 */
3218 ata_qc_set_pc_nbytes(qc);
3219
3220 /* We may not issue DMA commands if no DMA mode is set */
6ac586f2 3221 if (tf->protocol == ATA_PROT_DMA && !ata_dma_enabled(dev)) {
bcfc867d 3222 fp = 1;
bd30add8 3223 goto invalid_fld;
2c1ec6fd
EB
3224 }
3225
3226 /* We may not issue NCQ commands to devices not supporting NCQ */
3227 if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) {
3228 fp = 1;
3229 goto invalid_fld;
bcfc867d 3230 }
bd30add8 3231
1dce589c 3232 /* sanity check for pio multi commands */
bcfc867d
HR
3233 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
3234 fp = 1;
1dce589c 3235 goto invalid_fld;
bcfc867d 3236 }
1dce589c
AL
3237
3238 if (is_multi_taskfile(tf)) {
3239 unsigned int multi_count = 1 << (cdb[1] >> 5);
3240
3241 /* compare the passed through multi_count
3242 * with the cached multi_count of libata
3243 */
3244 if (multi_count != dev->multi_count)
a9a79dfe
JP
3245 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3246 multi_count);
d26fc955 3247 }
1dce589c 3248
b095518e
JG
3249 /*
3250 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3251 * SET_FEATURES - XFER MODE must be preceded/succeeded
3252 * by an update to hardware-specific registers for each
3253 * controller (i.e. the reason for ->set_piomode(),
3254 * ->set_dmamode(), and ->post_set_mode() hooks).
3255 */
bd30add8 3256 if (tf->command == ATA_CMD_SET_FEATURES &&
bcfc867d
HR
3257 tf->feature == SETFEATURES_XFER) {
3258 fp = (cdb[0] == ATA_16) ? 4 : 3;
9a405257 3259 goto invalid_fld;
bcfc867d 3260 }
b095518e
JG
3261
3262 /*
bd30add8
TH
3263 * Filter TPM commands by default. These provide an
3264 * essentially uncontrolled encrypted "back door" between
3265 * applications and the disk. Set libata.allow_tpm=1 if you
3266 * have a real reason for wanting to use them. This ensures
3267 * that installed software cannot easily mess stuff up without
3268 * user intent. DVR type users will probably ship with this enabled
3269 * for movie content management.
b095518e 3270 *
bd30add8
TH
3271 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3272 * for this and should do in future but that it is not sufficient as
3273 * DCS is an optional feature set. Thus we also do the software filter
3274 * so that we comply with the TC consortium stated goal that the user
3275 * can turn off TC features of their system.
b095518e 3276 */
bcfc867d
HR
3277 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
3278 fp = (cdb[0] == ATA_16) ? 14 : 9;
bd30add8 3279 goto invalid_fld;
bcfc867d 3280 }
e61e0672 3281
b095518e 3282 return 0;
9a405257
TH
3283
3284 invalid_fld:
0df10b84 3285 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
9a405257 3286 return 1;
b095518e
JG
3287}
3288
9379e6b8
ST
3289/**
3290 * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim
3291 * @cmd: SCSI command being translated
ef2d7392 3292 * @trmax: Maximum number of entries that will fit in sector_size bytes.
9379e6b8 3293 * @sector: Starting sector
7b203094 3294 * @count: Total Range of request in logical sectors
9379e6b8
ST
3295 *
3296 * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted
3297 * descriptor.
3298 *
3299 * Upto 64 entries of the format:
3300 * 63:48 Range Length
3301 * 47:0 LBA
3302 *
3303 * Range Length of 0 is ignored.
3304 * LBA's should be sorted order and not overlap.
3305 *
3306 * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET
ef2d7392
ST
3307 *
3308 * Return: Number of bytes copied into sglist.
9379e6b8 3309 */
ef2d7392
ST
3310static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax,
3311 u64 sector, u32 count)
3312{
3313 struct scsi_device *sdp = cmd->device;
3314 size_t len = sdp->sector_size;
3315 size_t r;
3316 __le64 *buf;
3317 u32 i = 0;
9379e6b8
ST
3318 unsigned long flags;
3319
ef2d7392
ST
3320 WARN_ON(len > ATA_SCSI_RBUF_SIZE);
3321
3322 if (len > ATA_SCSI_RBUF_SIZE)
3323 len = ATA_SCSI_RBUF_SIZE;
9379e6b8
ST
3324
3325 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
ef2d7392
ST
3326 buf = ((void *)ata_scsi_rbuf);
3327 memset(buf, 0, len);
3328 while (i < trmax) {
9379e6b8
ST
3329 u64 entry = sector |
3330 ((u64)(count > 0xffff ? 0xffff : count) << 48);
ef2d7392 3331 buf[i++] = __cpu_to_le64(entry);
9379e6b8
ST
3332 if (count <= 0xffff)
3333 break;
3334 count -= 0xffff;
3335 sector += 0xffff;
3336 }
ef2d7392 3337 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
9379e6b8
ST
3338 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
3339
ef2d7392 3340 return r;
9379e6b8
ST
3341}
3342
7b203094
ST
3343/**
3344 * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same
3345 * @qc: Command to be translated
3346 *
3347 * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or
3348 * an SCT Write Same command.
6baf20bc
MCC
3349 * Based on WRITE SAME has the UNMAP flag:
3350 *
3351 * - When set translate to DSM TRIM
3352 * - When clear translate to SCT Write Same
7b203094 3353 */
18f0f978
CH
3354static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3355{
3356 struct ata_taskfile *tf = &qc->tf;
3357 struct scsi_cmnd *scmd = qc->scsicmd;
ef2d7392
ST
3358 struct scsi_device *sdp = scmd->device;
3359 size_t len = sdp->sector_size;
18f0f978
CH
3360 struct ata_device *dev = qc->dev;
3361 const u8 *cdb = scmd->cmnd;
3362 u64 block;
3363 u32 n_block;
ef2d7392 3364 const u32 trmax = len >> 3;
18f0f978 3365 u32 size;
bcfc867d 3366 u16 fp;
0df10b84 3367 u8 bp = 0xff;
7b203094 3368 u8 unmap = cdb[1] & 0x8;
18f0f978
CH
3369
3370 /* we may not issue DMA commands if no DMA mode is set */
6ac586f2 3371 if (unlikely(!ata_dma_enabled(dev)))
bcfc867d 3372 goto invalid_opcode;
18f0f978 3373
c6ade20f
CH
3374 /*
3375 * We only allow sending this command through the block layer,
3376 * as it modifies the DATA OUT buffer, which would corrupt user
3377 * memory for SG_IO commands.
3378 */
c8329cd5 3379 if (unlikely(blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))))
c6ade20f
CH
3380 goto invalid_opcode;
3381
bcfc867d
HR
3382 if (unlikely(scmd->cmd_len < 16)) {
3383 fp = 15;
18f0f978 3384 goto invalid_fld;
bcfc867d 3385 }
18f0f978
CH
3386 scsi_16_lba_len(cdb, &block, &n_block);
3387
7ebd8c5a 3388 if (!unmap || (dev->quirks & ATA_QUIRK_NOTRIM) ||
63ccc191
CH
3389 !ata_id_has_trim(dev->id)) {
3390 fp = 1;
3391 bp = 3;
3392 goto invalid_fld;
3393 }
3394 /* If the request is too large the cmd is invalid */
3395 if (n_block > 0xffff * trmax) {
3396 fp = 2;
3397 goto invalid_fld;
bcfc867d 3398 }
18f0f978
CH
3399
3400 /*
3401 * WRITE SAME always has a sector sized buffer as payload, this
3402 * should never be a multiple entry S/G list.
3403 */
3404 if (!scsi_sg_count(scmd))
bcfc867d 3405 goto invalid_param_len;
18f0f978 3406
ef2d7392
ST
3407 /*
3408 * size must match sector size in bytes
3409 * For DATA SET MANAGEMENT TRIM in ACS-2 nsect (aka count)
3410 * is defined as number of 512 byte blocks to be transferred.
3411 */
87fb6c31 3412
63ccc191
CH
3413 size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block);
3414 if (size != len)
3415 goto invalid_param_len;
3416
3417 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3418 /* Newer devices support queued TRIM commands */
3419 tf->protocol = ATA_PROT_NCQ;
3420 tf->command = ATA_CMD_FPDMA_SEND;
3421 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
4e5b6260 3422 tf->nsect = qc->hw_tag << 3;
63ccc191
CH
3423 tf->hob_feature = (size / 512) >> 8;
3424 tf->feature = size / 512;
3425
3426 tf->auxiliary = 1;
3427 } else {
7b203094 3428 tf->protocol = ATA_PROT_DMA;
63ccc191
CH
3429 tf->hob_feature = 0;
3430 tf->feature = ATA_DSM_TRIM;
3431 tf->hob_nsect = (size / 512) >> 8;
3432 tf->nsect = size / 512;
3433 tf->command = ATA_CMD_DSM;
87fb6c31
MC
3434 }
3435
18f0f978
CH
3436 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3437 ATA_TFLAG_WRITE;
3438
3439 ata_qc_set_pc_nbytes(qc);
3440
3441 return 0;
3442
bcfc867d 3443invalid_fld:
0df10b84 3444 ata_scsi_set_invalid_field(dev, scmd, fp, bp);
bcfc867d
HR
3445 return 1;
3446invalid_param_len:
3447 /* "Parameter list length error" */
3448 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3449 return 1;
3450invalid_opcode:
3451 /* "Invalid command operation code" */
3452 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
18f0f978
CH
3453 return 1;
3454}
3455
7b203094
ST
3456/**
3457 * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN
2365278e
DLM
3458 * @dev: Target device.
3459 * @cmd: SCSI command of interest.
7b203094
ST
3460 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
3461 *
3462 * Yields a subset to satisfy scsi_report_opcode()
3463 *
3464 * LOCKING:
3465 * spin_lock_irqsave(host lock)
3466 */
2365278e
DLM
3467static unsigned int ata_scsiop_maint_in(struct ata_device *dev,
3468 struct scsi_cmnd *cmd, u8 *rbuf)
7b203094 3469{
2365278e 3470 u8 *cdb = cmd->cmnd;
0de55801 3471 u8 supported = 0, cdlp = 0, rwcdlp = 0;
4ab7bb97
DLM
3472
3473 if ((cdb[1] & 0x1f) != MI_REPORT_SUPPORTED_OPERATION_CODES) {
2365278e 3474 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
5251ae22 3475 return 0;
4ab7bb97 3476 }
7b203094 3477
002c4871 3478 if (cdb[2] != 1 && cdb[2] != 3) {
7b203094 3479 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
2365278e 3480 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
5251ae22 3481 return 0;
7b203094 3482 }
002c4871 3483
7b203094
ST
3484 switch (cdb[3]) {
3485 case INQUIRY:
3486 case MODE_SENSE:
3487 case MODE_SENSE_10:
3488 case READ_CAPACITY:
3489 case SERVICE_ACTION_IN_16:
3490 case REPORT_LUNS:
3491 case REQUEST_SENSE:
3492 case SYNCHRONIZE_CACHE:
ea045fd3 3493 case SYNCHRONIZE_CACHE_16:
7b203094
ST
3494 case REZERO_UNIT:
3495 case SEEK_6:
3496 case SEEK_10:
3497 case TEST_UNIT_READY:
3498 case SEND_DIAGNOSTIC:
3499 case MAINTENANCE_IN:
3500 case READ_6:
3501 case READ_10:
7b203094
ST
3502 case WRITE_6:
3503 case WRITE_10:
7b203094
ST
3504 case ATA_12:
3505 case ATA_16:
3506 case VERIFY:
3507 case VERIFY_16:
3508 case MODE_SELECT:
3509 case MODE_SELECT_10:
3510 case START_STOP:
3511 supported = 3;
3512 break;
0de55801
DLM
3513 case READ_16:
3514 supported = 3;
3515 if (dev->flags & ATA_DFLAG_CDL) {
3516 /*
3517 * CDL read descriptors map to the T2A page, that is,
3518 * rwcdlp = 0x01 and cdlp = 0x01
3519 */
3520 rwcdlp = 0x01;
3521 cdlp = 0x01 << 3;
3522 }
3523 break;
3524 case WRITE_16:
3525 supported = 3;
3526 if (dev->flags & ATA_DFLAG_CDL) {
3527 /*
3528 * CDL write descriptors map to the T2B page, that is,
3529 * rwcdlp = 0x01 and cdlp = 0x02
3530 */
3531 rwcdlp = 0x01;
3532 cdlp = 0x02 << 3;
3533 }
3534 break;
7b203094
ST
3535 case ZBC_IN:
3536 case ZBC_OUT:
3537 if (ata_id_zoned_cap(dev->id) ||
3538 dev->class == ATA_DEV_ZAC)
3539 supported = 3;
3540 break;
818831c8
CH
3541 case SECURITY_PROTOCOL_IN:
3542 case SECURITY_PROTOCOL_OUT:
3543 if (dev->flags & ATA_DFLAG_TRUSTED)
3544 supported = 3;
3545 break;
7b203094
ST
3546 default:
3547 break;
3548 }
4ab7bb97 3549
0de55801
DLM
3550 /* One command format */
3551 rbuf[0] = rwcdlp;
3552 rbuf[1] = cdlp | supported;
4ab7bb97 3553
5251ae22 3554 return 4;
7b203094
ST
3555}
3556
28a3fc22
HR
3557/**
3558 * ata_scsi_report_zones_complete - convert ATA output
3559 * @qc: command structure returning the data
3560 *
3561 * Convert T-13 little-endian field representation into
3562 * T-10 big-endian field representation.
3563 * What a mess.
3564 */
3565static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
3566{
3567 struct scsi_cmnd *scmd = qc->scsicmd;
3568 struct sg_mapping_iter miter;
3569 unsigned long flags;
3570 unsigned int bytes = 0;
3571
3572 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
3573 SG_MITER_TO_SG | SG_MITER_ATOMIC);
3574
3575 local_irq_save(flags);
3576 while (sg_miter_next(&miter)) {
3577 unsigned int offset = 0;
3578
3579 if (bytes == 0) {
3580 char *hdr;
3581 u32 list_length;
3582 u64 max_lba, opt_lba;
3583 u16 same;
3584
3585 /* Swizzle header */
3586 hdr = miter.addr;
3587 list_length = get_unaligned_le32(&hdr[0]);
3588 same = get_unaligned_le16(&hdr[4]);
3589 max_lba = get_unaligned_le64(&hdr[8]);
3590 opt_lba = get_unaligned_le64(&hdr[16]);
3591 put_unaligned_be32(list_length, &hdr[0]);
3592 hdr[4] = same & 0xf;
3593 put_unaligned_be64(max_lba, &hdr[8]);
3594 put_unaligned_be64(opt_lba, &hdr[16]);
3595 offset += 64;
3596 bytes += 64;
3597 }
3598 while (offset < miter.length) {
3599 char *rec;
3600 u8 cond, type, non_seq, reset;
3601 u64 size, start, wp;
3602
3603 /* Swizzle zone descriptor */
3604 rec = miter.addr + offset;
3605 type = rec[0] & 0xf;
3606 cond = (rec[1] >> 4) & 0xf;
3607 non_seq = (rec[1] & 2);
3608 reset = (rec[1] & 1);
3609 size = get_unaligned_le64(&rec[8]);
3610 start = get_unaligned_le64(&rec[16]);
3611 wp = get_unaligned_le64(&rec[24]);
3612 rec[0] = type;
3613 rec[1] = (cond << 4) | non_seq | reset;
3614 put_unaligned_be64(size, &rec[8]);
3615 put_unaligned_be64(start, &rec[16]);
3616 put_unaligned_be64(wp, &rec[24]);
3617 WARN_ON(offset + 64 > miter.length);
3618 offset += 64;
3619 bytes += 64;
3620 }
3621 }
3622 sg_miter_stop(&miter);
3623 local_irq_restore(flags);
3624
3625 ata_scsi_qc_complete(qc);
3626}
3627
3628static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
3629{
3630 struct ata_taskfile *tf = &qc->tf;
3631 struct scsi_cmnd *scmd = qc->scsicmd;
3632 const u8 *cdb = scmd->cmnd;
3633 u16 sect, fp = (u16)-1;
3634 u8 sa, options, bp = 0xff;
3635 u64 block;
3636 u32 n_block;
3637
3638 if (unlikely(scmd->cmd_len < 16)) {
3639 ata_dev_warn(qc->dev, "invalid cdb length %d\n",
3640 scmd->cmd_len);
3641 fp = 15;
3642 goto invalid_fld;
3643 }
3644 scsi_16_lba_len(cdb, &block, &n_block);
3645 if (n_block != scsi_bufflen(scmd)) {
3646 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
3647 n_block, scsi_bufflen(scmd));
3648 goto invalid_param_len;
3649 }
3650 sa = cdb[1] & 0x1f;
3651 if (sa != ZI_REPORT_ZONES) {
3652 ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
3653 fp = 1;
3654 goto invalid_fld;
3655 }
3656 /*
3657 * ZAC allows only for transfers in 512 byte blocks,
3658 * and uses a 16 bit value for the transfer count.
3659 */
3660 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
3661 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
3662 goto invalid_param_len;
3663 }
3664 sect = n_block / 512;
3f17422b 3665 options = cdb[14] & 0xbf;
28a3fc22
HR
3666
3667 if (ata_ncq_enabled(qc->dev) &&
3668 ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
3669 tf->protocol = ATA_PROT_NCQ;
3670 tf->command = ATA_CMD_FPDMA_RECV;
3671 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
4e5b6260 3672 tf->nsect = qc->hw_tag << 3;
28a3fc22
HR
3673 tf->feature = sect & 0xff;
3674 tf->hob_feature = (sect >> 8) & 0xff;
3f17422b 3675 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
28a3fc22
HR
3676 } else {
3677 tf->command = ATA_CMD_ZAC_MGMT_IN;
3678 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
3679 tf->protocol = ATA_PROT_DMA;
3680 tf->hob_feature = options;
3681 tf->hob_nsect = (sect >> 8) & 0xff;
3682 tf->nsect = sect & 0xff;
3683 }
3684 tf->device = ATA_LBA;
3685 tf->lbah = (block >> 16) & 0xff;
3686 tf->lbam = (block >> 8) & 0xff;
3687 tf->lbal = block & 0xff;
3688 tf->hob_lbah = (block >> 40) & 0xff;
3689 tf->hob_lbam = (block >> 32) & 0xff;
3690 tf->hob_lbal = (block >> 24) & 0xff;
3691
3692 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3693 qc->flags |= ATA_QCFLAG_RESULT_TF;
3694
3695 ata_qc_set_pc_nbytes(qc);
3696
3697 qc->complete_fn = ata_scsi_report_zones_complete;
3698
3699 return 0;
3700
3701invalid_fld:
3702 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
3703 return 1;
3704
3705invalid_param_len:
3706 /* "Parameter list length error" */
3707 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3708 return 1;
3709}
3710
27708a95
HR
3711static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
3712{
3713 struct ata_taskfile *tf = &qc->tf;
3714 struct scsi_cmnd *scmd = qc->scsicmd;
3715 struct ata_device *dev = qc->dev;
3716 const u8 *cdb = scmd->cmnd;
2950cefa 3717 u8 all, sa;
27708a95
HR
3718 u64 block;
3719 u32 n_block;
3720 u16 fp = (u16)-1;
3721
3722 if (unlikely(scmd->cmd_len < 16)) {
3723 fp = 15;
3724 goto invalid_fld;
3725 }
3726
3727 sa = cdb[1] & 0x1f;
3728 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
3729 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
3730 fp = 1;
3731 goto invalid_fld;
3732 }
3733
3734 scsi_16_lba_len(cdb, &block, &n_block);
3735 if (n_block) {
3736 /*
3737 * ZAC MANAGEMENT OUT doesn't define any length
3738 */
3739 goto invalid_param_len;
3740 }
27708a95 3741
2950cefa 3742 all = cdb[14] & 0x1;
6edf1d4c
DLM
3743 if (all) {
3744 /*
3745 * Ignore the block address (zone ID) as defined by ZBC.
3746 */
3747 block = 0;
3748 } else if (block >= dev->n_sectors) {
b320a0a9
DLM
3749 /*
3750 * Block must be a valid zone ID (a zone start LBA).
3751 */
3752 fp = 2;
3753 goto invalid_fld;
3754 }
27708a95 3755
284b3b77
HR
3756 if (ata_ncq_enabled(qc->dev) &&
3757 ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
5b844b63 3758 tf->protocol = ATA_PROT_NCQ_NODATA;
284b3b77 3759 tf->command = ATA_CMD_NCQ_NON_DATA;
ee194b75 3760 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
4e5b6260 3761 tf->nsect = qc->hw_tag << 3;
2950cefa 3762 tf->auxiliary = sa | ((u16)all << 8);
284b3b77
HR
3763 } else {
3764 tf->protocol = ATA_PROT_NODATA;
3765 tf->command = ATA_CMD_ZAC_MGMT_OUT;
3766 tf->feature = sa;
2950cefa 3767 tf->hob_feature = all;
284b3b77 3768 }
27708a95
HR
3769 tf->lbah = (block >> 16) & 0xff;
3770 tf->lbam = (block >> 8) & 0xff;
3771 tf->lbal = block & 0xff;
3772 tf->hob_lbah = (block >> 40) & 0xff;
3773 tf->hob_lbam = (block >> 32) & 0xff;
3774 tf->hob_lbal = (block >> 24) & 0xff;
3775 tf->device = ATA_LBA;
3776 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3777
3778 return 0;
3779
18f0f978 3780 invalid_fld:
27708a95
HR
3781 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
3782 return 1;
27708a95
HR
3783invalid_param_len:
3784 /* "Parameter list length error" */
3785 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
18f0f978
CH
3786 return 1;
3787}
3788
1b26d29c
PB
3789/**
3790 * ata_mselect_caching - Simulate MODE SELECT for caching info page
3791 * @qc: Storage for translated ATA taskfile
3792 * @buf: input buffer
3793 * @len: number of valid bytes in the input buffer
7780081c 3794 * @fp: out parameter for the failed field on error
1b26d29c
PB
3795 *
3796 * Prepare a taskfile to modify caching information for the device.
3797 *
3798 * LOCKING:
3799 * None.
3800 */
3801static int ata_mselect_caching(struct ata_queued_cmd *qc,
7780081c 3802 const u8 *buf, int len, u16 *fp)
1b26d29c
PB
3803{
3804 struct ata_taskfile *tf = &qc->tf;
3805 struct ata_device *dev = qc->dev;
d7372cb6 3806 u8 mpage[CACHE_MPAGE_LEN];
1b26d29c 3807 u8 wce;
7780081c 3808 int i;
1b26d29c
PB
3809
3810 /*
3811 * The first two bytes of def_cache_mpage are a header, so offsets
3812 * in mpage are off by 2 compared to buf. Same for len.
3813 */
3814
7780081c 3815 if (len != CACHE_MPAGE_LEN - 2) {
da299470 3816 *fp = min(len, CACHE_MPAGE_LEN - 2);
1b26d29c 3817 return -EINVAL;
7780081c 3818 }
1b26d29c
PB
3819
3820 wce = buf[0] & (1 << 2);
3821
3822 /*
3823 * Check that read-only bits are not modified.
3824 */
3825 ata_msense_caching(dev->id, mpage, false);
7780081c
HR
3826 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
3827 if (i == 0)
3828 continue;
3829 if (mpage[i + 2] != buf[i]) {
3830 *fp = i;
3831 return -EINVAL;
3832 }
3833 }
1b26d29c
PB
3834
3835 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3836 tf->protocol = ATA_PROT_NODATA;
3837 tf->nsect = 0;
3838 tf->command = ATA_CMD_SET_FEATURES;
3839 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3840 return 0;
3841}
3842
df60f9c6
DLM
3843/*
3844 * Simulate MODE SELECT control mode page, sub-page 0.
06dbde5f 3845 */
df60f9c6
DLM
3846static int ata_mselect_control_spg0(struct ata_queued_cmd *qc,
3847 const u8 *buf, int len, u16 *fp)
06dbde5f
HR
3848{
3849 struct ata_device *dev = qc->dev;
d7372cb6 3850 u8 mpage[CONTROL_MPAGE_LEN];
06dbde5f 3851 u8 d_sense;
7780081c 3852 int i;
06dbde5f
HR
3853
3854 /*
3855 * The first two bytes of def_control_mpage are a header, so offsets
3856 * in mpage are off by 2 compared to buf. Same for len.
3857 */
3858
7780081c 3859 if (len != CONTROL_MPAGE_LEN - 2) {
da299470 3860 *fp = min(len, CONTROL_MPAGE_LEN - 2);
06dbde5f 3861 return -EINVAL;
7780081c 3862 }
06dbde5f
HR
3863
3864 d_sense = buf[0] & (1 << 2);
3865
3866 /*
3867 * Check that read-only bits are not modified.
3868 */
673b2fe6 3869 ata_msense_control_spg0(dev, mpage, false);
7780081c
HR
3870 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
3871 if (i == 0)
3872 continue;
3873 if (mpage[2 + i] != buf[i]) {
3874 *fp = i;
3875 return -EINVAL;
3876 }
3877 }
06dbde5f
HR
3878 if (d_sense & (1 << 2))
3879 dev->flags |= ATA_DFLAG_D_SENSE;
3880 else
3881 dev->flags &= ~ATA_DFLAG_D_SENSE;
06dbde5f
HR
3882 return 0;
3883}
3884
df60f9c6 3885/*
db91586b 3886 * Translate MODE SELECT control mode page, sub-page f2h (ATA feature mode
df60f9c6
DLM
3887 * page) into a SET FEATURES command.
3888 */
db91586b
DLM
3889static int ata_mselect_control_ata_feature(struct ata_queued_cmd *qc,
3890 const u8 *buf, int len, u16 *fp)
df60f9c6
DLM
3891{
3892 struct ata_device *dev = qc->dev;
3893 struct ata_taskfile *tf = &qc->tf;
3894 u8 cdl_action;
3895
3896 /*
3897 * The first four bytes of ATA Feature Control mode page are a header,
3898 * so offsets in mpage are off by 4 compared to buf. Same for len.
3899 */
3900 if (len != ATA_FEATURE_SUB_MPAGE_LEN - 4) {
3901 *fp = min(len, ATA_FEATURE_SUB_MPAGE_LEN - 4);
3902 return -EINVAL;
3903 }
3904
3905 /* Check cdl_ctrl */
3906 switch (buf[0] & 0x03) {
3907 case 0:
17e897a4
DLM
3908 /* Disable CDL if it is enabled */
3909 if (!(dev->flags & ATA_DFLAG_CDL_ENABLED))
3910 return 0;
3911 ata_dev_dbg(dev, "Disabling CDL\n");
df60f9c6
DLM
3912 cdl_action = 0;
3913 dev->flags &= ~ATA_DFLAG_CDL_ENABLED;
3914 break;
3915 case 0x02:
17e897a4
DLM
3916 /*
3917 * Enable CDL if not already enabled. Since this is mutually
3918 * exclusive with NCQ priority, allow this only if NCQ priority
3919 * is disabled.
3920 */
3921 if (dev->flags & ATA_DFLAG_CDL_ENABLED)
3922 return 0;
df60f9c6
DLM
3923 if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED) {
3924 ata_dev_err(dev,
3925 "NCQ priority must be disabled to enable CDL\n");
3926 return -EINVAL;
3927 }
17e897a4 3928 ata_dev_dbg(dev, "Enabling CDL\n");
df60f9c6
DLM
3929 cdl_action = 1;
3930 dev->flags |= ATA_DFLAG_CDL_ENABLED;
3931 break;
3932 default:
3933 *fp = 0;
3934 return -EINVAL;
3935 }
3936
3937 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3938 tf->protocol = ATA_PROT_NODATA;
3939 tf->command = ATA_CMD_SET_FEATURES;
3940 tf->feature = SETFEATURES_CDL;
3941 tf->nsect = cdl_action;
3942
3943 return 1;
3944}
3945
3946/**
3947 * ata_mselect_control - Simulate MODE SELECT for control page
3948 * @qc: Storage for translated ATA taskfile
401f8ef3 3949 * @spg: target sub-page of the control page
df60f9c6
DLM
3950 * @buf: input buffer
3951 * @len: number of valid bytes in the input buffer
3952 * @fp: out parameter for the failed field on error
3953 *
3954 * Prepare a taskfile to modify caching information for the device.
3955 *
3956 * LOCKING:
3957 * None.
3958 */
3959static int ata_mselect_control(struct ata_queued_cmd *qc, u8 spg,
3960 const u8 *buf, int len, u16 *fp)
3961{
3962 switch (spg) {
3963 case 0:
3964 return ata_mselect_control_spg0(qc, buf, len, fp);
3965 case ATA_FEATURE_SUB_MPAGE:
3966 return ata_mselect_control_ata_feature(qc, buf, len, fp);
3967 default:
3968 return -EINVAL;
3969 }
3970}
3971
1b26d29c 3972/**
01efd600 3973 * ata_scsi_mode_select_xlat - Simulate MODE SELECT 6, 10 commands
1b26d29c
PB
3974 * @qc: Storage for translated ATA taskfile
3975 *
3976 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
3977 * Assume this is invoked for direct access devices (e.g. disks) only.
3978 * There should be no block descriptor for other device types.
3979 *
3980 * LOCKING:
3981 * spin_lock_irqsave(host lock)
3982 */
3983static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
3984{
3985 struct scsi_cmnd *scmd = qc->scsicmd;
3986 const u8 *cdb = scmd->cmnd;
1b26d29c
PB
3987 u8 pg, spg;
3988 unsigned six_byte, pg_len, hdr_len, bd_len;
df60f9c6 3989 int len, ret;
7780081c
HR
3990 u16 fp = (u16)-1;
3991 u8 bp = 0xff;
f650ef61
YB
3992 u8 buffer[64];
3993 const u8 *p = buffer;
1b26d29c 3994
1b26d29c
PB
3995 six_byte = (cdb[0] == MODE_SELECT);
3996 if (six_byte) {
bcfc867d
HR
3997 if (scmd->cmd_len < 5) {
3998 fp = 4;
1b26d29c 3999 goto invalid_fld;
bcfc867d 4000 }
1b26d29c
PB
4001
4002 len = cdb[4];
4003 hdr_len = 4;
4004 } else {
bcfc867d
HR
4005 if (scmd->cmd_len < 9) {
4006 fp = 8;
1b26d29c 4007 goto invalid_fld;
bcfc867d 4008 }
1b26d29c 4009
261e1507 4010 len = get_unaligned_be16(&cdb[7]);
1b26d29c
PB
4011 hdr_len = 8;
4012 }
4013
4014 /* We only support PF=1, SP=0. */
bcfc867d
HR
4015 if ((cdb[1] & 0x11) != 0x10) {
4016 fp = 1;
0df10b84 4017 bp = (cdb[1] & 0x01) ? 1 : 5;
1b26d29c 4018 goto invalid_fld;
bcfc867d 4019 }
1b26d29c
PB
4020
4021 /* Test early for possible overrun. */
4022 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
4023 goto invalid_param_len;
4024
1b26d29c
PB
4025 /* Move past header and block descriptors. */
4026 if (len < hdr_len)
4027 goto invalid_param_len;
4028
f650ef61
YB
4029 if (!sg_copy_to_buffer(scsi_sglist(scmd), scsi_sg_count(scmd),
4030 buffer, sizeof(buffer)))
4031 goto invalid_param_len;
4032
1b26d29c
PB
4033 if (six_byte)
4034 bd_len = p[3];
4035 else
261e1507 4036 bd_len = get_unaligned_be16(&p[6]);
1b26d29c
PB
4037
4038 len -= hdr_len;
4039 p += hdr_len;
4040 if (len < bd_len)
4041 goto invalid_param_len;
7780081c
HR
4042 if (bd_len != 0 && bd_len != 8) {
4043 fp = (six_byte) ? 3 : 6;
4044 fp += bd_len + hdr_len;
1b26d29c 4045 goto invalid_param;
7780081c 4046 }
1b26d29c
PB
4047
4048 len -= bd_len;
4049 p += bd_len;
4050 if (len == 0)
4051 goto skip;
4052
4053 /* Parse both possible formats for the mode page headers. */
4054 pg = p[0] & 0x3f;
4055 if (p[0] & 0x40) {
4056 if (len < 4)
4057 goto invalid_param_len;
4058
4059 spg = p[1];
261e1507 4060 pg_len = get_unaligned_be16(&p[2]);
1b26d29c
PB
4061 p += 4;
4062 len -= 4;
4063 } else {
4064 if (len < 2)
4065 goto invalid_param_len;
4066
4067 spg = 0;
4068 pg_len = p[1];
4069 p += 2;
4070 len -= 2;
4071 }
4072
4073 /*
df60f9c6
DLM
4074 * Supported subpages: all subpages and ATA feature sub-page f2h of
4075 * the control page.
1b26d29c 4076 */
df60f9c6
DLM
4077 if (spg) {
4078 switch (spg) {
4079 case ALL_SUB_MPAGES:
4080 /* All subpages is not supported for the control page */
4081 if (pg == CONTROL_MPAGE) {
4082 fp = (p[0] & 0x40) ? 1 : 0;
4083 fp += hdr_len + bd_len;
4084 goto invalid_param;
4085 }
4086 break;
4087 case ATA_FEATURE_SUB_MPAGE:
4088 if (qc->dev->flags & ATA_DFLAG_CDL &&
4089 pg == CONTROL_MPAGE)
4090 break;
4091 fallthrough;
4092 default:
4093 fp = (p[0] & 0x40) ? 1 : 0;
4094 fp += hdr_len + bd_len;
4095 goto invalid_param;
4096 }
7780081c 4097 }
1b26d29c
PB
4098 if (pg_len > len)
4099 goto invalid_param_len;
4100
4101 switch (pg) {
4102 case CACHE_MPAGE:
7780081c
HR
4103 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
4104 fp += hdr_len + bd_len;
1b26d29c 4105 goto invalid_param;
7780081c 4106 }
1b26d29c 4107 break;
06dbde5f 4108 case CONTROL_MPAGE:
df60f9c6
DLM
4109 ret = ata_mselect_control(qc, spg, p, pg_len, &fp);
4110 if (ret < 0) {
7780081c 4111 fp += hdr_len + bd_len;
06dbde5f 4112 goto invalid_param;
7780081c 4113 }
df60f9c6
DLM
4114 if (!ret)
4115 goto skip; /* No ATA command to send */
1b26d29c 4116 break;
df60f9c6
DLM
4117 default:
4118 /* Invalid page code */
7780081c 4119 fp = bd_len + hdr_len;
1b26d29c
PB
4120 goto invalid_param;
4121 }
4122
4123 /*
4124 * Only one page has changeable data, so we only support setting one
4125 * page at a time.
4126 */
4127 if (len > pg_len)
4128 goto invalid_param;
4129
4130 return 0;
4131
4132 invalid_fld:
0df10b84 4133 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
1b26d29c
PB
4134 return 1;
4135
4136 invalid_param:
7780081c 4137 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
1b26d29c
PB
4138 return 1;
4139
4140 invalid_param_len:
4141 /* "Parameter list length error" */
06dbde5f 4142 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
1b26d29c
PB
4143 return 1;
4144
4145 skip:
4146 scmd->result = SAM_STAT_GOOD;
4147 return 1;
4148}
4149
818831c8
CH
4150static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma)
4151{
4152 if (len == 0)
4153 return ATA_CMD_TRUSTED_NONDATA;
4154 else if (send)
4155 return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND;
4156 else
4157 return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV;
4158}
4159
4160static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc)
4161{
4162 struct scsi_cmnd *scmd = qc->scsicmd;
4163 const u8 *cdb = scmd->cmnd;
4164 struct ata_taskfile *tf = &qc->tf;
4165 u8 secp = cdb[1];
4166 bool send = (cdb[0] == SECURITY_PROTOCOL_OUT);
4167 u16 spsp = get_unaligned_be16(&cdb[2]);
4168 u32 len = get_unaligned_be32(&cdb[6]);
4169 bool dma = !(qc->dev->flags & ATA_DFLAG_PIO);
4170
4171 /*
4172 * We don't support the ATA "security" protocol.
4173 */
4174 if (secp == 0xef) {
4175 ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0);
4176 return 1;
4177 }
4178
4179 if (cdb[4] & 7) { /* INC_512 */
4180 if (len > 0xffff) {
4181 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4182 return 1;
4183 }
4184 } else {
4185 if (len > 0x01fffe00) {
4186 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4187 return 1;
4188 }
4189
4190 /* convert to the sector-based ATA addressing */
4191 len = (len + 511) / 512;
4192 }
4193
4194 tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO;
4195 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA;
4196 if (send)
4197 tf->flags |= ATA_TFLAG_WRITE;
4198 tf->command = ata_scsi_trusted_op(len, send, dma);
4199 tf->feature = secp;
4200 tf->lbam = spsp & 0xff;
4201 tf->lbah = spsp >> 8;
4202
4203 if (len) {
4204 tf->nsect = len & 0xff;
4205 tf->lbal = len >> 8;
4206 } else {
4207 if (!send)
4208 tf->lbah = (1 << 7);
4209 }
4210
4211 ata_qc_set_pc_nbytes(qc);
4212 return 0;
4213}
4214
b1ffbf85
MI
4215/**
4216 * ata_scsi_var_len_cdb_xlat - SATL variable length CDB to Handler
4217 * @qc: Command to be translated
4218 *
4219 * Translate a SCSI variable length CDB to specified commands.
4220 * It checks a service action value in CDB to call corresponding handler.
4221 *
4222 * RETURNS:
4223 * Zero on success, non-zero on failure
4224 *
4225 */
4226static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc)
4227{
4228 struct scsi_cmnd *scmd = qc->scsicmd;
4229 const u8 *cdb = scmd->cmnd;
4230 const u16 sa = get_unaligned_be16(&cdb[8]);
4231
4232 /*
4233 * if service action represents a ata pass-thru(32) command,
4234 * then pass it to ata_scsi_pass_thru handler.
4235 */
4236 if (sa == ATA_32)
4237 return ata_scsi_pass_thru(qc);
4238
b1ffbf85
MI
4239 /* unsupported service action */
4240 return 1;
4241}
4242
1da177e4
LT
4243/**
4244 * ata_get_xlat_func - check if SCSI to ATA translation is possible
4245 * @dev: ATA device
4246 * @cmd: SCSI command opcode to consider
4247 *
4248 * Look up the SCSI command given, and determine whether the
4249 * SCSI command is to be translated or simulated.
4250 *
4251 * RETURNS:
4252 * Pointer to translation function if possible, %NULL if not.
4253 */
4254
4255static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
4256{
4257 switch (cmd) {
4258 case READ_6:
4259 case READ_10:
4260 case READ_16:
4261
4262 case WRITE_6:
4263 case WRITE_10:
4264 case WRITE_16:
4265 return ata_scsi_rw_xlat;
4266
0cdd6eb7 4267 case WRITE_SAME_16:
18f0f978
CH
4268 return ata_scsi_write_same_xlat;
4269
1da177e4 4270 case SYNCHRONIZE_CACHE:
ea045fd3 4271 case SYNCHRONIZE_CACHE_16:
1da177e4
LT
4272 if (ata_try_flush_cache(dev))
4273 return ata_scsi_flush_xlat;
4274 break;
4275
4276 case VERIFY:
4277 case VERIFY_16:
4278 return ata_scsi_verify_xlat;
b095518e
JG
4279
4280 case ATA_12:
4281 case ATA_16:
4282 return ata_scsi_pass_thru;
da61396d 4283
b1ffbf85
MI
4284 case VARIABLE_LENGTH_CMD:
4285 return ata_scsi_var_len_cdb_xlat;
4286
1b26d29c
PB
4287 case MODE_SELECT:
4288 case MODE_SELECT_10:
4289 return ata_scsi_mode_select_xlat;
1b26d29c 4290
28a3fc22
HR
4291 case ZBC_IN:
4292 return ata_scsi_zbc_in_xlat;
4293
27708a95
HR
4294 case ZBC_OUT:
4295 return ata_scsi_zbc_out_xlat;
4296
818831c8
CH
4297 case SECURITY_PROTOCOL_IN:
4298 case SECURITY_PROTOCOL_OUT:
4299 if (!(dev->flags & ATA_DFLAG_TRUSTED))
4300 break;
4301 return ata_scsi_security_inout_xlat;
4302
972dcafb
DG
4303 case START_STOP:
4304 return ata_scsi_start_stop_xlat;
1da177e4
LT
4305 }
4306
4307 return NULL;
4308}
4309
15964ff7 4310int __ata_scsi_queuecmd(struct scsi_cmnd *scmd, struct ata_device *dev)
eb3f0f9c 4311{
e20e81a2 4312 struct ata_port *ap = dev->link->ap;
baf4fdfa
ML
4313 u8 scsi_op = scmd->cmnd[0];
4314 ata_xlat_func_t xlat_func;
84eac327 4315
e20e81a2
NC
4316 /*
4317 * scsi_queue_rq() will defer commands if scsi_host_in_recovery().
4318 * However, this check is done without holding the ap->lock (a libata
4319 * specific lock), so we can have received an error irq since then,
4320 * therefore we must check if EH is pending, while holding ap->lock.
4321 */
4322 if (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS))
4323 return SCSI_MLQUEUE_DEVICE_BUSY;
4324
84eac327
WH
4325 if (unlikely(!scmd->cmd_len))
4326 goto bad_cdb_len;
2115ea94 4327
9162c657 4328 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
84eac327 4329 if (unlikely(scmd->cmd_len > dev->cdb_len))
baf4fdfa 4330 goto bad_cdb_len;
eb3f0f9c 4331
baf4fdfa 4332 xlat_func = ata_get_xlat_func(dev, scsi_op);
84eac327
WH
4333 } else if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
4334 /* relay SCSI command to ATAPI device */
4335 int len = COMMAND_SIZE(scsi_op);
baf4fdfa 4336
84eac327
WH
4337 if (unlikely(len > scmd->cmd_len ||
4338 len > dev->cdb_len ||
4339 scmd->cmd_len > ATAPI_CDB_LEN))
4340 goto bad_cdb_len;
baf4fdfa 4341
84eac327
WH
4342 xlat_func = atapi_xlat;
4343 } else {
4344 /* ATA_16 passthru, treat as an ATA command */
4345 if (unlikely(scmd->cmd_len > 16))
4346 goto bad_cdb_len;
baf4fdfa 4347
84eac327 4348 xlat_func = ata_get_xlat_func(dev, scsi_op);
baf4fdfa
ML
4349 }
4350
4351 if (xlat_func)
84eac327 4352 return ata_scsi_translate(dev, scmd, xlat_func);
2115ea94 4353
84eac327
WH
4354 ata_scsi_simulate(dev, scmd);
4355
4356 return 0;
baf4fdfa
ML
4357
4358 bad_cdb_len:
baf4fdfa 4359 scmd->result = DID_ERROR << 16;
58bf201d 4360 scsi_done(scmd);
baf4fdfa 4361 return 0;
eb3f0f9c
BK
4362}
4363
1da177e4
LT
4364/**
4365 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
23e701e6 4366 * @shost: SCSI host of command to be sent
1da177e4 4367 * @cmd: SCSI command to be sent
1da177e4
LT
4368 *
4369 * In some cases, this function translates SCSI commands into
4370 * ATA taskfiles, and queues the taskfiles to be sent to
4371 * hardware. In other cases, this function simulates a
4372 * SCSI device by evaluating and responding to certain
4373 * SCSI commands. This creates the overall effect of
4374 * ATA and ATAPI devices appearing as SCSI devices.
4375 *
4376 * LOCKING:
23e701e6 4377 * ATA host lock
1da177e4
LT
4378 *
4379 * RETURNS:
2115ea94
TH
4380 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
4381 * 0 otherwise.
1da177e4 4382 */
23e701e6 4383int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
1da177e4
LT
4384{
4385 struct ata_port *ap;
4386 struct ata_device *dev;
4387 struct scsi_device *scsidev = cmd->device;
2115ea94 4388 int rc = 0;
23e701e6 4389 unsigned long irq_flags;
1da177e4 4390
35bb94b1 4391 ap = ata_shost_to_port(shost);
005a5a06 4392
23e701e6 4393 spin_lock_irqsave(ap->lock, irq_flags);
1da177e4 4394
1da177e4 4395 dev = ata_scsi_find_dev(ap, scsidev);
eb3f0f9c 4396 if (likely(dev))
b27dcfb0 4397 rc = __ata_scsi_queuecmd(cmd, dev);
eb3f0f9c 4398 else {
1da177e4 4399 cmd->result = (DID_BAD_TARGET << 16);
58bf201d 4400 scsi_done(cmd);
1da177e4
LT
4401 }
4402
23e701e6
JG
4403 spin_unlock_irqrestore(ap->lock, irq_flags);
4404
2115ea94 4405 return rc;
1da177e4 4406}
a52fbcfc 4407EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
1da177e4
LT
4408
4409/**
4410 * ata_scsi_simulate - simulate SCSI command on ATA device
c893a3ae 4411 * @dev: the target device
1da177e4 4412 * @cmd: SCSI command being sent to device.
1da177e4
LT
4413 *
4414 * Interprets and directly executes a select list of SCSI commands
4415 * that can be handled internally.
4416 *
4417 * LOCKING:
cca3974e 4418 * spin_lock_irqsave(host lock)
1da177e4
LT
4419 */
4420
b27dcfb0 4421void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
1da177e4 4422{
057ace5e 4423 const u8 *scsicmd = cmd->cmnd;
45394145 4424 u8 tmp8;
1da177e4 4425
1da177e4 4426 switch(scsicmd[0]) {
2dcb407e 4427 case INQUIRY:
2365278e 4428 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_inquiry);
2dcb407e
JG
4429 break;
4430
4431 case MODE_SENSE:
4432 case MODE_SENSE_10:
2365278e 4433 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_mode_sense);
2dcb407e
JG
4434 break;
4435
2dcb407e 4436 case READ_CAPACITY:
eb846d9f 4437 case SERVICE_ACTION_IN_16:
2365278e 4438 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_read_cap);
2dcb407e 4439 break;
1da177e4 4440
2dcb407e 4441 case REPORT_LUNS:
2365278e 4442 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_report_luns);
2dcb407e 4443 break;
1da177e4 4444
2dcb407e 4445 case REQUEST_SENSE:
06dbde5f 4446 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
2dcb407e 4447 break;
1da177e4 4448
2dcb407e
JG
4449 /* if we reach this, then writeback caching is disabled,
4450 * turning this into a no-op.
4451 */
4452 case SYNCHRONIZE_CACHE:
ea045fd3 4453 case SYNCHRONIZE_CACHE_16:
df561f66 4454 fallthrough;
2dcb407e
JG
4455
4456 /* no-op's, complete with success */
4457 case REZERO_UNIT:
4458 case SEEK_6:
4459 case SEEK_10:
4460 case TEST_UNIT_READY:
2dcb407e 4461 break;
45394145 4462
2dcb407e
JG
4463 case SEND_DIAGNOSTIC:
4464 tmp8 = scsicmd[1] & ~(1 << 3);
a0c0b0e9 4465 if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4])
8fc6c065 4466 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
2dcb407e 4467 break;
1da177e4 4468
7b203094 4469 case MAINTENANCE_IN:
2365278e 4470 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_maint_in);
7b203094
ST
4471 break;
4472
2dcb407e
JG
4473 /* all other commands */
4474 default:
06dbde5f 4475 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2dcb407e 4476 /* "Invalid command operation code" */
2dcb407e 4477 break;
1da177e4 4478 }
8fc6c065 4479
58bf201d 4480 scsi_done(cmd);
1da177e4
LT
4481}
4482
25df73d9 4483int ata_scsi_add_hosts(struct ata_host *host, const struct scsi_host_template *sht)
f3187195
TH
4484{
4485 int i, rc;
4486
4487 for (i = 0; i < host->n_ports; i++) {
4488 struct ata_port *ap = host->ports[i];
4489 struct Scsi_Host *shost;
4490
4491 rc = -ENOMEM;
4492 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
4493 if (!shost)
4494 goto err_alloc;
4495
9ee4f393 4496 shost->eh_noresume = 1;
f3187195
TH
4497 *(struct ata_port **)&shost->hostdata[0] = ap;
4498 ap->scsi_host = shost;
4499
d9027470 4500 shost->transportt = ata_scsi_transport_template;
f3187195
TH
4501 shost->unique_id = ap->print_id;
4502 shost->max_id = 16;
4503 shost->max_lun = 1;
4504 shost->max_channel = 1;
b1ffbf85 4505 shost->max_cmd_len = 32;
f3187195 4506
31cc23b3
TH
4507 /* Schedule policy is determined by ->qc_defer()
4508 * callback and it needs to see every deferred qc.
4509 * Set host_blocked to 1 to prevent SCSI midlayer from
4510 * automatically deferring requests.
4511 */
4512 shost->max_host_blocked = 1;
4513
1d72f7ae 4514 rc = scsi_add_host_with_dma(shost, &ap->tdev, ap->host->dev);
f3187195 4515 if (rc)
1d72f7ae 4516 goto err_alloc;
f3187195
TH
4517 }
4518
4519 return 0;
4520
f3187195
TH
4521 err_alloc:
4522 while (--i >= 0) {
4523 struct Scsi_Host *shost = host->ports[i]->scsi_host;
4524
1d72f7ae 4525 /* scsi_host_put() is in ata_devres_release() */
f3187195 4526 scsi_remove_host(shost);
f3187195
TH
4527 }
4528 return rc;
4529}
4530
45b8084f
LW
4531#ifdef CONFIG_OF
4532static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
4533{
4534 struct scsi_device *sdev = dev->sdev;
4535 struct device *d = ap->host->dev;
4536 struct device_node *np = d->of_node;
4537 struct device_node *child;
4538
4539 for_each_available_child_of_node(np, child) {
4540 int ret;
4541 u32 val;
4542
4543 ret = of_property_read_u32(child, "reg", &val);
4544 if (ret)
4545 continue;
4546 if (val == dev->devno) {
4547 dev_dbg(d, "found matching device node\n");
4548 sdev->sdev_gendev.of_node = child;
4549 return;
4550 }
4551 }
4552}
4553#else
4554static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
4555{
4556}
4557#endif
4558
1ae46317 4559void ata_scsi_scan_host(struct ata_port *ap, int sync)
644dd0cc 4560{
1ae46317
TH
4561 int tries = 5;
4562 struct ata_device *last_failed_dev = NULL;
41bda9c9 4563 struct ata_link *link;
1ae46317 4564 struct ata_device *dev;
644dd0cc 4565
1ae46317 4566 repeat:
1eca4365
TH
4567 ata_for_each_link(link, ap, EDGE) {
4568 ata_for_each_dev(dev, link, ENABLED) {
41bda9c9
TH
4569 struct scsi_device *sdev;
4570 int channel = 0, id = 0;
3edebac4 4571
1eca4365 4572 if (dev->sdev)
41bda9c9 4573 continue;
3f19ee8c 4574
41bda9c9
TH
4575 if (ata_is_host_link(link))
4576 id = dev->devno;
4577 else
4578 channel = link->pmp;
4579
4580 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
4581 NULL);
4582 if (!IS_ERR(sdev)) {
4583 dev->sdev = sdev;
45b8084f 4584 ata_scsi_assign_ofnode(dev, ap);
41bda9c9 4585 scsi_device_put(sdev);
295124dc
GG
4586 } else {
4587 dev->sdev = NULL;
41bda9c9 4588 }
3edebac4 4589 }
3f19ee8c 4590 }
1ae46317
TH
4591
4592 /* If we scanned while EH was in progress or allocation
4593 * failure occurred, scan would have failed silently. Check
4594 * whether all devices are attached.
4595 */
1eca4365
TH
4596 ata_for_each_link(link, ap, EDGE) {
4597 ata_for_each_dev(dev, link, ENABLED) {
4598 if (!dev->sdev)
41bda9c9
TH
4599 goto exit_loop;
4600 }
1ae46317 4601 }
41bda9c9
TH
4602 exit_loop:
4603 if (!link)
1ae46317
TH
4604 return;
4605
4606 /* we're missing some SCSI devices */
4607 if (sync) {
4608 /* If caller requested synchrnous scan && we've made
4609 * any progress, sleep briefly and repeat.
4610 */
4611 if (dev != last_failed_dev) {
4612 msleep(100);
4613 last_failed_dev = dev;
4614 goto repeat;
4615 }
4616
4617 /* We might be failing to detect boot device, give it
4618 * a few more chances.
4619 */
4620 if (--tries) {
4621 msleep(100);
4622 goto repeat;
4623 }
4624
a9a79dfe
JP
4625 ata_port_err(ap,
4626 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
1ae46317
TH
4627 }
4628
ad72cf98 4629 queue_delayed_work(system_long_wq, &ap->hotplug_task,
1ae46317 4630 round_jiffies_relative(HZ));
644dd0cc 4631}
0ea035a3
TH
4632
4633/**
4634 * ata_scsi_offline_dev - offline attached SCSI device
4635 * @dev: ATA device to offline attached SCSI device for
4636 *
4637 * This function is called from ata_eh_hotplug() and responsible
4638 * for taking the SCSI device attached to @dev offline. This
cca3974e 4639 * function is called with host lock which protects dev->sdev
0ea035a3
TH
4640 * against clearing.
4641 *
4642 * LOCKING:
cca3974e 4643 * spin_lock_irqsave(host lock)
0ea035a3
TH
4644 *
4645 * RETURNS:
4646 * 1 if attached SCSI device exists, 0 otherwise.
4647 */
4648int ata_scsi_offline_dev(struct ata_device *dev)
4649{
4650 if (dev->sdev) {
4651 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
4652 return 1;
4653 }
4654 return 0;
4655}
580b2102
TH
4656
4657/**
4658 * ata_scsi_remove_dev - remove attached SCSI device
4659 * @dev: ATA device to remove attached SCSI device for
4660 *
4661 * This function is called from ata_eh_scsi_hotplug() and
4662 * responsible for removing the SCSI device attached to @dev.
4663 *
4664 * LOCKING:
4665 * Kernel thread context (may sleep).
4666 */
4667static void ata_scsi_remove_dev(struct ata_device *dev)
4668{
9af5c9c9 4669 struct ata_port *ap = dev->link->ap;
580b2102
TH
4670 struct scsi_device *sdev;
4671 unsigned long flags;
4672
4673 /* Alas, we need to grab scan_mutex to ensure SCSI device
4674 * state doesn't change underneath us and thus
4675 * scsi_device_get() always succeeds. The mutex locking can
4676 * be removed if there is __scsi_device_get() interface which
4677 * increments reference counts regardless of device state.
4678 */
cca3974e 4679 mutex_lock(&ap->scsi_host->scan_mutex);
ba6a1308 4680 spin_lock_irqsave(ap->lock, flags);
580b2102 4681
cca3974e 4682 /* clearing dev->sdev is protected by host lock */
580b2102
TH
4683 sdev = dev->sdev;
4684 dev->sdev = NULL;
4685
4686 if (sdev) {
4687 /* If user initiated unplug races with us, sdev can go
cca3974e 4688 * away underneath us after the host lock and
580b2102
TH
4689 * scan_mutex are released. Hold onto it.
4690 */
4691 if (scsi_device_get(sdev) == 0) {
4692 /* The following ensures the attached sdev is
4693 * offline on return from ata_scsi_offline_dev()
4694 * regardless it wins or loses the race
4695 * against this function.
4696 */
4697 scsi_device_set_state(sdev, SDEV_OFFLINE);
4698 } else {
4699 WARN_ON(1);
4700 sdev = NULL;
4701 }
4702 }
4703
ba6a1308 4704 spin_unlock_irqrestore(ap->lock, flags);
cca3974e 4705 mutex_unlock(&ap->scsi_host->scan_mutex);
580b2102
TH
4706
4707 if (sdev) {
a9a79dfe
JP
4708 ata_dev_info(dev, "detaching (SCSI %s)\n",
4709 dev_name(&sdev->sdev_gendev));
580b2102
TH
4710
4711 scsi_remove_device(sdev);
4712 scsi_device_put(sdev);
4713 }
4714}
4715
41bda9c9
TH
4716static void ata_scsi_handle_link_detach(struct ata_link *link)
4717{
4718 struct ata_port *ap = link->ap;
4719 struct ata_device *dev;
4720
1eca4365 4721 ata_for_each_dev(dev, link, ALL) {
41bda9c9
TH
4722 unsigned long flags;
4723
a1695151
DLM
4724 spin_lock_irqsave(ap->lock, flags);
4725 if (!(dev->flags & ATA_DFLAG_DETACHED)) {
4726 spin_unlock_irqrestore(ap->lock, flags);
41bda9c9 4727 continue;
a1695151 4728 }
41bda9c9 4729
41bda9c9
TH
4730 dev->flags &= ~ATA_DFLAG_DETACHED;
4731 spin_unlock_irqrestore(ap->lock, flags);
4732
4733 ata_scsi_remove_dev(dev);
4734 }
4735}
4736
2f294968
KCA
4737/**
4738 * ata_scsi_media_change_notify - send media change event
c5d0e6a0 4739 * @dev: Pointer to the disk device with media change event
2f294968
KCA
4740 *
4741 * Tell the block layer to send a media change notification
4742 * event.
4743 *
4744 * LOCKING:
854c73a2 4745 * spin_lock_irqsave(host lock)
2f294968 4746 */
854c73a2 4747void ata_scsi_media_change_notify(struct ata_device *dev)
2f294968 4748{
854c73a2 4749 if (dev->sdev)
f26792d5
JG
4750 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
4751 GFP_ATOMIC);
2f294968 4752}
2f294968 4753
580b2102
TH
4754/**
4755 * ata_scsi_hotplug - SCSI part of hotplug
65f27f38 4756 * @work: Pointer to ATA port to perform SCSI hotplug on
580b2102
TH
4757 *
4758 * Perform SCSI part of hotplug. It's executed from a separate
4759 * workqueue after EH completes. This is necessary because SCSI
4760 * hot plugging requires working EH and hot unplugging is
4761 * synchronized with hot plugging with a mutex.
4762 *
4763 * LOCKING:
4764 * Kernel thread context (may sleep).
4765 */
65f27f38 4766void ata_scsi_hotplug(struct work_struct *work)
580b2102 4767{
65f27f38
DH
4768 struct ata_port *ap =
4769 container_of(work, struct ata_port, hotplug_task.work);
41bda9c9 4770 int i;
580b2102 4771
1fe9fb71 4772 if (ap->pflags & ATA_PFLAG_UNLOADING)
580b2102 4773 return;
580b2102 4774
ad72cf98 4775 mutex_lock(&ap->scsi_scan_mutex);
580b2102 4776
41bda9c9
TH
4777 /* Unplug detached devices. We cannot use link iterator here
4778 * because PMP links have to be scanned even if PMP is
4779 * currently not attached. Iterate manually.
4780 */
4781 ata_scsi_handle_link_detach(&ap->link);
4782 if (ap->pmp_link)
4783 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
4784 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
580b2102
TH
4785
4786 /* scan for new ones */
1ae46317 4787 ata_scsi_scan_host(ap, 0);
580b2102 4788
ad72cf98 4789 mutex_unlock(&ap->scsi_scan_mutex);
580b2102 4790}
83c47bcb
TH
4791
4792/**
4793 * ata_scsi_user_scan - indication for user-initiated bus scan
4794 * @shost: SCSI host to scan
4795 * @channel: Channel to scan
4796 * @id: ID to scan
4797 * @lun: LUN to scan
4798 *
4799 * This function is called when user explicitly requests bus
4800 * scan. Set probe pending flag and invoke EH.
4801 *
4802 * LOCKING:
4803 * SCSI layer (we don't care)
4804 *
4805 * RETURNS:
4806 * Zero.
4807 */
d9027470 4808int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
9cb78c16 4809 unsigned int id, u64 lun)
83c47bcb
TH
4810{
4811 struct ata_port *ap = ata_shost_to_port(shost);
4812 unsigned long flags;
41bda9c9 4813 int devno, rc = 0;
83c47bcb 4814
41bda9c9 4815 if (lun != SCAN_WILD_CARD && lun)
83c47bcb
TH
4816 return -EINVAL;
4817
071f44b1 4818 if (!sata_pmp_attached(ap)) {
41bda9c9
TH
4819 if (channel != SCAN_WILD_CARD && channel)
4820 return -EINVAL;
4821 devno = id;
4822 } else {
4823 if (id != SCAN_WILD_CARD && id)
4824 return -EINVAL;
4825 devno = channel;
4826 }
4827
ba6a1308 4828 spin_lock_irqsave(ap->lock, flags);
83c47bcb 4829
41bda9c9
TH
4830 if (devno == SCAN_WILD_CARD) {
4831 struct ata_link *link;
4832
1eca4365 4833 ata_for_each_link(link, ap, EDGE) {
41bda9c9 4834 struct ata_eh_info *ehi = &link->eh_info;
b558eddd 4835 ehi->probe_mask |= ATA_ALL_DEVICES;
cf480626 4836 ehi->action |= ATA_EH_RESET;
41bda9c9 4837 }
83c47bcb 4838 } else {
41bda9c9 4839 struct ata_device *dev = ata_find_dev(ap, devno);
83c47bcb
TH
4840
4841 if (dev) {
41bda9c9 4842 struct ata_eh_info *ehi = &dev->link->eh_info;
9af5c9c9 4843 ehi->probe_mask |= 1 << dev->devno;
cf480626 4844 ehi->action |= ATA_EH_RESET;
83c47bcb
TH
4845 } else
4846 rc = -EINVAL;
4847 }
4848
309afcb5 4849 if (rc == 0) {
83c47bcb 4850 ata_port_schedule_eh(ap);
309afcb5
TH
4851 spin_unlock_irqrestore(ap->lock, flags);
4852 ata_port_wait_eh(ap);
4853 } else
4854 spin_unlock_irqrestore(ap->lock, flags);
83c47bcb
TH
4855
4856 return rc;
4857}
3057ac3c 4858
4859/**
d0171269 4860 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
65f27f38 4861 * @work: Pointer to ATA port to perform scsi_rescan_device()
3057ac3c 4862 *
d0171269 4863 * After ATA pass thru (SAT) commands are executed successfully,
ad72cf98 4864 * libata need to propagate the changes to SCSI layer.
3057ac3c 4865 *
d0171269
TH
4866 * LOCKING:
4867 * Kernel thread context (may sleep).
3057ac3c 4868 */
65f27f38 4869void ata_scsi_dev_rescan(struct work_struct *work)
3057ac3c 4870{
65f27f38 4871 struct ata_port *ap =
6aa0365a 4872 container_of(work, struct ata_port, scsi_rescan_task.work);
41bda9c9 4873 struct ata_link *link;
f58229f8 4874 struct ata_device *dev;
f84e7e41 4875 unsigned long flags;
0c76106c 4876 bool do_resume;
8b4d9469 4877 int ret = 0;
3057ac3c 4878
ad72cf98 4879 mutex_lock(&ap->scsi_scan_mutex);
f84e7e41
TH
4880 spin_lock_irqsave(ap->lock, flags);
4881
1eca4365
TH
4882 ata_for_each_link(link, ap, EDGE) {
4883 ata_for_each_dev(dev, link, ENABLED) {
41bda9c9 4884 struct scsi_device *sdev = dev->sdev;
3057ac3c 4885
8b4d9469
DLM
4886 /*
4887 * If the port was suspended before this was scheduled,
4888 * bail out.
4889 */
4890 if (ap->pflags & ATA_PFLAG_SUSPENDED)
79336504 4891 goto unlock_ap;
8b4d9469 4892
1eca4365 4893 if (!sdev)
41bda9c9
TH
4894 continue;
4895 if (scsi_device_get(sdev))
4896 continue;
f84e7e41 4897
0c76106c
DLM
4898 do_resume = dev->flags & ATA_DFLAG_RESUMING;
4899
41bda9c9 4900 spin_unlock_irqrestore(ap->lock, flags);
0c76106c
DLM
4901 if (do_resume) {
4902 ret = scsi_resume_device(sdev);
4903 if (ret == -EWOULDBLOCK)
79336504 4904 goto unlock_scan;
0c76106c
DLM
4905 dev->flags &= ~ATA_DFLAG_RESUMING;
4906 }
8b4d9469 4907 ret = scsi_rescan_device(sdev);
41bda9c9
TH
4908 scsi_device_put(sdev);
4909 spin_lock_irqsave(ap->lock, flags);
8b4d9469
DLM
4910
4911 if (ret)
79336504 4912 goto unlock_ap;
41bda9c9 4913 }
3057ac3c 4914 }
f84e7e41 4915
79336504 4916unlock_ap:
f84e7e41 4917 spin_unlock_irqrestore(ap->lock, flags);
79336504 4918unlock_scan:
ad72cf98 4919 mutex_unlock(&ap->scsi_scan_mutex);
6aa0365a 4920
8b4d9469
DLM
4921 /* Reschedule with a delay if scsi_rescan_device() returned an error */
4922 if (ret)
6aa0365a
DLM
4923 schedule_delayed_work(&ap->scsi_rescan_task,
4924 msecs_to_jiffies(5));
3057ac3c 4925}