24 #include <linux/module.h>
26 #include <generated/utsrelease.h>
27 #include <linux/utsname.h>
29 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
34 #include <linux/ctype.h>
40 #include <asm/unaligned.h>
41 #include <scsi/scsi.h>
91 static char *tcm_vhost_get_fabric_name(
void)
110 pr_err(
"Unknown tport_proto_id: 0x%02x, using"
160 pr_err(
"Unknown tport_proto_id: 0x%02x, using"
189 pr_err(
"Unknown tport_proto_id: 0x%02x, using"
198 static char *tcm_vhost_parse_pr_out_transport_id(
struct se_portal_group *se_tpg,
201 char **port_nexus_ptr)
218 pr_err(
"Unknown tport_proto_id: 0x%02x, using"
227 static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
234 pr_err(
"Unable to alocate struct tcm_vhost_nacl\n");
241 static void tcm_vhost_release_fabric_acl(
struct se_portal_group *se_tpg,
254 static void tcm_vhost_release_cmd(
struct se_cmd *
se_cmd)
259 static int tcm_vhost_shutdown_session(
struct se_session *se_sess)
264 static void tcm_vhost_close_session(
struct se_session *se_sess)
269 static u32 tcm_vhost_sess_get_index(
struct se_session *se_sess)
274 static int tcm_vhost_write_pending(
struct se_cmd *
se_cmd)
281 static int tcm_vhost_write_pending_status(
struct se_cmd *
se_cmd)
286 static void tcm_vhost_set_default_node_attrs(
struct se_node_acl *nacl)
296 static int tcm_vhost_get_cmd_state(
struct se_cmd *
se_cmd)
301 static void vhost_scsi_complete_cmd(
struct tcm_vhost_cmd *tv_cmd)
312 static int tcm_vhost_queue_data_in(
struct se_cmd *
se_cmd)
316 vhost_scsi_complete_cmd(tv_cmd);
320 static int tcm_vhost_queue_status(
struct se_cmd *
se_cmd)
324 vhost_scsi_complete_cmd(tv_cmd);
328 static int tcm_vhost_queue_tm_rsp(
struct se_cmd *
se_cmd)
333 static void vhost_scsi_free_cmd(
struct tcm_vhost_cmd *tv_cmd)
352 static struct tcm_vhost_cmd *vhost_scsi_get_cmd_from_completion(
383 while ((tv_cmd = vhost_scsi_get_cmd_from_completion(vs))) {
388 pr_debug(
"%s tv_cmd %p resid %u status %#02x\n", __func__,
391 memset(&v_rsp, 0,
sizeof(v_rsp));
402 pr_err(
"Faulted on virtio_scsi_cmd_resp\n");
404 vhost_scsi_free_cmd(tv_cmd);
422 pr_err(
"Unable to locate active struct tcm_vhost_nexus\n");
423 return ERR_PTR(-
EIO);
429 pr_err(
"Unable to allocate struct tcm_vhost_cmd\n");
448 unsigned int sgl_count,
void __user *
ptr,
size_t len,
int write)
451 unsigned int npages = 0;
460 if (npages == sgl_count) {
470 sg_set_page(sg, page, nbytes, offset);
480 for (sg = sgl; sg != &sgl[npages]; sg++)
485 static int vhost_scsi_map_iov_to_sgl(
struct tcm_vhost_cmd *tv_cmd,
486 struct iovec *iov,
unsigned int niov,
int write)
497 for (i = 0; i < niov; i++) {
498 sgl_count += (((
uintptr_t)iov[i].iov_base + iov[i].iov_len +
507 pr_debug(
"%s sg %p sgl_count %u is_err %d\n", __func__,
514 pr_debug(
"Mapping %u iovecs for %u pages\n", niov, sgl_count);
515 for (i = 0; i < niov; i++) {
516 ret = vhost_scsi_map_to_sgl(sg, sgl_count, iov[i].iov_base,
517 iov[i].iov_len, write);
533 static void tcm_vhost_submission_work(
struct work_struct *work)
540 int rc, sg_no_bidi = 0;
565 sg_bidi_ptr, sg_no_bidi);
573 static void vhost_scsi_handle_vq(
struct vhost_scsi *vs)
586 pr_err(
"%s endpoint not set\n", __func__);
597 pr_debug(
"vhost_get_vq_desc: head: %d, out: %u in: %u\n",
603 if (head == vq->
num) {
612 if (out == 1 && in == 1) {
616 }
else if (out == 1 && in > 1) {
618 data_first = out + 1;
620 }
else if (out > 1 && in == 1) {
625 vq_err(vq,
"Invalid buffer layout out: %u in: %u\n",
636 vq_err(vq,
"Expecting virtio_scsi_cmd_resp, got %zu"
637 " bytes\n", vq->
iov[out].iov_len);
641 if (
unlikely(vq->
iov[0].iov_len !=
sizeof(v_req))) {
642 vq_err(vq,
"Expecting virtio_scsi_cmd_req, got %zu"
643 " bytes\n", vq->
iov[0].iov_len);
646 pr_debug(
"Calling __copy_from_user: vq->iov[0].iov_base: %p,"
647 " len: %zu\n", vq->
iov[0].iov_base,
sizeof(v_req));
651 vq_err(vq,
"Faulted on virtio_scsi_cmd_req\n");
656 for (i = 0; i < data_num; i++)
657 exp_data_len += vq->
iov[data_first + i].iov_len;
659 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
660 exp_data_len, data_direction);
661 if (IS_ERR(tv_cmd)) {
662 vq_err(vq,
"vhost_scsi_allocate_cmd failed %ld\n",
666 pr_debug(
"Allocated tv_cmd: %p exp_data_len: %d, data_direction"
667 ": %d\n", tv_cmd, exp_data_len, data_direction);
673 vq_err(vq,
"Expecting virtio_scsi_cmd_resp, got %zu"
674 " bytes, out: %d, in: %d\n",
675 vq->
iov[out].iov_len, out, in);
694 vq_err(vq,
"Received SCSI CDB with command_size: %d that"
695 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
696 scsi_command_size(tv_cmd->
tvc_cdb),
700 tv_cmd->
tvc_lun = ((v_req.
lun[2] << 8) | v_req.
lun[3]) & 0x3FFF;
702 pr_debug(
"vhost_scsi got command opcode: %#02x, lun: %d\n",
706 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
707 &vq->
iov[data_first], data_num,
710 vq_err(vq,
"Failed to map iov to sgl\n");
734 static void vhost_scsi_ctl_handle_kick(
struct vhost_work *work)
736 pr_debug(
"%s: The handling func for control queue.\n", __func__);
739 static void vhost_scsi_evt_handle_kick(
struct vhost_work *work)
741 pr_debug(
"%s: The handling func for event queue.\n", __func__);
744 static void vhost_scsi_handle_kick(
struct vhost_work *work)
750 vhost_scsi_handle_vq(vs);
757 static int vhost_scsi_set_endpoint(
767 for (index = 0; index < vs->
dev.nvqs; ++
index) {
787 tv_tport = tv_tpg->
tport;
815 static int vhost_scsi_clear_endpoint(
825 for (index = 0; index < vs->
dev.nvqs; ++
index) {
837 tv_tport = tv_tpg->
tport;
841 pr_warn(
"tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
842 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
885 static int vhost_scsi_release(
struct inode *inode,
struct file *f)
892 memcpy(backend.vhost_wwpn, s->
vs_tpg->tport->tport_name,
893 sizeof(backend.vhost_wwpn));
894 backend.vhost_tpgt = s->
vs_tpg->tport_tpgt;
895 vhost_scsi_clear_endpoint(s, &backend);
903 static void vhost_scsi_flush_vq(
struct vhost_scsi *vs,
int index)
908 static void vhost_scsi_flush(
struct vhost_scsi *vs)
928 vhost_scsi_flush(vs);
933 static long vhost_scsi_ioctl(
struct file *f,
unsigned int ioctl,
947 if (backend.reserved != 0)
950 return vhost_scsi_set_endpoint(vs, &backend);
954 if (backend.reserved != 0)
957 return vhost_scsi_clear_endpoint(vs, &backend);
959 if (
copy_to_user(argp, &abi_version,
sizeof abi_version))
970 return vhost_scsi_set_features(vs, features);
980 static long vhost_scsi_compat_ioctl(
struct file *f,
unsigned int ioctl,
983 return vhost_scsi_ioctl(f, ioctl, (
unsigned long)compat_ptr(arg));
989 .release = vhost_scsi_release,
990 .unlocked_ioctl = vhost_scsi_ioctl,
992 .compat_ioctl = vhost_scsi_compat_ioctl,
994 .open = vhost_scsi_open,
1004 static int __init vhost_scsi_register(
void)
1009 static int vhost_scsi_deregister(
void)
1054 static struct se_node_acl *tcm_vhost_make_nodeacl(
1066 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1077 if (IS_ERR(se_nacl)) {
1078 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1090 static void tcm_vhost_drop_nodeacl(
struct se_node_acl *se_acl)
1098 static int tcm_vhost_make_nexus(
struct tcm_vhost_tpg *tv_tpg,
1107 pr_debug(
"tv_tpg->tpg_nexus already exists\n");
1110 se_tpg = &tv_tpg->
se_tpg;
1115 pr_err(
"Unable to allocate struct tcm_vhost_nexus\n");
1133 se_tpg, (
unsigned char *)name);
1136 pr_debug(
"core_tpg_check_initiator_node_acl() failed"
1174 pr_err(
"Unable to remove TCM_vhost I_T Nexus with"
1175 " active TPG port count: %d\n",
1182 pr_err(
"Unable to remove TCM_vhost I_T Nexus with"
1183 " active TPG vhost count: %d\n",
1188 pr_debug(
"TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1189 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->
tport),
1190 tv_nexus->
tvn_se_sess->se_node_acl->initiatorname);
1217 tv_nexus->
tvn_se_sess->se_node_acl->initiatorname);
1235 if (!
strncmp(page,
"NULL", 4)) {
1236 ret = tcm_vhost_drop_nexus(tv_tpg);
1237 return (!ret) ? count :
ret;
1245 pr_err(
"Emulated NAA Sas Address: %s, exceeds"
1251 ptr =
strstr(i_port,
"naa.");
1254 pr_err(
"Passed SAS Initiator Port %s does not"
1255 " match target port protoid: %s\n", i_port,
1256 tcm_vhost_dump_proto_id(tport_wwn));
1259 port_ptr = &i_port[0];
1262 ptr =
strstr(i_port,
"fc.");
1265 pr_err(
"Passed FCP Initiator Port %s does not"
1266 " match target port protoid: %s\n", i_port,
1267 tcm_vhost_dump_proto_id(tport_wwn));
1270 port_ptr = &i_port[3];
1273 ptr =
strstr(i_port,
"iqn.");
1276 pr_err(
"Passed iSCSI Initiator Port %s does not"
1277 " match target port protoid: %s\n", i_port,
1278 tcm_vhost_dump_proto_id(tport_wwn));
1281 port_ptr = &i_port[0];
1284 pr_err(
"Unable to locate prefix for emulated Initiator Port:"
1291 if (i_port[
strlen(i_port)-1] ==
'\n')
1292 i_port[
strlen(i_port)-1] =
'\0';
1294 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1304 &tcm_vhost_tpg_nexus.attr,
1319 if (
strstr(name,
"tpgt_") != name)
1321 if (kstrtoul(name + 5, 10, &tpgt) || tpgt >
UINT_MAX)
1326 pr_err(
"Unable to allocate struct tcm_vhost_tpg");
1358 tcm_vhost_drop_nexus(tpg);
1380 pr_err(
"Unable to allocate struct tcm_vhost_tport");
1388 ptr =
strstr(name,
"naa.");
1393 ptr =
strstr(name,
"fc.");
1399 ptr =
strstr(name,
"iqn.");
1405 pr_err(
"Unable to locate prefix for emulated Target Port:"
1412 pr_err(
"Emulated %s Address: %s, exceeds"
1413 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1420 pr_debug(
"TCM_VHost_ConfigFS: Allocated emulated Target"
1421 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1426 static void tcm_vhost_drop_tport(
struct se_wwn *wwn)
1431 pr_debug(
"TCM_VHost_ConfigFS: Deallocating emulated Target"
1432 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1438 static ssize_t tcm_vhost_wwn_show_attr_version(
1442 return sprintf(page,
"TCM_VHOST fabric module %s on %s/%s"
1450 &tcm_vhost_wwn_version.attr,
1455 .get_fabric_name = tcm_vhost_get_fabric_name,
1456 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1457 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1458 .tpg_get_tag = tcm_vhost_get_tag,
1459 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1460 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1461 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1462 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1463 .tpg_check_demo_mode = tcm_vhost_check_true,
1464 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1465 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1466 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1467 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1468 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1469 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1470 .release_cmd = tcm_vhost_release_cmd,
1471 .shutdown_session = tcm_vhost_shutdown_session,
1472 .close_session = tcm_vhost_close_session,
1473 .sess_get_index = tcm_vhost_sess_get_index,
1474 .sess_get_initiator_sid =
NULL,
1475 .write_pending = tcm_vhost_write_pending,
1476 .write_pending_status = tcm_vhost_write_pending_status,
1477 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1478 .get_task_tag = tcm_vhost_get_task_tag,
1479 .get_cmd_state = tcm_vhost_get_cmd_state,
1480 .queue_data_in = tcm_vhost_queue_data_in,
1481 .queue_status = tcm_vhost_queue_status,
1482 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
1486 .fabric_make_wwn = tcm_vhost_make_tport,
1487 .fabric_drop_wwn = tcm_vhost_drop_tport,
1488 .fabric_make_tpg = tcm_vhost_make_tpg,
1489 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1490 .fabric_post_link = tcm_vhost_port_link,
1491 .fabric_pre_unlink = tcm_vhost_port_unlink,
1492 .fabric_make_np =
NULL,
1493 .fabric_drop_np =
NULL,
1494 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1495 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1498 static int tcm_vhost_register_configfs(
void)
1503 pr_debug(
"TCM_VHOST fabric module %s on %s/%s"
1510 if (IS_ERR(fabric)) {
1511 pr_err(
"target_fabric_configfs_init() failed\n");
1512 return PTR_ERR(fabric);
1517 fabric->
tf_ops = tcm_vhost_ops;
1521 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1522 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1535 pr_err(
"target_fabric_configfs_register() failed"
1536 " for TCM_VHOST\n");
1542 tcm_vhost_fabric_configfs = fabric;
1543 pr_debug(
"TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1547 static void tcm_vhost_deregister_configfs(
void)
1549 if (!tcm_vhost_fabric_configfs)
1553 tcm_vhost_fabric_configfs =
NULL;
1554 pr_debug(
"TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1557 static int __init tcm_vhost_init(
void)
1565 if (!tcm_vhost_workqueue)
1568 ret = vhost_scsi_register();
1570 goto out_destroy_workqueue;
1572 ret = tcm_vhost_register_configfs();
1574 goto out_vhost_scsi_deregister;
1578 out_vhost_scsi_deregister:
1579 vhost_scsi_deregister();
1580 out_destroy_workqueue:
1586 static void tcm_vhost_exit(
void)
1588 tcm_vhost_deregister_configfs();
1589 vhost_scsi_deregister();