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