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>
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_net.h> /* TODO vhost.h currently depends on this */
49 #include <linux/virtio_scsi.h>
50 #include <linux/llist.h>
51 #include <linux/bitmap.h>
55 #include "tcm_vhost.h"
58 VHOST_SCSI_VQ_CTL
= 0,
59 VHOST_SCSI_VQ_EVT
= 1,
64 * VIRTIO_RING_F_EVENT_IDX seems broken. Not sure the bug is in
65 * kernel but disabling it helps.
66 * TODO: debug and remove the workaround.
69 VHOST_SCSI_FEATURES
= VHOST_FEATURES
& (~VIRTIO_RING_F_EVENT_IDX
)
72 #define VHOST_SCSI_MAX_TARGET 256
73 #define VHOST_SCSI_MAX_VQ 128
76 /* Protected by vhost_scsi->dev.mutex */
77 struct tcm_vhost_tpg
*vs_tpg
[VHOST_SCSI_MAX_TARGET
];
78 char vs_vhost_wwpn
[TRANSPORT_IQN_LEN
];
82 struct vhost_virtqueue vqs
[VHOST_SCSI_MAX_VQ
];
84 struct vhost_work vs_completion_work
; /* cmd completion work item */
85 struct llist_head vs_completion_list
; /* cmd completion queue */
88 /* Local pointer to allocated TCM configfs fabric module */
89 static struct target_fabric_configfs
*tcm_vhost_fabric_configfs
;
91 static struct workqueue_struct
*tcm_vhost_workqueue
;
93 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
94 static DEFINE_MUTEX(tcm_vhost_mutex
);
95 static LIST_HEAD(tcm_vhost_list
);
97 static int iov_num_pages(struct iovec
*iov
)
99 return (PAGE_ALIGN((unsigned long)iov
->iov_base
+ iov
->iov_len
) -
100 ((unsigned long)iov
->iov_base
& PAGE_MASK
)) >> PAGE_SHIFT
;
103 static int tcm_vhost_check_true(struct se_portal_group
*se_tpg
)
108 static int tcm_vhost_check_false(struct se_portal_group
*se_tpg
)
113 static char *tcm_vhost_get_fabric_name(void)
118 static u8
tcm_vhost_get_fabric_proto_ident(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 switch (tport
->tport_proto_id
) {
125 case SCSI_PROTOCOL_SAS
:
126 return sas_get_fabric_proto_ident(se_tpg
);
127 case SCSI_PROTOCOL_FCP
:
128 return fc_get_fabric_proto_ident(se_tpg
);
129 case SCSI_PROTOCOL_ISCSI
:
130 return iscsi_get_fabric_proto_ident(se_tpg
);
132 pr_err("Unknown tport_proto_id: 0x%02x, using"
133 " SAS emulation\n", tport
->tport_proto_id
);
137 return sas_get_fabric_proto_ident(se_tpg
);
140 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group
*se_tpg
)
142 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
143 struct tcm_vhost_tpg
, se_tpg
);
144 struct tcm_vhost_tport
*tport
= tpg
->tport
;
146 return &tport
->tport_name
[0];
149 static u16
tcm_vhost_get_tag(struct se_portal_group
*se_tpg
)
151 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
152 struct tcm_vhost_tpg
, se_tpg
);
153 return tpg
->tport_tpgt
;
156 static u32
tcm_vhost_get_default_depth(struct se_portal_group
*se_tpg
)
161 static u32
tcm_vhost_get_pr_transport_id(struct se_portal_group
*se_tpg
,
162 struct se_node_acl
*se_nacl
,
163 struct t10_pr_registration
*pr_reg
,
167 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
168 struct tcm_vhost_tpg
, se_tpg
);
169 struct tcm_vhost_tport
*tport
= tpg
->tport
;
171 switch (tport
->tport_proto_id
) {
172 case SCSI_PROTOCOL_SAS
:
173 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
175 case SCSI_PROTOCOL_FCP
:
176 return fc_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
178 case SCSI_PROTOCOL_ISCSI
:
179 return iscsi_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
182 pr_err("Unknown tport_proto_id: 0x%02x, using"
183 " SAS emulation\n", tport
->tport_proto_id
);
187 return sas_get_pr_transport_id(se_tpg
, se_nacl
, pr_reg
,
191 static u32
tcm_vhost_get_pr_transport_id_len(struct se_portal_group
*se_tpg
,
192 struct se_node_acl
*se_nacl
,
193 struct t10_pr_registration
*pr_reg
,
196 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
197 struct tcm_vhost_tpg
, se_tpg
);
198 struct tcm_vhost_tport
*tport
= tpg
->tport
;
200 switch (tport
->tport_proto_id
) {
201 case SCSI_PROTOCOL_SAS
:
202 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
204 case SCSI_PROTOCOL_FCP
:
205 return fc_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
207 case SCSI_PROTOCOL_ISCSI
:
208 return iscsi_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
211 pr_err("Unknown tport_proto_id: 0x%02x, using"
212 " SAS emulation\n", tport
->tport_proto_id
);
216 return sas_get_pr_transport_id_len(se_tpg
, se_nacl
, pr_reg
,
220 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group
*se_tpg
,
223 char **port_nexus_ptr
)
225 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
226 struct tcm_vhost_tpg
, se_tpg
);
227 struct tcm_vhost_tport
*tport
= tpg
->tport
;
229 switch (tport
->tport_proto_id
) {
230 case SCSI_PROTOCOL_SAS
:
231 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
233 case SCSI_PROTOCOL_FCP
:
234 return fc_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
236 case SCSI_PROTOCOL_ISCSI
:
237 return iscsi_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
240 pr_err("Unknown tport_proto_id: 0x%02x, using"
241 " SAS emulation\n", tport
->tport_proto_id
);
245 return sas_parse_pr_out_transport_id(se_tpg
, buf
, out_tid_len
,
249 static struct se_node_acl
*tcm_vhost_alloc_fabric_acl(
250 struct se_portal_group
*se_tpg
)
252 struct tcm_vhost_nacl
*nacl
;
254 nacl
= kzalloc(sizeof(struct tcm_vhost_nacl
), GFP_KERNEL
);
256 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
260 return &nacl
->se_node_acl
;
263 static void tcm_vhost_release_fabric_acl(struct se_portal_group
*se_tpg
,
264 struct se_node_acl
*se_nacl
)
266 struct tcm_vhost_nacl
*nacl
= container_of(se_nacl
,
267 struct tcm_vhost_nacl
, se_node_acl
);
271 static u32
tcm_vhost_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
276 static void tcm_vhost_release_cmd(struct se_cmd
*se_cmd
)
281 static int tcm_vhost_shutdown_session(struct se_session
*se_sess
)
286 static void tcm_vhost_close_session(struct se_session
*se_sess
)
291 static u32
tcm_vhost_sess_get_index(struct se_session
*se_sess
)
296 static int tcm_vhost_write_pending(struct se_cmd
*se_cmd
)
298 /* Go ahead and process the write immediately */
299 target_execute_cmd(se_cmd
);
303 static int tcm_vhost_write_pending_status(struct se_cmd
*se_cmd
)
308 static void tcm_vhost_set_default_node_attrs(struct se_node_acl
*nacl
)
313 static u32
tcm_vhost_get_task_tag(struct se_cmd
*se_cmd
)
318 static int tcm_vhost_get_cmd_state(struct se_cmd
*se_cmd
)
323 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd
*tv_cmd
)
325 struct vhost_scsi
*vs
= tv_cmd
->tvc_vhost
;
327 llist_add(&tv_cmd
->tvc_completion_list
, &vs
->vs_completion_list
);
329 vhost_work_queue(&vs
->dev
, &vs
->vs_completion_work
);
332 static int tcm_vhost_queue_data_in(struct se_cmd
*se_cmd
)
334 struct tcm_vhost_cmd
*tv_cmd
= container_of(se_cmd
,
335 struct tcm_vhost_cmd
, tvc_se_cmd
);
336 vhost_scsi_complete_cmd(tv_cmd
);
340 static int tcm_vhost_queue_status(struct se_cmd
*se_cmd
)
342 struct tcm_vhost_cmd
*tv_cmd
= container_of(se_cmd
,
343 struct tcm_vhost_cmd
, tvc_se_cmd
);
344 vhost_scsi_complete_cmd(tv_cmd
);
348 static int tcm_vhost_queue_tm_rsp(struct se_cmd
*se_cmd
)
353 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd
*tv_cmd
)
355 struct se_cmd
*se_cmd
= &tv_cmd
->tvc_se_cmd
;
357 /* TODO locking against target/backend threads? */
358 transport_generic_free_cmd(se_cmd
, 1);
360 if (tv_cmd
->tvc_sgl_count
) {
362 for (i
= 0; i
< tv_cmd
->tvc_sgl_count
; i
++)
363 put_page(sg_page(&tv_cmd
->tvc_sgl
[i
]));
365 kfree(tv_cmd
->tvc_sgl
);
371 /* Fill in status and signal that we are done processing this command
373 * This is scheduled in the vhost work queue so we are called with the owner
374 * process mm and can access the vring.
376 static void vhost_scsi_complete_cmd_work(struct vhost_work
*work
)
378 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
380 DECLARE_BITMAP(signal
, VHOST_SCSI_MAX_VQ
);
381 struct virtio_scsi_cmd_resp v_rsp
;
382 struct tcm_vhost_cmd
*tv_cmd
;
383 struct llist_node
*llnode
;
384 struct se_cmd
*se_cmd
;
387 bitmap_zero(signal
, VHOST_SCSI_MAX_VQ
);
388 llnode
= llist_del_all(&vs
->vs_completion_list
);
390 tv_cmd
= llist_entry(llnode
, struct tcm_vhost_cmd
,
391 tvc_completion_list
);
392 llnode
= llist_next(llnode
);
393 se_cmd
= &tv_cmd
->tvc_se_cmd
;
395 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__
,
396 tv_cmd
, se_cmd
->residual_count
, se_cmd
->scsi_status
);
398 memset(&v_rsp
, 0, sizeof(v_rsp
));
399 v_rsp
.resid
= se_cmd
->residual_count
;
400 /* TODO is status_qualifier field needed? */
401 v_rsp
.status
= se_cmd
->scsi_status
;
402 v_rsp
.sense_len
= se_cmd
->scsi_sense_length
;
403 memcpy(v_rsp
.sense
, tv_cmd
->tvc_sense_buf
,
405 ret
= copy_to_user(tv_cmd
->tvc_resp
, &v_rsp
, sizeof(v_rsp
));
406 if (likely(ret
== 0)) {
407 vhost_add_used(tv_cmd
->tvc_vq
, tv_cmd
->tvc_vq_desc
, 0);
408 vq
= tv_cmd
->tvc_vq
- vs
->vqs
;
409 __set_bit(vq
, signal
);
411 pr_err("Faulted on virtio_scsi_cmd_resp\n");
413 vhost_scsi_free_cmd(tv_cmd
);
417 while ((vq
= find_next_bit(signal
, VHOST_SCSI_MAX_VQ
, vq
+ 1))
419 vhost_signal(&vs
->dev
, &vs
->vqs
[vq
]);
422 static struct tcm_vhost_cmd
*vhost_scsi_allocate_cmd(
423 struct tcm_vhost_tpg
*tv_tpg
,
424 struct virtio_scsi_cmd_req
*v_req
,
428 struct tcm_vhost_cmd
*tv_cmd
;
429 struct tcm_vhost_nexus
*tv_nexus
;
431 tv_nexus
= tv_tpg
->tpg_nexus
;
433 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
434 return ERR_PTR(-EIO
);
437 tv_cmd
= kzalloc(sizeof(struct tcm_vhost_cmd
), GFP_ATOMIC
);
439 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
440 return ERR_PTR(-ENOMEM
);
442 tv_cmd
->tvc_tag
= v_req
->tag
;
443 tv_cmd
->tvc_task_attr
= v_req
->task_attr
;
444 tv_cmd
->tvc_exp_data_len
= exp_data_len
;
445 tv_cmd
->tvc_data_direction
= data_direction
;
446 tv_cmd
->tvc_nexus
= tv_nexus
;
452 * Map a user memory range into a scatterlist
454 * Returns the number of scatterlist entries used or -errno on error.
456 static int vhost_scsi_map_to_sgl(struct scatterlist
*sgl
,
457 unsigned int sgl_count
, struct iovec
*iov
, int write
)
459 unsigned int npages
= 0, pages_nr
, offset
, nbytes
;
460 struct scatterlist
*sg
= sgl
;
461 void __user
*ptr
= iov
->iov_base
;
462 size_t len
= iov
->iov_len
;
466 pages_nr
= iov_num_pages(iov
);
467 if (pages_nr
> sgl_count
)
470 pages
= kmalloc(pages_nr
* sizeof(struct page
*), GFP_KERNEL
);
474 ret
= get_user_pages_fast((unsigned long)ptr
, pages_nr
, write
, pages
);
475 /* No pages were pinned */
478 /* Less pages pinned than wanted */
479 if (ret
!= pages_nr
) {
480 for (i
= 0; i
< ret
; i
++)
487 offset
= (uintptr_t)ptr
& ~PAGE_MASK
;
488 nbytes
= min_t(unsigned int, PAGE_SIZE
- offset
, len
);
489 sg_set_page(sg
, pages
[npages
], nbytes
, offset
);
501 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd
*tv_cmd
,
502 struct iovec
*iov
, unsigned int niov
, int write
)
507 struct scatterlist
*sg
;
510 * Find out how long sglist needs to be
513 for (i
= 0; i
< niov
; i
++)
514 sgl_count
+= iov_num_pages(&iov
[i
]);
516 /* TODO overflow checking */
518 sg
= kmalloc(sizeof(tv_cmd
->tvc_sgl
[0]) * sgl_count
, GFP_ATOMIC
);
521 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__
,
523 sg_init_table(sg
, sgl_count
);
525 tv_cmd
->tvc_sgl
= sg
;
526 tv_cmd
->tvc_sgl_count
= sgl_count
;
528 pr_debug("Mapping %u iovecs for %u pages\n", niov
, sgl_count
);
529 for (i
= 0; i
< niov
; i
++) {
530 ret
= vhost_scsi_map_to_sgl(sg
, sgl_count
, &iov
[i
], write
);
532 for (i
= 0; i
< tv_cmd
->tvc_sgl_count
; i
++)
533 put_page(sg_page(&tv_cmd
->tvc_sgl
[i
]));
534 kfree(tv_cmd
->tvc_sgl
);
535 tv_cmd
->tvc_sgl
= NULL
;
536 tv_cmd
->tvc_sgl_count
= 0;
546 static void tcm_vhost_submission_work(struct work_struct
*work
)
548 struct tcm_vhost_cmd
*tv_cmd
=
549 container_of(work
, struct tcm_vhost_cmd
, work
);
550 struct tcm_vhost_nexus
*tv_nexus
;
551 struct se_cmd
*se_cmd
= &tv_cmd
->tvc_se_cmd
;
552 struct scatterlist
*sg_ptr
, *sg_bidi_ptr
= NULL
;
553 int rc
, sg_no_bidi
= 0;
555 if (tv_cmd
->tvc_sgl_count
) {
556 sg_ptr
= tv_cmd
->tvc_sgl
;
557 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
559 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
567 tv_nexus
= tv_cmd
->tvc_nexus
;
569 rc
= target_submit_cmd_map_sgls(se_cmd
, tv_nexus
->tvn_se_sess
,
570 tv_cmd
->tvc_cdb
, &tv_cmd
->tvc_sense_buf
[0],
571 tv_cmd
->tvc_lun
, tv_cmd
->tvc_exp_data_len
,
572 tv_cmd
->tvc_task_attr
, tv_cmd
->tvc_data_direction
,
573 0, sg_ptr
, tv_cmd
->tvc_sgl_count
,
574 sg_bidi_ptr
, sg_no_bidi
);
576 transport_send_check_condition_and_sense(se_cmd
,
577 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
578 transport_generic_free_cmd(se_cmd
, 0);
582 static void vhost_scsi_handle_vq(struct vhost_scsi
*vs
,
583 struct vhost_virtqueue
*vq
)
585 struct virtio_scsi_cmd_req v_req
;
586 struct tcm_vhost_tpg
*tv_tpg
;
587 struct tcm_vhost_cmd
*tv_cmd
;
588 u32 exp_data_len
, data_first
, data_num
, data_direction
;
593 /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
594 if (unlikely(!vs
->vs_endpoint
))
597 mutex_lock(&vq
->mutex
);
598 vhost_disable_notify(&vs
->dev
, vq
);
601 head
= vhost_get_vq_desc(&vs
->dev
, vq
, vq
->iov
,
602 ARRAY_SIZE(vq
->iov
), &out
, &in
,
604 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
606 /* On error, stop handling until the next kick. */
607 if (unlikely(head
< 0))
609 /* Nothing new? Wait for eventfd to tell us they refilled. */
610 if (head
== vq
->num
) {
611 if (unlikely(vhost_enable_notify(&vs
->dev
, vq
))) {
612 vhost_disable_notify(&vs
->dev
, vq
);
618 /* FIXME: BIDI operation */
619 if (out
== 1 && in
== 1) {
620 data_direction
= DMA_NONE
;
623 } else if (out
== 1 && in
> 1) {
624 data_direction
= DMA_FROM_DEVICE
;
625 data_first
= out
+ 1;
627 } else if (out
> 1 && in
== 1) {
628 data_direction
= DMA_TO_DEVICE
;
632 vq_err(vq
, "Invalid buffer layout out: %u in: %u\n",
638 * Check for a sane resp buffer so we can report errors to
641 if (unlikely(vq
->iov
[out
].iov_len
!=
642 sizeof(struct virtio_scsi_cmd_resp
))) {
643 vq_err(vq
, "Expecting virtio_scsi_cmd_resp, got %zu"
644 " bytes\n", vq
->iov
[out
].iov_len
);
648 if (unlikely(vq
->iov
[0].iov_len
!= sizeof(v_req
))) {
649 vq_err(vq
, "Expecting virtio_scsi_cmd_req, got %zu"
650 " bytes\n", vq
->iov
[0].iov_len
);
653 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
654 " len: %zu\n", vq
->iov
[0].iov_base
, sizeof(v_req
));
655 ret
= __copy_from_user(&v_req
, vq
->iov
[0].iov_base
,
658 vq_err(vq
, "Faulted on virtio_scsi_cmd_req\n");
662 /* Extract the tpgt */
663 target
= v_req
.lun
[1];
664 tv_tpg
= vs
->vs_tpg
[target
];
666 /* Target does not exist, fail the request */
667 if (unlikely(!tv_tpg
)) {
668 struct virtio_scsi_cmd_resp __user
*resp
;
669 struct virtio_scsi_cmd_resp rsp
;
671 memset(&rsp
, 0, sizeof(rsp
));
672 rsp
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
673 resp
= vq
->iov
[out
].iov_base
;
674 ret
= __copy_to_user(resp
, &rsp
, sizeof(rsp
));
676 vhost_add_used_and_signal(&vs
->dev
,
679 pr_err("Faulted on virtio_scsi_cmd_resp\n");
685 for (i
= 0; i
< data_num
; i
++)
686 exp_data_len
+= vq
->iov
[data_first
+ i
].iov_len
;
688 tv_cmd
= vhost_scsi_allocate_cmd(tv_tpg
, &v_req
,
689 exp_data_len
, data_direction
);
690 if (IS_ERR(tv_cmd
)) {
691 vq_err(vq
, "vhost_scsi_allocate_cmd failed %ld\n",
695 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
696 ": %d\n", tv_cmd
, exp_data_len
, data_direction
);
698 tv_cmd
->tvc_vhost
= vs
;
701 if (unlikely(vq
->iov
[out
].iov_len
!=
702 sizeof(struct virtio_scsi_cmd_resp
))) {
703 vq_err(vq
, "Expecting virtio_scsi_cmd_resp, got %zu"
704 " bytes, out: %d, in: %d\n",
705 vq
->iov
[out
].iov_len
, out
, in
);
709 tv_cmd
->tvc_resp
= vq
->iov
[out
].iov_base
;
712 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
713 * that will be used by tcm_vhost_new_cmd_map() and down into
714 * target_setup_cmd_from_cdb()
716 memcpy(tv_cmd
->tvc_cdb
, v_req
.cdb
, TCM_VHOST_MAX_CDB_SIZE
);
718 * Check that the recieved CDB size does not exceeded our
719 * hardcoded max for tcm_vhost
721 /* TODO what if cdb was too small for varlen cdb header? */
722 if (unlikely(scsi_command_size(tv_cmd
->tvc_cdb
) >
723 TCM_VHOST_MAX_CDB_SIZE
)) {
724 vq_err(vq
, "Received SCSI CDB with command_size: %d that"
725 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
726 scsi_command_size(tv_cmd
->tvc_cdb
),
727 TCM_VHOST_MAX_CDB_SIZE
);
730 tv_cmd
->tvc_lun
= ((v_req
.lun
[2] << 8) | v_req
.lun
[3]) & 0x3FFF;
732 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
733 tv_cmd
->tvc_cdb
[0], tv_cmd
->tvc_lun
);
735 if (data_direction
!= DMA_NONE
) {
736 ret
= vhost_scsi_map_iov_to_sgl(tv_cmd
,
737 &vq
->iov
[data_first
], data_num
,
738 data_direction
== DMA_TO_DEVICE
);
740 vq_err(vq
, "Failed to map iov to sgl\n");
746 * Save the descriptor from vhost_get_vq_desc() to be used to
747 * complete the virtio-scsi request in TCM callback context via
748 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
750 tv_cmd
->tvc_vq_desc
= head
;
752 * Dispatch tv_cmd descriptor for cmwq execution in process
753 * context provided by tcm_vhost_workqueue. This also ensures
754 * tv_cmd is executed on the same kworker CPU as this vhost
755 * thread to gain positive L2 cache locality effects..
757 INIT_WORK(&tv_cmd
->work
, tcm_vhost_submission_work
);
758 queue_work(tcm_vhost_workqueue
, &tv_cmd
->work
);
761 mutex_unlock(&vq
->mutex
);
764 static void vhost_scsi_ctl_handle_kick(struct vhost_work
*work
)
766 pr_debug("%s: The handling func for control queue.\n", __func__
);
769 static void vhost_scsi_evt_handle_kick(struct vhost_work
*work
)
771 pr_debug("%s: The handling func for event queue.\n", __func__
);
774 static void vhost_scsi_handle_kick(struct vhost_work
*work
)
776 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
778 struct vhost_scsi
*vs
= container_of(vq
->dev
, struct vhost_scsi
, dev
);
780 vhost_scsi_handle_vq(vs
, vq
);
784 * Called from vhost_scsi_ioctl() context to walk the list of available
785 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
787 static int vhost_scsi_set_endpoint(
788 struct vhost_scsi
*vs
,
789 struct vhost_scsi_target
*t
)
791 struct tcm_vhost_tport
*tv_tport
;
792 struct tcm_vhost_tpg
*tv_tpg
;
796 mutex_lock(&vs
->dev
.mutex
);
797 /* Verify that ring has been setup correctly. */
798 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
799 /* Verify that ring has been setup correctly. */
800 if (!vhost_vq_access_ok(&vs
->vqs
[index
])) {
801 mutex_unlock(&vs
->dev
.mutex
);
806 mutex_lock(&tcm_vhost_mutex
);
807 list_for_each_entry(tv_tpg
, &tcm_vhost_list
, tv_tpg_list
) {
808 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
809 if (!tv_tpg
->tpg_nexus
) {
810 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
813 if (tv_tpg
->tv_tpg_vhost_count
!= 0) {
814 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
817 tv_tport
= tv_tpg
->tport
;
819 if (!strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
)) {
820 if (vs
->vs_tpg
[tv_tpg
->tport_tpgt
]) {
821 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
822 mutex_unlock(&tcm_vhost_mutex
);
823 mutex_unlock(&vs
->dev
.mutex
);
826 tv_tpg
->tv_tpg_vhost_count
++;
827 vs
->vs_tpg
[tv_tpg
->tport_tpgt
] = tv_tpg
;
828 smp_mb__after_atomic_inc();
831 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
833 mutex_unlock(&tcm_vhost_mutex
);
836 memcpy(vs
->vs_vhost_wwpn
, t
->vhost_wwpn
,
837 sizeof(vs
->vs_vhost_wwpn
));
838 vs
->vs_endpoint
= true;
844 mutex_unlock(&vs
->dev
.mutex
);
848 static int vhost_scsi_clear_endpoint(
849 struct vhost_scsi
*vs
,
850 struct vhost_scsi_target
*t
)
852 struct tcm_vhost_tport
*tv_tport
;
853 struct tcm_vhost_tpg
*tv_tpg
;
857 mutex_lock(&vs
->dev
.mutex
);
858 /* Verify that ring has been setup correctly. */
859 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
860 if (!vhost_vq_access_ok(&vs
->vqs
[index
])) {
865 for (i
= 0; i
< VHOST_SCSI_MAX_TARGET
; i
++) {
868 tv_tpg
= vs
->vs_tpg
[target
];
872 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
873 tv_tport
= tv_tpg
->tport
;
879 if (strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
)) {
880 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
881 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
882 tv_tport
->tport_name
, tv_tpg
->tport_tpgt
,
883 t
->vhost_wwpn
, t
->vhost_tpgt
);
887 tv_tpg
->tv_tpg_vhost_count
--;
888 vs
->vs_tpg
[target
] = NULL
;
889 vs
->vs_endpoint
= false;
890 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
892 mutex_unlock(&vs
->dev
.mutex
);
896 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
898 mutex_unlock(&vs
->dev
.mutex
);
902 static int vhost_scsi_open(struct inode
*inode
, struct file
*f
)
904 struct vhost_scsi
*s
;
907 s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
911 vhost_work_init(&s
->vs_completion_work
, vhost_scsi_complete_cmd_work
);
913 s
->vqs
[VHOST_SCSI_VQ_CTL
].handle_kick
= vhost_scsi_ctl_handle_kick
;
914 s
->vqs
[VHOST_SCSI_VQ_EVT
].handle_kick
= vhost_scsi_evt_handle_kick
;
915 for (i
= VHOST_SCSI_VQ_IO
; i
< VHOST_SCSI_MAX_VQ
; i
++)
916 s
->vqs
[i
].handle_kick
= vhost_scsi_handle_kick
;
917 r
= vhost_dev_init(&s
->dev
, s
->vqs
, VHOST_SCSI_MAX_VQ
);
927 static int vhost_scsi_release(struct inode
*inode
, struct file
*f
)
929 struct vhost_scsi
*s
= f
->private_data
;
930 struct vhost_scsi_target t
;
932 mutex_lock(&s
->dev
.mutex
);
933 memcpy(t
.vhost_wwpn
, s
->vs_vhost_wwpn
, sizeof(t
.vhost_wwpn
));
934 mutex_unlock(&s
->dev
.mutex
);
935 vhost_scsi_clear_endpoint(s
, &t
);
936 vhost_dev_stop(&s
->dev
);
937 vhost_dev_cleanup(&s
->dev
, false);
942 static void vhost_scsi_flush_vq(struct vhost_scsi
*vs
, int index
)
944 vhost_poll_flush(&vs
->dev
.vqs
[index
].poll
);
947 static void vhost_scsi_flush(struct vhost_scsi
*vs
)
951 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
952 vhost_scsi_flush_vq(vs
, i
);
953 vhost_work_flush(&vs
->dev
, &vs
->vs_completion_work
);
956 static int vhost_scsi_set_features(struct vhost_scsi
*vs
, u64 features
)
958 if (features
& ~VHOST_SCSI_FEATURES
)
961 mutex_lock(&vs
->dev
.mutex
);
962 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
963 !vhost_log_access_ok(&vs
->dev
)) {
964 mutex_unlock(&vs
->dev
.mutex
);
967 vs
->dev
.acked_features
= features
;
969 vhost_scsi_flush(vs
);
970 mutex_unlock(&vs
->dev
.mutex
);
974 static long vhost_scsi_ioctl(struct file
*f
, unsigned int ioctl
,
977 struct vhost_scsi
*vs
= f
->private_data
;
978 struct vhost_scsi_target backend
;
979 void __user
*argp
= (void __user
*)arg
;
980 u64 __user
*featurep
= argp
;
982 int r
, abi_version
= VHOST_SCSI_ABI_VERSION
;
985 case VHOST_SCSI_SET_ENDPOINT
:
986 if (copy_from_user(&backend
, argp
, sizeof backend
))
988 if (backend
.reserved
!= 0)
991 return vhost_scsi_set_endpoint(vs
, &backend
);
992 case VHOST_SCSI_CLEAR_ENDPOINT
:
993 if (copy_from_user(&backend
, argp
, sizeof backend
))
995 if (backend
.reserved
!= 0)
998 return vhost_scsi_clear_endpoint(vs
, &backend
);
999 case VHOST_SCSI_GET_ABI_VERSION
:
1000 if (copy_to_user(argp
, &abi_version
, sizeof abi_version
))
1003 case VHOST_GET_FEATURES
:
1004 features
= VHOST_SCSI_FEATURES
;
1005 if (copy_to_user(featurep
, &features
, sizeof features
))
1008 case VHOST_SET_FEATURES
:
1009 if (copy_from_user(&features
, featurep
, sizeof features
))
1011 return vhost_scsi_set_features(vs
, features
);
1013 mutex_lock(&vs
->dev
.mutex
);
1014 r
= vhost_dev_ioctl(&vs
->dev
, ioctl
, argp
);
1015 /* TODO: flush backend after dev ioctl. */
1016 if (r
== -ENOIOCTLCMD
)
1017 r
= vhost_vring_ioctl(&vs
->dev
, ioctl
, argp
);
1018 mutex_unlock(&vs
->dev
.mutex
);
1023 #ifdef CONFIG_COMPAT
1024 static long vhost_scsi_compat_ioctl(struct file
*f
, unsigned int ioctl
,
1027 return vhost_scsi_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
1031 static const struct file_operations vhost_scsi_fops
= {
1032 .owner
= THIS_MODULE
,
1033 .release
= vhost_scsi_release
,
1034 .unlocked_ioctl
= vhost_scsi_ioctl
,
1035 #ifdef CONFIG_COMPAT
1036 .compat_ioctl
= vhost_scsi_compat_ioctl
,
1038 .open
= vhost_scsi_open
,
1039 .llseek
= noop_llseek
,
1042 static struct miscdevice vhost_scsi_misc
= {
1048 static int __init
vhost_scsi_register(void)
1050 return misc_register(&vhost_scsi_misc
);
1053 static int vhost_scsi_deregister(void)
1055 return misc_deregister(&vhost_scsi_misc
);
1058 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport
*tport
)
1060 switch (tport
->tport_proto_id
) {
1061 case SCSI_PROTOCOL_SAS
:
1063 case SCSI_PROTOCOL_FCP
:
1065 case SCSI_PROTOCOL_ISCSI
:
1074 static int tcm_vhost_port_link(struct se_portal_group
*se_tpg
,
1077 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1078 struct tcm_vhost_tpg
, se_tpg
);
1080 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1081 tv_tpg
->tv_tpg_port_count
++;
1082 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1087 static void tcm_vhost_port_unlink(struct se_portal_group
*se_tpg
,
1088 struct se_lun
*se_lun
)
1090 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1091 struct tcm_vhost_tpg
, se_tpg
);
1093 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1094 tv_tpg
->tv_tpg_port_count
--;
1095 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1098 static struct se_node_acl
*tcm_vhost_make_nodeacl(
1099 struct se_portal_group
*se_tpg
,
1100 struct config_group
*group
,
1103 struct se_node_acl
*se_nacl
, *se_nacl_new
;
1104 struct tcm_vhost_nacl
*nacl
;
1108 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1109 return ERR_PTR(-EINVAL); */
1110 se_nacl_new
= tcm_vhost_alloc_fabric_acl(se_tpg
);
1112 return ERR_PTR(-ENOMEM
);
1116 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1117 * when converting a NodeACL from demo mode -> explict
1119 se_nacl
= core_tpg_add_initiator_node_acl(se_tpg
, se_nacl_new
,
1121 if (IS_ERR(se_nacl
)) {
1122 tcm_vhost_release_fabric_acl(se_tpg
, se_nacl_new
);
1126 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1128 nacl
= container_of(se_nacl
, struct tcm_vhost_nacl
, se_node_acl
);
1129 nacl
->iport_wwpn
= wwpn
;
1134 static void tcm_vhost_drop_nodeacl(struct se_node_acl
*se_acl
)
1136 struct tcm_vhost_nacl
*nacl
= container_of(se_acl
,
1137 struct tcm_vhost_nacl
, se_node_acl
);
1138 core_tpg_del_initiator_node_acl(se_acl
->se_tpg
, se_acl
, 1);
1142 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg
*tv_tpg
,
1145 struct se_portal_group
*se_tpg
;
1146 struct tcm_vhost_nexus
*tv_nexus
;
1148 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1149 if (tv_tpg
->tpg_nexus
) {
1150 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1151 pr_debug("tv_tpg->tpg_nexus already exists\n");
1154 se_tpg
= &tv_tpg
->se_tpg
;
1156 tv_nexus
= kzalloc(sizeof(struct tcm_vhost_nexus
), GFP_KERNEL
);
1158 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1159 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1163 * Initialize the struct se_session pointer
1165 tv_nexus
->tvn_se_sess
= transport_init_session();
1166 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1167 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1172 * Since we are running in 'demo mode' this call with generate a
1173 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1174 * the SCSI Initiator port name of the passed configfs group 'name'.
1176 tv_nexus
->tvn_se_sess
->se_node_acl
= core_tpg_check_initiator_node_acl(
1177 se_tpg
, (unsigned char *)name
);
1178 if (!tv_nexus
->tvn_se_sess
->se_node_acl
) {
1179 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1180 pr_debug("core_tpg_check_initiator_node_acl() failed"
1182 transport_free_session(tv_nexus
->tvn_se_sess
);
1187 * Now register the TCM vhost virtual I_T Nexus as active with the
1188 * call to __transport_register_session()
1190 __transport_register_session(se_tpg
, tv_nexus
->tvn_se_sess
->se_node_acl
,
1191 tv_nexus
->tvn_se_sess
, tv_nexus
);
1192 tv_tpg
->tpg_nexus
= tv_nexus
;
1194 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1198 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg
*tpg
)
1200 struct se_session
*se_sess
;
1201 struct tcm_vhost_nexus
*tv_nexus
;
1203 mutex_lock(&tpg
->tv_tpg_mutex
);
1204 tv_nexus
= tpg
->tpg_nexus
;
1206 mutex_unlock(&tpg
->tv_tpg_mutex
);
1210 se_sess
= tv_nexus
->tvn_se_sess
;
1212 mutex_unlock(&tpg
->tv_tpg_mutex
);
1216 if (tpg
->tv_tpg_port_count
!= 0) {
1217 mutex_unlock(&tpg
->tv_tpg_mutex
);
1218 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1219 " active TPG port count: %d\n",
1220 tpg
->tv_tpg_port_count
);
1224 if (tpg
->tv_tpg_vhost_count
!= 0) {
1225 mutex_unlock(&tpg
->tv_tpg_mutex
);
1226 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1227 " active TPG vhost count: %d\n",
1228 tpg
->tv_tpg_vhost_count
);
1232 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1233 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg
->tport
),
1234 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1236 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1238 transport_deregister_session(tv_nexus
->tvn_se_sess
);
1239 tpg
->tpg_nexus
= NULL
;
1240 mutex_unlock(&tpg
->tv_tpg_mutex
);
1246 static ssize_t
tcm_vhost_tpg_show_nexus(struct se_portal_group
*se_tpg
,
1249 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1250 struct tcm_vhost_tpg
, se_tpg
);
1251 struct tcm_vhost_nexus
*tv_nexus
;
1254 mutex_lock(&tv_tpg
->tv_tpg_mutex
);
1255 tv_nexus
= tv_tpg
->tpg_nexus
;
1257 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1260 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1261 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1262 mutex_unlock(&tv_tpg
->tv_tpg_mutex
);
1267 static ssize_t
tcm_vhost_tpg_store_nexus(struct se_portal_group
*se_tpg
,
1271 struct tcm_vhost_tpg
*tv_tpg
= container_of(se_tpg
,
1272 struct tcm_vhost_tpg
, se_tpg
);
1273 struct tcm_vhost_tport
*tport_wwn
= tv_tpg
->tport
;
1274 unsigned char i_port
[TCM_VHOST_NAMELEN
], *ptr
, *port_ptr
;
1277 * Shutdown the active I_T nexus if 'NULL' is passed..
1279 if (!strncmp(page
, "NULL", 4)) {
1280 ret
= tcm_vhost_drop_nexus(tv_tpg
);
1281 return (!ret
) ? count
: ret
;
1284 * Otherwise make sure the passed virtual Initiator port WWN matches
1285 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1286 * tcm_vhost_make_nexus().
1288 if (strlen(page
) >= TCM_VHOST_NAMELEN
) {
1289 pr_err("Emulated NAA Sas Address: %s, exceeds"
1290 " max: %d\n", page
, TCM_VHOST_NAMELEN
);
1293 snprintf(&i_port
[0], TCM_VHOST_NAMELEN
, "%s", page
);
1295 ptr
= strstr(i_port
, "naa.");
1297 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1298 pr_err("Passed SAS Initiator Port %s does not"
1299 " match target port protoid: %s\n", i_port
,
1300 tcm_vhost_dump_proto_id(tport_wwn
));
1303 port_ptr
= &i_port
[0];
1306 ptr
= strstr(i_port
, "fc.");
1308 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1309 pr_err("Passed FCP Initiator Port %s does not"
1310 " match target port protoid: %s\n", i_port
,
1311 tcm_vhost_dump_proto_id(tport_wwn
));
1314 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1317 ptr
= strstr(i_port
, "iqn.");
1319 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1320 pr_err("Passed iSCSI Initiator Port %s does not"
1321 " match target port protoid: %s\n", i_port
,
1322 tcm_vhost_dump_proto_id(tport_wwn
));
1325 port_ptr
= &i_port
[0];
1328 pr_err("Unable to locate prefix for emulated Initiator Port:"
1332 * Clear any trailing newline for the NAA WWN
1335 if (i_port
[strlen(i_port
)-1] == '\n')
1336 i_port
[strlen(i_port
)-1] = '\0';
1338 ret
= tcm_vhost_make_nexus(tv_tpg
, port_ptr
);
1345 TF_TPG_BASE_ATTR(tcm_vhost
, nexus
, S_IRUGO
| S_IWUSR
);
1347 static struct configfs_attribute
*tcm_vhost_tpg_attrs
[] = {
1348 &tcm_vhost_tpg_nexus
.attr
,
1352 static struct se_portal_group
*tcm_vhost_make_tpg(struct se_wwn
*wwn
,
1353 struct config_group
*group
,
1356 struct tcm_vhost_tport
*tport
= container_of(wwn
,
1357 struct tcm_vhost_tport
, tport_wwn
);
1359 struct tcm_vhost_tpg
*tpg
;
1363 if (strstr(name
, "tpgt_") != name
)
1364 return ERR_PTR(-EINVAL
);
1365 if (kstrtoul(name
+ 5, 10, &tpgt
) || tpgt
> UINT_MAX
)
1366 return ERR_PTR(-EINVAL
);
1368 tpg
= kzalloc(sizeof(struct tcm_vhost_tpg
), GFP_KERNEL
);
1370 pr_err("Unable to allocate struct tcm_vhost_tpg");
1371 return ERR_PTR(-ENOMEM
);
1373 mutex_init(&tpg
->tv_tpg_mutex
);
1374 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1376 tpg
->tport_tpgt
= tpgt
;
1378 ret
= core_tpg_register(&tcm_vhost_fabric_configfs
->tf_ops
, wwn
,
1379 &tpg
->se_tpg
, tpg
, TRANSPORT_TPG_TYPE_NORMAL
);
1384 mutex_lock(&tcm_vhost_mutex
);
1385 list_add_tail(&tpg
->tv_tpg_list
, &tcm_vhost_list
);
1386 mutex_unlock(&tcm_vhost_mutex
);
1388 return &tpg
->se_tpg
;
1391 static void tcm_vhost_drop_tpg(struct se_portal_group
*se_tpg
)
1393 struct tcm_vhost_tpg
*tpg
= container_of(se_tpg
,
1394 struct tcm_vhost_tpg
, se_tpg
);
1396 mutex_lock(&tcm_vhost_mutex
);
1397 list_del(&tpg
->tv_tpg_list
);
1398 mutex_unlock(&tcm_vhost_mutex
);
1400 * Release the virtual I_T Nexus for this vhost TPG
1402 tcm_vhost_drop_nexus(tpg
);
1404 * Deregister the se_tpg from TCM..
1406 core_tpg_deregister(se_tpg
);
1410 static struct se_wwn
*tcm_vhost_make_tport(struct target_fabric_configfs
*tf
,
1411 struct config_group
*group
,
1414 struct tcm_vhost_tport
*tport
;
1419 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1420 return ERR_PTR(-EINVAL); */
1422 tport
= kzalloc(sizeof(struct tcm_vhost_tport
), GFP_KERNEL
);
1424 pr_err("Unable to allocate struct tcm_vhost_tport");
1425 return ERR_PTR(-ENOMEM
);
1427 tport
->tport_wwpn
= wwpn
;
1429 * Determine the emulated Protocol Identifier and Target Port Name
1430 * based on the incoming configfs directory name.
1432 ptr
= strstr(name
, "naa.");
1434 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
1437 ptr
= strstr(name
, "fc.");
1439 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
1440 off
= 3; /* Skip over "fc." */
1443 ptr
= strstr(name
, "iqn.");
1445 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
1449 pr_err("Unable to locate prefix for emulated Target Port:"
1452 return ERR_PTR(-EINVAL
);
1455 if (strlen(name
) >= TCM_VHOST_NAMELEN
) {
1456 pr_err("Emulated %s Address: %s, exceeds"
1457 " max: %d\n", name
, tcm_vhost_dump_proto_id(tport
),
1460 return ERR_PTR(-EINVAL
);
1462 snprintf(&tport
->tport_name
[0], TCM_VHOST_NAMELEN
, "%s", &name
[off
]);
1464 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1465 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
), name
);
1467 return &tport
->tport_wwn
;
1470 static void tcm_vhost_drop_tport(struct se_wwn
*wwn
)
1472 struct tcm_vhost_tport
*tport
= container_of(wwn
,
1473 struct tcm_vhost_tport
, tport_wwn
);
1475 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1476 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport
),
1482 static ssize_t
tcm_vhost_wwn_show_attr_version(
1483 struct target_fabric_configfs
*tf
,
1486 return sprintf(page
, "TCM_VHOST fabric module %s on %s/%s"
1487 "on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
1488 utsname()->machine
);
1491 TF_WWN_ATTR_RO(tcm_vhost
, version
);
1493 static struct configfs_attribute
*tcm_vhost_wwn_attrs
[] = {
1494 &tcm_vhost_wwn_version
.attr
,
1498 static struct target_core_fabric_ops tcm_vhost_ops
= {
1499 .get_fabric_name
= tcm_vhost_get_fabric_name
,
1500 .get_fabric_proto_ident
= tcm_vhost_get_fabric_proto_ident
,
1501 .tpg_get_wwn
= tcm_vhost_get_fabric_wwn
,
1502 .tpg_get_tag
= tcm_vhost_get_tag
,
1503 .tpg_get_default_depth
= tcm_vhost_get_default_depth
,
1504 .tpg_get_pr_transport_id
= tcm_vhost_get_pr_transport_id
,
1505 .tpg_get_pr_transport_id_len
= tcm_vhost_get_pr_transport_id_len
,
1506 .tpg_parse_pr_out_transport_id
= tcm_vhost_parse_pr_out_transport_id
,
1507 .tpg_check_demo_mode
= tcm_vhost_check_true
,
1508 .tpg_check_demo_mode_cache
= tcm_vhost_check_true
,
1509 .tpg_check_demo_mode_write_protect
= tcm_vhost_check_false
,
1510 .tpg_check_prod_mode_write_protect
= tcm_vhost_check_false
,
1511 .tpg_alloc_fabric_acl
= tcm_vhost_alloc_fabric_acl
,
1512 .tpg_release_fabric_acl
= tcm_vhost_release_fabric_acl
,
1513 .tpg_get_inst_index
= tcm_vhost_tpg_get_inst_index
,
1514 .release_cmd
= tcm_vhost_release_cmd
,
1515 .shutdown_session
= tcm_vhost_shutdown_session
,
1516 .close_session
= tcm_vhost_close_session
,
1517 .sess_get_index
= tcm_vhost_sess_get_index
,
1518 .sess_get_initiator_sid
= NULL
,
1519 .write_pending
= tcm_vhost_write_pending
,
1520 .write_pending_status
= tcm_vhost_write_pending_status
,
1521 .set_default_node_attributes
= tcm_vhost_set_default_node_attrs
,
1522 .get_task_tag
= tcm_vhost_get_task_tag
,
1523 .get_cmd_state
= tcm_vhost_get_cmd_state
,
1524 .queue_data_in
= tcm_vhost_queue_data_in
,
1525 .queue_status
= tcm_vhost_queue_status
,
1526 .queue_tm_rsp
= tcm_vhost_queue_tm_rsp
,
1528 * Setup callers for generic logic in target_core_fabric_configfs.c
1530 .fabric_make_wwn
= tcm_vhost_make_tport
,
1531 .fabric_drop_wwn
= tcm_vhost_drop_tport
,
1532 .fabric_make_tpg
= tcm_vhost_make_tpg
,
1533 .fabric_drop_tpg
= tcm_vhost_drop_tpg
,
1534 .fabric_post_link
= tcm_vhost_port_link
,
1535 .fabric_pre_unlink
= tcm_vhost_port_unlink
,
1536 .fabric_make_np
= NULL
,
1537 .fabric_drop_np
= NULL
,
1538 .fabric_make_nodeacl
= tcm_vhost_make_nodeacl
,
1539 .fabric_drop_nodeacl
= tcm_vhost_drop_nodeacl
,
1542 static int tcm_vhost_register_configfs(void)
1544 struct target_fabric_configfs
*fabric
;
1547 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1548 " on "UTS_RELEASE
"\n", TCM_VHOST_VERSION
, utsname()->sysname
,
1549 utsname()->machine
);
1551 * Register the top level struct config_item_type with TCM core
1553 fabric
= target_fabric_configfs_init(THIS_MODULE
, "vhost");
1554 if (IS_ERR(fabric
)) {
1555 pr_err("target_fabric_configfs_init() failed\n");
1556 return PTR_ERR(fabric
);
1559 * Setup fabric->tf_ops from our local tcm_vhost_ops
1561 fabric
->tf_ops
= tcm_vhost_ops
;
1563 * Setup default attribute lists for various fabric->tf_cit_tmpl
1565 TF_CIT_TMPL(fabric
)->tfc_wwn_cit
.ct_attrs
= tcm_vhost_wwn_attrs
;
1566 TF_CIT_TMPL(fabric
)->tfc_tpg_base_cit
.ct_attrs
= tcm_vhost_tpg_attrs
;
1567 TF_CIT_TMPL(fabric
)->tfc_tpg_attrib_cit
.ct_attrs
= NULL
;
1568 TF_CIT_TMPL(fabric
)->tfc_tpg_param_cit
.ct_attrs
= NULL
;
1569 TF_CIT_TMPL(fabric
)->tfc_tpg_np_base_cit
.ct_attrs
= NULL
;
1570 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_base_cit
.ct_attrs
= NULL
;
1571 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_attrib_cit
.ct_attrs
= NULL
;
1572 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_auth_cit
.ct_attrs
= NULL
;
1573 TF_CIT_TMPL(fabric
)->tfc_tpg_nacl_param_cit
.ct_attrs
= NULL
;
1575 * Register the fabric for use within TCM
1577 ret
= target_fabric_configfs_register(fabric
);
1579 pr_err("target_fabric_configfs_register() failed"
1580 " for TCM_VHOST\n");
1584 * Setup our local pointer to *fabric
1586 tcm_vhost_fabric_configfs
= fabric
;
1587 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1591 static void tcm_vhost_deregister_configfs(void)
1593 if (!tcm_vhost_fabric_configfs
)
1596 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs
);
1597 tcm_vhost_fabric_configfs
= NULL
;
1598 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1601 static int __init
tcm_vhost_init(void)
1605 * Use our own dedicated workqueue for submitting I/O into
1606 * target core to avoid contention within system_wq.
1608 tcm_vhost_workqueue
= alloc_workqueue("tcm_vhost", 0, 0);
1609 if (!tcm_vhost_workqueue
)
1612 ret
= vhost_scsi_register();
1614 goto out_destroy_workqueue
;
1616 ret
= tcm_vhost_register_configfs();
1618 goto out_vhost_scsi_deregister
;
1622 out_vhost_scsi_deregister
:
1623 vhost_scsi_deregister();
1624 out_destroy_workqueue
:
1625 destroy_workqueue(tcm_vhost_workqueue
);
1630 static void tcm_vhost_exit(void)
1632 tcm_vhost_deregister_configfs();
1633 vhost_scsi_deregister();
1634 destroy_workqueue(tcm_vhost_workqueue
);
1637 MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1638 MODULE_LICENSE("GPL");
1639 module_init(tcm_vhost_init
);
1640 module_exit(tcm_vhost_exit
);