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 void vhost_scsi_free_cmd(struct tcm_vhost_cmd
*tv_cmd
)
335 struct se_cmd
*se_cmd
= &tv_cmd
->tvc_se_cmd
;
337 /* TODO locking against target/backend threads? */
338 transport_generic_free_cmd(se_cmd
, 1);
340 if (tv_cmd
->tvc_sgl_count
) {
342 for (i
= 0; i
< tv_cmd
->tvc_sgl_count
; i
++)
343 put_page(sg_page(&tv_cmd
->tvc_sgl
[i
]));
345 kfree(tv_cmd
->tvc_sgl
);
351 /* Dequeue a command from the completion list */
352 static struct tcm_vhost_cmd
*vhost_scsi_get_cmd_from_completion(
353 struct vhost_scsi
*vs
)
355 struct tcm_vhost_cmd
*tv_cmd
= NULL
;
357 spin_lock_bh(&vs
->vs_completion_lock
);
358 if (list_empty(&vs
->vs_completion_list
)) {
359 spin_unlock_bh(&vs
->vs_completion_lock
);
363 list_for_each_entry(tv_cmd
, &vs
->vs_completion_list
,
364 tvc_completion_list
) {
365 list_del(&tv_cmd
->tvc_completion_list
);
368 spin_unlock_bh(&vs
->vs_completion_lock
);
372 /* Fill in status and signal that we are done processing this command
374 * This is scheduled in the vhost work queue so we are called with the owner
375 * process mm and can access the vring.
377 static void vhost_scsi_complete_cmd_work(struct vhost_work
*work
)
379 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
381 struct tcm_vhost_cmd
*tv_cmd
;
383 while ((tv_cmd
= vhost_scsi_get_cmd_from_completion(vs
))) {
384 struct virtio_scsi_cmd_resp v_rsp
;
385 struct se_cmd
*se_cmd
= &tv_cmd
->tvc_se_cmd
;
388 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__
,
389 tv_cmd
, se_cmd
->residual_count
, se_cmd
->scsi_status
);
391 memset(&v_rsp
, 0, sizeof(v_rsp
));
392 v_rsp
.resid
= se_cmd
->residual_count
;
393 /* TODO is status_qualifier field needed? */
394 v_rsp
.status
= se_cmd
->scsi_status
;
395 v_rsp
.sense_len
= se_cmd
->scsi_sense_length
;
396 memcpy(v_rsp
.sense
, tv_cmd
->tvc_sense_buf
,
398 ret
= copy_to_user(tv_cmd
->tvc_resp
, &v_rsp
, sizeof(v_rsp
));
399 if (likely(ret
== 0))
400 vhost_add_used(&vs
->vqs
[2], tv_cmd
->tvc_vq_desc
, 0);
402 pr_err("Faulted on virtio_scsi_cmd_resp\n");
404 vhost_scsi_free_cmd(tv_cmd
);
407 vhost_signal(&vs
->dev
, &vs
->vqs
[2]);
410 static struct tcm_vhost_cmd
*vhost_scsi_allocate_cmd(
411 struct tcm_vhost_tpg
*tv_tpg
,
412 struct virtio_scsi_cmd_req
*v_req
,
416 struct tcm_vhost_cmd
*tv_cmd
;
417 struct tcm_vhost_nexus
*tv_nexus
;
418 struct se_session
*se_sess
;
420 tv_nexus
= tv_tpg
->tpg_nexus
;
422 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
423 return ERR_PTR(-EIO
);
425 se_sess
= tv_nexus
->tvn_se_sess
;
427 tv_cmd
= kzalloc(sizeof(struct tcm_vhost_cmd
), GFP_ATOMIC
);
429 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
430 return ERR_PTR(-ENOMEM
);
432 INIT_LIST_HEAD(&tv_cmd
->tvc_completion_list
);
433 tv_cmd
->tvc_tag
= v_req
->tag
;
434 tv_cmd
->tvc_task_attr
= v_req
->task_attr
;
435 tv_cmd
->tvc_exp_data_len
= exp_data_len
;
436 tv_cmd
->tvc_data_direction
= data_direction
;
437 tv_cmd
->tvc_nexus
= tv_nexus
;
443 * Map a user memory range into a scatterlist
445 * Returns the number of scatterlist entries used or -errno on error.
447 static int vhost_scsi_map_to_sgl(struct scatterlist
*sgl
,
448 unsigned int sgl_count
, void __user
*ptr
, size_t len
, int write
)
450 struct scatterlist
*sg
= sgl
;
451 unsigned int npages
= 0;
456 unsigned int offset
= (uintptr_t)ptr
& ~PAGE_MASK
;
457 unsigned int nbytes
= min_t(unsigned int,
458 PAGE_SIZE
- offset
, len
);
460 if (npages
== sgl_count
) {
465 ret
= get_user_pages_fast((unsigned long)ptr
, 1, write
, &page
);
466 BUG_ON(ret
== 0); /* we should either get our page or fail */
470 sg_set_page(sg
, page
, nbytes
, offset
);
479 /* Put pages that we hold */
480 for (sg
= sgl
; sg
!= &sgl
[npages
]; sg
++)
481 put_page(sg_page(sg
));
485 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd
*tv_cmd
,
486 struct iovec
*iov
, unsigned int niov
, int write
)
491 struct scatterlist
*sg
;
494 * Find out how long sglist needs to be
497 for (i
= 0; i
< niov
; i
++) {
498 sgl_count
+= (((uintptr_t)iov
[i
].iov_base
+ iov
[i
].iov_len
+
499 PAGE_SIZE
- 1) >> PAGE_SHIFT
) -
500 ((uintptr_t)iov
[i
].iov_base
>> PAGE_SHIFT
);
502 /* TODO overflow checking */
504 sg
= kmalloc(sizeof(tv_cmd
->tvc_sgl
[0]) * sgl_count
, GFP_ATOMIC
);
507 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__
,
509 sg_init_table(sg
, sgl_count
);
511 tv_cmd
->tvc_sgl
= sg
;
512 tv_cmd
->tvc_sgl_count
= sgl_count
;
514 pr_debug("Mapping %u iovecs for %u pages\n", niov
, sgl_count
);
515 for (i
= 0; i
< niov
; i
++) {
516 ret
= vhost_scsi_map_to_sgl(sg
, sgl_count
, iov
[i
].iov_base
,
517 iov
[i
].iov_len
, write
);
519 for (i
= 0; i
< tv_cmd
->tvc_sgl_count
; i
++)
520 put_page(sg_page(&tv_cmd
->tvc_sgl
[i
]));
521 kfree(tv_cmd
->tvc_sgl
);
522 tv_cmd
->tvc_sgl
= NULL
;
523 tv_cmd
->tvc_sgl_count
= 0;
533 static void tcm_vhost_submission_work(struct work_struct
*work
)
535 struct tcm_vhost_cmd
*tv_cmd
=
536 container_of(work
, struct tcm_vhost_cmd
, work
);
537 struct tcm_vhost_nexus
*tv_nexus
;
538 struct se_cmd
*se_cmd
= &tv_cmd
->tvc_se_cmd
;
539 struct scatterlist
*sg_ptr
, *sg_bidi_ptr
= NULL
;
540 int rc
, sg_no_bidi
= 0;
542 if (tv_cmd
->tvc_sgl_count
) {
543 sg_ptr
= tv_cmd
->tvc_sgl
;
545 * For BIDI commands, pass in the extra READ buffer
546 * to transport_generic_map_mem_to_cmd() below..
548 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
550 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
558 tv_nexus
= tv_cmd
->tvc_nexus
;
560 rc
= target_submit_cmd_map_sgls(se_cmd
, tv_nexus
->tvn_se_sess
,
561 tv_cmd
->tvc_cdb
, &tv_cmd
->tvc_sense_buf
[0],
562 tv_cmd
->tvc_lun
, tv_cmd
->tvc_exp_data_len
,
563 tv_cmd
->tvc_task_attr
, tv_cmd
->tvc_data_direction
,
564 0, sg_ptr
, tv_cmd
->tvc_sgl_count
,
565 sg_bidi_ptr
, sg_no_bidi
);
567 transport_send_check_condition_and_sense(se_cmd
,
568 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
569 transport_generic_free_cmd(se_cmd
, 0);
573 static void vhost_scsi_handle_vq(struct vhost_scsi
*vs
)
575 struct vhost_virtqueue
*vq
= &vs
->vqs
[2];
576 struct virtio_scsi_cmd_req v_req
;
577 struct tcm_vhost_tpg
*tv_tpg
;
578 struct tcm_vhost_cmd
*tv_cmd
;
579 u32 exp_data_len
, data_first
, data_num
, data_direction
;
583 /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
585 if (unlikely(!tv_tpg
)) {
586 pr_err("%s endpoint not set\n", __func__
);
590 mutex_lock(&vq
->mutex
);
591 vhost_disable_notify(&vs
->dev
, vq
);
594 head
= vhost_get_vq_desc(&vs
->dev
, vq
, vq
->iov
,
595 ARRAY_SIZE(vq
->iov
), &out
, &in
,
597 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
599 /* On error, stop handling until the next kick. */
600 if (unlikely(head
< 0))
602 /* Nothing new? Wait for eventfd to tell us they refilled. */
603 if (head
== vq
->num
) {
604 if (unlikely(vhost_enable_notify(&vs
->dev
, vq
))) {
605 vhost_disable_notify(&vs
->dev
, vq
);
611 /* FIXME: BIDI operation */
612 if (out
== 1 && in
== 1) {
613 data_direction
= DMA_NONE
;
616 } else if (out
== 1 && in
> 1) {
617 data_direction
= DMA_FROM_DEVICE
;
618 data_first
= out
+ 1;
620 } else if (out
> 1 && in
== 1) {
621 data_direction
= DMA_TO_DEVICE
;
625 vq_err(vq
, "Invalid buffer layout out: %u in: %u\n",
631 * Check for a sane resp buffer so we can report errors to
634 if (unlikely(vq
->iov
[out
].iov_len
!=
635 sizeof(struct virtio_scsi_cmd_resp
))) {
636 vq_err(vq
, "Expecting virtio_scsi_cmd_resp, got %zu"
637 " bytes\n", vq
->iov
[out
].iov_len
);
641 if (unlikely(vq
->iov
[0].iov_len
!= sizeof(v_req
))) {
642 vq_err(vq
, "Expecting virtio_scsi_cmd_req, got %zu"
643 " bytes\n", vq
->iov
[0].iov_len
);
646 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
647 " len: %zu\n", vq
->iov
[0].iov_base
, sizeof(v_req
));
648 ret
= __copy_from_user(&v_req
, vq
->iov
[0].iov_base
,
651 vq_err(vq
, "Faulted on virtio_scsi_cmd_req\n");
656 for (i
= 0; i
< data_num
; i
++)
657 exp_data_len
+= vq
->iov
[data_first
+ i
].iov_len
;
659 tv_cmd
= vhost_scsi_allocate_cmd(tv_tpg
, &v_req
,
660 exp_data_len
, data_direction
);
661 if (IS_ERR(tv_cmd
)) {
662 vq_err(vq
, "vhost_scsi_allocate_cmd failed %ld\n",
666 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
667 ": %d\n", tv_cmd
, exp_data_len
, data_direction
);
669 tv_cmd
->tvc_vhost
= vs
;
671 if (unlikely(vq
->iov
[out
].iov_len
!=
672 sizeof(struct virtio_scsi_cmd_resp
))) {
673 vq_err(vq
, "Expecting virtio_scsi_cmd_resp, got %zu"
674 " bytes, out: %d, in: %d\n",
675 vq
->iov
[out
].iov_len
, out
, in
);
679 tv_cmd
->tvc_resp
= vq
->iov
[out
].iov_base
;
682 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
683 * that will be used by tcm_vhost_new_cmd_map() and down into
684 * target_setup_cmd_from_cdb()
686 memcpy(tv_cmd
->tvc_cdb
, v_req
.cdb
, TCM_VHOST_MAX_CDB_SIZE
);
688 * Check that the recieved CDB size does not exceeded our
689 * hardcoded max for tcm_vhost
691 /* TODO what if cdb was too small for varlen cdb header? */
692 if (unlikely(scsi_command_size(tv_cmd
->tvc_cdb
) >
693 TCM_VHOST_MAX_CDB_SIZE
)) {
694 vq_err(vq
, "Received SCSI CDB with command_size: %d that"
695 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
696 scsi_command_size(tv_cmd
->tvc_cdb
),
697 TCM_VHOST_MAX_CDB_SIZE
);
700 tv_cmd
->tvc_lun
= ((v_req
.lun
[2] << 8) | v_req
.lun
[3]) & 0x3FFF;
702 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
703 tv_cmd
->tvc_cdb
[0], tv_cmd
->tvc_lun
);
705 if (data_direction
!= DMA_NONE
) {
706 ret
= vhost_scsi_map_iov_to_sgl(tv_cmd
,
707 &vq
->iov
[data_first
], data_num
,
708 data_direction
== DMA_TO_DEVICE
);
710 vq_err(vq
, "Failed to map iov to sgl\n");
716 * Save the descriptor from vhost_get_vq_desc() to be used to
717 * complete the virtio-scsi request in TCM callback context via
718 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
720 tv_cmd
->tvc_vq_desc
= head
;
722 * Dispatch tv_cmd descriptor for cmwq execution in process
723 * context provided by tcm_vhost_workqueue. This also ensures
724 * tv_cmd is executed on the same kworker CPU as this vhost
725 * thread to gain positive L2 cache locality effects..
727 INIT_WORK(&tv_cmd
->work
, tcm_vhost_submission_work
);
728 queue_work(tcm_vhost_workqueue
, &tv_cmd
->work
);
731 mutex_unlock(&vq
->mutex
);
734 static void vhost_scsi_ctl_handle_kick(struct vhost_work
*work
)
736 pr_debug("%s: The handling func for control queue.\n", __func__
);
739 static void vhost_scsi_evt_handle_kick(struct vhost_work
*work
)
741 pr_debug("%s: The handling func for event queue.\n", __func__
);
744 static void vhost_scsi_handle_kick(struct vhost_work
*work
)
746 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
748 struct vhost_scsi
*vs
= container_of(vq
->dev
, struct vhost_scsi
, dev
);
750 vhost_scsi_handle_vq(vs
);
754 * Called from vhost_scsi_ioctl() context to walk the list of available
755 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
757 static int vhost_scsi_set_endpoint(
758 struct vhost_scsi
*vs
,
759 struct vhost_scsi_target
*t
)
761 struct tcm_vhost_tport
*tv_tport
;
762 struct tcm_vhost_tpg
*tv_tpg
;
765 mutex_lock(&vs
->dev
.mutex
);
766 /* Verify that ring has been setup correctly. */
767 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
768 /* Verify that ring has been setup correctly. */
769 if (!vhost_vq_access_ok(&vs
->vqs
[index
])) {
770 mutex_unlock(&vs
->dev
.mutex
);
774 mutex_unlock(&vs
->dev
.mutex
);
776 mutex_lock(&tcm_vhost_mutex
);
777 list_for_each_entry(tv_tpg
, &tcm_vhost_list
, tv_tpg_list
) {
778 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
779 if (!tv_tpg
->tpg_nexus
) {
780 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
783 if (tv_tpg
->tv_tpg_vhost_count
!= 0) {
784 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
787 tv_tport
= tv_tpg
->tport
;
789 if (!strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
) &&
790 (tv_tpg
->tport_tpgt
== t
->vhost_tpgt
)) {
791 tv_tpg
->tv_tpg_vhost_count
++;
792 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
793 mutex_unlock(&tcm_vhost_mutex
);
795 mutex_lock(&vs
->dev
.mutex
);
797 mutex_unlock(&vs
->dev
.mutex
);
798 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
799 tv_tpg
->tv_tpg_vhost_count
--;
800 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
805 smp_mb__after_atomic_inc();
806 mutex_unlock(&vs
->dev
.mutex
);
809 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
811 mutex_unlock(&tcm_vhost_mutex
);
815 static int vhost_scsi_clear_endpoint(
816 struct vhost_scsi
*vs
,
817 struct vhost_scsi_target
*t
)
819 struct tcm_vhost_tport
*tv_tport
;
820 struct tcm_vhost_tpg
*tv_tpg
;
823 mutex_lock(&vs
->dev
.mutex
);
824 /* Verify that ring has been setup correctly. */
825 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
826 if (!vhost_vq_access_ok(&vs
->vqs
[index
])) {
837 tv_tport
= tv_tpg
->tport
;
839 if (strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
) ||
840 (tv_tpg
->tport_tpgt
!= t
->vhost_tpgt
)) {
841 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
842 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
843 tv_tport
->tport_name
, tv_tpg
->tport_tpgt
,
844 t
->vhost_wwpn
, t
->vhost_tpgt
);
848 tv_tpg
->tv_tpg_vhost_count
--;
850 mutex_unlock(&vs
->dev
.mutex
);
855 mutex_unlock(&vs
->dev
.mutex
);
859 static int vhost_scsi_open(struct inode
*inode
, struct file
*f
)
861 struct vhost_scsi
*s
;
864 s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
868 vhost_work_init(&s
->vs_completion_work
, vhost_scsi_complete_cmd_work
);
869 INIT_LIST_HEAD(&s
->vs_completion_list
);
870 spin_lock_init(&s
->vs_completion_lock
);
872 s
->vqs
[VHOST_SCSI_VQ_CTL
].handle_kick
= vhost_scsi_ctl_handle_kick
;
873 s
->vqs
[VHOST_SCSI_VQ_EVT
].handle_kick
= vhost_scsi_evt_handle_kick
;
874 s
->vqs
[VHOST_SCSI_VQ_IO
].handle_kick
= vhost_scsi_handle_kick
;
875 r
= vhost_dev_init(&s
->dev
, s
->vqs
, 3);
885 static int vhost_scsi_release(struct inode
*inode
, struct file
*f
)
887 struct vhost_scsi
*s
= f
->private_data
;
889 if (s
->vs_tpg
&& s
->vs_tpg
->tport
) {
890 struct vhost_scsi_target backend
;
892 memcpy(backend
.vhost_wwpn
, s
->vs_tpg
->tport
->tport_name
,
893 sizeof(backend
.vhost_wwpn
));
894 backend
.vhost_tpgt
= s
->vs_tpg
->tport_tpgt
;
895 vhost_scsi_clear_endpoint(s
, &backend
);
898 vhost_dev_cleanup(&s
->dev
, false);
903 static void vhost_scsi_flush_vq(struct vhost_scsi
*vs
, int index
)
905 vhost_poll_flush(&vs
->dev
.vqs
[index
].poll
);
908 static void vhost_scsi_flush(struct vhost_scsi
*vs
)
910 vhost_scsi_flush_vq(vs
, VHOST_SCSI_VQ_CTL
);
911 vhost_scsi_flush_vq(vs
, VHOST_SCSI_VQ_EVT
);
912 vhost_scsi_flush_vq(vs
, VHOST_SCSI_VQ_IO
);
915 static int vhost_scsi_set_features(struct vhost_scsi
*vs
, u64 features
)
917 if (features
& ~VHOST_FEATURES
)
920 mutex_lock(&vs
->dev
.mutex
);
921 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
922 !vhost_log_access_ok(&vs
->dev
)) {
923 mutex_unlock(&vs
->dev
.mutex
);
926 vs
->dev
.acked_features
= features
;
928 vhost_scsi_flush(vs
);
929 mutex_unlock(&vs
->dev
.mutex
);
933 static long vhost_scsi_ioctl(struct file
*f
, unsigned int ioctl
,
936 struct vhost_scsi
*vs
= f
->private_data
;
937 struct vhost_scsi_target backend
;
938 void __user
*argp
= (void __user
*)arg
;
939 u64 __user
*featurep
= argp
;
941 int r
, abi_version
= VHOST_SCSI_ABI_VERSION
;
944 case VHOST_SCSI_SET_ENDPOINT
:
945 if (copy_from_user(&backend
, argp
, sizeof backend
))
947 if (backend
.reserved
!= 0)
950 return vhost_scsi_set_endpoint(vs
, &backend
);
951 case VHOST_SCSI_CLEAR_ENDPOINT
:
952 if (copy_from_user(&backend
, argp
, sizeof backend
))
954 if (backend
.reserved
!= 0)
957 return vhost_scsi_clear_endpoint(vs
, &backend
);
958 case VHOST_SCSI_GET_ABI_VERSION
:
959 if (copy_to_user(argp
, &abi_version
, sizeof abi_version
))
962 case VHOST_GET_FEATURES
:
963 features
= VHOST_FEATURES
;
964 if (copy_to_user(featurep
, &features
, sizeof features
))
967 case VHOST_SET_FEATURES
:
968 if (copy_from_user(&features
, featurep
, sizeof features
))
970 return vhost_scsi_set_features(vs
, features
);
972 mutex_lock(&vs
->dev
.mutex
);
973 r
= vhost_dev_ioctl(&vs
->dev
, ioctl
, arg
);
974 mutex_unlock(&vs
->dev
.mutex
);
980 static long vhost_scsi_compat_ioctl(struct file
*f
, unsigned int ioctl
,
983 return vhost_scsi_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
987 static const struct file_operations vhost_scsi_fops
= {
988 .owner
= THIS_MODULE
,
989 .release
= vhost_scsi_release
,
990 .unlocked_ioctl
= vhost_scsi_ioctl
,
992 .compat_ioctl
= vhost_scsi_compat_ioctl
,
994 .open
= vhost_scsi_open
,
995 .llseek
= noop_llseek
,
998 static struct miscdevice vhost_scsi_misc
= {
1004 static int __init
vhost_scsi_register(void)
1006 return misc_register(&vhost_scsi_misc
);
1009 static int vhost_scsi_deregister(void)
1011 return misc_deregister(&vhost_scsi_misc
);
1014 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport
*tport
)
1016 switch (tport
->tport_proto_id
) {
1017 case SCSI_PROTOCOL_SAS
:
1019 case SCSI_PROTOCOL_FCP
:
1021 case SCSI_PROTOCOL_ISCSI
:
1030 static int tcm_vhost_port_link(struct se_portal_group
*se_tpg
,
1033 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1034 struct tcm_vhost_tpg
, se_tpg
);
1036 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1037 tv_tpg
->tv_tpg_port_count
++;
1038 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1043 static void tcm_vhost_port_unlink(struct se_portal_group
*se_tpg
,
1044 struct se_lun
*se_lun
)
1046 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1047 struct tcm_vhost_tpg
, se_tpg
);
1049 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1050 tv_tpg
->tv_tpg_port_count
--;
1051 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1054 static struct se_node_acl
*tcm_vhost_make_nodeacl(
1055 struct se_portal_group
*se_tpg
,
1056 struct config_group
*group
,
1059 struct se_node_acl
*se_nacl
, *se_nacl_new
;
1060 struct tcm_vhost_nacl
*nacl
;
1064 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1065 return ERR_PTR(-EINVAL); */
1066 se_nacl_new
= tcm_vhost_alloc_fabric_acl(se_tpg
);
1068 return ERR_PTR(-ENOMEM
);
1072 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1073 * when converting a NodeACL from demo mode -> explict
1075 se_nacl
= core_tpg_add_initiator_node_acl(se_tpg
, se_nacl_new
,
1077 if (IS_ERR(se_nacl
)) {
1078 tcm_vhost_release_fabric_acl(se_tpg
, se_nacl_new
);
1082 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1084 nacl
= container_of(se_nacl
, struct tcm_vhost_nacl
, se_node_acl
);
1085 nacl
->iport_wwpn
= wwpn
;
1090 static void tcm_vhost_drop_nodeacl(struct se_node_acl
*se_acl
)
1092 struct tcm_vhost_nacl
*nacl
= container_of(se_acl
,
1093 struct tcm_vhost_nacl
, se_node_acl
);
1094 core_tpg_del_initiator_node_acl(se_acl
->se_tpg
, se_acl
, 1);
1098 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg
*tv_tpg
,
1101 struct se_portal_group
*se_tpg
;
1102 struct tcm_vhost_nexus
*tv_nexus
;
1104 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1105 if (tv_tpg
->tpg_nexus
) {
1106 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1107 pr_debug("tv_tpg->tpg_nexus already exists\n");
1110 se_tpg
= &tv_tpg
->se_tpg
;
1112 tv_nexus
= kzalloc(sizeof(struct tcm_vhost_nexus
), GFP_KERNEL
);
1114 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1115 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1119 * Initialize the struct se_session pointer
1121 tv_nexus
->tvn_se_sess
= transport_init_session();
1122 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1123 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1128 * Since we are running in 'demo mode' this call with generate a
1129 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1130 * the SCSI Initiator port name of the passed configfs group 'name'.
1132 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1133 se_tpg
, (unsigned char *)name
);
1134 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1135 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1136 pr_debug("core_tpg_check_initiator_node_acl() failed"
1138 transport_free_session(tv_nexus
->tvn_se_sess
);
1143 * Now register the TCM vhost virtual I_T Nexus as active with the
1144 * call to __transport_register_session()
1146 __transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1147 tv_nexus
->tvn_se_sess
, tv_nexus
);
1148 tv_tpg
->tpg_nexus
= tv_nexus
;
1150 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1154 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg
*tpg
)
1156 struct se_session
*se_sess
;
1157 struct tcm_vhost_nexus
*tv_nexus
;
1159 mutex_lock(&tpg
->tv_tpg_mutex
);
1160 tv_nexus
= tpg
->tpg_nexus
;
1162 mutex_unlock(&tpg
->tv_tpg_mutex
);
1166 se_sess
= tv_nexus
->tvn_se_sess
;
1168 mutex_unlock(&tpg
->tv_tpg_mutex
);
1172 if (tpg
->tv_tpg_port_count
!= 0) {
1173 mutex_unlock(&tpg
->tv_tpg_mutex
);
1174 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1175 " active TPG port count: %d\n",
1176 tpg
->tv_tpg_port_count
);
1180 if (tpg
->tv_tpg_vhost_count
!= 0) {
1181 mutex_unlock(&tpg
->tv_tpg_mutex
);
1182 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1183 " active TPG vhost count: %d\n",
1184 tpg
->tv_tpg_vhost_count
);
1188 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1189 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg
->tport
),
1190 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1192 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1194 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1195 tpg
->tpg_nexus
= NULL
;
1196 mutex_unlock(&tpg
->tv_tpg_mutex
);
1202 static ssize_t
tcm_vhost_tpg_show_nexus(struct se_portal_group
*se_tpg
,
1205 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1206 struct tcm_vhost_tpg
, se_tpg
);
1207 struct tcm_vhost_nexus
*tv_nexus
;
1210 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1211 tv_nexus
= tv_tpg
->tpg_nexus
;
1213 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1216 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1217 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1218 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1223 static ssize_t
tcm_vhost_tpg_store_nexus(struct se_portal_group
*se_tpg
,
1227 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1228 struct tcm_vhost_tpg
, se_tpg
);
1229 struct tcm_vhost_tport
*tport_wwn
= tv_tpg
->tport
;
1230 unsigned char i_port
[TCM_VHOST_NAMELEN
], *ptr
, *port_ptr
;
1233 * Shutdown the active I_T nexus if 'NULL' is passed..
1235 if (!strncmp(page
, "NULL", 4)) {
1236 ret
= tcm_vhost_drop_nexus(tv_tpg
);
1237 return (!ret
) ? count
: ret
;
1240 * Otherwise make sure the passed virtual Initiator port WWN matches
1241 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1242 * tcm_vhost_make_nexus().
1244 if (strlen(page
) >= TCM_VHOST_NAMELEN
) {
1245 pr_err("Emulated NAA Sas Address: %s, exceeds"
1246 " max: %d\n", page
, TCM_VHOST_NAMELEN
);
1249 snprintf(&i_port
[0], TCM_VHOST_NAMELEN
, "%s", page
);
1251 ptr
= strstr(i_port
, "naa.");
1253 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1254 pr_err("Passed SAS Initiator Port %s does not"
1255 " match target port protoid: %s\n", i_port
,
1256 tcm_vhost_dump_proto_id(tport_wwn
));
1259 port_ptr
= &i_port
[0];
1262 ptr
= strstr(i_port
, "fc.");
1264 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1265 pr_err("Passed FCP Initiator Port %s does not"
1266 " match target port protoid: %s\n", i_port
,
1267 tcm_vhost_dump_proto_id(tport_wwn
));
1270 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1273 ptr
= strstr(i_port
, "iqn.");
1275 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1276 pr_err("Passed iSCSI Initiator Port %s does not"
1277 " match target port protoid: %s\n", i_port
,
1278 tcm_vhost_dump_proto_id(tport_wwn
));
1281 port_ptr
= &i_port
[0];
1284 pr_err("Unable to locate prefix for emulated Initiator Port:"
1288 * Clear any trailing newline for the NAA WWN
1291 if (i_port
[strlen(i_port
)-1] == '\n')
1292 i_port
[strlen(i_port
)-1] = '\0';
1294 ret
= tcm_vhost_make_nexus(tv_tpg
, port_ptr
);
1301 TF_TPG_BASE_ATTR(tcm_vhost
, nexus
, S_IRUGO
| S_IWUSR
);
1303 static struct configfs_attribute
*tcm_vhost_tpg_attrs
[] = {
1304 &tcm_vhost_tpg_nexus
.attr
,
1308 static struct se_portal_group
*tcm_vhost_make_tpg(struct se_wwn
*wwn
,
1309 struct config_group
*group
,
1312 struct tcm_vhost_tport
*tport
= container_of(wwn
,
1313 struct tcm_vhost_tport
, tport_wwn
);
1315 struct tcm_vhost_tpg
*tpg
;
1319 if (strstr(name
, "tpgt_") != name
)
1320 return ERR_PTR(-EINVAL
);
1321 if (kstrtoul(name
+ 5, 10, &tpgt
) || tpgt
> UINT_MAX
)
1322 return ERR_PTR(-EINVAL
);
1324 tpg
= kzalloc(sizeof(struct tcm_vhost_tpg
), GFP_KERNEL
);
1326 pr_err("Unable to allocate struct tcm_vhost_tpg");
1327 return ERR_PTR(-ENOMEM
);
1329 mutex_init(&tpg
->tv_tpg_mutex
);
1330 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1332 tpg
->tport_tpgt
= tpgt
;
1334 ret
= core_tpg_register(&tcm_vhost_fabric_configfs
->tf_ops
, wwn
,
1335 &tpg
->se_tpg
, tpg
, TRANSPORT_TPG_TYPE_NORMAL
);
1340 mutex_lock(&tcm_vhost_mutex
);
1341 list_add_tail(&tpg
->tv_tpg_list
, &tcm_vhost_list
);
1342 mutex_unlock(&tcm_vhost_mutex
);
1344 return &tpg
->se_tpg
;
1347 static void tcm_vhost_drop_tpg(struct se_portal_group
*se_tpg
)
1349 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1350 struct tcm_vhost_tpg
, se_tpg
);
1352 mutex_lock(&tcm_vhost_mutex
);
1353 list_del(&tpg
->tv_tpg_list
);
1354 mutex_unlock(&tcm_vhost_mutex
);
1356 * Release the virtual I_T Nexus for this vhost TPG
1358 tcm_vhost_drop_nexus(tpg
);
1360 * Deregister the se_tpg from TCM..
1362 core_tpg_deregister(se_tpg
);
1366 static struct se_wwn
*tcm_vhost_make_tport(struct target_fabric_configfs
*tf
,
1367 struct config_group
*group
,
1370 struct tcm_vhost_tport
*tport
;
1375 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1376 return ERR_PTR(-EINVAL); */
1378 tport
= kzalloc(sizeof(struct tcm_vhost_tport
), GFP_KERNEL
);
1380 pr_err("Unable to allocate struct tcm_vhost_tport");
1381 return ERR_PTR(-ENOMEM
);
1383 tport
->tport_wwpn
= wwpn
;
1385 * Determine the emulated Protocol Identifier and Target Port Name
1386 * based on the incoming configfs directory name.
1388 ptr
= strstr(name
, "naa.");
1390 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
1393 ptr
= strstr(name
, "fc.");
1395 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
1396 off
= 3; /* Skip over "fc." */
1399 ptr
= strstr(name
, "iqn.");
1401 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
1405 pr_err("Unable to locate prefix for emulated Target Port:"
1408 return ERR_PTR(-EINVAL
);
1411 if (strlen(name
) >= TCM_VHOST_NAMELEN
) {
1412 pr_err("Emulated %s Address: %s, exceeds"
1413 " max: %d\n", name
, tcm_vhost_dump_proto_id(tport
),
1416 return ERR_PTR(-EINVAL
);
1418 snprintf(&tport
->tport_name
[0], TCM_VHOST_NAMELEN
, "%s", &name
[off
]);
1420 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1421 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
), name
);
1423 return &tport
->tport_wwn
;
1426 static void tcm_vhost_drop_tport(struct se_wwn
*wwn
)
1428 struct tcm_vhost_tport
*tport
= container_of(wwn
,
1429 struct tcm_vhost_tport
, tport_wwn
);
1431 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1432 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
),
1438 static ssize_t
tcm_vhost_wwn_show_attr_version(
1439 struct target_fabric_configfs
*tf
,
1442 return sprintf(page
, "TCM_VHOST fabric module %s on %s/%s"
1443 "on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
1444 utsname()->machine
);
1447 TF_WWN_ATTR_RO(tcm_vhost
, version
);
1449 static struct configfs_attribute
*tcm_vhost_wwn_attrs
[] = {
1450 &tcm_vhost_wwn_version
.attr
,
1454 static struct target_core_fabric_ops tcm_vhost_ops
= {
1455 .get_fabric_name
= tcm_vhost_get_fabric_name
,
1456 .get_fabric_proto_ident
= tcm_vhost_get_fabric_proto_ident
,
1457 .tpg_get_wwn
= tcm_vhost_get_fabric_wwn
,
1458 .tpg_get_tag
= tcm_vhost_get_tag
,
1459 .tpg_get_default_depth
= tcm_vhost_get_default_depth
,
1460 .tpg_get_pr_transport_id
= tcm_vhost_get_pr_transport_id
,
1461 .tpg_get_pr_transport_id_len
= tcm_vhost_get_pr_transport_id_len
,
1462 .tpg_parse_pr_out_transport_id
= tcm_vhost_parse_pr_out_transport_id
,
1463 .tpg_check_demo_mode
= tcm_vhost_check_true
,
1464 .tpg_check_demo_mode_cache
= tcm_vhost_check_true
,
1465 .tpg_check_demo_mode_write_protect
= tcm_vhost_check_false
,
1466 .tpg_check_prod_mode_write_protect
= tcm_vhost_check_false
,
1467 .tpg_alloc_fabric_acl
= tcm_vhost_alloc_fabric_acl
,
1468 .tpg_release_fabric_acl
= tcm_vhost_release_fabric_acl
,
1469 .tpg_get_inst_index
= tcm_vhost_tpg_get_inst_index
,
1470 .release_cmd
= tcm_vhost_release_cmd
,
1471 .shutdown_session
= tcm_vhost_shutdown_session
,
1472 .close_session
= tcm_vhost_close_session
,
1473 .sess_get_index
= tcm_vhost_sess_get_index
,
1474 .sess_get_initiator_sid
= NULL
,
1475 .write_pending
= tcm_vhost_write_pending
,
1476 .write_pending_status
= tcm_vhost_write_pending_status
,
1477 .set_default_node_attributes
= tcm_vhost_set_default_node_attrs
,
1478 .get_task_tag
= tcm_vhost_get_task_tag
,
1479 .get_cmd_state
= tcm_vhost_get_cmd_state
,
1480 .queue_data_in
= tcm_vhost_queue_data_in
,
1481 .queue_status
= tcm_vhost_queue_status
,
1482 .queue_tm_rsp
= tcm_vhost_queue_tm_rsp
,
1484 * Setup callers for generic logic in target_core_fabric_configfs.c
1486 .fabric_make_wwn
= tcm_vhost_make_tport
,
1487 .fabric_drop_wwn
= tcm_vhost_drop_tport
,
1488 .fabric_make_tpg
= tcm_vhost_make_tpg
,
1489 .fabric_drop_tpg
= tcm_vhost_drop_tpg
,
1490 .fabric_post_link
= tcm_vhost_port_link
,
1491 .fabric_pre_unlink
= tcm_vhost_port_unlink
,
1492 .fabric_make_np
= NULL
,
1493 .fabric_drop_np
= NULL
,
1494 .fabric_make_nodeacl
= tcm_vhost_make_nodeacl
,
1495 .fabric_drop_nodeacl
= tcm_vhost_drop_nodeacl
,
1498 static int tcm_vhost_register_configfs(void)
1500 struct target_fabric_configfs
*fabric
;
1503 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1504 " on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
1505 utsname()->machine
);
1507 * Register the top level struct config_item_type with TCM core
1509 fabric
= target_fabric_configfs_init(THIS_MODULE
, "vhost");
1510 if (IS_ERR(fabric
)) {
1511 pr_err("target_fabric_configfs_init() failed\n");
1512 return PTR_ERR(fabric
);
1515 * Setup fabric->tf_ops from our local tcm_vhost_ops
1517 fabric
->tf_ops
= tcm_vhost_ops
;
1519 * Setup default attribute lists for various fabric->tf_cit_tmpl
1521 TF_CIT_TMPL(fabric
)->tfc_wwn_cit
.ct_attrs
= tcm_vhost_wwn_attrs
;
1522 TF_CIT_TMPL(fabric
)->tfc_tpg_base_cit
.ct_attrs
= tcm_vhost_tpg_attrs
;
1523 TF_CIT_TMPL(fabric
)->tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
1524 TF_CIT_TMPL(fabric
)->tfc_tpg_param_cit
.ct_attrs
= NULL
;
1525 TF_CIT_TMPL(fabric
)->tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
1526 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_base_cit
.ct_attrs
= NULL
;
1527 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
1528 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
1529 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
1531 * Register the fabric for use within TCM
1533 ret
= target_fabric_configfs_register(fabric
);
1535 pr_err("target_fabric_configfs_register() failed"
1536 " for TCM_VHOST\n");
1540 * Setup our local pointer to *fabric
1542 tcm_vhost_fabric_configfs
= fabric
;
1543 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1547 static void tcm_vhost_deregister_configfs(void)
1549 if (!tcm_vhost_fabric_configfs
)
1552 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs
);
1553 tcm_vhost_fabric_configfs
= NULL
;
1554 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1557 static int __init
tcm_vhost_init(void)
1561 * Use our own dedicated workqueue for submitting I/O into
1562 * target core to avoid contention within system_wq.
1564 tcm_vhost_workqueue
= alloc_workqueue("tcm_vhost", 0, 0);
1565 if (!tcm_vhost_workqueue
)
1568 ret
= vhost_scsi_register();
1570 goto out_destroy_workqueue
;
1572 ret
= tcm_vhost_register_configfs();
1574 goto out_vhost_scsi_deregister
;
1578 out_vhost_scsi_deregister
:
1579 vhost_scsi_deregister();
1580 out_destroy_workqueue
:
1581 destroy_workqueue(tcm_vhost_workqueue
);
1586 static void tcm_vhost_exit(void)
1588 tcm_vhost_deregister_configfs();
1589 vhost_scsi_deregister();
1590 destroy_workqueue(tcm_vhost_workqueue
);
1593 MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1594 MODULE_LICENSE("GPL");
1595 module_init(tcm_vhost_init
);
1596 module_exit(tcm_vhost_exit
);