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_scsi.h>
49 #include <linux/llist.h>
50 #include <linux/bitmap.h>
51 #include <linux/percpu_ida.h>
55 #define TCM_VHOST_VERSION "v0.1"
56 #define TCM_VHOST_NAMELEN 256
57 #define TCM_VHOST_MAX_CDB_SIZE 32
58 #define TCM_VHOST_DEFAULT_TAGS 256
60 struct vhost_scsi_inflight {
61 /* Wait for the flush operation to finish */
62 struct completion comp;
63 /* Refcount for the inflight reqs */
67 struct tcm_vhost_cmd {
68 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
70 /* virtio-scsi initiator task attribute */
72 /* virtio-scsi initiator data direction */
73 enum dma_data_direction tvc_data_direction;
74 /* Expected data transfer length from virtio-scsi header */
76 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
78 /* The number of scatterlists associated with this cmd */
80 /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */
82 /* Pointer to the SGL formatted memory from virtio-scsi */
83 struct scatterlist *tvc_sgl;
84 /* Pointer to response */
85 struct virtio_scsi_cmd_resp __user *tvc_resp;
86 /* Pointer to vhost_scsi for our device */
87 struct vhost_scsi *tvc_vhost;
88 /* Pointer to vhost_virtqueue for the cmd */
89 struct vhost_virtqueue *tvc_vq;
90 /* Pointer to vhost nexus memory */
91 struct tcm_vhost_nexus *tvc_nexus;
92 /* The TCM I/O descriptor that is accessed via container_of() */
93 struct se_cmd tvc_se_cmd;
94 /* work item used for cmwq dispatch to tcm_vhost_submission_work() */
95 struct work_struct work;
96 /* Copy of the incoming SCSI command descriptor block (CDB) */
97 unsigned char tvc_cdb[TCM_VHOST_MAX_CDB_SIZE];
98 /* Sense buffer that will be mapped into outgoing status */
99 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
100 /* Completed commands list, serviced from vhost worker thread */
101 struct llist_node tvc_completion_list;
102 /* Used to track inflight cmd */
103 struct vhost_scsi_inflight *inflight;
106 struct tcm_vhost_nexus {
107 /* Pointer to TCM session for I_T Nexus */
108 struct se_session *tvn_se_sess;
111 struct tcm_vhost_nacl {
112 /* Binary World Wide unique Port Name for Vhost Initiator port */
114 /* ASCII formatted WWPN for Sas Initiator port */
115 char iport_name[TCM_VHOST_NAMELEN];
116 /* Returned by tcm_vhost_make_nodeacl() */
117 struct se_node_acl se_node_acl;
120 struct tcm_vhost_tpg {
121 /* Vhost port target portal group tag for TCM */
123 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
124 int tv_tpg_port_count;
125 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
126 int tv_tpg_vhost_count;
127 /* list for tcm_vhost_list */
128 struct list_head tv_tpg_list;
129 /* Used to protect access for tpg_nexus */
130 struct mutex tv_tpg_mutex;
131 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
132 struct tcm_vhost_nexus *tpg_nexus;
133 /* Pointer back to tcm_vhost_tport */
134 struct tcm_vhost_tport *tport;
135 /* Returned by tcm_vhost_make_tpg() */
136 struct se_portal_group se_tpg;
137 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
138 struct vhost_scsi *vhost_scsi;
141 struct tcm_vhost_tport {
142 /* SCSI protocol the tport is providing */
144 /* Binary World Wide unique Port Name for Vhost Target port */
146 /* ASCII formatted WWPN for Vhost Target port */
147 char tport_name[TCM_VHOST_NAMELEN];
148 /* Returned by tcm_vhost_make_tport() */
149 struct se_wwn tport_wwn;
152 struct tcm_vhost_evt {
153 /* event to be sent to guest */
154 struct virtio_scsi_event event;
155 /* event list, serviced from vhost worker thread */
156 struct llist_node list;
160 VHOST_SCSI_VQ_CTL = 0,
161 VHOST_SCSI_VQ_EVT = 1,
162 VHOST_SCSI_VQ_IO = 2,
166 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG)
169 #define VHOST_SCSI_MAX_TARGET 256
170 #define VHOST_SCSI_MAX_VQ 128
171 #define VHOST_SCSI_MAX_EVENT 128
173 struct vhost_scsi_virtqueue {
174 struct vhost_virtqueue vq;
176 * Reference counting for inflight reqs, used for flush operation. At
177 * each time, one reference tracks new commands submitted, while we
178 * wait for another one to reach 0.
180 struct vhost_scsi_inflight inflights[2];
182 * Indicate current inflight in use, protected by vq->mutex.
183 * Writers must also take dev mutex and flush under it.
189 /* Protected by vhost_scsi->dev.mutex */
190 struct tcm_vhost_tpg **vs_tpg;
191 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
193 struct vhost_dev dev;
194 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
196 struct vhost_work vs_completion_work; /* cmd completion work item */
197 struct llist_head vs_completion_list; /* cmd completion queue */
199 struct vhost_work vs_event_work; /* evt injection work item */
200 struct llist_head vs_event_list; /* evt injection queue */
202 bool vs_events_missed; /* any missed events, protected by vq->mutex */
203 int vs_events_nr; /* num of pending events, protected by vq->mutex */
206 /* Local pointer to allocated TCM configfs fabric module */
207 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
209 static struct workqueue_struct *tcm_vhost_workqueue;
211 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
212 static DEFINE_MUTEX(tcm_vhost_mutex);
213 static LIST_HEAD(tcm_vhost_list);
215 static int iov_num_pages(struct iovec *iov)
217 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
218 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
221 static void tcm_vhost_done_inflight(struct kref *kref)
223 struct vhost_scsi_inflight *inflight;
225 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
226 complete(&inflight->comp);
229 static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
230 struct vhost_scsi_inflight *old_inflight[])
232 struct vhost_scsi_inflight *new_inflight;
233 struct vhost_virtqueue *vq;
236 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
239 mutex_lock(&vq->mutex);
241 /* store old infight */
242 idx = vs->vqs[i].inflight_idx;
244 old_inflight[i] = &vs->vqs[i].inflights[idx];
246 /* setup new infight */
247 vs->vqs[i].inflight_idx = idx ^ 1;
248 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
249 kref_init(&new_inflight->kref);
250 init_completion(&new_inflight->comp);
252 mutex_unlock(&vq->mutex);
256 static struct vhost_scsi_inflight *
257 tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
259 struct vhost_scsi_inflight *inflight;
260 struct vhost_scsi_virtqueue *svq;
262 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
263 inflight = &svq->inflights[svq->inflight_idx];
264 kref_get(&inflight->kref);
269 static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
271 kref_put(&inflight->kref, tcm_vhost_done_inflight);
274 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
279 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
284 static char *tcm_vhost_get_fabric_name(void)
289 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
291 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
292 struct tcm_vhost_tpg, se_tpg);
293 struct tcm_vhost_tport *tport = tpg->tport;
295 switch (tport->tport_proto_id) {
296 case SCSI_PROTOCOL_SAS:
297 return sas_get_fabric_proto_ident(se_tpg);
298 case SCSI_PROTOCOL_FCP:
299 return fc_get_fabric_proto_ident(se_tpg);
300 case SCSI_PROTOCOL_ISCSI:
301 return iscsi_get_fabric_proto_ident(se_tpg);
303 pr_err("Unknown tport_proto_id: 0x%02x, using"
304 " SAS emulation\n", tport->tport_proto_id);
308 return sas_get_fabric_proto_ident(se_tpg);
311 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
313 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
314 struct tcm_vhost_tpg, se_tpg);
315 struct tcm_vhost_tport *tport = tpg->tport;
317 return &tport->tport_name[0];
320 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
322 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
323 struct tcm_vhost_tpg, se_tpg);
324 return tpg->tport_tpgt;
327 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
333 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
334 struct se_node_acl *se_nacl,
335 struct t10_pr_registration *pr_reg,
339 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
340 struct tcm_vhost_tpg, se_tpg);
341 struct tcm_vhost_tport *tport = tpg->tport;
343 switch (tport->tport_proto_id) {
344 case SCSI_PROTOCOL_SAS:
345 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
347 case SCSI_PROTOCOL_FCP:
348 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
350 case SCSI_PROTOCOL_ISCSI:
351 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
354 pr_err("Unknown tport_proto_id: 0x%02x, using"
355 " SAS emulation\n", tport->tport_proto_id);
359 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
364 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
365 struct se_node_acl *se_nacl,
366 struct t10_pr_registration *pr_reg,
369 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
370 struct tcm_vhost_tpg, se_tpg);
371 struct tcm_vhost_tport *tport = tpg->tport;
373 switch (tport->tport_proto_id) {
374 case SCSI_PROTOCOL_SAS:
375 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
377 case SCSI_PROTOCOL_FCP:
378 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
380 case SCSI_PROTOCOL_ISCSI:
381 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
384 pr_err("Unknown tport_proto_id: 0x%02x, using"
385 " SAS emulation\n", tport->tport_proto_id);
389 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
394 tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
397 char **port_nexus_ptr)
399 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
400 struct tcm_vhost_tpg, se_tpg);
401 struct tcm_vhost_tport *tport = tpg->tport;
403 switch (tport->tport_proto_id) {
404 case SCSI_PROTOCOL_SAS:
405 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
407 case SCSI_PROTOCOL_FCP:
408 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
410 case SCSI_PROTOCOL_ISCSI:
411 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
414 pr_err("Unknown tport_proto_id: 0x%02x, using"
415 " SAS emulation\n", tport->tport_proto_id);
419 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
423 static struct se_node_acl *
424 tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg)
426 struct tcm_vhost_nacl *nacl;
428 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
430 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
434 return &nacl->se_node_acl;
438 tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
439 struct se_node_acl *se_nacl)
441 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
442 struct tcm_vhost_nacl, se_node_acl);
446 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
451 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
453 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
454 struct tcm_vhost_cmd, tvc_se_cmd);
455 struct se_session *se_sess = se_cmd->se_sess;
457 if (tv_cmd->tvc_sgl_count) {
459 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
460 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
462 kfree(tv_cmd->tvc_sgl);
465 tcm_vhost_put_inflight(tv_cmd->inflight);
466 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
469 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
474 static void tcm_vhost_close_session(struct se_session *se_sess)
479 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
484 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
486 /* Go ahead and process the write immediately */
487 target_execute_cmd(se_cmd);
491 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
496 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
501 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
506 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
511 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *cmd)
513 struct vhost_scsi *vs = cmd->tvc_vhost;
515 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
517 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
520 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
522 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
523 struct tcm_vhost_cmd, tvc_se_cmd);
524 vhost_scsi_complete_cmd(cmd);
528 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
530 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
531 struct tcm_vhost_cmd, tvc_se_cmd);
532 vhost_scsi_complete_cmd(cmd);
536 static void tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
541 static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
547 static struct tcm_vhost_evt *
548 tcm_vhost_allocate_evt(struct vhost_scsi *vs,
549 u32 event, u32 reason)
551 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
552 struct tcm_vhost_evt *evt;
554 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
555 vs->vs_events_missed = true;
559 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
561 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
562 vs->vs_events_missed = true;
566 evt->event.event = event;
567 evt->event.reason = reason;
573 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *cmd)
575 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
577 /* TODO locking against target/backend threads? */
578 transport_generic_free_cmd(se_cmd, 0);
582 static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
584 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
588 tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
590 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
591 struct virtio_scsi_event *event = &evt->event;
592 struct virtio_scsi_event __user *eventp;
596 if (!vq->private_data) {
597 vs->vs_events_missed = true;
602 vhost_disable_notify(&vs->dev, vq);
603 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
604 ARRAY_SIZE(vq->iov), &out, &in,
607 vs->vs_events_missed = true;
610 if (head == vq->num) {
611 if (vhost_enable_notify(&vs->dev, vq))
613 vs->vs_events_missed = true;
617 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
618 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
619 vq->iov[out].iov_len);
620 vs->vs_events_missed = true;
624 if (vs->vs_events_missed) {
625 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
626 vs->vs_events_missed = false;
629 eventp = vq->iov[out].iov_base;
630 ret = __copy_to_user(eventp, event, sizeof(*event));
632 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
634 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
637 static void tcm_vhost_evt_work(struct vhost_work *work)
639 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
641 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
642 struct tcm_vhost_evt *evt;
643 struct llist_node *llnode;
645 mutex_lock(&vq->mutex);
646 llnode = llist_del_all(&vs->vs_event_list);
648 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
649 llnode = llist_next(llnode);
650 tcm_vhost_do_evt_work(vs, evt);
651 tcm_vhost_free_evt(vs, evt);
653 mutex_unlock(&vq->mutex);
656 /* Fill in status and signal that we are done processing this command
658 * This is scheduled in the vhost work queue so we are called with the owner
659 * process mm and can access the vring.
661 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
663 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
665 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
666 struct virtio_scsi_cmd_resp v_rsp;
667 struct tcm_vhost_cmd *cmd;
668 struct llist_node *llnode;
669 struct se_cmd *se_cmd;
672 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
673 llnode = llist_del_all(&vs->vs_completion_list);
675 cmd = llist_entry(llnode, struct tcm_vhost_cmd,
676 tvc_completion_list);
677 llnode = llist_next(llnode);
678 se_cmd = &cmd->tvc_se_cmd;
680 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
681 cmd, se_cmd->residual_count, se_cmd->scsi_status);
683 memset(&v_rsp, 0, sizeof(v_rsp));
684 v_rsp.resid = se_cmd->residual_count;
685 /* TODO is status_qualifier field needed? */
686 v_rsp.status = se_cmd->scsi_status;
687 v_rsp.sense_len = se_cmd->scsi_sense_length;
688 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
690 ret = copy_to_user(cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
691 if (likely(ret == 0)) {
692 struct vhost_scsi_virtqueue *q;
693 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
694 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
696 __set_bit(vq, signal);
698 pr_err("Faulted on virtio_scsi_cmd_resp\n");
700 vhost_scsi_free_cmd(cmd);
704 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
706 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
709 static struct tcm_vhost_cmd *
710 vhost_scsi_get_tag(struct vhost_virtqueue *vq,
711 struct tcm_vhost_tpg *tpg,
712 struct virtio_scsi_cmd_req *v_req,
716 struct tcm_vhost_cmd *cmd;
717 struct tcm_vhost_nexus *tv_nexus;
718 struct se_session *se_sess;
721 tv_nexus = tpg->tpg_nexus;
723 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
724 return ERR_PTR(-EIO);
726 se_sess = tv_nexus->tvn_se_sess;
728 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, GFP_KERNEL);
729 cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[tag];
730 memset(cmd, 0, sizeof(struct tcm_vhost_cmd));
732 cmd->tvc_se_cmd.map_tag = tag;
733 cmd->tvc_tag = v_req->tag;
734 cmd->tvc_task_attr = v_req->task_attr;
735 cmd->tvc_exp_data_len = exp_data_len;
736 cmd->tvc_data_direction = data_direction;
737 cmd->tvc_nexus = tv_nexus;
738 cmd->inflight = tcm_vhost_get_inflight(vq);
744 * Map a user memory range into a scatterlist
746 * Returns the number of scatterlist entries used or -errno on error.
749 vhost_scsi_map_to_sgl(struct scatterlist *sgl,
750 unsigned int sgl_count,
754 unsigned int npages = 0, pages_nr, offset, nbytes;
755 struct scatterlist *sg = sgl;
756 void __user *ptr = iov->iov_base;
757 size_t len = iov->iov_len;
761 pages_nr = iov_num_pages(iov);
762 if (pages_nr > sgl_count)
765 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
769 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
770 /* No pages were pinned */
773 /* Less pages pinned than wanted */
774 if (ret != pages_nr) {
775 for (i = 0; i < ret; i++)
782 offset = (uintptr_t)ptr & ~PAGE_MASK;
783 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
784 sg_set_page(sg, pages[npages], nbytes, offset);
797 vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *cmd,
805 struct scatterlist *sg;
808 * Find out how long sglist needs to be
811 for (i = 0; i < niov; i++)
812 sgl_count += iov_num_pages(&iov[i]);
814 /* TODO overflow checking */
816 sg = kmalloc(sizeof(cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
819 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
821 sg_init_table(sg, sgl_count);
824 cmd->tvc_sgl_count = sgl_count;
826 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
827 for (i = 0; i < niov; i++) {
828 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
830 for (i = 0; i < cmd->tvc_sgl_count; i++)
831 put_page(sg_page(&cmd->tvc_sgl[i]));
834 cmd->tvc_sgl_count = 0;
844 static void tcm_vhost_submission_work(struct work_struct *work)
846 struct tcm_vhost_cmd *cmd =
847 container_of(work, struct tcm_vhost_cmd, work);
848 struct tcm_vhost_nexus *tv_nexus;
849 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
850 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
851 int rc, sg_no_bidi = 0;
853 if (cmd->tvc_sgl_count) {
854 sg_ptr = cmd->tvc_sgl;
855 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
857 if (se_cmd->se_cmd_flags & SCF_BIDI) {
865 tv_nexus = cmd->tvc_nexus;
867 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
868 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
869 cmd->tvc_lun, cmd->tvc_exp_data_len,
870 cmd->tvc_task_attr, cmd->tvc_data_direction,
871 TARGET_SCF_ACK_KREF, sg_ptr, cmd->tvc_sgl_count,
872 sg_bidi_ptr, sg_no_bidi);
874 transport_send_check_condition_and_sense(se_cmd,
875 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
876 transport_generic_free_cmd(se_cmd, 0);
881 vhost_scsi_send_bad_target(struct vhost_scsi *vs,
882 struct vhost_virtqueue *vq,
883 int head, unsigned out)
885 struct virtio_scsi_cmd_resp __user *resp;
886 struct virtio_scsi_cmd_resp rsp;
889 memset(&rsp, 0, sizeof(rsp));
890 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
891 resp = vq->iov[out].iov_base;
892 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
894 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
896 pr_err("Faulted on virtio_scsi_cmd_resp\n");
900 vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
902 struct tcm_vhost_tpg **vs_tpg;
903 struct virtio_scsi_cmd_req v_req;
904 struct tcm_vhost_tpg *tpg;
905 struct tcm_vhost_cmd *cmd;
906 u32 exp_data_len, data_first, data_num, data_direction;
911 mutex_lock(&vq->mutex);
913 * We can handle the vq only after the endpoint is setup by calling the
914 * VHOST_SCSI_SET_ENDPOINT ioctl.
916 vs_tpg = vq->private_data;
920 vhost_disable_notify(&vs->dev, vq);
923 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
924 ARRAY_SIZE(vq->iov), &out, &in,
926 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
928 /* On error, stop handling until the next kick. */
929 if (unlikely(head < 0))
931 /* Nothing new? Wait for eventfd to tell us they refilled. */
932 if (head == vq->num) {
933 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
934 vhost_disable_notify(&vs->dev, vq);
940 /* FIXME: BIDI operation */
941 if (out == 1 && in == 1) {
942 data_direction = DMA_NONE;
945 } else if (out == 1 && in > 1) {
946 data_direction = DMA_FROM_DEVICE;
947 data_first = out + 1;
949 } else if (out > 1 && in == 1) {
950 data_direction = DMA_TO_DEVICE;
954 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
960 * Check for a sane resp buffer so we can report errors to
963 if (unlikely(vq->iov[out].iov_len !=
964 sizeof(struct virtio_scsi_cmd_resp))) {
965 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
966 " bytes\n", vq->iov[out].iov_len);
970 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
971 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
972 " bytes\n", vq->iov[0].iov_len);
975 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
976 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
977 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
980 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
984 /* Extract the tpgt */
985 target = v_req.lun[1];
986 tpg = ACCESS_ONCE(vs_tpg[target]);
988 /* Target does not exist, fail the request */
989 if (unlikely(!tpg)) {
990 vhost_scsi_send_bad_target(vs, vq, head, out);
995 for (i = 0; i < data_num; i++)
996 exp_data_len += vq->iov[data_first + i].iov_len;
998 cmd = vhost_scsi_get_tag(vq, tpg, &v_req,
999 exp_data_len, data_direction);
1001 vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
1005 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
1006 ": %d\n", cmd, exp_data_len, data_direction);
1008 cmd->tvc_vhost = vs;
1010 cmd->tvc_resp = vq->iov[out].iov_base;
1013 * Copy in the recieved CDB descriptor into cmd->tvc_cdb
1014 * that will be used by tcm_vhost_new_cmd_map() and down into
1015 * target_setup_cmd_from_cdb()
1017 memcpy(cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
1019 * Check that the recieved CDB size does not exceeded our
1020 * hardcoded max for tcm_vhost
1022 /* TODO what if cdb was too small for varlen cdb header? */
1023 if (unlikely(scsi_command_size(cmd->tvc_cdb) >
1024 TCM_VHOST_MAX_CDB_SIZE)) {
1025 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1026 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1027 scsi_command_size(cmd->tvc_cdb),
1028 TCM_VHOST_MAX_CDB_SIZE);
1031 cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1033 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1034 cmd->tvc_cdb[0], cmd->tvc_lun);
1036 if (data_direction != DMA_NONE) {
1037 ret = vhost_scsi_map_iov_to_sgl(cmd,
1038 &vq->iov[data_first], data_num,
1039 data_direction == DMA_TO_DEVICE);
1040 if (unlikely(ret)) {
1041 vq_err(vq, "Failed to map iov to sgl\n");
1047 * Save the descriptor from vhost_get_vq_desc() to be used to
1048 * complete the virtio-scsi request in TCM callback context via
1049 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1051 cmd->tvc_vq_desc = head;
1053 * Dispatch tv_cmd descriptor for cmwq execution in process
1054 * context provided by tcm_vhost_workqueue. This also ensures
1055 * tv_cmd is executed on the same kworker CPU as this vhost
1056 * thread to gain positive L2 cache locality effects..
1058 INIT_WORK(&cmd->work, tcm_vhost_submission_work);
1059 queue_work(tcm_vhost_workqueue, &cmd->work);
1062 mutex_unlock(&vq->mutex);
1066 vhost_scsi_free_cmd(cmd);
1068 vhost_scsi_send_bad_target(vs, vq, head, out);
1070 mutex_unlock(&vq->mutex);
1073 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1075 pr_debug("%s: The handling func for control queue.\n", __func__);
1079 tcm_vhost_send_evt(struct vhost_scsi *vs,
1080 struct tcm_vhost_tpg *tpg,
1085 struct tcm_vhost_evt *evt;
1087 evt = tcm_vhost_allocate_evt(vs, event, reason);
1092 /* TODO: share lun setup code with virtio-scsi.ko */
1094 * Note: evt->event is zeroed when we allocate it and
1095 * lun[4-7] need to be zero according to virtio-scsi spec.
1097 evt->event.lun[0] = 0x01;
1098 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
1099 if (lun->unpacked_lun >= 256)
1100 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1101 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1104 llist_add(&evt->list, &vs->vs_event_list);
1105 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1108 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1110 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1112 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1114 mutex_lock(&vq->mutex);
1115 if (!vq->private_data)
1118 if (vs->vs_events_missed)
1119 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1121 mutex_unlock(&vq->mutex);
1124 static void vhost_scsi_handle_kick(struct vhost_work *work)
1126 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1128 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1130 vhost_scsi_handle_vq(vs, vq);
1133 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1135 vhost_poll_flush(&vs->vqs[index].vq.poll);
1138 /* Callers must hold dev mutex */
1139 static void vhost_scsi_flush(struct vhost_scsi *vs)
1141 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
1144 /* Init new inflight and remember the old inflight */
1145 tcm_vhost_init_inflight(vs, old_inflight);
1148 * The inflight->kref was initialized to 1. We decrement it here to
1149 * indicate the start of the flush operation so that it will reach 0
1150 * when all the reqs are finished.
1152 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1153 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1155 /* Flush both the vhost poll and vhost work */
1156 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1157 vhost_scsi_flush_vq(vs, i);
1158 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
1159 vhost_work_flush(&vs->dev, &vs->vs_event_work);
1161 /* Wait for all reqs issued before the flush to be finished */
1162 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1163 wait_for_completion(&old_inflight[i]->comp);
1167 * Called from vhost_scsi_ioctl() context to walk the list of available
1168 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
1170 * The lock nesting rule is:
1171 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1174 vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1175 struct vhost_scsi_target *t)
1177 struct tcm_vhost_tport *tv_tport;
1178 struct tcm_vhost_tpg *tpg;
1179 struct tcm_vhost_tpg **vs_tpg;
1180 struct vhost_virtqueue *vq;
1181 int index, ret, i, len;
1184 mutex_lock(&tcm_vhost_mutex);
1185 mutex_lock(&vs->dev.mutex);
1187 /* Verify that ring has been setup correctly. */
1188 for (index = 0; index < vs->dev.nvqs; ++index) {
1189 /* Verify that ring has been setup correctly. */
1190 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1196 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1197 vs_tpg = kzalloc(len, GFP_KERNEL);
1203 memcpy(vs_tpg, vs->vs_tpg, len);
1205 list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
1206 mutex_lock(&tpg->tv_tpg_mutex);
1207 if (!tpg->tpg_nexus) {
1208 mutex_unlock(&tpg->tv_tpg_mutex);
1211 if (tpg->tv_tpg_vhost_count != 0) {
1212 mutex_unlock(&tpg->tv_tpg_mutex);
1215 tv_tport = tpg->tport;
1217 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1218 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
1220 mutex_unlock(&tpg->tv_tpg_mutex);
1224 tpg->tv_tpg_vhost_count++;
1225 tpg->vhost_scsi = vs;
1226 vs_tpg[tpg->tport_tpgt] = tpg;
1227 smp_mb__after_atomic_inc();
1230 mutex_unlock(&tpg->tv_tpg_mutex);
1234 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1235 sizeof(vs->vs_vhost_wwpn));
1236 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1237 vq = &vs->vqs[i].vq;
1238 mutex_lock(&vq->mutex);
1239 vq->private_data = vs_tpg;
1240 vhost_init_used(vq);
1241 mutex_unlock(&vq->mutex);
1249 * Act as synchronize_rcu to make sure access to
1250 * old vs->vs_tpg is finished.
1252 vhost_scsi_flush(vs);
1254 vs->vs_tpg = vs_tpg;
1257 mutex_unlock(&vs->dev.mutex);
1258 mutex_unlock(&tcm_vhost_mutex);
1263 vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1264 struct vhost_scsi_target *t)
1266 struct tcm_vhost_tport *tv_tport;
1267 struct tcm_vhost_tpg *tpg;
1268 struct vhost_virtqueue *vq;
1273 mutex_lock(&tcm_vhost_mutex);
1274 mutex_lock(&vs->dev.mutex);
1275 /* Verify that ring has been setup correctly. */
1276 for (index = 0; index < vs->dev.nvqs; ++index) {
1277 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1288 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1290 tpg = vs->vs_tpg[target];
1294 mutex_lock(&tpg->tv_tpg_mutex);
1295 tv_tport = tpg->tport;
1301 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1302 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
1303 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1304 tv_tport->tport_name, tpg->tport_tpgt,
1305 t->vhost_wwpn, t->vhost_tpgt);
1309 tpg->tv_tpg_vhost_count--;
1310 tpg->vhost_scsi = NULL;
1311 vs->vs_tpg[target] = NULL;
1313 mutex_unlock(&tpg->tv_tpg_mutex);
1316 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1317 vq = &vs->vqs[i].vq;
1318 mutex_lock(&vq->mutex);
1319 vq->private_data = NULL;
1320 mutex_unlock(&vq->mutex);
1324 * Act as synchronize_rcu to make sure access to
1325 * old vs->vs_tpg is finished.
1327 vhost_scsi_flush(vs);
1330 WARN_ON(vs->vs_events_nr);
1331 mutex_unlock(&vs->dev.mutex);
1332 mutex_unlock(&tcm_vhost_mutex);
1336 mutex_unlock(&tpg->tv_tpg_mutex);
1338 mutex_unlock(&vs->dev.mutex);
1339 mutex_unlock(&tcm_vhost_mutex);
1343 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1345 if (features & ~VHOST_SCSI_FEATURES)
1348 mutex_lock(&vs->dev.mutex);
1349 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1350 !vhost_log_access_ok(&vs->dev)) {
1351 mutex_unlock(&vs->dev.mutex);
1354 vs->dev.acked_features = features;
1356 vhost_scsi_flush(vs);
1357 mutex_unlock(&vs->dev.mutex);
1361 static int vhost_scsi_open(struct inode *inode, struct file *f)
1363 struct vhost_scsi *vs;
1364 struct vhost_virtqueue **vqs;
1367 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
1371 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1377 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1378 vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
1380 vs->vs_events_nr = 0;
1381 vs->vs_events_missed = false;
1383 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1384 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1385 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1386 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
1387 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
1388 vqs[i] = &vs->vqs[i].vq;
1389 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
1391 r = vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
1393 tcm_vhost_init_inflight(vs, NULL);
1401 f->private_data = vs;
1405 static int vhost_scsi_release(struct inode *inode, struct file *f)
1407 struct vhost_scsi *vs = f->private_data;
1408 struct vhost_scsi_target t;
1410 mutex_lock(&vs->dev.mutex);
1411 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1412 mutex_unlock(&vs->dev.mutex);
1413 vhost_scsi_clear_endpoint(vs, &t);
1414 vhost_dev_stop(&vs->dev);
1415 vhost_dev_cleanup(&vs->dev, false);
1416 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1417 vhost_scsi_flush(vs);
1424 vhost_scsi_ioctl(struct file *f,
1428 struct vhost_scsi *vs = f->private_data;
1429 struct vhost_scsi_target backend;
1430 void __user *argp = (void __user *)arg;
1431 u64 __user *featurep = argp;
1432 u32 __user *eventsp = argp;
1435 int r, abi_version = VHOST_SCSI_ABI_VERSION;
1436 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1439 case VHOST_SCSI_SET_ENDPOINT:
1440 if (copy_from_user(&backend, argp, sizeof backend))
1442 if (backend.reserved != 0)
1445 return vhost_scsi_set_endpoint(vs, &backend);
1446 case VHOST_SCSI_CLEAR_ENDPOINT:
1447 if (copy_from_user(&backend, argp, sizeof backend))
1449 if (backend.reserved != 0)
1452 return vhost_scsi_clear_endpoint(vs, &backend);
1453 case VHOST_SCSI_GET_ABI_VERSION:
1454 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1457 case VHOST_SCSI_SET_EVENTS_MISSED:
1458 if (get_user(events_missed, eventsp))
1460 mutex_lock(&vq->mutex);
1461 vs->vs_events_missed = events_missed;
1462 mutex_unlock(&vq->mutex);
1464 case VHOST_SCSI_GET_EVENTS_MISSED:
1465 mutex_lock(&vq->mutex);
1466 events_missed = vs->vs_events_missed;
1467 mutex_unlock(&vq->mutex);
1468 if (put_user(events_missed, eventsp))
1471 case VHOST_GET_FEATURES:
1472 features = VHOST_SCSI_FEATURES;
1473 if (copy_to_user(featurep, &features, sizeof features))
1476 case VHOST_SET_FEATURES:
1477 if (copy_from_user(&features, featurep, sizeof features))
1479 return vhost_scsi_set_features(vs, features);
1481 mutex_lock(&vs->dev.mutex);
1482 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1483 /* TODO: flush backend after dev ioctl. */
1484 if (r == -ENOIOCTLCMD)
1485 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1486 mutex_unlock(&vs->dev.mutex);
1491 #ifdef CONFIG_COMPAT
1492 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1495 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1499 static const struct file_operations vhost_scsi_fops = {
1500 .owner = THIS_MODULE,
1501 .release = vhost_scsi_release,
1502 .unlocked_ioctl = vhost_scsi_ioctl,
1503 #ifdef CONFIG_COMPAT
1504 .compat_ioctl = vhost_scsi_compat_ioctl,
1506 .open = vhost_scsi_open,
1507 .llseek = noop_llseek,
1510 static struct miscdevice vhost_scsi_misc = {
1516 static int __init vhost_scsi_register(void)
1518 return misc_register(&vhost_scsi_misc);
1521 static int vhost_scsi_deregister(void)
1523 return misc_deregister(&vhost_scsi_misc);
1526 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1528 switch (tport->tport_proto_id) {
1529 case SCSI_PROTOCOL_SAS:
1531 case SCSI_PROTOCOL_FCP:
1533 case SCSI_PROTOCOL_ISCSI:
1543 tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1544 struct se_lun *lun, bool plug)
1547 struct vhost_scsi *vs = tpg->vhost_scsi;
1548 struct vhost_virtqueue *vq;
1554 mutex_lock(&vs->dev.mutex);
1555 if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1556 mutex_unlock(&vs->dev.mutex);
1561 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1563 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1565 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1566 mutex_lock(&vq->mutex);
1567 tcm_vhost_send_evt(vs, tpg, lun,
1568 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1569 mutex_unlock(&vq->mutex);
1570 mutex_unlock(&vs->dev.mutex);
1573 static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1575 tcm_vhost_do_plug(tpg, lun, true);
1578 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1580 tcm_vhost_do_plug(tpg, lun, false);
1583 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1586 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1587 struct tcm_vhost_tpg, se_tpg);
1589 mutex_lock(&tcm_vhost_mutex);
1591 mutex_lock(&tpg->tv_tpg_mutex);
1592 tpg->tv_tpg_port_count++;
1593 mutex_unlock(&tpg->tv_tpg_mutex);
1595 tcm_vhost_hotplug(tpg, lun);
1597 mutex_unlock(&tcm_vhost_mutex);
1602 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1605 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1606 struct tcm_vhost_tpg, se_tpg);
1608 mutex_lock(&tcm_vhost_mutex);
1610 mutex_lock(&tpg->tv_tpg_mutex);
1611 tpg->tv_tpg_port_count--;
1612 mutex_unlock(&tpg->tv_tpg_mutex);
1614 tcm_vhost_hotunplug(tpg, lun);
1616 mutex_unlock(&tcm_vhost_mutex);
1619 static struct se_node_acl *
1620 tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
1621 struct config_group *group,
1624 struct se_node_acl *se_nacl, *se_nacl_new;
1625 struct tcm_vhost_nacl *nacl;
1629 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1630 return ERR_PTR(-EINVAL); */
1631 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1633 return ERR_PTR(-ENOMEM);
1637 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1638 * when converting a NodeACL from demo mode -> explict
1640 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1642 if (IS_ERR(se_nacl)) {
1643 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1647 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1649 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1650 nacl->iport_wwpn = wwpn;
1655 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1657 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1658 struct tcm_vhost_nacl, se_node_acl);
1659 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1663 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
1666 struct se_portal_group *se_tpg;
1667 struct tcm_vhost_nexus *tv_nexus;
1669 mutex_lock(&tpg->tv_tpg_mutex);
1670 if (tpg->tpg_nexus) {
1671 mutex_unlock(&tpg->tv_tpg_mutex);
1672 pr_debug("tpg->tpg_nexus already exists\n");
1675 se_tpg = &tpg->se_tpg;
1677 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1679 mutex_unlock(&tpg->tv_tpg_mutex);
1680 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1684 * Initialize the struct se_session pointer and setup tagpool
1685 * for struct tcm_vhost_cmd descriptors
1687 tv_nexus->tvn_se_sess = transport_init_session_tags(
1688 TCM_VHOST_DEFAULT_TAGS,
1689 sizeof(struct tcm_vhost_cmd));
1690 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1691 mutex_unlock(&tpg->tv_tpg_mutex);
1696 * Since we are running in 'demo mode' this call with generate a
1697 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1698 * the SCSI Initiator port name of the passed configfs group 'name'.
1700 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1701 se_tpg, (unsigned char *)name);
1702 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1703 mutex_unlock(&tpg->tv_tpg_mutex);
1704 pr_debug("core_tpg_check_initiator_node_acl() failed"
1706 transport_free_session(tv_nexus->tvn_se_sess);
1711 * Now register the TCM vhost virtual I_T Nexus as active with the
1712 * call to __transport_register_session()
1714 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1715 tv_nexus->tvn_se_sess, tv_nexus);
1716 tpg->tpg_nexus = tv_nexus;
1718 mutex_unlock(&tpg->tv_tpg_mutex);
1722 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1724 struct se_session *se_sess;
1725 struct tcm_vhost_nexus *tv_nexus;
1727 mutex_lock(&tpg->tv_tpg_mutex);
1728 tv_nexus = tpg->tpg_nexus;
1730 mutex_unlock(&tpg->tv_tpg_mutex);
1734 se_sess = tv_nexus->tvn_se_sess;
1736 mutex_unlock(&tpg->tv_tpg_mutex);
1740 if (tpg->tv_tpg_port_count != 0) {
1741 mutex_unlock(&tpg->tv_tpg_mutex);
1742 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1743 " active TPG port count: %d\n",
1744 tpg->tv_tpg_port_count);
1748 if (tpg->tv_tpg_vhost_count != 0) {
1749 mutex_unlock(&tpg->tv_tpg_mutex);
1750 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1751 " active TPG vhost count: %d\n",
1752 tpg->tv_tpg_vhost_count);
1756 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1757 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1758 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1760 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1762 transport_deregister_session(tv_nexus->tvn_se_sess);
1763 tpg->tpg_nexus = NULL;
1764 mutex_unlock(&tpg->tv_tpg_mutex);
1770 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1773 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1774 struct tcm_vhost_tpg, se_tpg);
1775 struct tcm_vhost_nexus *tv_nexus;
1778 mutex_lock(&tpg->tv_tpg_mutex);
1779 tv_nexus = tpg->tpg_nexus;
1781 mutex_unlock(&tpg->tv_tpg_mutex);
1784 ret = snprintf(page, PAGE_SIZE, "%s\n",
1785 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1786 mutex_unlock(&tpg->tv_tpg_mutex);
1791 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1795 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1796 struct tcm_vhost_tpg, se_tpg);
1797 struct tcm_vhost_tport *tport_wwn = tpg->tport;
1798 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1801 * Shutdown the active I_T nexus if 'NULL' is passed..
1803 if (!strncmp(page, "NULL", 4)) {
1804 ret = tcm_vhost_drop_nexus(tpg);
1805 return (!ret) ? count : ret;
1808 * Otherwise make sure the passed virtual Initiator port WWN matches
1809 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1810 * tcm_vhost_make_nexus().
1812 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1813 pr_err("Emulated NAA Sas Address: %s, exceeds"
1814 " max: %d\n", page, TCM_VHOST_NAMELEN);
1817 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1819 ptr = strstr(i_port, "naa.");
1821 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1822 pr_err("Passed SAS Initiator Port %s does not"
1823 " match target port protoid: %s\n", i_port,
1824 tcm_vhost_dump_proto_id(tport_wwn));
1827 port_ptr = &i_port[0];
1830 ptr = strstr(i_port, "fc.");
1832 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1833 pr_err("Passed FCP Initiator Port %s does not"
1834 " match target port protoid: %s\n", i_port,
1835 tcm_vhost_dump_proto_id(tport_wwn));
1838 port_ptr = &i_port[3]; /* Skip over "fc." */
1841 ptr = strstr(i_port, "iqn.");
1843 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1844 pr_err("Passed iSCSI Initiator Port %s does not"
1845 " match target port protoid: %s\n", i_port,
1846 tcm_vhost_dump_proto_id(tport_wwn));
1849 port_ptr = &i_port[0];
1852 pr_err("Unable to locate prefix for emulated Initiator Port:"
1856 * Clear any trailing newline for the NAA WWN
1859 if (i_port[strlen(i_port)-1] == '\n')
1860 i_port[strlen(i_port)-1] = '\0';
1862 ret = tcm_vhost_make_nexus(tpg, port_ptr);
1869 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1871 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1872 &tcm_vhost_tpg_nexus.attr,
1876 static struct se_portal_group *
1877 tcm_vhost_make_tpg(struct se_wwn *wwn,
1878 struct config_group *group,
1881 struct tcm_vhost_tport *tport = container_of(wwn,
1882 struct tcm_vhost_tport, tport_wwn);
1884 struct tcm_vhost_tpg *tpg;
1888 if (strstr(name, "tpgt_") != name)
1889 return ERR_PTR(-EINVAL);
1890 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1891 return ERR_PTR(-EINVAL);
1893 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1895 pr_err("Unable to allocate struct tcm_vhost_tpg");
1896 return ERR_PTR(-ENOMEM);
1898 mutex_init(&tpg->tv_tpg_mutex);
1899 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1901 tpg->tport_tpgt = tpgt;
1903 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1904 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1909 mutex_lock(&tcm_vhost_mutex);
1910 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1911 mutex_unlock(&tcm_vhost_mutex);
1913 return &tpg->se_tpg;
1916 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1918 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1919 struct tcm_vhost_tpg, se_tpg);
1921 mutex_lock(&tcm_vhost_mutex);
1922 list_del(&tpg->tv_tpg_list);
1923 mutex_unlock(&tcm_vhost_mutex);
1925 * Release the virtual I_T Nexus for this vhost TPG
1927 tcm_vhost_drop_nexus(tpg);
1929 * Deregister the se_tpg from TCM..
1931 core_tpg_deregister(se_tpg);
1935 static struct se_wwn *
1936 tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1937 struct config_group *group,
1940 struct tcm_vhost_tport *tport;
1945 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1946 return ERR_PTR(-EINVAL); */
1948 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1950 pr_err("Unable to allocate struct tcm_vhost_tport");
1951 return ERR_PTR(-ENOMEM);
1953 tport->tport_wwpn = wwpn;
1955 * Determine the emulated Protocol Identifier and Target Port Name
1956 * based on the incoming configfs directory name.
1958 ptr = strstr(name, "naa.");
1960 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1963 ptr = strstr(name, "fc.");
1965 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1966 off = 3; /* Skip over "fc." */
1969 ptr = strstr(name, "iqn.");
1971 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1975 pr_err("Unable to locate prefix for emulated Target Port:"
1978 return ERR_PTR(-EINVAL);
1981 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1982 pr_err("Emulated %s Address: %s, exceeds"
1983 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1986 return ERR_PTR(-EINVAL);
1988 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1990 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1991 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1993 return &tport->tport_wwn;
1996 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1998 struct tcm_vhost_tport *tport = container_of(wwn,
1999 struct tcm_vhost_tport, tport_wwn);
2001 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2002 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
2009 tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
2012 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
2013 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2014 utsname()->machine);
2017 TF_WWN_ATTR_RO(tcm_vhost, version);
2019 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
2020 &tcm_vhost_wwn_version.attr,
2024 static struct target_core_fabric_ops tcm_vhost_ops = {
2025 .get_fabric_name = tcm_vhost_get_fabric_name,
2026 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
2027 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
2028 .tpg_get_tag = tcm_vhost_get_tag,
2029 .tpg_get_default_depth = tcm_vhost_get_default_depth,
2030 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
2031 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
2032 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
2033 .tpg_check_demo_mode = tcm_vhost_check_true,
2034 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
2035 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
2036 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
2037 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
2038 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
2039 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
2040 .release_cmd = tcm_vhost_release_cmd,
2041 .check_stop_free = vhost_scsi_check_stop_free,
2042 .shutdown_session = tcm_vhost_shutdown_session,
2043 .close_session = tcm_vhost_close_session,
2044 .sess_get_index = tcm_vhost_sess_get_index,
2045 .sess_get_initiator_sid = NULL,
2046 .write_pending = tcm_vhost_write_pending,
2047 .write_pending_status = tcm_vhost_write_pending_status,
2048 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
2049 .get_task_tag = tcm_vhost_get_task_tag,
2050 .get_cmd_state = tcm_vhost_get_cmd_state,
2051 .queue_data_in = tcm_vhost_queue_data_in,
2052 .queue_status = tcm_vhost_queue_status,
2053 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
2055 * Setup callers for generic logic in target_core_fabric_configfs.c
2057 .fabric_make_wwn = tcm_vhost_make_tport,
2058 .fabric_drop_wwn = tcm_vhost_drop_tport,
2059 .fabric_make_tpg = tcm_vhost_make_tpg,
2060 .fabric_drop_tpg = tcm_vhost_drop_tpg,
2061 .fabric_post_link = tcm_vhost_port_link,
2062 .fabric_pre_unlink = tcm_vhost_port_unlink,
2063 .fabric_make_np = NULL,
2064 .fabric_drop_np = NULL,
2065 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
2066 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
2069 static int tcm_vhost_register_configfs(void)
2071 struct target_fabric_configfs *fabric;
2074 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2075 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2076 utsname()->machine);
2078 * Register the top level struct config_item_type with TCM core
2080 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
2081 if (IS_ERR(fabric)) {
2082 pr_err("target_fabric_configfs_init() failed\n");
2083 return PTR_ERR(fabric);
2086 * Setup fabric->tf_ops from our local tcm_vhost_ops
2088 fabric->tf_ops = tcm_vhost_ops;
2090 * Setup default attribute lists for various fabric->tf_cit_tmpl
2092 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
2093 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
2094 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
2095 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
2096 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
2097 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2098 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2099 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2100 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
2102 * Register the fabric for use within TCM
2104 ret = target_fabric_configfs_register(fabric);
2106 pr_err("target_fabric_configfs_register() failed"
2107 " for TCM_VHOST\n");
2111 * Setup our local pointer to *fabric
2113 tcm_vhost_fabric_configfs = fabric;
2114 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2118 static void tcm_vhost_deregister_configfs(void)
2120 if (!tcm_vhost_fabric_configfs)
2123 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2124 tcm_vhost_fabric_configfs = NULL;
2125 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2128 static int __init tcm_vhost_init(void)
2132 * Use our own dedicated workqueue for submitting I/O into
2133 * target core to avoid contention within system_wq.
2135 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2136 if (!tcm_vhost_workqueue)
2139 ret = vhost_scsi_register();
2141 goto out_destroy_workqueue;
2143 ret = tcm_vhost_register_configfs();
2145 goto out_vhost_scsi_deregister;
2149 out_vhost_scsi_deregister:
2150 vhost_scsi_deregister();
2151 out_destroy_workqueue:
2152 destroy_workqueue(tcm_vhost_workqueue);
2157 static void tcm_vhost_exit(void)
2159 tcm_vhost_deregister_configfs();
2160 vhost_scsi_deregister();
2161 destroy_workqueue(tcm_vhost_workqueue);
2164 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2165 MODULE_ALIAS("tcm_vhost");
2166 MODULE_LICENSE("GPL");
2167 module_init(tcm_vhost_init);
2168 module_exit(tcm_vhost_exit);