1 /*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
4 * (C) Copyright 2010-2013 Datera, Inc.
5 * (C) Copyright 2010-2012 IBM Corp.
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
9 * Authors: Nicholas A. Bellinger <nab@daterainc.com>
10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 ****************************************************************************/
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <generated/utsrelease.h>
27 #include <linux/utsname.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/kthread.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/configfs.h>
34 #include <linux/ctype.h>
35 #include <linux/compat.h>
36 #include <linux/eventfd.h>
38 #include <linux/vmalloc.h>
39 #include <linux/miscdevice.h>
40 #include <asm/unaligned.h>
41 #include <scsi/scsi_common.h>
42 #include <scsi/scsi_proto.h>
43 #include <target/target_core_base.h>
44 #include <target/target_core_fabric.h>
45 #include <linux/vhost.h>
46 #include <linux/virtio_scsi.h>
47 #include <linux/llist.h>
48 #include <linux/bitmap.h>
52 #define VHOST_SCSI_VERSION "v0.1"
53 #define VHOST_SCSI_NAMELEN 256
54 #define VHOST_SCSI_MAX_CDB_SIZE 32
55 #define VHOST_SCSI_DEFAULT_TAGS 256
56 #define VHOST_SCSI_PREALLOC_SGLS 2048
57 #define VHOST_SCSI_PREALLOC_UPAGES 2048
58 #define VHOST_SCSI_PREALLOC_PROT_SGLS 2048
60 /* Max number of requests before requeueing the job.
61 * Using this limit prevents one virtqueue from starving others with
64 #define VHOST_SCSI_WEIGHT 256
66 struct vhost_scsi_inflight
{
67 /* Wait for the flush operation to finish */
68 struct completion comp
;
69 /* Refcount for the inflight reqs */
73 struct vhost_scsi_cmd
{
74 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
76 /* virtio-scsi initiator task attribute */
78 /* virtio-scsi response incoming iovecs */
80 /* virtio-scsi initiator data direction */
81 enum dma_data_direction tvc_data_direction
;
82 /* Expected data transfer length from virtio-scsi header */
84 /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
86 /* The number of scatterlists associated with this cmd */
88 u32 tvc_prot_sgl_count
;
89 /* Saved unpacked SCSI LUN for vhost_scsi_submission_work() */
91 /* Pointer to the SGL formatted memory from virtio-scsi */
92 struct scatterlist
*tvc_sgl
;
93 struct scatterlist
*tvc_prot_sgl
;
94 struct page
**tvc_upages
;
95 /* Pointer to response header iovec */
96 struct iovec tvc_resp_iov
;
97 /* Pointer to vhost_scsi for our device */
98 struct vhost_scsi
*tvc_vhost
;
99 /* Pointer to vhost_virtqueue for the cmd */
100 struct vhost_virtqueue
*tvc_vq
;
101 /* Pointer to vhost nexus memory */
102 struct vhost_scsi_nexus
*tvc_nexus
;
103 /* The TCM I/O descriptor that is accessed via container_of() */
104 struct se_cmd tvc_se_cmd
;
105 /* work item used for cmwq dispatch to vhost_scsi_submission_work() */
106 struct work_struct work
;
107 /* Copy of the incoming SCSI command descriptor block (CDB) */
108 unsigned char tvc_cdb
[VHOST_SCSI_MAX_CDB_SIZE
];
109 /* Sense buffer that will be mapped into outgoing status */
110 unsigned char tvc_sense_buf
[TRANSPORT_SENSE_BUFFER
];
111 /* Completed commands list, serviced from vhost worker thread */
112 struct llist_node tvc_completion_list
;
113 /* Used to track inflight cmd */
114 struct vhost_scsi_inflight
*inflight
;
117 struct vhost_scsi_nexus
{
118 /* Pointer to TCM session for I_T Nexus */
119 struct se_session
*tvn_se_sess
;
122 struct vhost_scsi_tpg
{
123 /* Vhost port target portal group tag for TCM */
125 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
126 int tv_tpg_port_count
;
127 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
128 int tv_tpg_vhost_count
;
129 /* Used for enabling T10-PI with legacy devices */
130 int tv_fabric_prot_type
;
131 /* list for vhost_scsi_list */
132 struct list_head tv_tpg_list
;
133 /* Used to protect access for tpg_nexus */
134 struct mutex tv_tpg_mutex
;
135 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
136 struct vhost_scsi_nexus
*tpg_nexus
;
137 /* Pointer back to vhost_scsi_tport */
138 struct vhost_scsi_tport
*tport
;
139 /* Returned by vhost_scsi_make_tpg() */
140 struct se_portal_group se_tpg
;
141 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
142 struct vhost_scsi
*vhost_scsi
;
145 struct vhost_scsi_tport
{
146 /* SCSI protocol the tport is providing */
148 /* Binary World Wide unique Port Name for Vhost Target port */
150 /* ASCII formatted WWPN for Vhost Target port */
151 char tport_name
[VHOST_SCSI_NAMELEN
];
152 /* Returned by vhost_scsi_make_tport() */
153 struct se_wwn tport_wwn
;
156 struct vhost_scsi_evt
{
157 /* event to be sent to guest */
158 struct virtio_scsi_event event
;
159 /* event list, serviced from vhost worker thread */
160 struct llist_node list
;
164 VHOST_SCSI_VQ_CTL
= 0,
165 VHOST_SCSI_VQ_EVT
= 1,
166 VHOST_SCSI_VQ_IO
= 2,
169 /* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
171 VHOST_SCSI_FEATURES
= VHOST_FEATURES
| (1ULL << VIRTIO_SCSI_F_HOTPLUG
) |
172 (1ULL << VIRTIO_SCSI_F_T10_PI
)
175 #define VHOST_SCSI_MAX_TARGET 256
176 #define VHOST_SCSI_MAX_VQ 128
177 #define VHOST_SCSI_MAX_EVENT 128
179 struct vhost_scsi_virtqueue
{
180 struct vhost_virtqueue vq
;
182 * Reference counting for inflight reqs, used for flush operation. At
183 * each time, one reference tracks new commands submitted, while we
184 * wait for another one to reach 0.
186 struct vhost_scsi_inflight inflights
[2];
188 * Indicate current inflight in use, protected by vq->mutex.
189 * Writers must also take dev mutex and flush under it.
195 /* Protected by vhost_scsi->dev.mutex */
196 struct vhost_scsi_tpg
**vs_tpg
;
197 char vs_vhost_wwpn
[TRANSPORT_IQN_LEN
];
199 struct vhost_dev dev
;
200 struct vhost_scsi_virtqueue vqs
[VHOST_SCSI_MAX_VQ
];
202 struct vhost_work vs_completion_work
; /* cmd completion work item */
203 struct llist_head vs_completion_list
; /* cmd completion queue */
205 struct vhost_work vs_event_work
; /* evt injection work item */
206 struct llist_head vs_event_list
; /* evt injection queue */
208 bool vs_events_missed
; /* any missed events, protected by vq->mutex */
209 int vs_events_nr
; /* num of pending events, protected by vq->mutex */
212 static struct workqueue_struct
*vhost_scsi_workqueue
;
214 /* Global spinlock to protect vhost_scsi TPG list for vhost IOCTL access */
215 static DEFINE_MUTEX(vhost_scsi_mutex
);
216 static LIST_HEAD(vhost_scsi_list
);
218 static void vhost_scsi_done_inflight(struct kref
*kref
)
220 struct vhost_scsi_inflight
*inflight
;
222 inflight
= container_of(kref
, struct vhost_scsi_inflight
, kref
);
223 complete(&inflight
->comp
);
226 static void vhost_scsi_init_inflight(struct vhost_scsi
*vs
,
227 struct vhost_scsi_inflight
*old_inflight
[])
229 struct vhost_scsi_inflight
*new_inflight
;
230 struct vhost_virtqueue
*vq
;
233 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
236 mutex_lock(&vq
->mutex
);
238 /* store old infight */
239 idx
= vs
->vqs
[i
].inflight_idx
;
241 old_inflight
[i
] = &vs
->vqs
[i
].inflights
[idx
];
243 /* setup new infight */
244 vs
->vqs
[i
].inflight_idx
= idx
^ 1;
245 new_inflight
= &vs
->vqs
[i
].inflights
[idx
^ 1];
246 kref_init(&new_inflight
->kref
);
247 init_completion(&new_inflight
->comp
);
249 mutex_unlock(&vq
->mutex
);
253 static struct vhost_scsi_inflight
*
254 vhost_scsi_get_inflight(struct vhost_virtqueue
*vq
)
256 struct vhost_scsi_inflight
*inflight
;
257 struct vhost_scsi_virtqueue
*svq
;
259 svq
= container_of(vq
, struct vhost_scsi_virtqueue
, vq
);
260 inflight
= &svq
->inflights
[svq
->inflight_idx
];
261 kref_get(&inflight
->kref
);
266 static void vhost_scsi_put_inflight(struct vhost_scsi_inflight
*inflight
)
268 kref_put(&inflight
->kref
, vhost_scsi_done_inflight
);
271 static int vhost_scsi_check_true(struct se_portal_group
*se_tpg
)
276 static int vhost_scsi_check_false(struct se_portal_group
*se_tpg
)
281 static char *vhost_scsi_get_fabric_name(void)
286 static char *vhost_scsi_get_fabric_wwn(struct se_portal_group
*se_tpg
)
288 struct vhost_scsi_tpg
*tpg
= container_of(se_tpg
,
289 struct vhost_scsi_tpg
, se_tpg
);
290 struct vhost_scsi_tport
*tport
= tpg
->tport
;
292 return &tport
->tport_name
[0];
295 static u16
vhost_scsi_get_tpgt(struct se_portal_group
*se_tpg
)
297 struct vhost_scsi_tpg
*tpg
= container_of(se_tpg
,
298 struct vhost_scsi_tpg
, se_tpg
);
299 return tpg
->tport_tpgt
;
302 static int vhost_scsi_check_prot_fabric_only(struct se_portal_group
*se_tpg
)
304 struct vhost_scsi_tpg
*tpg
= container_of(se_tpg
,
305 struct vhost_scsi_tpg
, se_tpg
);
307 return tpg
->tv_fabric_prot_type
;
310 static u32
vhost_scsi_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
315 static void vhost_scsi_release_cmd(struct se_cmd
*se_cmd
)
317 struct vhost_scsi_cmd
*tv_cmd
= container_of(se_cmd
,
318 struct vhost_scsi_cmd
, tvc_se_cmd
);
319 struct se_session
*se_sess
= tv_cmd
->tvc_nexus
->tvn_se_sess
;
322 if (tv_cmd
->tvc_sgl_count
) {
323 for (i
= 0; i
< tv_cmd
->tvc_sgl_count
; i
++)
324 put_page(sg_page(&tv_cmd
->tvc_sgl
[i
]));
326 if (tv_cmd
->tvc_prot_sgl_count
) {
327 for (i
= 0; i
< tv_cmd
->tvc_prot_sgl_count
; i
++)
328 put_page(sg_page(&tv_cmd
->tvc_prot_sgl
[i
]));
331 vhost_scsi_put_inflight(tv_cmd
->inflight
);
332 target_free_tag(se_sess
, se_cmd
);
335 static u32
vhost_scsi_sess_get_index(struct se_session
*se_sess
)
340 static int vhost_scsi_write_pending(struct se_cmd
*se_cmd
)
342 /* Go ahead and process the write immediately */
343 target_execute_cmd(se_cmd
);
347 static int vhost_scsi_write_pending_status(struct se_cmd
*se_cmd
)
352 static void vhost_scsi_set_default_node_attrs(struct se_node_acl
*nacl
)
357 static int vhost_scsi_get_cmd_state(struct se_cmd
*se_cmd
)
362 static void vhost_scsi_complete_cmd(struct vhost_scsi_cmd
*cmd
)
364 struct vhost_scsi
*vs
= cmd
->tvc_vhost
;
366 llist_add(&cmd
->tvc_completion_list
, &vs
->vs_completion_list
);
368 vhost_work_queue(&vs
->dev
, &vs
->vs_completion_work
);
371 static int vhost_scsi_queue_data_in(struct se_cmd
*se_cmd
)
373 struct vhost_scsi_cmd
*cmd
= container_of(se_cmd
,
374 struct vhost_scsi_cmd
, tvc_se_cmd
);
375 vhost_scsi_complete_cmd(cmd
);
379 static int vhost_scsi_queue_status(struct se_cmd
*se_cmd
)
381 struct vhost_scsi_cmd
*cmd
= container_of(se_cmd
,
382 struct vhost_scsi_cmd
, tvc_se_cmd
);
383 vhost_scsi_complete_cmd(cmd
);
387 static void vhost_scsi_queue_tm_rsp(struct se_cmd
*se_cmd
)
392 static void vhost_scsi_aborted_task(struct se_cmd
*se_cmd
)
397 static void vhost_scsi_free_evt(struct vhost_scsi
*vs
, struct vhost_scsi_evt
*evt
)
403 static struct vhost_scsi_evt
*
404 vhost_scsi_allocate_evt(struct vhost_scsi
*vs
,
405 u32 event
, u32 reason
)
407 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
408 struct vhost_scsi_evt
*evt
;
410 if (vs
->vs_events_nr
> VHOST_SCSI_MAX_EVENT
) {
411 vs
->vs_events_missed
= true;
415 evt
= kzalloc(sizeof(*evt
), GFP_KERNEL
);
417 vq_err(vq
, "Failed to allocate vhost_scsi_evt\n");
418 vs
->vs_events_missed
= true;
422 evt
->event
.event
= cpu_to_vhost32(vq
, event
);
423 evt
->event
.reason
= cpu_to_vhost32(vq
, reason
);
429 static void vhost_scsi_free_cmd(struct vhost_scsi_cmd
*cmd
)
431 struct se_cmd
*se_cmd
= &cmd
->tvc_se_cmd
;
433 /* TODO locking against target/backend threads? */
434 transport_generic_free_cmd(se_cmd
, 0);
438 static int vhost_scsi_check_stop_free(struct se_cmd
*se_cmd
)
440 return target_put_sess_cmd(se_cmd
);
444 vhost_scsi_do_evt_work(struct vhost_scsi
*vs
, struct vhost_scsi_evt
*evt
)
446 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
447 struct virtio_scsi_event
*event
= &evt
->event
;
448 struct virtio_scsi_event __user
*eventp
;
452 if (!vq
->private_data
) {
453 vs
->vs_events_missed
= true;
458 vhost_disable_notify(&vs
->dev
, vq
);
459 head
= vhost_get_vq_desc(vq
, vq
->iov
,
460 ARRAY_SIZE(vq
->iov
), &out
, &in
,
463 vs
->vs_events_missed
= true;
466 if (head
== vq
->num
) {
467 if (vhost_enable_notify(&vs
->dev
, vq
))
469 vs
->vs_events_missed
= true;
473 if ((vq
->iov
[out
].iov_len
!= sizeof(struct virtio_scsi_event
))) {
474 vq_err(vq
, "Expecting virtio_scsi_event, got %zu bytes\n",
475 vq
->iov
[out
].iov_len
);
476 vs
->vs_events_missed
= true;
480 if (vs
->vs_events_missed
) {
481 event
->event
|= cpu_to_vhost32(vq
, VIRTIO_SCSI_T_EVENTS_MISSED
);
482 vs
->vs_events_missed
= false;
485 eventp
= vq
->iov
[out
].iov_base
;
486 ret
= __copy_to_user(eventp
, event
, sizeof(*event
));
488 vhost_add_used_and_signal(&vs
->dev
, vq
, head
, 0);
490 vq_err(vq
, "Faulted on vhost_scsi_send_event\n");
493 static void vhost_scsi_evt_work(struct vhost_work
*work
)
495 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
497 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
498 struct vhost_scsi_evt
*evt
, *t
;
499 struct llist_node
*llnode
;
501 mutex_lock(&vq
->mutex
);
502 llnode
= llist_del_all(&vs
->vs_event_list
);
503 llist_for_each_entry_safe(evt
, t
, llnode
, list
) {
504 vhost_scsi_do_evt_work(vs
, evt
);
505 vhost_scsi_free_evt(vs
, evt
);
507 mutex_unlock(&vq
->mutex
);
510 /* Fill in status and signal that we are done processing this command
512 * This is scheduled in the vhost work queue so we are called with the owner
513 * process mm and can access the vring.
515 static void vhost_scsi_complete_cmd_work(struct vhost_work
*work
)
517 struct vhost_scsi
*vs
= container_of(work
, struct vhost_scsi
,
519 DECLARE_BITMAP(signal
, VHOST_SCSI_MAX_VQ
);
520 struct virtio_scsi_cmd_resp v_rsp
;
521 struct vhost_scsi_cmd
*cmd
, *t
;
522 struct llist_node
*llnode
;
523 struct se_cmd
*se_cmd
;
524 struct iov_iter iov_iter
;
527 bitmap_zero(signal
, VHOST_SCSI_MAX_VQ
);
528 llnode
= llist_del_all(&vs
->vs_completion_list
);
529 llist_for_each_entry_safe(cmd
, t
, llnode
, tvc_completion_list
) {
530 se_cmd
= &cmd
->tvc_se_cmd
;
532 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__
,
533 cmd
, se_cmd
->residual_count
, se_cmd
->scsi_status
);
535 memset(&v_rsp
, 0, sizeof(v_rsp
));
536 v_rsp
.resid
= cpu_to_vhost32(cmd
->tvc_vq
, se_cmd
->residual_count
);
537 /* TODO is status_qualifier field needed? */
538 v_rsp
.status
= se_cmd
->scsi_status
;
539 v_rsp
.sense_len
= cpu_to_vhost32(cmd
->tvc_vq
,
540 se_cmd
->scsi_sense_length
);
541 memcpy(v_rsp
.sense
, cmd
->tvc_sense_buf
,
542 se_cmd
->scsi_sense_length
);
544 iov_iter_init(&iov_iter
, READ
, &cmd
->tvc_resp_iov
,
545 cmd
->tvc_in_iovs
, sizeof(v_rsp
));
546 ret
= copy_to_iter(&v_rsp
, sizeof(v_rsp
), &iov_iter
);
547 if (likely(ret
== sizeof(v_rsp
))) {
548 struct vhost_scsi_virtqueue
*q
;
549 vhost_add_used(cmd
->tvc_vq
, cmd
->tvc_vq_desc
, 0);
550 q
= container_of(cmd
->tvc_vq
, struct vhost_scsi_virtqueue
, vq
);
552 __set_bit(vq
, signal
);
554 pr_err("Faulted on virtio_scsi_cmd_resp\n");
556 vhost_scsi_free_cmd(cmd
);
560 while ((vq
= find_next_bit(signal
, VHOST_SCSI_MAX_VQ
, vq
+ 1))
562 vhost_signal(&vs
->dev
, &vs
->vqs
[vq
].vq
);
565 static struct vhost_scsi_cmd
*
566 vhost_scsi_get_tag(struct vhost_virtqueue
*vq
, struct vhost_scsi_tpg
*tpg
,
567 unsigned char *cdb
, u64 scsi_tag
, u16 lun
, u8 task_attr
,
568 u32 exp_data_len
, int data_direction
)
570 struct vhost_scsi_cmd
*cmd
;
571 struct vhost_scsi_nexus
*tv_nexus
;
572 struct se_session
*se_sess
;
573 struct scatterlist
*sg
, *prot_sg
;
577 tv_nexus
= tpg
->tpg_nexus
;
579 pr_err("Unable to locate active struct vhost_scsi_nexus\n");
580 return ERR_PTR(-EIO
);
582 se_sess
= tv_nexus
->tvn_se_sess
;
584 tag
= sbitmap_queue_get(&se_sess
->sess_tag_pool
, &cpu
);
586 pr_err("Unable to obtain tag for vhost_scsi_cmd\n");
587 return ERR_PTR(-ENOMEM
);
590 cmd
= &((struct vhost_scsi_cmd
*)se_sess
->sess_cmd_map
)[tag
];
592 prot_sg
= cmd
->tvc_prot_sgl
;
593 pages
= cmd
->tvc_upages
;
594 memset(cmd
, 0, sizeof(*cmd
));
596 cmd
->tvc_prot_sgl
= prot_sg
;
597 cmd
->tvc_upages
= pages
;
598 cmd
->tvc_se_cmd
.map_tag
= tag
;
599 cmd
->tvc_se_cmd
.map_cpu
= cpu
;
600 cmd
->tvc_tag
= scsi_tag
;
602 cmd
->tvc_task_attr
= task_attr
;
603 cmd
->tvc_exp_data_len
= exp_data_len
;
604 cmd
->tvc_data_direction
= data_direction
;
605 cmd
->tvc_nexus
= tv_nexus
;
606 cmd
->inflight
= vhost_scsi_get_inflight(vq
);
608 memcpy(cmd
->tvc_cdb
, cdb
, VHOST_SCSI_MAX_CDB_SIZE
);
614 * Map a user memory range into a scatterlist
616 * Returns the number of scatterlist entries used or -errno on error.
619 vhost_scsi_map_to_sgl(struct vhost_scsi_cmd
*cmd
,
620 struct iov_iter
*iter
,
621 struct scatterlist
*sgl
,
624 struct page
**pages
= cmd
->tvc_upages
;
625 struct scatterlist
*sg
= sgl
;
628 unsigned int npages
= 0;
630 bytes
= iov_iter_get_pages(iter
, pages
, LONG_MAX
,
631 VHOST_SCSI_PREALLOC_UPAGES
, &offset
);
632 /* No pages were pinned */
634 return bytes
< 0 ? bytes
: -EFAULT
;
636 iov_iter_advance(iter
, bytes
);
639 unsigned n
= min_t(unsigned, PAGE_SIZE
- offset
, bytes
);
640 sg_set_page(sg
++, pages
[npages
++], n
, offset
);
648 vhost_scsi_calc_sgls(struct iov_iter
*iter
, size_t bytes
, int max_sgls
)
652 if (!iter
|| !iter
->iov
) {
653 pr_err("%s: iter->iov is NULL, but expected bytes: %zu"
654 " present\n", __func__
, bytes
);
658 sgl_count
= iov_iter_npages(iter
, 0xffff);
659 if (sgl_count
> max_sgls
) {
660 pr_err("%s: requested sgl_count: %d exceeds pre-allocated"
661 " max_sgls: %d\n", __func__
, sgl_count
, max_sgls
);
668 vhost_scsi_iov_to_sgl(struct vhost_scsi_cmd
*cmd
, bool write
,
669 struct iov_iter
*iter
,
670 struct scatterlist
*sg
, int sg_count
)
672 struct scatterlist
*p
= sg
;
675 while (iov_iter_count(iter
)) {
676 ret
= vhost_scsi_map_to_sgl(cmd
, iter
, sg
, write
);
679 struct page
*page
= sg_page(p
++);
691 vhost_scsi_mapal(struct vhost_scsi_cmd
*cmd
,
692 size_t prot_bytes
, struct iov_iter
*prot_iter
,
693 size_t data_bytes
, struct iov_iter
*data_iter
)
696 bool write
= (cmd
->tvc_data_direction
== DMA_FROM_DEVICE
);
699 sgl_count
= vhost_scsi_calc_sgls(prot_iter
, prot_bytes
,
700 VHOST_SCSI_PREALLOC_PROT_SGLS
);
704 sg_init_table(cmd
->tvc_prot_sgl
, sgl_count
);
705 cmd
->tvc_prot_sgl_count
= sgl_count
;
706 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__
,
707 cmd
->tvc_prot_sgl
, cmd
->tvc_prot_sgl_count
);
709 ret
= vhost_scsi_iov_to_sgl(cmd
, write
, prot_iter
,
711 cmd
->tvc_prot_sgl_count
);
713 cmd
->tvc_prot_sgl_count
= 0;
717 sgl_count
= vhost_scsi_calc_sgls(data_iter
, data_bytes
,
718 VHOST_SCSI_PREALLOC_SGLS
);
722 sg_init_table(cmd
->tvc_sgl
, sgl_count
);
723 cmd
->tvc_sgl_count
= sgl_count
;
724 pr_debug("%s data_sg %p data_sgl_count %u\n", __func__
,
725 cmd
->tvc_sgl
, cmd
->tvc_sgl_count
);
727 ret
= vhost_scsi_iov_to_sgl(cmd
, write
, data_iter
,
728 cmd
->tvc_sgl
, cmd
->tvc_sgl_count
);
730 cmd
->tvc_sgl_count
= 0;
736 static int vhost_scsi_to_tcm_attr(int attr
)
739 case VIRTIO_SCSI_S_SIMPLE
:
740 return TCM_SIMPLE_TAG
;
741 case VIRTIO_SCSI_S_ORDERED
:
742 return TCM_ORDERED_TAG
;
743 case VIRTIO_SCSI_S_HEAD
:
745 case VIRTIO_SCSI_S_ACA
:
750 return TCM_SIMPLE_TAG
;
753 static void vhost_scsi_submission_work(struct work_struct
*work
)
755 struct vhost_scsi_cmd
*cmd
=
756 container_of(work
, struct vhost_scsi_cmd
, work
);
757 struct vhost_scsi_nexus
*tv_nexus
;
758 struct se_cmd
*se_cmd
= &cmd
->tvc_se_cmd
;
759 struct scatterlist
*sg_ptr
, *sg_prot_ptr
= NULL
;
762 /* FIXME: BIDI operation */
763 if (cmd
->tvc_sgl_count
) {
764 sg_ptr
= cmd
->tvc_sgl
;
766 if (cmd
->tvc_prot_sgl_count
)
767 sg_prot_ptr
= cmd
->tvc_prot_sgl
;
769 se_cmd
->prot_pto
= true;
773 tv_nexus
= cmd
->tvc_nexus
;
776 rc
= target_submit_cmd_map_sgls(se_cmd
, tv_nexus
->tvn_se_sess
,
777 cmd
->tvc_cdb
, &cmd
->tvc_sense_buf
[0],
778 cmd
->tvc_lun
, cmd
->tvc_exp_data_len
,
779 vhost_scsi_to_tcm_attr(cmd
->tvc_task_attr
),
780 cmd
->tvc_data_direction
, TARGET_SCF_ACK_KREF
,
781 sg_ptr
, cmd
->tvc_sgl_count
, NULL
, 0, sg_prot_ptr
,
782 cmd
->tvc_prot_sgl_count
);
784 transport_send_check_condition_and_sense(se_cmd
,
785 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
786 transport_generic_free_cmd(se_cmd
, 0);
791 vhost_scsi_send_bad_target(struct vhost_scsi
*vs
,
792 struct vhost_virtqueue
*vq
,
793 int head
, unsigned out
)
795 struct virtio_scsi_cmd_resp __user
*resp
;
796 struct virtio_scsi_cmd_resp rsp
;
799 memset(&rsp
, 0, sizeof(rsp
));
800 rsp
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
801 resp
= vq
->iov
[out
].iov_base
;
802 ret
= __copy_to_user(resp
, &rsp
, sizeof(rsp
));
804 vhost_add_used_and_signal(&vs
->dev
, vq
, head
, 0);
806 pr_err("Faulted on virtio_scsi_cmd_resp\n");
810 vhost_scsi_handle_vq(struct vhost_scsi
*vs
, struct vhost_virtqueue
*vq
)
812 struct vhost_scsi_tpg
**vs_tpg
, *tpg
;
813 struct virtio_scsi_cmd_req v_req
;
814 struct virtio_scsi_cmd_req_pi v_req_pi
;
815 struct vhost_scsi_cmd
*cmd
;
816 struct iov_iter out_iter
, in_iter
, prot_iter
, data_iter
;
818 u32 exp_data_len
, data_direction
;
819 unsigned int out
= 0, in
= 0;
820 int head
, ret
, prot_bytes
, c
= 0;
821 size_t req_size
, rsp_size
= sizeof(struct virtio_scsi_cmd_resp
);
822 size_t out_size
, in_size
;
824 u8
*target
, *lunp
, task_attr
;
825 bool t10_pi
= vhost_has_feature(vq
, VIRTIO_SCSI_F_T10_PI
);
828 mutex_lock(&vq
->mutex
);
830 * We can handle the vq only after the endpoint is setup by calling the
831 * VHOST_SCSI_SET_ENDPOINT ioctl.
833 vs_tpg
= vq
->private_data
;
837 vhost_disable_notify(&vs
->dev
, vq
);
840 head
= vhost_get_vq_desc(vq
, vq
->iov
,
841 ARRAY_SIZE(vq
->iov
), &out
, &in
,
843 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
845 /* On error, stop handling until the next kick. */
846 if (unlikely(head
< 0))
848 /* Nothing new? Wait for eventfd to tell us they refilled. */
849 if (head
== vq
->num
) {
850 if (unlikely(vhost_enable_notify(&vs
->dev
, vq
))) {
851 vhost_disable_notify(&vs
->dev
, vq
);
857 * Check for a sane response buffer so we can report early
858 * errors back to the guest.
860 if (unlikely(vq
->iov
[out
].iov_len
< rsp_size
)) {
861 vq_err(vq
, "Expecting at least virtio_scsi_cmd_resp"
862 " size, got %zu bytes\n", vq
->iov
[out
].iov_len
);
866 * Setup pointers and values based upon different virtio-scsi
867 * request header if T10_PI is enabled in KVM guest.
871 req_size
= sizeof(v_req_pi
);
872 lunp
= &v_req_pi
.lun
[0];
873 target
= &v_req_pi
.lun
[1];
876 req_size
= sizeof(v_req
);
877 lunp
= &v_req
.lun
[0];
878 target
= &v_req
.lun
[1];
881 * FIXME: Not correct for BIDI operation
883 out_size
= iov_length(vq
->iov
, out
);
884 in_size
= iov_length(&vq
->iov
[out
], in
);
887 * Copy over the virtio-scsi request header, which for a
888 * ANY_LAYOUT enabled guest may span multiple iovecs, or a
889 * single iovec may contain both the header + outgoing
892 * copy_from_iter() will advance out_iter, so that it will
893 * point at the start of the outgoing WRITE payload, if
894 * DMA_TO_DEVICE is set.
896 iov_iter_init(&out_iter
, WRITE
, vq
->iov
, out
, out_size
);
898 if (unlikely(!copy_from_iter_full(req
, req_size
, &out_iter
))) {
899 vq_err(vq
, "Faulted on copy_from_iter\n");
900 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
903 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
904 if (unlikely(*lunp
!= 1)) {
905 vq_err(vq
, "Illegal virtio-scsi lun: %u\n", *lunp
);
906 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
910 tpg
= READ_ONCE(vs_tpg
[*target
]);
911 if (unlikely(!tpg
)) {
912 /* Target does not exist, fail the request */
913 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
917 * Determine data_direction by calculating the total outgoing
918 * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
919 * response headers respectively.
921 * For DMA_TO_DEVICE this is out_iter, which is already pointing
922 * to the right place.
924 * For DMA_FROM_DEVICE, the iovec will be just past the end
925 * of the virtio-scsi response header in either the same
926 * or immediately following iovec.
928 * Any associated T10_PI bytes for the outgoing / incoming
929 * payloads are included in calculation of exp_data_len here.
933 if (out_size
> req_size
) {
934 data_direction
= DMA_TO_DEVICE
;
935 exp_data_len
= out_size
- req_size
;
936 data_iter
= out_iter
;
937 } else if (in_size
> rsp_size
) {
938 data_direction
= DMA_FROM_DEVICE
;
939 exp_data_len
= in_size
- rsp_size
;
941 iov_iter_init(&in_iter
, READ
, &vq
->iov
[out
], in
,
942 rsp_size
+ exp_data_len
);
943 iov_iter_advance(&in_iter
, rsp_size
);
946 data_direction
= DMA_NONE
;
950 * If T10_PI header + payload is present, setup prot_iter values
951 * and recalculate data_iter for vhost_scsi_mapal() mapping to
952 * host scatterlists via get_user_pages_fast().
955 if (v_req_pi
.pi_bytesout
) {
956 if (data_direction
!= DMA_TO_DEVICE
) {
957 vq_err(vq
, "Received non zero pi_bytesout,"
958 " but wrong data_direction\n");
959 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
962 prot_bytes
= vhost32_to_cpu(vq
, v_req_pi
.pi_bytesout
);
963 } else if (v_req_pi
.pi_bytesin
) {
964 if (data_direction
!= DMA_FROM_DEVICE
) {
965 vq_err(vq
, "Received non zero pi_bytesin,"
966 " but wrong data_direction\n");
967 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
970 prot_bytes
= vhost32_to_cpu(vq
, v_req_pi
.pi_bytesin
);
973 * Set prot_iter to data_iter and truncate it to
974 * prot_bytes, and advance data_iter past any
975 * preceeding prot_bytes that may be present.
977 * Also fix up the exp_data_len to reflect only the
978 * actual data payload length.
981 exp_data_len
-= prot_bytes
;
982 prot_iter
= data_iter
;
983 iov_iter_truncate(&prot_iter
, prot_bytes
);
984 iov_iter_advance(&data_iter
, prot_bytes
);
986 tag
= vhost64_to_cpu(vq
, v_req_pi
.tag
);
987 task_attr
= v_req_pi
.task_attr
;
988 cdb
= &v_req_pi
.cdb
[0];
989 lun
= ((v_req_pi
.lun
[2] << 8) | v_req_pi
.lun
[3]) & 0x3FFF;
991 tag
= vhost64_to_cpu(vq
, v_req
.tag
);
992 task_attr
= v_req
.task_attr
;
994 lun
= ((v_req
.lun
[2] << 8) | v_req
.lun
[3]) & 0x3FFF;
997 * Check that the received CDB size does not exceeded our
998 * hardcoded max for vhost-scsi, then get a pre-allocated
999 * cmd descriptor for the new virtio-scsi tag.
1001 * TODO what if cdb was too small for varlen cdb header?
1003 if (unlikely(scsi_command_size(cdb
) > VHOST_SCSI_MAX_CDB_SIZE
)) {
1004 vq_err(vq
, "Received SCSI CDB with command_size: %d that"
1005 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1006 scsi_command_size(cdb
), VHOST_SCSI_MAX_CDB_SIZE
);
1007 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1010 cmd
= vhost_scsi_get_tag(vq
, tpg
, cdb
, tag
, lun
, task_attr
,
1011 exp_data_len
+ prot_bytes
,
1014 vq_err(vq
, "vhost_scsi_get_tag failed %ld\n",
1016 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1019 cmd
->tvc_vhost
= vs
;
1021 cmd
->tvc_resp_iov
= vq
->iov
[out
];
1022 cmd
->tvc_in_iovs
= in
;
1024 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1025 cmd
->tvc_cdb
[0], cmd
->tvc_lun
);
1026 pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
1027 " %d\n", cmd
, exp_data_len
, prot_bytes
, data_direction
);
1029 if (data_direction
!= DMA_NONE
) {
1030 ret
= vhost_scsi_mapal(cmd
,
1031 prot_bytes
, &prot_iter
,
1032 exp_data_len
, &data_iter
);
1033 if (unlikely(ret
)) {
1034 vq_err(vq
, "Failed to map iov to sgl\n");
1035 vhost_scsi_release_cmd(&cmd
->tvc_se_cmd
);
1036 vhost_scsi_send_bad_target(vs
, vq
, head
, out
);
1041 * Save the descriptor from vhost_get_vq_desc() to be used to
1042 * complete the virtio-scsi request in TCM callback context via
1043 * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
1045 cmd
->tvc_vq_desc
= head
;
1047 * Dispatch cmd descriptor for cmwq execution in process
1048 * context provided by vhost_scsi_workqueue. This also ensures
1049 * cmd is executed on the same kworker CPU as this vhost
1050 * thread to gain positive L2 cache locality effects.
1052 INIT_WORK(&cmd
->work
, vhost_scsi_submission_work
);
1053 queue_work(vhost_scsi_workqueue
, &cmd
->work
);
1054 } while (likely(!vhost_exceeds_weight(vq
, ++c
, 0)));
1056 mutex_unlock(&vq
->mutex
);
1059 static void vhost_scsi_ctl_handle_kick(struct vhost_work
*work
)
1061 pr_debug("%s: The handling func for control queue.\n", __func__
);
1065 vhost_scsi_send_evt(struct vhost_scsi
*vs
,
1066 struct vhost_scsi_tpg
*tpg
,
1071 struct vhost_scsi_evt
*evt
;
1073 evt
= vhost_scsi_allocate_evt(vs
, event
, reason
);
1078 /* TODO: share lun setup code with virtio-scsi.ko */
1080 * Note: evt->event is zeroed when we allocate it and
1081 * lun[4-7] need to be zero according to virtio-scsi spec.
1083 evt
->event
.lun
[0] = 0x01;
1084 evt
->event
.lun
[1] = tpg
->tport_tpgt
;
1085 if (lun
->unpacked_lun
>= 256)
1086 evt
->event
.lun
[2] = lun
->unpacked_lun
>> 8 | 0x40 ;
1087 evt
->event
.lun
[3] = lun
->unpacked_lun
& 0xFF;
1090 llist_add(&evt
->list
, &vs
->vs_event_list
);
1091 vhost_work_queue(&vs
->dev
, &vs
->vs_event_work
);
1094 static void vhost_scsi_evt_handle_kick(struct vhost_work
*work
)
1096 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
1098 struct vhost_scsi
*vs
= container_of(vq
->dev
, struct vhost_scsi
, dev
);
1100 mutex_lock(&vq
->mutex
);
1101 if (!vq
->private_data
)
1104 if (vs
->vs_events_missed
)
1105 vhost_scsi_send_evt(vs
, NULL
, NULL
, VIRTIO_SCSI_T_NO_EVENT
, 0);
1107 mutex_unlock(&vq
->mutex
);
1110 static void vhost_scsi_handle_kick(struct vhost_work
*work
)
1112 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
1114 struct vhost_scsi
*vs
= container_of(vq
->dev
, struct vhost_scsi
, dev
);
1116 vhost_scsi_handle_vq(vs
, vq
);
1119 static void vhost_scsi_flush_vq(struct vhost_scsi
*vs
, int index
)
1121 vhost_poll_flush(&vs
->vqs
[index
].vq
.poll
);
1124 /* Callers must hold dev mutex */
1125 static void vhost_scsi_flush(struct vhost_scsi
*vs
)
1127 struct vhost_scsi_inflight
*old_inflight
[VHOST_SCSI_MAX_VQ
];
1130 /* Init new inflight and remember the old inflight */
1131 vhost_scsi_init_inflight(vs
, old_inflight
);
1134 * The inflight->kref was initialized to 1. We decrement it here to
1135 * indicate the start of the flush operation so that it will reach 0
1136 * when all the reqs are finished.
1138 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1139 kref_put(&old_inflight
[i
]->kref
, vhost_scsi_done_inflight
);
1141 /* Flush both the vhost poll and vhost work */
1142 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1143 vhost_scsi_flush_vq(vs
, i
);
1144 vhost_work_flush(&vs
->dev
, &vs
->vs_completion_work
);
1145 vhost_work_flush(&vs
->dev
, &vs
->vs_event_work
);
1147 /* Wait for all reqs issued before the flush to be finished */
1148 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++)
1149 wait_for_completion(&old_inflight
[i
]->comp
);
1153 * Called from vhost_scsi_ioctl() context to walk the list of available
1154 * vhost_scsi_tpg with an active struct vhost_scsi_nexus
1156 * The lock nesting rule is:
1157 * vhost_scsi_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1160 vhost_scsi_set_endpoint(struct vhost_scsi
*vs
,
1161 struct vhost_scsi_target
*t
)
1163 struct se_portal_group
*se_tpg
;
1164 struct vhost_scsi_tport
*tv_tport
;
1165 struct vhost_scsi_tpg
*tpg
;
1166 struct vhost_scsi_tpg
**vs_tpg
;
1167 struct vhost_virtqueue
*vq
;
1168 int index
, ret
, i
, len
;
1171 mutex_lock(&vhost_scsi_mutex
);
1172 mutex_lock(&vs
->dev
.mutex
);
1174 /* Verify that ring has been setup correctly. */
1175 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
1176 /* Verify that ring has been setup correctly. */
1177 if (!vhost_vq_access_ok(&vs
->vqs
[index
].vq
)) {
1183 len
= sizeof(vs_tpg
[0]) * VHOST_SCSI_MAX_TARGET
;
1184 vs_tpg
= kzalloc(len
, GFP_KERNEL
);
1190 memcpy(vs_tpg
, vs
->vs_tpg
, len
);
1192 list_for_each_entry(tpg
, &vhost_scsi_list
, tv_tpg_list
) {
1193 mutex_lock(&tpg
->tv_tpg_mutex
);
1194 if (!tpg
->tpg_nexus
) {
1195 mutex_unlock(&tpg
->tv_tpg_mutex
);
1198 if (tpg
->tv_tpg_vhost_count
!= 0) {
1199 mutex_unlock(&tpg
->tv_tpg_mutex
);
1202 tv_tport
= tpg
->tport
;
1204 if (!strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
)) {
1205 if (vs
->vs_tpg
&& vs
->vs_tpg
[tpg
->tport_tpgt
]) {
1207 mutex_unlock(&tpg
->tv_tpg_mutex
);
1212 * In order to ensure individual vhost-scsi configfs
1213 * groups cannot be removed while in use by vhost ioctl,
1214 * go ahead and take an explicit se_tpg->tpg_group.cg_item
1217 se_tpg
= &tpg
->se_tpg
;
1218 ret
= target_depend_item(&se_tpg
->tpg_group
.cg_item
);
1220 pr_warn("configfs_depend_item() failed: %d\n", ret
);
1222 mutex_unlock(&tpg
->tv_tpg_mutex
);
1225 tpg
->tv_tpg_vhost_count
++;
1226 tpg
->vhost_scsi
= vs
;
1227 vs_tpg
[tpg
->tport_tpgt
] = tpg
;
1228 smp_mb__after_atomic();
1231 mutex_unlock(&tpg
->tv_tpg_mutex
);
1235 memcpy(vs
->vs_vhost_wwpn
, t
->vhost_wwpn
,
1236 sizeof(vs
->vs_vhost_wwpn
));
1237 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1238 vq
= &vs
->vqs
[i
].vq
;
1239 mutex_lock(&vq
->mutex
);
1240 vq
->private_data
= vs_tpg
;
1241 vhost_vq_init_access(vq
);
1242 mutex_unlock(&vq
->mutex
);
1250 * Act as synchronize_rcu to make sure access to
1251 * old vs->vs_tpg is finished.
1253 vhost_scsi_flush(vs
);
1255 vs
->vs_tpg
= vs_tpg
;
1258 mutex_unlock(&vs
->dev
.mutex
);
1259 mutex_unlock(&vhost_scsi_mutex
);
1264 vhost_scsi_clear_endpoint(struct vhost_scsi
*vs
,
1265 struct vhost_scsi_target
*t
)
1267 struct se_portal_group
*se_tpg
;
1268 struct vhost_scsi_tport
*tv_tport
;
1269 struct vhost_scsi_tpg
*tpg
;
1270 struct vhost_virtqueue
*vq
;
1275 mutex_lock(&vhost_scsi_mutex
);
1276 mutex_lock(&vs
->dev
.mutex
);
1277 /* Verify that ring has been setup correctly. */
1278 for (index
= 0; index
< vs
->dev
.nvqs
; ++index
) {
1279 if (!vhost_vq_access_ok(&vs
->vqs
[index
].vq
)) {
1290 for (i
= 0; i
< VHOST_SCSI_MAX_TARGET
; i
++) {
1292 tpg
= vs
->vs_tpg
[target
];
1296 mutex_lock(&tpg
->tv_tpg_mutex
);
1297 tv_tport
= tpg
->tport
;
1303 if (strcmp(tv_tport
->tport_name
, t
->vhost_wwpn
)) {
1304 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
1305 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1306 tv_tport
->tport_name
, tpg
->tport_tpgt
,
1307 t
->vhost_wwpn
, t
->vhost_tpgt
);
1311 tpg
->tv_tpg_vhost_count
--;
1312 tpg
->vhost_scsi
= NULL
;
1313 vs
->vs_tpg
[target
] = NULL
;
1315 mutex_unlock(&tpg
->tv_tpg_mutex
);
1317 * Release se_tpg->tpg_group.cg_item configfs dependency now
1318 * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
1320 se_tpg
= &tpg
->se_tpg
;
1321 target_undepend_item(&se_tpg
->tpg_group
.cg_item
);
1324 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1325 vq
= &vs
->vqs
[i
].vq
;
1326 mutex_lock(&vq
->mutex
);
1327 vq
->private_data
= NULL
;
1328 mutex_unlock(&vq
->mutex
);
1332 * Act as synchronize_rcu to make sure access to
1333 * old vs->vs_tpg is finished.
1335 vhost_scsi_flush(vs
);
1338 WARN_ON(vs
->vs_events_nr
);
1339 mutex_unlock(&vs
->dev
.mutex
);
1340 mutex_unlock(&vhost_scsi_mutex
);
1344 mutex_unlock(&tpg
->tv_tpg_mutex
);
1346 mutex_unlock(&vs
->dev
.mutex
);
1347 mutex_unlock(&vhost_scsi_mutex
);
1351 static int vhost_scsi_set_features(struct vhost_scsi
*vs
, u64 features
)
1353 struct vhost_virtqueue
*vq
;
1356 if (features
& ~VHOST_SCSI_FEATURES
)
1359 mutex_lock(&vs
->dev
.mutex
);
1360 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
1361 !vhost_log_access_ok(&vs
->dev
)) {
1362 mutex_unlock(&vs
->dev
.mutex
);
1366 for (i
= 0; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1367 vq
= &vs
->vqs
[i
].vq
;
1368 mutex_lock(&vq
->mutex
);
1369 vq
->acked_features
= features
;
1370 mutex_unlock(&vq
->mutex
);
1372 mutex_unlock(&vs
->dev
.mutex
);
1376 static int vhost_scsi_open(struct inode
*inode
, struct file
*f
)
1378 struct vhost_scsi
*vs
;
1379 struct vhost_virtqueue
**vqs
;
1382 vs
= kzalloc(sizeof(*vs
), GFP_KERNEL
| __GFP_NOWARN
| __GFP_RETRY_MAYFAIL
);
1384 vs
= vzalloc(sizeof(*vs
));
1389 vqs
= kmalloc_array(VHOST_SCSI_MAX_VQ
, sizeof(*vqs
), GFP_KERNEL
);
1393 vhost_work_init(&vs
->vs_completion_work
, vhost_scsi_complete_cmd_work
);
1394 vhost_work_init(&vs
->vs_event_work
, vhost_scsi_evt_work
);
1396 vs
->vs_events_nr
= 0;
1397 vs
->vs_events_missed
= false;
1399 vqs
[VHOST_SCSI_VQ_CTL
] = &vs
->vqs
[VHOST_SCSI_VQ_CTL
].vq
;
1400 vqs
[VHOST_SCSI_VQ_EVT
] = &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1401 vs
->vqs
[VHOST_SCSI_VQ_CTL
].vq
.handle_kick
= vhost_scsi_ctl_handle_kick
;
1402 vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
.handle_kick
= vhost_scsi_evt_handle_kick
;
1403 for (i
= VHOST_SCSI_VQ_IO
; i
< VHOST_SCSI_MAX_VQ
; i
++) {
1404 vqs
[i
] = &vs
->vqs
[i
].vq
;
1405 vs
->vqs
[i
].vq
.handle_kick
= vhost_scsi_handle_kick
;
1407 vhost_dev_init(&vs
->dev
, vqs
, VHOST_SCSI_MAX_VQ
, UIO_MAXIOV
,
1408 VHOST_SCSI_WEIGHT
, 0);
1410 vhost_scsi_init_inflight(vs
, NULL
);
1412 f
->private_data
= vs
;
1421 static int vhost_scsi_release(struct inode
*inode
, struct file
*f
)
1423 struct vhost_scsi
*vs
= f
->private_data
;
1424 struct vhost_scsi_target t
;
1426 mutex_lock(&vs
->dev
.mutex
);
1427 memcpy(t
.vhost_wwpn
, vs
->vs_vhost_wwpn
, sizeof(t
.vhost_wwpn
));
1428 mutex_unlock(&vs
->dev
.mutex
);
1429 vhost_scsi_clear_endpoint(vs
, &t
);
1430 vhost_dev_stop(&vs
->dev
);
1431 vhost_dev_cleanup(&vs
->dev
);
1432 /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1433 vhost_scsi_flush(vs
);
1440 vhost_scsi_ioctl(struct file
*f
,
1444 struct vhost_scsi
*vs
= f
->private_data
;
1445 struct vhost_scsi_target backend
;
1446 void __user
*argp
= (void __user
*)arg
;
1447 u64 __user
*featurep
= argp
;
1448 u32 __user
*eventsp
= argp
;
1451 int r
, abi_version
= VHOST_SCSI_ABI_VERSION
;
1452 struct vhost_virtqueue
*vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1455 case VHOST_SCSI_SET_ENDPOINT
:
1456 if (copy_from_user(&backend
, argp
, sizeof backend
))
1458 if (backend
.reserved
!= 0)
1461 return vhost_scsi_set_endpoint(vs
, &backend
);
1462 case VHOST_SCSI_CLEAR_ENDPOINT
:
1463 if (copy_from_user(&backend
, argp
, sizeof backend
))
1465 if (backend
.reserved
!= 0)
1468 return vhost_scsi_clear_endpoint(vs
, &backend
);
1469 case VHOST_SCSI_GET_ABI_VERSION
:
1470 if (copy_to_user(argp
, &abi_version
, sizeof abi_version
))
1473 case VHOST_SCSI_SET_EVENTS_MISSED
:
1474 if (get_user(events_missed
, eventsp
))
1476 mutex_lock(&vq
->mutex
);
1477 vs
->vs_events_missed
= events_missed
;
1478 mutex_unlock(&vq
->mutex
);
1480 case VHOST_SCSI_GET_EVENTS_MISSED
:
1481 mutex_lock(&vq
->mutex
);
1482 events_missed
= vs
->vs_events_missed
;
1483 mutex_unlock(&vq
->mutex
);
1484 if (put_user(events_missed
, eventsp
))
1487 case VHOST_GET_FEATURES
:
1488 features
= VHOST_SCSI_FEATURES
;
1489 if (copy_to_user(featurep
, &features
, sizeof features
))
1492 case VHOST_SET_FEATURES
:
1493 if (copy_from_user(&features
, featurep
, sizeof features
))
1495 return vhost_scsi_set_features(vs
, features
);
1497 mutex_lock(&vs
->dev
.mutex
);
1498 r
= vhost_dev_ioctl(&vs
->dev
, ioctl
, argp
);
1499 /* TODO: flush backend after dev ioctl. */
1500 if (r
== -ENOIOCTLCMD
)
1501 r
= vhost_vring_ioctl(&vs
->dev
, ioctl
, argp
);
1502 mutex_unlock(&vs
->dev
.mutex
);
1507 #ifdef CONFIG_COMPAT
1508 static long vhost_scsi_compat_ioctl(struct file
*f
, unsigned int ioctl
,
1511 return vhost_scsi_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
1515 static const struct file_operations vhost_scsi_fops
= {
1516 .owner
= THIS_MODULE
,
1517 .release
= vhost_scsi_release
,
1518 .unlocked_ioctl
= vhost_scsi_ioctl
,
1519 #ifdef CONFIG_COMPAT
1520 .compat_ioctl
= vhost_scsi_compat_ioctl
,
1522 .open
= vhost_scsi_open
,
1523 .llseek
= noop_llseek
,
1526 static struct miscdevice vhost_scsi_misc
= {
1532 static int __init
vhost_scsi_register(void)
1534 return misc_register(&vhost_scsi_misc
);
1537 static void vhost_scsi_deregister(void)
1539 misc_deregister(&vhost_scsi_misc
);
1542 static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport
*tport
)
1544 switch (tport
->tport_proto_id
) {
1545 case SCSI_PROTOCOL_SAS
:
1547 case SCSI_PROTOCOL_FCP
:
1549 case SCSI_PROTOCOL_ISCSI
:
1559 vhost_scsi_do_plug(struct vhost_scsi_tpg
*tpg
,
1560 struct se_lun
*lun
, bool plug
)
1563 struct vhost_scsi
*vs
= tpg
->vhost_scsi
;
1564 struct vhost_virtqueue
*vq
;
1570 mutex_lock(&vs
->dev
.mutex
);
1573 reason
= VIRTIO_SCSI_EVT_RESET_RESCAN
;
1575 reason
= VIRTIO_SCSI_EVT_RESET_REMOVED
;
1577 vq
= &vs
->vqs
[VHOST_SCSI_VQ_EVT
].vq
;
1578 mutex_lock(&vq
->mutex
);
1579 if (vhost_has_feature(vq
, VIRTIO_SCSI_F_HOTPLUG
))
1580 vhost_scsi_send_evt(vs
, tpg
, lun
,
1581 VIRTIO_SCSI_T_TRANSPORT_RESET
, reason
);
1582 mutex_unlock(&vq
->mutex
);
1583 mutex_unlock(&vs
->dev
.mutex
);
1586 static void vhost_scsi_hotplug(struct vhost_scsi_tpg
*tpg
, struct se_lun
*lun
)
1588 vhost_scsi_do_plug(tpg
, lun
, true);
1591 static void vhost_scsi_hotunplug(struct vhost_scsi_tpg
*tpg
, struct se_lun
*lun
)
1593 vhost_scsi_do_plug(tpg
, lun
, false);
1596 static int vhost_scsi_port_link(struct se_portal_group
*se_tpg
,
1599 struct vhost_scsi_tpg
*tpg
= container_of(se_tpg
,
1600 struct vhost_scsi_tpg
, se_tpg
);
1602 mutex_lock(&vhost_scsi_mutex
);
1604 mutex_lock(&tpg
->tv_tpg_mutex
);
1605 tpg
->tv_tpg_port_count
++;
1606 mutex_unlock(&tpg
->tv_tpg_mutex
);
1608 vhost_scsi_hotplug(tpg
, lun
);
1610 mutex_unlock(&vhost_scsi_mutex
);
1615 static void vhost_scsi_port_unlink(struct se_portal_group
*se_tpg
,
1618 struct vhost_scsi_tpg
*tpg
= container_of(se_tpg
,
1619 struct vhost_scsi_tpg
, se_tpg
);
1621 mutex_lock(&vhost_scsi_mutex
);
1623 mutex_lock(&tpg
->tv_tpg_mutex
);
1624 tpg
->tv_tpg_port_count
--;
1625 mutex_unlock(&tpg
->tv_tpg_mutex
);
1627 vhost_scsi_hotunplug(tpg
, lun
);
1629 mutex_unlock(&vhost_scsi_mutex
);
1632 static void vhost_scsi_free_cmd_map_res(struct se_session
*se_sess
)
1634 struct vhost_scsi_cmd
*tv_cmd
;
1637 if (!se_sess
->sess_cmd_map
)
1640 for (i
= 0; i
< VHOST_SCSI_DEFAULT_TAGS
; i
++) {
1641 tv_cmd
= &((struct vhost_scsi_cmd
*)se_sess
->sess_cmd_map
)[i
];
1643 kfree(tv_cmd
->tvc_sgl
);
1644 kfree(tv_cmd
->tvc_prot_sgl
);
1645 kfree(tv_cmd
->tvc_upages
);
1649 static ssize_t
vhost_scsi_tpg_attrib_fabric_prot_type_store(
1650 struct config_item
*item
, const char *page
, size_t count
)
1652 struct se_portal_group
*se_tpg
= attrib_to_tpg(item
);
1653 struct vhost_scsi_tpg
*tpg
= container_of(se_tpg
,
1654 struct vhost_scsi_tpg
, se_tpg
);
1656 int ret
= kstrtoul(page
, 0, &val
);
1659 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret
);
1662 if (val
!= 0 && val
!= 1 && val
!= 3) {
1663 pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val
);
1666 tpg
->tv_fabric_prot_type
= val
;
1671 static ssize_t
vhost_scsi_tpg_attrib_fabric_prot_type_show(
1672 struct config_item
*item
, char *page
)
1674 struct se_portal_group
*se_tpg
= attrib_to_tpg(item
);
1675 struct vhost_scsi_tpg
*tpg
= container_of(se_tpg
,
1676 struct vhost_scsi_tpg
, se_tpg
);
1678 return sprintf(page
, "%d\n", tpg
->tv_fabric_prot_type
);
1681 CONFIGFS_ATTR(vhost_scsi_tpg_attrib_
, fabric_prot_type
);
1683 static struct configfs_attribute
*vhost_scsi_tpg_attrib_attrs
[] = {
1684 &vhost_scsi_tpg_attrib_attr_fabric_prot_type
,
1688 static int vhost_scsi_nexus_cb(struct se_portal_group
*se_tpg
,
1689 struct se_session
*se_sess
, void *p
)
1691 struct vhost_scsi_cmd
*tv_cmd
;
1694 for (i
= 0; i
< VHOST_SCSI_DEFAULT_TAGS
; i
++) {
1695 tv_cmd
= &((struct vhost_scsi_cmd
*)se_sess
->sess_cmd_map
)[i
];
1697 tv_cmd
->tvc_sgl
= kcalloc(VHOST_SCSI_PREALLOC_SGLS
,
1698 sizeof(struct scatterlist
),
1700 if (!tv_cmd
->tvc_sgl
) {
1701 pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1705 tv_cmd
->tvc_upages
= kcalloc(VHOST_SCSI_PREALLOC_UPAGES
,
1706 sizeof(struct page
*),
1708 if (!tv_cmd
->tvc_upages
) {
1709 pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1713 tv_cmd
->tvc_prot_sgl
= kcalloc(VHOST_SCSI_PREALLOC_PROT_SGLS
,
1714 sizeof(struct scatterlist
),
1716 if (!tv_cmd
->tvc_prot_sgl
) {
1717 pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1723 vhost_scsi_free_cmd_map_res(se_sess
);
1727 static int vhost_scsi_make_nexus(struct vhost_scsi_tpg
*tpg
,
1730 struct vhost_scsi_nexus
*tv_nexus
;
1732 mutex_lock(&tpg
->tv_tpg_mutex
);
1733 if (tpg
->tpg_nexus
) {
1734 mutex_unlock(&tpg
->tv_tpg_mutex
);
1735 pr_debug("tpg->tpg_nexus already exists\n");
1739 tv_nexus
= kzalloc(sizeof(*tv_nexus
), GFP_KERNEL
);
1741 mutex_unlock(&tpg
->tv_tpg_mutex
);
1742 pr_err("Unable to allocate struct vhost_scsi_nexus\n");
1746 * Since we are running in 'demo mode' this call with generate a
1747 * struct se_node_acl for the vhost_scsi struct se_portal_group with
1748 * the SCSI Initiator port name of the passed configfs group 'name'.
1750 tv_nexus
->tvn_se_sess
= target_setup_session(&tpg
->se_tpg
,
1751 VHOST_SCSI_DEFAULT_TAGS
,
1752 sizeof(struct vhost_scsi_cmd
),
1753 TARGET_PROT_DIN_PASS
| TARGET_PROT_DOUT_PASS
,
1754 (unsigned char *)name
, tv_nexus
,
1755 vhost_scsi_nexus_cb
);
1756 if (IS_ERR(tv_nexus
->tvn_se_sess
)) {
1757 mutex_unlock(&tpg
->tv_tpg_mutex
);
1761 tpg
->tpg_nexus
= tv_nexus
;
1763 mutex_unlock(&tpg
->tv_tpg_mutex
);
1767 static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg
*tpg
)
1769 struct se_session
*se_sess
;
1770 struct vhost_scsi_nexus
*tv_nexus
;
1772 mutex_lock(&tpg
->tv_tpg_mutex
);
1773 tv_nexus
= tpg
->tpg_nexus
;
1775 mutex_unlock(&tpg
->tv_tpg_mutex
);
1779 se_sess
= tv_nexus
->tvn_se_sess
;
1781 mutex_unlock(&tpg
->tv_tpg_mutex
);
1785 if (tpg
->tv_tpg_port_count
!= 0) {
1786 mutex_unlock(&tpg
->tv_tpg_mutex
);
1787 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1788 " active TPG port count: %d\n",
1789 tpg
->tv_tpg_port_count
);
1793 if (tpg
->tv_tpg_vhost_count
!= 0) {
1794 mutex_unlock(&tpg
->tv_tpg_mutex
);
1795 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1796 " active TPG vhost count: %d\n",
1797 tpg
->tv_tpg_vhost_count
);
1801 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1802 " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg
->tport
),
1803 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1805 vhost_scsi_free_cmd_map_res(se_sess
);
1807 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1809 target_remove_session(se_sess
);
1810 tpg
->tpg_nexus
= NULL
;
1811 mutex_unlock(&tpg
->tv_tpg_mutex
);
1817 static ssize_t
vhost_scsi_tpg_nexus_show(struct config_item
*item
, char *page
)
1819 struct se_portal_group
*se_tpg
= to_tpg(item
);
1820 struct vhost_scsi_tpg
*tpg
= container_of(se_tpg
,
1821 struct vhost_scsi_tpg
, se_tpg
);
1822 struct vhost_scsi_nexus
*tv_nexus
;
1825 mutex_lock(&tpg
->tv_tpg_mutex
);
1826 tv_nexus
= tpg
->tpg_nexus
;
1828 mutex_unlock(&tpg
->tv_tpg_mutex
);
1831 ret
= snprintf(page
, PAGE_SIZE
, "%s\n",
1832 tv_nexus
->tvn_se_sess
->se_node_acl
->initiatorname
);
1833 mutex_unlock(&tpg
->tv_tpg_mutex
);
1838 static ssize_t
vhost_scsi_tpg_nexus_store(struct config_item
*item
,
1839 const char *page
, size_t count
)
1841 struct se_portal_group
*se_tpg
= to_tpg(item
);
1842 struct vhost_scsi_tpg
*tpg
= container_of(se_tpg
,
1843 struct vhost_scsi_tpg
, se_tpg
);
1844 struct vhost_scsi_tport
*tport_wwn
= tpg
->tport
;
1845 unsigned char i_port
[VHOST_SCSI_NAMELEN
], *ptr
, *port_ptr
;
1848 * Shutdown the active I_T nexus if 'NULL' is passed..
1850 if (!strncmp(page
, "NULL", 4)) {
1851 ret
= vhost_scsi_drop_nexus(tpg
);
1852 return (!ret
) ? count
: ret
;
1855 * Otherwise make sure the passed virtual Initiator port WWN matches
1856 * the fabric protocol_id set in vhost_scsi_make_tport(), and call
1857 * vhost_scsi_make_nexus().
1859 if (strlen(page
) >= VHOST_SCSI_NAMELEN
) {
1860 pr_err("Emulated NAA Sas Address: %s, exceeds"
1861 " max: %d\n", page
, VHOST_SCSI_NAMELEN
);
1864 snprintf(&i_port
[0], VHOST_SCSI_NAMELEN
, "%s", page
);
1866 ptr
= strstr(i_port
, "naa.");
1868 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_SAS
) {
1869 pr_err("Passed SAS Initiator Port %s does not"
1870 " match target port protoid: %s\n", i_port
,
1871 vhost_scsi_dump_proto_id(tport_wwn
));
1874 port_ptr
= &i_port
[0];
1877 ptr
= strstr(i_port
, "fc.");
1879 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_FCP
) {
1880 pr_err("Passed FCP Initiator Port %s does not"
1881 " match target port protoid: %s\n", i_port
,
1882 vhost_scsi_dump_proto_id(tport_wwn
));
1885 port_ptr
= &i_port
[3]; /* Skip over "fc." */
1888 ptr
= strstr(i_port
, "iqn.");
1890 if (tport_wwn
->tport_proto_id
!= SCSI_PROTOCOL_ISCSI
) {
1891 pr_err("Passed iSCSI Initiator Port %s does not"
1892 " match target port protoid: %s\n", i_port
,
1893 vhost_scsi_dump_proto_id(tport_wwn
));
1896 port_ptr
= &i_port
[0];
1899 pr_err("Unable to locate prefix for emulated Initiator Port:"
1903 * Clear any trailing newline for the NAA WWN
1906 if (i_port
[strlen(i_port
)-1] == '\n')
1907 i_port
[strlen(i_port
)-1] = '\0';
1909 ret
= vhost_scsi_make_nexus(tpg
, port_ptr
);
1916 CONFIGFS_ATTR(vhost_scsi_tpg_
, nexus
);
1918 static struct configfs_attribute
*vhost_scsi_tpg_attrs
[] = {
1919 &vhost_scsi_tpg_attr_nexus
,
1923 static struct se_portal_group
*
1924 vhost_scsi_make_tpg(struct se_wwn
*wwn
, const char *name
)
1926 struct vhost_scsi_tport
*tport
= container_of(wwn
,
1927 struct vhost_scsi_tport
, tport_wwn
);
1929 struct vhost_scsi_tpg
*tpg
;
1933 if (strstr(name
, "tpgt_") != name
)
1934 return ERR_PTR(-EINVAL
);
1935 if (kstrtou16(name
+ 5, 10, &tpgt
) || tpgt
>= VHOST_SCSI_MAX_TARGET
)
1936 return ERR_PTR(-EINVAL
);
1938 tpg
= kzalloc(sizeof(*tpg
), GFP_KERNEL
);
1940 pr_err("Unable to allocate struct vhost_scsi_tpg");
1941 return ERR_PTR(-ENOMEM
);
1943 mutex_init(&tpg
->tv_tpg_mutex
);
1944 INIT_LIST_HEAD(&tpg
->tv_tpg_list
);
1946 tpg
->tport_tpgt
= tpgt
;
1948 ret
= core_tpg_register(wwn
, &tpg
->se_tpg
, tport
->tport_proto_id
);
1953 mutex_lock(&vhost_scsi_mutex
);
1954 list_add_tail(&tpg
->tv_tpg_list
, &vhost_scsi_list
);
1955 mutex_unlock(&vhost_scsi_mutex
);
1957 return &tpg
->se_tpg
;
1960 static void vhost_scsi_drop_tpg(struct se_portal_group
*se_tpg
)
1962 struct vhost_scsi_tpg
*tpg
= container_of(se_tpg
,
1963 struct vhost_scsi_tpg
, se_tpg
);
1965 mutex_lock(&vhost_scsi_mutex
);
1966 list_del(&tpg
->tv_tpg_list
);
1967 mutex_unlock(&vhost_scsi_mutex
);
1969 * Release the virtual I_T Nexus for this vhost TPG
1971 vhost_scsi_drop_nexus(tpg
);
1973 * Deregister the se_tpg from TCM..
1975 core_tpg_deregister(se_tpg
);
1979 static struct se_wwn
*
1980 vhost_scsi_make_tport(struct target_fabric_configfs
*tf
,
1981 struct config_group
*group
,
1984 struct vhost_scsi_tport
*tport
;
1989 /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
1990 return ERR_PTR(-EINVAL); */
1992 tport
= kzalloc(sizeof(*tport
), GFP_KERNEL
);
1994 pr_err("Unable to allocate struct vhost_scsi_tport");
1995 return ERR_PTR(-ENOMEM
);
1997 tport
->tport_wwpn
= wwpn
;
1999 * Determine the emulated Protocol Identifier and Target Port Name
2000 * based on the incoming configfs directory name.
2002 ptr
= strstr(name
, "naa.");
2004 tport
->tport_proto_id
= SCSI_PROTOCOL_SAS
;
2007 ptr
= strstr(name
, "fc.");
2009 tport
->tport_proto_id
= SCSI_PROTOCOL_FCP
;
2010 off
= 3; /* Skip over "fc." */
2013 ptr
= strstr(name
, "iqn.");
2015 tport
->tport_proto_id
= SCSI_PROTOCOL_ISCSI
;
2019 pr_err("Unable to locate prefix for emulated Target Port:"
2022 return ERR_PTR(-EINVAL
);
2025 if (strlen(name
) >= VHOST_SCSI_NAMELEN
) {
2026 pr_err("Emulated %s Address: %s, exceeds"
2027 " max: %d\n", name
, vhost_scsi_dump_proto_id(tport
),
2028 VHOST_SCSI_NAMELEN
);
2030 return ERR_PTR(-EINVAL
);
2032 snprintf(&tport
->tport_name
[0], VHOST_SCSI_NAMELEN
, "%s", &name
[off
]);
2034 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
2035 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport
), name
);
2037 return &tport
->tport_wwn
;
2040 static void vhost_scsi_drop_tport(struct se_wwn
*wwn
)
2042 struct vhost_scsi_tport
*tport
= container_of(wwn
,
2043 struct vhost_scsi_tport
, tport_wwn
);
2045 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2046 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport
),
2053 vhost_scsi_wwn_version_show(struct config_item
*item
, char *page
)
2055 return sprintf(page
, "TCM_VHOST fabric module %s on %s/%s"
2056 "on "UTS_RELEASE
"\n", VHOST_SCSI_VERSION
, utsname()->sysname
,
2057 utsname()->machine
);
2060 CONFIGFS_ATTR_RO(vhost_scsi_wwn_
, version
);
2062 static struct configfs_attribute
*vhost_scsi_wwn_attrs
[] = {
2063 &vhost_scsi_wwn_attr_version
,
2067 static const struct target_core_fabric_ops vhost_scsi_ops
= {
2068 .module
= THIS_MODULE
,
2070 .get_fabric_name
= vhost_scsi_get_fabric_name
,
2071 .tpg_get_wwn
= vhost_scsi_get_fabric_wwn
,
2072 .tpg_get_tag
= vhost_scsi_get_tpgt
,
2073 .tpg_check_demo_mode
= vhost_scsi_check_true
,
2074 .tpg_check_demo_mode_cache
= vhost_scsi_check_true
,
2075 .tpg_check_demo_mode_write_protect
= vhost_scsi_check_false
,
2076 .tpg_check_prod_mode_write_protect
= vhost_scsi_check_false
,
2077 .tpg_check_prot_fabric_only
= vhost_scsi_check_prot_fabric_only
,
2078 .tpg_get_inst_index
= vhost_scsi_tpg_get_inst_index
,
2079 .release_cmd
= vhost_scsi_release_cmd
,
2080 .check_stop_free
= vhost_scsi_check_stop_free
,
2081 .sess_get_index
= vhost_scsi_sess_get_index
,
2082 .sess_get_initiator_sid
= NULL
,
2083 .write_pending
= vhost_scsi_write_pending
,
2084 .write_pending_status
= vhost_scsi_write_pending_status
,
2085 .set_default_node_attributes
= vhost_scsi_set_default_node_attrs
,
2086 .get_cmd_state
= vhost_scsi_get_cmd_state
,
2087 .queue_data_in
= vhost_scsi_queue_data_in
,
2088 .queue_status
= vhost_scsi_queue_status
,
2089 .queue_tm_rsp
= vhost_scsi_queue_tm_rsp
,
2090 .aborted_task
= vhost_scsi_aborted_task
,
2092 * Setup callers for generic logic in target_core_fabric_configfs.c
2094 .fabric_make_wwn
= vhost_scsi_make_tport
,
2095 .fabric_drop_wwn
= vhost_scsi_drop_tport
,
2096 .fabric_make_tpg
= vhost_scsi_make_tpg
,
2097 .fabric_drop_tpg
= vhost_scsi_drop_tpg
,
2098 .fabric_post_link
= vhost_scsi_port_link
,
2099 .fabric_pre_unlink
= vhost_scsi_port_unlink
,
2101 .tfc_wwn_attrs
= vhost_scsi_wwn_attrs
,
2102 .tfc_tpg_base_attrs
= vhost_scsi_tpg_attrs
,
2103 .tfc_tpg_attrib_attrs
= vhost_scsi_tpg_attrib_attrs
,
2106 static int __init
vhost_scsi_init(void)
2110 pr_debug("TCM_VHOST fabric module %s on %s/%s"
2111 " on "UTS_RELEASE
"\n", VHOST_SCSI_VERSION
, utsname()->sysname
,
2112 utsname()->machine
);
2115 * Use our own dedicated workqueue for submitting I/O into
2116 * target core to avoid contention within system_wq.
2118 vhost_scsi_workqueue
= alloc_workqueue("vhost_scsi", 0, 0);
2119 if (!vhost_scsi_workqueue
)
2122 ret
= vhost_scsi_register();
2124 goto out_destroy_workqueue
;
2126 ret
= target_register_template(&vhost_scsi_ops
);
2128 goto out_vhost_scsi_deregister
;
2132 out_vhost_scsi_deregister
:
2133 vhost_scsi_deregister();
2134 out_destroy_workqueue
:
2135 destroy_workqueue(vhost_scsi_workqueue
);
2140 static void vhost_scsi_exit(void)
2142 target_unregister_template(&vhost_scsi_ops
);
2143 vhost_scsi_deregister();
2144 destroy_workqueue(vhost_scsi_workqueue
);
2147 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2148 MODULE_ALIAS("tcm_vhost");
2149 MODULE_LICENSE("GPL");
2150 module_init(vhost_scsi_init
);
2151 module_exit(vhost_scsi_exit
);