1 /*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
4 * (C) Copyright 2010-2012 RisingTide Systems LLC.
5 * (C) Copyright 2010-2012 IBM Corp.
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
9 * Authors: Nicholas A. Bellinger <nab@risingtidesystems.com>
10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 ****************************************************************************/
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <generated/utsrelease.h>
27 #include <linux/utsname.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/kthread.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/configfs.h>
34 #include <linux/ctype.h>
35 #include <linux/compat.h>
36 #include <linux/eventfd.h>
38 #include <linux/miscdevice.h>
39 #include <asm/unaligned.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_tcq.h>
42 #include <target/target_core_base.h>
43 #include <target/target_core_fabric.h>
44 #include <target/target_core_fabric_configfs.h>
45 #include <target/target_core_configfs.h>
46 #include <target/configfs_macros.h>
47 #include <linux/vhost.h>
48 #include <linux/virtio_net.h> /* TODO vhost.h currently depends on this */
49 #include <linux/virtio_scsi.h>
50 #include <linux/llist.h>
51 #include <linux/bitmap.h>
55 #include "tcm_vhost.h"
58 VHOST_SCSI_VQ_CTL = 0,
59 VHOST_SCSI_VQ_EVT = 1,
64 * VIRTIO_RING_F_EVENT_IDX seems broken. Not sure the bug is in
65 * kernel but disabling it helps.
66 * TODO: debug and remove the workaround.
69 VHOST_SCSI_FEATURES = (VHOST_FEATURES & (~VIRTIO_RING_F_EVENT_IDX)) |
70 (1ULL << VIRTIO_SCSI_F_HOTPLUG)
73 #define VHOST_SCSI_MAX_TARGET 256
74 #define VHOST_SCSI_MAX_VQ 128
75 #define VHOST_SCSI_MAX_EVENT 128
77 struct vhost_scsi_virtqueue {
78 struct vhost_virtqueue vq;
82 /* Protected by vhost_scsi->dev.mutex */
83 struct tcm_vhost_tpg **vs_tpg;
84 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
87 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
89 struct vhost_work vs_completion_work; /* cmd completion work item */
90 struct llist_head vs_completion_list; /* cmd completion queue */
92 struct vhost_work vs_event_work; /* evt injection work item */
93 struct llist_head vs_event_list; /* evt injection queue */
95 bool vs_events_missed; /* any missed events, protected by vq->mutex */
96 int vs_events_nr; /* num of pending events, protected by vq->mutex */
99 /* Local pointer to allocated TCM configfs fabric module */
100 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
102 static struct workqueue_struct *tcm_vhost_workqueue;
104 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
105 static DEFINE_MUTEX(tcm_vhost_mutex);
106 static LIST_HEAD(tcm_vhost_list);
108 static int iov_num_pages(struct iovec *iov)
110 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
111 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
114 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
119 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
124 static char *tcm_vhost_get_fabric_name(void)
129 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
131 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
132 struct tcm_vhost_tpg, se_tpg);
133 struct tcm_vhost_tport *tport = tpg->tport;
135 switch (tport->tport_proto_id) {
136 case SCSI_PROTOCOL_SAS:
137 return sas_get_fabric_proto_ident(se_tpg);
138 case SCSI_PROTOCOL_FCP:
139 return fc_get_fabric_proto_ident(se_tpg);
140 case SCSI_PROTOCOL_ISCSI:
141 return iscsi_get_fabric_proto_ident(se_tpg);
143 pr_err("Unknown tport_proto_id: 0x%02x, using"
144 " SAS emulation\n", tport->tport_proto_id);
148 return sas_get_fabric_proto_ident(se_tpg);
151 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
153 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
154 struct tcm_vhost_tpg, se_tpg);
155 struct tcm_vhost_tport *tport = tpg->tport;
157 return &tport->tport_name[0];
160 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
162 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
163 struct tcm_vhost_tpg, se_tpg);
164 return tpg->tport_tpgt;
167 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
172 static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
173 struct se_node_acl *se_nacl,
174 struct t10_pr_registration *pr_reg,
178 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
179 struct tcm_vhost_tpg, se_tpg);
180 struct tcm_vhost_tport *tport = tpg->tport;
182 switch (tport->tport_proto_id) {
183 case SCSI_PROTOCOL_SAS:
184 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
186 case SCSI_PROTOCOL_FCP:
187 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
189 case SCSI_PROTOCOL_ISCSI:
190 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
193 pr_err("Unknown tport_proto_id: 0x%02x, using"
194 " SAS emulation\n", tport->tport_proto_id);
198 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
202 static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
203 struct se_node_acl *se_nacl,
204 struct t10_pr_registration *pr_reg,
207 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
208 struct tcm_vhost_tpg, se_tpg);
209 struct tcm_vhost_tport *tport = tpg->tport;
211 switch (tport->tport_proto_id) {
212 case SCSI_PROTOCOL_SAS:
213 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
215 case SCSI_PROTOCOL_FCP:
216 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
218 case SCSI_PROTOCOL_ISCSI:
219 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
222 pr_err("Unknown tport_proto_id: 0x%02x, using"
223 " SAS emulation\n", tport->tport_proto_id);
227 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
231 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
234 char **port_nexus_ptr)
236 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
237 struct tcm_vhost_tpg, se_tpg);
238 struct tcm_vhost_tport *tport = tpg->tport;
240 switch (tport->tport_proto_id) {
241 case SCSI_PROTOCOL_SAS:
242 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
244 case SCSI_PROTOCOL_FCP:
245 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
247 case SCSI_PROTOCOL_ISCSI:
248 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
251 pr_err("Unknown tport_proto_id: 0x%02x, using"
252 " SAS emulation\n", tport->tport_proto_id);
256 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
260 static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
261 struct se_portal_group *se_tpg)
263 struct tcm_vhost_nacl *nacl;
265 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
267 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
271 return &nacl->se_node_acl;
274 static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
275 struct se_node_acl *se_nacl)
277 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
278 struct tcm_vhost_nacl, se_node_acl);
282 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
287 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
292 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
297 static void tcm_vhost_close_session(struct se_session *se_sess)
302 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
307 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
309 /* Go ahead and process the write immediately */
310 target_execute_cmd(se_cmd);
314 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
319 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
324 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
329 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
334 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
336 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
338 llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
340 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
343 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
345 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
346 struct tcm_vhost_cmd, tvc_se_cmd);
347 vhost_scsi_complete_cmd(tv_cmd);
351 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
353 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
354 struct tcm_vhost_cmd, tvc_se_cmd);
355 vhost_scsi_complete_cmd(tv_cmd);
359 static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
364 static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
370 static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs,
371 u32 event, u32 reason)
373 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
374 struct tcm_vhost_evt *evt;
376 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
377 vs->vs_events_missed = true;
381 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
383 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
384 vs->vs_events_missed = true;
388 evt->event.event = event;
389 evt->event.reason = reason;
395 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
397 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
399 /* TODO locking against target/backend threads? */
400 transport_generic_free_cmd(se_cmd, 1);
402 if (tv_cmd->tvc_sgl_count) {
404 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
405 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
407 kfree(tv_cmd->tvc_sgl);
413 static void tcm_vhost_do_evt_work(struct vhost_scsi *vs,
414 struct tcm_vhost_evt *evt)
416 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
417 struct virtio_scsi_event *event = &evt->event;
418 struct virtio_scsi_event __user *eventp;
422 if (!vq->private_data) {
423 vs->vs_events_missed = true;
428 vhost_disable_notify(&vs->dev, vq);
429 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
430 ARRAY_SIZE(vq->iov), &out, &in,
433 vs->vs_events_missed = true;
436 if (head == vq->num) {
437 if (vhost_enable_notify(&vs->dev, vq))
439 vs->vs_events_missed = true;
443 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
444 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
445 vq->iov[out].iov_len);
446 vs->vs_events_missed = true;
450 if (vs->vs_events_missed) {
451 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
452 vs->vs_events_missed = false;
455 eventp = vq->iov[out].iov_base;
456 ret = __copy_to_user(eventp, event, sizeof(*event));
458 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
460 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
463 static void tcm_vhost_evt_work(struct vhost_work *work)
465 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
467 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
468 struct tcm_vhost_evt *evt;
469 struct llist_node *llnode;
471 mutex_lock(&vq->mutex);
472 llnode = llist_del_all(&vs->vs_event_list);
474 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
475 llnode = llist_next(llnode);
476 tcm_vhost_do_evt_work(vs, evt);
477 tcm_vhost_free_evt(vs, evt);
479 mutex_unlock(&vq->mutex);
482 /* Fill in status and signal that we are done processing this command
484 * This is scheduled in the vhost work queue so we are called with the owner
485 * process mm and can access the vring.
487 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
489 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
491 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
492 struct virtio_scsi_cmd_resp v_rsp;
493 struct tcm_vhost_cmd *tv_cmd;
494 struct llist_node *llnode;
495 struct se_cmd *se_cmd;
498 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
499 llnode = llist_del_all(&vs->vs_completion_list);
501 tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
502 tvc_completion_list);
503 llnode = llist_next(llnode);
504 se_cmd = &tv_cmd->tvc_se_cmd;
506 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
507 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
509 memset(&v_rsp, 0, sizeof(v_rsp));
510 v_rsp.resid = se_cmd->residual_count;
511 /* TODO is status_qualifier field needed? */
512 v_rsp.status = se_cmd->scsi_status;
513 v_rsp.sense_len = se_cmd->scsi_sense_length;
514 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
516 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
517 if (likely(ret == 0)) {
518 struct vhost_scsi_virtqueue *q;
519 vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0);
520 q = container_of(tv_cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
522 __set_bit(vq, signal);
524 pr_err("Faulted on virtio_scsi_cmd_resp\n");
526 vhost_scsi_free_cmd(tv_cmd);
530 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
532 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
535 static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
536 struct tcm_vhost_tpg *tv_tpg,
537 struct virtio_scsi_cmd_req *v_req,
541 struct tcm_vhost_cmd *tv_cmd;
542 struct tcm_vhost_nexus *tv_nexus;
544 tv_nexus = tv_tpg->tpg_nexus;
546 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
547 return ERR_PTR(-EIO);
550 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
552 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
553 return ERR_PTR(-ENOMEM);
555 tv_cmd->tvc_tag = v_req->tag;
556 tv_cmd->tvc_task_attr = v_req->task_attr;
557 tv_cmd->tvc_exp_data_len = exp_data_len;
558 tv_cmd->tvc_data_direction = data_direction;
559 tv_cmd->tvc_nexus = tv_nexus;
565 * Map a user memory range into a scatterlist
567 * Returns the number of scatterlist entries used or -errno on error.
569 static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
570 unsigned int sgl_count, struct iovec *iov, int write)
572 unsigned int npages = 0, pages_nr, offset, nbytes;
573 struct scatterlist *sg = sgl;
574 void __user *ptr = iov->iov_base;
575 size_t len = iov->iov_len;
579 pages_nr = iov_num_pages(iov);
580 if (pages_nr > sgl_count)
583 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
587 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
588 /* No pages were pinned */
591 /* Less pages pinned than wanted */
592 if (ret != pages_nr) {
593 for (i = 0; i < ret; i++)
600 offset = (uintptr_t)ptr & ~PAGE_MASK;
601 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
602 sg_set_page(sg, pages[npages], nbytes, offset);
614 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
615 struct iovec *iov, unsigned int niov, int write)
620 struct scatterlist *sg;
623 * Find out how long sglist needs to be
626 for (i = 0; i < niov; i++)
627 sgl_count += iov_num_pages(&iov[i]);
629 /* TODO overflow checking */
631 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
634 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
636 sg_init_table(sg, sgl_count);
638 tv_cmd->tvc_sgl = sg;
639 tv_cmd->tvc_sgl_count = sgl_count;
641 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
642 for (i = 0; i < niov; i++) {
643 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
645 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
646 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
647 kfree(tv_cmd->tvc_sgl);
648 tv_cmd->tvc_sgl = NULL;
649 tv_cmd->tvc_sgl_count = 0;
659 static void tcm_vhost_submission_work(struct work_struct *work)
661 struct tcm_vhost_cmd *tv_cmd =
662 container_of(work, struct tcm_vhost_cmd, work);
663 struct tcm_vhost_nexus *tv_nexus;
664 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
665 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
666 int rc, sg_no_bidi = 0;
668 if (tv_cmd->tvc_sgl_count) {
669 sg_ptr = tv_cmd->tvc_sgl;
670 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
672 if (se_cmd->se_cmd_flags & SCF_BIDI) {
680 tv_nexus = tv_cmd->tvc_nexus;
682 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
683 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
684 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
685 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
686 0, sg_ptr, tv_cmd->tvc_sgl_count,
687 sg_bidi_ptr, sg_no_bidi);
689 transport_send_check_condition_and_sense(se_cmd,
690 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
691 transport_generic_free_cmd(se_cmd, 0);
695 static void vhost_scsi_send_bad_target(struct vhost_scsi *vs,
696 struct vhost_virtqueue *vq, int head, unsigned out)
698 struct virtio_scsi_cmd_resp __user *resp;
699 struct virtio_scsi_cmd_resp rsp;
702 memset(&rsp, 0, sizeof(rsp));
703 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
704 resp = vq->iov[out].iov_base;
705 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
707 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
709 pr_err("Faulted on virtio_scsi_cmd_resp\n");
712 static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
713 struct vhost_virtqueue *vq)
715 struct tcm_vhost_tpg **vs_tpg;
716 struct virtio_scsi_cmd_req v_req;
717 struct tcm_vhost_tpg *tv_tpg;
718 struct tcm_vhost_cmd *tv_cmd;
719 u32 exp_data_len, data_first, data_num, data_direction;
725 * We can handle the vq only after the endpoint is setup by calling the
726 * VHOST_SCSI_SET_ENDPOINT ioctl.
728 * TODO: Check that we are running from vhost_worker which acts
729 * as read-side critical section for vhost kind of RCU.
730 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
732 vs_tpg = rcu_dereference_check(vq->private_data, 1);
736 mutex_lock(&vq->mutex);
737 vhost_disable_notify(&vs->dev, vq);
740 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
741 ARRAY_SIZE(vq->iov), &out, &in,
743 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
745 /* On error, stop handling until the next kick. */
746 if (unlikely(head < 0))
748 /* Nothing new? Wait for eventfd to tell us they refilled. */
749 if (head == vq->num) {
750 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
751 vhost_disable_notify(&vs->dev, vq);
757 /* FIXME: BIDI operation */
758 if (out == 1 && in == 1) {
759 data_direction = DMA_NONE;
762 } else if (out == 1 && in > 1) {
763 data_direction = DMA_FROM_DEVICE;
764 data_first = out + 1;
766 } else if (out > 1 && in == 1) {
767 data_direction = DMA_TO_DEVICE;
771 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
777 * Check for a sane resp buffer so we can report errors to
780 if (unlikely(vq->iov[out].iov_len !=
781 sizeof(struct virtio_scsi_cmd_resp))) {
782 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
783 " bytes\n", vq->iov[out].iov_len);
787 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
788 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
789 " bytes\n", vq->iov[0].iov_len);
792 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
793 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
794 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
797 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
801 /* Extract the tpgt */
802 target = v_req.lun[1];
803 tv_tpg = ACCESS_ONCE(vs_tpg[target]);
805 /* Target does not exist, fail the request */
806 if (unlikely(!tv_tpg)) {
807 vhost_scsi_send_bad_target(vs, vq, head, out);
812 for (i = 0; i < data_num; i++)
813 exp_data_len += vq->iov[data_first + i].iov_len;
815 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
816 exp_data_len, data_direction);
817 if (IS_ERR(tv_cmd)) {
818 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
822 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
823 ": %d\n", tv_cmd, exp_data_len, data_direction);
825 tv_cmd->tvc_vhost = vs;
827 tv_cmd->tvc_resp = vq->iov[out].iov_base;
830 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
831 * that will be used by tcm_vhost_new_cmd_map() and down into
832 * target_setup_cmd_from_cdb()
834 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
836 * Check that the recieved CDB size does not exceeded our
837 * hardcoded max for tcm_vhost
839 /* TODO what if cdb was too small for varlen cdb header? */
840 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
841 TCM_VHOST_MAX_CDB_SIZE)) {
842 vq_err(vq, "Received SCSI CDB with command_size: %d that"
843 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
844 scsi_command_size(tv_cmd->tvc_cdb),
845 TCM_VHOST_MAX_CDB_SIZE);
848 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
850 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
851 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
853 if (data_direction != DMA_NONE) {
854 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
855 &vq->iov[data_first], data_num,
856 data_direction == DMA_TO_DEVICE);
858 vq_err(vq, "Failed to map iov to sgl\n");
864 * Save the descriptor from vhost_get_vq_desc() to be used to
865 * complete the virtio-scsi request in TCM callback context via
866 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
868 tv_cmd->tvc_vq_desc = head;
870 * Dispatch tv_cmd descriptor for cmwq execution in process
871 * context provided by tcm_vhost_workqueue. This also ensures
872 * tv_cmd is executed on the same kworker CPU as this vhost
873 * thread to gain positive L2 cache locality effects..
875 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
876 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
879 mutex_unlock(&vq->mutex);
883 vhost_scsi_free_cmd(tv_cmd);
885 vhost_scsi_send_bad_target(vs, vq, head, out);
886 mutex_unlock(&vq->mutex);
889 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
891 pr_debug("%s: The handling func for control queue.\n", __func__);
894 static void tcm_vhost_send_evt(struct vhost_scsi *vs, struct tcm_vhost_tpg *tpg,
895 struct se_lun *lun, u32 event, u32 reason)
897 struct tcm_vhost_evt *evt;
899 evt = tcm_vhost_allocate_evt(vs, event, reason);
904 /* TODO: share lun setup code with virtio-scsi.ko */
906 * Note: evt->event is zeroed when we allocate it and
907 * lun[4-7] need to be zero according to virtio-scsi spec.
909 evt->event.lun[0] = 0x01;
910 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
911 if (lun->unpacked_lun >= 256)
912 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
913 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
916 llist_add(&evt->list, &vs->vs_event_list);
917 vhost_work_queue(&vs->dev, &vs->vs_event_work);
920 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
922 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
924 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
926 mutex_lock(&vq->mutex);
927 if (!vq->private_data)
930 if (vs->vs_events_missed)
931 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
933 mutex_unlock(&vq->mutex);
936 static void vhost_scsi_handle_kick(struct vhost_work *work)
938 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
940 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
942 vhost_scsi_handle_vq(vs, vq);
945 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
947 vhost_poll_flush(&vs->vqs[index].vq.poll);
950 static void vhost_scsi_flush(struct vhost_scsi *vs)
954 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
955 vhost_scsi_flush_vq(vs, i);
956 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
957 vhost_work_flush(&vs->dev, &vs->vs_event_work);
961 * Called from vhost_scsi_ioctl() context to walk the list of available
962 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
964 * The lock nesting rule is:
965 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
967 static int vhost_scsi_set_endpoint(
968 struct vhost_scsi *vs,
969 struct vhost_scsi_target *t)
971 struct tcm_vhost_tport *tv_tport;
972 struct tcm_vhost_tpg *tv_tpg;
973 struct tcm_vhost_tpg **vs_tpg;
974 struct vhost_virtqueue *vq;
975 int index, ret, i, len;
978 mutex_lock(&tcm_vhost_mutex);
979 mutex_lock(&vs->dev.mutex);
981 /* Verify that ring has been setup correctly. */
982 for (index = 0; index < vs->dev.nvqs; ++index) {
983 /* Verify that ring has been setup correctly. */
984 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
990 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
991 vs_tpg = kzalloc(len, GFP_KERNEL);
997 memcpy(vs_tpg, vs->vs_tpg, len);
999 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
1000 mutex_lock(&tv_tpg->tv_tpg_mutex);
1001 if (!tv_tpg->tpg_nexus) {
1002 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1005 if (tv_tpg->tv_tpg_vhost_count != 0) {
1006 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1009 tv_tport = tv_tpg->tport;
1011 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1012 if (vs->vs_tpg && vs->vs_tpg[tv_tpg->tport_tpgt]) {
1014 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1018 tv_tpg->tv_tpg_vhost_count++;
1019 tv_tpg->vhost_scsi = vs;
1020 vs_tpg[tv_tpg->tport_tpgt] = tv_tpg;
1021 smp_mb__after_atomic_inc();
1024 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1028 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1029 sizeof(vs->vs_vhost_wwpn));
1030 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1031 vq = &vs->vqs[i].vq;
1032 /* Flushing the vhost_work acts as synchronize_rcu */
1033 mutex_lock(&vq->mutex);
1034 rcu_assign_pointer(vq->private_data, vs_tpg);
1035 vhost_init_used(vq);
1036 mutex_unlock(&vq->mutex);
1044 * Act as synchronize_rcu to make sure access to
1045 * old vs->vs_tpg is finished.
1047 vhost_scsi_flush(vs);
1049 vs->vs_tpg = vs_tpg;
1052 mutex_unlock(&vs->dev.mutex);
1053 mutex_unlock(&tcm_vhost_mutex);
1057 static int vhost_scsi_clear_endpoint(
1058 struct vhost_scsi *vs,
1059 struct vhost_scsi_target *t)
1061 struct tcm_vhost_tport *tv_tport;
1062 struct tcm_vhost_tpg *tv_tpg;
1063 struct vhost_virtqueue *vq;
1068 mutex_lock(&tcm_vhost_mutex);
1069 mutex_lock(&vs->dev.mutex);
1070 /* Verify that ring has been setup correctly. */
1071 for (index = 0; index < vs->dev.nvqs; ++index) {
1072 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1083 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1085 tv_tpg = vs->vs_tpg[target];
1089 mutex_lock(&tv_tpg->tv_tpg_mutex);
1090 tv_tport = tv_tpg->tport;
1096 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1097 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
1098 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1099 tv_tport->tport_name, tv_tpg->tport_tpgt,
1100 t->vhost_wwpn, t->vhost_tpgt);
1104 tv_tpg->tv_tpg_vhost_count--;
1105 tv_tpg->vhost_scsi = NULL;
1106 vs->vs_tpg[target] = NULL;
1108 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1111 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1112 vq = &vs->vqs[i].vq;
1113 /* Flushing the vhost_work acts as synchronize_rcu */
1114 mutex_lock(&vq->mutex);
1115 rcu_assign_pointer(vq->private_data, NULL);
1116 mutex_unlock(&vq->mutex);
1120 * Act as synchronize_rcu to make sure access to
1121 * old vs->vs_tpg is finished.
1123 vhost_scsi_flush(vs);
1126 WARN_ON(vs->vs_events_nr);
1127 mutex_unlock(&vs->dev.mutex);
1128 mutex_unlock(&tcm_vhost_mutex);
1132 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1134 mutex_unlock(&vs->dev.mutex);
1135 mutex_unlock(&tcm_vhost_mutex);
1139 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1141 if (features & ~VHOST_SCSI_FEATURES)
1144 mutex_lock(&vs->dev.mutex);
1145 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1146 !vhost_log_access_ok(&vs->dev)) {
1147 mutex_unlock(&vs->dev.mutex);
1150 vs->dev.acked_features = features;
1152 vhost_scsi_flush(vs);
1153 mutex_unlock(&vs->dev.mutex);
1157 static int vhost_scsi_open(struct inode *inode, struct file *f)
1159 struct vhost_scsi *s;
1160 struct vhost_virtqueue **vqs;
1163 s = kzalloc(sizeof(*s), GFP_KERNEL);
1167 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1173 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
1174 vhost_work_init(&s->vs_event_work, tcm_vhost_evt_work);
1176 s->vs_events_nr = 0;
1177 s->vs_events_missed = false;
1179 vqs[VHOST_SCSI_VQ_CTL] = &s->vqs[VHOST_SCSI_VQ_CTL].vq;
1180 vqs[VHOST_SCSI_VQ_EVT] = &s->vqs[VHOST_SCSI_VQ_EVT].vq;
1181 s->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1182 s->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
1183 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
1184 vqs[i] = &s->vqs[i].vq;
1185 s->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
1187 r = vhost_dev_init(&s->dev, vqs, VHOST_SCSI_MAX_VQ);
1194 f->private_data = s;
1198 static int vhost_scsi_release(struct inode *inode, struct file *f)
1200 struct vhost_scsi *s = f->private_data;
1201 struct vhost_scsi_target t;
1203 mutex_lock(&s->dev.mutex);
1204 memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1205 mutex_unlock(&s->dev.mutex);
1206 vhost_scsi_clear_endpoint(s, &t);
1207 vhost_dev_stop(&s->dev);
1208 vhost_dev_cleanup(&s->dev, false);
1209 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1210 vhost_scsi_flush(s);
1216 static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
1219 struct vhost_scsi *vs = f->private_data;
1220 struct vhost_scsi_target backend;
1221 void __user *argp = (void __user *)arg;
1222 u64 __user *featurep = argp;
1223 u32 __user *eventsp = argp;
1226 int r, abi_version = VHOST_SCSI_ABI_VERSION;
1227 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1230 case VHOST_SCSI_SET_ENDPOINT:
1231 if (copy_from_user(&backend, argp, sizeof backend))
1233 if (backend.reserved != 0)
1236 return vhost_scsi_set_endpoint(vs, &backend);
1237 case VHOST_SCSI_CLEAR_ENDPOINT:
1238 if (copy_from_user(&backend, argp, sizeof backend))
1240 if (backend.reserved != 0)
1243 return vhost_scsi_clear_endpoint(vs, &backend);
1244 case VHOST_SCSI_GET_ABI_VERSION:
1245 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1248 case VHOST_SCSI_SET_EVENTS_MISSED:
1249 if (get_user(events_missed, eventsp))
1251 mutex_lock(&vq->mutex);
1252 vs->vs_events_missed = events_missed;
1253 mutex_unlock(&vq->mutex);
1255 case VHOST_SCSI_GET_EVENTS_MISSED:
1256 mutex_lock(&vq->mutex);
1257 events_missed = vs->vs_events_missed;
1258 mutex_unlock(&vq->mutex);
1259 if (put_user(events_missed, eventsp))
1262 case VHOST_GET_FEATURES:
1263 features = VHOST_SCSI_FEATURES;
1264 if (copy_to_user(featurep, &features, sizeof features))
1267 case VHOST_SET_FEATURES:
1268 if (copy_from_user(&features, featurep, sizeof features))
1270 return vhost_scsi_set_features(vs, features);
1272 mutex_lock(&vs->dev.mutex);
1273 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1274 /* TODO: flush backend after dev ioctl. */
1275 if (r == -ENOIOCTLCMD)
1276 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1277 mutex_unlock(&vs->dev.mutex);
1282 #ifdef CONFIG_COMPAT
1283 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1286 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1290 static const struct file_operations vhost_scsi_fops = {
1291 .owner = THIS_MODULE,
1292 .release = vhost_scsi_release,
1293 .unlocked_ioctl = vhost_scsi_ioctl,
1294 #ifdef CONFIG_COMPAT
1295 .compat_ioctl = vhost_scsi_compat_ioctl,
1297 .open = vhost_scsi_open,
1298 .llseek = noop_llseek,
1301 static struct miscdevice vhost_scsi_misc = {
1307 static int __init vhost_scsi_register(void)
1309 return misc_register(&vhost_scsi_misc);
1312 static int vhost_scsi_deregister(void)
1314 return misc_deregister(&vhost_scsi_misc);
1317 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1319 switch (tport->tport_proto_id) {
1320 case SCSI_PROTOCOL_SAS:
1322 case SCSI_PROTOCOL_FCP:
1324 case SCSI_PROTOCOL_ISCSI:
1333 static void tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1334 struct se_lun *lun, bool plug)
1337 struct vhost_scsi *vs = tpg->vhost_scsi;
1338 struct vhost_virtqueue *vq;
1344 mutex_lock(&vs->dev.mutex);
1345 if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1346 mutex_unlock(&vs->dev.mutex);
1351 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1353 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1355 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1356 mutex_lock(&vq->mutex);
1357 tcm_vhost_send_evt(vs, tpg, lun,
1358 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1359 mutex_unlock(&vq->mutex);
1360 mutex_unlock(&vs->dev.mutex);
1363 static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1365 tcm_vhost_do_plug(tpg, lun, true);
1368 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1370 tcm_vhost_do_plug(tpg, lun, false);
1373 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1376 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1377 struct tcm_vhost_tpg, se_tpg);
1379 mutex_lock(&tcm_vhost_mutex);
1381 mutex_lock(&tv_tpg->tv_tpg_mutex);
1382 tv_tpg->tv_tpg_port_count++;
1383 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1385 tcm_vhost_hotplug(tv_tpg, lun);
1387 mutex_unlock(&tcm_vhost_mutex);
1392 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1395 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1396 struct tcm_vhost_tpg, se_tpg);
1398 mutex_lock(&tcm_vhost_mutex);
1400 mutex_lock(&tv_tpg->tv_tpg_mutex);
1401 tv_tpg->tv_tpg_port_count--;
1402 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1404 tcm_vhost_hotunplug(tv_tpg, lun);
1406 mutex_unlock(&tcm_vhost_mutex);
1409 static struct se_node_acl *tcm_vhost_make_nodeacl(
1410 struct se_portal_group *se_tpg,
1411 struct config_group *group,
1414 struct se_node_acl *se_nacl, *se_nacl_new;
1415 struct tcm_vhost_nacl *nacl;
1419 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1420 return ERR_PTR(-EINVAL); */
1421 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1423 return ERR_PTR(-ENOMEM);
1427 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1428 * when converting a NodeACL from demo mode -> explict
1430 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1432 if (IS_ERR(se_nacl)) {
1433 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1437 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1439 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1440 nacl->iport_wwpn = wwpn;
1445 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1447 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1448 struct tcm_vhost_nacl, se_node_acl);
1449 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1453 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1456 struct se_portal_group *se_tpg;
1457 struct tcm_vhost_nexus *tv_nexus;
1459 mutex_lock(&tv_tpg->tv_tpg_mutex);
1460 if (tv_tpg->tpg_nexus) {
1461 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1462 pr_debug("tv_tpg->tpg_nexus already exists\n");
1465 se_tpg = &tv_tpg->se_tpg;
1467 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1469 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1470 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1474 * Initialize the struct se_session pointer
1476 tv_nexus->tvn_se_sess = transport_init_session();
1477 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1478 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1483 * Since we are running in 'demo mode' this call with generate a
1484 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1485 * the SCSI Initiator port name of the passed configfs group 'name'.
1487 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1488 se_tpg, (unsigned char *)name);
1489 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1490 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1491 pr_debug("core_tpg_check_initiator_node_acl() failed"
1493 transport_free_session(tv_nexus->tvn_se_sess);
1498 * Now register the TCM vhost virtual I_T Nexus as active with the
1499 * call to __transport_register_session()
1501 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1502 tv_nexus->tvn_se_sess, tv_nexus);
1503 tv_tpg->tpg_nexus = tv_nexus;
1505 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1509 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1511 struct se_session *se_sess;
1512 struct tcm_vhost_nexus *tv_nexus;
1514 mutex_lock(&tpg->tv_tpg_mutex);
1515 tv_nexus = tpg->tpg_nexus;
1517 mutex_unlock(&tpg->tv_tpg_mutex);
1521 se_sess = tv_nexus->tvn_se_sess;
1523 mutex_unlock(&tpg->tv_tpg_mutex);
1527 if (tpg->tv_tpg_port_count != 0) {
1528 mutex_unlock(&tpg->tv_tpg_mutex);
1529 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1530 " active TPG port count: %d\n",
1531 tpg->tv_tpg_port_count);
1535 if (tpg->tv_tpg_vhost_count != 0) {
1536 mutex_unlock(&tpg->tv_tpg_mutex);
1537 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1538 " active TPG vhost count: %d\n",
1539 tpg->tv_tpg_vhost_count);
1543 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1544 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1545 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1547 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1549 transport_deregister_session(tv_nexus->tvn_se_sess);
1550 tpg->tpg_nexus = NULL;
1551 mutex_unlock(&tpg->tv_tpg_mutex);
1557 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1560 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1561 struct tcm_vhost_tpg, se_tpg);
1562 struct tcm_vhost_nexus *tv_nexus;
1565 mutex_lock(&tv_tpg->tv_tpg_mutex);
1566 tv_nexus = tv_tpg->tpg_nexus;
1568 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1571 ret = snprintf(page, PAGE_SIZE, "%s\n",
1572 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1573 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1578 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1582 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1583 struct tcm_vhost_tpg, se_tpg);
1584 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1585 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1588 * Shutdown the active I_T nexus if 'NULL' is passed..
1590 if (!strncmp(page, "NULL", 4)) {
1591 ret = tcm_vhost_drop_nexus(tv_tpg);
1592 return (!ret) ? count : ret;
1595 * Otherwise make sure the passed virtual Initiator port WWN matches
1596 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1597 * tcm_vhost_make_nexus().
1599 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1600 pr_err("Emulated NAA Sas Address: %s, exceeds"
1601 " max: %d\n", page, TCM_VHOST_NAMELEN);
1604 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1606 ptr = strstr(i_port, "naa.");
1608 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1609 pr_err("Passed SAS Initiator Port %s does not"
1610 " match target port protoid: %s\n", i_port,
1611 tcm_vhost_dump_proto_id(tport_wwn));
1614 port_ptr = &i_port[0];
1617 ptr = strstr(i_port, "fc.");
1619 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1620 pr_err("Passed FCP Initiator Port %s does not"
1621 " match target port protoid: %s\n", i_port,
1622 tcm_vhost_dump_proto_id(tport_wwn));
1625 port_ptr = &i_port[3]; /* Skip over "fc." */
1628 ptr = strstr(i_port, "iqn.");
1630 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1631 pr_err("Passed iSCSI Initiator Port %s does not"
1632 " match target port protoid: %s\n", i_port,
1633 tcm_vhost_dump_proto_id(tport_wwn));
1636 port_ptr = &i_port[0];
1639 pr_err("Unable to locate prefix for emulated Initiator Port:"
1643 * Clear any trailing newline for the NAA WWN
1646 if (i_port[strlen(i_port)-1] == '\n')
1647 i_port[strlen(i_port)-1] = '\0';
1649 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1656 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1658 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1659 &tcm_vhost_tpg_nexus.attr,
1663 static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
1664 struct config_group *group,
1667 struct tcm_vhost_tport *tport = container_of(wwn,
1668 struct tcm_vhost_tport, tport_wwn);
1670 struct tcm_vhost_tpg *tpg;
1674 if (strstr(name, "tpgt_") != name)
1675 return ERR_PTR(-EINVAL);
1676 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1677 return ERR_PTR(-EINVAL);
1679 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1681 pr_err("Unable to allocate struct tcm_vhost_tpg");
1682 return ERR_PTR(-ENOMEM);
1684 mutex_init(&tpg->tv_tpg_mutex);
1685 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1687 tpg->tport_tpgt = tpgt;
1689 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1690 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1695 mutex_lock(&tcm_vhost_mutex);
1696 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1697 mutex_unlock(&tcm_vhost_mutex);
1699 return &tpg->se_tpg;
1702 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1704 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1705 struct tcm_vhost_tpg, se_tpg);
1707 mutex_lock(&tcm_vhost_mutex);
1708 list_del(&tpg->tv_tpg_list);
1709 mutex_unlock(&tcm_vhost_mutex);
1711 * Release the virtual I_T Nexus for this vhost TPG
1713 tcm_vhost_drop_nexus(tpg);
1715 * Deregister the se_tpg from TCM..
1717 core_tpg_deregister(se_tpg);
1721 static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1722 struct config_group *group,
1725 struct tcm_vhost_tport *tport;
1730 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1731 return ERR_PTR(-EINVAL); */
1733 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1735 pr_err("Unable to allocate struct tcm_vhost_tport");
1736 return ERR_PTR(-ENOMEM);
1738 tport->tport_wwpn = wwpn;
1740 * Determine the emulated Protocol Identifier and Target Port Name
1741 * based on the incoming configfs directory name.
1743 ptr = strstr(name, "naa.");
1745 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1748 ptr = strstr(name, "fc.");
1750 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1751 off = 3; /* Skip over "fc." */
1754 ptr = strstr(name, "iqn.");
1756 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1760 pr_err("Unable to locate prefix for emulated Target Port:"
1763 return ERR_PTR(-EINVAL);
1766 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1767 pr_err("Emulated %s Address: %s, exceeds"
1768 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1771 return ERR_PTR(-EINVAL);
1773 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1775 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1776 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1778 return &tport->tport_wwn;
1781 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1783 struct tcm_vhost_tport *tport = container_of(wwn,
1784 struct tcm_vhost_tport, tport_wwn);
1786 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1787 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1793 static ssize_t tcm_vhost_wwn_show_attr_version(
1794 struct target_fabric_configfs *tf,
1797 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1798 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1799 utsname()->machine);
1802 TF_WWN_ATTR_RO(tcm_vhost, version);
1804 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1805 &tcm_vhost_wwn_version.attr,
1809 static struct target_core_fabric_ops tcm_vhost_ops = {
1810 .get_fabric_name = tcm_vhost_get_fabric_name,
1811 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1812 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1813 .tpg_get_tag = tcm_vhost_get_tag,
1814 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1815 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1816 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1817 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1818 .tpg_check_demo_mode = tcm_vhost_check_true,
1819 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1820 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1821 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1822 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1823 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1824 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1825 .release_cmd = tcm_vhost_release_cmd,
1826 .shutdown_session = tcm_vhost_shutdown_session,
1827 .close_session = tcm_vhost_close_session,
1828 .sess_get_index = tcm_vhost_sess_get_index,
1829 .sess_get_initiator_sid = NULL,
1830 .write_pending = tcm_vhost_write_pending,
1831 .write_pending_status = tcm_vhost_write_pending_status,
1832 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1833 .get_task_tag = tcm_vhost_get_task_tag,
1834 .get_cmd_state = tcm_vhost_get_cmd_state,
1835 .queue_data_in = tcm_vhost_queue_data_in,
1836 .queue_status = tcm_vhost_queue_status,
1837 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
1839 * Setup callers for generic logic in target_core_fabric_configfs.c
1841 .fabric_make_wwn = tcm_vhost_make_tport,
1842 .fabric_drop_wwn = tcm_vhost_drop_tport,
1843 .fabric_make_tpg = tcm_vhost_make_tpg,
1844 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1845 .fabric_post_link = tcm_vhost_port_link,
1846 .fabric_pre_unlink = tcm_vhost_port_unlink,
1847 .fabric_make_np = NULL,
1848 .fabric_drop_np = NULL,
1849 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1850 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1853 static int tcm_vhost_register_configfs(void)
1855 struct target_fabric_configfs *fabric;
1858 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1859 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1860 utsname()->machine);
1862 * Register the top level struct config_item_type with TCM core
1864 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1865 if (IS_ERR(fabric)) {
1866 pr_err("target_fabric_configfs_init() failed\n");
1867 return PTR_ERR(fabric);
1870 * Setup fabric->tf_ops from our local tcm_vhost_ops
1872 fabric->tf_ops = tcm_vhost_ops;
1874 * Setup default attribute lists for various fabric->tf_cit_tmpl
1876 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1877 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1878 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1879 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1880 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1881 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1882 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1883 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1884 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1886 * Register the fabric for use within TCM
1888 ret = target_fabric_configfs_register(fabric);
1890 pr_err("target_fabric_configfs_register() failed"
1891 " for TCM_VHOST\n");
1895 * Setup our local pointer to *fabric
1897 tcm_vhost_fabric_configfs = fabric;
1898 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1902 static void tcm_vhost_deregister_configfs(void)
1904 if (!tcm_vhost_fabric_configfs)
1907 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1908 tcm_vhost_fabric_configfs = NULL;
1909 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1912 static int __init tcm_vhost_init(void)
1916 * Use our own dedicated workqueue for submitting I/O into
1917 * target core to avoid contention within system_wq.
1919 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1920 if (!tcm_vhost_workqueue)
1923 ret = vhost_scsi_register();
1925 goto out_destroy_workqueue;
1927 ret = tcm_vhost_register_configfs();
1929 goto out_vhost_scsi_deregister;
1933 out_vhost_scsi_deregister:
1934 vhost_scsi_deregister();
1935 out_destroy_workqueue:
1936 destroy_workqueue(tcm_vhost_workqueue);
1941 static void tcm_vhost_exit(void)
1943 tcm_vhost_deregister_configfs();
1944 vhost_scsi_deregister();
1945 destroy_workqueue(tcm_vhost_workqueue);
1948 MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1949 MODULE_LICENSE("GPL");
1950 module_init(tcm_vhost_init);
1951 module_exit(tcm_vhost_exit);