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 * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
90 * A RESPONSE DATA FORMAT field set to 2h indicates that the
91 * standard INQUIRY data is in the format defined in this
92 * standard. Response data format values less than 2h are
93 * obsolete. Response data format values greater than 2h are
99 * Enable SCCS and TPGS fields for Emulated ALUA
101 if (T10_ALUA(dev->se_sub_dev)->alua_type == SPC3_ALUA_EMULATED)
102 target_fill_alua_data(lun->lun_sep, buf);
104 if (cmd->data_length < 8) {
105 buf[4] = 1; /* Set additional length to 1 */
109 buf[7] = 0x2; /* CmdQue=1 */
112 * Do not include vendor, product, reversion info in INQUIRY
113 * response payload for cdbs with a small allocation length.
115 if (cmd->data_length < 36) {
116 buf[4] = 3; /* Set additional length to 3 */
120 snprintf((unsigned char *)&buf[8], 8, "LIO-ORG");
121 snprintf((unsigned char *)&buf[16], 16, "%s",
122 &DEV_T10_WWN(dev)->model[0]);
123 snprintf((unsigned char *)&buf[32], 4, "%s",
124 &DEV_T10_WWN(dev)->revision[0]);
125 buf[4] = 31; /* Set additional length to 31 */
129 /* supported vital product data pages */
131 target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
134 if (cmd->data_length < 8)
139 * Only report the INQUIRY EVPD=1 pages after a valid NAA
140 * Registered Extended LUN WWN has been set via ConfigFS
141 * during device creation/restart.
143 if (SE_DEV(cmd)->se_sub_dev->su_dev_flags &
144 SDF_EMULATED_VPD_UNIT_SERIAL) {
154 /* unit serial number */
156 target_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
158 struct se_device *dev = SE_DEV(cmd);
162 if (dev->se_sub_dev->su_dev_flags &
163 SDF_EMULATED_VPD_UNIT_SERIAL) {
167 strlen(&DEV_T10_WWN(dev)->unit_serial[0]);
168 unit_serial_len++; /* For NULL Terminator */
170 if (((len + 4) + unit_serial_len) > cmd->data_length) {
171 len += unit_serial_len;
172 buf[2] = ((len >> 8) & 0xff);
173 buf[3] = (len & 0xff);
176 len += sprintf((unsigned char *)&buf[4], "%s",
177 &DEV_T10_WWN(dev)->unit_serial[0]);
178 len++; /* Extra Byte for NULL Terminator */
185 * Device identification VPD, for a complete list of
186 * DESIGNATOR TYPEs see spc4r17 Table 459.
189 target_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
191 struct se_device *dev = SE_DEV(cmd);
192 struct se_lun *lun = SE_LUN(cmd);
193 struct se_port *port = NULL;
194 struct se_portal_group *tpg = NULL;
195 struct t10_alua_lu_gp_member *lu_gp_mem;
196 struct t10_alua_tg_pt_gp *tg_pt_gp;
197 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
198 unsigned char binary, binary_new;
199 unsigned char *prod = &DEV_T10_WWN(dev)->model[0];
201 u32 unit_serial_len, off = 0;
209 * NAA IEEE Registered Extended Assigned designator format, see
210 * spc4r17 section 7.7.3.6.5
212 * We depend upon a target_core_mod/ConfigFS provided
213 * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
214 * value in order to return the NAA id.
216 if (!(dev->se_sub_dev->su_dev_flags & SDF_EMULATED_VPD_UNIT_SERIAL))
217 goto check_t10_vend_desc;
219 if (off + 20 > cmd->data_length)
220 goto check_t10_vend_desc;
222 /* CODE SET == Binary */
225 /* Set ASSOICATION == addressed logical unit: 0)b */
228 /* Identifier/Designator type == NAA identifier */
232 /* Identifier/Designator length */
236 * Start NAA IEEE Registered Extended Identifier/Designator
238 buf[off++] = (0x6 << 4);
241 * Use OpenFabrics IEEE Company ID: 00 14 05
245 buf[off] = (0x5 << 4);
248 * Return ConfigFS Unit Serial Number information for
249 * VENDOR_SPECIFIC_IDENTIFIER and
250 * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
252 binary = transport_asciihex_to_binaryhex(
253 &DEV_T10_WWN(dev)->unit_serial[0]);
254 buf[off++] |= (binary & 0xf0) >> 4;
255 for (i = 0; i < 24; i += 2) {
256 binary_new = transport_asciihex_to_binaryhex(
257 &DEV_T10_WWN(dev)->unit_serial[i+2]);
258 buf[off] = (binary & 0x0f) << 4;
259 buf[off++] |= (binary_new & 0xf0) >> 4;
267 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
269 id_len = 8; /* For Vendor field */
270 prod_len = 4; /* For VPD Header */
271 prod_len += 8; /* For Vendor field */
272 prod_len += strlen(prod);
273 prod_len++; /* For : */
275 if (dev->se_sub_dev->su_dev_flags &
276 SDF_EMULATED_VPD_UNIT_SERIAL) {
278 strlen(&DEV_T10_WWN(dev)->unit_serial[0]);
279 unit_serial_len++; /* For NULL Terminator */
281 if ((len + (id_len + 4) +
282 (prod_len + unit_serial_len)) >
284 len += (prod_len + unit_serial_len);
287 id_len += sprintf((unsigned char *)&buf[off+12],
289 &DEV_T10_WWN(dev)->unit_serial[0]);
291 buf[off] = 0x2; /* ASCII */
292 buf[off+1] = 0x1; /* T10 Vendor ID */
294 memcpy((unsigned char *)&buf[off+4], "LIO-ORG", 8);
295 /* Extra Byte for NULL Terminator */
297 /* Identifier Length */
299 /* Header size for Designation descriptor */
303 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
308 struct t10_alua_lu_gp *lu_gp;
309 u32 padding, scsi_name_len;
316 * Relative target port identifer, see spc4r17
319 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
320 * section 7.5.1 Table 362
322 if (((len + 4) + 8) > cmd->data_length) {
327 (TPG_TFO(tpg)->get_fabric_proto_ident(tpg) << 4);
328 buf[off++] |= 0x1; /* CODE SET == Binary */
329 buf[off] = 0x80; /* Set PIV=1 */
330 /* Set ASSOICATION == target port: 01b */
332 /* DESIGNATOR TYPE == Relative target port identifer */
334 off++; /* Skip over Reserved */
335 buf[off++] = 4; /* DESIGNATOR LENGTH */
336 /* Skip over Obsolete field in RTPI payload
339 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
340 buf[off++] = (port->sep_rtpi & 0xff);
341 len += 8; /* Header size + Designation descriptor */
343 * Target port group identifier, see spc4r17
346 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
347 * section 7.5.1 Table 362
350 if (T10_ALUA(dev->se_sub_dev)->alua_type !=
352 goto check_scsi_name;
354 if (((len + 4) + 8) > cmd->data_length) {
358 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
362 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
363 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
365 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
368 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
369 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
372 (TPG_TFO(tpg)->get_fabric_proto_ident(tpg) << 4);
373 buf[off++] |= 0x1; /* CODE SET == Binary */
374 buf[off] = 0x80; /* Set PIV=1 */
375 /* Set ASSOICATION == target port: 01b */
377 /* DESIGNATOR TYPE == Target port group identifier */
379 off++; /* Skip over Reserved */
380 buf[off++] = 4; /* DESIGNATOR LENGTH */
381 off += 2; /* Skip over Reserved Field */
382 buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
383 buf[off++] = (tg_pt_gp_id & 0xff);
384 len += 8; /* Header size + Designation descriptor */
386 * Logical Unit Group identifier, see spc4r17
390 if (((len + 4) + 8) > cmd->data_length) {
392 goto check_scsi_name;
394 lu_gp_mem = dev->dev_alua_lu_gp_mem;
396 goto check_scsi_name;
398 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
399 lu_gp = lu_gp_mem->lu_gp;
401 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
402 goto check_scsi_name;
404 lu_gp_id = lu_gp->lu_gp_id;
405 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
407 buf[off++] |= 0x1; /* CODE SET == Binary */
408 /* DESIGNATOR TYPE == Logical Unit Group identifier */
410 off++; /* Skip over Reserved */
411 buf[off++] = 4; /* DESIGNATOR LENGTH */
412 off += 2; /* Skip over Reserved Field */
413 buf[off++] = ((lu_gp_id >> 8) & 0xff);
414 buf[off++] = (lu_gp_id & 0xff);
415 len += 8; /* Header size + Designation descriptor */
417 * SCSI name string designator, see spc4r17
420 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
421 * section 7.5.1 Table 362
424 scsi_name_len = strlen(TPG_TFO(tpg)->tpg_get_wwn(tpg));
425 /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
427 /* Check for 4-byte padding */
428 padding = ((-scsi_name_len) & 3);
430 scsi_name_len += padding;
431 /* Header size + Designation descriptor */
434 if (((len + 4) + scsi_name_len) > cmd->data_length) {
435 len += scsi_name_len;
439 (TPG_TFO(tpg)->get_fabric_proto_ident(tpg) << 4);
440 buf[off++] |= 0x3; /* CODE SET == UTF-8 */
441 buf[off] = 0x80; /* Set PIV=1 */
442 /* Set ASSOICATION == target port: 01b */
444 /* DESIGNATOR TYPE == SCSI name string */
446 off += 2; /* Skip over Reserved and length */
448 * SCSI name string identifer containing, $FABRIC_MOD
449 * dependent information. For LIO-Target and iSCSI
450 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
453 tpgt = TPG_TFO(tpg)->tpg_get_tag(tpg);
454 scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
455 TPG_TFO(tpg)->tpg_get_wwn(tpg), tpgt);
456 scsi_name_len += 1 /* Include NULL terminator */;
458 * The null-terminated, null-padded (see 4.4.2) SCSI
459 * NAME STRING field contains a UTF-8 format string.
460 * The number of bytes in the SCSI NAME STRING field
461 * (i.e., the value in the DESIGNATOR LENGTH field)
462 * shall be no larger than 256 and shall be a multiple
466 scsi_name_len += padding;
468 buf[off-1] = scsi_name_len;
469 off += scsi_name_len;
470 /* Header size + Designation descriptor */
471 len += (scsi_name_len + 4);
474 buf[2] = ((len >> 8) & 0xff);
475 buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
479 /* Extended INQUIRY Data VPD Page */
481 target_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
483 if (cmd->data_length < 60)
488 /* Set HEADSUP, ORDSUP, SIMPSUP */
491 /* If WriteCache emulation is enabled, set V_SUP */
492 if (DEV_ATTRIB(SE_DEV(cmd))->emulate_write_cache > 0)
497 /* Block Limits VPD page */
499 target_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
501 struct se_device *dev = SE_DEV(cmd);
505 * Following sbc3r22 section 6.5.3 Block Limits VPD page, when
506 * emulate_tpu=1 or emulate_tpws=1 we will be expect a
507 * different page length for Thin Provisioning.
509 if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
512 if (cmd->data_length < (0x10 + 4)) {
513 printk(KERN_INFO "Received data_length: %u"
514 " too small for EVPD 0xb0\n",
519 if (have_tp && cmd->data_length < (0x3c + 4)) {
520 printk(KERN_INFO "Received data_length: %u"
521 " too small for TPE=1 EVPD 0xb0\n",
526 buf[0] = dev->transport->get_device_type(dev);
528 buf[3] = have_tp ? 0x3c : 0x10;
531 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
533 put_unaligned_be16(1, &buf[6]);
536 * Set MAXIMUM TRANSFER LENGTH
538 put_unaligned_be32(DEV_ATTRIB(dev)->max_sectors, &buf[8]);
541 * Set OPTIMAL TRANSFER LENGTH
543 put_unaligned_be32(DEV_ATTRIB(dev)->optimal_sectors, &buf[12]);
546 * Exit now if we don't support TP or the initiator sent a too
549 if (!have_tp || cmd->data_length < (0x3c + 4))
553 * Set MAXIMUM UNMAP LBA COUNT
555 put_unaligned_be32(DEV_ATTRIB(dev)->max_unmap_lba_count, &buf[20]);
558 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
560 put_unaligned_be32(DEV_ATTRIB(dev)->max_unmap_block_desc_count,
564 * Set OPTIMAL UNMAP GRANULARITY
566 put_unaligned_be32(DEV_ATTRIB(dev)->unmap_granularity, &buf[28]);
569 * UNMAP GRANULARITY ALIGNMENT
571 put_unaligned_be32(DEV_ATTRIB(dev)->unmap_granularity_alignment,
573 if (DEV_ATTRIB(dev)->unmap_granularity_alignment != 0)
574 buf[32] |= 0x80; /* Set the UGAVALID bit */
579 /* Thin Provisioning VPD */
581 target_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
583 struct se_device *dev = SE_DEV(cmd);
586 * From sbc3r22 section 6.5.4 Thin Provisioning VPD page:
588 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
589 * zero, then the page length shall be set to 0004h. If the DP bit
590 * is set to one, then the page length shall be set to the value
591 * defined in table 162.
593 buf[0] = dev->transport->get_device_type(dev);
597 * Set Hardcoded length mentioned above for DP=0
599 put_unaligned_be16(0x0004, &buf[2]);
602 * The THRESHOLD EXPONENT field indicates the threshold set size in
603 * LBAs as a power of 2 (i.e., the threshold set size is equal to
604 * 2(threshold exponent)).
606 * Note that this is currently set to 0x00 as mkp says it will be
607 * changing again. We can enable this once it has settled in T10
608 * and is actually used by Linux/SCSI ML code.
613 * A TPU bit set to one indicates that the device server supports
614 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
615 * that the device server does not support the UNMAP command.
617 if (DEV_ATTRIB(dev)->emulate_tpu != 0)
621 * A TPWS bit set to one indicates that the device server supports
622 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
623 * A TPWS bit set to zero indicates that the device server does not
624 * support the use of the WRITE SAME (16) command to unmap LBAs.
626 if (DEV_ATTRIB(dev)->emulate_tpws != 0)
633 target_emulate_inquiry(struct se_cmd *cmd)
635 struct se_device *dev = SE_DEV(cmd);
636 unsigned char *buf = cmd->t_task->t_task_buf;
637 unsigned char *cdb = cmd->t_task->t_task_cdb;
640 return target_emulate_inquiry_std(cmd);
643 * Make sure we at least have 4 bytes of INQUIRY response
644 * payload for 0x00 going back for EVPD=1. Note that 0x80
645 * and 0x83 will check for enough payload data length and
646 * jump to set_len: label when there is not enough inquiry EVPD
647 * payload length left for the next outgoing EVPD metadata
649 if (cmd->data_length < 4) {
650 printk(KERN_ERR "SCSI Inquiry payload length: %u"
651 " too small for EVPD=1\n", cmd->data_length);
654 buf[0] = dev->transport->get_device_type(dev);
658 return target_emulate_evpd_00(cmd, buf);
660 return target_emulate_evpd_80(cmd, buf);
662 return target_emulate_evpd_83(cmd, buf);
664 return target_emulate_evpd_86(cmd, buf);
666 return target_emulate_evpd_b0(cmd, buf);
668 return target_emulate_evpd_b2(cmd, buf);
670 printk(KERN_ERR "Unknown VPD Code: 0x%02x\n", cdb[2]);
678 target_emulate_readcapacity(struct se_cmd *cmd)
680 struct se_device *dev = SE_DEV(cmd);
681 unsigned char *buf = cmd->t_task->t_task_buf;
682 unsigned long long blocks_long = dev->transport->get_blocks(dev);
685 if (blocks_long >= 0x00000000ffffffff)
688 blocks = (u32)blocks_long;
690 buf[0] = (blocks >> 24) & 0xff;
691 buf[1] = (blocks >> 16) & 0xff;
692 buf[2] = (blocks >> 8) & 0xff;
693 buf[3] = blocks & 0xff;
694 buf[4] = (DEV_ATTRIB(dev)->block_size >> 24) & 0xff;
695 buf[5] = (DEV_ATTRIB(dev)->block_size >> 16) & 0xff;
696 buf[6] = (DEV_ATTRIB(dev)->block_size >> 8) & 0xff;
697 buf[7] = DEV_ATTRIB(dev)->block_size & 0xff;
699 * Set max 32-bit blocks to signal SERVICE ACTION READ_CAPACITY_16
701 if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
702 put_unaligned_be32(0xFFFFFFFF, &buf[0]);
708 target_emulate_readcapacity_16(struct se_cmd *cmd)
710 struct se_device *dev = SE_DEV(cmd);
711 unsigned char *buf = cmd->t_task->t_task_buf;
712 unsigned long long blocks = dev->transport->get_blocks(dev);
714 buf[0] = (blocks >> 56) & 0xff;
715 buf[1] = (blocks >> 48) & 0xff;
716 buf[2] = (blocks >> 40) & 0xff;
717 buf[3] = (blocks >> 32) & 0xff;
718 buf[4] = (blocks >> 24) & 0xff;
719 buf[5] = (blocks >> 16) & 0xff;
720 buf[6] = (blocks >> 8) & 0xff;
721 buf[7] = blocks & 0xff;
722 buf[8] = (DEV_ATTRIB(dev)->block_size >> 24) & 0xff;
723 buf[9] = (DEV_ATTRIB(dev)->block_size >> 16) & 0xff;
724 buf[10] = (DEV_ATTRIB(dev)->block_size >> 8) & 0xff;
725 buf[11] = DEV_ATTRIB(dev)->block_size & 0xff;
727 * Set Thin Provisioning Enable bit following sbc3r22 in section
728 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
730 if (DEV_ATTRIB(dev)->emulate_tpu || DEV_ATTRIB(dev)->emulate_tpws)
737 target_modesense_rwrecovery(unsigned char *p)
746 target_modesense_control(struct se_device *dev, unsigned char *p)
752 * From spc4r17, section 7.4.6 Control mode Page
754 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
756 * 00b: The logical unit shall clear any unit attention condition
757 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
758 * status and shall not establish a unit attention condition when a com-
759 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
762 * 10b: The logical unit shall not clear any unit attention condition
763 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
764 * status and shall not establish a unit attention condition when
765 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
768 * 11b a The logical unit shall not clear any unit attention condition
769 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
770 * status and shall establish a unit attention condition for the
771 * initiator port associated with the I_T nexus on which the BUSY,
772 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
773 * Depending on the status, the additional sense code shall be set to
774 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
775 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
776 * command, a unit attention condition shall be established only once
777 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
778 * to the number of commands completed with one of those status codes.
780 p[4] = (DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl == 2) ? 0x30 :
781 (DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
783 * From spc4r17, section 7.4.6 Control mode Page
785 * Task Aborted Status (TAS) bit set to zero.
787 * A task aborted status (TAS) bit set to zero specifies that aborted
788 * tasks shall be terminated by the device server without any response
789 * to the application client. A TAS bit set to one specifies that tasks
790 * aborted by the actions of an I_T nexus other than the I_T nexus on
791 * which the command was received shall be completed with TASK ABORTED
792 * status (see SAM-4).
794 p[5] = (DEV_ATTRIB(dev)->emulate_tas) ? 0x40 : 0x00;
803 target_modesense_caching(struct se_device *dev, unsigned char *p)
807 if (DEV_ATTRIB(dev)->emulate_write_cache > 0)
808 p[2] = 0x04; /* Write Cache Enable */
809 p[12] = 0x20; /* Disabled Read Ahead */
815 target_modesense_write_protect(unsigned char *buf, int type)
818 * I believe that the WP bit (bit 7) in the mode header is the same for
825 buf[0] |= 0x80; /* WP bit */
831 target_modesense_dpofua(unsigned char *buf, int type)
835 buf[0] |= 0x10; /* DPOFUA bit */
843 target_emulate_modesense(struct se_cmd *cmd, int ten)
845 struct se_device *dev = SE_DEV(cmd);
846 char *cdb = cmd->t_task->t_task_cdb;
847 unsigned char *rbuf = cmd->t_task->t_task_buf;
848 int type = dev->transport->get_device_type(dev);
849 int offset = (ten) ? 8 : 4;
851 unsigned char buf[SE_MODE_PAGE_BUF];
853 memset(buf, 0, SE_MODE_PAGE_BUF);
855 switch (cdb[2] & 0x3f) {
857 length = target_modesense_rwrecovery(&buf[offset]);
860 length = target_modesense_caching(dev, &buf[offset]);
863 length = target_modesense_control(dev, &buf[offset]);
866 length = target_modesense_rwrecovery(&buf[offset]);
867 length += target_modesense_caching(dev, &buf[offset+length]);
868 length += target_modesense_control(dev, &buf[offset+length]);
871 printk(KERN_ERR "Got Unknown Mode Page: 0x%02x\n",
873 return PYX_TRANSPORT_UNKNOWN_MODE_PAGE;
879 buf[0] = (offset >> 8) & 0xff;
880 buf[1] = offset & 0xff;
882 if ((SE_LUN(cmd)->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
884 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
885 target_modesense_write_protect(&buf[3], type);
887 if ((DEV_ATTRIB(dev)->emulate_write_cache > 0) &&
888 (DEV_ATTRIB(dev)->emulate_fua_write > 0))
889 target_modesense_dpofua(&buf[3], type);
891 if ((offset + 2) > cmd->data_length)
892 offset = cmd->data_length;
896 buf[0] = offset & 0xff;
898 if ((SE_LUN(cmd)->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
900 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
901 target_modesense_write_protect(&buf[2], type);
903 if ((DEV_ATTRIB(dev)->emulate_write_cache > 0) &&
904 (DEV_ATTRIB(dev)->emulate_fua_write > 0))
905 target_modesense_dpofua(&buf[2], type);
907 if ((offset + 1) > cmd->data_length)
908 offset = cmd->data_length;
910 memcpy(rbuf, buf, offset);
916 target_emulate_request_sense(struct se_cmd *cmd)
918 unsigned char *cdb = cmd->t_task->t_task_cdb;
919 unsigned char *buf = cmd->t_task->t_task_buf;
920 u8 ua_asc = 0, ua_ascq = 0;
923 printk(KERN_ERR "REQUEST_SENSE description emulation not"
925 return PYX_TRANSPORT_INVALID_CDB_FIELD;
927 if (!(core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq))) {
929 * CURRENT ERROR, UNIT ATTENTION
932 buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
934 * Make sure request data length is enough for additional
937 if (cmd->data_length <= 18) {
942 * The Additional Sense Code (ASC) from the UNIT ATTENTION
944 buf[SPC_ASC_KEY_OFFSET] = ua_asc;
945 buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
949 * CURRENT ERROR, NO SENSE
952 buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
954 * Make sure request data length is enough for additional
957 if (cmd->data_length <= 18) {
962 * NO ADDITIONAL SENSE INFORMATION
964 buf[SPC_ASC_KEY_OFFSET] = 0x00;
972 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
973 * Note this is not used for TCM/pSCSI passthrough
976 target_emulate_unmap(struct se_task *task)
978 struct se_cmd *cmd = TASK_CMD(task);
979 struct se_device *dev = SE_DEV(cmd);
980 unsigned char *buf = cmd->t_task->t_task_buf, *ptr = NULL;
981 unsigned char *cdb = &cmd->t_task->t_task_cdb[0];
983 unsigned int size = cmd->data_length, range;
985 unsigned short dl, bd_dl;
987 /* First UNMAP block descriptor starts at 8 byte offset */
990 dl = get_unaligned_be16(&cdb[0]);
991 bd_dl = get_unaligned_be16(&cdb[2]);
993 printk(KERN_INFO "UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
994 " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
997 lba = get_unaligned_be64(&ptr[0]);
998 range = get_unaligned_be32(&ptr[8]);
999 printk(KERN_INFO "UNMAP: Using lba: %llu and range: %u\n",
1000 (unsigned long long)lba, range);
1002 ret = dev->transport->do_discard(dev, lba, range);
1004 printk(KERN_ERR "blkdev_issue_discard() failed: %d\n",
1013 task->task_scsi_status = GOOD;
1014 transport_complete_task(task, 1);
1019 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
1020 * Note this is not used for TCM/pSCSI passthrough
1023 target_emulate_write_same(struct se_task *task)
1025 struct se_cmd *cmd = TASK_CMD(task);
1026 struct se_device *dev = SE_DEV(cmd);
1027 sector_t lba = cmd->t_task->t_task_lba;
1031 range = (cmd->data_length / DEV_ATTRIB(dev)->block_size);
1033 printk(KERN_INFO "WRITE_SAME UNMAP: LBA: %llu Range: %u\n",
1034 (unsigned long long)lba, range);
1036 ret = dev->transport->do_discard(dev, lba, range);
1038 printk(KERN_INFO "blkdev_issue_discard() failed for WRITE_SAME\n");
1042 task->task_scsi_status = GOOD;
1043 transport_complete_task(task, 1);
1048 transport_emulate_control_cdb(struct se_task *task)
1050 struct se_cmd *cmd = TASK_CMD(task);
1051 struct se_device *dev = SE_DEV(cmd);
1052 unsigned short service_action;
1055 switch (cmd->t_task->t_task_cdb[0]) {
1057 ret = target_emulate_inquiry(cmd);
1060 ret = target_emulate_readcapacity(cmd);
1063 ret = target_emulate_modesense(cmd, 0);
1066 ret = target_emulate_modesense(cmd, 1);
1068 case SERVICE_ACTION_IN:
1069 switch (cmd->t_task->t_task_cdb[1] & 0x1f) {
1070 case SAI_READ_CAPACITY_16:
1071 ret = target_emulate_readcapacity_16(cmd);
1074 printk(KERN_ERR "Unsupported SA: 0x%02x\n",
1075 cmd->t_task->t_task_cdb[1] & 0x1f);
1076 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1080 ret = target_emulate_request_sense(cmd);
1083 if (!dev->transport->do_discard) {
1084 printk(KERN_ERR "UNMAP emulation not supported for: %s\n",
1085 dev->transport->name);
1086 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1088 ret = target_emulate_unmap(task);
1091 if (!dev->transport->do_discard) {
1092 printk(KERN_ERR "WRITE_SAME_16 emulation not supported"
1093 " for: %s\n", dev->transport->name);
1094 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1096 ret = target_emulate_write_same(task);
1098 case VARIABLE_LENGTH_CMD:
1100 get_unaligned_be16(&cmd->t_task->t_task_cdb[8]);
1101 switch (service_action) {
1103 if (!dev->transport->do_discard) {
1104 printk(KERN_ERR "WRITE_SAME_32 SA emulation not"
1105 " supported for: %s\n",
1106 dev->transport->name);
1107 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1109 ret = target_emulate_write_same(task);
1112 printk(KERN_ERR "Unsupported VARIABLE_LENGTH_CMD SA:"
1113 " 0x%02x\n", service_action);
1117 case SYNCHRONIZE_CACHE:
1118 case 0x91: /* SYNCHRONIZE_CACHE_16: */
1119 if (!dev->transport->do_sync_cache) {
1121 "SYNCHRONIZE_CACHE emulation not supported"
1122 " for: %s\n", dev->transport->name);
1123 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1125 dev->transport->do_sync_cache(task);
1127 case ALLOW_MEDIUM_REMOVAL:
1133 case TEST_UNIT_READY:
1135 case WRITE_FILEMARKS:
1138 printk(KERN_ERR "Unsupported SCSI Opcode: 0x%02x for %s\n",
1139 cmd->t_task->t_task_cdb[0], dev->transport->name);
1140 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1145 task->task_scsi_status = GOOD;
1146 transport_complete_task(task, 1);
1148 return PYX_TRANSPORT_SENT_TO_TRANSPORT;