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 void ata_scsi_requeue_deferred_qc(struct ata_port *ap) 1689 { 1690 struct ata_link *link; 1691 1692 lockdep_assert_held(ap->lock); 1693 1694 /* 1695 * If we have a deferred qc when a reset occurs or NCQ commands fail, 1696 * do not try to be smart about what to do with this deferred command 1697 * and simply requeue it by completing it with DID_REQUEUE. 1698 */ 1699 ata_for_each_link(link, ap, PMP_FIRST) { 1700 struct ata_queued_cmd *qc = link->deferred_qc; 1701 1702 if (qc) { 1703 link->deferred_qc = NULL; 1704 cancel_work(&link->deferred_qc_work); 1705 ata_scsi_qc_done(qc, true, DID_REQUEUE << 16); 1706 } 1707 } 1708 } 1709 1710 static void ata_scsi_schedule_deferred_qc(struct ata_link *link) 1711 { 1712 struct ata_queued_cmd *qc = link->deferred_qc; 1713 struct ata_port *ap = link->ap; 1714 1715 lockdep_assert_held(ap->lock); 1716 1717 /* 1718 * If we have a deferred qc, then qc_defer() is defined and we can use 1719 * this callback to determine if this qc is good to go, unless EH has 1720 * been scheduled. 1721 */ 1722 if (!qc) 1723 return; 1724 1725 if (ata_port_eh_scheduled(ap)) { 1726 ata_scsi_requeue_deferred_qc(ap); 1727 return; 1728 } 1729 if (!ap->ops->qc_defer(qc)) 1730 queue_work(system_highpri_wq, &link->deferred_qc_work); 1731 } 1732 1733 static void ata_scsi_qc_complete(struct ata_queued_cmd *qc) 1734 { 1735 struct ata_link *link = qc->dev->link; 1736 struct scsi_cmnd *cmd = qc->scsicmd; 1737 u8 *cdb = cmd->cmnd; 1738 bool have_sense = qc->flags & ATA_QCFLAG_SENSE_VALID; 1739 bool is_ata_passthru = cdb[0] == ATA_16 || cdb[0] == ATA_12; 1740 bool is_ck_cond_request = cdb[2] & 0x20; 1741 bool is_error = qc->err_mask != 0; 1742 1743 /* For ATA pass thru (SAT) commands, generate a sense block if 1744 * user mandated it or if there's an error. Note that if we 1745 * generate because the user forced us to [CK_COND=1], a check 1746 * condition is generated and the ATA register values are returned 1747 * whether the command completed successfully or not. If there 1748 * was no error, and CK_COND=1, we use the following sense data: 1749 * sk = RECOVERED ERROR 1750 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE 1751 */ 1752 if (is_ata_passthru && (is_ck_cond_request || is_error || have_sense)) { 1753 if (!have_sense) 1754 ata_gen_passthru_sense(qc); 1755 ata_scsi_set_passthru_sense_fields(qc); 1756 if (is_ck_cond_request) 1757 set_status_byte(qc->scsicmd, SAM_STAT_CHECK_CONDITION); 1758 } else if (is_error) { 1759 if (!have_sense) 1760 ata_gen_ata_sense(qc); 1761 ata_scsi_set_sense_information(qc); 1762 } 1763 1764 ata_scsi_qc_done(qc, false, 0); 1765 1766 ata_scsi_schedule_deferred_qc(link); 1767 } 1768 1769 static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc) 1770 __must_hold(ap->lock) 1771 { 1772 struct ata_link *link = qc->dev->link; 1773 int ret; 1774 1775 if (!ap->ops->qc_defer) 1776 goto issue_qc; 1777 1778 /* 1779 * If we already have a deferred qc, then rely on the SCSI layer to 1780 * requeue and defer all incoming commands until the deferred qc is 1781 * processed, once all on-going commands complete. 1782 */ 1783 if (link->deferred_qc) { 1784 ata_qc_free(qc); 1785 return SCSI_MLQUEUE_DEVICE_BUSY; 1786 } 1787 1788 /* Check if the command needs to be deferred. */ 1789 ret = ap->ops->qc_defer(qc); 1790 switch (ret) { 1791 case 0: 1792 break; 1793 case ATA_DEFER_LINK: 1794 ret = SCSI_MLQUEUE_DEVICE_BUSY; 1795 goto defer_qc; 1796 case ATA_DEFER_LINK_EXCL: 1797 /* 1798 * Drivers making use of ap->excl_link cannot store the QC in 1799 * link->deferred_qc, because the ap->excl_link handling is 1800 * incompatible with the link->deferred_qc workqueue handling. 1801 */ 1802 ret = SCSI_MLQUEUE_DEVICE_BUSY; 1803 goto free_qc; 1804 case ATA_DEFER_PORT: 1805 ret = SCSI_MLQUEUE_HOST_BUSY; 1806 goto free_qc; 1807 default: 1808 WARN_ON_ONCE(1); 1809 ret = SCSI_MLQUEUE_HOST_BUSY; 1810 goto free_qc; 1811 } 1812 1813 issue_qc: 1814 ata_qc_issue(ap, qc); 1815 return 0; 1816 1817 defer_qc: 1818 /* 1819 * We must defer this qc: if this is not an NCQ command, keep 1820 * this qc as a deferred one and report to the SCSI layer that 1821 * we issued it so that it is not requeued. The deferred qc will 1822 * be issued with the port deferred_qc_work once all on-going 1823 * commands complete. 1824 */ 1825 if (!ata_is_ncq(qc->tf.protocol)) { 1826 link->deferred_qc = qc; 1827 return 0; 1828 } 1829 1830 free_qc: 1831 /* Force a requeue of the command to defer its execution. */ 1832 ata_qc_free(qc); 1833 1834 return ret; 1835 } 1836 1837 /** 1838 * ata_scsi_translate - Translate then issue SCSI command to ATA device 1839 * @dev: ATA device to which the command is addressed 1840 * @cmd: SCSI command to execute 1841 * @xlat_func: Actor which translates @cmd to an ATA taskfile 1842 * @ap: ATA port of interest 1843 * 1844 * Our ->queuecommand() function has decided that the SCSI 1845 * command issued can be directly translated into an ATA 1846 * command, rather than handled internally. 1847 * 1848 * This function sets up an ata_queued_cmd structure for the 1849 * SCSI command, and sends that ata_queued_cmd to the hardware. 1850 * 1851 * The xlat_func argument (actor) returns 0 if ready to execute 1852 * ATA command, else 1 to finish translation. If 1 is returned 1853 * then cmd->result (and possibly cmd->sense_buffer) are assumed 1854 * to be set reflecting an error condition or clean (early) 1855 * termination. 1856 * 1857 * LOCKING: 1858 * spin_lock_irqsave(host lock) 1859 * 1860 * RETURNS: 1861 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY or SCSI_MLQUEUE_HOST_BUSY if the 1862 * command needs to be deferred. 1863 */ 1864 static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd, 1865 ata_xlat_func_t xlat_func, struct ata_port *ap) 1866 __must_hold(ap->lock) 1867 { 1868 struct ata_queued_cmd *qc; 1869 1870 lockdep_assert_held(ap->lock); 1871 1872 /* 1873 * ata_scsi_qc_new() calls scsi_done(cmd) in case of failure. So we 1874 * have nothing further to do when allocating a qc fails. 1875 */ 1876 qc = ata_scsi_qc_new(dev, cmd); 1877 if (!qc) 1878 return 0; 1879 1880 /* data is present; dma-map it */ 1881 if (cmd->sc_data_direction == DMA_FROM_DEVICE || 1882 cmd->sc_data_direction == DMA_TO_DEVICE) { 1883 if (unlikely(scsi_bufflen(cmd) < 1)) { 1884 ata_dev_warn(dev, "WARNING: zero len r/w req\n"); 1885 cmd->result = (DID_ERROR << 16); 1886 goto done; 1887 } 1888 1889 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd)); 1890 qc->dma_dir = cmd->sc_data_direction; 1891 } 1892 1893 qc->complete_fn = ata_scsi_qc_complete; 1894 1895 if (xlat_func(qc)) 1896 goto done; 1897 1898 return ata_scsi_qc_issue(ap, qc); 1899 1900 done: 1901 ata_qc_free(qc); 1902 scsi_done(cmd); 1903 return 0; 1904 } 1905 1906 /** 1907 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators 1908 * @dev: Target device. 1909 * @cmd: SCSI command of interest. 1910 * @actor: Callback hook for desired SCSI command simulator 1911 * 1912 * Takes care of the hard work of simulating a SCSI command... 1913 * Mapping the response buffer, calling the command's handler, 1914 * and handling the handler's return value. This return value 1915 * indicates whether the handler wishes the SCSI command to be 1916 * completed successfully (0), or not (in which case cmd->result 1917 * and sense buffer are assumed to be set). 1918 * 1919 * LOCKING: 1920 * spin_lock_irqsave(host lock) 1921 */ 1922 static void ata_scsi_rbuf_fill(struct ata_device *dev, struct scsi_cmnd *cmd, 1923 unsigned int (*actor)(struct ata_device *dev, 1924 struct scsi_cmnd *cmd, u8 *rbuf)) 1925 { 1926 unsigned long flags; 1927 unsigned int len; 1928 1929 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags); 1930 1931 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE); 1932 len = actor(dev, cmd, ata_scsi_rbuf); 1933 if (len) { 1934 if (WARN_ON(len > ATA_SCSI_RBUF_SIZE)) { 1935 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0); 1936 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags); 1937 return; 1938 } 1939 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), 1940 ata_scsi_rbuf, len); 1941 cmd->result = SAM_STAT_GOOD; 1942 if (scsi_bufflen(cmd) > len) 1943 scsi_set_resid(cmd, scsi_bufflen(cmd) - len); 1944 } 1945 1946 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags); 1947 } 1948 1949 /** 1950 * ata_scsiop_inq_std - Simulate standard INQUIRY command 1951 * @dev: Target device. 1952 * @cmd: SCSI command of interest. 1953 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 1954 * 1955 * Returns standard device identification data associated 1956 * with non-VPD INQUIRY command output. 1957 * 1958 * LOCKING: 1959 * spin_lock_irqsave(host lock) 1960 */ 1961 static unsigned int ata_scsiop_inq_std(struct ata_device *dev, 1962 struct scsi_cmnd *cmd, u8 *rbuf) 1963 { 1964 static const u8 versions[] = { 1965 0x00, 1966 0x60, /* SAM-3 (no version claimed) */ 1967 1968 0x03, 1969 0x20, /* SBC-2 (no version claimed) */ 1970 1971 0x03, 1972 0x00 /* SPC-3 (no version claimed) */ 1973 }; 1974 static const u8 versions_zbc[] = { 1975 0x00, 1976 0xA0, /* SAM-5 (no version claimed) */ 1977 1978 0x06, 1979 0x00, /* SBC-4 (no version claimed) */ 1980 1981 0x05, 1982 0xC0, /* SPC-5 (no version claimed) */ 1983 1984 0x60, 1985 0x24, /* ZBC r05 */ 1986 }; 1987 1988 u8 hdr[] = { 1989 TYPE_DISK, 1990 0, 1991 0x5, /* claim SPC-3 version compatibility */ 1992 2, 1993 95 - 4, 1994 0, 1995 0, 1996 2 1997 }; 1998 1999 /* 2000 * Set the SCSI Removable Media Bit (RMB) if the ATA removable media 2001 * device bit (obsolete since ATA-8 ACS) is set. 2002 */ 2003 if (ata_id_removable(dev->id)) 2004 hdr[1] |= (1 << 7); 2005 2006 if (dev->class == ATA_DEV_ZAC) { 2007 hdr[0] = TYPE_ZBC; 2008 hdr[2] = 0x7; /* claim SPC-5 version compatibility */ 2009 } 2010 2011 if (dev->flags & ATA_DFLAG_CDL) 2012 hdr[2] = 0xd; /* claim SPC-6 version compatibility */ 2013 2014 memcpy(rbuf, hdr, sizeof(hdr)); 2015 memcpy(&rbuf[8], "ATA ", 8); 2016 ata_id_string(dev->id, &rbuf[16], ATA_ID_PROD, 16); 2017 2018 /* From SAT, use last 2 words from fw rev unless they are spaces */ 2019 ata_id_string(dev->id, &rbuf[32], ATA_ID_FW_REV + 2, 4); 2020 if (strncmp(&rbuf[32], " ", 4) == 0) 2021 ata_id_string(dev->id, &rbuf[32], ATA_ID_FW_REV, 4); 2022 2023 if (rbuf[32] == 0 || rbuf[32] == ' ') 2024 memcpy(&rbuf[32], "n/a ", 4); 2025 2026 if (ata_id_zoned_cap(dev->id) || dev->class == ATA_DEV_ZAC) 2027 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc)); 2028 else 2029 memcpy(rbuf + 58, versions, sizeof(versions)); 2030 2031 /* 2032 * Include all 8 possible version descriptors, even if not all of 2033 * them are popoulated. 2034 */ 2035 return 96; 2036 } 2037 2038 /** 2039 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages 2040 * @dev: Target device. 2041 * @cmd: SCSI command of interest. 2042 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2043 * 2044 * Returns list of inquiry VPD pages available. 2045 * 2046 * LOCKING: 2047 * spin_lock_irqsave(host lock) 2048 */ 2049 static unsigned int ata_scsiop_inq_00(struct ata_device *dev, 2050 struct scsi_cmnd *cmd, u8 *rbuf) 2051 { 2052 int i, num_pages = 0; 2053 static const u8 pages[] = { 2054 0x00, /* page 0x00, this page */ 2055 0x80, /* page 0x80, unit serial no page */ 2056 0x83, /* page 0x83, device ident page */ 2057 0x89, /* page 0x89, ata info page */ 2058 0xb0, /* page 0xb0, block limits page */ 2059 0xb1, /* page 0xb1, block device characteristics page */ 2060 0xb2, /* page 0xb2, thin provisioning page */ 2061 0xb6, /* page 0xb6, zoned block device characteristics */ 2062 0xb9, /* page 0xb9, concurrent positioning ranges */ 2063 }; 2064 2065 for (i = 0; i < sizeof(pages); i++) { 2066 if (pages[i] == 0xb6 && !ata_dev_is_zac(dev)) 2067 continue; 2068 rbuf[num_pages + 4] = pages[i]; 2069 num_pages++; 2070 } 2071 rbuf[3] = num_pages; /* number of supported VPD pages */ 2072 2073 return get_unaligned_be16(&rbuf[2]) + 4; 2074 } 2075 2076 /** 2077 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number 2078 * @dev: Target device. 2079 * @cmd: SCSI command of interest. 2080 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2081 * 2082 * Returns ATA device serial number. 2083 * 2084 * LOCKING: 2085 * spin_lock_irqsave(host lock) 2086 */ 2087 static unsigned int ata_scsiop_inq_80(struct ata_device *dev, 2088 struct scsi_cmnd *cmd, u8 *rbuf) 2089 { 2090 static const u8 hdr[] = { 2091 0, 2092 0x80, /* this page code */ 2093 0, 2094 ATA_ID_SERNO_LEN, /* page len */ 2095 }; 2096 2097 memcpy(rbuf, hdr, sizeof(hdr)); 2098 ata_id_string(dev->id, (unsigned char *) &rbuf[4], 2099 ATA_ID_SERNO, ATA_ID_SERNO_LEN); 2100 2101 return get_unaligned_be16(&rbuf[2]) + 4; 2102 } 2103 2104 /** 2105 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity 2106 * @dev: Target device. 2107 * @cmd: SCSI command of interest. 2108 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2109 * 2110 * Yields two logical unit device identification designators: 2111 * - vendor specific ASCII containing the ATA serial number 2112 * - SAT defined "t10 vendor id based" containing ASCII vendor 2113 * name ("ATA "), model and serial numbers. 2114 * 2115 * LOCKING: 2116 * spin_lock_irqsave(host lock) 2117 */ 2118 static unsigned int ata_scsiop_inq_83(struct ata_device *dev, 2119 struct scsi_cmnd *cmd, u8 *rbuf) 2120 { 2121 const int sat_model_serial_desc_len = 68; 2122 int num; 2123 2124 rbuf[1] = 0x83; /* this page code */ 2125 num = 4; 2126 2127 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */ 2128 rbuf[num + 0] = 2; 2129 rbuf[num + 3] = ATA_ID_SERNO_LEN; 2130 num += 4; 2131 ata_id_string(dev->id, (unsigned char *) rbuf + num, 2132 ATA_ID_SERNO, ATA_ID_SERNO_LEN); 2133 num += ATA_ID_SERNO_LEN; 2134 2135 /* SAT defined lu model and serial numbers descriptor */ 2136 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */ 2137 rbuf[num + 0] = 2; 2138 rbuf[num + 1] = 1; 2139 rbuf[num + 3] = sat_model_serial_desc_len; 2140 num += 4; 2141 memcpy(rbuf + num, "ATA ", 8); 2142 num += 8; 2143 ata_id_string(dev->id, (unsigned char *) rbuf + num, ATA_ID_PROD, 2144 ATA_ID_PROD_LEN); 2145 num += ATA_ID_PROD_LEN; 2146 ata_id_string(dev->id, (unsigned char *) rbuf + num, ATA_ID_SERNO, 2147 ATA_ID_SERNO_LEN); 2148 num += ATA_ID_SERNO_LEN; 2149 2150 if (ata_id_has_wwn(dev->id)) { 2151 /* SAT defined lu world wide name */ 2152 /* piv=0, assoc=lu, code_set=binary, designator=NAA */ 2153 rbuf[num + 0] = 1; 2154 rbuf[num + 1] = 3; 2155 rbuf[num + 3] = ATA_ID_WWN_LEN; 2156 num += 4; 2157 ata_id_string(dev->id, (unsigned char *) rbuf + num, 2158 ATA_ID_WWN, ATA_ID_WWN_LEN); 2159 num += ATA_ID_WWN_LEN; 2160 } 2161 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */ 2162 2163 return get_unaligned_be16(&rbuf[2]) + 4; 2164 } 2165 2166 /** 2167 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info 2168 * @dev: Target device. 2169 * @cmd: SCSI command of interest. 2170 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2171 * 2172 * Yields SAT-specified ATA VPD page. 2173 * 2174 * LOCKING: 2175 * spin_lock_irqsave(host lock) 2176 */ 2177 static unsigned int ata_scsiop_inq_89(struct ata_device *dev, 2178 struct scsi_cmnd *cmd, u8 *rbuf) 2179 { 2180 rbuf[1] = 0x89; /* our page code */ 2181 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */ 2182 rbuf[3] = (0x238 & 0xff); 2183 2184 memcpy(&rbuf[8], "linux ", 8); 2185 memcpy(&rbuf[16], "libata ", 16); 2186 memcpy(&rbuf[32], DRV_VERSION, 4); 2187 2188 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */ 2189 rbuf[37] = (1 << 7); /* bit 7 indicates Command FIS */ 2190 /* TODO: PMP? */ 2191 2192 /* we don't store the ATA device signature, so we fake it */ 2193 rbuf[38] = ATA_DRDY; /* really, this is Status reg */ 2194 rbuf[40] = 0x1; 2195 rbuf[48] = 0x1; 2196 2197 rbuf[56] = ATA_CMD_ID_ATA; 2198 2199 memcpy(&rbuf[60], &dev->id[0], 512); 2200 2201 return get_unaligned_be16(&rbuf[2]) + 4; 2202 } 2203 2204 /** 2205 * ata_scsiop_inq_b0 - Simulate INQUIRY VPD page B0, Block Limits 2206 * @dev: Target device. 2207 * @cmd: SCSI command of interest. 2208 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2209 * 2210 * Return data for the VPD page B0h (Block Limits). 2211 * 2212 * LOCKING: 2213 * spin_lock_irqsave(host lock) 2214 */ 2215 static unsigned int ata_scsiop_inq_b0(struct ata_device *dev, 2216 struct scsi_cmnd *cmd, u8 *rbuf) 2217 { 2218 u16 min_io_sectors; 2219 2220 rbuf[1] = 0xb0; 2221 rbuf[3] = 0x3c; /* required VPD size with unmap support */ 2222 2223 /* 2224 * Optimal transfer length granularity. 2225 * 2226 * This is always one physical block, but for disks with a smaller 2227 * logical than physical sector size we need to figure out what the 2228 * latter is. 2229 */ 2230 min_io_sectors = 1 << ata_id_log2_per_physical_sector(dev->id); 2231 put_unaligned_be16(min_io_sectors, &rbuf[6]); 2232 2233 /* 2234 * Optimal unmap granularity. 2235 * 2236 * The ATA spec doesn't even know about a granularity or alignment 2237 * for the TRIM command. We can leave away most of the unmap related 2238 * VPD page entries, but we have specifify a granularity to signal 2239 * that we support some form of unmap - in thise case via WRITE SAME 2240 * with the unmap bit set. 2241 */ 2242 if (ata_id_has_trim(dev->id)) { 2243 u64 max_blocks = 65535 * ATA_MAX_TRIM_RNUM; 2244 2245 if (dev->quirks & ATA_QUIRK_MAX_TRIM_128M) 2246 max_blocks = 128 << (20 - SECTOR_SHIFT); 2247 2248 put_unaligned_be64(max_blocks, &rbuf[36]); 2249 put_unaligned_be32(1, &rbuf[28]); 2250 } 2251 2252 return get_unaligned_be16(&rbuf[2]) + 4; 2253 } 2254 2255 /** 2256 * ata_scsiop_inq_b1 - Simulate INQUIRY VPD page B1, Block Device 2257 * Characteristics 2258 * @dev: Target device. 2259 * @cmd: SCSI command of interest. 2260 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2261 * 2262 * Return data for the VPD page B1h (Block Device Characteristics). 2263 * 2264 * LOCKING: 2265 * spin_lock_irqsave(host lock) 2266 */ 2267 static unsigned int ata_scsiop_inq_b1(struct ata_device *dev, 2268 struct scsi_cmnd *cmd, u8 *rbuf) 2269 { 2270 int form_factor = ata_id_form_factor(dev->id); 2271 int media_rotation_rate = ata_id_rotation_rate(dev->id); 2272 u8 zoned = ata_id_zoned_cap(dev->id); 2273 2274 rbuf[1] = 0xb1; 2275 rbuf[3] = 0x3c; 2276 rbuf[4] = media_rotation_rate >> 8; 2277 rbuf[5] = media_rotation_rate; 2278 rbuf[7] = form_factor; 2279 if (zoned) 2280 rbuf[8] = (zoned << 4); 2281 2282 return get_unaligned_be16(&rbuf[2]) + 4; 2283 } 2284 2285 /** 2286 * ata_scsiop_inq_b2 - Simulate INQUIRY VPD page B2, Logical Block 2287 * Provisioning 2288 * @dev: Target device. 2289 * @cmd: SCSI command of interest. 2290 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2291 * 2292 * Return data for the VPD page B2h (Logical Block Provisioning). 2293 * 2294 * LOCKING: 2295 * spin_lock_irqsave(host lock) 2296 */ 2297 static unsigned int ata_scsiop_inq_b2(struct ata_device *dev, 2298 struct scsi_cmnd *cmd, u8 *rbuf) 2299 { 2300 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */ 2301 rbuf[1] = 0xb2; 2302 rbuf[3] = 0x4; 2303 rbuf[5] = 1 << 6; /* TPWS */ 2304 2305 return get_unaligned_be16(&rbuf[2]) + 4; 2306 } 2307 2308 /** 2309 * ata_scsiop_inq_b6 - Simulate INQUIRY VPD page B6, Zoned Block Device 2310 * Characteristics 2311 * @dev: Target device. 2312 * @cmd: SCSI command of interest. 2313 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2314 * 2315 * Return data for the VPD page B2h (Zoned Block Device Characteristics). 2316 * 2317 * LOCKING: 2318 * spin_lock_irqsave(host lock) 2319 */ 2320 static unsigned int ata_scsiop_inq_b6(struct ata_device *dev, 2321 struct scsi_cmnd *cmd, u8 *rbuf) 2322 { 2323 if (!ata_dev_is_zac(dev)) { 2324 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff); 2325 return 0; 2326 } 2327 2328 /* 2329 * zbc-r05 SCSI Zoned Block device characteristics VPD page 2330 */ 2331 rbuf[1] = 0xb6; 2332 rbuf[3] = 0x3C; 2333 2334 /* 2335 * URSWRZ bit is only meaningful for host-managed ZAC drives 2336 */ 2337 if (dev->zac_zoned_cap & 1) 2338 rbuf[4] |= 1; 2339 put_unaligned_be32(dev->zac_zones_optimal_open, &rbuf[8]); 2340 put_unaligned_be32(dev->zac_zones_optimal_nonseq, &rbuf[12]); 2341 put_unaligned_be32(dev->zac_zones_max_open, &rbuf[16]); 2342 2343 return get_unaligned_be16(&rbuf[2]) + 4; 2344 } 2345 2346 /** 2347 * ata_scsiop_inq_b9 - Simulate INQUIRY VPD page B9, Concurrent Positioning 2348 * Ranges 2349 * @dev: Target device. 2350 * @cmd: SCSI command of interest. 2351 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2352 * 2353 * Return data for the VPD page B9h (Concurrent Positioning Ranges). 2354 * 2355 * LOCKING: 2356 * spin_lock_irqsave(host lock) 2357 */ 2358 static unsigned int ata_scsiop_inq_b9(struct ata_device *dev, 2359 struct scsi_cmnd *cmd, u8 *rbuf) 2360 { 2361 struct ata_cpr_log *cpr_log = dev->cpr_log; 2362 u8 *desc = &rbuf[64]; 2363 int i; 2364 2365 if (!cpr_log) { 2366 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff); 2367 return 0; 2368 } 2369 2370 /* SCSI Concurrent Positioning Ranges VPD page: SBC-5 rev 1 or later */ 2371 rbuf[1] = 0xb9; 2372 put_unaligned_be16(64 + (int)cpr_log->nr_cpr * 32 - 4, &rbuf[2]); 2373 2374 for (i = 0; i < cpr_log->nr_cpr; i++, desc += 32) { 2375 desc[0] = cpr_log->cpr[i].num; 2376 desc[1] = cpr_log->cpr[i].num_storage_elements; 2377 put_unaligned_be64(cpr_log->cpr[i].start_lba, &desc[8]); 2378 put_unaligned_be64(cpr_log->cpr[i].num_lbas, &desc[16]); 2379 } 2380 2381 return get_unaligned_be16(&rbuf[2]) + 4; 2382 } 2383 2384 /** 2385 * ata_scsiop_inquiry - Simulate INQUIRY command 2386 * @dev: Target device. 2387 * @cmd: SCSI command of interest. 2388 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2389 * 2390 * Returns data associated with an INQUIRY command output. 2391 * 2392 * LOCKING: 2393 * spin_lock_irqsave(host lock) 2394 */ 2395 static unsigned int ata_scsiop_inquiry(struct ata_device *dev, 2396 struct scsi_cmnd *cmd, u8 *rbuf) 2397 { 2398 const u8 *scsicmd = cmd->cmnd; 2399 2400 /* is CmdDt set? */ 2401 if (scsicmd[1] & 2) { 2402 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff); 2403 return 0; 2404 } 2405 2406 /* Is EVPD clear? */ 2407 if ((scsicmd[1] & 1) == 0) 2408 return ata_scsiop_inq_std(dev, cmd, rbuf); 2409 2410 switch (scsicmd[2]) { 2411 case 0x00: 2412 return ata_scsiop_inq_00(dev, cmd, rbuf); 2413 case 0x80: 2414 return ata_scsiop_inq_80(dev, cmd, rbuf); 2415 case 0x83: 2416 return ata_scsiop_inq_83(dev, cmd, rbuf); 2417 case 0x89: 2418 return ata_scsiop_inq_89(dev, cmd, rbuf); 2419 case 0xb0: 2420 return ata_scsiop_inq_b0(dev, cmd, rbuf); 2421 case 0xb1: 2422 return ata_scsiop_inq_b1(dev, cmd, rbuf); 2423 case 0xb2: 2424 return ata_scsiop_inq_b2(dev, cmd, rbuf); 2425 case 0xb6: 2426 return ata_scsiop_inq_b6(dev, cmd, rbuf); 2427 case 0xb9: 2428 return ata_scsiop_inq_b9(dev, cmd, rbuf); 2429 default: 2430 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff); 2431 return 0; 2432 } 2433 } 2434 2435 /** 2436 * modecpy - Prepare response for MODE SENSE 2437 * @dest: output buffer 2438 * @src: data being copied 2439 * @n: length of mode page 2440 * @changeable: whether changeable parameters are requested 2441 * 2442 * Generate a generic MODE SENSE page for either current or changeable 2443 * parameters. 2444 * 2445 * LOCKING: 2446 * None. 2447 */ 2448 static void modecpy(u8 *dest, const u8 *src, int n, bool changeable) 2449 { 2450 if (changeable) { 2451 memcpy(dest, src, 2); 2452 memset(dest + 2, 0, n - 2); 2453 } else { 2454 memcpy(dest, src, n); 2455 } 2456 } 2457 2458 /** 2459 * ata_msense_caching - Simulate MODE SENSE caching info page 2460 * @id: device IDENTIFY data 2461 * @buf: output buffer 2462 * @changeable: whether changeable parameters are requested 2463 * 2464 * Generate a caching info page, which conditionally indicates 2465 * write caching to the SCSI layer, depending on device 2466 * capabilities. 2467 * 2468 * LOCKING: 2469 * None. 2470 */ 2471 static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable) 2472 { 2473 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable); 2474 if (changeable) { 2475 buf[2] |= (1 << 2); /* ata_mselect_caching() */ 2476 } else { 2477 buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */ 2478 buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */ 2479 } 2480 return sizeof(def_cache_mpage); 2481 } 2482 2483 /* 2484 * Simulate MODE SENSE control mode page, sub-page 0. 2485 */ 2486 static unsigned int ata_msense_control_spg0(struct ata_device *dev, u8 *buf, 2487 bool changeable) 2488 { 2489 modecpy(buf, def_control_mpage, 2490 sizeof(def_control_mpage), changeable); 2491 if (changeable) { 2492 /* ata_mselect_control() */ 2493 buf[2] |= (1 << 2); 2494 } else { 2495 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE); 2496 2497 /* descriptor format sense data */ 2498 buf[2] |= (d_sense << 2); 2499 } 2500 2501 return sizeof(def_control_mpage); 2502 } 2503 2504 /* 2505 * Translate an ATA duration limit in microseconds to a SCSI duration limit 2506 * using the t2cdlunits 0xa (10ms). Since the SCSI duration limits are 2-bytes 2507 * only, take care of overflows. 2508 */ 2509 static inline u16 ata_xlat_cdl_limit(u8 *buf) 2510 { 2511 u32 limit = get_unaligned_le32(buf); 2512 2513 return min_t(u32, limit / 10000, 65535); 2514 } 2515 2516 /* 2517 * Simulate MODE SENSE control mode page, sub-pages 07h and 08h 2518 * (command duration limits T2A and T2B mode pages). 2519 */ 2520 static unsigned int ata_msense_control_spgt2(struct ata_device *dev, u8 *buf, 2521 u8 spg) 2522 { 2523 u8 *b, *cdl, *desc; 2524 u32 policy; 2525 int i; 2526 2527 if (!(dev->flags & ATA_DFLAG_CDL) || !dev->cdl) 2528 return 0; 2529 2530 cdl = dev->cdl->desc_log_buf; 2531 2532 /* 2533 * Fill the subpage. The first four bytes of the T2A/T2B mode pages 2534 * are a header. The PAGE LENGTH field is the size of the page 2535 * excluding the header. 2536 */ 2537 buf[0] = CONTROL_MPAGE; 2538 buf[1] = spg; 2539 put_unaligned_be16(CDL_T2_SUB_MPAGE_LEN - 4, &buf[2]); 2540 if (spg == CDL_T2A_SUB_MPAGE) { 2541 /* 2542 * Read descriptors map to the T2A page: 2543 * set perf_vs_duration_guidleine. 2544 */ 2545 buf[7] = (cdl[0] & 0x03) << 4; 2546 desc = cdl + 64; 2547 } else { 2548 /* Write descriptors map to the T2B page */ 2549 desc = cdl + 288; 2550 } 2551 2552 /* Fill the T2 page descriptors */ 2553 b = &buf[8]; 2554 policy = get_unaligned_le32(&cdl[0]); 2555 for (i = 0; i < 7; i++, b += 32, desc += 32) { 2556 /* t2cdlunits: fixed to 10ms */ 2557 b[0] = 0x0a; 2558 2559 /* Max inactive time and its policy */ 2560 put_unaligned_be16(ata_xlat_cdl_limit(&desc[8]), &b[2]); 2561 b[6] = ((policy >> 8) & 0x0f) << 4; 2562 2563 /* Max active time and its policy */ 2564 put_unaligned_be16(ata_xlat_cdl_limit(&desc[4]), &b[4]); 2565 b[6] |= (policy >> 4) & 0x0f; 2566 2567 /* Command duration guideline and its policy */ 2568 put_unaligned_be16(ata_xlat_cdl_limit(&desc[16]), &b[10]); 2569 b[14] = policy & 0x0f; 2570 } 2571 2572 return CDL_T2_SUB_MPAGE_LEN; 2573 } 2574 2575 /* 2576 * Simulate MODE SENSE control mode page, sub-page f2h 2577 * (ATA feature control mode page). 2578 */ 2579 static unsigned int ata_msense_control_ata_feature(struct ata_device *dev, 2580 u8 *buf) 2581 { 2582 /* PS=0, SPF=1 */ 2583 buf[0] = CONTROL_MPAGE | (1 << 6); 2584 buf[1] = ATA_FEATURE_SUB_MPAGE; 2585 2586 /* 2587 * The first four bytes of ATA Feature Control mode page are a header. 2588 * The PAGE LENGTH field is the size of the page excluding the header. 2589 */ 2590 put_unaligned_be16(ATA_FEATURE_SUB_MPAGE_LEN - 4, &buf[2]); 2591 2592 if (dev->flags & ATA_DFLAG_CDL_ENABLED) 2593 buf[4] = 0x02; /* T2A and T2B pages enabled */ 2594 else 2595 buf[4] = 0; 2596 2597 return ATA_FEATURE_SUB_MPAGE_LEN; 2598 } 2599 2600 /** 2601 * ata_msense_control - Simulate MODE SENSE control mode page 2602 * @dev: ATA device of interest 2603 * @buf: output buffer 2604 * @spg: sub-page code 2605 * @changeable: whether changeable parameters are requested 2606 * 2607 * Generate a generic MODE SENSE control mode page. 2608 * 2609 * LOCKING: 2610 * None. 2611 */ 2612 static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf, 2613 u8 spg, bool changeable) 2614 { 2615 unsigned int n; 2616 2617 switch (spg) { 2618 case 0: 2619 return ata_msense_control_spg0(dev, buf, changeable); 2620 case CDL_T2A_SUB_MPAGE: 2621 case CDL_T2B_SUB_MPAGE: 2622 return ata_msense_control_spgt2(dev, buf, spg); 2623 case ATA_FEATURE_SUB_MPAGE: 2624 return ata_msense_control_ata_feature(dev, buf); 2625 case ALL_SUB_MPAGES: 2626 n = ata_msense_control_spg0(dev, buf, changeable); 2627 n += ata_msense_control_spgt2(dev, buf + n, CDL_T2A_SUB_MPAGE); 2628 n += ata_msense_control_spgt2(dev, buf + n, CDL_T2B_SUB_MPAGE); 2629 n += ata_msense_control_ata_feature(dev, buf + n); 2630 return n; 2631 default: 2632 return 0; 2633 } 2634 } 2635 2636 /** 2637 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page 2638 * @buf: output buffer 2639 * @changeable: whether changeable parameters are requested 2640 * 2641 * Generate a generic MODE SENSE r/w error recovery page. 2642 * 2643 * LOCKING: 2644 * None. 2645 */ 2646 static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable) 2647 { 2648 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage), 2649 changeable); 2650 return sizeof(def_rw_recovery_mpage); 2651 } 2652 2653 /** 2654 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands 2655 * @dev: Target device. 2656 * @cmd: SCSI command of interest. 2657 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2658 * 2659 * Simulate MODE SENSE commands. Assume this is invoked for direct 2660 * access devices (e.g. disks) only. There should be no block 2661 * descriptor for other device types. 2662 * 2663 * LOCKING: 2664 * spin_lock_irqsave(host lock) 2665 */ 2666 static unsigned int ata_scsiop_mode_sense(struct ata_device *dev, 2667 struct scsi_cmnd *cmd, u8 *rbuf) 2668 { 2669 u8 *scsicmd = cmd->cmnd, *p = rbuf; 2670 static const u8 sat_blk_desc[] = { 2671 0, 0, 0, 0, /* number of blocks: sat unspecified */ 2672 0, 2673 0, 0x2, 0x0 /* block length: 512 bytes */ 2674 }; 2675 u8 pg, spg; 2676 unsigned int ebd, page_control, six_byte; 2677 u8 dpofua = 0, bp = 0xff; 2678 u16 fp; 2679 2680 six_byte = (scsicmd[0] == MODE_SENSE); 2681 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */ 2682 /* 2683 * LLBA bit in msense(10) ignored (compliant) 2684 */ 2685 2686 page_control = scsicmd[2] >> 6; 2687 switch (page_control) { 2688 case 0: /* current */ 2689 case 1: /* changeable */ 2690 case 2: /* defaults */ 2691 break; /* supported */ 2692 case 3: /* saved */ 2693 goto saving_not_supp; 2694 default: 2695 fp = 2; 2696 bp = 6; 2697 goto invalid_fld; 2698 } 2699 2700 if (six_byte) 2701 p += 4 + (ebd ? 8 : 0); 2702 else 2703 p += 8 + (ebd ? 8 : 0); 2704 2705 pg = scsicmd[2] & 0x3f; 2706 spg = scsicmd[3]; 2707 2708 /* 2709 * Supported subpages: all subpages and sub-pages 07h, 08h and f2h of 2710 * the control page. 2711 */ 2712 if (spg) { 2713 switch (spg) { 2714 case ALL_SUB_MPAGES: 2715 break; 2716 case CDL_T2A_SUB_MPAGE: 2717 case CDL_T2B_SUB_MPAGE: 2718 case ATA_FEATURE_SUB_MPAGE: 2719 if (dev->flags & ATA_DFLAG_CDL && pg == CONTROL_MPAGE) 2720 break; 2721 fallthrough; 2722 default: 2723 fp = 3; 2724 goto invalid_fld; 2725 } 2726 } 2727 2728 switch(pg) { 2729 case RW_RECOVERY_MPAGE: 2730 p += ata_msense_rw_recovery(p, page_control == 1); 2731 break; 2732 2733 case CACHE_MPAGE: 2734 p += ata_msense_caching(dev->id, p, page_control == 1); 2735 break; 2736 2737 case CONTROL_MPAGE: 2738 p += ata_msense_control(dev, p, spg, page_control == 1); 2739 break; 2740 2741 case ALL_MPAGES: 2742 p += ata_msense_rw_recovery(p, page_control == 1); 2743 p += ata_msense_caching(dev->id, p, page_control == 1); 2744 p += ata_msense_control(dev, p, spg, page_control == 1); 2745 break; 2746 2747 default: /* invalid page code */ 2748 fp = 2; 2749 goto invalid_fld; 2750 } 2751 2752 if (dev->flags & ATA_DFLAG_FUA) 2753 dpofua = 1 << 4; 2754 2755 if (six_byte) { 2756 rbuf[0] = p - rbuf - 1; 2757 rbuf[2] |= dpofua; 2758 if (ebd) { 2759 rbuf[3] = sizeof(sat_blk_desc); 2760 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc)); 2761 } 2762 2763 return rbuf[0] + 1; 2764 } 2765 2766 put_unaligned_be16(p - rbuf - 2, &rbuf[0]); 2767 rbuf[3] |= dpofua; 2768 if (ebd) { 2769 rbuf[7] = sizeof(sat_blk_desc); 2770 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc)); 2771 } 2772 2773 return get_unaligned_be16(&rbuf[0]) + 2; 2774 2775 invalid_fld: 2776 ata_scsi_set_invalid_field(dev, cmd, fp, bp); 2777 return 0; 2778 2779 saving_not_supp: 2780 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x39, 0x0); 2781 /* "Saving parameters not supported" */ 2782 return 0; 2783 } 2784 2785 /** 2786 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands 2787 * @dev: Target device. 2788 * @cmd: SCSI command of interest. 2789 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2790 * 2791 * Simulate READ CAPACITY commands. 2792 * 2793 * LOCKING: 2794 * None. 2795 */ 2796 static unsigned int ata_scsiop_read_cap(struct ata_device *dev, 2797 struct scsi_cmnd *cmd, u8 *rbuf) 2798 { 2799 u8 *scsicmd = cmd->cmnd; 2800 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */ 2801 u32 sector_size; /* physical sector size in bytes */ 2802 u8 log2_per_phys; 2803 u16 lowest_aligned; 2804 2805 sector_size = ata_id_logical_sector_size(dev->id); 2806 log2_per_phys = ata_id_log2_per_physical_sector(dev->id); 2807 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys); 2808 2809 if (scsicmd[0] == READ_CAPACITY) { 2810 if (last_lba >= 0xffffffffULL) 2811 last_lba = 0xffffffff; 2812 2813 /* sector count, 32-bit */ 2814 rbuf[0] = last_lba >> (8 * 3); 2815 rbuf[1] = last_lba >> (8 * 2); 2816 rbuf[2] = last_lba >> (8 * 1); 2817 rbuf[3] = last_lba; 2818 2819 /* sector size */ 2820 rbuf[4] = sector_size >> (8 * 3); 2821 rbuf[5] = sector_size >> (8 * 2); 2822 rbuf[6] = sector_size >> (8 * 1); 2823 rbuf[7] = sector_size; 2824 2825 return 8; 2826 } 2827 2828 /* 2829 * READ CAPACITY 16 command is defined as a service action 2830 * (SERVICE_ACTION_IN_16 command). 2831 */ 2832 if (scsicmd[0] != SERVICE_ACTION_IN_16 || 2833 (scsicmd[1] & 0x1f) != SAI_READ_CAPACITY_16) { 2834 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff); 2835 return 0; 2836 } 2837 2838 /* sector count, 64-bit */ 2839 rbuf[0] = last_lba >> (8 * 7); 2840 rbuf[1] = last_lba >> (8 * 6); 2841 rbuf[2] = last_lba >> (8 * 5); 2842 rbuf[3] = last_lba >> (8 * 4); 2843 rbuf[4] = last_lba >> (8 * 3); 2844 rbuf[5] = last_lba >> (8 * 2); 2845 rbuf[6] = last_lba >> (8 * 1); 2846 rbuf[7] = last_lba; 2847 2848 /* sector size */ 2849 rbuf[ 8] = sector_size >> (8 * 3); 2850 rbuf[ 9] = sector_size >> (8 * 2); 2851 rbuf[10] = sector_size >> (8 * 1); 2852 rbuf[11] = sector_size; 2853 2854 if (ata_id_zoned_cap(dev->id) || dev->class == ATA_DEV_ZAC) 2855 rbuf[12] = (1 << 4); /* RC_BASIS */ 2856 rbuf[13] = log2_per_phys; 2857 rbuf[14] = (lowest_aligned >> 8) & 0x3f; 2858 rbuf[15] = lowest_aligned; 2859 2860 if (ata_id_has_trim(dev->id) && !(dev->quirks & ATA_QUIRK_NOTRIM)) { 2861 rbuf[14] |= 0x80; /* LBPME */ 2862 2863 if (ata_id_has_zero_after_trim(dev->id) && 2864 dev->quirks & ATA_QUIRK_ZERO_AFTER_TRIM) { 2865 ata_dev_info(dev, "Enabling discard_zeroes_data\n"); 2866 rbuf[14] |= 0x40; /* LBPRZ */ 2867 } 2868 } 2869 2870 return 16; 2871 } 2872 2873 /** 2874 * ata_scsiop_report_luns - Simulate REPORT LUNS command 2875 * @dev: Target device. 2876 * @cmd: SCSI command of interest. 2877 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 2878 * 2879 * Simulate REPORT LUNS command. 2880 * 2881 * LOCKING: 2882 * spin_lock_irqsave(host lock) 2883 */ 2884 static unsigned int ata_scsiop_report_luns(struct ata_device *dev, 2885 struct scsi_cmnd *cmd, u8 *rbuf) 2886 { 2887 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */ 2888 2889 return 16; 2890 } 2891 2892 /* 2893 * ATAPI devices typically report zero for their SCSI version, and sometimes 2894 * deviate from the spec WRT response data format. If SCSI version is 2895 * reported as zero like normal, then we make the following fixups: 2896 * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a 2897 * modern device. 2898 * 2) Ensure response data format / ATAPI information are always correct. 2899 */ 2900 static void atapi_fixup_inquiry(struct scsi_cmnd *cmd) 2901 { 2902 u8 buf[4]; 2903 2904 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4); 2905 if (buf[2] == 0) { 2906 buf[2] = 0x5; 2907 buf[3] = 0x32; 2908 } 2909 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4); 2910 } 2911 2912 static void atapi_qc_complete(struct ata_queued_cmd *qc) 2913 { 2914 struct scsi_cmnd *cmd = qc->scsicmd; 2915 unsigned int err_mask = qc->err_mask; 2916 2917 /* handle completion from EH */ 2918 if (unlikely(err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID)) { 2919 2920 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) 2921 ata_gen_passthru_sense(qc); 2922 2923 /* SCSI EH automatically locks door if sdev->locked is 2924 * set. Sometimes door lock request continues to 2925 * fail, for example, when no media is present. This 2926 * creates a loop - SCSI EH issues door lock which 2927 * fails and gets invoked again to acquire sense data 2928 * for the failed command. 2929 * 2930 * If door lock fails, always clear sdev->locked to 2931 * avoid this infinite loop. 2932 * 2933 * This may happen before SCSI scan is complete. Make 2934 * sure qc->dev->sdev isn't NULL before dereferencing. 2935 */ 2936 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev) 2937 qc->dev->sdev->locked = 0; 2938 2939 ata_scsi_qc_done(qc, true, SAM_STAT_CHECK_CONDITION); 2940 return; 2941 } 2942 2943 /* successful completion path */ 2944 if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0) 2945 atapi_fixup_inquiry(cmd); 2946 2947 ata_scsi_qc_done(qc, true, SAM_STAT_GOOD); 2948 } 2949 /** 2950 * atapi_xlat - Initialize PACKET taskfile 2951 * @qc: command structure to be initialized 2952 * 2953 * LOCKING: 2954 * spin_lock_irqsave(host lock) 2955 * 2956 * RETURNS: 2957 * Zero on success, non-zero on failure. 2958 */ 2959 static unsigned int atapi_xlat(struct ata_queued_cmd *qc) 2960 { 2961 struct scsi_cmnd *scmd = qc->scsicmd; 2962 struct ata_device *dev = qc->dev; 2963 int nodata = (scmd->sc_data_direction == DMA_NONE); 2964 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO); 2965 unsigned int nbytes; 2966 2967 memset(qc->cdb, 0, dev->cdb_len); 2968 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len); 2969 2970 qc->complete_fn = atapi_qc_complete; 2971 2972 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 2973 if (scmd->sc_data_direction == DMA_TO_DEVICE) { 2974 qc->tf.flags |= ATA_TFLAG_WRITE; 2975 } 2976 2977 qc->tf.command = ATA_CMD_PACKET; 2978 ata_qc_set_pc_nbytes(qc); 2979 2980 /* check whether ATAPI DMA is safe */ 2981 if (!nodata && !using_pio && atapi_check_dma(qc)) 2982 using_pio = 1; 2983 2984 /* Some controller variants snoop this value for Packet 2985 * transfers to do state machine and FIFO management. Thus we 2986 * want to set it properly, and for DMA where it is 2987 * effectively meaningless. 2988 */ 2989 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024); 2990 2991 /* Most ATAPI devices which honor transfer chunk size don't 2992 * behave according to the spec when odd chunk size which 2993 * matches the transfer length is specified. If the number of 2994 * bytes to transfer is 2n+1. According to the spec, what 2995 * should happen is to indicate that 2n+1 is going to be 2996 * transferred and transfer 2n+2 bytes where the last byte is 2997 * padding. 2998 * 2999 * In practice, this doesn't happen. ATAPI devices first 3000 * indicate and transfer 2n bytes and then indicate and 3001 * transfer 2 bytes where the last byte is padding. 3002 * 3003 * This inconsistency confuses several controllers which 3004 * perform PIO using DMA such as Intel AHCIs and sil3124/32. 3005 * These controllers use actual number of transferred bytes to 3006 * update DMA pointer and transfer of 4n+2 bytes make those 3007 * controller push DMA pointer by 4n+4 bytes because SATA data 3008 * FISes are aligned to 4 bytes. This causes data corruption 3009 * and buffer overrun. 3010 * 3011 * Always setting nbytes to even number solves this problem 3012 * because then ATAPI devices don't have to split data at 2n 3013 * boundaries. 3014 */ 3015 if (nbytes & 0x1) 3016 nbytes++; 3017 3018 qc->tf.lbam = (nbytes & 0xFF); 3019 qc->tf.lbah = (nbytes >> 8); 3020 3021 if (nodata) 3022 qc->tf.protocol = ATAPI_PROT_NODATA; 3023 else if (using_pio) 3024 qc->tf.protocol = ATAPI_PROT_PIO; 3025 else { 3026 /* DMA data xfer */ 3027 qc->tf.protocol = ATAPI_PROT_DMA; 3028 qc->tf.feature |= ATAPI_PKT_DMA; 3029 3030 if ((dev->flags & ATA_DFLAG_DMADIR) && 3031 (scmd->sc_data_direction != DMA_TO_DEVICE)) 3032 /* some SATA bridges need us to indicate data xfer direction */ 3033 qc->tf.feature |= ATAPI_DMADIR; 3034 } 3035 3036 3037 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE 3038 as ATAPI tape drives don't get this right otherwise */ 3039 return 0; 3040 } 3041 3042 static struct ata_device *ata_find_dev(struct ata_port *ap, unsigned int devno) 3043 { 3044 /* 3045 * For the non-PMP case, ata_link_max_devices() returns 1 (SATA case), 3046 * or 2 (IDE master + slave case). However, the former case includes 3047 * libsas hosted devices which are numbered per scsi host, leading 3048 * to devno potentially being larger than 0 but with each struct 3049 * ata_device having its own struct ata_port and struct ata_link. 3050 * To accommodate these, ignore devno and always use device number 0. 3051 */ 3052 if (likely(!sata_pmp_attached(ap))) { 3053 int link_max_devices = ata_link_max_devices(&ap->link); 3054 3055 if (link_max_devices == 1) 3056 return &ap->link.device[0]; 3057 3058 if (devno < link_max_devices) 3059 return &ap->link.device[devno]; 3060 3061 return NULL; 3062 } 3063 3064 /* 3065 * For PMP-attached devices, the device number corresponds to C 3066 * (channel) of SCSI [H:C:I:L], indicating the port pmp link 3067 * for the device. 3068 */ 3069 if (devno < ap->nr_pmp_links) 3070 return &ap->pmp_link[devno].device[0]; 3071 3072 return NULL; 3073 } 3074 3075 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap, 3076 const struct scsi_device *scsidev) 3077 { 3078 int devno; 3079 3080 /* skip commands not addressed to targets we simulate */ 3081 if (!sata_pmp_attached(ap)) { 3082 if (unlikely(scsidev->channel || scsidev->lun)) 3083 return NULL; 3084 devno = scsidev->id; 3085 } else { 3086 if (unlikely(scsidev->id || scsidev->lun)) 3087 return NULL; 3088 devno = scsidev->channel; 3089 } 3090 3091 return ata_find_dev(ap, devno); 3092 } 3093 3094 /** 3095 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd 3096 * @ap: ATA port to which the device is attached 3097 * @scsidev: SCSI device from which we derive the ATA device 3098 * 3099 * Given various information provided in struct scsi_cmnd, 3100 * map that onto an ATA bus, and using that mapping 3101 * determine which ata_device is associated with the 3102 * SCSI command to be sent. 3103 * 3104 * LOCKING: 3105 * spin_lock_irqsave(host lock) 3106 * 3107 * RETURNS: 3108 * Associated ATA device, or %NULL if not found. 3109 */ 3110 struct ata_device * 3111 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev) 3112 { 3113 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev); 3114 3115 if (!ata_adapter_is_online(ap)) 3116 return NULL; 3117 3118 if (unlikely(!dev || !ata_dev_enabled(dev))) 3119 return NULL; 3120 3121 return dev; 3122 } 3123 3124 /* 3125 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value. 3126 * @byte1: Byte 1 from pass-thru CDB. 3127 * 3128 * RETURNS: 3129 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise. 3130 */ 3131 static u8 3132 ata_scsi_map_proto(u8 byte1) 3133 { 3134 switch((byte1 & 0x1e) >> 1) { 3135 case 3: /* Non-data */ 3136 return ATA_PROT_NODATA; 3137 3138 case 6: /* DMA */ 3139 case 10: /* UDMA Data-in */ 3140 case 11: /* UDMA Data-Out */ 3141 return ATA_PROT_DMA; 3142 3143 case 4: /* PIO Data-in */ 3144 case 5: /* PIO Data-out */ 3145 return ATA_PROT_PIO; 3146 3147 case 12: /* FPDMA */ 3148 return ATA_PROT_NCQ; 3149 3150 case 0: /* Hard Reset */ 3151 case 1: /* SRST */ 3152 case 8: /* Device Diagnostic */ 3153 case 9: /* Device Reset */ 3154 case 7: /* DMA Queued */ 3155 case 15: /* Return Response Info */ 3156 default: /* Reserved */ 3157 break; 3158 } 3159 3160 return ATA_PROT_UNKNOWN; 3161 } 3162 3163 /** 3164 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile 3165 * @qc: command structure to be initialized 3166 * 3167 * Handles either 12, 16, or 32-byte versions of the CDB. 3168 * 3169 * RETURNS: 3170 * Zero on success, non-zero on failure. 3171 */ 3172 static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc) 3173 { 3174 struct ata_taskfile *tf = &(qc->tf); 3175 struct scsi_cmnd *scmd = qc->scsicmd; 3176 struct ata_device *dev = qc->dev; 3177 const u8 *cdb = scmd->cmnd; 3178 u16 fp; 3179 u16 cdb_offset = 0; 3180 3181 /* 7Fh variable length cmd means a ata pass-thru(32) */ 3182 if (cdb[0] == VARIABLE_LENGTH_CMD) 3183 cdb_offset = 9; 3184 3185 tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]); 3186 if (tf->protocol == ATA_PROT_UNKNOWN) { 3187 fp = 1; 3188 goto invalid_fld; 3189 } 3190 3191 if ((cdb[2 + cdb_offset] & 0x3) == 0) { 3192 /* 3193 * When T_LENGTH is zero (No data is transferred), dir should 3194 * be DMA_NONE. 3195 */ 3196 if (scmd->sc_data_direction != DMA_NONE) { 3197 fp = 2 + cdb_offset; 3198 goto invalid_fld; 3199 } 3200 3201 if (ata_is_ncq(tf->protocol)) 3202 tf->protocol = ATA_PROT_NCQ_NODATA; 3203 } 3204 3205 /* enable LBA */ 3206 tf->flags |= ATA_TFLAG_LBA; 3207 3208 /* 3209 * 12 and 16 byte CDBs use different offsets to 3210 * provide the various register values. 3211 */ 3212 switch (cdb[0]) { 3213 case ATA_16: 3214 /* 3215 * 16-byte CDB - may contain extended commands. 3216 * 3217 * If that is the case, copy the upper byte register values. 3218 */ 3219 if (cdb[1] & 0x01) { 3220 tf->hob_feature = cdb[3]; 3221 tf->hob_nsect = cdb[5]; 3222 tf->hob_lbal = cdb[7]; 3223 tf->hob_lbam = cdb[9]; 3224 tf->hob_lbah = cdb[11]; 3225 tf->flags |= ATA_TFLAG_LBA48; 3226 } else 3227 tf->flags &= ~ATA_TFLAG_LBA48; 3228 3229 /* 3230 * Always copy low byte, device and command registers. 3231 */ 3232 tf->feature = cdb[4]; 3233 tf->nsect = cdb[6]; 3234 tf->lbal = cdb[8]; 3235 tf->lbam = cdb[10]; 3236 tf->lbah = cdb[12]; 3237 tf->device = cdb[13]; 3238 tf->command = cdb[14]; 3239 break; 3240 case ATA_12: 3241 /* 3242 * 12-byte CDB - incapable of extended commands. 3243 */ 3244 tf->flags &= ~ATA_TFLAG_LBA48; 3245 3246 tf->feature = cdb[3]; 3247 tf->nsect = cdb[4]; 3248 tf->lbal = cdb[5]; 3249 tf->lbam = cdb[6]; 3250 tf->lbah = cdb[7]; 3251 tf->device = cdb[8]; 3252 tf->command = cdb[9]; 3253 break; 3254 default: 3255 /* 3256 * 32-byte CDB - may contain extended command fields. 3257 * 3258 * If that is the case, copy the upper byte register values. 3259 */ 3260 if (cdb[10] & 0x01) { 3261 tf->hob_feature = cdb[20]; 3262 tf->hob_nsect = cdb[22]; 3263 tf->hob_lbal = cdb[16]; 3264 tf->hob_lbam = cdb[15]; 3265 tf->hob_lbah = cdb[14]; 3266 tf->flags |= ATA_TFLAG_LBA48; 3267 } else 3268 tf->flags &= ~ATA_TFLAG_LBA48; 3269 3270 tf->feature = cdb[21]; 3271 tf->nsect = cdb[23]; 3272 tf->lbal = cdb[19]; 3273 tf->lbam = cdb[18]; 3274 tf->lbah = cdb[17]; 3275 tf->device = cdb[24]; 3276 tf->command = cdb[25]; 3277 tf->auxiliary = get_unaligned_be32(&cdb[28]); 3278 break; 3279 } 3280 3281 /* For NCQ commands copy the tag value */ 3282 if (ata_is_ncq(tf->protocol)) 3283 tf->nsect = qc->hw_tag << 3; 3284 3285 /* enforce correct master/slave bit */ 3286 tf->device = dev->devno ? 3287 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1; 3288 3289 switch (tf->command) { 3290 /* READ/WRITE LONG use a non-standard sect_size */ 3291 case ATA_CMD_READ_LONG: 3292 case ATA_CMD_READ_LONG_ONCE: 3293 case ATA_CMD_WRITE_LONG: 3294 case ATA_CMD_WRITE_LONG_ONCE: 3295 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) { 3296 fp = 1; 3297 goto invalid_fld; 3298 } 3299 qc->sect_size = scsi_bufflen(scmd); 3300 break; 3301 3302 /* commands using reported Logical Block size (e.g. 512 or 4K) */ 3303 case ATA_CMD_CFA_WRITE_NE: 3304 case ATA_CMD_CFA_TRANS_SECT: 3305 case ATA_CMD_CFA_WRITE_MULT_NE: 3306 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */ 3307 case ATA_CMD_READ: 3308 case ATA_CMD_READ_EXT: 3309 case ATA_CMD_READ_QUEUED: 3310 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */ 3311 case ATA_CMD_FPDMA_READ: 3312 case ATA_CMD_READ_MULTI: 3313 case ATA_CMD_READ_MULTI_EXT: 3314 case ATA_CMD_PIO_READ: 3315 case ATA_CMD_PIO_READ_EXT: 3316 case ATA_CMD_READ_STREAM_DMA_EXT: 3317 case ATA_CMD_READ_STREAM_EXT: 3318 case ATA_CMD_VERIFY: 3319 case ATA_CMD_VERIFY_EXT: 3320 case ATA_CMD_WRITE: 3321 case ATA_CMD_WRITE_EXT: 3322 case ATA_CMD_WRITE_FUA_EXT: 3323 case ATA_CMD_WRITE_QUEUED: 3324 case ATA_CMD_WRITE_QUEUED_FUA_EXT: 3325 case ATA_CMD_FPDMA_WRITE: 3326 case ATA_CMD_WRITE_MULTI: 3327 case ATA_CMD_WRITE_MULTI_EXT: 3328 case ATA_CMD_WRITE_MULTI_FUA_EXT: 3329 case ATA_CMD_PIO_WRITE: 3330 case ATA_CMD_PIO_WRITE_EXT: 3331 case ATA_CMD_WRITE_STREAM_DMA_EXT: 3332 case ATA_CMD_WRITE_STREAM_EXT: 3333 qc->sect_size = scmd->device->sector_size; 3334 break; 3335 3336 /* Everything else uses 512 byte "sectors" */ 3337 default: 3338 qc->sect_size = ATA_SECT_SIZE; 3339 } 3340 3341 /* 3342 * Set flags so that all registers will be written, pass on 3343 * write indication (used for PIO/DMA setup), result TF is 3344 * copied back and we don't whine too much about its failure. 3345 */ 3346 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 3347 if (scmd->sc_data_direction == DMA_TO_DEVICE) 3348 tf->flags |= ATA_TFLAG_WRITE; 3349 3350 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET; 3351 3352 /* 3353 * Set transfer length. 3354 * 3355 * TODO: find out if we need to do more here to 3356 * cover scatter/gather case. 3357 */ 3358 ata_qc_set_pc_nbytes(qc); 3359 3360 /* We may not issue DMA commands if no DMA mode is set */ 3361 if (tf->protocol == ATA_PROT_DMA && !ata_dma_enabled(dev)) { 3362 fp = 1; 3363 goto invalid_fld; 3364 } 3365 3366 /* We may not issue NCQ commands to devices not supporting NCQ */ 3367 if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) { 3368 fp = 1; 3369 goto invalid_fld; 3370 } 3371 3372 /* sanity check for pio multi commands */ 3373 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) { 3374 fp = 1; 3375 goto invalid_fld; 3376 } 3377 3378 if (is_multi_taskfile(tf)) { 3379 unsigned int multi_count = 1 << (cdb[1] >> 5); 3380 3381 /* compare the passed through multi_count 3382 * with the cached multi_count of libata 3383 */ 3384 if (multi_count != dev->multi_count) 3385 ata_dev_warn(dev, "invalid multi_count %u ignored\n", 3386 multi_count); 3387 } 3388 3389 /* 3390 * Filter SET_FEATURES - XFER MODE command -- otherwise, 3391 * SET_FEATURES - XFER MODE must be preceded/succeeded 3392 * by an update to hardware-specific registers for each 3393 * controller (i.e. the reason for ->set_piomode(), 3394 * ->set_dmamode(), and ->post_set_mode() hooks). 3395 */ 3396 if (tf->command == ATA_CMD_SET_FEATURES && 3397 tf->feature == SETFEATURES_XFER) { 3398 fp = (cdb[0] == ATA_16) ? 4 : 3; 3399 goto invalid_fld; 3400 } 3401 3402 /* 3403 * Filter TPM commands by default. These provide an 3404 * essentially uncontrolled encrypted "back door" between 3405 * applications and the disk. Set libata.allow_tpm=1 if you 3406 * have a real reason for wanting to use them. This ensures 3407 * that installed software cannot easily mess stuff up without 3408 * user intent. DVR type users will probably ship with this enabled 3409 * for movie content management. 3410 * 3411 * Note that for ATA8 we can issue a DCS change and DCS freeze lock 3412 * for this and should do in future but that it is not sufficient as 3413 * DCS is an optional feature set. Thus we also do the software filter 3414 * so that we comply with the TC consortium stated goal that the user 3415 * can turn off TC features of their system. 3416 */ 3417 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) { 3418 fp = (cdb[0] == ATA_16) ? 14 : 9; 3419 goto invalid_fld; 3420 } 3421 3422 return 0; 3423 3424 invalid_fld: 3425 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff); 3426 return 1; 3427 } 3428 3429 /** 3430 * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim 3431 * @cmd: SCSI command being translated 3432 * @trmax: Maximum number of entries that will fit in sector_size bytes. 3433 * @sector: Starting sector 3434 * @count: Total Range of request in logical sectors 3435 * 3436 * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted 3437 * descriptor. 3438 * 3439 * Upto 64 entries of the format: 3440 * 63:48 Range Length 3441 * 47:0 LBA 3442 * 3443 * Range Length of 0 is ignored. 3444 * LBA's should be sorted order and not overlap. 3445 * 3446 * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET 3447 * 3448 * Return: Number of bytes copied into sglist. 3449 */ 3450 static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax, 3451 u64 sector, u32 count) 3452 { 3453 struct scsi_device *sdp = cmd->device; 3454 size_t len = sdp->sector_size; 3455 size_t r; 3456 __le64 *buf; 3457 u32 i = 0; 3458 unsigned long flags; 3459 3460 WARN_ON(len > ATA_SCSI_RBUF_SIZE); 3461 3462 if (len > ATA_SCSI_RBUF_SIZE) 3463 len = ATA_SCSI_RBUF_SIZE; 3464 3465 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags); 3466 buf = ((void *)ata_scsi_rbuf); 3467 memset(buf, 0, len); 3468 while (i < trmax) { 3469 u64 entry = sector | 3470 ((u64)(count > 0xffff ? 0xffff : count) << 48); 3471 buf[i++] = __cpu_to_le64(entry); 3472 if (count <= 0xffff) 3473 break; 3474 count -= 0xffff; 3475 sector += 0xffff; 3476 } 3477 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len); 3478 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags); 3479 3480 return r; 3481 } 3482 3483 /** 3484 * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same 3485 * @qc: Command to be translated 3486 * 3487 * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or 3488 * an SCT Write Same command. 3489 * Based on WRITE SAME has the UNMAP flag: 3490 * 3491 * - When set translate to DSM TRIM 3492 * - When clear translate to SCT Write Same 3493 */ 3494 static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc) 3495 { 3496 struct ata_taskfile *tf = &qc->tf; 3497 struct scsi_cmnd *scmd = qc->scsicmd; 3498 struct scsi_device *sdp = scmd->device; 3499 size_t len = sdp->sector_size; 3500 struct ata_device *dev = qc->dev; 3501 const u8 *cdb = scmd->cmnd; 3502 u64 block; 3503 u32 n_block; 3504 const u32 trmax = len >> 3; 3505 u32 size; 3506 u16 fp; 3507 u8 bp = 0xff; 3508 u8 unmap = cdb[1] & 0x8; 3509 3510 /* we may not issue DMA commands if no DMA mode is set */ 3511 if (unlikely(!ata_dma_enabled(dev))) 3512 goto invalid_opcode; 3513 3514 /* 3515 * We only allow sending this command through the block layer, 3516 * as it modifies the DATA OUT buffer, which would corrupt user 3517 * memory for SG_IO commands. 3518 */ 3519 if (unlikely(blk_rq_is_passthrough(scsi_cmd_to_rq(scmd)))) 3520 goto invalid_opcode; 3521 3522 if (unlikely(scmd->cmd_len < 16)) { 3523 fp = 15; 3524 goto invalid_fld; 3525 } 3526 scsi_16_lba_len(cdb, &block, &n_block); 3527 3528 if (!unmap || (dev->quirks & ATA_QUIRK_NOTRIM) || 3529 !ata_id_has_trim(dev->id)) { 3530 fp = 1; 3531 bp = 3; 3532 goto invalid_fld; 3533 } 3534 /* If the request is too large the cmd is invalid */ 3535 if (n_block > 0xffff * trmax) { 3536 fp = 2; 3537 goto invalid_fld; 3538 } 3539 3540 /* 3541 * WRITE SAME always has a sector sized buffer as payload, this 3542 * should never be a multiple entry S/G list. 3543 */ 3544 if (!scsi_sg_count(scmd)) 3545 goto invalid_param_len; 3546 3547 /* 3548 * size must match sector size in bytes 3549 * For DATA SET MANAGEMENT TRIM in ACS-2 nsect (aka count) 3550 * is defined as number of 512 byte blocks to be transferred. 3551 */ 3552 3553 size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block); 3554 if (size != len) 3555 goto invalid_param_len; 3556 3557 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) { 3558 /* Newer devices support queued TRIM commands */ 3559 tf->protocol = ATA_PROT_NCQ; 3560 tf->command = ATA_CMD_FPDMA_SEND; 3561 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f; 3562 tf->nsect = qc->hw_tag << 3; 3563 tf->hob_feature = (size / 512) >> 8; 3564 tf->feature = size / 512; 3565 3566 tf->auxiliary = 1; 3567 } else { 3568 tf->protocol = ATA_PROT_DMA; 3569 tf->hob_feature = 0; 3570 tf->feature = ATA_DSM_TRIM; 3571 tf->hob_nsect = (size / 512) >> 8; 3572 tf->nsect = size / 512; 3573 tf->command = ATA_CMD_DSM; 3574 } 3575 3576 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 | 3577 ATA_TFLAG_WRITE; 3578 3579 ata_qc_set_pc_nbytes(qc); 3580 3581 return 0; 3582 3583 invalid_fld: 3584 ata_scsi_set_invalid_field(dev, scmd, fp, bp); 3585 return 1; 3586 invalid_param_len: 3587 /* "Parameter list length error" */ 3588 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0); 3589 return 1; 3590 invalid_opcode: 3591 /* "Invalid command operation code" */ 3592 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0); 3593 return 1; 3594 } 3595 3596 static unsigned int ata_scsi_report_supported_opcodes(struct ata_device *dev, 3597 struct scsi_cmnd *cmd, 3598 u8 *rbuf) 3599 { 3600 u8 *cdb = cmd->cmnd; 3601 u8 supported = 0, cdlp = 0, rwcdlp = 0; 3602 3603 if (cdb[2] != 1 && cdb[2] != 3) { 3604 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]); 3605 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff); 3606 return 0; 3607 } 3608 3609 switch (cdb[3]) { 3610 case INQUIRY: 3611 case MODE_SENSE: 3612 case MODE_SENSE_10: 3613 case READ_CAPACITY: 3614 case SERVICE_ACTION_IN_16: 3615 case REPORT_LUNS: 3616 case REQUEST_SENSE: 3617 case SYNCHRONIZE_CACHE: 3618 case SYNCHRONIZE_CACHE_16: 3619 case REZERO_UNIT: 3620 case SEEK_6: 3621 case SEEK_10: 3622 case TEST_UNIT_READY: 3623 case SEND_DIAGNOSTIC: 3624 case MAINTENANCE_IN: 3625 case READ_6: 3626 case READ_10: 3627 case WRITE_6: 3628 case WRITE_10: 3629 case ATA_12: 3630 case ATA_16: 3631 case VERIFY: 3632 case VERIFY_16: 3633 case MODE_SELECT: 3634 case MODE_SELECT_10: 3635 case START_STOP: 3636 supported = 3; 3637 break; 3638 case READ_16: 3639 supported = 3; 3640 if (dev->flags & ATA_DFLAG_CDL) { 3641 /* 3642 * CDL read descriptors map to the T2A page, that is, 3643 * rwcdlp = 0x01 and cdlp = 0x01 3644 */ 3645 rwcdlp = 0x01; 3646 cdlp = 0x01 << 3; 3647 } 3648 break; 3649 case WRITE_16: 3650 supported = 3; 3651 if (dev->flags & ATA_DFLAG_CDL) { 3652 /* 3653 * CDL write descriptors map to the T2B page, that is, 3654 * rwcdlp = 0x01 and cdlp = 0x02 3655 */ 3656 rwcdlp = 0x01; 3657 cdlp = 0x02 << 3; 3658 } 3659 break; 3660 case ZBC_IN: 3661 case ZBC_OUT: 3662 if (ata_id_zoned_cap(dev->id) || 3663 dev->class == ATA_DEV_ZAC) 3664 supported = 3; 3665 break; 3666 case SECURITY_PROTOCOL_IN: 3667 case SECURITY_PROTOCOL_OUT: 3668 if (dev->flags & ATA_DFLAG_TRUSTED) 3669 supported = 3; 3670 break; 3671 default: 3672 break; 3673 } 3674 3675 /* One command format */ 3676 rbuf[0] = rwcdlp; 3677 rbuf[1] = cdlp | supported; 3678 3679 return 4; 3680 } 3681 3682 /** 3683 * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN 3684 * @dev: Target device. 3685 * @cmd: SCSI command of interest. 3686 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 3687 * 3688 * Yields a subset to satisfy scsi_report_opcode() 3689 * 3690 * LOCKING: 3691 * spin_lock_irqsave(host lock) 3692 */ 3693 static unsigned int ata_scsiop_maint_in(struct ata_device *dev, 3694 struct scsi_cmnd *cmd, u8 *rbuf) 3695 { 3696 u8 *cdb = cmd->cmnd; 3697 u8 service_action = cdb[1] & 0x1f; 3698 3699 switch (service_action) { 3700 case MI_REPORT_SUPPORTED_OPERATION_CODES: 3701 return ata_scsi_report_supported_opcodes(dev, cmd, rbuf); 3702 default: 3703 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff); 3704 return 0; 3705 } 3706 } 3707 3708 /** 3709 * ata_scsi_report_zones_complete - convert ATA output 3710 * @qc: command structure returning the data 3711 * 3712 * Convert T-13 little-endian field representation into 3713 * T-10 big-endian field representation. 3714 * What a mess. 3715 */ 3716 static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc) 3717 { 3718 struct scsi_cmnd *scmd = qc->scsicmd; 3719 struct sg_mapping_iter miter; 3720 unsigned int bytes = 0; 3721 3722 lockdep_assert_held(qc->ap->lock); 3723 3724 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd), 3725 SG_MITER_TO_SG | SG_MITER_ATOMIC); 3726 3727 while (sg_miter_next(&miter)) { 3728 unsigned int offset = 0; 3729 3730 if (bytes == 0) { 3731 char *hdr; 3732 u32 list_length; 3733 u64 max_lba, opt_lba; 3734 u16 same; 3735 3736 /* Swizzle header */ 3737 hdr = miter.addr; 3738 list_length = get_unaligned_le32(&hdr[0]); 3739 same = get_unaligned_le16(&hdr[4]); 3740 max_lba = get_unaligned_le64(&hdr[8]); 3741 opt_lba = get_unaligned_le64(&hdr[16]); 3742 put_unaligned_be32(list_length, &hdr[0]); 3743 hdr[4] = same & 0xf; 3744 put_unaligned_be64(max_lba, &hdr[8]); 3745 put_unaligned_be64(opt_lba, &hdr[16]); 3746 offset += 64; 3747 bytes += 64; 3748 } 3749 while (offset < miter.length) { 3750 char *rec; 3751 u8 cond, type, non_seq, reset; 3752 u64 size, start, wp; 3753 3754 /* Swizzle zone descriptor */ 3755 rec = miter.addr + offset; 3756 type = rec[0] & 0xf; 3757 cond = (rec[1] >> 4) & 0xf; 3758 non_seq = (rec[1] & 2); 3759 reset = (rec[1] & 1); 3760 size = get_unaligned_le64(&rec[8]); 3761 start = get_unaligned_le64(&rec[16]); 3762 wp = get_unaligned_le64(&rec[24]); 3763 rec[0] = type; 3764 rec[1] = (cond << 4) | non_seq | reset; 3765 put_unaligned_be64(size, &rec[8]); 3766 put_unaligned_be64(start, &rec[16]); 3767 put_unaligned_be64(wp, &rec[24]); 3768 WARN_ON(offset + 64 > miter.length); 3769 offset += 64; 3770 bytes += 64; 3771 } 3772 } 3773 sg_miter_stop(&miter); 3774 3775 ata_scsi_qc_complete(qc); 3776 } 3777 3778 static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc) 3779 { 3780 struct ata_taskfile *tf = &qc->tf; 3781 struct scsi_cmnd *scmd = qc->scsicmd; 3782 const u8 *cdb = scmd->cmnd; 3783 u16 sect, fp = (u16)-1; 3784 u8 sa, options, bp = 0xff; 3785 u64 block; 3786 u32 n_block; 3787 3788 if (unlikely(scmd->cmd_len < 16)) { 3789 ata_dev_warn(qc->dev, "invalid cdb length %d\n", 3790 scmd->cmd_len); 3791 fp = 15; 3792 goto invalid_fld; 3793 } 3794 scsi_16_lba_len(cdb, &block, &n_block); 3795 if (n_block != scsi_bufflen(scmd)) { 3796 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n", 3797 n_block, scsi_bufflen(scmd)); 3798 goto invalid_param_len; 3799 } 3800 sa = cdb[1] & 0x1f; 3801 if (sa != ZI_REPORT_ZONES) { 3802 ata_dev_warn(qc->dev, "invalid service action %d\n", sa); 3803 fp = 1; 3804 goto invalid_fld; 3805 } 3806 /* 3807 * ZAC allows only for transfers in 512 byte blocks, 3808 * and uses a 16 bit value for the transfer count. 3809 */ 3810 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) { 3811 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block); 3812 goto invalid_param_len; 3813 } 3814 sect = n_block / 512; 3815 options = cdb[14] & 0xbf; 3816 3817 if (ata_ncq_enabled(qc->dev) && 3818 ata_fpdma_zac_mgmt_in_supported(qc->dev)) { 3819 tf->protocol = ATA_PROT_NCQ; 3820 tf->command = ATA_CMD_FPDMA_RECV; 3821 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f; 3822 tf->nsect = qc->hw_tag << 3; 3823 tf->feature = sect & 0xff; 3824 tf->hob_feature = (sect >> 8) & 0xff; 3825 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8); 3826 } else { 3827 tf->command = ATA_CMD_ZAC_MGMT_IN; 3828 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES; 3829 tf->protocol = ATA_PROT_DMA; 3830 tf->hob_feature = options; 3831 tf->hob_nsect = (sect >> 8) & 0xff; 3832 tf->nsect = sect & 0xff; 3833 } 3834 tf->device = ATA_LBA; 3835 tf->lbah = (block >> 16) & 0xff; 3836 tf->lbam = (block >> 8) & 0xff; 3837 tf->lbal = block & 0xff; 3838 tf->hob_lbah = (block >> 40) & 0xff; 3839 tf->hob_lbam = (block >> 32) & 0xff; 3840 tf->hob_lbal = (block >> 24) & 0xff; 3841 3842 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48; 3843 qc->flags |= ATA_QCFLAG_RESULT_TF; 3844 3845 ata_qc_set_pc_nbytes(qc); 3846 3847 qc->complete_fn = ata_scsi_report_zones_complete; 3848 3849 return 0; 3850 3851 invalid_fld: 3852 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp); 3853 return 1; 3854 3855 invalid_param_len: 3856 /* "Parameter list length error" */ 3857 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0); 3858 return 1; 3859 } 3860 3861 static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc) 3862 { 3863 struct ata_taskfile *tf = &qc->tf; 3864 struct scsi_cmnd *scmd = qc->scsicmd; 3865 struct ata_device *dev = qc->dev; 3866 const u8 *cdb = scmd->cmnd; 3867 u8 all, sa; 3868 u64 block; 3869 u32 n_block; 3870 u16 fp = (u16)-1; 3871 3872 if (unlikely(scmd->cmd_len < 16)) { 3873 fp = 15; 3874 goto invalid_fld; 3875 } 3876 3877 sa = cdb[1] & 0x1f; 3878 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) && 3879 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) { 3880 fp = 1; 3881 goto invalid_fld; 3882 } 3883 3884 scsi_16_lba_len(cdb, &block, &n_block); 3885 if (n_block) { 3886 /* 3887 * ZAC MANAGEMENT OUT doesn't define any length 3888 */ 3889 goto invalid_param_len; 3890 } 3891 3892 all = cdb[14] & 0x1; 3893 if (all) { 3894 /* 3895 * Ignore the block address (zone ID) as defined by ZBC. 3896 */ 3897 block = 0; 3898 } else if (block >= dev->n_sectors) { 3899 /* 3900 * Block must be a valid zone ID (a zone start LBA). 3901 */ 3902 fp = 2; 3903 goto invalid_fld; 3904 } 3905 3906 if (ata_ncq_enabled(qc->dev) && 3907 ata_fpdma_zac_mgmt_out_supported(qc->dev)) { 3908 tf->protocol = ATA_PROT_NCQ_NODATA; 3909 tf->command = ATA_CMD_NCQ_NON_DATA; 3910 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT; 3911 tf->nsect = qc->hw_tag << 3; 3912 tf->auxiliary = sa | ((u16)all << 8); 3913 } else { 3914 tf->protocol = ATA_PROT_NODATA; 3915 tf->command = ATA_CMD_ZAC_MGMT_OUT; 3916 tf->feature = sa; 3917 tf->hob_feature = all; 3918 } 3919 tf->lbah = (block >> 16) & 0xff; 3920 tf->lbam = (block >> 8) & 0xff; 3921 tf->lbal = block & 0xff; 3922 tf->hob_lbah = (block >> 40) & 0xff; 3923 tf->hob_lbam = (block >> 32) & 0xff; 3924 tf->hob_lbal = (block >> 24) & 0xff; 3925 tf->device = ATA_LBA; 3926 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48; 3927 3928 return 0; 3929 3930 invalid_fld: 3931 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff); 3932 return 1; 3933 invalid_param_len: 3934 /* "Parameter list length error" */ 3935 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0); 3936 return 1; 3937 } 3938 3939 /** 3940 * ata_mselect_caching - Simulate MODE SELECT for caching info page 3941 * @qc: Storage for translated ATA taskfile 3942 * @buf: input buffer 3943 * @len: number of valid bytes in the input buffer 3944 * @fp: out parameter for the failed field on error 3945 * 3946 * Prepare a taskfile to modify caching information for the device. 3947 * 3948 * LOCKING: 3949 * None. 3950 */ 3951 static int ata_mselect_caching(struct ata_queued_cmd *qc, 3952 const u8 *buf, int len, u16 *fp) 3953 { 3954 struct ata_taskfile *tf = &qc->tf; 3955 struct ata_device *dev = qc->dev; 3956 u8 mpage[CACHE_MPAGE_LEN]; 3957 u8 wce; 3958 int i; 3959 3960 /* 3961 * The first two bytes of def_cache_mpage are a header, so offsets 3962 * in mpage are off by 2 compared to buf. Same for len. 3963 */ 3964 3965 if (len != CACHE_MPAGE_LEN - 2) { 3966 *fp = min(len, CACHE_MPAGE_LEN - 2); 3967 return -EINVAL; 3968 } 3969 3970 wce = buf[0] & (1 << 2); 3971 3972 /* 3973 * Check that read-only bits are not modified. 3974 */ 3975 ata_msense_caching(dev->id, mpage, false); 3976 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) { 3977 if (i == 0) 3978 continue; 3979 if (mpage[i + 2] != buf[i]) { 3980 *fp = i; 3981 return -EINVAL; 3982 } 3983 } 3984 3985 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; 3986 tf->protocol = ATA_PROT_NODATA; 3987 tf->nsect = 0; 3988 tf->command = ATA_CMD_SET_FEATURES; 3989 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF; 3990 return 0; 3991 } 3992 3993 /* 3994 * Simulate MODE SELECT control mode page, sub-page 0. 3995 */ 3996 static int ata_mselect_control_spg0(struct ata_queued_cmd *qc, 3997 const u8 *buf, int len, u16 *fp) 3998 { 3999 struct ata_device *dev = qc->dev; 4000 u8 mpage[CONTROL_MPAGE_LEN]; 4001 u8 d_sense; 4002 int i; 4003 4004 /* 4005 * The first two bytes of def_control_mpage are a header, so offsets 4006 * in mpage are off by 2 compared to buf. Same for len. 4007 */ 4008 4009 if (len != CONTROL_MPAGE_LEN - 2) { 4010 *fp = min(len, CONTROL_MPAGE_LEN - 2); 4011 return -EINVAL; 4012 } 4013 4014 d_sense = buf[0] & (1 << 2); 4015 4016 /* 4017 * Check that read-only bits are not modified. 4018 */ 4019 ata_msense_control_spg0(dev, mpage, false); 4020 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) { 4021 if (i == 0) 4022 continue; 4023 if (mpage[2 + i] != buf[i]) { 4024 *fp = i; 4025 return -EINVAL; 4026 } 4027 } 4028 if (d_sense & (1 << 2)) 4029 dev->flags |= ATA_DFLAG_D_SENSE; 4030 else 4031 dev->flags &= ~ATA_DFLAG_D_SENSE; 4032 return 0; 4033 } 4034 4035 /* 4036 * Translate MODE SELECT control mode page, sub-page f2h (ATA feature mode 4037 * page) into a SET FEATURES command. 4038 */ 4039 static int ata_mselect_control_ata_feature(struct ata_queued_cmd *qc, 4040 const u8 *buf, int len, u16 *fp) 4041 { 4042 struct ata_device *dev = qc->dev; 4043 struct ata_taskfile *tf = &qc->tf; 4044 u8 cdl_action; 4045 4046 /* 4047 * The first four bytes of ATA Feature Control mode page are a header, 4048 * so offsets in mpage are off by 4 compared to buf. Same for len. 4049 */ 4050 if (len != ATA_FEATURE_SUB_MPAGE_LEN - 4) { 4051 *fp = min(len, ATA_FEATURE_SUB_MPAGE_LEN - 4); 4052 return -EINVAL; 4053 } 4054 4055 /* Check cdl_ctrl */ 4056 switch (buf[0] & 0x03) { 4057 case 0: 4058 /* Disable CDL */ 4059 ata_dev_dbg(dev, "Disabling CDL\n"); 4060 cdl_action = 0; 4061 dev->flags &= ~ATA_DFLAG_CDL_ENABLED; 4062 break; 4063 case 0x02: 4064 /* 4065 * Enable CDL. Since CDL is mutually exclusive with NCQ 4066 * priority, allow this only if NCQ priority is disabled. 4067 */ 4068 if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED) { 4069 ata_dev_err(dev, 4070 "NCQ priority must be disabled to enable CDL\n"); 4071 return -EINVAL; 4072 } 4073 ata_dev_dbg(dev, "Enabling CDL\n"); 4074 cdl_action = 1; 4075 dev->flags |= ATA_DFLAG_CDL_ENABLED; 4076 break; 4077 default: 4078 *fp = 0; 4079 return -EINVAL; 4080 } 4081 4082 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; 4083 tf->protocol = ATA_PROT_NODATA; 4084 tf->command = ATA_CMD_SET_FEATURES; 4085 tf->feature = SETFEATURES_CDL; 4086 tf->nsect = cdl_action; 4087 4088 return 1; 4089 } 4090 4091 /** 4092 * ata_mselect_control - Simulate MODE SELECT for control page 4093 * @qc: Storage for translated ATA taskfile 4094 * @spg: target sub-page of the control page 4095 * @buf: input buffer 4096 * @len: number of valid bytes in the input buffer 4097 * @fp: out parameter for the failed field on error 4098 * 4099 * Prepare a taskfile to modify caching information for the device. 4100 * 4101 * LOCKING: 4102 * None. 4103 */ 4104 static int ata_mselect_control(struct ata_queued_cmd *qc, u8 spg, 4105 const u8 *buf, int len, u16 *fp) 4106 { 4107 switch (spg) { 4108 case 0: 4109 return ata_mselect_control_spg0(qc, buf, len, fp); 4110 case ATA_FEATURE_SUB_MPAGE: 4111 return ata_mselect_control_ata_feature(qc, buf, len, fp); 4112 default: 4113 return -EINVAL; 4114 } 4115 } 4116 4117 /** 4118 * ata_scsi_mode_select_xlat - Simulate MODE SELECT 6, 10 commands 4119 * @qc: Storage for translated ATA taskfile 4120 * 4121 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile. 4122 * Assume this is invoked for direct access devices (e.g. disks) only. 4123 * There should be no block descriptor for other device types. 4124 * 4125 * LOCKING: 4126 * spin_lock_irqsave(host lock) 4127 */ 4128 static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc) 4129 { 4130 struct scsi_cmnd *scmd = qc->scsicmd; 4131 const u8 *cdb = scmd->cmnd; 4132 u8 pg, spg; 4133 unsigned six_byte, pg_len, hdr_len, bd_len; 4134 int len, ret; 4135 u16 fp = (u16)-1; 4136 u8 bp = 0xff; 4137 u8 buffer[64]; 4138 const u8 *p = buffer; 4139 4140 six_byte = (cdb[0] == MODE_SELECT); 4141 if (six_byte) { 4142 if (scmd->cmd_len < 5) { 4143 fp = 4; 4144 goto invalid_fld; 4145 } 4146 4147 len = cdb[4]; 4148 hdr_len = 4; 4149 } else { 4150 if (scmd->cmd_len < 9) { 4151 fp = 8; 4152 goto invalid_fld; 4153 } 4154 4155 len = get_unaligned_be16(&cdb[7]); 4156 hdr_len = 8; 4157 } 4158 4159 /* We only support PF=1, SP=0. */ 4160 if ((cdb[1] & 0x11) != 0x10) { 4161 fp = 1; 4162 bp = (cdb[1] & 0x01) ? 1 : 5; 4163 goto invalid_fld; 4164 } 4165 4166 /* Test early for possible overrun. */ 4167 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len) 4168 goto invalid_param_len; 4169 4170 /* Move past header and block descriptors. */ 4171 if (len < hdr_len) 4172 goto invalid_param_len; 4173 4174 if (!sg_copy_to_buffer(scsi_sglist(scmd), scsi_sg_count(scmd), 4175 buffer, sizeof(buffer))) 4176 goto invalid_param_len; 4177 4178 if (six_byte) 4179 bd_len = p[3]; 4180 else 4181 bd_len = get_unaligned_be16(&p[6]); 4182 4183 len -= hdr_len; 4184 p += hdr_len; 4185 if (len < bd_len) 4186 goto invalid_param_len; 4187 if (bd_len != 0 && bd_len != 8) { 4188 fp = (six_byte) ? 3 : 6; 4189 fp += bd_len + hdr_len; 4190 goto invalid_param; 4191 } 4192 4193 len -= bd_len; 4194 p += bd_len; 4195 if (len == 0) 4196 goto skip; 4197 4198 /* Parse both possible formats for the mode page headers. */ 4199 pg = p[0] & 0x3f; 4200 if (p[0] & 0x40) { 4201 if (len < 4) 4202 goto invalid_param_len; 4203 4204 spg = p[1]; 4205 pg_len = get_unaligned_be16(&p[2]); 4206 p += 4; 4207 len -= 4; 4208 } else { 4209 if (len < 2) 4210 goto invalid_param_len; 4211 4212 spg = 0; 4213 pg_len = p[1]; 4214 p += 2; 4215 len -= 2; 4216 } 4217 4218 /* 4219 * Supported subpages: all subpages and ATA feature sub-page f2h of 4220 * the control page. 4221 */ 4222 if (spg) { 4223 switch (spg) { 4224 case ALL_SUB_MPAGES: 4225 /* All subpages is not supported for the control page */ 4226 if (pg == CONTROL_MPAGE) { 4227 fp = (p[0] & 0x40) ? 1 : 0; 4228 fp += hdr_len + bd_len; 4229 goto invalid_param; 4230 } 4231 break; 4232 case ATA_FEATURE_SUB_MPAGE: 4233 if (qc->dev->flags & ATA_DFLAG_CDL && 4234 pg == CONTROL_MPAGE) 4235 break; 4236 fallthrough; 4237 default: 4238 fp = (p[0] & 0x40) ? 1 : 0; 4239 fp += hdr_len + bd_len; 4240 goto invalid_param; 4241 } 4242 } 4243 if (pg_len > len) 4244 goto invalid_param_len; 4245 4246 switch (pg) { 4247 case CACHE_MPAGE: 4248 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) { 4249 fp += hdr_len + bd_len; 4250 goto invalid_param; 4251 } 4252 break; 4253 case CONTROL_MPAGE: 4254 ret = ata_mselect_control(qc, spg, p, pg_len, &fp); 4255 if (ret < 0) { 4256 fp += hdr_len + bd_len; 4257 goto invalid_param; 4258 } 4259 if (!ret) 4260 goto skip; /* No ATA command to send */ 4261 break; 4262 default: 4263 /* Invalid page code */ 4264 fp = bd_len + hdr_len; 4265 goto invalid_param; 4266 } 4267 4268 /* 4269 * Only one page has changeable data, so we only support setting one 4270 * page at a time. 4271 */ 4272 if (len > pg_len) 4273 goto invalid_param; 4274 4275 return 0; 4276 4277 invalid_fld: 4278 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp); 4279 return 1; 4280 4281 invalid_param: 4282 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp); 4283 return 1; 4284 4285 invalid_param_len: 4286 /* "Parameter list length error" */ 4287 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0); 4288 return 1; 4289 4290 skip: 4291 scmd->result = SAM_STAT_GOOD; 4292 return 1; 4293 } 4294 4295 static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma) 4296 { 4297 if (len == 0) 4298 return ATA_CMD_TRUSTED_NONDATA; 4299 else if (send) 4300 return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND; 4301 else 4302 return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV; 4303 } 4304 4305 static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc) 4306 { 4307 struct scsi_cmnd *scmd = qc->scsicmd; 4308 const u8 *cdb = scmd->cmnd; 4309 struct ata_taskfile *tf = &qc->tf; 4310 u8 secp = cdb[1]; 4311 bool send = (cdb[0] == SECURITY_PROTOCOL_OUT); 4312 u16 spsp = get_unaligned_be16(&cdb[2]); 4313 u32 len = get_unaligned_be32(&cdb[6]); 4314 bool dma = !(qc->dev->flags & ATA_DFLAG_PIO); 4315 4316 /* 4317 * We don't support the ATA "security" protocol. 4318 */ 4319 if (secp == 0xef) { 4320 ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0); 4321 return 1; 4322 } 4323 4324 if (cdb[4] & 7) { /* INC_512 */ 4325 if (len > 0xffff) { 4326 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0); 4327 return 1; 4328 } 4329 } else { 4330 if (len > 0x01fffe00) { 4331 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0); 4332 return 1; 4333 } 4334 4335 /* convert to the sector-based ATA addressing */ 4336 len = (len + 511) / 512; 4337 } 4338 4339 tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO; 4340 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA; 4341 if (send) 4342 tf->flags |= ATA_TFLAG_WRITE; 4343 tf->command = ata_scsi_trusted_op(len, send, dma); 4344 tf->feature = secp; 4345 tf->lbam = spsp & 0xff; 4346 tf->lbah = spsp >> 8; 4347 4348 if (len) { 4349 tf->nsect = len & 0xff; 4350 tf->lbal = len >> 8; 4351 } else { 4352 if (!send) 4353 tf->lbah = (1 << 7); 4354 } 4355 4356 ata_qc_set_pc_nbytes(qc); 4357 return 0; 4358 } 4359 4360 /** 4361 * ata_scsi_var_len_cdb_xlat - SATL variable length CDB to Handler 4362 * @qc: Command to be translated 4363 * 4364 * Translate a SCSI variable length CDB to specified commands. 4365 * It checks a service action value in CDB to call corresponding handler. 4366 * 4367 * RETURNS: 4368 * Zero on success, non-zero on failure 4369 * 4370 */ 4371 static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc) 4372 { 4373 struct scsi_cmnd *scmd = qc->scsicmd; 4374 const u8 *cdb = scmd->cmnd; 4375 const u16 sa = get_unaligned_be16(&cdb[8]); 4376 4377 /* 4378 * if service action represents a ata pass-thru(32) command, 4379 * then pass it to ata_scsi_pass_thru handler. 4380 */ 4381 if (sa == ATA_32) 4382 return ata_scsi_pass_thru(qc); 4383 4384 /* unsupported service action */ 4385 return 1; 4386 } 4387 4388 /** 4389 * ata_get_xlat_func - check if SCSI to ATA translation is possible 4390 * @dev: ATA device 4391 * @cmd: SCSI command opcode to consider 4392 * 4393 * Look up the SCSI command given, and determine whether the 4394 * SCSI command is to be translated or simulated. 4395 * 4396 * RETURNS: 4397 * Pointer to translation function if possible, %NULL if not. 4398 */ 4399 4400 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd) 4401 { 4402 switch (cmd) { 4403 case READ_6: 4404 case READ_10: 4405 case READ_16: 4406 4407 case WRITE_6: 4408 case WRITE_10: 4409 case WRITE_16: 4410 return ata_scsi_rw_xlat; 4411 4412 case WRITE_SAME_16: 4413 return ata_scsi_write_same_xlat; 4414 4415 case SYNCHRONIZE_CACHE: 4416 case SYNCHRONIZE_CACHE_16: 4417 if (ata_try_flush_cache(dev)) 4418 return ata_scsi_flush_xlat; 4419 break; 4420 4421 case VERIFY: 4422 case VERIFY_16: 4423 return ata_scsi_verify_xlat; 4424 4425 case ATA_12: 4426 case ATA_16: 4427 return ata_scsi_pass_thru; 4428 4429 case VARIABLE_LENGTH_CMD: 4430 return ata_scsi_var_len_cdb_xlat; 4431 4432 case MODE_SELECT: 4433 case MODE_SELECT_10: 4434 return ata_scsi_mode_select_xlat; 4435 4436 case ZBC_IN: 4437 return ata_scsi_zbc_in_xlat; 4438 4439 case ZBC_OUT: 4440 return ata_scsi_zbc_out_xlat; 4441 4442 case SECURITY_PROTOCOL_IN: 4443 case SECURITY_PROTOCOL_OUT: 4444 if (!(dev->flags & ATA_DFLAG_TRUSTED)) 4445 break; 4446 return ata_scsi_security_inout_xlat; 4447 4448 case START_STOP: 4449 return ata_scsi_start_stop_xlat; 4450 } 4451 4452 return NULL; 4453 } 4454 4455 /** 4456 * ata_scsi_simulate - simulate SCSI command on ATA device 4457 * @dev: the target device 4458 * @cmd: SCSI command being sent to device. 4459 * 4460 * Interprets and directly executes a select list of SCSI commands 4461 * that can be handled internally. 4462 * 4463 * LOCKING: 4464 * spin_lock_irqsave(host lock) 4465 */ 4466 static void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd) 4467 { 4468 const u8 *scsicmd = cmd->cmnd; 4469 u8 tmp8; 4470 4471 switch (scsicmd[0]) { 4472 case INQUIRY: 4473 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_inquiry); 4474 break; 4475 4476 case MODE_SENSE: 4477 case MODE_SENSE_10: 4478 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_mode_sense); 4479 break; 4480 4481 case READ_CAPACITY: 4482 case SERVICE_ACTION_IN_16: 4483 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_read_cap); 4484 break; 4485 4486 case REPORT_LUNS: 4487 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_report_luns); 4488 break; 4489 4490 case REQUEST_SENSE: 4491 ata_scsi_set_sense(dev, cmd, 0, 0, 0); 4492 break; 4493 4494 /* if we reach this, then writeback caching is disabled, 4495 * turning this into a no-op. 4496 */ 4497 case SYNCHRONIZE_CACHE: 4498 case SYNCHRONIZE_CACHE_16: 4499 fallthrough; 4500 4501 /* no-op's, complete with success */ 4502 case REZERO_UNIT: 4503 case SEEK_6: 4504 case SEEK_10: 4505 case TEST_UNIT_READY: 4506 break; 4507 4508 case SEND_DIAGNOSTIC: 4509 tmp8 = scsicmd[1] & ~(1 << 3); 4510 if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4]) 4511 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff); 4512 break; 4513 4514 case MAINTENANCE_IN: 4515 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_maint_in); 4516 break; 4517 4518 /* all other commands */ 4519 default: 4520 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0); 4521 /* "Invalid command operation code" */ 4522 break; 4523 } 4524 4525 scsi_done(cmd); 4526 } 4527 4528 enum scsi_qc_status __ata_scsi_queuecmd(struct scsi_cmnd *scmd, 4529 struct ata_device *dev, 4530 struct ata_port *ap) 4531 __must_hold(ap->lock) 4532 { 4533 u8 scsi_op = scmd->cmnd[0]; 4534 ata_xlat_func_t xlat_func; 4535 4536 /* 4537 * scsi_queue_rq() will defer commands if scsi_host_in_recovery(). 4538 * However, this check is done without holding the ap->lock (a libata 4539 * specific lock), so we can have received an error irq since then, 4540 * therefore we must check if EH is pending or running, while holding 4541 * ap->lock. 4542 */ 4543 if (ata_port_eh_scheduled(ap)) 4544 return SCSI_MLQUEUE_DEVICE_BUSY; 4545 4546 if (unlikely(!scmd->cmd_len)) 4547 goto bad_cdb_len; 4548 4549 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) { 4550 if (unlikely(scmd->cmd_len > dev->cdb_len)) 4551 goto bad_cdb_len; 4552 4553 xlat_func = ata_get_xlat_func(dev, scsi_op); 4554 } else if (likely((scsi_op != ATA_16) || !atapi_passthru16)) { 4555 /* relay SCSI command to ATAPI device */ 4556 int len = COMMAND_SIZE(scsi_op); 4557 4558 if (unlikely(len > scmd->cmd_len || 4559 len > dev->cdb_len || 4560 scmd->cmd_len > ATAPI_CDB_LEN)) 4561 goto bad_cdb_len; 4562 4563 xlat_func = atapi_xlat; 4564 } else { 4565 /* ATA_16 passthru, treat as an ATA command */ 4566 if (unlikely(scmd->cmd_len > 16)) 4567 goto bad_cdb_len; 4568 4569 xlat_func = ata_get_xlat_func(dev, scsi_op); 4570 } 4571 4572 if (xlat_func) 4573 return ata_scsi_translate(dev, scmd, xlat_func, ap); 4574 4575 ata_scsi_simulate(dev, scmd); 4576 4577 return 0; 4578 4579 bad_cdb_len: 4580 scmd->result = DID_ERROR << 16; 4581 scsi_done(scmd); 4582 return 0; 4583 } 4584 4585 /** 4586 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device 4587 * @shost: SCSI host of command to be sent 4588 * @cmd: SCSI command to be sent 4589 * 4590 * In some cases, this function translates SCSI commands into 4591 * ATA taskfiles, and queues the taskfiles to be sent to 4592 * hardware. In other cases, this function simulates a 4593 * SCSI device by evaluating and responding to certain 4594 * SCSI commands. This creates the overall effect of 4595 * ATA and ATAPI devices appearing as SCSI devices. 4596 * 4597 * LOCKING: 4598 * ATA host lock 4599 * 4600 * RETURNS: 4601 * Return value from __ata_scsi_queuecmd() if @cmd can be queued, 4602 * 0 otherwise. 4603 */ 4604 enum scsi_qc_status ata_scsi_queuecmd(struct Scsi_Host *shost, 4605 struct scsi_cmnd *cmd) 4606 { 4607 struct ata_port *ap; 4608 struct ata_device *dev; 4609 struct scsi_device *scsidev = cmd->device; 4610 enum scsi_qc_status rc = 0; 4611 unsigned long irq_flags; 4612 4613 ap = ata_shost_to_port(shost); 4614 4615 spin_lock_irqsave(ap->lock, irq_flags); 4616 4617 dev = ata_scsi_find_dev(ap, scsidev); 4618 if (likely(dev)) 4619 rc = __ata_scsi_queuecmd(cmd, dev, ap); 4620 else { 4621 cmd->result = (DID_BAD_TARGET << 16); 4622 scsi_done(cmd); 4623 } 4624 4625 spin_unlock_irqrestore(ap->lock, irq_flags); 4626 4627 return rc; 4628 } 4629 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd); 4630 4631 int ata_scsi_add_hosts(struct ata_host *host, const struct scsi_host_template *sht) 4632 { 4633 int i, rc; 4634 4635 for (i = 0; i < host->n_ports; i++) { 4636 struct ata_port *ap = host->ports[i]; 4637 struct Scsi_Host *shost; 4638 4639 rc = -ENOMEM; 4640 shost = scsi_host_alloc(sht, sizeof(struct ata_port *)); 4641 if (!shost) 4642 goto err_alloc; 4643 4644 shost->eh_noresume = 1; 4645 *(struct ata_port **)&shost->hostdata[0] = ap; 4646 ap->scsi_host = shost; 4647 4648 shost->transportt = &ata_scsi_transportt; 4649 shost->unique_id = ap->print_id; 4650 shost->max_id = 16; 4651 shost->max_lun = 1; 4652 shost->max_channel = 1; 4653 shost->max_cmd_len = 32; 4654 4655 /* Schedule policy is determined by ->qc_defer() 4656 * callback and it needs to see every deferred qc. 4657 * Set host_blocked to 1 to prevent SCSI midlayer from 4658 * automatically deferring requests. 4659 */ 4660 shost->max_host_blocked = 1; 4661 4662 rc = scsi_add_host_with_dma(shost, &ap->tdev, ap->host->dev); 4663 if (rc) 4664 goto err_alloc; 4665 } 4666 4667 return 0; 4668 4669 err_alloc: 4670 while (--i >= 0) { 4671 struct Scsi_Host *shost = host->ports[i]->scsi_host; 4672 4673 /* scsi_host_put() is in ata_devres_release() */ 4674 scsi_remove_host(shost); 4675 } 4676 return rc; 4677 } 4678 4679 #ifdef CONFIG_OF 4680 static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap) 4681 { 4682 struct scsi_device *sdev = dev->sdev; 4683 struct device *d = ap->host->dev; 4684 struct device_node *np = d->of_node; 4685 struct device_node *child; 4686 4687 for_each_available_child_of_node(np, child) { 4688 int ret; 4689 u32 val; 4690 4691 ret = of_property_read_u32(child, "reg", &val); 4692 if (ret) 4693 continue; 4694 if (val == dev->devno) { 4695 dev_dbg(d, "found matching device node\n"); 4696 sdev->sdev_gendev.of_node = child; 4697 return; 4698 } 4699 } 4700 } 4701 #else 4702 static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap) 4703 { 4704 } 4705 #endif 4706 4707 void ata_scsi_scan_host(struct ata_port *ap, int sync) 4708 { 4709 int tries = 5; 4710 struct ata_device *last_failed_dev = NULL; 4711 struct ata_link *link; 4712 struct ata_device *dev; 4713 4714 repeat: 4715 ata_for_each_link(link, ap, EDGE) { 4716 ata_for_each_dev(dev, link, ENABLED) { 4717 struct scsi_device *sdev; 4718 int channel = 0, id = 0; 4719 4720 if (dev->sdev) 4721 continue; 4722 4723 if (ata_is_host_link(link)) 4724 id = dev->devno; 4725 else 4726 channel = link->pmp; 4727 4728 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0, 4729 NULL); 4730 if (!IS_ERR(sdev)) { 4731 dev->sdev = sdev; 4732 ata_scsi_assign_ofnode(dev, ap); 4733 scsi_device_put(sdev); 4734 } else { 4735 dev->sdev = NULL; 4736 } 4737 } 4738 } 4739 4740 /* If we scanned while EH was in progress or allocation 4741 * failure occurred, scan would have failed silently. Check 4742 * whether all devices are attached. 4743 */ 4744 ata_for_each_link(link, ap, EDGE) { 4745 ata_for_each_dev(dev, link, ENABLED) { 4746 if (!dev->sdev) 4747 goto exit_loop; 4748 } 4749 } 4750 exit_loop: 4751 if (!link) 4752 return; 4753 4754 /* we're missing some SCSI devices */ 4755 if (sync) { 4756 /* If caller requested synchrnous scan && we've made 4757 * any progress, sleep briefly and repeat. 4758 */ 4759 if (dev != last_failed_dev) { 4760 msleep(100); 4761 last_failed_dev = dev; 4762 goto repeat; 4763 } 4764 4765 /* We might be failing to detect boot device, give it 4766 * a few more chances. 4767 */ 4768 if (--tries) { 4769 msleep(100); 4770 goto repeat; 4771 } 4772 4773 ata_port_err(ap, 4774 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n"); 4775 } 4776 4777 queue_delayed_work(system_dfl_long_wq, &ap->hotplug_task, 4778 round_jiffies_relative(HZ)); 4779 } 4780 4781 /** 4782 * ata_scsi_offline_dev - offline attached SCSI device 4783 * @dev: ATA device to offline attached SCSI device for 4784 * 4785 * This function is called from ata_eh_detach_dev() and is responsible for 4786 * taking the SCSI device attached to @dev offline. This function is 4787 * called with host lock which protects dev->sdev against clearing. 4788 * 4789 * LOCKING: 4790 * spin_lock_irqsave(host lock) 4791 * 4792 * RETURNS: 4793 * true if attached SCSI device exists, false otherwise. 4794 */ 4795 bool ata_scsi_offline_dev(struct ata_device *dev) 4796 { 4797 if (dev->sdev) { 4798 scsi_device_set_state(dev->sdev, SDEV_OFFLINE); 4799 return true; 4800 } 4801 return false; 4802 } 4803 4804 /** 4805 * ata_scsi_remove_dev - remove attached SCSI device 4806 * @dev: ATA device to remove attached SCSI device for 4807 * 4808 * This function is called from ata_eh_scsi_hotplug() and 4809 * responsible for removing the SCSI device attached to @dev. 4810 * 4811 * LOCKING: 4812 * Kernel thread context (may sleep). 4813 */ 4814 static void ata_scsi_remove_dev(struct ata_device *dev) 4815 { 4816 struct ata_port *ap = dev->link->ap; 4817 struct scsi_device *sdev; 4818 unsigned long flags; 4819 4820 /* Alas, we need to grab scan_mutex to ensure SCSI device 4821 * state doesn't change underneath us and thus 4822 * scsi_device_get() always succeeds. The mutex locking can 4823 * be removed if there is __scsi_device_get() interface which 4824 * increments reference counts regardless of device state. 4825 */ 4826 mutex_lock(&ap->scsi_host->scan_mutex); 4827 spin_lock_irqsave(ap->lock, flags); 4828 4829 /* clearing dev->sdev is protected by host lock */ 4830 sdev = dev->sdev; 4831 dev->sdev = NULL; 4832 4833 if (sdev) { 4834 /* If user initiated unplug races with us, sdev can go 4835 * away underneath us after the host lock and 4836 * scan_mutex are released. Hold onto it. 4837 */ 4838 if (scsi_device_get(sdev) == 0) { 4839 /* The following ensures the attached sdev is 4840 * offline on return from ata_scsi_offline_dev() 4841 * regardless it wins or loses the race 4842 * against this function. 4843 */ 4844 scsi_device_set_state(sdev, SDEV_OFFLINE); 4845 } else { 4846 WARN_ON(1); 4847 sdev = NULL; 4848 } 4849 } 4850 4851 spin_unlock_irqrestore(ap->lock, flags); 4852 mutex_unlock(&ap->scsi_host->scan_mutex); 4853 4854 if (sdev) { 4855 ata_dev_info(dev, "detaching (SCSI %s)\n", 4856 dev_name(&sdev->sdev_gendev)); 4857 4858 scsi_remove_device(sdev); 4859 scsi_device_put(sdev); 4860 } 4861 } 4862 4863 static void ata_scsi_handle_link_detach(struct ata_link *link) 4864 { 4865 struct ata_port *ap = link->ap; 4866 struct ata_device *dev; 4867 4868 ata_for_each_dev(dev, link, ALL) { 4869 unsigned long flags; 4870 4871 spin_lock_irqsave(ap->lock, flags); 4872 if (!(dev->flags & ATA_DFLAG_DETACHED)) { 4873 spin_unlock_irqrestore(ap->lock, flags); 4874 continue; 4875 } 4876 4877 dev->flags &= ~ATA_DFLAG_DETACHED; 4878 spin_unlock_irqrestore(ap->lock, flags); 4879 4880 ata_scsi_remove_dev(dev); 4881 } 4882 } 4883 4884 /** 4885 * ata_scsi_media_change_notify - send media change event 4886 * @dev: Pointer to the disk device with media change event 4887 * 4888 * Tell the block layer to send a media change notification 4889 * event. 4890 * 4891 * LOCKING: 4892 * spin_lock_irqsave(host lock) 4893 */ 4894 void ata_scsi_media_change_notify(struct ata_device *dev) 4895 { 4896 if (dev->sdev) 4897 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE, 4898 GFP_ATOMIC); 4899 } 4900 4901 /** 4902 * ata_scsi_hotplug - SCSI part of hotplug 4903 * @work: Pointer to ATA port to perform SCSI hotplug on 4904 * 4905 * Perform SCSI part of hotplug. It's executed from a separate 4906 * workqueue after EH completes. This is necessary because SCSI 4907 * hot plugging requires working EH and hot unplugging is 4908 * synchronized with hot plugging with a mutex. 4909 * 4910 * LOCKING: 4911 * Kernel thread context (may sleep). 4912 */ 4913 void ata_scsi_hotplug(struct work_struct *work) 4914 { 4915 struct ata_port *ap = 4916 container_of(work, struct ata_port, hotplug_task.work); 4917 int i; 4918 4919 if (ap->pflags & ATA_PFLAG_UNLOADING) 4920 return; 4921 4922 mutex_lock(&ap->scsi_scan_mutex); 4923 4924 /* Unplug detached devices. We cannot use link iterator here 4925 * because PMP links have to be scanned even if PMP is 4926 * currently not attached. Iterate manually. 4927 */ 4928 ata_scsi_handle_link_detach(&ap->link); 4929 if (ap->pmp_link) 4930 for (i = 0; i < SATA_PMP_MAX_PORTS; i++) 4931 ata_scsi_handle_link_detach(&ap->pmp_link[i]); 4932 4933 /* scan for new ones */ 4934 ata_scsi_scan_host(ap, 0); 4935 4936 mutex_unlock(&ap->scsi_scan_mutex); 4937 } 4938 4939 /** 4940 * ata_scsi_user_scan - indication for user-initiated bus scan 4941 * @shost: SCSI host to scan 4942 * @channel: Channel to scan 4943 * @id: ID to scan 4944 * @lun: LUN to scan 4945 * 4946 * This function is called when user explicitly requests bus 4947 * scan. Set probe pending flag and invoke EH. 4948 * 4949 * LOCKING: 4950 * SCSI layer (we don't care) 4951 * 4952 * RETURNS: 4953 * Zero. 4954 */ 4955 int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel, 4956 unsigned int id, u64 lun) 4957 { 4958 struct ata_port *ap = ata_shost_to_port(shost); 4959 unsigned long flags; 4960 int devno, rc = 0; 4961 4962 if (lun != SCAN_WILD_CARD && lun) 4963 return -EINVAL; 4964 4965 if (!sata_pmp_attached(ap)) { 4966 if (channel != SCAN_WILD_CARD && channel) 4967 return -EINVAL; 4968 devno = id; 4969 } else { 4970 if (id != SCAN_WILD_CARD && id) 4971 return -EINVAL; 4972 devno = channel; 4973 } 4974 4975 spin_lock_irqsave(ap->lock, flags); 4976 4977 if (devno == SCAN_WILD_CARD) { 4978 struct ata_link *link; 4979 4980 ata_for_each_link(link, ap, EDGE) { 4981 struct ata_eh_info *ehi = &link->eh_info; 4982 ehi->probe_mask |= ATA_ALL_DEVICES; 4983 ehi->action |= ATA_EH_RESET; 4984 } 4985 } else { 4986 struct ata_device *dev = ata_find_dev(ap, devno); 4987 4988 if (dev) { 4989 struct ata_eh_info *ehi = &dev->link->eh_info; 4990 ehi->probe_mask |= 1 << dev->devno; 4991 ehi->action |= ATA_EH_RESET; 4992 } else 4993 rc = -EINVAL; 4994 } 4995 4996 if (rc == 0) { 4997 ata_port_schedule_eh(ap); 4998 spin_unlock_irqrestore(ap->lock, flags); 4999 ata_port_wait_eh(ap); 5000 } else 5001 spin_unlock_irqrestore(ap->lock, flags); 5002 5003 return rc; 5004 } 5005 5006 /** 5007 * ata_scsi_dev_rescan - initiate scsi_rescan_device() 5008 * @work: Pointer to ATA port to perform scsi_rescan_device() 5009 * 5010 * After ATA pass thru (SAT) commands are executed successfully, 5011 * libata need to propagate the changes to SCSI layer. 5012 * 5013 * LOCKING: 5014 * Kernel thread context (may sleep). 5015 */ 5016 void ata_scsi_dev_rescan(struct work_struct *work) 5017 { 5018 struct ata_port *ap = 5019 container_of(work, struct ata_port, scsi_rescan_task.work); 5020 struct ata_link *link; 5021 struct ata_device *dev; 5022 unsigned long flags; 5023 bool do_resume; 5024 int ret = 0; 5025 5026 mutex_lock(&ap->scsi_scan_mutex); 5027 spin_lock_irqsave(ap->lock, flags); 5028 5029 ata_for_each_link(link, ap, EDGE) { 5030 ata_for_each_dev(dev, link, ENABLED) { 5031 struct scsi_device *sdev = dev->sdev; 5032 5033 /* 5034 * If the port was suspended before this was scheduled, 5035 * bail out. 5036 */ 5037 if (ap->pflags & ATA_PFLAG_SUSPENDED) 5038 goto unlock_ap; 5039 5040 if (!sdev) 5041 continue; 5042 if (scsi_device_get(sdev)) 5043 continue; 5044 5045 do_resume = dev->flags & ATA_DFLAG_RESUMING; 5046 5047 spin_unlock_irqrestore(ap->lock, flags); 5048 if (do_resume) { 5049 ret = scsi_resume_device(sdev); 5050 if (ret == -EWOULDBLOCK) { 5051 scsi_device_put(sdev); 5052 goto unlock_scan; 5053 } 5054 dev->flags &= ~ATA_DFLAG_RESUMING; 5055 } 5056 ret = scsi_rescan_device(sdev); 5057 scsi_device_put(sdev); 5058 spin_lock_irqsave(ap->lock, flags); 5059 5060 if (ret) 5061 goto unlock_ap; 5062 } 5063 } 5064 5065 unlock_ap: 5066 spin_unlock_irqrestore(ap->lock, flags); 5067 unlock_scan: 5068 mutex_unlock(&ap->scsi_scan_mutex); 5069 5070 /* Reschedule with a delay if scsi_rescan_device() returned an error */ 5071 if (ret) 5072 schedule_delayed_work(&ap->scsi_rescan_task, 5073 msecs_to_jiffies(5)); 5074 } 5075