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_inflight {
78 /* Wait for the flush operation to finish */
79 struct completion comp;
80 /* Refcount for the inflight reqs */
84 struct vhost_scsi_virtqueue {
85 struct vhost_virtqueue vq;
87 * Reference counting for inflight reqs, used for flush operation. At
88 * each time, one reference tracks new commands submitted, while we
89 * wait for another one to reach 0.
91 struct vhost_scsi_inflight inflights[2];
93 * Indicate current inflight in use, protected by vq->mutex.
94 * Writers must also take dev mutex and flush under it.
100 /* Protected by vhost_scsi->dev.mutex */
101 struct tcm_vhost_tpg **vs_tpg;
102 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
104 struct vhost_dev dev;
105 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
107 struct vhost_work vs_completion_work; /* cmd completion work item */
108 struct llist_head vs_completion_list; /* cmd completion queue */
110 struct vhost_work vs_event_work; /* evt injection work item */
111 struct llist_head vs_event_list; /* evt injection queue */
113 bool vs_events_missed; /* any missed events, protected by vq->mutex */
114 int vs_events_nr; /* num of pending events, protected by vq->mutex */
117 /* Local pointer to allocated TCM configfs fabric module */
118 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
120 static struct workqueue_struct *tcm_vhost_workqueue;
122 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
123 static DEFINE_MUTEX(tcm_vhost_mutex);
124 static LIST_HEAD(tcm_vhost_list);
126 static int iov_num_pages(struct iovec *iov)
128 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
129 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
132 void tcm_vhost_done_inflight(struct kref *kref)
134 struct vhost_scsi_inflight *inflight;
136 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
137 complete(&inflight->comp);
140 static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
141 struct vhost_scsi_inflight *old_inflight[])
143 struct vhost_scsi_inflight *new_inflight;
144 struct vhost_virtqueue *vq;
147 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
150 mutex_lock(&vq->mutex);
152 /* store old infight */
153 idx = vs->vqs[i].inflight_idx;
155 old_inflight[i] = &vs->vqs[i].inflights[idx];
157 /* setup new infight */
158 vs->vqs[i].inflight_idx = idx ^ 1;
159 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
160 kref_init(&new_inflight->kref);
161 init_completion(&new_inflight->comp);
163 mutex_unlock(&vq->mutex);
167 static struct vhost_scsi_inflight *
168 tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
170 struct vhost_scsi_inflight *inflight;
171 struct vhost_scsi_virtqueue *svq;
173 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
174 inflight = &svq->inflights[svq->inflight_idx];
175 kref_get(&inflight->kref);
180 static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
182 kref_put(&inflight->kref, tcm_vhost_done_inflight);
185 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
190 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
195 static char *tcm_vhost_get_fabric_name(void)
200 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
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_get_fabric_proto_ident(se_tpg);
209 case SCSI_PROTOCOL_FCP:
210 return fc_get_fabric_proto_ident(se_tpg);
211 case SCSI_PROTOCOL_ISCSI:
212 return iscsi_get_fabric_proto_ident(se_tpg);
214 pr_err("Unknown tport_proto_id: 0x%02x, using"
215 " SAS emulation\n", tport->tport_proto_id);
219 return sas_get_fabric_proto_ident(se_tpg);
222 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
224 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
225 struct tcm_vhost_tpg, se_tpg);
226 struct tcm_vhost_tport *tport = tpg->tport;
228 return &tport->tport_name[0];
231 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
233 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
234 struct tcm_vhost_tpg, se_tpg);
235 return tpg->tport_tpgt;
238 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
243 static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
244 struct se_node_acl *se_nacl,
245 struct t10_pr_registration *pr_reg,
249 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
250 struct tcm_vhost_tpg, se_tpg);
251 struct tcm_vhost_tport *tport = tpg->tport;
253 switch (tport->tport_proto_id) {
254 case SCSI_PROTOCOL_SAS:
255 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
257 case SCSI_PROTOCOL_FCP:
258 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
260 case SCSI_PROTOCOL_ISCSI:
261 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
264 pr_err("Unknown tport_proto_id: 0x%02x, using"
265 " SAS emulation\n", tport->tport_proto_id);
269 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
273 static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
274 struct se_node_acl *se_nacl,
275 struct t10_pr_registration *pr_reg,
278 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
279 struct tcm_vhost_tpg, se_tpg);
280 struct tcm_vhost_tport *tport = tpg->tport;
282 switch (tport->tport_proto_id) {
283 case SCSI_PROTOCOL_SAS:
284 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
286 case SCSI_PROTOCOL_FCP:
287 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
289 case SCSI_PROTOCOL_ISCSI:
290 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
293 pr_err("Unknown tport_proto_id: 0x%02x, using"
294 " SAS emulation\n", tport->tport_proto_id);
298 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
302 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
305 char **port_nexus_ptr)
307 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
308 struct tcm_vhost_tpg, se_tpg);
309 struct tcm_vhost_tport *tport = tpg->tport;
311 switch (tport->tport_proto_id) {
312 case SCSI_PROTOCOL_SAS:
313 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
315 case SCSI_PROTOCOL_FCP:
316 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
318 case SCSI_PROTOCOL_ISCSI:
319 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
322 pr_err("Unknown tport_proto_id: 0x%02x, using"
323 " SAS emulation\n", tport->tport_proto_id);
327 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
331 static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
332 struct se_portal_group *se_tpg)
334 struct tcm_vhost_nacl *nacl;
336 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
338 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
342 return &nacl->se_node_acl;
345 static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
346 struct se_node_acl *se_nacl)
348 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
349 struct tcm_vhost_nacl, se_node_acl);
353 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
358 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
363 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
368 static void tcm_vhost_close_session(struct se_session *se_sess)
373 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
378 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
380 /* Go ahead and process the write immediately */
381 target_execute_cmd(se_cmd);
385 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
390 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
395 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
400 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
405 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
407 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
409 llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
411 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
414 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
416 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
417 struct tcm_vhost_cmd, tvc_se_cmd);
418 vhost_scsi_complete_cmd(tv_cmd);
422 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
424 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
425 struct tcm_vhost_cmd, tvc_se_cmd);
426 vhost_scsi_complete_cmd(tv_cmd);
430 static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
435 static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
441 static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs,
442 u32 event, u32 reason)
444 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
445 struct tcm_vhost_evt *evt;
447 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
448 vs->vs_events_missed = true;
452 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
454 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
455 vs->vs_events_missed = true;
459 evt->event.event = event;
460 evt->event.reason = reason;
466 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
468 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
470 /* TODO locking against target/backend threads? */
471 transport_generic_free_cmd(se_cmd, 1);
473 if (tv_cmd->tvc_sgl_count) {
475 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
476 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
478 kfree(tv_cmd->tvc_sgl);
481 tcm_vhost_put_inflight(tv_cmd->inflight);
486 static void tcm_vhost_do_evt_work(struct vhost_scsi *vs,
487 struct tcm_vhost_evt *evt)
489 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
490 struct virtio_scsi_event *event = &evt->event;
491 struct virtio_scsi_event __user *eventp;
495 if (!vq->private_data) {
496 vs->vs_events_missed = true;
501 vhost_disable_notify(&vs->dev, vq);
502 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
503 ARRAY_SIZE(vq->iov), &out, &in,
506 vs->vs_events_missed = true;
509 if (head == vq->num) {
510 if (vhost_enable_notify(&vs->dev, vq))
512 vs->vs_events_missed = true;
516 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
517 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
518 vq->iov[out].iov_len);
519 vs->vs_events_missed = true;
523 if (vs->vs_events_missed) {
524 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
525 vs->vs_events_missed = false;
528 eventp = vq->iov[out].iov_base;
529 ret = __copy_to_user(eventp, event, sizeof(*event));
531 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
533 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
536 static void tcm_vhost_evt_work(struct vhost_work *work)
538 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
540 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
541 struct tcm_vhost_evt *evt;
542 struct llist_node *llnode;
544 mutex_lock(&vq->mutex);
545 llnode = llist_del_all(&vs->vs_event_list);
547 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
548 llnode = llist_next(llnode);
549 tcm_vhost_do_evt_work(vs, evt);
550 tcm_vhost_free_evt(vs, evt);
552 mutex_unlock(&vq->mutex);
555 /* Fill in status and signal that we are done processing this command
557 * This is scheduled in the vhost work queue so we are called with the owner
558 * process mm and can access the vring.
560 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
562 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
564 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
565 struct virtio_scsi_cmd_resp v_rsp;
566 struct tcm_vhost_cmd *tv_cmd;
567 struct llist_node *llnode;
568 struct se_cmd *se_cmd;
571 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
572 llnode = llist_del_all(&vs->vs_completion_list);
574 tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
575 tvc_completion_list);
576 llnode = llist_next(llnode);
577 se_cmd = &tv_cmd->tvc_se_cmd;
579 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
580 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
582 memset(&v_rsp, 0, sizeof(v_rsp));
583 v_rsp.resid = se_cmd->residual_count;
584 /* TODO is status_qualifier field needed? */
585 v_rsp.status = se_cmd->scsi_status;
586 v_rsp.sense_len = se_cmd->scsi_sense_length;
587 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
589 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
590 if (likely(ret == 0)) {
591 struct vhost_scsi_virtqueue *q;
592 vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0);
593 q = container_of(tv_cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
595 __set_bit(vq, signal);
597 pr_err("Faulted on virtio_scsi_cmd_resp\n");
599 vhost_scsi_free_cmd(tv_cmd);
603 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
605 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
608 static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
609 struct vhost_virtqueue *vq,
610 struct tcm_vhost_tpg *tv_tpg,
611 struct virtio_scsi_cmd_req *v_req,
615 struct tcm_vhost_cmd *tv_cmd;
616 struct tcm_vhost_nexus *tv_nexus;
618 tv_nexus = tv_tpg->tpg_nexus;
620 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
621 return ERR_PTR(-EIO);
624 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
626 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
627 return ERR_PTR(-ENOMEM);
629 tv_cmd->tvc_tag = v_req->tag;
630 tv_cmd->tvc_task_attr = v_req->task_attr;
631 tv_cmd->tvc_exp_data_len = exp_data_len;
632 tv_cmd->tvc_data_direction = data_direction;
633 tv_cmd->tvc_nexus = tv_nexus;
634 tv_cmd->inflight = tcm_vhost_get_inflight(vq);
640 * Map a user memory range into a scatterlist
642 * Returns the number of scatterlist entries used or -errno on error.
644 static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
645 unsigned int sgl_count, struct iovec *iov, int write)
647 unsigned int npages = 0, pages_nr, offset, nbytes;
648 struct scatterlist *sg = sgl;
649 void __user *ptr = iov->iov_base;
650 size_t len = iov->iov_len;
654 pages_nr = iov_num_pages(iov);
655 if (pages_nr > sgl_count)
658 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
662 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
663 /* No pages were pinned */
666 /* Less pages pinned than wanted */
667 if (ret != pages_nr) {
668 for (i = 0; i < ret; i++)
675 offset = (uintptr_t)ptr & ~PAGE_MASK;
676 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
677 sg_set_page(sg, pages[npages], nbytes, offset);
689 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
690 struct iovec *iov, unsigned int niov, int write)
695 struct scatterlist *sg;
698 * Find out how long sglist needs to be
701 for (i = 0; i < niov; i++)
702 sgl_count += iov_num_pages(&iov[i]);
704 /* TODO overflow checking */
706 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
709 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
711 sg_init_table(sg, sgl_count);
713 tv_cmd->tvc_sgl = sg;
714 tv_cmd->tvc_sgl_count = sgl_count;
716 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
717 for (i = 0; i < niov; i++) {
718 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
720 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
721 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
722 kfree(tv_cmd->tvc_sgl);
723 tv_cmd->tvc_sgl = NULL;
724 tv_cmd->tvc_sgl_count = 0;
734 static void tcm_vhost_submission_work(struct work_struct *work)
736 struct tcm_vhost_cmd *tv_cmd =
737 container_of(work, struct tcm_vhost_cmd, work);
738 struct tcm_vhost_nexus *tv_nexus;
739 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
740 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
741 int rc, sg_no_bidi = 0;
743 if (tv_cmd->tvc_sgl_count) {
744 sg_ptr = tv_cmd->tvc_sgl;
745 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
747 if (se_cmd->se_cmd_flags & SCF_BIDI) {
755 tv_nexus = tv_cmd->tvc_nexus;
757 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
758 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
759 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
760 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
761 0, sg_ptr, tv_cmd->tvc_sgl_count,
762 sg_bidi_ptr, sg_no_bidi);
764 transport_send_check_condition_and_sense(se_cmd,
765 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
766 transport_generic_free_cmd(se_cmd, 0);
770 static void vhost_scsi_send_bad_target(struct vhost_scsi *vs,
771 struct vhost_virtqueue *vq, int head, unsigned out)
773 struct virtio_scsi_cmd_resp __user *resp;
774 struct virtio_scsi_cmd_resp rsp;
777 memset(&rsp, 0, sizeof(rsp));
778 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
779 resp = vq->iov[out].iov_base;
780 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
782 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
784 pr_err("Faulted on virtio_scsi_cmd_resp\n");
787 static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
788 struct vhost_virtqueue *vq)
790 struct tcm_vhost_tpg **vs_tpg;
791 struct virtio_scsi_cmd_req v_req;
792 struct tcm_vhost_tpg *tv_tpg;
793 struct tcm_vhost_cmd *tv_cmd;
794 u32 exp_data_len, data_first, data_num, data_direction;
800 * We can handle the vq only after the endpoint is setup by calling the
801 * VHOST_SCSI_SET_ENDPOINT ioctl.
803 * TODO: Check that we are running from vhost_worker which acts
804 * as read-side critical section for vhost kind of RCU.
805 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
807 vs_tpg = rcu_dereference_check(vq->private_data, 1);
811 mutex_lock(&vq->mutex);
812 vhost_disable_notify(&vs->dev, vq);
815 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
816 ARRAY_SIZE(vq->iov), &out, &in,
818 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
820 /* On error, stop handling until the next kick. */
821 if (unlikely(head < 0))
823 /* Nothing new? Wait for eventfd to tell us they refilled. */
824 if (head == vq->num) {
825 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
826 vhost_disable_notify(&vs->dev, vq);
832 /* FIXME: BIDI operation */
833 if (out == 1 && in == 1) {
834 data_direction = DMA_NONE;
837 } else if (out == 1 && in > 1) {
838 data_direction = DMA_FROM_DEVICE;
839 data_first = out + 1;
841 } else if (out > 1 && in == 1) {
842 data_direction = DMA_TO_DEVICE;
846 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
852 * Check for a sane resp buffer so we can report errors to
855 if (unlikely(vq->iov[out].iov_len !=
856 sizeof(struct virtio_scsi_cmd_resp))) {
857 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
858 " bytes\n", vq->iov[out].iov_len);
862 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
863 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
864 " bytes\n", vq->iov[0].iov_len);
867 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
868 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
869 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
872 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
876 /* Extract the tpgt */
877 target = v_req.lun[1];
878 tv_tpg = ACCESS_ONCE(vs_tpg[target]);
880 /* Target does not exist, fail the request */
881 if (unlikely(!tv_tpg)) {
882 vhost_scsi_send_bad_target(vs, vq, head, out);
887 for (i = 0; i < data_num; i++)
888 exp_data_len += vq->iov[data_first + i].iov_len;
890 tv_cmd = vhost_scsi_allocate_cmd(vq, tv_tpg, &v_req,
891 exp_data_len, data_direction);
892 if (IS_ERR(tv_cmd)) {
893 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
897 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
898 ": %d\n", tv_cmd, exp_data_len, data_direction);
900 tv_cmd->tvc_vhost = vs;
902 tv_cmd->tvc_resp = vq->iov[out].iov_base;
905 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
906 * that will be used by tcm_vhost_new_cmd_map() and down into
907 * target_setup_cmd_from_cdb()
909 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
911 * Check that the recieved CDB size does not exceeded our
912 * hardcoded max for tcm_vhost
914 /* TODO what if cdb was too small for varlen cdb header? */
915 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
916 TCM_VHOST_MAX_CDB_SIZE)) {
917 vq_err(vq, "Received SCSI CDB with command_size: %d that"
918 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
919 scsi_command_size(tv_cmd->tvc_cdb),
920 TCM_VHOST_MAX_CDB_SIZE);
923 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
925 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
926 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
928 if (data_direction != DMA_NONE) {
929 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
930 &vq->iov[data_first], data_num,
931 data_direction == DMA_TO_DEVICE);
933 vq_err(vq, "Failed to map iov to sgl\n");
939 * Save the descriptor from vhost_get_vq_desc() to be used to
940 * complete the virtio-scsi request in TCM callback context via
941 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
943 tv_cmd->tvc_vq_desc = head;
945 * Dispatch tv_cmd descriptor for cmwq execution in process
946 * context provided by tcm_vhost_workqueue. This also ensures
947 * tv_cmd is executed on the same kworker CPU as this vhost
948 * thread to gain positive L2 cache locality effects..
950 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
951 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
954 mutex_unlock(&vq->mutex);
958 vhost_scsi_free_cmd(tv_cmd);
960 vhost_scsi_send_bad_target(vs, vq, head, out);
961 mutex_unlock(&vq->mutex);
964 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
966 pr_debug("%s: The handling func for control queue.\n", __func__);
969 static void tcm_vhost_send_evt(struct vhost_scsi *vs, struct tcm_vhost_tpg *tpg,
970 struct se_lun *lun, u32 event, u32 reason)
972 struct tcm_vhost_evt *evt;
974 evt = tcm_vhost_allocate_evt(vs, event, reason);
979 /* TODO: share lun setup code with virtio-scsi.ko */
981 * Note: evt->event is zeroed when we allocate it and
982 * lun[4-7] need to be zero according to virtio-scsi spec.
984 evt->event.lun[0] = 0x01;
985 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
986 if (lun->unpacked_lun >= 256)
987 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
988 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
991 llist_add(&evt->list, &vs->vs_event_list);
992 vhost_work_queue(&vs->dev, &vs->vs_event_work);
995 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
997 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
999 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1001 mutex_lock(&vq->mutex);
1002 if (!vq->private_data)
1005 if (vs->vs_events_missed)
1006 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1008 mutex_unlock(&vq->mutex);
1011 static void vhost_scsi_handle_kick(struct vhost_work *work)
1013 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1015 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1017 vhost_scsi_handle_vq(vs, vq);
1020 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1022 vhost_poll_flush(&vs->vqs[index].vq.poll);
1025 /* Callers must hold dev mutex */
1026 static void vhost_scsi_flush(struct vhost_scsi *vs)
1028 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
1031 /* Init new inflight and remember the old inflight */
1032 tcm_vhost_init_inflight(vs, old_inflight);
1035 * The inflight->kref was initialized to 1. We decrement it here to
1036 * indicate the start of the flush operation so that it will reach 0
1037 * when all the reqs are finished.
1039 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1040 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1042 /* Flush both the vhost poll and vhost work */
1043 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1044 vhost_scsi_flush_vq(vs, i);
1045 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
1046 vhost_work_flush(&vs->dev, &vs->vs_event_work);
1048 /* Wait for all reqs issued before the flush to be finished */
1049 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1050 wait_for_completion(&old_inflight[i]->comp);
1054 * Called from vhost_scsi_ioctl() context to walk the list of available
1055 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
1057 * The lock nesting rule is:
1058 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1060 static int vhost_scsi_set_endpoint(
1061 struct vhost_scsi *vs,
1062 struct vhost_scsi_target *t)
1064 struct tcm_vhost_tport *tv_tport;
1065 struct tcm_vhost_tpg *tv_tpg;
1066 struct tcm_vhost_tpg **vs_tpg;
1067 struct vhost_virtqueue *vq;
1068 int index, ret, i, len;
1071 mutex_lock(&tcm_vhost_mutex);
1072 mutex_lock(&vs->dev.mutex);
1074 /* Verify that ring has been setup correctly. */
1075 for (index = 0; index < vs->dev.nvqs; ++index) {
1076 /* Verify that ring has been setup correctly. */
1077 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1083 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1084 vs_tpg = kzalloc(len, GFP_KERNEL);
1090 memcpy(vs_tpg, vs->vs_tpg, len);
1092 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
1093 mutex_lock(&tv_tpg->tv_tpg_mutex);
1094 if (!tv_tpg->tpg_nexus) {
1095 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1098 if (tv_tpg->tv_tpg_vhost_count != 0) {
1099 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1102 tv_tport = tv_tpg->tport;
1104 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1105 if (vs->vs_tpg && vs->vs_tpg[tv_tpg->tport_tpgt]) {
1107 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1111 tv_tpg->tv_tpg_vhost_count++;
1112 tv_tpg->vhost_scsi = vs;
1113 vs_tpg[tv_tpg->tport_tpgt] = tv_tpg;
1114 smp_mb__after_atomic_inc();
1117 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1121 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1122 sizeof(vs->vs_vhost_wwpn));
1123 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1124 vq = &vs->vqs[i].vq;
1125 /* Flushing the vhost_work acts as synchronize_rcu */
1126 mutex_lock(&vq->mutex);
1127 rcu_assign_pointer(vq->private_data, vs_tpg);
1128 vhost_init_used(vq);
1129 mutex_unlock(&vq->mutex);
1137 * Act as synchronize_rcu to make sure access to
1138 * old vs->vs_tpg is finished.
1140 vhost_scsi_flush(vs);
1142 vs->vs_tpg = vs_tpg;
1145 mutex_unlock(&vs->dev.mutex);
1146 mutex_unlock(&tcm_vhost_mutex);
1150 static int vhost_scsi_clear_endpoint(
1151 struct vhost_scsi *vs,
1152 struct vhost_scsi_target *t)
1154 struct tcm_vhost_tport *tv_tport;
1155 struct tcm_vhost_tpg *tv_tpg;
1156 struct vhost_virtqueue *vq;
1161 mutex_lock(&tcm_vhost_mutex);
1162 mutex_lock(&vs->dev.mutex);
1163 /* Verify that ring has been setup correctly. */
1164 for (index = 0; index < vs->dev.nvqs; ++index) {
1165 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1176 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1178 tv_tpg = vs->vs_tpg[target];
1182 mutex_lock(&tv_tpg->tv_tpg_mutex);
1183 tv_tport = tv_tpg->tport;
1189 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1190 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
1191 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1192 tv_tport->tport_name, tv_tpg->tport_tpgt,
1193 t->vhost_wwpn, t->vhost_tpgt);
1197 tv_tpg->tv_tpg_vhost_count--;
1198 tv_tpg->vhost_scsi = NULL;
1199 vs->vs_tpg[target] = NULL;
1201 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1204 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1205 vq = &vs->vqs[i].vq;
1206 /* Flushing the vhost_work acts as synchronize_rcu */
1207 mutex_lock(&vq->mutex);
1208 rcu_assign_pointer(vq->private_data, NULL);
1209 mutex_unlock(&vq->mutex);
1213 * Act as synchronize_rcu to make sure access to
1214 * old vs->vs_tpg is finished.
1216 vhost_scsi_flush(vs);
1219 WARN_ON(vs->vs_events_nr);
1220 mutex_unlock(&vs->dev.mutex);
1221 mutex_unlock(&tcm_vhost_mutex);
1225 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1227 mutex_unlock(&vs->dev.mutex);
1228 mutex_unlock(&tcm_vhost_mutex);
1232 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1234 if (features & ~VHOST_SCSI_FEATURES)
1237 mutex_lock(&vs->dev.mutex);
1238 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1239 !vhost_log_access_ok(&vs->dev)) {
1240 mutex_unlock(&vs->dev.mutex);
1243 vs->dev.acked_features = features;
1245 vhost_scsi_flush(vs);
1246 mutex_unlock(&vs->dev.mutex);
1250 static int vhost_scsi_open(struct inode *inode, struct file *f)
1252 struct vhost_scsi *s;
1253 struct vhost_virtqueue **vqs;
1256 s = kzalloc(sizeof(*s), GFP_KERNEL);
1260 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1266 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
1267 vhost_work_init(&s->vs_event_work, tcm_vhost_evt_work);
1269 s->vs_events_nr = 0;
1270 s->vs_events_missed = false;
1272 vqs[VHOST_SCSI_VQ_CTL] = &s->vqs[VHOST_SCSI_VQ_CTL].vq;
1273 vqs[VHOST_SCSI_VQ_EVT] = &s->vqs[VHOST_SCSI_VQ_EVT].vq;
1274 s->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1275 s->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
1276 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
1277 vqs[i] = &s->vqs[i].vq;
1278 s->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
1280 r = vhost_dev_init(&s->dev, vqs, VHOST_SCSI_MAX_VQ);
1282 tcm_vhost_init_inflight(s, NULL);
1290 f->private_data = s;
1294 static int vhost_scsi_release(struct inode *inode, struct file *f)
1296 struct vhost_scsi *s = f->private_data;
1297 struct vhost_scsi_target t;
1299 mutex_lock(&s->dev.mutex);
1300 memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1301 mutex_unlock(&s->dev.mutex);
1302 vhost_scsi_clear_endpoint(s, &t);
1303 vhost_dev_stop(&s->dev);
1304 vhost_dev_cleanup(&s->dev, false);
1305 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1306 vhost_scsi_flush(s);
1312 static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
1315 struct vhost_scsi *vs = f->private_data;
1316 struct vhost_scsi_target backend;
1317 void __user *argp = (void __user *)arg;
1318 u64 __user *featurep = argp;
1319 u32 __user *eventsp = argp;
1322 int r, abi_version = VHOST_SCSI_ABI_VERSION;
1323 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1326 case VHOST_SCSI_SET_ENDPOINT:
1327 if (copy_from_user(&backend, argp, sizeof backend))
1329 if (backend.reserved != 0)
1332 return vhost_scsi_set_endpoint(vs, &backend);
1333 case VHOST_SCSI_CLEAR_ENDPOINT:
1334 if (copy_from_user(&backend, argp, sizeof backend))
1336 if (backend.reserved != 0)
1339 return vhost_scsi_clear_endpoint(vs, &backend);
1340 case VHOST_SCSI_GET_ABI_VERSION:
1341 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1344 case VHOST_SCSI_SET_EVENTS_MISSED:
1345 if (get_user(events_missed, eventsp))
1347 mutex_lock(&vq->mutex);
1348 vs->vs_events_missed = events_missed;
1349 mutex_unlock(&vq->mutex);
1351 case VHOST_SCSI_GET_EVENTS_MISSED:
1352 mutex_lock(&vq->mutex);
1353 events_missed = vs->vs_events_missed;
1354 mutex_unlock(&vq->mutex);
1355 if (put_user(events_missed, eventsp))
1358 case VHOST_GET_FEATURES:
1359 features = VHOST_SCSI_FEATURES;
1360 if (copy_to_user(featurep, &features, sizeof features))
1363 case VHOST_SET_FEATURES:
1364 if (copy_from_user(&features, featurep, sizeof features))
1366 return vhost_scsi_set_features(vs, features);
1368 mutex_lock(&vs->dev.mutex);
1369 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1370 /* TODO: flush backend after dev ioctl. */
1371 if (r == -ENOIOCTLCMD)
1372 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1373 mutex_unlock(&vs->dev.mutex);
1378 #ifdef CONFIG_COMPAT
1379 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1382 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1386 static const struct file_operations vhost_scsi_fops = {
1387 .owner = THIS_MODULE,
1388 .release = vhost_scsi_release,
1389 .unlocked_ioctl = vhost_scsi_ioctl,
1390 #ifdef CONFIG_COMPAT
1391 .compat_ioctl = vhost_scsi_compat_ioctl,
1393 .open = vhost_scsi_open,
1394 .llseek = noop_llseek,
1397 static struct miscdevice vhost_scsi_misc = {
1403 static int __init vhost_scsi_register(void)
1405 return misc_register(&vhost_scsi_misc);
1408 static int vhost_scsi_deregister(void)
1410 return misc_deregister(&vhost_scsi_misc);
1413 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1415 switch (tport->tport_proto_id) {
1416 case SCSI_PROTOCOL_SAS:
1418 case SCSI_PROTOCOL_FCP:
1420 case SCSI_PROTOCOL_ISCSI:
1429 static void tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1430 struct se_lun *lun, bool plug)
1433 struct vhost_scsi *vs = tpg->vhost_scsi;
1434 struct vhost_virtqueue *vq;
1440 mutex_lock(&vs->dev.mutex);
1441 if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1442 mutex_unlock(&vs->dev.mutex);
1447 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1449 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1451 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1452 mutex_lock(&vq->mutex);
1453 tcm_vhost_send_evt(vs, tpg, lun,
1454 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1455 mutex_unlock(&vq->mutex);
1456 mutex_unlock(&vs->dev.mutex);
1459 static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1461 tcm_vhost_do_plug(tpg, lun, true);
1464 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1466 tcm_vhost_do_plug(tpg, lun, false);
1469 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1472 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1473 struct tcm_vhost_tpg, se_tpg);
1475 mutex_lock(&tcm_vhost_mutex);
1477 mutex_lock(&tv_tpg->tv_tpg_mutex);
1478 tv_tpg->tv_tpg_port_count++;
1479 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1481 tcm_vhost_hotplug(tv_tpg, lun);
1483 mutex_unlock(&tcm_vhost_mutex);
1488 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1491 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1492 struct tcm_vhost_tpg, se_tpg);
1494 mutex_lock(&tcm_vhost_mutex);
1496 mutex_lock(&tv_tpg->tv_tpg_mutex);
1497 tv_tpg->tv_tpg_port_count--;
1498 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1500 tcm_vhost_hotunplug(tv_tpg, lun);
1502 mutex_unlock(&tcm_vhost_mutex);
1505 static struct se_node_acl *tcm_vhost_make_nodeacl(
1506 struct se_portal_group *se_tpg,
1507 struct config_group *group,
1510 struct se_node_acl *se_nacl, *se_nacl_new;
1511 struct tcm_vhost_nacl *nacl;
1515 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1516 return ERR_PTR(-EINVAL); */
1517 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1519 return ERR_PTR(-ENOMEM);
1523 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1524 * when converting a NodeACL from demo mode -> explict
1526 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1528 if (IS_ERR(se_nacl)) {
1529 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1533 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1535 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1536 nacl->iport_wwpn = wwpn;
1541 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1543 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1544 struct tcm_vhost_nacl, se_node_acl);
1545 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1549 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1552 struct se_portal_group *se_tpg;
1553 struct tcm_vhost_nexus *tv_nexus;
1555 mutex_lock(&tv_tpg->tv_tpg_mutex);
1556 if (tv_tpg->tpg_nexus) {
1557 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1558 pr_debug("tv_tpg->tpg_nexus already exists\n");
1561 se_tpg = &tv_tpg->se_tpg;
1563 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1565 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1566 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1570 * Initialize the struct se_session pointer
1572 tv_nexus->tvn_se_sess = transport_init_session();
1573 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1574 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1579 * Since we are running in 'demo mode' this call with generate a
1580 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1581 * the SCSI Initiator port name of the passed configfs group 'name'.
1583 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1584 se_tpg, (unsigned char *)name);
1585 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1586 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1587 pr_debug("core_tpg_check_initiator_node_acl() failed"
1589 transport_free_session(tv_nexus->tvn_se_sess);
1594 * Now register the TCM vhost virtual I_T Nexus as active with the
1595 * call to __transport_register_session()
1597 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1598 tv_nexus->tvn_se_sess, tv_nexus);
1599 tv_tpg->tpg_nexus = tv_nexus;
1601 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1605 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1607 struct se_session *se_sess;
1608 struct tcm_vhost_nexus *tv_nexus;
1610 mutex_lock(&tpg->tv_tpg_mutex);
1611 tv_nexus = tpg->tpg_nexus;
1613 mutex_unlock(&tpg->tv_tpg_mutex);
1617 se_sess = tv_nexus->tvn_se_sess;
1619 mutex_unlock(&tpg->tv_tpg_mutex);
1623 if (tpg->tv_tpg_port_count != 0) {
1624 mutex_unlock(&tpg->tv_tpg_mutex);
1625 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1626 " active TPG port count: %d\n",
1627 tpg->tv_tpg_port_count);
1631 if (tpg->tv_tpg_vhost_count != 0) {
1632 mutex_unlock(&tpg->tv_tpg_mutex);
1633 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1634 " active TPG vhost count: %d\n",
1635 tpg->tv_tpg_vhost_count);
1639 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1640 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1641 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1643 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1645 transport_deregister_session(tv_nexus->tvn_se_sess);
1646 tpg->tpg_nexus = NULL;
1647 mutex_unlock(&tpg->tv_tpg_mutex);
1653 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1656 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1657 struct tcm_vhost_tpg, se_tpg);
1658 struct tcm_vhost_nexus *tv_nexus;
1661 mutex_lock(&tv_tpg->tv_tpg_mutex);
1662 tv_nexus = tv_tpg->tpg_nexus;
1664 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1667 ret = snprintf(page, PAGE_SIZE, "%s\n",
1668 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1669 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1674 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1678 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1679 struct tcm_vhost_tpg, se_tpg);
1680 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1681 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1684 * Shutdown the active I_T nexus if 'NULL' is passed..
1686 if (!strncmp(page, "NULL", 4)) {
1687 ret = tcm_vhost_drop_nexus(tv_tpg);
1688 return (!ret) ? count : ret;
1691 * Otherwise make sure the passed virtual Initiator port WWN matches
1692 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1693 * tcm_vhost_make_nexus().
1695 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1696 pr_err("Emulated NAA Sas Address: %s, exceeds"
1697 " max: %d\n", page, TCM_VHOST_NAMELEN);
1700 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1702 ptr = strstr(i_port, "naa.");
1704 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1705 pr_err("Passed SAS Initiator Port %s does not"
1706 " match target port protoid: %s\n", i_port,
1707 tcm_vhost_dump_proto_id(tport_wwn));
1710 port_ptr = &i_port[0];
1713 ptr = strstr(i_port, "fc.");
1715 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1716 pr_err("Passed FCP Initiator Port %s does not"
1717 " match target port protoid: %s\n", i_port,
1718 tcm_vhost_dump_proto_id(tport_wwn));
1721 port_ptr = &i_port[3]; /* Skip over "fc." */
1724 ptr = strstr(i_port, "iqn.");
1726 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1727 pr_err("Passed iSCSI Initiator Port %s does not"
1728 " match target port protoid: %s\n", i_port,
1729 tcm_vhost_dump_proto_id(tport_wwn));
1732 port_ptr = &i_port[0];
1735 pr_err("Unable to locate prefix for emulated Initiator Port:"
1739 * Clear any trailing newline for the NAA WWN
1742 if (i_port[strlen(i_port)-1] == '\n')
1743 i_port[strlen(i_port)-1] = '\0';
1745 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1752 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1754 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1755 &tcm_vhost_tpg_nexus.attr,
1759 static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
1760 struct config_group *group,
1763 struct tcm_vhost_tport *tport = container_of(wwn,
1764 struct tcm_vhost_tport, tport_wwn);
1766 struct tcm_vhost_tpg *tpg;
1770 if (strstr(name, "tpgt_") != name)
1771 return ERR_PTR(-EINVAL);
1772 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1773 return ERR_PTR(-EINVAL);
1775 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1777 pr_err("Unable to allocate struct tcm_vhost_tpg");
1778 return ERR_PTR(-ENOMEM);
1780 mutex_init(&tpg->tv_tpg_mutex);
1781 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1783 tpg->tport_tpgt = tpgt;
1785 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1786 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1791 mutex_lock(&tcm_vhost_mutex);
1792 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1793 mutex_unlock(&tcm_vhost_mutex);
1795 return &tpg->se_tpg;
1798 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1800 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1801 struct tcm_vhost_tpg, se_tpg);
1803 mutex_lock(&tcm_vhost_mutex);
1804 list_del(&tpg->tv_tpg_list);
1805 mutex_unlock(&tcm_vhost_mutex);
1807 * Release the virtual I_T Nexus for this vhost TPG
1809 tcm_vhost_drop_nexus(tpg);
1811 * Deregister the se_tpg from TCM..
1813 core_tpg_deregister(se_tpg);
1817 static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1818 struct config_group *group,
1821 struct tcm_vhost_tport *tport;
1826 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1827 return ERR_PTR(-EINVAL); */
1829 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1831 pr_err("Unable to allocate struct tcm_vhost_tport");
1832 return ERR_PTR(-ENOMEM);
1834 tport->tport_wwpn = wwpn;
1836 * Determine the emulated Protocol Identifier and Target Port Name
1837 * based on the incoming configfs directory name.
1839 ptr = strstr(name, "naa.");
1841 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1844 ptr = strstr(name, "fc.");
1846 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1847 off = 3; /* Skip over "fc." */
1850 ptr = strstr(name, "iqn.");
1852 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1856 pr_err("Unable to locate prefix for emulated Target Port:"
1859 return ERR_PTR(-EINVAL);
1862 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1863 pr_err("Emulated %s Address: %s, exceeds"
1864 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1867 return ERR_PTR(-EINVAL);
1869 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1871 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1872 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1874 return &tport->tport_wwn;
1877 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1879 struct tcm_vhost_tport *tport = container_of(wwn,
1880 struct tcm_vhost_tport, tport_wwn);
1882 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1883 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1889 static ssize_t tcm_vhost_wwn_show_attr_version(
1890 struct target_fabric_configfs *tf,
1893 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1894 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1895 utsname()->machine);
1898 TF_WWN_ATTR_RO(tcm_vhost, version);
1900 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1901 &tcm_vhost_wwn_version.attr,
1905 static struct target_core_fabric_ops tcm_vhost_ops = {
1906 .get_fabric_name = tcm_vhost_get_fabric_name,
1907 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1908 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1909 .tpg_get_tag = tcm_vhost_get_tag,
1910 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1911 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1912 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1913 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1914 .tpg_check_demo_mode = tcm_vhost_check_true,
1915 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1916 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1917 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1918 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1919 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1920 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1921 .release_cmd = tcm_vhost_release_cmd,
1922 .shutdown_session = tcm_vhost_shutdown_session,
1923 .close_session = tcm_vhost_close_session,
1924 .sess_get_index = tcm_vhost_sess_get_index,
1925 .sess_get_initiator_sid = NULL,
1926 .write_pending = tcm_vhost_write_pending,
1927 .write_pending_status = tcm_vhost_write_pending_status,
1928 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1929 .get_task_tag = tcm_vhost_get_task_tag,
1930 .get_cmd_state = tcm_vhost_get_cmd_state,
1931 .queue_data_in = tcm_vhost_queue_data_in,
1932 .queue_status = tcm_vhost_queue_status,
1933 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
1935 * Setup callers for generic logic in target_core_fabric_configfs.c
1937 .fabric_make_wwn = tcm_vhost_make_tport,
1938 .fabric_drop_wwn = tcm_vhost_drop_tport,
1939 .fabric_make_tpg = tcm_vhost_make_tpg,
1940 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1941 .fabric_post_link = tcm_vhost_port_link,
1942 .fabric_pre_unlink = tcm_vhost_port_unlink,
1943 .fabric_make_np = NULL,
1944 .fabric_drop_np = NULL,
1945 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1946 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1949 static int tcm_vhost_register_configfs(void)
1951 struct target_fabric_configfs *fabric;
1954 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1955 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1956 utsname()->machine);
1958 * Register the top level struct config_item_type with TCM core
1960 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1961 if (IS_ERR(fabric)) {
1962 pr_err("target_fabric_configfs_init() failed\n");
1963 return PTR_ERR(fabric);
1966 * Setup fabric->tf_ops from our local tcm_vhost_ops
1968 fabric->tf_ops = tcm_vhost_ops;
1970 * Setup default attribute lists for various fabric->tf_cit_tmpl
1972 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1973 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1974 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1975 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1976 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1977 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1978 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1979 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1980 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1982 * Register the fabric for use within TCM
1984 ret = target_fabric_configfs_register(fabric);
1986 pr_err("target_fabric_configfs_register() failed"
1987 " for TCM_VHOST\n");
1991 * Setup our local pointer to *fabric
1993 tcm_vhost_fabric_configfs = fabric;
1994 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1998 static void tcm_vhost_deregister_configfs(void)
2000 if (!tcm_vhost_fabric_configfs)
2003 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2004 tcm_vhost_fabric_configfs = NULL;
2005 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2008 static int __init tcm_vhost_init(void)
2012 * Use our own dedicated workqueue for submitting I/O into
2013 * target core to avoid contention within system_wq.
2015 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2016 if (!tcm_vhost_workqueue)
2019 ret = vhost_scsi_register();
2021 goto out_destroy_workqueue;
2023 ret = tcm_vhost_register_configfs();
2025 goto out_vhost_scsi_deregister;
2029 out_vhost_scsi_deregister:
2030 vhost_scsi_deregister();
2031 out_destroy_workqueue:
2032 destroy_workqueue(tcm_vhost_workqueue);
2037 static void tcm_vhost_exit(void)
2039 tcm_vhost_deregister_configfs();
2040 vhost_scsi_deregister();
2041 destroy_workqueue(tcm_vhost_workqueue);
2044 MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
2045 MODULE_LICENSE("GPL");
2046 module_init(tcm_vhost_init);
2047 module_exit(tcm_vhost_exit);