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