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 unsigned long long blocks_long
= dev
->transport
->get_blocks(dev
);
673 if (blocks_long
>= 0x00000000ffffffff)
676 blocks
= (u32
)blocks_long
;
678 buf
[0] = (blocks
>> 24) & 0xff;
679 buf
[1] = (blocks
>> 16) & 0xff;
680 buf
[2] = (blocks
>> 8) & 0xff;
681 buf
[3] = blocks
& 0xff;
682 buf
[4] = (DEV_ATTRIB(dev
)->block_size
>> 24) & 0xff;
683 buf
[5] = (DEV_ATTRIB(dev
)->block_size
>> 16) & 0xff;
684 buf
[6] = (DEV_ATTRIB(dev
)->block_size
>> 8) & 0xff;
685 buf
[7] = DEV_ATTRIB(dev
)->block_size
& 0xff;
687 * Set max 32-bit blocks to signal SERVICE ACTION READ_CAPACITY_16
689 if (DEV_ATTRIB(dev
)->emulate_tpu
|| DEV_ATTRIB(dev
)->emulate_tpws
)
690 put_unaligned_be32(0xFFFFFFFF, &buf
[0]);
696 target_emulate_readcapacity_16(struct se_cmd
*cmd
)
698 struct se_device
*dev
= SE_DEV(cmd
);
699 unsigned char *buf
= cmd
->t_task
->t_task_buf
;
700 unsigned long long blocks
= dev
->transport
->get_blocks(dev
);
702 buf
[0] = (blocks
>> 56) & 0xff;
703 buf
[1] = (blocks
>> 48) & 0xff;
704 buf
[2] = (blocks
>> 40) & 0xff;
705 buf
[3] = (blocks
>> 32) & 0xff;
706 buf
[4] = (blocks
>> 24) & 0xff;
707 buf
[5] = (blocks
>> 16) & 0xff;
708 buf
[6] = (blocks
>> 8) & 0xff;
709 buf
[7] = blocks
& 0xff;
710 buf
[8] = (DEV_ATTRIB(dev
)->block_size
>> 24) & 0xff;
711 buf
[9] = (DEV_ATTRIB(dev
)->block_size
>> 16) & 0xff;
712 buf
[10] = (DEV_ATTRIB(dev
)->block_size
>> 8) & 0xff;
713 buf
[11] = DEV_ATTRIB(dev
)->block_size
& 0xff;
715 * Set Thin Provisioning Enable bit following sbc3r22 in section
716 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
718 if (DEV_ATTRIB(dev
)->emulate_tpu
|| DEV_ATTRIB(dev
)->emulate_tpws
)
725 target_modesense_rwrecovery(unsigned char *p
)
734 target_modesense_control(struct se_device
*dev
, unsigned char *p
)
740 * From spc4r17, section 7.4.6 Control mode Page
742 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
744 * 00b: The logical unit shall 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 a com-
747 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
750 * 10b: 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 not establish a unit attention condition when
753 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
756 * 11b a The logical unit shall not clear any unit attention condition
757 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
758 * status and shall establish a unit attention condition for the
759 * initiator port associated with the I_T nexus on which the BUSY,
760 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
761 * Depending on the status, the additional sense code shall be set to
762 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
763 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
764 * command, a unit attention condition shall be established only once
765 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
766 * to the number of commands completed with one of those status codes.
768 p
[4] = (DEV_ATTRIB(dev
)->emulate_ua_intlck_ctrl
== 2) ? 0x30 :
769 (DEV_ATTRIB(dev
)->emulate_ua_intlck_ctrl
== 1) ? 0x20 : 0x00;
771 * From spc4r17, section 7.4.6 Control mode Page
773 * Task Aborted Status (TAS) bit set to zero.
775 * A task aborted status (TAS) bit set to zero specifies that aborted
776 * tasks shall be terminated by the device server without any response
777 * to the application client. A TAS bit set to one specifies that tasks
778 * aborted by the actions of an I_T nexus other than the I_T nexus on
779 * which the command was received shall be completed with TASK ABORTED
780 * status (see SAM-4).
782 p
[5] = (DEV_ATTRIB(dev
)->emulate_tas
) ? 0x40 : 0x00;
791 target_modesense_caching(struct se_device
*dev
, unsigned char *p
)
795 if (DEV_ATTRIB(dev
)->emulate_write_cache
> 0)
796 p
[2] = 0x04; /* Write Cache Enable */
797 p
[12] = 0x20; /* Disabled Read Ahead */
803 target_modesense_write_protect(unsigned char *buf
, int type
)
806 * I believe that the WP bit (bit 7) in the mode header is the same for
813 buf
[0] |= 0x80; /* WP bit */
819 target_modesense_dpofua(unsigned char *buf
, int type
)
823 buf
[0] |= 0x10; /* DPOFUA bit */
831 target_emulate_modesense(struct se_cmd
*cmd
, int ten
)
833 struct se_device
*dev
= SE_DEV(cmd
);
834 char *cdb
= cmd
->t_task
->t_task_cdb
;
835 unsigned char *rbuf
= cmd
->t_task
->t_task_buf
;
836 int type
= dev
->transport
->get_device_type(dev
);
837 int offset
= (ten
) ? 8 : 4;
839 unsigned char buf
[SE_MODE_PAGE_BUF
];
841 memset(buf
, 0, SE_MODE_PAGE_BUF
);
843 switch (cdb
[2] & 0x3f) {
845 length
= target_modesense_rwrecovery(&buf
[offset
]);
848 length
= target_modesense_caching(dev
, &buf
[offset
]);
851 length
= target_modesense_control(dev
, &buf
[offset
]);
854 length
= target_modesense_rwrecovery(&buf
[offset
]);
855 length
+= target_modesense_caching(dev
, &buf
[offset
+length
]);
856 length
+= target_modesense_control(dev
, &buf
[offset
+length
]);
859 printk(KERN_ERR
"Got Unknown Mode Page: 0x%02x\n",
861 return PYX_TRANSPORT_UNKNOWN_MODE_PAGE
;
867 buf
[0] = (offset
>> 8) & 0xff;
868 buf
[1] = offset
& 0xff;
870 if ((SE_LUN(cmd
)->lun_access
& TRANSPORT_LUNFLAGS_READ_ONLY
) ||
872 (cmd
->se_deve
->lun_flags
& TRANSPORT_LUNFLAGS_READ_ONLY
)))
873 target_modesense_write_protect(&buf
[3], type
);
875 if ((DEV_ATTRIB(dev
)->emulate_write_cache
> 0) &&
876 (DEV_ATTRIB(dev
)->emulate_fua_write
> 0))
877 target_modesense_dpofua(&buf
[3], type
);
879 if ((offset
+ 2) > cmd
->data_length
)
880 offset
= cmd
->data_length
;
884 buf
[0] = offset
& 0xff;
886 if ((SE_LUN(cmd
)->lun_access
& TRANSPORT_LUNFLAGS_READ_ONLY
) ||
888 (cmd
->se_deve
->lun_flags
& TRANSPORT_LUNFLAGS_READ_ONLY
)))
889 target_modesense_write_protect(&buf
[2], type
);
891 if ((DEV_ATTRIB(dev
)->emulate_write_cache
> 0) &&
892 (DEV_ATTRIB(dev
)->emulate_fua_write
> 0))
893 target_modesense_dpofua(&buf
[2], type
);
895 if ((offset
+ 1) > cmd
->data_length
)
896 offset
= cmd
->data_length
;
898 memcpy(rbuf
, buf
, offset
);
904 target_emulate_request_sense(struct se_cmd
*cmd
)
906 unsigned char *cdb
= cmd
->t_task
->t_task_cdb
;
907 unsigned char *buf
= cmd
->t_task
->t_task_buf
;
908 u8 ua_asc
= 0, ua_ascq
= 0;
911 printk(KERN_ERR
"REQUEST_SENSE description emulation not"
913 return PYX_TRANSPORT_INVALID_CDB_FIELD
;
915 if (!(core_scsi3_ua_clear_for_request_sense(cmd
, &ua_asc
, &ua_ascq
))) {
917 * CURRENT ERROR, UNIT ATTENTION
920 buf
[SPC_SENSE_KEY_OFFSET
] = UNIT_ATTENTION
;
922 * Make sure request data length is enough for additional
925 if (cmd
->data_length
<= 18) {
930 * The Additional Sense Code (ASC) from the UNIT ATTENTION
932 buf
[SPC_ASC_KEY_OFFSET
] = ua_asc
;
933 buf
[SPC_ASCQ_KEY_OFFSET
] = ua_ascq
;
937 * CURRENT ERROR, NO SENSE
940 buf
[SPC_SENSE_KEY_OFFSET
] = NO_SENSE
;
942 * Make sure request data length is enough for additional
945 if (cmd
->data_length
<= 18) {
950 * NO ADDITIONAL SENSE INFORMATION
952 buf
[SPC_ASC_KEY_OFFSET
] = 0x00;
960 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
961 * Note this is not used for TCM/pSCSI passthrough
964 target_emulate_unmap(struct se_task
*task
)
966 struct se_cmd
*cmd
= TASK_CMD(task
);
967 struct se_device
*dev
= SE_DEV(cmd
);
968 unsigned char *buf
= cmd
->t_task
->t_task_buf
, *ptr
= NULL
;
969 unsigned char *cdb
= &cmd
->t_task
->t_task_cdb
[0];
971 unsigned int size
= cmd
->data_length
, range
;
973 unsigned short dl
, bd_dl
;
975 /* First UNMAP block descriptor starts at 8 byte offset */
978 dl
= get_unaligned_be16(&cdb
[0]);
979 bd_dl
= get_unaligned_be16(&cdb
[2]);
981 printk(KERN_INFO
"UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
982 " ptr: %p\n", dev
->transport
->name
, dl
, bd_dl
, size
, ptr
);
985 lba
= get_unaligned_be64(&ptr
[0]);
986 range
= get_unaligned_be32(&ptr
[8]);
987 printk(KERN_INFO
"UNMAP: Using lba: %llu and range: %u\n",
988 (unsigned long long)lba
, range
);
990 ret
= dev
->transport
->do_discard(dev
, lba
, range
);
992 printk(KERN_ERR
"blkdev_issue_discard() failed: %d\n",
1001 task
->task_scsi_status
= GOOD
;
1002 transport_complete_task(task
, 1);
1007 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
1008 * Note this is not used for TCM/pSCSI passthrough
1011 target_emulate_write_same(struct se_task
*task
)
1013 struct se_cmd
*cmd
= TASK_CMD(task
);
1014 struct se_device
*dev
= SE_DEV(cmd
);
1015 sector_t lba
= cmd
->t_task
->t_task_lba
;
1019 range
= (cmd
->data_length
/ DEV_ATTRIB(dev
)->block_size
);
1021 printk(KERN_INFO
"WRITE_SAME UNMAP: LBA: %llu Range: %u\n",
1022 (unsigned long long)lba
, range
);
1024 ret
= dev
->transport
->do_discard(dev
, lba
, range
);
1026 printk(KERN_INFO
"blkdev_issue_discard() failed for WRITE_SAME\n");
1030 task
->task_scsi_status
= GOOD
;
1031 transport_complete_task(task
, 1);
1036 transport_emulate_control_cdb(struct se_task
*task
)
1038 struct se_cmd
*cmd
= TASK_CMD(task
);
1039 struct se_device
*dev
= SE_DEV(cmd
);
1040 unsigned short service_action
;
1043 switch (cmd
->t_task
->t_task_cdb
[0]) {
1045 ret
= target_emulate_inquiry(cmd
);
1048 ret
= target_emulate_readcapacity(cmd
);
1051 ret
= target_emulate_modesense(cmd
, 0);
1054 ret
= target_emulate_modesense(cmd
, 1);
1056 case SERVICE_ACTION_IN
:
1057 switch (cmd
->t_task
->t_task_cdb
[1] & 0x1f) {
1058 case SAI_READ_CAPACITY_16
:
1059 ret
= target_emulate_readcapacity_16(cmd
);
1062 printk(KERN_ERR
"Unsupported SA: 0x%02x\n",
1063 cmd
->t_task
->t_task_cdb
[1] & 0x1f);
1064 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1068 ret
= target_emulate_request_sense(cmd
);
1071 if (!dev
->transport
->do_discard
) {
1072 printk(KERN_ERR
"UNMAP emulation not supported for: %s\n",
1073 dev
->transport
->name
);
1074 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1076 ret
= target_emulate_unmap(task
);
1079 if (!dev
->transport
->do_discard
) {
1080 printk(KERN_ERR
"WRITE_SAME_16 emulation not supported"
1081 " for: %s\n", dev
->transport
->name
);
1082 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1084 ret
= target_emulate_write_same(task
);
1086 case VARIABLE_LENGTH_CMD
:
1088 get_unaligned_be16(&cmd
->t_task
->t_task_cdb
[8]);
1089 switch (service_action
) {
1091 if (!dev
->transport
->do_discard
) {
1092 printk(KERN_ERR
"WRITE_SAME_32 SA emulation not"
1093 " supported for: %s\n",
1094 dev
->transport
->name
);
1095 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1097 ret
= target_emulate_write_same(task
);
1100 printk(KERN_ERR
"Unsupported VARIABLE_LENGTH_CMD SA:"
1101 " 0x%02x\n", service_action
);
1105 case SYNCHRONIZE_CACHE
:
1106 case 0x91: /* SYNCHRONIZE_CACHE_16: */
1107 if (!dev
->transport
->do_sync_cache
) {
1109 "SYNCHRONIZE_CACHE emulation not supported"
1110 " for: %s\n", dev
->transport
->name
);
1111 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1113 dev
->transport
->do_sync_cache(task
);
1115 case ALLOW_MEDIUM_REMOVAL
:
1121 case TEST_UNIT_READY
:
1123 case WRITE_FILEMARKS
:
1126 printk(KERN_ERR
"Unsupported SCSI Opcode: 0x%02x for %s\n",
1127 cmd
->t_task
->t_task_cdb
[0], dev
->transport
->name
);
1128 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
1133 task
->task_scsi_status
= GOOD
;
1134 transport_complete_task(task
, 1);
1136 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;