2 * SCSI Primary Commands (SPC) parsing and emulation.
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>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_tcq.h>
33 #include <target/target_core_base.h>
34 #include <target/target_core_backend.h>
35 #include <target/target_core_fabric.h>
37 #include "target_core_internal.h"
38 #include "target_core_alua.h"
39 #include "target_core_pr.h"
40 #include "target_core_ua.h"
43 static void spc_fill_alua_data(struct se_port
*port
, unsigned char *buf
)
45 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
46 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
49 * Set SCCS for MAINTENANCE_IN + REPORT_TARGET_PORT_GROUPS.
54 * Set TPGS field for explict and/or implict ALUA access type
57 * See spc4r17 section 6.4.2 Table 135
61 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
65 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
66 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
68 buf
[5] |= tg_pt_gp
->tg_pt_gp_alua_access_type
;
69 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
72 static int spc_emulate_inquiry_std(struct se_cmd
*cmd
, char *buf
)
74 struct se_lun
*lun
= cmd
->se_lun
;
75 struct se_device
*dev
= cmd
->se_dev
;
77 /* Set RMB (removable media) for tape devices */
78 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
)
81 buf
[2] = dev
->transport
->get_device_rev(dev
);
84 * NORMACA and HISUP = 0, RESPONSE DATA FORMAT = 2
87 * A RESPONSE DATA FORMAT field set to 2h indicates that the
88 * standard INQUIRY data is in the format defined in this
89 * standard. Response data format values less than 2h are
90 * obsolete. Response data format values greater than 2h are
96 * Enable SCCS and TPGS fields for Emulated ALUA
98 if (dev
->se_sub_dev
->t10_alua
.alua_type
== SPC3_ALUA_EMULATED
)
99 spc_fill_alua_data(lun
->lun_sep
, buf
);
101 buf
[7] = 0x2; /* CmdQue=1 */
103 snprintf(&buf
[8], 8, "LIO-ORG");
104 snprintf(&buf
[16], 16, "%s", dev
->se_sub_dev
->t10_wwn
.model
);
105 snprintf(&buf
[32], 4, "%s", dev
->se_sub_dev
->t10_wwn
.revision
);
106 buf
[4] = 31; /* Set additional length to 31 */
111 /* unit serial number */
112 static int spc_emulate_evpd_80(struct se_cmd
*cmd
, unsigned char *buf
)
114 struct se_device
*dev
= cmd
->se_dev
;
117 if (dev
->se_sub_dev
->su_dev_flags
&
118 SDF_EMULATED_VPD_UNIT_SERIAL
) {
121 unit_serial_len
= strlen(dev
->se_sub_dev
->t10_wwn
.unit_serial
);
122 unit_serial_len
++; /* For NULL Terminator */
124 len
+= sprintf(&buf
[4], "%s",
125 dev
->se_sub_dev
->t10_wwn
.unit_serial
);
126 len
++; /* Extra Byte for NULL Terminator */
132 static void spc_parse_naa_6h_vendor_specific(struct se_device
*dev
,
135 unsigned char *p
= &dev
->se_sub_dev
->t10_wwn
.unit_serial
[0];
140 * Generate up to 36 bits of VENDOR SPECIFIC IDENTIFIER starting on
141 * byte 3 bit 3-0 for NAA IEEE Registered Extended DESIGNATOR field
142 * format, followed by 64 bits of VENDOR SPECIFIC IDENTIFIER EXTENSION
143 * to complete the payload. These are based from VPD=0x80 PRODUCT SERIAL
144 * NUMBER set via vpd_unit_serial in target_core_configfs.c to ensure
145 * per device uniqeness.
147 for (cnt
= 0; *p
&& cnt
< 13; p
++) {
148 int val
= hex_to_bin(*p
);
164 * Device identification VPD, for a complete list of
165 * DESIGNATOR TYPEs see spc4r17 Table 459.
167 static int spc_emulate_evpd_83(struct se_cmd
*cmd
, unsigned char *buf
)
169 struct se_device
*dev
= cmd
->se_dev
;
170 struct se_lun
*lun
= cmd
->se_lun
;
171 struct se_port
*port
= NULL
;
172 struct se_portal_group
*tpg
= NULL
;
173 struct t10_alua_lu_gp_member
*lu_gp_mem
;
174 struct t10_alua_tg_pt_gp
*tg_pt_gp
;
175 struct t10_alua_tg_pt_gp_member
*tg_pt_gp_mem
;
176 unsigned char *prod
= &dev
->se_sub_dev
->t10_wwn
.model
[0];
178 u32 unit_serial_len
, off
= 0;
184 * NAA IEEE Registered Extended Assigned designator format, see
185 * spc4r17 section 7.7.3.6.5
187 * We depend upon a target_core_mod/ConfigFS provided
188 * /sys/kernel/config/target/core/$HBA/$DEV/wwn/vpd_unit_serial
189 * value in order to return the NAA id.
191 if (!(dev
->se_sub_dev
->su_dev_flags
& SDF_EMULATED_VPD_UNIT_SERIAL
))
192 goto check_t10_vend_desc
;
194 /* CODE SET == Binary */
197 /* Set ASSOCIATION == addressed logical unit: 0)b */
200 /* Identifier/Designator type == NAA identifier */
204 /* Identifier/Designator length */
208 * Start NAA IEEE Registered Extended Identifier/Designator
210 buf
[off
++] = (0x6 << 4);
213 * Use OpenFabrics IEEE Company ID: 00 14 05
217 buf
[off
] = (0x5 << 4);
220 * Return ConfigFS Unit Serial Number information for
221 * VENDOR_SPECIFIC_IDENTIFIER and
222 * VENDOR_SPECIFIC_IDENTIFIER_EXTENTION
224 spc_parse_naa_6h_vendor_specific(dev
, &buf
[off
]);
231 * T10 Vendor Identifier Page, see spc4r17 section 7.7.3.4
233 id_len
= 8; /* For Vendor field */
234 prod_len
= 4; /* For VPD Header */
235 prod_len
+= 8; /* For Vendor field */
236 prod_len
+= strlen(prod
);
237 prod_len
++; /* For : */
239 if (dev
->se_sub_dev
->su_dev_flags
&
240 SDF_EMULATED_VPD_UNIT_SERIAL
) {
242 strlen(&dev
->se_sub_dev
->t10_wwn
.unit_serial
[0]);
243 unit_serial_len
++; /* For NULL Terminator */
245 id_len
+= sprintf(&buf
[off
+12], "%s:%s", prod
,
246 &dev
->se_sub_dev
->t10_wwn
.unit_serial
[0]);
248 buf
[off
] = 0x2; /* ASCII */
249 buf
[off
+1] = 0x1; /* T10 Vendor ID */
251 memcpy(&buf
[off
+4], "LIO-ORG", 8);
252 /* Extra Byte for NULL Terminator */
254 /* Identifier Length */
256 /* Header size for Designation descriptor */
260 * struct se_port is only set for INQUIRY VPD=1 through $FABRIC_MOD
264 struct t10_alua_lu_gp
*lu_gp
;
265 u32 padding
, scsi_name_len
;
272 * Relative target port identifer, see spc4r17
275 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
276 * section 7.5.1 Table 362
279 (tpg
->se_tpg_tfo
->get_fabric_proto_ident(tpg
) << 4);
280 buf
[off
++] |= 0x1; /* CODE SET == Binary */
281 buf
[off
] = 0x80; /* Set PIV=1 */
282 /* Set ASSOCIATION == target port: 01b */
284 /* DESIGNATOR TYPE == Relative target port identifer */
286 off
++; /* Skip over Reserved */
287 buf
[off
++] = 4; /* DESIGNATOR LENGTH */
288 /* Skip over Obsolete field in RTPI payload
291 buf
[off
++] = ((port
->sep_rtpi
>> 8) & 0xff);
292 buf
[off
++] = (port
->sep_rtpi
& 0xff);
293 len
+= 8; /* Header size + Designation descriptor */
295 * Target port group identifier, see spc4r17
298 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
299 * section 7.5.1 Table 362
301 if (dev
->se_sub_dev
->t10_alua
.alua_type
!=
303 goto check_scsi_name
;
305 tg_pt_gp_mem
= port
->sep_alua_tg_pt_gp_mem
;
309 spin_lock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
310 tg_pt_gp
= tg_pt_gp_mem
->tg_pt_gp
;
312 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
315 tg_pt_gp_id
= tg_pt_gp
->tg_pt_gp_id
;
316 spin_unlock(&tg_pt_gp_mem
->tg_pt_gp_mem_lock
);
319 (tpg
->se_tpg_tfo
->get_fabric_proto_ident(tpg
) << 4);
320 buf
[off
++] |= 0x1; /* CODE SET == Binary */
321 buf
[off
] = 0x80; /* Set PIV=1 */
322 /* Set ASSOCIATION == target port: 01b */
324 /* DESIGNATOR TYPE == Target port group identifier */
326 off
++; /* Skip over Reserved */
327 buf
[off
++] = 4; /* DESIGNATOR LENGTH */
328 off
+= 2; /* Skip over Reserved Field */
329 buf
[off
++] = ((tg_pt_gp_id
>> 8) & 0xff);
330 buf
[off
++] = (tg_pt_gp_id
& 0xff);
331 len
+= 8; /* Header size + Designation descriptor */
333 * Logical Unit Group identifier, see spc4r17
337 lu_gp_mem
= dev
->dev_alua_lu_gp_mem
;
339 goto check_scsi_name
;
341 spin_lock(&lu_gp_mem
->lu_gp_mem_lock
);
342 lu_gp
= lu_gp_mem
->lu_gp
;
344 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
345 goto check_scsi_name
;
347 lu_gp_id
= lu_gp
->lu_gp_id
;
348 spin_unlock(&lu_gp_mem
->lu_gp_mem_lock
);
350 buf
[off
++] |= 0x1; /* CODE SET == Binary */
351 /* DESIGNATOR TYPE == Logical Unit Group identifier */
353 off
++; /* Skip over Reserved */
354 buf
[off
++] = 4; /* DESIGNATOR LENGTH */
355 off
+= 2; /* Skip over Reserved Field */
356 buf
[off
++] = ((lu_gp_id
>> 8) & 0xff);
357 buf
[off
++] = (lu_gp_id
& 0xff);
358 len
+= 8; /* Header size + Designation descriptor */
360 * SCSI name string designator, see spc4r17
363 * Get the PROTOCOL IDENTIFIER as defined by spc4r17
364 * section 7.5.1 Table 362
367 scsi_name_len
= strlen(tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
));
368 /* UTF-8 ",t,0x<16-bit TPGT>" + NULL Terminator */
370 /* Check for 4-byte padding */
371 padding
= ((-scsi_name_len
) & 3);
373 scsi_name_len
+= padding
;
374 /* Header size + Designation descriptor */
378 (tpg
->se_tpg_tfo
->get_fabric_proto_ident(tpg
) << 4);
379 buf
[off
++] |= 0x3; /* CODE SET == UTF-8 */
380 buf
[off
] = 0x80; /* Set PIV=1 */
381 /* Set ASSOCIATION == target port: 01b */
383 /* DESIGNATOR TYPE == SCSI name string */
385 off
+= 2; /* Skip over Reserved and length */
387 * SCSI name string identifer containing, $FABRIC_MOD
388 * dependent information. For LIO-Target and iSCSI
389 * Target Port, this means "<iSCSI name>,t,0x<TPGT> in
392 tpgt
= tpg
->se_tpg_tfo
->tpg_get_tag(tpg
);
393 scsi_name_len
= sprintf(&buf
[off
], "%s,t,0x%04x",
394 tpg
->se_tpg_tfo
->tpg_get_wwn(tpg
), tpgt
);
395 scsi_name_len
+= 1 /* Include NULL terminator */;
397 * The null-terminated, null-padded (see 4.4.2) SCSI
398 * NAME STRING field contains a UTF-8 format string.
399 * The number of bytes in the SCSI NAME STRING field
400 * (i.e., the value in the DESIGNATOR LENGTH field)
401 * shall be no larger than 256 and shall be a multiple
405 scsi_name_len
+= padding
;
407 buf
[off
-1] = scsi_name_len
;
408 off
+= scsi_name_len
;
409 /* Header size + Designation descriptor */
410 len
+= (scsi_name_len
+ 4);
412 buf
[2] = ((len
>> 8) & 0xff);
413 buf
[3] = (len
& 0xff); /* Page Length for VPD 0x83 */
417 /* Extended INQUIRY Data VPD Page */
418 static int spc_emulate_evpd_86(struct se_cmd
*cmd
, unsigned char *buf
)
421 /* Set HEADSUP, ORDSUP, SIMPSUP */
424 /* If WriteCache emulation is enabled, set V_SUP */
425 if (cmd
->se_dev
->se_sub_dev
->se_dev_attrib
.emulate_write_cache
> 0)
430 /* Block Limits VPD page */
431 static int spc_emulate_evpd_b0(struct se_cmd
*cmd
, unsigned char *buf
)
433 struct se_device
*dev
= cmd
->se_dev
;
438 * Following spc3r22 section 6.5.3 Block Limits VPD page, when
439 * emulate_tpu=1 or emulate_tpws=1 we will be expect a
440 * different page length for Thin Provisioning.
442 if (dev
->se_sub_dev
->se_dev_attrib
.emulate_tpu
|| dev
->se_sub_dev
->se_dev_attrib
.emulate_tpws
)
445 buf
[0] = dev
->transport
->get_device_type(dev
);
446 buf
[3] = have_tp
? 0x3c : 0x10;
452 * Set OPTIMAL TRANSFER LENGTH GRANULARITY
454 put_unaligned_be16(1, &buf
[6]);
457 * Set MAXIMUM TRANSFER LENGTH
459 max_sectors
= min(dev
->se_sub_dev
->se_dev_attrib
.fabric_max_sectors
,
460 dev
->se_sub_dev
->se_dev_attrib
.hw_max_sectors
);
461 put_unaligned_be32(max_sectors
, &buf
[8]);
464 * Set OPTIMAL TRANSFER LENGTH
466 put_unaligned_be32(dev
->se_sub_dev
->se_dev_attrib
.optimal_sectors
, &buf
[12]);
469 * Exit now if we don't support TP.
475 * Set MAXIMUM UNMAP LBA COUNT
477 put_unaligned_be32(dev
->se_sub_dev
->se_dev_attrib
.max_unmap_lba_count
, &buf
[20]);
480 * Set MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT
482 put_unaligned_be32(dev
->se_sub_dev
->se_dev_attrib
.max_unmap_block_desc_count
,
486 * Set OPTIMAL UNMAP GRANULARITY
488 put_unaligned_be32(dev
->se_sub_dev
->se_dev_attrib
.unmap_granularity
, &buf
[28]);
491 * UNMAP GRANULARITY ALIGNMENT
493 put_unaligned_be32(dev
->se_sub_dev
->se_dev_attrib
.unmap_granularity_alignment
,
495 if (dev
->se_sub_dev
->se_dev_attrib
.unmap_granularity_alignment
!= 0)
496 buf
[32] |= 0x80; /* Set the UGAVALID bit */
501 /* Block Device Characteristics VPD page */
502 static int spc_emulate_evpd_b1(struct se_cmd
*cmd
, unsigned char *buf
)
504 struct se_device
*dev
= cmd
->se_dev
;
506 buf
[0] = dev
->transport
->get_device_type(dev
);
508 buf
[5] = dev
->se_sub_dev
->se_dev_attrib
.is_nonrot
? 1 : 0;
513 /* Thin Provisioning VPD */
514 static int spc_emulate_evpd_b2(struct se_cmd
*cmd
, unsigned char *buf
)
516 struct se_device
*dev
= cmd
->se_dev
;
519 * From spc3r22 section 6.5.4 Thin Provisioning VPD page:
521 * The PAGE LENGTH field is defined in SPC-4. If the DP bit is set to
522 * zero, then the page length shall be set to 0004h. If the DP bit
523 * is set to one, then the page length shall be set to the value
524 * defined in table 162.
526 buf
[0] = dev
->transport
->get_device_type(dev
);
529 * Set Hardcoded length mentioned above for DP=0
531 put_unaligned_be16(0x0004, &buf
[2]);
534 * The THRESHOLD EXPONENT field indicates the threshold set size in
535 * LBAs as a power of 2 (i.e., the threshold set size is equal to
536 * 2(threshold exponent)).
538 * Note that this is currently set to 0x00 as mkp says it will be
539 * changing again. We can enable this once it has settled in T10
540 * and is actually used by Linux/SCSI ML code.
545 * A TPU bit set to one indicates that the device server supports
546 * the UNMAP command (see 5.25). A TPU bit set to zero indicates
547 * that the device server does not support the UNMAP command.
549 if (dev
->se_sub_dev
->se_dev_attrib
.emulate_tpu
!= 0)
553 * A TPWS bit set to one indicates that the device server supports
554 * the use of the WRITE SAME (16) command (see 5.42) to unmap LBAs.
555 * A TPWS bit set to zero indicates that the device server does not
556 * support the use of the WRITE SAME (16) command to unmap LBAs.
558 if (dev
->se_sub_dev
->se_dev_attrib
.emulate_tpws
!= 0)
564 static int spc_emulate_evpd_00(struct se_cmd
*cmd
, unsigned char *buf
);
568 int (*emulate
)(struct se_cmd
*, unsigned char *);
569 } evpd_handlers
[] = {
570 { .page
= 0x00, .emulate
= spc_emulate_evpd_00
},
571 { .page
= 0x80, .emulate
= spc_emulate_evpd_80
},
572 { .page
= 0x83, .emulate
= spc_emulate_evpd_83
},
573 { .page
= 0x86, .emulate
= spc_emulate_evpd_86
},
574 { .page
= 0xb0, .emulate
= spc_emulate_evpd_b0
},
575 { .page
= 0xb1, .emulate
= spc_emulate_evpd_b1
},
576 { .page
= 0xb2, .emulate
= spc_emulate_evpd_b2
},
579 /* supported vital product data pages */
580 static int spc_emulate_evpd_00(struct se_cmd
*cmd
, unsigned char *buf
)
585 * Only report the INQUIRY EVPD=1 pages after a valid NAA
586 * Registered Extended LUN WWN has been set via ConfigFS
587 * during device creation/restart.
589 if (cmd
->se_dev
->se_sub_dev
->su_dev_flags
&
590 SDF_EMULATED_VPD_UNIT_SERIAL
) {
591 buf
[3] = ARRAY_SIZE(evpd_handlers
);
592 for (p
= 0; p
< ARRAY_SIZE(evpd_handlers
); ++p
)
593 buf
[p
+ 4] = evpd_handlers
[p
].page
;
599 static int spc_emulate_inquiry(struct se_cmd
*cmd
)
601 struct se_device
*dev
= cmd
->se_dev
;
602 struct se_portal_group
*tpg
= cmd
->se_lun
->lun_sep
->sep_tpg
;
604 unsigned char *cdb
= cmd
->t_task_cdb
;
605 unsigned char buf
[SE_INQUIRY_BUF
];
608 if (dev
== tpg
->tpg_virt_lun0
.lun_se_dev
)
609 buf
[0] = 0x3f; /* Not connected */
611 buf
[0] = dev
->transport
->get_device_type(dev
);
613 if (!(cdb
[1] & 0x1)) {
615 pr_err("INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
617 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
622 ret
= spc_emulate_inquiry_std(cmd
, buf
);
626 for (p
= 0; p
< ARRAY_SIZE(evpd_handlers
); ++p
) {
627 if (cdb
[2] == evpd_handlers
[p
].page
) {
629 ret
= evpd_handlers
[p
].emulate(cmd
, buf
);
634 pr_err("Unknown VPD Code: 0x%02x\n", cdb
[2]);
635 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
639 rbuf
= transport_kmap_data_sg(cmd
);
641 memcpy(rbuf
, buf
, min_t(u32
, sizeof(buf
), cmd
->data_length
));
642 transport_kunmap_data_sg(cmd
);
646 target_complete_cmd(cmd
, GOOD
);
650 static int spc_modesense_rwrecovery(unsigned char *p
)
658 static int spc_modesense_control(struct se_device
*dev
, unsigned char *p
)
664 * From spc4r23, 7.4.7 Control mode page
666 * The QUEUE ALGORITHM MODIFIER field (see table 368) specifies
667 * restrictions on the algorithm used for reordering commands
668 * having the SIMPLE task attribute (see SAM-4).
670 * Table 368 -- QUEUE ALGORITHM MODIFIER field
672 * 0h Restricted reordering
673 * 1h Unrestricted reordering allowed
675 * 8h to Fh Vendor specific
677 * A value of zero in the QUEUE ALGORITHM MODIFIER field specifies that
678 * the device server shall order the processing sequence of commands
679 * having the SIMPLE task attribute such that data integrity is maintained
680 * for that I_T nexus (i.e., if the transmission of new SCSI transport protocol
681 * requests is halted at any time, the final value of all data observable
682 * on the medium shall be the same as if all the commands had been processed
683 * with the ORDERED task attribute).
685 * A value of one in the QUEUE ALGORITHM MODIFIER field specifies that the
686 * device server may reorder the processing sequence of commands having the
687 * SIMPLE task attribute in any manner. Any data integrity exposures related to
688 * command sequence order shall be explicitly handled by the application client
689 * through the selection of appropriate ommands and task attributes.
691 p
[3] = (dev
->se_sub_dev
->se_dev_attrib
.emulate_rest_reord
== 1) ? 0x00 : 0x10;
693 * From spc4r17, section 7.4.6 Control mode Page
695 * Unit Attention interlocks control (UN_INTLCK_CTRL) to code 00b
697 * 00b: The logical unit shall clear any unit attention condition
698 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
699 * status and shall not establish a unit attention condition when a com-
700 * mand is completed with BUSY, TASK SET FULL, or RESERVATION CONFLICT
703 * 10b: The logical unit shall not clear any unit attention condition
704 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
705 * status and shall not establish a unit attention condition when
706 * a command is completed with BUSY, TASK SET FULL, or RESERVATION
709 * 11b a The logical unit shall not clear any unit attention condition
710 * reported in the same I_T_L_Q nexus transaction as a CHECK CONDITION
711 * status and shall establish a unit attention condition for the
712 * initiator port associated with the I_T nexus on which the BUSY,
713 * TASK SET FULL, or RESERVATION CONFLICT status is being returned.
714 * Depending on the status, the additional sense code shall be set to
715 * PREVIOUS BUSY STATUS, PREVIOUS TASK SET FULL STATUS, or PREVIOUS
716 * RESERVATION CONFLICT STATUS. Until it is cleared by a REQUEST SENSE
717 * command, a unit attention condition shall be established only once
718 * for a BUSY, TASK SET FULL, or RESERVATION CONFLICT status regardless
719 * to the number of commands completed with one of those status codes.
721 p
[4] = (dev
->se_sub_dev
->se_dev_attrib
.emulate_ua_intlck_ctrl
== 2) ? 0x30 :
722 (dev
->se_sub_dev
->se_dev_attrib
.emulate_ua_intlck_ctrl
== 1) ? 0x20 : 0x00;
724 * From spc4r17, section 7.4.6 Control mode Page
726 * Task Aborted Status (TAS) bit set to zero.
728 * A task aborted status (TAS) bit set to zero specifies that aborted
729 * tasks shall be terminated by the device server without any response
730 * to the application client. A TAS bit set to one specifies that tasks
731 * aborted by the actions of an I_T nexus other than the I_T nexus on
732 * which the command was received shall be completed with TASK ABORTED
733 * status (see SAM-4).
735 p
[5] = (dev
->se_sub_dev
->se_dev_attrib
.emulate_tas
) ? 0x40 : 0x00;
743 static int spc_modesense_caching(struct se_device
*dev
, unsigned char *p
)
747 if (dev
->se_sub_dev
->se_dev_attrib
.emulate_write_cache
> 0)
748 p
[2] = 0x04; /* Write Cache Enable */
749 p
[12] = 0x20; /* Disabled Read Ahead */
754 static void spc_modesense_write_protect(unsigned char *buf
, int type
)
757 * I believe that the WP bit (bit 7) in the mode header is the same for
764 buf
[0] |= 0x80; /* WP bit */
769 static void spc_modesense_dpofua(unsigned char *buf
, int type
)
773 buf
[0] |= 0x10; /* DPOFUA bit */
780 static int spc_emulate_modesense(struct se_cmd
*cmd
)
782 struct se_device
*dev
= cmd
->se_dev
;
783 char *cdb
= cmd
->t_task_cdb
;
785 int type
= dev
->transport
->get_device_type(dev
);
786 int ten
= (cmd
->t_task_cdb
[0] == MODE_SENSE_10
);
787 u32 offset
= ten
? 8 : 4;
789 unsigned char buf
[SE_MODE_PAGE_BUF
];
791 memset(buf
, 0, SE_MODE_PAGE_BUF
);
793 switch (cdb
[2] & 0x3f) {
795 length
= spc_modesense_rwrecovery(&buf
[offset
]);
798 length
= spc_modesense_caching(dev
, &buf
[offset
]);
801 length
= spc_modesense_control(dev
, &buf
[offset
]);
804 length
= spc_modesense_rwrecovery(&buf
[offset
]);
805 length
+= spc_modesense_caching(dev
, &buf
[offset
+length
]);
806 length
+= spc_modesense_control(dev
, &buf
[offset
+length
]);
809 pr_err("MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
810 cdb
[2] & 0x3f, cdb
[3]);
811 cmd
->scsi_sense_reason
= TCM_UNKNOWN_MODE_PAGE
;
818 buf
[0] = (offset
>> 8) & 0xff;
819 buf
[1] = offset
& 0xff;
822 if ((cmd
->se_lun
->lun_access
& TRANSPORT_LUNFLAGS_READ_ONLY
) ||
824 (cmd
->se_deve
->lun_flags
& TRANSPORT_LUNFLAGS_READ_ONLY
)))
825 spc_modesense_write_protect(&buf
[3], type
);
827 if ((dev
->se_sub_dev
->se_dev_attrib
.emulate_write_cache
> 0) &&
828 (dev
->se_sub_dev
->se_dev_attrib
.emulate_fua_write
> 0))
829 spc_modesense_dpofua(&buf
[3], type
);
832 buf
[0] = offset
& 0xff;
835 if ((cmd
->se_lun
->lun_access
& TRANSPORT_LUNFLAGS_READ_ONLY
) ||
837 (cmd
->se_deve
->lun_flags
& TRANSPORT_LUNFLAGS_READ_ONLY
)))
838 spc_modesense_write_protect(&buf
[2], type
);
840 if ((dev
->se_sub_dev
->se_dev_attrib
.emulate_write_cache
> 0) &&
841 (dev
->se_sub_dev
->se_dev_attrib
.emulate_fua_write
> 0))
842 spc_modesense_dpofua(&buf
[2], type
);
845 rbuf
= transport_kmap_data_sg(cmd
);
847 memcpy(rbuf
, buf
, min(offset
, cmd
->data_length
));
848 transport_kunmap_data_sg(cmd
);
851 target_complete_cmd(cmd
, GOOD
);
855 static int spc_emulate_request_sense(struct se_cmd
*cmd
)
857 unsigned char *cdb
= cmd
->t_task_cdb
;
859 u8 ua_asc
= 0, ua_ascq
= 0;
860 unsigned char buf
[SE_SENSE_BUF
];
862 memset(buf
, 0, SE_SENSE_BUF
);
865 pr_err("REQUEST_SENSE description emulation not"
867 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
871 rbuf
= transport_kmap_data_sg(cmd
);
872 if (cmd
->scsi_sense_reason
!= 0) {
874 * Out of memory. We will fail with CHECK CONDITION, so
875 * we must not clear the unit attention condition.
877 target_complete_cmd(cmd
, CHECK_CONDITION
);
879 } else if (!core_scsi3_ua_clear_for_request_sense(cmd
, &ua_asc
, &ua_ascq
)) {
881 * CURRENT ERROR, UNIT ATTENTION
884 buf
[SPC_SENSE_KEY_OFFSET
] = UNIT_ATTENTION
;
887 * The Additional Sense Code (ASC) from the UNIT ATTENTION
889 buf
[SPC_ASC_KEY_OFFSET
] = ua_asc
;
890 buf
[SPC_ASCQ_KEY_OFFSET
] = ua_ascq
;
894 * CURRENT ERROR, NO SENSE
897 buf
[SPC_SENSE_KEY_OFFSET
] = NO_SENSE
;
900 * NO ADDITIONAL SENSE INFORMATION
902 buf
[SPC_ASC_KEY_OFFSET
] = 0x00;
907 memcpy(rbuf
, buf
, min_t(u32
, sizeof(buf
), cmd
->data_length
));
908 transport_kunmap_data_sg(cmd
);
911 target_complete_cmd(cmd
, GOOD
);
915 static int spc_emulate_testunitready(struct se_cmd
*cmd
)
917 target_complete_cmd(cmd
, GOOD
);
921 int spc_parse_cdb(struct se_cmd
*cmd
, unsigned int *size
)
923 struct se_device
*dev
= cmd
->se_dev
;
924 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
925 unsigned char *cdb
= cmd
->t_task_cdb
;
932 *size
= (cdb
[7] << 8) + cdb
[8];
936 cmd
->execute_cmd
= spc_emulate_modesense
;
939 *size
= (cdb
[7] << 8) + cdb
[8];
940 cmd
->execute_cmd
= spc_emulate_modesense
;
944 *size
= (cdb
[7] << 8) + cdb
[8];
946 case PERSISTENT_RESERVE_IN
:
947 if (su_dev
->t10_pr
.res_type
== SPC3_PERSISTENT_RESERVATIONS
)
948 cmd
->execute_cmd
= target_scsi3_emulate_pr_in
;
949 *size
= (cdb
[7] << 8) + cdb
[8];
951 case PERSISTENT_RESERVE_OUT
:
952 if (su_dev
->t10_pr
.res_type
== SPC3_PERSISTENT_RESERVATIONS
)
953 cmd
->execute_cmd
= target_scsi3_emulate_pr_out
;
954 *size
= (cdb
[7] << 8) + cdb
[8];
958 if (cdb
[0] == RELEASE_10
)
959 *size
= (cdb
[7] << 8) | cdb
[8];
961 *size
= cmd
->data_length
;
963 if (su_dev
->t10_pr
.res_type
!= SPC_PASSTHROUGH
)
964 cmd
->execute_cmd
= target_scsi2_reservation_release
;
969 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
970 * Assume the passthrough or $FABRIC_MOD will tell us about it.
972 if (cdb
[0] == RESERVE_10
)
973 *size
= (cdb
[7] << 8) | cdb
[8];
975 *size
= cmd
->data_length
;
978 * Setup the legacy emulated handler for SPC-2 and
979 * >= SPC-3 compatible reservation handling (CRH=1)
980 * Otherwise, we assume the underlying SCSI logic is
981 * is running in SPC_PASSTHROUGH, and wants reservations
982 * emulation disabled.
984 if (su_dev
->t10_pr
.res_type
!= SPC_PASSTHROUGH
)
985 cmd
->execute_cmd
= target_scsi2_reservation_reserve
;
989 cmd
->execute_cmd
= spc_emulate_request_sense
;
992 *size
= (cdb
[3] << 8) + cdb
[4];
995 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
996 * See spc4r17 section 5.3
998 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
999 cmd
->sam_task_attr
= MSG_HEAD_TAG
;
1000 cmd
->execute_cmd
= spc_emulate_inquiry
;
1002 case SECURITY_PROTOCOL_IN
:
1003 case SECURITY_PROTOCOL_OUT
:
1004 *size
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
1007 case READ_ATTRIBUTE
:
1008 case RECEIVE_COPY_RESULTS
:
1009 case WRITE_ATTRIBUTE
:
1010 *size
= (cdb
[10] << 24) | (cdb
[11] << 16) |
1011 (cdb
[12] << 8) | cdb
[13];
1013 case RECEIVE_DIAGNOSTIC
:
1014 case SEND_DIAGNOSTIC
:
1015 *size
= (cdb
[3] << 8) | cdb
[4];
1018 *size
= (cdb
[6] << 16) + (cdb
[7] << 8) + cdb
[8];
1021 cmd
->execute_cmd
= target_report_luns
;
1022 *size
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
1024 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
1025 * See spc4r17 section 5.3
1027 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
1028 cmd
->sam_task_attr
= MSG_HEAD_TAG
;
1030 case TEST_UNIT_READY
:
1031 cmd
->execute_cmd
= spc_emulate_testunitready
;
1034 case MAINTENANCE_IN
:
1035 if (dev
->transport
->get_device_type(dev
) != TYPE_ROM
) {
1037 * MAINTENANCE_IN from SCC-2
1038 * Check for emulated MI_REPORT_TARGET_PGS
1040 if ((cdb
[1] & 0x1f) == MI_REPORT_TARGET_PGS
&&
1041 su_dev
->t10_alua
.alua_type
== SPC3_ALUA_EMULATED
) {
1043 target_emulate_report_target_port_groups
;
1045 *size
= get_unaligned_be32(&cdb
[6]);
1048 * GPCMD_SEND_KEY from multi media commands
1050 *size
= get_unaligned_be16(&cdb
[8]);
1053 case MAINTENANCE_OUT
:
1054 if (dev
->transport
->get_device_type(dev
) != TYPE_ROM
) {
1056 * MAINTENANCE_OUT from SCC-2
1057 * Check for emulated MO_SET_TARGET_PGS.
1059 if (cdb
[1] == MO_SET_TARGET_PGS
&&
1060 su_dev
->t10_alua
.alua_type
== SPC3_ALUA_EMULATED
) {
1062 target_emulate_set_target_port_groups
;
1064 *size
= get_unaligned_be32(&cdb
[6]);
1067 * GPCMD_SEND_KEY from multi media commands
1069 *size
= get_unaligned_be16(&cdb
[8]);
1073 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
1074 " 0x%02x, sending CHECK_CONDITION.\n",
1075 cmd
->se_tfo
->get_fabric_name(), cdb
[0]);
1076 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1077 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
1083 EXPORT_SYMBOL(spc_parse_cdb
);