aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/vhost/scsi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/vhost/scsi.c')
-rw-r--r--drivers/vhost/scsi.c460
1 files changed, 240 insertions, 220 deletions
diff --git a/drivers/vhost/scsi.c b/drivers/vhost/scsi.c
index b35193807f0b..06adf31a9248 100644
--- a/drivers/vhost/scsi.c
+++ b/drivers/vhost/scsi.c
@@ -49,7 +49,6 @@
49#include <linux/llist.h> 49#include <linux/llist.h>
50#include <linux/bitmap.h> 50#include <linux/bitmap.h>
51 51
52#include "vhost.c"
53#include "vhost.h" 52#include "vhost.h"
54 53
55#define TCM_VHOST_VERSION "v0.1" 54#define TCM_VHOST_VERSION "v0.1"
@@ -116,7 +115,6 @@ struct tcm_vhost_nacl {
116 struct se_node_acl se_node_acl; 115 struct se_node_acl se_node_acl;
117}; 116};
118 117
119struct vhost_scsi;
120struct tcm_vhost_tpg { 118struct tcm_vhost_tpg {
121 /* Vhost port target portal group tag for TCM */ 119 /* Vhost port target portal group tag for TCM */
122 u16 tport_tpgt; 120 u16 tport_tpgt;
@@ -218,7 +216,7 @@ static int iov_num_pages(struct iovec *iov)
218 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT; 216 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
219} 217}
220 218
221void tcm_vhost_done_inflight(struct kref *kref) 219static void tcm_vhost_done_inflight(struct kref *kref)
222{ 220{
223 struct vhost_scsi_inflight *inflight; 221 struct vhost_scsi_inflight *inflight;
224 222
@@ -329,11 +327,12 @@ static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
329 return 1; 327 return 1;
330} 328}
331 329
332static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg, 330static u32
333 struct se_node_acl *se_nacl, 331tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
334 struct t10_pr_registration *pr_reg, 332 struct se_node_acl *se_nacl,
335 int *format_code, 333 struct t10_pr_registration *pr_reg,
336 unsigned char *buf) 334 int *format_code,
335 unsigned char *buf)
337{ 336{
338 struct tcm_vhost_tpg *tpg = container_of(se_tpg, 337 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
339 struct tcm_vhost_tpg, se_tpg); 338 struct tcm_vhost_tpg, se_tpg);
@@ -359,10 +358,11 @@ static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
359 format_code, buf); 358 format_code, buf);
360} 359}
361 360
362static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg, 361static u32
363 struct se_node_acl *se_nacl, 362tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
364 struct t10_pr_registration *pr_reg, 363 struct se_node_acl *se_nacl,
365 int *format_code) 364 struct t10_pr_registration *pr_reg,
365 int *format_code)
366{ 366{
367 struct tcm_vhost_tpg *tpg = container_of(se_tpg, 367 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
368 struct tcm_vhost_tpg, se_tpg); 368 struct tcm_vhost_tpg, se_tpg);
@@ -388,10 +388,11 @@ static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
388 format_code); 388 format_code);
389} 389}
390 390
391static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg, 391static char *
392 const char *buf, 392tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
393 u32 *out_tid_len, 393 const char *buf,
394 char **port_nexus_ptr) 394 u32 *out_tid_len,
395 char **port_nexus_ptr)
395{ 396{
396 struct tcm_vhost_tpg *tpg = container_of(se_tpg, 397 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
397 struct tcm_vhost_tpg, se_tpg); 398 struct tcm_vhost_tpg, se_tpg);
@@ -417,8 +418,8 @@ static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
417 port_nexus_ptr); 418 port_nexus_ptr);
418} 419}
419 420
420static struct se_node_acl *tcm_vhost_alloc_fabric_acl( 421static struct se_node_acl *
421 struct se_portal_group *se_tpg) 422tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg)
422{ 423{
423 struct tcm_vhost_nacl *nacl; 424 struct tcm_vhost_nacl *nacl;
424 425
@@ -431,8 +432,9 @@ static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
431 return &nacl->se_node_acl; 432 return &nacl->se_node_acl;
432} 433}
433 434
434static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg, 435static void
435 struct se_node_acl *se_nacl) 436tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
437 struct se_node_acl *se_nacl)
436{ 438{
437 struct tcm_vhost_nacl *nacl = container_of(se_nacl, 439 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
438 struct tcm_vhost_nacl, se_node_acl); 440 struct tcm_vhost_nacl, se_node_acl);
@@ -503,28 +505,28 @@ static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
503 return 0; 505 return 0;
504} 506}
505 507
506static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd) 508static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *cmd)
507{ 509{
508 struct vhost_scsi *vs = tv_cmd->tvc_vhost; 510 struct vhost_scsi *vs = cmd->tvc_vhost;
509 511
510 llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list); 512 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
511 513
512 vhost_work_queue(&vs->dev, &vs->vs_completion_work); 514 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
513} 515}
514 516
515static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd) 517static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
516{ 518{
517 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd, 519 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
518 struct tcm_vhost_cmd, tvc_se_cmd); 520 struct tcm_vhost_cmd, tvc_se_cmd);
519 vhost_scsi_complete_cmd(tv_cmd); 521 vhost_scsi_complete_cmd(cmd);
520 return 0; 522 return 0;
521} 523}
522 524
523static int tcm_vhost_queue_status(struct se_cmd *se_cmd) 525static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
524{ 526{
525 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd, 527 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
526 struct tcm_vhost_cmd, tvc_se_cmd); 528 struct tcm_vhost_cmd, tvc_se_cmd);
527 vhost_scsi_complete_cmd(tv_cmd); 529 vhost_scsi_complete_cmd(cmd);
528 return 0; 530 return 0;
529} 531}
530 532
@@ -539,8 +541,9 @@ static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
539 kfree(evt); 541 kfree(evt);
540} 542}
541 543
542static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs, 544static struct tcm_vhost_evt *
543 u32 event, u32 reason) 545tcm_vhost_allocate_evt(struct vhost_scsi *vs,
546 u32 event, u32 reason)
544{ 547{
545 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 548 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
546 struct tcm_vhost_evt *evt; 549 struct tcm_vhost_evt *evt;
@@ -564,9 +567,9 @@ static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs,
564 return evt; 567 return evt;
565} 568}
566 569
567static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd) 570static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *cmd)
568{ 571{
569 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd; 572 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
570 573
571 /* TODO locking against target/backend threads? */ 574 /* TODO locking against target/backend threads? */
572 transport_generic_free_cmd(se_cmd, 0); 575 transport_generic_free_cmd(se_cmd, 0);
@@ -578,8 +581,8 @@ static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
578 return target_put_sess_cmd(se_cmd->se_sess, se_cmd); 581 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
579} 582}
580 583
581static void tcm_vhost_do_evt_work(struct vhost_scsi *vs, 584static void
582 struct tcm_vhost_evt *evt) 585tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
583{ 586{
584 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 587 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
585 struct virtio_scsi_event *event = &evt->event; 588 struct virtio_scsi_event *event = &evt->event;
@@ -658,7 +661,7 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
658 vs_completion_work); 661 vs_completion_work);
659 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ); 662 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
660 struct virtio_scsi_cmd_resp v_rsp; 663 struct virtio_scsi_cmd_resp v_rsp;
661 struct tcm_vhost_cmd *tv_cmd; 664 struct tcm_vhost_cmd *cmd;
662 struct llist_node *llnode; 665 struct llist_node *llnode;
663 struct se_cmd *se_cmd; 666 struct se_cmd *se_cmd;
664 int ret, vq; 667 int ret, vq;
@@ -666,32 +669,32 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
666 bitmap_zero(signal, VHOST_SCSI_MAX_VQ); 669 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
667 llnode = llist_del_all(&vs->vs_completion_list); 670 llnode = llist_del_all(&vs->vs_completion_list);
668 while (llnode) { 671 while (llnode) {
669 tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd, 672 cmd = llist_entry(llnode, struct tcm_vhost_cmd,
670 tvc_completion_list); 673 tvc_completion_list);
671 llnode = llist_next(llnode); 674 llnode = llist_next(llnode);
672 se_cmd = &tv_cmd->tvc_se_cmd; 675 se_cmd = &cmd->tvc_se_cmd;
673 676
674 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__, 677 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
675 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status); 678 cmd, se_cmd->residual_count, se_cmd->scsi_status);
676 679
677 memset(&v_rsp, 0, sizeof(v_rsp)); 680 memset(&v_rsp, 0, sizeof(v_rsp));
678 v_rsp.resid = se_cmd->residual_count; 681 v_rsp.resid = se_cmd->residual_count;
679 /* TODO is status_qualifier field needed? */ 682 /* TODO is status_qualifier field needed? */
680 v_rsp.status = se_cmd->scsi_status; 683 v_rsp.status = se_cmd->scsi_status;
681 v_rsp.sense_len = se_cmd->scsi_sense_length; 684 v_rsp.sense_len = se_cmd->scsi_sense_length;
682 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf, 685 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
683 v_rsp.sense_len); 686 v_rsp.sense_len);
684 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp)); 687 ret = copy_to_user(cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
685 if (likely(ret == 0)) { 688 if (likely(ret == 0)) {
686 struct vhost_scsi_virtqueue *q; 689 struct vhost_scsi_virtqueue *q;
687 vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0); 690 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
688 q = container_of(tv_cmd->tvc_vq, struct vhost_scsi_virtqueue, vq); 691 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
689 vq = q - vs->vqs; 692 vq = q - vs->vqs;
690 __set_bit(vq, signal); 693 __set_bit(vq, signal);
691 } else 694 } else
692 pr_err("Faulted on virtio_scsi_cmd_resp\n"); 695 pr_err("Faulted on virtio_scsi_cmd_resp\n");
693 696
694 vhost_scsi_free_cmd(tv_cmd); 697 vhost_scsi_free_cmd(cmd);
695 } 698 }
696 699
697 vq = -1; 700 vq = -1;
@@ -700,35 +703,35 @@ static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
700 vhost_signal(&vs->dev, &vs->vqs[vq].vq); 703 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
701} 704}
702 705
703static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd( 706static struct tcm_vhost_cmd *
704 struct vhost_virtqueue *vq, 707vhost_scsi_allocate_cmd(struct vhost_virtqueue *vq,
705 struct tcm_vhost_tpg *tv_tpg, 708 struct tcm_vhost_tpg *tpg,
706 struct virtio_scsi_cmd_req *v_req, 709 struct virtio_scsi_cmd_req *v_req,
707 u32 exp_data_len, 710 u32 exp_data_len,
708 int data_direction) 711 int data_direction)
709{ 712{
710 struct tcm_vhost_cmd *tv_cmd; 713 struct tcm_vhost_cmd *cmd;
711 struct tcm_vhost_nexus *tv_nexus; 714 struct tcm_vhost_nexus *tv_nexus;
712 715
713 tv_nexus = tv_tpg->tpg_nexus; 716 tv_nexus = tpg->tpg_nexus;
714 if (!tv_nexus) { 717 if (!tv_nexus) {
715 pr_err("Unable to locate active struct tcm_vhost_nexus\n"); 718 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
716 return ERR_PTR(-EIO); 719 return ERR_PTR(-EIO);
717 } 720 }
718 721
719 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC); 722 cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
720 if (!tv_cmd) { 723 if (!cmd) {
721 pr_err("Unable to allocate struct tcm_vhost_cmd\n"); 724 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
722 return ERR_PTR(-ENOMEM); 725 return ERR_PTR(-ENOMEM);
723 } 726 }
724 tv_cmd->tvc_tag = v_req->tag; 727 cmd->tvc_tag = v_req->tag;
725 tv_cmd->tvc_task_attr = v_req->task_attr; 728 cmd->tvc_task_attr = v_req->task_attr;
726 tv_cmd->tvc_exp_data_len = exp_data_len; 729 cmd->tvc_exp_data_len = exp_data_len;
727 tv_cmd->tvc_data_direction = data_direction; 730 cmd->tvc_data_direction = data_direction;
728 tv_cmd->tvc_nexus = tv_nexus; 731 cmd->tvc_nexus = tv_nexus;
729 tv_cmd->inflight = tcm_vhost_get_inflight(vq); 732 cmd->inflight = tcm_vhost_get_inflight(vq);
730 733
731 return tv_cmd; 734 return cmd;
732} 735}
733 736
734/* 737/*
@@ -736,8 +739,11 @@ static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
736 * 739 *
737 * Returns the number of scatterlist entries used or -errno on error. 740 * Returns the number of scatterlist entries used or -errno on error.
738 */ 741 */
739static int vhost_scsi_map_to_sgl(struct scatterlist *sgl, 742static int
740 unsigned int sgl_count, struct iovec *iov, int write) 743vhost_scsi_map_to_sgl(struct scatterlist *sgl,
744 unsigned int sgl_count,
745 struct iovec *iov,
746 int write)
741{ 747{
742 unsigned int npages = 0, pages_nr, offset, nbytes; 748 unsigned int npages = 0, pages_nr, offset, nbytes;
743 struct scatterlist *sg = sgl; 749 struct scatterlist *sg = sgl;
@@ -781,8 +787,11 @@ out:
781 return ret; 787 return ret;
782} 788}
783 789
784static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd, 790static int
785 struct iovec *iov, unsigned int niov, int write) 791vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *cmd,
792 struct iovec *iov,
793 unsigned int niov,
794 int write)
786{ 795{
787 int ret; 796 int ret;
788 unsigned int i; 797 unsigned int i;
@@ -798,25 +807,25 @@ static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
798 807
799 /* TODO overflow checking */ 808 /* TODO overflow checking */
800 809
801 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC); 810 sg = kmalloc(sizeof(cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
802 if (!sg) 811 if (!sg)
803 return -ENOMEM; 812 return -ENOMEM;
804 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__, 813 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
805 sg, sgl_count, !sg); 814 sg, sgl_count, !sg);
806 sg_init_table(sg, sgl_count); 815 sg_init_table(sg, sgl_count);
807 816
808 tv_cmd->tvc_sgl = sg; 817 cmd->tvc_sgl = sg;
809 tv_cmd->tvc_sgl_count = sgl_count; 818 cmd->tvc_sgl_count = sgl_count;
810 819
811 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count); 820 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
812 for (i = 0; i < niov; i++) { 821 for (i = 0; i < niov; i++) {
813 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write); 822 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
814 if (ret < 0) { 823 if (ret < 0) {
815 for (i = 0; i < tv_cmd->tvc_sgl_count; i++) 824 for (i = 0; i < cmd->tvc_sgl_count; i++)
816 put_page(sg_page(&tv_cmd->tvc_sgl[i])); 825 put_page(sg_page(&cmd->tvc_sgl[i]));
817 kfree(tv_cmd->tvc_sgl); 826 kfree(cmd->tvc_sgl);
818 tv_cmd->tvc_sgl = NULL; 827 cmd->tvc_sgl = NULL;
819 tv_cmd->tvc_sgl_count = 0; 828 cmd->tvc_sgl_count = 0;
820 return ret; 829 return ret;
821 } 830 }
822 831
@@ -828,15 +837,15 @@ static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
828 837
829static void tcm_vhost_submission_work(struct work_struct *work) 838static void tcm_vhost_submission_work(struct work_struct *work)
830{ 839{
831 struct tcm_vhost_cmd *tv_cmd = 840 struct tcm_vhost_cmd *cmd =
832 container_of(work, struct tcm_vhost_cmd, work); 841 container_of(work, struct tcm_vhost_cmd, work);
833 struct tcm_vhost_nexus *tv_nexus; 842 struct tcm_vhost_nexus *tv_nexus;
834 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd; 843 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
835 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL; 844 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
836 int rc, sg_no_bidi = 0; 845 int rc, sg_no_bidi = 0;
837 846
838 if (tv_cmd->tvc_sgl_count) { 847 if (cmd->tvc_sgl_count) {
839 sg_ptr = tv_cmd->tvc_sgl; 848 sg_ptr = cmd->tvc_sgl;
840/* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */ 849/* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
841#if 0 850#if 0
842 if (se_cmd->se_cmd_flags & SCF_BIDI) { 851 if (se_cmd->se_cmd_flags & SCF_BIDI) {
@@ -847,13 +856,13 @@ static void tcm_vhost_submission_work(struct work_struct *work)
847 } else { 856 } else {
848 sg_ptr = NULL; 857 sg_ptr = NULL;
849 } 858 }
850 tv_nexus = tv_cmd->tvc_nexus; 859 tv_nexus = cmd->tvc_nexus;
851 860
852 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess, 861 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
853 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0], 862 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
854 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len, 863 cmd->tvc_lun, cmd->tvc_exp_data_len,
855 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction, 864 cmd->tvc_task_attr, cmd->tvc_data_direction,
856 TARGET_SCF_ACK_KREF, sg_ptr, tv_cmd->tvc_sgl_count, 865 TARGET_SCF_ACK_KREF, sg_ptr, cmd->tvc_sgl_count,
857 sg_bidi_ptr, sg_no_bidi); 866 sg_bidi_ptr, sg_no_bidi);
858 if (rc < 0) { 867 if (rc < 0) {
859 transport_send_check_condition_and_sense(se_cmd, 868 transport_send_check_condition_and_sense(se_cmd,
@@ -862,8 +871,10 @@ static void tcm_vhost_submission_work(struct work_struct *work)
862 } 871 }
863} 872}
864 873
865static void vhost_scsi_send_bad_target(struct vhost_scsi *vs, 874static void
866 struct vhost_virtqueue *vq, int head, unsigned out) 875vhost_scsi_send_bad_target(struct vhost_scsi *vs,
876 struct vhost_virtqueue *vq,
877 int head, unsigned out)
867{ 878{
868 struct virtio_scsi_cmd_resp __user *resp; 879 struct virtio_scsi_cmd_resp __user *resp;
869 struct virtio_scsi_cmd_resp rsp; 880 struct virtio_scsi_cmd_resp rsp;
@@ -879,13 +890,13 @@ static void vhost_scsi_send_bad_target(struct vhost_scsi *vs,
879 pr_err("Faulted on virtio_scsi_cmd_resp\n"); 890 pr_err("Faulted on virtio_scsi_cmd_resp\n");
880} 891}
881 892
882static void vhost_scsi_handle_vq(struct vhost_scsi *vs, 893static void
883 struct vhost_virtqueue *vq) 894vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
884{ 895{
885 struct tcm_vhost_tpg **vs_tpg; 896 struct tcm_vhost_tpg **vs_tpg;
886 struct virtio_scsi_cmd_req v_req; 897 struct virtio_scsi_cmd_req v_req;
887 struct tcm_vhost_tpg *tv_tpg; 898 struct tcm_vhost_tpg *tpg;
888 struct tcm_vhost_cmd *tv_cmd; 899 struct tcm_vhost_cmd *cmd;
889 u32 exp_data_len, data_first, data_num, data_direction; 900 u32 exp_data_len, data_first, data_num, data_direction;
890 unsigned out, in, i; 901 unsigned out, in, i;
891 int head, ret; 902 int head, ret;
@@ -970,10 +981,10 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
970 981
971 /* Extract the tpgt */ 982 /* Extract the tpgt */
972 target = v_req.lun[1]; 983 target = v_req.lun[1];
973 tv_tpg = ACCESS_ONCE(vs_tpg[target]); 984 tpg = ACCESS_ONCE(vs_tpg[target]);
974 985
975 /* Target does not exist, fail the request */ 986 /* Target does not exist, fail the request */
976 if (unlikely(!tv_tpg)) { 987 if (unlikely(!tpg)) {
977 vhost_scsi_send_bad_target(vs, vq, head, out); 988 vhost_scsi_send_bad_target(vs, vq, head, out);
978 continue; 989 continue;
979 } 990 }
@@ -982,46 +993,46 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
982 for (i = 0; i < data_num; i++) 993 for (i = 0; i < data_num; i++)
983 exp_data_len += vq->iov[data_first + i].iov_len; 994 exp_data_len += vq->iov[data_first + i].iov_len;
984 995
985 tv_cmd = vhost_scsi_allocate_cmd(vq, tv_tpg, &v_req, 996 cmd = vhost_scsi_allocate_cmd(vq, tpg, &v_req,
986 exp_data_len, data_direction); 997 exp_data_len, data_direction);
987 if (IS_ERR(tv_cmd)) { 998 if (IS_ERR(cmd)) {
988 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n", 999 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
989 PTR_ERR(tv_cmd)); 1000 PTR_ERR(cmd));
990 goto err_cmd; 1001 goto err_cmd;
991 } 1002 }
992 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction" 1003 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
993 ": %d\n", tv_cmd, exp_data_len, data_direction); 1004 ": %d\n", cmd, exp_data_len, data_direction);
994 1005
995 tv_cmd->tvc_vhost = vs; 1006 cmd->tvc_vhost = vs;
996 tv_cmd->tvc_vq = vq; 1007 cmd->tvc_vq = vq;
997 tv_cmd->tvc_resp = vq->iov[out].iov_base; 1008 cmd->tvc_resp = vq->iov[out].iov_base;
998 1009
999 /* 1010 /*
1000 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb 1011 * Copy in the recieved CDB descriptor into cmd->tvc_cdb
1001 * that will be used by tcm_vhost_new_cmd_map() and down into 1012 * that will be used by tcm_vhost_new_cmd_map() and down into
1002 * target_setup_cmd_from_cdb() 1013 * target_setup_cmd_from_cdb()
1003 */ 1014 */
1004 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE); 1015 memcpy(cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
1005 /* 1016 /*
1006 * Check that the recieved CDB size does not exceeded our 1017 * Check that the recieved CDB size does not exceeded our
1007 * hardcoded max for tcm_vhost 1018 * hardcoded max for tcm_vhost
1008 */ 1019 */
1009 /* TODO what if cdb was too small for varlen cdb header? */ 1020 /* TODO what if cdb was too small for varlen cdb header? */
1010 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) > 1021 if (unlikely(scsi_command_size(cmd->tvc_cdb) >
1011 TCM_VHOST_MAX_CDB_SIZE)) { 1022 TCM_VHOST_MAX_CDB_SIZE)) {
1012 vq_err(vq, "Received SCSI CDB with command_size: %d that" 1023 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1013 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", 1024 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1014 scsi_command_size(tv_cmd->tvc_cdb), 1025 scsi_command_size(cmd->tvc_cdb),
1015 TCM_VHOST_MAX_CDB_SIZE); 1026 TCM_VHOST_MAX_CDB_SIZE);
1016 goto err_free; 1027 goto err_free;
1017 } 1028 }
1018 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF; 1029 cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1019 1030
1020 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n", 1031 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1021 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun); 1032 cmd->tvc_cdb[0], cmd->tvc_lun);
1022 1033
1023 if (data_direction != DMA_NONE) { 1034 if (data_direction != DMA_NONE) {
1024 ret = vhost_scsi_map_iov_to_sgl(tv_cmd, 1035 ret = vhost_scsi_map_iov_to_sgl(cmd,
1025 &vq->iov[data_first], data_num, 1036 &vq->iov[data_first], data_num,
1026 data_direction == DMA_TO_DEVICE); 1037 data_direction == DMA_TO_DEVICE);
1027 if (unlikely(ret)) { 1038 if (unlikely(ret)) {
@@ -1035,22 +1046,22 @@ static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
1035 * complete the virtio-scsi request in TCM callback context via 1046 * complete the virtio-scsi request in TCM callback context via
1036 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status() 1047 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1037 */ 1048 */
1038 tv_cmd->tvc_vq_desc = head; 1049 cmd->tvc_vq_desc = head;
1039 /* 1050 /*
1040 * Dispatch tv_cmd descriptor for cmwq execution in process 1051 * Dispatch tv_cmd descriptor for cmwq execution in process
1041 * context provided by tcm_vhost_workqueue. This also ensures 1052 * context provided by tcm_vhost_workqueue. This also ensures
1042 * tv_cmd is executed on the same kworker CPU as this vhost 1053 * tv_cmd is executed on the same kworker CPU as this vhost
1043 * thread to gain positive L2 cache locality effects.. 1054 * thread to gain positive L2 cache locality effects..
1044 */ 1055 */
1045 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work); 1056 INIT_WORK(&cmd->work, tcm_vhost_submission_work);
1046 queue_work(tcm_vhost_workqueue, &tv_cmd->work); 1057 queue_work(tcm_vhost_workqueue, &cmd->work);
1047 } 1058 }
1048 1059
1049 mutex_unlock(&vq->mutex); 1060 mutex_unlock(&vq->mutex);
1050 return; 1061 return;
1051 1062
1052err_free: 1063err_free:
1053 vhost_scsi_free_cmd(tv_cmd); 1064 vhost_scsi_free_cmd(cmd);
1054err_cmd: 1065err_cmd:
1055 vhost_scsi_send_bad_target(vs, vq, head, out); 1066 vhost_scsi_send_bad_target(vs, vq, head, out);
1056 mutex_unlock(&vq->mutex); 1067 mutex_unlock(&vq->mutex);
@@ -1061,8 +1072,12 @@ static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1061 pr_debug("%s: The handling func for control queue.\n", __func__); 1072 pr_debug("%s: The handling func for control queue.\n", __func__);
1062} 1073}
1063 1074
1064static void tcm_vhost_send_evt(struct vhost_scsi *vs, struct tcm_vhost_tpg *tpg, 1075static void
1065 struct se_lun *lun, u32 event, u32 reason) 1076tcm_vhost_send_evt(struct vhost_scsi *vs,
1077 struct tcm_vhost_tpg *tpg,
1078 struct se_lun *lun,
1079 u32 event,
1080 u32 reason)
1066{ 1081{
1067 struct tcm_vhost_evt *evt; 1082 struct tcm_vhost_evt *evt;
1068 1083
@@ -1152,12 +1167,12 @@ static void vhost_scsi_flush(struct vhost_scsi *vs)
1152 * The lock nesting rule is: 1167 * The lock nesting rule is:
1153 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex 1168 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1154 */ 1169 */
1155static int vhost_scsi_set_endpoint( 1170static int
1156 struct vhost_scsi *vs, 1171vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1157 struct vhost_scsi_target *t) 1172 struct vhost_scsi_target *t)
1158{ 1173{
1159 struct tcm_vhost_tport *tv_tport; 1174 struct tcm_vhost_tport *tv_tport;
1160 struct tcm_vhost_tpg *tv_tpg; 1175 struct tcm_vhost_tpg *tpg;
1161 struct tcm_vhost_tpg **vs_tpg; 1176 struct tcm_vhost_tpg **vs_tpg;
1162 struct vhost_virtqueue *vq; 1177 struct vhost_virtqueue *vq;
1163 int index, ret, i, len; 1178 int index, ret, i, len;
@@ -1184,32 +1199,32 @@ static int vhost_scsi_set_endpoint(
1184 if (vs->vs_tpg) 1199 if (vs->vs_tpg)
1185 memcpy(vs_tpg, vs->vs_tpg, len); 1200 memcpy(vs_tpg, vs->vs_tpg, len);
1186 1201
1187 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) { 1202 list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
1188 mutex_lock(&tv_tpg->tv_tpg_mutex); 1203 mutex_lock(&tpg->tv_tpg_mutex);
1189 if (!tv_tpg->tpg_nexus) { 1204 if (!tpg->tpg_nexus) {
1190 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1205 mutex_unlock(&tpg->tv_tpg_mutex);
1191 continue; 1206 continue;
1192 } 1207 }
1193 if (tv_tpg->tv_tpg_vhost_count != 0) { 1208 if (tpg->tv_tpg_vhost_count != 0) {
1194 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1209 mutex_unlock(&tpg->tv_tpg_mutex);
1195 continue; 1210 continue;
1196 } 1211 }
1197 tv_tport = tv_tpg->tport; 1212 tv_tport = tpg->tport;
1198 1213
1199 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) { 1214 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1200 if (vs->vs_tpg && vs->vs_tpg[tv_tpg->tport_tpgt]) { 1215 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
1201 kfree(vs_tpg); 1216 kfree(vs_tpg);
1202 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1217 mutex_unlock(&tpg->tv_tpg_mutex);
1203 ret = -EEXIST; 1218 ret = -EEXIST;
1204 goto out; 1219 goto out;
1205 } 1220 }
1206 tv_tpg->tv_tpg_vhost_count++; 1221 tpg->tv_tpg_vhost_count++;
1207 tv_tpg->vhost_scsi = vs; 1222 tpg->vhost_scsi = vs;
1208 vs_tpg[tv_tpg->tport_tpgt] = tv_tpg; 1223 vs_tpg[tpg->tport_tpgt] = tpg;
1209 smp_mb__after_atomic_inc(); 1224 smp_mb__after_atomic_inc();
1210 match = true; 1225 match = true;
1211 } 1226 }
1212 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1227 mutex_unlock(&tpg->tv_tpg_mutex);
1213 } 1228 }
1214 1229
1215 if (match) { 1230 if (match) {
@@ -1242,12 +1257,12 @@ out:
1242 return ret; 1257 return ret;
1243} 1258}
1244 1259
1245static int vhost_scsi_clear_endpoint( 1260static int
1246 struct vhost_scsi *vs, 1261vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1247 struct vhost_scsi_target *t) 1262 struct vhost_scsi_target *t)
1248{ 1263{
1249 struct tcm_vhost_tport *tv_tport; 1264 struct tcm_vhost_tport *tv_tport;
1250 struct tcm_vhost_tpg *tv_tpg; 1265 struct tcm_vhost_tpg *tpg;
1251 struct vhost_virtqueue *vq; 1266 struct vhost_virtqueue *vq;
1252 bool match = false; 1267 bool match = false;
1253 int index, ret, i; 1268 int index, ret, i;
@@ -1270,30 +1285,30 @@ static int vhost_scsi_clear_endpoint(
1270 1285
1271 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) { 1286 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1272 target = i; 1287 target = i;
1273 tv_tpg = vs->vs_tpg[target]; 1288 tpg = vs->vs_tpg[target];
1274 if (!tv_tpg) 1289 if (!tpg)
1275 continue; 1290 continue;
1276 1291
1277 mutex_lock(&tv_tpg->tv_tpg_mutex); 1292 mutex_lock(&tpg->tv_tpg_mutex);
1278 tv_tport = tv_tpg->tport; 1293 tv_tport = tpg->tport;
1279 if (!tv_tport) { 1294 if (!tv_tport) {
1280 ret = -ENODEV; 1295 ret = -ENODEV;
1281 goto err_tpg; 1296 goto err_tpg;
1282 } 1297 }
1283 1298
1284 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) { 1299 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1285 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu" 1300 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
1286 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n", 1301 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1287 tv_tport->tport_name, tv_tpg->tport_tpgt, 1302 tv_tport->tport_name, tpg->tport_tpgt,
1288 t->vhost_wwpn, t->vhost_tpgt); 1303 t->vhost_wwpn, t->vhost_tpgt);
1289 ret = -EINVAL; 1304 ret = -EINVAL;
1290 goto err_tpg; 1305 goto err_tpg;
1291 } 1306 }
1292 tv_tpg->tv_tpg_vhost_count--; 1307 tpg->tv_tpg_vhost_count--;
1293 tv_tpg->vhost_scsi = NULL; 1308 tpg->vhost_scsi = NULL;
1294 vs->vs_tpg[target] = NULL; 1309 vs->vs_tpg[target] = NULL;
1295 match = true; 1310 match = true;
1296 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1311 mutex_unlock(&tpg->tv_tpg_mutex);
1297 } 1312 }
1298 if (match) { 1313 if (match) {
1299 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) { 1314 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
@@ -1317,7 +1332,7 @@ static int vhost_scsi_clear_endpoint(
1317 return 0; 1332 return 0;
1318 1333
1319err_tpg: 1334err_tpg:
1320 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1335 mutex_unlock(&tpg->tv_tpg_mutex);
1321err_dev: 1336err_dev:
1322 mutex_unlock(&vs->dev.mutex); 1337 mutex_unlock(&vs->dev.mutex);
1323 mutex_unlock(&tcm_vhost_mutex); 1338 mutex_unlock(&tcm_vhost_mutex);
@@ -1344,68 +1359,70 @@ static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1344 1359
1345static int vhost_scsi_open(struct inode *inode, struct file *f) 1360static int vhost_scsi_open(struct inode *inode, struct file *f)
1346{ 1361{
1347 struct vhost_scsi *s; 1362 struct vhost_scsi *vs;
1348 struct vhost_virtqueue **vqs; 1363 struct vhost_virtqueue **vqs;
1349 int r, i; 1364 int r, i;
1350 1365
1351 s = kzalloc(sizeof(*s), GFP_KERNEL); 1366 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
1352 if (!s) 1367 if (!vs)
1353 return -ENOMEM; 1368 return -ENOMEM;
1354 1369
1355 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL); 1370 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1356 if (!vqs) { 1371 if (!vqs) {
1357 kfree(s); 1372 kfree(vs);
1358 return -ENOMEM; 1373 return -ENOMEM;
1359 } 1374 }
1360 1375
1361 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work); 1376 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1362 vhost_work_init(&s->vs_event_work, tcm_vhost_evt_work); 1377 vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
1363 1378
1364 s->vs_events_nr = 0; 1379 vs->vs_events_nr = 0;
1365 s->vs_events_missed = false; 1380 vs->vs_events_missed = false;
1366 1381
1367 vqs[VHOST_SCSI_VQ_CTL] = &s->vqs[VHOST_SCSI_VQ_CTL].vq; 1382 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1368 vqs[VHOST_SCSI_VQ_EVT] = &s->vqs[VHOST_SCSI_VQ_EVT].vq; 1383 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1369 s->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick; 1384 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1370 s->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick; 1385 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
1371 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) { 1386 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
1372 vqs[i] = &s->vqs[i].vq; 1387 vqs[i] = &vs->vqs[i].vq;
1373 s->vqs[i].vq.handle_kick = vhost_scsi_handle_kick; 1388 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
1374 } 1389 }
1375 r = vhost_dev_init(&s->dev, vqs, VHOST_SCSI_MAX_VQ); 1390 r = vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
1376 1391
1377 tcm_vhost_init_inflight(s, NULL); 1392 tcm_vhost_init_inflight(vs, NULL);
1378 1393
1379 if (r < 0) { 1394 if (r < 0) {
1380 kfree(vqs); 1395 kfree(vqs);
1381 kfree(s); 1396 kfree(vs);
1382 return r; 1397 return r;
1383 } 1398 }
1384 1399
1385 f->private_data = s; 1400 f->private_data = vs;
1386 return 0; 1401 return 0;
1387} 1402}
1388 1403
1389static int vhost_scsi_release(struct inode *inode, struct file *f) 1404static int vhost_scsi_release(struct inode *inode, struct file *f)
1390{ 1405{
1391 struct vhost_scsi *s = f->private_data; 1406 struct vhost_scsi *vs = f->private_data;
1392 struct vhost_scsi_target t; 1407 struct vhost_scsi_target t;
1393 1408
1394 mutex_lock(&s->dev.mutex); 1409 mutex_lock(&vs->dev.mutex);
1395 memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn)); 1410 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1396 mutex_unlock(&s->dev.mutex); 1411 mutex_unlock(&vs->dev.mutex);
1397 vhost_scsi_clear_endpoint(s, &t); 1412 vhost_scsi_clear_endpoint(vs, &t);
1398 vhost_dev_stop(&s->dev); 1413 vhost_dev_stop(&vs->dev);
1399 vhost_dev_cleanup(&s->dev, false); 1414 vhost_dev_cleanup(&vs->dev, false);
1400 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */ 1415 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1401 vhost_scsi_flush(s); 1416 vhost_scsi_flush(vs);
1402 kfree(s->dev.vqs); 1417 kfree(vs->dev.vqs);
1403 kfree(s); 1418 kfree(vs);
1404 return 0; 1419 return 0;
1405} 1420}
1406 1421
1407static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl, 1422static long
1408 unsigned long arg) 1423vhost_scsi_ioctl(struct file *f,
1424 unsigned int ioctl,
1425 unsigned long arg)
1409{ 1426{
1410 struct vhost_scsi *vs = f->private_data; 1427 struct vhost_scsi *vs = f->private_data;
1411 struct vhost_scsi_target backend; 1428 struct vhost_scsi_target backend;
@@ -1521,8 +1538,9 @@ static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1521 return "Unknown"; 1538 return "Unknown";
1522} 1539}
1523 1540
1524static void tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg, 1541static void
1525 struct se_lun *lun, bool plug) 1542tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1543 struct se_lun *lun, bool plug)
1526{ 1544{
1527 1545
1528 struct vhost_scsi *vs = tpg->vhost_scsi; 1546 struct vhost_scsi *vs = tpg->vhost_scsi;
@@ -1562,18 +1580,18 @@ static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1562} 1580}
1563 1581
1564static int tcm_vhost_port_link(struct se_portal_group *se_tpg, 1582static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1565 struct se_lun *lun) 1583 struct se_lun *lun)
1566{ 1584{
1567 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, 1585 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1568 struct tcm_vhost_tpg, se_tpg); 1586 struct tcm_vhost_tpg, se_tpg);
1569 1587
1570 mutex_lock(&tcm_vhost_mutex); 1588 mutex_lock(&tcm_vhost_mutex);
1571 1589
1572 mutex_lock(&tv_tpg->tv_tpg_mutex); 1590 mutex_lock(&tpg->tv_tpg_mutex);
1573 tv_tpg->tv_tpg_port_count++; 1591 tpg->tv_tpg_port_count++;
1574 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1592 mutex_unlock(&tpg->tv_tpg_mutex);
1575 1593
1576 tcm_vhost_hotplug(tv_tpg, lun); 1594 tcm_vhost_hotplug(tpg, lun);
1577 1595
1578 mutex_unlock(&tcm_vhost_mutex); 1596 mutex_unlock(&tcm_vhost_mutex);
1579 1597
@@ -1581,26 +1599,26 @@ static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1581} 1599}
1582 1600
1583static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg, 1601static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1584 struct se_lun *lun) 1602 struct se_lun *lun)
1585{ 1603{
1586 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, 1604 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1587 struct tcm_vhost_tpg, se_tpg); 1605 struct tcm_vhost_tpg, se_tpg);
1588 1606
1589 mutex_lock(&tcm_vhost_mutex); 1607 mutex_lock(&tcm_vhost_mutex);
1590 1608
1591 mutex_lock(&tv_tpg->tv_tpg_mutex); 1609 mutex_lock(&tpg->tv_tpg_mutex);
1592 tv_tpg->tv_tpg_port_count--; 1610 tpg->tv_tpg_port_count--;
1593 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1611 mutex_unlock(&tpg->tv_tpg_mutex);
1594 1612
1595 tcm_vhost_hotunplug(tv_tpg, lun); 1613 tcm_vhost_hotunplug(tpg, lun);
1596 1614
1597 mutex_unlock(&tcm_vhost_mutex); 1615 mutex_unlock(&tcm_vhost_mutex);
1598} 1616}
1599 1617
1600static struct se_node_acl *tcm_vhost_make_nodeacl( 1618static struct se_node_acl *
1601 struct se_portal_group *se_tpg, 1619tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
1602 struct config_group *group, 1620 struct config_group *group,
1603 const char *name) 1621 const char *name)
1604{ 1622{
1605 struct se_node_acl *se_nacl, *se_nacl_new; 1623 struct se_node_acl *se_nacl, *se_nacl_new;
1606 struct tcm_vhost_nacl *nacl; 1624 struct tcm_vhost_nacl *nacl;
@@ -1641,23 +1659,23 @@ static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1641 kfree(nacl); 1659 kfree(nacl);
1642} 1660}
1643 1661
1644static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg, 1662static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
1645 const char *name) 1663 const char *name)
1646{ 1664{
1647 struct se_portal_group *se_tpg; 1665 struct se_portal_group *se_tpg;
1648 struct tcm_vhost_nexus *tv_nexus; 1666 struct tcm_vhost_nexus *tv_nexus;
1649 1667
1650 mutex_lock(&tv_tpg->tv_tpg_mutex); 1668 mutex_lock(&tpg->tv_tpg_mutex);
1651 if (tv_tpg->tpg_nexus) { 1669 if (tpg->tpg_nexus) {
1652 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1670 mutex_unlock(&tpg->tv_tpg_mutex);
1653 pr_debug("tv_tpg->tpg_nexus already exists\n"); 1671 pr_debug("tpg->tpg_nexus already exists\n");
1654 return -EEXIST; 1672 return -EEXIST;
1655 } 1673 }
1656 se_tpg = &tv_tpg->se_tpg; 1674 se_tpg = &tpg->se_tpg;
1657 1675
1658 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL); 1676 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1659 if (!tv_nexus) { 1677 if (!tv_nexus) {
1660 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1678 mutex_unlock(&tpg->tv_tpg_mutex);
1661 pr_err("Unable to allocate struct tcm_vhost_nexus\n"); 1679 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1662 return -ENOMEM; 1680 return -ENOMEM;
1663 } 1681 }
@@ -1666,7 +1684,7 @@ static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1666 */ 1684 */
1667 tv_nexus->tvn_se_sess = transport_init_session(); 1685 tv_nexus->tvn_se_sess = transport_init_session();
1668 if (IS_ERR(tv_nexus->tvn_se_sess)) { 1686 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1669 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1687 mutex_unlock(&tpg->tv_tpg_mutex);
1670 kfree(tv_nexus); 1688 kfree(tv_nexus);
1671 return -ENOMEM; 1689 return -ENOMEM;
1672 } 1690 }
@@ -1678,7 +1696,7 @@ static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1678 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl( 1696 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1679 se_tpg, (unsigned char *)name); 1697 se_tpg, (unsigned char *)name);
1680 if (!tv_nexus->tvn_se_sess->se_node_acl) { 1698 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1681 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1699 mutex_unlock(&tpg->tv_tpg_mutex);
1682 pr_debug("core_tpg_check_initiator_node_acl() failed" 1700 pr_debug("core_tpg_check_initiator_node_acl() failed"
1683 " for %s\n", name); 1701 " for %s\n", name);
1684 transport_free_session(tv_nexus->tvn_se_sess); 1702 transport_free_session(tv_nexus->tvn_se_sess);
@@ -1691,9 +1709,9 @@ static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1691 */ 1709 */
1692 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl, 1710 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1693 tv_nexus->tvn_se_sess, tv_nexus); 1711 tv_nexus->tvn_se_sess, tv_nexus);
1694 tv_tpg->tpg_nexus = tv_nexus; 1712 tpg->tpg_nexus = tv_nexus;
1695 1713
1696 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1714 mutex_unlock(&tpg->tv_tpg_mutex);
1697 return 0; 1715 return 0;
1698} 1716}
1699 1717
@@ -1746,40 +1764,40 @@ static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1746} 1764}
1747 1765
1748static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg, 1766static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1749 char *page) 1767 char *page)
1750{ 1768{
1751 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, 1769 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1752 struct tcm_vhost_tpg, se_tpg); 1770 struct tcm_vhost_tpg, se_tpg);
1753 struct tcm_vhost_nexus *tv_nexus; 1771 struct tcm_vhost_nexus *tv_nexus;
1754 ssize_t ret; 1772 ssize_t ret;
1755 1773
1756 mutex_lock(&tv_tpg->tv_tpg_mutex); 1774 mutex_lock(&tpg->tv_tpg_mutex);
1757 tv_nexus = tv_tpg->tpg_nexus; 1775 tv_nexus = tpg->tpg_nexus;
1758 if (!tv_nexus) { 1776 if (!tv_nexus) {
1759 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1777 mutex_unlock(&tpg->tv_tpg_mutex);
1760 return -ENODEV; 1778 return -ENODEV;
1761 } 1779 }
1762 ret = snprintf(page, PAGE_SIZE, "%s\n", 1780 ret = snprintf(page, PAGE_SIZE, "%s\n",
1763 tv_nexus->tvn_se_sess->se_node_acl->initiatorname); 1781 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1764 mutex_unlock(&tv_tpg->tv_tpg_mutex); 1782 mutex_unlock(&tpg->tv_tpg_mutex);
1765 1783
1766 return ret; 1784 return ret;
1767} 1785}
1768 1786
1769static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg, 1787static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1770 const char *page, 1788 const char *page,
1771 size_t count) 1789 size_t count)
1772{ 1790{
1773 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg, 1791 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1774 struct tcm_vhost_tpg, se_tpg); 1792 struct tcm_vhost_tpg, se_tpg);
1775 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport; 1793 struct tcm_vhost_tport *tport_wwn = tpg->tport;
1776 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr; 1794 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1777 int ret; 1795 int ret;
1778 /* 1796 /*
1779 * Shutdown the active I_T nexus if 'NULL' is passed.. 1797 * Shutdown the active I_T nexus if 'NULL' is passed..
1780 */ 1798 */
1781 if (!strncmp(page, "NULL", 4)) { 1799 if (!strncmp(page, "NULL", 4)) {
1782 ret = tcm_vhost_drop_nexus(tv_tpg); 1800 ret = tcm_vhost_drop_nexus(tpg);
1783 return (!ret) ? count : ret; 1801 return (!ret) ? count : ret;
1784 } 1802 }
1785 /* 1803 /*
@@ -1837,7 +1855,7 @@ check_newline:
1837 if (i_port[strlen(i_port)-1] == '\n') 1855 if (i_port[strlen(i_port)-1] == '\n')
1838 i_port[strlen(i_port)-1] = '\0'; 1856 i_port[strlen(i_port)-1] = '\0';
1839 1857
1840 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr); 1858 ret = tcm_vhost_make_nexus(tpg, port_ptr);
1841 if (ret < 0) 1859 if (ret < 0)
1842 return ret; 1860 return ret;
1843 1861
@@ -1851,9 +1869,10 @@ static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1851 NULL, 1869 NULL,
1852}; 1870};
1853 1871
1854static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn, 1872static struct se_portal_group *
1855 struct config_group *group, 1873tcm_vhost_make_tpg(struct se_wwn *wwn,
1856 const char *name) 1874 struct config_group *group,
1875 const char *name)
1857{ 1876{
1858 struct tcm_vhost_tport *tport = container_of(wwn, 1877 struct tcm_vhost_tport *tport = container_of(wwn,
1859 struct tcm_vhost_tport, tport_wwn); 1878 struct tcm_vhost_tport, tport_wwn);
@@ -1909,9 +1928,10 @@ static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1909 kfree(tpg); 1928 kfree(tpg);
1910} 1929}
1911 1930
1912static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf, 1931static struct se_wwn *
1913 struct config_group *group, 1932tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1914 const char *name) 1933 struct config_group *group,
1934 const char *name)
1915{ 1935{
1916 struct tcm_vhost_tport *tport; 1936 struct tcm_vhost_tport *tport;
1917 char *ptr; 1937 char *ptr;
@@ -1981,9 +2001,9 @@ static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1981 kfree(tport); 2001 kfree(tport);
1982} 2002}
1983 2003
1984static ssize_t tcm_vhost_wwn_show_attr_version( 2004static ssize_t
1985 struct target_fabric_configfs *tf, 2005tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
1986 char *page) 2006 char *page)
1987{ 2007{
1988 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s" 2008 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1989 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname, 2009 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,