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 <linux/module.h>
28 #include <asm/unaligned.h>
29 #include <scsi/scsi.h>
31 #include <target/target_core_base.h>
32 #include <target/target_core_backend.h>
33 #include <target/target_core_fabric.h>
35 #include "target_core_internal.h"
36 #include "target_core_ua.h"
39 target_fill_alua_data(struct se_port *port, unsigned char *buf)
41 struct t10_alua_tg_pt_gp *tg_pt_gp;
42 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
45 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
50 * Set TPGS field for explict and/or implict ALUA access type
53 * See spc4r17 section 6.4.2 Table 135
57 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
61 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
62 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
64 buf[5] |= tg_pt_gp->tg_pt_gp_alua_access_type;
65 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
69 target_emulate_inquiry_std(struct se_cmd *cmd)
71 struct se_lun *lun = cmd->se_lun;
72 struct se_device *dev = cmd->se_dev;
73 struct se_portal_group *tpg = lun->lun_sep->sep_tpg;
77 * Make sure we at least have 6 bytes of INQUIRY response
78 * payload going back for EVPD=0
80 if (cmd->data_length < 6) {
81 pr_err("SCSI Inquiry payload length: %u"
82 " too small for EVPD=0\n", cmd->data_length);
86 buf = transport_kmap_first_data_page(cmd);
88 if (dev == tpg->tpg_virt_lun0.lun_se_dev) {
89 buf[0] = 0x3f; /* Not connected */
91 buf[0] = dev->transport->get_device_type(dev);
92 if (buf[0] == TYPE_TAPE)
95 buf[2] = dev->transport->get_device_rev(dev);
98 * Enable SCCS and TPGS fields for Emulated ALUA
100 if (dev->se_sub_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED)
101 target_fill_alua_data(lun->lun_sep, buf);
103 if (cmd->data_length < 8) {
104 buf[4] = 1; /* Set additional length to 1 */
108 buf[7] = 0x32; /* Sync=1 and CmdQue=1 */
111 * Do not include vendor, product, reversion info in INQUIRY
112 * response payload for cdbs with a small allocation length.
114 if (cmd->data_length < 36) {
115 buf[4] = 3; /* Set additional length to 3 */
119 snprintf((unsigned char *)&buf[8], 8, "LIO-ORG");
120 snprintf((unsigned char *)&buf[16], 16, "%s",
121 &dev->se_sub_dev->t10_wwn.model[0]);
122 snprintf((unsigned char *)&buf[32], 4, "%s",
123 &dev->se_sub_dev->t10_wwn.revision[0]);
124 buf[4] = 31; /* Set additional length to 31 */
127 transport_kunmap_first_data_page(cmd);
131 /* unit serial number */
133 target_emulate_evpd_80(struct se_cmd *cmd, unsigned char *buf)
135 struct se_device *dev = cmd->se_dev;
138 if (dev->se_sub_dev->su_dev_flags &
139 SDF_EMULATED_VPD_UNIT_SERIAL) {
143 strlen(&dev->se_sub_dev->t10_wwn.unit_serial[0]);
144 unit_serial_len++; /* For NULL Terminator */
146 if (((len + 4) + unit_serial_len) > cmd->data_length) {
147 len += unit_serial_len;
148 buf[2] = ((len >> 8) & 0xff);
149 buf[3] = (len & 0xff);
152 len += sprintf((unsigned char *)&buf[4], "%s",
153 &dev->se_sub_dev->t10_wwn.unit_serial[0]);
154 len++; /* Extra Byte for NULL Terminator */
161 target_parse_naa_6h_vendor_specific(struct se_device *dev, unsigned char *buf)
163 unsigned char *p = &dev->se_sub_dev->t10_wwn.unit_serial[0];
168 * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on
169 * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field
170 * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION
171 * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL
172 * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure
173 * per device uniqeness.
175 for (cnt = 0; *p && cnt < 13; p++) {
176 int val = hex_to_bin(*p);
192 * Device identification VPD, for a complete list of
193 * DESIGNATOR TYPEs see spc4r17 Table 459.
196 target_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
198 struct se_device *dev = cmd->se_dev;
199 struct se_lun *lun = cmd->se_lun;
200 struct se_port *port = NULL;
201 struct se_portal_group *tpg = NULL;
202 struct t10_alua_lu_gp_member *lu_gp_mem;
203 struct t10_alua_tg_pt_gp *tg_pt_gp;
204 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
205 unsigned char *prod = &dev->se_sub_dev->t10_wwn.model[0];
207 u32 unit_serial_len, off = 0;
213 * NAA IEEE Registered Extended Assigned designator format, see
214 * spc4r17 section 7.7.3.6.5
216 * We depend upon a target_core_mod/ConfigFS provided
217 * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
218 * value in order to return the NAA id.
220 if (!(dev->se_sub_dev->su_dev_flags & SDF_EMULATED_VPD_UNIT_SERIAL))
221 goto check_t10_vend_desc;
223 if (off + 20 > cmd->data_length)
224 goto check_t10_vend_desc;
226 /* CODE SET == Binary */
229 /* Set ASSOCIATION == addressed logical unit: 0)b */
232 /* Identifier/Designator type == NAA identifier */
236 /* Identifier/Designator length */
240 * Start NAA IEEE Registered Extended Identifier/Designator
242 buf[off++] = (0x6 << 4);
245 * Use OpenFabrics IEEE Company ID: 00 14 05
249 buf[off] = (0x5 << 4);
252 * Return ConfigFS Unit Serial Number information for
253 * VENDOR_SPECIFIC_IDENTIFIER and
254 * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
256 target_parse_naa_6h_vendor_specific(dev, &buf[off]);
263 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
265 id_len = 8; /* For Vendor field */
266 prod_len = 4; /* For VPD Header */
267 prod_len += 8; /* For Vendor field */
268 prod_len += strlen(prod);
269 prod_len++; /* For : */
271 if (dev->se_sub_dev->su_dev_flags &
272 SDF_EMULATED_VPD_UNIT_SERIAL) {
274 strlen(&dev->se_sub_dev->t10_wwn.unit_serial[0]);
275 unit_serial_len++; /* For NULL Terminator */
277 if ((len + (id_len + 4) +
278 (prod_len + unit_serial_len)) >
280 len += (prod_len + unit_serial_len);
283 id_len += sprintf((unsigned char *)&buf[off+12],
285 &dev->se_sub_dev->t10_wwn.unit_serial[0]);
287 buf[off] = 0x2; /* ASCII */
288 buf[off+1] = 0x1; /* T10 Vendor ID */
290 memcpy((unsigned char *)&buf[off+4], "LIO-ORG", 8);
291 /* Extra Byte for NULL Terminator */
293 /* Identifier Length */
295 /* Header size for Designation descriptor */
299 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
304 struct t10_alua_lu_gp *lu_gp;
305 u32 padding, scsi_name_len;
312 * Relative target port identifer, see spc4r17
315 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
316 * section 7.5.1 Table 362
318 if (((len + 4) + 8) > cmd->data_length) {
323 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
324 buf[off++] |= 0x1; /* CODE SET == Binary */
325 buf[off] = 0x80; /* Set PIV=1 */
326 /* Set ASSOCIATION == target port: 01b */
328 /* DESIGNATOR TYPE == Relative target port identifer */
330 off++; /* Skip over Reserved */
331 buf[off++] = 4; /* DESIGNATOR LENGTH */
332 /* Skip over Obsolete field in RTPI payload
335 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
336 buf[off++] = (port->sep_rtpi & 0xff);
337 len += 8; /* Header size + Designation descriptor */
339 * Target port group identifier, see spc4r17
342 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
343 * section 7.5.1 Table 362
346 if (dev->se_sub_dev->t10_alua.alua_type !=
348 goto check_scsi_name;
350 if (((len + 4) + 8) > cmd->data_length) {
354 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
358 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
359 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
361 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
364 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
365 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
368 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
369 buf[off++] |= 0x1; /* CODE SET == Binary */
370 buf[off] = 0x80; /* Set PIV=1 */
371 /* Set ASSOCIATION == target port: 01b */
373 /* DESIGNATOR TYPE == Target port group identifier */
375 off++; /* Skip over Reserved */
376 buf[off++] = 4; /* DESIGNATOR LENGTH */
377 off += 2; /* Skip over Reserved Field */
378 buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
379 buf[off++] = (tg_pt_gp_id & 0xff);
380 len += 8; /* Header size + Designation descriptor */
382 * Logical Unit Group identifier, see spc4r17
386 if (((len + 4) + 8) > cmd->data_length) {
388 goto check_scsi_name;
390 lu_gp_mem = dev->dev_alua_lu_gp_mem;
392 goto check_scsi_name;
394 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
395 lu_gp = lu_gp_mem->lu_gp;
397 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
398 goto check_scsi_name;
400 lu_gp_id = lu_gp->lu_gp_id;
401 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
403 buf[off++] |= 0x1; /* CODE SET == Binary */
404 /* DESIGNATOR TYPE == Logical Unit Group identifier */
406 off++; /* Skip over Reserved */
407 buf[off++] = 4; /* DESIGNATOR LENGTH */
408 off += 2; /* Skip over Reserved Field */
409 buf[off++] = ((lu_gp_id >> 8) & 0xff);
410 buf[off++] = (lu_gp_id & 0xff);
411 len += 8; /* Header size + Designation descriptor */
413 * SCSI name string designator, see spc4r17
416 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
417 * section 7.5.1 Table 362
420 scsi_name_len = strlen(tpg->se_tpg_tfo->tpg_get_wwn(tpg));
421 /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
423 /* Check for 4-byte padding */
424 padding = ((-scsi_name_len) & 3);
426 scsi_name_len += padding;
427 /* Header size + Designation descriptor */
430 if (((len + 4) + scsi_name_len) > cmd->data_length) {
431 len += scsi_name_len;
435 (tpg->se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
436 buf[off++] |= 0x3; /* CODE SET == UTF-8 */
437 buf[off] = 0x80; /* Set PIV=1 */
438 /* Set ASSOCIATION == target port: 01b */
440 /* DESIGNATOR TYPE == SCSI name string */
442 off += 2; /* Skip over Reserved and length */
444 * SCSI name string identifer containing, $FABRIC_MOD
445 * dependent information. For LIO-Target and iSCSI
446 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
449 tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
450 scsi_name_len = sprintf(&buf[off], "%s,t,0x%04x",
451 tpg->se_tpg_tfo->tpg_get_wwn(tpg), tpgt);
452 scsi_name_len += 1 /* Include NULL terminator */;
454 * The null-terminated, null-padded (see 4.4.2) SCSI
455 * NAME STRING field contains a UTF-8 format string.
456 * The number of bytes in the SCSI NAME STRING field
457 * (i.e., the value in the DESIGNATOR LENGTH field)
458 * shall be no larger than 256 and shall be a multiple
462 scsi_name_len += padding;
464 buf[off-1] = scsi_name_len;
465 off += scsi_name_len;
466 /* Header size + Designation descriptor */
467 len += (scsi_name_len + 4);
470 buf[2] = ((len >> 8) & 0xff);
471 buf[3] = (len & 0xff); /* Page Length for VPD 0x83 */
475 /* Extended INQUIRY Data VPD Page */
477 target_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
479 if (cmd->data_length < 60)
483 /* Set HEADSUP, ORDSUP, SIMPSUP */
486 /* If WriteCache emulation is enabled, set V_SUP */
487 if (cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
492 /* Block Limits VPD page */
494 target_emulate_evpd_b0(struct se_cmd *cmd, unsigned char *buf)
496 struct se_device *dev = cmd->se_dev;
500 * Following sbc3r22 section 6.5.3 Block Limits VPD page, when
501 * emulate_tpu=1 or emulate_tpws=1 we will be expect a
502 * different page length for Thin Provisioning.
504 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
507 if (cmd->data_length < (0x10 + 4)) {
508 pr_debug("Received data_length: %u"
509 " too small for EVPD 0xb0\n",
514 if (have_tp && cmd->data_length < (0x3c + 4)) {
515 pr_debug("Received data_length: %u"
516 " too small for TPE=1 EVPD 0xb0\n",
521 buf[0] = dev->transport->get_device_type(dev);
522 buf[3] = have_tp ? 0x3c : 0x10;
528 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
530 put_unaligned_be16(1, &buf[6]);
533 * Set MAXIMUM TRANSFER LENGTH
535 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_sectors, &buf[8]);
538 * Set OPTIMAL TRANSFER LENGTH
540 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.optimal_sectors, &buf[12]);
543 * Exit now if we don't support TP or the initiator sent a too
546 if (!have_tp || cmd->data_length < (0x3c + 4))
550 * Set MAXIMUM UNMAP LBA COUNT
552 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count, &buf[20]);
555 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
557 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count,
561 * Set OPTIMAL UNMAP GRANULARITY
563 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.unmap_granularity, &buf[28]);
566 * UNMAP GRANULARITY ALIGNMENT
568 put_unaligned_be32(dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment,
570 if (dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment != 0)
571 buf[32] |= 0x80; /* Set the UGAVALID bit */
576 /* Block Device Characteristics VPD page */
578 target_emulate_evpd_b1(struct se_cmd *cmd, unsigned char *buf)
580 struct se_device *dev = cmd->se_dev;
582 buf[0] = dev->transport->get_device_type(dev);
585 if (cmd->data_length >= 5 &&
586 dev->se_sub_dev->se_dev_attrib.is_nonrot)
592 /* Thin Provisioning VPD */
594 target_emulate_evpd_b2(struct se_cmd *cmd, unsigned char *buf)
596 struct se_device *dev = cmd->se_dev;
599 * From sbc3r22 section 6.5.4 Thin Provisioning VPD page:
601 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
602 * zero, then the page length shall be set to 0004h. If the DP bit
603 * is set to one, then the page length shall be set to the value
604 * defined in table 162.
606 buf[0] = dev->transport->get_device_type(dev);
609 * Set Hardcoded length mentioned above for DP=0
611 put_unaligned_be16(0x0004, &buf[2]);
614 * The THRESHOLD EXPONENT field indicates the threshold set size in
615 * LBAs as a power of 2 (i.e., the threshold set size is equal to
616 * 2(threshold exponent)).
618 * Note that this is currently set to 0x00 as mkp says it will be
619 * changing again. We can enable this once it has settled in T10
620 * and is actually used by Linux/SCSI ML code.
625 * A TPU bit set to one indicates that the device server supports
626 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
627 * that the device server does not support the UNMAP command.
629 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu != 0)
633 * A TPWS bit set to one indicates that the device server supports
634 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
635 * A TPWS bit set to zero indicates that the device server does not
636 * support the use of the WRITE SAME (16) command to unmap LBAs.
638 if (dev->se_sub_dev->se_dev_attrib.emulate_tpws != 0)
645 target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf);
649 int (*emulate)(struct se_cmd *, unsigned char *);
650 } evpd_handlers[] = {
651 { .page = 0x00, .emulate = target_emulate_evpd_00 },
652 { .page = 0x80, .emulate = target_emulate_evpd_80 },
653 { .page = 0x83, .emulate = target_emulate_evpd_83 },
654 { .page = 0x86, .emulate = target_emulate_evpd_86 },
655 { .page = 0xb0, .emulate = target_emulate_evpd_b0 },
656 { .page = 0xb1, .emulate = target_emulate_evpd_b1 },
657 { .page = 0xb2, .emulate = target_emulate_evpd_b2 },
660 /* supported vital product data pages */
662 target_emulate_evpd_00(struct se_cmd *cmd, unsigned char *buf)
666 if (cmd->data_length < 8)
669 * Only report the INQUIRY EVPD=1 pages after a valid NAA
670 * Registered Extended LUN WWN has been set via ConfigFS
671 * during device creation/restart.
673 if (cmd->se_dev->se_sub_dev->su_dev_flags &
674 SDF_EMULATED_VPD_UNIT_SERIAL) {
675 buf[3] = ARRAY_SIZE(evpd_handlers);
676 for (p = 0; p < min_t(int, ARRAY_SIZE(evpd_handlers),
677 cmd->data_length - 4); ++p)
678 buf[p + 4] = evpd_handlers[p].page;
684 int target_emulate_inquiry(struct se_task *task)
686 struct se_cmd *cmd = task->task_se_cmd;
687 struct se_device *dev = cmd->se_dev;
689 unsigned char *cdb = cmd->t_task_cdb;
692 if (!(cdb[1] & 0x1)) {
693 ret = target_emulate_inquiry_std(cmd);
698 * Make sure we at least have 4 bytes of INQUIRY response
699 * payload for 0x00 going back for EVPD=1. Note that 0x80
700 * and 0x83 will check for enough payload data length and
701 * jump to set_len: label when there is not enough inquiry EVPD
702 * payload length left for the next outgoing EVPD metadata
704 if (cmd->data_length < 4) {
705 pr_err("SCSI Inquiry payload length: %u"
706 " too small for EVPD=1\n", cmd->data_length);
707 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
711 buf = transport_kmap_first_data_page(cmd);
713 buf[0] = dev->transport->get_device_type(dev);
715 for (p = 0; p < ARRAY_SIZE(evpd_handlers); ++p) {
716 if (cdb[2] == evpd_handlers[p].page) {
718 ret = evpd_handlers[p].emulate(cmd, buf);
723 pr_err("Unknown VPD Code: 0x%02x\n", cdb[2]);
724 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
728 transport_kunmap_first_data_page(cmd);
731 task->task_scsi_status = GOOD;
732 transport_complete_task(task, 1);
737 int target_emulate_readcapacity(struct se_task *task)
739 struct se_cmd *cmd = task->task_se_cmd;
740 struct se_device *dev = cmd->se_dev;
742 unsigned long long blocks_long = dev->transport->get_blocks(dev);
745 if (blocks_long >= 0x00000000ffffffff)
748 blocks = (u32)blocks_long;
750 buf = transport_kmap_first_data_page(cmd);
752 buf[0] = (blocks >> 24) & 0xff;
753 buf[1] = (blocks >> 16) & 0xff;
754 buf[2] = (blocks >> 8) & 0xff;
755 buf[3] = blocks & 0xff;
756 buf[4] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
757 buf[5] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
758 buf[6] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
759 buf[7] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
761 * Set max 32-bit blocks to signal SERVICE ACTION READ_CAPACITY_16
763 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
764 put_unaligned_be32(0xFFFFFFFF, &buf[0]);
766 transport_kunmap_first_data_page(cmd);
768 task->task_scsi_status = GOOD;
769 transport_complete_task(task, 1);
773 int target_emulate_readcapacity_16(struct se_task *task)
775 struct se_cmd *cmd = task->task_se_cmd;
776 struct se_device *dev = cmd->se_dev;
778 unsigned long long blocks = dev->transport->get_blocks(dev);
780 buf = transport_kmap_first_data_page(cmd);
782 buf[0] = (blocks >> 56) & 0xff;
783 buf[1] = (blocks >> 48) & 0xff;
784 buf[2] = (blocks >> 40) & 0xff;
785 buf[3] = (blocks >> 32) & 0xff;
786 buf[4] = (blocks >> 24) & 0xff;
787 buf[5] = (blocks >> 16) & 0xff;
788 buf[6] = (blocks >> 8) & 0xff;
789 buf[7] = blocks & 0xff;
790 buf[8] = (dev->se_sub_dev->se_dev_attrib.block_size >> 24) & 0xff;
791 buf[9] = (dev->se_sub_dev->se_dev_attrib.block_size >> 16) & 0xff;
792 buf[10] = (dev->se_sub_dev->se_dev_attrib.block_size >> 8) & 0xff;
793 buf[11] = dev->se_sub_dev->se_dev_attrib.block_size & 0xff;
795 * Set Thin Provisioning Enable bit following sbc3r22 in section
796 * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
798 if (dev->se_sub_dev->se_dev_attrib.emulate_tpu || dev->se_sub_dev->se_dev_attrib.emulate_tpws)
801 transport_kunmap_first_data_page(cmd);
803 task->task_scsi_status = GOOD;
804 transport_complete_task(task, 1);
809 target_modesense_rwrecovery(unsigned char *p)
818 target_modesense_control(struct se_device *dev, unsigned char *p)
824 * From spc4r23, 7.4.7 Control mode page
826 * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
827 * restrictions on the algorithm used for reordering commands
828 * having the SIMPLE task attribute (see SAM-4).
830 * Table 368 -- QUEUE ALGORITHM MODIFIER field
832 * 0h Restricted reordering
833 * 1h Unrestricted reordering allowed
835 * 8h to Fh Vendor specific
837 * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
838 * the device server shall order the processing sequence of commands
839 * having the SIMPLE task attribute such that data integrity is maintained
840 * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
841 * requests is halted at any time, the final value of all data observable
842 * on the medium shall be the same as if all the commands had been processed
843 * with the ORDERED task attribute).
845 * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
846 * device server may reorder the processing sequence of commands having the
847 * SIMPLE task attribute in any manner. Any data integrity exposures related to
848 * command sequence order shall be explicitly handled by the application client
849 * through the selection of appropriate ommands and task attributes.
851 p[3] = (dev->se_sub_dev->se_dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
853 * From spc4r17, section 7.4.6 Control mode Page
855 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
857 * 00b: The logical unit shall clear any unit attention condition
858 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
859 * status and shall not establish a unit attention condition when a com-
860 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
863 * 10b: The logical unit shall not clear any unit attention condition
864 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
865 * status and shall not establish a unit attention condition when
866 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
869 * 11b a The logical unit shall not clear any unit attention condition
870 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
871 * status and shall establish a unit attention condition for the
872 * initiator port associated with the I_T nexus on which the BUSY,
873 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
874 * Depending on the status, the additional sense code shall be set to
875 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
876 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
877 * command, a unit attention condition shall be established only once
878 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
879 * to the number of commands completed with one of those status codes.
881 p[4] = (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
882 (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
884 * From spc4r17, section 7.4.6 Control mode Page
886 * Task Aborted Status (TAS) bit set to zero.
888 * A task aborted status (TAS) bit set to zero specifies that aborted
889 * tasks shall be terminated by the device server without any response
890 * to the application client. A TAS bit set to one specifies that tasks
891 * aborted by the actions of an I_T nexus other than the I_T nexus on
892 * which the command was received shall be completed with TASK ABORTED
893 * status (see SAM-4).
895 p[5] = (dev->se_sub_dev->se_dev_attrib.emulate_tas) ? 0x40 : 0x00;
904 target_modesense_caching(struct se_device *dev, unsigned char *p)
908 if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
909 p[2] = 0x04; /* Write Cache Enable */
910 p[12] = 0x20; /* Disabled Read Ahead */
916 target_modesense_write_protect(unsigned char *buf, int type)
919 * I believe that the WP bit (bit 7) in the mode header is the same for
926 buf[0] |= 0x80; /* WP bit */
932 target_modesense_dpofua(unsigned char *buf, int type)
936 buf[0] |= 0x10; /* DPOFUA bit */
943 int target_emulate_modesense(struct se_task *task)
945 struct se_cmd *cmd = task->task_se_cmd;
946 struct se_device *dev = cmd->se_dev;
947 char *cdb = cmd->t_task_cdb;
949 int type = dev->transport->get_device_type(dev);
950 int ten = (cmd->t_task_cdb[0] == MODE_SENSE_10);
951 int offset = ten ? 8 : 4;
953 unsigned char buf[SE_MODE_PAGE_BUF];
955 memset(buf, 0, SE_MODE_PAGE_BUF);
957 switch (cdb[2] & 0x3f) {
959 length = target_modesense_rwrecovery(&buf[offset]);
962 length = target_modesense_caching(dev, &buf[offset]);
965 length = target_modesense_control(dev, &buf[offset]);
968 length = target_modesense_rwrecovery(&buf[offset]);
969 length += target_modesense_caching(dev, &buf[offset+length]);
970 length += target_modesense_control(dev, &buf[offset+length]);
973 pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
974 cdb[2] & 0x3f, cdb[3]);
975 cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
982 buf[0] = (offset >> 8) & 0xff;
983 buf[1] = offset & 0xff;
985 if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
987 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
988 target_modesense_write_protect(&buf[3], type);
990 if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
991 (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
992 target_modesense_dpofua(&buf[3], type);
994 if ((offset + 2) > cmd->data_length)
995 offset = cmd->data_length;
999 buf[0] = offset & 0xff;
1001 if ((cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ||
1003 (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
1004 target_modesense_write_protect(&buf[2], type);
1006 if ((dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
1007 (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
1008 target_modesense_dpofua(&buf[2], type);
1010 if ((offset + 1) > cmd->data_length)
1011 offset = cmd->data_length;
1014 rbuf = transport_kmap_first_data_page(cmd);
1015 memcpy(rbuf, buf, offset);
1016 transport_kunmap_first_data_page(cmd);
1018 task->task_scsi_status = GOOD;
1019 transport_complete_task(task, 1);
1023 int target_emulate_request_sense(struct se_task *task)
1025 struct se_cmd *cmd = task->task_se_cmd;
1026 unsigned char *cdb = cmd->t_task_cdb;
1028 u8 ua_asc = 0, ua_ascq = 0;
1031 if (cdb[1] & 0x01) {
1032 pr_err("REQUEST_SENSE description emulation not"
1034 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1038 buf = transport_kmap_first_data_page(cmd);
1040 if (!core_scsi3_ua_clear_for_request_sense(cmd, &ua_asc, &ua_ascq)) {
1042 * CURRENT ERROR, UNIT ATTENTION
1045 buf[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
1047 * Make sure request data length is enough for additional
1050 if (cmd->data_length <= 18) {
1056 * The Additional Sense Code (ASC) from the UNIT ATTENTION
1058 buf[SPC_ASC_KEY_OFFSET] = ua_asc;
1059 buf[SPC_ASCQ_KEY_OFFSET] = ua_ascq;
1063 * CURRENT ERROR, NO SENSE
1066 buf[SPC_SENSE_KEY_OFFSET] = NO_SENSE;
1068 * Make sure request data length is enough for additional
1071 if (cmd->data_length <= 18) {
1077 * NO ADDITIONAL SENSE INFORMATION
1079 buf[SPC_ASC_KEY_OFFSET] = 0x00;
1084 transport_kunmap_first_data_page(cmd);
1085 task->task_scsi_status = GOOD;
1086 transport_complete_task(task, 1);
1091 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
1092 * Note this is not used for TCM/pSCSI passthrough
1094 int target_emulate_unmap(struct se_task *task)
1096 struct se_cmd *cmd = task->task_se_cmd;
1097 struct se_device *dev = cmd->se_dev;
1098 unsigned char *buf, *ptr = NULL;
1099 unsigned char *cdb = &cmd->t_task_cdb[0];
1101 unsigned int size = cmd->data_length, range;
1102 int ret = 0, offset;
1103 unsigned short dl, bd_dl;
1105 if (!dev->transport->do_discard) {
1106 pr_err("UNMAP emulation not supported for: %s\n",
1107 dev->transport->name);
1108 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1112 /* First UNMAP block descriptor starts at 8 byte offset */
1115 dl = get_unaligned_be16(&cdb[0]);
1116 bd_dl = get_unaligned_be16(&cdb[2]);
1118 buf = transport_kmap_first_data_page(cmd);
1121 pr_debug("UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
1122 " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
1125 lba = get_unaligned_be64(&ptr[0]);
1126 range = get_unaligned_be32(&ptr[8]);
1127 pr_debug("UNMAP: Using lba: %llu and range: %u\n",
1128 (unsigned long long)lba, range);
1130 ret = dev->transport->do_discard(dev, lba, range);
1132 pr_err("blkdev_issue_discard() failed: %d\n",
1142 transport_kunmap_first_data_page(cmd);
1144 task->task_scsi_status = GOOD;
1145 transport_complete_task(task, 1);
1151 * Used for TCM/IBLOCK and TCM/FILEIO for block/blk-lib.c level discard support.
1152 * Note this is not used for TCM/pSCSI passthrough
1154 int target_emulate_write_same(struct se_task *task)
1156 struct se_cmd *cmd = task->task_se_cmd;
1157 struct se_device *dev = cmd->se_dev;
1159 sector_t lba = cmd->t_task_lba;
1163 if (!dev->transport->do_discard) {
1164 pr_err("WRITE_SAME emulation not supported"
1165 " for: %s\n", dev->transport->name);
1166 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1170 if (cmd->t_task_cdb[0] == WRITE_SAME)
1171 num_blocks = get_unaligned_be16(&cmd->t_task_cdb[7]);
1172 else if (cmd->t_task_cdb[0] == WRITE_SAME_16)
1173 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[10]);
1174 else /* WRITE_SAME_32 via VARIABLE_LENGTH_CMD */
1175 num_blocks = get_unaligned_be32(&cmd->t_task_cdb[28]);
1178 * Use the explicit range when non zero is supplied, otherwise calculate
1179 * the remaining range based on ->get_blocks() - starting LBA.
1181 if (num_blocks != 0)
1184 range = (dev->transport->get_blocks(dev) - lba);
1186 pr_debug("WRITE_SAME UNMAP: LBA: %llu Range: %llu\n",
1187 (unsigned long long)lba, (unsigned long long)range);
1189 ret = dev->transport->do_discard(dev, lba, range);
1191 pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
1195 task->task_scsi_status = GOOD;
1196 transport_complete_task(task, 1);
1200 int target_emulate_synchronize_cache(struct se_task *task)
1202 struct se_device *dev = task->task_se_cmd->se_dev;
1203 struct se_cmd *cmd = task->task_se_cmd;
1205 if (!dev->transport->do_sync_cache) {
1206 pr_err("SYNCHRONIZE_CACHE emulation not supported"
1207 " for: %s\n", dev->transport->name);
1208 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1212 dev->transport->do_sync_cache(task);
1216 int target_emulate_noop(struct se_task *task)
1218 task->task_scsi_status = GOOD;
1219 transport_complete_task(task, 1);
1224 * Write a CDB into @cdb that is based on the one the intiator sent us,
1225 * but updated to only cover the sectors that the current task handles.
1227 void target_get_task_cdb(struct se_task *task, unsigned char *cdb)
1229 struct se_cmd *cmd = task->task_se_cmd;
1230 unsigned int cdb_len = scsi_command_size(cmd->t_task_cdb);
1232 memcpy(cdb, cmd->t_task_cdb, cdb_len);
1233 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
1234 unsigned long long lba = task->task_lba;
1235 u32 sectors = task->task_sectors;
1239 /* 21-bit LBA and 8-bit sectors */
1240 cdb[1] = (lba >> 16) & 0x1f;
1241 cdb[2] = (lba >> 8) & 0xff;
1242 cdb[3] = lba & 0xff;
1243 cdb[4] = sectors & 0xff;
1246 /* 32-bit LBA and 16-bit sectors */
1247 put_unaligned_be32(lba, &cdb[2]);
1248 put_unaligned_be16(sectors, &cdb[7]);
1251 /* 32-bit LBA and 32-bit sectors */
1252 put_unaligned_be32(lba, &cdb[2]);
1253 put_unaligned_be32(sectors, &cdb[6]);
1256 /* 64-bit LBA and 32-bit sectors */
1257 put_unaligned_be64(lba, &cdb[2]);
1258 put_unaligned_be32(sectors, &cdb[10]);
1261 /* 64-bit LBA and 32-bit sectors, extended CDB */
1262 put_unaligned_be64(lba, &cdb[12]);
1263 put_unaligned_be32(sectors, &cdb[28]);
1270 EXPORT_SYMBOL(target_get_task_cdb);