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