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