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
) |
173 (1ULL << VIRTIO_SCSI_F_T10_PI
)
176 #define VHOST_SCSI_MAX_TARGET 256
177 #define VHOST_SCSI_MAX_VQ 128
178 #define VHOST_SCSI_MAX_EVENT 128
180 struct vhost_scsi_virtqueue
{
181 struct vhost_virtqueue vq
;
183 * Reference counting for inflight reqs, used for flush operation. At
184 * each time, one reference tracks new commands submitted, while we
185 * wait for another one to reach 0.
187 struct vhost_scsi_inflight inflights
[2];
189 * Indicate current inflight in use, protected by vq->mutex.
190 * Writers must also take dev mutex and flush under it.
196 /* Protected by vhost_scsi->dev.mutex */
197 struct tcm_vhost_tpg
**vs_tpg
;
198 char vs_vhost_wwpn
[TRANSPORT_IQN_LEN
];
200 struct vhost_dev dev
;
201 struct vhost_scsi_virtqueue vqs
[VHOST_SCSI_MAX_VQ
];
203 struct vhost_work vs_completion_work
; /* cmd completion work item */
204 struct llist_head vs_completion_list
; /* cmd completion queue */
206 struct vhost_work vs_event_work
; /* evt injection work item */
207 struct llist_head vs_event_list
; /* evt injection queue */
209 bool vs_events_missed
; /* any missed events, protected by vq->mutex */
210 int vs_events_nr
; /* num of pending events, protected by vq->mutex */
213 /* Local pointer to allocated TCM configfs fabric module */
214 static struct target_fabric_configfs
*tcm_vhost_fabric_configfs
;
216 static struct workqueue_struct
*tcm_vhost_workqueue
;
218 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
219 static DEFINE_MUTEX(tcm_vhost_mutex
);
220 static LIST_HEAD(tcm_vhost_list
);
222 static int iov_num_pages(struct iovec
*iov
)
224 return (PAGE_ALIGN((unsigned long)iov
->iov_base
+ iov
->iov_len
) -
225 ((unsigned long)iov
->iov_base
& PAGE_MASK
)) >> PAGE_SHIFT
;
228 static void tcm_vhost_done_inflight(struct kref
*kref
)
230 struct vhost_scsi_inflight
*inflight
;
232 inflight
= container_of(kref
, struct vhost_scsi_inflight
, kref
);
233 complete(&inflight
->comp
);
236 static void tcm_vhost_init_inflight(struct vhost_scsi
*vs
,
237 struct vhost_scsi_inflight
*old_inflight
[])
239 struct vhost_scsi_inflight
*new_inflight
;
240 struct vhost_virtqueue
*vq
;
243 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
246 mutex_lock(&vq
->mutex
);
248 /* store old infight */
249 idx
= vs
->vqs
[i
].inflight_idx
;
251 old_inflight
[i
] = &vs
->vqs
[i
].inflights
[idx
];
253 /* setup new infight */
254 vs
->vqs
[i
].inflight_idx
= idx
^ 1;
255 new_inflight
= &vs
->vqs
[i
].inflights
[idx
^ 1];
256 kref_init(&new_inflight
->kref
);
257 init_completion(&new_inflight
->comp
);
259 mutex_unlock(&vq
->mutex
);
263 static struct vhost_scsi_inflight
*
264 tcm_vhost_get_inflight(struct vhost_virtqueue
*vq
)
266 struct vhost_scsi_inflight
*inflight
;
267 struct vhost_scsi_virtqueue
*svq
;
269 svq
= container_of(vq
, struct vhost_scsi_virtqueue
, vq
);
270 inflight
= &svq
->inflights
[svq
->inflight_idx
];
271 kref_get(&inflight
->kref
);
276 static void tcm_vhost_put_inflight(struct vhost_scsi_inflight
*inflight
)
278 kref_put(&inflight
->kref
, tcm_vhost_done_inflight
);
281 static int tcm_vhost_check_true(struct se_portal_group
*se_tpg
)
286 static int tcm_vhost_check_false(struct se_portal_group
*se_tpg
)
291 static char *tcm_vhost_get_fabric_name(void)
296 static u8
tcm_vhost_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
298 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
299 struct tcm_vhost_tpg
, se_tpg
);
300 struct tcm_vhost_tport
*tport
= tpg
->tport
;
302 switch (tport
->tport_proto_id
) {
303 case SCSI_PROTOCOL_SAS
:
304 return sas_get_fabric_proto_ident(se_tpg
);
305 case SCSI_PROTOCOL_FCP
:
306 return fc_get_fabric_proto_ident(se_tpg
);
307 case SCSI_PROTOCOL_ISCSI
:
308 return iscsi_get_fabric_proto_ident(se_tpg
);
310 pr_err("Unknown tport_proto_id: 0x%02x, using"
311 " SAS emulation\n", tport
->tport_proto_id
);
315 return sas_get_fabric_proto_ident(se_tpg
);
318 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group
*se_tpg
)
320 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
321 struct tcm_vhost_tpg
, se_tpg
);
322 struct tcm_vhost_tport
*tport
= tpg
->tport
;
324 return &tport
->tport_name
[0];
327 static u16
tcm_vhost_get_tag(struct se_portal_group
*se_tpg
)
329 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
330 struct tcm_vhost_tpg
, se_tpg
);
331 return tpg
->tport_tpgt
;
334 static u32
tcm_vhost_get_default_depth(struct se_portal_group
*se_tpg
)
340 tcm_vhost_get_pr_transport_id(struct se_portal_group
*se_tpg
,
341 struct se_node_acl
*se_nacl
,
342 struct t10_pr_registration
*pr_reg
,
346 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
347 struct tcm_vhost_tpg
, se_tpg
);
348 struct tcm_vhost_tport
*tport
= tpg
->tport
;
350 switch (tport
->tport_proto_id
) {
351 case SCSI_PROTOCOL_SAS
:
352 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
354 case SCSI_PROTOCOL_FCP
:
355 return fc_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
357 case SCSI_PROTOCOL_ISCSI
:
358 return iscsi_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
361 pr_err("Unknown tport_proto_id: 0x%02x, using"
362 " SAS emulation\n", tport
->tport_proto_id
);
366 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
371 tcm_vhost_get_pr_transport_id_len(struct se_portal_group
*se_tpg
,
372 struct se_node_acl
*se_nacl
,
373 struct t10_pr_registration
*pr_reg
,
376 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
377 struct tcm_vhost_tpg
, se_tpg
);
378 struct tcm_vhost_tport
*tport
= tpg
->tport
;
380 switch (tport
->tport_proto_id
) {
381 case SCSI_PROTOCOL_SAS
:
382 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
384 case SCSI_PROTOCOL_FCP
:
385 return fc_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
387 case SCSI_PROTOCOL_ISCSI
:
388 return iscsi_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
391 pr_err("Unknown tport_proto_id: 0x%02x, using"
392 " SAS emulation\n", tport
->tport_proto_id
);
396 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
401 tcm_vhost_parse_pr_out_transport_id(struct se_portal_group
*se_tpg
,
404 char **port_nexus_ptr
)
406 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
407 struct tcm_vhost_tpg
, se_tpg
);
408 struct tcm_vhost_tport
*tport
= tpg
->tport
;
410 switch (tport
->tport_proto_id
) {
411 case SCSI_PROTOCOL_SAS
:
412 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
414 case SCSI_PROTOCOL_FCP
:
415 return fc_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
417 case SCSI_PROTOCOL_ISCSI
:
418 return iscsi_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
421 pr_err("Unknown tport_proto_id: 0x%02x, using"
422 " SAS emulation\n", tport
->tport_proto_id
);
426 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
430 static struct se_node_acl
*
431 tcm_vhost_alloc_fabric_acl(struct se_portal_group
*se_tpg
)
433 struct tcm_vhost_nacl
*nacl
;
435 nacl
= kzalloc(sizeof(struct tcm_vhost_nacl
), GFP_KERNEL
);
437 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
441 return &nacl
->se_node_acl
;
445 tcm_vhost_release_fabric_acl(struct se_portal_group
*se_tpg
,
446 struct se_node_acl
*se_nacl
)
448 struct tcm_vhost_nacl
*nacl
= container_of(se_nacl
,
449 struct tcm_vhost_nacl
, se_node_acl
);
453 static u32
tcm_vhost_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
458 static void tcm_vhost_release_cmd(struct se_cmd
*se_cmd
)
460 struct tcm_vhost_cmd
*tv_cmd
= container_of(se_cmd
,
461 struct tcm_vhost_cmd
, tvc_se_cmd
);
462 struct se_session
*se_sess
= se_cmd
->se_sess
;
465 if (tv_cmd
->tvc_sgl_count
) {
466 for (i
= 0; i
< tv_cmd
->tvc_sgl_count
; i
++)
467 put_page(sg_page(&tv_cmd
->tvc_sgl
[i
]));
469 if (tv_cmd
->tvc_prot_sgl_count
) {
470 for (i
= 0; i
< tv_cmd
->tvc_prot_sgl_count
; i
++)
471 put_page(sg_page(&tv_cmd
->tvc_prot_sgl
[i
]));
474 tcm_vhost_put_inflight(tv_cmd
->inflight
);
475 percpu_ida_free(&se_sess
->sess_tag_pool
, se_cmd
->map_tag
);
478 static int tcm_vhost_shutdown_session(struct se_session
*se_sess
)
483 static void tcm_vhost_close_session(struct se_session
*se_sess
)
488 static u32
tcm_vhost_sess_get_index(struct se_session
*se_sess
)
493 static int tcm_vhost_write_pending(struct se_cmd
*se_cmd
)
495 /* Go ahead and process the write immediately */
496 target_execute_cmd(se_cmd
);
500 static int tcm_vhost_write_pending_status(struct se_cmd
*se_cmd
)
505 static void tcm_vhost_set_default_node_attrs(struct se_node_acl
*nacl
)
510 static u32
tcm_vhost_get_task_tag(struct se_cmd
*se_cmd
)
515 static int tcm_vhost_get_cmd_state(struct se_cmd
*se_cmd
)
520 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd
*cmd
)
522 struct vhost_scsi
*vs
= cmd
->tvc_vhost
;
524 llist_add(&cmd
->tvc_completion_list
, &vs
->vs_completion_list
);
526 vhost_work_queue(&vs
->dev
, &vs
->vs_completion_work
);
529 static int tcm_vhost_queue_data_in(struct se_cmd
*se_cmd
)
531 struct tcm_vhost_cmd
*cmd
= container_of(se_cmd
,
532 struct tcm_vhost_cmd
, tvc_se_cmd
);
533 vhost_scsi_complete_cmd(cmd
);
537 static int tcm_vhost_queue_status(struct se_cmd
*se_cmd
)
539 struct tcm_vhost_cmd
*cmd
= container_of(se_cmd
,
540 struct tcm_vhost_cmd
, tvc_se_cmd
);
541 vhost_scsi_complete_cmd(cmd
);
545 static void tcm_vhost_queue_tm_rsp(struct se_cmd
*se_cmd
)
550 static void tcm_vhost_aborted_task(struct se_cmd
*se_cmd
)
555 static void tcm_vhost_free_evt(struct vhost_scsi
*vs
, struct tcm_vhost_evt
*evt
)
561 static struct tcm_vhost_evt
*
562 tcm_vhost_allocate_evt(struct vhost_scsi
*vs
,
563 u32 event
, u32 reason
)
565 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
566 struct tcm_vhost_evt
*evt
;
568 if (vs
->vs_events_nr
> VHOST_SCSI_MAX_EVENT
) {
569 vs
->vs_events_missed
= true;
573 evt
= kzalloc(sizeof(*evt
), GFP_KERNEL
);
575 vq_err(vq
, "Failed to allocate tcm_vhost_evt\n");
576 vs
->vs_events_missed
= true;
580 evt
->event
.event
= event
;
581 evt
->event
.reason
= reason
;
587 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd
*cmd
)
589 struct se_cmd
*se_cmd
= &cmd
->tvc_se_cmd
;
591 /* TODO locking against target/backend threads? */
592 transport_generic_free_cmd(se_cmd
, 0);
596 static int vhost_scsi_check_stop_free(struct se_cmd
*se_cmd
)
598 return target_put_sess_cmd(se_cmd
);
602 tcm_vhost_do_evt_work(struct vhost_scsi
*vs
, struct tcm_vhost_evt
*evt
)
604 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
605 struct virtio_scsi_event
*event
= &evt
->event
;
606 struct virtio_scsi_event __user
*eventp
;
610 if (!vq
->private_data
) {
611 vs
->vs_events_missed
= true;
616 vhost_disable_notify(&vs
->dev
, vq
);
617 head
= vhost_get_vq_desc(vq
, vq
->iov
,
618 ARRAY_SIZE(vq
->iov
), &out
, &in
,
621 vs
->vs_events_missed
= true;
624 if (head
== vq
->num
) {
625 if (vhost_enable_notify(&vs
->dev
, vq
))
627 vs
->vs_events_missed
= true;
631 if ((vq
->iov
[out
].iov_len
!= sizeof(struct virtio_scsi_event
))) {
632 vq_err(vq
, "Expecting virtio_scsi_event, got %zu bytes\n",
633 vq
->iov
[out
].iov_len
);
634 vs
->vs_events_missed
= true;
638 if (vs
->vs_events_missed
) {
639 event
->event
|= VIRTIO_SCSI_T_EVENTS_MISSED
;
640 vs
->vs_events_missed
= false;
643 eventp
= vq
->iov
[out
].iov_base
;
644 ret
= __copy_to_user(eventp
, event
, sizeof(*event
));
646 vhost_add_used_and_signal(&vs
->dev
, vq
, head
, 0);
648 vq_err(vq
, "Faulted on tcm_vhost_send_event\n");
651 static void tcm_vhost_evt_work(struct vhost_work
*work
)
653 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
655 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
656 struct tcm_vhost_evt
*evt
;
657 struct llist_node
*llnode
;
659 mutex_lock(&vq
->mutex
);
660 llnode
= llist_del_all(&vs
->vs_event_list
);
662 evt
= llist_entry(llnode
, struct tcm_vhost_evt
, list
);
663 llnode
= llist_next(llnode
);
664 tcm_vhost_do_evt_work(vs
, evt
);
665 tcm_vhost_free_evt(vs
, evt
);
667 mutex_unlock(&vq
->mutex
);
670 /* Fill in status and signal that we are done processing this command
672 * This is scheduled in the vhost work queue so we are called with the owner
673 * process mm and can access the vring.
675 static void vhost_scsi_complete_cmd_work(struct vhost_work
*work
)
677 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
679 DECLARE_BITMAP(signal
, VHOST_SCSI_MAX_VQ
);
680 struct virtio_scsi_cmd_resp v_rsp
;
681 struct tcm_vhost_cmd
*cmd
;
682 struct llist_node
*llnode
;
683 struct se_cmd
*se_cmd
;
686 bitmap_zero(signal
, VHOST_SCSI_MAX_VQ
);
687 llnode
= llist_del_all(&vs
->vs_completion_list
);
689 cmd
= llist_entry(llnode
, struct tcm_vhost_cmd
,
690 tvc_completion_list
);
691 llnode
= llist_next(llnode
);
692 se_cmd
= &cmd
->tvc_se_cmd
;
694 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__
,
695 cmd
, se_cmd
->residual_count
, se_cmd
->scsi_status
);
697 memset(&v_rsp
, 0, sizeof(v_rsp
));
698 v_rsp
.resid
= se_cmd
->residual_count
;
699 /* TODO is status_qualifier field needed? */
700 v_rsp
.status
= se_cmd
->scsi_status
;
701 v_rsp
.sense_len
= se_cmd
->scsi_sense_length
;
702 memcpy(v_rsp
.sense
, cmd
->tvc_sense_buf
,
704 ret
= copy_to_user(cmd
->tvc_resp
, &v_rsp
, sizeof(v_rsp
));
705 if (likely(ret
== 0)) {
706 struct vhost_scsi_virtqueue
*q
;
707 vhost_add_used(cmd
->tvc_vq
, cmd
->tvc_vq_desc
, 0);
708 q
= container_of(cmd
->tvc_vq
, struct vhost_scsi_virtqueue
, vq
);
710 __set_bit(vq
, signal
);
712 pr_err("Faulted on virtio_scsi_cmd_resp\n");
714 vhost_scsi_free_cmd(cmd
);
718 while ((vq
= find_next_bit(signal
, VHOST_SCSI_MAX_VQ
, vq
+ 1))
720 vhost_signal(&vs
->dev
, &vs
->vqs
[vq
].vq
);
723 static struct tcm_vhost_cmd
*
724 vhost_scsi_get_tag(struct vhost_virtqueue
*vq
, struct tcm_vhost_tpg
*tpg
,
725 unsigned char *cdb
, u64 scsi_tag
, u16 lun
, u8 task_attr
,
726 u32 exp_data_len
, int data_direction
)
728 struct tcm_vhost_cmd
*cmd
;
729 struct tcm_vhost_nexus
*tv_nexus
;
730 struct se_session
*se_sess
;
731 struct scatterlist
*sg
, *prot_sg
;
735 tv_nexus
= tpg
->tpg_nexus
;
737 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
738 return ERR_PTR(-EIO
);
740 se_sess
= tv_nexus
->tvn_se_sess
;
742 tag
= percpu_ida_alloc(&se_sess
->sess_tag_pool
, TASK_RUNNING
);
744 pr_err("Unable to obtain tag for tcm_vhost_cmd\n");
745 return ERR_PTR(-ENOMEM
);
748 cmd
= &((struct tcm_vhost_cmd
*)se_sess
->sess_cmd_map
)[tag
];
750 prot_sg
= cmd
->tvc_prot_sgl
;
751 pages
= cmd
->tvc_upages
;
752 memset(cmd
, 0, sizeof(struct tcm_vhost_cmd
));
755 cmd
->tvc_prot_sgl
= prot_sg
;
756 cmd
->tvc_upages
= pages
;
757 cmd
->tvc_se_cmd
.map_tag
= tag
;
758 cmd
->tvc_tag
= scsi_tag
;
760 cmd
->tvc_task_attr
= 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
);
766 memcpy(cmd
->tvc_cdb
, cdb
, TCM_VHOST_MAX_CDB_SIZE
);
772 * Map a user memory range into a scatterlist
774 * Returns the number of scatterlist entries used or -errno on error.
777 vhost_scsi_map_to_sgl(struct tcm_vhost_cmd
*tv_cmd
,
778 struct scatterlist
*sgl
,
779 unsigned int sgl_count
,
784 unsigned int npages
= 0, pages_nr
, offset
, nbytes
;
785 struct scatterlist
*sg
= sgl
;
786 void __user
*ptr
= iov
->iov_base
;
787 size_t len
= iov
->iov_len
;
790 pages_nr
= iov_num_pages(iov
);
791 if (pages_nr
> sgl_count
) {
792 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
793 " sgl_count: %u\n", pages_nr
, sgl_count
);
796 if (pages_nr
> TCM_VHOST_PREALLOC_UPAGES
) {
797 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
798 " preallocated TCM_VHOST_PREALLOC_UPAGES: %u\n",
799 pages_nr
, TCM_VHOST_PREALLOC_UPAGES
);
803 ret
= get_user_pages_fast((unsigned long)ptr
, pages_nr
, write
, pages
);
804 /* No pages were pinned */
807 /* Less pages pinned than wanted */
808 if (ret
!= pages_nr
) {
809 for (i
= 0; i
< ret
; i
++)
816 offset
= (uintptr_t)ptr
& ~PAGE_MASK
;
817 nbytes
= min_t(unsigned int, PAGE_SIZE
- offset
, len
);
818 sg_set_page(sg
, pages
[npages
], nbytes
, offset
);
830 vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd
*cmd
,
835 struct scatterlist
*sg
= cmd
->tvc_sgl
;
836 unsigned int sgl_count
= 0;
839 for (i
= 0; i
< niov
; i
++)
840 sgl_count
+= iov_num_pages(&iov
[i
]);
842 if (sgl_count
> TCM_VHOST_PREALLOC_SGLS
) {
843 pr_err("vhost_scsi_map_iov_to_sgl() sgl_count: %u greater than"
844 " preallocated TCM_VHOST_PREALLOC_SGLS: %u\n",
845 sgl_count
, TCM_VHOST_PREALLOC_SGLS
);
849 pr_debug("%s sg %p sgl_count %u\n", __func__
, sg
, sgl_count
);
850 sg_init_table(sg
, sgl_count
);
851 cmd
->tvc_sgl_count
= sgl_count
;
853 pr_debug("Mapping iovec %p for %u pages\n", &iov
[0], sgl_count
);
855 for (i
= 0; i
< niov
; i
++) {
856 ret
= vhost_scsi_map_to_sgl(cmd
, sg
, sgl_count
, &iov
[i
],
857 cmd
->tvc_upages
, write
);
859 for (i
= 0; i
< cmd
->tvc_sgl_count
; i
++)
860 put_page(sg_page(&cmd
->tvc_sgl
[i
]));
862 cmd
->tvc_sgl_count
= 0;
872 vhost_scsi_map_iov_to_prot(struct tcm_vhost_cmd
*cmd
,
877 struct scatterlist
*prot_sg
= cmd
->tvc_prot_sgl
;
878 unsigned int prot_sgl_count
= 0;
881 for (i
= 0; i
< niov
; i
++)
882 prot_sgl_count
+= iov_num_pages(&iov
[i
]);
884 if (prot_sgl_count
> TCM_VHOST_PREALLOC_PROT_SGLS
) {
885 pr_err("vhost_scsi_map_iov_to_prot() sgl_count: %u greater than"
886 " preallocated TCM_VHOST_PREALLOC_PROT_SGLS: %u\n",
887 prot_sgl_count
, TCM_VHOST_PREALLOC_PROT_SGLS
);
891 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__
,
892 prot_sg
, prot_sgl_count
);
893 sg_init_table(prot_sg
, prot_sgl_count
);
894 cmd
->tvc_prot_sgl_count
= prot_sgl_count
;
896 for (i
= 0; i
< niov
; i
++) {
897 ret
= vhost_scsi_map_to_sgl(cmd
, prot_sg
, prot_sgl_count
, &iov
[i
],
898 cmd
->tvc_upages
, write
);
900 for (i
= 0; i
< cmd
->tvc_prot_sgl_count
; i
++)
901 put_page(sg_page(&cmd
->tvc_prot_sgl
[i
]));
903 cmd
->tvc_prot_sgl_count
= 0;
907 prot_sgl_count
-= ret
;
912 static int vhost_scsi_to_tcm_attr(int attr
)
915 case VIRTIO_SCSI_S_SIMPLE
:
916 return MSG_SIMPLE_TAG
;
917 case VIRTIO_SCSI_S_ORDERED
:
918 return MSG_ORDERED_TAG
;
919 case VIRTIO_SCSI_S_HEAD
:
921 case VIRTIO_SCSI_S_ACA
:
926 return MSG_SIMPLE_TAG
;
929 static void tcm_vhost_submission_work(struct work_struct
*work
)
931 struct tcm_vhost_cmd
*cmd
=
932 container_of(work
, struct tcm_vhost_cmd
, work
);
933 struct tcm_vhost_nexus
*tv_nexus
;
934 struct se_cmd
*se_cmd
= &cmd
->tvc_se_cmd
;
935 struct scatterlist
*sg_ptr
, *sg_prot_ptr
= NULL
;
938 /* FIXME: BIDI operation */
939 if (cmd
->tvc_sgl_count
) {
940 sg_ptr
= cmd
->tvc_sgl
;
942 if (cmd
->tvc_prot_sgl_count
)
943 sg_prot_ptr
= cmd
->tvc_prot_sgl
;
945 se_cmd
->prot_pto
= true;
949 tv_nexus
= cmd
->tvc_nexus
;
951 rc
= target_submit_cmd_map_sgls(se_cmd
, tv_nexus
->tvn_se_sess
,
952 cmd
->tvc_cdb
, &cmd
->tvc_sense_buf
[0],
953 cmd
->tvc_lun
, cmd
->tvc_exp_data_len
,
954 vhost_scsi_to_tcm_attr(cmd
->tvc_task_attr
),
955 cmd
->tvc_data_direction
, TARGET_SCF_ACK_KREF
,
956 sg_ptr
, cmd
->tvc_sgl_count
, NULL
, 0,
957 sg_prot_ptr
, cmd
->tvc_prot_sgl_count
);
959 transport_send_check_condition_and_sense(se_cmd
,
960 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
961 transport_generic_free_cmd(se_cmd
, 0);
966 vhost_scsi_send_bad_target(struct vhost_scsi
*vs
,
967 struct vhost_virtqueue
*vq
,
968 int head
, unsigned out
)
970 struct virtio_scsi_cmd_resp __user
*resp
;
971 struct virtio_scsi_cmd_resp rsp
;
974 memset(&rsp
, 0, sizeof(rsp
));
975 rsp
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
976 resp
= vq
->iov
[out
].iov_base
;
977 ret
= __copy_to_user(resp
, &rsp
, sizeof(rsp
));
979 vhost_add_used_and_signal(&vs
->dev
, vq
, head
, 0);
981 pr_err("Faulted on virtio_scsi_cmd_resp\n");
985 vhost_scsi_handle_vq(struct vhost_scsi
*vs
, struct vhost_virtqueue
*vq
)
987 struct tcm_vhost_tpg
**vs_tpg
;
988 struct virtio_scsi_cmd_req v_req
;
989 struct virtio_scsi_cmd_req_pi v_req_pi
;
990 struct tcm_vhost_tpg
*tpg
;
991 struct tcm_vhost_cmd
*cmd
;
993 u32 exp_data_len
, data_first
, data_num
, data_direction
, prot_first
;
995 int head
, ret
, data_niov
, prot_niov
, prot_bytes
;
998 u8
*target
, *lunp
, task_attr
;
1002 mutex_lock(&vq
->mutex
);
1004 * We can handle the vq only after the endpoint is setup by calling the
1005 * VHOST_SCSI_SET_ENDPOINT ioctl.
1007 vs_tpg
= vq
->private_data
;
1011 vhost_disable_notify(&vs
->dev
, vq
);
1014 head
= vhost_get_vq_desc(vq
, vq
->iov
,
1015 ARRAY_SIZE(vq
->iov
), &out
, &in
,
1017 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
1019 /* On error, stop handling until the next kick. */
1020 if (unlikely(head
< 0))
1022 /* Nothing new? Wait for eventfd to tell us they refilled. */
1023 if (head
== vq
->num
) {
1024 if (unlikely(vhost_enable_notify(&vs
->dev
, vq
))) {
1025 vhost_disable_notify(&vs
->dev
, vq
);
1031 /* FIXME: BIDI operation */
1032 if (out
== 1 && in
== 1) {
1033 data_direction
= DMA_NONE
;
1036 } else if (out
== 1 && in
> 1) {
1037 data_direction
= DMA_FROM_DEVICE
;
1038 data_first
= out
+ 1;
1040 } else if (out
> 1 && in
== 1) {
1041 data_direction
= DMA_TO_DEVICE
;
1045 vq_err(vq
, "Invalid buffer layout out: %u in: %u\n",
1051 * Check for a sane resp buffer so we can report errors to
1054 if (unlikely(vq
->iov
[out
].iov_len
!=
1055 sizeof(struct virtio_scsi_cmd_resp
))) {
1056 vq_err(vq
, "Expecting virtio_scsi_cmd_resp, got %zu"
1057 " bytes\n", vq
->iov
[out
].iov_len
);
1061 if (vhost_has_feature(vq
, VIRTIO_SCSI_F_T10_PI
)) {
1063 lunp
= &v_req_pi
.lun
[0];
1064 target
= &v_req_pi
.lun
[1];
1065 req_size
= sizeof(v_req_pi
);
1069 lunp
= &v_req
.lun
[0];
1070 target
= &v_req
.lun
[1];
1071 req_size
= sizeof(v_req
);
1075 if (unlikely(vq
->iov
[0].iov_len
< req_size
)) {
1076 pr_err("Expecting virtio-scsi header: %zu, got %zu\n",
1077 req_size
, vq
->iov
[0].iov_len
);
1080 ret
= memcpy_fromiovecend(req
, &vq
->iov
[0], 0, req_size
);
1081 if (unlikely(ret
)) {
1082 vq_err(vq
, "Faulted on virtio_scsi_cmd_req\n");
1086 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
1087 if (unlikely(*lunp
!= 1)) {
1088 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1092 tpg
= ACCESS_ONCE(vs_tpg
[*target
]);
1094 /* Target does not exist, fail the request */
1095 if (unlikely(!tpg
)) {
1096 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1100 data_niov
= data_num
;
1101 prot_niov
= prot_first
= prot_bytes
= 0;
1103 * Determine if any protection information iovecs are preceeding
1104 * the actual data payload, and adjust data_first + data_niov
1105 * values accordingly for vhost_scsi_map_iov_to_sgl() below.
1107 * Also extract virtio_scsi header bits for vhost_scsi_get_tag()
1110 if (v_req_pi
.pi_bytesout
) {
1111 if (data_direction
!= DMA_TO_DEVICE
) {
1112 vq_err(vq
, "Received non zero do_pi_niov"
1113 ", but wrong data_direction\n");
1116 prot_bytes
= v_req_pi
.pi_bytesout
;
1117 } else if (v_req_pi
.pi_bytesin
) {
1118 if (data_direction
!= DMA_FROM_DEVICE
) {
1119 vq_err(vq
, "Received non zero di_pi_niov"
1120 ", but wrong data_direction\n");
1123 prot_bytes
= v_req_pi
.pi_bytesin
;
1128 for (i
= 0; i
< data_num
; i
++) {
1129 tmp
+= vq
->iov
[data_first
+ i
].iov_len
;
1131 if (tmp
>= prot_bytes
)
1134 prot_first
= data_first
;
1135 data_first
+= prot_niov
;
1136 data_niov
= data_num
- prot_niov
;
1139 task_attr
= v_req_pi
.task_attr
;
1140 cdb
= &v_req_pi
.cdb
[0];
1141 lun
= ((v_req_pi
.lun
[2] << 8) | v_req_pi
.lun
[3]) & 0x3FFF;
1144 task_attr
= v_req
.task_attr
;
1145 cdb
= &v_req
.cdb
[0];
1146 lun
= ((v_req
.lun
[2] << 8) | v_req
.lun
[3]) & 0x3FFF;
1149 for (i
= 0; i
< data_niov
; i
++)
1150 exp_data_len
+= vq
->iov
[data_first
+ i
].iov_len
;
1152 * Check that the recieved CDB size does not exceeded our
1153 * hardcoded max for vhost-scsi
1155 * TODO what if cdb was too small for varlen cdb header?
1157 if (unlikely(scsi_command_size(cdb
) > TCM_VHOST_MAX_CDB_SIZE
)) {
1158 vq_err(vq
, "Received SCSI CDB with command_size: %d that"
1159 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1160 scsi_command_size(cdb
), TCM_VHOST_MAX_CDB_SIZE
);
1164 cmd
= vhost_scsi_get_tag(vq
, tpg
, cdb
, tag
, lun
, task_attr
,
1165 exp_data_len
+ prot_bytes
,
1168 vq_err(vq
, "vhost_scsi_get_tag failed %ld\n",
1173 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
1174 ": %d\n", cmd
, exp_data_len
, data_direction
);
1176 cmd
->tvc_vhost
= vs
;
1178 cmd
->tvc_resp
= vq
->iov
[out
].iov_base
;
1180 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1181 cmd
->tvc_cdb
[0], cmd
->tvc_lun
);
1184 ret
= vhost_scsi_map_iov_to_prot(cmd
,
1185 &vq
->iov
[prot_first
], prot_niov
,
1186 data_direction
== DMA_FROM_DEVICE
);
1187 if (unlikely(ret
)) {
1188 vq_err(vq
, "Failed to map iov to"
1193 if (data_direction
!= DMA_NONE
) {
1194 ret
= vhost_scsi_map_iov_to_sgl(cmd
,
1195 &vq
->iov
[data_first
], data_niov
,
1196 data_direction
== DMA_FROM_DEVICE
);
1197 if (unlikely(ret
)) {
1198 vq_err(vq
, "Failed to map iov to sgl\n");
1203 * Save the descriptor from vhost_get_vq_desc() to be used to
1204 * complete the virtio-scsi request in TCM callback context via
1205 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
1207 cmd
->tvc_vq_desc
= head
;
1209 * Dispatch tv_cmd descriptor for cmwq execution in process
1210 * context provided by tcm_vhost_workqueue. This also ensures
1211 * tv_cmd is executed on the same kworker CPU as this vhost
1212 * thread to gain positive L2 cache locality effects..
1214 INIT_WORK(&cmd
->work
, tcm_vhost_submission_work
);
1215 queue_work(tcm_vhost_workqueue
, &cmd
->work
);
1218 mutex_unlock(&vq
->mutex
);
1222 vhost_scsi_free_cmd(cmd
);
1224 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1226 mutex_unlock(&vq
->mutex
);
1229 static void vhost_scsi_ctl_handle_kick(struct vhost_work
*work
)
1231 pr_debug("%s: The handling func for control queue.\n", __func__
);
1235 tcm_vhost_send_evt(struct vhost_scsi
*vs
,
1236 struct tcm_vhost_tpg
*tpg
,
1241 struct tcm_vhost_evt
*evt
;
1243 evt
= tcm_vhost_allocate_evt(vs
, event
, reason
);
1248 /* TODO: share lun setup code with virtio-scsi.ko */
1250 * Note: evt->event is zeroed when we allocate it and
1251 * lun[4-7] need to be zero according to virtio-scsi spec.
1253 evt
->event
.lun
[0] = 0x01;
1254 evt
->event
.lun
[1] = tpg
->tport_tpgt
;
1255 if (lun
->unpacked_lun
>= 256)
1256 evt
->event
.lun
[2] = lun
->unpacked_lun
>> 8 | 0x40 ;
1257 evt
->event
.lun
[3] = lun
->unpacked_lun
& 0xFF;
1260 llist_add(&evt
->list
, &vs
->vs_event_list
);
1261 vhost_work_queue(&vs
->dev
, &vs
->vs_event_work
);
1264 static void vhost_scsi_evt_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 mutex_lock(&vq
->mutex
);
1271 if (!vq
->private_data
)
1274 if (vs
->vs_events_missed
)
1275 tcm_vhost_send_evt(vs
, NULL
, NULL
, VIRTIO_SCSI_T_NO_EVENT
, 0);
1277 mutex_unlock(&vq
->mutex
);
1280 static void vhost_scsi_handle_kick(struct vhost_work
*work
)
1282 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
1284 struct vhost_scsi
*vs
= container_of(vq
->dev
, struct vhost_scsi
, dev
);
1286 vhost_scsi_handle_vq(vs
, vq
);
1289 static void vhost_scsi_flush_vq(struct vhost_scsi
*vs
, int index
)
1291 vhost_poll_flush(&vs
->vqs
[index
].vq
.poll
);
1294 /* Callers must hold dev mutex */
1295 static void vhost_scsi_flush(struct vhost_scsi
*vs
)
1297 struct vhost_scsi_inflight
*old_inflight
[VHOST_SCSI_MAX_VQ
];
1300 /* Init new inflight and remember the old inflight */
1301 tcm_vhost_init_inflight(vs
, old_inflight
);
1304 * The inflight->kref was initialized to 1. We decrement it here to
1305 * indicate the start of the flush operation so that it will reach 0
1306 * when all the reqs are finished.
1308 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1309 kref_put(&old_inflight
[i
]->kref
, tcm_vhost_done_inflight
);
1311 /* Flush both the vhost poll and vhost work */
1312 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1313 vhost_scsi_flush_vq(vs
, i
);
1314 vhost_work_flush(&vs
->dev
, &vs
->vs_completion_work
);
1315 vhost_work_flush(&vs
->dev
, &vs
->vs_event_work
);
1317 /* Wait for all reqs issued before the flush to be finished */
1318 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1319 wait_for_completion(&old_inflight
[i
]->comp
);
1323 * Called from vhost_scsi_ioctl() context to walk the list of available
1324 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
1326 * The lock nesting rule is:
1327 * tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1330 vhost_scsi_set_endpoint(struct vhost_scsi
*vs
,
1331 struct vhost_scsi_target
*t
)
1333 struct se_portal_group
*se_tpg
;
1334 struct tcm_vhost_tport
*tv_tport
;
1335 struct tcm_vhost_tpg
*tpg
;
1336 struct tcm_vhost_tpg
**vs_tpg
;
1337 struct vhost_virtqueue
*vq
;
1338 int index
, ret
, i
, len
;
1341 mutex_lock(&tcm_vhost_mutex
);
1342 mutex_lock(&vs
->dev
.mutex
);
1344 /* Verify that ring has been setup correctly. */
1345 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
1346 /* Verify that ring has been setup correctly. */
1347 if (!vhost_vq_access_ok(&vs
->vqs
[index
].vq
)) {
1353 len
= sizeof(vs_tpg
[0]) * VHOST_SCSI_MAX_TARGET
;
1354 vs_tpg
= kzalloc(len
, GFP_KERNEL
);
1360 memcpy(vs_tpg
, vs
->vs_tpg
, len
);
1362 list_for_each_entry(tpg
, &tcm_vhost_list
, tv_tpg_list
) {
1363 mutex_lock(&tpg
->tv_tpg_mutex
);
1364 if (!tpg
->tpg_nexus
) {
1365 mutex_unlock(&tpg
->tv_tpg_mutex
);
1368 if (tpg
->tv_tpg_vhost_count
!= 0) {
1369 mutex_unlock(&tpg
->tv_tpg_mutex
);
1372 tv_tport
= tpg
->tport
;
1374 if (!strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
)) {
1375 if (vs
->vs_tpg
&& vs
->vs_tpg
[tpg
->tport_tpgt
]) {
1377 mutex_unlock(&tpg
->tv_tpg_mutex
);
1382 * In order to ensure individual vhost-scsi configfs
1383 * groups cannot be removed while in use by vhost ioctl,
1384 * go ahead and take an explicit se_tpg->tpg_group.cg_item
1387 se_tpg
= &tpg
->se_tpg
;
1388 ret
= configfs_depend_item(se_tpg
->se_tpg_tfo
->tf_subsys
,
1389 &se_tpg
->tpg_group
.cg_item
);
1391 pr_warn("configfs_depend_item() failed: %d\n", ret
);
1393 mutex_unlock(&tpg
->tv_tpg_mutex
);
1396 tpg
->tv_tpg_vhost_count
++;
1397 tpg
->vhost_scsi
= vs
;
1398 vs_tpg
[tpg
->tport_tpgt
] = tpg
;
1399 smp_mb__after_atomic();
1402 mutex_unlock(&tpg
->tv_tpg_mutex
);
1406 memcpy(vs
->vs_vhost_wwpn
, t
->vhost_wwpn
,
1407 sizeof(vs
->vs_vhost_wwpn
));
1408 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1409 vq
= &vs
->vqs
[i
].vq
;
1410 mutex_lock(&vq
->mutex
);
1411 vq
->private_data
= vs_tpg
;
1412 vhost_init_used(vq
);
1413 mutex_unlock(&vq
->mutex
);
1421 * Act as synchronize_rcu to make sure access to
1422 * old vs->vs_tpg is finished.
1424 vhost_scsi_flush(vs
);
1426 vs
->vs_tpg
= vs_tpg
;
1429 mutex_unlock(&vs
->dev
.mutex
);
1430 mutex_unlock(&tcm_vhost_mutex
);
1435 vhost_scsi_clear_endpoint(struct vhost_scsi
*vs
,
1436 struct vhost_scsi_target
*t
)
1438 struct se_portal_group
*se_tpg
;
1439 struct tcm_vhost_tport
*tv_tport
;
1440 struct tcm_vhost_tpg
*tpg
;
1441 struct vhost_virtqueue
*vq
;
1446 mutex_lock(&tcm_vhost_mutex
);
1447 mutex_lock(&vs
->dev
.mutex
);
1448 /* Verify that ring has been setup correctly. */
1449 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
1450 if (!vhost_vq_access_ok(&vs
->vqs
[index
].vq
)) {
1461 for (i
= 0; i
< VHOST_SCSI_MAX_TARGET
; i
++) {
1463 tpg
= vs
->vs_tpg
[target
];
1467 mutex_lock(&tpg
->tv_tpg_mutex
);
1468 tv_tport
= tpg
->tport
;
1474 if (strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
)) {
1475 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
1476 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1477 tv_tport
->tport_name
, tpg
->tport_tpgt
,
1478 t
->vhost_wwpn
, t
->vhost_tpgt
);
1482 tpg
->tv_tpg_vhost_count
--;
1483 tpg
->vhost_scsi
= NULL
;
1484 vs
->vs_tpg
[target
] = NULL
;
1486 mutex_unlock(&tpg
->tv_tpg_mutex
);
1488 * Release se_tpg->tpg_group.cg_item configfs dependency now
1489 * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
1491 se_tpg
= &tpg
->se_tpg
;
1492 configfs_undepend_item(se_tpg
->se_tpg_tfo
->tf_subsys
,
1493 &se_tpg
->tpg_group
.cg_item
);
1496 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1497 vq
= &vs
->vqs
[i
].vq
;
1498 mutex_lock(&vq
->mutex
);
1499 vq
->private_data
= NULL
;
1500 mutex_unlock(&vq
->mutex
);
1504 * Act as synchronize_rcu to make sure access to
1505 * old vs->vs_tpg is finished.
1507 vhost_scsi_flush(vs
);
1510 WARN_ON(vs
->vs_events_nr
);
1511 mutex_unlock(&vs
->dev
.mutex
);
1512 mutex_unlock(&tcm_vhost_mutex
);
1516 mutex_unlock(&tpg
->tv_tpg_mutex
);
1518 mutex_unlock(&vs
->dev
.mutex
);
1519 mutex_unlock(&tcm_vhost_mutex
);
1523 static int vhost_scsi_set_features(struct vhost_scsi
*vs
, u64 features
)
1525 struct vhost_virtqueue
*vq
;
1528 if (features
& ~VHOST_SCSI_FEATURES
)
1531 mutex_lock(&vs
->dev
.mutex
);
1532 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
1533 !vhost_log_access_ok(&vs
->dev
)) {
1534 mutex_unlock(&vs
->dev
.mutex
);
1538 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1539 vq
= &vs
->vqs
[i
].vq
;
1540 mutex_lock(&vq
->mutex
);
1541 vq
->acked_features
= features
;
1542 mutex_unlock(&vq
->mutex
);
1544 mutex_unlock(&vs
->dev
.mutex
);
1548 static int vhost_scsi_open(struct inode
*inode
, struct file
*f
)
1550 struct vhost_scsi
*vs
;
1551 struct vhost_virtqueue
**vqs
;
1554 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
| __GFP_NOWARN
| __GFP_REPEAT
);
1556 vs
= vzalloc(sizeof(*vs
));
1561 vqs
= kmalloc(VHOST_SCSI_MAX_VQ
* sizeof(*vqs
), GFP_KERNEL
);
1565 vhost_work_init(&vs
->vs_completion_work
, vhost_scsi_complete_cmd_work
);
1566 vhost_work_init(&vs
->vs_event_work
, tcm_vhost_evt_work
);
1568 vs
->vs_events_nr
= 0;
1569 vs
->vs_events_missed
= false;
1571 vqs
[VHOST_SCSI_VQ_CTL
] = &vs
->vqs
[VHOST_SCSI_VQ_CTL
].vq
;
1572 vqs
[VHOST_SCSI_VQ_EVT
] = &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1573 vs
->vqs
[VHOST_SCSI_VQ_CTL
].vq
.handle_kick
= vhost_scsi_ctl_handle_kick
;
1574 vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
.handle_kick
= vhost_scsi_evt_handle_kick
;
1575 for (i
= VHOST_SCSI_VQ_IO
; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1576 vqs
[i
] = &vs
->vqs
[i
].vq
;
1577 vs
->vqs
[i
].vq
.handle_kick
= vhost_scsi_handle_kick
;
1579 vhost_dev_init(&vs
->dev
, vqs
, VHOST_SCSI_MAX_VQ
);
1581 tcm_vhost_init_inflight(vs
, NULL
);
1583 f
->private_data
= vs
;
1592 static int vhost_scsi_release(struct inode
*inode
, struct file
*f
)
1594 struct vhost_scsi
*vs
= f
->private_data
;
1595 struct vhost_scsi_target t
;
1597 mutex_lock(&vs
->dev
.mutex
);
1598 memcpy(t
.vhost_wwpn
, vs
->vs_vhost_wwpn
, sizeof(t
.vhost_wwpn
));
1599 mutex_unlock(&vs
->dev
.mutex
);
1600 vhost_scsi_clear_endpoint(vs
, &t
);
1601 vhost_dev_stop(&vs
->dev
);
1602 vhost_dev_cleanup(&vs
->dev
, false);
1603 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1604 vhost_scsi_flush(vs
);
1611 vhost_scsi_ioctl(struct file
*f
,
1615 struct vhost_scsi
*vs
= f
->private_data
;
1616 struct vhost_scsi_target backend
;
1617 void __user
*argp
= (void __user
*)arg
;
1618 u64 __user
*featurep
= argp
;
1619 u32 __user
*eventsp
= argp
;
1622 int r
, abi_version
= VHOST_SCSI_ABI_VERSION
;
1623 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1626 case VHOST_SCSI_SET_ENDPOINT
:
1627 if (copy_from_user(&backend
, argp
, sizeof backend
))
1629 if (backend
.reserved
!= 0)
1632 return vhost_scsi_set_endpoint(vs
, &backend
);
1633 case VHOST_SCSI_CLEAR_ENDPOINT
:
1634 if (copy_from_user(&backend
, argp
, sizeof backend
))
1636 if (backend
.reserved
!= 0)
1639 return vhost_scsi_clear_endpoint(vs
, &backend
);
1640 case VHOST_SCSI_GET_ABI_VERSION
:
1641 if (copy_to_user(argp
, &abi_version
, sizeof abi_version
))
1644 case VHOST_SCSI_SET_EVENTS_MISSED
:
1645 if (get_user(events_missed
, eventsp
))
1647 mutex_lock(&vq
->mutex
);
1648 vs
->vs_events_missed
= events_missed
;
1649 mutex_unlock(&vq
->mutex
);
1651 case VHOST_SCSI_GET_EVENTS_MISSED
:
1652 mutex_lock(&vq
->mutex
);
1653 events_missed
= vs
->vs_events_missed
;
1654 mutex_unlock(&vq
->mutex
);
1655 if (put_user(events_missed
, eventsp
))
1658 case VHOST_GET_FEATURES
:
1659 features
= VHOST_SCSI_FEATURES
;
1660 if (copy_to_user(featurep
, &features
, sizeof features
))
1663 case VHOST_SET_FEATURES
:
1664 if (copy_from_user(&features
, featurep
, sizeof features
))
1666 return vhost_scsi_set_features(vs
, features
);
1668 mutex_lock(&vs
->dev
.mutex
);
1669 r
= vhost_dev_ioctl(&vs
->dev
, ioctl
, argp
);
1670 /* TODO: flush backend after dev ioctl. */
1671 if (r
== -ENOIOCTLCMD
)
1672 r
= vhost_vring_ioctl(&vs
->dev
, ioctl
, argp
);
1673 mutex_unlock(&vs
->dev
.mutex
);
1678 #ifdef CONFIG_COMPAT
1679 static long vhost_scsi_compat_ioctl(struct file
*f
, unsigned int ioctl
,
1682 return vhost_scsi_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
1686 static const struct file_operations vhost_scsi_fops
= {
1687 .owner
= THIS_MODULE
,
1688 .release
= vhost_scsi_release
,
1689 .unlocked_ioctl
= vhost_scsi_ioctl
,
1690 #ifdef CONFIG_COMPAT
1691 .compat_ioctl
= vhost_scsi_compat_ioctl
,
1693 .open
= vhost_scsi_open
,
1694 .llseek
= noop_llseek
,
1697 static struct miscdevice vhost_scsi_misc
= {
1703 static int __init
vhost_scsi_register(void)
1705 return misc_register(&vhost_scsi_misc
);
1708 static int vhost_scsi_deregister(void)
1710 return misc_deregister(&vhost_scsi_misc
);
1713 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport
*tport
)
1715 switch (tport
->tport_proto_id
) {
1716 case SCSI_PROTOCOL_SAS
:
1718 case SCSI_PROTOCOL_FCP
:
1720 case SCSI_PROTOCOL_ISCSI
:
1730 tcm_vhost_do_plug(struct tcm_vhost_tpg
*tpg
,
1731 struct se_lun
*lun
, bool plug
)
1734 struct vhost_scsi
*vs
= tpg
->vhost_scsi
;
1735 struct vhost_virtqueue
*vq
;
1741 mutex_lock(&vs
->dev
.mutex
);
1744 reason
= VIRTIO_SCSI_EVT_RESET_RESCAN
;
1746 reason
= VIRTIO_SCSI_EVT_RESET_REMOVED
;
1748 vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1749 mutex_lock(&vq
->mutex
);
1750 if (vhost_has_feature(vq
, VIRTIO_SCSI_F_HOTPLUG
))
1751 tcm_vhost_send_evt(vs
, tpg
, lun
,
1752 VIRTIO_SCSI_T_TRANSPORT_RESET
, reason
);
1753 mutex_unlock(&vq
->mutex
);
1754 mutex_unlock(&vs
->dev
.mutex
);
1757 static void tcm_vhost_hotplug(struct tcm_vhost_tpg
*tpg
, struct se_lun
*lun
)
1759 tcm_vhost_do_plug(tpg
, lun
, true);
1762 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg
*tpg
, struct se_lun
*lun
)
1764 tcm_vhost_do_plug(tpg
, lun
, false);
1767 static int tcm_vhost_port_link(struct se_portal_group
*se_tpg
,
1770 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1771 struct tcm_vhost_tpg
, se_tpg
);
1773 mutex_lock(&tcm_vhost_mutex
);
1775 mutex_lock(&tpg
->tv_tpg_mutex
);
1776 tpg
->tv_tpg_port_count
++;
1777 mutex_unlock(&tpg
->tv_tpg_mutex
);
1779 tcm_vhost_hotplug(tpg
, lun
);
1781 mutex_unlock(&tcm_vhost_mutex
);
1786 static void tcm_vhost_port_unlink(struct se_portal_group
*se_tpg
,
1789 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1790 struct tcm_vhost_tpg
, se_tpg
);
1792 mutex_lock(&tcm_vhost_mutex
);
1794 mutex_lock(&tpg
->tv_tpg_mutex
);
1795 tpg
->tv_tpg_port_count
--;
1796 mutex_unlock(&tpg
->tv_tpg_mutex
);
1798 tcm_vhost_hotunplug(tpg
, lun
);
1800 mutex_unlock(&tcm_vhost_mutex
);
1803 static struct se_node_acl
*
1804 tcm_vhost_make_nodeacl(struct se_portal_group
*se_tpg
,
1805 struct config_group
*group
,
1808 struct se_node_acl
*se_nacl
, *se_nacl_new
;
1809 struct tcm_vhost_nacl
*nacl
;
1813 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1814 return ERR_PTR(-EINVAL); */
1815 se_nacl_new
= tcm_vhost_alloc_fabric_acl(se_tpg
);
1817 return ERR_PTR(-ENOMEM
);
1821 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1822 * when converting a NodeACL from demo mode -> explict
1824 se_nacl
= core_tpg_add_initiator_node_acl(se_tpg
, se_nacl_new
,
1826 if (IS_ERR(se_nacl
)) {
1827 tcm_vhost_release_fabric_acl(se_tpg
, se_nacl_new
);
1831 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1833 nacl
= container_of(se_nacl
, struct tcm_vhost_nacl
, se_node_acl
);
1834 nacl
->iport_wwpn
= wwpn
;
1839 static void tcm_vhost_drop_nodeacl(struct se_node_acl
*se_acl
)
1841 struct tcm_vhost_nacl
*nacl
= container_of(se_acl
,
1842 struct tcm_vhost_nacl
, se_node_acl
);
1843 core_tpg_del_initiator_node_acl(se_acl
->se_tpg
, se_acl
, 1);
1847 static void tcm_vhost_free_cmd_map_res(struct tcm_vhost_nexus
*nexus
,
1848 struct se_session
*se_sess
)
1850 struct tcm_vhost_cmd
*tv_cmd
;
1853 if (!se_sess
->sess_cmd_map
)
1856 for (i
= 0; i
< TCM_VHOST_DEFAULT_TAGS
; i
++) {
1857 tv_cmd
= &((struct tcm_vhost_cmd
*)se_sess
->sess_cmd_map
)[i
];
1859 kfree(tv_cmd
->tvc_sgl
);
1860 kfree(tv_cmd
->tvc_prot_sgl
);
1861 kfree(tv_cmd
->tvc_upages
);
1865 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg
*tpg
,
1868 struct se_portal_group
*se_tpg
;
1869 struct se_session
*se_sess
;
1870 struct tcm_vhost_nexus
*tv_nexus
;
1871 struct tcm_vhost_cmd
*tv_cmd
;
1874 mutex_lock(&tpg
->tv_tpg_mutex
);
1875 if (tpg
->tpg_nexus
) {
1876 mutex_unlock(&tpg
->tv_tpg_mutex
);
1877 pr_debug("tpg->tpg_nexus already exists\n");
1880 se_tpg
= &tpg
->se_tpg
;
1882 tv_nexus
= kzalloc(sizeof(struct tcm_vhost_nexus
), GFP_KERNEL
);
1884 mutex_unlock(&tpg
->tv_tpg_mutex
);
1885 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1889 * Initialize the struct se_session pointer and setup tagpool
1890 * for struct tcm_vhost_cmd descriptors
1892 tv_nexus
->tvn_se_sess
= transport_init_session_tags(
1893 TCM_VHOST_DEFAULT_TAGS
,
1894 sizeof(struct tcm_vhost_cmd
),
1895 TARGET_PROT_DIN_PASS
| TARGET_PROT_DOUT_PASS
);
1896 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1897 mutex_unlock(&tpg
->tv_tpg_mutex
);
1901 se_sess
= tv_nexus
->tvn_se_sess
;
1902 for (i
= 0; i
< TCM_VHOST_DEFAULT_TAGS
; i
++) {
1903 tv_cmd
= &((struct tcm_vhost_cmd
*)se_sess
->sess_cmd_map
)[i
];
1905 tv_cmd
->tvc_sgl
= kzalloc(sizeof(struct scatterlist
) *
1906 TCM_VHOST_PREALLOC_SGLS
, GFP_KERNEL
);
1907 if (!tv_cmd
->tvc_sgl
) {
1908 mutex_unlock(&tpg
->tv_tpg_mutex
);
1909 pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1913 tv_cmd
->tvc_upages
= kzalloc(sizeof(struct page
*) *
1914 TCM_VHOST_PREALLOC_UPAGES
, GFP_KERNEL
);
1915 if (!tv_cmd
->tvc_upages
) {
1916 mutex_unlock(&tpg
->tv_tpg_mutex
);
1917 pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1921 tv_cmd
->tvc_prot_sgl
= kzalloc(sizeof(struct scatterlist
) *
1922 TCM_VHOST_PREALLOC_PROT_SGLS
, GFP_KERNEL
);
1923 if (!tv_cmd
->tvc_prot_sgl
) {
1924 mutex_unlock(&tpg
->tv_tpg_mutex
);
1925 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1930 * Since we are running in 'demo mode' this call with generate a
1931 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1932 * the SCSI Initiator port name of the passed configfs group 'name'.
1934 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1935 se_tpg
, (unsigned char *)name
);
1936 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1937 mutex_unlock(&tpg
->tv_tpg_mutex
);
1938 pr_debug("core_tpg_check_initiator_node_acl() failed"
1943 * Now register the TCM vhost virtual I_T Nexus as active with the
1944 * call to __transport_register_session()
1946 __transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1947 tv_nexus
->tvn_se_sess
, tv_nexus
);
1948 tpg
->tpg_nexus
= tv_nexus
;
1950 mutex_unlock(&tpg
->tv_tpg_mutex
);
1954 tcm_vhost_free_cmd_map_res(tv_nexus
, se_sess
);
1955 transport_free_session(se_sess
);
1960 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg
*tpg
)
1962 struct se_session
*se_sess
;
1963 struct tcm_vhost_nexus
*tv_nexus
;
1965 mutex_lock(&tpg
->tv_tpg_mutex
);
1966 tv_nexus
= tpg
->tpg_nexus
;
1968 mutex_unlock(&tpg
->tv_tpg_mutex
);
1972 se_sess
= tv_nexus
->tvn_se_sess
;
1974 mutex_unlock(&tpg
->tv_tpg_mutex
);
1978 if (tpg
->tv_tpg_port_count
!= 0) {
1979 mutex_unlock(&tpg
->tv_tpg_mutex
);
1980 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1981 " active TPG port count: %d\n",
1982 tpg
->tv_tpg_port_count
);
1986 if (tpg
->tv_tpg_vhost_count
!= 0) {
1987 mutex_unlock(&tpg
->tv_tpg_mutex
);
1988 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1989 " active TPG vhost count: %d\n",
1990 tpg
->tv_tpg_vhost_count
);
1994 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1995 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg
->tport
),
1996 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1998 tcm_vhost_free_cmd_map_res(tv_nexus
, se_sess
);
2000 * Release the SCSI I_T Nexus to the emulated vhost Target Port
2002 transport_deregister_session(tv_nexus
->tvn_se_sess
);
2003 tpg
->tpg_nexus
= NULL
;
2004 mutex_unlock(&tpg
->tv_tpg_mutex
);
2010 static ssize_t
tcm_vhost_tpg_show_nexus(struct se_portal_group
*se_tpg
,
2013 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
2014 struct tcm_vhost_tpg
, se_tpg
);
2015 struct tcm_vhost_nexus
*tv_nexus
;
2018 mutex_lock(&tpg
->tv_tpg_mutex
);
2019 tv_nexus
= tpg
->tpg_nexus
;
2021 mutex_unlock(&tpg
->tv_tpg_mutex
);
2024 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
2025 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
2026 mutex_unlock(&tpg
->tv_tpg_mutex
);
2031 static ssize_t
tcm_vhost_tpg_store_nexus(struct se_portal_group
*se_tpg
,
2035 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
2036 struct tcm_vhost_tpg
, se_tpg
);
2037 struct tcm_vhost_tport
*tport_wwn
= tpg
->tport
;
2038 unsigned char i_port
[TCM_VHOST_NAMELEN
], *ptr
, *port_ptr
;
2041 * Shutdown the active I_T nexus if 'NULL' is passed..
2043 if (!strncmp(page
, "NULL", 4)) {
2044 ret
= tcm_vhost_drop_nexus(tpg
);
2045 return (!ret
) ? count
: ret
;
2048 * Otherwise make sure the passed virtual Initiator port WWN matches
2049 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
2050 * tcm_vhost_make_nexus().
2052 if (strlen(page
) >= TCM_VHOST_NAMELEN
) {
2053 pr_err("Emulated NAA Sas Address: %s, exceeds"
2054 " max: %d\n", page
, TCM_VHOST_NAMELEN
);
2057 snprintf(&i_port
[0], TCM_VHOST_NAMELEN
, "%s", page
);
2059 ptr
= strstr(i_port
, "naa.");
2061 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
2062 pr_err("Passed SAS Initiator Port %s does not"
2063 " match target port protoid: %s\n", i_port
,
2064 tcm_vhost_dump_proto_id(tport_wwn
));
2067 port_ptr
= &i_port
[0];
2070 ptr
= strstr(i_port
, "fc.");
2072 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
2073 pr_err("Passed FCP Initiator Port %s does not"
2074 " match target port protoid: %s\n", i_port
,
2075 tcm_vhost_dump_proto_id(tport_wwn
));
2078 port_ptr
= &i_port
[3]; /* Skip over "fc." */
2081 ptr
= strstr(i_port
, "iqn.");
2083 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
2084 pr_err("Passed iSCSI Initiator Port %s does not"
2085 " match target port protoid: %s\n", i_port
,
2086 tcm_vhost_dump_proto_id(tport_wwn
));
2089 port_ptr
= &i_port
[0];
2092 pr_err("Unable to locate prefix for emulated Initiator Port:"
2096 * Clear any trailing newline for the NAA WWN
2099 if (i_port
[strlen(i_port
)-1] == '\n')
2100 i_port
[strlen(i_port
)-1] = '\0';
2102 ret
= tcm_vhost_make_nexus(tpg
, port_ptr
);
2109 TF_TPG_BASE_ATTR(tcm_vhost
, nexus
, S_IRUGO
| S_IWUSR
);
2111 static struct configfs_attribute
*tcm_vhost_tpg_attrs
[] = {
2112 &tcm_vhost_tpg_nexus
.attr
,
2116 static struct se_portal_group
*
2117 tcm_vhost_make_tpg(struct se_wwn
*wwn
,
2118 struct config_group
*group
,
2121 struct tcm_vhost_tport
*tport
= container_of(wwn
,
2122 struct tcm_vhost_tport
, tport_wwn
);
2124 struct tcm_vhost_tpg
*tpg
;
2128 if (strstr(name
, "tpgt_") != name
)
2129 return ERR_PTR(-EINVAL
);
2130 if (kstrtou16(name
+ 5, 10, &tpgt
) || tpgt
>= VHOST_SCSI_MAX_TARGET
)
2131 return ERR_PTR(-EINVAL
);
2133 tpg
= kzalloc(sizeof(struct tcm_vhost_tpg
), GFP_KERNEL
);
2135 pr_err("Unable to allocate struct tcm_vhost_tpg");
2136 return ERR_PTR(-ENOMEM
);
2138 mutex_init(&tpg
->tv_tpg_mutex
);
2139 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
2141 tpg
->tport_tpgt
= tpgt
;
2143 ret
= core_tpg_register(&tcm_vhost_fabric_configfs
->tf_ops
, wwn
,
2144 &tpg
->se_tpg
, tpg
, TRANSPORT_TPG_TYPE_NORMAL
);
2149 mutex_lock(&tcm_vhost_mutex
);
2150 list_add_tail(&tpg
->tv_tpg_list
, &tcm_vhost_list
);
2151 mutex_unlock(&tcm_vhost_mutex
);
2153 return &tpg
->se_tpg
;
2156 static void tcm_vhost_drop_tpg(struct se_portal_group
*se_tpg
)
2158 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
2159 struct tcm_vhost_tpg
, se_tpg
);
2161 mutex_lock(&tcm_vhost_mutex
);
2162 list_del(&tpg
->tv_tpg_list
);
2163 mutex_unlock(&tcm_vhost_mutex
);
2165 * Release the virtual I_T Nexus for this vhost TPG
2167 tcm_vhost_drop_nexus(tpg
);
2169 * Deregister the se_tpg from TCM..
2171 core_tpg_deregister(se_tpg
);
2175 static struct se_wwn
*
2176 tcm_vhost_make_tport(struct target_fabric_configfs
*tf
,
2177 struct config_group
*group
,
2180 struct tcm_vhost_tport
*tport
;
2185 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
2186 return ERR_PTR(-EINVAL); */
2188 tport
= kzalloc(sizeof(struct tcm_vhost_tport
), GFP_KERNEL
);
2190 pr_err("Unable to allocate struct tcm_vhost_tport");
2191 return ERR_PTR(-ENOMEM
);
2193 tport
->tport_wwpn
= wwpn
;
2195 * Determine the emulated Protocol Identifier and Target Port Name
2196 * based on the incoming configfs directory name.
2198 ptr
= strstr(name
, "naa.");
2200 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
2203 ptr
= strstr(name
, "fc.");
2205 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
2206 off
= 3; /* Skip over "fc." */
2209 ptr
= strstr(name
, "iqn.");
2211 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
2215 pr_err("Unable to locate prefix for emulated Target Port:"
2218 return ERR_PTR(-EINVAL
);
2221 if (strlen(name
) >= TCM_VHOST_NAMELEN
) {
2222 pr_err("Emulated %s Address: %s, exceeds"
2223 " max: %d\n", name
, tcm_vhost_dump_proto_id(tport
),
2226 return ERR_PTR(-EINVAL
);
2228 snprintf(&tport
->tport_name
[0], TCM_VHOST_NAMELEN
, "%s", &name
[off
]);
2230 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
2231 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
), name
);
2233 return &tport
->tport_wwn
;
2236 static void tcm_vhost_drop_tport(struct se_wwn
*wwn
)
2238 struct tcm_vhost_tport
*tport
= container_of(wwn
,
2239 struct tcm_vhost_tport
, tport_wwn
);
2241 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2242 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
),
2249 tcm_vhost_wwn_show_attr_version(struct target_fabric_configfs
*tf
,
2252 return sprintf(page
, "TCM_VHOST fabric module %s on %s/%s"
2253 "on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
2254 utsname()->machine
);
2257 TF_WWN_ATTR_RO(tcm_vhost
, version
);
2259 static struct configfs_attribute
*tcm_vhost_wwn_attrs
[] = {
2260 &tcm_vhost_wwn_version
.attr
,
2264 static struct target_core_fabric_ops tcm_vhost_ops
= {
2265 .get_fabric_name
= tcm_vhost_get_fabric_name
,
2266 .get_fabric_proto_ident
= tcm_vhost_get_fabric_proto_ident
,
2267 .tpg_get_wwn
= tcm_vhost_get_fabric_wwn
,
2268 .tpg_get_tag
= tcm_vhost_get_tag
,
2269 .tpg_get_default_depth
= tcm_vhost_get_default_depth
,
2270 .tpg_get_pr_transport_id
= tcm_vhost_get_pr_transport_id
,
2271 .tpg_get_pr_transport_id_len
= tcm_vhost_get_pr_transport_id_len
,
2272 .tpg_parse_pr_out_transport_id
= tcm_vhost_parse_pr_out_transport_id
,
2273 .tpg_check_demo_mode
= tcm_vhost_check_true
,
2274 .tpg_check_demo_mode_cache
= tcm_vhost_check_true
,
2275 .tpg_check_demo_mode_write_protect
= tcm_vhost_check_false
,
2276 .tpg_check_prod_mode_write_protect
= tcm_vhost_check_false
,
2277 .tpg_alloc_fabric_acl
= tcm_vhost_alloc_fabric_acl
,
2278 .tpg_release_fabric_acl
= tcm_vhost_release_fabric_acl
,
2279 .tpg_get_inst_index
= tcm_vhost_tpg_get_inst_index
,
2280 .release_cmd
= tcm_vhost_release_cmd
,
2281 .check_stop_free
= vhost_scsi_check_stop_free
,
2282 .shutdown_session
= tcm_vhost_shutdown_session
,
2283 .close_session
= tcm_vhost_close_session
,
2284 .sess_get_index
= tcm_vhost_sess_get_index
,
2285 .sess_get_initiator_sid
= NULL
,
2286 .write_pending
= tcm_vhost_write_pending
,
2287 .write_pending_status
= tcm_vhost_write_pending_status
,
2288 .set_default_node_attributes
= tcm_vhost_set_default_node_attrs
,
2289 .get_task_tag
= tcm_vhost_get_task_tag
,
2290 .get_cmd_state
= tcm_vhost_get_cmd_state
,
2291 .queue_data_in
= tcm_vhost_queue_data_in
,
2292 .queue_status
= tcm_vhost_queue_status
,
2293 .queue_tm_rsp
= tcm_vhost_queue_tm_rsp
,
2294 .aborted_task
= tcm_vhost_aborted_task
,
2296 * Setup callers for generic logic in target_core_fabric_configfs.c
2298 .fabric_make_wwn
= tcm_vhost_make_tport
,
2299 .fabric_drop_wwn
= tcm_vhost_drop_tport
,
2300 .fabric_make_tpg
= tcm_vhost_make_tpg
,
2301 .fabric_drop_tpg
= tcm_vhost_drop_tpg
,
2302 .fabric_post_link
= tcm_vhost_port_link
,
2303 .fabric_pre_unlink
= tcm_vhost_port_unlink
,
2304 .fabric_make_np
= NULL
,
2305 .fabric_drop_np
= NULL
,
2306 .fabric_make_nodeacl
= tcm_vhost_make_nodeacl
,
2307 .fabric_drop_nodeacl
= tcm_vhost_drop_nodeacl
,
2310 static int tcm_vhost_register_configfs(void)
2312 struct target_fabric_configfs
*fabric
;
2315 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2316 " on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
2317 utsname()->machine
);
2319 * Register the top level struct config_item_type with TCM core
2321 fabric
= target_fabric_configfs_init(THIS_MODULE
, "vhost");
2322 if (IS_ERR(fabric
)) {
2323 pr_err("target_fabric_configfs_init() failed\n");
2324 return PTR_ERR(fabric
);
2327 * Setup fabric->tf_ops from our local tcm_vhost_ops
2329 fabric
->tf_ops
= tcm_vhost_ops
;
2331 * Setup default attribute lists for various fabric->tf_cit_tmpl
2333 fabric
->tf_cit_tmpl
.tfc_wwn_cit
.ct_attrs
= tcm_vhost_wwn_attrs
;
2334 fabric
->tf_cit_tmpl
.tfc_tpg_base_cit
.ct_attrs
= tcm_vhost_tpg_attrs
;
2335 fabric
->tf_cit_tmpl
.tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
2336 fabric
->tf_cit_tmpl
.tfc_tpg_param_cit
.ct_attrs
= NULL
;
2337 fabric
->tf_cit_tmpl
.tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
2338 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_base_cit
.ct_attrs
= NULL
;
2339 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
2340 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
2341 fabric
->tf_cit_tmpl
.tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
2343 * Register the fabric for use within TCM
2345 ret
= target_fabric_configfs_register(fabric
);
2347 pr_err("target_fabric_configfs_register() failed"
2348 " for TCM_VHOST\n");
2352 * Setup our local pointer to *fabric
2354 tcm_vhost_fabric_configfs
= fabric
;
2355 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
2359 static void tcm_vhost_deregister_configfs(void)
2361 if (!tcm_vhost_fabric_configfs
)
2364 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs
);
2365 tcm_vhost_fabric_configfs
= NULL
;
2366 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
2369 static int __init
tcm_vhost_init(void)
2373 * Use our own dedicated workqueue for submitting I/O into
2374 * target core to avoid contention within system_wq.
2376 tcm_vhost_workqueue
= alloc_workqueue("tcm_vhost", 0, 0);
2377 if (!tcm_vhost_workqueue
)
2380 ret
= vhost_scsi_register();
2382 goto out_destroy_workqueue
;
2384 ret
= tcm_vhost_register_configfs();
2386 goto out_vhost_scsi_deregister
;
2390 out_vhost_scsi_deregister
:
2391 vhost_scsi_deregister();
2392 out_destroy_workqueue
:
2393 destroy_workqueue(tcm_vhost_workqueue
);
2398 static void tcm_vhost_exit(void)
2400 tcm_vhost_deregister_configfs();
2401 vhost_scsi_deregister();
2402 destroy_workqueue(tcm_vhost_workqueue
);
2405 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2406 MODULE_ALIAS("tcm_vhost");
2407 MODULE_LICENSE("GPL");
2408 module_init(tcm_vhost_init
);
2409 module_exit(tcm_vhost_exit
);