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