26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <asm/unaligned.h>
30 #include <scsi/scsi.h>
43 static void spc_fill_alua_data(
struct se_port *
port,
unsigned char *
buf)
72 static int spc_emulate_inquiry_std(
struct se_cmd *
cmd,
char *buf)
81 buf[2] = dev->
transport->get_device_rev(dev);
99 spc_fill_alua_data(lun->
lun_sep, buf);
112 static int spc_emulate_evpd_80(
struct se_cmd *cmd,
unsigned char *buf)
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];
147 for (cnt = 0; *p && cnt < 13; p++) {
167 static int spc_emulate_evpd_83(
struct se_cmd *cmd,
unsigned char *buf)
176 unsigned char *prod = &dev->
se_sub_dev->t10_wwn.model[0];
178 u32 unit_serial_len, off = 0;
192 goto check_t10_vend_desc;
210 buf[off++] = (0x6 << 4);
217 buf[off] = (0x5 << 4);
224 spc_parse_naa_6h_vendor_specific(dev, &buf[off]);
245 id_len +=
sprintf(&buf[off+12],
"%s:%s", prod,
251 memcpy(&buf[off+4],
"LIO-ORG", 8);
279 (tpg->
se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
291 buf[off++] = ((port->
sep_rtpi >> 8) & 0xff);
292 buf[off++] = (port->
sep_rtpi & 0xff);
303 goto check_scsi_name;
319 (tpg->
se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
329 buf[off++] = ((tg_pt_gp_id >> 8) & 0xff);
330 buf[off++] = (tg_pt_gp_id & 0xff);
339 goto check_scsi_name;
342 lu_gp = lu_gp_mem->
lu_gp;
345 goto check_scsi_name;
356 buf[off++] = ((lu_gp_id >> 8) & 0xff);
357 buf[off++] = (lu_gp_id & 0xff);
371 padding = ((-scsi_name_len) & 3);
378 (tpg->
se_tpg_tfo->get_fabric_proto_ident(tpg) << 4);
393 scsi_name_len =
sprintf(&buf[off],
"%s,t,0x%04x",
407 buf[off-1] = scsi_name_len;
408 off += scsi_name_len;
410 len += (scsi_name_len + 4);
412 buf[2] = ((len >> 8) & 0xff);
413 buf[3] = (len & 0xff);
418 static int spc_emulate_evpd_86(
struct se_cmd *cmd,
unsigned char *buf)
425 if (cmd->
se_dev->se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
431 static int spc_emulate_evpd_b0(
struct se_cmd *cmd,
unsigned char *buf)
445 buf[0] = dev->
transport->get_device_type(dev);
446 buf[3] = have_tp ? 0x3c : 0x10;
454 put_unaligned_be16(1, &buf[6]);
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);
495 if (dev->
se_sub_dev->se_dev_attrib.unmap_granularity_alignment != 0)
502 static int spc_emulate_evpd_b1(
struct se_cmd *cmd,
unsigned char *buf)
506 buf[0] = dev->
transport->get_device_type(dev);
508 buf[5] = dev->
se_sub_dev->se_dev_attrib.is_nonrot ? 1 : 0;
514 static int spc_emulate_evpd_b2(
struct se_cmd *cmd,
unsigned char *buf)
526 buf[0] = dev->
transport->get_device_type(dev);
531 put_unaligned_be16(0x0004, &buf[2]);
549 if (dev->
se_sub_dev->se_dev_attrib.emulate_tpu != 0)
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);
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 },
580 static int spc_emulate_evpd_00(
struct se_cmd *cmd,
unsigned char *buf)
589 if (cmd->
se_dev->se_sub_dev->su_dev_flags &
593 buf[p + 4] = evpd_handlers[p].
page;
599 static int spc_emulate_inquiry(
struct se_cmd *cmd)
613 buf[0] = dev->
transport->get_device_type(dev);
615 if (!(cdb[1] & 0x1)) {
617 pr_err(
"INQUIRY with EVPD==0 but PAGE CODE=%02x\n",
624 ret = spc_emulate_inquiry_std(cmd, buf);
629 if (cdb[2] == evpd_handlers[p].
page) {
631 ret = evpd_handlers[
p].emulate(cmd, buf);
636 pr_err(
"Unknown VPD Code: 0x%02x\n", cdb[2]);
652 static int spc_modesense_rwrecovery(
unsigned char *p)
660 static int spc_modesense_control(
struct se_device *dev,
unsigned char *p)
693 p[3] = (dev->
se_sub_dev->se_dev_attrib.emulate_rest_reord == 1) ? 0x00 : 0x10;
723 p[4] = (dev->
se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2) ? 0x30 :
724 (dev->
se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 1) ? 0x20 : 0x00;
737 p[5] = (dev->
se_sub_dev->se_dev_attrib.emulate_tas) ? 0x40 : 0x00;
745 static int spc_modesense_caching(
struct se_device *dev,
unsigned char *p)
749 if (dev->
se_sub_dev->se_dev_attrib.emulate_write_cache > 0)
756 static void spc_modesense_write_protect(
unsigned char *buf,
int type)
771 static void spc_modesense_dpofua(
unsigned char *buf,
int type)
782 static int spc_emulate_modesense(
struct se_cmd *cmd)
787 int type = dev->
transport->get_device_type(dev);
795 switch (cdb[2] & 0x3f) {
797 length = spc_modesense_rwrecovery(&buf[offset]);
800 length = spc_modesense_caching(dev, &buf[offset]);
803 length = spc_modesense_control(dev, &buf[offset]);
806 length = spc_modesense_rwrecovery(&buf[offset]);
807 length += spc_modesense_caching(dev, &buf[offset+length]);
808 length += spc_modesense_control(dev, &buf[offset+length]);
811 pr_err(
"MODE SENSE: unimplemented page/subpage: 0x%02x/0x%02x\n",
812 cdb[2] & 0x3f, cdb[3]);
820 buf[0] = (offset >> 8) & 0xff;
821 buf[1] = offset & 0xff;
827 spc_modesense_write_protect(&buf[3], type);
829 if ((dev->
se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
830 (dev->
se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
831 spc_modesense_dpofua(&buf[3], type);
834 buf[0] = offset & 0xff;
840 spc_modesense_write_protect(&buf[2], type);
842 if ((dev->
se_sub_dev->se_dev_attrib.emulate_write_cache > 0) &&
843 (dev->
se_sub_dev->se_dev_attrib.emulate_fua_write > 0))
844 spc_modesense_dpofua(&buf[2], type);
857 static int spc_emulate_request_sense(
struct se_cmd *cmd)
861 u8 ua_asc = 0, ua_ascq = 0;
867 pr_err(
"REQUEST_SENSE description emulation not"
917 static int spc_emulate_testunitready(
struct se_cmd *cmd)
934 *size = (cdb[7] << 8) + cdb[8];
941 *size = (cdb[7] << 8) + cdb[8];
946 *size = (cdb[7] << 8) + cdb[8];
951 *size = (cdb[7] << 8) + cdb[8];
956 *size = (cdb[7] << 8) + cdb[8];
961 *size = (cdb[7] << 8) | cdb[8];
975 *size = (cdb[7] << 8) | cdb[8];
994 *size = (cdb[3] << 8) + cdb[4];
1006 *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1012 *size = (cdb[10] << 24) | (cdb[11] << 16) |
1013 (cdb[12] << 8) | cdb[13];
1017 *size = (cdb[3] << 8) | cdb[4];
1020 *size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
1024 *size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1052 *size = get_unaligned_be16(&cdb[8]);
1071 *size = get_unaligned_be16(&cdb[8]);
1075 pr_warn(
"TARGET_CORE[%s]: Unsupported SCSI Opcode"
1076 " 0x%02x, sending CHECK_CONDITION.\n",
1077 cmd->
se_tfo->get_fabric_name(), cdb[0]);