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