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,
171 /* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
173 VHOST_SCSI_FEATURES
= VHOST_FEATURES
| (1ULL << VIRTIO_SCSI_F_HOTPLUG
) |
174 (1ULL << VIRTIO_SCSI_F_T10_PI
)
177 #define VHOST_SCSI_MAX_TARGET 256
178 #define VHOST_SCSI_MAX_VQ 128
179 #define VHOST_SCSI_MAX_EVENT 128
181 struct vhost_scsi_virtqueue
{
182 struct vhost_virtqueue vq
;
184 * Reference counting for inflight reqs, used for flush operation. At
185 * each time, one reference tracks new commands submitted, while we
186 * wait for another one to reach 0.
188 struct vhost_scsi_inflight inflights
[2];
190 * Indicate current inflight in use, protected by vq->mutex.
191 * Writers must also take dev mutex and flush under it.
197 /* Protected by vhost_scsi->dev.mutex */
198 struct tcm_vhost_tpg
**vs_tpg
;
199 char vs_vhost_wwpn
[TRANSPORT_IQN_LEN
];
201 struct vhost_dev dev
;
202 struct vhost_scsi_virtqueue vqs
[VHOST_SCSI_MAX_VQ
];
204 struct vhost_work vs_completion_work
; /* cmd completion work item */
205 struct llist_head vs_completion_list
; /* cmd completion queue */
207 struct vhost_work vs_event_work
; /* evt injection work item */
208 struct llist_head vs_event_list
; /* evt injection queue */
210 bool vs_events_missed
; /* any missed events, protected by vq->mutex */
211 int vs_events_nr
; /* num of pending events, protected by vq->mutex */
214 /* Local pointer to allocated TCM configfs fabric module */
215 static struct target_fabric_configfs
*tcm_vhost_fabric_configfs
;
217 static struct workqueue_struct
*tcm_vhost_workqueue
;
219 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
220 static DEFINE_MUTEX(tcm_vhost_mutex
);
221 static LIST_HEAD(tcm_vhost_list
);
223 static int iov_num_pages(struct iovec
*iov
)
225 return (PAGE_ALIGN((unsigned long)iov
->iov_base
+ iov
->iov_len
) -
226 ((unsigned long)iov
->iov_base
& PAGE_MASK
)) >> PAGE_SHIFT
;
229 static void tcm_vhost_done_inflight(struct kref
*kref
)
231 struct vhost_scsi_inflight
*inflight
;
233 inflight
= container_of(kref
, struct vhost_scsi_inflight
, kref
);
234 complete(&inflight
->comp
);
237 static void tcm_vhost_init_inflight(struct vhost_scsi
*vs
,
238 struct vhost_scsi_inflight
*old_inflight
[])
240 struct vhost_scsi_inflight
*new_inflight
;
241 struct vhost_virtqueue
*vq
;
244 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
247 mutex_lock(&vq
->mutex
);
249 /* store old infight */
250 idx
= vs
->vqs
[i
].inflight_idx
;
252 old_inflight
[i
] = &vs
->vqs
[i
].inflights
[idx
];
254 /* setup new infight */
255 vs
->vqs
[i
].inflight_idx
= idx
^ 1;
256 new_inflight
= &vs
->vqs
[i
].inflights
[idx
^ 1];
257 kref_init(&new_inflight
->kref
);
258 init_completion(&new_inflight
->comp
);
260 mutex_unlock(&vq
->mutex
);
264 static struct vhost_scsi_inflight
*
265 tcm_vhost_get_inflight(struct vhost_virtqueue
*vq
)
267 struct vhost_scsi_inflight
*inflight
;
268 struct vhost_scsi_virtqueue
*svq
;
270 svq
= container_of(vq
, struct vhost_scsi_virtqueue
, vq
);
271 inflight
= &svq
->inflights
[svq
->inflight_idx
];
272 kref_get(&inflight
->kref
);
277 static void tcm_vhost_put_inflight(struct vhost_scsi_inflight
*inflight
)
279 kref_put(&inflight
->kref
, tcm_vhost_done_inflight
);
282 static int tcm_vhost_check_true(struct se_portal_group
*se_tpg
)
287 static int tcm_vhost_check_false(struct se_portal_group
*se_tpg
)
292 static char *tcm_vhost_get_fabric_name(void)
297 static u8
tcm_vhost_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
299 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
300 struct tcm_vhost_tpg
, se_tpg
);
301 struct tcm_vhost_tport
*tport
= tpg
->tport
;
303 switch (tport
->tport_proto_id
) {
304 case SCSI_PROTOCOL_SAS
:
305 return sas_get_fabric_proto_ident(se_tpg
);
306 case SCSI_PROTOCOL_FCP
:
307 return fc_get_fabric_proto_ident(se_tpg
);
308 case SCSI_PROTOCOL_ISCSI
:
309 return iscsi_get_fabric_proto_ident(se_tpg
);
311 pr_err("Unknown tport_proto_id: 0x%02x, using"
312 " SAS emulation\n", tport
->tport_proto_id
);
316 return sas_get_fabric_proto_ident(se_tpg
);
319 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group
*se_tpg
)
321 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
322 struct tcm_vhost_tpg
, se_tpg
);
323 struct tcm_vhost_tport
*tport
= tpg
->tport
;
325 return &tport
->tport_name
[0];
328 static u16
tcm_vhost_get_tag(struct se_portal_group
*se_tpg
)
330 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
331 struct tcm_vhost_tpg
, se_tpg
);
332 return tpg
->tport_tpgt
;
335 static u32
tcm_vhost_get_default_depth(struct se_portal_group
*se_tpg
)
341 tcm_vhost_get_pr_transport_id(struct se_portal_group
*se_tpg
,
342 struct se_node_acl
*se_nacl
,
343 struct t10_pr_registration
*pr_reg
,
347 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
348 struct tcm_vhost_tpg
, se_tpg
);
349 struct tcm_vhost_tport
*tport
= tpg
->tport
;
351 switch (tport
->tport_proto_id
) {
352 case SCSI_PROTOCOL_SAS
:
353 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
355 case SCSI_PROTOCOL_FCP
:
356 return fc_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
358 case SCSI_PROTOCOL_ISCSI
:
359 return iscsi_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
362 pr_err("Unknown tport_proto_id: 0x%02x, using"
363 " SAS emulation\n", tport
->tport_proto_id
);
367 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
372 tcm_vhost_get_pr_transport_id_len(struct se_portal_group
*se_tpg
,
373 struct se_node_acl
*se_nacl
,
374 struct t10_pr_registration
*pr_reg
,
377 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
378 struct tcm_vhost_tpg
, se_tpg
);
379 struct tcm_vhost_tport
*tport
= tpg
->tport
;
381 switch (tport
->tport_proto_id
) {
382 case SCSI_PROTOCOL_SAS
:
383 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
385 case SCSI_PROTOCOL_FCP
:
386 return fc_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
388 case SCSI_PROTOCOL_ISCSI
:
389 return iscsi_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
392 pr_err("Unknown tport_proto_id: 0x%02x, using"
393 " SAS emulation\n", tport
->tport_proto_id
);
397 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
402 tcm_vhost_parse_pr_out_transport_id(struct se_portal_group
*se_tpg
,
405 char **port_nexus_ptr
)
407 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
408 struct tcm_vhost_tpg
, se_tpg
);
409 struct tcm_vhost_tport
*tport
= tpg
->tport
;
411 switch (tport
->tport_proto_id
) {
412 case SCSI_PROTOCOL_SAS
:
413 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
415 case SCSI_PROTOCOL_FCP
:
416 return fc_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
418 case SCSI_PROTOCOL_ISCSI
:
419 return iscsi_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
422 pr_err("Unknown tport_proto_id: 0x%02x, using"
423 " SAS emulation\n", tport
->tport_proto_id
);
427 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
431 static struct se_node_acl
*
432 tcm_vhost_alloc_fabric_acl(struct se_portal_group
*se_tpg
)
434 struct tcm_vhost_nacl
*nacl
;
436 nacl
= kzalloc(sizeof(struct tcm_vhost_nacl
), GFP_KERNEL
);
438 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
442 return &nacl
->se_node_acl
;
446 tcm_vhost_release_fabric_acl(struct se_portal_group
*se_tpg
,
447 struct se_node_acl
*se_nacl
)
449 struct tcm_vhost_nacl
*nacl
= container_of(se_nacl
,
450 struct tcm_vhost_nacl
, se_node_acl
);
454 static u32
tcm_vhost_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
459 static void tcm_vhost_release_cmd(struct se_cmd
*se_cmd
)
461 struct tcm_vhost_cmd
*tv_cmd
= container_of(se_cmd
,
462 struct tcm_vhost_cmd
, tvc_se_cmd
);
463 struct se_session
*se_sess
= se_cmd
->se_sess
;
466 if (tv_cmd
->tvc_sgl_count
) {
467 for (i
= 0; i
< tv_cmd
->tvc_sgl_count
; i
++)
468 put_page(sg_page(&tv_cmd
->tvc_sgl
[i
]));
470 if (tv_cmd
->tvc_prot_sgl_count
) {
471 for (i
= 0; i
< tv_cmd
->tvc_prot_sgl_count
; i
++)
472 put_page(sg_page(&tv_cmd
->tvc_prot_sgl
[i
]));
475 tcm_vhost_put_inflight(tv_cmd
->inflight
);
476 percpu_ida_free(&se_sess
->sess_tag_pool
, se_cmd
->map_tag
);
479 static int tcm_vhost_shutdown_session(struct se_session
*se_sess
)
484 static void tcm_vhost_close_session(struct se_session
*se_sess
)
489 static u32
tcm_vhost_sess_get_index(struct se_session
*se_sess
)
494 static int tcm_vhost_write_pending(struct se_cmd
*se_cmd
)
496 /* Go ahead and process the write immediately */
497 target_execute_cmd(se_cmd
);
501 static int tcm_vhost_write_pending_status(struct se_cmd
*se_cmd
)
506 static void tcm_vhost_set_default_node_attrs(struct se_node_acl
*nacl
)
511 static u32
tcm_vhost_get_task_tag(struct se_cmd
*se_cmd
)
516 static int tcm_vhost_get_cmd_state(struct se_cmd
*se_cmd
)
521 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd
*cmd
)
523 struct vhost_scsi
*vs
= cmd
->tvc_vhost
;
525 llist_add(&cmd
->tvc_completion_list
, &vs
->vs_completion_list
);
527 vhost_work_queue(&vs
->dev
, &vs
->vs_completion_work
);
530 static int tcm_vhost_queue_data_in(struct se_cmd
*se_cmd
)
532 struct tcm_vhost_cmd
*cmd
= container_of(se_cmd
,
533 struct tcm_vhost_cmd
, tvc_se_cmd
);
534 vhost_scsi_complete_cmd(cmd
);
538 static int tcm_vhost_queue_status(struct se_cmd
*se_cmd
)
540 struct tcm_vhost_cmd
*cmd
= container_of(se_cmd
,
541 struct tcm_vhost_cmd
, tvc_se_cmd
);
542 vhost_scsi_complete_cmd(cmd
);
546 static void tcm_vhost_queue_tm_rsp(struct se_cmd
*se_cmd
)
551 static void tcm_vhost_aborted_task(struct se_cmd
*se_cmd
)
556 static void tcm_vhost_free_evt(struct vhost_scsi
*vs
, struct tcm_vhost_evt
*evt
)
562 static struct tcm_vhost_evt
*
563 tcm_vhost_allocate_evt(struct vhost_scsi
*vs
,
564 u32 event
, u32 reason
)
566 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
567 struct tcm_vhost_evt
*evt
;
569 if (vs
->vs_events_nr
> VHOST_SCSI_MAX_EVENT
) {
570 vs
->vs_events_missed
= true;
574 evt
= kzalloc(sizeof(*evt
), GFP_KERNEL
);
576 vq_err(vq
, "Failed to allocate tcm_vhost_evt\n");
577 vs
->vs_events_missed
= true;
581 evt
->event
.event
= cpu_to_vhost32(vq
, event
);
582 evt
->event
.reason
= cpu_to_vhost32(vq
, reason
);
588 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd
*cmd
)
590 struct se_cmd
*se_cmd
= &cmd
->tvc_se_cmd
;
592 /* TODO locking against target/backend threads? */
593 transport_generic_free_cmd(se_cmd
, 0);
597 static int vhost_scsi_check_stop_free(struct se_cmd
*se_cmd
)
599 return target_put_sess_cmd(se_cmd
->se_sess
, se_cmd
);
603 tcm_vhost_do_evt_work(struct vhost_scsi
*vs
, struct tcm_vhost_evt
*evt
)
605 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
606 struct virtio_scsi_event
*event
= &evt
->event
;
607 struct virtio_scsi_event __user
*eventp
;
611 if (!vq
->private_data
) {
612 vs
->vs_events_missed
= true;
617 vhost_disable_notify(&vs
->dev
, vq
);
618 head
= vhost_get_vq_desc(vq
, vq
->iov
,
619 ARRAY_SIZE(vq
->iov
), &out
, &in
,
622 vs
->vs_events_missed
= true;
625 if (head
== vq
->num
) {
626 if (vhost_enable_notify(&vs
->dev
, vq
))
628 vs
->vs_events_missed
= true;
632 if ((vq
->iov
[out
].iov_len
!= sizeof(struct virtio_scsi_event
))) {
633 vq_err(vq
, "Expecting virtio_scsi_event, got %zu bytes\n",
634 vq
->iov
[out
].iov_len
);
635 vs
->vs_events_missed
= true;
639 if (vs
->vs_events_missed
) {
640 event
->event
|= cpu_to_vhost32(vq
, VIRTIO_SCSI_T_EVENTS_MISSED
);
641 vs
->vs_events_missed
= false;
644 eventp
= vq
->iov
[out
].iov_base
;
645 ret
= __copy_to_user(eventp
, event
, sizeof(*event
));
647 vhost_add_used_and_signal(&vs
->dev
, vq
, head
, 0);
649 vq_err(vq
, "Faulted on tcm_vhost_send_event\n");
652 static void tcm_vhost_evt_work(struct vhost_work
*work
)
654 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
656 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
657 struct tcm_vhost_evt
*evt
;
658 struct llist_node
*llnode
;
660 mutex_lock(&vq
->mutex
);
661 llnode
= llist_del_all(&vs
->vs_event_list
);
663 evt
= llist_entry(llnode
, struct tcm_vhost_evt
, list
);
664 llnode
= llist_next(llnode
);
665 tcm_vhost_do_evt_work(vs
, evt
);
666 tcm_vhost_free_evt(vs
, evt
);
668 mutex_unlock(&vq
->mutex
);
671 /* Fill in status and signal that we are done processing this command
673 * This is scheduled in the vhost work queue so we are called with the owner
674 * process mm and can access the vring.
676 static void vhost_scsi_complete_cmd_work(struct vhost_work
*work
)
678 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
680 DECLARE_BITMAP(signal
, VHOST_SCSI_MAX_VQ
);
681 struct virtio_scsi_cmd_resp v_rsp
;
682 struct tcm_vhost_cmd
*cmd
;
683 struct llist_node
*llnode
;
684 struct se_cmd
*se_cmd
;
687 bitmap_zero(signal
, VHOST_SCSI_MAX_VQ
);
688 llnode
= llist_del_all(&vs
->vs_completion_list
);
690 cmd
= llist_entry(llnode
, struct tcm_vhost_cmd
,
691 tvc_completion_list
);
692 llnode
= llist_next(llnode
);
693 se_cmd
= &cmd
->tvc_se_cmd
;
695 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__
,
696 cmd
, se_cmd
->residual_count
, se_cmd
->scsi_status
);
698 memset(&v_rsp
, 0, sizeof(v_rsp
));
699 v_rsp
.resid
= cpu_to_vhost32(cmd
->tvc_vq
, se_cmd
->residual_count
);
700 /* TODO is status_qualifier field needed? */
701 v_rsp
.status
= se_cmd
->scsi_status
;
702 v_rsp
.sense_len
= cpu_to_vhost32(cmd
->tvc_vq
,
703 se_cmd
->scsi_sense_length
);
704 memcpy(v_rsp
.sense
, cmd
->tvc_sense_buf
,
705 se_cmd
->scsi_sense_length
);
706 ret
= copy_to_user(cmd
->tvc_resp
, &v_rsp
, sizeof(v_rsp
));
707 if (likely(ret
== 0)) {
708 struct vhost_scsi_virtqueue
*q
;
709 vhost_add_used(cmd
->tvc_vq
, cmd
->tvc_vq_desc
, 0);
710 q
= container_of(cmd
->tvc_vq
, struct vhost_scsi_virtqueue
, vq
);
712 __set_bit(vq
, signal
);
714 pr_err("Faulted on virtio_scsi_cmd_resp\n");
716 vhost_scsi_free_cmd(cmd
);
720 while ((vq
= find_next_bit(signal
, VHOST_SCSI_MAX_VQ
, vq
+ 1))
722 vhost_signal(&vs
->dev
, &vs
->vqs
[vq
].vq
);
725 static struct tcm_vhost_cmd
*
726 vhost_scsi_get_tag(struct vhost_virtqueue
*vq
, struct tcm_vhost_tpg
*tpg
,
727 unsigned char *cdb
, u64 scsi_tag
, u16 lun
, u8 task_attr
,
728 u32 exp_data_len
, int data_direction
)
730 struct tcm_vhost_cmd
*cmd
;
731 struct tcm_vhost_nexus
*tv_nexus
;
732 struct se_session
*se_sess
;
733 struct scatterlist
*sg
, *prot_sg
;
737 tv_nexus
= tpg
->tpg_nexus
;
739 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
740 return ERR_PTR(-EIO
);
742 se_sess
= tv_nexus
->tvn_se_sess
;
744 tag
= percpu_ida_alloc(&se_sess
->sess_tag_pool
, TASK_RUNNING
);
746 pr_err("Unable to obtain tag for tcm_vhost_cmd\n");
747 return ERR_PTR(-ENOMEM
);
750 cmd
= &((struct tcm_vhost_cmd
*)se_sess
->sess_cmd_map
)[tag
];
752 prot_sg
= cmd
->tvc_prot_sgl
;
753 pages
= cmd
->tvc_upages
;
754 memset(cmd
, 0, sizeof(struct tcm_vhost_cmd
));
757 cmd
->tvc_prot_sgl
= prot_sg
;
758 cmd
->tvc_upages
= pages
;
759 cmd
->tvc_se_cmd
.map_tag
= tag
;
760 cmd
->tvc_tag
= scsi_tag
;
762 cmd
->tvc_task_attr
= task_attr
;
763 cmd
->tvc_exp_data_len
= exp_data_len
;
764 cmd
->tvc_data_direction
= data_direction
;
765 cmd
->tvc_nexus
= tv_nexus
;
766 cmd
->inflight
= tcm_vhost_get_inflight(vq
);
768 memcpy(cmd
->tvc_cdb
, cdb
, TCM_VHOST_MAX_CDB_SIZE
);
774 * Map a user memory range into a scatterlist
776 * Returns the number of scatterlist entries used or -errno on error.
779 vhost_scsi_map_to_sgl(struct tcm_vhost_cmd
*tv_cmd
,
780 struct scatterlist
*sgl
,
781 unsigned int sgl_count
,
786 unsigned int npages
= 0, pages_nr
, offset
, nbytes
;
787 struct scatterlist
*sg
= sgl
;
788 void __user
*ptr
= iov
->iov_base
;
789 size_t len
= iov
->iov_len
;
792 pages_nr
= iov_num_pages(iov
);
793 if (pages_nr
> sgl_count
) {
794 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
795 " sgl_count: %u\n", pages_nr
, sgl_count
);
798 if (pages_nr
> TCM_VHOST_PREALLOC_UPAGES
) {
799 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
800 " preallocated TCM_VHOST_PREALLOC_UPAGES: %u\n",
801 pages_nr
, TCM_VHOST_PREALLOC_UPAGES
);
805 ret
= get_user_pages_fast((unsigned long)ptr
, pages_nr
, write
, pages
);
806 /* No pages were pinned */
809 /* Less pages pinned than wanted */
810 if (ret
!= pages_nr
) {
811 for (i
= 0; i
< ret
; i
++)
818 offset
= (uintptr_t)ptr
& ~PAGE_MASK
;
819 nbytes
= min_t(unsigned int, PAGE_SIZE
- offset
, len
);
820 sg_set_page(sg
, pages
[npages
], nbytes
, offset
);
832 vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd
*cmd
,
837 struct scatterlist
*sg
= cmd
->tvc_sgl
;
838 unsigned int sgl_count
= 0;
841 for (i
= 0; i
< niov
; i
++)
842 sgl_count
+= iov_num_pages(&iov
[i
]);
844 if (sgl_count
> TCM_VHOST_PREALLOC_SGLS
) {
845 pr_err("vhost_scsi_map_iov_to_sgl() sgl_count: %u greater than"
846 " preallocated TCM_VHOST_PREALLOC_SGLS: %u\n",
847 sgl_count
, TCM_VHOST_PREALLOC_SGLS
);
851 pr_debug("%s sg %p sgl_count %u\n", __func__
, sg
, sgl_count
);
852 sg_init_table(sg
, sgl_count
);
853 cmd
->tvc_sgl_count
= sgl_count
;
855 pr_debug("Mapping iovec %p for %u pages\n", &iov
[0], sgl_count
);
857 for (i
= 0; i
< niov
; i
++) {
858 ret
= vhost_scsi_map_to_sgl(cmd
, sg
, sgl_count
, &iov
[i
],
859 cmd
->tvc_upages
, write
);
861 for (i
= 0; i
< cmd
->tvc_sgl_count
; i
++)
862 put_page(sg_page(&cmd
->tvc_sgl
[i
]));
864 cmd
->tvc_sgl_count
= 0;
874 vhost_scsi_map_iov_to_prot(struct tcm_vhost_cmd
*cmd
,
879 struct scatterlist
*prot_sg
= cmd
->tvc_prot_sgl
;
880 unsigned int prot_sgl_count
= 0;
883 for (i
= 0; i
< niov
; i
++)
884 prot_sgl_count
+= iov_num_pages(&iov
[i
]);
886 if (prot_sgl_count
> TCM_VHOST_PREALLOC_PROT_SGLS
) {
887 pr_err("vhost_scsi_map_iov_to_prot() sgl_count: %u greater than"
888 " preallocated TCM_VHOST_PREALLOC_PROT_SGLS: %u\n",
889 prot_sgl_count
, TCM_VHOST_PREALLOC_PROT_SGLS
);
893 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__
,
894 prot_sg
, prot_sgl_count
);
895 sg_init_table(prot_sg
, prot_sgl_count
);
896 cmd
->tvc_prot_sgl_count
= prot_sgl_count
;
898 for (i
= 0; i
< niov
; i
++) {
899 ret
= vhost_scsi_map_to_sgl(cmd
, prot_sg
, prot_sgl_count
, &iov
[i
],
900 cmd
->tvc_upages
, write
);
902 for (i
= 0; i
< cmd
->tvc_prot_sgl_count
; i
++)
903 put_page(sg_page(&cmd
->tvc_prot_sgl
[i
]));
905 cmd
->tvc_prot_sgl_count
= 0;
909 prot_sgl_count
-= ret
;
914 static void tcm_vhost_submission_work(struct work_struct
*work
)
916 struct tcm_vhost_cmd
*cmd
=
917 container_of(work
, struct tcm_vhost_cmd
, work
);
918 struct tcm_vhost_nexus
*tv_nexus
;
919 struct se_cmd
*se_cmd
= &cmd
->tvc_se_cmd
;
920 struct scatterlist
*sg_ptr
, *sg_prot_ptr
= NULL
;
923 /* FIXME: BIDI operation */
924 if (cmd
->tvc_sgl_count
) {
925 sg_ptr
= cmd
->tvc_sgl
;
927 if (cmd
->tvc_prot_sgl_count
)
928 sg_prot_ptr
= cmd
->tvc_prot_sgl
;
930 se_cmd
->prot_pto
= true;
934 tv_nexus
= cmd
->tvc_nexus
;
936 rc
= target_submit_cmd_map_sgls(se_cmd
, tv_nexus
->tvn_se_sess
,
937 cmd
->tvc_cdb
, &cmd
->tvc_sense_buf
[0],
938 cmd
->tvc_lun
, cmd
->tvc_exp_data_len
,
939 cmd
->tvc_task_attr
, cmd
->tvc_data_direction
,
940 TARGET_SCF_ACK_KREF
, sg_ptr
, cmd
->tvc_sgl_count
,
941 NULL
, 0, sg_prot_ptr
, cmd
->tvc_prot_sgl_count
);
943 transport_send_check_condition_and_sense(se_cmd
,
944 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
945 transport_generic_free_cmd(se_cmd
, 0);
950 vhost_scsi_send_bad_target(struct vhost_scsi
*vs
,
951 struct vhost_virtqueue
*vq
,
952 int head
, unsigned out
)
954 struct virtio_scsi_cmd_resp __user
*resp
;
955 struct virtio_scsi_cmd_resp rsp
;
958 memset(&rsp
, 0, sizeof(rsp
));
959 rsp
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
960 resp
= vq
->iov
[out
].iov_base
;
961 ret
= __copy_to_user(resp
, &rsp
, sizeof(rsp
));
963 vhost_add_used_and_signal(&vs
->dev
, vq
, head
, 0);
965 pr_err("Faulted on virtio_scsi_cmd_resp\n");
969 vhost_scsi_handle_vq(struct vhost_scsi
*vs
, struct vhost_virtqueue
*vq
)
971 struct tcm_vhost_tpg
**vs_tpg
;
972 struct virtio_scsi_cmd_req v_req
;
973 struct virtio_scsi_cmd_req_pi v_req_pi
;
974 struct tcm_vhost_tpg
*tpg
;
975 struct tcm_vhost_cmd
*cmd
;
977 u32 exp_data_len
, data_first
, data_num
, data_direction
, prot_first
;
979 int head
, ret
, data_niov
, prot_niov
, prot_bytes
;
982 u8
*target
, *lunp
, task_attr
;
986 mutex_lock(&vq
->mutex
);
988 * We can handle the vq only after the endpoint is setup by calling the
989 * VHOST_SCSI_SET_ENDPOINT ioctl.
991 vs_tpg
= vq
->private_data
;
995 vhost_disable_notify(&vs
->dev
, vq
);
998 head
= vhost_get_vq_desc(vq
, vq
->iov
,
999 ARRAY_SIZE(vq
->iov
), &out
, &in
,
1001 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
1003 /* On error, stop handling until the next kick. */
1004 if (unlikely(head
< 0))
1006 /* Nothing new? Wait for eventfd to tell us they refilled. */
1007 if (head
== vq
->num
) {
1008 if (unlikely(vhost_enable_notify(&vs
->dev
, vq
))) {
1009 vhost_disable_notify(&vs
->dev
, vq
);
1015 /* FIXME: BIDI operation */
1016 if (out
== 1 && in
== 1) {
1017 data_direction
= DMA_NONE
;
1020 } else if (out
== 1 && in
> 1) {
1021 data_direction
= DMA_FROM_DEVICE
;
1022 data_first
= out
+ 1;
1024 } else if (out
> 1 && in
== 1) {
1025 data_direction
= DMA_TO_DEVICE
;
1029 vq_err(vq
, "Invalid buffer layout out: %u in: %u\n",
1035 * Check for a sane resp buffer so we can report errors to
1038 if (unlikely(vq
->iov
[out
].iov_len
!=
1039 sizeof(struct virtio_scsi_cmd_resp
))) {
1040 vq_err(vq
, "Expecting virtio_scsi_cmd_resp, got %zu"
1041 " bytes\n", vq
->iov
[out
].iov_len
);
1045 if (vhost_has_feature(vq
, VIRTIO_SCSI_F_T10_PI
)) {
1047 lunp
= &v_req_pi
.lun
[0];
1048 target
= &v_req_pi
.lun
[1];
1049 req_size
= sizeof(v_req_pi
);
1053 lunp
= &v_req
.lun
[0];
1054 target
= &v_req
.lun
[1];
1055 req_size
= sizeof(v_req
);
1059 if (unlikely(vq
->iov
[0].iov_len
< req_size
)) {
1060 pr_err("Expecting virtio-scsi header: %zu, got %zu\n",
1061 req_size
, vq
->iov
[0].iov_len
);
1064 ret
= memcpy_fromiovecend(req
, &vq
->iov
[0], 0, req_size
);
1065 if (unlikely(ret
)) {
1066 vq_err(vq
, "Faulted on virtio_scsi_cmd_req\n");
1070 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
1071 if (unlikely(*lunp
!= 1)) {
1072 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1076 tpg
= ACCESS_ONCE(vs_tpg
[*target
]);
1078 /* Target does not exist, fail the request */
1079 if (unlikely(!tpg
)) {
1080 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1084 data_niov
= data_num
;
1085 prot_niov
= prot_first
= prot_bytes
= 0;
1087 * Determine if any protection information iovecs are preceeding
1088 * the actual data payload, and adjust data_first + data_niov
1089 * values accordingly for vhost_scsi_map_iov_to_sgl() below.
1091 * Also extract virtio_scsi header bits for vhost_scsi_get_tag()
1094 if (v_req_pi
.pi_bytesout
) {
1095 if (data_direction
!= DMA_TO_DEVICE
) {
1096 vq_err(vq
, "Received non zero do_pi_niov"
1097 ", but wrong data_direction\n");
1100 prot_bytes
= vhost32_to_cpu(vq
, v_req_pi
.pi_bytesout
);
1101 } else if (v_req_pi
.pi_bytesin
) {
1102 if (data_direction
!= DMA_FROM_DEVICE
) {
1103 vq_err(vq
, "Received non zero di_pi_niov"
1104 ", but wrong data_direction\n");
1107 prot_bytes
= vhost32_to_cpu(vq
, v_req_pi
.pi_bytesin
);
1112 for (i
= 0; i
< data_num
; i
++) {
1113 tmp
+= vq
->iov
[data_first
+ i
].iov_len
;
1115 if (tmp
>= prot_bytes
)
1118 prot_first
= data_first
;
1119 data_first
+= prot_niov
;
1120 data_niov
= data_num
- prot_niov
;
1122 tag
= vhost64_to_cpu(vq
, v_req_pi
.tag
);
1123 task_attr
= v_req_pi
.task_attr
;
1124 cdb
= &v_req_pi
.cdb
[0];
1125 lun
= ((v_req_pi
.lun
[2] << 8) | v_req_pi
.lun
[3]) & 0x3FFF;
1127 tag
= vhost64_to_cpu(vq
, v_req
.tag
);
1128 task_attr
= v_req
.task_attr
;
1129 cdb
= &v_req
.cdb
[0];
1130 lun
= ((v_req
.lun
[2] << 8) | v_req
.lun
[3]) & 0x3FFF;
1133 for (i
= 0; i
< data_niov
; i
++)
1134 exp_data_len
+= vq
->iov
[data_first
+ i
].iov_len
;
1136 * Check that the recieved CDB size does not exceeded our
1137 * hardcoded max for vhost-scsi
1139 * TODO what if cdb was too small for varlen cdb header?
1141 if (unlikely(scsi_command_size(cdb
) > TCM_VHOST_MAX_CDB_SIZE
)) {
1142 vq_err(vq
, "Received SCSI CDB with command_size: %d that"
1143 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1144 scsi_command_size(cdb
), TCM_VHOST_MAX_CDB_SIZE
);
1148 cmd
= vhost_scsi_get_tag(vq
, tpg
, cdb
, tag
, lun
, task_attr
,
1149 exp_data_len
+ prot_bytes
,
1152 vq_err(vq
, "vhost_scsi_get_tag failed %ld\n",
1157 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
1158 ": %d\n", cmd
, exp_data_len
, data_direction
);
1160 cmd
->tvc_vhost
= vs
;
1162 cmd
->tvc_resp
= vq
->iov
[out
].iov_base
;
1164 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1165 cmd
->tvc_cdb
[0], cmd
->tvc_lun
);
1168 ret
= vhost_scsi_map_iov_to_prot(cmd
,
1169 &vq
->iov
[prot_first
], prot_niov
,
1170 data_direction
== DMA_FROM_DEVICE
);
1171 if (unlikely(ret
)) {
1172 vq_err(vq
, "Failed to map iov to"
1177 if (data_direction
!= DMA_NONE
) {
1178 ret
= vhost_scsi_map_iov_to_sgl(cmd
,
1179 &vq
->iov
[data_first
], data_niov
,
1180 data_direction
== DMA_FROM_DEVICE
);
1181 if (unlikely(ret
)) {
1182 vq_err(vq
, "Failed to map iov to sgl\n");
1187 * Save the descriptor from vhost_get_vq_desc() to be used to
1188 * complete the virtio-scsi request in TCM callback context via
1189 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1191 cmd
->tvc_vq_desc
= head
;
1193 * Dispatch tv_cmd descriptor for cmwq execution in process
1194 * context provided by tcm_vhost_workqueue. This also ensures
1195 * tv_cmd is executed on the same kworker CPU as this vhost
1196 * thread to gain positive L2 cache locality effects..
1198 INIT_WORK(&cmd
->work
, tcm_vhost_submission_work
);
1199 queue_work(tcm_vhost_workqueue
, &cmd
->work
);
1202 mutex_unlock(&vq
->mutex
);
1206 vhost_scsi_free_cmd(cmd
);
1208 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1210 mutex_unlock(&vq
->mutex
);
1213 static void vhost_scsi_ctl_handle_kick(struct vhost_work
*work
)
1215 pr_debug("%s: The handling func for control queue.\n", __func__
);
1219 tcm_vhost_send_evt(struct vhost_scsi
*vs
,
1220 struct tcm_vhost_tpg
*tpg
,
1225 struct tcm_vhost_evt
*evt
;
1227 evt
= tcm_vhost_allocate_evt(vs
, event
, reason
);
1232 /* TODO: share lun setup code with virtio-scsi.ko */
1234 * Note: evt->event is zeroed when we allocate it and
1235 * lun[4-7] need to be zero according to virtio-scsi spec.
1237 evt
->event
.lun
[0] = 0x01;
1238 evt
->event
.lun
[1] = tpg
->tport_tpgt
& 0xFF;
1239 if (lun
->unpacked_lun
>= 256)
1240 evt
->event
.lun
[2] = lun
->unpacked_lun
>> 8 | 0x40 ;
1241 evt
->event
.lun
[3] = lun
->unpacked_lun
& 0xFF;
1244 llist_add(&evt
->list
, &vs
->vs_event_list
);
1245 vhost_work_queue(&vs
->dev
, &vs
->vs_event_work
);
1248 static void vhost_scsi_evt_handle_kick(struct vhost_work
*work
)
1250 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
1252 struct vhost_scsi
*vs
= container_of(vq
->dev
, struct vhost_scsi
, dev
);
1254 mutex_lock(&vq
->mutex
);
1255 if (!vq
->private_data
)
1258 if (vs
->vs_events_missed
)
1259 tcm_vhost_send_evt(vs
, NULL
, NULL
, VIRTIO_SCSI_T_NO_EVENT
, 0);
1261 mutex_unlock(&vq
->mutex
);
1264 static void vhost_scsi_handle_kick(struct vhost_work
*work
)
1266 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
1268 struct vhost_scsi
*vs
= container_of(vq
->dev
, struct vhost_scsi
, dev
);
1270 vhost_scsi_handle_vq(vs
, vq
);
1273 static void vhost_scsi_flush_vq(struct vhost_scsi
*vs
, int index
)
1275 vhost_poll_flush(&vs
->vqs
[index
].vq
.poll
);
1278 /* Callers must hold dev mutex */
1279 static void vhost_scsi_flush(struct vhost_scsi
*vs
)
1281 struct vhost_scsi_inflight
*old_inflight
[VHOST_SCSI_MAX_VQ
];
1284 /* Init new inflight and remember the old inflight */
1285 tcm_vhost_init_inflight(vs
, old_inflight
);
1288 * The inflight->kref was initialized to 1. We decrement it here to
1289 * indicate the start of the flush operation so that it will reach 0
1290 * when all the reqs are finished.
1292 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1293 kref_put(&old_inflight
[i
]->kref
, tcm_vhost_done_inflight
);
1295 /* Flush both the vhost poll and vhost work */
1296 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1297 vhost_scsi_flush_vq(vs
, i
);
1298 vhost_work_flush(&vs
->dev
, &vs
->vs_completion_work
);
1299 vhost_work_flush(&vs
->dev
, &vs
->vs_event_work
);
1301 /* Wait for all reqs issued before the flush to be finished */
1302 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1303 wait_for_completion(&old_inflight
[i
]->comp
);
1307 * Called from vhost_scsi_ioctl() context to walk the list of available
1308 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
1310 * The lock nesting rule is:
1311 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1314 vhost_scsi_set_endpoint(struct vhost_scsi
*vs
,
1315 struct vhost_scsi_target
*t
)
1317 struct se_portal_group
*se_tpg
;
1318 struct tcm_vhost_tport
*tv_tport
;
1319 struct tcm_vhost_tpg
*tpg
;
1320 struct tcm_vhost_tpg
**vs_tpg
;
1321 struct vhost_virtqueue
*vq
;
1322 int index
, ret
, i
, len
;
1325 mutex_lock(&tcm_vhost_mutex
);
1326 mutex_lock(&vs
->dev
.mutex
);
1328 /* Verify that ring has been setup correctly. */
1329 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
1330 /* Verify that ring has been setup correctly. */
1331 if (!vhost_vq_access_ok(&vs
->vqs
[index
].vq
)) {
1337 len
= sizeof(vs_tpg
[0]) * VHOST_SCSI_MAX_TARGET
;
1338 vs_tpg
= kzalloc(len
, GFP_KERNEL
);
1344 memcpy(vs_tpg
, vs
->vs_tpg
, len
);
1346 list_for_each_entry(tpg
, &tcm_vhost_list
, tv_tpg_list
) {
1347 mutex_lock(&tpg
->tv_tpg_mutex
);
1348 if (!tpg
->tpg_nexus
) {
1349 mutex_unlock(&tpg
->tv_tpg_mutex
);
1352 if (tpg
->tv_tpg_vhost_count
!= 0) {
1353 mutex_unlock(&tpg
->tv_tpg_mutex
);
1356 tv_tport
= tpg
->tport
;
1358 if (!strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
)) {
1359 if (vs
->vs_tpg
&& vs
->vs_tpg
[tpg
->tport_tpgt
]) {
1361 mutex_unlock(&tpg
->tv_tpg_mutex
);
1366 * In order to ensure individual vhost-scsi configfs
1367 * groups cannot be removed while in use by vhost ioctl,
1368 * go ahead and take an explicit se_tpg->tpg_group.cg_item
1371 se_tpg
= &tpg
->se_tpg
;
1372 ret
= configfs_depend_item(se_tpg
->se_tpg_tfo
->tf_subsys
,
1373 &se_tpg
->tpg_group
.cg_item
);
1375 pr_warn("configfs_depend_item() failed: %d\n", ret
);
1377 mutex_unlock(&tpg
->tv_tpg_mutex
);
1380 tpg
->tv_tpg_vhost_count
++;
1381 tpg
->vhost_scsi
= vs
;
1382 vs_tpg
[tpg
->tport_tpgt
] = tpg
;
1383 smp_mb__after_atomic();
1386 mutex_unlock(&tpg
->tv_tpg_mutex
);
1390 memcpy(vs
->vs_vhost_wwpn
, t
->vhost_wwpn
,
1391 sizeof(vs
->vs_vhost_wwpn
));
1392 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1393 vq
= &vs
->vqs
[i
].vq
;
1394 mutex_lock(&vq
->mutex
);
1395 vq
->private_data
= vs_tpg
;
1396 vhost_init_used(vq
);
1397 mutex_unlock(&vq
->mutex
);
1405 * Act as synchronize_rcu to make sure access to
1406 * old vs->vs_tpg is finished.
1408 vhost_scsi_flush(vs
);
1410 vs
->vs_tpg
= vs_tpg
;
1413 mutex_unlock(&vs
->dev
.mutex
);
1414 mutex_unlock(&tcm_vhost_mutex
);
1419 vhost_scsi_clear_endpoint(struct vhost_scsi
*vs
,
1420 struct vhost_scsi_target
*t
)
1422 struct se_portal_group
*se_tpg
;
1423 struct tcm_vhost_tport
*tv_tport
;
1424 struct tcm_vhost_tpg
*tpg
;
1425 struct vhost_virtqueue
*vq
;
1430 mutex_lock(&tcm_vhost_mutex
);
1431 mutex_lock(&vs
->dev
.mutex
);
1432 /* Verify that ring has been setup correctly. */
1433 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
1434 if (!vhost_vq_access_ok(&vs
->vqs
[index
].vq
)) {
1445 for (i
= 0; i
< VHOST_SCSI_MAX_TARGET
; i
++) {
1447 tpg
= vs
->vs_tpg
[target
];
1451 mutex_lock(&tpg
->tv_tpg_mutex
);
1452 tv_tport
= tpg
->tport
;
1458 if (strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
)) {
1459 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
1460 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1461 tv_tport
->tport_name
, tpg
->tport_tpgt
,
1462 t
->vhost_wwpn
, t
->vhost_tpgt
);
1466 tpg
->tv_tpg_vhost_count
--;
1467 tpg
->vhost_scsi
= NULL
;
1468 vs
->vs_tpg
[target
] = NULL
;
1470 mutex_unlock(&tpg
->tv_tpg_mutex
);
1472 * Release se_tpg->tpg_group.cg_item configfs dependency now
1473 * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
1475 se_tpg
= &tpg
->se_tpg
;
1476 configfs_undepend_item(se_tpg
->se_tpg_tfo
->tf_subsys
,
1477 &se_tpg
->tpg_group
.cg_item
);
1480 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1481 vq
= &vs
->vqs
[i
].vq
;
1482 mutex_lock(&vq
->mutex
);
1483 vq
->private_data
= NULL
;
1484 mutex_unlock(&vq
->mutex
);
1488 * Act as synchronize_rcu to make sure access to
1489 * old vs->vs_tpg is finished.
1491 vhost_scsi_flush(vs
);
1494 WARN_ON(vs
->vs_events_nr
);
1495 mutex_unlock(&vs
->dev
.mutex
);
1496 mutex_unlock(&tcm_vhost_mutex
);
1500 mutex_unlock(&tpg
->tv_tpg_mutex
);
1502 mutex_unlock(&vs
->dev
.mutex
);
1503 mutex_unlock(&tcm_vhost_mutex
);
1507 static int vhost_scsi_set_features(struct vhost_scsi
*vs
, u64 features
)
1509 struct vhost_virtqueue
*vq
;
1512 if (features
& ~VHOST_SCSI_FEATURES
)
1515 mutex_lock(&vs
->dev
.mutex
);
1516 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
1517 !vhost_log_access_ok(&vs
->dev
)) {
1518 mutex_unlock(&vs
->dev
.mutex
);
1522 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1523 vq
= &vs
->vqs
[i
].vq
;
1524 mutex_lock(&vq
->mutex
);
1525 vq
->acked_features
= features
;
1526 mutex_unlock(&vq
->mutex
);
1528 mutex_unlock(&vs
->dev
.mutex
);
1532 static int vhost_scsi_open(struct inode
*inode
, struct file
*f
)
1534 struct vhost_scsi
*vs
;
1535 struct vhost_virtqueue
**vqs
;
1538 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
| __GFP_NOWARN
| __GFP_REPEAT
);
1540 vs
= vzalloc(sizeof(*vs
));
1545 vqs
= kmalloc(VHOST_SCSI_MAX_VQ
* sizeof(*vqs
), GFP_KERNEL
);
1549 vhost_work_init(&vs
->vs_completion_work
, vhost_scsi_complete_cmd_work
);
1550 vhost_work_init(&vs
->vs_event_work
, tcm_vhost_evt_work
);
1552 vs
->vs_events_nr
= 0;
1553 vs
->vs_events_missed
= false;
1555 vqs
[VHOST_SCSI_VQ_CTL
] = &vs
->vqs
[VHOST_SCSI_VQ_CTL
].vq
;
1556 vqs
[VHOST_SCSI_VQ_EVT
] = &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1557 vs
->vqs
[VHOST_SCSI_VQ_CTL
].vq
.handle_kick
= vhost_scsi_ctl_handle_kick
;
1558 vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
.handle_kick
= vhost_scsi_evt_handle_kick
;
1559 for (i
= VHOST_SCSI_VQ_IO
; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1560 vqs
[i
] = &vs
->vqs
[i
].vq
;
1561 vs
->vqs
[i
].vq
.handle_kick
= vhost_scsi_handle_kick
;
1563 vhost_dev_init(&vs
->dev
, vqs
, VHOST_SCSI_MAX_VQ
);
1565 tcm_vhost_init_inflight(vs
, NULL
);
1567 f
->private_data
= vs
;
1576 static int vhost_scsi_release(struct inode
*inode
, struct file
*f
)
1578 struct vhost_scsi
*vs
= f
->private_data
;
1579 struct vhost_scsi_target t
;
1581 mutex_lock(&vs
->dev
.mutex
);
1582 memcpy(t
.vhost_wwpn
, vs
->vs_vhost_wwpn
, sizeof(t
.vhost_wwpn
));
1583 mutex_unlock(&vs
->dev
.mutex
);
1584 vhost_scsi_clear_endpoint(vs
, &t
);
1585 vhost_dev_stop(&vs
->dev
);
1586 vhost_dev_cleanup(&vs
->dev
, false);
1587 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1588 vhost_scsi_flush(vs
);
1595 vhost_scsi_ioctl(struct file
*f
,
1599 struct vhost_scsi
*vs
= f
->private_data
;
1600 struct vhost_scsi_target backend
;
1601 void __user
*argp
= (void __user
*)arg
;
1602 u64 __user
*featurep
= argp
;
1603 u32 __user
*eventsp
= argp
;
1606 int r
, abi_version
= VHOST_SCSI_ABI_VERSION
;
1607 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1610 case VHOST_SCSI_SET_ENDPOINT
:
1611 if (copy_from_user(&backend
, argp
, sizeof backend
))
1613 if (backend
.reserved
!= 0)
1616 return vhost_scsi_set_endpoint(vs
, &backend
);
1617 case VHOST_SCSI_CLEAR_ENDPOINT
:
1618 if (copy_from_user(&backend
, argp
, sizeof backend
))
1620 if (backend
.reserved
!= 0)
1623 return vhost_scsi_clear_endpoint(vs
, &backend
);
1624 case VHOST_SCSI_GET_ABI_VERSION
:
1625 if (copy_to_user(argp
, &abi_version
, sizeof abi_version
))
1628 case VHOST_SCSI_SET_EVENTS_MISSED
:
1629 if (get_user(events_missed
, eventsp
))
1631 mutex_lock(&vq
->mutex
);
1632 vs
->vs_events_missed
= events_missed
;
1633 mutex_unlock(&vq
->mutex
);
1635 case VHOST_SCSI_GET_EVENTS_MISSED
:
1636 mutex_lock(&vq
->mutex
);
1637 events_missed
= vs
->vs_events_missed
;
1638 mutex_unlock(&vq
->mutex
);
1639 if (put_user(events_missed
, eventsp
))
1642 case VHOST_GET_FEATURES
:
1643 features
= VHOST_SCSI_FEATURES
;
1644 if (copy_to_user(featurep
, &features
, sizeof features
))
1647 case VHOST_SET_FEATURES
:
1648 if (copy_from_user(&features
, featurep
, sizeof features
))
1650 return vhost_scsi_set_features(vs
, features
);
1652 mutex_lock(&vs
->dev
.mutex
);
1653 r
= vhost_dev_ioctl(&vs
->dev
, ioctl
, argp
);
1654 /* TODO: flush backend after dev ioctl. */
1655 if (r
== -ENOIOCTLCMD
)
1656 r
= vhost_vring_ioctl(&vs
->dev
, ioctl
, argp
);
1657 mutex_unlock(&vs
->dev
.mutex
);
1662 #ifdef CONFIG_COMPAT
1663 static long vhost_scsi_compat_ioctl(struct file
*f
, unsigned int ioctl
,
1666 return vhost_scsi_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
1670 static const struct file_operations vhost_scsi_fops
= {
1671 .owner
= THIS_MODULE
,
1672 .release
= vhost_scsi_release
,
1673 .unlocked_ioctl
= vhost_scsi_ioctl
,
1674 #ifdef CONFIG_COMPAT
1675 .compat_ioctl
= vhost_scsi_compat_ioctl
,
1677 .open
= vhost_scsi_open
,
1678 .llseek
= noop_llseek
,
1681 static struct miscdevice vhost_scsi_misc
= {
1687 static int __init
vhost_scsi_register(void)
1689 return misc_register(&vhost_scsi_misc
);
1692 static int vhost_scsi_deregister(void)
1694 return misc_deregister(&vhost_scsi_misc
);
1697 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport
*tport
)
1699 switch (tport
->tport_proto_id
) {
1700 case SCSI_PROTOCOL_SAS
:
1702 case SCSI_PROTOCOL_FCP
:
1704 case SCSI_PROTOCOL_ISCSI
:
1714 tcm_vhost_do_plug(struct tcm_vhost_tpg
*tpg
,
1715 struct se_lun
*lun
, bool plug
)
1718 struct vhost_scsi
*vs
= tpg
->vhost_scsi
;
1719 struct vhost_virtqueue
*vq
;
1725 mutex_lock(&vs
->dev
.mutex
);
1728 reason
= VIRTIO_SCSI_EVT_RESET_RESCAN
;
1730 reason
= VIRTIO_SCSI_EVT_RESET_REMOVED
;
1732 vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1733 mutex_lock(&vq
->mutex
);
1734 if (vhost_has_feature(vq
, VIRTIO_SCSI_F_HOTPLUG
))
1735 tcm_vhost_send_evt(vs
, tpg
, lun
,
1736 VIRTIO_SCSI_T_TRANSPORT_RESET
, reason
);
1737 mutex_unlock(&vq
->mutex
);
1738 mutex_unlock(&vs
->dev
.mutex
);
1741 static void tcm_vhost_hotplug(struct tcm_vhost_tpg
*tpg
, struct se_lun
*lun
)
1743 tcm_vhost_do_plug(tpg
, lun
, true);
1746 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg
*tpg
, struct se_lun
*lun
)
1748 tcm_vhost_do_plug(tpg
, lun
, false);
1751 static int tcm_vhost_port_link(struct se_portal_group
*se_tpg
,
1754 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1755 struct tcm_vhost_tpg
, se_tpg
);
1757 mutex_lock(&tcm_vhost_mutex
);
1759 mutex_lock(&tpg
->tv_tpg_mutex
);
1760 tpg
->tv_tpg_port_count
++;
1761 mutex_unlock(&tpg
->tv_tpg_mutex
);
1763 tcm_vhost_hotplug(tpg
, lun
);
1765 mutex_unlock(&tcm_vhost_mutex
);
1770 static void tcm_vhost_port_unlink(struct se_portal_group
*se_tpg
,
1773 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1774 struct tcm_vhost_tpg
, se_tpg
);
1776 mutex_lock(&tcm_vhost_mutex
);
1778 mutex_lock(&tpg
->tv_tpg_mutex
);
1779 tpg
->tv_tpg_port_count
--;
1780 mutex_unlock(&tpg
->tv_tpg_mutex
);
1782 tcm_vhost_hotunplug(tpg
, lun
);
1784 mutex_unlock(&tcm_vhost_mutex
);
1787 static struct se_node_acl
*
1788 tcm_vhost_make_nodeacl(struct se_portal_group
*se_tpg
,
1789 struct config_group
*group
,
1792 struct se_node_acl
*se_nacl
, *se_nacl_new
;
1793 struct tcm_vhost_nacl
*nacl
;
1797 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1798 return ERR_PTR(-EINVAL); */
1799 se_nacl_new
= tcm_vhost_alloc_fabric_acl(se_tpg
);
1801 return ERR_PTR(-ENOMEM
);
1805 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1806 * when converting a NodeACL from demo mode -> explict
1808 se_nacl
= core_tpg_add_initiator_node_acl(se_tpg
, se_nacl_new
,
1810 if (IS_ERR(se_nacl
)) {
1811 tcm_vhost_release_fabric_acl(se_tpg
, se_nacl_new
);
1815 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1817 nacl
= container_of(se_nacl
, struct tcm_vhost_nacl
, se_node_acl
);
1818 nacl
->iport_wwpn
= wwpn
;
1823 static void tcm_vhost_drop_nodeacl(struct se_node_acl
*se_acl
)
1825 struct tcm_vhost_nacl
*nacl
= container_of(se_acl
,
1826 struct tcm_vhost_nacl
, se_node_acl
);
1827 core_tpg_del_initiator_node_acl(se_acl
->se_tpg
, se_acl
, 1);
1831 static void tcm_vhost_free_cmd_map_res(struct tcm_vhost_nexus
*nexus
,
1832 struct se_session
*se_sess
)
1834 struct tcm_vhost_cmd
*tv_cmd
;
1837 if (!se_sess
->sess_cmd_map
)
1840 for (i
= 0; i
< TCM_VHOST_DEFAULT_TAGS
; i
++) {
1841 tv_cmd
= &((struct tcm_vhost_cmd
*)se_sess
->sess_cmd_map
)[i
];
1843 kfree(tv_cmd
->tvc_sgl
);
1844 kfree(tv_cmd
->tvc_prot_sgl
);
1845 kfree(tv_cmd
->tvc_upages
);
1849 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg
*tpg
,
1852 struct se_portal_group
*se_tpg
;
1853 struct se_session
*se_sess
;
1854 struct tcm_vhost_nexus
*tv_nexus
;
1855 struct tcm_vhost_cmd
*tv_cmd
;
1858 mutex_lock(&tpg
->tv_tpg_mutex
);
1859 if (tpg
->tpg_nexus
) {
1860 mutex_unlock(&tpg
->tv_tpg_mutex
);
1861 pr_debug("tpg->tpg_nexus already exists\n");
1864 se_tpg
= &tpg
->se_tpg
;
1866 tv_nexus
= kzalloc(sizeof(struct tcm_vhost_nexus
), GFP_KERNEL
);
1868 mutex_unlock(&tpg
->tv_tpg_mutex
);
1869 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1873 * Initialize the struct se_session pointer and setup tagpool
1874 * for struct tcm_vhost_cmd descriptors
1876 tv_nexus
->tvn_se_sess
= transport_init_session_tags(
1877 TCM_VHOST_DEFAULT_TAGS
,
1878 sizeof(struct tcm_vhost_cmd
),
1879 TARGET_PROT_DIN_PASS
| TARGET_PROT_DOUT_PASS
);
1880 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1881 mutex_unlock(&tpg
->tv_tpg_mutex
);
1885 se_sess
= tv_nexus
->tvn_se_sess
;
1886 for (i
= 0; i
< TCM_VHOST_DEFAULT_TAGS
; i
++) {
1887 tv_cmd
= &((struct tcm_vhost_cmd
*)se_sess
->sess_cmd_map
)[i
];
1889 tv_cmd
->tvc_sgl
= kzalloc(sizeof(struct scatterlist
) *
1890 TCM_VHOST_PREALLOC_SGLS
, GFP_KERNEL
);
1891 if (!tv_cmd
->tvc_sgl
) {
1892 mutex_unlock(&tpg
->tv_tpg_mutex
);
1893 pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1897 tv_cmd
->tvc_upages
= kzalloc(sizeof(struct page
*) *
1898 TCM_VHOST_PREALLOC_UPAGES
, GFP_KERNEL
);
1899 if (!tv_cmd
->tvc_upages
) {
1900 mutex_unlock(&tpg
->tv_tpg_mutex
);
1901 pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1905 tv_cmd
->tvc_prot_sgl
= kzalloc(sizeof(struct scatterlist
) *
1906 TCM_VHOST_PREALLOC_PROT_SGLS
, GFP_KERNEL
);
1907 if (!tv_cmd
->tvc_prot_sgl
) {
1908 mutex_unlock(&tpg
->tv_tpg_mutex
);
1909 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1914 * Since we are running in 'demo mode' this call with generate a
1915 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1916 * the SCSI Initiator port name of the passed configfs group 'name'.
1918 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1919 se_tpg
, (unsigned char *)name
);
1920 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1921 mutex_unlock(&tpg
->tv_tpg_mutex
);
1922 pr_debug("core_tpg_check_initiator_node_acl() failed"
1927 * Now register the TCM vhost virtual I_T Nexus as active with the
1928 * call to __transport_register_session()
1930 __transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1931 tv_nexus
->tvn_se_sess
, tv_nexus
);
1932 tpg
->tpg_nexus
= tv_nexus
;
1934 mutex_unlock(&tpg
->tv_tpg_mutex
);
1938 tcm_vhost_free_cmd_map_res(tv_nexus
, se_sess
);
1939 transport_free_session(se_sess
);
1944 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg
*tpg
)
1946 struct se_session
*se_sess
;
1947 struct tcm_vhost_nexus
*tv_nexus
;
1949 mutex_lock(&tpg
->tv_tpg_mutex
);
1950 tv_nexus
= tpg
->tpg_nexus
;
1952 mutex_unlock(&tpg
->tv_tpg_mutex
);
1956 se_sess
= tv_nexus
->tvn_se_sess
;
1958 mutex_unlock(&tpg
->tv_tpg_mutex
);
1962 if (tpg
->tv_tpg_port_count
!= 0) {
1963 mutex_unlock(&tpg
->tv_tpg_mutex
);
1964 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1965 " active TPG port count: %d\n",
1966 tpg
->tv_tpg_port_count
);
1970 if (tpg
->tv_tpg_vhost_count
!= 0) {
1971 mutex_unlock(&tpg
->tv_tpg_mutex
);
1972 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1973 " active TPG vhost count: %d\n",
1974 tpg
->tv_tpg_vhost_count
);
1978 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1979 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg
->tport
),
1980 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1982 tcm_vhost_free_cmd_map_res(tv_nexus
, se_sess
);
1984 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1986 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1987 tpg
->tpg_nexus
= NULL
;
1988 mutex_unlock(&tpg
->tv_tpg_mutex
);
1994 static ssize_t
tcm_vhost_tpg_show_nexus(struct se_portal_group
*se_tpg
,
1997 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1998 struct tcm_vhost_tpg
, se_tpg
);
1999 struct tcm_vhost_nexus
*tv_nexus
;
2002 mutex_lock(&tpg
->tv_tpg_mutex
);
2003 tv_nexus
= tpg
->tpg_nexus
;
2005 mutex_unlock(&tpg
->tv_tpg_mutex
);
2008 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
2009 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
2010 mutex_unlock(&tpg
->tv_tpg_mutex
);
2015 static ssize_t
tcm_vhost_tpg_store_nexus(struct se_portal_group
*se_tpg
,
2019 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
2020 struct tcm_vhost_tpg
, se_tpg
);
2021 struct tcm_vhost_tport
*tport_wwn
= tpg
->tport
;
2022 unsigned char i_port
[TCM_VHOST_NAMELEN
], *ptr
, *port_ptr
;
2025 * Shutdown the active I_T nexus if 'NULL' is passed..
2027 if (!strncmp(page
, "NULL", 4)) {
2028 ret
= tcm_vhost_drop_nexus(tpg
);
2029 return (!ret
) ? count
: ret
;
2032 * Otherwise make sure the passed virtual Initiator port WWN matches
2033 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
2034 * tcm_vhost_make_nexus().
2036 if (strlen(page
) >= TCM_VHOST_NAMELEN
) {
2037 pr_err("Emulated NAA Sas Address: %s, exceeds"
2038 " max: %d\n", page
, TCM_VHOST_NAMELEN
);
2041 snprintf(&i_port
[0], TCM_VHOST_NAMELEN
, "%s", page
);
2043 ptr
= strstr(i_port
, "naa.");
2045 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
2046 pr_err("Passed SAS Initiator Port %s does not"
2047 " match target port protoid: %s\n", i_port
,
2048 tcm_vhost_dump_proto_id(tport_wwn
));
2051 port_ptr
= &i_port
[0];
2054 ptr
= strstr(i_port
, "fc.");
2056 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
2057 pr_err("Passed FCP Initiator Port %s does not"
2058 " match target port protoid: %s\n", i_port
,
2059 tcm_vhost_dump_proto_id(tport_wwn
));
2062 port_ptr
= &i_port
[3]; /* Skip over "fc." */
2065 ptr
= strstr(i_port
, "iqn.");
2067 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
2068 pr_err("Passed iSCSI Initiator Port %s does not"
2069 " match target port protoid: %s\n", i_port
,
2070 tcm_vhost_dump_proto_id(tport_wwn
));
2073 port_ptr
= &i_port
[0];
2076 pr_err("Unable to locate prefix for emulated Initiator Port:"
2080 * Clear any trailing newline for the NAA WWN
2083 if (i_port
[strlen(i_port
)-1] == '\n')
2084 i_port
[strlen(i_port
)-1] = '\0';
2086 ret
= tcm_vhost_make_nexus(tpg
, port_ptr
);
2093 TF_TPG_BASE_ATTR(tcm_vhost
, nexus
, S_IRUGO
| S_IWUSR
);
2095 static struct configfs_attribute
*tcm_vhost_tpg_attrs
[] = {
2096 &tcm_vhost_tpg_nexus
.attr
,
2100 static struct se_portal_group
*
2101 tcm_vhost_make_tpg(struct se_wwn
*wwn
,
2102 struct config_group
*group
,
2105 struct tcm_vhost_tport
*tport
= container_of(wwn
,
2106 struct tcm_vhost_tport
, tport_wwn
);
2108 struct tcm_vhost_tpg
*tpg
;
2112 if (strstr(name
, "tpgt_") != name
)
2113 return ERR_PTR(-EINVAL
);
2114 if (kstrtoul(name
+ 5, 10, &tpgt
) || tpgt
> UINT_MAX
)
2115 return ERR_PTR(-EINVAL
);
2117 tpg
= kzalloc(sizeof(struct tcm_vhost_tpg
), GFP_KERNEL
);
2119 pr_err("Unable to allocate struct tcm_vhost_tpg");
2120 return ERR_PTR(-ENOMEM
);
2122 mutex_init(&tpg
->tv_tpg_mutex
);
2123 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
2125 tpg
->tport_tpgt
= tpgt
;
2127 ret
= core_tpg_register(&tcm_vhost_fabric_configfs
->tf_ops
, wwn
,
2128 &tpg
->se_tpg
, tpg
, TRANSPORT_TPG_TYPE_NORMAL
);
2133 mutex_lock(&tcm_vhost_mutex
);
2134 list_add_tail(&tpg
->tv_tpg_list
, &tcm_vhost_list
);
2135 mutex_unlock(&tcm_vhost_mutex
);
2137 return &tpg
->se_tpg
;
2140 static void tcm_vhost_drop_tpg(struct se_portal_group
*se_tpg
)
2142 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
2143 struct tcm_vhost_tpg
, se_tpg
);
2145 mutex_lock(&tcm_vhost_mutex
);
2146 list_del(&tpg
->tv_tpg_list
);
2147 mutex_unlock(&tcm_vhost_mutex
);
2149 * Release the virtual I_T Nexus for this vhost TPG
2151 tcm_vhost_drop_nexus(tpg
);
2153 * Deregister the se_tpg from TCM..
2155 core_tpg_deregister(se_tpg
);
2159 static struct se_wwn
*
2160 tcm_vhost_make_tport(struct target_fabric_configfs
*tf
,
2161 struct config_group
*group
,
2164 struct tcm_vhost_tport
*tport
;
2169 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
2170 return ERR_PTR(-EINVAL); */
2172 tport
= kzalloc(sizeof(struct tcm_vhost_tport
), GFP_KERNEL
);
2174 pr_err("Unable to allocate struct tcm_vhost_tport");
2175 return ERR_PTR(-ENOMEM
);
2177 tport
->tport_wwpn
= wwpn
;
2179 * Determine the emulated Protocol Identifier and Target Port Name
2180 * based on the incoming configfs directory name.
2182 ptr
= strstr(name
, "naa.");
2184 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
2187 ptr
= strstr(name
, "fc.");
2189 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
2190 off
= 3; /* Skip over "fc." */
2193 ptr
= strstr(name
, "iqn.");
2195 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
2199 pr_err("Unable to locate prefix for emulated Target Port:"
2202 return ERR_PTR(-EINVAL
);
2205 if (strlen(name
) >= TCM_VHOST_NAMELEN
) {
2206 pr_err("Emulated %s Address: %s, exceeds"
2207 " max: %d\n", name
, tcm_vhost_dump_proto_id(tport
),
2210 return ERR_PTR(-EINVAL
);
2212 snprintf(&tport
->tport_name
[0], TCM_VHOST_NAMELEN
, "%s", &name
[off
]);
2214 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
2215 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
), name
);
2217 return &tport
->tport_wwn
;
2220 static void tcm_vhost_drop_tport(struct se_wwn
*wwn
)
2222 struct tcm_vhost_tport
*tport
= container_of(wwn
,
2223 struct tcm_vhost_tport
, tport_wwn
);
2225 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2226 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
),
2233 tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs
*tf
,
2236 return sprintf(page
, "TCM_VHOST fabric module %s on %s/%s"
2237 "on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
2238 utsname()->machine
);
2241 TF_WWN_ATTR_RO(tcm_vhost
, version
);
2243 static struct configfs_attribute
*tcm_vhost_wwn_attrs
[] = {
2244 &tcm_vhost_wwn_version
.attr
,
2248 static struct target_core_fabric_ops tcm_vhost_ops
= {
2249 .get_fabric_name
= tcm_vhost_get_fabric_name
,
2250 .get_fabric_proto_ident
= tcm_vhost_get_fabric_proto_ident
,
2251 .tpg_get_wwn
= tcm_vhost_get_fabric_wwn
,
2252 .tpg_get_tag
= tcm_vhost_get_tag
,
2253 .tpg_get_default_depth
= tcm_vhost_get_default_depth
,
2254 .tpg_get_pr_transport_id
= tcm_vhost_get_pr_transport_id
,
2255 .tpg_get_pr_transport_id_len
= tcm_vhost_get_pr_transport_id_len
,
2256 .tpg_parse_pr_out_transport_id
= tcm_vhost_parse_pr_out_transport_id
,
2257 .tpg_check_demo_mode
= tcm_vhost_check_true
,
2258 .tpg_check_demo_mode_cache
= tcm_vhost_check_true
,
2259 .tpg_check_demo_mode_write_protect
= tcm_vhost_check_false
,
2260 .tpg_check_prod_mode_write_protect
= tcm_vhost_check_false
,
2261 .tpg_alloc_fabric_acl
= tcm_vhost_alloc_fabric_acl
,
2262 .tpg_release_fabric_acl
= tcm_vhost_release_fabric_acl
,
2263 .tpg_get_inst_index
= tcm_vhost_tpg_get_inst_index
,
2264 .release_cmd
= tcm_vhost_release_cmd
,
2265 .check_stop_free
= vhost_scsi_check_stop_free
,
2266 .shutdown_session
= tcm_vhost_shutdown_session
,
2267 .close_session
= tcm_vhost_close_session
,
2268 .sess_get_index
= tcm_vhost_sess_get_index
,
2269 .sess_get_initiator_sid
= NULL
,
2270 .write_pending
= tcm_vhost_write_pending
,
2271 .write_pending_status
= tcm_vhost_write_pending_status
,
2272 .set_default_node_attributes
= tcm_vhost_set_default_node_attrs
,
2273 .get_task_tag
= tcm_vhost_get_task_tag
,
2274 .get_cmd_state
= tcm_vhost_get_cmd_state
,
2275 .queue_data_in
= tcm_vhost_queue_data_in
,
2276 .queue_status
= tcm_vhost_queue_status
,
2277 .queue_tm_rsp
= tcm_vhost_queue_tm_rsp
,
2278 .aborted_task
= tcm_vhost_aborted_task
,
2280 * Setup callers for generic logic in target_core_fabric_configfs.c
2282 .fabric_make_wwn
= tcm_vhost_make_tport
,
2283 .fabric_drop_wwn
= tcm_vhost_drop_tport
,
2284 .fabric_make_tpg
= tcm_vhost_make_tpg
,
2285 .fabric_drop_tpg
= tcm_vhost_drop_tpg
,
2286 .fabric_post_link
= tcm_vhost_port_link
,
2287 .fabric_pre_unlink
= tcm_vhost_port_unlink
,
2288 .fabric_make_np
= NULL
,
2289 .fabric_drop_np
= NULL
,
2290 .fabric_make_nodeacl
= tcm_vhost_make_nodeacl
,
2291 .fabric_drop_nodeacl
= tcm_vhost_drop_nodeacl
,
2294 static int tcm_vhost_register_configfs(void)
2296 struct target_fabric_configfs
*fabric
;
2299 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2300 " on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
2301 utsname()->machine
);
2303 * Register the top level struct config_item_type with TCM core
2305 fabric
= target_fabric_configfs_init(THIS_MODULE
, "vhost");
2306 if (IS_ERR(fabric
)) {
2307 pr_err("target_fabric_configfs_init() failed\n");
2308 return PTR_ERR(fabric
);
2311 * Setup fabric->tf_ops from our local tcm_vhost_ops
2313 fabric
->tf_ops
= tcm_vhost_ops
;
2315 * Setup default attribute lists for various fabric->tf_cit_tmpl
2317 fabric
->tf_cit_tmpl
.tfc_wwn_cit
.ct_attrs
= tcm_vhost_wwn_attrs
;
2318 fabric
->tf_cit_tmpl
.tfc_tpg_base_cit
.ct_attrs
= tcm_vhost_tpg_attrs
;
2319 fabric
->tf_cit_tmpl
.tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
2320 fabric
->tf_cit_tmpl
.tfc_tpg_param_cit
.ct_attrs
= NULL
;
2321 fabric
->tf_cit_tmpl
.tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
2322 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_base_cit
.ct_attrs
= NULL
;
2323 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
2324 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
2325 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
2327 * Register the fabric for use within TCM
2329 ret
= target_fabric_configfs_register(fabric
);
2331 pr_err("target_fabric_configfs_register() failed"
2332 " for TCM_VHOST\n");
2336 * Setup our local pointer to *fabric
2338 tcm_vhost_fabric_configfs
= fabric
;
2339 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2343 static void tcm_vhost_deregister_configfs(void)
2345 if (!tcm_vhost_fabric_configfs
)
2348 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs
);
2349 tcm_vhost_fabric_configfs
= NULL
;
2350 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2353 static int __init
tcm_vhost_init(void)
2357 * Use our own dedicated workqueue for submitting I/O into
2358 * target core to avoid contention within system_wq.
2360 tcm_vhost_workqueue
= alloc_workqueue("tcm_vhost", 0, 0);
2361 if (!tcm_vhost_workqueue
)
2364 ret
= vhost_scsi_register();
2366 goto out_destroy_workqueue
;
2368 ret
= tcm_vhost_register_configfs();
2370 goto out_vhost_scsi_deregister
;
2374 out_vhost_scsi_deregister
:
2375 vhost_scsi_deregister();
2376 out_destroy_workqueue
:
2377 destroy_workqueue(tcm_vhost_workqueue
);
2382 static void tcm_vhost_exit(void)
2384 tcm_vhost_deregister_configfs();
2385 vhost_scsi_deregister();
2386 destroy_workqueue(tcm_vhost_workqueue
);
2389 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2390 MODULE_ALIAS("tcm_vhost");
2391 MODULE_LICENSE("GPL");
2392 module_init(tcm_vhost_init
);
2393 module_exit(tcm_vhost_exit
);