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>
53 #include "tcm_vhost.h"
56 VHOST_SCSI_VQ_CTL = 0,
57 VHOST_SCSI_VQ_EVT = 1,
62 struct tcm_vhost_tpg *vs_tpg; /* Protected by vhost_scsi->dev.mutex */
64 struct vhost_virtqueue vqs[3];
66 struct vhost_work vs_completion_work; /* cmd completion work item */
67 struct list_head vs_completion_list; /* cmd completion queue */
68 spinlock_t vs_completion_lock; /* protects s_completion_list */
71 /* Local pointer to allocated TCM configfs fabric module */
72 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
74 static struct workqueue_struct *tcm_vhost_workqueue;
76 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
77 static DEFINE_MUTEX(tcm_vhost_mutex);
78 static LIST_HEAD(tcm_vhost_list);
80 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
85 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
90 static char *tcm_vhost_get_fabric_name(void)
95 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
97 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
98 struct tcm_vhost_tpg, se_tpg);
99 struct tcm_vhost_tport *tport = tpg->tport;
101 switch (tport->tport_proto_id) {
102 case SCSI_PROTOCOL_SAS:
103 return sas_get_fabric_proto_ident(se_tpg);
104 case SCSI_PROTOCOL_FCP:
105 return fc_get_fabric_proto_ident(se_tpg);
106 case SCSI_PROTOCOL_ISCSI:
107 return iscsi_get_fabric_proto_ident(se_tpg);
109 pr_err("Unknown tport_proto_id: 0x%02x, using"
110 " SAS emulation\n", tport->tport_proto_id);
114 return sas_get_fabric_proto_ident(se_tpg);
117 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
119 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
120 struct tcm_vhost_tpg, se_tpg);
121 struct tcm_vhost_tport *tport = tpg->tport;
123 return &tport->tport_name[0];
126 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
128 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
129 struct tcm_vhost_tpg, se_tpg);
130 return tpg->tport_tpgt;
133 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
138 static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
139 struct se_node_acl *se_nacl,
140 struct t10_pr_registration *pr_reg,
144 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
145 struct tcm_vhost_tpg, se_tpg);
146 struct tcm_vhost_tport *tport = tpg->tport;
148 switch (tport->tport_proto_id) {
149 case SCSI_PROTOCOL_SAS:
150 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
152 case SCSI_PROTOCOL_FCP:
153 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
155 case SCSI_PROTOCOL_ISCSI:
156 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
159 pr_err("Unknown tport_proto_id: 0x%02x, using"
160 " SAS emulation\n", tport->tport_proto_id);
164 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
168 static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
169 struct se_node_acl *se_nacl,
170 struct t10_pr_registration *pr_reg,
173 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
174 struct tcm_vhost_tpg, se_tpg);
175 struct tcm_vhost_tport *tport = tpg->tport;
177 switch (tport->tport_proto_id) {
178 case SCSI_PROTOCOL_SAS:
179 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
181 case SCSI_PROTOCOL_FCP:
182 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
184 case SCSI_PROTOCOL_ISCSI:
185 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
188 pr_err("Unknown tport_proto_id: 0x%02x, using"
189 " SAS emulation\n", tport->tport_proto_id);
193 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
197 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
200 char **port_nexus_ptr)
202 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
203 struct tcm_vhost_tpg, se_tpg);
204 struct tcm_vhost_tport *tport = tpg->tport;
206 switch (tport->tport_proto_id) {
207 case SCSI_PROTOCOL_SAS:
208 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
210 case SCSI_PROTOCOL_FCP:
211 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
213 case SCSI_PROTOCOL_ISCSI:
214 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
217 pr_err("Unknown tport_proto_id: 0x%02x, using"
218 " SAS emulation\n", tport->tport_proto_id);
222 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
226 static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
227 struct se_portal_group *se_tpg)
229 struct tcm_vhost_nacl *nacl;
231 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
233 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
237 return &nacl->se_node_acl;
240 static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
241 struct se_node_acl *se_nacl)
243 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
244 struct tcm_vhost_nacl, se_node_acl);
248 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
253 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
258 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
263 static void tcm_vhost_close_session(struct se_session *se_sess)
268 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
273 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
275 /* Go ahead and process the write immediately */
276 target_execute_cmd(se_cmd);
280 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
285 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
290 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
295 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
300 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
302 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
304 spin_lock_bh(&vs->vs_completion_lock);
305 list_add_tail(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
306 spin_unlock_bh(&vs->vs_completion_lock);
308 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
311 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
313 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
314 struct tcm_vhost_cmd, tvc_se_cmd);
315 vhost_scsi_complete_cmd(tv_cmd);
319 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
321 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
322 struct tcm_vhost_cmd, tvc_se_cmd);
323 vhost_scsi_complete_cmd(tv_cmd);
327 static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
332 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
334 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
336 /* TODO locking against target/backend threads? */
337 transport_generic_free_cmd(se_cmd, 1);
339 if (tv_cmd->tvc_sgl_count) {
341 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
342 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
344 kfree(tv_cmd->tvc_sgl);
350 /* Dequeue a command from the completion list */
351 static struct tcm_vhost_cmd *vhost_scsi_get_cmd_from_completion(
352 struct vhost_scsi *vs)
354 struct tcm_vhost_cmd *tv_cmd = NULL;
356 spin_lock_bh(&vs->vs_completion_lock);
357 if (list_empty(&vs->vs_completion_list)) {
358 spin_unlock_bh(&vs->vs_completion_lock);
362 list_for_each_entry(tv_cmd, &vs->vs_completion_list,
363 tvc_completion_list) {
364 list_del(&tv_cmd->tvc_completion_list);
367 spin_unlock_bh(&vs->vs_completion_lock);
371 /* Fill in status and signal that we are done processing this command
373 * This is scheduled in the vhost work queue so we are called with the owner
374 * process mm and can access the vring.
376 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
378 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
380 struct tcm_vhost_cmd *tv_cmd;
382 while ((tv_cmd = vhost_scsi_get_cmd_from_completion(vs))) {
383 struct virtio_scsi_cmd_resp v_rsp;
384 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
387 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
388 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
390 memset(&v_rsp, 0, sizeof(v_rsp));
391 v_rsp.resid = se_cmd->residual_count;
392 /* TODO is status_qualifier field needed? */
393 v_rsp.status = se_cmd->scsi_status;
394 v_rsp.sense_len = se_cmd->scsi_sense_length;
395 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
397 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
398 if (likely(ret == 0))
399 vhost_add_used(&vs->vqs[2], tv_cmd->tvc_vq_desc, 0);
401 pr_err("Faulted on virtio_scsi_cmd_resp\n");
403 vhost_scsi_free_cmd(tv_cmd);
406 vhost_signal(&vs->dev, &vs->vqs[2]);
409 static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
410 struct tcm_vhost_tpg *tv_tpg,
411 struct virtio_scsi_cmd_req *v_req,
415 struct tcm_vhost_cmd *tv_cmd;
416 struct tcm_vhost_nexus *tv_nexus;
418 tv_nexus = tv_tpg->tpg_nexus;
420 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
421 return ERR_PTR(-EIO);
424 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
426 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
427 return ERR_PTR(-ENOMEM);
429 INIT_LIST_HEAD(&tv_cmd->tvc_completion_list);
430 tv_cmd->tvc_tag = v_req->tag;
431 tv_cmd->tvc_task_attr = v_req->task_attr;
432 tv_cmd->tvc_exp_data_len = exp_data_len;
433 tv_cmd->tvc_data_direction = data_direction;
434 tv_cmd->tvc_nexus = tv_nexus;
440 * Map a user memory range into a scatterlist
442 * Returns the number of scatterlist entries used or -errno on error.
444 static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
445 unsigned int sgl_count, void __user *ptr, size_t len, int write)
447 struct scatterlist *sg = sgl;
448 unsigned int npages = 0;
453 unsigned int offset = (uintptr_t)ptr & ~PAGE_MASK;
454 unsigned int nbytes = min_t(unsigned int,
455 PAGE_SIZE - offset, len);
457 if (npages == sgl_count) {
462 ret = get_user_pages_fast((unsigned long)ptr, 1, write, &page);
463 BUG_ON(ret == 0); /* we should either get our page or fail */
467 sg_set_page(sg, page, nbytes, offset);
476 /* Put pages that we hold */
477 for (sg = sgl; sg != &sgl[npages]; sg++)
478 put_page(sg_page(sg));
482 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
483 struct iovec *iov, unsigned int niov, int write)
488 struct scatterlist *sg;
491 * Find out how long sglist needs to be
494 for (i = 0; i < niov; i++) {
495 sgl_count += (((uintptr_t)iov[i].iov_base + iov[i].iov_len +
496 PAGE_SIZE - 1) >> PAGE_SHIFT) -
497 ((uintptr_t)iov[i].iov_base >> PAGE_SHIFT);
499 /* TODO overflow checking */
501 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
504 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
506 sg_init_table(sg, sgl_count);
508 tv_cmd->tvc_sgl = sg;
509 tv_cmd->tvc_sgl_count = sgl_count;
511 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
512 for (i = 0; i < niov; i++) {
513 ret = vhost_scsi_map_to_sgl(sg, sgl_count, iov[i].iov_base,
514 iov[i].iov_len, write);
516 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
517 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
518 kfree(tv_cmd->tvc_sgl);
519 tv_cmd->tvc_sgl = NULL;
520 tv_cmd->tvc_sgl_count = 0;
530 static void tcm_vhost_submission_work(struct work_struct *work)
532 struct tcm_vhost_cmd *tv_cmd =
533 container_of(work, struct tcm_vhost_cmd, work);
534 struct tcm_vhost_nexus *tv_nexus;
535 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
536 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
537 int rc, sg_no_bidi = 0;
539 if (tv_cmd->tvc_sgl_count) {
540 sg_ptr = tv_cmd->tvc_sgl;
541 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
543 if (se_cmd->se_cmd_flags & SCF_BIDI) {
551 tv_nexus = tv_cmd->tvc_nexus;
553 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
554 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
555 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
556 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
557 0, sg_ptr, tv_cmd->tvc_sgl_count,
558 sg_bidi_ptr, sg_no_bidi);
560 transport_send_check_condition_and_sense(se_cmd,
561 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
562 transport_generic_free_cmd(se_cmd, 0);
566 static void vhost_scsi_handle_vq(struct vhost_scsi *vs)
568 struct vhost_virtqueue *vq = &vs->vqs[2];
569 struct virtio_scsi_cmd_req v_req;
570 struct tcm_vhost_tpg *tv_tpg;
571 struct tcm_vhost_cmd *tv_cmd;
572 u32 exp_data_len, data_first, data_num, data_direction;
576 /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
578 if (unlikely(!tv_tpg)) {
579 pr_err("%s endpoint not set\n", __func__);
583 mutex_lock(&vq->mutex);
584 vhost_disable_notify(&vs->dev, vq);
587 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
588 ARRAY_SIZE(vq->iov), &out, &in,
590 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
592 /* On error, stop handling until the next kick. */
593 if (unlikely(head < 0))
595 /* Nothing new? Wait for eventfd to tell us they refilled. */
596 if (head == vq->num) {
597 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
598 vhost_disable_notify(&vs->dev, vq);
604 /* FIXME: BIDI operation */
605 if (out == 1 && in == 1) {
606 data_direction = DMA_NONE;
609 } else if (out == 1 && in > 1) {
610 data_direction = DMA_FROM_DEVICE;
611 data_first = out + 1;
613 } else if (out > 1 && in == 1) {
614 data_direction = DMA_TO_DEVICE;
618 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
624 * Check for a sane resp buffer so we can report errors to
627 if (unlikely(vq->iov[out].iov_len !=
628 sizeof(struct virtio_scsi_cmd_resp))) {
629 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
630 " bytes\n", vq->iov[out].iov_len);
634 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
635 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
636 " bytes\n", vq->iov[0].iov_len);
639 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
640 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
641 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
644 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
649 for (i = 0; i < data_num; i++)
650 exp_data_len += vq->iov[data_first + i].iov_len;
652 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
653 exp_data_len, data_direction);
654 if (IS_ERR(tv_cmd)) {
655 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
659 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
660 ": %d\n", tv_cmd, exp_data_len, data_direction);
662 tv_cmd->tvc_vhost = vs;
664 if (unlikely(vq->iov[out].iov_len !=
665 sizeof(struct virtio_scsi_cmd_resp))) {
666 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
667 " bytes, out: %d, in: %d\n",
668 vq->iov[out].iov_len, out, in);
672 tv_cmd->tvc_resp = vq->iov[out].iov_base;
675 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
676 * that will be used by tcm_vhost_new_cmd_map() and down into
677 * target_setup_cmd_from_cdb()
679 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
681 * Check that the recieved CDB size does not exceeded our
682 * hardcoded max for tcm_vhost
684 /* TODO what if cdb was too small for varlen cdb header? */
685 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
686 TCM_VHOST_MAX_CDB_SIZE)) {
687 vq_err(vq, "Received SCSI CDB with command_size: %d that"
688 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
689 scsi_command_size(tv_cmd->tvc_cdb),
690 TCM_VHOST_MAX_CDB_SIZE);
693 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
695 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
696 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
698 if (data_direction != DMA_NONE) {
699 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
700 &vq->iov[data_first], data_num,
701 data_direction == DMA_TO_DEVICE);
703 vq_err(vq, "Failed to map iov to sgl\n");
709 * Save the descriptor from vhost_get_vq_desc() to be used to
710 * complete the virtio-scsi request in TCM callback context via
711 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
713 tv_cmd->tvc_vq_desc = head;
715 * Dispatch tv_cmd descriptor for cmwq execution in process
716 * context provided by tcm_vhost_workqueue. This also ensures
717 * tv_cmd is executed on the same kworker CPU as this vhost
718 * thread to gain positive L2 cache locality effects..
720 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
721 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
724 mutex_unlock(&vq->mutex);
727 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
729 pr_debug("%s: The handling func for control queue.\n", __func__);
732 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
734 pr_debug("%s: The handling func for event queue.\n", __func__);
737 static void vhost_scsi_handle_kick(struct vhost_work *work)
739 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
741 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
743 vhost_scsi_handle_vq(vs);
747 * Called from vhost_scsi_ioctl() context to walk the list of available
748 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
750 static int vhost_scsi_set_endpoint(
751 struct vhost_scsi *vs,
752 struct vhost_scsi_target *t)
754 struct tcm_vhost_tport *tv_tport;
755 struct tcm_vhost_tpg *tv_tpg;
758 mutex_lock(&vs->dev.mutex);
759 /* Verify that ring has been setup correctly. */
760 for (index = 0; index < vs->dev.nvqs; ++index) {
761 /* Verify that ring has been setup correctly. */
762 if (!vhost_vq_access_ok(&vs->vqs[index])) {
763 mutex_unlock(&vs->dev.mutex);
767 mutex_unlock(&vs->dev.mutex);
769 mutex_lock(&tcm_vhost_mutex);
770 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
771 mutex_lock(&tv_tpg->tv_tpg_mutex);
772 if (!tv_tpg->tpg_nexus) {
773 mutex_unlock(&tv_tpg->tv_tpg_mutex);
776 if (tv_tpg->tv_tpg_vhost_count != 0) {
777 mutex_unlock(&tv_tpg->tv_tpg_mutex);
780 tv_tport = tv_tpg->tport;
782 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn) &&
783 (tv_tpg->tport_tpgt == t->vhost_tpgt)) {
784 tv_tpg->tv_tpg_vhost_count++;
785 mutex_unlock(&tv_tpg->tv_tpg_mutex);
786 mutex_unlock(&tcm_vhost_mutex);
788 mutex_lock(&vs->dev.mutex);
790 mutex_unlock(&vs->dev.mutex);
791 mutex_lock(&tv_tpg->tv_tpg_mutex);
792 tv_tpg->tv_tpg_vhost_count--;
793 mutex_unlock(&tv_tpg->tv_tpg_mutex);
798 smp_mb__after_atomic_inc();
799 mutex_unlock(&vs->dev.mutex);
802 mutex_unlock(&tv_tpg->tv_tpg_mutex);
804 mutex_unlock(&tcm_vhost_mutex);
808 static int vhost_scsi_clear_endpoint(
809 struct vhost_scsi *vs,
810 struct vhost_scsi_target *t)
812 struct tcm_vhost_tport *tv_tport;
813 struct tcm_vhost_tpg *tv_tpg;
816 mutex_lock(&vs->dev.mutex);
817 /* Verify that ring has been setup correctly. */
818 for (index = 0; index < vs->dev.nvqs; ++index) {
819 if (!vhost_vq_access_ok(&vs->vqs[index])) {
830 tv_tport = tv_tpg->tport;
832 if (strcmp(tv_tport->tport_name, t->vhost_wwpn) ||
833 (tv_tpg->tport_tpgt != t->vhost_tpgt)) {
834 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
835 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
836 tv_tport->tport_name, tv_tpg->tport_tpgt,
837 t->vhost_wwpn, t->vhost_tpgt);
841 tv_tpg->tv_tpg_vhost_count--;
843 mutex_unlock(&vs->dev.mutex);
848 mutex_unlock(&vs->dev.mutex);
852 static int vhost_scsi_open(struct inode *inode, struct file *f)
854 struct vhost_scsi *s;
857 s = kzalloc(sizeof(*s), GFP_KERNEL);
861 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
862 INIT_LIST_HEAD(&s->vs_completion_list);
863 spin_lock_init(&s->vs_completion_lock);
865 s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
866 s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
867 s->vqs[VHOST_SCSI_VQ_IO].handle_kick = vhost_scsi_handle_kick;
868 r = vhost_dev_init(&s->dev, s->vqs, 3);
878 static int vhost_scsi_release(struct inode *inode, struct file *f)
880 struct vhost_scsi *s = f->private_data;
882 if (s->vs_tpg && s->vs_tpg->tport) {
883 struct vhost_scsi_target backend;
885 memcpy(backend.vhost_wwpn, s->vs_tpg->tport->tport_name,
886 sizeof(backend.vhost_wwpn));
887 backend.vhost_tpgt = s->vs_tpg->tport_tpgt;
888 vhost_scsi_clear_endpoint(s, &backend);
891 vhost_dev_stop(&s->dev);
892 vhost_dev_cleanup(&s->dev, false);
897 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
899 vhost_poll_flush(&vs->dev.vqs[index].poll);
902 static void vhost_scsi_flush(struct vhost_scsi *vs)
904 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_CTL);
905 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_EVT);
906 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_IO);
909 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
911 if (features & ~VHOST_FEATURES)
914 mutex_lock(&vs->dev.mutex);
915 if ((features & (1 << VHOST_F_LOG_ALL)) &&
916 !vhost_log_access_ok(&vs->dev)) {
917 mutex_unlock(&vs->dev.mutex);
920 vs->dev.acked_features = features;
922 vhost_scsi_flush(vs);
923 mutex_unlock(&vs->dev.mutex);
927 static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
930 struct vhost_scsi *vs = f->private_data;
931 struct vhost_scsi_target backend;
932 void __user *argp = (void __user *)arg;
933 u64 __user *featurep = argp;
935 int r, abi_version = VHOST_SCSI_ABI_VERSION;
938 case VHOST_SCSI_SET_ENDPOINT:
939 if (copy_from_user(&backend, argp, sizeof backend))
941 if (backend.reserved != 0)
944 return vhost_scsi_set_endpoint(vs, &backend);
945 case VHOST_SCSI_CLEAR_ENDPOINT:
946 if (copy_from_user(&backend, argp, sizeof backend))
948 if (backend.reserved != 0)
951 return vhost_scsi_clear_endpoint(vs, &backend);
952 case VHOST_SCSI_GET_ABI_VERSION:
953 if (copy_to_user(argp, &abi_version, sizeof abi_version))
956 case VHOST_GET_FEATURES:
957 features = VHOST_FEATURES;
958 if (copy_to_user(featurep, &features, sizeof features))
961 case VHOST_SET_FEATURES:
962 if (copy_from_user(&features, featurep, sizeof features))
964 return vhost_scsi_set_features(vs, features);
966 mutex_lock(&vs->dev.mutex);
967 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
968 /* TODO: flush backend after dev ioctl. */
969 if (r == -ENOIOCTLCMD)
970 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
971 mutex_unlock(&vs->dev.mutex);
977 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
980 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
984 static const struct file_operations vhost_scsi_fops = {
985 .owner = THIS_MODULE,
986 .release = vhost_scsi_release,
987 .unlocked_ioctl = vhost_scsi_ioctl,
989 .compat_ioctl = vhost_scsi_compat_ioctl,
991 .open = vhost_scsi_open,
992 .llseek = noop_llseek,
995 static struct miscdevice vhost_scsi_misc = {
1001 static int __init vhost_scsi_register(void)
1003 return misc_register(&vhost_scsi_misc);
1006 static int vhost_scsi_deregister(void)
1008 return misc_deregister(&vhost_scsi_misc);
1011 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1013 switch (tport->tport_proto_id) {
1014 case SCSI_PROTOCOL_SAS:
1016 case SCSI_PROTOCOL_FCP:
1018 case SCSI_PROTOCOL_ISCSI:
1027 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1030 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1031 struct tcm_vhost_tpg, se_tpg);
1033 mutex_lock(&tv_tpg->tv_tpg_mutex);
1034 tv_tpg->tv_tpg_port_count++;
1035 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1040 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1041 struct se_lun *se_lun)
1043 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1044 struct tcm_vhost_tpg, se_tpg);
1046 mutex_lock(&tv_tpg->tv_tpg_mutex);
1047 tv_tpg->tv_tpg_port_count--;
1048 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1051 static struct se_node_acl *tcm_vhost_make_nodeacl(
1052 struct se_portal_group *se_tpg,
1053 struct config_group *group,
1056 struct se_node_acl *se_nacl, *se_nacl_new;
1057 struct tcm_vhost_nacl *nacl;
1061 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1062 return ERR_PTR(-EINVAL); */
1063 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1065 return ERR_PTR(-ENOMEM);
1069 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1070 * when converting a NodeACL from demo mode -> explict
1072 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1074 if (IS_ERR(se_nacl)) {
1075 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1079 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1081 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1082 nacl->iport_wwpn = wwpn;
1087 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1089 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1090 struct tcm_vhost_nacl, se_node_acl);
1091 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1095 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1098 struct se_portal_group *se_tpg;
1099 struct tcm_vhost_nexus *tv_nexus;
1101 mutex_lock(&tv_tpg->tv_tpg_mutex);
1102 if (tv_tpg->tpg_nexus) {
1103 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1104 pr_debug("tv_tpg->tpg_nexus already exists\n");
1107 se_tpg = &tv_tpg->se_tpg;
1109 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1111 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1112 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1116 * Initialize the struct se_session pointer
1118 tv_nexus->tvn_se_sess = transport_init_session();
1119 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1120 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1125 * Since we are running in 'demo mode' this call with generate a
1126 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1127 * the SCSI Initiator port name of the passed configfs group 'name'.
1129 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1130 se_tpg, (unsigned char *)name);
1131 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1132 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1133 pr_debug("core_tpg_check_initiator_node_acl() failed"
1135 transport_free_session(tv_nexus->tvn_se_sess);
1140 * Now register the TCM vhost virtual I_T Nexus as active with the
1141 * call to __transport_register_session()
1143 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1144 tv_nexus->tvn_se_sess, tv_nexus);
1145 tv_tpg->tpg_nexus = tv_nexus;
1147 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1151 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1153 struct se_session *se_sess;
1154 struct tcm_vhost_nexus *tv_nexus;
1156 mutex_lock(&tpg->tv_tpg_mutex);
1157 tv_nexus = tpg->tpg_nexus;
1159 mutex_unlock(&tpg->tv_tpg_mutex);
1163 se_sess = tv_nexus->tvn_se_sess;
1165 mutex_unlock(&tpg->tv_tpg_mutex);
1169 if (tpg->tv_tpg_port_count != 0) {
1170 mutex_unlock(&tpg->tv_tpg_mutex);
1171 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1172 " active TPG port count: %d\n",
1173 tpg->tv_tpg_port_count);
1177 if (tpg->tv_tpg_vhost_count != 0) {
1178 mutex_unlock(&tpg->tv_tpg_mutex);
1179 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1180 " active TPG vhost count: %d\n",
1181 tpg->tv_tpg_vhost_count);
1185 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1186 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1187 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1189 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1191 transport_deregister_session(tv_nexus->tvn_se_sess);
1192 tpg->tpg_nexus = NULL;
1193 mutex_unlock(&tpg->tv_tpg_mutex);
1199 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1202 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1203 struct tcm_vhost_tpg, se_tpg);
1204 struct tcm_vhost_nexus *tv_nexus;
1207 mutex_lock(&tv_tpg->tv_tpg_mutex);
1208 tv_nexus = tv_tpg->tpg_nexus;
1210 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1213 ret = snprintf(page, PAGE_SIZE, "%s\n",
1214 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1215 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1220 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1224 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1225 struct tcm_vhost_tpg, se_tpg);
1226 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1227 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1230 * Shutdown the active I_T nexus if 'NULL' is passed..
1232 if (!strncmp(page, "NULL", 4)) {
1233 ret = tcm_vhost_drop_nexus(tv_tpg);
1234 return (!ret) ? count : ret;
1237 * Otherwise make sure the passed virtual Initiator port WWN matches
1238 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1239 * tcm_vhost_make_nexus().
1241 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1242 pr_err("Emulated NAA Sas Address: %s, exceeds"
1243 " max: %d\n", page, TCM_VHOST_NAMELEN);
1246 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1248 ptr = strstr(i_port, "naa.");
1250 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1251 pr_err("Passed SAS Initiator Port %s does not"
1252 " match target port protoid: %s\n", i_port,
1253 tcm_vhost_dump_proto_id(tport_wwn));
1256 port_ptr = &i_port[0];
1259 ptr = strstr(i_port, "fc.");
1261 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1262 pr_err("Passed FCP Initiator Port %s does not"
1263 " match target port protoid: %s\n", i_port,
1264 tcm_vhost_dump_proto_id(tport_wwn));
1267 port_ptr = &i_port[3]; /* Skip over "fc." */
1270 ptr = strstr(i_port, "iqn.");
1272 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1273 pr_err("Passed iSCSI Initiator Port %s does not"
1274 " match target port protoid: %s\n", i_port,
1275 tcm_vhost_dump_proto_id(tport_wwn));
1278 port_ptr = &i_port[0];
1281 pr_err("Unable to locate prefix for emulated Initiator Port:"
1285 * Clear any trailing newline for the NAA WWN
1288 if (i_port[strlen(i_port)-1] == '\n')
1289 i_port[strlen(i_port)-1] = '\0';
1291 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1298 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1300 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1301 &tcm_vhost_tpg_nexus.attr,
1305 static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
1306 struct config_group *group,
1309 struct tcm_vhost_tport *tport = container_of(wwn,
1310 struct tcm_vhost_tport, tport_wwn);
1312 struct tcm_vhost_tpg *tpg;
1316 if (strstr(name, "tpgt_") != name)
1317 return ERR_PTR(-EINVAL);
1318 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1319 return ERR_PTR(-EINVAL);
1321 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1323 pr_err("Unable to allocate struct tcm_vhost_tpg");
1324 return ERR_PTR(-ENOMEM);
1326 mutex_init(&tpg->tv_tpg_mutex);
1327 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1329 tpg->tport_tpgt = tpgt;
1331 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1332 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1337 mutex_lock(&tcm_vhost_mutex);
1338 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1339 mutex_unlock(&tcm_vhost_mutex);
1341 return &tpg->se_tpg;
1344 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1346 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1347 struct tcm_vhost_tpg, se_tpg);
1349 mutex_lock(&tcm_vhost_mutex);
1350 list_del(&tpg->tv_tpg_list);
1351 mutex_unlock(&tcm_vhost_mutex);
1353 * Release the virtual I_T Nexus for this vhost TPG
1355 tcm_vhost_drop_nexus(tpg);
1357 * Deregister the se_tpg from TCM..
1359 core_tpg_deregister(se_tpg);
1363 static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1364 struct config_group *group,
1367 struct tcm_vhost_tport *tport;
1372 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1373 return ERR_PTR(-EINVAL); */
1375 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1377 pr_err("Unable to allocate struct tcm_vhost_tport");
1378 return ERR_PTR(-ENOMEM);
1380 tport->tport_wwpn = wwpn;
1382 * Determine the emulated Protocol Identifier and Target Port Name
1383 * based on the incoming configfs directory name.
1385 ptr = strstr(name, "naa.");
1387 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1390 ptr = strstr(name, "fc.");
1392 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1393 off = 3; /* Skip over "fc." */
1396 ptr = strstr(name, "iqn.");
1398 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1402 pr_err("Unable to locate prefix for emulated Target Port:"
1405 return ERR_PTR(-EINVAL);
1408 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1409 pr_err("Emulated %s Address: %s, exceeds"
1410 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1413 return ERR_PTR(-EINVAL);
1415 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1417 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1418 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1420 return &tport->tport_wwn;
1423 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1425 struct tcm_vhost_tport *tport = container_of(wwn,
1426 struct tcm_vhost_tport, tport_wwn);
1428 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1429 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1435 static ssize_t tcm_vhost_wwn_show_attr_version(
1436 struct target_fabric_configfs *tf,
1439 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1440 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1441 utsname()->machine);
1444 TF_WWN_ATTR_RO(tcm_vhost, version);
1446 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1447 &tcm_vhost_wwn_version.attr,
1451 static struct target_core_fabric_ops tcm_vhost_ops = {
1452 .get_fabric_name = tcm_vhost_get_fabric_name,
1453 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1454 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1455 .tpg_get_tag = tcm_vhost_get_tag,
1456 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1457 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1458 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1459 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1460 .tpg_check_demo_mode = tcm_vhost_check_true,
1461 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1462 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1463 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1464 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1465 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1466 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1467 .release_cmd = tcm_vhost_release_cmd,
1468 .shutdown_session = tcm_vhost_shutdown_session,
1469 .close_session = tcm_vhost_close_session,
1470 .sess_get_index = tcm_vhost_sess_get_index,
1471 .sess_get_initiator_sid = NULL,
1472 .write_pending = tcm_vhost_write_pending,
1473 .write_pending_status = tcm_vhost_write_pending_status,
1474 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1475 .get_task_tag = tcm_vhost_get_task_tag,
1476 .get_cmd_state = tcm_vhost_get_cmd_state,
1477 .queue_data_in = tcm_vhost_queue_data_in,
1478 .queue_status = tcm_vhost_queue_status,
1479 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
1481 * Setup callers for generic logic in target_core_fabric_configfs.c
1483 .fabric_make_wwn = tcm_vhost_make_tport,
1484 .fabric_drop_wwn = tcm_vhost_drop_tport,
1485 .fabric_make_tpg = tcm_vhost_make_tpg,
1486 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1487 .fabric_post_link = tcm_vhost_port_link,
1488 .fabric_pre_unlink = tcm_vhost_port_unlink,
1489 .fabric_make_np = NULL,
1490 .fabric_drop_np = NULL,
1491 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1492 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1495 static int tcm_vhost_register_configfs(void)
1497 struct target_fabric_configfs *fabric;
1500 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1501 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1502 utsname()->machine);
1504 * Register the top level struct config_item_type with TCM core
1506 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1507 if (IS_ERR(fabric)) {
1508 pr_err("target_fabric_configfs_init() failed\n");
1509 return PTR_ERR(fabric);
1512 * Setup fabric->tf_ops from our local tcm_vhost_ops
1514 fabric->tf_ops = tcm_vhost_ops;
1516 * Setup default attribute lists for various fabric->tf_cit_tmpl
1518 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1519 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1520 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1521 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1522 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1523 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1524 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1525 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1526 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1528 * Register the fabric for use within TCM
1530 ret = target_fabric_configfs_register(fabric);
1532 pr_err("target_fabric_configfs_register() failed"
1533 " for TCM_VHOST\n");
1537 * Setup our local pointer to *fabric
1539 tcm_vhost_fabric_configfs = fabric;
1540 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1544 static void tcm_vhost_deregister_configfs(void)
1546 if (!tcm_vhost_fabric_configfs)
1549 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1550 tcm_vhost_fabric_configfs = NULL;
1551 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1554 static int __init tcm_vhost_init(void)
1558 * Use our own dedicated workqueue for submitting I/O into
1559 * target core to avoid contention within system_wq.
1561 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1562 if (!tcm_vhost_workqueue)
1565 ret = vhost_scsi_register();
1567 goto out_destroy_workqueue;
1569 ret = tcm_vhost_register_configfs();
1571 goto out_vhost_scsi_deregister;
1575 out_vhost_scsi_deregister:
1576 vhost_scsi_deregister();
1577 out_destroy_workqueue:
1578 destroy_workqueue(tcm_vhost_workqueue);
1583 static void tcm_vhost_exit(void)
1585 tcm_vhost_deregister_configfs();
1586 vhost_scsi_deregister();
1587 destroy_workqueue(tcm_vhost_workqueue);
1590 MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1591 MODULE_LICENSE("GPL");
1592 module_init(tcm_vhost_init);
1593 module_exit(tcm_vhost_exit);