2 * CDB emulation for non-READ/WRITE commands.
4 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
5 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
6 * Copyright (c) 2007-2010 Rising Tide Systems
7 * Copyright (c) 2008-2010 Linux-iSCSI.org
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <asm/unaligned.h>
27 #include <scsi/scsi.h>
29 #include <target/target_core_base.h>
30 #include <target/target_core_transport.h>
31 #include <target/target_core_fabric_ops.h>
32 #include "target_core_ua.h"
35 target_fill_alua_data(struct se_port
*port
, unsigned char *buf
)
37 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
38 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
41 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
46 * Set TPGS field for explict and/or implict ALUA access type
49 * See spc4r17 section 6.4.2 Table 135
53 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
57 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
58 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
60 buf
[5] |= tg_pt_gp
->tg_pt_gp_alua_access_type
;
61 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
65 target_emulate_inquiry_std(struct se_cmd
*cmd
)
67 struct se_lun
*lun
= SE_LUN(cmd
);
68 struct se_device
*dev
= SE_DEV(cmd
);
69 unsigned char *buf
= cmd
->t_task
->t_task_buf
;
72 * Make sure we at least have 6 bytes of INQUIRY response
73 * payload going back for EVPD=0
75 if (cmd
->data_length
< 6) {
76 printk(KERN_ERR
"SCSI Inquiry payload length: %u"
77 " too small for EVPD=0\n", cmd
->data_length
);
81 buf
[0] = dev
->transport
->get_device_type(dev
);
82 if (buf
[0] == TYPE_TAPE
)
84 buf
[2] = dev
->transport
->get_device_rev(dev
);
87 * Enable SCCS and TPGS fields for Emulated ALUA
89 if (T10_ALUA(dev
->se_sub_dev
)->alua_type
== SPC3_ALUA_EMULATED
)
90 target_fill_alua_data(lun
->lun_sep
, buf
);
92 if (cmd
->data_length
< 8) {
93 buf
[4] = 1; /* Set additional length to 1 */
97 buf
[7] = 0x32; /* Sync=1 and CmdQue=1 */
100 * Do not include vendor, product, reversion info in INQUIRY
101 * response payload for cdbs with a small allocation length.
103 if (cmd
->data_length
< 36) {
104 buf
[4] = 3; /* Set additional length to 3 */
108 snprintf((unsigned char *)&buf
[8], 8, "LIO-ORG");
109 snprintf((unsigned char *)&buf
[16], 16, "%s",
110 &DEV_T10_WWN(dev
)->model
[0]);
111 snprintf((unsigned char *)&buf
[32], 4, "%s",
112 &DEV_T10_WWN(dev
)->revision
[0]);
113 buf
[4] = 31; /* Set additional length to 31 */
117 /* supported vital product data pages */
119 target_emulate_evpd_00(struct se_cmd
*cmd
, unsigned char *buf
)
122 if (cmd
->data_length
< 8)
127 * Only report the INQUIRY EVPD=1 pages after a valid NAA
128 * Registered Extended LUN WWN has been set via ConfigFS
129 * during device creation/restart.
131 if (SE_DEV(cmd
)->se_sub_dev
->su_dev_flags
&
132 SDF_EMULATED_VPD_UNIT_SERIAL
) {
142 /* unit serial number */
144 target_emulate_evpd_80(struct se_cmd
*cmd
, unsigned char *buf
)
146 struct se_device
*dev
= SE_DEV(cmd
);
150 if (dev
->se_sub_dev
->su_dev_flags
&
151 SDF_EMULATED_VPD_UNIT_SERIAL
) {
155 strlen(&DEV_T10_WWN(dev
)->unit_serial
[0]);
156 unit_serial_len
++; /* For NULL Terminator */
158 if (((len
+ 4) + unit_serial_len
) > cmd
->data_length
) {
159 len
+= unit_serial_len
;
160 buf
[2] = ((len
>> 8) & 0xff);
161 buf
[3] = (len
& 0xff);
164 len
+= sprintf((unsigned char *)&buf
[4], "%s",
165 &DEV_T10_WWN(dev
)->unit_serial
[0]);
166 len
++; /* Extra Byte for NULL Terminator */
173 * Device identification VPD, for a complete list of
174 * DESIGNATOR TYPEs see spc4r17 Table 459.
177 target_emulate_evpd_83(struct se_cmd
*cmd
, unsigned char *buf
)
179 struct se_device
*dev
= SE_DEV(cmd
);
180 struct se_lun
*lun
= SE_LUN(cmd
);
181 struct se_port
*port
= NULL
;
182 struct se_portal_group
*tpg
= NULL
;
183 struct t10_alua_lu_gp_member
*lu_gp_mem
;
184 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
185 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
186 unsigned char binary
, binary_new
;
187 unsigned char *prod
= &DEV_T10_WWN(dev
)->model
[0];
189 u32 unit_serial_len
, off
= 0;
197 * NAA IEEE Registered Extended Assigned designator format, see
198 * spc4r17 section 7.7.3.6.5
200 * We depend upon a target_core_mod/ConfigFS provided
201 * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
202 * value in order to return the NAA id.
204 if (!(dev
->se_sub_dev
->su_dev_flags
& SDF_EMULATED_VPD_UNIT_SERIAL
))
205 goto check_t10_vend_desc
;
207 if (off
+ 20 > cmd
->data_length
)
208 goto check_t10_vend_desc
;
210 /* CODE SET == Binary */
213 /* Set ASSOICATION == addressed logical unit: 0)b */
216 /* Identifier/Designator type == NAA identifier */
220 /* Identifier/Designator length */
224 * Start NAA IEEE Registered Extended Identifier/Designator
226 buf
[off
++] = (0x6 << 4);
229 * Use OpenFabrics IEEE Company ID: 00 14 05
233 buf
[off
] = (0x5 << 4);
236 * Return ConfigFS Unit Serial Number information for
237 * VENDOR_SPECIFIC_IDENTIFIER and
238 * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
240 binary
= transport_asciihex_to_binaryhex(
241 &DEV_T10_WWN(dev
)->unit_serial
[0]);
242 buf
[off
++] |= (binary
& 0xf0) >> 4;
243 for (i
= 0; i
< 24; i
+= 2) {
244 binary_new
= transport_asciihex_to_binaryhex(
245 &DEV_T10_WWN(dev
)->unit_serial
[i
+2]);
246 buf
[off
] = (binary
& 0x0f) << 4;
247 buf
[off
++] |= (binary_new
& 0xf0) >> 4;
255 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
257 id_len
= 8; /* For Vendor field */
258 prod_len
= 4; /* For VPD Header */
259 prod_len
+= 8; /* For Vendor field */
260 prod_len
+= strlen(prod
);
261 prod_len
++; /* For : */
263 if (dev
->se_sub_dev
->su_dev_flags
&
264 SDF_EMULATED_VPD_UNIT_SERIAL
) {
266 strlen(&DEV_T10_WWN(dev
)->unit_serial
[0]);
267 unit_serial_len
++; /* For NULL Terminator */
269 if ((len
+ (id_len
+ 4) +
270 (prod_len
+ unit_serial_len
)) >
272 len
+= (prod_len
+ unit_serial_len
);
275 id_len
+= sprintf((unsigned char *)&buf
[off
+12],
277 &DEV_T10_WWN(dev
)->unit_serial
[0]);
279 buf
[off
] = 0x2; /* ASCII */
280 buf
[off
+1] = 0x1; /* T10 Vendor ID */
282 memcpy((unsigned char *)&buf
[off
+4], "LIO-ORG", 8);
283 /* Extra Byte for NULL Terminator */
285 /* Identifier Length */
287 /* Header size for Designation descriptor */
291 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
296 struct t10_alua_lu_gp
*lu_gp
;
297 u32 padding
, scsi_name_len
;
304 * Relative target port identifer, see spc4r17
307 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
308 * section 7.5.1 Table 362
310 if (((len
+ 4) + 8) > cmd
->data_length
) {
315 (TPG_TFO(tpg
)->get_fabric_proto_ident(tpg
) << 4);
316 buf
[off
++] |= 0x1; /* CODE SET == Binary */
317 buf
[off
] = 0x80; /* Set PIV=1 */
318 /* Set ASSOICATION == target port: 01b */
320 /* DESIGNATOR TYPE == Relative target port identifer */
322 off
++; /* Skip over Reserved */
323 buf
[off
++] = 4; /* DESIGNATOR LENGTH */
324 /* Skip over Obsolete field in RTPI payload
327 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
328 buf
[off
++] = (port
->sep_rtpi
& 0xff);
329 len
+= 8; /* Header size + Designation descriptor */
331 * Target port group identifier, see spc4r17
334 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
335 * section 7.5.1 Table 362
338 if (T10_ALUA(dev
->se_sub_dev
)->alua_type
!=
340 goto check_scsi_name
;
342 if (((len
+ 4) + 8) > cmd
->data_length
) {
346 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
350 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
351 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
353 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
356 tg_pt_gp_id
= tg_pt_gp
->tg_pt_gp_id
;
357 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
360 (TPG_TFO(tpg
)->get_fabric_proto_ident(tpg
) << 4);
361 buf
[off
++] |= 0x1; /* CODE SET == Binary */
362 buf
[off
] = 0x80; /* Set PIV=1 */
363 /* Set ASSOICATION == target port: 01b */
365 /* DESIGNATOR TYPE == Target port group identifier */
367 off
++; /* Skip over Reserved */
368 buf
[off
++] = 4; /* DESIGNATOR LENGTH */
369 off
+= 2; /* Skip over Reserved Field */
370 buf
[off
++] = ((tg_pt_gp_id
>> 8) & 0xff);
371 buf
[off
++] = (tg_pt_gp_id
& 0xff);
372 len
+= 8; /* Header size + Designation descriptor */
374 * Logical Unit Group identifier, see spc4r17
378 if (((len
+ 4) + 8) > cmd
->data_length
) {
380 goto check_scsi_name
;
382 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
384 goto check_scsi_name
;
386 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
387 lu_gp
= lu_gp_mem
->lu_gp
;
389 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
390 goto check_scsi_name
;
392 lu_gp_id
= lu_gp
->lu_gp_id
;
393 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
395 buf
[off
++] |= 0x1; /* CODE SET == Binary */
396 /* DESIGNATOR TYPE == Logical Unit Group identifier */
398 off
++; /* Skip over Reserved */
399 buf
[off
++] = 4; /* DESIGNATOR LENGTH */
400 off
+= 2; /* Skip over Reserved Field */
401 buf
[off
++] = ((lu_gp_id
>> 8) & 0xff);
402 buf
[off
++] = (lu_gp_id
& 0xff);
403 len
+= 8; /* Header size + Designation descriptor */
405 * SCSI name string designator, see spc4r17
408 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
409 * section 7.5.1 Table 362
412 scsi_name_len
= strlen(TPG_TFO(tpg
)->tpg_get_wwn(tpg
));
413 /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
415 /* Check for 4-byte padding */
416 padding
= ((-scsi_name_len
) & 3);
418 scsi_name_len
+= padding
;
419 /* Header size + Designation descriptor */
422 if (((len
+ 4) + scsi_name_len
) > cmd
->data_length
) {
423 len
+= scsi_name_len
;
427 (TPG_TFO(tpg
)->get_fabric_proto_ident(tpg
) << 4);
428 buf
[off
++] |= 0x3; /* CODE SET == UTF-8 */
429 buf
[off
] = 0x80; /* Set PIV=1 */
430 /* Set ASSOICATION == target port: 01b */
432 /* DESIGNATOR TYPE == SCSI name string */
434 off
+= 2; /* Skip over Reserved and length */
436 * SCSI name string identifer containing, $FABRIC_MOD
437 * dependent information. For LIO-Target and iSCSI
438 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
441 tpgt
= TPG_TFO(tpg
)->tpg_get_tag(tpg
);
442 scsi_name_len
= sprintf(&buf
[off
], "%s,t,0x%04x",
443 TPG_TFO(tpg
)->tpg_get_wwn(tpg
), tpgt
);
444 scsi_name_len
+= 1 /* Include NULL terminator */;
446 * The null-terminated, null-padded (see 4.4.2) SCSI
447 * NAME STRING field contains a UTF-8 format string.
448 * The number of bytes in the SCSI NAME STRING field
449 * (i.e., the value in the DESIGNATOR LENGTH field)
450 * shall be no larger than 256 and shall be a multiple
454 scsi_name_len
+= padding
;
456 buf
[off
-1] = scsi_name_len
;
457 off
+= scsi_name_len
;
458 /* Header size + Designation descriptor */
459 len
+= (scsi_name_len
+ 4);
462 buf
[2] = ((len
>> 8) & 0xff);
463 buf
[3] = (len
& 0xff); /* Page Length for VPD 0x83 */
467 /* Extended INQUIRY Data VPD Page */
469 target_emulate_evpd_86(struct se_cmd
*cmd
, unsigned char *buf
)
471 if (cmd
->data_length
< 60)
476 /* Set HEADSUP, ORDSUP, SIMPSUP */
479 /* If WriteCache emulation is enabled, set V_SUP */
480 if (DEV_ATTRIB(SE_DEV(cmd
))->emulate_write_cache
> 0)
485 /* Block Limits VPD page */
487 target_emulate_evpd_b0(struct se_cmd
*cmd
, unsigned char *buf
)
489 struct se_device
*dev
= SE_DEV(cmd
);
493 * Following sbc3r22 section 6.5.3 Block Limits VPD page, when
494 * emulate_tpu=1 or emulate_tpws=1 we will be expect a
495 * different page length for Thin Provisioning.
497 if (DEV_ATTRIB(dev
)->emulate_tpu
|| DEV_ATTRIB(dev
)->emulate_tpws
)
500 if (cmd
->data_length
< (0x10 + 4)) {
501 printk(KERN_INFO
"Received data_length: %u"
502 " too small for EVPD 0xb0\n",
507 if (have_tp
&& cmd
->data_length
< (0x3c + 4)) {
508 printk(KERN_INFO
"Received data_length: %u"
509 " too small for TPE=1 EVPD 0xb0\n",
514 buf
[0] = dev
->transport
->get_device_type(dev
);
516 buf
[3] = have_tp
? 0x3c : 0x10;
519 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
521 put_unaligned_be16(1, &buf
[6]);
524 * Set MAXIMUM TRANSFER LENGTH
526 put_unaligned_be32(DEV_ATTRIB(dev
)->max_sectors
, &buf
[8]);
529 * Set OPTIMAL TRANSFER LENGTH
531 put_unaligned_be32(DEV_ATTRIB(dev
)->optimal_sectors
, &buf
[12]);
534 * Exit now if we don't support TP or the initiator sent a too
537 if (!have_tp
|| cmd
->data_length
< (0x3c + 4))
541 * Set MAXIMUM UNMAP LBA COUNT
543 put_unaligned_be32(DEV_ATTRIB(dev
)->max_unmap_lba_count
, &buf
[20]);
546 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
548 put_unaligned_be32(DEV_ATTRIB(dev
)->max_unmap_block_desc_count
,
552 * Set OPTIMAL UNMAP GRANULARITY
554 put_unaligned_be32(DEV_ATTRIB(dev
)->unmap_granularity
, &buf
[28]);
557 * UNMAP GRANULARITY ALIGNMENT
559 put_unaligned_be32(DEV_ATTRIB(dev
)->unmap_granularity_alignment
,
561 if (DEV_ATTRIB(dev
)->unmap_granularity_alignment
!= 0)
562 buf
[32] |= 0x80; /* Set the UGAVALID bit */
567 /* Thin Provisioning VPD */
569 target_emulate_evpd_b2(struct se_cmd
*cmd
, unsigned char *buf
)
571 struct se_device
*dev
= SE_DEV(cmd
);
574 * From sbc3r22 section 6.5.4 Thin Provisioning VPD page:
576 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
577 * zero, then the page length shall be set to 0004h. If the DP bit
578 * is set to one, then the page length shall be set to the value
579 * defined in table 162.
581 buf
[0] = dev
->transport
->get_device_type(dev
);
585 * Set Hardcoded length mentioned above for DP=0
587 put_unaligned_be16(0x0004, &buf
[2]);
590 * The THRESHOLD EXPONENT field indicates the threshold set size in
591 * LBAs as a power of 2 (i.e., the threshold set size is equal to
592 * 2(threshold exponent)).
594 * Note that this is currently set to 0x00 as mkp says it will be
595 * changing again. We can enable this once it has settled in T10
596 * and is actually used by Linux/SCSI ML code.
601 * A TPU bit set to one indicates that the device server supports
602 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
603 * that the device server does not support the UNMAP command.
605 if (DEV_ATTRIB(dev
)->emulate_tpu
!= 0)
609 * A TPWS bit set to one indicates that the device server supports
610 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
611 * A TPWS bit set to zero indicates that the device server does not
612 * support the use of the WRITE SAME (16) command to unmap LBAs.
614 if (DEV_ATTRIB(dev
)->emulate_tpws
!= 0)
621 target_emulate_inquiry(struct se_cmd
*cmd
)
623 struct se_device
*dev
= SE_DEV(cmd
);
624 unsigned char *buf
= cmd
->t_task
->t_task_buf
;
625 unsigned char *cdb
= cmd
->t_task
->t_task_cdb
;
628 return target_emulate_inquiry_std(cmd
);
631 * Make sure we at least have 4 bytes of INQUIRY response
632 * payload for 0x00 going back for EVPD=1. Note that 0x80
633 * and 0x83 will check for enough payload data length and
634 * jump to set_len: label when there is not enough inquiry EVPD
635 * payload length left for the next outgoing EVPD metadata
637 if (cmd
->data_length
< 4) {
638 printk(KERN_ERR
"SCSI Inquiry payload length: %u"
639 " too small for EVPD=1\n", cmd
->data_length
);
642 buf
[0] = dev
->transport
->get_device_type(dev
);
646 return target_emulate_evpd_00(cmd
, buf
);
648 return target_emulate_evpd_80(cmd
, buf
);
650 return target_emulate_evpd_83(cmd
, buf
);
652 return target_emulate_evpd_86(cmd
, buf
);
654 return target_emulate_evpd_b0(cmd
, buf
);
656 return target_emulate_evpd_b2(cmd
, buf
);
658 printk(KERN_ERR
"Unknown VPD Code: 0x%02x\n", cdb
[2]);
666 target_emulate_readcapacity(struct se_cmd
*cmd
)
668 struct se_device
*dev
= SE_DEV(cmd
);
669 unsigned char *buf
= cmd
->t_task
->t_task_buf
;
670 u32 blocks
= dev
->transport
->get_blocks(dev
);
672 buf
[0] = (blocks
>> 24) & 0xff;
673 buf
[1] = (blocks
>> 16) & 0xff;
674 buf
[2] = (blocks
>> 8) & 0xff;
675 buf
[3] = blocks
& 0xff;
676 buf
[4] = (DEV_ATTRIB(dev
)->block_size
>> 24) & 0xff;
677 buf
[5] = (DEV_ATTRIB(dev
)->block_size
>> 16) & 0xff;
678 buf
[6] = (DEV_ATTRIB(dev
)->block_size
>> 8) & 0xff;
679 buf
[7] = DEV_ATTRIB(dev
)->block_size
& 0xff;
681 * Set max 32-bit blocks to signal SERVICE ACTION READ_CAPACITY_16
683 if (DEV_ATTRIB(dev
)->emulate_tpu
|| DEV_ATTRIB(dev
)->emulate_tpws
)
684 put_unaligned_be32(0xFFFFFFFF, &buf
[0]);
690 target_emulate_readcapacity_16(struct se_cmd
*cmd
)
692 struct se_device
*dev
= SE_DEV(cmd
);
693 unsigned char *buf
= cmd
->t_task
->t_task_buf
;
694 unsigned long long blocks
= dev
->transport
->get_blocks(dev
);
696 buf
[0] = (blocks
>> 56) & 0xff;
697 buf
[1] = (blocks
>> 48) & 0xff;
698 buf
[2] = (blocks
>> 40) & 0xff;
699 buf
[3] = (blocks
>> 32) & 0xff;
700 buf
[4] = (blocks
>> 24) & 0xff;
701 buf
[5] = (blocks
>> 16) & 0xff;
702 buf
[6] = (blocks
>> 8) & 0xff;
703 buf
[7] = blocks
& 0xff;
704 buf
[8] = (DEV_ATTRIB(dev
)->block_size
>> 24) & 0xff;
705 buf
[9] = (DEV_ATTRIB(dev
)->block_size
>> 16) & 0xff;
706 buf
[10] = (DEV_ATTRIB(dev
)->block_size
>> 8) & 0xff;
707 buf
[11] = DEV_ATTRIB(dev
)->block_size
& 0xff;
709 * Set Thin Provisioning Enable bit following sbc3r22 in section
710 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
712 if (DEV_ATTRIB(dev
)->emulate_tpu
|| DEV_ATTRIB(dev
)->emulate_tpws
)
719 target_modesense_rwrecovery(unsigned char *p
)
728 target_modesense_control(struct se_device
*dev
, unsigned char *p
)
734 * From spc4r17, section 7.4.6 Control mode Page
736 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
738 * 00b: The logical unit shall clear any unit attention condition
739 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
740 * status and shall not establish a unit attention condition when a com-
741 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
744 * 10b: The logical unit shall not clear any unit attention condition
745 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
746 * status and shall not establish a unit attention condition when
747 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
750 * 11b a The logical unit shall not clear any unit attention condition
751 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
752 * status and shall establish a unit attention condition for the
753 * initiator port associated with the I_T nexus on which the BUSY,
754 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
755 * Depending on the status, the additional sense code shall be set to
756 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
757 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
758 * command, a unit attention condition shall be established only once
759 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
760 * to the number of commands completed with one of those status codes.
762 p
[4] = (DEV_ATTRIB(dev
)->emulate_ua_intlck_ctrl
== 2) ? 0x30 :
763 (DEV_ATTRIB(dev
)->emulate_ua_intlck_ctrl
== 1) ? 0x20 : 0x00;
765 * From spc4r17, section 7.4.6 Control mode Page
767 * Task Aborted Status (TAS) bit set to zero.
769 * A task aborted status (TAS) bit set to zero specifies that aborted
770 * tasks shall be terminated by the device server without any response
771 * to the application client. A TAS bit set to one specifies that tasks
772 * aborted by the actions of an I_T nexus other than the I_T nexus on
773 * which the command was received shall be completed with TASK ABORTED
774 * status (see SAM-4).
776 p
[5] = (DEV_ATTRIB(dev
)->emulate_tas
) ? 0x40 : 0x00;
785 target_modesense_caching(struct se_device
*dev
, unsigned char *p
)
789 if (DEV_ATTRIB(dev
)->emulate_write_cache
> 0)
790 p
[2] = 0x04; /* Write Cache Enable */
791 p
[12] = 0x20; /* Disabled Read Ahead */
797 target_modesense_write_protect(unsigned char *buf
, int type
)
800 * I believe that the WP bit (bit 7) in the mode header is the same for
807 buf
[0] |= 0x80; /* WP bit */
813 target_modesense_dpofua(unsigned char *buf
, int type
)
817 buf
[0] |= 0x10; /* DPOFUA bit */
825 target_emulate_modesense(struct se_cmd
*cmd
, int ten
)
827 struct se_device
*dev
= SE_DEV(cmd
);
828 char *cdb
= cmd
->t_task
->t_task_cdb
;
829 unsigned char *rbuf
= cmd
->t_task
->t_task_buf
;
830 int type
= dev
->transport
->get_device_type(dev
);
831 int offset
= (ten
) ? 8 : 4;
833 unsigned char buf
[SE_MODE_PAGE_BUF
];
835 memset(buf
, 0, SE_MODE_PAGE_BUF
);
837 switch (cdb
[2] & 0x3f) {
839 length
= target_modesense_rwrecovery(&buf
[offset
]);
842 length
= target_modesense_caching(dev
, &buf
[offset
]);
845 length
= target_modesense_control(dev
, &buf
[offset
]);
848 length
= target_modesense_rwrecovery(&buf
[offset
]);
849 length
+= target_modesense_caching(dev
, &buf
[offset
+length
]);
850 length
+= target_modesense_control(dev
, &buf
[offset
+length
]);
853 printk(KERN_ERR
"Got Unknown Mode Page: 0x%02x\n",
855 return PYX_TRANSPORT_UNKNOWN_MODE_PAGE
;
861 buf
[0] = (offset
>> 8) & 0xff;
862 buf
[1] = offset
& 0xff;
864 if ((SE_LUN(cmd
)->lun_access
& TRANSPORT_LUNFLAGS_READ_ONLY
) ||
866 (cmd
->se_deve
->lun_flags
& TRANSPORT_LUNFLAGS_READ_ONLY
)))
867 target_modesense_write_protect(&buf
[3], type
);
869 if ((DEV_ATTRIB(dev
)->emulate_write_cache
> 0) &&
870 (DEV_ATTRIB(dev
)->emulate_fua_write
> 0))
871 target_modesense_dpofua(&buf
[3], type
);
873 if ((offset
+ 2) > cmd
->data_length
)
874 offset
= cmd
->data_length
;
878 buf
[0] = offset
& 0xff;
880 if ((SE_LUN(cmd
)->lun_access
& TRANSPORT_LUNFLAGS_READ_ONLY
) ||
882 (cmd
->se_deve
->lun_flags
& TRANSPORT_LUNFLAGS_READ_ONLY
)))
883 target_modesense_write_protect(&buf
[2], type
);
885 if ((DEV_ATTRIB(dev
)->emulate_write_cache
> 0) &&
886 (DEV_ATTRIB(dev
)->emulate_fua_write
> 0))
887 target_modesense_dpofua(&buf
[2], type
);
889 if ((offset
+ 1) > cmd
->data_length
)
890 offset
= cmd
->data_length
;
892 memcpy(rbuf
, buf
, offset
);
898 target_emulate_request_sense(struct se_cmd
*cmd
)
900 unsigned char *cdb
= cmd
->t_task
->t_task_cdb
;
901 unsigned char *buf
= cmd
->t_task
->t_task_buf
;
902 u8 ua_asc
= 0, ua_ascq
= 0;
905 printk(KERN_ERR
"REQUEST_SENSE description emulation not"
907 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
909 if (!(core_scsi3_ua_clear_for_request_sense(cmd
, &ua_asc
, &ua_ascq
))) {
911 * CURRENT ERROR, UNIT ATTENTION
914 buf
[SPC_SENSE_KEY_OFFSET
] = UNIT_ATTENTION
;
916 * Make sure request data length is enough for additional
919 if (cmd
->data_length
<= 18) {
924 * The Additional Sense Code (ASC) from the UNIT ATTENTION
926 buf
[SPC_ASC_KEY_OFFSET
] = ua_asc
;
927 buf
[SPC_ASCQ_KEY_OFFSET
] = ua_ascq
;
931 * CURRENT ERROR, NO SENSE
934 buf
[SPC_SENSE_KEY_OFFSET
] = NO_SENSE
;
936 * Make sure request data length is enough for additional
939 if (cmd
->data_length
<= 18) {
944 * NO ADDITIONAL SENSE INFORMATION
946 buf
[SPC_ASC_KEY_OFFSET
] = 0x00;
954 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
955 * Note this is not used for TCM/pSCSI passthrough
958 target_emulate_unmap(struct se_task
*task
)
960 struct se_cmd
*cmd
= TASK_CMD(task
);
961 struct se_device
*dev
= SE_DEV(cmd
);
962 unsigned char *buf
= cmd
->t_task
->t_task_buf
, *ptr
= NULL
;
963 unsigned char *cdb
= &cmd
->t_task
->t_task_cdb
[0];
965 unsigned int size
= cmd
->data_length
, range
;
967 unsigned short dl
, bd_dl
;
969 /* First UNMAP block descriptor starts at 8 byte offset */
972 dl
= get_unaligned_be16(&cdb
[0]);
973 bd_dl
= get_unaligned_be16(&cdb
[2]);
975 printk(KERN_INFO
"UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
976 " ptr: %p\n", dev
->transport
->name
, dl
, bd_dl
, size
, ptr
);
979 lba
= get_unaligned_be64(&ptr
[0]);
980 range
= get_unaligned_be32(&ptr
[8]);
981 printk(KERN_INFO
"UNMAP: Using lba: %llu and range: %u\n",
982 (unsigned long long)lba
, range
);
984 ret
= dev
->transport
->do_discard(dev
, lba
, range
);
986 printk(KERN_ERR
"blkdev_issue_discard() failed: %d\n",
995 task
->task_scsi_status
= GOOD
;
996 transport_complete_task(task
, 1);
1001 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
1002 * Note this is not used for TCM/pSCSI passthrough
1005 target_emulate_write_same(struct se_task
*task
)
1007 struct se_cmd
*cmd
= TASK_CMD(task
);
1008 struct se_device
*dev
= SE_DEV(cmd
);
1009 sector_t lba
= cmd
->t_task
->t_task_lba
;
1013 range
= (cmd
->data_length
/ DEV_ATTRIB(dev
)->block_size
);
1015 printk(KERN_INFO
"WRITE_SAME UNMAP: LBA: %llu Range: %u\n",
1016 (unsigned long long)lba
, range
);
1018 ret
= dev
->transport
->do_discard(dev
, lba
, range
);
1020 printk(KERN_INFO
"blkdev_issue_discard() failed for WRITE_SAME\n");
1024 task
->task_scsi_status
= GOOD
;
1025 transport_complete_task(task
, 1);
1030 transport_emulate_control_cdb(struct se_task
*task
)
1032 struct se_cmd
*cmd
= TASK_CMD(task
);
1033 struct se_device
*dev
= SE_DEV(cmd
);
1034 unsigned short service_action
;
1037 switch (cmd
->t_task
->t_task_cdb
[0]) {
1039 ret
= target_emulate_inquiry(cmd
);
1042 ret
= target_emulate_readcapacity(cmd
);
1045 ret
= target_emulate_modesense(cmd
, 0);
1048 ret
= target_emulate_modesense(cmd
, 1);
1050 case SERVICE_ACTION_IN
:
1051 switch (cmd
->t_task
->t_task_cdb
[1] & 0x1f) {
1052 case SAI_READ_CAPACITY_16
:
1053 ret
= target_emulate_readcapacity_16(cmd
);
1056 printk(KERN_ERR
"Unsupported SA: 0x%02x\n",
1057 cmd
->t_task
->t_task_cdb
[1] & 0x1f);
1058 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1062 ret
= target_emulate_request_sense(cmd
);
1065 if (!dev
->transport
->do_discard
) {
1066 printk(KERN_ERR
"UNMAP emulation not supported for: %s\n",
1067 dev
->transport
->name
);
1068 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1070 ret
= target_emulate_unmap(task
);
1073 if (!dev
->transport
->do_discard
) {
1074 printk(KERN_ERR
"WRITE_SAME_16 emulation not supported"
1075 " for: %s\n", dev
->transport
->name
);
1076 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1078 ret
= target_emulate_write_same(task
);
1080 case VARIABLE_LENGTH_CMD
:
1082 get_unaligned_be16(&cmd
->t_task
->t_task_cdb
[8]);
1083 switch (service_action
) {
1085 if (!dev
->transport
->do_discard
) {
1086 printk(KERN_ERR
"WRITE_SAME_32 SA emulation not"
1087 " supported for: %s\n",
1088 dev
->transport
->name
);
1089 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1091 ret
= target_emulate_write_same(task
);
1094 printk(KERN_ERR
"Unsupported VARIABLE_LENGTH_CMD SA:"
1095 " 0x%02x\n", service_action
);
1099 case SYNCHRONIZE_CACHE
:
1100 case 0x91: /* SYNCHRONIZE_CACHE_16: */
1101 if (!dev
->transport
->do_sync_cache
) {
1103 "SYNCHRONIZE_CACHE emulation not supported"
1104 " for: %s\n", dev
->transport
->name
);
1105 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1107 dev
->transport
->do_sync_cache(task
);
1109 case ALLOW_MEDIUM_REMOVAL
:
1115 case TEST_UNIT_READY
:
1117 case WRITE_FILEMARKS
:
1120 printk(KERN_ERR
"Unsupported SCSI Opcode: 0x%02x for %s\n",
1121 cmd
->t_task
->t_task_cdb
[0], dev
->transport
->name
);
1122 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1127 task
->task_scsi_status
= GOOD
;
1128 transport_complete_task(task
, 1);
1130 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;