1 /*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
4 * (C) Copyright 2010-2013 Datera, Inc.
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@daterainc.com>
10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 ****************************************************************************/
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <generated/utsrelease.h>
27 #include <linux/utsname.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/kthread.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/configfs.h>
34 #include <linux/ctype.h>
35 #include <linux/compat.h>
36 #include <linux/eventfd.h>
38 #include <linux/miscdevice.h>
39 #include <asm/unaligned.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_tcq.h>
42 #include <target/target_core_base.h>
43 #include <target/target_core_fabric.h>
44 #include <target/target_core_fabric_configfs.h>
45 #include <target/target_core_configfs.h>
46 #include <target/configfs_macros.h>
47 #include <linux/vhost.h>
48 #include <linux/virtio_scsi.h>
49 #include <linux/llist.h>
50 #include <linux/bitmap.h>
51 #include <linux/percpu_ida.h>
55 #define TCM_VHOST_VERSION "v0.1"
56 #define TCM_VHOST_NAMELEN 256
57 #define TCM_VHOST_MAX_CDB_SIZE 32
58 #define TCM_VHOST_DEFAULT_TAGS 256
59 #define TCM_VHOST_PREALLOC_SGLS 2048
60 #define TCM_VHOST_PREALLOC_UPAGES 2048
61 #define TCM_VHOST_PREALLOC_PROT_SGLS 512
63 struct vhost_scsi_inflight {
64 /* Wait for the flush operation to finish */
65 struct completion comp;
66 /* Refcount for the inflight reqs */
70 struct tcm_vhost_cmd {
71 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
73 /* virtio-scsi initiator task attribute */
75 /* virtio-scsi response incoming iovecs */
77 /* virtio-scsi initiator data direction */
78 enum dma_data_direction tvc_data_direction;
79 /* Expected data transfer length from virtio-scsi header */
81 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
83 /* The number of scatterlists associated with this cmd */
85 u32 tvc_prot_sgl_count;
86 /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */
88 /* Pointer to the SGL formatted memory from virtio-scsi */
89 struct scatterlist *tvc_sgl;
90 struct scatterlist *tvc_prot_sgl;
91 struct page **tvc_upages;
92 /* Pointer to response header iovec */
93 struct iovec *tvc_resp_iov;
94 /* Pointer to vhost_scsi for our device */
95 struct vhost_scsi *tvc_vhost;
96 /* Pointer to vhost_virtqueue for the cmd */
97 struct vhost_virtqueue *tvc_vq;
98 /* Pointer to vhost nexus memory */
99 struct tcm_vhost_nexus *tvc_nexus;
100 /* The TCM I/O descriptor that is accessed via container_of() */
101 struct se_cmd tvc_se_cmd;
102 /* work item used for cmwq dispatch to tcm_vhost_submission_work() */
103 struct work_struct work;
104 /* Copy of the incoming SCSI command descriptor block (CDB) */
105 unsigned char tvc_cdb[TCM_VHOST_MAX_CDB_SIZE];
106 /* Sense buffer that will be mapped into outgoing status */
107 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
108 /* Completed commands list, serviced from vhost worker thread */
109 struct llist_node tvc_completion_list;
110 /* Used to track inflight cmd */
111 struct vhost_scsi_inflight *inflight;
114 struct tcm_vhost_nexus {
115 /* Pointer to TCM session for I_T Nexus */
116 struct se_session *tvn_se_sess;
119 struct tcm_vhost_nacl {
120 /* Binary World Wide unique Port Name for Vhost Initiator port */
122 /* ASCII formatted WWPN for Sas Initiator port */
123 char iport_name[TCM_VHOST_NAMELEN];
124 /* Returned by tcm_vhost_make_nodeacl() */
125 struct se_node_acl se_node_acl;
128 struct tcm_vhost_tpg {
129 /* Vhost port target portal group tag for TCM */
131 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
132 int tv_tpg_port_count;
133 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
134 int tv_tpg_vhost_count;
135 /* list for tcm_vhost_list */
136 struct list_head tv_tpg_list;
137 /* Used to protect access for tpg_nexus */
138 struct mutex tv_tpg_mutex;
139 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
140 struct tcm_vhost_nexus *tpg_nexus;
141 /* Pointer back to tcm_vhost_tport */
142 struct tcm_vhost_tport *tport;
143 /* Returned by tcm_vhost_make_tpg() */
144 struct se_portal_group se_tpg;
145 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
146 struct vhost_scsi *vhost_scsi;
149 struct tcm_vhost_tport {
150 /* SCSI protocol the tport is providing */
152 /* Binary World Wide unique Port Name for Vhost Target port */
154 /* ASCII formatted WWPN for Vhost Target port */
155 char tport_name[TCM_VHOST_NAMELEN];
156 /* Returned by tcm_vhost_make_tport() */
157 struct se_wwn tport_wwn;
160 struct tcm_vhost_evt {
161 /* event to be sent to guest */
162 struct virtio_scsi_event event;
163 /* event list, serviced from vhost worker thread */
164 struct llist_node list;
168 VHOST_SCSI_VQ_CTL = 0,
169 VHOST_SCSI_VQ_EVT = 1,
170 VHOST_SCSI_VQ_IO = 2,
173 /* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
175 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
176 (1ULL << VIRTIO_SCSI_F_T10_PI)
179 #define VHOST_SCSI_MAX_TARGET 256
180 #define VHOST_SCSI_MAX_VQ 128
181 #define VHOST_SCSI_MAX_EVENT 128
183 struct vhost_scsi_virtqueue {
184 struct vhost_virtqueue vq;
186 * Reference counting for inflight reqs, used for flush operation. At
187 * each time, one reference tracks new commands submitted, while we
188 * wait for another one to reach 0.
190 struct vhost_scsi_inflight inflights[2];
192 * Indicate current inflight in use, protected by vq->mutex.
193 * Writers must also take dev mutex and flush under it.
199 /* Protected by vhost_scsi->dev.mutex */
200 struct tcm_vhost_tpg **vs_tpg;
201 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
203 struct vhost_dev dev;
204 struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
206 struct vhost_work vs_completion_work; /* cmd completion work item */
207 struct llist_head vs_completion_list; /* cmd completion queue */
209 struct vhost_work vs_event_work; /* evt injection work item */
210 struct llist_head vs_event_list; /* evt injection queue */
212 bool vs_events_missed; /* any missed events, protected by vq->mutex */
213 int vs_events_nr; /* num of pending events, protected by vq->mutex */
216 /* Local pointer to allocated TCM configfs fabric module */
217 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
219 static struct workqueue_struct *tcm_vhost_workqueue;
221 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
222 static DEFINE_MUTEX(tcm_vhost_mutex);
223 static LIST_HEAD(tcm_vhost_list);
225 static int iov_num_pages(struct iovec *iov)
227 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
228 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
231 static void tcm_vhost_done_inflight(struct kref *kref)
233 struct vhost_scsi_inflight *inflight;
235 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
236 complete(&inflight->comp);
239 static void tcm_vhost_init_inflight(struct vhost_scsi *vs,
240 struct vhost_scsi_inflight *old_inflight[])
242 struct vhost_scsi_inflight *new_inflight;
243 struct vhost_virtqueue *vq;
246 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
249 mutex_lock(&vq->mutex);
251 /* store old infight */
252 idx = vs->vqs[i].inflight_idx;
254 old_inflight[i] = &vs->vqs[i].inflights[idx];
256 /* setup new infight */
257 vs->vqs[i].inflight_idx = idx ^ 1;
258 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
259 kref_init(&new_inflight->kref);
260 init_completion(&new_inflight->comp);
262 mutex_unlock(&vq->mutex);
266 static struct vhost_scsi_inflight *
267 tcm_vhost_get_inflight(struct vhost_virtqueue *vq)
269 struct vhost_scsi_inflight *inflight;
270 struct vhost_scsi_virtqueue *svq;
272 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
273 inflight = &svq->inflights[svq->inflight_idx];
274 kref_get(&inflight->kref);
279 static void tcm_vhost_put_inflight(struct vhost_scsi_inflight *inflight)
281 kref_put(&inflight->kref, tcm_vhost_done_inflight);
284 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
289 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
294 static char *tcm_vhost_get_fabric_name(void)
299 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
301 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
302 struct tcm_vhost_tpg, se_tpg);
303 struct tcm_vhost_tport *tport = tpg->tport;
305 switch (tport->tport_proto_id) {
306 case SCSI_PROTOCOL_SAS:
307 return sas_get_fabric_proto_ident(se_tpg);
308 case SCSI_PROTOCOL_FCP:
309 return fc_get_fabric_proto_ident(se_tpg);
310 case SCSI_PROTOCOL_ISCSI:
311 return iscsi_get_fabric_proto_ident(se_tpg);
313 pr_err("Unknown tport_proto_id: 0x%02x, using"
314 " SAS emulation\n", tport->tport_proto_id);
318 return sas_get_fabric_proto_ident(se_tpg);
321 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
323 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
324 struct tcm_vhost_tpg, se_tpg);
325 struct tcm_vhost_tport *tport = tpg->tport;
327 return &tport->tport_name[0];
330 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
332 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
333 struct tcm_vhost_tpg, se_tpg);
334 return tpg->tport_tpgt;
337 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
343 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
344 struct se_node_acl *se_nacl,
345 struct t10_pr_registration *pr_reg,
349 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
350 struct tcm_vhost_tpg, se_tpg);
351 struct tcm_vhost_tport *tport = tpg->tport;
353 switch (tport->tport_proto_id) {
354 case SCSI_PROTOCOL_SAS:
355 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
357 case SCSI_PROTOCOL_FCP:
358 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
360 case SCSI_PROTOCOL_ISCSI:
361 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
364 pr_err("Unknown tport_proto_id: 0x%02x, using"
365 " SAS emulation\n", tport->tport_proto_id);
369 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
374 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
375 struct se_node_acl *se_nacl,
376 struct t10_pr_registration *pr_reg,
379 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
380 struct tcm_vhost_tpg, se_tpg);
381 struct tcm_vhost_tport *tport = tpg->tport;
383 switch (tport->tport_proto_id) {
384 case SCSI_PROTOCOL_SAS:
385 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
387 case SCSI_PROTOCOL_FCP:
388 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
390 case SCSI_PROTOCOL_ISCSI:
391 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
394 pr_err("Unknown tport_proto_id: 0x%02x, using"
395 " SAS emulation\n", tport->tport_proto_id);
399 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
404 tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
407 char **port_nexus_ptr)
409 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
410 struct tcm_vhost_tpg, se_tpg);
411 struct tcm_vhost_tport *tport = tpg->tport;
413 switch (tport->tport_proto_id) {
414 case SCSI_PROTOCOL_SAS:
415 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
417 case SCSI_PROTOCOL_FCP:
418 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
420 case SCSI_PROTOCOL_ISCSI:
421 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
424 pr_err("Unknown tport_proto_id: 0x%02x, using"
425 " SAS emulation\n", tport->tport_proto_id);
429 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
433 static struct se_node_acl *
434 tcm_vhost_alloc_fabric_acl(struct se_portal_group *se_tpg)
436 struct tcm_vhost_nacl *nacl;
438 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
440 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
444 return &nacl->se_node_acl;
448 tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
449 struct se_node_acl *se_nacl)
451 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
452 struct tcm_vhost_nacl, se_node_acl);
456 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
461 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
463 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
464 struct tcm_vhost_cmd, tvc_se_cmd);
465 struct se_session *se_sess = tv_cmd->tvc_nexus->tvn_se_sess;
468 if (tv_cmd->tvc_sgl_count) {
469 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
470 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
472 if (tv_cmd->tvc_prot_sgl_count) {
473 for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++)
474 put_page(sg_page(&tv_cmd->tvc_prot_sgl[i]));
477 tcm_vhost_put_inflight(tv_cmd->inflight);
478 percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
481 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
486 static void tcm_vhost_close_session(struct se_session *se_sess)
491 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
496 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
498 /* Go ahead and process the write immediately */
499 target_execute_cmd(se_cmd);
503 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
508 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
513 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
518 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
523 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *cmd)
525 struct vhost_scsi *vs = cmd->tvc_vhost;
527 llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
529 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
532 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
534 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
535 struct tcm_vhost_cmd, tvc_se_cmd);
536 vhost_scsi_complete_cmd(cmd);
540 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
542 struct tcm_vhost_cmd *cmd = container_of(se_cmd,
543 struct tcm_vhost_cmd, tvc_se_cmd);
544 vhost_scsi_complete_cmd(cmd);
548 static void tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
553 static void tcm_vhost_aborted_task(struct se_cmd *se_cmd)
558 static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
564 static struct tcm_vhost_evt *
565 tcm_vhost_allocate_evt(struct vhost_scsi *vs,
566 u32 event, u32 reason)
568 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
569 struct tcm_vhost_evt *evt;
571 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
572 vs->vs_events_missed = true;
576 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
578 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
579 vs->vs_events_missed = true;
583 evt->event.event = cpu_to_vhost32(vq, event);
584 evt->event.reason = cpu_to_vhost32(vq, reason);
590 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *cmd)
592 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
594 /* TODO locking against target/backend threads? */
595 transport_generic_free_cmd(se_cmd, 0);
599 static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
601 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
605 tcm_vhost_do_evt_work(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
607 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
608 struct virtio_scsi_event *event = &evt->event;
609 struct virtio_scsi_event __user *eventp;
613 if (!vq->private_data) {
614 vs->vs_events_missed = true;
619 vhost_disable_notify(&vs->dev, vq);
620 head = vhost_get_vq_desc(vq, vq->iov,
621 ARRAY_SIZE(vq->iov), &out, &in,
624 vs->vs_events_missed = true;
627 if (head == vq->num) {
628 if (vhost_enable_notify(&vs->dev, vq))
630 vs->vs_events_missed = true;
634 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
635 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
636 vq->iov[out].iov_len);
637 vs->vs_events_missed = true;
641 if (vs->vs_events_missed) {
642 event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
643 vs->vs_events_missed = false;
646 eventp = vq->iov[out].iov_base;
647 ret = __copy_to_user(eventp, event, sizeof(*event));
649 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
651 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
654 static void tcm_vhost_evt_work(struct vhost_work *work)
656 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
658 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
659 struct tcm_vhost_evt *evt;
660 struct llist_node *llnode;
662 mutex_lock(&vq->mutex);
663 llnode = llist_del_all(&vs->vs_event_list);
665 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
666 llnode = llist_next(llnode);
667 tcm_vhost_do_evt_work(vs, evt);
668 tcm_vhost_free_evt(vs, evt);
670 mutex_unlock(&vq->mutex);
673 /* Fill in status and signal that we are done processing this command
675 * This is scheduled in the vhost work queue so we are called with the owner
676 * process mm and can access the vring.
678 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
680 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
682 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
683 struct virtio_scsi_cmd_resp v_rsp;
684 struct tcm_vhost_cmd *cmd;
685 struct llist_node *llnode;
686 struct se_cmd *se_cmd;
687 struct iov_iter iov_iter;
690 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
691 llnode = llist_del_all(&vs->vs_completion_list);
693 cmd = llist_entry(llnode, struct tcm_vhost_cmd,
694 tvc_completion_list);
695 llnode = llist_next(llnode);
696 se_cmd = &cmd->tvc_se_cmd;
698 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
699 cmd, se_cmd->residual_count, se_cmd->scsi_status);
701 memset(&v_rsp, 0, sizeof(v_rsp));
702 v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq, se_cmd->residual_count);
703 /* TODO is status_qualifier field needed? */
704 v_rsp.status = se_cmd->scsi_status;
705 v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
706 se_cmd->scsi_sense_length);
707 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
708 se_cmd->scsi_sense_length);
710 iov_iter_init(&iov_iter, READ, cmd->tvc_resp_iov,
711 cmd->tvc_in_iovs, sizeof(v_rsp));
712 ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter);
713 if (likely(ret == sizeof(v_rsp))) {
714 struct vhost_scsi_virtqueue *q;
715 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
716 q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
718 __set_bit(vq, signal);
720 pr_err("Faulted on virtio_scsi_cmd_resp\n");
722 vhost_scsi_free_cmd(cmd);
726 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
728 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
731 static struct tcm_vhost_cmd *
732 vhost_scsi_get_tag(struct vhost_virtqueue *vq, struct tcm_vhost_tpg *tpg,
733 unsigned char *cdb, u64 scsi_tag, u16 lun, u8 task_attr,
734 u32 exp_data_len, int data_direction)
736 struct tcm_vhost_cmd *cmd;
737 struct tcm_vhost_nexus *tv_nexus;
738 struct se_session *se_sess;
739 struct scatterlist *sg, *prot_sg;
743 tv_nexus = tpg->tpg_nexus;
745 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
746 return ERR_PTR(-EIO);
748 se_sess = tv_nexus->tvn_se_sess;
750 tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
752 pr_err("Unable to obtain tag for tcm_vhost_cmd\n");
753 return ERR_PTR(-ENOMEM);
756 cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[tag];
758 prot_sg = cmd->tvc_prot_sgl;
759 pages = cmd->tvc_upages;
760 memset(cmd, 0, sizeof(struct tcm_vhost_cmd));
763 cmd->tvc_prot_sgl = prot_sg;
764 cmd->tvc_upages = pages;
765 cmd->tvc_se_cmd.map_tag = tag;
766 cmd->tvc_tag = scsi_tag;
768 cmd->tvc_task_attr = task_attr;
769 cmd->tvc_exp_data_len = exp_data_len;
770 cmd->tvc_data_direction = data_direction;
771 cmd->tvc_nexus = tv_nexus;
772 cmd->inflight = tcm_vhost_get_inflight(vq);
774 memcpy(cmd->tvc_cdb, cdb, TCM_VHOST_MAX_CDB_SIZE);
780 * Map a user memory range into a scatterlist
782 * Returns the number of scatterlist entries used or -errno on error.
785 vhost_scsi_map_to_sgl(struct tcm_vhost_cmd *tv_cmd,
786 struct scatterlist *sgl,
787 unsigned int sgl_count,
792 unsigned int npages = 0, pages_nr, offset, nbytes;
793 struct scatterlist *sg = sgl;
794 void __user *ptr = iov->iov_base;
795 size_t len = iov->iov_len;
798 pages_nr = iov_num_pages(iov);
799 if (pages_nr > sgl_count) {
800 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
801 " sgl_count: %u\n", pages_nr, sgl_count);
804 if (pages_nr > TCM_VHOST_PREALLOC_UPAGES) {
805 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
806 " preallocated TCM_VHOST_PREALLOC_UPAGES: %u\n",
807 pages_nr, TCM_VHOST_PREALLOC_UPAGES);
811 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
812 /* No pages were pinned */
815 /* Less pages pinned than wanted */
816 if (ret != pages_nr) {
817 for (i = 0; i < ret; i++)
824 offset = (uintptr_t)ptr & ~PAGE_MASK;
825 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
826 sg_set_page(sg, pages[npages], nbytes, offset);
838 vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *cmd,
843 struct scatterlist *sg = cmd->tvc_sgl;
844 unsigned int sgl_count = 0;
847 for (i = 0; i < niov; i++)
848 sgl_count += iov_num_pages(&iov[i]);
850 if (sgl_count > TCM_VHOST_PREALLOC_SGLS) {
851 pr_err("vhost_scsi_map_iov_to_sgl() sgl_count: %u greater than"
852 " preallocated TCM_VHOST_PREALLOC_SGLS: %u\n",
853 sgl_count, TCM_VHOST_PREALLOC_SGLS);
857 pr_debug("%s sg %p sgl_count %u\n", __func__, sg, sgl_count);
858 sg_init_table(sg, sgl_count);
859 cmd->tvc_sgl_count = sgl_count;
861 pr_debug("Mapping iovec %p for %u pages\n", &iov[0], sgl_count);
863 for (i = 0; i < niov; i++) {
864 ret = vhost_scsi_map_to_sgl(cmd, sg, sgl_count, &iov[i],
865 cmd->tvc_upages, write);
867 for (i = 0; i < cmd->tvc_sgl_count; i++) {
868 struct page *page = sg_page(&cmd->tvc_sgl[i]);
872 cmd->tvc_sgl_count = 0;
882 vhost_scsi_map_iov_to_prot(struct tcm_vhost_cmd *cmd,
887 struct scatterlist *prot_sg = cmd->tvc_prot_sgl;
888 unsigned int prot_sgl_count = 0;
891 for (i = 0; i < niov; i++)
892 prot_sgl_count += iov_num_pages(&iov[i]);
894 if (prot_sgl_count > TCM_VHOST_PREALLOC_PROT_SGLS) {
895 pr_err("vhost_scsi_map_iov_to_prot() sgl_count: %u greater than"
896 " preallocated TCM_VHOST_PREALLOC_PROT_SGLS: %u\n",
897 prot_sgl_count, TCM_VHOST_PREALLOC_PROT_SGLS);
901 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
902 prot_sg, prot_sgl_count);
903 sg_init_table(prot_sg, prot_sgl_count);
904 cmd->tvc_prot_sgl_count = prot_sgl_count;
906 for (i = 0; i < niov; i++) {
907 ret = vhost_scsi_map_to_sgl(cmd, prot_sg, prot_sgl_count, &iov[i],
908 cmd->tvc_upages, write);
910 for (i = 0; i < cmd->tvc_prot_sgl_count; i++) {
911 struct page *page = sg_page(&cmd->tvc_prot_sgl[i]);
915 cmd->tvc_prot_sgl_count = 0;
919 prot_sgl_count -= ret;
924 static void tcm_vhost_submission_work(struct work_struct *work)
926 struct tcm_vhost_cmd *cmd =
927 container_of(work, struct tcm_vhost_cmd, work);
928 struct tcm_vhost_nexus *tv_nexus;
929 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
930 struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
933 /* FIXME: BIDI operation */
934 if (cmd->tvc_sgl_count) {
935 sg_ptr = cmd->tvc_sgl;
937 if (cmd->tvc_prot_sgl_count)
938 sg_prot_ptr = cmd->tvc_prot_sgl;
940 se_cmd->prot_pto = true;
944 tv_nexus = cmd->tvc_nexus;
946 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
947 cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
948 cmd->tvc_lun, cmd->tvc_exp_data_len,
949 cmd->tvc_task_attr, cmd->tvc_data_direction,
950 TARGET_SCF_ACK_KREF, sg_ptr, cmd->tvc_sgl_count,
951 NULL, 0, sg_prot_ptr, cmd->tvc_prot_sgl_count);
953 transport_send_check_condition_and_sense(se_cmd,
954 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
955 transport_generic_free_cmd(se_cmd, 0);
960 vhost_scsi_send_bad_target(struct vhost_scsi *vs,
961 struct vhost_virtqueue *vq,
962 int head, unsigned out)
964 struct virtio_scsi_cmd_resp __user *resp;
965 struct virtio_scsi_cmd_resp rsp;
968 memset(&rsp, 0, sizeof(rsp));
969 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
970 resp = vq->iov[out].iov_base;
971 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
973 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
975 pr_err("Faulted on virtio_scsi_cmd_resp\n");
979 vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
981 struct tcm_vhost_tpg **vs_tpg;
982 struct virtio_scsi_cmd_req v_req;
983 struct virtio_scsi_cmd_req_pi v_req_pi;
984 struct tcm_vhost_tpg *tpg;
985 struct tcm_vhost_cmd *cmd;
987 u32 exp_data_len, data_first, data_num, data_direction, prot_first;
989 int head, ret, data_niov, prot_niov, prot_bytes;
992 u8 *target, *lunp, task_attr;
996 mutex_lock(&vq->mutex);
998 * We can handle the vq only after the endpoint is setup by calling the
999 * VHOST_SCSI_SET_ENDPOINT ioctl.
1001 vs_tpg = vq->private_data;
1005 vhost_disable_notify(&vs->dev, vq);
1008 head = vhost_get_vq_desc(vq, vq->iov,
1009 ARRAY_SIZE(vq->iov), &out, &in,
1011 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
1013 /* On error, stop handling until the next kick. */
1014 if (unlikely(head < 0))
1016 /* Nothing new? Wait for eventfd to tell us they refilled. */
1017 if (head == vq->num) {
1018 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
1019 vhost_disable_notify(&vs->dev, vq);
1025 /* FIXME: BIDI operation */
1026 if (out == 1 && in == 1) {
1027 data_direction = DMA_NONE;
1030 } else if (out == 1 && in > 1) {
1031 data_direction = DMA_FROM_DEVICE;
1032 data_first = out + 1;
1034 } else if (out > 1 && in == 1) {
1035 data_direction = DMA_TO_DEVICE;
1039 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
1045 * Check for a sane resp buffer so we can report errors to
1048 if (unlikely(vq->iov[out].iov_len !=
1049 sizeof(struct virtio_scsi_cmd_resp))) {
1050 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
1051 " bytes\n", vq->iov[out].iov_len);
1055 if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI)) {
1057 lunp = &v_req_pi.lun[0];
1058 target = &v_req_pi.lun[1];
1059 req_size = sizeof(v_req_pi);
1063 lunp = &v_req.lun[0];
1064 target = &v_req.lun[1];
1065 req_size = sizeof(v_req);
1069 if (unlikely(vq->iov[0].iov_len < req_size)) {
1070 pr_err("Expecting virtio-scsi header: %zu, got %zu\n",
1071 req_size, vq->iov[0].iov_len);
1072 vhost_scsi_send_bad_target(vs, vq, head, out);
1075 ret = memcpy_fromiovecend(req, &vq->iov[0], 0, req_size);
1076 if (unlikely(ret)) {
1077 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
1078 vhost_scsi_send_bad_target(vs, vq, head, out);
1082 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
1083 if (unlikely(*lunp != 1)) {
1084 vhost_scsi_send_bad_target(vs, vq, head, out);
1088 tpg = ACCESS_ONCE(vs_tpg[*target]);
1090 /* Target does not exist, fail the request */
1091 if (unlikely(!tpg)) {
1092 vhost_scsi_send_bad_target(vs, vq, head, out);
1096 data_niov = data_num;
1097 prot_niov = prot_first = prot_bytes = 0;
1099 * Determine if any protection information iovecs are preceeding
1100 * the actual data payload, and adjust data_first + data_niov
1101 * values accordingly for vhost_scsi_map_iov_to_sgl() below.
1103 * Also extract virtio_scsi header bits for vhost_scsi_get_tag()
1106 if (v_req_pi.pi_bytesout) {
1107 if (data_direction != DMA_TO_DEVICE) {
1108 vq_err(vq, "Received non zero do_pi_niov"
1109 ", but wrong data_direction\n");
1110 vhost_scsi_send_bad_target(vs, vq, head, out);
1113 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
1114 } else if (v_req_pi.pi_bytesin) {
1115 if (data_direction != DMA_FROM_DEVICE) {
1116 vq_err(vq, "Received non zero di_pi_niov"
1117 ", but wrong data_direction\n");
1118 vhost_scsi_send_bad_target(vs, vq, head, out);
1121 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
1126 for (i = 0; i < data_num; i++) {
1127 tmp += vq->iov[data_first + i].iov_len;
1129 if (tmp >= prot_bytes)
1132 prot_first = data_first;
1133 data_first += prot_niov;
1134 data_niov = data_num - prot_niov;
1136 tag = vhost64_to_cpu(vq, v_req_pi.tag);
1137 task_attr = v_req_pi.task_attr;
1138 cdb = &v_req_pi.cdb[0];
1139 lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
1141 tag = vhost64_to_cpu(vq, v_req.tag);
1142 task_attr = v_req.task_attr;
1143 cdb = &v_req.cdb[0];
1144 lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1147 for (i = 0; i < data_niov; i++)
1148 exp_data_len += vq->iov[data_first + i].iov_len;
1150 * Check that the recieved CDB size does not exceeded our
1151 * hardcoded max for vhost-scsi
1153 * TODO what if cdb was too small for varlen cdb header?
1155 if (unlikely(scsi_command_size(cdb) > TCM_VHOST_MAX_CDB_SIZE)) {
1156 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1157 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1158 scsi_command_size(cdb), TCM_VHOST_MAX_CDB_SIZE);
1159 vhost_scsi_send_bad_target(vs, vq, head, out);
1163 cmd = vhost_scsi_get_tag(vq, tpg, cdb, tag, lun, task_attr,
1164 exp_data_len + prot_bytes,
1167 vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
1169 vhost_scsi_send_bad_target(vs, vq, head, out);
1173 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
1174 ": %d\n", cmd, exp_data_len, data_direction);
1176 cmd->tvc_vhost = vs;
1178 cmd->tvc_resp_iov = &vq->iov[out];
1179 cmd->tvc_in_iovs = in;
1181 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1182 cmd->tvc_cdb[0], cmd->tvc_lun);
1185 ret = vhost_scsi_map_iov_to_prot(cmd,
1186 &vq->iov[prot_first], prot_niov,
1187 data_direction == DMA_FROM_DEVICE);
1188 if (unlikely(ret)) {
1189 vq_err(vq, "Failed to map iov to"
1191 tcm_vhost_release_cmd(&cmd->tvc_se_cmd);
1192 vhost_scsi_send_bad_target(vs, vq, head, out);
1196 if (data_direction != DMA_NONE) {
1197 ret = vhost_scsi_map_iov_to_sgl(cmd,
1198 &vq->iov[data_first], data_niov,
1199 data_direction == DMA_FROM_DEVICE);
1200 if (unlikely(ret)) {
1201 vq_err(vq, "Failed to map iov to sgl\n");
1202 tcm_vhost_release_cmd(&cmd->tvc_se_cmd);
1203 vhost_scsi_send_bad_target(vs, vq, head, out);
1208 * Save the descriptor from vhost_get_vq_desc() to be used to
1209 * complete the virtio-scsi request in TCM callback context via
1210 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1212 cmd->tvc_vq_desc = head;
1214 * Dispatch tv_cmd descriptor for cmwq execution in process
1215 * context provided by tcm_vhost_workqueue. This also ensures
1216 * tv_cmd is executed on the same kworker CPU as this vhost
1217 * thread to gain positive L2 cache locality effects..
1219 INIT_WORK(&cmd->work, tcm_vhost_submission_work);
1220 queue_work(tcm_vhost_workqueue, &cmd->work);
1223 mutex_unlock(&vq->mutex);
1226 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1228 pr_debug("%s: The handling func for control queue.\n", __func__);
1232 tcm_vhost_send_evt(struct vhost_scsi *vs,
1233 struct tcm_vhost_tpg *tpg,
1238 struct tcm_vhost_evt *evt;
1240 evt = tcm_vhost_allocate_evt(vs, event, reason);
1245 /* TODO: share lun setup code with virtio-scsi.ko */
1247 * Note: evt->event is zeroed when we allocate it and
1248 * lun[4-7] need to be zero according to virtio-scsi spec.
1250 evt->event.lun[0] = 0x01;
1251 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
1252 if (lun->unpacked_lun >= 256)
1253 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1254 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1257 llist_add(&evt->list, &vs->vs_event_list);
1258 vhost_work_queue(&vs->dev, &vs->vs_event_work);
1261 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1263 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1265 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1267 mutex_lock(&vq->mutex);
1268 if (!vq->private_data)
1271 if (vs->vs_events_missed)
1272 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1274 mutex_unlock(&vq->mutex);
1277 static void vhost_scsi_handle_kick(struct vhost_work *work)
1279 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1281 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1283 vhost_scsi_handle_vq(vs, vq);
1286 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1288 vhost_poll_flush(&vs->vqs[index].vq.poll);
1291 /* Callers must hold dev mutex */
1292 static void vhost_scsi_flush(struct vhost_scsi *vs)
1294 struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
1297 /* Init new inflight and remember the old inflight */
1298 tcm_vhost_init_inflight(vs, old_inflight);
1301 * The inflight->kref was initialized to 1. We decrement it here to
1302 * indicate the start of the flush operation so that it will reach 0
1303 * when all the reqs are finished.
1305 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1306 kref_put(&old_inflight[i]->kref, tcm_vhost_done_inflight);
1308 /* Flush both the vhost poll and vhost work */
1309 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1310 vhost_scsi_flush_vq(vs, i);
1311 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
1312 vhost_work_flush(&vs->dev, &vs->vs_event_work);
1314 /* Wait for all reqs issued before the flush to be finished */
1315 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1316 wait_for_completion(&old_inflight[i]->comp);
1320 * Called from vhost_scsi_ioctl() context to walk the list of available
1321 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
1323 * The lock nesting rule is:
1324 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1327 vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1328 struct vhost_scsi_target *t)
1330 struct se_portal_group *se_tpg;
1331 struct tcm_vhost_tport *tv_tport;
1332 struct tcm_vhost_tpg *tpg;
1333 struct tcm_vhost_tpg **vs_tpg;
1334 struct vhost_virtqueue *vq;
1335 int index, ret, i, len;
1338 mutex_lock(&tcm_vhost_mutex);
1339 mutex_lock(&vs->dev.mutex);
1341 /* Verify that ring has been setup correctly. */
1342 for (index = 0; index < vs->dev.nvqs; ++index) {
1343 /* Verify that ring has been setup correctly. */
1344 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1350 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1351 vs_tpg = kzalloc(len, GFP_KERNEL);
1357 memcpy(vs_tpg, vs->vs_tpg, len);
1359 list_for_each_entry(tpg, &tcm_vhost_list, tv_tpg_list) {
1360 mutex_lock(&tpg->tv_tpg_mutex);
1361 if (!tpg->tpg_nexus) {
1362 mutex_unlock(&tpg->tv_tpg_mutex);
1365 if (tpg->tv_tpg_vhost_count != 0) {
1366 mutex_unlock(&tpg->tv_tpg_mutex);
1369 tv_tport = tpg->tport;
1371 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1372 if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
1374 mutex_unlock(&tpg->tv_tpg_mutex);
1379 * In order to ensure individual vhost-scsi configfs
1380 * groups cannot be removed while in use by vhost ioctl,
1381 * go ahead and take an explicit se_tpg->tpg_group.cg_item
1384 se_tpg = &tpg->se_tpg;
1385 ret = configfs_depend_item(se_tpg->se_tpg_tfo->tf_subsys,
1386 &se_tpg->tpg_group.cg_item);
1388 pr_warn("configfs_depend_item() failed: %d\n", ret);
1390 mutex_unlock(&tpg->tv_tpg_mutex);
1393 tpg->tv_tpg_vhost_count++;
1394 tpg->vhost_scsi = vs;
1395 vs_tpg[tpg->tport_tpgt] = tpg;
1396 smp_mb__after_atomic();
1399 mutex_unlock(&tpg->tv_tpg_mutex);
1403 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1404 sizeof(vs->vs_vhost_wwpn));
1405 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1406 vq = &vs->vqs[i].vq;
1407 mutex_lock(&vq->mutex);
1408 vq->private_data = vs_tpg;
1409 vhost_init_used(vq);
1410 mutex_unlock(&vq->mutex);
1418 * Act as synchronize_rcu to make sure access to
1419 * old vs->vs_tpg is finished.
1421 vhost_scsi_flush(vs);
1423 vs->vs_tpg = vs_tpg;
1426 mutex_unlock(&vs->dev.mutex);
1427 mutex_unlock(&tcm_vhost_mutex);
1432 vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1433 struct vhost_scsi_target *t)
1435 struct se_portal_group *se_tpg;
1436 struct tcm_vhost_tport *tv_tport;
1437 struct tcm_vhost_tpg *tpg;
1438 struct vhost_virtqueue *vq;
1443 mutex_lock(&tcm_vhost_mutex);
1444 mutex_lock(&vs->dev.mutex);
1445 /* Verify that ring has been setup correctly. */
1446 for (index = 0; index < vs->dev.nvqs; ++index) {
1447 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1458 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1460 tpg = vs->vs_tpg[target];
1464 mutex_lock(&tpg->tv_tpg_mutex);
1465 tv_tport = tpg->tport;
1471 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1472 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
1473 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1474 tv_tport->tport_name, tpg->tport_tpgt,
1475 t->vhost_wwpn, t->vhost_tpgt);
1479 tpg->tv_tpg_vhost_count--;
1480 tpg->vhost_scsi = NULL;
1481 vs->vs_tpg[target] = NULL;
1483 mutex_unlock(&tpg->tv_tpg_mutex);
1485 * Release se_tpg->tpg_group.cg_item configfs dependency now
1486 * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
1488 se_tpg = &tpg->se_tpg;
1489 configfs_undepend_item(se_tpg->se_tpg_tfo->tf_subsys,
1490 &se_tpg->tpg_group.cg_item);
1493 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1494 vq = &vs->vqs[i].vq;
1495 mutex_lock(&vq->mutex);
1496 vq->private_data = NULL;
1497 mutex_unlock(&vq->mutex);
1501 * Act as synchronize_rcu to make sure access to
1502 * old vs->vs_tpg is finished.
1504 vhost_scsi_flush(vs);
1507 WARN_ON(vs->vs_events_nr);
1508 mutex_unlock(&vs->dev.mutex);
1509 mutex_unlock(&tcm_vhost_mutex);
1513 mutex_unlock(&tpg->tv_tpg_mutex);
1515 mutex_unlock(&vs->dev.mutex);
1516 mutex_unlock(&tcm_vhost_mutex);
1520 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1522 struct vhost_virtqueue *vq;
1525 if (features & ~VHOST_SCSI_FEATURES)
1528 mutex_lock(&vs->dev.mutex);
1529 if ((features & (1 << VHOST_F_LOG_ALL)) &&
1530 !vhost_log_access_ok(&vs->dev)) {
1531 mutex_unlock(&vs->dev.mutex);
1535 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1536 vq = &vs->vqs[i].vq;
1537 mutex_lock(&vq->mutex);
1538 vq->acked_features = features;
1539 mutex_unlock(&vq->mutex);
1541 mutex_unlock(&vs->dev.mutex);
1545 static int vhost_scsi_open(struct inode *inode, struct file *f)
1547 struct vhost_scsi *vs;
1548 struct vhost_virtqueue **vqs;
1551 vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
1553 vs = vzalloc(sizeof(*vs));
1558 vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1562 vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1563 vhost_work_init(&vs->vs_event_work, tcm_vhost_evt_work);
1565 vs->vs_events_nr = 0;
1566 vs->vs_events_missed = false;
1568 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1569 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1570 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1571 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
1572 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
1573 vqs[i] = &vs->vqs[i].vq;
1574 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
1576 vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
1578 tcm_vhost_init_inflight(vs, NULL);
1580 f->private_data = vs;
1589 static int vhost_scsi_release(struct inode *inode, struct file *f)
1591 struct vhost_scsi *vs = f->private_data;
1592 struct vhost_scsi_target t;
1594 mutex_lock(&vs->dev.mutex);
1595 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1596 mutex_unlock(&vs->dev.mutex);
1597 vhost_scsi_clear_endpoint(vs, &t);
1598 vhost_dev_stop(&vs->dev);
1599 vhost_dev_cleanup(&vs->dev, false);
1600 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1601 vhost_scsi_flush(vs);
1608 vhost_scsi_ioctl(struct file *f,
1612 struct vhost_scsi *vs = f->private_data;
1613 struct vhost_scsi_target backend;
1614 void __user *argp = (void __user *)arg;
1615 u64 __user *featurep = argp;
1616 u32 __user *eventsp = argp;
1619 int r, abi_version = VHOST_SCSI_ABI_VERSION;
1620 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1623 case VHOST_SCSI_SET_ENDPOINT:
1624 if (copy_from_user(&backend, argp, sizeof backend))
1626 if (backend.reserved != 0)
1629 return vhost_scsi_set_endpoint(vs, &backend);
1630 case VHOST_SCSI_CLEAR_ENDPOINT:
1631 if (copy_from_user(&backend, argp, sizeof backend))
1633 if (backend.reserved != 0)
1636 return vhost_scsi_clear_endpoint(vs, &backend);
1637 case VHOST_SCSI_GET_ABI_VERSION:
1638 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1641 case VHOST_SCSI_SET_EVENTS_MISSED:
1642 if (get_user(events_missed, eventsp))
1644 mutex_lock(&vq->mutex);
1645 vs->vs_events_missed = events_missed;
1646 mutex_unlock(&vq->mutex);
1648 case VHOST_SCSI_GET_EVENTS_MISSED:
1649 mutex_lock(&vq->mutex);
1650 events_missed = vs->vs_events_missed;
1651 mutex_unlock(&vq->mutex);
1652 if (put_user(events_missed, eventsp))
1655 case VHOST_GET_FEATURES:
1656 features = VHOST_SCSI_FEATURES;
1657 if (copy_to_user(featurep, &features, sizeof features))
1660 case VHOST_SET_FEATURES:
1661 if (copy_from_user(&features, featurep, sizeof features))
1663 return vhost_scsi_set_features(vs, features);
1665 mutex_lock(&vs->dev.mutex);
1666 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1667 /* TODO: flush backend after dev ioctl. */
1668 if (r == -ENOIOCTLCMD)
1669 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1670 mutex_unlock(&vs->dev.mutex);
1675 #ifdef CONFIG_COMPAT
1676 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1679 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1683 static const struct file_operations vhost_scsi_fops = {
1684 .owner = THIS_MODULE,
1685 .release = vhost_scsi_release,
1686 .unlocked_ioctl = vhost_scsi_ioctl,
1687 #ifdef CONFIG_COMPAT
1688 .compat_ioctl = vhost_scsi_compat_ioctl,
1690 .open = vhost_scsi_open,
1691 .llseek = noop_llseek,
1694 static struct miscdevice vhost_scsi_misc = {
1700 static int __init vhost_scsi_register(void)
1702 return misc_register(&vhost_scsi_misc);
1705 static int vhost_scsi_deregister(void)
1707 return misc_deregister(&vhost_scsi_misc);
1710 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1712 switch (tport->tport_proto_id) {
1713 case SCSI_PROTOCOL_SAS:
1715 case SCSI_PROTOCOL_FCP:
1717 case SCSI_PROTOCOL_ISCSI:
1727 tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1728 struct se_lun *lun, bool plug)
1731 struct vhost_scsi *vs = tpg->vhost_scsi;
1732 struct vhost_virtqueue *vq;
1738 mutex_lock(&vs->dev.mutex);
1741 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1743 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1745 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1746 mutex_lock(&vq->mutex);
1747 if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
1748 tcm_vhost_send_evt(vs, tpg, lun,
1749 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1750 mutex_unlock(&vq->mutex);
1751 mutex_unlock(&vs->dev.mutex);
1754 static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1756 tcm_vhost_do_plug(tpg, lun, true);
1759 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1761 tcm_vhost_do_plug(tpg, lun, false);
1764 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1767 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1768 struct tcm_vhost_tpg, se_tpg);
1770 mutex_lock(&tcm_vhost_mutex);
1772 mutex_lock(&tpg->tv_tpg_mutex);
1773 tpg->tv_tpg_port_count++;
1774 mutex_unlock(&tpg->tv_tpg_mutex);
1776 tcm_vhost_hotplug(tpg, lun);
1778 mutex_unlock(&tcm_vhost_mutex);
1783 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1786 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1787 struct tcm_vhost_tpg, se_tpg);
1789 mutex_lock(&tcm_vhost_mutex);
1791 mutex_lock(&tpg->tv_tpg_mutex);
1792 tpg->tv_tpg_port_count--;
1793 mutex_unlock(&tpg->tv_tpg_mutex);
1795 tcm_vhost_hotunplug(tpg, lun);
1797 mutex_unlock(&tcm_vhost_mutex);
1800 static struct se_node_acl *
1801 tcm_vhost_make_nodeacl(struct se_portal_group *se_tpg,
1802 struct config_group *group,
1805 struct se_node_acl *se_nacl, *se_nacl_new;
1806 struct tcm_vhost_nacl *nacl;
1810 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1811 return ERR_PTR(-EINVAL); */
1812 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1814 return ERR_PTR(-ENOMEM);
1818 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1819 * when converting a NodeACL from demo mode -> explict
1821 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1823 if (IS_ERR(se_nacl)) {
1824 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1828 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1830 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1831 nacl->iport_wwpn = wwpn;
1836 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1838 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1839 struct tcm_vhost_nacl, se_node_acl);
1840 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1844 static void tcm_vhost_free_cmd_map_res(struct tcm_vhost_nexus *nexus,
1845 struct se_session *se_sess)
1847 struct tcm_vhost_cmd *tv_cmd;
1850 if (!se_sess->sess_cmd_map)
1853 for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
1854 tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
1856 kfree(tv_cmd->tvc_sgl);
1857 kfree(tv_cmd->tvc_prot_sgl);
1858 kfree(tv_cmd->tvc_upages);
1862 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tpg,
1865 struct se_portal_group *se_tpg;
1866 struct se_session *se_sess;
1867 struct tcm_vhost_nexus *tv_nexus;
1868 struct tcm_vhost_cmd *tv_cmd;
1871 mutex_lock(&tpg->tv_tpg_mutex);
1872 if (tpg->tpg_nexus) {
1873 mutex_unlock(&tpg->tv_tpg_mutex);
1874 pr_debug("tpg->tpg_nexus already exists\n");
1877 se_tpg = &tpg->se_tpg;
1879 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1881 mutex_unlock(&tpg->tv_tpg_mutex);
1882 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1886 * Initialize the struct se_session pointer and setup tagpool
1887 * for struct tcm_vhost_cmd descriptors
1889 tv_nexus->tvn_se_sess = transport_init_session_tags(
1890 TCM_VHOST_DEFAULT_TAGS,
1891 sizeof(struct tcm_vhost_cmd),
1892 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
1893 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1894 mutex_unlock(&tpg->tv_tpg_mutex);
1898 se_sess = tv_nexus->tvn_se_sess;
1899 for (i = 0; i < TCM_VHOST_DEFAULT_TAGS; i++) {
1900 tv_cmd = &((struct tcm_vhost_cmd *)se_sess->sess_cmd_map)[i];
1902 tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
1903 TCM_VHOST_PREALLOC_SGLS, GFP_KERNEL);
1904 if (!tv_cmd->tvc_sgl) {
1905 mutex_unlock(&tpg->tv_tpg_mutex);
1906 pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1910 tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
1911 TCM_VHOST_PREALLOC_UPAGES, GFP_KERNEL);
1912 if (!tv_cmd->tvc_upages) {
1913 mutex_unlock(&tpg->tv_tpg_mutex);
1914 pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1918 tv_cmd->tvc_prot_sgl = kzalloc(sizeof(struct scatterlist) *
1919 TCM_VHOST_PREALLOC_PROT_SGLS, GFP_KERNEL);
1920 if (!tv_cmd->tvc_prot_sgl) {
1921 mutex_unlock(&tpg->tv_tpg_mutex);
1922 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1927 * Since we are running in 'demo mode' this call with generate a
1928 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1929 * the SCSI Initiator port name of the passed configfs group 'name'.
1931 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1932 se_tpg, (unsigned char *)name);
1933 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1934 mutex_unlock(&tpg->tv_tpg_mutex);
1935 pr_debug("core_tpg_check_initiator_node_acl() failed"
1940 * Now register the TCM vhost virtual I_T Nexus as active with the
1941 * call to __transport_register_session()
1943 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1944 tv_nexus->tvn_se_sess, tv_nexus);
1945 tpg->tpg_nexus = tv_nexus;
1947 mutex_unlock(&tpg->tv_tpg_mutex);
1951 tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
1952 transport_free_session(se_sess);
1957 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1959 struct se_session *se_sess;
1960 struct tcm_vhost_nexus *tv_nexus;
1962 mutex_lock(&tpg->tv_tpg_mutex);
1963 tv_nexus = tpg->tpg_nexus;
1965 mutex_unlock(&tpg->tv_tpg_mutex);
1969 se_sess = tv_nexus->tvn_se_sess;
1971 mutex_unlock(&tpg->tv_tpg_mutex);
1975 if (tpg->tv_tpg_port_count != 0) {
1976 mutex_unlock(&tpg->tv_tpg_mutex);
1977 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1978 " active TPG port count: %d\n",
1979 tpg->tv_tpg_port_count);
1983 if (tpg->tv_tpg_vhost_count != 0) {
1984 mutex_unlock(&tpg->tv_tpg_mutex);
1985 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1986 " active TPG vhost count: %d\n",
1987 tpg->tv_tpg_vhost_count);
1991 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1992 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1993 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1995 tcm_vhost_free_cmd_map_res(tv_nexus, se_sess);
1997 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1999 transport_deregister_session(tv_nexus->tvn_se_sess);
2000 tpg->tpg_nexus = NULL;
2001 mutex_unlock(&tpg->tv_tpg_mutex);
2007 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
2010 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
2011 struct tcm_vhost_tpg, se_tpg);
2012 struct tcm_vhost_nexus *tv_nexus;
2015 mutex_lock(&tpg->tv_tpg_mutex);
2016 tv_nexus = tpg->tpg_nexus;
2018 mutex_unlock(&tpg->tv_tpg_mutex);
2021 ret = snprintf(page, PAGE_SIZE, "%s\n",
2022 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
2023 mutex_unlock(&tpg->tv_tpg_mutex);
2028 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
2032 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
2033 struct tcm_vhost_tpg, se_tpg);
2034 struct tcm_vhost_tport *tport_wwn = tpg->tport;
2035 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
2038 * Shutdown the active I_T nexus if 'NULL' is passed..
2040 if (!strncmp(page, "NULL", 4)) {
2041 ret = tcm_vhost_drop_nexus(tpg);
2042 return (!ret) ? count : ret;
2045 * Otherwise make sure the passed virtual Initiator port WWN matches
2046 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
2047 * tcm_vhost_make_nexus().
2049 if (strlen(page) >= TCM_VHOST_NAMELEN) {
2050 pr_err("Emulated NAA Sas Address: %s, exceeds"
2051 " max: %d\n", page, TCM_VHOST_NAMELEN);
2054 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
2056 ptr = strstr(i_port, "naa.");
2058 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
2059 pr_err("Passed SAS Initiator Port %s does not"
2060 " match target port protoid: %s\n", i_port,
2061 tcm_vhost_dump_proto_id(tport_wwn));
2064 port_ptr = &i_port[0];
2067 ptr = strstr(i_port, "fc.");
2069 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
2070 pr_err("Passed FCP Initiator Port %s does not"
2071 " match target port protoid: %s\n", i_port,
2072 tcm_vhost_dump_proto_id(tport_wwn));
2075 port_ptr = &i_port[3]; /* Skip over "fc." */
2078 ptr = strstr(i_port, "iqn.");
2080 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
2081 pr_err("Passed iSCSI Initiator Port %s does not"
2082 " match target port protoid: %s\n", i_port,
2083 tcm_vhost_dump_proto_id(tport_wwn));
2086 port_ptr = &i_port[0];
2089 pr_err("Unable to locate prefix for emulated Initiator Port:"
2093 * Clear any trailing newline for the NAA WWN
2096 if (i_port[strlen(i_port)-1] == '\n')
2097 i_port[strlen(i_port)-1] = '\0';
2099 ret = tcm_vhost_make_nexus(tpg, port_ptr);
2106 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
2108 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
2109 &tcm_vhost_tpg_nexus.attr,
2113 static struct se_portal_group *
2114 tcm_vhost_make_tpg(struct se_wwn *wwn,
2115 struct config_group *group,
2118 struct tcm_vhost_tport *tport = container_of(wwn,
2119 struct tcm_vhost_tport, tport_wwn);
2121 struct tcm_vhost_tpg *tpg;
2125 if (strstr(name, "tpgt_") != name)
2126 return ERR_PTR(-EINVAL);
2127 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
2128 return ERR_PTR(-EINVAL);
2130 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
2132 pr_err("Unable to allocate struct tcm_vhost_tpg");
2133 return ERR_PTR(-ENOMEM);
2135 mutex_init(&tpg->tv_tpg_mutex);
2136 INIT_LIST_HEAD(&tpg->tv_tpg_list);
2138 tpg->tport_tpgt = tpgt;
2140 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
2141 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
2146 mutex_lock(&tcm_vhost_mutex);
2147 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
2148 mutex_unlock(&tcm_vhost_mutex);
2150 return &tpg->se_tpg;
2153 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
2155 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
2156 struct tcm_vhost_tpg, se_tpg);
2158 mutex_lock(&tcm_vhost_mutex);
2159 list_del(&tpg->tv_tpg_list);
2160 mutex_unlock(&tcm_vhost_mutex);
2162 * Release the virtual I_T Nexus for this vhost TPG
2164 tcm_vhost_drop_nexus(tpg);
2166 * Deregister the se_tpg from TCM..
2168 core_tpg_deregister(se_tpg);
2172 static struct se_wwn *
2173 tcm_vhost_make_tport(struct target_fabric_configfs *tf,
2174 struct config_group *group,
2177 struct tcm_vhost_tport *tport;
2182 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
2183 return ERR_PTR(-EINVAL); */
2185 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
2187 pr_err("Unable to allocate struct tcm_vhost_tport");
2188 return ERR_PTR(-ENOMEM);
2190 tport->tport_wwpn = wwpn;
2192 * Determine the emulated Protocol Identifier and Target Port Name
2193 * based on the incoming configfs directory name.
2195 ptr = strstr(name, "naa.");
2197 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
2200 ptr = strstr(name, "fc.");
2202 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
2203 off = 3; /* Skip over "fc." */
2206 ptr = strstr(name, "iqn.");
2208 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
2212 pr_err("Unable to locate prefix for emulated Target Port:"
2215 return ERR_PTR(-EINVAL);
2218 if (strlen(name) >= TCM_VHOST_NAMELEN) {
2219 pr_err("Emulated %s Address: %s, exceeds"
2220 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
2223 return ERR_PTR(-EINVAL);
2225 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
2227 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
2228 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
2230 return &tport->tport_wwn;
2233 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
2235 struct tcm_vhost_tport *tport = container_of(wwn,
2236 struct tcm_vhost_tport, tport_wwn);
2238 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2239 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
2246 tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs *tf,
2249 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
2250 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2251 utsname()->machine);
2254 TF_WWN_ATTR_RO(tcm_vhost, version);
2256 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
2257 &tcm_vhost_wwn_version.attr,
2261 static struct target_core_fabric_ops tcm_vhost_ops = {
2262 .get_fabric_name = tcm_vhost_get_fabric_name,
2263 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
2264 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
2265 .tpg_get_tag = tcm_vhost_get_tag,
2266 .tpg_get_default_depth = tcm_vhost_get_default_depth,
2267 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
2268 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
2269 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
2270 .tpg_check_demo_mode = tcm_vhost_check_true,
2271 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
2272 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
2273 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
2274 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
2275 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
2276 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
2277 .release_cmd = tcm_vhost_release_cmd,
2278 .check_stop_free = vhost_scsi_check_stop_free,
2279 .shutdown_session = tcm_vhost_shutdown_session,
2280 .close_session = tcm_vhost_close_session,
2281 .sess_get_index = tcm_vhost_sess_get_index,
2282 .sess_get_initiator_sid = NULL,
2283 .write_pending = tcm_vhost_write_pending,
2284 .write_pending_status = tcm_vhost_write_pending_status,
2285 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
2286 .get_task_tag = tcm_vhost_get_task_tag,
2287 .get_cmd_state = tcm_vhost_get_cmd_state,
2288 .queue_data_in = tcm_vhost_queue_data_in,
2289 .queue_status = tcm_vhost_queue_status,
2290 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
2291 .aborted_task = tcm_vhost_aborted_task,
2293 * Setup callers for generic logic in target_core_fabric_configfs.c
2295 .fabric_make_wwn = tcm_vhost_make_tport,
2296 .fabric_drop_wwn = tcm_vhost_drop_tport,
2297 .fabric_make_tpg = tcm_vhost_make_tpg,
2298 .fabric_drop_tpg = tcm_vhost_drop_tpg,
2299 .fabric_post_link = tcm_vhost_port_link,
2300 .fabric_pre_unlink = tcm_vhost_port_unlink,
2301 .fabric_make_np = NULL,
2302 .fabric_drop_np = NULL,
2303 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
2304 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
2307 static int tcm_vhost_register_configfs(void)
2309 struct target_fabric_configfs *fabric;
2312 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2313 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
2314 utsname()->machine);
2316 * Register the top level struct config_item_type with TCM core
2318 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
2319 if (IS_ERR(fabric)) {
2320 pr_err("target_fabric_configfs_init() failed\n");
2321 return PTR_ERR(fabric);
2324 * Setup fabric->tf_ops from our local tcm_vhost_ops
2326 fabric->tf_ops = tcm_vhost_ops;
2328 * Setup default attribute lists for various fabric->tf_cit_tmpl
2330 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
2331 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
2332 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
2333 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
2334 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
2335 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2336 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2337 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2338 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
2340 * Register the fabric for use within TCM
2342 ret = target_fabric_configfs_register(fabric);
2344 pr_err("target_fabric_configfs_register() failed"
2345 " for TCM_VHOST\n");
2349 * Setup our local pointer to *fabric
2351 tcm_vhost_fabric_configfs = fabric;
2352 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2356 static void tcm_vhost_deregister_configfs(void)
2358 if (!tcm_vhost_fabric_configfs)
2361 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
2362 tcm_vhost_fabric_configfs = NULL;
2363 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2366 static int __init tcm_vhost_init(void)
2370 * Use our own dedicated workqueue for submitting I/O into
2371 * target core to avoid contention within system_wq.
2373 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
2374 if (!tcm_vhost_workqueue)
2377 ret = vhost_scsi_register();
2379 goto out_destroy_workqueue;
2381 ret = tcm_vhost_register_configfs();
2383 goto out_vhost_scsi_deregister;
2387 out_vhost_scsi_deregister:
2388 vhost_scsi_deregister();
2389 out_destroy_workqueue:
2390 destroy_workqueue(tcm_vhost_workqueue);
2395 static void tcm_vhost_exit(void)
2397 tcm_vhost_deregister_configfs();
2398 vhost_scsi_deregister();
2399 destroy_workqueue(tcm_vhost_workqueue);
2402 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2403 MODULE_ALIAS("tcm_vhost");
2404 MODULE_LICENSE("GPL");
2405 module_init(tcm_vhost_init);
2406 module_exit(tcm_vhost_exit);