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