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