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