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 <linux/kernel.h>
27 #include <asm/unaligned.h>
28 #include <scsi/scsi.h>
30 #include <target/target_core_base.h>
31 #include <target/target_core_transport.h>
32 #include <target/target_core_fabric_ops.h>
33 #include "target_core_ua.h"
36 target_fill_alua_data(struct se_port *port, unsigned char *buf)
38 struct t10_alua_tg_pt_gp *tg_pt_gp;
39 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
42 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
47 * Set TPGS field for explict and/or implict ALUA access type
50 * See spc4r17 section 6.4.2 Table 135
54 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
58 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
59 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
61 buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
62 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
66 target_emulate_inquiry_std(struct se_cmd *cmd)
68 struct se_lun *lun = cmd->se_lun;
69 struct se_device *dev = cmd->se_dev;
73 * Make sure we at least have 6 bytes of INQUIRY response
74 * payload going back for EVPD=0
76 if (cmd->data_length < 6) {
77 pr_err("SCSI Inquiry payload length: %u"
78 " too small for EVPD=0\n", cmd->data_length);
82 buf = transport_kmap_first_data_page(cmd);
84 buf[0] = dev->transport->get_device_type(dev);
85 if (buf[0] == TYPE_TAPE)
87 buf[2] = dev->transport->get_device_rev(dev);
90 * Enable SCCS and TPGS fields for Emulated ALUA
92 if (dev->se_sub_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED)
93 target_fill_alua_data(lun->lun_sep, buf);
95 if (cmd->data_length < 8) {
96 buf[4] = 1; /* Set additional length to 1 */
100 buf[7] = 0x32; /* Sync=1 and CmdQue=1 */
103 * Do not include vendor, product, reversion info in INQUIRY
104 * response payload for cdbs with a small allocation length.
106 if (cmd->data_length < 36) {
107 buf[4] = 3; /* Set additional length to 3 */
111 snprintf((unsigned char *)&buf[8], 8, "LIO-ORG");
112 snprintf((unsigned char *)&buf[16], 16, "%s",
113 &dev->se_sub_dev->t10_wwn.model[0]);
114 snprintf((unsigned char *)&buf[32], 4, "%s",
115 &dev->se_sub_dev->t10_wwn.revision[0]);
116 buf[4] = 31; /* Set additional length to 31 */
119 transport_kunmap_first_data_page(cmd);
123 /* unit serial number */
125 target_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
127 struct se_device *dev = cmd->se_dev;
130 if (dev->se_sub_dev->su_dev_flags &
131 SDF_EMULATED_VPD_UNIT_SERIAL) {
135 strlen(&dev->se_sub_dev->t10_wwn.unit_serial[0]);
136 unit_serial_len++; /* For NULL Terminator */
138 if (((len + 4) + unit_serial_len) > cmd->data_length) {
139 len += unit_serial_len;
140 buf[2] = ((len >> 8) & 0xff);
141 buf[3] = (len & 0xff);
144 len += sprintf((unsigned char *)&buf[4], "%s",
145 &dev->se_sub_dev->t10_wwn.unit_serial[0]);
146 len++; /* Extra Byte for NULL Terminator */
153 * Device identification VPD, for a complete list of
154 * DESIGNATOR TYPEs see spc4r17 Table 459.
157 target_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
159 struct se_device *dev = cmd->se_dev;
160 struct se_lun *lun = cmd->se_lun;
161 struct se_port *port = NULL;
162 struct se_portal_group *tpg = NULL;
163 struct t10_alua_lu_gp_member *lu_gp_mem;
164 struct t10_alua_tg_pt_gp *tg_pt_gp;
165 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
166 unsigned char *prod = &dev->se_sub_dev->t10_wwn.model[0];
168 u32 unit_serial_len, off = 0;
174 * NAA IEEE Registered Extended Assigned designator format, see
175 * spc4r17 section 7.7.3.6.5
177 * We depend upon a target_core_mod/ConfigFS provided
178 * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
179 * value in order to return the NAA id.
181 if (!(dev->se_sub_dev->su_dev_flags & SDF_EMULATED_VPD_UNIT_SERIAL))
182 goto check_t10_vend_desc;
184 if (off + 20 > cmd->data_length)
185 goto check_t10_vend_desc;
187 /* CODE SET == Binary */
190 /* Set ASSOCIATION == addressed logical unit: 0)b */
193 /* Identifier/Designator type == NAA identifier */
197 /* Identifier/Designator length */
201 * Start NAA IEEE Registered Extended Identifier/Designator
203 buf[off++] = (0x6 << 4);
206 * Use OpenFabrics IEEE Company ID: 00 14 05
210 buf[off] = (0x5 << 4);
213 * Return ConfigFS Unit Serial Number information for
214 * VENDOR_SPECIFIC_IDENTIFIER and
215 * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
217 buf[off++] |= hex_to_bin(dev->se_sub_dev->t10_wwn.unit_serial[0]);
218 hex2bin(&buf[off], &dev->se_sub_dev->t10_wwn.unit_serial[1], 12);
225 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
227 id_len = 8; /* For Vendor field */
228 prod_len = 4; /* For VPD Header */
229 prod_len += 8; /* For Vendor field */
230 prod_len += strlen(prod);
231 prod_len++; /* For : */
233 if (dev->se_sub_dev->su_dev_flags &
234 SDF_EMULATED_VPD_UNIT_SERIAL) {
236 strlen(&dev->se_sub_dev->t10_wwn.unit_serial[0]);
237 unit_serial_len++; /* For NULL Terminator */
239 if ((len + (id_len + 4) +
240 (prod_len + unit_serial_len)) >
242 len += (prod_len + unit_serial_len);
245 id_len += sprintf((unsigned char *)&buf[off+12],
247 &dev->se_sub_dev->t10_wwn.unit_serial[0]);
249 buf[off] = 0x2; /* ASCII */
250 buf[off+1] = 0x1; /* T10 Vendor ID */
252 memcpy((unsigned char *)&buf[off+4], "LIO-ORG", 8);
253 /* Extra Byte for NULL Terminator */
255 /* Identifier Length */
257 /* Header size for Designation descriptor */
261 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
266 struct t10_alua_lu_gp *lu_gp;
267 u32 padding, scsi_name_len;
274 * Relative target port identifer, see spc4r17
277 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
278 * section 7.5.1 Table 362
280 if (((len + 4) + 8) > cmd->data_length) {
285 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
286 buf[off++] |= 0x1; /* CODE SET == Binary */
287 buf[off] = 0x80; /* Set PIV=1 */
288 /* Set ASSOCIATION == target port: 01b */
290 /* DESIGNATOR TYPE == Relative target port identifer */
292 off++; /* Skip over Reserved */
293 buf[off++] = 4; /* DESIGNATOR LENGTH */
294 /* Skip over Obsolete field in RTPI payload
297 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
298 buf[off++] = (port->sep_rtpi & 0xff);
299 len += 8; /* Header size + Designation descriptor */
301 * Target port group identifier, see spc4r17
304 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
305 * section 7.5.1 Table 362
308 if (dev->se_sub_dev->t10_alua.alua_type !=
310 goto check_scsi_name;
312 if (((len + 4) + 8) > cmd->data_length) {
316 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
320 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
321 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
323 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
326 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
327 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
330 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
331 buf[off++] |= 0x1; /* CODE SET == Binary */
332 buf[off] = 0x80; /* Set PIV=1 */
333 /* Set ASSOCIATION == target port: 01b */
335 /* DESIGNATOR TYPE == Target port group identifier */
337 off++; /* Skip over Reserved */
338 buf[off++] = 4; /* DESIGNATOR LENGTH */
339 off += 2; /* Skip over Reserved Field */
340 buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
341 buf[off++] = (tg_pt_gp_id & 0xff);
342 len += 8; /* Header size + Designation descriptor */
344 * Logical Unit Group identifier, see spc4r17
348 if (((len + 4) + 8) > cmd->data_length) {
350 goto check_scsi_name;
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 buf[off++] = ((lu_gp_id >> 8) & 0xff);
372 buf[off++] = (lu_gp_id & 0xff);
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 scsi_name_len = strlen(tpg->se_tpg_tfo->tpg_get_wwn(tpg));
383 /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
385 /* Check for 4-byte padding */
386 padding = ((-scsi_name_len) & 3);
388 scsi_name_len += padding;
389 /* Header size + Designation descriptor */
392 if (((len + 4) + scsi_name_len) > cmd->data_length) {
393 len += scsi_name_len;
397 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
398 buf[off++] |= 0x3; /* CODE SET == UTF-8 */
399 buf[off] = 0x80; /* Set PIV=1 */
400 /* Set ASSOCIATION == target port: 01b */
402 /* DESIGNATOR TYPE == SCSI name string */
404 off += 2; /* Skip over Reserved and length */
406 * SCSI name string identifer containing, $FABRIC_MOD
407 * dependent information. For LIO-Target and iSCSI
408 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
411 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
412 scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
413 tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
414 scsi_name_len += 1 /* Include NULL terminator */;
416 * The null-terminated, null-padded (see 4.4.2) SCSI
417 * NAME STRING field contains a UTF-8 format string.
418 * The number of bytes in the SCSI NAME STRING field
419 * (i.e., the value in the DESIGNATOR LENGTH field)
420 * shall be no larger than 256 and shall be a multiple
424 scsi_name_len += padding;
426 buf[off-1] = scsi_name_len;
427 off += scsi_name_len;
428 /* Header size + Designation descriptor */
429 len += (scsi_name_len + 4);
432 buf[2] = ((len >> 8) & 0xff);
433 buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
437 /* Extended INQUIRY Data VPD Page */
439 target_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
441 if (cmd->data_length < 60)
445 /* Set HEADSUP, ORDSUP, SIMPSUP */
448 /* If WriteCache emulation is enabled, set V_SUP */
449 if (cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
454 /* Block Limits VPD page */
456 target_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
458 struct se_device *dev = cmd->se_dev;
462 * Following sbc3r22 section 6.5.3 Block Limits VPD page, when
463 * emulate_tpu=1 or emulate_tpws=1 we will be expect a
464 * different page length for Thin Provisioning.
466 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
469 if (cmd->data_length < (0x10 + 4)) {
470 pr_debug("Received data_length: %u"
471 " too small for EVPD 0xb0\n",
476 if (have_tp && cmd->data_length < (0x3c + 4)) {
477 pr_debug("Received data_length: %u"
478 " too small for TPE=1 EVPD 0xb0\n",
483 buf[0] = dev->transport->get_device_type(dev);
484 buf[3] = have_tp ? 0x3c : 0x10;
490 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
492 put_unaligned_be16(1, &buf[6]);
495 * Set MAXIMUM TRANSFER LENGTH
497 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_sectors, &buf[8]);
500 * Set OPTIMAL TRANSFER LENGTH
502 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.optimal_sectors, &buf[12]);
505 * Exit now if we don't support TP or the initiator sent a too
508 if (!have_tp || cmd->data_length < (0x3c + 4))
512 * Set MAXIMUM UNMAP LBA COUNT
514 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count, &buf[20]);
517 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
519 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count,
523 * Set OPTIMAL UNMAP GRANULARITY
525 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.unmap_granularity, &buf[28]);
528 * UNMAP GRANULARITY ALIGNMENT
530 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment,
532 if (dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment != 0)
533 buf[32] |= 0x80; /* Set the UGAVALID bit */
538 /* Block Device Characteristics VPD page */
540 target_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
542 struct se_device *dev = cmd->se_dev;
544 buf[0] = dev->transport->get_device_type(dev);
547 if (cmd->data_length >= 5 &&
548 dev->se_sub_dev->se_dev_attrib.is_nonrot)
554 /* Thin Provisioning VPD */
556 target_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
558 struct se_device *dev = cmd->se_dev;
561 * From sbc3r22 section 6.5.4 Thin Provisioning VPD page:
563 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
564 * zero, then the page length shall be set to 0004h. If the DP bit
565 * is set to one, then the page length shall be set to the value
566 * defined in table 162.
568 buf[0] = dev->transport->get_device_type(dev);
571 * Set Hardcoded length mentioned above for DP=0
573 put_unaligned_be16(0x0004, &buf[2]);
576 * The THRESHOLD EXPONENT field indicates the threshold set size in
577 * LBAs as a power of 2 (i.e., the threshold set size is equal to
578 * 2(threshold exponent)).
580 * Note that this is currently set to 0x00 as mkp says it will be
581 * changing again. We can enable this once it has settled in T10
582 * and is actually used by Linux/SCSI ML code.
587 * A TPU bit set to one indicates that the device server supports
588 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
589 * that the device server does not support the UNMAP command.
591 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu != 0)
595 * A TPWS bit set to one indicates that the device server supports
596 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
597 * A TPWS bit set to zero indicates that the device server does not
598 * support the use of the WRITE SAME (16) command to unmap LBAs.
600 if (dev->se_sub_dev->se_dev_attrib.emulate_tpws != 0)
607 target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
611 int (*emulate)(struct se_cmd *, unsigned char *);
612 } evpd_handlers[] = {
613 { .page = 0x00, .emulate = target_emulate_evpd_00 },
614 { .page = 0x80, .emulate = target_emulate_evpd_80 },
615 { .page = 0x83, .emulate = target_emulate_evpd_83 },
616 { .page = 0x86, .emulate = target_emulate_evpd_86 },
617 { .page = 0xb0, .emulate = target_emulate_evpd_b0 },
618 { .page = 0xb1, .emulate = target_emulate_evpd_b1 },
619 { .page = 0xb2, .emulate = target_emulate_evpd_b2 },
622 /* supported vital product data pages */
624 target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
628 if (cmd->data_length < 8)
631 * Only report the INQUIRY EVPD=1 pages after a valid NAA
632 * Registered Extended LUN WWN has been set via ConfigFS
633 * during device creation/restart.
635 if (cmd->se_dev->se_sub_dev->su_dev_flags &
636 SDF_EMULATED_VPD_UNIT_SERIAL) {
637 buf[3] = ARRAY_SIZE(evpd_handlers);
638 for (p = 0; p < min_t(int, ARRAY_SIZE(evpd_handlers),
639 cmd->data_length - 4); ++p)
640 buf[p + 4] = evpd_handlers[p].page;
647 target_emulate_inquiry(struct se_cmd *cmd)
649 struct se_device *dev = cmd->se_dev;
651 unsigned char *cdb = cmd->t_task_cdb;
655 return target_emulate_inquiry_std(cmd);
658 * Make sure we at least have 4 bytes of INQUIRY response
659 * payload for 0x00 going back for EVPD=1. Note that 0x80
660 * and 0x83 will check for enough payload data length and
661 * jump to set_len: label when there is not enough inquiry EVPD
662 * payload length left for the next outgoing EVPD metadata
664 if (cmd->data_length < 4) {
665 pr_err("SCSI Inquiry payload length: %u"
666 " too small for EVPD=1\n", cmd->data_length);
670 buf = transport_kmap_first_data_page(cmd);
672 buf[0] = dev->transport->get_device_type(dev);
674 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p)
675 if (cdb[2] == evpd_handlers[p].page) {
677 ret = evpd_handlers[p].emulate(cmd, buf);
678 transport_kunmap_first_data_page(cmd);
682 transport_kunmap_first_data_page(cmd);
683 pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
688 target_emulate_readcapacity(struct se_cmd *cmd)
690 struct se_device *dev = cmd->se_dev;
692 unsigned long long blocks_long = dev->transport->get_blocks(dev);
695 if (blocks_long >= 0x00000000ffffffff)
698 blocks = (u32)blocks_long;
700 buf = transport_kmap_first_data_page(cmd);
702 buf[0] = (blocks >> 24) & 0xff;
703 buf[1] = (blocks >> 16) & 0xff;
704 buf[2] = (blocks >> 8) & 0xff;
705 buf[3] = blocks & 0xff;
706 buf[4] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
707 buf[5] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
708 buf[6] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
709 buf[7] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
711 * Set max 32-bit blocks to signal SERVICE ACTION READ_CAPACITY_16
713 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
714 put_unaligned_be32(0xFFFFFFFF, &buf[0]);
716 transport_kunmap_first_data_page(cmd);
722 target_emulate_readcapacity_16(struct se_cmd *cmd)
724 struct se_device *dev = cmd->se_dev;
726 unsigned long long blocks = dev->transport->get_blocks(dev);
728 buf = transport_kmap_first_data_page(cmd);
730 buf[0] = (blocks >> 56) & 0xff;
731 buf[1] = (blocks >> 48) & 0xff;
732 buf[2] = (blocks >> 40) & 0xff;
733 buf[3] = (blocks >> 32) & 0xff;
734 buf[4] = (blocks >> 24) & 0xff;
735 buf[5] = (blocks >> 16) & 0xff;
736 buf[6] = (blocks >> 8) & 0xff;
737 buf[7] = blocks & 0xff;
738 buf[8] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
739 buf[9] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
740 buf[10] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
741 buf[11] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
743 * Set Thin Provisioning Enable bit following sbc3r22 in section
744 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
746 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
749 transport_kunmap_first_data_page(cmd);
755 target_modesense_rwrecovery(unsigned char *p)
764 target_modesense_control(struct se_device *dev, unsigned char *p)
770 * From spc4r23, 7.4.7 Control mode page
772 * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
773 * restrictions on the algorithm used for reordering commands
774 * having the SIMPLE task attribute (see SAM-4).
776 * Table 368 -- QUEUE ALGORITHM MODIFIER field
778 * 0h Restricted reordering
779 * 1h Unrestricted reordering allowed
781 * 8h to Fh Vendor specific
783 * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
784 * the device server shall order the processing sequence of commands
785 * having the SIMPLE task attribute such that data integrity is maintained
786 * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
787 * requests is halted at any time, the final value of all data observable
788 * on the medium shall be the same as if all the commands had been processed
789 * with the ORDERED task attribute).
791 * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
792 * device server may reorder the processing sequence of commands having the
793 * SIMPLE task attribute in any manner. Any data integrity exposures related to
794 * command sequence order shall be explicitly handled by the application client
795 * through the selection of appropriate ommands and task attributes.
797 p[3] = (dev->se_sub_dev->se_dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
799 * From spc4r17, section 7.4.6 Control mode Page
801 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
803 * 00b: The logical unit shall clear any unit attention condition
804 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
805 * status and shall not establish a unit attention condition when a com-
806 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
809 * 10b: The logical unit shall not clear any unit attention condition
810 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
811 * status and shall not establish a unit attention condition when
812 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
815 * 11b a The logical unit shall not clear any unit attention condition
816 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
817 * status and shall establish a unit attention condition for the
818 * initiator port associated with the I_T nexus on which the BUSY,
819 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
820 * Depending on the status, the additional sense code shall be set to
821 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
822 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
823 * command, a unit attention condition shall be established only once
824 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
825 * to the number of commands completed with one of those status codes.
827 p[4] = (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
828 (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
830 * From spc4r17, section 7.4.6 Control mode Page
832 * Task Aborted Status (TAS) bit set to zero.
834 * A task aborted status (TAS) bit set to zero specifies that aborted
835 * tasks shall be terminated by the device server without any response
836 * to the application client. A TAS bit set to one specifies that tasks
837 * aborted by the actions of an I_T nexus other than the I_T nexus on
838 * which the command was received shall be completed with TASK ABORTED
839 * status (see SAM-4).
841 p[5] = (dev->se_sub_dev->se_dev_attrib.emulate_tas) ? 0x40 : 0x00;
850 target_modesense_caching(struct se_device *dev, unsigned char *p)
854 if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
855 p[2] = 0x04; /* Write Cache Enable */
856 p[12] = 0x20; /* Disabled Read Ahead */
862 target_modesense_write_protect(unsigned char *buf, int type)
865 * I believe that the WP bit (bit 7) in the mode header is the same for
872 buf[0] |= 0x80; /* WP bit */
878 target_modesense_dpofua(unsigned char *buf, int type)
882 buf[0] |= 0x10; /* DPOFUA bit */
890 target_emulate_modesense(struct se_cmd *cmd, int ten)
892 struct se_device *dev = cmd->se_dev;
893 char *cdb = cmd->t_task_cdb;
895 int type = dev->transport->get_device_type(dev);
896 int offset = (ten) ? 8 : 4;
898 unsigned char buf[SE_MODE_PAGE_BUF];
900 memset(buf, 0, SE_MODE_PAGE_BUF);
902 switch (cdb[2] & 0x3f) {
904 length = target_modesense_rwrecovery(&buf[offset]);
907 length = target_modesense_caching(dev, &buf[offset]);
910 length = target_modesense_control(dev, &buf[offset]);
913 length = target_modesense_rwrecovery(&buf[offset]);
914 length += target_modesense_caching(dev, &buf[offset+length]);
915 length += target_modesense_control(dev, &buf[offset+length]);
918 pr_err("Got Unknown Mode Page: 0x%02x\n",
920 return PYX_TRANSPORT_UNKNOWN_MODE_PAGE;
926 buf[0] = (offset >> 8) & 0xff;
927 buf[1] = offset & 0xff;
929 if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
931 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
932 target_modesense_write_protect(&buf[3], type);
934 if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
935 (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
936 target_modesense_dpofua(&buf[3], type);
938 if ((offset + 2) > cmd->data_length)
939 offset = cmd->data_length;
943 buf[0] = offset & 0xff;
945 if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
947 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
948 target_modesense_write_protect(&buf[2], type);
950 if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
951 (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
952 target_modesense_dpofua(&buf[2], type);
954 if ((offset + 1) > cmd->data_length)
955 offset = cmd->data_length;
958 rbuf = transport_kmap_first_data_page(cmd);
959 memcpy(rbuf, buf, offset);
960 transport_kunmap_first_data_page(cmd);
966 target_emulate_request_sense(struct se_cmd *cmd)
968 unsigned char *cdb = cmd->t_task_cdb;
970 u8 ua_asc = 0, ua_ascq = 0;
974 pr_err("REQUEST_SENSE description emulation not"
976 return PYX_TRANSPORT_INVALID_CDB_FIELD;
979 buf = transport_kmap_first_data_page(cmd);
981 if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
983 * CURRENT ERROR, UNIT ATTENTION
986 buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
988 * Make sure request data length is enough for additional
991 if (cmd->data_length <= 18) {
997 * The Additional Sense Code (ASC) from the UNIT ATTENTION
999 buf[SPC_ASC_KEY_OFFSET] = ua_asc;
1000 buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
1004 * CURRENT ERROR, NO SENSE
1007 buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
1009 * Make sure request data length is enough for additional
1012 if (cmd->data_length <= 18) {
1018 * NO ADDITIONAL SENSE INFORMATION
1020 buf[SPC_ASC_KEY_OFFSET] = 0x00;
1025 transport_kunmap_first_data_page(cmd);
1031 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
1032 * Note this is not used for TCM/pSCSI passthrough
1035 target_emulate_unmap(struct se_task *task)
1037 struct se_cmd *cmd = task->task_se_cmd;
1038 struct se_device *dev = cmd->se_dev;
1039 unsigned char *buf, *ptr = NULL;
1040 unsigned char *cdb = &cmd->t_task_cdb[0];
1042 unsigned int size = cmd->data_length, range;
1043 int ret = 0, offset;
1044 unsigned short dl, bd_dl;
1046 /* First UNMAP block descriptor starts at 8 byte offset */
1049 dl = get_unaligned_be16(&cdb[0]);
1050 bd_dl = get_unaligned_be16(&cdb[2]);
1052 buf = transport_kmap_first_data_page(cmd);
1055 pr_debug("UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
1056 " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
1059 lba = get_unaligned_be64(&ptr[0]);
1060 range = get_unaligned_be32(&ptr[8]);
1061 pr_debug("UNMAP: Using lba: %llu and range: %u\n",
1062 (unsigned long long)lba, range);
1064 ret = dev->transport->do_discard(dev, lba, range);
1066 pr_err("blkdev_issue_discard() failed: %d\n",
1075 task->task_scsi_status = GOOD;
1076 transport_complete_task(task, 1);
1078 transport_kunmap_first_data_page(cmd);
1084 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
1085 * Note this is not used for TCM/pSCSI passthrough
1088 target_emulate_write_same(struct se_task *task, int write_same32)
1090 struct se_cmd *cmd = task->task_se_cmd;
1091 struct se_device *dev = cmd->se_dev;
1093 sector_t lba = cmd->t_task_lba;
1094 unsigned int num_blocks;
1097 * Extract num_blocks from the WRITE_SAME_* CDB. Then use the explict
1098 * range when non zero is supplied, otherwise calculate the remaining
1099 * range based on ->get_blocks() - starting LBA.
1102 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
1104 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
1106 if (num_blocks != 0)
1109 range = (dev->transport->get_blocks(dev) - lba);
1111 pr_debug("WRITE_SAME UNMAP: LBA: %llu Range: %llu\n",
1112 (unsigned long long)lba, (unsigned long long)range);
1114 ret = dev->transport->do_discard(dev, lba, range);
1116 pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
1120 task->task_scsi_status = GOOD;
1121 transport_complete_task(task, 1);
1126 transport_emulate_control_cdb(struct se_task *task)
1128 struct se_cmd *cmd = task->task_se_cmd;
1129 struct se_device *dev = cmd->se_dev;
1130 unsigned short service_action;
1133 switch (cmd->t_task_cdb[0]) {
1135 ret = target_emulate_inquiry(cmd);
1138 ret = target_emulate_readcapacity(cmd);
1141 ret = target_emulate_modesense(cmd, 0);
1144 ret = target_emulate_modesense(cmd, 1);
1146 case SERVICE_ACTION_IN:
1147 switch (cmd->t_task_cdb[1] & 0x1f) {
1148 case SAI_READ_CAPACITY_16:
1149 ret = target_emulate_readcapacity_16(cmd);
1152 pr_err("Unsupported SA: 0x%02x\n",
1153 cmd->t_task_cdb[1] & 0x1f);
1154 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1158 ret = target_emulate_request_sense(cmd);
1161 if (!dev->transport->do_discard) {
1162 pr_err("UNMAP emulation not supported for: %s\n",
1163 dev->transport->name);
1164 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1166 ret = target_emulate_unmap(task);
1169 if (!dev->transport->do_discard) {
1170 pr_err("WRITE_SAME_16 emulation not supported"
1171 " for: %s\n", dev->transport->name);
1172 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1174 ret = target_emulate_write_same(task, 0);
1176 case VARIABLE_LENGTH_CMD:
1178 get_unaligned_be16(&cmd->t_task_cdb[8]);
1179 switch (service_action) {
1181 if (!dev->transport->do_discard) {
1182 pr_err("WRITE_SAME_32 SA emulation not"
1183 " supported for: %s\n",
1184 dev->transport->name);
1185 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1187 ret = target_emulate_write_same(task, 1);
1190 pr_err("Unsupported VARIABLE_LENGTH_CMD SA:"
1191 " 0x%02x\n", service_action);
1195 case SYNCHRONIZE_CACHE:
1196 case 0x91: /* SYNCHRONIZE_CACHE_16: */
1197 if (!dev->transport->do_sync_cache) {
1198 pr_err("SYNCHRONIZE_CACHE emulation not supported"
1199 " for: %s\n", dev->transport->name);
1200 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1202 dev->transport->do_sync_cache(task);
1204 case ALLOW_MEDIUM_REMOVAL:
1210 case TEST_UNIT_READY:
1212 case WRITE_FILEMARKS:
1215 pr_err("Unsupported SCSI Opcode: 0x%02x for %s\n",
1216 cmd->t_task_cdb[0], dev->transport->name);
1217 return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1222 task->task_scsi_status = GOOD;
1223 transport_complete_task(task, 1);
1225 return PYX_TRANSPORT_SENT_TO_TRANSPORT;