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;
119 struct tcm_vhost_tpg {
120 /* Vhost port target portal group tag for TCM */
122 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
123 int tv_tpg_port_count;
124 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
125 int tv_tpg_vhost_count;
126 /* list for tcm_vhost_list */
127 struct list_head tv_tpg_list;
128 /* Used to protect access for tpg_nexus */
129 struct mutex tv_tpg_mutex;
130 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
131 struct tcm_vhost_nexus *tpg_nexus;
132 /* Pointer back to tcm_vhost_tport */
133 struct tcm_vhost_tport *tport;
134 /* Returned by tcm_vhost_make_tpg() */
135 struct se_portal_group se_tpg;
136 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
137 struct vhost_scsi *vhost_scsi;
140 struct tcm_vhost_tport {
141 /* SCSI protocol the tport is providing */
143 /* Binary World Wide unique Port Name for Vhost Target port */
145 /* ASCII formatted WWPN for Vhost Target port */
146 char tport_name[TCM_VHOST_NAMELEN];
147 /* Returned by tcm_vhost_make_tport() */
148 struct se_wwn tport_wwn;
151 struct tcm_vhost_evt {
152 /* event to be sent to guest */
153 struct virtio_scsi_event event;
154 /* event list, serviced from vhost worker thread */
155 struct llist_node list;
159 VHOST_SCSI_VQ_CTL = 0,
160 VHOST_SCSI_VQ_EVT = 1,
161 VHOST_SCSI_VQ_IO = 2,
165 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG)
168 #define VHOST_SCSI_MAX_TARGET 256
169 #define VHOST_SCSI_MAX_VQ 128
170 #define VHOST_SCSI_MAX_EVENT 128
172 struct vhost_scsi_virtqueue {
173 struct vhost_virtqueue vq;
175 * Reference counting for inflight reqs, used for flush operation. At
176 * each time, one reference tracks new commands submitted, while we
177 * wait for another one to reach 0.
179 struct vhost_scsi_inflight inflights[2];
181 * Indicate current inflight in use, protected by vq->mutex.
182 * Writers must also take dev mutex and flush under it.
188 /* Protected by vhost_scsi->dev.mutex */
189 struct tcm_vhost_tpg **vs_tpg;
190 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
192 struct vhost_dev dev;
193 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
195 struct vhost_work vs_completion_work; /* cmd completion work item */
196 struct llist_head vs_completion_list; /* cmd completion queue */
198 struct vhost_work vs_event_work; /* evt injection work item */
199 struct llist_head vs_event_list; /* evt injection queue */
201 bool vs_events_missed; /* any missed events, protected by vq->mutex */
202 int vs_events_nr; /* num of pending events, protected by vq->mutex */
205 /* Local pointer to allocated TCM configfs fabric module */
206 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
208 static struct workqueue_struct *tcm_vhost_workqueue;
210 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
211 static DEFINE_MUTEX(tcm_vhost_mutex);
212 static LIST_HEAD(tcm_vhost_list);
214 static int iov_num_pages(struct iovec *iov)
216 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
217 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
220 void tcm_vhost_done_inflight(struct kref *kref)
222 struct vhost_scsi_inflight *inflight;
224 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
225 complete(&inflight->comp);
228 static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
229 struct vhost_scsi_inflight *old_inflight[])
231 struct vhost_scsi_inflight *new_inflight;
232 struct vhost_virtqueue *vq;
235 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
238 mutex_lock(&vq->mutex);
240 /* store old infight */
241 idx = vs->vqs[i].inflight_idx;
243 old_inflight[i] = &vs->vqs[i].inflights[idx];
245 /* setup new infight */
246 vs->vqs[i].inflight_idx = idx ^ 1;
247 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
248 kref_init(&new_inflight->kref);
249 init_completion(&new_inflight->comp);
251 mutex_unlock(&vq->mutex);
255 static struct vhost_scsi_inflight *
256 tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
258 struct vhost_scsi_inflight *inflight;
259 struct vhost_scsi_virtqueue *svq;
261 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
262 inflight = &svq->inflights[svq->inflight_idx];
263 kref_get(&inflight->kref);
268 static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
270 kref_put(&inflight->kref, tcm_vhost_done_inflight);
273 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
278 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
283 static char *tcm_vhost_get_fabric_name(void)
288 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
290 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
291 struct tcm_vhost_tpg, se_tpg);
292 struct tcm_vhost_tport *tport = tpg->tport;
294 switch (tport->tport_proto_id) {
295 case SCSI_PROTOCOL_SAS:
296 return sas_get_fabric_proto_ident(se_tpg);
297 case SCSI_PROTOCOL_FCP:
298 return fc_get_fabric_proto_ident(se_tpg);
299 case SCSI_PROTOCOL_ISCSI:
300 return iscsi_get_fabric_proto_ident(se_tpg);
302 pr_err("Unknown tport_proto_id: 0x%02x, using"
303 " SAS emulation\n", tport->tport_proto_id);
307 return sas_get_fabric_proto_ident(se_tpg);
310 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
312 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
313 struct tcm_vhost_tpg, se_tpg);
314 struct tcm_vhost_tport *tport = tpg->tport;
316 return &tport->tport_name[0];
319 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
321 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
322 struct tcm_vhost_tpg, se_tpg);
323 return tpg->tport_tpgt;
326 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
331 static u32 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,
361 static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
362 struct se_node_acl *se_nacl,
363 struct t10_pr_registration *pr_reg,
366 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
367 struct tcm_vhost_tpg, se_tpg);
368 struct tcm_vhost_tport *tport = tpg->tport;
370 switch (tport->tport_proto_id) {
371 case SCSI_PROTOCOL_SAS:
372 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
374 case SCSI_PROTOCOL_FCP:
375 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
377 case SCSI_PROTOCOL_ISCSI:
378 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
381 pr_err("Unknown tport_proto_id: 0x%02x, using"
382 " SAS emulation\n", tport->tport_proto_id);
386 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
390 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
393 char **port_nexus_ptr)
395 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
396 struct tcm_vhost_tpg, se_tpg);
397 struct tcm_vhost_tport *tport = tpg->tport;
399 switch (tport->tport_proto_id) {
400 case SCSI_PROTOCOL_SAS:
401 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
403 case SCSI_PROTOCOL_FCP:
404 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
406 case SCSI_PROTOCOL_ISCSI:
407 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
410 pr_err("Unknown tport_proto_id: 0x%02x, using"
411 " SAS emulation\n", tport->tport_proto_id);
415 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
419 static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
420 struct se_portal_group *se_tpg)
422 struct tcm_vhost_nacl *nacl;
424 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
426 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
430 return &nacl->se_node_acl;
433 static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
434 struct se_node_acl *se_nacl)
436 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
437 struct tcm_vhost_nacl, se_node_acl);
441 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
446 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
451 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
456 static void tcm_vhost_close_session(struct se_session *se_sess)
461 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
466 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
468 /* Go ahead and process the write immediately */
469 target_execute_cmd(se_cmd);
473 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
478 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
483 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
488 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
493 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
495 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
497 llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
499 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
502 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
504 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
505 struct tcm_vhost_cmd, tvc_se_cmd);
506 vhost_scsi_complete_cmd(tv_cmd);
510 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
512 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
513 struct tcm_vhost_cmd, tvc_se_cmd);
514 vhost_scsi_complete_cmd(tv_cmd);
518 static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
523 static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
529 static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs,
530 u32 event, u32 reason)
532 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
533 struct tcm_vhost_evt *evt;
535 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
536 vs->vs_events_missed = true;
540 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
542 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
543 vs->vs_events_missed = true;
547 evt->event.event = event;
548 evt->event.reason = reason;
554 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
556 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
558 /* TODO locking against target/backend threads? */
559 transport_generic_free_cmd(se_cmd, 1);
561 if (tv_cmd->tvc_sgl_count) {
563 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
564 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
566 kfree(tv_cmd->tvc_sgl);
569 tcm_vhost_put_inflight(tv_cmd->inflight);
574 static void tcm_vhost_do_evt_work(struct vhost_scsi *vs,
575 struct tcm_vhost_evt *evt)
577 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
578 struct virtio_scsi_event *event = &evt->event;
579 struct virtio_scsi_event __user *eventp;
583 if (!vq->private_data) {
584 vs->vs_events_missed = true;
589 vhost_disable_notify(&vs->dev, vq);
590 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
591 ARRAY_SIZE(vq->iov), &out, &in,
594 vs->vs_events_missed = true;
597 if (head == vq->num) {
598 if (vhost_enable_notify(&vs->dev, vq))
600 vs->vs_events_missed = true;
604 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
605 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
606 vq->iov[out].iov_len);
607 vs->vs_events_missed = true;
611 if (vs->vs_events_missed) {
612 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
613 vs->vs_events_missed = false;
616 eventp = vq->iov[out].iov_base;
617 ret = __copy_to_user(eventp, event, sizeof(*event));
619 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
621 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
624 static void tcm_vhost_evt_work(struct vhost_work *work)
626 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
628 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
629 struct tcm_vhost_evt *evt;
630 struct llist_node *llnode;
632 mutex_lock(&vq->mutex);
633 llnode = llist_del_all(&vs->vs_event_list);
635 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
636 llnode = llist_next(llnode);
637 tcm_vhost_do_evt_work(vs, evt);
638 tcm_vhost_free_evt(vs, evt);
640 mutex_unlock(&vq->mutex);
643 /* Fill in status and signal that we are done processing this command
645 * This is scheduled in the vhost work queue so we are called with the owner
646 * process mm and can access the vring.
648 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
650 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
652 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
653 struct virtio_scsi_cmd_resp v_rsp;
654 struct tcm_vhost_cmd *tv_cmd;
655 struct llist_node *llnode;
656 struct se_cmd *se_cmd;
659 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
660 llnode = llist_del_all(&vs->vs_completion_list);
662 tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
663 tvc_completion_list);
664 llnode = llist_next(llnode);
665 se_cmd = &tv_cmd->tvc_se_cmd;
667 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
668 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
670 memset(&v_rsp, 0, sizeof(v_rsp));
671 v_rsp.resid = se_cmd->residual_count;
672 /* TODO is status_qualifier field needed? */
673 v_rsp.status = se_cmd->scsi_status;
674 v_rsp.sense_len = se_cmd->scsi_sense_length;
675 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
677 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
678 if (likely(ret == 0)) {
679 struct vhost_scsi_virtqueue *q;
680 vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0);
681 q = container_of(tv_cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
683 __set_bit(vq, signal);
685 pr_err("Faulted on virtio_scsi_cmd_resp\n");
687 vhost_scsi_free_cmd(tv_cmd);
691 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
693 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
696 static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
697 struct vhost_virtqueue *vq,
698 struct tcm_vhost_tpg *tv_tpg,
699 struct virtio_scsi_cmd_req *v_req,
703 struct tcm_vhost_cmd *tv_cmd;
704 struct tcm_vhost_nexus *tv_nexus;
706 tv_nexus = tv_tpg->tpg_nexus;
708 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
709 return ERR_PTR(-EIO);
712 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
714 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
715 return ERR_PTR(-ENOMEM);
717 tv_cmd->tvc_tag = v_req->tag;
718 tv_cmd->tvc_task_attr = v_req->task_attr;
719 tv_cmd->tvc_exp_data_len = exp_data_len;
720 tv_cmd->tvc_data_direction = data_direction;
721 tv_cmd->tvc_nexus = tv_nexus;
722 tv_cmd->inflight = tcm_vhost_get_inflight(vq);
728 * Map a user memory range into a scatterlist
730 * Returns the number of scatterlist entries used or -errno on error.
732 static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
733 unsigned int sgl_count, struct iovec *iov, int write)
735 unsigned int npages = 0, pages_nr, offset, nbytes;
736 struct scatterlist *sg = sgl;
737 void __user *ptr = iov->iov_base;
738 size_t len = iov->iov_len;
742 pages_nr = iov_num_pages(iov);
743 if (pages_nr > sgl_count)
746 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
750 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
751 /* No pages were pinned */
754 /* Less pages pinned than wanted */
755 if (ret != pages_nr) {
756 for (i = 0; i < ret; i++)
763 offset = (uintptr_t)ptr & ~PAGE_MASK;
764 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
765 sg_set_page(sg, pages[npages], nbytes, offset);
777 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
778 struct iovec *iov, unsigned int niov, int write)
783 struct scatterlist *sg;
786 * Find out how long sglist needs to be
789 for (i = 0; i < niov; i++)
790 sgl_count += iov_num_pages(&iov[i]);
792 /* TODO overflow checking */
794 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
797 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
799 sg_init_table(sg, sgl_count);
801 tv_cmd->tvc_sgl = sg;
802 tv_cmd->tvc_sgl_count = sgl_count;
804 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
805 for (i = 0; i < niov; i++) {
806 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
808 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
809 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
810 kfree(tv_cmd->tvc_sgl);
811 tv_cmd->tvc_sgl = NULL;
812 tv_cmd->tvc_sgl_count = 0;
822 static void tcm_vhost_submission_work(struct work_struct *work)
824 struct tcm_vhost_cmd *tv_cmd =
825 container_of(work, struct tcm_vhost_cmd, work);
826 struct tcm_vhost_nexus *tv_nexus;
827 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
828 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
829 int rc, sg_no_bidi = 0;
831 if (tv_cmd->tvc_sgl_count) {
832 sg_ptr = tv_cmd->tvc_sgl;
833 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
835 if (se_cmd->se_cmd_flags & SCF_BIDI) {
843 tv_nexus = tv_cmd->tvc_nexus;
845 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
846 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
847 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
848 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
849 0, sg_ptr, tv_cmd->tvc_sgl_count,
850 sg_bidi_ptr, sg_no_bidi);
852 transport_send_check_condition_and_sense(se_cmd,
853 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
854 transport_generic_free_cmd(se_cmd, 0);
858 static void vhost_scsi_send_bad_target(struct vhost_scsi *vs,
859 struct vhost_virtqueue *vq, int head, unsigned out)
861 struct virtio_scsi_cmd_resp __user *resp;
862 struct virtio_scsi_cmd_resp rsp;
865 memset(&rsp, 0, sizeof(rsp));
866 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
867 resp = vq->iov[out].iov_base;
868 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
870 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
872 pr_err("Faulted on virtio_scsi_cmd_resp\n");
875 static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
876 struct vhost_virtqueue *vq)
878 struct tcm_vhost_tpg **vs_tpg;
879 struct virtio_scsi_cmd_req v_req;
880 struct tcm_vhost_tpg *tv_tpg;
881 struct tcm_vhost_cmd *tv_cmd;
882 u32 exp_data_len, data_first, data_num, data_direction;
888 * We can handle the vq only after the endpoint is setup by calling the
889 * VHOST_SCSI_SET_ENDPOINT ioctl.
891 * TODO: Check that we are running from vhost_worker which acts
892 * as read-side critical section for vhost kind of RCU.
893 * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
895 vs_tpg = rcu_dereference_check(vq->private_data, 1);
899 mutex_lock(&vq->mutex);
900 vhost_disable_notify(&vs->dev, vq);
903 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
904 ARRAY_SIZE(vq->iov), &out, &in,
906 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
908 /* On error, stop handling until the next kick. */
909 if (unlikely(head < 0))
911 /* Nothing new? Wait for eventfd to tell us they refilled. */
912 if (head == vq->num) {
913 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
914 vhost_disable_notify(&vs->dev, vq);
920 /* FIXME: BIDI operation */
921 if (out == 1 && in == 1) {
922 data_direction = DMA_NONE;
925 } else if (out == 1 && in > 1) {
926 data_direction = DMA_FROM_DEVICE;
927 data_first = out + 1;
929 } else if (out > 1 && in == 1) {
930 data_direction = DMA_TO_DEVICE;
934 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
940 * Check for a sane resp buffer so we can report errors to
943 if (unlikely(vq->iov[out].iov_len !=
944 sizeof(struct virtio_scsi_cmd_resp))) {
945 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
946 " bytes\n", vq->iov[out].iov_len);
950 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
951 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
952 " bytes\n", vq->iov[0].iov_len);
955 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
956 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
957 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
960 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
964 /* Extract the tpgt */
965 target = v_req.lun[1];
966 tv_tpg = ACCESS_ONCE(vs_tpg[target]);
968 /* Target does not exist, fail the request */
969 if (unlikely(!tv_tpg)) {
970 vhost_scsi_send_bad_target(vs, vq, head, out);
975 for (i = 0; i < data_num; i++)
976 exp_data_len += vq->iov[data_first + i].iov_len;
978 tv_cmd = vhost_scsi_allocate_cmd(vq, tv_tpg, &v_req,
979 exp_data_len, data_direction);
980 if (IS_ERR(tv_cmd)) {
981 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
985 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
986 ": %d\n", tv_cmd, exp_data_len, data_direction);
988 tv_cmd->tvc_vhost = vs;
990 tv_cmd->tvc_resp = vq->iov[out].iov_base;
993 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
994 * that will be used by tcm_vhost_new_cmd_map() and down into
995 * target_setup_cmd_from_cdb()
997 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
999 * Check that the recieved CDB size does not exceeded our
1000 * hardcoded max for tcm_vhost
1002 /* TODO what if cdb was too small for varlen cdb header? */
1003 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
1004 TCM_VHOST_MAX_CDB_SIZE)) {
1005 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1006 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1007 scsi_command_size(tv_cmd->tvc_cdb),
1008 TCM_VHOST_MAX_CDB_SIZE);
1011 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1013 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1014 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
1016 if (data_direction != DMA_NONE) {
1017 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
1018 &vq->iov[data_first], data_num,
1019 data_direction == DMA_TO_DEVICE);
1020 if (unlikely(ret)) {
1021 vq_err(vq, "Failed to map iov to sgl\n");
1027 * Save the descriptor from vhost_get_vq_desc() to be used to
1028 * complete the virtio-scsi request in TCM callback context via
1029 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1031 tv_cmd->tvc_vq_desc = head;
1033 * Dispatch tv_cmd descriptor for cmwq execution in process
1034 * context provided by tcm_vhost_workqueue. This also ensures
1035 * tv_cmd is executed on the same kworker CPU as this vhost
1036 * thread to gain positive L2 cache locality effects..
1038 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
1039 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
1042 mutex_unlock(&vq->mutex);
1046 vhost_scsi_free_cmd(tv_cmd);
1048 vhost_scsi_send_bad_target(vs, vq, head, out);
1049 mutex_unlock(&vq->mutex);
1052 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1054 pr_debug("%s: The handling func for control queue.\n", __func__);
1057 static void tcm_vhost_send_evt(struct vhost_scsi *vs, struct tcm_vhost_tpg *tpg,
1058 struct se_lun *lun, u32 event, u32 reason)
1060 struct tcm_vhost_evt *evt;
1062 evt = tcm_vhost_allocate_evt(vs, event, reason);
1067 /* TODO: share lun setup code with virtio-scsi.ko */
1069 * Note: evt->event is zeroed when we allocate it and
1070 * lun[4-7] need to be zero according to virtio-scsi spec.
1072 evt->event.lun[0] = 0x01;
1073 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
1074 if (lun->unpacked_lun >= 256)
1075 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1076 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1079 llist_add(&evt->list, &vs->vs_event_list);
1080 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1083 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1085 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1087 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1089 mutex_lock(&vq->mutex);
1090 if (!vq->private_data)
1093 if (vs->vs_events_missed)
1094 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1096 mutex_unlock(&vq->mutex);
1099 static void vhost_scsi_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 vhost_scsi_handle_vq(vs, vq);
1108 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1110 vhost_poll_flush(&vs->vqs[index].vq.poll);
1113 /* Callers must hold dev mutex */
1114 static void vhost_scsi_flush(struct vhost_scsi *vs)
1116 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
1119 /* Init new inflight and remember the old inflight */
1120 tcm_vhost_init_inflight(vs, old_inflight);
1123 * The inflight->kref was initialized to 1. We decrement it here to
1124 * indicate the start of the flush operation so that it will reach 0
1125 * when all the reqs are finished.
1127 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1128 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1130 /* Flush both the vhost poll and vhost work */
1131 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1132 vhost_scsi_flush_vq(vs, i);
1133 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
1134 vhost_work_flush(&vs->dev, &vs->vs_event_work);
1136 /* Wait for all reqs issued before the flush to be finished */
1137 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1138 wait_for_completion(&old_inflight[i]->comp);
1142 * Called from vhost_scsi_ioctl() context to walk the list of available
1143 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
1145 * The lock nesting rule is:
1146 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1148 static int vhost_scsi_set_endpoint(
1149 struct vhost_scsi *vs,
1150 struct vhost_scsi_target *t)
1152 struct tcm_vhost_tport *tv_tport;
1153 struct tcm_vhost_tpg *tv_tpg;
1154 struct tcm_vhost_tpg **vs_tpg;
1155 struct vhost_virtqueue *vq;
1156 int index, ret, i, len;
1159 mutex_lock(&tcm_vhost_mutex);
1160 mutex_lock(&vs->dev.mutex);
1162 /* Verify that ring has been setup correctly. */
1163 for (index = 0; index < vs->dev.nvqs; ++index) {
1164 /* Verify that ring has been setup correctly. */
1165 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1171 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1172 vs_tpg = kzalloc(len, GFP_KERNEL);
1178 memcpy(vs_tpg, vs->vs_tpg, len);
1180 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
1181 mutex_lock(&tv_tpg->tv_tpg_mutex);
1182 if (!tv_tpg->tpg_nexus) {
1183 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1186 if (tv_tpg->tv_tpg_vhost_count != 0) {
1187 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1190 tv_tport = tv_tpg->tport;
1192 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1193 if (vs->vs_tpg && vs->vs_tpg[tv_tpg->tport_tpgt]) {
1195 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1199 tv_tpg->tv_tpg_vhost_count++;
1200 tv_tpg->vhost_scsi = vs;
1201 vs_tpg[tv_tpg->tport_tpgt] = tv_tpg;
1202 smp_mb__after_atomic_inc();
1205 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1209 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1210 sizeof(vs->vs_vhost_wwpn));
1211 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1212 vq = &vs->vqs[i].vq;
1213 /* Flushing the vhost_work acts as synchronize_rcu */
1214 mutex_lock(&vq->mutex);
1215 rcu_assign_pointer(vq->private_data, vs_tpg);
1216 vhost_init_used(vq);
1217 mutex_unlock(&vq->mutex);
1225 * Act as synchronize_rcu to make sure access to
1226 * old vs->vs_tpg is finished.
1228 vhost_scsi_flush(vs);
1230 vs->vs_tpg = vs_tpg;
1233 mutex_unlock(&vs->dev.mutex);
1234 mutex_unlock(&tcm_vhost_mutex);
1238 static int vhost_scsi_clear_endpoint(
1239 struct vhost_scsi *vs,
1240 struct vhost_scsi_target *t)
1242 struct tcm_vhost_tport *tv_tport;
1243 struct tcm_vhost_tpg *tv_tpg;
1244 struct vhost_virtqueue *vq;
1249 mutex_lock(&tcm_vhost_mutex);
1250 mutex_lock(&vs->dev.mutex);
1251 /* Verify that ring has been setup correctly. */
1252 for (index = 0; index < vs->dev.nvqs; ++index) {
1253 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1264 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1266 tv_tpg = vs->vs_tpg[target];
1270 mutex_lock(&tv_tpg->tv_tpg_mutex);
1271 tv_tport = tv_tpg->tport;
1277 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1278 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
1279 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1280 tv_tport->tport_name, tv_tpg->tport_tpgt,
1281 t->vhost_wwpn, t->vhost_tpgt);
1285 tv_tpg->tv_tpg_vhost_count--;
1286 tv_tpg->vhost_scsi = NULL;
1287 vs->vs_tpg[target] = NULL;
1289 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1292 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1293 vq = &vs->vqs[i].vq;
1294 /* Flushing the vhost_work acts as synchronize_rcu */
1295 mutex_lock(&vq->mutex);
1296 rcu_assign_pointer(vq->private_data, NULL);
1297 mutex_unlock(&vq->mutex);
1301 * Act as synchronize_rcu to make sure access to
1302 * old vs->vs_tpg is finished.
1304 vhost_scsi_flush(vs);
1307 WARN_ON(vs->vs_events_nr);
1308 mutex_unlock(&vs->dev.mutex);
1309 mutex_unlock(&tcm_vhost_mutex);
1313 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1315 mutex_unlock(&vs->dev.mutex);
1316 mutex_unlock(&tcm_vhost_mutex);
1320 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1322 if (features & ~VHOST_SCSI_FEATURES)
1325 mutex_lock(&vs->dev.mutex);
1326 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1327 !vhost_log_access_ok(&vs->dev)) {
1328 mutex_unlock(&vs->dev.mutex);
1331 vs->dev.acked_features = features;
1333 vhost_scsi_flush(vs);
1334 mutex_unlock(&vs->dev.mutex);
1338 static int vhost_scsi_open(struct inode *inode, struct file *f)
1340 struct vhost_scsi *vs;
1341 struct vhost_virtqueue **vqs;
1344 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
1348 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1354 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1355 vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
1357 vs->vs_events_nr = 0;
1358 vs->vs_events_missed = false;
1360 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1361 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1362 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1363 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
1364 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
1365 vqs[i] = &vs->vqs[i].vq;
1366 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
1368 r = vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
1370 tcm_vhost_init_inflight(vs, NULL);
1378 f->private_data = vs;
1382 static int vhost_scsi_release(struct inode *inode, struct file *f)
1384 struct vhost_scsi *vs = f->private_data;
1385 struct vhost_scsi_target t;
1387 mutex_lock(&vs->dev.mutex);
1388 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1389 mutex_unlock(&vs->dev.mutex);
1390 vhost_scsi_clear_endpoint(vs, &t);
1391 vhost_dev_stop(&vs->dev);
1392 vhost_dev_cleanup(&vs->dev, false);
1393 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1394 vhost_scsi_flush(vs);
1400 static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
1403 struct vhost_scsi *vs = f->private_data;
1404 struct vhost_scsi_target backend;
1405 void __user *argp = (void __user *)arg;
1406 u64 __user *featurep = argp;
1407 u32 __user *eventsp = argp;
1410 int r, abi_version = VHOST_SCSI_ABI_VERSION;
1411 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1414 case VHOST_SCSI_SET_ENDPOINT:
1415 if (copy_from_user(&backend, argp, sizeof backend))
1417 if (backend.reserved != 0)
1420 return vhost_scsi_set_endpoint(vs, &backend);
1421 case VHOST_SCSI_CLEAR_ENDPOINT:
1422 if (copy_from_user(&backend, argp, sizeof backend))
1424 if (backend.reserved != 0)
1427 return vhost_scsi_clear_endpoint(vs, &backend);
1428 case VHOST_SCSI_GET_ABI_VERSION:
1429 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1432 case VHOST_SCSI_SET_EVENTS_MISSED:
1433 if (get_user(events_missed, eventsp))
1435 mutex_lock(&vq->mutex);
1436 vs->vs_events_missed = events_missed;
1437 mutex_unlock(&vq->mutex);
1439 case VHOST_SCSI_GET_EVENTS_MISSED:
1440 mutex_lock(&vq->mutex);
1441 events_missed = vs->vs_events_missed;
1442 mutex_unlock(&vq->mutex);
1443 if (put_user(events_missed, eventsp))
1446 case VHOST_GET_FEATURES:
1447 features = VHOST_SCSI_FEATURES;
1448 if (copy_to_user(featurep, &features, sizeof features))
1451 case VHOST_SET_FEATURES:
1452 if (copy_from_user(&features, featurep, sizeof features))
1454 return vhost_scsi_set_features(vs, features);
1456 mutex_lock(&vs->dev.mutex);
1457 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1458 /* TODO: flush backend after dev ioctl. */
1459 if (r == -ENOIOCTLCMD)
1460 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1461 mutex_unlock(&vs->dev.mutex);
1466 #ifdef CONFIG_COMPAT
1467 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1470 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1474 static const struct file_operations vhost_scsi_fops = {
1475 .owner = THIS_MODULE,
1476 .release = vhost_scsi_release,
1477 .unlocked_ioctl = vhost_scsi_ioctl,
1478 #ifdef CONFIG_COMPAT
1479 .compat_ioctl = vhost_scsi_compat_ioctl,
1481 .open = vhost_scsi_open,
1482 .llseek = noop_llseek,
1485 static struct miscdevice vhost_scsi_misc = {
1491 static int __init vhost_scsi_register(void)
1493 return misc_register(&vhost_scsi_misc);
1496 static int vhost_scsi_deregister(void)
1498 return misc_deregister(&vhost_scsi_misc);
1501 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1503 switch (tport->tport_proto_id) {
1504 case SCSI_PROTOCOL_SAS:
1506 case SCSI_PROTOCOL_FCP:
1508 case SCSI_PROTOCOL_ISCSI:
1517 static void tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1518 struct se_lun *lun, bool plug)
1521 struct vhost_scsi *vs = tpg->vhost_scsi;
1522 struct vhost_virtqueue *vq;
1528 mutex_lock(&vs->dev.mutex);
1529 if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1530 mutex_unlock(&vs->dev.mutex);
1535 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1537 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1539 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1540 mutex_lock(&vq->mutex);
1541 tcm_vhost_send_evt(vs, tpg, lun,
1542 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1543 mutex_unlock(&vq->mutex);
1544 mutex_unlock(&vs->dev.mutex);
1547 static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1549 tcm_vhost_do_plug(tpg, lun, true);
1552 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1554 tcm_vhost_do_plug(tpg, lun, false);
1557 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1560 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1561 struct tcm_vhost_tpg, se_tpg);
1563 mutex_lock(&tcm_vhost_mutex);
1565 mutex_lock(&tv_tpg->tv_tpg_mutex);
1566 tv_tpg->tv_tpg_port_count++;
1567 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1569 tcm_vhost_hotplug(tv_tpg, lun);
1571 mutex_unlock(&tcm_vhost_mutex);
1576 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1579 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1580 struct tcm_vhost_tpg, se_tpg);
1582 mutex_lock(&tcm_vhost_mutex);
1584 mutex_lock(&tv_tpg->tv_tpg_mutex);
1585 tv_tpg->tv_tpg_port_count--;
1586 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1588 tcm_vhost_hotunplug(tv_tpg, lun);
1590 mutex_unlock(&tcm_vhost_mutex);
1593 static struct se_node_acl *tcm_vhost_make_nodeacl(
1594 struct se_portal_group *se_tpg,
1595 struct config_group *group,
1598 struct se_node_acl *se_nacl, *se_nacl_new;
1599 struct tcm_vhost_nacl *nacl;
1603 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1604 return ERR_PTR(-EINVAL); */
1605 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1607 return ERR_PTR(-ENOMEM);
1611 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1612 * when converting a NodeACL from demo mode -> explict
1614 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1616 if (IS_ERR(se_nacl)) {
1617 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1621 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1623 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1624 nacl->iport_wwpn = wwpn;
1629 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1631 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1632 struct tcm_vhost_nacl, se_node_acl);
1633 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1637 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1640 struct se_portal_group *se_tpg;
1641 struct tcm_vhost_nexus *tv_nexus;
1643 mutex_lock(&tv_tpg->tv_tpg_mutex);
1644 if (tv_tpg->tpg_nexus) {
1645 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1646 pr_debug("tv_tpg->tpg_nexus already exists\n");
1649 se_tpg = &tv_tpg->se_tpg;
1651 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1653 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1654 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1658 * Initialize the struct se_session pointer
1660 tv_nexus->tvn_se_sess = transport_init_session();
1661 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1662 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1667 * Since we are running in 'demo mode' this call with generate a
1668 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1669 * the SCSI Initiator port name of the passed configfs group 'name'.
1671 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1672 se_tpg, (unsigned char *)name);
1673 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1674 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1675 pr_debug("core_tpg_check_initiator_node_acl() failed"
1677 transport_free_session(tv_nexus->tvn_se_sess);
1682 * Now register the TCM vhost virtual I_T Nexus as active with the
1683 * call to __transport_register_session()
1685 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1686 tv_nexus->tvn_se_sess, tv_nexus);
1687 tv_tpg->tpg_nexus = tv_nexus;
1689 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1693 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1695 struct se_session *se_sess;
1696 struct tcm_vhost_nexus *tv_nexus;
1698 mutex_lock(&tpg->tv_tpg_mutex);
1699 tv_nexus = tpg->tpg_nexus;
1701 mutex_unlock(&tpg->tv_tpg_mutex);
1705 se_sess = tv_nexus->tvn_se_sess;
1707 mutex_unlock(&tpg->tv_tpg_mutex);
1711 if (tpg->tv_tpg_port_count != 0) {
1712 mutex_unlock(&tpg->tv_tpg_mutex);
1713 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1714 " active TPG port count: %d\n",
1715 tpg->tv_tpg_port_count);
1719 if (tpg->tv_tpg_vhost_count != 0) {
1720 mutex_unlock(&tpg->tv_tpg_mutex);
1721 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1722 " active TPG vhost count: %d\n",
1723 tpg->tv_tpg_vhost_count);
1727 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1728 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1729 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1731 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1733 transport_deregister_session(tv_nexus->tvn_se_sess);
1734 tpg->tpg_nexus = NULL;
1735 mutex_unlock(&tpg->tv_tpg_mutex);
1741 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1744 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1745 struct tcm_vhost_tpg, se_tpg);
1746 struct tcm_vhost_nexus *tv_nexus;
1749 mutex_lock(&tv_tpg->tv_tpg_mutex);
1750 tv_nexus = tv_tpg->tpg_nexus;
1752 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1755 ret = snprintf(page, PAGE_SIZE, "%s\n",
1756 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1757 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1762 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1766 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1767 struct tcm_vhost_tpg, se_tpg);
1768 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1769 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1772 * Shutdown the active I_T nexus if 'NULL' is passed..
1774 if (!strncmp(page, "NULL", 4)) {
1775 ret = tcm_vhost_drop_nexus(tv_tpg);
1776 return (!ret) ? count : ret;
1779 * Otherwise make sure the passed virtual Initiator port WWN matches
1780 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1781 * tcm_vhost_make_nexus().
1783 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1784 pr_err("Emulated NAA Sas Address: %s, exceeds"
1785 " max: %d\n", page, TCM_VHOST_NAMELEN);
1788 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1790 ptr = strstr(i_port, "naa.");
1792 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1793 pr_err("Passed SAS Initiator Port %s does not"
1794 " match target port protoid: %s\n", i_port,
1795 tcm_vhost_dump_proto_id(tport_wwn));
1798 port_ptr = &i_port[0];
1801 ptr = strstr(i_port, "fc.");
1803 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1804 pr_err("Passed FCP Initiator Port %s does not"
1805 " match target port protoid: %s\n", i_port,
1806 tcm_vhost_dump_proto_id(tport_wwn));
1809 port_ptr = &i_port[3]; /* Skip over "fc." */
1812 ptr = strstr(i_port, "iqn.");
1814 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1815 pr_err("Passed iSCSI Initiator Port %s does not"
1816 " match target port protoid: %s\n", i_port,
1817 tcm_vhost_dump_proto_id(tport_wwn));
1820 port_ptr = &i_port[0];
1823 pr_err("Unable to locate prefix for emulated Initiator Port:"
1827 * Clear any trailing newline for the NAA WWN
1830 if (i_port[strlen(i_port)-1] == '\n')
1831 i_port[strlen(i_port)-1] = '\0';
1833 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1840 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1842 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1843 &tcm_vhost_tpg_nexus.attr,
1847 static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
1848 struct config_group *group,
1851 struct tcm_vhost_tport *tport = container_of(wwn,
1852 struct tcm_vhost_tport, tport_wwn);
1854 struct tcm_vhost_tpg *tpg;
1858 if (strstr(name, "tpgt_") != name)
1859 return ERR_PTR(-EINVAL);
1860 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1861 return ERR_PTR(-EINVAL);
1863 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1865 pr_err("Unable to allocate struct tcm_vhost_tpg");
1866 return ERR_PTR(-ENOMEM);
1868 mutex_init(&tpg->tv_tpg_mutex);
1869 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1871 tpg->tport_tpgt = tpgt;
1873 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1874 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1879 mutex_lock(&tcm_vhost_mutex);
1880 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1881 mutex_unlock(&tcm_vhost_mutex);
1883 return &tpg->se_tpg;
1886 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1888 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1889 struct tcm_vhost_tpg, se_tpg);
1891 mutex_lock(&tcm_vhost_mutex);
1892 list_del(&tpg->tv_tpg_list);
1893 mutex_unlock(&tcm_vhost_mutex);
1895 * Release the virtual I_T Nexus for this vhost TPG
1897 tcm_vhost_drop_nexus(tpg);
1899 * Deregister the se_tpg from TCM..
1901 core_tpg_deregister(se_tpg);
1905 static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1906 struct config_group *group,
1909 struct tcm_vhost_tport *tport;
1914 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1915 return ERR_PTR(-EINVAL); */
1917 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1919 pr_err("Unable to allocate struct tcm_vhost_tport");
1920 return ERR_PTR(-ENOMEM);
1922 tport->tport_wwpn = wwpn;
1924 * Determine the emulated Protocol Identifier and Target Port Name
1925 * based on the incoming configfs directory name.
1927 ptr = strstr(name, "naa.");
1929 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1932 ptr = strstr(name, "fc.");
1934 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1935 off = 3; /* Skip over "fc." */
1938 ptr = strstr(name, "iqn.");
1940 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1944 pr_err("Unable to locate prefix for emulated Target Port:"
1947 return ERR_PTR(-EINVAL);
1950 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1951 pr_err("Emulated %s Address: %s, exceeds"
1952 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1955 return ERR_PTR(-EINVAL);
1957 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1959 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1960 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1962 return &tport->tport_wwn;
1965 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1967 struct tcm_vhost_tport *tport = container_of(wwn,
1968 struct tcm_vhost_tport, tport_wwn);
1970 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1971 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1977 static ssize_t tcm_vhost_wwn_show_attr_version(
1978 struct target_fabric_configfs *tf,
1981 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1982 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1983 utsname()->machine);
1986 TF_WWN_ATTR_RO(tcm_vhost, version);
1988 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1989 &tcm_vhost_wwn_version.attr,
1993 static struct target_core_fabric_ops tcm_vhost_ops = {
1994 .get_fabric_name = tcm_vhost_get_fabric_name,
1995 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1996 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1997 .tpg_get_tag = tcm_vhost_get_tag,
1998 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1999 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
2000 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
2001 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
2002 .tpg_check_demo_mode = tcm_vhost_check_true,
2003 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
2004 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
2005 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
2006 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
2007 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
2008 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
2009 .release_cmd = tcm_vhost_release_cmd,
2010 .shutdown_session = tcm_vhost_shutdown_session,
2011 .close_session = tcm_vhost_close_session,
2012 .sess_get_index = tcm_vhost_sess_get_index,
2013 .sess_get_initiator_sid = NULL,
2014 .write_pending = tcm_vhost_write_pending,
2015 .write_pending_status = tcm_vhost_write_pending_status,
2016 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
2017 .get_task_tag = tcm_vhost_get_task_tag,
2018 .get_cmd_state = tcm_vhost_get_cmd_state,
2019 .queue_data_in = tcm_vhost_queue_data_in,
2020 .queue_status = tcm_vhost_queue_status,
2021 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
2023 * Setup callers for generic logic in target_core_fabric_configfs.c
2025 .fabric_make_wwn = tcm_vhost_make_tport,
2026 .fabric_drop_wwn = tcm_vhost_drop_tport,
2027 .fabric_make_tpg = tcm_vhost_make_tpg,
2028 .fabric_drop_tpg = tcm_vhost_drop_tpg,
2029 .fabric_post_link = tcm_vhost_port_link,
2030 .fabric_pre_unlink = tcm_vhost_port_unlink,
2031 .fabric_make_np = NULL,
2032 .fabric_drop_np = NULL,
2033 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
2034 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
2037 static int tcm_vhost_register_configfs(void)
2039 struct target_fabric_configfs *fabric;
2042 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2043 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2044 utsname()->machine);
2046 * Register the top level struct config_item_type with TCM core
2048 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
2049 if (IS_ERR(fabric)) {
2050 pr_err("target_fabric_configfs_init() failed\n");
2051 return PTR_ERR(fabric);
2054 * Setup fabric->tf_ops from our local tcm_vhost_ops
2056 fabric->tf_ops = tcm_vhost_ops;
2058 * Setup default attribute lists for various fabric->tf_cit_tmpl
2060 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
2061 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
2062 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
2063 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
2064 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
2065 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2066 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2067 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2068 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
2070 * Register the fabric for use within TCM
2072 ret = target_fabric_configfs_register(fabric);
2074 pr_err("target_fabric_configfs_register() failed"
2075 " for TCM_VHOST\n");
2079 * Setup our local pointer to *fabric
2081 tcm_vhost_fabric_configfs = fabric;
2082 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2086 static void tcm_vhost_deregister_configfs(void)
2088 if (!tcm_vhost_fabric_configfs)
2091 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2092 tcm_vhost_fabric_configfs = NULL;
2093 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2096 static int __init tcm_vhost_init(void)
2100 * Use our own dedicated workqueue for submitting I/O into
2101 * target core to avoid contention within system_wq.
2103 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2104 if (!tcm_vhost_workqueue)
2107 ret = vhost_scsi_register();
2109 goto out_destroy_workqueue;
2111 ret = tcm_vhost_register_configfs();
2113 goto out_vhost_scsi_deregister;
2117 out_vhost_scsi_deregister:
2118 vhost_scsi_deregister();
2119 out_destroy_workqueue:
2120 destroy_workqueue(tcm_vhost_workqueue);
2125 static void tcm_vhost_exit(void)
2127 tcm_vhost_deregister_configfs();
2128 vhost_scsi_deregister();
2129 destroy_workqueue(tcm_vhost_workqueue);
2132 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2133 MODULE_ALIAS("tcm_vhost");
2134 MODULE_LICENSE("GPL");
2135 module_init(tcm_vhost_init);
2136 module_exit(tcm_vhost_exit);