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 (never less than one), that is the smaller of: 2301 * - MAX PAGES PER DSM COMMAND (IDENTIFY DEVICE word 105), when the device 2302 * reports a non-zero limit; and 2303 * - the logical sector size expressed in 512-byte pages (see above). 2304 */ 2305 static unsigned int ata_dsm_trim_pages(struct ata_device *dev) 2306 { 2307 unsigned int sector_size = ata_id_logical_sector_size(dev->id); 2308 unsigned int max_pages = ata_id_dsm_max_pages(dev->id); 2309 unsigned int pages = sector_size / ATA_SECT_SIZE; 2310 2311 /* If the device does not specify a limit, assume only a single page. */ 2312 if (!max_pages) 2313 max_pages = 1; 2314 2315 pages = min_not_zero(pages, max_pages); 2316 2317 return pages; 2318 } 2319 2320 /** 2321 * ata_scsiop_inq_b0 - Simulate INQUIRY VPD page B0, Block Limits 2322 * @dev: Target device. 2323 * @cmd: SCSI command of interest. 2324 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2325 * 2326 * Return data for the VPD page B0h (Block Limits). 2327 * 2328 * LOCKING: 2329 * spin_lock_irqsave(host lock) 2330 */ 2331 static unsigned int ata_scsiop_inq_b0(struct ata_device *dev, 2332 struct scsi_cmnd *cmd, u8 *rbuf) 2333 { 2334 u16 min_io_sectors; 2335 2336 rbuf[1] = 0xb0; 2337 rbuf[3] = 0x3c; /* required VPD size with unmap support */ 2338 2339 /* 2340 * Optimal transfer length granularity. 2341 * 2342 * This is always one physical block, but for disks with a smaller 2343 * logical than physical sector size we need to figure out what the 2344 * latter is. 2345 */ 2346 min_io_sectors = 1 << ata_id_log2_per_physical_sector(dev->id); 2347 put_unaligned_be16(min_io_sectors, &rbuf[6]); 2348 2349 /* 2350 * Optimal unmap granularity. 2351 * 2352 * The ATA spec doesn't even know about a granularity or alignment 2353 * for the TRIM command. We can leave away most of the unmap related 2354 * VPD page entries, but we have specifify a granularity to signal 2355 * that we support some form of unmap - in thise case via WRITE SAME 2356 * with the unmap bit set. 2357 */ 2358 if (ata_id_has_trim(dev->id)) { 2359 unsigned int max_pages = ata_dsm_trim_pages(dev); 2360 u64 max_blocks = max_pages * ATA_MAX_TRIM_RNUM * (u64)U16_MAX; 2361 2362 if (dev->quirks & ATA_QUIRK_MAX_TRIM_128M) 2363 max_blocks = 128 << (20 - SECTOR_SHIFT); 2364 2365 put_unaligned_be64(max_blocks, &rbuf[36]); 2366 put_unaligned_be32(1, &rbuf[28]); 2367 } 2368 2369 return get_unaligned_be16(&rbuf[2]) + 4; 2370 } 2371 2372 /** 2373 * ata_scsiop_inq_b1 - Simulate INQUIRY VPD page B1, Block Device 2374 * Characteristics 2375 * @dev: Target device. 2376 * @cmd: SCSI command of interest. 2377 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2378 * 2379 * Return data for the VPD page B1h (Block Device Characteristics). 2380 * 2381 * LOCKING: 2382 * spin_lock_irqsave(host lock) 2383 */ 2384 static unsigned int ata_scsiop_inq_b1(struct ata_device *dev, 2385 struct scsi_cmnd *cmd, u8 *rbuf) 2386 { 2387 int form_factor = ata_id_form_factor(dev->id); 2388 int media_rotation_rate = ata_id_rotation_rate(dev->id); 2389 u8 zoned = ata_id_zoned_cap(dev->id); 2390 2391 rbuf[1] = 0xb1; 2392 rbuf[3] = 0x3c; 2393 rbuf[4] = media_rotation_rate >> 8; 2394 rbuf[5] = media_rotation_rate; 2395 rbuf[7] = form_factor; 2396 if (zoned) 2397 rbuf[8] = (zoned << 4); 2398 2399 return get_unaligned_be16(&rbuf[2]) + 4; 2400 } 2401 2402 /** 2403 * ata_scsiop_inq_b2 - Simulate INQUIRY VPD page B2, Logical Block 2404 * Provisioning 2405 * @dev: Target device. 2406 * @cmd: SCSI command of interest. 2407 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2408 * 2409 * Return data for the VPD page B2h (Logical Block Provisioning). 2410 * 2411 * LOCKING: 2412 * spin_lock_irqsave(host lock) 2413 */ 2414 static unsigned int ata_scsiop_inq_b2(struct ata_device *dev, 2415 struct scsi_cmnd *cmd, u8 *rbuf) 2416 { 2417 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */ 2418 rbuf[1] = 0xb2; 2419 rbuf[3] = 0x4; 2420 rbuf[5] = 1 << 6; /* TPWS */ 2421 2422 return get_unaligned_be16(&rbuf[2]) + 4; 2423 } 2424 2425 /** 2426 * ata_scsiop_inq_b6 - Simulate INQUIRY VPD page B6, Zoned Block Device 2427 * Characteristics 2428 * @dev: Target device. 2429 * @cmd: SCSI command of interest. 2430 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2431 * 2432 * Return data for the VPD page B2h (Zoned Block Device Characteristics). 2433 * 2434 * LOCKING: 2435 * spin_lock_irqsave(host lock) 2436 */ 2437 static unsigned int ata_scsiop_inq_b6(struct ata_device *dev, 2438 struct scsi_cmnd *cmd, u8 *rbuf) 2439 { 2440 if (!ata_dev_is_zoned(dev)) { 2441 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff); 2442 return 0; 2443 } 2444 2445 /* 2446 * zbc-r05 SCSI Zoned Block device characteristics VPD page 2447 */ 2448 rbuf[1] = 0xb6; 2449 rbuf[3] = 0x3C; 2450 2451 /* 2452 * URSWRZ bit is only meaningful for host-managed ZAC drives 2453 */ 2454 if (dev->zac_zoned_cap & 1) 2455 rbuf[4] |= 1; 2456 put_unaligned_be32(dev->zac_zones_optimal_open, &rbuf[8]); 2457 put_unaligned_be32(dev->zac_zones_optimal_nonseq, &rbuf[12]); 2458 put_unaligned_be32(dev->zac_zones_max_open, &rbuf[16]); 2459 2460 return get_unaligned_be16(&rbuf[2]) + 4; 2461 } 2462 2463 /** 2464 * ata_scsiop_inq_b9 - Simulate INQUIRY VPD page B9, Concurrent Positioning 2465 * Ranges 2466 * @dev: Target device. 2467 * @cmd: SCSI command of interest. 2468 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2469 * 2470 * Return data for the VPD page B9h (Concurrent Positioning Ranges). 2471 * 2472 * LOCKING: 2473 * spin_lock_irqsave(host lock) 2474 */ 2475 static unsigned int ata_scsiop_inq_b9(struct ata_device *dev, 2476 struct scsi_cmnd *cmd, u8 *rbuf) 2477 { 2478 struct ata_cpr_log *cpr_log = dev->cpr_log; 2479 u8 *desc = &rbuf[64]; 2480 int i; 2481 2482 if (!cpr_log) { 2483 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff); 2484 return 0; 2485 } 2486 2487 /* SCSI Concurrent Positioning Ranges VPD page: SBC-5 rev 1 or later */ 2488 rbuf[1] = 0xb9; 2489 put_unaligned_be16(64 + (int)cpr_log->nr_cpr * 32 - 4, &rbuf[2]); 2490 2491 for (i = 0; i < cpr_log->nr_cpr; i++, desc += 32) { 2492 desc[0] = cpr_log->cpr[i].num; 2493 desc[1] = cpr_log->cpr[i].num_storage_elements; 2494 put_unaligned_be64(cpr_log->cpr[i].start_lba, &desc[8]); 2495 put_unaligned_be64(cpr_log->cpr[i].num_lbas, &desc[16]); 2496 } 2497 2498 return get_unaligned_be16(&rbuf[2]) + 4; 2499 } 2500 2501 /** 2502 * ata_scsiop_inquiry - Simulate INQUIRY command 2503 * @dev: Target device. 2504 * @cmd: SCSI command of interest. 2505 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2506 * 2507 * Returns data associated with an INQUIRY command output. 2508 * 2509 * LOCKING: 2510 * spin_lock_irqsave(host lock) 2511 */ 2512 static unsigned int ata_scsiop_inquiry(struct ata_device *dev, 2513 struct scsi_cmnd *cmd, u8 *rbuf) 2514 { 2515 const u8 *scsicmd = cmd->cmnd; 2516 2517 /* is CmdDt set? */ 2518 if (scsicmd[1] & 2) { 2519 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff); 2520 return 0; 2521 } 2522 2523 /* Is EVPD clear? */ 2524 if ((scsicmd[1] & 1) == 0) 2525 return ata_scsiop_inq_std(dev, cmd, rbuf); 2526 2527 switch (scsicmd[2]) { 2528 case 0x00: 2529 return ata_scsiop_inq_00(dev, cmd, rbuf); 2530 case 0x80: 2531 return ata_scsiop_inq_80(dev, cmd, rbuf); 2532 case 0x83: 2533 return ata_scsiop_inq_83(dev, cmd, rbuf); 2534 case 0x89: 2535 return ata_scsiop_inq_89(dev, cmd, rbuf); 2536 case 0xb0: 2537 return ata_scsiop_inq_b0(dev, cmd, rbuf); 2538 case 0xb1: 2539 return ata_scsiop_inq_b1(dev, cmd, rbuf); 2540 case 0xb2: 2541 return ata_scsiop_inq_b2(dev, cmd, rbuf); 2542 case 0xb6: 2543 return ata_scsiop_inq_b6(dev, cmd, rbuf); 2544 case 0xb9: 2545 return ata_scsiop_inq_b9(dev, cmd, rbuf); 2546 default: 2547 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff); 2548 return 0; 2549 } 2550 } 2551 2552 /** 2553 * modecpy - Prepare response for MODE SENSE 2554 * @dest: output buffer 2555 * @src: data being copied 2556 * @n: length of mode page 2557 * @changeable: whether changeable parameters are requested 2558 * 2559 * Generate a generic MODE SENSE page for either current or changeable 2560 * parameters. 2561 * 2562 * LOCKING: 2563 * None. 2564 */ 2565 static void modecpy(u8 *dest, const u8 *src, int n, bool changeable) 2566 { 2567 if (changeable) { 2568 memcpy(dest, src, 2); 2569 memset(dest + 2, 0, n - 2); 2570 } else { 2571 memcpy(dest, src, n); 2572 } 2573 } 2574 2575 /** 2576 * ata_msense_caching - Simulate MODE SENSE caching info page 2577 * @id: device IDENTIFY data 2578 * @buf: output buffer 2579 * @changeable: whether changeable parameters are requested 2580 * 2581 * Generate a caching info page, which conditionally indicates 2582 * write caching to the SCSI layer, depending on device 2583 * capabilities. 2584 * 2585 * LOCKING: 2586 * None. 2587 */ 2588 static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable) 2589 { 2590 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable); 2591 if (changeable) { 2592 buf[2] |= (1 << 2); /* ata_mselect_caching() */ 2593 } else { 2594 buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */ 2595 buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */ 2596 } 2597 return sizeof(def_cache_mpage); 2598 } 2599 2600 /* 2601 * Simulate MODE SENSE control mode page, sub-page 0. 2602 */ 2603 static unsigned int ata_msense_control_spg0(struct ata_device *dev, u8 *buf, 2604 bool changeable) 2605 { 2606 modecpy(buf, def_control_mpage, 2607 sizeof(def_control_mpage), changeable); 2608 if (changeable) { 2609 /* ata_mselect_control() */ 2610 buf[2] |= (1 << 2); 2611 } else { 2612 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE); 2613 2614 /* descriptor format sense data */ 2615 buf[2] |= (d_sense << 2); 2616 } 2617 2618 return sizeof(def_control_mpage); 2619 } 2620 2621 /* 2622 * Translate an ATA duration limit in microseconds to a SCSI duration limit 2623 * using the t2cdlunits 0xa (10ms). Since the SCSI duration limits are 2-bytes 2624 * only, take care of overflows. 2625 */ 2626 static inline u16 ata_xlat_cdl_limit(u8 *buf) 2627 { 2628 u32 limit = get_unaligned_le32(buf); 2629 2630 return min_t(u32, limit / 10000, 65535); 2631 } 2632 2633 /* 2634 * Simulate MODE SENSE control mode page, sub-pages 07h and 08h 2635 * (command duration limits T2A and T2B mode pages). 2636 */ 2637 static unsigned int ata_msense_control_spgt2(struct ata_device *dev, u8 *buf, 2638 u8 spg) 2639 { 2640 u8 *b, *cdl, *desc; 2641 u32 policy; 2642 int i; 2643 2644 if (!(dev->flags & ATA_DFLAG_CDL) || !dev->cdl) 2645 return 0; 2646 2647 cdl = dev->cdl->desc_log_buf; 2648 2649 /* 2650 * Fill the subpage. The first four bytes of the T2A/T2B mode pages 2651 * are a header. The PAGE LENGTH field is the size of the page 2652 * excluding the header. 2653 */ 2654 buf[0] = CONTROL_MPAGE; 2655 buf[1] = spg; 2656 put_unaligned_be16(CDL_T2_SUB_MPAGE_LEN - 4, &buf[2]); 2657 if (spg == CDL_T2A_SUB_MPAGE) { 2658 /* 2659 * Read descriptors map to the T2A page: 2660 * set perf_vs_duration_guidleine. 2661 */ 2662 buf[7] = (cdl[0] & 0x03) << 4; 2663 desc = cdl + 64; 2664 } else { 2665 /* Write descriptors map to the T2B page */ 2666 desc = cdl + 288; 2667 } 2668 2669 /* Fill the T2 page descriptors */ 2670 b = &buf[8]; 2671 policy = get_unaligned_le32(&cdl[0]); 2672 for (i = 0; i < 7; i++, b += 32, desc += 32) { 2673 /* t2cdlunits: fixed to 10ms */ 2674 b[0] = 0x0a; 2675 2676 /* Max inactive time and its policy */ 2677 put_unaligned_be16(ata_xlat_cdl_limit(&desc[8]), &b[2]); 2678 b[6] = ((policy >> 8) & 0x0f) << 4; 2679 2680 /* Max active time and its policy */ 2681 put_unaligned_be16(ata_xlat_cdl_limit(&desc[4]), &b[4]); 2682 b[6] |= (policy >> 4) & 0x0f; 2683 2684 /* Command duration guideline and its policy */ 2685 put_unaligned_be16(ata_xlat_cdl_limit(&desc[16]), &b[10]); 2686 b[14] = policy & 0x0f; 2687 } 2688 2689 return CDL_T2_SUB_MPAGE_LEN; 2690 } 2691 2692 /* 2693 * Simulate MODE SENSE control mode page, sub-page f2h 2694 * (ATA feature control mode page). 2695 */ 2696 static unsigned int ata_msense_control_ata_feature(struct ata_device *dev, 2697 u8 *buf) 2698 { 2699 /* PS=0, SPF=1 */ 2700 buf[0] = CONTROL_MPAGE | (1 << 6); 2701 buf[1] = ATA_FEATURE_SUB_MPAGE; 2702 2703 /* 2704 * The first four bytes of ATA Feature Control mode page are a header. 2705 * The PAGE LENGTH field is the size of the page excluding the header. 2706 */ 2707 put_unaligned_be16(ATA_FEATURE_SUB_MPAGE_LEN - 4, &buf[2]); 2708 2709 if (dev->flags & ATA_DFLAG_CDL_ENABLED) 2710 buf[4] = 0x02; /* T2A and T2B pages enabled */ 2711 else 2712 buf[4] = 0; 2713 2714 return ATA_FEATURE_SUB_MPAGE_LEN; 2715 } 2716 2717 /** 2718 * ata_msense_control - Simulate MODE SENSE control mode page 2719 * @dev: ATA device of interest 2720 * @buf: output buffer 2721 * @spg: sub-page code 2722 * @changeable: whether changeable parameters are requested 2723 * 2724 * Generate a generic MODE SENSE control mode page. 2725 * 2726 * LOCKING: 2727 * None. 2728 */ 2729 static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf, 2730 u8 spg, bool changeable) 2731 { 2732 unsigned int n; 2733 2734 switch (spg) { 2735 case 0: 2736 return ata_msense_control_spg0(dev, buf, changeable); 2737 case CDL_T2A_SUB_MPAGE: 2738 case CDL_T2B_SUB_MPAGE: 2739 return ata_msense_control_spgt2(dev, buf, spg); 2740 case ATA_FEATURE_SUB_MPAGE: 2741 return ata_msense_control_ata_feature(dev, buf); 2742 case ALL_SUB_MPAGES: 2743 n = ata_msense_control_spg0(dev, buf, changeable); 2744 n += ata_msense_control_spgt2(dev, buf + n, CDL_T2A_SUB_MPAGE); 2745 n += ata_msense_control_spgt2(dev, buf + n, CDL_T2B_SUB_MPAGE); 2746 n += ata_msense_control_ata_feature(dev, buf + n); 2747 return n; 2748 default: 2749 return 0; 2750 } 2751 } 2752 2753 /** 2754 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page 2755 * @buf: output buffer 2756 * @changeable: whether changeable parameters are requested 2757 * 2758 * Generate a generic MODE SENSE r/w error recovery page. 2759 * 2760 * LOCKING: 2761 * None. 2762 */ 2763 static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable) 2764 { 2765 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage), 2766 changeable); 2767 return sizeof(def_rw_recovery_mpage); 2768 } 2769 2770 /** 2771 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands 2772 * @dev: Target device. 2773 * @cmd: SCSI command of interest. 2774 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2775 * 2776 * Simulate MODE SENSE commands. Assume this is invoked for direct 2777 * access devices (e.g. disks) only. There should be no block 2778 * descriptor for other device types. 2779 * 2780 * LOCKING: 2781 * spin_lock_irqsave(host lock) 2782 */ 2783 static unsigned int ata_scsiop_mode_sense(struct ata_device *dev, 2784 struct scsi_cmnd *cmd, u8 *rbuf) 2785 { 2786 u8 *scsicmd = cmd->cmnd, *p = rbuf; 2787 static const u8 sat_blk_desc[] = { 2788 0, 0, 0, 0, /* number of blocks: sat unspecified */ 2789 0, 2790 0, 0x2, 0x0 /* block length: 512 bytes */ 2791 }; 2792 u8 pg, spg; 2793 unsigned int ebd, page_control, six_byte; 2794 u8 dpofua = 0, bp = 0xff; 2795 u16 fp; 2796 2797 six_byte = (scsicmd[0] == MODE_SENSE); 2798 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */ 2799 /* 2800 * LLBA bit in msense(10) ignored (compliant) 2801 */ 2802 2803 page_control = scsicmd[2] >> 6; 2804 switch (page_control) { 2805 case 0: /* current */ 2806 case 1: /* changeable */ 2807 case 2: /* defaults */ 2808 break; /* supported */ 2809 case 3: /* saved */ 2810 goto saving_not_supp; 2811 default: 2812 fp = 2; 2813 bp = 6; 2814 goto invalid_fld; 2815 } 2816 2817 if (six_byte) 2818 p += 4 + (ebd ? 8 : 0); 2819 else 2820 p += 8 + (ebd ? 8 : 0); 2821 2822 pg = scsicmd[2] & 0x3f; 2823 spg = scsicmd[3]; 2824 2825 /* 2826 * Supported subpages: all subpages and sub-pages 07h, 08h and f2h of 2827 * the control page. 2828 */ 2829 if (spg) { 2830 switch (spg) { 2831 case ALL_SUB_MPAGES: 2832 break; 2833 case CDL_T2A_SUB_MPAGE: 2834 case CDL_T2B_SUB_MPAGE: 2835 case ATA_FEATURE_SUB_MPAGE: 2836 if (dev->flags & ATA_DFLAG_CDL && pg == CONTROL_MPAGE) 2837 break; 2838 fallthrough; 2839 default: 2840 fp = 3; 2841 goto invalid_fld; 2842 } 2843 } 2844 2845 switch(pg) { 2846 case RW_RECOVERY_MPAGE: 2847 p += ata_msense_rw_recovery(p, page_control == 1); 2848 break; 2849 2850 case CACHE_MPAGE: 2851 p += ata_msense_caching(dev->id, p, page_control == 1); 2852 break; 2853 2854 case CONTROL_MPAGE: 2855 p += ata_msense_control(dev, p, spg, page_control == 1); 2856 break; 2857 2858 case ALL_MPAGES: 2859 p += ata_msense_rw_recovery(p, page_control == 1); 2860 p += ata_msense_caching(dev->id, p, page_control == 1); 2861 p += ata_msense_control(dev, p, spg, page_control == 1); 2862 break; 2863 2864 default: /* invalid page code */ 2865 fp = 2; 2866 goto invalid_fld; 2867 } 2868 2869 if (dev->flags & ATA_DFLAG_FUA) 2870 dpofua = 1 << 4; 2871 2872 if (six_byte) { 2873 rbuf[0] = p - rbuf - 1; 2874 rbuf[2] |= dpofua; 2875 if (ebd) { 2876 rbuf[3] = sizeof(sat_blk_desc); 2877 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc)); 2878 } 2879 2880 return rbuf[0] + 1; 2881 } 2882 2883 put_unaligned_be16(p - rbuf - 2, &rbuf[0]); 2884 rbuf[3] |= dpofua; 2885 if (ebd) { 2886 rbuf[7] = sizeof(sat_blk_desc); 2887 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc)); 2888 } 2889 2890 return get_unaligned_be16(&rbuf[0]) + 2; 2891 2892 invalid_fld: 2893 ata_scsi_set_invalid_field(dev, cmd, fp, bp); 2894 return 0; 2895 2896 saving_not_supp: 2897 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x39, 0x0); 2898 /* "Saving parameters not supported" */ 2899 return 0; 2900 } 2901 2902 /** 2903 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands 2904 * @dev: Target device. 2905 * @cmd: SCSI command of interest. 2906 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2907 * 2908 * Simulate READ CAPACITY commands. 2909 * 2910 * LOCKING: 2911 * None. 2912 */ 2913 static unsigned int ata_scsiop_read_cap(struct ata_device *dev, 2914 struct scsi_cmnd *cmd, u8 *rbuf) 2915 { 2916 u8 *scsicmd = cmd->cmnd; 2917 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */ 2918 u32 sector_size; /* physical sector size in bytes */ 2919 u8 log2_per_phys; 2920 u16 lowest_aligned; 2921 2922 sector_size = ata_id_logical_sector_size(dev->id); 2923 log2_per_phys = ata_id_log2_per_physical_sector(dev->id); 2924 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys); 2925 2926 if (scsicmd[0] == READ_CAPACITY) { 2927 if (last_lba >= 0xffffffffULL) 2928 last_lba = 0xffffffff; 2929 2930 /* sector count, 32-bit */ 2931 rbuf[0] = last_lba >> (8 * 3); 2932 rbuf[1] = last_lba >> (8 * 2); 2933 rbuf[2] = last_lba >> (8 * 1); 2934 rbuf[3] = last_lba; 2935 2936 /* sector size */ 2937 rbuf[4] = sector_size >> (8 * 3); 2938 rbuf[5] = sector_size >> (8 * 2); 2939 rbuf[6] = sector_size >> (8 * 1); 2940 rbuf[7] = sector_size; 2941 2942 return 8; 2943 } 2944 2945 /* 2946 * READ CAPACITY 16 command is defined as a service action 2947 * (SERVICE_ACTION_IN_16 command). 2948 */ 2949 if (scsicmd[0] != SERVICE_ACTION_IN_16 || 2950 (scsicmd[1] & 0x1f) != SAI_READ_CAPACITY_16) { 2951 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff); 2952 return 0; 2953 } 2954 2955 /* sector count, 64-bit */ 2956 rbuf[0] = last_lba >> (8 * 7); 2957 rbuf[1] = last_lba >> (8 * 6); 2958 rbuf[2] = last_lba >> (8 * 5); 2959 rbuf[3] = last_lba >> (8 * 4); 2960 rbuf[4] = last_lba >> (8 * 3); 2961 rbuf[5] = last_lba >> (8 * 2); 2962 rbuf[6] = last_lba >> (8 * 1); 2963 rbuf[7] = last_lba; 2964 2965 /* sector size */ 2966 rbuf[ 8] = sector_size >> (8 * 3); 2967 rbuf[ 9] = sector_size >> (8 * 2); 2968 rbuf[10] = sector_size >> (8 * 1); 2969 rbuf[11] = sector_size; 2970 2971 if (ata_dev_is_zoned(dev)) 2972 rbuf[12] = (1 << 4); /* RC_BASIS */ 2973 rbuf[13] = log2_per_phys; 2974 rbuf[14] = (lowest_aligned >> 8) & 0x3f; 2975 rbuf[15] = lowest_aligned; 2976 2977 if (ata_id_has_trim(dev->id) && !(dev->quirks & ATA_QUIRK_NOTRIM)) { 2978 rbuf[14] |= 0x80; /* LBPME */ 2979 2980 if (ata_id_has_zero_after_trim(dev->id) && 2981 dev->quirks & ATA_QUIRK_ZERO_AFTER_TRIM) { 2982 ata_dev_info(dev, "Enabling discard_zeroes_data\n"); 2983 rbuf[14] |= 0x40; /* LBPRZ */ 2984 } 2985 } 2986 2987 return 16; 2988 } 2989 2990 /** 2991 * ata_scsiop_report_luns - Simulate REPORT LUNS command 2992 * @dev: Target device. 2993 * @cmd: SCSI command of interest. 2994 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2995 * 2996 * Simulate REPORT LUNS command. 2997 * 2998 * LOCKING: 2999 * spin_lock_irqsave(host lock) 3000 */ 3001 static unsigned int ata_scsiop_report_luns(struct ata_device *dev, 3002 struct scsi_cmnd *cmd, u8 *rbuf) 3003 { 3004 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */ 3005 3006 return 16; 3007 } 3008 3009 /* 3010 * ATAPI devices typically report zero for their SCSI version, and sometimes 3011 * deviate from the spec WRT response data format. If SCSI version is 3012 * reported as zero like normal, then we make the following fixups: 3013 * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a 3014 * modern device. 3015 * 2) Ensure response data format / ATAPI information are always correct. 3016 */ 3017 static void atapi_fixup_inquiry(struct scsi_cmnd *cmd) 3018 { 3019 u8 buf[4]; 3020 3021 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4); 3022 if (buf[2] == 0) { 3023 buf[2] = 0x5; 3024 buf[3] = 0x32; 3025 } 3026 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4); 3027 } 3028 3029 static void atapi_qc_complete(struct ata_queued_cmd *qc) 3030 { 3031 struct ata_link *link = qc->dev->link; 3032 struct scsi_cmnd *cmd = qc->scsicmd; 3033 unsigned int err_mask = qc->err_mask; 3034 3035 /* handle completion from EH */ 3036 if (unlikely(err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID)) { 3037 3038 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) 3039 ata_gen_passthru_sense(qc); 3040 3041 /* SCSI EH automatically locks door if sdev->locked is 3042 * set. Sometimes door lock request continues to 3043 * fail, for example, when no media is present. This 3044 * creates a loop - SCSI EH issues door lock which 3045 * fails and gets invoked again to acquire sense data 3046 * for the failed command. 3047 * 3048 * If door lock fails, always clear sdev->locked to 3049 * avoid this infinite loop. 3050 * 3051 * This may happen before SCSI scan is complete. Make 3052 * sure qc->dev->sdev isn't NULL before dereferencing. 3053 */ 3054 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev) 3055 qc->dev->sdev->locked = 0; 3056 3057 if (cmd->result) 3058 ata_scsi_qc_done(qc, false, 0); 3059 else 3060 ata_scsi_qc_done(qc, true, SAM_STAT_CHECK_CONDITION); 3061 goto schedule_deferred; 3062 } 3063 3064 if (cmd->result) { 3065 ata_scsi_qc_done(qc, false, 0); 3066 goto schedule_deferred; 3067 } 3068 3069 /* successful completion path */ 3070 if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0) 3071 atapi_fixup_inquiry(cmd); 3072 3073 ata_scsi_qc_done(qc, true, SAM_STAT_GOOD); 3074 3075 schedule_deferred: 3076 ata_scsi_schedule_deferred_qc(link); 3077 } 3078 /** 3079 * atapi_xlat - Initialize PACKET taskfile 3080 * @qc: command structure to be initialized 3081 * 3082 * LOCKING: 3083 * spin_lock_irqsave(host lock) 3084 * 3085 * RETURNS: 3086 * Zero on success, non-zero on failure. 3087 */ 3088 static unsigned int atapi_xlat(struct ata_queued_cmd *qc) 3089 { 3090 struct scsi_cmnd *scmd = qc->scsicmd; 3091 struct ata_device *dev = qc->dev; 3092 int nodata = (scmd->sc_data_direction == DMA_NONE); 3093 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO); 3094 unsigned int nbytes; 3095 3096 memset(qc->cdb, 0, dev->cdb_len); 3097 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len); 3098 3099 qc->complete_fn = atapi_qc_complete; 3100 3101 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 3102 if (scmd->sc_data_direction == DMA_TO_DEVICE) { 3103 qc->tf.flags |= ATA_TFLAG_WRITE; 3104 } 3105 3106 qc->tf.command = ATA_CMD_PACKET; 3107 ata_qc_set_pc_nbytes(qc); 3108 3109 /* check whether ATAPI DMA is safe */ 3110 if (!nodata && !using_pio && atapi_check_dma(qc)) 3111 using_pio = 1; 3112 3113 /* Some controller variants snoop this value for Packet 3114 * transfers to do state machine and FIFO management. Thus we 3115 * want to set it properly, and for DMA where it is 3116 * effectively meaningless. 3117 */ 3118 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024); 3119 3120 /* Most ATAPI devices which honor transfer chunk size don't 3121 * behave according to the spec when odd chunk size which 3122 * matches the transfer length is specified. If the number of 3123 * bytes to transfer is 2n+1. According to the spec, what 3124 * should happen is to indicate that 2n+1 is going to be 3125 * transferred and transfer 2n+2 bytes where the last byte is 3126 * padding. 3127 * 3128 * In practice, this doesn't happen. ATAPI devices first 3129 * indicate and transfer 2n bytes and then indicate and 3130 * transfer 2 bytes where the last byte is padding. 3131 * 3132 * This inconsistency confuses several controllers which 3133 * perform PIO using DMA such as Intel AHCIs and sil3124/32. 3134 * These controllers use actual number of transferred bytes to 3135 * update DMA pointer and transfer of 4n+2 bytes make those 3136 * controller push DMA pointer by 4n+4 bytes because SATA data 3137 * FISes are aligned to 4 bytes. This causes data corruption 3138 * and buffer overrun. 3139 * 3140 * Always setting nbytes to even number solves this problem 3141 * because then ATAPI devices don't have to split data at 2n 3142 * boundaries. 3143 */ 3144 if (nbytes & 0x1) 3145 nbytes++; 3146 3147 qc->tf.lbam = (nbytes & 0xFF); 3148 qc->tf.lbah = (nbytes >> 8); 3149 3150 if (nodata) 3151 qc->tf.protocol = ATAPI_PROT_NODATA; 3152 else if (using_pio) 3153 qc->tf.protocol = ATAPI_PROT_PIO; 3154 else { 3155 /* DMA data xfer */ 3156 qc->tf.protocol = ATAPI_PROT_DMA; 3157 qc->tf.feature |= ATAPI_PKT_DMA; 3158 3159 if ((dev->flags & ATA_DFLAG_DMADIR) && 3160 (scmd->sc_data_direction != DMA_TO_DEVICE)) 3161 /* some SATA bridges need us to indicate data xfer direction */ 3162 qc->tf.feature |= ATAPI_DMADIR; 3163 } 3164 3165 3166 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE 3167 as ATAPI tape drives don't get this right otherwise */ 3168 return 0; 3169 } 3170 3171 static struct ata_device *ata_find_dev(struct ata_port *ap, unsigned int devno) 3172 { 3173 /* 3174 * For the non-PMP case, ata_link_max_devices() returns 1 (SATA case), 3175 * or 2 (IDE master + slave case). However, the former case includes 3176 * libsas hosted devices which are numbered per scsi host, leading 3177 * to devno potentially being larger than 0 but with each struct 3178 * ata_device having its own struct ata_port and struct ata_link. 3179 * To accommodate these, ignore devno and always use device number 0. 3180 */ 3181 if (likely(!sata_pmp_attached(ap))) { 3182 int link_max_devices = ata_link_max_devices(&ap->link); 3183 3184 if (link_max_devices == 1) 3185 return &ap->link.device[0]; 3186 3187 if (devno < link_max_devices) 3188 return &ap->link.device[devno]; 3189 3190 return NULL; 3191 } 3192 3193 /* 3194 * For PMP-attached devices, the device number corresponds to C 3195 * (channel) of SCSI [H:C:I:L], indicating the port pmp link 3196 * for the device. 3197 */ 3198 if (devno < ap->nr_pmp_links) 3199 return &ap->pmp_link[devno].device[0]; 3200 3201 return NULL; 3202 } 3203 3204 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap, 3205 const struct scsi_device *scsidev) 3206 { 3207 int devno; 3208 3209 /* skip commands not addressed to targets we simulate */ 3210 if (!sata_pmp_attached(ap)) { 3211 if (unlikely(scsidev->channel || scsidev->lun)) 3212 return NULL; 3213 devno = scsidev->id; 3214 } else { 3215 if (unlikely(scsidev->id || scsidev->lun)) 3216 return NULL; 3217 devno = scsidev->channel; 3218 } 3219 3220 return ata_find_dev(ap, devno); 3221 } 3222 3223 /** 3224 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd 3225 * @ap: ATA port to which the device is attached 3226 * @scsidev: SCSI device from which we derive the ATA device 3227 * 3228 * Given various information provided in struct scsi_cmnd, 3229 * map that onto an ATA bus, and using that mapping 3230 * determine which ata_device is associated with the 3231 * SCSI command to be sent. 3232 * 3233 * LOCKING: 3234 * spin_lock_irqsave(host lock) 3235 * 3236 * RETURNS: 3237 * Associated ATA device, or %NULL if not found. 3238 */ 3239 struct ata_device * 3240 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev) 3241 { 3242 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev); 3243 3244 if (!ata_adapter_is_online(ap)) 3245 return NULL; 3246 3247 if (unlikely(!dev || !ata_dev_enabled(dev))) 3248 return NULL; 3249 3250 return dev; 3251 } 3252 3253 /* 3254 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value. 3255 * @byte1: Byte 1 from pass-thru CDB. 3256 * 3257 * RETURNS: 3258 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise. 3259 */ 3260 static u8 3261 ata_scsi_map_proto(u8 byte1) 3262 { 3263 switch((byte1 & 0x1e) >> 1) { 3264 case 3: /* Non-data */ 3265 return ATA_PROT_NODATA; 3266 3267 case 6: /* DMA */ 3268 case 10: /* UDMA Data-in */ 3269 case 11: /* UDMA Data-Out */ 3270 return ATA_PROT_DMA; 3271 3272 case 4: /* PIO Data-in */ 3273 case 5: /* PIO Data-out */ 3274 return ATA_PROT_PIO; 3275 3276 case 12: /* FPDMA */ 3277 return ATA_PROT_NCQ; 3278 3279 case 0: /* Hard Reset */ 3280 case 1: /* SRST */ 3281 case 8: /* Device Diagnostic */ 3282 case 9: /* Device Reset */ 3283 case 7: /* DMA Queued */ 3284 case 15: /* Return Response Info */ 3285 default: /* Reserved */ 3286 break; 3287 } 3288 3289 return ATA_PROT_UNKNOWN; 3290 } 3291 3292 /** 3293 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile 3294 * @qc: command structure to be initialized 3295 * 3296 * Handles either 12, 16, or 32-byte versions of the CDB. 3297 * 3298 * RETURNS: 3299 * Zero on success, non-zero on failure. 3300 */ 3301 static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc) 3302 { 3303 struct ata_taskfile *tf = &(qc->tf); 3304 struct scsi_cmnd *scmd = qc->scsicmd; 3305 struct ata_device *dev = qc->dev; 3306 const u8 *cdb = scmd->cmnd; 3307 u16 fp; 3308 u16 cdb_offset = 0; 3309 3310 /* 7Fh variable length cmd means a ata pass-thru(32) */ 3311 if (cdb[0] == VARIABLE_LENGTH_CMD) 3312 cdb_offset = 9; 3313 3314 tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]); 3315 if (tf->protocol == ATA_PROT_UNKNOWN) { 3316 fp = 1; 3317 goto invalid_fld; 3318 } 3319 3320 if ((cdb[2 + cdb_offset] & 0x3) == 0) { 3321 /* 3322 * When T_LENGTH is zero (No data is transferred), dir should 3323 * be DMA_NONE. 3324 */ 3325 if (scmd->sc_data_direction != DMA_NONE) { 3326 fp = 2 + cdb_offset; 3327 goto invalid_fld; 3328 } 3329 3330 if (ata_is_ncq(tf->protocol)) 3331 tf->protocol = ATA_PROT_NCQ_NODATA; 3332 } 3333 3334 /* enable LBA */ 3335 tf->flags |= ATA_TFLAG_LBA; 3336 3337 /* 3338 * 12 and 16 byte CDBs use different offsets to 3339 * provide the various register values. 3340 */ 3341 switch (cdb[0]) { 3342 case ATA_16: 3343 /* 3344 * 16-byte CDB - may contain extended commands. 3345 * 3346 * If that is the case, copy the upper byte register values. 3347 */ 3348 if (cdb[1] & 0x01) { 3349 tf->hob_feature = cdb[3]; 3350 tf->hob_nsect = cdb[5]; 3351 tf->hob_lbal = cdb[7]; 3352 tf->hob_lbam = cdb[9]; 3353 tf->hob_lbah = cdb[11]; 3354 tf->flags |= ATA_TFLAG_LBA48; 3355 } else 3356 tf->flags &= ~ATA_TFLAG_LBA48; 3357 3358 /* 3359 * Always copy low byte, device and command registers. 3360 */ 3361 tf->feature = cdb[4]; 3362 tf->nsect = cdb[6]; 3363 tf->lbal = cdb[8]; 3364 tf->lbam = cdb[10]; 3365 tf->lbah = cdb[12]; 3366 tf->device = cdb[13]; 3367 tf->command = cdb[14]; 3368 break; 3369 case ATA_12: 3370 /* 3371 * 12-byte CDB - incapable of extended commands. 3372 */ 3373 tf->flags &= ~ATA_TFLAG_LBA48; 3374 3375 tf->feature = cdb[3]; 3376 tf->nsect = cdb[4]; 3377 tf->lbal = cdb[5]; 3378 tf->lbam = cdb[6]; 3379 tf->lbah = cdb[7]; 3380 tf->device = cdb[8]; 3381 tf->command = cdb[9]; 3382 break; 3383 default: 3384 /* 3385 * 32-byte CDB - may contain extended command fields. 3386 * 3387 * If that is the case, copy the upper byte register values. 3388 */ 3389 if (cdb[10] & 0x01) { 3390 tf->hob_feature = cdb[20]; 3391 tf->hob_nsect = cdb[22]; 3392 tf->hob_lbal = cdb[16]; 3393 tf->hob_lbam = cdb[15]; 3394 tf->hob_lbah = cdb[14]; 3395 tf->flags |= ATA_TFLAG_LBA48; 3396 } else 3397 tf->flags &= ~ATA_TFLAG_LBA48; 3398 3399 tf->feature = cdb[21]; 3400 tf->nsect = cdb[23]; 3401 tf->lbal = cdb[19]; 3402 tf->lbam = cdb[18]; 3403 tf->lbah = cdb[17]; 3404 tf->device = cdb[24]; 3405 tf->command = cdb[25]; 3406 tf->auxiliary = get_unaligned_be32(&cdb[28]); 3407 break; 3408 } 3409 3410 /* For NCQ commands copy the tag value */ 3411 if (ata_is_ncq(tf->protocol)) 3412 tf->nsect = qc->hw_tag << 3; 3413 3414 /* enforce correct master/slave bit */ 3415 tf->device = dev->devno ? 3416 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1; 3417 3418 switch (tf->command) { 3419 /* READ/WRITE LONG use a non-standard sect_size */ 3420 case ATA_CMD_READ_LONG: 3421 case ATA_CMD_READ_LONG_ONCE: 3422 case ATA_CMD_WRITE_LONG: 3423 case ATA_CMD_WRITE_LONG_ONCE: 3424 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) { 3425 fp = 1; 3426 goto invalid_fld; 3427 } 3428 qc->sect_size = scsi_bufflen(scmd); 3429 break; 3430 3431 /* commands using reported Logical Block size (e.g. 512 or 4K) */ 3432 case ATA_CMD_CFA_WRITE_NE: 3433 case ATA_CMD_CFA_TRANS_SECT: 3434 case ATA_CMD_CFA_WRITE_MULT_NE: 3435 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */ 3436 case ATA_CMD_READ: 3437 case ATA_CMD_READ_EXT: 3438 case ATA_CMD_READ_QUEUED: 3439 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */ 3440 case ATA_CMD_FPDMA_READ: 3441 case ATA_CMD_READ_MULTI: 3442 case ATA_CMD_READ_MULTI_EXT: 3443 case ATA_CMD_PIO_READ: 3444 case ATA_CMD_PIO_READ_EXT: 3445 case ATA_CMD_READ_STREAM_DMA_EXT: 3446 case ATA_CMD_READ_STREAM_EXT: 3447 case ATA_CMD_VERIFY: 3448 case ATA_CMD_VERIFY_EXT: 3449 case ATA_CMD_WRITE: 3450 case ATA_CMD_WRITE_EXT: 3451 case ATA_CMD_WRITE_FUA_EXT: 3452 case ATA_CMD_WRITE_QUEUED: 3453 case ATA_CMD_WRITE_QUEUED_FUA_EXT: 3454 case ATA_CMD_FPDMA_WRITE: 3455 case ATA_CMD_WRITE_MULTI: 3456 case ATA_CMD_WRITE_MULTI_EXT: 3457 case ATA_CMD_WRITE_MULTI_FUA_EXT: 3458 case ATA_CMD_PIO_WRITE: 3459 case ATA_CMD_PIO_WRITE_EXT: 3460 case ATA_CMD_WRITE_STREAM_DMA_EXT: 3461 case ATA_CMD_WRITE_STREAM_EXT: 3462 qc->sect_size = scmd->device->sector_size; 3463 break; 3464 3465 /* Everything else uses 512 byte "sectors" */ 3466 default: 3467 qc->sect_size = ATA_SECT_SIZE; 3468 } 3469 3470 /* 3471 * Set flags so that all registers will be written, pass on 3472 * write indication (used for PIO/DMA setup), result TF is 3473 * copied back and we don't whine too much about its failure. 3474 */ 3475 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 3476 if (scmd->sc_data_direction == DMA_TO_DEVICE) 3477 tf->flags |= ATA_TFLAG_WRITE; 3478 3479 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET; 3480 3481 /* 3482 * Set transfer length. 3483 * 3484 * TODO: find out if we need to do more here to 3485 * cover scatter/gather case. 3486 */ 3487 ata_qc_set_pc_nbytes(qc); 3488 3489 /* We may not issue DMA commands if no DMA mode is set */ 3490 if (tf->protocol == ATA_PROT_DMA && !ata_dma_enabled(dev)) { 3491 fp = 1; 3492 goto invalid_fld; 3493 } 3494 3495 /* We may not issue NCQ commands to devices not supporting NCQ */ 3496 if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) { 3497 fp = 1; 3498 goto invalid_fld; 3499 } 3500 3501 /* sanity check for pio multi commands */ 3502 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) { 3503 fp = 1; 3504 goto invalid_fld; 3505 } 3506 3507 if (is_multi_taskfile(tf)) { 3508 unsigned int multi_count = 1 << (cdb[1] >> 5); 3509 3510 /* compare the passed through multi_count 3511 * with the cached multi_count of libata 3512 */ 3513 if (multi_count != dev->multi_count) 3514 ata_dev_warn(dev, "invalid multi_count %u ignored\n", 3515 multi_count); 3516 } 3517 3518 /* 3519 * Filter SET_FEATURES - XFER MODE command -- otherwise, 3520 * SET_FEATURES - XFER MODE must be preceded/succeeded 3521 * by an update to hardware-specific registers for each 3522 * controller (i.e. the reason for ->set_piomode(), 3523 * ->set_dmamode(), and ->post_set_mode() hooks). 3524 */ 3525 if (tf->command == ATA_CMD_SET_FEATURES && 3526 tf->feature == SETFEATURES_XFER) { 3527 fp = (cdb[0] == ATA_16) ? 4 : 3; 3528 goto invalid_fld; 3529 } 3530 3531 /* 3532 * Filter TPM commands by default. These provide an 3533 * essentially uncontrolled encrypted "back door" between 3534 * applications and the disk. Set libata.allow_tpm=1 if you 3535 * have a real reason for wanting to use them. This ensures 3536 * that installed software cannot easily mess stuff up without 3537 * user intent. DVR type users will probably ship with this enabled 3538 * for movie content management. 3539 * 3540 * Note that for ATA8 we can issue a DCS change and DCS freeze lock 3541 * for this and should do in future but that it is not sufficient as 3542 * DCS is an optional feature set. Thus we also do the software filter 3543 * so that we comply with the TC consortium stated goal that the user 3544 * can turn off TC features of their system. 3545 */ 3546 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) { 3547 fp = (cdb[0] == ATA_16) ? 14 : 9; 3548 goto invalid_fld; 3549 } 3550 3551 return 0; 3552 3553 invalid_fld: 3554 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff); 3555 return 1; 3556 } 3557 3558 /** 3559 * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim 3560 * @cmd: SCSI command being translated 3561 * @size: DSM TRIM payload size in bytes (a multiple of 512) 3562 * @sector: Starting sector 3563 * @count: Total Range of request in logical sectors 3564 * 3565 * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted 3566 * descriptor. 3567 * 3568 * The payload is a list of @size / 8 entries of the format: 3569 * 63:48 Range Length 3570 * 47:0 LBA 3571 * 3572 * Range Length of 0 is ignored. 3573 * LBA's should be sorted order and not overlap. 3574 * 3575 * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET 3576 * 3577 * The descriptor is written straight into the WRITE SAME data-out buffer; 3578 * ata_dsm_trim_pages() guarantees @size does not exceed that buffer (one 3579 * logical block). An atomic sg_miter mapping is used so this works from the 3580 * command submission path and, unlike page_address(), copes with a high 3581 * memory payload. 3582 * 3583 * Return: Number of bytes written into the data-out buffer. 3584 */ 3585 static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, size_t size, 3586 u64 sector, u32 count) 3587 { 3588 struct sg_mapping_iter miter; 3589 size_t offset = 0; 3590 3591 sg_miter_start(&miter, scsi_sglist(cmd), scsi_sg_count(cmd), 3592 SG_MITER_TO_SG | SG_MITER_ATOMIC); 3593 while (offset < size && sg_miter_next(&miter)) { 3594 __le64 *buf = miter.addr; 3595 size_t chunk = min_t(size_t, miter.length, size - offset); 3596 unsigned int n = chunk / sizeof(__le64); 3597 unsigned int i; 3598 3599 for (i = 0; i < n; i++) { 3600 u64 entry = 0; 3601 3602 if (count) { 3603 u32 rlen = min_t(u32, count, 0xffff); 3604 3605 entry = sector | ((u64)rlen << 48); 3606 sector += rlen; 3607 count -= rlen; 3608 } 3609 buf[i] = cpu_to_le64(entry); 3610 } 3611 offset += n * sizeof(__le64); 3612 } 3613 sg_miter_stop(&miter); 3614 3615 return offset; 3616 } 3617 3618 /** 3619 * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same 3620 * @qc: Command to be translated 3621 * 3622 * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or 3623 * an SCT Write Same command. 3624 * Based on WRITE SAME has the UNMAP flag: 3625 * 3626 * - When set translate to DSM TRIM 3627 * - When clear translate to SCT Write Same 3628 */ 3629 static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc) 3630 { 3631 struct ata_taskfile *tf = &qc->tf; 3632 struct scsi_cmnd *scmd = qc->scsicmd; 3633 struct ata_device *dev = qc->dev; 3634 const u8 *cdb = scmd->cmnd; 3635 unsigned int max_pages = ata_dsm_trim_pages(dev); 3636 unsigned int n_pages; 3637 size_t size; 3638 u64 block; 3639 u32 n_block; 3640 u16 fp; 3641 u8 bp = 0xff; 3642 u8 unmap = cdb[1] & 0x8; 3643 3644 /* we may not issue DMA commands if no DMA mode is set */ 3645 if (unlikely(!ata_dma_enabled(dev))) 3646 goto invalid_opcode; 3647 3648 /* 3649 * We only allow sending this command through the block layer, 3650 * as it modifies the DATA OUT buffer, which would corrupt user 3651 * memory for SG_IO commands. 3652 */ 3653 if (unlikely(blk_rq_is_passthrough(scsi_cmd_to_rq(scmd)))) 3654 goto invalid_opcode; 3655 3656 if (unlikely(scmd->cmd_len < 16)) { 3657 fp = 15; 3658 goto invalid_fld; 3659 } 3660 scsi_16_lba_len(cdb, &block, &n_block); 3661 3662 if (!unmap || (dev->quirks & ATA_QUIRK_NOTRIM) || 3663 !ata_id_has_trim(dev->id)) { 3664 fp = 1; 3665 bp = 3; 3666 goto invalid_fld; 3667 } 3668 /* If the request is too large the cmd is invalid */ 3669 if (n_block > max_pages * ATA_MAX_TRIM_RNUM * (u64)U16_MAX) { 3670 fp = 2; 3671 goto invalid_fld; 3672 } 3673 3674 /* 3675 * WRITE SAME always has a sector sized buffer as payload, this 3676 * should never be a multiple entry S/G list. 3677 */ 3678 if (!scsi_sg_count(scmd)) 3679 goto invalid_param_len; 3680 3681 /* 3682 * The DATA SET MANAGEMENT TRIM payload is a whole number of 512-byte 3683 * pages (each holding up to ATA_MAX_TRIM_RNUM LBA Range Entries), 3684 * independent of the logical sector size. Only use as many pages as 3685 * are needed to describe the request, capped at max_pages. 3686 */ 3687 n_pages = DIV_ROUND_UP(DIV_ROUND_UP(n_block, U16_MAX), 3688 ATA_MAX_TRIM_RNUM); 3689 n_pages = clamp(n_pages, 1U, max_pages); 3690 size = (size_t)n_pages * ATA_SECT_SIZE; 3691 3692 if (ata_format_dsm_trim_descr(scmd, size, block, n_block) != size) 3693 goto invalid_param_len; 3694 3695 /* 3696 * For DATA SET MANAGEMENT TRIM the COUNT field (aka nsect) is the 3697 * number of 512-byte pages to be transferred. 3698 */ 3699 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) { 3700 /* Newer devices support queued TRIM commands */ 3701 tf->protocol = ATA_PROT_NCQ; 3702 tf->command = ATA_CMD_FPDMA_SEND; 3703 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f; 3704 tf->nsect = qc->hw_tag << 3; 3705 tf->hob_feature = n_pages >> 8; 3706 tf->feature = n_pages; 3707 3708 tf->auxiliary = 1; 3709 } else { 3710 tf->protocol = ATA_PROT_DMA; 3711 tf->hob_feature = 0; 3712 tf->feature = ATA_DSM_TRIM; 3713 tf->hob_nsect = n_pages >> 8; 3714 tf->nsect = n_pages; 3715 tf->command = ATA_CMD_DSM; 3716 } 3717 3718 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 | 3719 ATA_TFLAG_WRITE; 3720 3721 ata_qc_set_pc_nbytes(qc); 3722 /* 3723 * The DSM TRIM payload (size) may be smaller than the WRITE SAME 3724 * data-out buffer (one logical block); only transfer the pages that 3725 * were actually built so the transfer length matches the COUNT field. 3726 */ 3727 qc->nbytes = size; 3728 3729 return 0; 3730 3731 invalid_fld: 3732 ata_scsi_set_invalid_field(dev, scmd, fp, bp); 3733 return 1; 3734 invalid_param_len: 3735 /* "Parameter list length error" */ 3736 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0); 3737 return 1; 3738 invalid_opcode: 3739 /* "Invalid command operation code" */ 3740 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0); 3741 return 1; 3742 } 3743 3744 struct ata_scsi_cmd { 3745 u8 op; 3746 u8 cdb_len; 3747 bool sa_valid; 3748 u16 sa; 3749 }; 3750 3751 /* 3752 * Array of commands supported with translation or emulation, sorted in 3753 * ascending opcode and service action order. All of these commands are 3754 * processed either in ata_xlat_func() or in ata_scsi_simulate(); 3755 * 3756 * Note: commands that are not fully supported may be left out of this array 3757 * so that they are not reported as supported for passthrough but still 3758 * available through the block layer. For now, this includes the following 3759 * commands: 3760 * - WRITE_SAME_16: ata_scsi_write_same_xlat() forbids passthrough commands 3761 */ 3762 static const struct ata_scsi_cmd ata_supported_cmds[] = { 3763 { .op = TEST_UNIT_READY, .cdb_len = 6 }, 3764 { .op = REZERO_UNIT, .cdb_len = 6 }, 3765 { .op = REQUEST_SENSE, .cdb_len = 6 }, 3766 { .op = READ_6, .cdb_len = 6 }, 3767 { .op = WRITE_6, .cdb_len = 6 }, 3768 { .op = SEEK_6, .cdb_len = 6 }, 3769 { .op = INQUIRY, .cdb_len = 6 }, 3770 { .op = MODE_SELECT, .cdb_len = 6 }, 3771 { .op = MODE_SENSE, .cdb_len = 6 }, 3772 { .op = START_STOP, .cdb_len = 6 }, 3773 { .op = SEND_DIAGNOSTIC, .cdb_len = 6 }, 3774 { .op = READ_CAPACITY, .cdb_len = 10 }, 3775 { .op = READ_10, .cdb_len = 10 }, 3776 { .op = WRITE_10, .cdb_len = 10 }, 3777 { .op = SEEK_10, .cdb_len = 10 }, 3778 { .op = VERIFY, .cdb_len = 10 }, 3779 { .op = SYNCHRONIZE_CACHE, .cdb_len = 10 }, 3780 { .op = MODE_SELECT_10, .cdb_len = 10 }, 3781 { .op = MODE_SENSE_10, .cdb_len = 10 }, 3782 { 3783 .op = VARIABLE_LENGTH_CMD, .cdb_len = 32, 3784 .sa_valid = true, 3785 .sa = ATA_32 3786 }, 3787 { .op = ATA_16, .cdb_len = 16 }, 3788 { .op = READ_16, .cdb_len = 16 }, 3789 { .op = WRITE_16, .cdb_len = 16 }, 3790 { .op = VERIFY_16, .cdb_len = 16 }, 3791 { .op = SYNCHRONIZE_CACHE_16, .cdb_len = 16 }, 3792 { 3793 .op = ZBC_OUT, .cdb_len = 16, 3794 .sa_valid = true, 3795 .sa = ZO_CLOSE_ZONE 3796 }, 3797 { 3798 .op = ZBC_OUT, .cdb_len = 16, 3799 .sa_valid = true, 3800 .sa = ZO_FINISH_ZONE 3801 }, 3802 { 3803 .op = ZBC_OUT, .cdb_len = 16, 3804 .sa_valid = true, 3805 .sa = ZO_OPEN_ZONE 3806 }, 3807 { 3808 .op = ZBC_OUT, .cdb_len = 16, 3809 .sa_valid = true, 3810 .sa = ZO_RESET_WRITE_POINTER 3811 }, 3812 { 3813 .op = ZBC_IN, .cdb_len = 16, 3814 .sa_valid = true, 3815 .sa = ZI_REPORT_ZONES 3816 }, 3817 { 3818 .op = SERVICE_ACTION_IN_16, .cdb_len = 16, 3819 .sa_valid = true, 3820 .sa = SAI_READ_CAPACITY_16 3821 }, 3822 { 3823 .op = SERVICE_ACTION_IN_16, .cdb_len = 16, 3824 .sa_valid = true, 3825 .sa = SAI_GET_PHYSICAL_ELEMENT_STATUS 3826 }, 3827 { 3828 .op = SERVICE_ACTION_IN_16, .cdb_len = 16, 3829 .sa_valid = true, 3830 .sa = SAI_REMOVE_ELEMENT_AND_TRUNCATE 3831 }, 3832 { 3833 .op = SERVICE_ACTION_IN_16, .cdb_len = 16, 3834 .sa_valid = true, 3835 .sa = SAI_RESTORE_ELEMENTS_AND_REBUILD 3836 }, 3837 { 3838 .op = SERVICE_ACTION_IN_16, .cdb_len = 16, 3839 .sa_valid = true, 3840 .sa = SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES 3841 }, 3842 { .op = REPORT_LUNS, .cdb_len = 12 }, 3843 { .op = ATA_12, .cdb_len = 12 }, 3844 { .op = SECURITY_PROTOCOL_IN, .cdb_len = 12 }, 3845 { 3846 .op = MAINTENANCE_IN, .cdb_len = 12, 3847 .sa_valid = true, 3848 .sa = MI_REPORT_SUPPORTED_OPERATION_CODES 3849 }, 3850 { .op = SECURITY_PROTOCOL_OUT, .cdb_len = 12 }, 3851 }; 3852 3853 static const struct ata_scsi_cmd *ata_scsi_get_supported_cmd(u8 op, u16 sa) 3854 { 3855 const struct ata_scsi_cmd *cmd; 3856 int i; 3857 3858 for (i = 0; i < ARRAY_SIZE(ata_supported_cmds); i++) { 3859 cmd = &ata_supported_cmds[i]; 3860 if (cmd->op == op && cmd->sa == sa) 3861 return cmd; 3862 } 3863 3864 return NULL; 3865 } 3866 3867 static bool ata_scsi_supported_cmd_use_sa(u8 op) 3868 { 3869 const struct ata_scsi_cmd *cmd; 3870 int i; 3871 3872 for (i = 0; i < ARRAY_SIZE(ata_supported_cmds); i++) { 3873 cmd = &ata_supported_cmds[i]; 3874 if (cmd->op == op) 3875 return cmd->sa_valid; 3876 } 3877 3878 return false; 3879 } 3880 3881 struct ata_scsi_cmd_support { 3882 u8 cdlp; 3883 u8 rwcdlp; 3884 }; 3885 3886 static bool ata_scsi_cmd_is_supported(struct ata_device *dev, u8 op, u16 sa, 3887 struct ata_scsi_cmd_support *sup) 3888 { 3889 const struct ata_scsi_cmd *cmd; 3890 3891 /* First, see if we support the command. */ 3892 cmd = ata_scsi_get_supported_cmd(op, sa); 3893 if (!cmd) 3894 return false; 3895 3896 /* Now refine the support report depending on the device features. */ 3897 memset(sup, 0, sizeof(*sup)); 3898 switch (op) { 3899 case READ_16: 3900 if (dev->flags & ATA_DFLAG_CDL) { 3901 /* 3902 * CDL read descriptors map to the T2A page, that is, 3903 * rwcdlp = 0x01 and cdlp = 0x01 3904 */ 3905 sup->rwcdlp = 0x01; 3906 sup->cdlp = 0x01; 3907 } 3908 break; 3909 case WRITE_16: 3910 if (dev->flags & ATA_DFLAG_CDL) { 3911 /* 3912 * CDL write descriptors map to the T2B page, that is, 3913 * rwcdlp = 0x01 and cdlp = 0x02 3914 */ 3915 sup->rwcdlp = 0x01; 3916 sup->cdlp = 0x02; 3917 } 3918 break; 3919 case ZBC_IN: 3920 case ZBC_OUT: 3921 return ata_dev_is_zoned(dev); 3922 case SERVICE_ACTION_IN_16: 3923 switch (sa) { 3924 case SAI_GET_PHYSICAL_ELEMENT_STATUS: 3925 case SAI_REMOVE_ELEMENT_AND_TRUNCATE: 3926 return dev->flags & ATA_DFLAG_DEPOP; 3927 case SAI_RESTORE_ELEMENTS_AND_REBUILD: 3928 return dev->flags & ATA_DFLAG_DEPOP_RESTORE; 3929 case SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES: 3930 return dev->flags & ATA_DFLAG_DEPOP_MODIFY; 3931 default: 3932 return true; 3933 } 3934 break; 3935 case SECURITY_PROTOCOL_IN: 3936 case SECURITY_PROTOCOL_OUT: 3937 return dev->flags & ATA_DFLAG_TRUSTED; 3938 default: 3939 break; 3940 } 3941 3942 return true; 3943 } 3944 3945 static unsigned int 3946 ata_scsi_report_all_supported_opcodes(struct ata_device *dev, u8 *rbuf) 3947 { 3948 struct ata_scsi_cmd_support sup; 3949 const struct ata_scsi_cmd *cmd; 3950 unsigned int len = 4; 3951 u8 *buf = &rbuf[len]; 3952 int i; 3953 3954 for (i = 0; i < ARRAY_SIZE(ata_supported_cmds); i++) { 3955 if (len > ATA_SCSI_RBUF_SIZE - 8) 3956 break; 3957 3958 cmd = &ata_supported_cmds[i]; 3959 3960 /* All command format */ 3961 if (!ata_scsi_cmd_is_supported(dev, cmd->op, cmd->sa, &sup)) 3962 continue; 3963 3964 buf[0] = cmd->op; 3965 put_unaligned_be16(cmd->sa, &buf[2]); 3966 buf[5] = (sup.rwcdlp << 6) | (sup.cdlp << 2); 3967 if (cmd->sa_valid) 3968 buf[5] |= 0x01; 3969 put_unaligned_be16(cmd->cdb_len, &buf[6]); 3970 3971 /* CTDP == 0 */ 3972 len += 8; 3973 buf += 8; 3974 } 3975 3976 put_unaligned_be32(len - 4, &rbuf[0]); 3977 3978 return len; 3979 } 3980 3981 static unsigned int ata_scsi_report_supported_opcodes(struct ata_device *dev, 3982 struct scsi_cmnd *cmd, 3983 u8 *rbuf) 3984 { 3985 struct ata_scsi_cmd_support sup; 3986 u8 *cdb = cmd->cmnd; 3987 u16 sa = 0; 3988 3989 switch (cdb[2]) { 3990 case 0: 3991 /* All command format */ 3992 return ata_scsi_report_all_supported_opcodes(dev, rbuf); 3993 case 1: 3994 /* One command format with command support data, ignore sa. */ 3995 if (ata_scsi_supported_cmd_use_sa(cdb[3])) { 3996 ata_scsi_set_invalid_field(dev, cmd, 3, 0xff); 3997 return 0; 3998 } 3999 break; 4000 case 2: 4001 /* One command format, must have sa. */ 4002 if (!ata_scsi_supported_cmd_use_sa(cdb[3])) { 4003 ata_scsi_set_invalid_field(dev, cmd, 3, 0xff); 4004 return 0; 4005 } 4006 fallthrough; 4007 case 3: 4008 /* One command format */ 4009 sa = get_unaligned_be16(&cdb[4]); 4010 break; 4011 default: 4012 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]); 4013 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff); 4014 return 0; 4015 } 4016 4017 /* One command format */ 4018 if (ata_scsi_cmd_is_supported(dev, cdb[3], sa, &sup)) { 4019 rbuf[0] = sup.rwcdlp; 4020 rbuf[1] = (sup.cdlp << 3) | 0x03; 4021 } else { 4022 rbuf[1] = 0x01; 4023 } 4024 4025 return 4; 4026 } 4027 4028 /** 4029 * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN 4030 * @dev: Target device. 4031 * @cmd: SCSI command of interest. 4032 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 4033 * 4034 * Yields a subset to satisfy scsi_report_opcode() 4035 * 4036 * LOCKING: 4037 * spin_lock_irqsave(host lock) 4038 */ 4039 static unsigned int ata_scsiop_maint_in(struct ata_device *dev, 4040 struct scsi_cmnd *cmd, u8 *rbuf) 4041 { 4042 u8 *cdb = cmd->cmnd; 4043 u8 service_action = cdb[1] & 0x1f; 4044 4045 switch (service_action) { 4046 case MI_REPORT_SUPPORTED_OPERATION_CODES: 4047 return ata_scsi_report_supported_opcodes(dev, cmd, rbuf); 4048 default: 4049 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff); 4050 return 0; 4051 } 4052 } 4053 4054 /** 4055 * ata_scsi_report_zones_complete - convert ATA output 4056 * @qc: command structure returning the data 4057 * 4058 * Convert T-13 little-endian field representation into 4059 * T-10 big-endian field representation. 4060 * What a mess. 4061 */ 4062 static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc) 4063 { 4064 struct scsi_cmnd *scmd = qc->scsicmd; 4065 struct sg_mapping_iter miter; 4066 unsigned int bytes = 0; 4067 4068 lockdep_assert_held(qc->ap->lock); 4069 4070 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd), 4071 SG_MITER_TO_SG | SG_MITER_ATOMIC); 4072 4073 while (sg_miter_next(&miter)) { 4074 unsigned int offset = 0; 4075 4076 if (bytes == 0) { 4077 char *hdr; 4078 u32 list_length; 4079 u64 max_lba, opt_lba; 4080 u16 same; 4081 4082 /* Swizzle header */ 4083 hdr = miter.addr; 4084 list_length = get_unaligned_le32(&hdr[0]); 4085 same = get_unaligned_le16(&hdr[4]); 4086 max_lba = get_unaligned_le64(&hdr[8]); 4087 opt_lba = get_unaligned_le64(&hdr[16]); 4088 put_unaligned_be32(list_length, &hdr[0]); 4089 hdr[4] = same & 0xf; 4090 put_unaligned_be64(max_lba, &hdr[8]); 4091 put_unaligned_be64(opt_lba, &hdr[16]); 4092 offset += 64; 4093 bytes += 64; 4094 } 4095 while (offset < miter.length) { 4096 char *rec; 4097 u8 cond, type, non_seq, reset; 4098 u64 size, start, wp; 4099 4100 /* Swizzle zone descriptor */ 4101 rec = miter.addr + offset; 4102 type = rec[0] & 0xf; 4103 cond = (rec[1] >> 4) & 0xf; 4104 non_seq = (rec[1] & 2); 4105 reset = (rec[1] & 1); 4106 size = get_unaligned_le64(&rec[8]); 4107 start = get_unaligned_le64(&rec[16]); 4108 wp = get_unaligned_le64(&rec[24]); 4109 rec[0] = type; 4110 rec[1] = (cond << 4) | non_seq | reset; 4111 put_unaligned_be64(size, &rec[8]); 4112 put_unaligned_be64(start, &rec[16]); 4113 put_unaligned_be64(wp, &rec[24]); 4114 WARN_ON(offset + 64 > miter.length); 4115 offset += 64; 4116 bytes += 64; 4117 } 4118 } 4119 sg_miter_stop(&miter); 4120 4121 ata_scsi_qc_complete(qc); 4122 } 4123 4124 static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc) 4125 { 4126 struct ata_taskfile *tf = &qc->tf; 4127 struct scsi_cmnd *scmd = qc->scsicmd; 4128 const u8 *cdb = scmd->cmnd; 4129 u16 sect, fp = (u16)-1; 4130 u8 sa, options, bp = 0xff; 4131 u64 block; 4132 u32 n_block; 4133 4134 if (unlikely(scmd->cmd_len < 16)) { 4135 ata_dev_warn(qc->dev, "invalid cdb length %d\n", 4136 scmd->cmd_len); 4137 fp = 15; 4138 goto invalid_fld; 4139 } 4140 scsi_16_lba_len(cdb, &block, &n_block); 4141 if (n_block != scsi_bufflen(scmd)) { 4142 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n", 4143 n_block, scsi_bufflen(scmd)); 4144 goto invalid_param_len; 4145 } 4146 sa = cdb[1] & 0x1f; 4147 if (sa != ZI_REPORT_ZONES) { 4148 ata_dev_warn(qc->dev, "invalid service action %d\n", sa); 4149 fp = 1; 4150 goto invalid_fld; 4151 } 4152 /* 4153 * ZAC allows only for transfers in 512 byte blocks, 4154 * and uses a 16 bit value for the transfer count. 4155 */ 4156 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) { 4157 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block); 4158 goto invalid_param_len; 4159 } 4160 sect = n_block / 512; 4161 options = cdb[14] & 0xbf; 4162 4163 if (ata_ncq_enabled(qc->dev) && 4164 ata_fpdma_zac_mgmt_in_supported(qc->dev)) { 4165 tf->protocol = ATA_PROT_NCQ; 4166 tf->command = ATA_CMD_FPDMA_RECV; 4167 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f; 4168 tf->nsect = qc->hw_tag << 3; 4169 tf->feature = sect & 0xff; 4170 tf->hob_feature = (sect >> 8) & 0xff; 4171 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8); 4172 } else { 4173 tf->command = ATA_CMD_ZAC_MGMT_IN; 4174 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES; 4175 tf->protocol = ATA_PROT_DMA; 4176 tf->hob_feature = options; 4177 tf->hob_nsect = (sect >> 8) & 0xff; 4178 tf->nsect = sect & 0xff; 4179 } 4180 tf->device = ATA_LBA; 4181 tf->lbah = (block >> 16) & 0xff; 4182 tf->lbam = (block >> 8) & 0xff; 4183 tf->lbal = block & 0xff; 4184 tf->hob_lbah = (block >> 40) & 0xff; 4185 tf->hob_lbam = (block >> 32) & 0xff; 4186 tf->hob_lbal = (block >> 24) & 0xff; 4187 4188 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48; 4189 qc->flags |= ATA_QCFLAG_RESULT_TF; 4190 4191 ata_qc_set_pc_nbytes(qc); 4192 4193 qc->complete_fn = ata_scsi_report_zones_complete; 4194 4195 return 0; 4196 4197 invalid_fld: 4198 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp); 4199 return 1; 4200 4201 invalid_param_len: 4202 /* "Parameter list length error" */ 4203 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0); 4204 return 1; 4205 } 4206 4207 static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc) 4208 { 4209 struct ata_taskfile *tf = &qc->tf; 4210 struct scsi_cmnd *scmd = qc->scsicmd; 4211 struct ata_device *dev = qc->dev; 4212 const u8 *cdb = scmd->cmnd; 4213 u8 all, sa; 4214 u64 block; 4215 u32 n_block; 4216 u16 fp = (u16)-1; 4217 4218 if (unlikely(scmd->cmd_len < 16)) { 4219 fp = 15; 4220 goto invalid_fld; 4221 } 4222 4223 sa = cdb[1] & 0x1f; 4224 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) && 4225 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) { 4226 fp = 1; 4227 goto invalid_fld; 4228 } 4229 4230 scsi_16_lba_len(cdb, &block, &n_block); 4231 if (n_block) { 4232 /* 4233 * ZAC MANAGEMENT OUT doesn't define any length 4234 */ 4235 goto invalid_param_len; 4236 } 4237 4238 all = cdb[14] & 0x1; 4239 if (all) { 4240 /* 4241 * Ignore the block address (zone ID) as defined by ZBC. 4242 */ 4243 block = 0; 4244 } else if (block >= dev->n_sectors) { 4245 /* 4246 * Block must be a valid zone ID (a zone start LBA). 4247 */ 4248 fp = 2; 4249 goto invalid_fld; 4250 } 4251 4252 if (ata_ncq_enabled(qc->dev) && 4253 ata_fpdma_zac_mgmt_out_supported(qc->dev)) { 4254 tf->protocol = ATA_PROT_NCQ_NODATA; 4255 tf->command = ATA_CMD_NCQ_NON_DATA; 4256 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT; 4257 tf->nsect = qc->hw_tag << 3; 4258 tf->auxiliary = sa | ((u16)all << 8); 4259 } else { 4260 tf->protocol = ATA_PROT_NODATA; 4261 tf->command = ATA_CMD_ZAC_MGMT_OUT; 4262 tf->feature = sa; 4263 tf->hob_feature = all; 4264 } 4265 tf->lbah = (block >> 16) & 0xff; 4266 tf->lbam = (block >> 8) & 0xff; 4267 tf->lbal = block & 0xff; 4268 tf->hob_lbah = (block >> 40) & 0xff; 4269 tf->hob_lbam = (block >> 32) & 0xff; 4270 tf->hob_lbal = (block >> 24) & 0xff; 4271 tf->device = ATA_LBA; 4272 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48; 4273 4274 return 0; 4275 4276 invalid_fld: 4277 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff); 4278 return 1; 4279 invalid_param_len: 4280 /* "Parameter list length error" */ 4281 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0); 4282 return 1; 4283 } 4284 4285 /** 4286 * ata_mselect_caching - Simulate MODE SELECT for caching info page 4287 * @qc: Storage for translated ATA taskfile 4288 * @buf: input buffer 4289 * @len: number of valid bytes in the input buffer 4290 * @fp: out parameter for the failed field on error 4291 * 4292 * Prepare a taskfile to modify caching information for the device. 4293 * 4294 * LOCKING: 4295 * None. 4296 */ 4297 static int ata_mselect_caching(struct ata_queued_cmd *qc, 4298 const u8 *buf, int len, u16 *fp) 4299 { 4300 struct ata_taskfile *tf = &qc->tf; 4301 struct ata_device *dev = qc->dev; 4302 u8 mpage[CACHE_MPAGE_LEN]; 4303 u8 wce; 4304 int i; 4305 4306 /* 4307 * The first two bytes of def_cache_mpage are a header, so offsets 4308 * in mpage are off by 2 compared to buf. Same for len. 4309 */ 4310 4311 if (len != CACHE_MPAGE_LEN - 2) { 4312 *fp = min(len, CACHE_MPAGE_LEN - 2); 4313 return -EINVAL; 4314 } 4315 4316 wce = buf[0] & (1 << 2); 4317 4318 /* 4319 * Check that read-only bits are not modified. 4320 */ 4321 ata_msense_caching(dev->id, mpage, false); 4322 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) { 4323 if (i == 0) 4324 continue; 4325 if (mpage[i + 2] != buf[i]) { 4326 *fp = i; 4327 return -EINVAL; 4328 } 4329 } 4330 4331 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; 4332 tf->protocol = ATA_PROT_NODATA; 4333 tf->nsect = 0; 4334 tf->command = ATA_CMD_SET_FEATURES; 4335 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF; 4336 return 0; 4337 } 4338 4339 /* 4340 * Simulate MODE SELECT control mode page, sub-page 0. 4341 */ 4342 static int ata_mselect_control_spg0(struct ata_queued_cmd *qc, 4343 const u8 *buf, int len, u16 *fp) 4344 { 4345 struct ata_device *dev = qc->dev; 4346 u8 mpage[CONTROL_MPAGE_LEN]; 4347 u8 d_sense; 4348 int i; 4349 4350 /* 4351 * The first two bytes of def_control_mpage are a header, so offsets 4352 * in mpage are off by 2 compared to buf. Same for len. 4353 */ 4354 4355 if (len != CONTROL_MPAGE_LEN - 2) { 4356 *fp = min(len, CONTROL_MPAGE_LEN - 2); 4357 return -EINVAL; 4358 } 4359 4360 d_sense = buf[0] & (1 << 2); 4361 4362 /* 4363 * Check that read-only bits are not modified. 4364 */ 4365 ata_msense_control_spg0(dev, mpage, false); 4366 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) { 4367 if (i == 0) 4368 continue; 4369 if (mpage[2 + i] != buf[i]) { 4370 *fp = i; 4371 return -EINVAL; 4372 } 4373 } 4374 if (d_sense & (1 << 2)) 4375 dev->flags |= ATA_DFLAG_D_SENSE; 4376 else 4377 dev->flags &= ~ATA_DFLAG_D_SENSE; 4378 return 0; 4379 } 4380 4381 /* 4382 * Translate MODE SELECT control mode page, sub-page f2h (ATA feature mode 4383 * page) into a SET FEATURES command. 4384 */ 4385 static int ata_mselect_control_ata_feature(struct ata_queued_cmd *qc, 4386 const u8 *buf, int len, u16 *fp) 4387 { 4388 struct ata_device *dev = qc->dev; 4389 struct ata_taskfile *tf = &qc->tf; 4390 u8 cdl_action; 4391 4392 /* 4393 * The first four bytes of ATA Feature Control mode page are a header, 4394 * so offsets in mpage are off by 4 compared to buf. Same for len. 4395 */ 4396 if (len != ATA_FEATURE_SUB_MPAGE_LEN - 4) { 4397 *fp = min(len, ATA_FEATURE_SUB_MPAGE_LEN - 4); 4398 return -EINVAL; 4399 } 4400 4401 /* Check cdl_ctrl */ 4402 switch (buf[0] & 0x03) { 4403 case 0: 4404 /* Disable CDL */ 4405 ata_dev_dbg(dev, "Disabling CDL\n"); 4406 cdl_action = 0; 4407 dev->flags &= ~ATA_DFLAG_CDL_ENABLED; 4408 break; 4409 case 0x02: 4410 /* 4411 * Enable CDL. Since CDL is mutually exclusive with NCQ 4412 * priority, allow this only if NCQ priority is disabled. 4413 */ 4414 if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED) { 4415 ata_dev_err(dev, 4416 "NCQ priority must be disabled to enable CDL\n"); 4417 return -EINVAL; 4418 } 4419 ata_dev_dbg(dev, "Enabling CDL\n"); 4420 cdl_action = 1; 4421 dev->flags |= ATA_DFLAG_CDL_ENABLED; 4422 break; 4423 default: 4424 *fp = 0; 4425 return -EINVAL; 4426 } 4427 4428 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; 4429 tf->protocol = ATA_PROT_NODATA; 4430 tf->command = ATA_CMD_SET_FEATURES; 4431 tf->feature = SETFEATURES_CDL; 4432 tf->nsect = cdl_action; 4433 4434 return 1; 4435 } 4436 4437 /** 4438 * ata_mselect_control - Simulate MODE SELECT for control page 4439 * @qc: Storage for translated ATA taskfile 4440 * @spg: target sub-page of the control page 4441 * @buf: input buffer 4442 * @len: number of valid bytes in the input buffer 4443 * @fp: out parameter for the failed field on error 4444 * 4445 * Prepare a taskfile to modify caching information for the device. 4446 * 4447 * LOCKING: 4448 * None. 4449 */ 4450 static int ata_mselect_control(struct ata_queued_cmd *qc, u8 spg, 4451 const u8 *buf, int len, u16 *fp) 4452 { 4453 switch (spg) { 4454 case 0: 4455 return ata_mselect_control_spg0(qc, buf, len, fp); 4456 case ATA_FEATURE_SUB_MPAGE: 4457 return ata_mselect_control_ata_feature(qc, buf, len, fp); 4458 default: 4459 return -EINVAL; 4460 } 4461 } 4462 4463 /** 4464 * ata_scsi_mode_select_xlat - Simulate MODE SELECT 6, 10 commands 4465 * @qc: Storage for translated ATA taskfile 4466 * 4467 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile. 4468 * Assume this is invoked for direct access devices (e.g. disks) only. 4469 * There should be no block descriptor for other device types. 4470 * 4471 * LOCKING: 4472 * spin_lock_irqsave(host lock) 4473 */ 4474 static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc) 4475 { 4476 struct scsi_cmnd *scmd = qc->scsicmd; 4477 const u8 *cdb = scmd->cmnd; 4478 u8 pg, spg; 4479 unsigned six_byte, pg_len, hdr_len, bd_len; 4480 int len, ret; 4481 u16 fp = (u16)-1; 4482 u8 bp = 0xff; 4483 u8 buffer[64]; 4484 const u8 *p = buffer; 4485 4486 six_byte = (cdb[0] == MODE_SELECT); 4487 if (six_byte) { 4488 if (scmd->cmd_len < 5) { 4489 fp = 4; 4490 goto invalid_fld; 4491 } 4492 4493 len = cdb[4]; 4494 hdr_len = 4; 4495 } else { 4496 if (scmd->cmd_len < 9) { 4497 fp = 8; 4498 goto invalid_fld; 4499 } 4500 4501 len = get_unaligned_be16(&cdb[7]); 4502 hdr_len = 8; 4503 } 4504 4505 /* We only support PF=1, SP=0. */ 4506 if ((cdb[1] & 0x11) != 0x10) { 4507 fp = 1; 4508 bp = (cdb[1] & 0x01) ? 1 : 5; 4509 goto invalid_fld; 4510 } 4511 4512 /* Test early for possible overrun. */ 4513 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len) 4514 goto invalid_param_len; 4515 4516 /* Move past header and block descriptors. */ 4517 if (len < hdr_len) 4518 goto invalid_param_len; 4519 4520 if (!sg_copy_to_buffer(scsi_sglist(scmd), scsi_sg_count(scmd), 4521 buffer, sizeof(buffer))) 4522 goto invalid_param_len; 4523 4524 if (six_byte) 4525 bd_len = p[3]; 4526 else 4527 bd_len = get_unaligned_be16(&p[6]); 4528 4529 len -= hdr_len; 4530 p += hdr_len; 4531 if (len < bd_len) 4532 goto invalid_param_len; 4533 if (bd_len != 0 && bd_len != 8) { 4534 fp = (six_byte) ? 3 : 6; 4535 fp += bd_len + hdr_len; 4536 goto invalid_param; 4537 } 4538 4539 len -= bd_len; 4540 p += bd_len; 4541 if (len == 0) 4542 goto skip; 4543 4544 /* Parse both possible formats for the mode page headers. */ 4545 pg = p[0] & 0x3f; 4546 if (p[0] & 0x40) { 4547 if (len < 4) 4548 goto invalid_param_len; 4549 4550 spg = p[1]; 4551 pg_len = get_unaligned_be16(&p[2]); 4552 p += 4; 4553 len -= 4; 4554 } else { 4555 if (len < 2) 4556 goto invalid_param_len; 4557 4558 spg = 0; 4559 pg_len = p[1]; 4560 p += 2; 4561 len -= 2; 4562 } 4563 4564 /* 4565 * Supported subpages: all subpages and ATA feature sub-page f2h of 4566 * the control page. 4567 */ 4568 if (spg) { 4569 switch (spg) { 4570 case ALL_SUB_MPAGES: 4571 /* All subpages is not supported for the control page */ 4572 if (pg == CONTROL_MPAGE) { 4573 fp = (p[0] & 0x40) ? 1 : 0; 4574 fp += hdr_len + bd_len; 4575 goto invalid_param; 4576 } 4577 break; 4578 case ATA_FEATURE_SUB_MPAGE: 4579 if (qc->dev->flags & ATA_DFLAG_CDL && 4580 pg == CONTROL_MPAGE) 4581 break; 4582 fallthrough; 4583 default: 4584 fp = (p[0] & 0x40) ? 1 : 0; 4585 fp += hdr_len + bd_len; 4586 goto invalid_param; 4587 } 4588 } 4589 if (pg_len > len) 4590 goto invalid_param_len; 4591 4592 switch (pg) { 4593 case CACHE_MPAGE: 4594 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) { 4595 fp += hdr_len + bd_len; 4596 goto invalid_param; 4597 } 4598 break; 4599 case CONTROL_MPAGE: 4600 ret = ata_mselect_control(qc, spg, p, pg_len, &fp); 4601 if (ret < 0) { 4602 fp += hdr_len + bd_len; 4603 goto invalid_param; 4604 } 4605 if (!ret) 4606 goto skip; /* No ATA command to send */ 4607 break; 4608 default: 4609 /* Invalid page code */ 4610 fp = bd_len + hdr_len; 4611 goto invalid_param; 4612 } 4613 4614 /* 4615 * Only one page has changeable data, so we only support setting one 4616 * page at a time. 4617 */ 4618 if (len > pg_len) 4619 goto invalid_param; 4620 4621 return 0; 4622 4623 invalid_fld: 4624 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp); 4625 return 1; 4626 4627 invalid_param: 4628 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp); 4629 return 1; 4630 4631 invalid_param_len: 4632 /* "Parameter list length error" */ 4633 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0); 4634 return 1; 4635 4636 skip: 4637 scmd->result = SAM_STAT_GOOD; 4638 return 1; 4639 } 4640 4641 static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma) 4642 { 4643 if (len == 0) 4644 return ATA_CMD_TRUSTED_NONDATA; 4645 else if (send) 4646 return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND; 4647 else 4648 return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV; 4649 } 4650 4651 static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc) 4652 { 4653 struct scsi_cmnd *scmd = qc->scsicmd; 4654 const u8 *cdb = scmd->cmnd; 4655 struct ata_taskfile *tf = &qc->tf; 4656 u8 secp = cdb[1]; 4657 bool send = (cdb[0] == SECURITY_PROTOCOL_OUT); 4658 u16 spsp = get_unaligned_be16(&cdb[2]); 4659 u32 len = get_unaligned_be32(&cdb[6]); 4660 bool dma = !(qc->dev->flags & ATA_DFLAG_PIO); 4661 4662 /* 4663 * We don't support the ATA "security" protocol. 4664 */ 4665 if (secp == 0xef) { 4666 ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0); 4667 return 1; 4668 } 4669 4670 if (cdb[4] & 7) { /* INC_512 */ 4671 if (len > 0xffff) { 4672 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0); 4673 return 1; 4674 } 4675 } else { 4676 if (len > 0x01fffe00) { 4677 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0); 4678 return 1; 4679 } 4680 4681 /* convert to the sector-based ATA addressing */ 4682 len = (len + 511) / 512; 4683 } 4684 4685 tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO; 4686 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA; 4687 if (send) 4688 tf->flags |= ATA_TFLAG_WRITE; 4689 tf->command = ata_scsi_trusted_op(len, send, dma); 4690 tf->feature = secp; 4691 tf->lbam = spsp & 0xff; 4692 tf->lbah = spsp >> 8; 4693 4694 if (len) { 4695 tf->nsect = len & 0xff; 4696 tf->lbal = len >> 8; 4697 } else { 4698 if (!send) 4699 tf->lbah = (1 << 7); 4700 } 4701 4702 ata_qc_set_pc_nbytes(qc); 4703 return 0; 4704 } 4705 4706 /* 4707 * Convert T-13 little-endian field representation of GET PHYSICAL ELEMENT 4708 * STATUS DMA command reply into T-10 big-endian field representation. 4709 */ 4710 static void ata_scsi_get_phys_element_status_complete(struct ata_queued_cmd *qc) 4711 { 4712 struct scsi_cmnd *scmd = qc->scsicmd; 4713 struct sg_mapping_iter miter; 4714 unsigned int bytes = 0; 4715 4716 lockdep_assert_held(qc->ap->lock); 4717 4718 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd), 4719 SG_MITER_TO_SG | SG_MITER_ATOMIC); 4720 4721 while (sg_miter_next(&miter)) { 4722 unsigned int offset = 0; 4723 4724 if (bytes == 0) { 4725 u32 num_desc, num_desc_returned, id; 4726 u16 max_depop, cur_depop; 4727 char *hdr; 4728 4729 /* Swizzle the header */ 4730 hdr = miter.addr; 4731 num_desc = get_unaligned_le32(&hdr[0]); 4732 num_desc_returned = get_unaligned_le32(&hdr[4]); 4733 id = get_unaligned_le32(&hdr[8]); 4734 max_depop = get_unaligned_le16(&hdr[12]); 4735 cur_depop = get_unaligned_le16(&hdr[14]); 4736 4737 put_unaligned_be32(num_desc, &hdr[0]); 4738 put_unaligned_be32(num_desc_returned, &hdr[4]); 4739 put_unaligned_be32(id, &hdr[8]); 4740 put_unaligned_be16(max_depop, &hdr[12]); 4741 put_unaligned_be16(cur_depop, &hdr[14]); 4742 4743 offset += 32; 4744 bytes += 32; 4745 } 4746 4747 /* Swizzle the descriptors. */ 4748 while (offset < miter.length) { 4749 char *desc; 4750 u32 id; 4751 u8 type; 4752 4753 desc = miter.addr + offset; 4754 id = get_unaligned_le32(&desc[4]); 4755 put_unaligned_be32(id, &desc[4]); 4756 4757 type = desc[14]; 4758 if (type == SCSI_PHYS_ELEM_TYPE_ALL_ACCESS_STORAGE) { 4759 u64 capacity = get_unaligned_le64(&desc[16]); 4760 4761 put_unaligned_be64(capacity, &desc[16]); 4762 } else { 4763 u64 num_zones; 4764 4765 id = get_unaligned_le32(&desc[16]); 4766 num_zones = get_unaligned_le64(&desc[24]); 4767 4768 put_unaligned_be32(id, &desc[16]); 4769 put_unaligned_be64(num_zones, &desc[24]); 4770 } 4771 4772 offset += 32; 4773 bytes += 32; 4774 } 4775 } 4776 sg_miter_stop(&miter); 4777 4778 ata_scsi_qc_complete(qc); 4779 } 4780 4781 static unsigned int 4782 ata_scsi_get_phys_element_status_xlat(struct ata_queued_cmd *qc) 4783 { 4784 struct scsi_cmnd *scmd = qc->scsicmd; 4785 const u8 *cdb = scmd->cmnd; 4786 struct ata_device *dev = qc->dev; 4787 struct ata_taskfile *tf = &qc->tf; 4788 u32 starting_element, len; 4789 4790 /* ATA_CMD_GET_PHYS_ELEMENT_STATUS is a DMA command. */ 4791 if (!(dev->flags & ATA_DFLAG_DEPOP) || !ata_dma_enabled(dev)) { 4792 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0); 4793 return 1; 4794 } 4795 4796 len = get_unaligned_be32(&cdb[10]) / ATA_SECT_SIZE; 4797 if (!len || len > U16_MAX) { 4798 ata_scsi_set_invalid_field(dev, scmd, 10, 0); 4799 return 1; 4800 } 4801 4802 tf->protocol = ATA_PROT_DMA; 4803 tf->command = ATA_CMD_GET_PHYS_ELEMENT_STATUS; 4804 tf->hob_feature = cdb[14]; 4805 tf->hob_nsect = (len >> 8) & 0xff; 4806 tf->nsect = len & 0xff; 4807 4808 starting_element = get_unaligned_be32(&cdb[6]); 4809 if (starting_element) { 4810 tf->hob_lbal = (starting_element >> 24) & 0xff; 4811 tf->lbah = (starting_element >> 16) & 0xff; 4812 tf->lbam = (starting_element >> 8) & 0xff; 4813 tf->lbal = starting_element & 0xff; 4814 } 4815 tf->device = ATA_LBA; 4816 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48; 4817 4818 ata_qc_set_pc_nbytes(qc); 4819 4820 qc->flags |= ATA_QCFLAG_RESULT_TF; 4821 qc->complete_fn = ata_scsi_get_phys_element_status_complete; 4822 4823 return 0; 4824 } 4825 4826 static void ata_scsi_depop_ua_cap_changed_complete(struct ata_queued_cmd *qc) 4827 { 4828 struct scsi_cmnd *scmd = qc->scsicmd; 4829 u8 *cdb = scmd->cmnd; 4830 bool is_ata_passthru = cdb[0] == ATA_16 || cdb[0] == ATA_12; 4831 bool is_success = qc->err_mask == 0; 4832 4833 /* 4834 * For successful non-passthrough commands, raise a UNIT ATTENTION with 4835 * the additional sense code set to CAPACITY DATA HAS CHANGED to be 4836 * raised. Note that this should be done only if the capacity has 4837 * actually changed, which may not be the case if the element that was 4838 * specified for depopulation was already depopulated, or we did not 4839 * restore any removed element. But a capacity change unit attention is 4840 * harmless, so always raise the unit attention. 4841 */ 4842 if (is_success && !is_ata_passthru) 4843 ata_scsi_set_sense(qc->dev, scmd, UNIT_ATTENTION, 4844 UA_CHANGED_ASC, CAPACITY_CHANGED_ASCQ); 4845 ata_scsi_qc_complete(qc); 4846 } 4847 4848 static unsigned int 4849 ata_scsi_remove_element_and_truncate_xlat(struct ata_queued_cmd *qc) 4850 { 4851 struct scsi_cmnd *scmd = qc->scsicmd; 4852 const u8 *cdb = scmd->cmnd; 4853 struct ata_device *dev = qc->dev; 4854 struct ata_taskfile *tf = &qc->tf; 4855 u64 req_capacity; 4856 u32 id; 4857 4858 if (!(dev->flags & ATA_DFLAG_DEPOP)) { 4859 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0); 4860 return 1; 4861 } 4862 4863 req_capacity = get_unaligned_be64(&cdb[2]); 4864 if (req_capacity == 1) { 4865 ata_scsi_set_invalid_field(dev, scmd, 2, 0); 4866 return 1; 4867 } 4868 4869 id = get_unaligned_be32(&cdb[10]); 4870 4871 tf->protocol = ATA_PROT_NODATA; 4872 tf->command = ATA_CMD_REMOVE_ELEMENT_AND_TRUNCATE; 4873 tf->hob_feature = (id >> 24) & 0xff; 4874 tf->feature = (id >> 16) & 0xff; 4875 tf->hob_nsect = (id >> 8) & 0xff; 4876 tf->nsect = id & 0xff; 4877 tf->hob_lbah = (req_capacity >> 40) & 0xff; 4878 tf->hob_lbam = (req_capacity >> 32) & 0xff; 4879 tf->hob_lbal = (req_capacity >> 24) & 0xff; 4880 tf->lbah = (req_capacity >> 16) & 0xff; 4881 tf->lbam = (req_capacity >> 8) & 0xff; 4882 tf->lbal = req_capacity & 0xff; 4883 tf->device = ATA_LBA; 4884 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48; 4885 4886 qc->flags |= ATA_QCFLAG_RESULT_TF; 4887 qc->complete_fn = ata_scsi_depop_ua_cap_changed_complete; 4888 4889 return 0; 4890 } 4891 4892 static unsigned int 4893 ata_scsi_remove_element_and_modify_zones_xlat(struct ata_queued_cmd *qc) 4894 { 4895 struct scsi_cmnd *scmd = qc->scsicmd; 4896 const u8 *cdb = scmd->cmnd; 4897 struct ata_device *dev = qc->dev; 4898 struct ata_taskfile *tf = &qc->tf; 4899 u32 id; 4900 4901 if (!(dev->flags & ATA_DFLAG_DEPOP_MODIFY)) { 4902 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0); 4903 return 1; 4904 } 4905 4906 id = get_unaligned_be32(&cdb[10]); 4907 4908 tf->protocol = ATA_PROT_NODATA; 4909 tf->command = ATA_CMD_REMOVE_ELEMENT_AND_MODIFY_ZONES; 4910 tf->hob_feature = (id >> 24) & 0xff; 4911 tf->feature = (id >> 16) & 0xff; 4912 tf->hob_nsect = (id >> 8) & 0xff; 4913 tf->nsect = id & 0xff; 4914 tf->device = ATA_LBA; 4915 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48; 4916 4917 qc->flags |= ATA_QCFLAG_RESULT_TF; 4918 4919 return 0; 4920 } 4921 4922 static unsigned int 4923 ata_scsi_restore_elements_and_rebuild_xlat(struct ata_queued_cmd *qc) 4924 { 4925 struct scsi_cmnd *scmd = qc->scsicmd; 4926 struct ata_device *dev = qc->dev; 4927 struct ata_taskfile *tf = &qc->tf; 4928 4929 if (!(dev->flags & ATA_DFLAG_DEPOP_RESTORE)) { 4930 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0); 4931 return 1; 4932 } 4933 4934 tf->protocol = ATA_PROT_NODATA; 4935 tf->command = ATA_CMD_RESTORE_ELEMENTS_AND_REBUILD; 4936 tf->device = ATA_LBA; 4937 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48; 4938 4939 qc->flags |= ATA_QCFLAG_RESULT_TF; 4940 qc->complete_fn = ata_scsi_depop_ua_cap_changed_complete; 4941 4942 return 0; 4943 } 4944 4945 /** 4946 * ata_scsi_var_len_cdb_xlat - SATL variable length CDB to Handler 4947 * @qc: Command to be translated 4948 * 4949 * Translate a SCSI variable length CDB to specified commands. 4950 * It checks a service action value in CDB to call corresponding handler. 4951 * 4952 * RETURNS: 4953 * Zero on success, non-zero on failure 4954 * 4955 */ 4956 static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc) 4957 { 4958 struct scsi_cmnd *scmd = qc->scsicmd; 4959 const u8 *cdb = scmd->cmnd; 4960 const u16 sa = get_unaligned_be16(&cdb[8]); 4961 4962 /* 4963 * if service action represents a ata pass-thru(32) command, 4964 * then pass it to ata_scsi_pass_thru handler. 4965 */ 4966 if (sa == ATA_32) 4967 return ata_scsi_pass_thru(qc); 4968 4969 /* unsupported service action */ 4970 return 1; 4971 } 4972 4973 /** 4974 * ata_get_xlat_func - check if SCSI to ATA translation is possible 4975 * @dev: ATA device 4976 * @cdb: CDB of the SCSI command to consider 4977 * 4978 * Look up the SCSI command given, and determine whether the 4979 * SCSI command is to be translated or simulated. 4980 * 4981 * RETURNS: 4982 * Pointer to translation function if possible, %NULL if not. 4983 */ 4984 4985 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, 4986 u8 *cdb) 4987 { 4988 u8 sa; 4989 4990 switch (cdb[0]) { 4991 case READ_6: 4992 case READ_10: 4993 case READ_16: 4994 4995 case WRITE_6: 4996 case WRITE_10: 4997 case WRITE_16: 4998 return ata_scsi_rw_xlat; 4999 5000 case WRITE_SAME_16: 5001 return ata_scsi_write_same_xlat; 5002 5003 case SYNCHRONIZE_CACHE: 5004 case SYNCHRONIZE_CACHE_16: 5005 if (ata_try_flush_cache(dev)) 5006 return ata_scsi_flush_xlat; 5007 break; 5008 5009 case VERIFY: 5010 case VERIFY_16: 5011 return ata_scsi_verify_xlat; 5012 5013 case ATA_12: 5014 case ATA_16: 5015 return ata_scsi_pass_thru; 5016 5017 case VARIABLE_LENGTH_CMD: 5018 return ata_scsi_var_len_cdb_xlat; 5019 5020 case MODE_SELECT: 5021 case MODE_SELECT_10: 5022 return ata_scsi_mode_select_xlat; 5023 5024 case SERVICE_ACTION_IN_16: 5025 sa = cdb[1] & 0x1f; 5026 if (sa == SAI_GET_PHYSICAL_ELEMENT_STATUS) 5027 return ata_scsi_get_phys_element_status_xlat; 5028 if (sa == SAI_REMOVE_ELEMENT_AND_TRUNCATE) 5029 return ata_scsi_remove_element_and_truncate_xlat; 5030 if (sa == SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES) 5031 return ata_scsi_remove_element_and_modify_zones_xlat; 5032 if (sa == SAI_RESTORE_ELEMENTS_AND_REBUILD) 5033 return ata_scsi_restore_elements_and_rebuild_xlat; 5034 break; 5035 5036 case ZBC_IN: 5037 return ata_scsi_zbc_in_xlat; 5038 5039 case ZBC_OUT: 5040 return ata_scsi_zbc_out_xlat; 5041 5042 case SECURITY_PROTOCOL_IN: 5043 case SECURITY_PROTOCOL_OUT: 5044 if (!(dev->flags & ATA_DFLAG_TRUSTED)) 5045 break; 5046 return ata_scsi_security_inout_xlat; 5047 5048 case START_STOP: 5049 return ata_scsi_start_stop_xlat; 5050 } 5051 5052 return NULL; 5053 } 5054 5055 /** 5056 * ata_scsi_simulate - simulate SCSI command on ATA device 5057 * @dev: the target device 5058 * @cmd: SCSI command being sent to device. 5059 * 5060 * Interprets and directly executes a select list of SCSI commands 5061 * that can be handled internally. 5062 * 5063 * LOCKING: 5064 * spin_lock_irqsave(host lock) 5065 */ 5066 static void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd) 5067 { 5068 const u8 *scsicmd = cmd->cmnd; 5069 u8 tmp8; 5070 5071 switch (scsicmd[0]) { 5072 case INQUIRY: 5073 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_inquiry); 5074 break; 5075 5076 case MODE_SENSE: 5077 case MODE_SENSE_10: 5078 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_mode_sense); 5079 break; 5080 5081 case READ_CAPACITY: 5082 case SERVICE_ACTION_IN_16: 5083 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_read_cap); 5084 break; 5085 5086 case REPORT_LUNS: 5087 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_report_luns); 5088 break; 5089 5090 case REQUEST_SENSE: 5091 ata_scsi_set_sense(dev, cmd, 0, 0, 0); 5092 break; 5093 5094 /* if we reach this, then writeback caching is disabled, 5095 * turning this into a no-op. 5096 */ 5097 case SYNCHRONIZE_CACHE: 5098 case SYNCHRONIZE_CACHE_16: 5099 fallthrough; 5100 5101 /* no-op's, complete with success */ 5102 case REZERO_UNIT: 5103 case SEEK_6: 5104 case SEEK_10: 5105 case TEST_UNIT_READY: 5106 break; 5107 5108 case SEND_DIAGNOSTIC: 5109 tmp8 = scsicmd[1] & ~(1 << 3); 5110 if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4]) 5111 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff); 5112 break; 5113 5114 case MAINTENANCE_IN: 5115 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_maint_in); 5116 break; 5117 5118 /* all other commands */ 5119 default: 5120 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0); 5121 /* "Invalid command operation code" */ 5122 break; 5123 } 5124 5125 scsi_done(cmd); 5126 } 5127 5128 enum scsi_qc_status __ata_scsi_queuecmd(struct scsi_cmnd *scmd, 5129 struct ata_device *dev, 5130 struct ata_port *ap) 5131 __must_hold(ap->lock) 5132 { 5133 u8 *cdb = scmd->cmnd; 5134 u8 scsi_op = cdb[0]; 5135 ata_xlat_func_t xlat_func; 5136 5137 /* 5138 * scsi_queue_rq() will defer commands if scsi_host_in_recovery(). 5139 * However, this check is done without holding the ap->lock (a libata 5140 * specific lock), so we can have received an error irq since then, 5141 * therefore we must check if EH is pending or running, while holding 5142 * ap->lock. 5143 */ 5144 if (ata_port_eh_scheduled(ap)) 5145 return SCSI_MLQUEUE_DEVICE_BUSY; 5146 5147 if (unlikely(!scmd->cmd_len)) 5148 goto bad_cdb_len; 5149 5150 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) { 5151 if (unlikely(scmd->cmd_len > dev->cdb_len)) 5152 goto bad_cdb_len; 5153 5154 xlat_func = ata_get_xlat_func(dev, cdb); 5155 } else if (likely((scsi_op != ATA_16) || !atapi_passthru16)) { 5156 /* relay SCSI command to ATAPI device */ 5157 int len = COMMAND_SIZE(scsi_op); 5158 5159 if (unlikely(len > scmd->cmd_len || 5160 len > dev->cdb_len || 5161 scmd->cmd_len > ATAPI_CDB_LEN)) 5162 goto bad_cdb_len; 5163 5164 xlat_func = atapi_xlat; 5165 } else { 5166 /* ATA_16 passthru, treat as an ATA command */ 5167 if (unlikely(scmd->cmd_len > 16)) 5168 goto bad_cdb_len; 5169 5170 xlat_func = ata_get_xlat_func(dev, cdb); 5171 } 5172 5173 if (xlat_func) 5174 return ata_scsi_translate(dev, scmd, xlat_func, ap); 5175 5176 ata_scsi_simulate(dev, scmd); 5177 5178 return 0; 5179 5180 bad_cdb_len: 5181 scmd->result = DID_ERROR << 16; 5182 scsi_done(scmd); 5183 return 0; 5184 } 5185 5186 /** 5187 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device 5188 * @shost: SCSI host of command to be sent 5189 * @cmd: SCSI command to be sent 5190 * 5191 * In some cases, this function translates SCSI commands into 5192 * ATA taskfiles, and queues the taskfiles to be sent to 5193 * hardware. In other cases, this function simulates a 5194 * SCSI device by evaluating and responding to certain 5195 * SCSI commands. This creates the overall effect of 5196 * ATA and ATAPI devices appearing as SCSI devices. 5197 * 5198 * LOCKING: 5199 * ATA host lock 5200 * 5201 * RETURNS: 5202 * Return value from __ata_scsi_queuecmd() if @cmd can be queued, 5203 * 0 otherwise. 5204 */ 5205 enum scsi_qc_status ata_scsi_queuecmd(struct Scsi_Host *shost, 5206 struct scsi_cmnd *cmd) 5207 { 5208 struct ata_port *ap; 5209 struct ata_device *dev; 5210 struct scsi_device *scsidev = cmd->device; 5211 enum scsi_qc_status rc = 0; 5212 unsigned long irq_flags; 5213 5214 ap = ata_shost_to_port(shost); 5215 5216 spin_lock_irqsave(ap->lock, irq_flags); 5217 5218 dev = ata_scsi_find_dev(ap, scsidev); 5219 if (likely(dev)) 5220 rc = __ata_scsi_queuecmd(cmd, dev, ap); 5221 else { 5222 cmd->result = (DID_BAD_TARGET << 16); 5223 scsi_done(cmd); 5224 } 5225 5226 spin_unlock_irqrestore(ap->lock, irq_flags); 5227 5228 return rc; 5229 } 5230 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd); 5231 5232 int ata_scsi_add_hosts(struct ata_host *host, const struct scsi_host_template *sht) 5233 { 5234 int i, rc; 5235 5236 for (i = 0; i < host->n_ports; i++) { 5237 struct ata_port *ap = host->ports[i]; 5238 struct Scsi_Host *shost; 5239 5240 rc = -ENOMEM; 5241 shost = scsi_host_alloc(sht, sizeof(struct ata_port *)); 5242 if (!shost) 5243 goto err_alloc; 5244 5245 shost->eh_noresume = 1; 5246 *(struct ata_port **)&shost->hostdata[0] = ap; 5247 ap->scsi_host = shost; 5248 5249 shost->transportt = &ata_scsi_transportt; 5250 shost->unique_id = ap->print_id; 5251 shost->max_id = 16; 5252 shost->max_lun = 1; 5253 shost->max_channel = 1; 5254 shost->max_cmd_len = 32; 5255 5256 /* Schedule policy is determined by ->qc_defer() 5257 * callback and it needs to see every deferred qc. 5258 * Set host_blocked to 1 to prevent SCSI midlayer from 5259 * automatically deferring requests. 5260 */ 5261 shost->max_host_blocked = 1; 5262 5263 rc = scsi_add_host_with_dma(shost, &ap->tdev, ap->host->dev); 5264 if (rc) 5265 goto err_alloc; 5266 } 5267 5268 return 0; 5269 5270 err_alloc: 5271 while (--i >= 0) { 5272 struct Scsi_Host *shost = host->ports[i]->scsi_host; 5273 5274 /* scsi_host_put() is in ata_devres_release() */ 5275 scsi_remove_host(shost); 5276 } 5277 return rc; 5278 } 5279 5280 #ifdef CONFIG_OF 5281 static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap) 5282 { 5283 struct scsi_device *sdev = dev->sdev; 5284 struct device *d = ap->host->dev; 5285 struct device_node *np = d->of_node; 5286 struct device_node *child; 5287 5288 for_each_available_child_of_node(np, child) { 5289 int ret; 5290 u32 val; 5291 5292 ret = of_property_read_u32(child, "reg", &val); 5293 if (ret) 5294 continue; 5295 if (val == dev->devno) { 5296 dev_dbg(d, "found matching device node\n"); 5297 sdev->sdev_gendev.of_node = child; 5298 return; 5299 } 5300 } 5301 } 5302 #else 5303 static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap) 5304 { 5305 } 5306 #endif 5307 5308 void ata_scsi_scan_host(struct ata_port *ap, int sync) 5309 { 5310 int tries = 5; 5311 struct ata_device *last_failed_dev = NULL; 5312 struct ata_link *link; 5313 struct ata_device *dev; 5314 5315 repeat: 5316 ata_for_each_link(link, ap, EDGE) { 5317 ata_for_each_dev(dev, link, ENABLED) { 5318 struct scsi_device *sdev; 5319 int channel = 0, id = 0; 5320 5321 if (dev->sdev) 5322 continue; 5323 5324 if (ata_is_host_link(link)) 5325 id = dev->devno; 5326 else 5327 channel = link->pmp; 5328 5329 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0, 5330 NULL); 5331 if (!IS_ERR(sdev)) { 5332 dev->sdev = sdev; 5333 ata_scsi_assign_ofnode(dev, ap); 5334 scsi_device_put(sdev); 5335 } else { 5336 dev->sdev = NULL; 5337 } 5338 } 5339 } 5340 5341 /* If we scanned while EH was in progress or allocation 5342 * failure occurred, scan would have failed silently. Check 5343 * whether all devices are attached. 5344 */ 5345 ata_for_each_link(link, ap, EDGE) { 5346 ata_for_each_dev(dev, link, ENABLED) { 5347 if (!dev->sdev) 5348 goto exit_loop; 5349 } 5350 } 5351 exit_loop: 5352 if (!link) 5353 return; 5354 5355 /* we're missing some SCSI devices */ 5356 if (sync) { 5357 /* If caller requested synchrnous scan && we've made 5358 * any progress, sleep briefly and repeat. 5359 */ 5360 if (dev != last_failed_dev) { 5361 msleep(100); 5362 last_failed_dev = dev; 5363 goto repeat; 5364 } 5365 5366 /* We might be failing to detect boot device, give it 5367 * a few more chances. 5368 */ 5369 if (--tries) { 5370 msleep(100); 5371 goto repeat; 5372 } 5373 5374 ata_port_err(ap, 5375 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n"); 5376 } 5377 5378 queue_delayed_work(system_dfl_long_wq, &ap->hotplug_task, 5379 round_jiffies_relative(HZ)); 5380 } 5381 5382 /** 5383 * ata_scsi_offline_dev - offline attached SCSI device 5384 * @dev: ATA device to offline attached SCSI device for 5385 * 5386 * This function is called from ata_eh_detach_dev() and is responsible for 5387 * taking the SCSI device attached to @dev offline. This function is 5388 * called with host lock which protects dev->sdev against clearing. 5389 * 5390 * LOCKING: 5391 * spin_lock_irqsave(host lock) 5392 * 5393 * RETURNS: 5394 * true if attached SCSI device exists, false otherwise. 5395 */ 5396 bool ata_scsi_offline_dev(struct ata_device *dev) 5397 { 5398 if (dev->sdev) { 5399 scsi_device_set_state(dev->sdev, SDEV_OFFLINE); 5400 return true; 5401 } 5402 return false; 5403 } 5404 5405 /** 5406 * ata_scsi_remove_dev - remove attached SCSI device 5407 * @dev: ATA device to remove attached SCSI device for 5408 * 5409 * This function is called from ata_eh_scsi_hotplug() and 5410 * responsible for removing the SCSI device attached to @dev. 5411 * 5412 * LOCKING: 5413 * Kernel thread context (may sleep). 5414 */ 5415 static void ata_scsi_remove_dev(struct ata_device *dev) 5416 { 5417 struct ata_port *ap = dev->link->ap; 5418 struct scsi_device *sdev; 5419 unsigned long flags; 5420 5421 /* Alas, we need to grab scan_mutex to ensure SCSI device 5422 * state doesn't change underneath us and thus 5423 * scsi_device_get() always succeeds. The mutex locking can 5424 * be removed if there is __scsi_device_get() interface which 5425 * increments reference counts regardless of device state. 5426 */ 5427 mutex_lock(&ap->scsi_host->scan_mutex); 5428 spin_lock_irqsave(ap->lock, flags); 5429 5430 /* clearing dev->sdev is protected by host lock */ 5431 sdev = dev->sdev; 5432 dev->sdev = NULL; 5433 5434 if (sdev) { 5435 /* If user initiated unplug races with us, sdev can go 5436 * away underneath us after the host lock and 5437 * scan_mutex are released. Hold onto it. 5438 */ 5439 if (scsi_device_get(sdev) == 0) { 5440 /* The following ensures the attached sdev is 5441 * offline on return from ata_scsi_offline_dev() 5442 * regardless it wins or loses the race 5443 * against this function. 5444 */ 5445 scsi_device_set_state(sdev, SDEV_OFFLINE); 5446 } else { 5447 WARN_ON(1); 5448 sdev = NULL; 5449 } 5450 } 5451 5452 spin_unlock_irqrestore(ap->lock, flags); 5453 mutex_unlock(&ap->scsi_host->scan_mutex); 5454 5455 if (sdev) { 5456 ata_dev_info(dev, "detaching (SCSI %s)\n", 5457 dev_name(&sdev->sdev_gendev)); 5458 5459 scsi_remove_device(sdev); 5460 scsi_device_put(sdev); 5461 } 5462 } 5463 5464 static void ata_scsi_handle_link_detach(struct ata_link *link) 5465 { 5466 struct ata_port *ap = link->ap; 5467 struct ata_device *dev; 5468 5469 ata_for_each_dev(dev, link, ALL) { 5470 unsigned long flags; 5471 5472 spin_lock_irqsave(ap->lock, flags); 5473 if (!(dev->flags & ATA_DFLAG_DETACHED)) { 5474 spin_unlock_irqrestore(ap->lock, flags); 5475 continue; 5476 } 5477 5478 dev->flags &= ~ATA_DFLAG_DETACHED; 5479 spin_unlock_irqrestore(ap->lock, flags); 5480 5481 ata_scsi_remove_dev(dev); 5482 } 5483 } 5484 5485 /** 5486 * ata_scsi_media_change_notify - send media change event 5487 * @dev: Pointer to the disk device with media change event 5488 * 5489 * Tell the block layer to send a media change notification 5490 * event. 5491 * 5492 * LOCKING: 5493 * spin_lock_irqsave(host lock) 5494 */ 5495 void ata_scsi_media_change_notify(struct ata_device *dev) 5496 { 5497 if (dev->sdev) 5498 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE, 5499 GFP_ATOMIC); 5500 } 5501 5502 /** 5503 * ata_scsi_hotplug - SCSI part of hotplug 5504 * @work: Pointer to ATA port to perform SCSI hotplug on 5505 * 5506 * Perform SCSI part of hotplug. It's executed from a separate 5507 * workqueue after EH completes. This is necessary because SCSI 5508 * hot plugging requires working EH and hot unplugging is 5509 * synchronized with hot plugging with a mutex. 5510 * 5511 * LOCKING: 5512 * Kernel thread context (may sleep). 5513 */ 5514 void ata_scsi_hotplug(struct work_struct *work) 5515 { 5516 struct ata_port *ap = 5517 container_of(work, struct ata_port, hotplug_task.work); 5518 int i; 5519 5520 if (ap->pflags & ATA_PFLAG_UNLOADING) 5521 return; 5522 5523 mutex_lock(&ap->scsi_scan_mutex); 5524 5525 /* Unplug detached devices. We cannot use link iterator here 5526 * because PMP links have to be scanned even if PMP is 5527 * currently not attached. Iterate manually. 5528 */ 5529 ata_scsi_handle_link_detach(&ap->link); 5530 if (ap->pmp_link) 5531 for (i = 0; i < SATA_PMP_MAX_PORTS; i++) 5532 ata_scsi_handle_link_detach(&ap->pmp_link[i]); 5533 5534 /* scan for new ones */ 5535 ata_scsi_scan_host(ap, 0); 5536 5537 mutex_unlock(&ap->scsi_scan_mutex); 5538 } 5539 5540 /** 5541 * ata_scsi_user_scan - indication for user-initiated bus scan 5542 * @shost: SCSI host to scan 5543 * @channel: Channel to scan 5544 * @id: ID to scan 5545 * @lun: LUN to scan 5546 * 5547 * This function is called when user explicitly requests bus 5548 * scan. Set probe pending flag and invoke EH. 5549 * 5550 * LOCKING: 5551 * SCSI layer (we don't care) 5552 * 5553 * RETURNS: 5554 * Zero. 5555 */ 5556 int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel, 5557 unsigned int id, u64 lun) 5558 { 5559 struct ata_port *ap = ata_shost_to_port(shost); 5560 unsigned long flags; 5561 int devno, rc = 0; 5562 5563 if (lun != SCAN_WILD_CARD && lun) 5564 return -EINVAL; 5565 5566 if (!sata_pmp_attached(ap)) { 5567 if (channel != SCAN_WILD_CARD && channel) 5568 return -EINVAL; 5569 devno = id; 5570 } else { 5571 if (id != SCAN_WILD_CARD && id) 5572 return -EINVAL; 5573 devno = channel; 5574 } 5575 5576 spin_lock_irqsave(ap->lock, flags); 5577 5578 if (devno == SCAN_WILD_CARD) { 5579 struct ata_link *link; 5580 5581 ata_for_each_link(link, ap, EDGE) { 5582 struct ata_eh_info *ehi = &link->eh_info; 5583 ehi->probe_mask |= ATA_ALL_DEVICES; 5584 ehi->action |= ATA_EH_RESET; 5585 } 5586 } else { 5587 struct ata_device *dev = ata_find_dev(ap, devno); 5588 5589 if (dev) { 5590 struct ata_eh_info *ehi = &dev->link->eh_info; 5591 ehi->probe_mask |= 1 << dev->devno; 5592 ehi->action |= ATA_EH_RESET; 5593 } else 5594 rc = -EINVAL; 5595 } 5596 5597 if (rc == 0) { 5598 ata_port_schedule_eh(ap); 5599 spin_unlock_irqrestore(ap->lock, flags); 5600 ata_port_wait_eh(ap); 5601 } else 5602 spin_unlock_irqrestore(ap->lock, flags); 5603 5604 return rc; 5605 } 5606 5607 /** 5608 * ata_scsi_dev_rescan - initiate scsi_rescan_device() 5609 * @work: Pointer to ATA port to perform scsi_rescan_device() 5610 * 5611 * After ATA pass thru (SAT) commands are executed successfully, 5612 * libata need to propagate the changes to SCSI layer. 5613 * 5614 * LOCKING: 5615 * Kernel thread context (may sleep). 5616 */ 5617 void ata_scsi_dev_rescan(struct work_struct *work) 5618 { 5619 struct ata_port *ap = 5620 container_of(work, struct ata_port, scsi_rescan_task.work); 5621 struct ata_link *link; 5622 struct ata_device *dev; 5623 unsigned long flags; 5624 bool do_resume; 5625 int ret = 0; 5626 5627 mutex_lock(&ap->scsi_scan_mutex); 5628 spin_lock_irqsave(ap->lock, flags); 5629 5630 ata_for_each_link(link, ap, EDGE) { 5631 ata_for_each_dev(dev, link, ENABLED) { 5632 struct scsi_device *sdev = dev->sdev; 5633 5634 /* 5635 * If the port was suspended before this was scheduled, 5636 * bail out. 5637 */ 5638 if (ap->pflags & ATA_PFLAG_SUSPENDED) 5639 goto unlock_ap; 5640 5641 if (!sdev) 5642 continue; 5643 if (scsi_device_get(sdev)) 5644 continue; 5645 5646 do_resume = dev->flags & ATA_DFLAG_RESUMING; 5647 5648 spin_unlock_irqrestore(ap->lock, flags); 5649 if (do_resume) { 5650 ret = scsi_resume_device(sdev); 5651 if (ret == -EWOULDBLOCK) { 5652 scsi_device_put(sdev); 5653 goto unlock_scan; 5654 } 5655 dev->flags &= ~ATA_DFLAG_RESUMING; 5656 } 5657 ret = scsi_rescan_device(sdev); 5658 scsi_device_put(sdev); 5659 spin_lock_irqsave(ap->lock, flags); 5660 5661 if (ret) 5662 goto unlock_ap; 5663 } 5664 } 5665 5666 unlock_ap: 5667 spin_unlock_irqrestore(ap->lock, flags); 5668 unlock_scan: 5669 mutex_unlock(&ap->scsi_scan_mutex); 5670 5671 /* Reschedule with a delay if scsi_rescan_device() returned an error */ 5672 if (ret) 5673 schedule_delayed_work(&ap->scsi_rescan_task, 5674 msecs_to_jiffies(5)); 5675 } 5676