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 initiator data direction */
76 enum dma_data_direction tvc_data_direction
;
77 /* Expected data transfer length from virtio-scsi header */
79 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
81 /* The number of scatterlists associated with this cmd */
83 u32 tvc_prot_sgl_count
;
84 /* Saved unpacked SCSI LUN for tcm_vhost_submission_work() */
86 /* Pointer to the SGL formatted memory from virtio-scsi */
87 struct scatterlist
*tvc_sgl
;
88 struct scatterlist
*tvc_prot_sgl
;
89 struct page
**tvc_upages
;
90 /* Pointer to response */
91 struct virtio_scsi_cmd_resp __user
*tvc_resp
;
92 /* Pointer to vhost_scsi for our device */
93 struct vhost_scsi
*tvc_vhost
;
94 /* Pointer to vhost_virtqueue for the cmd */
95 struct vhost_virtqueue
*tvc_vq
;
96 /* Pointer to vhost nexus memory */
97 struct tcm_vhost_nexus
*tvc_nexus
;
98 /* The TCM I/O descriptor that is accessed via container_of() */
99 struct se_cmd tvc_se_cmd
;
100 /* work item used for cmwq dispatch to tcm_vhost_submission_work() */
101 struct work_struct work
;
102 /* Copy of the incoming SCSI command descriptor block (CDB) */
103 unsigned char tvc_cdb
[TCM_VHOST_MAX_CDB_SIZE
];
104 /* Sense buffer that will be mapped into outgoing status */
105 unsigned char tvc_sense_buf
[TRANSPORT_SENSE_BUFFER
];
106 /* Completed commands list, serviced from vhost worker thread */
107 struct llist_node tvc_completion_list
;
108 /* Used to track inflight cmd */
109 struct vhost_scsi_inflight
*inflight
;
112 struct tcm_vhost_nexus
{
113 /* Pointer to TCM session for I_T Nexus */
114 struct se_session
*tvn_se_sess
;
117 struct tcm_vhost_nacl
{
118 /* Binary World Wide unique Port Name for Vhost Initiator port */
120 /* ASCII formatted WWPN for Sas Initiator port */
121 char iport_name
[TCM_VHOST_NAMELEN
];
122 /* Returned by tcm_vhost_make_nodeacl() */
123 struct se_node_acl se_node_acl
;
126 struct tcm_vhost_tpg
{
127 /* Vhost port target portal group tag for TCM */
129 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
130 int tv_tpg_port_count
;
131 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
132 int tv_tpg_vhost_count
;
133 /* list for tcm_vhost_list */
134 struct list_head tv_tpg_list
;
135 /* Used to protect access for tpg_nexus */
136 struct mutex tv_tpg_mutex
;
137 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
138 struct tcm_vhost_nexus
*tpg_nexus
;
139 /* Pointer back to tcm_vhost_tport */
140 struct tcm_vhost_tport
*tport
;
141 /* Returned by tcm_vhost_make_tpg() */
142 struct se_portal_group se_tpg
;
143 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
144 struct vhost_scsi
*vhost_scsi
;
147 struct tcm_vhost_tport
{
148 /* SCSI protocol the tport is providing */
150 /* Binary World Wide unique Port Name for Vhost Target port */
152 /* ASCII formatted WWPN for Vhost Target port */
153 char tport_name
[TCM_VHOST_NAMELEN
];
154 /* Returned by tcm_vhost_make_tport() */
155 struct se_wwn tport_wwn
;
158 struct tcm_vhost_evt
{
159 /* event to be sent to guest */
160 struct virtio_scsi_event event
;
161 /* event list, serviced from vhost worker thread */
162 struct llist_node list
;
166 VHOST_SCSI_VQ_CTL
= 0,
167 VHOST_SCSI_VQ_EVT
= 1,
168 VHOST_SCSI_VQ_IO
= 2,
172 VHOST_SCSI_FEATURES
= VHOST_FEATURES
| (1ULL << VIRTIO_SCSI_F_HOTPLUG
)
175 #define VHOST_SCSI_MAX_TARGET 256
176 #define VHOST_SCSI_MAX_VQ 128
177 #define VHOST_SCSI_MAX_EVENT 128
179 struct vhost_scsi_virtqueue
{
180 struct vhost_virtqueue vq
;
182 * Reference counting for inflight reqs, used for flush operation. At
183 * each time, one reference tracks new commands submitted, while we
184 * wait for another one to reach 0.
186 struct vhost_scsi_inflight inflights
[2];
188 * Indicate current inflight in use, protected by vq->mutex.
189 * Writers must also take dev mutex and flush under it.
195 /* Protected by vhost_scsi->dev.mutex */
196 struct tcm_vhost_tpg
**vs_tpg
;
197 char vs_vhost_wwpn
[TRANSPORT_IQN_LEN
];
199 struct vhost_dev dev
;
200 struct vhost_scsi_virtqueue vqs
[VHOST_SCSI_MAX_VQ
];
202 struct vhost_work vs_completion_work
; /* cmd completion work item */
203 struct llist_head vs_completion_list
; /* cmd completion queue */
205 struct vhost_work vs_event_work
; /* evt injection work item */
206 struct llist_head vs_event_list
; /* evt injection queue */
208 bool vs_events_missed
; /* any missed events, protected by vq->mutex */
209 int vs_events_nr
; /* num of pending events, protected by vq->mutex */
212 /* Local pointer to allocated TCM configfs fabric module */
213 static struct target_fabric_configfs
*tcm_vhost_fabric_configfs
;
215 static struct workqueue_struct
*tcm_vhost_workqueue
;
217 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
218 static DEFINE_MUTEX(tcm_vhost_mutex
);
219 static LIST_HEAD(tcm_vhost_list
);
221 static int iov_num_pages(struct iovec
*iov
)
223 return (PAGE_ALIGN((unsigned long)iov
->iov_base
+ iov
->iov_len
) -
224 ((unsigned long)iov
->iov_base
& PAGE_MASK
)) >> PAGE_SHIFT
;
227 static void tcm_vhost_done_inflight(struct kref
*kref
)
229 struct vhost_scsi_inflight
*inflight
;
231 inflight
= container_of(kref
, struct vhost_scsi_inflight
, kref
);
232 complete(&inflight
->comp
);
235 static void tcm_vhost_init_inflight(struct vhost_scsi
*vs
,
236 struct vhost_scsi_inflight
*old_inflight
[])
238 struct vhost_scsi_inflight
*new_inflight
;
239 struct vhost_virtqueue
*vq
;
242 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
245 mutex_lock(&vq
->mutex
);
247 /* store old infight */
248 idx
= vs
->vqs
[i
].inflight_idx
;
250 old_inflight
[i
] = &vs
->vqs
[i
].inflights
[idx
];
252 /* setup new infight */
253 vs
->vqs
[i
].inflight_idx
= idx
^ 1;
254 new_inflight
= &vs
->vqs
[i
].inflights
[idx
^ 1];
255 kref_init(&new_inflight
->kref
);
256 init_completion(&new_inflight
->comp
);
258 mutex_unlock(&vq
->mutex
);
262 static struct vhost_scsi_inflight
*
263 tcm_vhost_get_inflight(struct vhost_virtqueue
*vq
)
265 struct vhost_scsi_inflight
*inflight
;
266 struct vhost_scsi_virtqueue
*svq
;
268 svq
= container_of(vq
, struct vhost_scsi_virtqueue
, vq
);
269 inflight
= &svq
->inflights
[svq
->inflight_idx
];
270 kref_get(&inflight
->kref
);
275 static void tcm_vhost_put_inflight(struct vhost_scsi_inflight
*inflight
)
277 kref_put(&inflight
->kref
, tcm_vhost_done_inflight
);
280 static int tcm_vhost_check_true(struct se_portal_group
*se_tpg
)
285 static int tcm_vhost_check_false(struct se_portal_group
*se_tpg
)
290 static char *tcm_vhost_get_fabric_name(void)
295 static u8
tcm_vhost_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
297 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
298 struct tcm_vhost_tpg
, se_tpg
);
299 struct tcm_vhost_tport
*tport
= tpg
->tport
;
301 switch (tport
->tport_proto_id
) {
302 case SCSI_PROTOCOL_SAS
:
303 return sas_get_fabric_proto_ident(se_tpg
);
304 case SCSI_PROTOCOL_FCP
:
305 return fc_get_fabric_proto_ident(se_tpg
);
306 case SCSI_PROTOCOL_ISCSI
:
307 return iscsi_get_fabric_proto_ident(se_tpg
);
309 pr_err("Unknown tport_proto_id: 0x%02x, using"
310 " SAS emulation\n", tport
->tport_proto_id
);
314 return sas_get_fabric_proto_ident(se_tpg
);
317 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group
*se_tpg
)
319 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
320 struct tcm_vhost_tpg
, se_tpg
);
321 struct tcm_vhost_tport
*tport
= tpg
->tport
;
323 return &tport
->tport_name
[0];
326 static u16
tcm_vhost_get_tag(struct se_portal_group
*se_tpg
)
328 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
329 struct tcm_vhost_tpg
, se_tpg
);
330 return tpg
->tport_tpgt
;
333 static u32
tcm_vhost_get_default_depth(struct se_portal_group
*se_tpg
)
339 tcm_vhost_get_pr_transport_id(struct se_portal_group
*se_tpg
,
340 struct se_node_acl
*se_nacl
,
341 struct t10_pr_registration
*pr_reg
,
345 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
346 struct tcm_vhost_tpg
, se_tpg
);
347 struct tcm_vhost_tport
*tport
= tpg
->tport
;
349 switch (tport
->tport_proto_id
) {
350 case SCSI_PROTOCOL_SAS
:
351 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
353 case SCSI_PROTOCOL_FCP
:
354 return fc_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
356 case SCSI_PROTOCOL_ISCSI
:
357 return iscsi_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
360 pr_err("Unknown tport_proto_id: 0x%02x, using"
361 " SAS emulation\n", tport
->tport_proto_id
);
365 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
370 tcm_vhost_get_pr_transport_id_len(struct se_portal_group
*se_tpg
,
371 struct se_node_acl
*se_nacl
,
372 struct t10_pr_registration
*pr_reg
,
375 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
376 struct tcm_vhost_tpg
, se_tpg
);
377 struct tcm_vhost_tport
*tport
= tpg
->tport
;
379 switch (tport
->tport_proto_id
) {
380 case SCSI_PROTOCOL_SAS
:
381 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
383 case SCSI_PROTOCOL_FCP
:
384 return fc_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
386 case SCSI_PROTOCOL_ISCSI
:
387 return iscsi_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
390 pr_err("Unknown tport_proto_id: 0x%02x, using"
391 " SAS emulation\n", tport
->tport_proto_id
);
395 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
400 tcm_vhost_parse_pr_out_transport_id(struct se_portal_group
*se_tpg
,
403 char **port_nexus_ptr
)
405 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
406 struct tcm_vhost_tpg
, se_tpg
);
407 struct tcm_vhost_tport
*tport
= tpg
->tport
;
409 switch (tport
->tport_proto_id
) {
410 case SCSI_PROTOCOL_SAS
:
411 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
413 case SCSI_PROTOCOL_FCP
:
414 return fc_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
416 case SCSI_PROTOCOL_ISCSI
:
417 return iscsi_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
420 pr_err("Unknown tport_proto_id: 0x%02x, using"
421 " SAS emulation\n", tport
->tport_proto_id
);
425 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
429 static struct se_node_acl
*
430 tcm_vhost_alloc_fabric_acl(struct se_portal_group
*se_tpg
)
432 struct tcm_vhost_nacl
*nacl
;
434 nacl
= kzalloc(sizeof(struct tcm_vhost_nacl
), GFP_KERNEL
);
436 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
440 return &nacl
->se_node_acl
;
444 tcm_vhost_release_fabric_acl(struct se_portal_group
*se_tpg
,
445 struct se_node_acl
*se_nacl
)
447 struct tcm_vhost_nacl
*nacl
= container_of(se_nacl
,
448 struct tcm_vhost_nacl
, se_node_acl
);
452 static u32
tcm_vhost_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
457 static void tcm_vhost_release_cmd(struct se_cmd
*se_cmd
)
459 struct tcm_vhost_cmd
*tv_cmd
= container_of(se_cmd
,
460 struct tcm_vhost_cmd
, tvc_se_cmd
);
461 struct se_session
*se_sess
= se_cmd
->se_sess
;
464 if (tv_cmd
->tvc_sgl_count
) {
465 for (i
= 0; i
< tv_cmd
->tvc_sgl_count
; i
++)
466 put_page(sg_page(&tv_cmd
->tvc_sgl
[i
]));
468 if (tv_cmd
->tvc_prot_sgl_count
) {
469 for (i
= 0; i
< tv_cmd
->tvc_prot_sgl_count
; i
++)
470 put_page(sg_page(&tv_cmd
->tvc_prot_sgl
[i
]));
473 tcm_vhost_put_inflight(tv_cmd
->inflight
);
474 percpu_ida_free(&se_sess
->sess_tag_pool
, se_cmd
->map_tag
);
477 static int tcm_vhost_shutdown_session(struct se_session
*se_sess
)
482 static void tcm_vhost_close_session(struct se_session
*se_sess
)
487 static u32
tcm_vhost_sess_get_index(struct se_session
*se_sess
)
492 static int tcm_vhost_write_pending(struct se_cmd
*se_cmd
)
494 /* Go ahead and process the write immediately */
495 target_execute_cmd(se_cmd
);
499 static int tcm_vhost_write_pending_status(struct se_cmd
*se_cmd
)
504 static void tcm_vhost_set_default_node_attrs(struct se_node_acl
*nacl
)
509 static u32
tcm_vhost_get_task_tag(struct se_cmd
*se_cmd
)
514 static int tcm_vhost_get_cmd_state(struct se_cmd
*se_cmd
)
519 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd
*cmd
)
521 struct vhost_scsi
*vs
= cmd
->tvc_vhost
;
523 llist_add(&cmd
->tvc_completion_list
, &vs
->vs_completion_list
);
525 vhost_work_queue(&vs
->dev
, &vs
->vs_completion_work
);
528 static int tcm_vhost_queue_data_in(struct se_cmd
*se_cmd
)
530 struct tcm_vhost_cmd
*cmd
= container_of(se_cmd
,
531 struct tcm_vhost_cmd
, tvc_se_cmd
);
532 vhost_scsi_complete_cmd(cmd
);
536 static int tcm_vhost_queue_status(struct se_cmd
*se_cmd
)
538 struct tcm_vhost_cmd
*cmd
= container_of(se_cmd
,
539 struct tcm_vhost_cmd
, tvc_se_cmd
);
540 vhost_scsi_complete_cmd(cmd
);
544 static void tcm_vhost_queue_tm_rsp(struct se_cmd
*se_cmd
)
549 static void tcm_vhost_aborted_task(struct se_cmd
*se_cmd
)
554 static void tcm_vhost_free_evt(struct vhost_scsi
*vs
, struct tcm_vhost_evt
*evt
)
560 static struct tcm_vhost_evt
*
561 tcm_vhost_allocate_evt(struct vhost_scsi
*vs
,
562 u32 event
, u32 reason
)
564 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
565 struct tcm_vhost_evt
*evt
;
567 if (vs
->vs_events_nr
> VHOST_SCSI_MAX_EVENT
) {
568 vs
->vs_events_missed
= true;
572 evt
= kzalloc(sizeof(*evt
), GFP_KERNEL
);
574 vq_err(vq
, "Failed to allocate tcm_vhost_evt\n");
575 vs
->vs_events_missed
= true;
579 evt
->event
.event
= event
;
580 evt
->event
.reason
= reason
;
586 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd
*cmd
)
588 struct se_cmd
*se_cmd
= &cmd
->tvc_se_cmd
;
590 /* TODO locking against target/backend threads? */
591 transport_generic_free_cmd(se_cmd
, 0);
595 static int vhost_scsi_check_stop_free(struct se_cmd
*se_cmd
)
597 return target_put_sess_cmd(se_cmd
->se_sess
, se_cmd
);
601 tcm_vhost_do_evt_work(struct vhost_scsi
*vs
, struct tcm_vhost_evt
*evt
)
603 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
604 struct virtio_scsi_event
*event
= &evt
->event
;
605 struct virtio_scsi_event __user
*eventp
;
609 if (!vq
->private_data
) {
610 vs
->vs_events_missed
= true;
615 vhost_disable_notify(&vs
->dev
, vq
);
616 head
= vhost_get_vq_desc(&vs
->dev
, vq
, vq
->iov
,
617 ARRAY_SIZE(vq
->iov
), &out
, &in
,
620 vs
->vs_events_missed
= true;
623 if (head
== vq
->num
) {
624 if (vhost_enable_notify(&vs
->dev
, vq
))
626 vs
->vs_events_missed
= true;
630 if ((vq
->iov
[out
].iov_len
!= sizeof(struct virtio_scsi_event
))) {
631 vq_err(vq
, "Expecting virtio_scsi_event, got %zu bytes\n",
632 vq
->iov
[out
].iov_len
);
633 vs
->vs_events_missed
= true;
637 if (vs
->vs_events_missed
) {
638 event
->event
|= VIRTIO_SCSI_T_EVENTS_MISSED
;
639 vs
->vs_events_missed
= false;
642 eventp
= vq
->iov
[out
].iov_base
;
643 ret
= __copy_to_user(eventp
, event
, sizeof(*event
));
645 vhost_add_used_and_signal(&vs
->dev
, vq
, head
, 0);
647 vq_err(vq
, "Faulted on tcm_vhost_send_event\n");
650 static void tcm_vhost_evt_work(struct vhost_work
*work
)
652 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
654 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
655 struct tcm_vhost_evt
*evt
;
656 struct llist_node
*llnode
;
658 mutex_lock(&vq
->mutex
);
659 llnode
= llist_del_all(&vs
->vs_event_list
);
661 evt
= llist_entry(llnode
, struct tcm_vhost_evt
, list
);
662 llnode
= llist_next(llnode
);
663 tcm_vhost_do_evt_work(vs
, evt
);
664 tcm_vhost_free_evt(vs
, evt
);
666 mutex_unlock(&vq
->mutex
);
669 /* Fill in status and signal that we are done processing this command
671 * This is scheduled in the vhost work queue so we are called with the owner
672 * process mm and can access the vring.
674 static void vhost_scsi_complete_cmd_work(struct vhost_work
*work
)
676 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
678 DECLARE_BITMAP(signal
, VHOST_SCSI_MAX_VQ
);
679 struct virtio_scsi_cmd_resp v_rsp
;
680 struct tcm_vhost_cmd
*cmd
;
681 struct llist_node
*llnode
;
682 struct se_cmd
*se_cmd
;
685 bitmap_zero(signal
, VHOST_SCSI_MAX_VQ
);
686 llnode
= llist_del_all(&vs
->vs_completion_list
);
688 cmd
= llist_entry(llnode
, struct tcm_vhost_cmd
,
689 tvc_completion_list
);
690 llnode
= llist_next(llnode
);
691 se_cmd
= &cmd
->tvc_se_cmd
;
693 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__
,
694 cmd
, se_cmd
->residual_count
, se_cmd
->scsi_status
);
696 memset(&v_rsp
, 0, sizeof(v_rsp
));
697 v_rsp
.resid
= se_cmd
->residual_count
;
698 /* TODO is status_qualifier field needed? */
699 v_rsp
.status
= se_cmd
->scsi_status
;
700 v_rsp
.sense_len
= se_cmd
->scsi_sense_length
;
701 memcpy(v_rsp
.sense
, cmd
->tvc_sense_buf
,
703 ret
= copy_to_user(cmd
->tvc_resp
, &v_rsp
, sizeof(v_rsp
));
704 if (likely(ret
== 0)) {
705 struct vhost_scsi_virtqueue
*q
;
706 vhost_add_used(cmd
->tvc_vq
, cmd
->tvc_vq_desc
, 0);
707 q
= container_of(cmd
->tvc_vq
, struct vhost_scsi_virtqueue
, vq
);
709 __set_bit(vq
, signal
);
711 pr_err("Faulted on virtio_scsi_cmd_resp\n");
713 vhost_scsi_free_cmd(cmd
);
717 while ((vq
= find_next_bit(signal
, VHOST_SCSI_MAX_VQ
, vq
+ 1))
719 vhost_signal(&vs
->dev
, &vs
->vqs
[vq
].vq
);
722 static struct tcm_vhost_cmd
*
723 vhost_scsi_get_tag(struct vhost_virtqueue
*vq
,
724 struct tcm_vhost_tpg
*tpg
,
725 struct virtio_scsi_cmd_req
*v_req
,
729 struct tcm_vhost_cmd
*cmd
;
730 struct tcm_vhost_nexus
*tv_nexus
;
731 struct se_session
*se_sess
;
732 struct scatterlist
*sg
, *prot_sg
;
736 tv_nexus
= tpg
->tpg_nexus
;
738 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
739 return ERR_PTR(-EIO
);
741 se_sess
= tv_nexus
->tvn_se_sess
;
743 tag
= percpu_ida_alloc(&se_sess
->sess_tag_pool
, TASK_RUNNING
);
745 pr_err("Unable to obtain tag for tcm_vhost_cmd\n");
746 return ERR_PTR(-ENOMEM
);
749 cmd
= &((struct tcm_vhost_cmd
*)se_sess
->sess_cmd_map
)[tag
];
751 prot_sg
= cmd
->tvc_prot_sgl
;
752 pages
= cmd
->tvc_upages
;
753 memset(cmd
, 0, sizeof(struct tcm_vhost_cmd
));
756 cmd
->tvc_prot_sgl
= prot_sg
;
757 cmd
->tvc_upages
= pages
;
758 cmd
->tvc_se_cmd
.map_tag
= tag
;
759 cmd
->tvc_tag
= v_req
->tag
;
760 cmd
->tvc_task_attr
= v_req
->task_attr
;
761 cmd
->tvc_exp_data_len
= exp_data_len
;
762 cmd
->tvc_data_direction
= data_direction
;
763 cmd
->tvc_nexus
= tv_nexus
;
764 cmd
->inflight
= tcm_vhost_get_inflight(vq
);
770 * Map a user memory range into a scatterlist
772 * Returns the number of scatterlist entries used or -errno on error.
775 vhost_scsi_map_to_sgl(struct tcm_vhost_cmd
*tv_cmd
,
776 struct scatterlist
*sgl
,
777 unsigned int sgl_count
,
782 unsigned int npages
= 0, pages_nr
, offset
, nbytes
;
783 struct scatterlist
*sg
= sgl
;
784 void __user
*ptr
= iov
->iov_base
;
785 size_t len
= iov
->iov_len
;
788 pages_nr
= iov_num_pages(iov
);
789 if (pages_nr
> sgl_count
) {
790 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
791 " sgl_count: %u\n", pages_nr
, sgl_count
);
794 if (pages_nr
> TCM_VHOST_PREALLOC_UPAGES
) {
795 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
796 " preallocated TCM_VHOST_PREALLOC_UPAGES: %u\n",
797 pages_nr
, TCM_VHOST_PREALLOC_UPAGES
);
801 ret
= get_user_pages_fast((unsigned long)ptr
, pages_nr
, write
, pages
);
802 /* No pages were pinned */
805 /* Less pages pinned than wanted */
806 if (ret
!= pages_nr
) {
807 for (i
= 0; i
< ret
; i
++)
814 offset
= (uintptr_t)ptr
& ~PAGE_MASK
;
815 nbytes
= min_t(unsigned int, PAGE_SIZE
- offset
, len
);
816 sg_set_page(sg
, pages
[npages
], nbytes
, offset
);
828 vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd
*cmd
,
833 struct scatterlist
*sg
= cmd
->tvc_sgl
;
834 unsigned int sgl_count
= 0;
837 for (i
= 0; i
< niov
; i
++)
838 sgl_count
+= iov_num_pages(&iov
[i
]);
840 if (sgl_count
> TCM_VHOST_PREALLOC_SGLS
) {
841 pr_err("vhost_scsi_map_iov_to_sgl() sgl_count: %u greater than"
842 " preallocated TCM_VHOST_PREALLOC_SGLS: %u\n",
843 sgl_count
, TCM_VHOST_PREALLOC_SGLS
);
847 pr_debug("%s sg %p sgl_count %u\n", __func__
, sg
, sgl_count
);
848 sg_init_table(sg
, sgl_count
);
849 cmd
->tvc_sgl_count
= sgl_count
;
851 pr_debug("Mapping iovec %p for %u pages\n", &iov
[0], sgl_count
);
853 for (i
= 0; i
< niov
; i
++) {
854 ret
= vhost_scsi_map_to_sgl(cmd
, sg
, sgl_count
, &iov
[i
],
855 cmd
->tvc_upages
, write
);
857 for (i
= 0; i
< cmd
->tvc_sgl_count
; i
++)
858 put_page(sg_page(&cmd
->tvc_sgl
[i
]));
860 cmd
->tvc_sgl_count
= 0;
870 vhost_scsi_map_iov_to_prot(struct tcm_vhost_cmd
*cmd
,
875 struct scatterlist
*prot_sg
= cmd
->tvc_prot_sgl
;
876 unsigned int prot_sgl_count
= 0;
879 for (i
= 0; i
< niov
; i
++)
880 prot_sgl_count
+= iov_num_pages(&iov
[i
]);
882 if (prot_sgl_count
> TCM_VHOST_PREALLOC_PROT_SGLS
) {
883 pr_err("vhost_scsi_map_iov_to_prot() sgl_count: %u greater than"
884 " preallocated TCM_VHOST_PREALLOC_PROT_SGLS: %u\n",
885 prot_sgl_count
, TCM_VHOST_PREALLOC_PROT_SGLS
);
889 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__
,
890 prot_sg
, prot_sgl_count
);
891 sg_init_table(prot_sg
, prot_sgl_count
);
892 cmd
->tvc_prot_sgl_count
= prot_sgl_count
;
894 for (i
= 0; i
< niov
; i
++) {
895 ret
= vhost_scsi_map_to_sgl(cmd
, prot_sg
, prot_sgl_count
, &iov
[i
],
896 cmd
->tvc_upages
, write
);
898 for (i
= 0; i
< cmd
->tvc_prot_sgl_count
; i
++)
899 put_page(sg_page(&cmd
->tvc_prot_sgl
[i
]));
901 cmd
->tvc_prot_sgl_count
= 0;
905 prot_sgl_count
-= ret
;
910 static void tcm_vhost_submission_work(struct work_struct
*work
)
912 struct tcm_vhost_cmd
*cmd
=
913 container_of(work
, struct tcm_vhost_cmd
, work
);
914 struct tcm_vhost_nexus
*tv_nexus
;
915 struct se_cmd
*se_cmd
= &cmd
->tvc_se_cmd
;
916 struct scatterlist
*sg_ptr
, *sg_bidi_ptr
= NULL
;
917 int rc
, sg_no_bidi
= 0;
919 if (cmd
->tvc_sgl_count
) {
920 sg_ptr
= cmd
->tvc_sgl
;
921 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
923 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
931 tv_nexus
= cmd
->tvc_nexus
;
933 rc
= target_submit_cmd_map_sgls(se_cmd
, tv_nexus
->tvn_se_sess
,
934 cmd
->tvc_cdb
, &cmd
->tvc_sense_buf
[0],
935 cmd
->tvc_lun
, cmd
->tvc_exp_data_len
,
936 cmd
->tvc_task_attr
, cmd
->tvc_data_direction
,
937 TARGET_SCF_ACK_KREF
, sg_ptr
, cmd
->tvc_sgl_count
,
938 sg_bidi_ptr
, sg_no_bidi
, NULL
, 0);
940 transport_send_check_condition_and_sense(se_cmd
,
941 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
942 transport_generic_free_cmd(se_cmd
, 0);
947 vhost_scsi_send_bad_target(struct vhost_scsi
*vs
,
948 struct vhost_virtqueue
*vq
,
949 int head
, unsigned out
)
951 struct virtio_scsi_cmd_resp __user
*resp
;
952 struct virtio_scsi_cmd_resp rsp
;
955 memset(&rsp
, 0, sizeof(rsp
));
956 rsp
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
957 resp
= vq
->iov
[out
].iov_base
;
958 ret
= __copy_to_user(resp
, &rsp
, sizeof(rsp
));
960 vhost_add_used_and_signal(&vs
->dev
, vq
, head
, 0);
962 pr_err("Faulted on virtio_scsi_cmd_resp\n");
966 vhost_scsi_handle_vq(struct vhost_scsi
*vs
, struct vhost_virtqueue
*vq
)
968 struct tcm_vhost_tpg
**vs_tpg
;
969 struct virtio_scsi_cmd_req v_req
;
970 struct tcm_vhost_tpg
*tpg
;
971 struct tcm_vhost_cmd
*cmd
;
972 u32 exp_data_len
, data_first
, data_num
, data_direction
;
977 mutex_lock(&vq
->mutex
);
979 * We can handle the vq only after the endpoint is setup by calling the
980 * VHOST_SCSI_SET_ENDPOINT ioctl.
982 vs_tpg
= vq
->private_data
;
986 vhost_disable_notify(&vs
->dev
, vq
);
989 head
= vhost_get_vq_desc(&vs
->dev
, vq
, vq
->iov
,
990 ARRAY_SIZE(vq
->iov
), &out
, &in
,
992 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
994 /* On error, stop handling until the next kick. */
995 if (unlikely(head
< 0))
997 /* Nothing new? Wait for eventfd to tell us they refilled. */
998 if (head
== vq
->num
) {
999 if (unlikely(vhost_enable_notify(&vs
->dev
, vq
))) {
1000 vhost_disable_notify(&vs
->dev
, vq
);
1006 /* FIXME: BIDI operation */
1007 if (out
== 1 && in
== 1) {
1008 data_direction
= DMA_NONE
;
1011 } else if (out
== 1 && in
> 1) {
1012 data_direction
= DMA_FROM_DEVICE
;
1013 data_first
= out
+ 1;
1015 } else if (out
> 1 && in
== 1) {
1016 data_direction
= DMA_TO_DEVICE
;
1020 vq_err(vq
, "Invalid buffer layout out: %u in: %u\n",
1026 * Check for a sane resp buffer so we can report errors to
1029 if (unlikely(vq
->iov
[out
].iov_len
!=
1030 sizeof(struct virtio_scsi_cmd_resp
))) {
1031 vq_err(vq
, "Expecting virtio_scsi_cmd_resp, got %zu"
1032 " bytes\n", vq
->iov
[out
].iov_len
);
1036 if (unlikely(vq
->iov
[0].iov_len
!= sizeof(v_req
))) {
1037 vq_err(vq
, "Expecting virtio_scsi_cmd_req, got %zu"
1038 " bytes\n", vq
->iov
[0].iov_len
);
1041 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
1042 " len: %zu\n", vq
->iov
[0].iov_base
, sizeof(v_req
));
1043 ret
= __copy_from_user(&v_req
, vq
->iov
[0].iov_base
,
1045 if (unlikely(ret
)) {
1046 vq_err(vq
, "Faulted on virtio_scsi_cmd_req\n");
1050 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
1051 if (unlikely(v_req
.lun
[0] != 1)) {
1052 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1056 /* Extract the tpgt */
1057 target
= v_req
.lun
[1];
1058 tpg
= ACCESS_ONCE(vs_tpg
[target
]);
1060 /* Target does not exist, fail the request */
1061 if (unlikely(!tpg
)) {
1062 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1067 for (i
= 0; i
< data_num
; i
++)
1068 exp_data_len
+= vq
->iov
[data_first
+ i
].iov_len
;
1070 cmd
= vhost_scsi_get_tag(vq
, tpg
, &v_req
,
1071 exp_data_len
, data_direction
);
1073 vq_err(vq
, "vhost_scsi_get_tag failed %ld\n",
1077 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
1078 ": %d\n", cmd
, exp_data_len
, data_direction
);
1080 cmd
->tvc_vhost
= vs
;
1082 cmd
->tvc_resp
= vq
->iov
[out
].iov_base
;
1085 * Copy in the recieved CDB descriptor into cmd->tvc_cdb
1086 * that will be used by tcm_vhost_new_cmd_map() and down into
1087 * target_setup_cmd_from_cdb()
1089 memcpy(cmd
->tvc_cdb
, v_req
.cdb
, TCM_VHOST_MAX_CDB_SIZE
);
1091 * Check that the recieved CDB size does not exceeded our
1092 * hardcoded max for tcm_vhost
1094 /* TODO what if cdb was too small for varlen cdb header? */
1095 if (unlikely(scsi_command_size(cmd
->tvc_cdb
) >
1096 TCM_VHOST_MAX_CDB_SIZE
)) {
1097 vq_err(vq
, "Received SCSI CDB with command_size: %d that"
1098 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1099 scsi_command_size(cmd
->tvc_cdb
),
1100 TCM_VHOST_MAX_CDB_SIZE
);
1103 cmd
->tvc_lun
= ((v_req
.lun
[2] << 8) | v_req
.lun
[3]) & 0x3FFF;
1105 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1106 cmd
->tvc_cdb
[0], cmd
->tvc_lun
);
1108 if (data_direction
!= DMA_NONE
) {
1109 ret
= vhost_scsi_map_iov_to_sgl(cmd
,
1110 &vq
->iov
[data_first
], data_num
,
1111 data_direction
== DMA_FROM_DEVICE
);
1112 if (unlikely(ret
)) {
1113 vq_err(vq
, "Failed to map iov to sgl\n");
1119 * Save the descriptor from vhost_get_vq_desc() to be used to
1120 * complete the virtio-scsi request in TCM callback context via
1121 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1123 cmd
->tvc_vq_desc
= head
;
1125 * Dispatch tv_cmd descriptor for cmwq execution in process
1126 * context provided by tcm_vhost_workqueue. This also ensures
1127 * tv_cmd is executed on the same kworker CPU as this vhost
1128 * thread to gain positive L2 cache locality effects..
1130 INIT_WORK(&cmd
->work
, tcm_vhost_submission_work
);
1131 queue_work(tcm_vhost_workqueue
, &cmd
->work
);
1134 mutex_unlock(&vq
->mutex
);
1138 vhost_scsi_free_cmd(cmd
);
1140 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1142 mutex_unlock(&vq
->mutex
);
1145 static void vhost_scsi_ctl_handle_kick(struct vhost_work
*work
)
1147 pr_debug("%s: The handling func for control queue.\n", __func__
);
1151 tcm_vhost_send_evt(struct vhost_scsi
*vs
,
1152 struct tcm_vhost_tpg
*tpg
,
1157 struct tcm_vhost_evt
*evt
;
1159 evt
= tcm_vhost_allocate_evt(vs
, event
, reason
);
1164 /* TODO: share lun setup code with virtio-scsi.ko */
1166 * Note: evt->event is zeroed when we allocate it and
1167 * lun[4-7] need to be zero according to virtio-scsi spec.
1169 evt
->event
.lun
[0] = 0x01;
1170 evt
->event
.lun
[1] = tpg
->tport_tpgt
& 0xFF;
1171 if (lun
->unpacked_lun
>= 256)
1172 evt
->event
.lun
[2] = lun
->unpacked_lun
>> 8 | 0x40 ;
1173 evt
->event
.lun
[3] = lun
->unpacked_lun
& 0xFF;
1176 llist_add(&evt
->list
, &vs
->vs_event_list
);
1177 vhost_work_queue(&vs
->dev
, &vs
->vs_event_work
);
1180 static void vhost_scsi_evt_handle_kick(struct vhost_work
*work
)
1182 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
1184 struct vhost_scsi
*vs
= container_of(vq
->dev
, struct vhost_scsi
, dev
);
1186 mutex_lock(&vq
->mutex
);
1187 if (!vq
->private_data
)
1190 if (vs
->vs_events_missed
)
1191 tcm_vhost_send_evt(vs
, NULL
, NULL
, VIRTIO_SCSI_T_NO_EVENT
, 0);
1193 mutex_unlock(&vq
->mutex
);
1196 static void vhost_scsi_handle_kick(struct vhost_work
*work
)
1198 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
1200 struct vhost_scsi
*vs
= container_of(vq
->dev
, struct vhost_scsi
, dev
);
1202 vhost_scsi_handle_vq(vs
, vq
);
1205 static void vhost_scsi_flush_vq(struct vhost_scsi
*vs
, int index
)
1207 vhost_poll_flush(&vs
->vqs
[index
].vq
.poll
);
1210 /* Callers must hold dev mutex */
1211 static void vhost_scsi_flush(struct vhost_scsi
*vs
)
1213 struct vhost_scsi_inflight
*old_inflight
[VHOST_SCSI_MAX_VQ
];
1216 /* Init new inflight and remember the old inflight */
1217 tcm_vhost_init_inflight(vs
, old_inflight
);
1220 * The inflight->kref was initialized to 1. We decrement it here to
1221 * indicate the start of the flush operation so that it will reach 0
1222 * when all the reqs are finished.
1224 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1225 kref_put(&old_inflight
[i
]->kref
, tcm_vhost_done_inflight
);
1227 /* Flush both the vhost poll and vhost work */
1228 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1229 vhost_scsi_flush_vq(vs
, i
);
1230 vhost_work_flush(&vs
->dev
, &vs
->vs_completion_work
);
1231 vhost_work_flush(&vs
->dev
, &vs
->vs_event_work
);
1233 /* Wait for all reqs issued before the flush to be finished */
1234 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1235 wait_for_completion(&old_inflight
[i
]->comp
);
1239 * Called from vhost_scsi_ioctl() context to walk the list of available
1240 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
1242 * The lock nesting rule is:
1243 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1246 vhost_scsi_set_endpoint(struct vhost_scsi
*vs
,
1247 struct vhost_scsi_target
*t
)
1249 struct tcm_vhost_tport
*tv_tport
;
1250 struct tcm_vhost_tpg
*tpg
;
1251 struct tcm_vhost_tpg
**vs_tpg
;
1252 struct vhost_virtqueue
*vq
;
1253 int index
, ret
, i
, len
;
1256 mutex_lock(&tcm_vhost_mutex
);
1257 mutex_lock(&vs
->dev
.mutex
);
1259 /* Verify that ring has been setup correctly. */
1260 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
1261 /* Verify that ring has been setup correctly. */
1262 if (!vhost_vq_access_ok(&vs
->vqs
[index
].vq
)) {
1268 len
= sizeof(vs_tpg
[0]) * VHOST_SCSI_MAX_TARGET
;
1269 vs_tpg
= kzalloc(len
, GFP_KERNEL
);
1275 memcpy(vs_tpg
, vs
->vs_tpg
, len
);
1277 list_for_each_entry(tpg
, &tcm_vhost_list
, tv_tpg_list
) {
1278 mutex_lock(&tpg
->tv_tpg_mutex
);
1279 if (!tpg
->tpg_nexus
) {
1280 mutex_unlock(&tpg
->tv_tpg_mutex
);
1283 if (tpg
->tv_tpg_vhost_count
!= 0) {
1284 mutex_unlock(&tpg
->tv_tpg_mutex
);
1287 tv_tport
= tpg
->tport
;
1289 if (!strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
)) {
1290 if (vs
->vs_tpg
&& vs
->vs_tpg
[tpg
->tport_tpgt
]) {
1292 mutex_unlock(&tpg
->tv_tpg_mutex
);
1296 tpg
->tv_tpg_vhost_count
++;
1297 tpg
->vhost_scsi
= vs
;
1298 vs_tpg
[tpg
->tport_tpgt
] = tpg
;
1299 smp_mb__after_atomic_inc();
1302 mutex_unlock(&tpg
->tv_tpg_mutex
);
1306 memcpy(vs
->vs_vhost_wwpn
, t
->vhost_wwpn
,
1307 sizeof(vs
->vs_vhost_wwpn
));
1308 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1309 vq
= &vs
->vqs
[i
].vq
;
1310 mutex_lock(&vq
->mutex
);
1311 vq
->private_data
= vs_tpg
;
1312 vhost_init_used(vq
);
1313 mutex_unlock(&vq
->mutex
);
1321 * Act as synchronize_rcu to make sure access to
1322 * old vs->vs_tpg is finished.
1324 vhost_scsi_flush(vs
);
1326 vs
->vs_tpg
= vs_tpg
;
1329 mutex_unlock(&vs
->dev
.mutex
);
1330 mutex_unlock(&tcm_vhost_mutex
);
1335 vhost_scsi_clear_endpoint(struct vhost_scsi
*vs
,
1336 struct vhost_scsi_target
*t
)
1338 struct tcm_vhost_tport
*tv_tport
;
1339 struct tcm_vhost_tpg
*tpg
;
1340 struct vhost_virtqueue
*vq
;
1345 mutex_lock(&tcm_vhost_mutex
);
1346 mutex_lock(&vs
->dev
.mutex
);
1347 /* Verify that ring has been setup correctly. */
1348 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
1349 if (!vhost_vq_access_ok(&vs
->vqs
[index
].vq
)) {
1360 for (i
= 0; i
< VHOST_SCSI_MAX_TARGET
; i
++) {
1362 tpg
= vs
->vs_tpg
[target
];
1366 mutex_lock(&tpg
->tv_tpg_mutex
);
1367 tv_tport
= tpg
->tport
;
1373 if (strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
)) {
1374 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
1375 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1376 tv_tport
->tport_name
, tpg
->tport_tpgt
,
1377 t
->vhost_wwpn
, t
->vhost_tpgt
);
1381 tpg
->tv_tpg_vhost_count
--;
1382 tpg
->vhost_scsi
= NULL
;
1383 vs
->vs_tpg
[target
] = NULL
;
1385 mutex_unlock(&tpg
->tv_tpg_mutex
);
1388 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1389 vq
= &vs
->vqs
[i
].vq
;
1390 mutex_lock(&vq
->mutex
);
1391 vq
->private_data
= NULL
;
1392 mutex_unlock(&vq
->mutex
);
1396 * Act as synchronize_rcu to make sure access to
1397 * old vs->vs_tpg is finished.
1399 vhost_scsi_flush(vs
);
1402 WARN_ON(vs
->vs_events_nr
);
1403 mutex_unlock(&vs
->dev
.mutex
);
1404 mutex_unlock(&tcm_vhost_mutex
);
1408 mutex_unlock(&tpg
->tv_tpg_mutex
);
1410 mutex_unlock(&vs
->dev
.mutex
);
1411 mutex_unlock(&tcm_vhost_mutex
);
1415 static int vhost_scsi_set_features(struct vhost_scsi
*vs
, u64 features
)
1417 if (features
& ~VHOST_SCSI_FEATURES
)
1420 mutex_lock(&vs
->dev
.mutex
);
1421 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
1422 !vhost_log_access_ok(&vs
->dev
)) {
1423 mutex_unlock(&vs
->dev
.mutex
);
1426 vs
->dev
.acked_features
= features
;
1428 vhost_scsi_flush(vs
);
1429 mutex_unlock(&vs
->dev
.mutex
);
1433 static void vhost_scsi_free(struct vhost_scsi
*vs
)
1435 if (is_vmalloc_addr(vs
))
1441 static int vhost_scsi_open(struct inode
*inode
, struct file
*f
)
1443 struct vhost_scsi
*vs
;
1444 struct vhost_virtqueue
**vqs
;
1447 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
| __GFP_NOWARN
| __GFP_REPEAT
);
1449 vs
= vzalloc(sizeof(*vs
));
1454 vqs
= kmalloc(VHOST_SCSI_MAX_VQ
* sizeof(*vqs
), GFP_KERNEL
);
1458 vhost_work_init(&vs
->vs_completion_work
, vhost_scsi_complete_cmd_work
);
1459 vhost_work_init(&vs
->vs_event_work
, tcm_vhost_evt_work
);
1461 vs
->vs_events_nr
= 0;
1462 vs
->vs_events_missed
= false;
1464 vqs
[VHOST_SCSI_VQ_CTL
] = &vs
->vqs
[VHOST_SCSI_VQ_CTL
].vq
;
1465 vqs
[VHOST_SCSI_VQ_EVT
] = &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1466 vs
->vqs
[VHOST_SCSI_VQ_CTL
].vq
.handle_kick
= vhost_scsi_ctl_handle_kick
;
1467 vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
.handle_kick
= vhost_scsi_evt_handle_kick
;
1468 for (i
= VHOST_SCSI_VQ_IO
; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1469 vqs
[i
] = &vs
->vqs
[i
].vq
;
1470 vs
->vqs
[i
].vq
.handle_kick
= vhost_scsi_handle_kick
;
1472 vhost_dev_init(&vs
->dev
, vqs
, VHOST_SCSI_MAX_VQ
);
1474 tcm_vhost_init_inflight(vs
, NULL
);
1476 f
->private_data
= vs
;
1480 vhost_scsi_free(vs
);
1485 static int vhost_scsi_release(struct inode
*inode
, struct file
*f
)
1487 struct vhost_scsi
*vs
= f
->private_data
;
1488 struct vhost_scsi_target t
;
1490 mutex_lock(&vs
->dev
.mutex
);
1491 memcpy(t
.vhost_wwpn
, vs
->vs_vhost_wwpn
, sizeof(t
.vhost_wwpn
));
1492 mutex_unlock(&vs
->dev
.mutex
);
1493 vhost_scsi_clear_endpoint(vs
, &t
);
1494 vhost_dev_stop(&vs
->dev
);
1495 vhost_dev_cleanup(&vs
->dev
, false);
1496 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1497 vhost_scsi_flush(vs
);
1499 vhost_scsi_free(vs
);
1504 vhost_scsi_ioctl(struct file
*f
,
1508 struct vhost_scsi
*vs
= f
->private_data
;
1509 struct vhost_scsi_target backend
;
1510 void __user
*argp
= (void __user
*)arg
;
1511 u64 __user
*featurep
= argp
;
1512 u32 __user
*eventsp
= argp
;
1515 int r
, abi_version
= VHOST_SCSI_ABI_VERSION
;
1516 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1519 case VHOST_SCSI_SET_ENDPOINT
:
1520 if (copy_from_user(&backend
, argp
, sizeof backend
))
1522 if (backend
.reserved
!= 0)
1525 return vhost_scsi_set_endpoint(vs
, &backend
);
1526 case VHOST_SCSI_CLEAR_ENDPOINT
:
1527 if (copy_from_user(&backend
, argp
, sizeof backend
))
1529 if (backend
.reserved
!= 0)
1532 return vhost_scsi_clear_endpoint(vs
, &backend
);
1533 case VHOST_SCSI_GET_ABI_VERSION
:
1534 if (copy_to_user(argp
, &abi_version
, sizeof abi_version
))
1537 case VHOST_SCSI_SET_EVENTS_MISSED
:
1538 if (get_user(events_missed
, eventsp
))
1540 mutex_lock(&vq
->mutex
);
1541 vs
->vs_events_missed
= events_missed
;
1542 mutex_unlock(&vq
->mutex
);
1544 case VHOST_SCSI_GET_EVENTS_MISSED
:
1545 mutex_lock(&vq
->mutex
);
1546 events_missed
= vs
->vs_events_missed
;
1547 mutex_unlock(&vq
->mutex
);
1548 if (put_user(events_missed
, eventsp
))
1551 case VHOST_GET_FEATURES
:
1552 features
= VHOST_SCSI_FEATURES
;
1553 if (copy_to_user(featurep
, &features
, sizeof features
))
1556 case VHOST_SET_FEATURES
:
1557 if (copy_from_user(&features
, featurep
, sizeof features
))
1559 return vhost_scsi_set_features(vs
, features
);
1561 mutex_lock(&vs
->dev
.mutex
);
1562 r
= vhost_dev_ioctl(&vs
->dev
, ioctl
, argp
);
1563 /* TODO: flush backend after dev ioctl. */
1564 if (r
== -ENOIOCTLCMD
)
1565 r
= vhost_vring_ioctl(&vs
->dev
, ioctl
, argp
);
1566 mutex_unlock(&vs
->dev
.mutex
);
1571 #ifdef CONFIG_COMPAT
1572 static long vhost_scsi_compat_ioctl(struct file
*f
, unsigned int ioctl
,
1575 return vhost_scsi_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
1579 static const struct file_operations vhost_scsi_fops
= {
1580 .owner
= THIS_MODULE
,
1581 .release
= vhost_scsi_release
,
1582 .unlocked_ioctl
= vhost_scsi_ioctl
,
1583 #ifdef CONFIG_COMPAT
1584 .compat_ioctl
= vhost_scsi_compat_ioctl
,
1586 .open
= vhost_scsi_open
,
1587 .llseek
= noop_llseek
,
1590 static struct miscdevice vhost_scsi_misc
= {
1596 static int __init
vhost_scsi_register(void)
1598 return misc_register(&vhost_scsi_misc
);
1601 static int vhost_scsi_deregister(void)
1603 return misc_deregister(&vhost_scsi_misc
);
1606 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport
*tport
)
1608 switch (tport
->tport_proto_id
) {
1609 case SCSI_PROTOCOL_SAS
:
1611 case SCSI_PROTOCOL_FCP
:
1613 case SCSI_PROTOCOL_ISCSI
:
1623 tcm_vhost_do_plug(struct tcm_vhost_tpg
*tpg
,
1624 struct se_lun
*lun
, bool plug
)
1627 struct vhost_scsi
*vs
= tpg
->vhost_scsi
;
1628 struct vhost_virtqueue
*vq
;
1634 mutex_lock(&vs
->dev
.mutex
);
1635 if (!vhost_has_feature(&vs
->dev
, VIRTIO_SCSI_F_HOTPLUG
)) {
1636 mutex_unlock(&vs
->dev
.mutex
);
1641 reason
= VIRTIO_SCSI_EVT_RESET_RESCAN
;
1643 reason
= VIRTIO_SCSI_EVT_RESET_REMOVED
;
1645 vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1646 mutex_lock(&vq
->mutex
);
1647 tcm_vhost_send_evt(vs
, tpg
, lun
,
1648 VIRTIO_SCSI_T_TRANSPORT_RESET
, reason
);
1649 mutex_unlock(&vq
->mutex
);
1650 mutex_unlock(&vs
->dev
.mutex
);
1653 static void tcm_vhost_hotplug(struct tcm_vhost_tpg
*tpg
, struct se_lun
*lun
)
1655 tcm_vhost_do_plug(tpg
, lun
, true);
1658 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg
*tpg
, struct se_lun
*lun
)
1660 tcm_vhost_do_plug(tpg
, lun
, false);
1663 static int tcm_vhost_port_link(struct se_portal_group
*se_tpg
,
1666 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1667 struct tcm_vhost_tpg
, se_tpg
);
1669 mutex_lock(&tcm_vhost_mutex
);
1671 mutex_lock(&tpg
->tv_tpg_mutex
);
1672 tpg
->tv_tpg_port_count
++;
1673 mutex_unlock(&tpg
->tv_tpg_mutex
);
1675 tcm_vhost_hotplug(tpg
, lun
);
1677 mutex_unlock(&tcm_vhost_mutex
);
1682 static void tcm_vhost_port_unlink(struct se_portal_group
*se_tpg
,
1685 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1686 struct tcm_vhost_tpg
, se_tpg
);
1688 mutex_lock(&tcm_vhost_mutex
);
1690 mutex_lock(&tpg
->tv_tpg_mutex
);
1691 tpg
->tv_tpg_port_count
--;
1692 mutex_unlock(&tpg
->tv_tpg_mutex
);
1694 tcm_vhost_hotunplug(tpg
, lun
);
1696 mutex_unlock(&tcm_vhost_mutex
);
1699 static struct se_node_acl
*
1700 tcm_vhost_make_nodeacl(struct se_portal_group
*se_tpg
,
1701 struct config_group
*group
,
1704 struct se_node_acl
*se_nacl
, *se_nacl_new
;
1705 struct tcm_vhost_nacl
*nacl
;
1709 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1710 return ERR_PTR(-EINVAL); */
1711 se_nacl_new
= tcm_vhost_alloc_fabric_acl(se_tpg
);
1713 return ERR_PTR(-ENOMEM
);
1717 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1718 * when converting a NodeACL from demo mode -> explict
1720 se_nacl
= core_tpg_add_initiator_node_acl(se_tpg
, se_nacl_new
,
1722 if (IS_ERR(se_nacl
)) {
1723 tcm_vhost_release_fabric_acl(se_tpg
, se_nacl_new
);
1727 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1729 nacl
= container_of(se_nacl
, struct tcm_vhost_nacl
, se_node_acl
);
1730 nacl
->iport_wwpn
= wwpn
;
1735 static void tcm_vhost_drop_nodeacl(struct se_node_acl
*se_acl
)
1737 struct tcm_vhost_nacl
*nacl
= container_of(se_acl
,
1738 struct tcm_vhost_nacl
, se_node_acl
);
1739 core_tpg_del_initiator_node_acl(se_acl
->se_tpg
, se_acl
, 1);
1743 static void tcm_vhost_free_cmd_map_res(struct tcm_vhost_nexus
*nexus
,
1744 struct se_session
*se_sess
)
1746 struct tcm_vhost_cmd
*tv_cmd
;
1749 if (!se_sess
->sess_cmd_map
)
1752 for (i
= 0; i
< TCM_VHOST_DEFAULT_TAGS
; i
++) {
1753 tv_cmd
= &((struct tcm_vhost_cmd
*)se_sess
->sess_cmd_map
)[i
];
1755 kfree(tv_cmd
->tvc_sgl
);
1756 kfree(tv_cmd
->tvc_prot_sgl
);
1757 kfree(tv_cmd
->tvc_upages
);
1761 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg
*tpg
,
1764 struct se_portal_group
*se_tpg
;
1765 struct se_session
*se_sess
;
1766 struct tcm_vhost_nexus
*tv_nexus
;
1767 struct tcm_vhost_cmd
*tv_cmd
;
1770 mutex_lock(&tpg
->tv_tpg_mutex
);
1771 if (tpg
->tpg_nexus
) {
1772 mutex_unlock(&tpg
->tv_tpg_mutex
);
1773 pr_debug("tpg->tpg_nexus already exists\n");
1776 se_tpg
= &tpg
->se_tpg
;
1778 tv_nexus
= kzalloc(sizeof(struct tcm_vhost_nexus
), GFP_KERNEL
);
1780 mutex_unlock(&tpg
->tv_tpg_mutex
);
1781 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1785 * Initialize the struct se_session pointer and setup tagpool
1786 * for struct tcm_vhost_cmd descriptors
1788 tv_nexus
->tvn_se_sess
= transport_init_session_tags(
1789 TCM_VHOST_DEFAULT_TAGS
,
1790 sizeof(struct tcm_vhost_cmd
),
1791 TARGET_PROT_NORMAL
);
1792 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1793 mutex_unlock(&tpg
->tv_tpg_mutex
);
1797 se_sess
= tv_nexus
->tvn_se_sess
;
1798 for (i
= 0; i
< TCM_VHOST_DEFAULT_TAGS
; i
++) {
1799 tv_cmd
= &((struct tcm_vhost_cmd
*)se_sess
->sess_cmd_map
)[i
];
1801 tv_cmd
->tvc_sgl
= kzalloc(sizeof(struct scatterlist
) *
1802 TCM_VHOST_PREALLOC_SGLS
, GFP_KERNEL
);
1803 if (!tv_cmd
->tvc_sgl
) {
1804 mutex_unlock(&tpg
->tv_tpg_mutex
);
1805 pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1809 tv_cmd
->tvc_upages
= kzalloc(sizeof(struct page
*) *
1810 TCM_VHOST_PREALLOC_UPAGES
, GFP_KERNEL
);
1811 if (!tv_cmd
->tvc_upages
) {
1812 mutex_unlock(&tpg
->tv_tpg_mutex
);
1813 pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1817 tv_cmd
->tvc_prot_sgl
= kzalloc(sizeof(struct scatterlist
) *
1818 TCM_VHOST_PREALLOC_PROT_SGLS
, GFP_KERNEL
);
1819 if (!tv_cmd
->tvc_prot_sgl
) {
1820 mutex_unlock(&tpg
->tv_tpg_mutex
);
1821 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1826 * Since we are running in 'demo mode' this call with generate a
1827 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1828 * the SCSI Initiator port name of the passed configfs group 'name'.
1830 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1831 se_tpg
, (unsigned char *)name
);
1832 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1833 mutex_unlock(&tpg
->tv_tpg_mutex
);
1834 pr_debug("core_tpg_check_initiator_node_acl() failed"
1839 * Now register the TCM vhost virtual I_T Nexus as active with the
1840 * call to __transport_register_session()
1842 __transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1843 tv_nexus
->tvn_se_sess
, tv_nexus
);
1844 tpg
->tpg_nexus
= tv_nexus
;
1846 mutex_unlock(&tpg
->tv_tpg_mutex
);
1850 tcm_vhost_free_cmd_map_res(tv_nexus
, se_sess
);
1851 transport_free_session(se_sess
);
1856 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg
*tpg
)
1858 struct se_session
*se_sess
;
1859 struct tcm_vhost_nexus
*tv_nexus
;
1861 mutex_lock(&tpg
->tv_tpg_mutex
);
1862 tv_nexus
= tpg
->tpg_nexus
;
1864 mutex_unlock(&tpg
->tv_tpg_mutex
);
1868 se_sess
= tv_nexus
->tvn_se_sess
;
1870 mutex_unlock(&tpg
->tv_tpg_mutex
);
1874 if (tpg
->tv_tpg_port_count
!= 0) {
1875 mutex_unlock(&tpg
->tv_tpg_mutex
);
1876 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1877 " active TPG port count: %d\n",
1878 tpg
->tv_tpg_port_count
);
1882 if (tpg
->tv_tpg_vhost_count
!= 0) {
1883 mutex_unlock(&tpg
->tv_tpg_mutex
);
1884 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1885 " active TPG vhost count: %d\n",
1886 tpg
->tv_tpg_vhost_count
);
1890 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1891 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg
->tport
),
1892 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1894 tcm_vhost_free_cmd_map_res(tv_nexus
, se_sess
);
1896 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1898 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1899 tpg
->tpg_nexus
= NULL
;
1900 mutex_unlock(&tpg
->tv_tpg_mutex
);
1906 static ssize_t
tcm_vhost_tpg_show_nexus(struct se_portal_group
*se_tpg
,
1909 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1910 struct tcm_vhost_tpg
, se_tpg
);
1911 struct tcm_vhost_nexus
*tv_nexus
;
1914 mutex_lock(&tpg
->tv_tpg_mutex
);
1915 tv_nexus
= tpg
->tpg_nexus
;
1917 mutex_unlock(&tpg
->tv_tpg_mutex
);
1920 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1921 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1922 mutex_unlock(&tpg
->tv_tpg_mutex
);
1927 static ssize_t
tcm_vhost_tpg_store_nexus(struct se_portal_group
*se_tpg
,
1931 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1932 struct tcm_vhost_tpg
, se_tpg
);
1933 struct tcm_vhost_tport
*tport_wwn
= tpg
->tport
;
1934 unsigned char i_port
[TCM_VHOST_NAMELEN
], *ptr
, *port_ptr
;
1937 * Shutdown the active I_T nexus if 'NULL' is passed..
1939 if (!strncmp(page
, "NULL", 4)) {
1940 ret
= tcm_vhost_drop_nexus(tpg
);
1941 return (!ret
) ? count
: ret
;
1944 * Otherwise make sure the passed virtual Initiator port WWN matches
1945 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1946 * tcm_vhost_make_nexus().
1948 if (strlen(page
) >= TCM_VHOST_NAMELEN
) {
1949 pr_err("Emulated NAA Sas Address: %s, exceeds"
1950 " max: %d\n", page
, TCM_VHOST_NAMELEN
);
1953 snprintf(&i_port
[0], TCM_VHOST_NAMELEN
, "%s", page
);
1955 ptr
= strstr(i_port
, "naa.");
1957 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1958 pr_err("Passed SAS Initiator Port %s does not"
1959 " match target port protoid: %s\n", i_port
,
1960 tcm_vhost_dump_proto_id(tport_wwn
));
1963 port_ptr
= &i_port
[0];
1966 ptr
= strstr(i_port
, "fc.");
1968 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1969 pr_err("Passed FCP Initiator Port %s does not"
1970 " match target port protoid: %s\n", i_port
,
1971 tcm_vhost_dump_proto_id(tport_wwn
));
1974 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1977 ptr
= strstr(i_port
, "iqn.");
1979 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1980 pr_err("Passed iSCSI Initiator Port %s does not"
1981 " match target port protoid: %s\n", i_port
,
1982 tcm_vhost_dump_proto_id(tport_wwn
));
1985 port_ptr
= &i_port
[0];
1988 pr_err("Unable to locate prefix for emulated Initiator Port:"
1992 * Clear any trailing newline for the NAA WWN
1995 if (i_port
[strlen(i_port
)-1] == '\n')
1996 i_port
[strlen(i_port
)-1] = '\0';
1998 ret
= tcm_vhost_make_nexus(tpg
, port_ptr
);
2005 TF_TPG_BASE_ATTR(tcm_vhost
, nexus
, S_IRUGO
| S_IWUSR
);
2007 static struct configfs_attribute
*tcm_vhost_tpg_attrs
[] = {
2008 &tcm_vhost_tpg_nexus
.attr
,
2012 static struct se_portal_group
*
2013 tcm_vhost_make_tpg(struct se_wwn
*wwn
,
2014 struct config_group
*group
,
2017 struct tcm_vhost_tport
*tport
= container_of(wwn
,
2018 struct tcm_vhost_tport
, tport_wwn
);
2020 struct tcm_vhost_tpg
*tpg
;
2024 if (strstr(name
, "tpgt_") != name
)
2025 return ERR_PTR(-EINVAL
);
2026 if (kstrtoul(name
+ 5, 10, &tpgt
) || tpgt
> UINT_MAX
)
2027 return ERR_PTR(-EINVAL
);
2029 tpg
= kzalloc(sizeof(struct tcm_vhost_tpg
), GFP_KERNEL
);
2031 pr_err("Unable to allocate struct tcm_vhost_tpg");
2032 return ERR_PTR(-ENOMEM
);
2034 mutex_init(&tpg
->tv_tpg_mutex
);
2035 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
2037 tpg
->tport_tpgt
= tpgt
;
2039 ret
= core_tpg_register(&tcm_vhost_fabric_configfs
->tf_ops
, wwn
,
2040 &tpg
->se_tpg
, tpg
, TRANSPORT_TPG_TYPE_NORMAL
);
2045 mutex_lock(&tcm_vhost_mutex
);
2046 list_add_tail(&tpg
->tv_tpg_list
, &tcm_vhost_list
);
2047 mutex_unlock(&tcm_vhost_mutex
);
2049 return &tpg
->se_tpg
;
2052 static void tcm_vhost_drop_tpg(struct se_portal_group
*se_tpg
)
2054 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
2055 struct tcm_vhost_tpg
, se_tpg
);
2057 mutex_lock(&tcm_vhost_mutex
);
2058 list_del(&tpg
->tv_tpg_list
);
2059 mutex_unlock(&tcm_vhost_mutex
);
2061 * Release the virtual I_T Nexus for this vhost TPG
2063 tcm_vhost_drop_nexus(tpg
);
2065 * Deregister the se_tpg from TCM..
2067 core_tpg_deregister(se_tpg
);
2071 static struct se_wwn
*
2072 tcm_vhost_make_tport(struct target_fabric_configfs
*tf
,
2073 struct config_group
*group
,
2076 struct tcm_vhost_tport
*tport
;
2081 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
2082 return ERR_PTR(-EINVAL); */
2084 tport
= kzalloc(sizeof(struct tcm_vhost_tport
), GFP_KERNEL
);
2086 pr_err("Unable to allocate struct tcm_vhost_tport");
2087 return ERR_PTR(-ENOMEM
);
2089 tport
->tport_wwpn
= wwpn
;
2091 * Determine the emulated Protocol Identifier and Target Port Name
2092 * based on the incoming configfs directory name.
2094 ptr
= strstr(name
, "naa.");
2096 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
2099 ptr
= strstr(name
, "fc.");
2101 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
2102 off
= 3; /* Skip over "fc." */
2105 ptr
= strstr(name
, "iqn.");
2107 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
2111 pr_err("Unable to locate prefix for emulated Target Port:"
2114 return ERR_PTR(-EINVAL
);
2117 if (strlen(name
) >= TCM_VHOST_NAMELEN
) {
2118 pr_err("Emulated %s Address: %s, exceeds"
2119 " max: %d\n", name
, tcm_vhost_dump_proto_id(tport
),
2122 return ERR_PTR(-EINVAL
);
2124 snprintf(&tport
->tport_name
[0], TCM_VHOST_NAMELEN
, "%s", &name
[off
]);
2126 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
2127 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
), name
);
2129 return &tport
->tport_wwn
;
2132 static void tcm_vhost_drop_tport(struct se_wwn
*wwn
)
2134 struct tcm_vhost_tport
*tport
= container_of(wwn
,
2135 struct tcm_vhost_tport
, tport_wwn
);
2137 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2138 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
),
2145 tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs
*tf
,
2148 return sprintf(page
, "TCM_VHOST fabric module %s on %s/%s"
2149 "on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
2150 utsname()->machine
);
2153 TF_WWN_ATTR_RO(tcm_vhost
, version
);
2155 static struct configfs_attribute
*tcm_vhost_wwn_attrs
[] = {
2156 &tcm_vhost_wwn_version
.attr
,
2160 static struct target_core_fabric_ops tcm_vhost_ops
= {
2161 .get_fabric_name
= tcm_vhost_get_fabric_name
,
2162 .get_fabric_proto_ident
= tcm_vhost_get_fabric_proto_ident
,
2163 .tpg_get_wwn
= tcm_vhost_get_fabric_wwn
,
2164 .tpg_get_tag
= tcm_vhost_get_tag
,
2165 .tpg_get_default_depth
= tcm_vhost_get_default_depth
,
2166 .tpg_get_pr_transport_id
= tcm_vhost_get_pr_transport_id
,
2167 .tpg_get_pr_transport_id_len
= tcm_vhost_get_pr_transport_id_len
,
2168 .tpg_parse_pr_out_transport_id
= tcm_vhost_parse_pr_out_transport_id
,
2169 .tpg_check_demo_mode
= tcm_vhost_check_true
,
2170 .tpg_check_demo_mode_cache
= tcm_vhost_check_true
,
2171 .tpg_check_demo_mode_write_protect
= tcm_vhost_check_false
,
2172 .tpg_check_prod_mode_write_protect
= tcm_vhost_check_false
,
2173 .tpg_alloc_fabric_acl
= tcm_vhost_alloc_fabric_acl
,
2174 .tpg_release_fabric_acl
= tcm_vhost_release_fabric_acl
,
2175 .tpg_get_inst_index
= tcm_vhost_tpg_get_inst_index
,
2176 .release_cmd
= tcm_vhost_release_cmd
,
2177 .check_stop_free
= vhost_scsi_check_stop_free
,
2178 .shutdown_session
= tcm_vhost_shutdown_session
,
2179 .close_session
= tcm_vhost_close_session
,
2180 .sess_get_index
= tcm_vhost_sess_get_index
,
2181 .sess_get_initiator_sid
= NULL
,
2182 .write_pending
= tcm_vhost_write_pending
,
2183 .write_pending_status
= tcm_vhost_write_pending_status
,
2184 .set_default_node_attributes
= tcm_vhost_set_default_node_attrs
,
2185 .get_task_tag
= tcm_vhost_get_task_tag
,
2186 .get_cmd_state
= tcm_vhost_get_cmd_state
,
2187 .queue_data_in
= tcm_vhost_queue_data_in
,
2188 .queue_status
= tcm_vhost_queue_status
,
2189 .queue_tm_rsp
= tcm_vhost_queue_tm_rsp
,
2190 .aborted_task
= tcm_vhost_aborted_task
,
2192 * Setup callers for generic logic in target_core_fabric_configfs.c
2194 .fabric_make_wwn
= tcm_vhost_make_tport
,
2195 .fabric_drop_wwn
= tcm_vhost_drop_tport
,
2196 .fabric_make_tpg
= tcm_vhost_make_tpg
,
2197 .fabric_drop_tpg
= tcm_vhost_drop_tpg
,
2198 .fabric_post_link
= tcm_vhost_port_link
,
2199 .fabric_pre_unlink
= tcm_vhost_port_unlink
,
2200 .fabric_make_np
= NULL
,
2201 .fabric_drop_np
= NULL
,
2202 .fabric_make_nodeacl
= tcm_vhost_make_nodeacl
,
2203 .fabric_drop_nodeacl
= tcm_vhost_drop_nodeacl
,
2206 static int tcm_vhost_register_configfs(void)
2208 struct target_fabric_configfs
*fabric
;
2211 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2212 " on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
2213 utsname()->machine
);
2215 * Register the top level struct config_item_type with TCM core
2217 fabric
= target_fabric_configfs_init(THIS_MODULE
, "vhost");
2218 if (IS_ERR(fabric
)) {
2219 pr_err("target_fabric_configfs_init() failed\n");
2220 return PTR_ERR(fabric
);
2223 * Setup fabric->tf_ops from our local tcm_vhost_ops
2225 fabric
->tf_ops
= tcm_vhost_ops
;
2227 * Setup default attribute lists for various fabric->tf_cit_tmpl
2229 fabric
->tf_cit_tmpl
.tfc_wwn_cit
.ct_attrs
= tcm_vhost_wwn_attrs
;
2230 fabric
->tf_cit_tmpl
.tfc_tpg_base_cit
.ct_attrs
= tcm_vhost_tpg_attrs
;
2231 fabric
->tf_cit_tmpl
.tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
2232 fabric
->tf_cit_tmpl
.tfc_tpg_param_cit
.ct_attrs
= NULL
;
2233 fabric
->tf_cit_tmpl
.tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
2234 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_base_cit
.ct_attrs
= NULL
;
2235 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
2236 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
2237 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
2239 * Register the fabric for use within TCM
2241 ret
= target_fabric_configfs_register(fabric
);
2243 pr_err("target_fabric_configfs_register() failed"
2244 " for TCM_VHOST\n");
2248 * Setup our local pointer to *fabric
2250 tcm_vhost_fabric_configfs
= fabric
;
2251 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2255 static void tcm_vhost_deregister_configfs(void)
2257 if (!tcm_vhost_fabric_configfs
)
2260 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs
);
2261 tcm_vhost_fabric_configfs
= NULL
;
2262 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2265 static int __init
tcm_vhost_init(void)
2269 * Use our own dedicated workqueue for submitting I/O into
2270 * target core to avoid contention within system_wq.
2272 tcm_vhost_workqueue
= alloc_workqueue("tcm_vhost", 0, 0);
2273 if (!tcm_vhost_workqueue
)
2276 ret
= vhost_scsi_register();
2278 goto out_destroy_workqueue
;
2280 ret
= tcm_vhost_register_configfs();
2282 goto out_vhost_scsi_deregister
;
2286 out_vhost_scsi_deregister
:
2287 vhost_scsi_deregister();
2288 out_destroy_workqueue
:
2289 destroy_workqueue(tcm_vhost_workqueue
);
2294 static void tcm_vhost_exit(void)
2296 tcm_vhost_deregister_configfs();
2297 vhost_scsi_deregister();
2298 destroy_workqueue(tcm_vhost_workqueue
);
2301 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2302 MODULE_ALIAS("tcm_vhost");
2303 MODULE_LICENSE("GPL");
2304 module_init(tcm_vhost_init
);
2305 module_exit(tcm_vhost_exit
);