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>
55 #define TCM_VHOST_VERSION "v0.1"
56 #define TCM_VHOST_NAMELEN 256
57 #define TCM_VHOST_MAX_CDB_SIZE 32
59 struct vhost_scsi_inflight {
60 /* Wait for the flush operation to finish */
61 struct completion comp;
62 /* Refcount for the inflight reqs */
66 struct tcm_vhost_cmd {
67 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
69 /* virtio-scsi initiator task attribute */
71 /* virtio-scsi initiator data direction */
72 enum dma_data_direction tvc_data_direction;
73 /* Expected data transfer length from virtio-scsi header */
75 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
77 /* The number of scatterlists associated with this cmd */
79 /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */
81 /* Pointer to the SGL formatted memory from virtio-scsi */
82 struct scatterlist *tvc_sgl;
83 /* Pointer to response */
84 struct virtio_scsi_cmd_resp __user *tvc_resp;
85 /* Pointer to vhost_scsi for our device */
86 struct vhost_scsi *tvc_vhost;
87 /* Pointer to vhost_virtqueue for the cmd */
88 struct vhost_virtqueue *tvc_vq;
89 /* Pointer to vhost nexus memory */
90 struct tcm_vhost_nexus *tvc_nexus;
91 /* The TCM I/O descriptor that is accessed via container_of() */
92 struct se_cmd tvc_se_cmd;
93 /* work item used for cmwq dispatch to tcm_vhost_submission_work() */
94 struct work_struct work;
95 /* Copy of the incoming SCSI command descriptor block (CDB) */
96 unsigned char tvc_cdb[TCM_VHOST_MAX_CDB_SIZE];
97 /* Sense buffer that will be mapped into outgoing status */
98 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
99 /* Completed commands list, serviced from vhost worker thread */
100 struct llist_node tvc_completion_list;
101 /* Used to track inflight cmd */
102 struct vhost_scsi_inflight *inflight;
105 struct tcm_vhost_nexus {
106 /* Pointer to TCM session for I_T Nexus */
107 struct se_session *tvn_se_sess;
110 struct tcm_vhost_nacl {
111 /* Binary World Wide unique Port Name for Vhost Initiator port */
113 /* ASCII formatted WWPN for Sas Initiator port */
114 char iport_name[TCM_VHOST_NAMELEN];
115 /* Returned by tcm_vhost_make_nodeacl() */
116 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 * VIRTIO_RING_F_EVENT_IDX seems broken. Not sure the bug is in
167 * kernel but disabling it helps.
168 * TODO: debug and remove the workaround.
171 VHOST_SCSI_FEATURES = (VHOST_FEATURES & (~VIRTIO_RING_F_EVENT_IDX)) |
172 (1ULL << VIRTIO_SCSI_F_HOTPLUG)
175 #define VHOST_SCSI_MAX_TARGET 256
176 #define VHOST_SCSI_MAX_VQ 128
177 #define VHOST_SCSI_MAX_EVENT 128
179 struct vhost_scsi_virtqueue {
180 struct vhost_virtqueue vq;
182 * Reference counting for inflight reqs, used for flush operation. At
183 * each time, one reference tracks new commands submitted, while we
184 * wait for another one to reach 0.
186 struct vhost_scsi_inflight inflights[2];
188 * Indicate current inflight in use, protected by vq->mutex.
189 * Writers must also take dev mutex and flush under it.
195 /* Protected by vhost_scsi->dev.mutex */
196 struct tcm_vhost_tpg **vs_tpg;
197 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
199 struct vhost_dev dev;
200 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
202 struct vhost_work vs_completion_work; /* cmd completion work item */
203 struct llist_head vs_completion_list; /* cmd completion queue */
205 struct vhost_work vs_event_work; /* evt injection work item */
206 struct llist_head vs_event_list; /* evt injection queue */
208 bool vs_events_missed; /* any missed events, protected by vq->mutex */
209 int vs_events_nr; /* num of pending events, protected by vq->mutex */
212 /* Local pointer to allocated TCM configfs fabric module */
213 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
215 static struct workqueue_struct *tcm_vhost_workqueue;
217 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
218 static DEFINE_MUTEX(tcm_vhost_mutex);
219 static LIST_HEAD(tcm_vhost_list);
221 static int iov_num_pages(struct iovec *iov)
223 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
224 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
227 void tcm_vhost_done_inflight(struct kref *kref)
229 struct vhost_scsi_inflight *inflight;
231 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
232 complete(&inflight->comp);
235 static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
236 struct vhost_scsi_inflight *old_inflight[])
238 struct vhost_scsi_inflight *new_inflight;
239 struct vhost_virtqueue *vq;
242 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
245 mutex_lock(&vq->mutex);
247 /* store old infight */
248 idx = vs->vqs[i].inflight_idx;
250 old_inflight[i] = &vs->vqs[i].inflights[idx];
252 /* setup new infight */
253 vs->vqs[i].inflight_idx = idx ^ 1;
254 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
255 kref_init(&new_inflight->kref);
256 init_completion(&new_inflight->comp);
258 mutex_unlock(&vq->mutex);
262 static struct vhost_scsi_inflight *
263 tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
265 struct vhost_scsi_inflight *inflight;
266 struct vhost_scsi_virtqueue *svq;
268 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
269 inflight = &svq->inflights[svq->inflight_idx];
270 kref_get(&inflight->kref);
275 static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
277 kref_put(&inflight->kref, tcm_vhost_done_inflight);
280 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
285 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
290 static char *tcm_vhost_get_fabric_name(void)
295 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
297 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
298 struct tcm_vhost_tpg, se_tpg);
299 struct tcm_vhost_tport *tport = tpg->tport;
301 switch (tport->tport_proto_id) {
302 case SCSI_PROTOCOL_SAS:
303 return sas_get_fabric_proto_ident(se_tpg);
304 case SCSI_PROTOCOL_FCP:
305 return fc_get_fabric_proto_ident(se_tpg);
306 case SCSI_PROTOCOL_ISCSI:
307 return iscsi_get_fabric_proto_ident(se_tpg);
309 pr_err("Unknown tport_proto_id: 0x%02x, using"
310 " SAS emulation\n", tport->tport_proto_id);
314 return sas_get_fabric_proto_ident(se_tpg);
317 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
319 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
320 struct tcm_vhost_tpg, se_tpg);
321 struct tcm_vhost_tport *tport = tpg->tport;
323 return &tport->tport_name[0];
326 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
328 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
329 struct tcm_vhost_tpg, se_tpg);
330 return tpg->tport_tpgt;
333 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
338 static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
339 struct se_node_acl *se_nacl,
340 struct t10_pr_registration *pr_reg,
344 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
345 struct tcm_vhost_tpg, se_tpg);
346 struct tcm_vhost_tport *tport = tpg->tport;
348 switch (tport->tport_proto_id) {
349 case SCSI_PROTOCOL_SAS:
350 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
352 case SCSI_PROTOCOL_FCP:
353 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
355 case SCSI_PROTOCOL_ISCSI:
356 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
359 pr_err("Unknown tport_proto_id: 0x%02x, using"
360 " SAS emulation\n", tport->tport_proto_id);
364 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
368 static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
369 struct se_node_acl *se_nacl,
370 struct t10_pr_registration *pr_reg,
373 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
374 struct tcm_vhost_tpg, se_tpg);
375 struct tcm_vhost_tport *tport = tpg->tport;
377 switch (tport->tport_proto_id) {
378 case SCSI_PROTOCOL_SAS:
379 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
381 case SCSI_PROTOCOL_FCP:
382 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
384 case SCSI_PROTOCOL_ISCSI:
385 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
388 pr_err("Unknown tport_proto_id: 0x%02x, using"
389 " SAS emulation\n", tport->tport_proto_id);
393 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
397 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
400 char **port_nexus_ptr)
402 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
403 struct tcm_vhost_tpg, se_tpg);
404 struct tcm_vhost_tport *tport = tpg->tport;
406 switch (tport->tport_proto_id) {
407 case SCSI_PROTOCOL_SAS:
408 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
410 case SCSI_PROTOCOL_FCP:
411 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
413 case SCSI_PROTOCOL_ISCSI:
414 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
417 pr_err("Unknown tport_proto_id: 0x%02x, using"
418 " SAS emulation\n", tport->tport_proto_id);
422 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
426 static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
427 struct se_portal_group *se_tpg)
429 struct tcm_vhost_nacl *nacl;
431 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
433 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
437 return &nacl->se_node_acl;
440 static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
441 struct se_node_acl *se_nacl)
443 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
444 struct tcm_vhost_nacl, se_node_acl);
448 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
453 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
458 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
463 static void tcm_vhost_close_session(struct se_session *se_sess)
468 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
473 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
475 /* Go ahead and process the write immediately */
476 target_execute_cmd(se_cmd);
480 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
485 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
490 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
495 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
500 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
502 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
504 llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
506 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
509 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
511 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
512 struct tcm_vhost_cmd, tvc_se_cmd);
513 vhost_scsi_complete_cmd(tv_cmd);
517 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
519 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
520 struct tcm_vhost_cmd, tvc_se_cmd);
521 vhost_scsi_complete_cmd(tv_cmd);
525 static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
530 static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
536 static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs,
537 u32 event, u32 reason)
539 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
540 struct tcm_vhost_evt *evt;
542 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
543 vs->vs_events_missed = true;
547 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
549 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
550 vs->vs_events_missed = true;
554 evt->event.event = event;
555 evt->event.reason = reason;
561 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
563 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
565 /* TODO locking against target/backend threads? */
566 transport_generic_free_cmd(se_cmd, 1);
568 if (tv_cmd->tvc_sgl_count) {
570 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
571 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
573 kfree(tv_cmd->tvc_sgl);
576 tcm_vhost_put_inflight(tv_cmd->inflight);
581 static void tcm_vhost_do_evt_work(struct vhost_scsi *vs,
582 struct tcm_vhost_evt *evt)
584 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
585 struct virtio_scsi_event *event = &evt->event;
586 struct virtio_scsi_event __user *eventp;
590 if (!vq->private_data) {
591 vs->vs_events_missed = true;
596 vhost_disable_notify(&vs->dev, vq);
597 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
598 ARRAY_SIZE(vq->iov), &out, &in,
601 vs->vs_events_missed = true;
604 if (head == vq->num) {
605 if (vhost_enable_notify(&vs->dev, vq))
607 vs->vs_events_missed = true;
611 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
612 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
613 vq->iov[out].iov_len);
614 vs->vs_events_missed = true;
618 if (vs->vs_events_missed) {
619 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
620 vs->vs_events_missed = false;
623 eventp = vq->iov[out].iov_base;
624 ret = __copy_to_user(eventp, event, sizeof(*event));
626 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
628 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
631 static void tcm_vhost_evt_work(struct vhost_work *work)
633 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
635 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
636 struct tcm_vhost_evt *evt;
637 struct llist_node *llnode;
639 mutex_lock(&vq->mutex);
640 llnode = llist_del_all(&vs->vs_event_list);
642 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
643 llnode = llist_next(llnode);
644 tcm_vhost_do_evt_work(vs, evt);
645 tcm_vhost_free_evt(vs, evt);
647 mutex_unlock(&vq->mutex);
650 /* Fill in status and signal that we are done processing this command
652 * This is scheduled in the vhost work queue so we are called with the owner
653 * process mm and can access the vring.
655 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
657 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
659 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
660 struct virtio_scsi_cmd_resp v_rsp;
661 struct tcm_vhost_cmd *tv_cmd;
662 struct llist_node *llnode;
663 struct se_cmd *se_cmd;
666 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
667 llnode = llist_del_all(&vs->vs_completion_list);
669 tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
670 tvc_completion_list);
671 llnode = llist_next(llnode);
672 se_cmd = &tv_cmd->tvc_se_cmd;
674 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
675 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
677 memset(&v_rsp, 0, sizeof(v_rsp));
678 v_rsp.resid = se_cmd->residual_count;
679 /* TODO is status_qualifier field needed? */
680 v_rsp.status = se_cmd->scsi_status;
681 v_rsp.sense_len = se_cmd->scsi_sense_length;
682 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
684 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
685 if (likely(ret == 0)) {
686 struct vhost_scsi_virtqueue *q;
687 vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0);
688 q = container_of(tv_cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
690 __set_bit(vq, signal);
692 pr_err("Faulted on virtio_scsi_cmd_resp\n");
694 vhost_scsi_free_cmd(tv_cmd);
698 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
700 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
703 static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
704 struct vhost_virtqueue *vq,
705 struct tcm_vhost_tpg *tv_tpg,
706 struct virtio_scsi_cmd_req *v_req,
710 struct tcm_vhost_cmd *tv_cmd;
711 struct tcm_vhost_nexus *tv_nexus;
713 tv_nexus = tv_tpg->tpg_nexus;
715 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
716 return ERR_PTR(-EIO);
719 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
721 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
722 return ERR_PTR(-ENOMEM);
724 tv_cmd->tvc_tag = v_req->tag;
725 tv_cmd->tvc_task_attr = v_req->task_attr;
726 tv_cmd->tvc_exp_data_len = exp_data_len;
727 tv_cmd->tvc_data_direction = data_direction;
728 tv_cmd->tvc_nexus = tv_nexus;
729 tv_cmd->inflight = tcm_vhost_get_inflight(vq);
735 * Map a user memory range into a scatterlist
737 * Returns the number of scatterlist entries used or -errno on error.
739 static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
740 unsigned int sgl_count, struct iovec *iov, int write)
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);
784 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
785 struct iovec *iov, unsigned int niov, int write)
790 struct scatterlist *sg;
793 * Find out how long sglist needs to be
796 for (i = 0; i < niov; i++)
797 sgl_count += iov_num_pages(&iov[i]);
799 /* TODO overflow checking */
801 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
804 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
806 sg_init_table(sg, sgl_count);
808 tv_cmd->tvc_sgl = sg;
809 tv_cmd->tvc_sgl_count = sgl_count;
811 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
812 for (i = 0; i < niov; i++) {
813 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
815 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
816 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
817 kfree(tv_cmd->tvc_sgl);
818 tv_cmd->tvc_sgl = NULL;
819 tv_cmd->tvc_sgl_count = 0;
829 static void tcm_vhost_submission_work(struct work_struct *work)
831 struct tcm_vhost_cmd *tv_cmd =
832 container_of(work, struct tcm_vhost_cmd, work);
833 struct tcm_vhost_nexus *tv_nexus;
834 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
835 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
836 int rc, sg_no_bidi = 0;
838 if (tv_cmd->tvc_sgl_count) {
839 sg_ptr = tv_cmd->tvc_sgl;
840 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
842 if (se_cmd->se_cmd_flags & SCF_BIDI) {
850 tv_nexus = tv_cmd->tvc_nexus;
852 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
853 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
854 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
855 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
856 0, sg_ptr, tv_cmd->tvc_sgl_count,
857 sg_bidi_ptr, sg_no_bidi);
859 transport_send_check_condition_and_sense(se_cmd,
860 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
861 transport_generic_free_cmd(se_cmd, 0);
865 static void vhost_scsi_send_bad_target(struct vhost_scsi *vs,
866 struct vhost_virtqueue *vq, int head, unsigned out)
868 struct virtio_scsi_cmd_resp __user *resp;
869 struct virtio_scsi_cmd_resp rsp;
872 memset(&rsp, 0, sizeof(rsp));
873 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
874 resp = vq->iov[out].iov_base;
875 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
877 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
879 pr_err("Faulted on virtio_scsi_cmd_resp\n");
882 static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
883 struct vhost_virtqueue *vq)
885 struct tcm_vhost_tpg **vs_tpg;
886 struct virtio_scsi_cmd_req v_req;
887 struct tcm_vhost_tpg *tv_tpg;
888 struct tcm_vhost_cmd *tv_cmd;
889 u32 exp_data_len, data_first, data_num, data_direction;
895 * We can handle the vq only after the endpoint is setup by calling the
896 * VHOST_SCSI_SET_ENDPOINT ioctl.
898 * TODO: Check that we are running from vhost_worker which acts
899 * as read-side critical section for vhost kind of RCU.
900 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
902 vs_tpg = rcu_dereference_check(vq->private_data, 1);
906 mutex_lock(&vq->mutex);
907 vhost_disable_notify(&vs->dev, vq);
910 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
911 ARRAY_SIZE(vq->iov), &out, &in,
913 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
915 /* On error, stop handling until the next kick. */
916 if (unlikely(head < 0))
918 /* Nothing new? Wait for eventfd to tell us they refilled. */
919 if (head == vq->num) {
920 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
921 vhost_disable_notify(&vs->dev, vq);
927 /* FIXME: BIDI operation */
928 if (out == 1 && in == 1) {
929 data_direction = DMA_NONE;
932 } else if (out == 1 && in > 1) {
933 data_direction = DMA_FROM_DEVICE;
934 data_first = out + 1;
936 } else if (out > 1 && in == 1) {
937 data_direction = DMA_TO_DEVICE;
941 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
947 * Check for a sane resp buffer so we can report errors to
950 if (unlikely(vq->iov[out].iov_len !=
951 sizeof(struct virtio_scsi_cmd_resp))) {
952 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
953 " bytes\n", vq->iov[out].iov_len);
957 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
958 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
959 " bytes\n", vq->iov[0].iov_len);
962 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
963 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
964 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
967 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
971 /* Extract the tpgt */
972 target = v_req.lun[1];
973 tv_tpg = ACCESS_ONCE(vs_tpg[target]);
975 /* Target does not exist, fail the request */
976 if (unlikely(!tv_tpg)) {
977 vhost_scsi_send_bad_target(vs, vq, head, out);
982 for (i = 0; i < data_num; i++)
983 exp_data_len += vq->iov[data_first + i].iov_len;
985 tv_cmd = vhost_scsi_allocate_cmd(vq, tv_tpg, &v_req,
986 exp_data_len, data_direction);
987 if (IS_ERR(tv_cmd)) {
988 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
992 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
993 ": %d\n", tv_cmd, exp_data_len, data_direction);
995 tv_cmd->tvc_vhost = vs;
997 tv_cmd->tvc_resp = vq->iov[out].iov_base;
1000 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
1001 * that will be used by tcm_vhost_new_cmd_map() and down into
1002 * target_setup_cmd_from_cdb()
1004 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
1006 * Check that the recieved CDB size does not exceeded our
1007 * hardcoded max for tcm_vhost
1009 /* TODO what if cdb was too small for varlen cdb header? */
1010 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
1011 TCM_VHOST_MAX_CDB_SIZE)) {
1012 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1013 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1014 scsi_command_size(tv_cmd->tvc_cdb),
1015 TCM_VHOST_MAX_CDB_SIZE);
1018 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1020 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1021 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
1023 if (data_direction != DMA_NONE) {
1024 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
1025 &vq->iov[data_first], data_num,
1026 data_direction == DMA_TO_DEVICE);
1027 if (unlikely(ret)) {
1028 vq_err(vq, "Failed to map iov to sgl\n");
1034 * Save the descriptor from vhost_get_vq_desc() to be used to
1035 * complete the virtio-scsi request in TCM callback context via
1036 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1038 tv_cmd->tvc_vq_desc = head;
1040 * Dispatch tv_cmd descriptor for cmwq execution in process
1041 * context provided by tcm_vhost_workqueue. This also ensures
1042 * tv_cmd is executed on the same kworker CPU as this vhost
1043 * thread to gain positive L2 cache locality effects..
1045 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
1046 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
1049 mutex_unlock(&vq->mutex);
1053 vhost_scsi_free_cmd(tv_cmd);
1055 vhost_scsi_send_bad_target(vs, vq, head, out);
1056 mutex_unlock(&vq->mutex);
1059 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1061 pr_debug("%s: The handling func for control queue.\n", __func__);
1064 static void tcm_vhost_send_evt(struct vhost_scsi *vs, struct tcm_vhost_tpg *tpg,
1065 struct se_lun *lun, u32 event, u32 reason)
1067 struct tcm_vhost_evt *evt;
1069 evt = tcm_vhost_allocate_evt(vs, event, reason);
1074 /* TODO: share lun setup code with virtio-scsi.ko */
1076 * Note: evt->event is zeroed when we allocate it and
1077 * lun[4-7] need to be zero according to virtio-scsi spec.
1079 evt->event.lun[0] = 0x01;
1080 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
1081 if (lun->unpacked_lun >= 256)
1082 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1083 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1086 llist_add(&evt->list, &vs->vs_event_list);
1087 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1090 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1092 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1094 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1096 mutex_lock(&vq->mutex);
1097 if (!vq->private_data)
1100 if (vs->vs_events_missed)
1101 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1103 mutex_unlock(&vq->mutex);
1106 static void vhost_scsi_handle_kick(struct vhost_work *work)
1108 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1110 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1112 vhost_scsi_handle_vq(vs, vq);
1115 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1117 vhost_poll_flush(&vs->vqs[index].vq.poll);
1120 /* Callers must hold dev mutex */
1121 static void vhost_scsi_flush(struct vhost_scsi *vs)
1123 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
1126 /* Init new inflight and remember the old inflight */
1127 tcm_vhost_init_inflight(vs, old_inflight);
1130 * The inflight->kref was initialized to 1. We decrement it here to
1131 * indicate the start of the flush operation so that it will reach 0
1132 * when all the reqs are finished.
1134 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1135 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1137 /* Flush both the vhost poll and vhost work */
1138 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1139 vhost_scsi_flush_vq(vs, i);
1140 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
1141 vhost_work_flush(&vs->dev, &vs->vs_event_work);
1143 /* Wait for all reqs issued before the flush to be finished */
1144 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1145 wait_for_completion(&old_inflight[i]->comp);
1149 * Called from vhost_scsi_ioctl() context to walk the list of available
1150 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
1152 * The lock nesting rule is:
1153 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1155 static int vhost_scsi_set_endpoint(
1156 struct vhost_scsi *vs,
1157 struct vhost_scsi_target *t)
1159 struct tcm_vhost_tport *tv_tport;
1160 struct tcm_vhost_tpg *tv_tpg;
1161 struct tcm_vhost_tpg **vs_tpg;
1162 struct vhost_virtqueue *vq;
1163 int index, ret, i, len;
1166 mutex_lock(&tcm_vhost_mutex);
1167 mutex_lock(&vs->dev.mutex);
1169 /* Verify that ring has been setup correctly. */
1170 for (index = 0; index < vs->dev.nvqs; ++index) {
1171 /* Verify that ring has been setup correctly. */
1172 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1178 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1179 vs_tpg = kzalloc(len, GFP_KERNEL);
1185 memcpy(vs_tpg, vs->vs_tpg, len);
1187 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
1188 mutex_lock(&tv_tpg->tv_tpg_mutex);
1189 if (!tv_tpg->tpg_nexus) {
1190 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1193 if (tv_tpg->tv_tpg_vhost_count != 0) {
1194 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1197 tv_tport = tv_tpg->tport;
1199 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1200 if (vs->vs_tpg && vs->vs_tpg[tv_tpg->tport_tpgt]) {
1202 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1206 tv_tpg->tv_tpg_vhost_count++;
1207 tv_tpg->vhost_scsi = vs;
1208 vs_tpg[tv_tpg->tport_tpgt] = tv_tpg;
1209 smp_mb__after_atomic_inc();
1212 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1216 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1217 sizeof(vs->vs_vhost_wwpn));
1218 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1219 vq = &vs->vqs[i].vq;
1220 /* Flushing the vhost_work acts as synchronize_rcu */
1221 mutex_lock(&vq->mutex);
1222 rcu_assign_pointer(vq->private_data, vs_tpg);
1223 vhost_init_used(vq);
1224 mutex_unlock(&vq->mutex);
1232 * Act as synchronize_rcu to make sure access to
1233 * old vs->vs_tpg is finished.
1235 vhost_scsi_flush(vs);
1237 vs->vs_tpg = vs_tpg;
1240 mutex_unlock(&vs->dev.mutex);
1241 mutex_unlock(&tcm_vhost_mutex);
1245 static int vhost_scsi_clear_endpoint(
1246 struct vhost_scsi *vs,
1247 struct vhost_scsi_target *t)
1249 struct tcm_vhost_tport *tv_tport;
1250 struct tcm_vhost_tpg *tv_tpg;
1251 struct vhost_virtqueue *vq;
1256 mutex_lock(&tcm_vhost_mutex);
1257 mutex_lock(&vs->dev.mutex);
1258 /* Verify that ring has been setup correctly. */
1259 for (index = 0; index < vs->dev.nvqs; ++index) {
1260 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1271 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1273 tv_tpg = vs->vs_tpg[target];
1277 mutex_lock(&tv_tpg->tv_tpg_mutex);
1278 tv_tport = tv_tpg->tport;
1284 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1285 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
1286 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1287 tv_tport->tport_name, tv_tpg->tport_tpgt,
1288 t->vhost_wwpn, t->vhost_tpgt);
1292 tv_tpg->tv_tpg_vhost_count--;
1293 tv_tpg->vhost_scsi = NULL;
1294 vs->vs_tpg[target] = NULL;
1296 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1299 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1300 vq = &vs->vqs[i].vq;
1301 /* Flushing the vhost_work acts as synchronize_rcu */
1302 mutex_lock(&vq->mutex);
1303 rcu_assign_pointer(vq->private_data, NULL);
1304 mutex_unlock(&vq->mutex);
1308 * Act as synchronize_rcu to make sure access to
1309 * old vs->vs_tpg is finished.
1311 vhost_scsi_flush(vs);
1314 WARN_ON(vs->vs_events_nr);
1315 mutex_unlock(&vs->dev.mutex);
1316 mutex_unlock(&tcm_vhost_mutex);
1320 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1322 mutex_unlock(&vs->dev.mutex);
1323 mutex_unlock(&tcm_vhost_mutex);
1327 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1329 if (features & ~VHOST_SCSI_FEATURES)
1332 mutex_lock(&vs->dev.mutex);
1333 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1334 !vhost_log_access_ok(&vs->dev)) {
1335 mutex_unlock(&vs->dev.mutex);
1338 vs->dev.acked_features = features;
1340 vhost_scsi_flush(vs);
1341 mutex_unlock(&vs->dev.mutex);
1345 static int vhost_scsi_open(struct inode *inode, struct file *f)
1347 struct vhost_scsi *s;
1348 struct vhost_virtqueue **vqs;
1351 s = kzalloc(sizeof(*s), GFP_KERNEL);
1355 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1361 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
1362 vhost_work_init(&s->vs_event_work, tcm_vhost_evt_work);
1364 s->vs_events_nr = 0;
1365 s->vs_events_missed = false;
1367 vqs[VHOST_SCSI_VQ_CTL] = &s->vqs[VHOST_SCSI_VQ_CTL].vq;
1368 vqs[VHOST_SCSI_VQ_EVT] = &s->vqs[VHOST_SCSI_VQ_EVT].vq;
1369 s->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1370 s->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
1371 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
1372 vqs[i] = &s->vqs[i].vq;
1373 s->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
1375 r = vhost_dev_init(&s->dev, vqs, VHOST_SCSI_MAX_VQ);
1377 tcm_vhost_init_inflight(s, NULL);
1385 f->private_data = s;
1389 static int vhost_scsi_release(struct inode *inode, struct file *f)
1391 struct vhost_scsi *s = f->private_data;
1392 struct vhost_scsi_target t;
1394 mutex_lock(&s->dev.mutex);
1395 memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1396 mutex_unlock(&s->dev.mutex);
1397 vhost_scsi_clear_endpoint(s, &t);
1398 vhost_dev_stop(&s->dev);
1399 vhost_dev_cleanup(&s->dev, false);
1400 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1401 vhost_scsi_flush(s);
1407 static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
1410 struct vhost_scsi *vs = f->private_data;
1411 struct vhost_scsi_target backend;
1412 void __user *argp = (void __user *)arg;
1413 u64 __user *featurep = argp;
1414 u32 __user *eventsp = argp;
1417 int r, abi_version = VHOST_SCSI_ABI_VERSION;
1418 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1421 case VHOST_SCSI_SET_ENDPOINT:
1422 if (copy_from_user(&backend, argp, sizeof backend))
1424 if (backend.reserved != 0)
1427 return vhost_scsi_set_endpoint(vs, &backend);
1428 case VHOST_SCSI_CLEAR_ENDPOINT:
1429 if (copy_from_user(&backend, argp, sizeof backend))
1431 if (backend.reserved != 0)
1434 return vhost_scsi_clear_endpoint(vs, &backend);
1435 case VHOST_SCSI_GET_ABI_VERSION:
1436 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1439 case VHOST_SCSI_SET_EVENTS_MISSED:
1440 if (get_user(events_missed, eventsp))
1442 mutex_lock(&vq->mutex);
1443 vs->vs_events_missed = events_missed;
1444 mutex_unlock(&vq->mutex);
1446 case VHOST_SCSI_GET_EVENTS_MISSED:
1447 mutex_lock(&vq->mutex);
1448 events_missed = vs->vs_events_missed;
1449 mutex_unlock(&vq->mutex);
1450 if (put_user(events_missed, eventsp))
1453 case VHOST_GET_FEATURES:
1454 features = VHOST_SCSI_FEATURES;
1455 if (copy_to_user(featurep, &features, sizeof features))
1458 case VHOST_SET_FEATURES:
1459 if (copy_from_user(&features, featurep, sizeof features))
1461 return vhost_scsi_set_features(vs, features);
1463 mutex_lock(&vs->dev.mutex);
1464 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1465 /* TODO: flush backend after dev ioctl. */
1466 if (r == -ENOIOCTLCMD)
1467 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1468 mutex_unlock(&vs->dev.mutex);
1473 #ifdef CONFIG_COMPAT
1474 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1477 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1481 static const struct file_operations vhost_scsi_fops = {
1482 .owner = THIS_MODULE,
1483 .release = vhost_scsi_release,
1484 .unlocked_ioctl = vhost_scsi_ioctl,
1485 #ifdef CONFIG_COMPAT
1486 .compat_ioctl = vhost_scsi_compat_ioctl,
1488 .open = vhost_scsi_open,
1489 .llseek = noop_llseek,
1492 static struct miscdevice vhost_scsi_misc = {
1498 static int __init vhost_scsi_register(void)
1500 return misc_register(&vhost_scsi_misc);
1503 static int vhost_scsi_deregister(void)
1505 return misc_deregister(&vhost_scsi_misc);
1508 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1510 switch (tport->tport_proto_id) {
1511 case SCSI_PROTOCOL_SAS:
1513 case SCSI_PROTOCOL_FCP:
1515 case SCSI_PROTOCOL_ISCSI:
1524 static void tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1525 struct se_lun *lun, bool plug)
1528 struct vhost_scsi *vs = tpg->vhost_scsi;
1529 struct vhost_virtqueue *vq;
1535 mutex_lock(&vs->dev.mutex);
1536 if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1537 mutex_unlock(&vs->dev.mutex);
1542 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1544 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1546 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1547 mutex_lock(&vq->mutex);
1548 tcm_vhost_send_evt(vs, tpg, lun,
1549 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1550 mutex_unlock(&vq->mutex);
1551 mutex_unlock(&vs->dev.mutex);
1554 static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1556 tcm_vhost_do_plug(tpg, lun, true);
1559 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1561 tcm_vhost_do_plug(tpg, lun, false);
1564 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1567 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1568 struct tcm_vhost_tpg, se_tpg);
1570 mutex_lock(&tcm_vhost_mutex);
1572 mutex_lock(&tv_tpg->tv_tpg_mutex);
1573 tv_tpg->tv_tpg_port_count++;
1574 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1576 tcm_vhost_hotplug(tv_tpg, lun);
1578 mutex_unlock(&tcm_vhost_mutex);
1583 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1586 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1587 struct tcm_vhost_tpg, se_tpg);
1589 mutex_lock(&tcm_vhost_mutex);
1591 mutex_lock(&tv_tpg->tv_tpg_mutex);
1592 tv_tpg->tv_tpg_port_count--;
1593 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1595 tcm_vhost_hotunplug(tv_tpg, lun);
1597 mutex_unlock(&tcm_vhost_mutex);
1600 static struct se_node_acl *tcm_vhost_make_nodeacl(
1601 struct se_portal_group *se_tpg,
1602 struct config_group *group,
1605 struct se_node_acl *se_nacl, *se_nacl_new;
1606 struct tcm_vhost_nacl *nacl;
1610 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1611 return ERR_PTR(-EINVAL); */
1612 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1614 return ERR_PTR(-ENOMEM);
1618 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1619 * when converting a NodeACL from demo mode -> explict
1621 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1623 if (IS_ERR(se_nacl)) {
1624 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1628 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1630 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1631 nacl->iport_wwpn = wwpn;
1636 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1638 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1639 struct tcm_vhost_nacl, se_node_acl);
1640 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1644 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1647 struct se_portal_group *se_tpg;
1648 struct tcm_vhost_nexus *tv_nexus;
1650 mutex_lock(&tv_tpg->tv_tpg_mutex);
1651 if (tv_tpg->tpg_nexus) {
1652 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1653 pr_debug("tv_tpg->tpg_nexus already exists\n");
1656 se_tpg = &tv_tpg->se_tpg;
1658 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1660 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1661 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1665 * Initialize the struct se_session pointer
1667 tv_nexus->tvn_se_sess = transport_init_session();
1668 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1669 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1674 * Since we are running in 'demo mode' this call with generate a
1675 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1676 * the SCSI Initiator port name of the passed configfs group 'name'.
1678 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1679 se_tpg, (unsigned char *)name);
1680 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1681 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1682 pr_debug("core_tpg_check_initiator_node_acl() failed"
1684 transport_free_session(tv_nexus->tvn_se_sess);
1689 * Now register the TCM vhost virtual I_T Nexus as active with the
1690 * call to __transport_register_session()
1692 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1693 tv_nexus->tvn_se_sess, tv_nexus);
1694 tv_tpg->tpg_nexus = tv_nexus;
1696 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1700 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1702 struct se_session *se_sess;
1703 struct tcm_vhost_nexus *tv_nexus;
1705 mutex_lock(&tpg->tv_tpg_mutex);
1706 tv_nexus = tpg->tpg_nexus;
1708 mutex_unlock(&tpg->tv_tpg_mutex);
1712 se_sess = tv_nexus->tvn_se_sess;
1714 mutex_unlock(&tpg->tv_tpg_mutex);
1718 if (tpg->tv_tpg_port_count != 0) {
1719 mutex_unlock(&tpg->tv_tpg_mutex);
1720 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1721 " active TPG port count: %d\n",
1722 tpg->tv_tpg_port_count);
1726 if (tpg->tv_tpg_vhost_count != 0) {
1727 mutex_unlock(&tpg->tv_tpg_mutex);
1728 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1729 " active TPG vhost count: %d\n",
1730 tpg->tv_tpg_vhost_count);
1734 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1735 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1736 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1738 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1740 transport_deregister_session(tv_nexus->tvn_se_sess);
1741 tpg->tpg_nexus = NULL;
1742 mutex_unlock(&tpg->tv_tpg_mutex);
1748 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1751 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1752 struct tcm_vhost_tpg, se_tpg);
1753 struct tcm_vhost_nexus *tv_nexus;
1756 mutex_lock(&tv_tpg->tv_tpg_mutex);
1757 tv_nexus = tv_tpg->tpg_nexus;
1759 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1762 ret = snprintf(page, PAGE_SIZE, "%s\n",
1763 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1764 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1769 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1773 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1774 struct tcm_vhost_tpg, se_tpg);
1775 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1776 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1779 * Shutdown the active I_T nexus if 'NULL' is passed..
1781 if (!strncmp(page, "NULL", 4)) {
1782 ret = tcm_vhost_drop_nexus(tv_tpg);
1783 return (!ret) ? count : ret;
1786 * Otherwise make sure the passed virtual Initiator port WWN matches
1787 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1788 * tcm_vhost_make_nexus().
1790 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1791 pr_err("Emulated NAA Sas Address: %s, exceeds"
1792 " max: %d\n", page, TCM_VHOST_NAMELEN);
1795 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1797 ptr = strstr(i_port, "naa.");
1799 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1800 pr_err("Passed SAS Initiator Port %s does not"
1801 " match target port protoid: %s\n", i_port,
1802 tcm_vhost_dump_proto_id(tport_wwn));
1805 port_ptr = &i_port[0];
1808 ptr = strstr(i_port, "fc.");
1810 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1811 pr_err("Passed FCP Initiator Port %s does not"
1812 " match target port protoid: %s\n", i_port,
1813 tcm_vhost_dump_proto_id(tport_wwn));
1816 port_ptr = &i_port[3]; /* Skip over "fc." */
1819 ptr = strstr(i_port, "iqn.");
1821 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1822 pr_err("Passed iSCSI 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 pr_err("Unable to locate prefix for emulated Initiator Port:"
1834 * Clear any trailing newline for the NAA WWN
1837 if (i_port[strlen(i_port)-1] == '\n')
1838 i_port[strlen(i_port)-1] = '\0';
1840 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1847 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1849 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1850 &tcm_vhost_tpg_nexus.attr,
1854 static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
1855 struct config_group *group,
1858 struct tcm_vhost_tport *tport = container_of(wwn,
1859 struct tcm_vhost_tport, tport_wwn);
1861 struct tcm_vhost_tpg *tpg;
1865 if (strstr(name, "tpgt_") != name)
1866 return ERR_PTR(-EINVAL);
1867 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1868 return ERR_PTR(-EINVAL);
1870 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1872 pr_err("Unable to allocate struct tcm_vhost_tpg");
1873 return ERR_PTR(-ENOMEM);
1875 mutex_init(&tpg->tv_tpg_mutex);
1876 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1878 tpg->tport_tpgt = tpgt;
1880 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1881 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1886 mutex_lock(&tcm_vhost_mutex);
1887 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1888 mutex_unlock(&tcm_vhost_mutex);
1890 return &tpg->se_tpg;
1893 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1895 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1896 struct tcm_vhost_tpg, se_tpg);
1898 mutex_lock(&tcm_vhost_mutex);
1899 list_del(&tpg->tv_tpg_list);
1900 mutex_unlock(&tcm_vhost_mutex);
1902 * Release the virtual I_T Nexus for this vhost TPG
1904 tcm_vhost_drop_nexus(tpg);
1906 * Deregister the se_tpg from TCM..
1908 core_tpg_deregister(se_tpg);
1912 static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1913 struct config_group *group,
1916 struct tcm_vhost_tport *tport;
1921 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1922 return ERR_PTR(-EINVAL); */
1924 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1926 pr_err("Unable to allocate struct tcm_vhost_tport");
1927 return ERR_PTR(-ENOMEM);
1929 tport->tport_wwpn = wwpn;
1931 * Determine the emulated Protocol Identifier and Target Port Name
1932 * based on the incoming configfs directory name.
1934 ptr = strstr(name, "naa.");
1936 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1939 ptr = strstr(name, "fc.");
1941 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1942 off = 3; /* Skip over "fc." */
1945 ptr = strstr(name, "iqn.");
1947 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1951 pr_err("Unable to locate prefix for emulated Target Port:"
1954 return ERR_PTR(-EINVAL);
1957 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1958 pr_err("Emulated %s Address: %s, exceeds"
1959 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1962 return ERR_PTR(-EINVAL);
1964 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1966 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1967 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1969 return &tport->tport_wwn;
1972 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1974 struct tcm_vhost_tport *tport = container_of(wwn,
1975 struct tcm_vhost_tport, tport_wwn);
1977 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1978 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1984 static ssize_t tcm_vhost_wwn_show_attr_version(
1985 struct target_fabric_configfs *tf,
1988 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1989 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1990 utsname()->machine);
1993 TF_WWN_ATTR_RO(tcm_vhost, version);
1995 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1996 &tcm_vhost_wwn_version.attr,
2000 static struct target_core_fabric_ops tcm_vhost_ops = {
2001 .get_fabric_name = tcm_vhost_get_fabric_name,
2002 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
2003 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
2004 .tpg_get_tag = tcm_vhost_get_tag,
2005 .tpg_get_default_depth = tcm_vhost_get_default_depth,
2006 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
2007 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
2008 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
2009 .tpg_check_demo_mode = tcm_vhost_check_true,
2010 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
2011 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
2012 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
2013 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
2014 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
2015 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
2016 .release_cmd = tcm_vhost_release_cmd,
2017 .shutdown_session = tcm_vhost_shutdown_session,
2018 .close_session = tcm_vhost_close_session,
2019 .sess_get_index = tcm_vhost_sess_get_index,
2020 .sess_get_initiator_sid = NULL,
2021 .write_pending = tcm_vhost_write_pending,
2022 .write_pending_status = tcm_vhost_write_pending_status,
2023 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
2024 .get_task_tag = tcm_vhost_get_task_tag,
2025 .get_cmd_state = tcm_vhost_get_cmd_state,
2026 .queue_data_in = tcm_vhost_queue_data_in,
2027 .queue_status = tcm_vhost_queue_status,
2028 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
2030 * Setup callers for generic logic in target_core_fabric_configfs.c
2032 .fabric_make_wwn = tcm_vhost_make_tport,
2033 .fabric_drop_wwn = tcm_vhost_drop_tport,
2034 .fabric_make_tpg = tcm_vhost_make_tpg,
2035 .fabric_drop_tpg = tcm_vhost_drop_tpg,
2036 .fabric_post_link = tcm_vhost_port_link,
2037 .fabric_pre_unlink = tcm_vhost_port_unlink,
2038 .fabric_make_np = NULL,
2039 .fabric_drop_np = NULL,
2040 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
2041 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
2044 static int tcm_vhost_register_configfs(void)
2046 struct target_fabric_configfs *fabric;
2049 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2050 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2051 utsname()->machine);
2053 * Register the top level struct config_item_type with TCM core
2055 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
2056 if (IS_ERR(fabric)) {
2057 pr_err("target_fabric_configfs_init() failed\n");
2058 return PTR_ERR(fabric);
2061 * Setup fabric->tf_ops from our local tcm_vhost_ops
2063 fabric->tf_ops = tcm_vhost_ops;
2065 * Setup default attribute lists for various fabric->tf_cit_tmpl
2067 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
2068 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
2069 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
2070 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
2071 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
2072 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2073 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2074 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2075 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
2077 * Register the fabric for use within TCM
2079 ret = target_fabric_configfs_register(fabric);
2081 pr_err("target_fabric_configfs_register() failed"
2082 " for TCM_VHOST\n");
2086 * Setup our local pointer to *fabric
2088 tcm_vhost_fabric_configfs = fabric;
2089 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2093 static void tcm_vhost_deregister_configfs(void)
2095 if (!tcm_vhost_fabric_configfs)
2098 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2099 tcm_vhost_fabric_configfs = NULL;
2100 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2103 static int __init tcm_vhost_init(void)
2107 * Use our own dedicated workqueue for submitting I/O into
2108 * target core to avoid contention within system_wq.
2110 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2111 if (!tcm_vhost_workqueue)
2114 ret = vhost_scsi_register();
2116 goto out_destroy_workqueue;
2118 ret = tcm_vhost_register_configfs();
2120 goto out_vhost_scsi_deregister;
2124 out_vhost_scsi_deregister:
2125 vhost_scsi_deregister();
2126 out_destroy_workqueue:
2127 destroy_workqueue(tcm_vhost_workqueue);
2132 static void tcm_vhost_exit(void)
2134 tcm_vhost_deregister_configfs();
2135 vhost_scsi_deregister();
2136 destroy_workqueue(tcm_vhost_workqueue);
2139 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2140 MODULE_ALIAS("tcm_vhost");
2141 MODULE_LICENSE("GPL");
2142 module_init(tcm_vhost_init);
2143 module_exit(tcm_vhost_exit);