1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SCSI Primary Commands (SPC) parsing and emulation.
5 * (c) Copyright 2002-2013 Datera, Inc.
7 * Nicholas A. Bellinger <nab@kernel.org>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/unaligned.h>
14 #include <scsi/scsi_proto.h>
15 #include <scsi/scsi_common.h>
16 #include <scsi/scsi_tcq.h>
18 #include <target/target_core_base.h>
19 #include <target/target_core_backend.h>
20 #include <target/target_core_fabric.h>
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"
28 static void spc_fill_alua_data(struct se_lun
*lun
, unsigned char *buf
)
30 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
33 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
38 * Set TPGS field for explicit and/or implicit ALUA access type
41 * See spc4r17 section 6.4.2 Table 135
44 tg_pt_gp
= rcu_dereference(lun
->lun_tg_pt_gp
);
46 buf
[5] |= tg_pt_gp
->tg_pt_gp_alua_access_type
;
51 spc_find_scsi_transport_vd(int 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
;
65 pr_warn("Cannot find VERSION DESCRIPTOR value for unknown SCSI"
66 " transport PROTOCOL IDENTIFIER %#x\n", proto_id
);
72 spc_emulate_inquiry_std(struct se_cmd
*cmd
, unsigned char *buf
)
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
;
79 /* Set RMB (removable media) for tape devices */
80 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
)
83 buf
[2] = 0x06; /* SPC-4 */
86 * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
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
98 * Enable SCCS and TPGS fields for Emulated ALUA
100 spc_fill_alua_data(lun
, buf
);
103 * Set Third-Party Copy (3PC) bit to indicate support for EXTENDED_COPY
105 if (dev
->dev_attrib
.emulate_3pc
)
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.
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
)
119 * Set MULTIP bit to indicate presence of multiple SCSI target ports
121 if (dev
->export_count
> 1)
124 buf
[7] = 0x2; /* CmdQue=1 */
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).
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
));
141 * Set the VERSION DESCRIPTOR fields
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]);
149 buf
[4] = 91; /* Set additional length to 91 */
153 EXPORT_SYMBOL(spc_emulate_inquiry_std
);
155 /* unit serial number */
156 static sense_reason_t
157 spc_emulate_evpd_80(struct se_cmd
*cmd
, unsigned char *buf
)
159 struct se_device
*dev
= cmd
->se_dev
;
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 */
171 * Generate NAA IEEE Registered Extended designator
173 void spc_gen_naa_6h_vendor_specific(struct se_device
*dev
,
176 unsigned char *p
= &dev
->t10_wwn
.unit_serial
[0];
177 u32 company_id
= dev
->t10_wwn
.company_id
;
182 * Start NAA IEEE Registered Extended Identifier/Designator
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;
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.
200 for (cnt
= off
+ 13; *p
&& off
< cnt
; p
++) {
201 int val
= hex_to_bin(*p
);
217 * Device identification VPD, for a complete list of
218 * DESIGNATOR TYPEs see spc4r17 Table 459.
221 spc_emulate_evpd_83(struct se_cmd
*cmd
, unsigned char *buf
)
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];
235 * NAA IEEE Registered Extended Assigned designator format, see
236 * spc4r17 section 7.7.3.6.5
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.
242 if (!(dev
->dev_flags
& DF_EMULATED_VPD_UNIT_SERIAL
))
243 goto check_t10_vend_desc
;
245 /* CODE SET == Binary */
248 /* Set ASSOCIATION == addressed logical unit: 0)b */
251 /* Identifier/Designator type == NAA identifier */
255 /* Identifier/Designator length */
258 /* NAA IEEE Registered Extended designator */
259 spc_gen_naa_6h_vendor_specific(dev
, &buf
[off
]);
266 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
268 id_len
= 8; /* For Vendor field */
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 */
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 */
282 /* Identifier Length */
284 /* Header size for Designation descriptor */
289 struct t10_alua_lu_gp
*lu_gp
;
290 u32 padding
, scsi_name_len
, scsi_target_len
;
297 * Relative target port identifer, see spc4r17
300 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
301 * section 7.5.1 Table 362
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 */
308 /* DESIGNATOR TYPE == Relative target port identifer */
310 off
++; /* Skip over Reserved */
311 buf
[off
++] = 4; /* DESIGNATOR LENGTH */
312 /* Skip over Obsolete field in RTPI payload
315 put_unaligned_be16(lun
->lun_tpg
->tpg_rtpi
, &buf
[off
]);
317 len
+= 8; /* Header size + Designation descriptor */
319 * Target port group identifier, see spc4r17
322 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
323 * section 7.5.1 Table 362
326 tg_pt_gp
= rcu_dereference(lun
->lun_tg_pt_gp
);
331 tg_pt_gp_id
= tg_pt_gp
->tg_pt_gp_id
;
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 */
339 /* DESIGNATOR TYPE == Target port group identifier */
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
]);
346 len
+= 8; /* Header size + Designation descriptor */
348 * Logical Unit Group identifier, see spc4r17
352 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
354 goto check_scsi_name
;
356 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
357 lu_gp
= lu_gp_mem
->lu_gp
;
359 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
360 goto check_scsi_name
;
362 lu_gp_id
= lu_gp
->lu_gp_id
;
363 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
365 buf
[off
++] |= 0x1; /* CODE SET == Binary */
366 /* DESIGNATOR TYPE == Logical Unit Group identifier */
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
]);
373 len
+= 8; /* Header size + Designation descriptor */
375 * SCSI name string designator, see spc4r17
378 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
379 * section 7.5.1 Table 362
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 */
387 /* DESIGNATOR TYPE == SCSI name string */
389 off
+= 2; /* Skip over Reserved and length */
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
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 */;
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
408 padding
= ((-scsi_name_len
) & 3);
410 scsi_name_len
+= padding
;
411 if (scsi_name_len
> 256)
414 buf
[off
-1] = scsi_name_len
;
415 off
+= scsi_name_len
;
416 /* Header size + Designation descriptor */
417 len
+= (scsi_name_len
+ 4);
420 * Target device designator
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 */
427 /* DESIGNATOR TYPE == SCSI name string */
429 off
+= 2; /* Skip over Reserved and length */
431 * SCSI name string identifer containing, $FABRIC_MOD
432 * dependent information. For LIO-Target and iSCSI
433 * Target Port, this means "<iSCSI name>" in
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 */;
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
447 padding
= ((-scsi_target_len
) & 3);
449 scsi_target_len
+= padding
;
450 if (scsi_target_len
> 256)
451 scsi_target_len
= 256;
453 buf
[off
-1] = scsi_target_len
;
454 off
+= scsi_target_len
;
456 /* Header size + Designation descriptor */
457 len
+= (scsi_target_len
+ 4);
459 put_unaligned_be16(len
, &buf
[2]); /* Page Length for VPD 0x83 */
462 EXPORT_SYMBOL(spc_emulate_evpd_83
);
464 /* Extended INQUIRY Data VPD Page */
465 static sense_reason_t
466 spc_emulate_evpd_86(struct se_cmd
*cmd
, unsigned char *buf
)
468 struct se_device
*dev
= cmd
->se_dev
;
469 struct se_session
*sess
= cmd
->se_sess
;
473 * Set GRD_CHK + REF_CHK for TYPE1 protection, or GRD_CHK
474 * only for TYPE3 protection.
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
)
480 else if (dev
->dev_attrib
.pi_prot_type
== TARGET_DIF_TYPE3_PROT
||
481 cmd
->se_sess
->sess_prot_type
== TARGET_DIF_TYPE3_PROT
)
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);
492 /* Set HEADSUP, ORDSUP, SIMPSUP */
495 /* If WriteCache emulation is enabled, set V_SUP */
496 if (target_check_wce(dev
))
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
))
502 spin_unlock(&cmd
->se_dev
->t10_alua
.lba_map_lock
);
506 /* Block Limits VPD page */
507 static sense_reason_t
508 spc_emulate_evpd_b0(struct se_cmd
*cmd
, unsigned char *buf
)
510 struct se_device
*dev
= cmd
->se_dev
;
512 int have_tp
= 0, opt
, min
;
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.
520 if (dev
->dev_attrib
.emulate_tpu
|| dev
->dev_attrib
.emulate_tpws
)
523 buf
[0] = dev
->transport
->get_device_type(dev
);
524 buf
[3] = have_tp
? 0x3c : 0x10;
529 * Set MAXIMUM COMPARE AND WRITE LENGTH
531 if (dev
->dev_attrib
.emulate_caw
)
535 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
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]);
540 put_unaligned_be16(1, &buf
[6]);
543 * Set MAXIMUM TRANSFER LENGTH
545 * XXX: Currently assumes single PAGE_SIZE per scatterlist for fabrics
546 * enforcing maximum HW scatter-gather-list entry limit
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
;
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]);
558 * Set OPTIMAL TRANSFER LENGTH
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]);
563 put_unaligned_be32(dev
->dev_attrib
.optimal_sectors
, &buf
[12]);
566 * Exit now if we don't support TP.
572 * Set MAXIMUM UNMAP LBA COUNT
574 put_unaligned_be32(dev
->dev_attrib
.max_unmap_lba_count
, &buf
[20]);
577 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
579 put_unaligned_be32(dev
->dev_attrib
.max_unmap_block_desc_count
,
583 * Set OPTIMAL UNMAP GRANULARITY
585 put_unaligned_be32(dev
->dev_attrib
.unmap_granularity
, &buf
[28]);
588 * UNMAP GRANULARITY ALIGNMENT
590 put_unaligned_be32(dev
->dev_attrib
.unmap_granularity_alignment
,
592 if (dev
->dev_attrib
.unmap_granularity_alignment
!= 0)
593 buf
[32] |= 0x80; /* Set the UGAVALID bit */
596 * MAXIMUM WRITE SAME LENGTH
599 put_unaligned_be64(dev
->dev_attrib
.max_write_same_len
, &buf
[36]);
604 /* Block Device Characteristics VPD page */
605 static sense_reason_t
606 spc_emulate_evpd_b1(struct se_cmd
*cmd
, unsigned char *buf
)
608 struct se_device
*dev
= cmd
->se_dev
;
610 buf
[0] = dev
->transport
->get_device_type(dev
);
612 buf
[5] = dev
->dev_attrib
.is_nonrot
? 1 : 0;
617 /* Thin Provisioning VPD */
618 static sense_reason_t
619 spc_emulate_evpd_b2(struct se_cmd
*cmd
, unsigned char *buf
)
621 struct se_device
*dev
= cmd
->se_dev
;
624 * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
626 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
627 * zero, then the page length shall be set to 0004h. If the DP bit
628 * is set to one, then the page length shall be set to the value
629 * defined in table 162.
631 buf
[0] = dev
->transport
->get_device_type(dev
);
634 * Set Hardcoded length mentioned above for DP=0
636 put_unaligned_be16(0x0004, &buf
[2]);
639 * The THRESHOLD EXPONENT field indicates the threshold set size in
640 * LBAs as a power of 2 (i.e., the threshold set size is equal to
641 * 2(threshold exponent)).
643 * Note that this is currently set to 0x00 as mkp says it will be
644 * changing again. We can enable this once it has settled in T10
645 * and is actually used by Linux/SCSI ML code.
650 * A TPU bit set to one indicates that the device server supports
651 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
652 * that the device server does not support the UNMAP command.
654 if (dev
->dev_attrib
.emulate_tpu
!= 0)
658 * A TPWS bit set to one indicates that the device server supports
659 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
660 * A TPWS bit set to zero indicates that the device server does not
661 * support the use of the WRITE SAME (16) command to unmap LBAs.
663 if (dev
->dev_attrib
.emulate_tpws
!= 0)
664 buf
[5] |= 0x40 | 0x20;
667 * The unmap_zeroes_data set means that the underlying device supports
668 * REQ_OP_DISCARD and has the discard_zeroes_data bit set. This
669 * satisfies the SBC requirements for LBPRZ, meaning that a subsequent
670 * read will return zeroes after an UNMAP or WRITE SAME (16) to an LBA
673 if (((dev
->dev_attrib
.emulate_tpu
!= 0) ||
674 (dev
->dev_attrib
.emulate_tpws
!= 0)) &&
675 (dev
->dev_attrib
.unmap_zeroes_data
!= 0))
681 /* Referrals VPD page */
682 static sense_reason_t
683 spc_emulate_evpd_b3(struct se_cmd
*cmd
, unsigned char *buf
)
685 struct se_device
*dev
= cmd
->se_dev
;
687 buf
[0] = dev
->transport
->get_device_type(dev
);
689 put_unaligned_be32(dev
->t10_alua
.lba_map_segment_size
, &buf
[8]);
690 put_unaligned_be32(dev
->t10_alua
.lba_map_segment_multiplier
, &buf
[12]);
695 static sense_reason_t
696 spc_emulate_evpd_00(struct se_cmd
*cmd
, unsigned char *buf
);
700 sense_reason_t (*emulate
)(struct se_cmd
*, unsigned char *);
701 } evpd_handlers
[] = {
702 { .page
= 0x00, .emulate
= spc_emulate_evpd_00
},
703 { .page
= 0x80, .emulate
= spc_emulate_evpd_80
},
704 { .page
= 0x83, .emulate
= spc_emulate_evpd_83
},
705 { .page
= 0x86, .emulate
= spc_emulate_evpd_86
},
706 { .page
= 0xb0, .emulate
= spc_emulate_evpd_b0
},
707 { .page
= 0xb1, .emulate
= spc_emulate_evpd_b1
},
708 { .page
= 0xb2, .emulate
= spc_emulate_evpd_b2
},
709 { .page
= 0xb3, .emulate
= spc_emulate_evpd_b3
},
712 /* supported vital product data pages */
713 static sense_reason_t
714 spc_emulate_evpd_00(struct se_cmd
*cmd
, unsigned char *buf
)
719 * Only report the INQUIRY EVPD=1 pages after a valid NAA
720 * Registered Extended LUN WWN has been set via ConfigFS
721 * during device creation/restart.
723 if (cmd
->se_dev
->dev_flags
& DF_EMULATED_VPD_UNIT_SERIAL
) {
724 buf
[3] = ARRAY_SIZE(evpd_handlers
);
725 for (p
= 0; p
< ARRAY_SIZE(evpd_handlers
); ++p
)
726 buf
[p
+ 4] = evpd_handlers
[p
].page
;
732 static sense_reason_t
733 spc_emulate_inquiry(struct se_cmd
*cmd
)
735 struct se_device
*dev
= cmd
->se_dev
;
737 unsigned char *cdb
= cmd
->t_task_cdb
;
743 buf
= kzalloc(SE_INQUIRY_BUF
, GFP_KERNEL
);
745 pr_err("Unable to allocate response buffer for INQUIRY\n");
746 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
749 buf
[0] = dev
->transport
->get_device_type(dev
);
751 if (!(cdb
[1] & 0x1)) {
753 pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
755 ret
= TCM_INVALID_CDB_FIELD
;
759 ret
= spc_emulate_inquiry_std(cmd
, buf
);
764 for (p
= 0; p
< ARRAY_SIZE(evpd_handlers
); ++p
) {
765 if (cdb
[2] == evpd_handlers
[p
].page
) {
767 ret
= evpd_handlers
[p
].emulate(cmd
, buf
);
768 len
= get_unaligned_be16(&buf
[2]) + 4;
773 pr_debug("Unknown VPD Code: 0x%02x\n", cdb
[2]);
774 ret
= TCM_INVALID_CDB_FIELD
;
777 rbuf
= transport_kmap_data_sg(cmd
);
779 memcpy(rbuf
, buf
, min_t(u32
, SE_INQUIRY_BUF
, cmd
->data_length
));
780 transport_kunmap_data_sg(cmd
);
785 target_complete_cmd_with_length(cmd
, SAM_STAT_GOOD
, len
);
789 static int spc_modesense_rwrecovery(struct se_cmd
*cmd
, u8 pc
, u8
*p
)
794 /* No changeable values for now */
802 static int spc_modesense_control(struct se_cmd
*cmd
, u8 pc
, u8
*p
)
804 struct se_device
*dev
= cmd
->se_dev
;
805 struct se_session
*sess
= cmd
->se_sess
;
810 /* No changeable values for now */
814 /* GLTSD: No implicit save of log parameters */
816 if (target_sense_desc_format(dev
))
817 /* D_SENSE: Descriptor format sense data for 64bit sectors */
821 * From spc4r23, 7.4.7 Control mode page
823 * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
824 * restrictions on the algorithm used for reordering commands
825 * having the SIMPLE task attribute (see SAM-4).
827 * Table 368 -- QUEUE ALGORITHM MODIFIER field
829 * 0h Restricted reordering
830 * 1h Unrestricted reordering allowed
832 * 8h to Fh Vendor specific
834 * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
835 * the device server shall order the processing sequence of commands
836 * having the SIMPLE task attribute such that data integrity is maintained
837 * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
838 * requests is halted at any time, the final value of all data observable
839 * on the medium shall be the same as if all the commands had been processed
840 * with the ORDERED task attribute).
842 * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
843 * device server may reorder the processing sequence of commands having the
844 * SIMPLE task attribute in any manner. Any data integrity exposures related to
845 * command sequence order shall be explicitly handled by the application client
846 * through the selection of appropriate ommands and task attributes.
848 p
[3] = (dev
->dev_attrib
.emulate_rest_reord
== 1) ? 0x00 : 0x10;
850 * From spc4r17, section 7.4.6 Control mode Page
852 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
854 * 00b: The logical unit shall clear any unit attention condition
855 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
856 * status and shall not establish a unit attention condition when a com-
857 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
860 * 10b: The logical unit shall not clear any unit attention condition
861 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
862 * status and shall not establish a unit attention condition when
863 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
866 * 11b a The logical unit shall not clear any unit attention condition
867 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
868 * status and shall establish a unit attention condition for the
869 * initiator port associated with the I_T nexus on which the BUSY,
870 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
871 * Depending on the status, the additional sense code shall be set to
872 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
873 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
874 * command, a unit attention condition shall be established only once
875 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
876 * to the number of commands completed with one of those status codes.
878 switch (dev
->dev_attrib
.emulate_ua_intlck_ctrl
) {
879 case TARGET_UA_INTLCK_CTRL_ESTABLISH_UA
:
882 case TARGET_UA_INTLCK_CTRL_NO_CLEAR
:
885 default: /* TARGET_UA_INTLCK_CTRL_CLEAR */
890 * From spc4r17, section 7.4.6 Control mode Page
892 * Task Aborted Status (TAS) bit set to zero.
894 * A task aborted status (TAS) bit set to zero specifies that aborted
895 * tasks shall be terminated by the device server without any response
896 * to the application client. A TAS bit set to one specifies that tasks
897 * aborted by the actions of an I_T nexus other than the I_T nexus on
898 * which the command was received shall be completed with TASK ABORTED
899 * status (see SAM-4).
901 p
[5] = (dev
->dev_attrib
.emulate_tas
) ? 0x40 : 0x00;
903 * From spc4r30, section 7.5.7 Control mode page
905 * Application Tag Owner (ATO) bit set to one.
907 * If the ATO bit is set to one the device server shall not modify the
908 * LOGICAL BLOCK APPLICATION TAG field and, depending on the protection
909 * type, shall not modify the contents of the LOGICAL BLOCK REFERENCE
912 if (sess
->sup_prot_ops
& (TARGET_PROT_DIN_PASS
| TARGET_PROT_DOUT_PASS
)) {
913 if (dev
->dev_attrib
.pi_prot_type
|| sess
->sess_prot_type
)
925 static int spc_modesense_caching(struct se_cmd
*cmd
, u8 pc
, u8
*p
)
927 struct se_device
*dev
= cmd
->se_dev
;
932 /* No changeable values for now */
936 if (target_check_wce(dev
))
937 p
[2] = 0x04; /* Write Cache Enable */
938 p
[12] = 0x20; /* Disabled Read Ahead */
944 static int spc_modesense_informational_exceptions(struct se_cmd
*cmd
, u8 pc
, unsigned char *p
)
949 /* No changeable values for now */
960 int (*emulate
)(struct se_cmd
*, u8
, unsigned char *);
961 } modesense_handlers
[] = {
962 { .page
= 0x01, .subpage
= 0x00, .emulate
= spc_modesense_rwrecovery
},
963 { .page
= 0x08, .subpage
= 0x00, .emulate
= spc_modesense_caching
},
964 { .page
= 0x0a, .subpage
= 0x00, .emulate
= spc_modesense_control
},
965 { .page
= 0x1c, .subpage
= 0x00, .emulate
= spc_modesense_informational_exceptions
},
968 static void spc_modesense_write_protect(unsigned char *buf
, int type
)
971 * I believe that the WP bit (bit 7) in the mode header is the same for
978 buf
[0] |= 0x80; /* WP bit */
983 static void spc_modesense_dpofua(unsigned char *buf
, int type
)
987 buf
[0] |= 0x10; /* DPOFUA bit */
994 static int spc_modesense_blockdesc(unsigned char *buf
, u64 blocks
, u32 block_size
)
997 put_unaligned_be32(min(blocks
, 0xffffffffull
), buf
);
999 put_unaligned_be32(block_size
, buf
);
1003 static int spc_modesense_long_blockdesc(unsigned char *buf
, u64 blocks
, u32 block_size
)
1005 if (blocks
<= 0xffffffff)
1006 return spc_modesense_blockdesc(buf
+ 3, blocks
, block_size
) + 3;
1008 *buf
++ = 1; /* LONGLBA */
1011 put_unaligned_be64(blocks
, buf
);
1013 put_unaligned_be32(block_size
, buf
);
1018 static sense_reason_t
spc_emulate_modesense(struct se_cmd
*cmd
)
1020 struct se_device
*dev
= cmd
->se_dev
;
1021 char *cdb
= cmd
->t_task_cdb
;
1022 unsigned char buf
[SE_MODE_PAGE_BUF
], *rbuf
;
1023 int type
= dev
->transport
->get_device_type(dev
);
1024 int ten
= (cmd
->t_task_cdb
[0] == MODE_SENSE_10
);
1025 bool dbd
= !!(cdb
[1] & 0x08);
1026 bool llba
= ten
? !!(cdb
[1] & 0x10) : false;
1027 u8 pc
= cdb
[2] >> 6;
1028 u8 page
= cdb
[2] & 0x3f;
1029 u8 subpage
= cdb
[3];
1034 memset(buf
, 0, SE_MODE_PAGE_BUF
);
1037 * Skip over MODE DATA LENGTH + MEDIUM TYPE fields to byte 3 for
1038 * MODE_SENSE_10 and byte 2 for MODE_SENSE (6).
1040 length
= ten
? 3 : 2;
1042 /* DEVICE-SPECIFIC PARAMETER */
1043 if (cmd
->se_lun
->lun_access_ro
|| target_lun_is_rdonly(cmd
))
1044 spc_modesense_write_protect(&buf
[length
], type
);
1047 * SBC only allows us to enable FUA and DPO together. Fortunately
1048 * DPO is explicitly specified as a hint, so a noop is a perfectly
1049 * valid implementation.
1051 if (target_check_fua(dev
))
1052 spc_modesense_dpofua(&buf
[length
], type
);
1056 /* BLOCK DESCRIPTOR */
1059 * For now we only include a block descriptor for disk (SBC)
1060 * devices; other command sets use a slightly different format.
1062 if (!dbd
&& type
== TYPE_DISK
) {
1063 u64 blocks
= dev
->transport
->get_blocks(dev
);
1064 u32 block_size
= dev
->dev_attrib
.block_size
;
1068 length
+= spc_modesense_long_blockdesc(&buf
[length
],
1069 blocks
, block_size
);
1072 length
+= spc_modesense_blockdesc(&buf
[length
],
1073 blocks
, block_size
);
1076 length
+= spc_modesense_blockdesc(&buf
[length
], blocks
,
1087 if (subpage
!= 0x00 && subpage
!= 0xff) {
1088 pr_warn("MODE_SENSE: Invalid subpage code: 0x%02x\n", subpage
);
1089 return TCM_INVALID_CDB_FIELD
;
1092 for (i
= 0; i
< ARRAY_SIZE(modesense_handlers
); ++i
) {
1094 * Tricky way to say all subpage 00h for
1095 * subpage==0, all subpages for subpage==0xff
1096 * (and we just checked above that those are
1097 * the only two possibilities).
1099 if ((modesense_handlers
[i
].subpage
& ~subpage
) == 0) {
1100 ret
= modesense_handlers
[i
].emulate(cmd
, pc
, &buf
[length
]);
1101 if (!ten
&& length
+ ret
>= 255)
1110 for (i
= 0; i
< ARRAY_SIZE(modesense_handlers
); ++i
)
1111 if (modesense_handlers
[i
].page
== page
&&
1112 modesense_handlers
[i
].subpage
== subpage
) {
1113 length
+= modesense_handlers
[i
].emulate(cmd
, pc
, &buf
[length
]);
1118 * We don't intend to implement:
1119 * - obsolete page 03h "format parameters" (checked by Solaris)
1122 pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
1125 return TCM_UNKNOWN_MODE_PAGE
;
1129 put_unaligned_be16(length
- 2, buf
);
1131 buf
[0] = length
- 1;
1133 rbuf
= transport_kmap_data_sg(cmd
);
1135 memcpy(rbuf
, buf
, min_t(u32
, SE_MODE_PAGE_BUF
, cmd
->data_length
));
1136 transport_kunmap_data_sg(cmd
);
1139 target_complete_cmd_with_length(cmd
, SAM_STAT_GOOD
, length
);
1143 static sense_reason_t
spc_emulate_modeselect(struct se_cmd
*cmd
)
1145 char *cdb
= cmd
->t_task_cdb
;
1146 bool ten
= cdb
[0] == MODE_SELECT_10
;
1147 int off
= ten
? 8 : 4;
1148 bool pf
= !!(cdb
[1] & 0x10);
1151 unsigned char tbuf
[SE_MODE_PAGE_BUF
];
1153 sense_reason_t ret
= 0;
1156 if (!cmd
->data_length
) {
1157 target_complete_cmd(cmd
, SAM_STAT_GOOD
);
1161 if (cmd
->data_length
< off
+ 2)
1162 return TCM_PARAMETER_LIST_LENGTH_ERROR
;
1164 buf
= transport_kmap_data_sg(cmd
);
1166 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1169 ret
= TCM_INVALID_CDB_FIELD
;
1173 page
= buf
[off
] & 0x3f;
1174 subpage
= buf
[off
] & 0x40 ? buf
[off
+ 1] : 0;
1176 for (i
= 0; i
< ARRAY_SIZE(modesense_handlers
); ++i
)
1177 if (modesense_handlers
[i
].page
== page
&&
1178 modesense_handlers
[i
].subpage
== subpage
) {
1179 memset(tbuf
, 0, SE_MODE_PAGE_BUF
);
1180 length
= modesense_handlers
[i
].emulate(cmd
, 0, tbuf
);
1181 goto check_contents
;
1184 ret
= TCM_UNKNOWN_MODE_PAGE
;
1188 if (cmd
->data_length
< off
+ length
) {
1189 ret
= TCM_PARAMETER_LIST_LENGTH_ERROR
;
1193 if (memcmp(buf
+ off
, tbuf
, length
))
1194 ret
= TCM_INVALID_PARAMETER_LIST
;
1197 transport_kunmap_data_sg(cmd
);
1200 target_complete_cmd(cmd
, SAM_STAT_GOOD
);
1204 static sense_reason_t
spc_emulate_request_sense(struct se_cmd
*cmd
)
1206 unsigned char *cdb
= cmd
->t_task_cdb
;
1207 unsigned char *rbuf
;
1208 u8 ua_asc
= 0, ua_ascq
= 0;
1209 unsigned char buf
[SE_SENSE_BUF
];
1210 bool desc_format
= target_sense_desc_format(cmd
->se_dev
);
1212 memset(buf
, 0, SE_SENSE_BUF
);
1214 if (cdb
[1] & 0x01) {
1215 pr_err("REQUEST_SENSE description emulation not"
1217 return TCM_INVALID_CDB_FIELD
;
1220 rbuf
= transport_kmap_data_sg(cmd
);
1222 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1224 if (!core_scsi3_ua_clear_for_request_sense(cmd
, &ua_asc
, &ua_ascq
))
1225 scsi_build_sense_buffer(desc_format
, buf
, UNIT_ATTENTION
,
1228 scsi_build_sense_buffer(desc_format
, buf
, NO_SENSE
, 0x0, 0x0);
1230 memcpy(rbuf
, buf
, min_t(u32
, sizeof(buf
), cmd
->data_length
));
1231 transport_kunmap_data_sg(cmd
);
1233 target_complete_cmd(cmd
, SAM_STAT_GOOD
);
1237 sense_reason_t
spc_emulate_report_luns(struct se_cmd
*cmd
)
1239 struct se_dev_entry
*deve
;
1240 struct se_session
*sess
= cmd
->se_sess
;
1241 struct se_node_acl
*nacl
;
1242 struct scsi_lun slun
;
1244 u32 lun_count
= 0, offset
= 8;
1247 buf
= transport_kmap_data_sg(cmd
);
1248 if (cmd
->data_length
&& !buf
)
1249 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1252 * If no struct se_session pointer is present, this struct se_cmd is
1253 * coming via a target_core_mod PASSTHROUGH op, and not through
1254 * a $FABRIC_MOD. In that case, report LUN=0 only.
1259 nacl
= sess
->se_node_acl
;
1262 hlist_for_each_entry_rcu(deve
, &nacl
->lun_entry_hlist
, link
) {
1264 * We determine the correct LUN LIST LENGTH even once we
1265 * have reached the initial allocation length.
1266 * See SPC2-R20 7.19.
1269 if (offset
>= cmd
->data_length
)
1272 int_to_scsilun(deve
->mapped_lun
, &slun
);
1273 memcpy(buf
+ offset
, &slun
,
1274 min(8u, cmd
->data_length
- offset
));
1280 * See SPC3 r07, page 159.
1284 * If no LUNs are accessible, report virtual LUN 0.
1286 if (lun_count
== 0) {
1287 int_to_scsilun(0, &slun
);
1288 if (cmd
->data_length
> 8)
1289 memcpy(buf
+ offset
, &slun
,
1290 min(8u, cmd
->data_length
- offset
));
1295 len
= cpu_to_be32(lun_count
* 8);
1296 memcpy(buf
, &len
, min_t(int, sizeof len
, cmd
->data_length
));
1297 transport_kunmap_data_sg(cmd
);
1300 target_complete_cmd_with_length(cmd
, SAM_STAT_GOOD
, 8 + lun_count
* 8);
1303 EXPORT_SYMBOL(spc_emulate_report_luns
);
1305 static sense_reason_t
1306 spc_emulate_testunitready(struct se_cmd
*cmd
)
1308 target_complete_cmd(cmd
, SAM_STAT_GOOD
);
1312 static void set_dpofua_usage_bits(u8
*usage_bits
, struct se_device
*dev
)
1314 if (!target_check_fua(dev
))
1315 usage_bits
[1] &= ~0x18;
1317 usage_bits
[1] |= 0x18;
1320 static void set_dpofua_usage_bits32(u8
*usage_bits
, struct se_device
*dev
)
1322 if (!target_check_fua(dev
))
1323 usage_bits
[10] &= ~0x18;
1325 usage_bits
[10] |= 0x18;
1328 static struct target_opcode_descriptor tcm_opcode_read6
= {
1329 .support
= SCSI_SUPPORT_FULL
,
1332 .usage_bits
= {READ_6
, 0x1f, 0xff, 0xff,
1333 0xff, SCSI_CONTROL_MASK
},
1336 static struct target_opcode_descriptor tcm_opcode_read10
= {
1337 .support
= SCSI_SUPPORT_FULL
,
1340 .usage_bits
= {READ_10
, 0xf8, 0xff, 0xff,
1341 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, 0xff,
1342 0xff, SCSI_CONTROL_MASK
},
1343 .update_usage_bits
= set_dpofua_usage_bits
,
1346 static struct target_opcode_descriptor tcm_opcode_read12
= {
1347 .support
= SCSI_SUPPORT_FULL
,
1350 .usage_bits
= {READ_12
, 0xf8, 0xff, 0xff,
1351 0xff, 0xff, 0xff, 0xff,
1352 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, SCSI_CONTROL_MASK
},
1353 .update_usage_bits
= set_dpofua_usage_bits
,
1356 static struct target_opcode_descriptor tcm_opcode_read16
= {
1357 .support
= SCSI_SUPPORT_FULL
,
1360 .usage_bits
= {READ_16
, 0xf8, 0xff, 0xff,
1361 0xff, 0xff, 0xff, 0xff,
1362 0xff, 0xff, 0xff, 0xff,
1363 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, SCSI_CONTROL_MASK
},
1364 .update_usage_bits
= set_dpofua_usage_bits
,
1367 static struct target_opcode_descriptor tcm_opcode_write6
= {
1368 .support
= SCSI_SUPPORT_FULL
,
1371 .usage_bits
= {WRITE_6
, 0x1f, 0xff, 0xff,
1372 0xff, SCSI_CONTROL_MASK
},
1375 static struct target_opcode_descriptor tcm_opcode_write10
= {
1376 .support
= SCSI_SUPPORT_FULL
,
1379 .usage_bits
= {WRITE_10
, 0xf8, 0xff, 0xff,
1380 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, 0xff,
1381 0xff, SCSI_CONTROL_MASK
},
1382 .update_usage_bits
= set_dpofua_usage_bits
,
1385 static struct target_opcode_descriptor tcm_opcode_write_verify10
= {
1386 .support
= SCSI_SUPPORT_FULL
,
1387 .opcode
= WRITE_VERIFY
,
1389 .usage_bits
= {WRITE_VERIFY
, 0xf0, 0xff, 0xff,
1390 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, 0xff,
1391 0xff, SCSI_CONTROL_MASK
},
1392 .update_usage_bits
= set_dpofua_usage_bits
,
1395 static struct target_opcode_descriptor tcm_opcode_write12
= {
1396 .support
= SCSI_SUPPORT_FULL
,
1399 .usage_bits
= {WRITE_12
, 0xf8, 0xff, 0xff,
1400 0xff, 0xff, 0xff, 0xff,
1401 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, SCSI_CONTROL_MASK
},
1402 .update_usage_bits
= set_dpofua_usage_bits
,
1405 static struct target_opcode_descriptor tcm_opcode_write16
= {
1406 .support
= SCSI_SUPPORT_FULL
,
1409 .usage_bits
= {WRITE_16
, 0xf8, 0xff, 0xff,
1410 0xff, 0xff, 0xff, 0xff,
1411 0xff, 0xff, 0xff, 0xff,
1412 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, SCSI_CONTROL_MASK
},
1413 .update_usage_bits
= set_dpofua_usage_bits
,
1416 static struct target_opcode_descriptor tcm_opcode_write_verify16
= {
1417 .support
= SCSI_SUPPORT_FULL
,
1418 .opcode
= WRITE_VERIFY_16
,
1420 .usage_bits
= {WRITE_VERIFY_16
, 0xf0, 0xff, 0xff,
1421 0xff, 0xff, 0xff, 0xff,
1422 0xff, 0xff, 0xff, 0xff,
1423 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, SCSI_CONTROL_MASK
},
1424 .update_usage_bits
= set_dpofua_usage_bits
,
1427 static bool tcm_is_ws_enabled(struct target_opcode_descriptor
*descr
,
1430 struct exec_cmd_ops
*ops
= cmd
->protocol_data
;
1431 struct se_device
*dev
= cmd
->se_dev
;
1433 return (dev
->dev_attrib
.emulate_tpws
&& !!ops
->execute_unmap
) ||
1434 !!ops
->execute_write_same
;
1437 static struct target_opcode_descriptor tcm_opcode_write_same32
= {
1438 .support
= SCSI_SUPPORT_FULL
,
1439 .serv_action_valid
= 1,
1440 .opcode
= VARIABLE_LENGTH_CMD
,
1441 .service_action
= WRITE_SAME_32
,
1443 .usage_bits
= {VARIABLE_LENGTH_CMD
, SCSI_CONTROL_MASK
, 0x00, 0x00,
1444 0x00, 0x00, SCSI_GROUP_NUMBER_MASK
, 0x18,
1445 0x00, WRITE_SAME_32
, 0xe8, 0x00,
1446 0xff, 0xff, 0xff, 0xff,
1447 0xff, 0xff, 0xff, 0xff,
1448 0x00, 0x00, 0x00, 0x00,
1449 0x00, 0x00, 0x00, 0x00,
1450 0xff, 0xff, 0xff, 0xff},
1451 .enabled
= tcm_is_ws_enabled
,
1452 .update_usage_bits
= set_dpofua_usage_bits32
,
1455 static bool tcm_is_caw_enabled(struct target_opcode_descriptor
*descr
,
1458 struct se_device
*dev
= cmd
->se_dev
;
1460 return dev
->dev_attrib
.emulate_caw
;
1463 static struct target_opcode_descriptor tcm_opcode_compare_write
= {
1464 .support
= SCSI_SUPPORT_FULL
,
1465 .opcode
= COMPARE_AND_WRITE
,
1467 .usage_bits
= {COMPARE_AND_WRITE
, 0x18, 0xff, 0xff,
1468 0xff, 0xff, 0xff, 0xff,
1469 0xff, 0xff, 0x00, 0x00,
1470 0x00, 0xff, SCSI_GROUP_NUMBER_MASK
, SCSI_CONTROL_MASK
},
1471 .enabled
= tcm_is_caw_enabled
,
1472 .update_usage_bits
= set_dpofua_usage_bits
,
1475 static struct target_opcode_descriptor tcm_opcode_read_capacity
= {
1476 .support
= SCSI_SUPPORT_FULL
,
1477 .opcode
= READ_CAPACITY
,
1479 .usage_bits
= {READ_CAPACITY
, 0x00, 0xff, 0xff,
1480 0xff, 0xff, 0x00, 0x00,
1481 0x01, SCSI_CONTROL_MASK
},
1484 static struct target_opcode_descriptor tcm_opcode_read_capacity16
= {
1485 .support
= SCSI_SUPPORT_FULL
,
1486 .serv_action_valid
= 1,
1487 .opcode
= SERVICE_ACTION_IN_16
,
1488 .service_action
= SAI_READ_CAPACITY_16
,
1490 .usage_bits
= {SERVICE_ACTION_IN_16
, SAI_READ_CAPACITY_16
, 0x00, 0x00,
1491 0x00, 0x00, 0x00, 0x00,
1492 0x00, 0x00, 0xff, 0xff,
1493 0xff, 0xff, 0x00, SCSI_CONTROL_MASK
},
1496 static bool tcm_is_rep_ref_enabled(struct target_opcode_descriptor
*descr
,
1499 struct se_device
*dev
= cmd
->se_dev
;
1501 spin_lock(&dev
->t10_alua
.lba_map_lock
);
1502 if (list_empty(&dev
->t10_alua
.lba_map_list
)) {
1503 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
1506 spin_unlock(&dev
->t10_alua
.lba_map_lock
);
1510 static struct target_opcode_descriptor tcm_opcode_read_report_refferals
= {
1511 .support
= SCSI_SUPPORT_FULL
,
1512 .serv_action_valid
= 1,
1513 .opcode
= SERVICE_ACTION_IN_16
,
1514 .service_action
= SAI_REPORT_REFERRALS
,
1516 .usage_bits
= {SERVICE_ACTION_IN_16
, SAI_REPORT_REFERRALS
, 0x00, 0x00,
1517 0x00, 0x00, 0x00, 0x00,
1518 0x00, 0x00, 0xff, 0xff,
1519 0xff, 0xff, 0x00, SCSI_CONTROL_MASK
},
1520 .enabled
= tcm_is_rep_ref_enabled
,
1523 static struct target_opcode_descriptor tcm_opcode_sync_cache
= {
1524 .support
= SCSI_SUPPORT_FULL
,
1525 .opcode
= SYNCHRONIZE_CACHE
,
1527 .usage_bits
= {SYNCHRONIZE_CACHE
, 0x02, 0xff, 0xff,
1528 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, 0xff,
1529 0xff, SCSI_CONTROL_MASK
},
1532 static struct target_opcode_descriptor tcm_opcode_sync_cache16
= {
1533 .support
= SCSI_SUPPORT_FULL
,
1534 .opcode
= SYNCHRONIZE_CACHE_16
,
1536 .usage_bits
= {SYNCHRONIZE_CACHE_16
, 0x02, 0xff, 0xff,
1537 0xff, 0xff, 0xff, 0xff,
1538 0xff, 0xff, 0xff, 0xff,
1539 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, SCSI_CONTROL_MASK
},
1542 static bool tcm_is_unmap_enabled(struct target_opcode_descriptor
*descr
,
1545 struct exec_cmd_ops
*ops
= cmd
->protocol_data
;
1546 struct se_device
*dev
= cmd
->se_dev
;
1548 return ops
->execute_unmap
&& dev
->dev_attrib
.emulate_tpu
;
1551 static struct target_opcode_descriptor tcm_opcode_unmap
= {
1552 .support
= SCSI_SUPPORT_FULL
,
1555 .usage_bits
= {UNMAP
, 0x00, 0x00, 0x00,
1556 0x00, 0x00, SCSI_GROUP_NUMBER_MASK
, 0xff,
1557 0xff, SCSI_CONTROL_MASK
},
1558 .enabled
= tcm_is_unmap_enabled
,
1561 static struct target_opcode_descriptor tcm_opcode_write_same
= {
1562 .support
= SCSI_SUPPORT_FULL
,
1563 .opcode
= WRITE_SAME
,
1565 .usage_bits
= {WRITE_SAME
, 0xe8, 0xff, 0xff,
1566 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, 0xff,
1567 0xff, SCSI_CONTROL_MASK
},
1568 .enabled
= tcm_is_ws_enabled
,
1571 static struct target_opcode_descriptor tcm_opcode_write_same16
= {
1572 .support
= SCSI_SUPPORT_FULL
,
1573 .opcode
= WRITE_SAME_16
,
1575 .usage_bits
= {WRITE_SAME_16
, 0xe8, 0xff, 0xff,
1576 0xff, 0xff, 0xff, 0xff,
1577 0xff, 0xff, 0xff, 0xff,
1578 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, SCSI_CONTROL_MASK
},
1579 .enabled
= tcm_is_ws_enabled
,
1582 static struct target_opcode_descriptor tcm_opcode_verify
= {
1583 .support
= SCSI_SUPPORT_FULL
,
1586 .usage_bits
= {VERIFY
, 0x00, 0xff, 0xff,
1587 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, 0xff,
1588 0xff, SCSI_CONTROL_MASK
},
1591 static struct target_opcode_descriptor tcm_opcode_verify16
= {
1592 .support
= SCSI_SUPPORT_FULL
,
1593 .opcode
= VERIFY_16
,
1595 .usage_bits
= {VERIFY_16
, 0x00, 0xff, 0xff,
1596 0xff, 0xff, 0xff, 0xff,
1597 0xff, 0xff, 0xff, 0xff,
1598 0xff, 0xff, SCSI_GROUP_NUMBER_MASK
, SCSI_CONTROL_MASK
},
1601 static struct target_opcode_descriptor tcm_opcode_start_stop
= {
1602 .support
= SCSI_SUPPORT_FULL
,
1603 .opcode
= START_STOP
,
1605 .usage_bits
= {START_STOP
, 0x01, 0x00, 0x00,
1606 0x01, SCSI_CONTROL_MASK
},
1609 static struct target_opcode_descriptor tcm_opcode_mode_select
= {
1610 .support
= SCSI_SUPPORT_FULL
,
1611 .opcode
= MODE_SELECT
,
1613 .usage_bits
= {MODE_SELECT
, 0x10, 0x00, 0x00,
1614 0xff, SCSI_CONTROL_MASK
},
1617 static struct target_opcode_descriptor tcm_opcode_mode_select10
= {
1618 .support
= SCSI_SUPPORT_FULL
,
1619 .opcode
= MODE_SELECT_10
,
1621 .usage_bits
= {MODE_SELECT_10
, 0x10, 0x00, 0x00,
1622 0x00, 0x00, 0x00, 0xff,
1623 0xff, SCSI_CONTROL_MASK
},
1626 static struct target_opcode_descriptor tcm_opcode_mode_sense
= {
1627 .support
= SCSI_SUPPORT_FULL
,
1628 .opcode
= MODE_SENSE
,
1630 .usage_bits
= {MODE_SENSE
, 0x08, 0xff, 0xff,
1631 0xff, SCSI_CONTROL_MASK
},
1634 static struct target_opcode_descriptor tcm_opcode_mode_sense10
= {
1635 .support
= SCSI_SUPPORT_FULL
,
1636 .opcode
= MODE_SENSE_10
,
1638 .usage_bits
= {MODE_SENSE_10
, 0x18, 0xff, 0xff,
1639 0x00, 0x00, 0x00, 0xff,
1640 0xff, SCSI_CONTROL_MASK
},
1643 static struct target_opcode_descriptor tcm_opcode_pri_read_keys
= {
1644 .support
= SCSI_SUPPORT_FULL
,
1645 .serv_action_valid
= 1,
1646 .opcode
= PERSISTENT_RESERVE_IN
,
1647 .service_action
= PRI_READ_KEYS
,
1649 .usage_bits
= {PERSISTENT_RESERVE_IN
, PRI_READ_KEYS
, 0x00, 0x00,
1650 0x00, 0x00, 0x00, 0xff,
1651 0xff, SCSI_CONTROL_MASK
},
1654 static struct target_opcode_descriptor tcm_opcode_pri_read_resrv
= {
1655 .support
= SCSI_SUPPORT_FULL
,
1656 .serv_action_valid
= 1,
1657 .opcode
= PERSISTENT_RESERVE_IN
,
1658 .service_action
= PRI_READ_RESERVATION
,
1660 .usage_bits
= {PERSISTENT_RESERVE_IN
, PRI_READ_RESERVATION
, 0x00, 0x00,
1661 0x00, 0x00, 0x00, 0xff,
1662 0xff, SCSI_CONTROL_MASK
},
1665 static bool tcm_is_pr_enabled(struct target_opcode_descriptor
*descr
,
1668 struct se_device
*dev
= cmd
->se_dev
;
1670 if (!dev
->dev_attrib
.emulate_pr
)
1673 if (!(dev
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_PGR
))
1676 switch (descr
->opcode
) {
1682 * The pr_ops which are used by the backend modules don't
1683 * support these commands.
1686 case PERSISTENT_RESERVE_OUT
:
1687 switch (descr
->service_action
) {
1688 case PRO_REGISTER_AND_MOVE
:
1689 case PRO_REPLACE_LOST_RESERVATION
:
1691 * The backend modules don't have access to ports and
1692 * I_T nexuses so they can't handle these type of
1698 case PERSISTENT_RESERVE_IN
:
1699 if (descr
->service_action
== PRI_READ_FULL_STATUS
)
1707 static struct target_opcode_descriptor tcm_opcode_pri_read_caps
= {
1708 .support
= SCSI_SUPPORT_FULL
,
1709 .serv_action_valid
= 1,
1710 .opcode
= PERSISTENT_RESERVE_IN
,
1711 .service_action
= PRI_REPORT_CAPABILITIES
,
1713 .usage_bits
= {PERSISTENT_RESERVE_IN
, PRI_REPORT_CAPABILITIES
, 0x00, 0x00,
1714 0x00, 0x00, 0x00, 0xff,
1715 0xff, SCSI_CONTROL_MASK
},
1716 .enabled
= tcm_is_pr_enabled
,
1719 static struct target_opcode_descriptor tcm_opcode_pri_read_full_status
= {
1720 .support
= SCSI_SUPPORT_FULL
,
1721 .serv_action_valid
= 1,
1722 .opcode
= PERSISTENT_RESERVE_IN
,
1723 .service_action
= PRI_READ_FULL_STATUS
,
1725 .usage_bits
= {PERSISTENT_RESERVE_IN
, PRI_READ_FULL_STATUS
, 0x00, 0x00,
1726 0x00, 0x00, 0x00, 0xff,
1727 0xff, SCSI_CONTROL_MASK
},
1728 .enabled
= tcm_is_pr_enabled
,
1731 static struct target_opcode_descriptor tcm_opcode_pro_register
= {
1732 .support
= SCSI_SUPPORT_FULL
,
1733 .serv_action_valid
= 1,
1734 .opcode
= PERSISTENT_RESERVE_OUT
,
1735 .service_action
= PRO_REGISTER
,
1737 .usage_bits
= {PERSISTENT_RESERVE_OUT
, PRO_REGISTER
, 0xff, 0x00,
1738 0x00, 0xff, 0xff, 0xff,
1739 0xff, SCSI_CONTROL_MASK
},
1740 .enabled
= tcm_is_pr_enabled
,
1743 static struct target_opcode_descriptor tcm_opcode_pro_reserve
= {
1744 .support
= SCSI_SUPPORT_FULL
,
1745 .serv_action_valid
= 1,
1746 .opcode
= PERSISTENT_RESERVE_OUT
,
1747 .service_action
= PRO_RESERVE
,
1749 .usage_bits
= {PERSISTENT_RESERVE_OUT
, PRO_RESERVE
, 0xff, 0x00,
1750 0x00, 0xff, 0xff, 0xff,
1751 0xff, SCSI_CONTROL_MASK
},
1752 .enabled
= tcm_is_pr_enabled
,
1755 static struct target_opcode_descriptor tcm_opcode_pro_release
= {
1756 .support
= SCSI_SUPPORT_FULL
,
1757 .serv_action_valid
= 1,
1758 .opcode
= PERSISTENT_RESERVE_OUT
,
1759 .service_action
= PRO_RELEASE
,
1761 .usage_bits
= {PERSISTENT_RESERVE_OUT
, PRO_RELEASE
, 0xff, 0x00,
1762 0x00, 0xff, 0xff, 0xff,
1763 0xff, SCSI_CONTROL_MASK
},
1764 .enabled
= tcm_is_pr_enabled
,
1767 static struct target_opcode_descriptor tcm_opcode_pro_clear
= {
1768 .support
= SCSI_SUPPORT_FULL
,
1769 .serv_action_valid
= 1,
1770 .opcode
= PERSISTENT_RESERVE_OUT
,
1771 .service_action
= PRO_CLEAR
,
1773 .usage_bits
= {PERSISTENT_RESERVE_OUT
, PRO_CLEAR
, 0xff, 0x00,
1774 0x00, 0xff, 0xff, 0xff,
1775 0xff, SCSI_CONTROL_MASK
},
1776 .enabled
= tcm_is_pr_enabled
,
1779 static struct target_opcode_descriptor tcm_opcode_pro_preempt
= {
1780 .support
= SCSI_SUPPORT_FULL
,
1781 .serv_action_valid
= 1,
1782 .opcode
= PERSISTENT_RESERVE_OUT
,
1783 .service_action
= PRO_PREEMPT
,
1785 .usage_bits
= {PERSISTENT_RESERVE_OUT
, PRO_PREEMPT
, 0xff, 0x00,
1786 0x00, 0xff, 0xff, 0xff,
1787 0xff, SCSI_CONTROL_MASK
},
1788 .enabled
= tcm_is_pr_enabled
,
1791 static struct target_opcode_descriptor tcm_opcode_pro_preempt_abort
= {
1792 .support
= SCSI_SUPPORT_FULL
,
1793 .serv_action_valid
= 1,
1794 .opcode
= PERSISTENT_RESERVE_OUT
,
1795 .service_action
= PRO_PREEMPT_AND_ABORT
,
1797 .usage_bits
= {PERSISTENT_RESERVE_OUT
, PRO_PREEMPT_AND_ABORT
, 0xff, 0x00,
1798 0x00, 0xff, 0xff, 0xff,
1799 0xff, SCSI_CONTROL_MASK
},
1800 .enabled
= tcm_is_pr_enabled
,
1803 static struct target_opcode_descriptor tcm_opcode_pro_reg_ign_exist
= {
1804 .support
= SCSI_SUPPORT_FULL
,
1805 .serv_action_valid
= 1,
1806 .opcode
= PERSISTENT_RESERVE_OUT
,
1807 .service_action
= PRO_REGISTER_AND_IGNORE_EXISTING_KEY
,
1810 PERSISTENT_RESERVE_OUT
, PRO_REGISTER_AND_IGNORE_EXISTING_KEY
,
1812 0x00, 0xff, 0xff, 0xff,
1813 0xff, SCSI_CONTROL_MASK
},
1814 .enabled
= tcm_is_pr_enabled
,
1817 static struct target_opcode_descriptor tcm_opcode_pro_register_move
= {
1818 .support
= SCSI_SUPPORT_FULL
,
1819 .serv_action_valid
= 1,
1820 .opcode
= PERSISTENT_RESERVE_OUT
,
1821 .service_action
= PRO_REGISTER_AND_MOVE
,
1823 .usage_bits
= {PERSISTENT_RESERVE_OUT
, PRO_REGISTER_AND_MOVE
, 0xff, 0x00,
1824 0x00, 0xff, 0xff, 0xff,
1825 0xff, SCSI_CONTROL_MASK
},
1826 .enabled
= tcm_is_pr_enabled
,
1829 static struct target_opcode_descriptor tcm_opcode_release
= {
1830 .support
= SCSI_SUPPORT_FULL
,
1833 .usage_bits
= {RELEASE
, 0x00, 0x00, 0x00,
1834 0x00, SCSI_CONTROL_MASK
},
1835 .enabled
= tcm_is_pr_enabled
,
1838 static struct target_opcode_descriptor tcm_opcode_release10
= {
1839 .support
= SCSI_SUPPORT_FULL
,
1840 .opcode
= RELEASE_10
,
1842 .usage_bits
= {RELEASE_10
, 0x00, 0x00, 0x00,
1843 0x00, 0x00, 0x00, 0xff,
1844 0xff, SCSI_CONTROL_MASK
},
1845 .enabled
= tcm_is_pr_enabled
,
1848 static struct target_opcode_descriptor tcm_opcode_reserve
= {
1849 .support
= SCSI_SUPPORT_FULL
,
1852 .usage_bits
= {RESERVE
, 0x00, 0x00, 0x00,
1853 0x00, SCSI_CONTROL_MASK
},
1854 .enabled
= tcm_is_pr_enabled
,
1857 static struct target_opcode_descriptor tcm_opcode_reserve10
= {
1858 .support
= SCSI_SUPPORT_FULL
,
1859 .opcode
= RESERVE_10
,
1861 .usage_bits
= {RESERVE_10
, 0x00, 0x00, 0x00,
1862 0x00, 0x00, 0x00, 0xff,
1863 0xff, SCSI_CONTROL_MASK
},
1864 .enabled
= tcm_is_pr_enabled
,
1867 static struct target_opcode_descriptor tcm_opcode_request_sense
= {
1868 .support
= SCSI_SUPPORT_FULL
,
1869 .opcode
= REQUEST_SENSE
,
1871 .usage_bits
= {REQUEST_SENSE
, 0x00, 0x00, 0x00,
1872 0xff, SCSI_CONTROL_MASK
},
1875 static struct target_opcode_descriptor tcm_opcode_inquiry
= {
1876 .support
= SCSI_SUPPORT_FULL
,
1879 .usage_bits
= {INQUIRY
, 0x01, 0xff, 0xff,
1880 0xff, SCSI_CONTROL_MASK
},
1883 static bool tcm_is_3pc_enabled(struct target_opcode_descriptor
*descr
,
1886 struct se_device
*dev
= cmd
->se_dev
;
1888 return dev
->dev_attrib
.emulate_3pc
;
1891 static struct target_opcode_descriptor tcm_opcode_extended_copy_lid1
= {
1892 .support
= SCSI_SUPPORT_FULL
,
1893 .serv_action_valid
= 1,
1894 .opcode
= EXTENDED_COPY
,
1896 .usage_bits
= {EXTENDED_COPY
, 0x00, 0x00, 0x00,
1897 0x00, 0x00, 0x00, 0x00,
1898 0x00, 0x00, 0xff, 0xff,
1899 0xff, 0xff, 0x00, SCSI_CONTROL_MASK
},
1900 .enabled
= tcm_is_3pc_enabled
,
1903 static struct target_opcode_descriptor tcm_opcode_rcv_copy_res_op_params
= {
1904 .support
= SCSI_SUPPORT_FULL
,
1905 .serv_action_valid
= 1,
1906 .opcode
= RECEIVE_COPY_RESULTS
,
1907 .service_action
= RCR_SA_OPERATING_PARAMETERS
,
1909 .usage_bits
= {RECEIVE_COPY_RESULTS
, RCR_SA_OPERATING_PARAMETERS
,
1911 0x00, 0x00, 0x00, 0x00,
1912 0x00, 0x00, 0xff, 0xff,
1913 0xff, 0xff, 0x00, SCSI_CONTROL_MASK
},
1914 .enabled
= tcm_is_3pc_enabled
,
1917 static struct target_opcode_descriptor tcm_opcode_report_luns
= {
1918 .support
= SCSI_SUPPORT_FULL
,
1919 .opcode
= REPORT_LUNS
,
1921 .usage_bits
= {REPORT_LUNS
, 0x00, 0xff, 0x00,
1922 0x00, 0x00, 0xff, 0xff,
1923 0xff, 0xff, 0x00, SCSI_CONTROL_MASK
},
1926 static struct target_opcode_descriptor tcm_opcode_test_unit_ready
= {
1927 .support
= SCSI_SUPPORT_FULL
,
1928 .opcode
= TEST_UNIT_READY
,
1930 .usage_bits
= {TEST_UNIT_READY
, 0x00, 0x00, 0x00,
1931 0x00, SCSI_CONTROL_MASK
},
1934 static struct target_opcode_descriptor tcm_opcode_report_target_pgs
= {
1935 .support
= SCSI_SUPPORT_FULL
,
1936 .serv_action_valid
= 1,
1937 .opcode
= MAINTENANCE_IN
,
1938 .service_action
= MI_REPORT_TARGET_PGS
,
1940 .usage_bits
= {MAINTENANCE_IN
, 0xE0 | MI_REPORT_TARGET_PGS
, 0x00, 0x00,
1941 0x00, 0x00, 0xff, 0xff,
1942 0xff, 0xff, 0x00, SCSI_CONTROL_MASK
},
1945 static bool spc_rsoc_enabled(struct target_opcode_descriptor
*descr
,
1948 struct se_device
*dev
= cmd
->se_dev
;
1950 return dev
->dev_attrib
.emulate_rsoc
;
1953 static struct target_opcode_descriptor tcm_opcode_report_supp_opcodes
= {
1954 .support
= SCSI_SUPPORT_FULL
,
1955 .serv_action_valid
= 1,
1956 .opcode
= MAINTENANCE_IN
,
1957 .service_action
= MI_REPORT_SUPPORTED_OPERATION_CODES
,
1959 .usage_bits
= {MAINTENANCE_IN
, MI_REPORT_SUPPORTED_OPERATION_CODES
,
1961 0xff, 0xff, 0xff, 0xff,
1962 0xff, 0xff, 0x00, SCSI_CONTROL_MASK
},
1963 .enabled
= spc_rsoc_enabled
,
1966 static bool tcm_is_set_tpg_enabled(struct target_opcode_descriptor
*descr
,
1969 struct t10_alua_tg_pt_gp
*l_tg_pt_gp
;
1970 struct se_lun
*l_lun
= cmd
->se_lun
;
1973 l_tg_pt_gp
= rcu_dereference(l_lun
->lun_tg_pt_gp
);
1978 if (!(l_tg_pt_gp
->tg_pt_gp_alua_access_type
& TPGS_EXPLICIT_ALUA
)) {
1987 static struct target_opcode_descriptor tcm_opcode_set_tpg
= {
1988 .support
= SCSI_SUPPORT_FULL
,
1989 .serv_action_valid
= 1,
1990 .opcode
= MAINTENANCE_OUT
,
1991 .service_action
= MO_SET_TARGET_PGS
,
1993 .usage_bits
= {MAINTENANCE_OUT
, MO_SET_TARGET_PGS
, 0x00, 0x00,
1994 0x00, 0x00, 0xff, 0xff,
1995 0xff, 0xff, 0x00, SCSI_CONTROL_MASK
},
1996 .enabled
= tcm_is_set_tpg_enabled
,
1999 static struct target_opcode_descriptor
*tcm_supported_opcodes
[] = {
2005 &tcm_opcode_write10
,
2006 &tcm_opcode_write_verify10
,
2007 &tcm_opcode_write12
,
2008 &tcm_opcode_write16
,
2009 &tcm_opcode_write_verify16
,
2010 &tcm_opcode_write_same32
,
2011 &tcm_opcode_compare_write
,
2012 &tcm_opcode_read_capacity
,
2013 &tcm_opcode_read_capacity16
,
2014 &tcm_opcode_read_report_refferals
,
2015 &tcm_opcode_sync_cache
,
2016 &tcm_opcode_sync_cache16
,
2018 &tcm_opcode_write_same
,
2019 &tcm_opcode_write_same16
,
2021 &tcm_opcode_verify16
,
2022 &tcm_opcode_start_stop
,
2023 &tcm_opcode_mode_select
,
2024 &tcm_opcode_mode_select10
,
2025 &tcm_opcode_mode_sense
,
2026 &tcm_opcode_mode_sense10
,
2027 &tcm_opcode_pri_read_keys
,
2028 &tcm_opcode_pri_read_resrv
,
2029 &tcm_opcode_pri_read_caps
,
2030 &tcm_opcode_pri_read_full_status
,
2031 &tcm_opcode_pro_register
,
2032 &tcm_opcode_pro_reserve
,
2033 &tcm_opcode_pro_release
,
2034 &tcm_opcode_pro_clear
,
2035 &tcm_opcode_pro_preempt
,
2036 &tcm_opcode_pro_preempt_abort
,
2037 &tcm_opcode_pro_reg_ign_exist
,
2038 &tcm_opcode_pro_register_move
,
2039 &tcm_opcode_release
,
2040 &tcm_opcode_release10
,
2041 &tcm_opcode_reserve
,
2042 &tcm_opcode_reserve10
,
2043 &tcm_opcode_request_sense
,
2044 &tcm_opcode_inquiry
,
2045 &tcm_opcode_extended_copy_lid1
,
2046 &tcm_opcode_rcv_copy_res_op_params
,
2047 &tcm_opcode_report_luns
,
2048 &tcm_opcode_test_unit_ready
,
2049 &tcm_opcode_report_target_pgs
,
2050 &tcm_opcode_report_supp_opcodes
,
2051 &tcm_opcode_set_tpg
,
2055 spc_rsoc_encode_command_timeouts_descriptor(unsigned char *buf
, u8 ctdp
,
2056 struct target_opcode_descriptor
*descr
)
2061 put_unaligned_be16(0xa, buf
);
2062 buf
[3] = descr
->specific_timeout
;
2063 put_unaligned_be32(descr
->nominal_timeout
, &buf
[4]);
2064 put_unaligned_be32(descr
->recommended_timeout
, &buf
[8]);
2070 spc_rsoc_encode_command_descriptor(unsigned char *buf
, u8 ctdp
,
2071 struct target_opcode_descriptor
*descr
)
2075 buf
[0] = descr
->opcode
;
2077 put_unaligned_be16(descr
->service_action
, &buf
[2]);
2079 buf
[5] = (ctdp
<< 1) | descr
->serv_action_valid
;
2080 put_unaligned_be16(descr
->cdb_size
, &buf
[6]);
2082 td_size
= spc_rsoc_encode_command_timeouts_descriptor(&buf
[8], ctdp
,
2089 spc_rsoc_encode_one_command_descriptor(unsigned char *buf
, u8 ctdp
,
2090 struct target_opcode_descriptor
*descr
,
2091 struct se_device
*dev
)
2096 buf
[1] = (ctdp
<< 7) | SCSI_SUPPORT_NOT_SUPPORTED
;
2100 buf
[1] = (ctdp
<< 7) | SCSI_SUPPORT_FULL
;
2101 put_unaligned_be16(descr
->cdb_size
, &buf
[2]);
2102 memcpy(&buf
[4], descr
->usage_bits
, descr
->cdb_size
);
2103 if (descr
->update_usage_bits
)
2104 descr
->update_usage_bits(&buf
[4], dev
);
2106 td_size
= spc_rsoc_encode_command_timeouts_descriptor(
2107 &buf
[4 + descr
->cdb_size
], ctdp
, descr
);
2109 return 4 + descr
->cdb_size
+ td_size
;
2112 static sense_reason_t
2113 spc_rsoc_get_descr(struct se_cmd
*cmd
, struct target_opcode_descriptor
**opcode
)
2115 struct target_opcode_descriptor
*descr
;
2116 struct se_session
*sess
= cmd
->se_sess
;
2117 unsigned char *cdb
= cmd
->t_task_cdb
;
2118 u8 opts
= cdb
[2] & 0x3;
2119 u8 requested_opcode
;
2123 requested_opcode
= cdb
[3];
2124 requested_sa
= ((u16
)cdb
[4]) << 8 | cdb
[5];
2128 pr_debug("TARGET_CORE[%s]: Invalid REPORT SUPPORTED OPERATION CODES"
2129 " with unsupported REPORTING OPTIONS %#x for 0x%08llx from %s\n",
2130 cmd
->se_tfo
->fabric_name
, opts
,
2131 cmd
->se_lun
->unpacked_lun
,
2132 sess
->se_node_acl
->initiatorname
);
2133 return TCM_INVALID_CDB_FIELD
;
2136 for (i
= 0; i
< ARRAY_SIZE(tcm_supported_opcodes
); i
++) {
2137 descr
= tcm_supported_opcodes
[i
];
2138 if (descr
->opcode
!= requested_opcode
)
2144 * If the REQUESTED OPERATION CODE field specifies an
2145 * operation code for which the device server implements
2146 * service actions, then the device server shall
2147 * terminate the command with CHECK CONDITION status,
2148 * with the sense key set to ILLEGAL REQUEST, and the
2149 * additional sense code set to INVALID FIELD IN CDB
2151 if (descr
->serv_action_valid
)
2152 return TCM_INVALID_CDB_FIELD
;
2154 if (!descr
->enabled
|| descr
->enabled(descr
, cmd
))
2159 * If the REQUESTED OPERATION CODE field specifies an
2160 * operation code for which the device server does not
2161 * implement service actions, then the device server
2162 * shall terminate the command with CHECK CONDITION
2163 * status, with the sense key set to ILLEGAL REQUEST,
2164 * and the additional sense code set to INVALID FIELD IN CDB.
2166 if (descr
->serv_action_valid
&&
2167 descr
->service_action
== requested_sa
) {
2168 if (!descr
->enabled
|| descr
->enabled(descr
,
2171 } else if (!descr
->serv_action_valid
)
2172 return TCM_INVALID_CDB_FIELD
;
2176 * The command support data for the operation code and
2177 * service action a specified in the REQUESTED OPERATION
2178 * CODE field and REQUESTED SERVICE ACTION field shall
2179 * be returned in the one_command parameter data format.
2181 if (descr
->service_action
== requested_sa
)
2182 if (!descr
->enabled
|| descr
->enabled(descr
,
2192 static sense_reason_t
2193 spc_emulate_report_supp_op_codes(struct se_cmd
*cmd
)
2195 int descr_num
= ARRAY_SIZE(tcm_supported_opcodes
);
2196 struct target_opcode_descriptor
*descr
= NULL
;
2197 unsigned char *cdb
= cmd
->t_task_cdb
;
2198 u8 rctd
= (cdb
[2] >> 7) & 0x1;
2199 unsigned char *buf
= NULL
;
2200 int response_length
= 0;
2201 u8 opts
= cdb
[2] & 0x3;
2202 unsigned char *rbuf
;
2203 sense_reason_t ret
= 0;
2206 if (!cmd
->se_dev
->dev_attrib
.emulate_rsoc
)
2207 return TCM_UNSUPPORTED_SCSI_OPCODE
;
2209 rbuf
= transport_kmap_data_sg(cmd
);
2210 if (cmd
->data_length
&& !rbuf
) {
2211 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2216 response_length
= 4 + (8 + rctd
* 12) * descr_num
;
2218 ret
= spc_rsoc_get_descr(cmd
, &descr
);
2223 response_length
= 4 + descr
->cdb_size
+ rctd
* 12;
2225 response_length
= 2;
2228 buf
= kzalloc(response_length
, GFP_KERNEL
);
2230 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2233 response_length
= 0;
2236 response_length
+= 4;
2238 for (i
= 0; i
< ARRAY_SIZE(tcm_supported_opcodes
); i
++) {
2239 descr
= tcm_supported_opcodes
[i
];
2240 if (descr
->enabled
&& !descr
->enabled(descr
, cmd
))
2243 response_length
+= spc_rsoc_encode_command_descriptor(
2244 &buf
[response_length
], rctd
, descr
);
2246 put_unaligned_be32(response_length
- 3, buf
);
2248 response_length
= spc_rsoc_encode_one_command_descriptor(
2249 &buf
[response_length
], rctd
, descr
,
2253 memcpy(rbuf
, buf
, min_t(u32
, response_length
, cmd
->data_length
));
2256 transport_kunmap_data_sg(cmd
);
2259 target_complete_cmd_with_length(cmd
, SAM_STAT_GOOD
, response_length
);
2264 spc_parse_cdb(struct se_cmd
*cmd
, unsigned int *size
)
2266 struct se_device
*dev
= cmd
->se_dev
;
2267 unsigned char *cdb
= cmd
->t_task_cdb
;
2274 if (!dev
->dev_attrib
.emulate_pr
)
2275 return TCM_UNSUPPORTED_SCSI_OPCODE
;
2277 if (dev
->transport_flags
& TRANSPORT_FLAG_PASSTHROUGH_PGR
)
2278 return TCM_UNSUPPORTED_SCSI_OPCODE
;
2280 case PERSISTENT_RESERVE_IN
:
2281 case PERSISTENT_RESERVE_OUT
:
2282 if (!dev
->dev_attrib
.emulate_pr
)
2283 return TCM_UNSUPPORTED_SCSI_OPCODE
;
2290 cmd
->execute_cmd
= spc_emulate_modeselect
;
2292 case MODE_SELECT_10
:
2293 *size
= get_unaligned_be16(&cdb
[7]);
2294 cmd
->execute_cmd
= spc_emulate_modeselect
;
2298 cmd
->execute_cmd
= spc_emulate_modesense
;
2301 *size
= get_unaligned_be16(&cdb
[7]);
2302 cmd
->execute_cmd
= spc_emulate_modesense
;
2306 *size
= get_unaligned_be16(&cdb
[7]);
2308 case PERSISTENT_RESERVE_IN
:
2309 *size
= get_unaligned_be16(&cdb
[7]);
2310 cmd
->execute_cmd
= target_scsi3_emulate_pr_in
;
2312 case PERSISTENT_RESERVE_OUT
:
2313 *size
= get_unaligned_be32(&cdb
[5]);
2314 cmd
->execute_cmd
= target_scsi3_emulate_pr_out
;
2318 if (cdb
[0] == RELEASE_10
)
2319 *size
= get_unaligned_be16(&cdb
[7]);
2321 *size
= cmd
->data_length
;
2323 cmd
->execute_cmd
= target_scsi2_reservation_release
;
2328 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2329 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2331 if (cdb
[0] == RESERVE_10
)
2332 *size
= get_unaligned_be16(&cdb
[7]);
2334 *size
= cmd
->data_length
;
2336 cmd
->execute_cmd
= target_scsi2_reservation_reserve
;
2340 cmd
->execute_cmd
= spc_emulate_request_sense
;
2343 *size
= get_unaligned_be16(&cdb
[3]);
2346 * Do implicit HEAD_OF_QUEUE processing for INQUIRY.
2347 * See spc4r17 section 5.3
2349 cmd
->sam_task_attr
= TCM_HEAD_TAG
;
2350 cmd
->execute_cmd
= spc_emulate_inquiry
;
2352 case SECURITY_PROTOCOL_IN
:
2353 case SECURITY_PROTOCOL_OUT
:
2354 *size
= get_unaligned_be32(&cdb
[6]);
2357 *size
= get_unaligned_be32(&cdb
[10]);
2358 cmd
->execute_cmd
= target_do_xcopy
;
2360 case RECEIVE_COPY_RESULTS
:
2361 *size
= get_unaligned_be32(&cdb
[10]);
2362 cmd
->execute_cmd
= target_do_receive_copy_results
;
2364 case READ_ATTRIBUTE
:
2365 case WRITE_ATTRIBUTE
:
2366 *size
= get_unaligned_be32(&cdb
[10]);
2368 case RECEIVE_DIAGNOSTIC
:
2369 case SEND_DIAGNOSTIC
:
2370 *size
= get_unaligned_be16(&cdb
[3]);
2373 *size
= get_unaligned_be24(&cdb
[6]);
2376 cmd
->execute_cmd
= spc_emulate_report_luns
;
2377 *size
= get_unaligned_be32(&cdb
[6]);
2379 * Do implicit HEAD_OF_QUEUE processing for REPORT_LUNS
2380 * See spc4r17 section 5.3
2382 cmd
->sam_task_attr
= TCM_HEAD_TAG
;
2384 case TEST_UNIT_READY
:
2385 cmd
->execute_cmd
= spc_emulate_testunitready
;
2388 case MAINTENANCE_IN
:
2389 if (dev
->transport
->get_device_type(dev
) != TYPE_ROM
) {
2391 * MAINTENANCE_IN from SCC-2
2392 * Check for emulated MI_REPORT_TARGET_PGS
2394 if ((cdb
[1] & 0x1f) == MI_REPORT_TARGET_PGS
) {
2396 target_emulate_report_target_port_groups
;
2398 if ((cdb
[1] & 0x1f) ==
2399 MI_REPORT_SUPPORTED_OPERATION_CODES
)
2401 spc_emulate_report_supp_op_codes
;
2402 *size
= get_unaligned_be32(&cdb
[6]);
2405 * GPCMD_SEND_KEY from multi media commands
2407 *size
= get_unaligned_be16(&cdb
[8]);
2410 case MAINTENANCE_OUT
:
2411 if (dev
->transport
->get_device_type(dev
) != TYPE_ROM
) {
2413 * MAINTENANCE_OUT from SCC-2
2414 * Check for emulated MO_SET_TARGET_PGS.
2416 if (cdb
[1] == MO_SET_TARGET_PGS
) {
2418 target_emulate_set_target_port_groups
;
2420 *size
= get_unaligned_be32(&cdb
[6]);
2423 * GPCMD_SEND_KEY from multi media commands
2425 *size
= get_unaligned_be16(&cdb
[8]);
2429 return TCM_UNSUPPORTED_SCSI_OPCODE
;
2434 EXPORT_SYMBOL(spc_parse_cdb
);