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.
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>
54 #define TCM_VHOST_VERSION "v0.1"
55 #define TCM_VHOST_NAMELEN 256
56 #define TCM_VHOST_MAX_CDB_SIZE 32
58 struct vhost_scsi_inflight {
59 /* Wait for the flush operation to finish */
60 struct completion comp;
61 /* Refcount for the inflight reqs */
65 struct tcm_vhost_cmd {
66 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
68 /* virtio-scsi initiator task attribute */
70 /* virtio-scsi initiator data direction */
71 enum dma_data_direction tvc_data_direction;
72 /* Expected data transfer length from virtio-scsi header */
74 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
76 /* The number of scatterlists associated with this cmd */
78 /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */
80 /* Pointer to the SGL formatted memory from virtio-scsi */
81 struct scatterlist *tvc_sgl;
82 /* Pointer to response */
83 struct virtio_scsi_cmd_resp __user *tvc_resp;
84 /* Pointer to vhost_scsi for our device */
85 struct vhost_scsi *tvc_vhost;
86 /* Pointer to vhost_virtqueue for the cmd */
87 struct vhost_virtqueue *tvc_vq;
88 /* Pointer to vhost nexus memory */
89 struct tcm_vhost_nexus *tvc_nexus;
90 /* The TCM I/O descriptor that is accessed via container_of() */
91 struct se_cmd tvc_se_cmd;
92 /* work item used for cmwq dispatch to tcm_vhost_submission_work() */
93 struct work_struct work;
94 /* Copy of the incoming SCSI command descriptor block (CDB) */
95 unsigned char tvc_cdb[TCM_VHOST_MAX_CDB_SIZE];
96 /* Sense buffer that will be mapped into outgoing status */
97 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
98 /* Completed commands list, serviced from vhost worker thread */
99 struct llist_node tvc_completion_list;
100 /* Used to track inflight cmd */
101 struct vhost_scsi_inflight *inflight;
104 struct tcm_vhost_nexus {
105 /* Pointer to TCM session for I_T Nexus */
106 struct se_session *tvn_se_sess;
109 struct tcm_vhost_nacl {
110 /* Binary World Wide unique Port Name for Vhost Initiator port */
112 /* ASCII formatted WWPN for Sas Initiator port */
113 char iport_name[TCM_VHOST_NAMELEN];
114 /* Returned by tcm_vhost_make_nodeacl() */
115 struct se_node_acl se_node_acl;
118 struct tcm_vhost_tpg {
119 /* Vhost port target portal group tag for TCM */
121 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
122 int tv_tpg_port_count;
123 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
124 int tv_tpg_vhost_count;
125 /* list for tcm_vhost_list */
126 struct list_head tv_tpg_list;
127 /* Used to protect access for tpg_nexus */
128 struct mutex tv_tpg_mutex;
129 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
130 struct tcm_vhost_nexus *tpg_nexus;
131 /* Pointer back to tcm_vhost_tport */
132 struct tcm_vhost_tport *tport;
133 /* Returned by tcm_vhost_make_tpg() */
134 struct se_portal_group se_tpg;
135 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
136 struct vhost_scsi *vhost_scsi;
139 struct tcm_vhost_tport {
140 /* SCSI protocol the tport is providing */
142 /* Binary World Wide unique Port Name for Vhost Target port */
144 /* ASCII formatted WWPN for Vhost Target port */
145 char tport_name[TCM_VHOST_NAMELEN];
146 /* Returned by tcm_vhost_make_tport() */
147 struct se_wwn tport_wwn;
150 struct tcm_vhost_evt {
151 /* event to be sent to guest */
152 struct virtio_scsi_event event;
153 /* event list, serviced from vhost worker thread */
154 struct llist_node list;
158 VHOST_SCSI_VQ_CTL = 0,
159 VHOST_SCSI_VQ_EVT = 1,
160 VHOST_SCSI_VQ_IO = 2,
164 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG)
167 #define VHOST_SCSI_MAX_TARGET 256
168 #define VHOST_SCSI_MAX_VQ 128
169 #define VHOST_SCSI_MAX_EVENT 128
171 struct vhost_scsi_virtqueue {
172 struct vhost_virtqueue vq;
174 * Reference counting for inflight reqs, used for flush operation. At
175 * each time, one reference tracks new commands submitted, while we
176 * wait for another one to reach 0.
178 struct vhost_scsi_inflight inflights[2];
180 * Indicate current inflight in use, protected by vq->mutex.
181 * Writers must also take dev mutex and flush under it.
187 /* Protected by vhost_scsi->dev.mutex */
188 struct tcm_vhost_tpg **vs_tpg;
189 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
191 struct vhost_dev dev;
192 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
194 struct vhost_work vs_completion_work; /* cmd completion work item */
195 struct llist_head vs_completion_list; /* cmd completion queue */
197 struct vhost_work vs_event_work; /* evt injection work item */
198 struct llist_head vs_event_list; /* evt injection queue */
200 bool vs_events_missed; /* any missed events, protected by vq->mutex */
201 int vs_events_nr; /* num of pending events, protected by vq->mutex */
204 /* Local pointer to allocated TCM configfs fabric module */
205 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
207 static struct workqueue_struct *tcm_vhost_workqueue;
209 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
210 static DEFINE_MUTEX(tcm_vhost_mutex);
211 static LIST_HEAD(tcm_vhost_list);
213 static int iov_num_pages(struct iovec *iov)
215 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
216 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
219 static void tcm_vhost_done_inflight(struct kref *kref)
221 struct vhost_scsi_inflight *inflight;
223 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
224 complete(&inflight->comp);
227 static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
228 struct vhost_scsi_inflight *old_inflight[])
230 struct vhost_scsi_inflight *new_inflight;
231 struct vhost_virtqueue *vq;
234 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
237 mutex_lock(&vq->mutex);
239 /* store old infight */
240 idx = vs->vqs[i].inflight_idx;
242 old_inflight[i] = &vs->vqs[i].inflights[idx];
244 /* setup new infight */
245 vs->vqs[i].inflight_idx = idx ^ 1;
246 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
247 kref_init(&new_inflight->kref);
248 init_completion(&new_inflight->comp);
250 mutex_unlock(&vq->mutex);
254 static struct vhost_scsi_inflight *
255 tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
257 struct vhost_scsi_inflight *inflight;
258 struct vhost_scsi_virtqueue *svq;
260 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
261 inflight = &svq->inflights[svq->inflight_idx];
262 kref_get(&inflight->kref);
267 static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
269 kref_put(&inflight->kref, tcm_vhost_done_inflight);
272 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
277 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
282 static char *tcm_vhost_get_fabric_name(void)
287 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
289 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
290 struct tcm_vhost_tpg, se_tpg);
291 struct tcm_vhost_tport *tport = tpg->tport;
293 switch (tport->tport_proto_id) {
294 case SCSI_PROTOCOL_SAS:
295 return sas_get_fabric_proto_ident(se_tpg);
296 case SCSI_PROTOCOL_FCP:
297 return fc_get_fabric_proto_ident(se_tpg);
298 case SCSI_PROTOCOL_ISCSI:
299 return iscsi_get_fabric_proto_ident(se_tpg);
301 pr_err("Unknown tport_proto_id: 0x%02x, using"
302 " SAS emulation\n", tport->tport_proto_id);
306 return sas_get_fabric_proto_ident(se_tpg);
309 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
311 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
312 struct tcm_vhost_tpg, se_tpg);
313 struct tcm_vhost_tport *tport = tpg->tport;
315 return &tport->tport_name[0];
318 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
320 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
321 struct tcm_vhost_tpg, se_tpg);
322 return tpg->tport_tpgt;
325 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
331 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
332 struct se_node_acl *se_nacl,
333 struct t10_pr_registration *pr_reg,
337 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
338 struct tcm_vhost_tpg, se_tpg);
339 struct tcm_vhost_tport *tport = tpg->tport;
341 switch (tport->tport_proto_id) {
342 case SCSI_PROTOCOL_SAS:
343 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
345 case SCSI_PROTOCOL_FCP:
346 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
348 case SCSI_PROTOCOL_ISCSI:
349 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
352 pr_err("Unknown tport_proto_id: 0x%02x, using"
353 " SAS emulation\n", tport->tport_proto_id);
357 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
362 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
363 struct se_node_acl *se_nacl,
364 struct t10_pr_registration *pr_reg,
367 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
368 struct tcm_vhost_tpg, se_tpg);
369 struct tcm_vhost_tport *tport = tpg->tport;
371 switch (tport->tport_proto_id) {
372 case SCSI_PROTOCOL_SAS:
373 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
375 case SCSI_PROTOCOL_FCP:
376 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
378 case SCSI_PROTOCOL_ISCSI:
379 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
382 pr_err("Unknown tport_proto_id: 0x%02x, using"
383 " SAS emulation\n", tport->tport_proto_id);
387 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
392 tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
395 char **port_nexus_ptr)
397 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
398 struct tcm_vhost_tpg, se_tpg);
399 struct tcm_vhost_tport *tport = tpg->tport;
401 switch (tport->tport_proto_id) {
402 case SCSI_PROTOCOL_SAS:
403 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
405 case SCSI_PROTOCOL_FCP:
406 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
408 case SCSI_PROTOCOL_ISCSI:
409 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
412 pr_err("Unknown tport_proto_id: 0x%02x, using"
413 " SAS emulation\n", tport->tport_proto_id);
417 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
421 static struct se_node_acl *
422 tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg)
424 struct tcm_vhost_nacl *nacl;
426 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
428 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
432 return &nacl->se_node_acl;
436 tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
437 struct se_node_acl *se_nacl)
439 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
440 struct tcm_vhost_nacl, se_node_acl);
444 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
449 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
454 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
459 static void tcm_vhost_close_session(struct se_session *se_sess)
464 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
469 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
471 /* Go ahead and process the write immediately */
472 target_execute_cmd(se_cmd);
476 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
481 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
486 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
491 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
496 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *cmd)
498 struct vhost_scsi *vs = cmd->tvc_vhost;
500 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
502 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
505 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
507 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
508 struct tcm_vhost_cmd, tvc_se_cmd);
509 vhost_scsi_complete_cmd(cmd);
513 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
515 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
516 struct tcm_vhost_cmd, tvc_se_cmd);
517 vhost_scsi_complete_cmd(cmd);
521 static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
526 static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
532 static struct tcm_vhost_evt *
533 tcm_vhost_allocate_evt(struct vhost_scsi *vs,
534 u32 event, u32 reason)
536 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
537 struct tcm_vhost_evt *evt;
539 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
540 vs->vs_events_missed = true;
544 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
546 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
547 vs->vs_events_missed = true;
551 evt->event.event = event;
552 evt->event.reason = reason;
558 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *cmd)
560 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
562 /* TODO locking against target/backend threads? */
563 transport_generic_free_cmd(se_cmd, 1);
565 if (cmd->tvc_sgl_count) {
567 for (i = 0; i < cmd->tvc_sgl_count; i++)
568 put_page(sg_page(&cmd->tvc_sgl[i]));
573 tcm_vhost_put_inflight(cmd->inflight);
579 tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
581 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
582 struct virtio_scsi_event *event = &evt->event;
583 struct virtio_scsi_event __user *eventp;
587 if (!vq->private_data) {
588 vs->vs_events_missed = true;
593 vhost_disable_notify(&vs->dev, vq);
594 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
595 ARRAY_SIZE(vq->iov), &out, &in,
598 vs->vs_events_missed = true;
601 if (head == vq->num) {
602 if (vhost_enable_notify(&vs->dev, vq))
604 vs->vs_events_missed = true;
608 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
609 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
610 vq->iov[out].iov_len);
611 vs->vs_events_missed = true;
615 if (vs->vs_events_missed) {
616 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
617 vs->vs_events_missed = false;
620 eventp = vq->iov[out].iov_base;
621 ret = __copy_to_user(eventp, event, sizeof(*event));
623 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
625 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
628 static void tcm_vhost_evt_work(struct vhost_work *work)
630 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
632 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
633 struct tcm_vhost_evt *evt;
634 struct llist_node *llnode;
636 mutex_lock(&vq->mutex);
637 llnode = llist_del_all(&vs->vs_event_list);
639 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
640 llnode = llist_next(llnode);
641 tcm_vhost_do_evt_work(vs, evt);
642 tcm_vhost_free_evt(vs, evt);
644 mutex_unlock(&vq->mutex);
647 /* Fill in status and signal that we are done processing this command
649 * This is scheduled in the vhost work queue so we are called with the owner
650 * process mm and can access the vring.
652 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
654 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
656 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
657 struct virtio_scsi_cmd_resp v_rsp;
658 struct tcm_vhost_cmd *cmd;
659 struct llist_node *llnode;
660 struct se_cmd *se_cmd;
663 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
664 llnode = llist_del_all(&vs->vs_completion_list);
666 cmd = llist_entry(llnode, struct tcm_vhost_cmd,
667 tvc_completion_list);
668 llnode = llist_next(llnode);
669 se_cmd = &cmd->tvc_se_cmd;
671 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
672 cmd, se_cmd->residual_count, se_cmd->scsi_status);
674 memset(&v_rsp, 0, sizeof(v_rsp));
675 v_rsp.resid = se_cmd->residual_count;
676 /* TODO is status_qualifier field needed? */
677 v_rsp.status = se_cmd->scsi_status;
678 v_rsp.sense_len = se_cmd->scsi_sense_length;
679 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
681 ret = copy_to_user(cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
682 if (likely(ret == 0)) {
683 struct vhost_scsi_virtqueue *q;
684 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
685 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
687 __set_bit(vq, signal);
689 pr_err("Faulted on virtio_scsi_cmd_resp\n");
691 vhost_scsi_free_cmd(cmd);
695 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
697 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
700 static struct tcm_vhost_cmd *
701 vhost_scsi_allocate_cmd(struct vhost_virtqueue *vq,
702 struct tcm_vhost_tpg *tpg,
703 struct virtio_scsi_cmd_req *v_req,
707 struct tcm_vhost_cmd *cmd;
708 struct tcm_vhost_nexus *tv_nexus;
710 tv_nexus = tpg->tpg_nexus;
712 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
713 return ERR_PTR(-EIO);
716 cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
718 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
719 return ERR_PTR(-ENOMEM);
721 cmd->tvc_tag = v_req->tag;
722 cmd->tvc_task_attr = v_req->task_attr;
723 cmd->tvc_exp_data_len = exp_data_len;
724 cmd->tvc_data_direction = data_direction;
725 cmd->tvc_nexus = tv_nexus;
726 cmd->inflight = tcm_vhost_get_inflight(vq);
732 * Map a user memory range into a scatterlist
734 * Returns the number of scatterlist entries used or -errno on error.
737 vhost_scsi_map_to_sgl(struct scatterlist *sgl,
738 unsigned int sgl_count,
742 unsigned int npages = 0, pages_nr, offset, nbytes;
743 struct scatterlist *sg = sgl;
744 void __user *ptr = iov->iov_base;
745 size_t len = iov->iov_len;
749 pages_nr = iov_num_pages(iov);
750 if (pages_nr > sgl_count)
753 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
757 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
758 /* No pages were pinned */
761 /* Less pages pinned than wanted */
762 if (ret != pages_nr) {
763 for (i = 0; i < ret; i++)
770 offset = (uintptr_t)ptr & ~PAGE_MASK;
771 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
772 sg_set_page(sg, pages[npages], nbytes, offset);
785 vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *cmd,
793 struct scatterlist *sg;
796 * Find out how long sglist needs to be
799 for (i = 0; i < niov; i++)
800 sgl_count += iov_num_pages(&iov[i]);
802 /* TODO overflow checking */
804 sg = kmalloc(sizeof(cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
807 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
809 sg_init_table(sg, sgl_count);
812 cmd->tvc_sgl_count = sgl_count;
814 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
815 for (i = 0; i < niov; i++) {
816 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
818 for (i = 0; i < cmd->tvc_sgl_count; i++)
819 put_page(sg_page(&cmd->tvc_sgl[i]));
822 cmd->tvc_sgl_count = 0;
832 static void tcm_vhost_submission_work(struct work_struct *work)
834 struct tcm_vhost_cmd *cmd =
835 container_of(work, struct tcm_vhost_cmd, work);
836 struct tcm_vhost_nexus *tv_nexus;
837 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
838 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
839 int rc, sg_no_bidi = 0;
841 if (cmd->tvc_sgl_count) {
842 sg_ptr = cmd->tvc_sgl;
843 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
845 if (se_cmd->se_cmd_flags & SCF_BIDI) {
853 tv_nexus = cmd->tvc_nexus;
855 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
856 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
857 cmd->tvc_lun, cmd->tvc_exp_data_len,
858 cmd->tvc_task_attr, cmd->tvc_data_direction,
859 0, sg_ptr, cmd->tvc_sgl_count,
860 sg_bidi_ptr, sg_no_bidi);
862 transport_send_check_condition_and_sense(se_cmd,
863 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
864 transport_generic_free_cmd(se_cmd, 0);
869 vhost_scsi_send_bad_target(struct vhost_scsi *vs,
870 struct vhost_virtqueue *vq,
871 int head, unsigned out)
873 struct virtio_scsi_cmd_resp __user *resp;
874 struct virtio_scsi_cmd_resp rsp;
877 memset(&rsp, 0, sizeof(rsp));
878 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
879 resp = vq->iov[out].iov_base;
880 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
882 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
884 pr_err("Faulted on virtio_scsi_cmd_resp\n");
888 vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
890 struct tcm_vhost_tpg **vs_tpg;
891 struct virtio_scsi_cmd_req v_req;
892 struct tcm_vhost_tpg *tpg;
893 struct tcm_vhost_cmd *cmd;
894 u32 exp_data_len, data_first, data_num, data_direction;
900 * We can handle the vq only after the endpoint is setup by calling the
901 * VHOST_SCSI_SET_ENDPOINT ioctl.
903 * TODO: Check that we are running from vhost_worker which acts
904 * as read-side critical section for vhost kind of RCU.
905 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
907 vs_tpg = rcu_dereference_check(vq->private_data, 1);
911 mutex_lock(&vq->mutex);
912 vhost_disable_notify(&vs->dev, vq);
915 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
916 ARRAY_SIZE(vq->iov), &out, &in,
918 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
920 /* On error, stop handling until the next kick. */
921 if (unlikely(head < 0))
923 /* Nothing new? Wait for eventfd to tell us they refilled. */
924 if (head == vq->num) {
925 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
926 vhost_disable_notify(&vs->dev, vq);
932 /* FIXME: BIDI operation */
933 if (out == 1 && in == 1) {
934 data_direction = DMA_NONE;
937 } else if (out == 1 && in > 1) {
938 data_direction = DMA_FROM_DEVICE;
939 data_first = out + 1;
941 } else if (out > 1 && in == 1) {
942 data_direction = DMA_TO_DEVICE;
946 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
952 * Check for a sane resp buffer so we can report errors to
955 if (unlikely(vq->iov[out].iov_len !=
956 sizeof(struct virtio_scsi_cmd_resp))) {
957 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
958 " bytes\n", vq->iov[out].iov_len);
962 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
963 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
964 " bytes\n", vq->iov[0].iov_len);
967 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
968 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
969 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
972 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
976 /* Extract the tpgt */
977 target = v_req.lun[1];
978 tpg = ACCESS_ONCE(vs_tpg[target]);
980 /* Target does not exist, fail the request */
981 if (unlikely(!tpg)) {
982 vhost_scsi_send_bad_target(vs, vq, head, out);
987 for (i = 0; i < data_num; i++)
988 exp_data_len += vq->iov[data_first + i].iov_len;
990 cmd = vhost_scsi_allocate_cmd(vq, tpg, &v_req,
991 exp_data_len, data_direction);
993 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
997 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
998 ": %d\n", cmd, exp_data_len, data_direction);
1000 cmd->tvc_vhost = vs;
1002 cmd->tvc_resp = vq->iov[out].iov_base;
1005 * Copy in the recieved CDB descriptor into cmd->tvc_cdb
1006 * that will be used by tcm_vhost_new_cmd_map() and down into
1007 * target_setup_cmd_from_cdb()
1009 memcpy(cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
1011 * Check that the recieved CDB size does not exceeded our
1012 * hardcoded max for tcm_vhost
1014 /* TODO what if cdb was too small for varlen cdb header? */
1015 if (unlikely(scsi_command_size(cmd->tvc_cdb) >
1016 TCM_VHOST_MAX_CDB_SIZE)) {
1017 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1018 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1019 scsi_command_size(cmd->tvc_cdb),
1020 TCM_VHOST_MAX_CDB_SIZE);
1023 cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1025 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1026 cmd->tvc_cdb[0], cmd->tvc_lun);
1028 if (data_direction != DMA_NONE) {
1029 ret = vhost_scsi_map_iov_to_sgl(cmd,
1030 &vq->iov[data_first], data_num,
1031 data_direction == DMA_TO_DEVICE);
1032 if (unlikely(ret)) {
1033 vq_err(vq, "Failed to map iov to sgl\n");
1039 * Save the descriptor from vhost_get_vq_desc() to be used to
1040 * complete the virtio-scsi request in TCM callback context via
1041 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1043 cmd->tvc_vq_desc = head;
1045 * Dispatch tv_cmd descriptor for cmwq execution in process
1046 * context provided by tcm_vhost_workqueue. This also ensures
1047 * tv_cmd is executed on the same kworker CPU as this vhost
1048 * thread to gain positive L2 cache locality effects..
1050 INIT_WORK(&cmd->work, tcm_vhost_submission_work);
1051 queue_work(tcm_vhost_workqueue, &cmd->work);
1054 mutex_unlock(&vq->mutex);
1058 vhost_scsi_free_cmd(cmd);
1060 vhost_scsi_send_bad_target(vs, vq, head, out);
1061 mutex_unlock(&vq->mutex);
1064 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1066 pr_debug("%s: The handling func for control queue.\n", __func__);
1070 tcm_vhost_send_evt(struct vhost_scsi *vs,
1071 struct tcm_vhost_tpg *tpg,
1076 struct tcm_vhost_evt *evt;
1078 evt = tcm_vhost_allocate_evt(vs, event, reason);
1083 /* TODO: share lun setup code with virtio-scsi.ko */
1085 * Note: evt->event is zeroed when we allocate it and
1086 * lun[4-7] need to be zero according to virtio-scsi spec.
1088 evt->event.lun[0] = 0x01;
1089 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
1090 if (lun->unpacked_lun >= 256)
1091 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1092 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1095 llist_add(&evt->list, &vs->vs_event_list);
1096 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1099 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1101 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1103 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1105 mutex_lock(&vq->mutex);
1106 if (!vq->private_data)
1109 if (vs->vs_events_missed)
1110 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1112 mutex_unlock(&vq->mutex);
1115 static void vhost_scsi_handle_kick(struct vhost_work *work)
1117 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1119 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1121 vhost_scsi_handle_vq(vs, vq);
1124 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1126 vhost_poll_flush(&vs->vqs[index].vq.poll);
1129 /* Callers must hold dev mutex */
1130 static void vhost_scsi_flush(struct vhost_scsi *vs)
1132 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
1135 /* Init new inflight and remember the old inflight */
1136 tcm_vhost_init_inflight(vs, old_inflight);
1139 * The inflight->kref was initialized to 1. We decrement it here to
1140 * indicate the start of the flush operation so that it will reach 0
1141 * when all the reqs are finished.
1143 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1144 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1146 /* Flush both the vhost poll and vhost work */
1147 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1148 vhost_scsi_flush_vq(vs, i);
1149 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
1150 vhost_work_flush(&vs->dev, &vs->vs_event_work);
1152 /* Wait for all reqs issued before the flush to be finished */
1153 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1154 wait_for_completion(&old_inflight[i]->comp);
1158 * Called from vhost_scsi_ioctl() context to walk the list of available
1159 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
1161 * The lock nesting rule is:
1162 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1165 vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1166 struct vhost_scsi_target *t)
1168 struct tcm_vhost_tport *tv_tport;
1169 struct tcm_vhost_tpg *tpg;
1170 struct tcm_vhost_tpg **vs_tpg;
1171 struct vhost_virtqueue *vq;
1172 int index, ret, i, len;
1175 mutex_lock(&tcm_vhost_mutex);
1176 mutex_lock(&vs->dev.mutex);
1178 /* Verify that ring has been setup correctly. */
1179 for (index = 0; index < vs->dev.nvqs; ++index) {
1180 /* Verify that ring has been setup correctly. */
1181 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1187 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1188 vs_tpg = kzalloc(len, GFP_KERNEL);
1194 memcpy(vs_tpg, vs->vs_tpg, len);
1196 list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
1197 mutex_lock(&tpg->tv_tpg_mutex);
1198 if (!tpg->tpg_nexus) {
1199 mutex_unlock(&tpg->tv_tpg_mutex);
1202 if (tpg->tv_tpg_vhost_count != 0) {
1203 mutex_unlock(&tpg->tv_tpg_mutex);
1206 tv_tport = tpg->tport;
1208 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1209 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
1211 mutex_unlock(&tpg->tv_tpg_mutex);
1215 tpg->tv_tpg_vhost_count++;
1216 tpg->vhost_scsi = vs;
1217 vs_tpg[tpg->tport_tpgt] = tpg;
1218 smp_mb__after_atomic_inc();
1221 mutex_unlock(&tpg->tv_tpg_mutex);
1225 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1226 sizeof(vs->vs_vhost_wwpn));
1227 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1228 vq = &vs->vqs[i].vq;
1229 /* Flushing the vhost_work acts as synchronize_rcu */
1230 mutex_lock(&vq->mutex);
1231 rcu_assign_pointer(vq->private_data, vs_tpg);
1232 vhost_init_used(vq);
1233 mutex_unlock(&vq->mutex);
1241 * Act as synchronize_rcu to make sure access to
1242 * old vs->vs_tpg is finished.
1244 vhost_scsi_flush(vs);
1246 vs->vs_tpg = vs_tpg;
1249 mutex_unlock(&vs->dev.mutex);
1250 mutex_unlock(&tcm_vhost_mutex);
1255 vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1256 struct vhost_scsi_target *t)
1258 struct tcm_vhost_tport *tv_tport;
1259 struct tcm_vhost_tpg *tpg;
1260 struct vhost_virtqueue *vq;
1265 mutex_lock(&tcm_vhost_mutex);
1266 mutex_lock(&vs->dev.mutex);
1267 /* Verify that ring has been setup correctly. */
1268 for (index = 0; index < vs->dev.nvqs; ++index) {
1269 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1280 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1282 tpg = vs->vs_tpg[target];
1286 mutex_lock(&tpg->tv_tpg_mutex);
1287 tv_tport = tpg->tport;
1293 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1294 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
1295 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1296 tv_tport->tport_name, tpg->tport_tpgt,
1297 t->vhost_wwpn, t->vhost_tpgt);
1301 tpg->tv_tpg_vhost_count--;
1302 tpg->vhost_scsi = NULL;
1303 vs->vs_tpg[target] = NULL;
1305 mutex_unlock(&tpg->tv_tpg_mutex);
1308 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1309 vq = &vs->vqs[i].vq;
1310 /* Flushing the vhost_work acts as synchronize_rcu */
1311 mutex_lock(&vq->mutex);
1312 rcu_assign_pointer(vq->private_data, NULL);
1313 mutex_unlock(&vq->mutex);
1317 * Act as synchronize_rcu to make sure access to
1318 * old vs->vs_tpg is finished.
1320 vhost_scsi_flush(vs);
1323 WARN_ON(vs->vs_events_nr);
1324 mutex_unlock(&vs->dev.mutex);
1325 mutex_unlock(&tcm_vhost_mutex);
1329 mutex_unlock(&tpg->tv_tpg_mutex);
1331 mutex_unlock(&vs->dev.mutex);
1332 mutex_unlock(&tcm_vhost_mutex);
1336 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1338 if (features & ~VHOST_SCSI_FEATURES)
1341 mutex_lock(&vs->dev.mutex);
1342 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1343 !vhost_log_access_ok(&vs->dev)) {
1344 mutex_unlock(&vs->dev.mutex);
1347 vs->dev.acked_features = features;
1349 vhost_scsi_flush(vs);
1350 mutex_unlock(&vs->dev.mutex);
1354 static int vhost_scsi_open(struct inode *inode, struct file *f)
1356 struct vhost_scsi *vs;
1357 struct vhost_virtqueue **vqs;
1360 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
1364 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1370 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1371 vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
1373 vs->vs_events_nr = 0;
1374 vs->vs_events_missed = false;
1376 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1377 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1378 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1379 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
1380 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
1381 vqs[i] = &vs->vqs[i].vq;
1382 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
1384 r = vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
1386 tcm_vhost_init_inflight(vs, NULL);
1394 f->private_data = vs;
1398 static int vhost_scsi_release(struct inode *inode, struct file *f)
1400 struct vhost_scsi *vs = f->private_data;
1401 struct vhost_scsi_target t;
1403 mutex_lock(&vs->dev.mutex);
1404 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1405 mutex_unlock(&vs->dev.mutex);
1406 vhost_scsi_clear_endpoint(vs, &t);
1407 vhost_dev_stop(&vs->dev);
1408 vhost_dev_cleanup(&vs->dev, false);
1409 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1410 vhost_scsi_flush(vs);
1417 vhost_scsi_ioctl(struct file *f,
1421 struct vhost_scsi *vs = f->private_data;
1422 struct vhost_scsi_target backend;
1423 void __user *argp = (void __user *)arg;
1424 u64 __user *featurep = argp;
1425 u32 __user *eventsp = argp;
1428 int r, abi_version = VHOST_SCSI_ABI_VERSION;
1429 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1432 case VHOST_SCSI_SET_ENDPOINT:
1433 if (copy_from_user(&backend, argp, sizeof backend))
1435 if (backend.reserved != 0)
1438 return vhost_scsi_set_endpoint(vs, &backend);
1439 case VHOST_SCSI_CLEAR_ENDPOINT:
1440 if (copy_from_user(&backend, argp, sizeof backend))
1442 if (backend.reserved != 0)
1445 return vhost_scsi_clear_endpoint(vs, &backend);
1446 case VHOST_SCSI_GET_ABI_VERSION:
1447 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1450 case VHOST_SCSI_SET_EVENTS_MISSED:
1451 if (get_user(events_missed, eventsp))
1453 mutex_lock(&vq->mutex);
1454 vs->vs_events_missed = events_missed;
1455 mutex_unlock(&vq->mutex);
1457 case VHOST_SCSI_GET_EVENTS_MISSED:
1458 mutex_lock(&vq->mutex);
1459 events_missed = vs->vs_events_missed;
1460 mutex_unlock(&vq->mutex);
1461 if (put_user(events_missed, eventsp))
1464 case VHOST_GET_FEATURES:
1465 features = VHOST_SCSI_FEATURES;
1466 if (copy_to_user(featurep, &features, sizeof features))
1469 case VHOST_SET_FEATURES:
1470 if (copy_from_user(&features, featurep, sizeof features))
1472 return vhost_scsi_set_features(vs, features);
1474 mutex_lock(&vs->dev.mutex);
1475 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1476 /* TODO: flush backend after dev ioctl. */
1477 if (r == -ENOIOCTLCMD)
1478 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1479 mutex_unlock(&vs->dev.mutex);
1484 #ifdef CONFIG_COMPAT
1485 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1488 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1492 static const struct file_operations vhost_scsi_fops = {
1493 .owner = THIS_MODULE,
1494 .release = vhost_scsi_release,
1495 .unlocked_ioctl = vhost_scsi_ioctl,
1496 #ifdef CONFIG_COMPAT
1497 .compat_ioctl = vhost_scsi_compat_ioctl,
1499 .open = vhost_scsi_open,
1500 .llseek = noop_llseek,
1503 static struct miscdevice vhost_scsi_misc = {
1509 static int __init vhost_scsi_register(void)
1511 return misc_register(&vhost_scsi_misc);
1514 static int vhost_scsi_deregister(void)
1516 return misc_deregister(&vhost_scsi_misc);
1519 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1521 switch (tport->tport_proto_id) {
1522 case SCSI_PROTOCOL_SAS:
1524 case SCSI_PROTOCOL_FCP:
1526 case SCSI_PROTOCOL_ISCSI:
1536 tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1537 struct se_lun *lun, bool plug)
1540 struct vhost_scsi *vs = tpg->vhost_scsi;
1541 struct vhost_virtqueue *vq;
1547 mutex_lock(&vs->dev.mutex);
1548 if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1549 mutex_unlock(&vs->dev.mutex);
1554 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1556 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1558 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1559 mutex_lock(&vq->mutex);
1560 tcm_vhost_send_evt(vs, tpg, lun,
1561 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1562 mutex_unlock(&vq->mutex);
1563 mutex_unlock(&vs->dev.mutex);
1566 static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1568 tcm_vhost_do_plug(tpg, lun, true);
1571 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1573 tcm_vhost_do_plug(tpg, lun, false);
1576 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1579 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1580 struct tcm_vhost_tpg, se_tpg);
1582 mutex_lock(&tcm_vhost_mutex);
1584 mutex_lock(&tpg->tv_tpg_mutex);
1585 tpg->tv_tpg_port_count++;
1586 mutex_unlock(&tpg->tv_tpg_mutex);
1588 tcm_vhost_hotplug(tpg, lun);
1590 mutex_unlock(&tcm_vhost_mutex);
1595 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1598 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1599 struct tcm_vhost_tpg, se_tpg);
1601 mutex_lock(&tcm_vhost_mutex);
1603 mutex_lock(&tpg->tv_tpg_mutex);
1604 tpg->tv_tpg_port_count--;
1605 mutex_unlock(&tpg->tv_tpg_mutex);
1607 tcm_vhost_hotunplug(tpg, lun);
1609 mutex_unlock(&tcm_vhost_mutex);
1612 static struct se_node_acl *
1613 tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
1614 struct config_group *group,
1617 struct se_node_acl *se_nacl, *se_nacl_new;
1618 struct tcm_vhost_nacl *nacl;
1622 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1623 return ERR_PTR(-EINVAL); */
1624 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1626 return ERR_PTR(-ENOMEM);
1630 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1631 * when converting a NodeACL from demo mode -> explict
1633 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1635 if (IS_ERR(se_nacl)) {
1636 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1640 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1642 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1643 nacl->iport_wwpn = wwpn;
1648 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1650 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1651 struct tcm_vhost_nacl, se_node_acl);
1652 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1656 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
1659 struct se_portal_group *se_tpg;
1660 struct tcm_vhost_nexus *tv_nexus;
1662 mutex_lock(&tpg->tv_tpg_mutex);
1663 if (tpg->tpg_nexus) {
1664 mutex_unlock(&tpg->tv_tpg_mutex);
1665 pr_debug("tpg->tpg_nexus already exists\n");
1668 se_tpg = &tpg->se_tpg;
1670 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1672 mutex_unlock(&tpg->tv_tpg_mutex);
1673 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1677 * Initialize the struct se_session pointer
1679 tv_nexus->tvn_se_sess = transport_init_session();
1680 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1681 mutex_unlock(&tpg->tv_tpg_mutex);
1686 * Since we are running in 'demo mode' this call with generate a
1687 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1688 * the SCSI Initiator port name of the passed configfs group 'name'.
1690 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1691 se_tpg, (unsigned char *)name);
1692 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1693 mutex_unlock(&tpg->tv_tpg_mutex);
1694 pr_debug("core_tpg_check_initiator_node_acl() failed"
1696 transport_free_session(tv_nexus->tvn_se_sess);
1701 * Now register the TCM vhost virtual I_T Nexus as active with the
1702 * call to __transport_register_session()
1704 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1705 tv_nexus->tvn_se_sess, tv_nexus);
1706 tpg->tpg_nexus = tv_nexus;
1708 mutex_unlock(&tpg->tv_tpg_mutex);
1712 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1714 struct se_session *se_sess;
1715 struct tcm_vhost_nexus *tv_nexus;
1717 mutex_lock(&tpg->tv_tpg_mutex);
1718 tv_nexus = tpg->tpg_nexus;
1720 mutex_unlock(&tpg->tv_tpg_mutex);
1724 se_sess = tv_nexus->tvn_se_sess;
1726 mutex_unlock(&tpg->tv_tpg_mutex);
1730 if (tpg->tv_tpg_port_count != 0) {
1731 mutex_unlock(&tpg->tv_tpg_mutex);
1732 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1733 " active TPG port count: %d\n",
1734 tpg->tv_tpg_port_count);
1738 if (tpg->tv_tpg_vhost_count != 0) {
1739 mutex_unlock(&tpg->tv_tpg_mutex);
1740 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1741 " active TPG vhost count: %d\n",
1742 tpg->tv_tpg_vhost_count);
1746 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1747 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1748 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1750 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1752 transport_deregister_session(tv_nexus->tvn_se_sess);
1753 tpg->tpg_nexus = NULL;
1754 mutex_unlock(&tpg->tv_tpg_mutex);
1760 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1763 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1764 struct tcm_vhost_tpg, se_tpg);
1765 struct tcm_vhost_nexus *tv_nexus;
1768 mutex_lock(&tpg->tv_tpg_mutex);
1769 tv_nexus = tpg->tpg_nexus;
1771 mutex_unlock(&tpg->tv_tpg_mutex);
1774 ret = snprintf(page, PAGE_SIZE, "%s\n",
1775 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1776 mutex_unlock(&tpg->tv_tpg_mutex);
1781 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1785 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1786 struct tcm_vhost_tpg, se_tpg);
1787 struct tcm_vhost_tport *tport_wwn = tpg->tport;
1788 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1791 * Shutdown the active I_T nexus if 'NULL' is passed..
1793 if (!strncmp(page, "NULL", 4)) {
1794 ret = tcm_vhost_drop_nexus(tpg);
1795 return (!ret) ? count : ret;
1798 * Otherwise make sure the passed virtual Initiator port WWN matches
1799 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1800 * tcm_vhost_make_nexus().
1802 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1803 pr_err("Emulated NAA Sas Address: %s, exceeds"
1804 " max: %d\n", page, TCM_VHOST_NAMELEN);
1807 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1809 ptr = strstr(i_port, "naa.");
1811 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1812 pr_err("Passed SAS Initiator Port %s does not"
1813 " match target port protoid: %s\n", i_port,
1814 tcm_vhost_dump_proto_id(tport_wwn));
1817 port_ptr = &i_port[0];
1820 ptr = strstr(i_port, "fc.");
1822 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1823 pr_err("Passed FCP Initiator Port %s does not"
1824 " match target port protoid: %s\n", i_port,
1825 tcm_vhost_dump_proto_id(tport_wwn));
1828 port_ptr = &i_port[3]; /* Skip over "fc." */
1831 ptr = strstr(i_port, "iqn.");
1833 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1834 pr_err("Passed iSCSI Initiator Port %s does not"
1835 " match target port protoid: %s\n", i_port,
1836 tcm_vhost_dump_proto_id(tport_wwn));
1839 port_ptr = &i_port[0];
1842 pr_err("Unable to locate prefix for emulated Initiator Port:"
1846 * Clear any trailing newline for the NAA WWN
1849 if (i_port[strlen(i_port)-1] == '\n')
1850 i_port[strlen(i_port)-1] = '\0';
1852 ret = tcm_vhost_make_nexus(tpg, port_ptr);
1859 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1861 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1862 &tcm_vhost_tpg_nexus.attr,
1866 static struct se_portal_group *
1867 tcm_vhost_make_tpg(struct se_wwn *wwn,
1868 struct config_group *group,
1871 struct tcm_vhost_tport *tport = container_of(wwn,
1872 struct tcm_vhost_tport, tport_wwn);
1874 struct tcm_vhost_tpg *tpg;
1878 if (strstr(name, "tpgt_") != name)
1879 return ERR_PTR(-EINVAL);
1880 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1881 return ERR_PTR(-EINVAL);
1883 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1885 pr_err("Unable to allocate struct tcm_vhost_tpg");
1886 return ERR_PTR(-ENOMEM);
1888 mutex_init(&tpg->tv_tpg_mutex);
1889 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1891 tpg->tport_tpgt = tpgt;
1893 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1894 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1899 mutex_lock(&tcm_vhost_mutex);
1900 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1901 mutex_unlock(&tcm_vhost_mutex);
1903 return &tpg->se_tpg;
1906 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1908 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1909 struct tcm_vhost_tpg, se_tpg);
1911 mutex_lock(&tcm_vhost_mutex);
1912 list_del(&tpg->tv_tpg_list);
1913 mutex_unlock(&tcm_vhost_mutex);
1915 * Release the virtual I_T Nexus for this vhost TPG
1917 tcm_vhost_drop_nexus(tpg);
1919 * Deregister the se_tpg from TCM..
1921 core_tpg_deregister(se_tpg);
1925 static struct se_wwn *
1926 tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1927 struct config_group *group,
1930 struct tcm_vhost_tport *tport;
1935 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1936 return ERR_PTR(-EINVAL); */
1938 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1940 pr_err("Unable to allocate struct tcm_vhost_tport");
1941 return ERR_PTR(-ENOMEM);
1943 tport->tport_wwpn = wwpn;
1945 * Determine the emulated Protocol Identifier and Target Port Name
1946 * based on the incoming configfs directory name.
1948 ptr = strstr(name, "naa.");
1950 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1953 ptr = strstr(name, "fc.");
1955 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1956 off = 3; /* Skip over "fc." */
1959 ptr = strstr(name, "iqn.");
1961 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1965 pr_err("Unable to locate prefix for emulated Target Port:"
1968 return ERR_PTR(-EINVAL);
1971 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1972 pr_err("Emulated %s Address: %s, exceeds"
1973 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1976 return ERR_PTR(-EINVAL);
1978 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1980 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1981 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1983 return &tport->tport_wwn;
1986 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1988 struct tcm_vhost_tport *tport = container_of(wwn,
1989 struct tcm_vhost_tport, tport_wwn);
1991 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1992 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1999 tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
2002 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
2003 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2004 utsname()->machine);
2007 TF_WWN_ATTR_RO(tcm_vhost, version);
2009 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
2010 &tcm_vhost_wwn_version.attr,
2014 static struct target_core_fabric_ops tcm_vhost_ops = {
2015 .get_fabric_name = tcm_vhost_get_fabric_name,
2016 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
2017 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
2018 .tpg_get_tag = tcm_vhost_get_tag,
2019 .tpg_get_default_depth = tcm_vhost_get_default_depth,
2020 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
2021 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
2022 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
2023 .tpg_check_demo_mode = tcm_vhost_check_true,
2024 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
2025 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
2026 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
2027 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
2028 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
2029 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
2030 .release_cmd = tcm_vhost_release_cmd,
2031 .shutdown_session = tcm_vhost_shutdown_session,
2032 .close_session = tcm_vhost_close_session,
2033 .sess_get_index = tcm_vhost_sess_get_index,
2034 .sess_get_initiator_sid = NULL,
2035 .write_pending = tcm_vhost_write_pending,
2036 .write_pending_status = tcm_vhost_write_pending_status,
2037 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
2038 .get_task_tag = tcm_vhost_get_task_tag,
2039 .get_cmd_state = tcm_vhost_get_cmd_state,
2040 .queue_data_in = tcm_vhost_queue_data_in,
2041 .queue_status = tcm_vhost_queue_status,
2042 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
2044 * Setup callers for generic logic in target_core_fabric_configfs.c
2046 .fabric_make_wwn = tcm_vhost_make_tport,
2047 .fabric_drop_wwn = tcm_vhost_drop_tport,
2048 .fabric_make_tpg = tcm_vhost_make_tpg,
2049 .fabric_drop_tpg = tcm_vhost_drop_tpg,
2050 .fabric_post_link = tcm_vhost_port_link,
2051 .fabric_pre_unlink = tcm_vhost_port_unlink,
2052 .fabric_make_np = NULL,
2053 .fabric_drop_np = NULL,
2054 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
2055 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
2058 static int tcm_vhost_register_configfs(void)
2060 struct target_fabric_configfs *fabric;
2063 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2064 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2065 utsname()->machine);
2067 * Register the top level struct config_item_type with TCM core
2069 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
2070 if (IS_ERR(fabric)) {
2071 pr_err("target_fabric_configfs_init() failed\n");
2072 return PTR_ERR(fabric);
2075 * Setup fabric->tf_ops from our local tcm_vhost_ops
2077 fabric->tf_ops = tcm_vhost_ops;
2079 * Setup default attribute lists for various fabric->tf_cit_tmpl
2081 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
2082 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
2083 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
2084 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
2085 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
2086 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2087 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2088 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2089 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
2091 * Register the fabric for use within TCM
2093 ret = target_fabric_configfs_register(fabric);
2095 pr_err("target_fabric_configfs_register() failed"
2096 " for TCM_VHOST\n");
2100 * Setup our local pointer to *fabric
2102 tcm_vhost_fabric_configfs = fabric;
2103 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2107 static void tcm_vhost_deregister_configfs(void)
2109 if (!tcm_vhost_fabric_configfs)
2112 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2113 tcm_vhost_fabric_configfs = NULL;
2114 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2117 static int __init tcm_vhost_init(void)
2121 * Use our own dedicated workqueue for submitting I/O into
2122 * target core to avoid contention within system_wq.
2124 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2125 if (!tcm_vhost_workqueue)
2128 ret = vhost_scsi_register();
2130 goto out_destroy_workqueue;
2132 ret = tcm_vhost_register_configfs();
2134 goto out_vhost_scsi_deregister;
2138 out_vhost_scsi_deregister:
2139 vhost_scsi_deregister();
2140 out_destroy_workqueue:
2141 destroy_workqueue(tcm_vhost_workqueue);
2146 static void tcm_vhost_exit(void)
2148 tcm_vhost_deregister_configfs();
2149 vhost_scsi_deregister();
2150 destroy_workqueue(tcm_vhost_workqueue);
2153 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2154 MODULE_ALIAS("tcm_vhost");
2155 MODULE_LICENSE("GPL");
2156 module_init(tcm_vhost_init);
2157 module_exit(tcm_vhost_exit);