1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * SCSI Primary Commands (SPC) parsing and emulation. 4 * 5 * (c) Copyright 2002-2013 Datera, Inc. 6 * 7 * Nicholas A. Bellinger <nab@kernel.org> 8 */ 9 10 #include <linux/hex.h> 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/unaligned.h> 14 15 #include <scsi/scsi_proto.h> 16 #include <scsi/scsi_common.h> 17 #include <scsi/scsi_tcq.h> 18 19 #include <target/target_core_base.h> 20 #include <target/target_core_backend.h> 21 #include <target/target_core_fabric.h> 22 23 #include "target_core_internal.h" 24 #include "target_core_alua.h" 25 #include "target_core_pr.h" 26 #include "target_core_ua.h" 27 #include "target_core_xcopy.h" 28 29 static void spc_fill_alua_data(struct se_lun *lun, unsigned char *buf) 30 { 31 struct t10_alua_tg_pt_gp *tg_pt_gp; 32 33 /* 34 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS. 35 */ 36 buf[5] = 0x80; 37 38 /* 39 * Set TPGS field for explicit and/or implicit ALUA access type 40 * and opteration. 41 * 42 * See spc4r17 section 6.4.2 Table 135 43 */ 44 rcu_read_lock(); 45 tg_pt_gp = rcu_dereference(lun->lun_tg_pt_gp); 46 if (tg_pt_gp) 47 buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type; 48 rcu_read_unlock(); 49 } 50 51 static u16 52 spc_find_scsi_transport_vd(int proto_id) 53 { 54 switch (proto_id) { 55 case SCSI_PROTOCOL_FCP: 56 return SCSI_VERSION_DESCRIPTOR_FCP4; 57 case SCSI_PROTOCOL_ISCSI: 58 return SCSI_VERSION_DESCRIPTOR_ISCSI; 59 case SCSI_PROTOCOL_SAS: 60 return SCSI_VERSION_DESCRIPTOR_SAS3; 61 case SCSI_PROTOCOL_SBP: 62 return SCSI_VERSION_DESCRIPTOR_SBP3; 63 case SCSI_PROTOCOL_SRP: 64 return SCSI_VERSION_DESCRIPTOR_SRP; 65 default: 66 pr_warn("Cannot find VERSION DESCRIPTOR value for unknown SCSI" 67 " transport PROTOCOL IDENTIFIER %#x\n", proto_id); 68 return 0; 69 } 70 } 71 72 sense_reason_t 73 spc_emulate_inquiry_std(struct se_cmd *cmd, unsigned char *buf) 74 { 75 struct se_lun *lun = cmd->se_lun; 76 struct se_portal_group *tpg = lun->lun_tpg; 77 struct se_device *dev = cmd->se_dev; 78 struct se_session *sess = cmd->se_sess; 79 80 /* Set RMB (removable media) for tape devices */ 81 if (dev->transport->get_device_type(dev) == TYPE_TAPE) 82 buf[1] = 0x80; 83 84 buf[2] = 0x06; /* SPC-4 */ 85 86 /* 87 * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2 88 * 89 * SPC4 says: 90 * A RESPONSE DATA FORMAT field set to 2h indicates that the 91 * standard INQUIRY data is in the format defined in this 92 * standard. Response data format values less than 2h are 93 * obsolete. Response data format values greater than 2h are 94 * reserved. 95 */ 96 buf[3] = 2; 97 98 /* 99 * Enable SCCS and TPGS fields for Emulated ALUA 100 */ 101 spc_fill_alua_data(lun, buf); 102 103 /* 104 * Set Third-Party Copy (3PC) bit to indicate support for EXTENDED_COPY 105 */ 106 if (dev->dev_attrib.emulate_3pc) 107 buf[5] |= 0x8; 108 /* 109 * Set Protection (PROTECT) bit when DIF has been enabled on the 110 * device, and the fabric supports VERIFY + PASS. Also report 111 * PROTECT=1 if sess_prot_type has been configured to allow T10-PI 112 * to unprotected devices. 113 */ 114 if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) { 115 if (dev->dev_attrib.pi_prot_type || cmd->se_sess->sess_prot_type) 116 buf[5] |= 0x1; 117 } 118 119 /* 120 * Set MULTIP bit to indicate presence of multiple SCSI target ports 121 */ 122 if (dev->export_count > 1) 123 buf[6] |= 0x10; 124 125 buf[7] = 0x2; /* CmdQue=1 */ 126 127 /* 128 * ASCII data fields described as being left-aligned shall have any 129 * unused bytes at the end of the field (i.e., highest offset) and the 130 * unused bytes shall be filled with ASCII space characters (20h). 131 */ 132 memset(&buf[8], 0x20, 133 INQUIRY_VENDOR_LEN + INQUIRY_MODEL_LEN + INQUIRY_REVISION_LEN); 134 memcpy(&buf[8], dev->t10_wwn.vendor, 135 strnlen(dev->t10_wwn.vendor, INQUIRY_VENDOR_LEN)); 136 memcpy(&buf[16], dev->t10_wwn.model, 137 strnlen(dev->t10_wwn.model, INQUIRY_MODEL_LEN)); 138 memcpy(&buf[32], dev->t10_wwn.revision, 139 strnlen(dev->t10_wwn.revision, INQUIRY_REVISION_LEN)); 140 141 /* 142 * Set the VERSION DESCRIPTOR fields 143 */ 144 put_unaligned_be16(SCSI_VERSION_DESCRIPTOR_SAM5, &buf[58]); 145 put_unaligned_be16(spc_find_scsi_transport_vd(tpg->proto_id), &buf[60]); 146 put_unaligned_be16(SCSI_VERSION_DESCRIPTOR_SPC4, &buf[62]); 147 if (cmd->se_dev->transport->get_device_type(dev) == TYPE_DISK) 148 put_unaligned_be16(SCSI_VERSION_DESCRIPTOR_SBC3, &buf[64]); 149 150 buf[4] = 91; /* Set additional length to 91 */ 151 152 return 0; 153 } 154 EXPORT_SYMBOL(spc_emulate_inquiry_std); 155 156 /* unit serial number */ 157 static sense_reason_t 158 spc_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf) 159 { 160 struct se_device *dev = cmd->se_dev; 161 u16 len; 162 163 if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) { 164 len = sprintf(&buf[4], "%s", dev->t10_wwn.unit_serial); 165 len++; /* Extra Byte for NULL Terminator */ 166 buf[3] = len; 167 } 168 return 0; 169 } 170 171 /* 172 * Generate NAA IEEE Registered Extended designator 173 */ 174 void spc_gen_naa_6h_vendor_specific(struct se_device *dev, 175 unsigned char *buf) 176 { 177 unsigned char *p = &dev->t10_wwn.unit_serial[0]; 178 u32 company_id = dev->t10_wwn.company_id; 179 int cnt, off = 0; 180 bool next = true; 181 182 /* 183 * Start NAA IEEE Registered Extended Identifier/Designator 184 */ 185 buf[off] = 0x6 << 4; 186 187 /* IEEE COMPANY_ID */ 188 buf[off++] |= (company_id >> 20) & 0xf; 189 buf[off++] = (company_id >> 12) & 0xff; 190 buf[off++] = (company_id >> 4) & 0xff; 191 buf[off] = (company_id & 0xf) << 4; 192 193 /* 194 * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on 195 * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field 196 * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION 197 * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL 198 * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure 199 * per device uniqeness. 200 */ 201 for (cnt = off + 13; *p && off < cnt; p++) { 202 int val = hex_to_bin(*p); 203 204 if (val < 0) 205 continue; 206 207 if (next) { 208 next = false; 209 buf[off++] |= val; 210 } else { 211 next = true; 212 buf[off] = val << 4; 213 } 214 } 215 } 216 217 /* 218 * Device identification VPD, for a complete list of 219 * DESIGNATOR TYPEs see spc4r17 Table 459. 220 */ 221 sense_reason_t 222 spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf) 223 { 224 struct se_device *dev = cmd->se_dev; 225 struct se_lun *lun = cmd->se_lun; 226 struct se_portal_group *tpg = NULL; 227 struct t10_alua_lu_gp_member *lu_gp_mem; 228 struct t10_alua_tg_pt_gp *tg_pt_gp; 229 unsigned char *prod = &dev->t10_wwn.model[0]; 230 u32 off = 0; 231 u16 len = 0, id_len; 232 233 off = 4; 234 235 /* 236 * NAA IEEE Registered Extended Assigned designator format, see 237 * spc4r17 section 7.7.3.6.5 238 * 239 * We depend upon a target_core_mod/ConfigFS provided 240 * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial 241 * value in order to return the NAA id. 242 */ 243 if (!(dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL)) 244 goto check_t10_vend_desc; 245 246 /* CODE SET == Binary */ 247 buf[off++] = 0x1; 248 249 /* Set ASSOCIATION == addressed logical unit: 0)b */ 250 buf[off] = 0x00; 251 252 /* Identifier/Designator type == NAA identifier */ 253 buf[off++] |= 0x3; 254 off++; 255 256 /* Identifier/Designator length */ 257 buf[off++] = 0x10; 258 259 /* NAA IEEE Registered Extended designator */ 260 spc_gen_naa_6h_vendor_specific(dev, &buf[off]); 261 262 len = 20; 263 off = (len + 4); 264 265 check_t10_vend_desc: 266 /* 267 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4 268 */ 269 id_len = 8; /* For Vendor field */ 270 271 if (dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) 272 id_len += sprintf(&buf[off+12], "%s:%s", prod, 273 &dev->t10_wwn.unit_serial[0]); 274 buf[off] = 0x2; /* ASCII */ 275 buf[off+1] = 0x1; /* T10 Vendor ID */ 276 buf[off+2] = 0x0; 277 /* left align Vendor ID and pad with spaces */ 278 memset(&buf[off+4], 0x20, INQUIRY_VENDOR_LEN); 279 memcpy(&buf[off+4], dev->t10_wwn.vendor, 280 strnlen(dev->t10_wwn.vendor, INQUIRY_VENDOR_LEN)); 281 /* Extra Byte for NULL Terminator */ 282 id_len++; 283 /* Identifier Length */ 284 buf[off+3] = id_len; 285 /* Header size for Designation descriptor */ 286 len += (id_len + 4); 287 off += (id_len + 4); 288 289 if (1) { 290 struct t10_alua_lu_gp *lu_gp; 291 u32 padding, scsi_name_len, scsi_target_len; 292 u16 lu_gp_id = 0; 293 u16 tg_pt_gp_id = 0; 294 u16 tpgt; 295 296 tpg = lun->lun_tpg; 297 /* 298 * Relative target port identifer, see spc4r17 299 * section 7.7.3.7 300 * 301 * Get the PROTOCOL IDENTIFIER as defined by spc4r17 302 * section 7.5.1 Table 362 303 */ 304 buf[off] = tpg->proto_id << 4; 305 buf[off++] |= 0x1; /* CODE SET == Binary */ 306 buf[off] = 0x80; /* Set PIV=1 */ 307 /* Set ASSOCIATION == target port: 01b */ 308 buf[off] |= 0x10; 309 /* DESIGNATOR TYPE == Relative target port identifer */ 310 buf[off++] |= 0x4; 311 off++; /* Skip over Reserved */ 312 buf[off++] = 4; /* DESIGNATOR LENGTH */ 313 /* Skip over Obsolete field in RTPI payload 314 * in Table 472 */ 315 off += 2; 316 put_unaligned_be16(lun->lun_tpg->tpg_rtpi, &buf[off]); 317 off += 2; 318 len += 8; /* Header size + Designation descriptor */ 319 /* 320 * Target port group identifier, see spc4r17 321 * section 7.7.3.8 322 * 323 * Get the PROTOCOL IDENTIFIER as defined by spc4r17 324 * section 7.5.1 Table 362 325 */ 326 rcu_read_lock(); 327 tg_pt_gp = rcu_dereference(lun->lun_tg_pt_gp); 328 if (!tg_pt_gp) { 329 rcu_read_unlock(); 330 goto check_lu_gp; 331 } 332 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id; 333 rcu_read_unlock(); 334 335 buf[off] = tpg->proto_id << 4; 336 buf[off++] |= 0x1; /* CODE SET == Binary */ 337 buf[off] = 0x80; /* Set PIV=1 */ 338 /* Set ASSOCIATION == target port: 01b */ 339 buf[off] |= 0x10; 340 /* DESIGNATOR TYPE == Target port group identifier */ 341 buf[off++] |= 0x5; 342 off++; /* Skip over Reserved */ 343 buf[off++] = 4; /* DESIGNATOR LENGTH */ 344 off += 2; /* Skip over Reserved Field */ 345 put_unaligned_be16(tg_pt_gp_id, &buf[off]); 346 off += 2; 347 len += 8; /* Header size + Designation descriptor */ 348 /* 349 * Logical Unit Group identifier, see spc4r17 350 * section 7.7.3.8 351 */ 352 check_lu_gp: 353 lu_gp_mem = dev->dev_alua_lu_gp_mem; 354 if (!lu_gp_mem) 355 goto check_scsi_name; 356 357 spin_lock(&lu_gp_mem->lu_gp_mem_lock); 358 lu_gp = lu_gp_mem->lu_gp; 359 if (!lu_gp) { 360 spin_unlock(&lu_gp_mem->lu_gp_mem_lock); 361 goto check_scsi_name; 362 } 363 lu_gp_id = lu_gp->lu_gp_id; 364 spin_unlock(&lu_gp_mem->lu_gp_mem_lock); 365 366 buf[off++] |= 0x1; /* CODE SET == Binary */ 367 /* DESIGNATOR TYPE == Logical Unit Group identifier */ 368 buf[off++] |= 0x6; 369 off++; /* Skip over Reserved */ 370 buf[off++] = 4; /* DESIGNATOR LENGTH */ 371 off += 2; /* Skip over Reserved Field */ 372 put_unaligned_be16(lu_gp_id, &buf[off]); 373 off += 2; 374 len += 8; /* Header size + Designation descriptor */ 375 /* 376 * SCSI name string designator, see spc4r17 377 * section 7.7.3.11 378 * 379 * Get the PROTOCOL IDENTIFIER as defined by spc4r17 380 * section 7.5.1 Table 362 381 */ 382 check_scsi_name: 383 buf[off] = tpg->proto_id << 4; 384 buf[off++] |= 0x3; /* CODE SET == UTF-8 */ 385 buf[off] = 0x80; /* Set PIV=1 */ 386 /* Set ASSOCIATION == target port: 01b */ 387 buf[off] |= 0x10; 388 /* DESIGNATOR TYPE == SCSI name string */ 389 buf[off++] |= 0x8; 390 off += 2; /* Skip over Reserved and length */ 391 /* 392 * SCSI name string identifer containing, $FABRIC_MOD 393 * dependent information. For LIO-Target and iSCSI 394 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in 395 * UTF-8 encoding. 396 */ 397 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg); 398 scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x", 399 tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt); 400 scsi_name_len += 1 /* Include NULL terminator */; 401 /* 402 * The null-terminated, null-padded (see 4.4.2) SCSI 403 * NAME STRING field contains a UTF-8 format string. 404 * The number of bytes in the SCSI NAME STRING field 405 * (i.e., the value in the DESIGNATOR LENGTH field) 406 * shall be no larger than 256 and shall be a multiple 407 * of four. 408 */ 409 padding = ((-scsi_name_len) & 3); 410 if (padding) 411 scsi_name_len += padding; 412 if (scsi_name_len > 256) 413 scsi_name_len = 256; 414 415 buf[off-1] = scsi_name_len; 416 off += scsi_name_len; 417 /* Header size + Designation descriptor */ 418 len += (scsi_name_len + 4); 419 420 /* 421 * Target device designator 422 */ 423 buf[off] = tpg->proto_id << 4; 424 buf[off++] |= 0x3; /* CODE SET == UTF-8 */ 425 buf[off] = 0x80; /* Set PIV=1 */ 426 /* Set ASSOCIATION == target device: 10b */ 427 buf[off] |= 0x20; 428 /* DESIGNATOR TYPE == SCSI name string */ 429 buf[off++] |= 0x8; 430 off += 2; /* Skip over Reserved and length */ 431 /* 432 * SCSI name string identifer containing, $FABRIC_MOD 433 * dependent information. For LIO-Target and iSCSI 434 * Target Port, this means "<iSCSI name>" in 435 * UTF-8 encoding. 436 */ 437 scsi_target_len = sprintf(&buf[off], "%s", 438 tpg->se_tpg_tfo->tpg_get_wwn(tpg)); 439 scsi_target_len += 1 /* Include NULL terminator */; 440 /* 441 * The null-terminated, null-padded (see 4.4.2) SCSI 442 * NAME STRING field contains a UTF-8 format string. 443 * The number of bytes in the SCSI NAME STRING field 444 * (i.e., the value in the DESIGNATOR LENGTH field) 445 * shall be no larger than 256 and shall be a multiple 446 * of four. 447 */ 448 padding = ((-scsi_target_len) & 3); 449 if (padding) 450 scsi_target_len += padding; 451 if (scsi_target_len > 256) 452 scsi_target_len = 256; 453 454 buf[off-1] = scsi_target_len; 455 off += scsi_target_len; 456 457 /* Header size + Designation descriptor */ 458 len += (scsi_target_len + 4); 459 } 460 put_unaligned_be16(len, &buf[2]); /* Page Length for VPD 0x83 */ 461 return 0; 462 } 463 EXPORT_SYMBOL(spc_emulate_evpd_83); 464 465 /* Extended INQUIRY Data VPD Page */ 466 static sense_reason_t 467 spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf) 468 { 469 struct se_device *dev = cmd->se_dev; 470 struct se_session *sess = cmd->se_sess; 471 472 buf[3] = 0x3c; 473 /* 474 * Set GRD_CHK + REF_CHK for TYPE1 protection, or GRD_CHK 475 * only for TYPE3 protection. 476 */ 477 if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) { 478 if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE1_PROT || 479 cmd->se_sess->sess_prot_type == TARGET_DIF_TYPE1_PROT) 480 buf[4] = 0x5; 481 else if (dev->dev_attrib.pi_prot_type == TARGET_DIF_TYPE3_PROT || 482 cmd->se_sess->sess_prot_type == TARGET_DIF_TYPE3_PROT) 483 buf[4] = 0x4; 484 } 485 486 /* logical unit supports type 1 and type 3 protection */ 487 if ((dev->transport->get_device_type(dev) == TYPE_DISK) && 488 (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) && 489 (dev->dev_attrib.pi_prot_type || cmd->se_sess->sess_prot_type)) { 490 buf[4] |= (0x3 << 3); 491 } 492 493 /* Set HEADSUP, ORDSUP, SIMPSUP */ 494 buf[5] = 0x07; 495 496 /* If WriteCache emulation is enabled, set V_SUP */ 497 if (target_check_wce(dev)) 498 buf[6] = 0x01; 499 /* If an LBA map is present set R_SUP */ 500 spin_lock(&cmd->se_dev->t10_alua.lba_map_lock); 501 if (!list_empty(&dev->t10_alua.lba_map_list)) 502 buf[8] = 0x10; 503 spin_unlock(&cmd->se_dev->t10_alua.lba_map_lock); 504 return 0; 505 } 506 507 /* Block Limits VPD page */ 508 static sense_reason_t 509 spc_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf) 510 { 511 struct se_device *dev = cmd->se_dev; 512 u32 mtl = 0; 513 int have_tp = 0, opt, min; 514 u32 io_max_blocks; 515 516 /* 517 * Following spc3r22 section 6.5.3 Block Limits VPD page, when 518 * emulate_tpu=1 or emulate_tpws=1 we will be expect a 519 * different page length for Thin Provisioning. 520 */ 521 if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) 522 have_tp = 1; 523 524 buf[0] = dev->transport->get_device_type(dev); 525 526 /* Set WSNZ to 1 */ 527 buf[4] = 0x01; 528 /* 529 * Set MAXIMUM COMPARE AND WRITE LENGTH 530 */ 531 if (dev->dev_attrib.emulate_caw) 532 buf[5] = 0x01; 533 534 /* 535 * Set OPTIMAL TRANSFER LENGTH GRANULARITY 536 */ 537 if (dev->transport->get_io_min && (min = dev->transport->get_io_min(dev))) 538 put_unaligned_be16(min / dev->dev_attrib.block_size, &buf[6]); 539 else 540 put_unaligned_be16(1, &buf[6]); 541 542 /* 543 * Set MAXIMUM TRANSFER LENGTH 544 * 545 * XXX: Currently assumes single PAGE_SIZE per scatterlist for fabrics 546 * enforcing maximum HW scatter-gather-list entry limit 547 */ 548 if (cmd->se_tfo->max_data_sg_nents) { 549 mtl = (cmd->se_tfo->max_data_sg_nents * PAGE_SIZE) / 550 dev->dev_attrib.block_size; 551 } 552 io_max_blocks = mult_frac(dev->dev_attrib.hw_max_sectors, 553 dev->dev_attrib.hw_block_size, 554 dev->dev_attrib.block_size); 555 put_unaligned_be32(min_not_zero(mtl, io_max_blocks), &buf[8]); 556 557 /* 558 * Set OPTIMAL TRANSFER LENGTH 559 */ 560 if (dev->transport->get_io_opt && (opt = dev->transport->get_io_opt(dev))) 561 put_unaligned_be32(opt / dev->dev_attrib.block_size, &buf[12]); 562 else 563 put_unaligned_be32(dev->dev_attrib.optimal_sectors, &buf[12]); 564 565 put_unaligned_be16(12, &buf[2]); 566 567 if (!have_tp) 568 goto try_atomic; 569 570 /* 571 * Set MAXIMUM UNMAP LBA COUNT 572 */ 573 put_unaligned_be32(dev->dev_attrib.max_unmap_lba_count, &buf[20]); 574 575 /* 576 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT 577 */ 578 put_unaligned_be32(dev->dev_attrib.max_unmap_block_desc_count, 579 &buf[24]); 580 581 /* 582 * Set OPTIMAL UNMAP GRANULARITY 583 */ 584 put_unaligned_be32(dev->dev_attrib.unmap_granularity, &buf[28]); 585 586 /* 587 * UNMAP GRANULARITY ALIGNMENT 588 */ 589 put_unaligned_be32(dev->dev_attrib.unmap_granularity_alignment, 590 &buf[32]); 591 if (dev->dev_attrib.unmap_granularity_alignment != 0) 592 buf[32] |= 0x80; /* Set the UGAVALID bit */ 593 594 /* 595 * MAXIMUM WRITE SAME LENGTH 596 */ 597 put_unaligned_be64(dev->dev_attrib.max_write_same_len, &buf[36]); 598 599 put_unaligned_be16(40, &buf[2]); 600 601 try_atomic: 602 /* 603 * ATOMIC 604 */ 605 if (!dev->dev_attrib.atomic_max_len) 606 goto done; 607 608 if (dev->dev_attrib.atomic_max_len < io_max_blocks) 609 put_unaligned_be32(dev->dev_attrib.atomic_max_len, &buf[44]); 610 else 611 put_unaligned_be32(io_max_blocks, &buf[44]); 612 613 put_unaligned_be32(dev->dev_attrib.atomic_alignment, &buf[48]); 614 put_unaligned_be32(dev->dev_attrib.atomic_granularity, &buf[52]); 615 put_unaligned_be32(dev->dev_attrib.atomic_max_with_boundary, &buf[56]); 616 put_unaligned_be32(dev->dev_attrib.atomic_max_boundary, &buf[60]); 617 618 put_unaligned_be16(60, &buf[2]); 619 done: 620 return 0; 621 } 622 623 /* Block Device Characteristics VPD page */ 624 static sense_reason_t 625 spc_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf) 626 { 627 struct se_device *dev = cmd->se_dev; 628 629 buf[0] = dev->transport->get_device_type(dev); 630 buf[3] = 0x3c; 631 buf[5] = dev->dev_attrib.is_nonrot ? 1 : 0; 632 633 return 0; 634 } 635 636 /* Thin Provisioning VPD */ 637 static sense_reason_t 638 spc_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf) 639 { 640 struct se_device *dev = cmd->se_dev; 641 642 /* 643 * From spc3r22 section 6.5.4 Thin Provisioning VPD page: 644 * 645 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to 646 * zero, then the page length shall be set to 0004h. If the DP bit 647 * is set to one, then the page length shall be set to the value 648 * defined in table 162. 649 */ 650 buf[0] = dev->transport->get_device_type(dev); 651 652 /* 653 * Set Hardcoded length mentioned above for DP=0 654 */ 655 put_unaligned_be16(0x0004, &buf[2]); 656 657 /* 658 * The THRESHOLD EXPONENT field indicates the threshold set size in 659 * LBAs as a power of 2 (i.e., the threshold set size is equal to 660 * 2(threshold exponent)). 661 * 662 * Note that this is currently set to 0x00 as mkp says it will be 663 * changing again. We can enable this once it has settled in T10 664 * and is actually used by Linux/SCSI ML code. 665 */ 666 buf[4] = 0x00; 667 668 /* 669 * A TPU bit set to one indicates that the device server supports 670 * the UNMAP command (see 5.25). A TPU bit set to zero indicates 671 * that the device server does not support the UNMAP command. 672 */ 673 if (dev->dev_attrib.emulate_tpu != 0) 674 buf[5] = 0x80; 675 676 /* 677 * A TPWS bit set to one indicates that the device server supports 678 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs. 679 * A TPWS bit set to zero indicates that the device server does not 680 * support the use of the WRITE SAME (16) command to unmap LBAs. 681 */ 682 if (dev->dev_attrib.emulate_tpws != 0) 683 buf[5] |= 0x40 | 0x20; 684 685 /* 686 * The unmap_zeroes_data set means that the underlying device supports 687 * REQ_OP_DISCARD and has the discard_zeroes_data bit set. This 688 * satisfies the SBC requirements for LBPRZ, meaning that a subsequent 689 * read will return zeroes after an UNMAP or WRITE SAME (16) to an LBA 690 * See sbc4r36 6.6.4. 691 */ 692 if (((dev->dev_attrib.emulate_tpu != 0) || 693 (dev->dev_attrib.emulate_tpws != 0)) && 694 (dev->dev_attrib.unmap_zeroes_data != 0)) 695 buf[5] |= 0x04; 696 697 return 0; 698 } 699 700 /* Referrals VPD page */ 701 static sense_reason_t 702 spc_emulate_evpd_b3(struct se_cmd *cmd, unsigned char *buf) 703 { 704 struct se_device *dev = cmd->se_dev; 705 706 buf[0] = dev->transport->get_device_type(dev); 707 buf[3] = 0x0c; 708 put_unaligned_be32(dev->t10_alua.lba_map_segment_size, &buf[8]); 709 put_unaligned_be32(dev->t10_alua.lba_map_segment_multiplier, &buf[12]); 710 711 return 0; 712 } 713 714 static sense_reason_t 715 spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf); 716 717 static struct { 718 uint8_t page; 719 sense_reason_t (*emulate)(struct se_cmd *, unsigned char *); 720 } evpd_handlers[] = { 721 { .page = 0x00, .emulate = spc_emulate_evpd_00 }, 722 { .page = 0x80, .emulate = spc_emulate_evpd_80 }, 723 { .page = 0x83, .emulate = spc_emulate_evpd_83 }, 724 { .page = 0x86, .emulate = spc_emulate_evpd_86 }, 725 { .page = 0xb0, .emulate = spc_emulate_evpd_b0 }, 726 { .page = 0xb1, .emulate = spc_emulate_evpd_b1 }, 727 { .page = 0xb2, .emulate = spc_emulate_evpd_b2 }, 728 { .page = 0xb3, .emulate = spc_emulate_evpd_b3 }, 729 }; 730 731 /* supported vital product data pages */ 732 static sense_reason_t 733 spc_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf) 734 { 735 int p; 736 737 /* 738 * Only report the INQUIRY EVPD=1 pages after a valid NAA 739 * Registered Extended LUN WWN has been set via ConfigFS 740 * during device creation/restart. 741 */ 742 if (cmd->se_dev->dev_flags & DF_EMULATED_VPD_UNIT_SERIAL) { 743 buf[3] = ARRAY_SIZE(evpd_handlers); 744 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) 745 buf[p + 4] = evpd_handlers[p].page; 746 } 747 748 return 0; 749 } 750 751 static sense_reason_t 752 spc_emulate_inquiry(struct se_cmd *cmd) 753 { 754 struct se_device *dev = cmd->se_dev; 755 unsigned char *rbuf; 756 unsigned char *cdb = cmd->t_task_cdb; 757 unsigned char *buf; 758 sense_reason_t ret; 759 int p; 760 int len = 0; 761 762 buf = kzalloc(SE_INQUIRY_BUF, GFP_KERNEL); 763 if (!buf) { 764 pr_err("Unable to allocate response buffer for INQUIRY\n"); 765 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 766 } 767 768 buf[0] = dev->transport->get_device_type(dev); 769 770 if (!(cdb[1] & 0x1)) { 771 if (cdb[2]) { 772 pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n", 773 cdb[2]); 774 ret = TCM_INVALID_CDB_FIELD; 775 goto out; 776 } 777 778 ret = spc_emulate_inquiry_std(cmd, buf); 779 len = buf[4] + 5; 780 goto out; 781 } 782 783 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) { 784 if (cdb[2] == evpd_handlers[p].page) { 785 buf[1] = cdb[2]; 786 ret = evpd_handlers[p].emulate(cmd, buf); 787 len = get_unaligned_be16(&buf[2]) + 4; 788 goto out; 789 } 790 } 791 792 pr_debug("Unknown VPD Code: 0x%02x\n", cdb[2]); 793 ret = TCM_INVALID_CDB_FIELD; 794 795 out: 796 rbuf = transport_kmap_data_sg(cmd); 797 if (rbuf) { 798 memcpy(rbuf, buf, min_t(u32, SE_INQUIRY_BUF, cmd->data_length)); 799 transport_kunmap_data_sg(cmd); 800 } 801 kfree(buf); 802 803 if (!ret) 804 target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, len); 805 return ret; 806 } 807 808 static int spc_modesense_rwrecovery(struct se_cmd *cmd, u8 pc, u8 *p) 809 { 810 p[0] = 0x01; 811 p[1] = 0x0a; 812 813 /* No changeable values for now */ 814 if (pc == 1) 815 goto out; 816 817 out: 818 return 12; 819 } 820 821 static int spc_modesense_control(struct se_cmd *cmd, u8 pc, u8 *p) 822 { 823 struct se_device *dev = cmd->se_dev; 824 struct se_session *sess = cmd->se_sess; 825 826 p[0] = 0x0a; 827 p[1] = 0x0a; 828 829 /* No changeable values for now */ 830 if (pc == 1) 831 goto out; 832 833 /* GLTSD: No implicit save of log parameters */ 834 p[2] = (1 << 1); 835 if (target_sense_desc_format(dev)) 836 /* D_SENSE: Descriptor format sense data for 64bit sectors */ 837 p[2] |= (1 << 2); 838 839 /* 840 * From spc4r23, 7.4.7 Control mode page 841 * 842 * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies 843 * restrictions on the algorithm used for reordering commands 844 * having the SIMPLE task attribute (see SAM-4). 845 * 846 * Table 368 -- QUEUE ALGORITHM MODIFIER field 847 * Code Description 848 * 0h Restricted reordering 849 * 1h Unrestricted reordering allowed 850 * 2h to 7h Reserved 851 * 8h to Fh Vendor specific 852 * 853 * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that 854 * the device server shall order the processing sequence of commands 855 * having the SIMPLE task attribute such that data integrity is maintained 856 * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol 857 * requests is halted at any time, the final value of all data observable 858 * on the medium shall be the same as if all the commands had been processed 859 * with the ORDERED task attribute). 860 * 861 * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the 862 * device server may reorder the processing sequence of commands having the 863 * SIMPLE task attribute in any manner. Any data integrity exposures related to 864 * command sequence order shall be explicitly handled by the application client 865 * through the selection of appropriate ommands and task attributes. 866 */ 867 p[3] = (dev->dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10; 868 /* 869 * From spc4r17, section 7.4.6 Control mode Page 870 * 871 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b 872 * 873 * 00b: The logical unit shall clear any unit attention condition 874 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION 875 * status and shall not establish a unit attention condition when a com- 876 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT 877 * status. 878 * 879 * 10b: The logical unit shall not clear any unit attention condition 880 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION 881 * status and shall not establish a unit attention condition when 882 * a command is completed with BUSY, TASK SET FULL, or RESERVATION 883 * CONFLICT status. 884 * 885 * 11b a The logical unit shall not clear any unit attention condition 886 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION 887 * status and shall establish a unit attention condition for the 888 * initiator port associated with the I_T nexus on which the BUSY, 889 * TASK SET FULL, or RESERVATION CONFLICT status is being returned. 890 * Depending on the status, the additional sense code shall be set to 891 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS 892 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE 893 * command, a unit attention condition shall be established only once 894 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless 895 * to the number of commands completed with one of those status codes. 896 */ 897 switch (dev->dev_attrib.emulate_ua_intlck_ctrl) { 898 case TARGET_UA_INTLCK_CTRL_ESTABLISH_UA: 899 p[4] = 0x30; 900 break; 901 case TARGET_UA_INTLCK_CTRL_NO_CLEAR: 902 p[4] = 0x20; 903 break; 904 default: /* TARGET_UA_INTLCK_CTRL_CLEAR */ 905 p[4] = 0x00; 906 break; 907 } 908 /* 909 * From spc4r17, section 7.4.6 Control mode Page 910 * 911 * Task Aborted Status (TAS) bit set to zero. 912 * 913 * A task aborted status (TAS) bit set to zero specifies that aborted 914 * tasks shall be terminated by the device server without any response 915 * to the application client. A TAS bit set to one specifies that tasks 916 * aborted by the actions of an I_T nexus other than the I_T nexus on 917 * which the command was received shall be completed with TASK ABORTED 918 * status (see SAM-4). 919 */ 920 p[5] = (dev->dev_attrib.emulate_tas) ? 0x40 : 0x00; 921 /* 922 * From spc4r30, section 7.5.7 Control mode page 923 * 924 * Application Tag Owner (ATO) bit set to one. 925 * 926 * If the ATO bit is set to one the device server shall not modify the 927 * LOGICAL BLOCK APPLICATION TAG field and, depending on the protection 928 * type, shall not modify the contents of the LOGICAL BLOCK REFERENCE 929 * TAG field. 930 */ 931 if (sess->sup_prot_ops & (TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS)) { 932 if (dev->dev_attrib.pi_prot_type || sess->sess_prot_type) 933 p[5] |= 0x80; 934 } 935 936 p[8] = 0xff; 937 p[9] = 0xff; 938 p[11] = 30; 939 940 out: 941 return 12; 942 } 943 944 static int spc_modesense_caching(struct se_cmd *cmd, u8 pc, u8 *p) 945 { 946 struct se_device *dev = cmd->se_dev; 947 948 p[0] = 0x08; 949 p[1] = 0x12; 950 951 /* No changeable values for now */ 952 if (pc == 1) 953 goto out; 954 955 if (target_check_wce(dev)) 956 p[2] = 0x04; /* Write Cache Enable */ 957 p[12] = 0x20; /* Disabled Read Ahead */ 958 959 out: 960 return 20; 961 } 962 963 static int spc_modesense_informational_exceptions(struct se_cmd *cmd, u8 pc, unsigned char *p) 964 { 965 p[0] = 0x1c; 966 p[1] = 0x0a; 967 968 /* No changeable values for now */ 969 if (pc == 1) 970 goto out; 971 972 out: 973 return 12; 974 } 975 976 static struct { 977 uint8_t page; 978 uint8_t subpage; 979 int (*emulate)(struct se_cmd *, u8, unsigned char *); 980 } modesense_handlers[] = { 981 { .page = 0x01, .subpage = 0x00, .emulate = spc_modesense_rwrecovery }, 982 { .page = 0x08, .subpage = 0x00, .emulate = spc_modesense_caching }, 983 { .page = 0x0a, .subpage = 0x00, .emulate = spc_modesense_control }, 984 { .page = 0x1c, .subpage = 0x00, .emulate = spc_modesense_informational_exceptions }, 985 }; 986 987 static void spc_modesense_write_protect(unsigned char *buf, int type) 988 { 989 /* 990 * I believe that the WP bit (bit 7) in the mode header is the same for 991 * all device types.. 992 */ 993 switch (type) { 994 case TYPE_DISK: 995 case TYPE_TAPE: 996 default: 997 buf[0] |= 0x80; /* WP bit */ 998 break; 999 } 1000 } 1001 1002 static void spc_modesense_dpofua(unsigned char *buf, int type) 1003 { 1004 switch (type) { 1005 case TYPE_DISK: 1006 buf[0] |= 0x10; /* DPOFUA bit */ 1007 break; 1008 default: 1009 break; 1010 } 1011 } 1012 1013 static int spc_modesense_blockdesc(unsigned char *buf, u64 blocks, u32 block_size) 1014 { 1015 *buf++ = 8; 1016 put_unaligned_be32(min(blocks, 0xffffffffull), buf); 1017 buf += 4; 1018 put_unaligned_be32(block_size, buf); 1019 return 9; 1020 } 1021 1022 static int spc_modesense_long_blockdesc(unsigned char *buf, u64 blocks, u32 block_size) 1023 { 1024 if (blocks <= 0xffffffff) 1025 return spc_modesense_blockdesc(buf + 3, blocks, block_size) + 3; 1026 1027 *buf++ = 1; /* LONGLBA */ 1028 buf += 2; 1029 *buf++ = 16; 1030 put_unaligned_be64(blocks, buf); 1031 buf += 12; 1032 put_unaligned_be32(block_size, buf); 1033 1034 return 17; 1035 } 1036 1037 static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd) 1038 { 1039 struct se_device *dev = cmd->se_dev; 1040 char *cdb = cmd->t_task_cdb; 1041 unsigned char buf[SE_MODE_PAGE_BUF], *rbuf; 1042 int type = dev->transport->get_device_type(dev); 1043 int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10); 1044 bool dbd = !!(cdb[1] & 0x08); 1045 bool llba = ten ? !!(cdb[1] & 0x10) : false; 1046 u8 pc = cdb[2] >> 6; 1047 u8 page = cdb[2] & 0x3f; 1048 u8 subpage = cdb[3]; 1049 int length = 0; 1050 int ret; 1051 int i; 1052 1053 memset(buf, 0, SE_MODE_PAGE_BUF); 1054 1055 /* 1056 * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for 1057 * MODE_SENSE_10 and byte 2 for MODE_SENSE (6). 1058 */ 1059 length = ten ? 3 : 2; 1060 1061 /* DEVICE-SPECIFIC PARAMETER */ 1062 if (cmd->se_lun->lun_access_ro || target_lun_is_rdonly(cmd)) 1063 spc_modesense_write_protect(&buf[length], type); 1064 1065 /* 1066 * SBC only allows us to enable FUA and DPO together. Fortunately 1067 * DPO is explicitly specified as a hint, so a noop is a perfectly 1068 * valid implementation. 1069 */ 1070 if (target_check_fua(dev)) 1071 spc_modesense_dpofua(&buf[length], type); 1072 1073 ++length; 1074 1075 /* BLOCK DESCRIPTOR */ 1076 1077 /* 1078 * For now we only include a block descriptor for disk (SBC) 1079 * devices; other command sets use a slightly different format. 1080 */ 1081 if (!dbd && type == TYPE_DISK) { 1082 u64 blocks = dev->transport->get_blocks(dev); 1083 u32 block_size = dev->dev_attrib.block_size; 1084 1085 if (ten) { 1086 if (llba) { 1087 length += spc_modesense_long_blockdesc(&buf[length], 1088 blocks, block_size); 1089 } else { 1090 length += 3; 1091 length += spc_modesense_blockdesc(&buf[length], 1092 blocks, block_size); 1093 } 1094 } else { 1095 length += spc_modesense_blockdesc(&buf[length], blocks, 1096 block_size); 1097 } 1098 } else { 1099 if (ten) 1100 length += 4; 1101 else 1102 length += 1; 1103 } 1104 1105 if (page == 0x3f) { 1106 if (subpage != 0x00 && subpage != 0xff) { 1107 pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage); 1108 return TCM_INVALID_CDB_FIELD; 1109 } 1110 1111 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) { 1112 /* 1113 * Tricky way to say all subpage 00h for 1114 * subpage==0, all subpages for subpage==0xff 1115 * (and we just checked above that those are 1116 * the only two possibilities). 1117 */ 1118 if ((modesense_handlers[i].subpage & ~subpage) == 0) { 1119 ret = modesense_handlers[i].emulate(cmd, pc, &buf[length]); 1120 if (!ten && length + ret >= 255) 1121 break; 1122 length += ret; 1123 } 1124 } 1125 1126 goto set_length; 1127 } 1128 1129 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) 1130 if (modesense_handlers[i].page == page && 1131 modesense_handlers[i].subpage == subpage) { 1132 length += modesense_handlers[i].emulate(cmd, pc, &buf[length]); 1133 goto set_length; 1134 } 1135 1136 /* 1137 * We don't intend to implement: 1138 * - obsolete page 03h "format parameters" (checked by Solaris) 1139 */ 1140 if (page != 0x03) 1141 pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n", 1142 page, subpage); 1143 1144 return TCM_UNKNOWN_MODE_PAGE; 1145 1146 set_length: 1147 if (ten) 1148 put_unaligned_be16(length - 2, buf); 1149 else 1150 buf[0] = length - 1; 1151 1152 rbuf = transport_kmap_data_sg(cmd); 1153 if (rbuf) { 1154 memcpy(rbuf, buf, min_t(u32, SE_MODE_PAGE_BUF, cmd->data_length)); 1155 transport_kunmap_data_sg(cmd); 1156 } 1157 1158 target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, length); 1159 return 0; 1160 } 1161 1162 static sense_reason_t spc_emulate_modeselect(struct se_cmd *cmd) 1163 { 1164 char *cdb = cmd->t_task_cdb; 1165 bool ten = cdb[0] == MODE_SELECT_10; 1166 int off = ten ? 8 : 4; 1167 bool pf = !!(cdb[1] & 0x10); 1168 u8 page, subpage; 1169 unsigned char *buf; 1170 unsigned char tbuf[SE_MODE_PAGE_BUF]; 1171 int length; 1172 sense_reason_t ret = 0; 1173 int i; 1174 1175 if (!cmd->data_length) { 1176 target_complete_cmd(cmd, SAM_STAT_GOOD); 1177 return 0; 1178 } 1179 1180 if (cmd->data_length < off + 2) 1181 return TCM_PARAMETER_LIST_LENGTH_ERROR; 1182 1183 buf = transport_kmap_data_sg(cmd); 1184 if (!buf) 1185 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1186 1187 if (!pf) { 1188 ret = TCM_INVALID_CDB_FIELD; 1189 goto out; 1190 } 1191 1192 page = buf[off] & 0x3f; 1193 subpage = buf[off] & 0x40 ? buf[off + 1] : 0; 1194 1195 for (i = 0; i < ARRAY_SIZE(modesense_handlers); ++i) 1196 if (modesense_handlers[i].page == page && 1197 modesense_handlers[i].subpage == subpage) { 1198 memset(tbuf, 0, SE_MODE_PAGE_BUF); 1199 length = modesense_handlers[i].emulate(cmd, 0, tbuf); 1200 goto check_contents; 1201 } 1202 1203 ret = TCM_UNKNOWN_MODE_PAGE; 1204 goto out; 1205 1206 check_contents: 1207 if (cmd->data_length < off + length) { 1208 ret = TCM_PARAMETER_LIST_LENGTH_ERROR; 1209 goto out; 1210 } 1211 1212 if (memcmp(buf + off, tbuf, length)) 1213 ret = TCM_INVALID_PARAMETER_LIST; 1214 1215 out: 1216 transport_kunmap_data_sg(cmd); 1217 1218 if (!ret) 1219 target_complete_cmd(cmd, SAM_STAT_GOOD); 1220 return ret; 1221 } 1222 1223 static sense_reason_t spc_emulate_request_sense(struct se_cmd *cmd) 1224 { 1225 unsigned char *cdb = cmd->t_task_cdb; 1226 unsigned char *rbuf; 1227 u8 ua_asc = 0, ua_ascq = 0; 1228 unsigned char buf[SE_SENSE_BUF]; 1229 bool desc_format = target_sense_desc_format(cmd->se_dev); 1230 1231 memset(buf, 0, SE_SENSE_BUF); 1232 1233 if (cdb[1] & 0x01) { 1234 pr_err("REQUEST_SENSE description emulation not" 1235 " supported\n"); 1236 return TCM_INVALID_CDB_FIELD; 1237 } 1238 1239 rbuf = transport_kmap_data_sg(cmd); 1240 if (!rbuf) 1241 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1242 1243 if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) 1244 scsi_build_sense_buffer(desc_format, buf, UNIT_ATTENTION, 1245 ua_asc, ua_ascq); 1246 else 1247 scsi_build_sense_buffer(desc_format, buf, NO_SENSE, 0x0, 0x0); 1248 1249 memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length)); 1250 transport_kunmap_data_sg(cmd); 1251 1252 target_complete_cmd(cmd, SAM_STAT_GOOD); 1253 return 0; 1254 } 1255 1256 sense_reason_t spc_emulate_report_luns(struct se_cmd *cmd) 1257 { 1258 struct se_dev_entry *deve; 1259 struct se_session *sess = cmd->se_sess; 1260 struct se_node_acl *nacl; 1261 struct scsi_lun slun; 1262 unsigned char *buf; 1263 u32 lun_count = 0, offset = 8; 1264 __be32 len; 1265 1266 buf = transport_kmap_data_sg(cmd); 1267 if (cmd->data_length && !buf) 1268 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1269 1270 /* 1271 * If no struct se_session pointer is present, this struct se_cmd is 1272 * coming via a target_core_mod PASSTHROUGH op, and not through 1273 * a $FABRIC_MOD. In that case, report LUN=0 only. 1274 */ 1275 if (!sess) 1276 goto done; 1277 1278 nacl = sess->se_node_acl; 1279 1280 rcu_read_lock(); 1281 hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) { 1282 /* 1283 * We determine the correct LUN LIST LENGTH even once we 1284 * have reached the initial allocation length. 1285 * See SPC2-R20 7.19. 1286 */ 1287 lun_count++; 1288 if (offset >= cmd->data_length) 1289 continue; 1290 1291 int_to_scsilun(deve->mapped_lun, &slun); 1292 memcpy(buf + offset, &slun, 1293 min(8u, cmd->data_length - offset)); 1294 offset += 8; 1295 } 1296 rcu_read_unlock(); 1297 1298 /* 1299 * See SPC3 r07, page 159. 1300 */ 1301 done: 1302 /* 1303 * If no LUNs are accessible, report virtual LUN 0. 1304 */ 1305 if (lun_count == 0) { 1306 int_to_scsilun(0, &slun); 1307 if (cmd->data_length > 8) 1308 memcpy(buf + offset, &slun, 1309 min(8u, cmd->data_length - offset)); 1310 lun_count = 1; 1311 } 1312 1313 if (buf) { 1314 len = cpu_to_be32(lun_count * 8); 1315 memcpy(buf, &len, min_t(int, sizeof len, cmd->data_length)); 1316 transport_kunmap_data_sg(cmd); 1317 } 1318 1319 target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, 8 + lun_count * 8); 1320 return 0; 1321 } 1322 EXPORT_SYMBOL(spc_emulate_report_luns); 1323 1324 static sense_reason_t 1325 spc_emulate_testunitready(struct se_cmd *cmd) 1326 { 1327 target_complete_cmd(cmd, SAM_STAT_GOOD); 1328 return 0; 1329 } 1330 1331 static void set_dpofua_usage_bits(u8 *usage_bits, struct se_device *dev) 1332 { 1333 if (!target_check_fua(dev)) 1334 usage_bits[1] &= ~0x18; 1335 else 1336 usage_bits[1] |= 0x18; 1337 } 1338 1339 static void set_dpofua_usage_bits32(u8 *usage_bits, struct se_device *dev) 1340 { 1341 if (!target_check_fua(dev)) 1342 usage_bits[10] &= ~0x18; 1343 else 1344 usage_bits[10] |= 0x18; 1345 } 1346 1347 static const struct target_opcode_descriptor tcm_opcode_read6 = { 1348 .support = SCSI_SUPPORT_FULL, 1349 .opcode = READ_6, 1350 .cdb_size = 6, 1351 .usage_bits = {READ_6, 0x1f, 0xff, 0xff, 1352 0xff, SCSI_CONTROL_MASK}, 1353 }; 1354 1355 static const struct target_opcode_descriptor tcm_opcode_read10 = { 1356 .support = SCSI_SUPPORT_FULL, 1357 .opcode = READ_10, 1358 .cdb_size = 10, 1359 .usage_bits = {READ_10, 0xf8, 0xff, 0xff, 1360 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff, 1361 0xff, SCSI_CONTROL_MASK}, 1362 .update_usage_bits = set_dpofua_usage_bits, 1363 }; 1364 1365 static const struct target_opcode_descriptor tcm_opcode_read12 = { 1366 .support = SCSI_SUPPORT_FULL, 1367 .opcode = READ_12, 1368 .cdb_size = 12, 1369 .usage_bits = {READ_12, 0xf8, 0xff, 0xff, 1370 0xff, 0xff, 0xff, 0xff, 1371 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK}, 1372 .update_usage_bits = set_dpofua_usage_bits, 1373 }; 1374 1375 static const struct target_opcode_descriptor tcm_opcode_read16 = { 1376 .support = SCSI_SUPPORT_FULL, 1377 .opcode = READ_16, 1378 .cdb_size = 16, 1379 .usage_bits = {READ_16, 0xf8, 0xff, 0xff, 1380 0xff, 0xff, 0xff, 0xff, 1381 0xff, 0xff, 0xff, 0xff, 1382 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK}, 1383 .update_usage_bits = set_dpofua_usage_bits, 1384 }; 1385 1386 static const struct target_opcode_descriptor tcm_opcode_write6 = { 1387 .support = SCSI_SUPPORT_FULL, 1388 .opcode = WRITE_6, 1389 .cdb_size = 6, 1390 .usage_bits = {WRITE_6, 0x1f, 0xff, 0xff, 1391 0xff, SCSI_CONTROL_MASK}, 1392 }; 1393 1394 static const struct target_opcode_descriptor tcm_opcode_write10 = { 1395 .support = SCSI_SUPPORT_FULL, 1396 .opcode = WRITE_10, 1397 .cdb_size = 10, 1398 .usage_bits = {WRITE_10, 0xf8, 0xff, 0xff, 1399 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff, 1400 0xff, SCSI_CONTROL_MASK}, 1401 .update_usage_bits = set_dpofua_usage_bits, 1402 }; 1403 1404 static const struct target_opcode_descriptor tcm_opcode_write_verify10 = { 1405 .support = SCSI_SUPPORT_FULL, 1406 .opcode = WRITE_VERIFY, 1407 .cdb_size = 10, 1408 .usage_bits = {WRITE_VERIFY, 0xf0, 0xff, 0xff, 1409 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff, 1410 0xff, SCSI_CONTROL_MASK}, 1411 .update_usage_bits = set_dpofua_usage_bits, 1412 }; 1413 1414 static const struct target_opcode_descriptor tcm_opcode_write12 = { 1415 .support = SCSI_SUPPORT_FULL, 1416 .opcode = WRITE_12, 1417 .cdb_size = 12, 1418 .usage_bits = {WRITE_12, 0xf8, 0xff, 0xff, 1419 0xff, 0xff, 0xff, 0xff, 1420 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK}, 1421 .update_usage_bits = set_dpofua_usage_bits, 1422 }; 1423 1424 static const struct target_opcode_descriptor tcm_opcode_write16 = { 1425 .support = SCSI_SUPPORT_FULL, 1426 .opcode = WRITE_16, 1427 .cdb_size = 16, 1428 .usage_bits = {WRITE_16, 0xf8, 0xff, 0xff, 1429 0xff, 0xff, 0xff, 0xff, 1430 0xff, 0xff, 0xff, 0xff, 1431 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK}, 1432 .update_usage_bits = set_dpofua_usage_bits, 1433 }; 1434 1435 static const struct target_opcode_descriptor tcm_opcode_write_verify16 = { 1436 .support = SCSI_SUPPORT_FULL, 1437 .opcode = WRITE_VERIFY_16, 1438 .cdb_size = 16, 1439 .usage_bits = {WRITE_VERIFY_16, 0xf0, 0xff, 0xff, 1440 0xff, 0xff, 0xff, 0xff, 1441 0xff, 0xff, 0xff, 0xff, 1442 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK}, 1443 .update_usage_bits = set_dpofua_usage_bits, 1444 }; 1445 1446 static bool tcm_is_ws_enabled(const struct target_opcode_descriptor *descr, 1447 struct se_cmd *cmd) 1448 { 1449 struct exec_cmd_ops *ops = cmd->protocol_data; 1450 struct se_device *dev = cmd->se_dev; 1451 1452 return (dev->dev_attrib.emulate_tpws && !!ops->execute_unmap) || 1453 !!ops->execute_write_same; 1454 } 1455 1456 static const struct target_opcode_descriptor tcm_opcode_write_same32 = { 1457 .support = SCSI_SUPPORT_FULL, 1458 .serv_action_valid = 1, 1459 .opcode = VARIABLE_LENGTH_CMD, 1460 .service_action = WRITE_SAME_32, 1461 .cdb_size = 32, 1462 .usage_bits = {VARIABLE_LENGTH_CMD, SCSI_CONTROL_MASK, 0x00, 0x00, 1463 0x00, 0x00, SCSI_GROUP_NUMBER_MASK, 0x18, 1464 0x00, WRITE_SAME_32, 0xe8, 0x00, 1465 0xff, 0xff, 0xff, 0xff, 1466 0xff, 0xff, 0xff, 0xff, 1467 0x00, 0x00, 0x00, 0x00, 1468 0x00, 0x00, 0x00, 0x00, 1469 0xff, 0xff, 0xff, 0xff}, 1470 .enabled = tcm_is_ws_enabled, 1471 .update_usage_bits = set_dpofua_usage_bits32, 1472 }; 1473 1474 static bool tcm_is_atomic_enabled(const struct target_opcode_descriptor *descr, 1475 struct se_cmd *cmd) 1476 { 1477 return cmd->se_dev->dev_attrib.atomic_max_len; 1478 } 1479 1480 static struct target_opcode_descriptor tcm_opcode_write_atomic16 = { 1481 .support = SCSI_SUPPORT_FULL, 1482 .opcode = WRITE_ATOMIC_16, 1483 .cdb_size = 16, 1484 .usage_bits = {WRITE_ATOMIC_16, 0xf8, 0xff, 0xff, 1485 0xff, 0xff, 0xff, 0xff, 1486 0xff, 0xff, 0xff, 0xff, 1487 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK}, 1488 .enabled = tcm_is_atomic_enabled, 1489 .update_usage_bits = set_dpofua_usage_bits, 1490 }; 1491 1492 static bool tcm_is_caw_enabled(const struct target_opcode_descriptor *descr, 1493 struct se_cmd *cmd) 1494 { 1495 struct se_device *dev = cmd->se_dev; 1496 1497 return dev->dev_attrib.emulate_caw; 1498 } 1499 1500 static const struct target_opcode_descriptor tcm_opcode_compare_write = { 1501 .support = SCSI_SUPPORT_FULL, 1502 .opcode = COMPARE_AND_WRITE, 1503 .cdb_size = 16, 1504 .usage_bits = {COMPARE_AND_WRITE, 0x18, 0xff, 0xff, 1505 0xff, 0xff, 0xff, 0xff, 1506 0xff, 0xff, 0x00, 0x00, 1507 0x00, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK}, 1508 .enabled = tcm_is_caw_enabled, 1509 .update_usage_bits = set_dpofua_usage_bits, 1510 }; 1511 1512 static const struct target_opcode_descriptor tcm_opcode_read_capacity = { 1513 .support = SCSI_SUPPORT_FULL, 1514 .opcode = READ_CAPACITY, 1515 .cdb_size = 10, 1516 .usage_bits = {READ_CAPACITY, 0x00, 0xff, 0xff, 1517 0xff, 0xff, 0x00, 0x00, 1518 0x01, SCSI_CONTROL_MASK}, 1519 }; 1520 1521 static const struct target_opcode_descriptor tcm_opcode_read_capacity16 = { 1522 .support = SCSI_SUPPORT_FULL, 1523 .serv_action_valid = 1, 1524 .opcode = SERVICE_ACTION_IN_16, 1525 .service_action = SAI_READ_CAPACITY_16, 1526 .cdb_size = 16, 1527 .usage_bits = {SERVICE_ACTION_IN_16, SAI_READ_CAPACITY_16, 0x00, 0x00, 1528 0x00, 0x00, 0x00, 0x00, 1529 0x00, 0x00, 0xff, 0xff, 1530 0xff, 0xff, 0x00, SCSI_CONTROL_MASK}, 1531 }; 1532 1533 static bool tcm_is_rep_ref_enabled(const struct target_opcode_descriptor *descr, 1534 struct se_cmd *cmd) 1535 { 1536 struct se_device *dev = cmd->se_dev; 1537 1538 spin_lock(&dev->t10_alua.lba_map_lock); 1539 if (list_empty(&dev->t10_alua.lba_map_list)) { 1540 spin_unlock(&dev->t10_alua.lba_map_lock); 1541 return false; 1542 } 1543 spin_unlock(&dev->t10_alua.lba_map_lock); 1544 return true; 1545 } 1546 1547 static const struct target_opcode_descriptor tcm_opcode_read_report_refferals = { 1548 .support = SCSI_SUPPORT_FULL, 1549 .serv_action_valid = 1, 1550 .opcode = SERVICE_ACTION_IN_16, 1551 .service_action = SAI_REPORT_REFERRALS, 1552 .cdb_size = 16, 1553 .usage_bits = {SERVICE_ACTION_IN_16, SAI_REPORT_REFERRALS, 0x00, 0x00, 1554 0x00, 0x00, 0x00, 0x00, 1555 0x00, 0x00, 0xff, 0xff, 1556 0xff, 0xff, 0x00, SCSI_CONTROL_MASK}, 1557 .enabled = tcm_is_rep_ref_enabled, 1558 }; 1559 1560 static const struct target_opcode_descriptor tcm_opcode_sync_cache = { 1561 .support = SCSI_SUPPORT_FULL, 1562 .opcode = SYNCHRONIZE_CACHE, 1563 .cdb_size = 10, 1564 .usage_bits = {SYNCHRONIZE_CACHE, 0x02, 0xff, 0xff, 1565 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff, 1566 0xff, SCSI_CONTROL_MASK}, 1567 }; 1568 1569 static const struct target_opcode_descriptor tcm_opcode_sync_cache16 = { 1570 .support = SCSI_SUPPORT_FULL, 1571 .opcode = SYNCHRONIZE_CACHE_16, 1572 .cdb_size = 16, 1573 .usage_bits = {SYNCHRONIZE_CACHE_16, 0x02, 0xff, 0xff, 1574 0xff, 0xff, 0xff, 0xff, 1575 0xff, 0xff, 0xff, 0xff, 1576 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK}, 1577 }; 1578 1579 static bool tcm_is_unmap_enabled(const struct target_opcode_descriptor *descr, 1580 struct se_cmd *cmd) 1581 { 1582 struct exec_cmd_ops *ops = cmd->protocol_data; 1583 struct se_device *dev = cmd->se_dev; 1584 1585 return ops->execute_unmap && dev->dev_attrib.emulate_tpu; 1586 } 1587 1588 static const struct target_opcode_descriptor tcm_opcode_unmap = { 1589 .support = SCSI_SUPPORT_FULL, 1590 .opcode = UNMAP, 1591 .cdb_size = 10, 1592 .usage_bits = {UNMAP, 0x00, 0x00, 0x00, 1593 0x00, 0x00, SCSI_GROUP_NUMBER_MASK, 0xff, 1594 0xff, SCSI_CONTROL_MASK}, 1595 .enabled = tcm_is_unmap_enabled, 1596 }; 1597 1598 static const struct target_opcode_descriptor tcm_opcode_write_same = { 1599 .support = SCSI_SUPPORT_FULL, 1600 .opcode = WRITE_SAME, 1601 .cdb_size = 10, 1602 .usage_bits = {WRITE_SAME, 0xe8, 0xff, 0xff, 1603 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff, 1604 0xff, SCSI_CONTROL_MASK}, 1605 .enabled = tcm_is_ws_enabled, 1606 }; 1607 1608 static const struct target_opcode_descriptor tcm_opcode_write_same16 = { 1609 .support = SCSI_SUPPORT_FULL, 1610 .opcode = WRITE_SAME_16, 1611 .cdb_size = 16, 1612 .usage_bits = {WRITE_SAME_16, 0xe8, 0xff, 0xff, 1613 0xff, 0xff, 0xff, 0xff, 1614 0xff, 0xff, 0xff, 0xff, 1615 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK}, 1616 .enabled = tcm_is_ws_enabled, 1617 }; 1618 1619 static const struct target_opcode_descriptor tcm_opcode_verify = { 1620 .support = SCSI_SUPPORT_FULL, 1621 .opcode = VERIFY, 1622 .cdb_size = 10, 1623 .usage_bits = {VERIFY, 0x00, 0xff, 0xff, 1624 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, 0xff, 1625 0xff, SCSI_CONTROL_MASK}, 1626 }; 1627 1628 static const struct target_opcode_descriptor tcm_opcode_verify16 = { 1629 .support = SCSI_SUPPORT_FULL, 1630 .opcode = VERIFY_16, 1631 .cdb_size = 16, 1632 .usage_bits = {VERIFY_16, 0x00, 0xff, 0xff, 1633 0xff, 0xff, 0xff, 0xff, 1634 0xff, 0xff, 0xff, 0xff, 1635 0xff, 0xff, SCSI_GROUP_NUMBER_MASK, SCSI_CONTROL_MASK}, 1636 }; 1637 1638 static const struct target_opcode_descriptor tcm_opcode_start_stop = { 1639 .support = SCSI_SUPPORT_FULL, 1640 .opcode = START_STOP, 1641 .cdb_size = 6, 1642 .usage_bits = {START_STOP, 0x01, 0x00, 0x00, 1643 0x01, SCSI_CONTROL_MASK}, 1644 }; 1645 1646 static const struct target_opcode_descriptor tcm_opcode_mode_select = { 1647 .support = SCSI_SUPPORT_FULL, 1648 .opcode = MODE_SELECT, 1649 .cdb_size = 6, 1650 .usage_bits = {MODE_SELECT, 0x10, 0x00, 0x00, 1651 0xff, SCSI_CONTROL_MASK}, 1652 }; 1653 1654 static const struct target_opcode_descriptor tcm_opcode_mode_select10 = { 1655 .support = SCSI_SUPPORT_FULL, 1656 .opcode = MODE_SELECT_10, 1657 .cdb_size = 10, 1658 .usage_bits = {MODE_SELECT_10, 0x10, 0x00, 0x00, 1659 0x00, 0x00, 0x00, 0xff, 1660 0xff, SCSI_CONTROL_MASK}, 1661 }; 1662 1663 static const struct target_opcode_descriptor tcm_opcode_mode_sense = { 1664 .support = SCSI_SUPPORT_FULL, 1665 .opcode = MODE_SENSE, 1666 .cdb_size = 6, 1667 .usage_bits = {MODE_SENSE, 0x08, 0xff, 0xff, 1668 0xff, SCSI_CONTROL_MASK}, 1669 }; 1670 1671 static const struct target_opcode_descriptor tcm_opcode_mode_sense10 = { 1672 .support = SCSI_SUPPORT_FULL, 1673 .opcode = MODE_SENSE_10, 1674 .cdb_size = 10, 1675 .usage_bits = {MODE_SENSE_10, 0x18, 0xff, 0xff, 1676 0x00, 0x00, 0x00, 0xff, 1677 0xff, SCSI_CONTROL_MASK}, 1678 }; 1679 1680 static const struct target_opcode_descriptor tcm_opcode_pri_read_keys = { 1681 .support = SCSI_SUPPORT_FULL, 1682 .serv_action_valid = 1, 1683 .opcode = PERSISTENT_RESERVE_IN, 1684 .service_action = PRI_READ_KEYS, 1685 .cdb_size = 10, 1686 .usage_bits = {PERSISTENT_RESERVE_IN, PRI_READ_KEYS, 0x00, 0x00, 1687 0x00, 0x00, 0x00, 0xff, 1688 0xff, SCSI_CONTROL_MASK}, 1689 }; 1690 1691 static const struct target_opcode_descriptor tcm_opcode_pri_read_resrv = { 1692 .support = SCSI_SUPPORT_FULL, 1693 .serv_action_valid = 1, 1694 .opcode = PERSISTENT_RESERVE_IN, 1695 .service_action = PRI_READ_RESERVATION, 1696 .cdb_size = 10, 1697 .usage_bits = {PERSISTENT_RESERVE_IN, PRI_READ_RESERVATION, 0x00, 0x00, 1698 0x00, 0x00, 0x00, 0xff, 1699 0xff, SCSI_CONTROL_MASK}, 1700 }; 1701 1702 static bool tcm_is_pr_enabled(const struct target_opcode_descriptor *descr, 1703 struct se_cmd *cmd) 1704 { 1705 struct se_device *dev = cmd->se_dev; 1706 1707 if (!dev->dev_attrib.emulate_pr) 1708 return false; 1709 1710 if (!(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)) 1711 return true; 1712 1713 switch (descr->opcode) { 1714 case RESERVE_6: 1715 case RESERVE_10: 1716 case RELEASE_6: 1717 case RELEASE_10: 1718 /* 1719 * The pr_ops which are used by the backend modules don't 1720 * support these commands. 1721 */ 1722 return false; 1723 case PERSISTENT_RESERVE_OUT: 1724 switch (descr->service_action) { 1725 case PRO_REGISTER_AND_MOVE: 1726 case PRO_REPLACE_LOST_RESERVATION: 1727 /* 1728 * The backend modules don't have access to ports and 1729 * I_T nexuses so they can't handle these type of 1730 * requests. 1731 */ 1732 return false; 1733 } 1734 break; 1735 case PERSISTENT_RESERVE_IN: 1736 if (descr->service_action == PRI_READ_FULL_STATUS) 1737 return false; 1738 break; 1739 } 1740 1741 return true; 1742 } 1743 1744 static const struct target_opcode_descriptor tcm_opcode_pri_read_caps = { 1745 .support = SCSI_SUPPORT_FULL, 1746 .serv_action_valid = 1, 1747 .opcode = PERSISTENT_RESERVE_IN, 1748 .service_action = PRI_REPORT_CAPABILITIES, 1749 .cdb_size = 10, 1750 .usage_bits = {PERSISTENT_RESERVE_IN, PRI_REPORT_CAPABILITIES, 0x00, 0x00, 1751 0x00, 0x00, 0x00, 0xff, 1752 0xff, SCSI_CONTROL_MASK}, 1753 .enabled = tcm_is_pr_enabled, 1754 }; 1755 1756 static const struct target_opcode_descriptor tcm_opcode_pri_read_full_status = { 1757 .support = SCSI_SUPPORT_FULL, 1758 .serv_action_valid = 1, 1759 .opcode = PERSISTENT_RESERVE_IN, 1760 .service_action = PRI_READ_FULL_STATUS, 1761 .cdb_size = 10, 1762 .usage_bits = {PERSISTENT_RESERVE_IN, PRI_READ_FULL_STATUS, 0x00, 0x00, 1763 0x00, 0x00, 0x00, 0xff, 1764 0xff, SCSI_CONTROL_MASK}, 1765 .enabled = tcm_is_pr_enabled, 1766 }; 1767 1768 static const struct target_opcode_descriptor tcm_opcode_pro_register = { 1769 .support = SCSI_SUPPORT_FULL, 1770 .serv_action_valid = 1, 1771 .opcode = PERSISTENT_RESERVE_OUT, 1772 .service_action = PRO_REGISTER, 1773 .cdb_size = 10, 1774 .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_REGISTER, 0xff, 0x00, 1775 0x00, 0xff, 0xff, 0xff, 1776 0xff, SCSI_CONTROL_MASK}, 1777 .enabled = tcm_is_pr_enabled, 1778 }; 1779 1780 static const struct target_opcode_descriptor tcm_opcode_pro_reserve = { 1781 .support = SCSI_SUPPORT_FULL, 1782 .serv_action_valid = 1, 1783 .opcode = PERSISTENT_RESERVE_OUT, 1784 .service_action = PRO_RESERVE, 1785 .cdb_size = 10, 1786 .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_RESERVE, 0xff, 0x00, 1787 0x00, 0xff, 0xff, 0xff, 1788 0xff, SCSI_CONTROL_MASK}, 1789 .enabled = tcm_is_pr_enabled, 1790 }; 1791 1792 static const struct target_opcode_descriptor tcm_opcode_pro_release = { 1793 .support = SCSI_SUPPORT_FULL, 1794 .serv_action_valid = 1, 1795 .opcode = PERSISTENT_RESERVE_OUT, 1796 .service_action = PRO_RELEASE, 1797 .cdb_size = 10, 1798 .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_RELEASE, 0xff, 0x00, 1799 0x00, 0xff, 0xff, 0xff, 1800 0xff, SCSI_CONTROL_MASK}, 1801 .enabled = tcm_is_pr_enabled, 1802 }; 1803 1804 static const struct target_opcode_descriptor tcm_opcode_pro_clear = { 1805 .support = SCSI_SUPPORT_FULL, 1806 .serv_action_valid = 1, 1807 .opcode = PERSISTENT_RESERVE_OUT, 1808 .service_action = PRO_CLEAR, 1809 .cdb_size = 10, 1810 .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_CLEAR, 0xff, 0x00, 1811 0x00, 0xff, 0xff, 0xff, 1812 0xff, SCSI_CONTROL_MASK}, 1813 .enabled = tcm_is_pr_enabled, 1814 }; 1815 1816 static const struct target_opcode_descriptor tcm_opcode_pro_preempt = { 1817 .support = SCSI_SUPPORT_FULL, 1818 .serv_action_valid = 1, 1819 .opcode = PERSISTENT_RESERVE_OUT, 1820 .service_action = PRO_PREEMPT, 1821 .cdb_size = 10, 1822 .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_PREEMPT, 0xff, 0x00, 1823 0x00, 0xff, 0xff, 0xff, 1824 0xff, SCSI_CONTROL_MASK}, 1825 .enabled = tcm_is_pr_enabled, 1826 }; 1827 1828 static const struct target_opcode_descriptor tcm_opcode_pro_preempt_abort = { 1829 .support = SCSI_SUPPORT_FULL, 1830 .serv_action_valid = 1, 1831 .opcode = PERSISTENT_RESERVE_OUT, 1832 .service_action = PRO_PREEMPT_AND_ABORT, 1833 .cdb_size = 10, 1834 .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_PREEMPT_AND_ABORT, 0xff, 0x00, 1835 0x00, 0xff, 0xff, 0xff, 1836 0xff, SCSI_CONTROL_MASK}, 1837 .enabled = tcm_is_pr_enabled, 1838 }; 1839 1840 static const struct target_opcode_descriptor tcm_opcode_pro_reg_ign_exist = { 1841 .support = SCSI_SUPPORT_FULL, 1842 .serv_action_valid = 1, 1843 .opcode = PERSISTENT_RESERVE_OUT, 1844 .service_action = PRO_REGISTER_AND_IGNORE_EXISTING_KEY, 1845 .cdb_size = 10, 1846 .usage_bits = { 1847 PERSISTENT_RESERVE_OUT, PRO_REGISTER_AND_IGNORE_EXISTING_KEY, 1848 0xff, 0x00, 1849 0x00, 0xff, 0xff, 0xff, 1850 0xff, SCSI_CONTROL_MASK}, 1851 .enabled = tcm_is_pr_enabled, 1852 }; 1853 1854 static const struct target_opcode_descriptor tcm_opcode_pro_register_move = { 1855 .support = SCSI_SUPPORT_FULL, 1856 .serv_action_valid = 1, 1857 .opcode = PERSISTENT_RESERVE_OUT, 1858 .service_action = PRO_REGISTER_AND_MOVE, 1859 .cdb_size = 10, 1860 .usage_bits = {PERSISTENT_RESERVE_OUT, PRO_REGISTER_AND_MOVE, 0xff, 0x00, 1861 0x00, 0xff, 0xff, 0xff, 1862 0xff, SCSI_CONTROL_MASK}, 1863 .enabled = tcm_is_pr_enabled, 1864 }; 1865 1866 static const struct target_opcode_descriptor tcm_opcode_release = { 1867 .support = SCSI_SUPPORT_FULL, 1868 .opcode = RELEASE_6, 1869 .cdb_size = 6, 1870 .usage_bits = {RELEASE_6, 0x00, 0x00, 0x00, 1871 0x00, SCSI_CONTROL_MASK}, 1872 .enabled = tcm_is_pr_enabled, 1873 }; 1874 1875 static const struct target_opcode_descriptor tcm_opcode_release10 = { 1876 .support = SCSI_SUPPORT_FULL, 1877 .opcode = RELEASE_10, 1878 .cdb_size = 10, 1879 .usage_bits = {RELEASE_10, 0x00, 0x00, 0x00, 1880 0x00, 0x00, 0x00, 0xff, 1881 0xff, SCSI_CONTROL_MASK}, 1882 .enabled = tcm_is_pr_enabled, 1883 }; 1884 1885 static const struct target_opcode_descriptor tcm_opcode_reserve = { 1886 .support = SCSI_SUPPORT_FULL, 1887 .opcode = RESERVE_6, 1888 .cdb_size = 6, 1889 .usage_bits = {RESERVE_6, 0x00, 0x00, 0x00, 1890 0x00, SCSI_CONTROL_MASK}, 1891 .enabled = tcm_is_pr_enabled, 1892 }; 1893 1894 static const struct target_opcode_descriptor tcm_opcode_reserve10 = { 1895 .support = SCSI_SUPPORT_FULL, 1896 .opcode = RESERVE_10, 1897 .cdb_size = 10, 1898 .usage_bits = {RESERVE_10, 0x00, 0x00, 0x00, 1899 0x00, 0x00, 0x00, 0xff, 1900 0xff, SCSI_CONTROL_MASK}, 1901 .enabled = tcm_is_pr_enabled, 1902 }; 1903 1904 static const struct target_opcode_descriptor tcm_opcode_request_sense = { 1905 .support = SCSI_SUPPORT_FULL, 1906 .opcode = REQUEST_SENSE, 1907 .cdb_size = 6, 1908 .usage_bits = {REQUEST_SENSE, 0x00, 0x00, 0x00, 1909 0xff, SCSI_CONTROL_MASK}, 1910 }; 1911 1912 static const struct target_opcode_descriptor tcm_opcode_inquiry = { 1913 .support = SCSI_SUPPORT_FULL, 1914 .opcode = INQUIRY, 1915 .cdb_size = 6, 1916 .usage_bits = {INQUIRY, 0x01, 0xff, 0xff, 1917 0xff, SCSI_CONTROL_MASK}, 1918 }; 1919 1920 static bool tcm_is_3pc_enabled(const struct target_opcode_descriptor *descr, 1921 struct se_cmd *cmd) 1922 { 1923 struct se_device *dev = cmd->se_dev; 1924 1925 return dev->dev_attrib.emulate_3pc; 1926 } 1927 1928 static const struct target_opcode_descriptor tcm_opcode_extended_copy_lid1 = { 1929 .support = SCSI_SUPPORT_FULL, 1930 .serv_action_valid = 1, 1931 .opcode = EXTENDED_COPY, 1932 .cdb_size = 16, 1933 .usage_bits = {EXTENDED_COPY, 0x00, 0x00, 0x00, 1934 0x00, 0x00, 0x00, 0x00, 1935 0x00, 0x00, 0xff, 0xff, 1936 0xff, 0xff, 0x00, SCSI_CONTROL_MASK}, 1937 .enabled = tcm_is_3pc_enabled, 1938 }; 1939 1940 static const struct target_opcode_descriptor tcm_opcode_rcv_copy_res_op_params = { 1941 .support = SCSI_SUPPORT_FULL, 1942 .serv_action_valid = 1, 1943 .opcode = RECEIVE_COPY_RESULTS, 1944 .service_action = RCR_SA_OPERATING_PARAMETERS, 1945 .cdb_size = 16, 1946 .usage_bits = {RECEIVE_COPY_RESULTS, RCR_SA_OPERATING_PARAMETERS, 1947 0x00, 0x00, 1948 0x00, 0x00, 0x00, 0x00, 1949 0x00, 0x00, 0xff, 0xff, 1950 0xff, 0xff, 0x00, SCSI_CONTROL_MASK}, 1951 .enabled = tcm_is_3pc_enabled, 1952 }; 1953 1954 static const struct target_opcode_descriptor tcm_opcode_report_luns = { 1955 .support = SCSI_SUPPORT_FULL, 1956 .opcode = REPORT_LUNS, 1957 .cdb_size = 12, 1958 .usage_bits = {REPORT_LUNS, 0x00, 0xff, 0x00, 1959 0x00, 0x00, 0xff, 0xff, 1960 0xff, 0xff, 0x00, SCSI_CONTROL_MASK}, 1961 }; 1962 1963 static const struct target_opcode_descriptor tcm_opcode_test_unit_ready = { 1964 .support = SCSI_SUPPORT_FULL, 1965 .opcode = TEST_UNIT_READY, 1966 .cdb_size = 6, 1967 .usage_bits = {TEST_UNIT_READY, 0x00, 0x00, 0x00, 1968 0x00, SCSI_CONTROL_MASK}, 1969 }; 1970 1971 static const struct target_opcode_descriptor tcm_opcode_report_target_pgs = { 1972 .support = SCSI_SUPPORT_FULL, 1973 .serv_action_valid = 1, 1974 .opcode = MAINTENANCE_IN, 1975 .service_action = MI_REPORT_TARGET_PGS, 1976 .cdb_size = 12, 1977 .usage_bits = {MAINTENANCE_IN, 0xE0 | MI_REPORT_TARGET_PGS, 0x00, 0x00, 1978 0x00, 0x00, 0xff, 0xff, 1979 0xff, 0xff, 0x00, SCSI_CONTROL_MASK}, 1980 }; 1981 1982 static bool spc_rsoc_enabled(const struct target_opcode_descriptor *descr, 1983 struct se_cmd *cmd) 1984 { 1985 struct se_device *dev = cmd->se_dev; 1986 1987 return dev->dev_attrib.emulate_rsoc; 1988 } 1989 1990 static const struct target_opcode_descriptor tcm_opcode_report_supp_opcodes = { 1991 .support = SCSI_SUPPORT_FULL, 1992 .serv_action_valid = 1, 1993 .opcode = MAINTENANCE_IN, 1994 .service_action = MI_REPORT_SUPPORTED_OPERATION_CODES, 1995 .cdb_size = 12, 1996 .usage_bits = {MAINTENANCE_IN, MI_REPORT_SUPPORTED_OPERATION_CODES, 1997 0x87, 0xff, 1998 0xff, 0xff, 0xff, 0xff, 1999 0xff, 0xff, 0x00, SCSI_CONTROL_MASK}, 2000 .enabled = spc_rsoc_enabled, 2001 }; 2002 2003 static bool tcm_is_set_tpg_enabled(const struct target_opcode_descriptor *descr, 2004 struct se_cmd *cmd) 2005 { 2006 struct t10_alua_tg_pt_gp *l_tg_pt_gp; 2007 struct se_lun *l_lun = cmd->se_lun; 2008 2009 rcu_read_lock(); 2010 l_tg_pt_gp = rcu_dereference(l_lun->lun_tg_pt_gp); 2011 if (!l_tg_pt_gp) { 2012 rcu_read_unlock(); 2013 return false; 2014 } 2015 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) { 2016 rcu_read_unlock(); 2017 return false; 2018 } 2019 rcu_read_unlock(); 2020 2021 return true; 2022 } 2023 2024 static const struct target_opcode_descriptor tcm_opcode_set_tpg = { 2025 .support = SCSI_SUPPORT_FULL, 2026 .serv_action_valid = 1, 2027 .opcode = MAINTENANCE_OUT, 2028 .service_action = MO_SET_TARGET_PGS, 2029 .cdb_size = 12, 2030 .usage_bits = {MAINTENANCE_OUT, MO_SET_TARGET_PGS, 0x00, 0x00, 2031 0x00, 0x00, 0xff, 0xff, 2032 0xff, 0xff, 0x00, SCSI_CONTROL_MASK}, 2033 .enabled = tcm_is_set_tpg_enabled, 2034 }; 2035 2036 static const struct target_opcode_descriptor *tcm_supported_opcodes[] = { 2037 &tcm_opcode_read6, 2038 &tcm_opcode_read10, 2039 &tcm_opcode_read12, 2040 &tcm_opcode_read16, 2041 &tcm_opcode_write6, 2042 &tcm_opcode_write10, 2043 &tcm_opcode_write_verify10, 2044 &tcm_opcode_write12, 2045 &tcm_opcode_write16, 2046 &tcm_opcode_write_verify16, 2047 &tcm_opcode_write_same32, 2048 &tcm_opcode_write_atomic16, 2049 &tcm_opcode_compare_write, 2050 &tcm_opcode_read_capacity, 2051 &tcm_opcode_read_capacity16, 2052 &tcm_opcode_read_report_refferals, 2053 &tcm_opcode_sync_cache, 2054 &tcm_opcode_sync_cache16, 2055 &tcm_opcode_unmap, 2056 &tcm_opcode_write_same, 2057 &tcm_opcode_write_same16, 2058 &tcm_opcode_verify, 2059 &tcm_opcode_verify16, 2060 &tcm_opcode_start_stop, 2061 &tcm_opcode_mode_select, 2062 &tcm_opcode_mode_select10, 2063 &tcm_opcode_mode_sense, 2064 &tcm_opcode_mode_sense10, 2065 &tcm_opcode_pri_read_keys, 2066 &tcm_opcode_pri_read_resrv, 2067 &tcm_opcode_pri_read_caps, 2068 &tcm_opcode_pri_read_full_status, 2069 &tcm_opcode_pro_register, 2070 &tcm_opcode_pro_reserve, 2071 &tcm_opcode_pro_release, 2072 &tcm_opcode_pro_clear, 2073 &tcm_opcode_pro_preempt, 2074 &tcm_opcode_pro_preempt_abort, 2075 &tcm_opcode_pro_reg_ign_exist, 2076 &tcm_opcode_pro_register_move, 2077 &tcm_opcode_release, 2078 &tcm_opcode_release10, 2079 &tcm_opcode_reserve, 2080 &tcm_opcode_reserve10, 2081 &tcm_opcode_request_sense, 2082 &tcm_opcode_inquiry, 2083 &tcm_opcode_extended_copy_lid1, 2084 &tcm_opcode_rcv_copy_res_op_params, 2085 &tcm_opcode_report_luns, 2086 &tcm_opcode_test_unit_ready, 2087 &tcm_opcode_report_target_pgs, 2088 &tcm_opcode_report_supp_opcodes, 2089 &tcm_opcode_set_tpg, 2090 }; 2091 2092 static int 2093 spc_rsoc_encode_command_timeouts_descriptor(unsigned char *buf, u8 ctdp, 2094 const struct target_opcode_descriptor *descr) 2095 { 2096 if (!ctdp) 2097 return 0; 2098 2099 put_unaligned_be16(0xa, buf); 2100 buf[3] = descr->specific_timeout; 2101 put_unaligned_be32(descr->nominal_timeout, &buf[4]); 2102 put_unaligned_be32(descr->recommended_timeout, &buf[8]); 2103 2104 return 12; 2105 } 2106 2107 static int 2108 spc_rsoc_encode_command_descriptor(unsigned char *buf, u8 ctdp, 2109 const struct target_opcode_descriptor *descr) 2110 { 2111 int td_size = 0; 2112 2113 buf[0] = descr->opcode; 2114 2115 put_unaligned_be16(descr->service_action, &buf[2]); 2116 2117 buf[5] = (ctdp << 1) | descr->serv_action_valid; 2118 put_unaligned_be16(descr->cdb_size, &buf[6]); 2119 2120 td_size = spc_rsoc_encode_command_timeouts_descriptor(&buf[8], ctdp, 2121 descr); 2122 2123 return 8 + td_size; 2124 } 2125 2126 static int 2127 spc_rsoc_encode_one_command_descriptor(unsigned char *buf, u8 ctdp, 2128 const struct target_opcode_descriptor *descr, 2129 struct se_device *dev) 2130 { 2131 int td_size = 0; 2132 2133 if (!descr) { 2134 buf[1] = (ctdp << 7) | SCSI_SUPPORT_NOT_SUPPORTED; 2135 return 2; 2136 } 2137 2138 buf[1] = (ctdp << 7) | SCSI_SUPPORT_FULL; 2139 put_unaligned_be16(descr->cdb_size, &buf[2]); 2140 memcpy(&buf[4], descr->usage_bits, descr->cdb_size); 2141 if (descr->update_usage_bits) 2142 descr->update_usage_bits(&buf[4], dev); 2143 2144 td_size = spc_rsoc_encode_command_timeouts_descriptor( 2145 &buf[4 + descr->cdb_size], ctdp, descr); 2146 2147 return 4 + descr->cdb_size + td_size; 2148 } 2149 2150 static sense_reason_t 2151 spc_rsoc_get_descr(struct se_cmd *cmd, const struct target_opcode_descriptor **opcode) 2152 { 2153 const struct target_opcode_descriptor *descr; 2154 struct se_session *sess = cmd->se_sess; 2155 unsigned char *cdb = cmd->t_task_cdb; 2156 u8 opts = cdb[2] & 0x3; 2157 u8 requested_opcode; 2158 u16 requested_sa; 2159 int i; 2160 2161 requested_opcode = cdb[3]; 2162 requested_sa = ((u16)cdb[4]) << 8 | cdb[5]; 2163 *opcode = NULL; 2164 2165 if (opts > 3) { 2166 pr_debug("TARGET_CORE[%s]: Invalid REPORT SUPPORTED OPERATION CODES" 2167 " with unsupported REPORTING OPTIONS %#x for 0x%08llx from %s\n", 2168 cmd->se_tfo->fabric_name, opts, 2169 cmd->se_lun->unpacked_lun, 2170 sess->se_node_acl->initiatorname); 2171 return TCM_INVALID_CDB_FIELD; 2172 } 2173 2174 for (i = 0; i < ARRAY_SIZE(tcm_supported_opcodes); i++) { 2175 descr = tcm_supported_opcodes[i]; 2176 if (descr->opcode != requested_opcode) 2177 continue; 2178 2179 switch (opts) { 2180 case 0x1: 2181 /* 2182 * If the REQUESTED OPERATION CODE field specifies an 2183 * operation code for which the device server implements 2184 * service actions, then the device server shall 2185 * terminate the command with CHECK CONDITION status, 2186 * with the sense key set to ILLEGAL REQUEST, and the 2187 * additional sense code set to INVALID FIELD IN CDB 2188 */ 2189 if (descr->serv_action_valid) 2190 return TCM_INVALID_CDB_FIELD; 2191 2192 if (!descr->enabled || descr->enabled(descr, cmd)) { 2193 *opcode = descr; 2194 return TCM_NO_SENSE; 2195 } 2196 break; 2197 case 0x2: 2198 /* 2199 * If the REQUESTED OPERATION CODE field specifies an 2200 * operation code for which the device server does not 2201 * implement service actions, then the device server 2202 * shall terminate the command with CHECK CONDITION 2203 * status, with the sense key set to ILLEGAL REQUEST, 2204 * and the additional sense code set to INVALID FIELD IN CDB. 2205 */ 2206 if (descr->serv_action_valid && 2207 descr->service_action == requested_sa) { 2208 if (!descr->enabled || descr->enabled(descr, 2209 cmd)) { 2210 *opcode = descr; 2211 return TCM_NO_SENSE; 2212 } 2213 } else if (!descr->serv_action_valid) 2214 return TCM_INVALID_CDB_FIELD; 2215 break; 2216 case 0x3: 2217 /* 2218 * The command support data for the operation code and 2219 * service action a specified in the REQUESTED OPERATION 2220 * CODE field and REQUESTED SERVICE ACTION field shall 2221 * be returned in the one_command parameter data format. 2222 */ 2223 if (descr->service_action == requested_sa) 2224 if (!descr->enabled || descr->enabled(descr, 2225 cmd)) { 2226 *opcode = descr; 2227 return TCM_NO_SENSE; 2228 } 2229 break; 2230 } 2231 } 2232 2233 return TCM_NO_SENSE; 2234 } 2235 2236 static sense_reason_t 2237 spc_emulate_report_supp_op_codes(struct se_cmd *cmd) 2238 { 2239 int descr_num = ARRAY_SIZE(tcm_supported_opcodes); 2240 const struct target_opcode_descriptor *descr = NULL; 2241 unsigned char *cdb = cmd->t_task_cdb; 2242 u8 rctd = (cdb[2] >> 7) & 0x1; 2243 unsigned char *buf = NULL; 2244 int response_length = 0; 2245 u8 opts = cdb[2] & 0x3; 2246 unsigned char *rbuf; 2247 sense_reason_t ret = 0; 2248 int i; 2249 2250 if (!cmd->se_dev->dev_attrib.emulate_rsoc) 2251 return TCM_UNSUPPORTED_SCSI_OPCODE; 2252 2253 rbuf = transport_kmap_data_sg(cmd); 2254 if (cmd->data_length && !rbuf) { 2255 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2256 goto out; 2257 } 2258 2259 if (opts == 0) 2260 response_length = 4 + (8 + rctd * 12) * descr_num; 2261 else { 2262 ret = spc_rsoc_get_descr(cmd, &descr); 2263 if (ret) 2264 goto out; 2265 2266 if (descr) 2267 response_length = 4 + descr->cdb_size + rctd * 12; 2268 else 2269 response_length = 2; 2270 } 2271 2272 buf = kzalloc(response_length, GFP_KERNEL); 2273 if (!buf) { 2274 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2275 goto out; 2276 } 2277 response_length = 0; 2278 2279 if (opts == 0) { 2280 response_length += 4; 2281 2282 for (i = 0; i < ARRAY_SIZE(tcm_supported_opcodes); i++) { 2283 descr = tcm_supported_opcodes[i]; 2284 if (descr->enabled && !descr->enabled(descr, cmd)) 2285 continue; 2286 2287 response_length += spc_rsoc_encode_command_descriptor( 2288 &buf[response_length], rctd, descr); 2289 } 2290 put_unaligned_be32(response_length - 4, buf); 2291 } else { 2292 response_length = spc_rsoc_encode_one_command_descriptor( 2293 &buf[response_length], rctd, descr, 2294 cmd->se_dev); 2295 } 2296 2297 memcpy(rbuf, buf, min_t(u32, response_length, cmd->data_length)); 2298 out: 2299 kfree(buf); 2300 transport_kunmap_data_sg(cmd); 2301 2302 if (!ret) 2303 target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, response_length); 2304 return ret; 2305 } 2306 2307 sense_reason_t 2308 spc_parse_cdb(struct se_cmd *cmd, unsigned int *size) 2309 { 2310 struct se_device *dev = cmd->se_dev; 2311 unsigned char *cdb = cmd->t_task_cdb; 2312 2313 switch (cdb[0]) { 2314 case RESERVE_6: 2315 case RESERVE_10: 2316 case RELEASE_6: 2317 case RELEASE_10: 2318 if (!dev->dev_attrib.emulate_pr) 2319 return TCM_UNSUPPORTED_SCSI_OPCODE; 2320 2321 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR) 2322 return TCM_UNSUPPORTED_SCSI_OPCODE; 2323 break; 2324 case PERSISTENT_RESERVE_IN: 2325 case PERSISTENT_RESERVE_OUT: 2326 if (!dev->dev_attrib.emulate_pr) 2327 return TCM_UNSUPPORTED_SCSI_OPCODE; 2328 break; 2329 } 2330 2331 switch (cdb[0]) { 2332 case MODE_SELECT: 2333 *size = cdb[4]; 2334 cmd->execute_cmd = spc_emulate_modeselect; 2335 break; 2336 case MODE_SELECT_10: 2337 *size = get_unaligned_be16(&cdb[7]); 2338 cmd->execute_cmd = spc_emulate_modeselect; 2339 break; 2340 case MODE_SENSE: 2341 *size = cdb[4]; 2342 cmd->execute_cmd = spc_emulate_modesense; 2343 break; 2344 case MODE_SENSE_10: 2345 *size = get_unaligned_be16(&cdb[7]); 2346 cmd->execute_cmd = spc_emulate_modesense; 2347 break; 2348 case LOG_SELECT: 2349 case LOG_SENSE: 2350 *size = get_unaligned_be16(&cdb[7]); 2351 break; 2352 case PERSISTENT_RESERVE_IN: 2353 *size = get_unaligned_be16(&cdb[7]); 2354 cmd->execute_cmd = target_scsi3_emulate_pr_in; 2355 break; 2356 case PERSISTENT_RESERVE_OUT: 2357 *size = get_unaligned_be32(&cdb[5]); 2358 cmd->execute_cmd = target_scsi3_emulate_pr_out; 2359 break; 2360 case RELEASE_6: 2361 case RELEASE_10: 2362 if (cdb[0] == RELEASE_10) 2363 *size = get_unaligned_be16(&cdb[7]); 2364 else 2365 *size = cmd->data_length; 2366 2367 cmd->execute_cmd = target_scsi2_reservation_release; 2368 break; 2369 case RESERVE_6: 2370 case RESERVE_10: 2371 /* 2372 * The SPC-2 RESERVE does not contain a size in the SCSI CDB. 2373 * Assume the passthrough or $FABRIC_MOD will tell us about it. 2374 */ 2375 if (cdb[0] == RESERVE_10) 2376 *size = get_unaligned_be16(&cdb[7]); 2377 else 2378 *size = cmd->data_length; 2379 2380 cmd->execute_cmd = target_scsi2_reservation_reserve; 2381 break; 2382 case REQUEST_SENSE: 2383 *size = cdb[4]; 2384 cmd->execute_cmd = spc_emulate_request_sense; 2385 break; 2386 case INQUIRY: 2387 *size = get_unaligned_be16(&cdb[3]); 2388 2389 /* 2390 * Do implicit HEAD_OF_QUEUE processing for INQUIRY. 2391 * See spc4r17 section 5.3 2392 */ 2393 cmd->sam_task_attr = TCM_HEAD_TAG; 2394 cmd->execute_cmd = spc_emulate_inquiry; 2395 break; 2396 case SECURITY_PROTOCOL_IN: 2397 case SECURITY_PROTOCOL_OUT: 2398 *size = get_unaligned_be32(&cdb[6]); 2399 break; 2400 case EXTENDED_COPY: 2401 *size = get_unaligned_be32(&cdb[10]); 2402 cmd->execute_cmd = target_do_xcopy; 2403 break; 2404 case RECEIVE_COPY_RESULTS: 2405 *size = get_unaligned_be32(&cdb[10]); 2406 cmd->execute_cmd = target_do_receive_copy_results; 2407 break; 2408 case READ_ATTRIBUTE: 2409 case WRITE_ATTRIBUTE: 2410 *size = get_unaligned_be32(&cdb[10]); 2411 break; 2412 case RECEIVE_DIAGNOSTIC: 2413 case SEND_DIAGNOSTIC: 2414 *size = get_unaligned_be16(&cdb[3]); 2415 break; 2416 case WRITE_BUFFER: 2417 *size = get_unaligned_be24(&cdb[6]); 2418 break; 2419 case REPORT_LUNS: 2420 cmd->execute_cmd = spc_emulate_report_luns; 2421 *size = get_unaligned_be32(&cdb[6]); 2422 /* 2423 * Do implicit HEAD_OF_QUEUE processing for REPORT_LUNS 2424 * See spc4r17 section 5.3 2425 */ 2426 cmd->sam_task_attr = TCM_HEAD_TAG; 2427 break; 2428 case TEST_UNIT_READY: 2429 cmd->execute_cmd = spc_emulate_testunitready; 2430 *size = 0; 2431 break; 2432 case MAINTENANCE_IN: 2433 if (dev->transport->get_device_type(dev) != TYPE_ROM) { 2434 /* 2435 * MAINTENANCE_IN from SCC-2 2436 * Check for emulated MI_REPORT_TARGET_PGS 2437 */ 2438 if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS) { 2439 cmd->execute_cmd = 2440 target_emulate_report_target_port_groups; 2441 } 2442 if ((cdb[1] & 0x1f) == 2443 MI_REPORT_SUPPORTED_OPERATION_CODES) 2444 cmd->execute_cmd = 2445 spc_emulate_report_supp_op_codes; 2446 *size = get_unaligned_be32(&cdb[6]); 2447 } else { 2448 /* 2449 * GPCMD_SEND_KEY from multi media commands 2450 */ 2451 *size = get_unaligned_be16(&cdb[8]); 2452 } 2453 break; 2454 case MAINTENANCE_OUT: 2455 if (dev->transport->get_device_type(dev) != TYPE_ROM) { 2456 /* 2457 * MAINTENANCE_OUT from SCC-2 2458 * Check for emulated MO_SET_TARGET_PGS. 2459 */ 2460 if (cdb[1] == MO_SET_TARGET_PGS) { 2461 cmd->execute_cmd = 2462 target_emulate_set_target_port_groups; 2463 } 2464 *size = get_unaligned_be32(&cdb[6]); 2465 } else { 2466 /* 2467 * GPCMD_SEND_KEY from multi media commands 2468 */ 2469 *size = get_unaligned_be16(&cdb[8]); 2470 } 2471 break; 2472 default: 2473 return TCM_UNSUPPORTED_SCSI_OPCODE; 2474 } 2475 2476 return 0; 2477 } 2478 EXPORT_SYMBOL(spc_parse_cdb); 2479