1 /*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
4 * (C) Copyright 2010-2012 RisingTide Systems LLC.
5 * (C) Copyright 2010-2012 IBM Corp.
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
9 * Authors: Nicholas A. Bellinger <nab@risingtidesystems.com>
10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 ****************************************************************************/
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <generated/utsrelease.h>
27 #include <linux/utsname.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/kthread.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/configfs.h>
34 #include <linux/ctype.h>
35 #include <linux/compat.h>
36 #include <linux/eventfd.h>
37 #include <linux/vhost.h>
39 #include <linux/miscdevice.h>
40 #include <asm/unaligned.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_tcq.h>
43 #include <target/target_core_base.h>
44 #include <target/target_core_fabric.h>
45 #include <target/target_core_fabric_configfs.h>
46 #include <target/target_core_configfs.h>
47 #include <target/configfs_macros.h>
48 #include <linux/vhost.h>
49 #include <linux/virtio_net.h> /* TODO vhost.h currently depends on this */
50 #include <linux/virtio_scsi.h>
54 #include "tcm_vhost.h"
57 VHOST_SCSI_VQ_CTL
= 0,
58 VHOST_SCSI_VQ_EVT
= 1,
63 struct tcm_vhost_tpg
*vs_tpg
; /* Protected by vhost_scsi->dev.mutex */
65 struct vhost_virtqueue vqs
[3];
67 struct vhost_work vs_completion_work
; /* cmd completion work item */
68 struct list_head vs_completion_list
; /* cmd completion queue */
69 spinlock_t vs_completion_lock
; /* protects s_completion_list */
72 /* Local pointer to allocated TCM configfs fabric module */
73 static struct target_fabric_configfs
*tcm_vhost_fabric_configfs
;
75 static struct workqueue_struct
*tcm_vhost_workqueue
;
77 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
78 static DEFINE_MUTEX(tcm_vhost_mutex
);
79 static LIST_HEAD(tcm_vhost_list
);
81 static int tcm_vhost_check_true(struct se_portal_group
*se_tpg
)
86 static int tcm_vhost_check_false(struct se_portal_group
*se_tpg
)
91 static char *tcm_vhost_get_fabric_name(void)
96 static u8
tcm_vhost_get_fabric_proto_ident(struct se_portal_group
*se_tpg
)
98 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
99 struct tcm_vhost_tpg
, se_tpg
);
100 struct tcm_vhost_tport
*tport
= tpg
->tport
;
102 switch (tport
->tport_proto_id
) {
103 case SCSI_PROTOCOL_SAS
:
104 return sas_get_fabric_proto_ident(se_tpg
);
105 case SCSI_PROTOCOL_FCP
:
106 return fc_get_fabric_proto_ident(se_tpg
);
107 case SCSI_PROTOCOL_ISCSI
:
108 return iscsi_get_fabric_proto_ident(se_tpg
);
110 pr_err("Unknown tport_proto_id: 0x%02x, using"
111 " SAS emulation\n", tport
->tport_proto_id
);
115 return sas_get_fabric_proto_ident(se_tpg
);
118 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group
*se_tpg
)
120 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
121 struct tcm_vhost_tpg
, se_tpg
);
122 struct tcm_vhost_tport
*tport
= tpg
->tport
;
124 return &tport
->tport_name
[0];
127 static u16
tcm_vhost_get_tag(struct se_portal_group
*se_tpg
)
129 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
130 struct tcm_vhost_tpg
, se_tpg
);
131 return tpg
->tport_tpgt
;
134 static u32
tcm_vhost_get_default_depth(struct se_portal_group
*se_tpg
)
139 static u32
tcm_vhost_get_pr_transport_id(struct se_portal_group
*se_tpg
,
140 struct se_node_acl
*se_nacl
,
141 struct t10_pr_registration
*pr_reg
,
145 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
146 struct tcm_vhost_tpg
, se_tpg
);
147 struct tcm_vhost_tport
*tport
= tpg
->tport
;
149 switch (tport
->tport_proto_id
) {
150 case SCSI_PROTOCOL_SAS
:
151 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
153 case SCSI_PROTOCOL_FCP
:
154 return fc_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
156 case SCSI_PROTOCOL_ISCSI
:
157 return iscsi_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
160 pr_err("Unknown tport_proto_id: 0x%02x, using"
161 " SAS emulation\n", tport
->tport_proto_id
);
165 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
169 static u32
tcm_vhost_get_pr_transport_id_len(struct se_portal_group
*se_tpg
,
170 struct se_node_acl
*se_nacl
,
171 struct t10_pr_registration
*pr_reg
,
174 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
175 struct tcm_vhost_tpg
, se_tpg
);
176 struct tcm_vhost_tport
*tport
= tpg
->tport
;
178 switch (tport
->tport_proto_id
) {
179 case SCSI_PROTOCOL_SAS
:
180 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
182 case SCSI_PROTOCOL_FCP
:
183 return fc_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
185 case SCSI_PROTOCOL_ISCSI
:
186 return iscsi_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
189 pr_err("Unknown tport_proto_id: 0x%02x, using"
190 " SAS emulation\n", tport
->tport_proto_id
);
194 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
198 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group
*se_tpg
,
201 char **port_nexus_ptr
)
203 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
204 struct tcm_vhost_tpg
, se_tpg
);
205 struct tcm_vhost_tport
*tport
= tpg
->tport
;
207 switch (tport
->tport_proto_id
) {
208 case SCSI_PROTOCOL_SAS
:
209 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
211 case SCSI_PROTOCOL_FCP
:
212 return fc_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
214 case SCSI_PROTOCOL_ISCSI
:
215 return iscsi_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
218 pr_err("Unknown tport_proto_id: 0x%02x, using"
219 " SAS emulation\n", tport
->tport_proto_id
);
223 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
227 static struct se_node_acl
*tcm_vhost_alloc_fabric_acl(
228 struct se_portal_group
*se_tpg
)
230 struct tcm_vhost_nacl
*nacl
;
232 nacl
= kzalloc(sizeof(struct tcm_vhost_nacl
), GFP_KERNEL
);
234 pr_err("Unable to alocate struct tcm_vhost_nacl\n");
238 return &nacl
->se_node_acl
;
241 static void tcm_vhost_release_fabric_acl(struct se_portal_group
*se_tpg
,
242 struct se_node_acl
*se_nacl
)
244 struct tcm_vhost_nacl
*nacl
= container_of(se_nacl
,
245 struct tcm_vhost_nacl
, se_node_acl
);
249 static u32
tcm_vhost_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
254 static void tcm_vhost_release_cmd(struct se_cmd
*se_cmd
)
259 static int tcm_vhost_shutdown_session(struct se_session
*se_sess
)
264 static void tcm_vhost_close_session(struct se_session
*se_sess
)
269 static u32
tcm_vhost_sess_get_index(struct se_session
*se_sess
)
274 static int tcm_vhost_write_pending(struct se_cmd
*se_cmd
)
276 /* Go ahead and process the write immediately */
277 target_execute_cmd(se_cmd
);
281 static int tcm_vhost_write_pending_status(struct se_cmd
*se_cmd
)
286 static void tcm_vhost_set_default_node_attrs(struct se_node_acl
*nacl
)
291 static u32
tcm_vhost_get_task_tag(struct se_cmd
*se_cmd
)
296 static int tcm_vhost_get_cmd_state(struct se_cmd
*se_cmd
)
301 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd
*tv_cmd
)
303 struct vhost_scsi
*vs
= tv_cmd
->tvc_vhost
;
305 spin_lock_bh(&vs
->vs_completion_lock
);
306 list_add_tail(&tv_cmd
->tvc_completion_list
, &vs
->vs_completion_list
);
307 spin_unlock_bh(&vs
->vs_completion_lock
);
309 vhost_work_queue(&vs
->dev
, &vs
->vs_completion_work
);
312 static int tcm_vhost_queue_data_in(struct se_cmd
*se_cmd
)
314 struct tcm_vhost_cmd
*tv_cmd
= container_of(se_cmd
,
315 struct tcm_vhost_cmd
, tvc_se_cmd
);
316 vhost_scsi_complete_cmd(tv_cmd
);
320 static int tcm_vhost_queue_status(struct se_cmd
*se_cmd
)
322 struct tcm_vhost_cmd
*tv_cmd
= container_of(se_cmd
,
323 struct tcm_vhost_cmd
, tvc_se_cmd
);
324 vhost_scsi_complete_cmd(tv_cmd
);
328 static int tcm_vhost_queue_tm_rsp(struct se_cmd
*se_cmd
)
333 static u16
tcm_vhost_set_fabric_sense_len(struct se_cmd
*se_cmd
,
339 static u16
tcm_vhost_get_fabric_sense_len(void)
344 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd
*tv_cmd
)
346 struct se_cmd
*se_cmd
= &tv_cmd
->tvc_se_cmd
;
348 /* TODO locking against target/backend threads? */
349 transport_generic_free_cmd(se_cmd
, 1);
351 if (tv_cmd
->tvc_sgl_count
) {
353 for (i
= 0; i
< tv_cmd
->tvc_sgl_count
; i
++)
354 put_page(sg_page(&tv_cmd
->tvc_sgl
[i
]));
356 kfree(tv_cmd
->tvc_sgl
);
362 /* Dequeue a command from the completion list */
363 static struct tcm_vhost_cmd
*vhost_scsi_get_cmd_from_completion(
364 struct vhost_scsi
*vs
)
366 struct tcm_vhost_cmd
*tv_cmd
= NULL
;
368 spin_lock_bh(&vs
->vs_completion_lock
);
369 if (list_empty(&vs
->vs_completion_list
)) {
370 spin_unlock_bh(&vs
->vs_completion_lock
);
374 list_for_each_entry(tv_cmd
, &vs
->vs_completion_list
,
375 tvc_completion_list
) {
376 list_del(&tv_cmd
->tvc_completion_list
);
379 spin_unlock_bh(&vs
->vs_completion_lock
);
383 /* Fill in status and signal that we are done processing this command
385 * This is scheduled in the vhost work queue so we are called with the owner
386 * process mm and can access the vring.
388 static void vhost_scsi_complete_cmd_work(struct vhost_work
*work
)
390 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
392 struct tcm_vhost_cmd
*tv_cmd
;
394 while ((tv_cmd
= vhost_scsi_get_cmd_from_completion(vs
))) {
395 struct virtio_scsi_cmd_resp v_rsp
;
396 struct se_cmd
*se_cmd
= &tv_cmd
->tvc_se_cmd
;
399 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__
,
400 tv_cmd
, se_cmd
->residual_count
, se_cmd
->scsi_status
);
402 memset(&v_rsp
, 0, sizeof(v_rsp
));
403 v_rsp
.resid
= se_cmd
->residual_count
;
404 /* TODO is status_qualifier field needed? */
405 v_rsp
.status
= se_cmd
->scsi_status
;
406 v_rsp
.sense_len
= se_cmd
->scsi_sense_length
;
407 memcpy(v_rsp
.sense
, tv_cmd
->tvc_sense_buf
,
409 ret
= copy_to_user(tv_cmd
->tvc_resp
, &v_rsp
, sizeof(v_rsp
));
410 if (likely(ret
== 0))
411 vhost_add_used(&vs
->vqs
[2], tv_cmd
->tvc_vq_desc
, 0);
413 pr_err("Faulted on virtio_scsi_cmd_resp\n");
415 vhost_scsi_free_cmd(tv_cmd
);
418 vhost_signal(&vs
->dev
, &vs
->vqs
[2]);
421 static struct tcm_vhost_cmd
*vhost_scsi_allocate_cmd(
422 struct tcm_vhost_tpg
*tv_tpg
,
423 struct virtio_scsi_cmd_req
*v_req
,
427 struct tcm_vhost_cmd
*tv_cmd
;
428 struct tcm_vhost_nexus
*tv_nexus
;
429 struct se_portal_group
*se_tpg
= &tv_tpg
->se_tpg
;
430 struct se_session
*se_sess
;
431 struct se_cmd
*se_cmd
;
434 tv_nexus
= tv_tpg
->tpg_nexus
;
436 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
437 return ERR_PTR(-EIO
);
439 se_sess
= tv_nexus
->tvn_se_sess
;
441 tv_cmd
= kzalloc(sizeof(struct tcm_vhost_cmd
), GFP_ATOMIC
);
443 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
444 return ERR_PTR(-ENOMEM
);
446 INIT_LIST_HEAD(&tv_cmd
->tvc_completion_list
);
447 tv_cmd
->tvc_tag
= v_req
->tag
;
449 se_cmd
= &tv_cmd
->tvc_se_cmd
;
451 * Locate the SAM Task Attr from virtio_scsi_cmd_req
453 sam_task_attr
= v_req
->task_attr
;
455 * Initialize struct se_cmd descriptor from TCM infrastructure
457 transport_init_se_cmd(se_cmd
, se_tpg
->se_tpg_tfo
, se_sess
, exp_data_len
,
458 data_direction
, sam_task_attr
,
459 &tv_cmd
->tvc_sense_buf
[0]);
461 #if 0 /* FIXME: vhost_scsi_allocate_cmd() BIDI operation */
463 se_cmd
->se_cmd_flags
|= SCF_BIDI
;
469 * Map a user memory range into a scatterlist
471 * Returns the number of scatterlist entries used or -errno on error.
473 static int vhost_scsi_map_to_sgl(struct scatterlist
*sgl
,
474 unsigned int sgl_count
, void __user
*ptr
, size_t len
, int write
)
476 struct scatterlist
*sg
= sgl
;
477 unsigned int npages
= 0;
482 unsigned int offset
= (uintptr_t)ptr
& ~PAGE_MASK
;
483 unsigned int nbytes
= min_t(unsigned int,
484 PAGE_SIZE
- offset
, len
);
486 if (npages
== sgl_count
) {
491 ret
= get_user_pages_fast((unsigned long)ptr
, 1, write
, &page
);
492 BUG_ON(ret
== 0); /* we should either get our page or fail */
496 sg_set_page(sg
, page
, nbytes
, offset
);
505 /* Put pages that we hold */
506 for (sg
= sgl
; sg
!= &sgl
[npages
]; sg
++)
507 put_page(sg_page(sg
));
511 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd
*tv_cmd
,
512 struct iovec
*iov
, unsigned int niov
, int write
)
517 struct scatterlist
*sg
;
520 * Find out how long sglist needs to be
523 for (i
= 0; i
< niov
; i
++) {
524 sgl_count
+= (((uintptr_t)iov
[i
].iov_base
+ iov
[i
].iov_len
+
525 PAGE_SIZE
- 1) >> PAGE_SHIFT
) -
526 ((uintptr_t)iov
[i
].iov_base
>> PAGE_SHIFT
);
528 /* TODO overflow checking */
530 sg
= kmalloc(sizeof(tv_cmd
->tvc_sgl
[0]) * sgl_count
, GFP_ATOMIC
);
533 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__
,
535 sg_init_table(sg
, sgl_count
);
537 tv_cmd
->tvc_sgl
= sg
;
538 tv_cmd
->tvc_sgl_count
= sgl_count
;
540 pr_debug("Mapping %u iovecs for %u pages\n", niov
, sgl_count
);
541 for (i
= 0; i
< niov
; i
++) {
542 ret
= vhost_scsi_map_to_sgl(sg
, sgl_count
, iov
[i
].iov_base
,
543 iov
[i
].iov_len
, write
);
545 for (i
= 0; i
< tv_cmd
->tvc_sgl_count
; i
++)
546 put_page(sg_page(&tv_cmd
->tvc_sgl
[i
]));
547 kfree(tv_cmd
->tvc_sgl
);
548 tv_cmd
->tvc_sgl
= NULL
;
549 tv_cmd
->tvc_sgl_count
= 0;
559 static void tcm_vhost_submission_work(struct work_struct
*work
)
561 struct tcm_vhost_cmd
*tv_cmd
=
562 container_of(work
, struct tcm_vhost_cmd
, work
);
563 struct se_cmd
*se_cmd
= &tv_cmd
->tvc_se_cmd
;
564 struct scatterlist
*sg_ptr
, *sg_bidi_ptr
= NULL
;
565 int rc
, sg_no_bidi
= 0;
567 * Locate the struct se_lun pointer based on v_req->lun, and
568 * attach it to struct se_cmd
570 rc
= transport_lookup_cmd_lun(&tv_cmd
->tvc_se_cmd
, tv_cmd
->tvc_lun
);
572 pr_err("Failed to look up lun: %d\n", tv_cmd
->tvc_lun
);
573 transport_send_check_condition_and_sense(&tv_cmd
->tvc_se_cmd
,
574 tv_cmd
->tvc_se_cmd
.scsi_sense_reason
, 0);
575 transport_generic_free_cmd(se_cmd
, 0);
579 rc
= target_setup_cmd_from_cdb(se_cmd
, tv_cmd
->tvc_cdb
);
581 transport_send_check_condition_and_sense(se_cmd
,
582 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
583 transport_generic_free_cmd(se_cmd
, 0);
586 if (se_cmd
->se_cmd_flags
& SCF_SCSI_RESERVATION_CONFLICT
)
587 tcm_vhost_queue_status(se_cmd
);
589 transport_send_check_condition_and_sense(se_cmd
,
590 se_cmd
->scsi_sense_reason
, 0);
591 transport_generic_free_cmd(se_cmd
, 0);
595 if (tv_cmd
->tvc_sgl_count
) {
596 sg_ptr
= tv_cmd
->tvc_sgl
;
598 * For BIDI commands, pass in the extra READ buffer
599 * to transport_generic_map_mem_to_cmd() below..
601 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
603 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
612 rc
= transport_generic_map_mem_to_cmd(se_cmd
, sg_ptr
,
613 tv_cmd
->tvc_sgl_count
, sg_bidi_ptr
,
616 transport_send_check_condition_and_sense(se_cmd
,
617 se_cmd
->scsi_sense_reason
, 0);
618 transport_generic_free_cmd(se_cmd
, 0);
621 transport_handle_cdb_direct(se_cmd
);
624 static void vhost_scsi_handle_vq(struct vhost_scsi
*vs
)
626 struct vhost_virtqueue
*vq
= &vs
->vqs
[2];
627 struct virtio_scsi_cmd_req v_req
;
628 struct tcm_vhost_tpg
*tv_tpg
;
629 struct tcm_vhost_cmd
*tv_cmd
;
630 u32 exp_data_len
, data_first
, data_num
, data_direction
;
634 /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
636 if (unlikely(!tv_tpg
)) {
637 pr_err("%s endpoint not set\n", __func__
);
641 mutex_lock(&vq
->mutex
);
642 vhost_disable_notify(&vs
->dev
, vq
);
645 head
= vhost_get_vq_desc(&vs
->dev
, vq
, vq
->iov
,
646 ARRAY_SIZE(vq
->iov
), &out
, &in
,
648 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
650 /* On error, stop handling until the next kick. */
651 if (unlikely(head
< 0))
653 /* Nothing new? Wait for eventfd to tell us they refilled. */
654 if (head
== vq
->num
) {
655 if (unlikely(vhost_enable_notify(&vs
->dev
, vq
))) {
656 vhost_disable_notify(&vs
->dev
, vq
);
662 /* FIXME: BIDI operation */
663 if (out
== 1 && in
== 1) {
664 data_direction
= DMA_NONE
;
667 } else if (out
== 1 && in
> 1) {
668 data_direction
= DMA_FROM_DEVICE
;
669 data_first
= out
+ 1;
671 } else if (out
> 1 && in
== 1) {
672 data_direction
= DMA_TO_DEVICE
;
676 vq_err(vq
, "Invalid buffer layout out: %u in: %u\n",
682 * Check for a sane resp buffer so we can report errors to
685 if (unlikely(vq
->iov
[out
].iov_len
!=
686 sizeof(struct virtio_scsi_cmd_resp
))) {
687 vq_err(vq
, "Expecting virtio_scsi_cmd_resp, got %zu"
688 " bytes\n", vq
->iov
[out
].iov_len
);
692 if (unlikely(vq
->iov
[0].iov_len
!= sizeof(v_req
))) {
693 vq_err(vq
, "Expecting virtio_scsi_cmd_req, got %zu"
694 " bytes\n", vq
->iov
[0].iov_len
);
697 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
698 " len: %zu\n", vq
->iov
[0].iov_base
, sizeof(v_req
));
699 ret
= __copy_from_user(&v_req
, vq
->iov
[0].iov_base
,
702 vq_err(vq
, "Faulted on virtio_scsi_cmd_req\n");
707 for (i
= 0; i
< data_num
; i
++)
708 exp_data_len
+= vq
->iov
[data_first
+ i
].iov_len
;
710 tv_cmd
= vhost_scsi_allocate_cmd(tv_tpg
, &v_req
,
711 exp_data_len
, data_direction
);
712 if (IS_ERR(tv_cmd
)) {
713 vq_err(vq
, "vhost_scsi_allocate_cmd failed %ld\n",
717 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
718 ": %d\n", tv_cmd
, exp_data_len
, data_direction
);
720 tv_cmd
->tvc_vhost
= vs
;
722 if (unlikely(vq
->iov
[out
].iov_len
!=
723 sizeof(struct virtio_scsi_cmd_resp
))) {
724 vq_err(vq
, "Expecting virtio_scsi_cmd_resp, got %zu"
725 " bytes, out: %d, in: %d\n",
726 vq
->iov
[out
].iov_len
, out
, in
);
730 tv_cmd
->tvc_resp
= vq
->iov
[out
].iov_base
;
733 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
734 * that will be used by tcm_vhost_new_cmd_map() and down into
735 * target_setup_cmd_from_cdb()
737 memcpy(tv_cmd
->tvc_cdb
, v_req
.cdb
, TCM_VHOST_MAX_CDB_SIZE
);
739 * Check that the recieved CDB size does not exceeded our
740 * hardcoded max for tcm_vhost
742 /* TODO what if cdb was too small for varlen cdb header? */
743 if (unlikely(scsi_command_size(tv_cmd
->tvc_cdb
) >
744 TCM_VHOST_MAX_CDB_SIZE
)) {
745 vq_err(vq
, "Received SCSI CDB with command_size: %d that"
746 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
747 scsi_command_size(tv_cmd
->tvc_cdb
),
748 TCM_VHOST_MAX_CDB_SIZE
);
751 tv_cmd
->tvc_lun
= ((v_req
.lun
[2] << 8) | v_req
.lun
[3]) & 0x3FFF;
753 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
754 tv_cmd
->tvc_cdb
[0], tv_cmd
->tvc_lun
);
756 if (data_direction
!= DMA_NONE
) {
757 ret
= vhost_scsi_map_iov_to_sgl(tv_cmd
,
758 &vq
->iov
[data_first
], data_num
,
759 data_direction
== DMA_TO_DEVICE
);
761 vq_err(vq
, "Failed to map iov to sgl\n");
767 * Save the descriptor from vhost_get_vq_desc() to be used to
768 * complete the virtio-scsi request in TCM callback context via
769 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
771 tv_cmd
->tvc_vq_desc
= head
;
773 * Dispatch tv_cmd descriptor for cmwq execution in process
774 * context provided by tcm_vhost_workqueue. This also ensures
775 * tv_cmd is executed on the same kworker CPU as this vhost
776 * thread to gain positive L2 cache locality effects..
778 INIT_WORK(&tv_cmd
->work
, tcm_vhost_submission_work
);
779 queue_work(tcm_vhost_workqueue
, &tv_cmd
->work
);
782 mutex_unlock(&vq
->mutex
);
785 static void vhost_scsi_ctl_handle_kick(struct vhost_work
*work
)
787 pr_debug("%s: The handling func for control queue.\n", __func__
);
790 static void vhost_scsi_evt_handle_kick(struct vhost_work
*work
)
792 pr_debug("%s: The handling func for event queue.\n", __func__
);
795 static void vhost_scsi_handle_kick(struct vhost_work
*work
)
797 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
799 struct vhost_scsi
*vs
= container_of(vq
->dev
, struct vhost_scsi
, dev
);
801 vhost_scsi_handle_vq(vs
);
805 * Called from vhost_scsi_ioctl() context to walk the list of available
806 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
808 static int vhost_scsi_set_endpoint(
809 struct vhost_scsi
*vs
,
810 struct vhost_scsi_target
*t
)
812 struct tcm_vhost_tport
*tv_tport
;
813 struct tcm_vhost_tpg
*tv_tpg
;
816 mutex_lock(&vs
->dev
.mutex
);
817 /* Verify that ring has been setup correctly. */
818 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
819 /* Verify that ring has been setup correctly. */
820 if (!vhost_vq_access_ok(&vs
->vqs
[index
])) {
821 mutex_unlock(&vs
->dev
.mutex
);
825 mutex_unlock(&vs
->dev
.mutex
);
827 mutex_lock(&tcm_vhost_mutex
);
828 list_for_each_entry(tv_tpg
, &tcm_vhost_list
, tv_tpg_list
) {
829 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
830 if (!tv_tpg
->tpg_nexus
) {
831 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
834 if (tv_tpg
->tv_tpg_vhost_count
!= 0) {
835 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
838 tv_tport
= tv_tpg
->tport
;
840 if (!strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
) &&
841 (tv_tpg
->tport_tpgt
== t
->vhost_tpgt
)) {
842 tv_tpg
->tv_tpg_vhost_count
++;
843 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
844 mutex_unlock(&tcm_vhost_mutex
);
846 mutex_lock(&vs
->dev
.mutex
);
848 mutex_unlock(&vs
->dev
.mutex
);
849 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
850 tv_tpg
->tv_tpg_vhost_count
--;
851 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
856 smp_mb__after_atomic_inc();
857 mutex_unlock(&vs
->dev
.mutex
);
860 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
862 mutex_unlock(&tcm_vhost_mutex
);
866 static int vhost_scsi_clear_endpoint(
867 struct vhost_scsi
*vs
,
868 struct vhost_scsi_target
*t
)
870 struct tcm_vhost_tport
*tv_tport
;
871 struct tcm_vhost_tpg
*tv_tpg
;
874 mutex_lock(&vs
->dev
.mutex
);
875 /* Verify that ring has been setup correctly. */
876 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
877 if (!vhost_vq_access_ok(&vs
->vqs
[index
])) {
888 tv_tport
= tv_tpg
->tport
;
890 if (strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
) ||
891 (tv_tpg
->tport_tpgt
!= t
->vhost_tpgt
)) {
892 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
893 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
894 tv_tport
->tport_name
, tv_tpg
->tport_tpgt
,
895 t
->vhost_wwpn
, t
->vhost_tpgt
);
899 tv_tpg
->tv_tpg_vhost_count
--;
901 mutex_unlock(&vs
->dev
.mutex
);
906 mutex_unlock(&vs
->dev
.mutex
);
910 static int vhost_scsi_open(struct inode
*inode
, struct file
*f
)
912 struct vhost_scsi
*s
;
915 s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
919 vhost_work_init(&s
->vs_completion_work
, vhost_scsi_complete_cmd_work
);
920 INIT_LIST_HEAD(&s
->vs_completion_list
);
921 spin_lock_init(&s
->vs_completion_lock
);
923 s
->vqs
[VHOST_SCSI_VQ_CTL
].handle_kick
= vhost_scsi_ctl_handle_kick
;
924 s
->vqs
[VHOST_SCSI_VQ_EVT
].handle_kick
= vhost_scsi_evt_handle_kick
;
925 s
->vqs
[VHOST_SCSI_VQ_IO
].handle_kick
= vhost_scsi_handle_kick
;
926 r
= vhost_dev_init(&s
->dev
, s
->vqs
, 3);
936 static int vhost_scsi_release(struct inode
*inode
, struct file
*f
)
938 struct vhost_scsi
*s
= f
->private_data
;
940 if (s
->vs_tpg
&& s
->vs_tpg
->tport
) {
941 struct vhost_scsi_target backend
;
943 memcpy(backend
.vhost_wwpn
, s
->vs_tpg
->tport
->tport_name
,
944 sizeof(backend
.vhost_wwpn
));
945 backend
.vhost_tpgt
= s
->vs_tpg
->tport_tpgt
;
946 vhost_scsi_clear_endpoint(s
, &backend
);
949 vhost_dev_cleanup(&s
->dev
, false);
954 static void vhost_scsi_flush_vq(struct vhost_scsi
*vs
, int index
)
956 vhost_poll_flush(&vs
->dev
.vqs
[index
].poll
);
959 static void vhost_scsi_flush(struct vhost_scsi
*vs
)
961 vhost_scsi_flush_vq(vs
, VHOST_SCSI_VQ_CTL
);
962 vhost_scsi_flush_vq(vs
, VHOST_SCSI_VQ_EVT
);
963 vhost_scsi_flush_vq(vs
, VHOST_SCSI_VQ_IO
);
966 static int vhost_scsi_set_features(struct vhost_scsi
*vs
, u64 features
)
968 if (features
& ~VHOST_FEATURES
)
971 mutex_lock(&vs
->dev
.mutex
);
972 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
973 !vhost_log_access_ok(&vs
->dev
)) {
974 mutex_unlock(&vs
->dev
.mutex
);
977 vs
->dev
.acked_features
= features
;
979 vhost_scsi_flush(vs
);
980 mutex_unlock(&vs
->dev
.mutex
);
984 static long vhost_scsi_ioctl(struct file
*f
, unsigned int ioctl
,
987 struct vhost_scsi
*vs
= f
->private_data
;
988 struct vhost_scsi_target backend
;
989 void __user
*argp
= (void __user
*)arg
;
990 u64 __user
*featurep
= argp
;
992 int r
, abi_version
= VHOST_SCSI_ABI_VERSION
;
995 case VHOST_SCSI_SET_ENDPOINT
:
996 if (copy_from_user(&backend
, argp
, sizeof backend
))
998 if (backend
.reserved
!= 0)
1001 return vhost_scsi_set_endpoint(vs
, &backend
);
1002 case VHOST_SCSI_CLEAR_ENDPOINT
:
1003 if (copy_from_user(&backend
, argp
, sizeof backend
))
1005 if (backend
.reserved
!= 0)
1008 return vhost_scsi_clear_endpoint(vs
, &backend
);
1009 case VHOST_SCSI_GET_ABI_VERSION
:
1010 if (copy_to_user(argp
, &abi_version
, sizeof abi_version
))
1013 case VHOST_GET_FEATURES
:
1014 features
= VHOST_FEATURES
;
1015 if (copy_to_user(featurep
, &features
, sizeof features
))
1018 case VHOST_SET_FEATURES
:
1019 if (copy_from_user(&features
, featurep
, sizeof features
))
1021 return vhost_scsi_set_features(vs
, features
);
1023 mutex_lock(&vs
->dev
.mutex
);
1024 r
= vhost_dev_ioctl(&vs
->dev
, ioctl
, arg
);
1025 mutex_unlock(&vs
->dev
.mutex
);
1030 #ifdef CONFIG_COMPAT
1031 static long vhost_scsi_compat_ioctl(struct file
*f
, unsigned int ioctl
,
1034 return vhost_scsi_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
1038 static const struct file_operations vhost_scsi_fops
= {
1039 .owner
= THIS_MODULE
,
1040 .release
= vhost_scsi_release
,
1041 .unlocked_ioctl
= vhost_scsi_ioctl
,
1042 #ifdef CONFIG_COMPAT
1043 .compat_ioctl
= vhost_scsi_compat_ioctl
,
1045 .open
= vhost_scsi_open
,
1046 .llseek
= noop_llseek
,
1049 static struct miscdevice vhost_scsi_misc
= {
1055 static int __init
vhost_scsi_register(void)
1057 return misc_register(&vhost_scsi_misc
);
1060 static int vhost_scsi_deregister(void)
1062 return misc_deregister(&vhost_scsi_misc
);
1065 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport
*tport
)
1067 switch (tport
->tport_proto_id
) {
1068 case SCSI_PROTOCOL_SAS
:
1070 case SCSI_PROTOCOL_FCP
:
1072 case SCSI_PROTOCOL_ISCSI
:
1081 static int tcm_vhost_port_link(struct se_portal_group
*se_tpg
,
1084 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1085 struct tcm_vhost_tpg
, se_tpg
);
1087 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1088 tv_tpg
->tv_tpg_port_count
++;
1089 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1094 static void tcm_vhost_port_unlink(struct se_portal_group
*se_tpg
,
1095 struct se_lun
*se_lun
)
1097 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1098 struct tcm_vhost_tpg
, se_tpg
);
1100 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1101 tv_tpg
->tv_tpg_port_count
--;
1102 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1105 static struct se_node_acl
*tcm_vhost_make_nodeacl(
1106 struct se_portal_group
*se_tpg
,
1107 struct config_group
*group
,
1110 struct se_node_acl
*se_nacl
, *se_nacl_new
;
1111 struct tcm_vhost_nacl
*nacl
;
1115 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1116 return ERR_PTR(-EINVAL); */
1117 se_nacl_new
= tcm_vhost_alloc_fabric_acl(se_tpg
);
1119 return ERR_PTR(-ENOMEM
);
1123 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1124 * when converting a NodeACL from demo mode -> explict
1126 se_nacl
= core_tpg_add_initiator_node_acl(se_tpg
, se_nacl_new
,
1128 if (IS_ERR(se_nacl
)) {
1129 tcm_vhost_release_fabric_acl(se_tpg
, se_nacl_new
);
1133 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1135 nacl
= container_of(se_nacl
, struct tcm_vhost_nacl
, se_node_acl
);
1136 nacl
->iport_wwpn
= wwpn
;
1141 static void tcm_vhost_drop_nodeacl(struct se_node_acl
*se_acl
)
1143 struct tcm_vhost_nacl
*nacl
= container_of(se_acl
,
1144 struct tcm_vhost_nacl
, se_node_acl
);
1145 core_tpg_del_initiator_node_acl(se_acl
->se_tpg
, se_acl
, 1);
1149 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg
*tv_tpg
,
1152 struct se_portal_group
*se_tpg
;
1153 struct tcm_vhost_nexus
*tv_nexus
;
1155 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1156 if (tv_tpg
->tpg_nexus
) {
1157 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1158 pr_debug("tv_tpg->tpg_nexus already exists\n");
1161 se_tpg
= &tv_tpg
->se_tpg
;
1163 tv_nexus
= kzalloc(sizeof(struct tcm_vhost_nexus
), GFP_KERNEL
);
1165 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1166 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1170 * Initialize the struct se_session pointer
1172 tv_nexus
->tvn_se_sess
= transport_init_session();
1173 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1174 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1179 * Since we are running in 'demo mode' this call with generate a
1180 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1181 * the SCSI Initiator port name of the passed configfs group 'name'.
1183 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1184 se_tpg
, (unsigned char *)name
);
1185 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1186 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1187 pr_debug("core_tpg_check_initiator_node_acl() failed"
1189 transport_free_session(tv_nexus
->tvn_se_sess
);
1194 * Now register the TCM vhost virtual I_T Nexus as active with the
1195 * call to __transport_register_session()
1197 __transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1198 tv_nexus
->tvn_se_sess
, tv_nexus
);
1199 tv_tpg
->tpg_nexus
= tv_nexus
;
1201 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1205 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg
*tpg
)
1207 struct se_session
*se_sess
;
1208 struct tcm_vhost_nexus
*tv_nexus
;
1210 mutex_lock(&tpg
->tv_tpg_mutex
);
1211 tv_nexus
= tpg
->tpg_nexus
;
1213 mutex_unlock(&tpg
->tv_tpg_mutex
);
1217 se_sess
= tv_nexus
->tvn_se_sess
;
1219 mutex_unlock(&tpg
->tv_tpg_mutex
);
1223 if (tpg
->tv_tpg_port_count
!= 0) {
1224 mutex_unlock(&tpg
->tv_tpg_mutex
);
1225 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1226 " active TPG port count: %d\n",
1227 tpg
->tv_tpg_port_count
);
1231 if (tpg
->tv_tpg_vhost_count
!= 0) {
1232 mutex_unlock(&tpg
->tv_tpg_mutex
);
1233 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1234 " active TPG vhost count: %d\n",
1235 tpg
->tv_tpg_vhost_count
);
1239 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1240 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg
->tport
),
1241 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1243 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1245 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1246 tpg
->tpg_nexus
= NULL
;
1247 mutex_unlock(&tpg
->tv_tpg_mutex
);
1253 static ssize_t
tcm_vhost_tpg_show_nexus(struct se_portal_group
*se_tpg
,
1256 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1257 struct tcm_vhost_tpg
, se_tpg
);
1258 struct tcm_vhost_nexus
*tv_nexus
;
1261 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1262 tv_nexus
= tv_tpg
->tpg_nexus
;
1264 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1267 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1268 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1269 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1274 static ssize_t
tcm_vhost_tpg_store_nexus(struct se_portal_group
*se_tpg
,
1278 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1279 struct tcm_vhost_tpg
, se_tpg
);
1280 struct tcm_vhost_tport
*tport_wwn
= tv_tpg
->tport
;
1281 unsigned char i_port
[TCM_VHOST_NAMELEN
], *ptr
, *port_ptr
;
1284 * Shutdown the active I_T nexus if 'NULL' is passed..
1286 if (!strncmp(page
, "NULL", 4)) {
1287 ret
= tcm_vhost_drop_nexus(tv_tpg
);
1288 return (!ret
) ? count
: ret
;
1291 * Otherwise make sure the passed virtual Initiator port WWN matches
1292 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1293 * tcm_vhost_make_nexus().
1295 if (strlen(page
) >= TCM_VHOST_NAMELEN
) {
1296 pr_err("Emulated NAA Sas Address: %s, exceeds"
1297 " max: %d\n", page
, TCM_VHOST_NAMELEN
);
1300 snprintf(&i_port
[0], TCM_VHOST_NAMELEN
, "%s", page
);
1302 ptr
= strstr(i_port
, "naa.");
1304 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1305 pr_err("Passed SAS Initiator Port %s does not"
1306 " match target port protoid: %s\n", i_port
,
1307 tcm_vhost_dump_proto_id(tport_wwn
));
1310 port_ptr
= &i_port
[0];
1313 ptr
= strstr(i_port
, "fc.");
1315 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1316 pr_err("Passed FCP Initiator Port %s does not"
1317 " match target port protoid: %s\n", i_port
,
1318 tcm_vhost_dump_proto_id(tport_wwn
));
1321 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1324 ptr
= strstr(i_port
, "iqn.");
1326 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1327 pr_err("Passed iSCSI Initiator Port %s does not"
1328 " match target port protoid: %s\n", i_port
,
1329 tcm_vhost_dump_proto_id(tport_wwn
));
1332 port_ptr
= &i_port
[0];
1335 pr_err("Unable to locate prefix for emulated Initiator Port:"
1339 * Clear any trailing newline for the NAA WWN
1342 if (i_port
[strlen(i_port
)-1] == '\n')
1343 i_port
[strlen(i_port
)-1] = '\0';
1345 ret
= tcm_vhost_make_nexus(tv_tpg
, port_ptr
);
1352 TF_TPG_BASE_ATTR(tcm_vhost
, nexus
, S_IRUGO
| S_IWUSR
);
1354 static struct configfs_attribute
*tcm_vhost_tpg_attrs
[] = {
1355 &tcm_vhost_tpg_nexus
.attr
,
1359 static struct se_portal_group
*tcm_vhost_make_tpg(struct se_wwn
*wwn
,
1360 struct config_group
*group
,
1363 struct tcm_vhost_tport
*tport
= container_of(wwn
,
1364 struct tcm_vhost_tport
, tport_wwn
);
1366 struct tcm_vhost_tpg
*tpg
;
1370 if (strstr(name
, "tpgt_") != name
)
1371 return ERR_PTR(-EINVAL
);
1372 if (kstrtoul(name
+ 5, 10, &tpgt
) || tpgt
> UINT_MAX
)
1373 return ERR_PTR(-EINVAL
);
1375 tpg
= kzalloc(sizeof(struct tcm_vhost_tpg
), GFP_KERNEL
);
1377 pr_err("Unable to allocate struct tcm_vhost_tpg");
1378 return ERR_PTR(-ENOMEM
);
1380 mutex_init(&tpg
->tv_tpg_mutex
);
1381 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1383 tpg
->tport_tpgt
= tpgt
;
1385 ret
= core_tpg_register(&tcm_vhost_fabric_configfs
->tf_ops
, wwn
,
1386 &tpg
->se_tpg
, tpg
, TRANSPORT_TPG_TYPE_NORMAL
);
1391 mutex_lock(&tcm_vhost_mutex
);
1392 list_add_tail(&tpg
->tv_tpg_list
, &tcm_vhost_list
);
1393 mutex_unlock(&tcm_vhost_mutex
);
1395 return &tpg
->se_tpg
;
1398 static void tcm_vhost_drop_tpg(struct se_portal_group
*se_tpg
)
1400 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1401 struct tcm_vhost_tpg
, se_tpg
);
1403 mutex_lock(&tcm_vhost_mutex
);
1404 list_del(&tpg
->tv_tpg_list
);
1405 mutex_unlock(&tcm_vhost_mutex
);
1407 * Release the virtual I_T Nexus for this vhost TPG
1409 tcm_vhost_drop_nexus(tpg
);
1411 * Deregister the se_tpg from TCM..
1413 core_tpg_deregister(se_tpg
);
1417 static struct se_wwn
*tcm_vhost_make_tport(struct target_fabric_configfs
*tf
,
1418 struct config_group
*group
,
1421 struct tcm_vhost_tport
*tport
;
1426 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1427 return ERR_PTR(-EINVAL); */
1429 tport
= kzalloc(sizeof(struct tcm_vhost_tport
), GFP_KERNEL
);
1431 pr_err("Unable to allocate struct tcm_vhost_tport");
1432 return ERR_PTR(-ENOMEM
);
1434 tport
->tport_wwpn
= wwpn
;
1436 * Determine the emulated Protocol Identifier and Target Port Name
1437 * based on the incoming configfs directory name.
1439 ptr
= strstr(name
, "naa.");
1441 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
1444 ptr
= strstr(name
, "fc.");
1446 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
1447 off
= 3; /* Skip over "fc." */
1450 ptr
= strstr(name
, "iqn.");
1452 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
1456 pr_err("Unable to locate prefix for emulated Target Port:"
1459 return ERR_PTR(-EINVAL
);
1462 if (strlen(name
) >= TCM_VHOST_NAMELEN
) {
1463 pr_err("Emulated %s Address: %s, exceeds"
1464 " max: %d\n", name
, tcm_vhost_dump_proto_id(tport
),
1467 return ERR_PTR(-EINVAL
);
1469 snprintf(&tport
->tport_name
[0], TCM_VHOST_NAMELEN
, "%s", &name
[off
]);
1471 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1472 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
), name
);
1474 return &tport
->tport_wwn
;
1477 static void tcm_vhost_drop_tport(struct se_wwn
*wwn
)
1479 struct tcm_vhost_tport
*tport
= container_of(wwn
,
1480 struct tcm_vhost_tport
, tport_wwn
);
1482 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1483 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
),
1489 static ssize_t
tcm_vhost_wwn_show_attr_version(
1490 struct target_fabric_configfs
*tf
,
1493 return sprintf(page
, "TCM_VHOST fabric module %s on %s/%s"
1494 "on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
1495 utsname()->machine
);
1498 TF_WWN_ATTR_RO(tcm_vhost
, version
);
1500 static struct configfs_attribute
*tcm_vhost_wwn_attrs
[] = {
1501 &tcm_vhost_wwn_version
.attr
,
1505 static struct target_core_fabric_ops tcm_vhost_ops
= {
1506 .get_fabric_name
= tcm_vhost_get_fabric_name
,
1507 .get_fabric_proto_ident
= tcm_vhost_get_fabric_proto_ident
,
1508 .tpg_get_wwn
= tcm_vhost_get_fabric_wwn
,
1509 .tpg_get_tag
= tcm_vhost_get_tag
,
1510 .tpg_get_default_depth
= tcm_vhost_get_default_depth
,
1511 .tpg_get_pr_transport_id
= tcm_vhost_get_pr_transport_id
,
1512 .tpg_get_pr_transport_id_len
= tcm_vhost_get_pr_transport_id_len
,
1513 .tpg_parse_pr_out_transport_id
= tcm_vhost_parse_pr_out_transport_id
,
1514 .tpg_check_demo_mode
= tcm_vhost_check_true
,
1515 .tpg_check_demo_mode_cache
= tcm_vhost_check_true
,
1516 .tpg_check_demo_mode_write_protect
= tcm_vhost_check_false
,
1517 .tpg_check_prod_mode_write_protect
= tcm_vhost_check_false
,
1518 .tpg_alloc_fabric_acl
= tcm_vhost_alloc_fabric_acl
,
1519 .tpg_release_fabric_acl
= tcm_vhost_release_fabric_acl
,
1520 .tpg_get_inst_index
= tcm_vhost_tpg_get_inst_index
,
1521 .release_cmd
= tcm_vhost_release_cmd
,
1522 .shutdown_session
= tcm_vhost_shutdown_session
,
1523 .close_session
= tcm_vhost_close_session
,
1524 .sess_get_index
= tcm_vhost_sess_get_index
,
1525 .sess_get_initiator_sid
= NULL
,
1526 .write_pending
= tcm_vhost_write_pending
,
1527 .write_pending_status
= tcm_vhost_write_pending_status
,
1528 .set_default_node_attributes
= tcm_vhost_set_default_node_attrs
,
1529 .get_task_tag
= tcm_vhost_get_task_tag
,
1530 .get_cmd_state
= tcm_vhost_get_cmd_state
,
1531 .queue_data_in
= tcm_vhost_queue_data_in
,
1532 .queue_status
= tcm_vhost_queue_status
,
1533 .queue_tm_rsp
= tcm_vhost_queue_tm_rsp
,
1534 .get_fabric_sense_len
= tcm_vhost_get_fabric_sense_len
,
1535 .set_fabric_sense_len
= tcm_vhost_set_fabric_sense_len
,
1537 * Setup callers for generic logic in target_core_fabric_configfs.c
1539 .fabric_make_wwn
= tcm_vhost_make_tport
,
1540 .fabric_drop_wwn
= tcm_vhost_drop_tport
,
1541 .fabric_make_tpg
= tcm_vhost_make_tpg
,
1542 .fabric_drop_tpg
= tcm_vhost_drop_tpg
,
1543 .fabric_post_link
= tcm_vhost_port_link
,
1544 .fabric_pre_unlink
= tcm_vhost_port_unlink
,
1545 .fabric_make_np
= NULL
,
1546 .fabric_drop_np
= NULL
,
1547 .fabric_make_nodeacl
= tcm_vhost_make_nodeacl
,
1548 .fabric_drop_nodeacl
= tcm_vhost_drop_nodeacl
,
1551 static int tcm_vhost_register_configfs(void)
1553 struct target_fabric_configfs
*fabric
;
1556 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1557 " on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
1558 utsname()->machine
);
1560 * Register the top level struct config_item_type with TCM core
1562 fabric
= target_fabric_configfs_init(THIS_MODULE
, "vhost");
1563 if (IS_ERR(fabric
)) {
1564 pr_err("target_fabric_configfs_init() failed\n");
1565 return PTR_ERR(fabric
);
1568 * Setup fabric->tf_ops from our local tcm_vhost_ops
1570 fabric
->tf_ops
= tcm_vhost_ops
;
1572 * Setup default attribute lists for various fabric->tf_cit_tmpl
1574 TF_CIT_TMPL(fabric
)->tfc_wwn_cit
.ct_attrs
= tcm_vhost_wwn_attrs
;
1575 TF_CIT_TMPL(fabric
)->tfc_tpg_base_cit
.ct_attrs
= tcm_vhost_tpg_attrs
;
1576 TF_CIT_TMPL(fabric
)->tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
1577 TF_CIT_TMPL(fabric
)->tfc_tpg_param_cit
.ct_attrs
= NULL
;
1578 TF_CIT_TMPL(fabric
)->tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
1579 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_base_cit
.ct_attrs
= NULL
;
1580 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
1581 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
1582 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
1584 * Register the fabric for use within TCM
1586 ret
= target_fabric_configfs_register(fabric
);
1588 pr_err("target_fabric_configfs_register() failed"
1589 " for TCM_VHOST\n");
1593 * Setup our local pointer to *fabric
1595 tcm_vhost_fabric_configfs
= fabric
;
1596 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1600 static void tcm_vhost_deregister_configfs(void)
1602 if (!tcm_vhost_fabric_configfs
)
1605 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs
);
1606 tcm_vhost_fabric_configfs
= NULL
;
1607 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1610 static int __init
tcm_vhost_init(void)
1614 * Use our own dedicated workqueue for submitting I/O into
1615 * target core to avoid contention within system_wq.
1617 tcm_vhost_workqueue
= alloc_workqueue("tcm_vhost", 0, 0);
1618 if (!tcm_vhost_workqueue
)
1621 ret
= vhost_scsi_register();
1623 goto out_destroy_workqueue
;
1625 ret
= tcm_vhost_register_configfs();
1627 goto out_vhost_scsi_deregister
;
1631 out_vhost_scsi_deregister
:
1632 vhost_scsi_deregister();
1633 out_destroy_workqueue
:
1634 destroy_workqueue(tcm_vhost_workqueue
);
1639 static void tcm_vhost_exit(void)
1641 tcm_vhost_deregister_configfs();
1642 vhost_scsi_deregister();
1643 destroy_workqueue(tcm_vhost_workqueue
);
1646 MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1647 MODULE_LICENSE("GPL");
1648 module_init(tcm_vhost_init
);
1649 module_exit(tcm_vhost_exit
);