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