]> Pileus Git - ~andy/linux/blob - drivers/vhost/tcm_vhost.c
tcm_vhost: Enable VIRTIO_SCSI_F_HOTPLUG
[~andy/linux] / drivers / vhost / tcm_vhost.c
1 /*******************************************************************************
2  * Vhost kernel TCM fabric driver for virtio SCSI initiators
3  *
4  * (C) Copyright 2010-2012 RisingTide Systems LLC.
5  * (C) Copyright 2010-2012 IBM Corp.
6  *
7  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8  *
9  * Authors: Nicholas A. Bellinger <nab@risingtidesystems.com>
10  *          Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
11  *
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.
16  *
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.
21  *
22  ****************************************************************************/
23
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <generated/utsrelease.h>
27 #include <linux/utsname.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/kthread.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/configfs.h>
34 #include <linux/ctype.h>
35 #include <linux/compat.h>
36 #include <linux/eventfd.h>
37 #include <linux/fs.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>
52
53 #include "vhost.c"
54 #include "vhost.h"
55 #include "tcm_vhost.h"
56
57 enum {
58         VHOST_SCSI_VQ_CTL = 0,
59         VHOST_SCSI_VQ_EVT = 1,
60         VHOST_SCSI_VQ_IO = 2,
61 };
62
63 /*
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.
67  */
68 enum {
69         VHOST_SCSI_FEATURES = (VHOST_FEATURES & (~VIRTIO_RING_F_EVENT_IDX)) |
70                               (1ULL << VIRTIO_SCSI_F_HOTPLUG)
71 };
72
73 #define VHOST_SCSI_MAX_TARGET   256
74 #define VHOST_SCSI_MAX_VQ       128
75 #define VHOST_SCSI_MAX_EVENT    128
76
77 struct vhost_scsi {
78         /* Protected by vhost_scsi->dev.mutex */
79         struct tcm_vhost_tpg **vs_tpg;
80         char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
81
82         struct vhost_dev dev;
83         struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
84
85         struct vhost_work vs_completion_work; /* cmd completion work item */
86         struct llist_head vs_completion_list; /* cmd completion queue */
87
88         struct vhost_work vs_event_work; /* evt injection work item */
89         struct llist_head vs_event_list; /* evt injection queue */
90
91         bool vs_events_missed; /* any missed events, protected by vq->mutex */
92         int vs_events_nr; /* num of pending events, protected by vq->mutex */
93 };
94
95 /* Local pointer to allocated TCM configfs fabric module */
96 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
97
98 static struct workqueue_struct *tcm_vhost_workqueue;
99
100 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
101 static DEFINE_MUTEX(tcm_vhost_mutex);
102 static LIST_HEAD(tcm_vhost_list);
103
104 static int iov_num_pages(struct iovec *iov)
105 {
106         return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
107                ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
108 }
109
110 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
111 {
112         return 1;
113 }
114
115 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
116 {
117         return 0;
118 }
119
120 static char *tcm_vhost_get_fabric_name(void)
121 {
122         return "vhost";
123 }
124
125 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
126 {
127         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
128                                 struct tcm_vhost_tpg, se_tpg);
129         struct tcm_vhost_tport *tport = tpg->tport;
130
131         switch (tport->tport_proto_id) {
132         case SCSI_PROTOCOL_SAS:
133                 return sas_get_fabric_proto_ident(se_tpg);
134         case SCSI_PROTOCOL_FCP:
135                 return fc_get_fabric_proto_ident(se_tpg);
136         case SCSI_PROTOCOL_ISCSI:
137                 return iscsi_get_fabric_proto_ident(se_tpg);
138         default:
139                 pr_err("Unknown tport_proto_id: 0x%02x, using"
140                         " SAS emulation\n", tport->tport_proto_id);
141                 break;
142         }
143
144         return sas_get_fabric_proto_ident(se_tpg);
145 }
146
147 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
148 {
149         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
150                                 struct tcm_vhost_tpg, se_tpg);
151         struct tcm_vhost_tport *tport = tpg->tport;
152
153         return &tport->tport_name[0];
154 }
155
156 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
157 {
158         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
159                                 struct tcm_vhost_tpg, se_tpg);
160         return tpg->tport_tpgt;
161 }
162
163 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
164 {
165         return 1;
166 }
167
168 static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
169         struct se_node_acl *se_nacl,
170         struct t10_pr_registration *pr_reg,
171         int *format_code,
172         unsigned char *buf)
173 {
174         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
175                                 struct tcm_vhost_tpg, se_tpg);
176         struct tcm_vhost_tport *tport = tpg->tport;
177
178         switch (tport->tport_proto_id) {
179         case SCSI_PROTOCOL_SAS:
180                 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
181                                         format_code, buf);
182         case SCSI_PROTOCOL_FCP:
183                 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
184                                         format_code, buf);
185         case SCSI_PROTOCOL_ISCSI:
186                 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
187                                         format_code, buf);
188         default:
189                 pr_err("Unknown tport_proto_id: 0x%02x, using"
190                         " SAS emulation\n", tport->tport_proto_id);
191                 break;
192         }
193
194         return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
195                         format_code, buf);
196 }
197
198 static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
199         struct se_node_acl *se_nacl,
200         struct t10_pr_registration *pr_reg,
201         int *format_code)
202 {
203         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
204                                 struct tcm_vhost_tpg, se_tpg);
205         struct tcm_vhost_tport *tport = tpg->tport;
206
207         switch (tport->tport_proto_id) {
208         case SCSI_PROTOCOL_SAS:
209                 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
210                                         format_code);
211         case SCSI_PROTOCOL_FCP:
212                 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
213                                         format_code);
214         case SCSI_PROTOCOL_ISCSI:
215                 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
216                                         format_code);
217         default:
218                 pr_err("Unknown tport_proto_id: 0x%02x, using"
219                         " SAS emulation\n", tport->tport_proto_id);
220                 break;
221         }
222
223         return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
224                         format_code);
225 }
226
227 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
228         const char *buf,
229         u32 *out_tid_len,
230         char **port_nexus_ptr)
231 {
232         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
233                                 struct tcm_vhost_tpg, se_tpg);
234         struct tcm_vhost_tport *tport = tpg->tport;
235
236         switch (tport->tport_proto_id) {
237         case SCSI_PROTOCOL_SAS:
238                 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
239                                         port_nexus_ptr);
240         case SCSI_PROTOCOL_FCP:
241                 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
242                                         port_nexus_ptr);
243         case SCSI_PROTOCOL_ISCSI:
244                 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
245                                         port_nexus_ptr);
246         default:
247                 pr_err("Unknown tport_proto_id: 0x%02x, using"
248                         " SAS emulation\n", tport->tport_proto_id);
249                 break;
250         }
251
252         return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
253                         port_nexus_ptr);
254 }
255
256 static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
257         struct se_portal_group *se_tpg)
258 {
259         struct tcm_vhost_nacl *nacl;
260
261         nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
262         if (!nacl) {
263                 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
264                 return NULL;
265         }
266
267         return &nacl->se_node_acl;
268 }
269
270 static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
271         struct se_node_acl *se_nacl)
272 {
273         struct tcm_vhost_nacl *nacl = container_of(se_nacl,
274                         struct tcm_vhost_nacl, se_node_acl);
275         kfree(nacl);
276 }
277
278 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
279 {
280         return 1;
281 }
282
283 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
284 {
285         return;
286 }
287
288 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
289 {
290         return 0;
291 }
292
293 static void tcm_vhost_close_session(struct se_session *se_sess)
294 {
295         return;
296 }
297
298 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
299 {
300         return 0;
301 }
302
303 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
304 {
305         /* Go ahead and process the write immediately */
306         target_execute_cmd(se_cmd);
307         return 0;
308 }
309
310 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
311 {
312         return 0;
313 }
314
315 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
316 {
317         return;
318 }
319
320 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
321 {
322         return 0;
323 }
324
325 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
326 {
327         return 0;
328 }
329
330 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
331 {
332         struct vhost_scsi *vs = tv_cmd->tvc_vhost;
333
334         llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
335
336         vhost_work_queue(&vs->dev, &vs->vs_completion_work);
337 }
338
339 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
340 {
341         struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
342                                 struct tcm_vhost_cmd, tvc_se_cmd);
343         vhost_scsi_complete_cmd(tv_cmd);
344         return 0;
345 }
346
347 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
348 {
349         struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
350                                 struct tcm_vhost_cmd, tvc_se_cmd);
351         vhost_scsi_complete_cmd(tv_cmd);
352         return 0;
353 }
354
355 static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
356 {
357         return 0;
358 }
359
360 static void tcm_vhost_free_evt(struct vhost_scsi *vs, struct tcm_vhost_evt *evt)
361 {
362         vs->vs_events_nr--;
363         kfree(evt);
364 }
365
366 static struct tcm_vhost_evt *tcm_vhost_allocate_evt(struct vhost_scsi *vs,
367         u32 event, u32 reason)
368 {
369         struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
370         struct tcm_vhost_evt *evt;
371
372         if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
373                 vs->vs_events_missed = true;
374                 return NULL;
375         }
376
377         evt = kzalloc(sizeof(*evt), GFP_KERNEL);
378         if (!evt) {
379                 vq_err(vq, "Failed to allocate tcm_vhost_evt\n");
380                 vs->vs_events_missed = true;
381                 return NULL;
382         }
383
384         evt->event.event = event;
385         evt->event.reason = reason;
386         vs->vs_events_nr++;
387
388         return evt;
389 }
390
391 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
392 {
393         struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
394
395         /* TODO locking against target/backend threads? */
396         transport_generic_free_cmd(se_cmd, 1);
397
398         if (tv_cmd->tvc_sgl_count) {
399                 u32 i;
400                 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
401                         put_page(sg_page(&tv_cmd->tvc_sgl[i]));
402
403                 kfree(tv_cmd->tvc_sgl);
404         }
405
406         kfree(tv_cmd);
407 }
408
409 static void tcm_vhost_do_evt_work(struct vhost_scsi *vs,
410         struct tcm_vhost_evt *evt)
411 {
412         struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
413         struct virtio_scsi_event *event = &evt->event;
414         struct virtio_scsi_event __user *eventp;
415         unsigned out, in;
416         int head, ret;
417
418         if (!vq->private_data) {
419                 vs->vs_events_missed = true;
420                 return;
421         }
422
423 again:
424         vhost_disable_notify(&vs->dev, vq);
425         head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
426                         ARRAY_SIZE(vq->iov), &out, &in,
427                         NULL, NULL);
428         if (head < 0) {
429                 vs->vs_events_missed = true;
430                 return;
431         }
432         if (head == vq->num) {
433                 if (vhost_enable_notify(&vs->dev, vq))
434                         goto again;
435                 vs->vs_events_missed = true;
436                 return;
437         }
438
439         if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
440                 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
441                                 vq->iov[out].iov_len);
442                 vs->vs_events_missed = true;
443                 return;
444         }
445
446         if (vs->vs_events_missed) {
447                 event->event |= VIRTIO_SCSI_T_EVENTS_MISSED;
448                 vs->vs_events_missed = false;
449         }
450
451         eventp = vq->iov[out].iov_base;
452         ret = __copy_to_user(eventp, event, sizeof(*event));
453         if (!ret)
454                 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
455         else
456                 vq_err(vq, "Faulted on tcm_vhost_send_event\n");
457 }
458
459 static void tcm_vhost_evt_work(struct vhost_work *work)
460 {
461         struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
462                                         vs_event_work);
463         struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
464         struct tcm_vhost_evt *evt;
465         struct llist_node *llnode;
466
467         mutex_lock(&vq->mutex);
468         llnode = llist_del_all(&vs->vs_event_list);
469         while (llnode) {
470                 evt = llist_entry(llnode, struct tcm_vhost_evt, list);
471                 llnode = llist_next(llnode);
472                 tcm_vhost_do_evt_work(vs, evt);
473                 tcm_vhost_free_evt(vs, evt);
474         }
475         mutex_unlock(&vq->mutex);
476 }
477
478 /* Fill in status and signal that we are done processing this command
479  *
480  * This is scheduled in the vhost work queue so we are called with the owner
481  * process mm and can access the vring.
482  */
483 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
484 {
485         struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
486                                         vs_completion_work);
487         DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
488         struct virtio_scsi_cmd_resp v_rsp;
489         struct tcm_vhost_cmd *tv_cmd;
490         struct llist_node *llnode;
491         struct se_cmd *se_cmd;
492         int ret, vq;
493
494         bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
495         llnode = llist_del_all(&vs->vs_completion_list);
496         while (llnode) {
497                 tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
498                                      tvc_completion_list);
499                 llnode = llist_next(llnode);
500                 se_cmd = &tv_cmd->tvc_se_cmd;
501
502                 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
503                         tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
504
505                 memset(&v_rsp, 0, sizeof(v_rsp));
506                 v_rsp.resid = se_cmd->residual_count;
507                 /* TODO is status_qualifier field needed? */
508                 v_rsp.status = se_cmd->scsi_status;
509                 v_rsp.sense_len = se_cmd->scsi_sense_length;
510                 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
511                        v_rsp.sense_len);
512                 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
513                 if (likely(ret == 0)) {
514                         vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0);
515                         vq = tv_cmd->tvc_vq - vs->vqs;
516                         __set_bit(vq, signal);
517                 } else
518                         pr_err("Faulted on virtio_scsi_cmd_resp\n");
519
520                 vhost_scsi_free_cmd(tv_cmd);
521         }
522
523         vq = -1;
524         while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
525                 < VHOST_SCSI_MAX_VQ)
526                 vhost_signal(&vs->dev, &vs->vqs[vq]);
527 }
528
529 static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
530         struct tcm_vhost_tpg *tv_tpg,
531         struct virtio_scsi_cmd_req *v_req,
532         u32 exp_data_len,
533         int data_direction)
534 {
535         struct tcm_vhost_cmd *tv_cmd;
536         struct tcm_vhost_nexus *tv_nexus;
537
538         tv_nexus = tv_tpg->tpg_nexus;
539         if (!tv_nexus) {
540                 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
541                 return ERR_PTR(-EIO);
542         }
543
544         tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
545         if (!tv_cmd) {
546                 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
547                 return ERR_PTR(-ENOMEM);
548         }
549         tv_cmd->tvc_tag = v_req->tag;
550         tv_cmd->tvc_task_attr = v_req->task_attr;
551         tv_cmd->tvc_exp_data_len = exp_data_len;
552         tv_cmd->tvc_data_direction = data_direction;
553         tv_cmd->tvc_nexus = tv_nexus;
554
555         return tv_cmd;
556 }
557
558 /*
559  * Map a user memory range into a scatterlist
560  *
561  * Returns the number of scatterlist entries used or -errno on error.
562  */
563 static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
564         unsigned int sgl_count, struct iovec *iov, int write)
565 {
566         unsigned int npages = 0, pages_nr, offset, nbytes;
567         struct scatterlist *sg = sgl;
568         void __user *ptr = iov->iov_base;
569         size_t len = iov->iov_len;
570         struct page **pages;
571         int ret, i;
572
573         pages_nr = iov_num_pages(iov);
574         if (pages_nr > sgl_count)
575                 return -ENOBUFS;
576
577         pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
578         if (!pages)
579                 return -ENOMEM;
580
581         ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
582         /* No pages were pinned */
583         if (ret < 0)
584                 goto out;
585         /* Less pages pinned than wanted */
586         if (ret != pages_nr) {
587                 for (i = 0; i < ret; i++)
588                         put_page(pages[i]);
589                 ret = -EFAULT;
590                 goto out;
591         }
592
593         while (len > 0) {
594                 offset = (uintptr_t)ptr & ~PAGE_MASK;
595                 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
596                 sg_set_page(sg, pages[npages], nbytes, offset);
597                 ptr += nbytes;
598                 len -= nbytes;
599                 sg++;
600                 npages++;
601         }
602
603 out:
604         kfree(pages);
605         return ret;
606 }
607
608 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
609         struct iovec *iov, unsigned int niov, int write)
610 {
611         int ret;
612         unsigned int i;
613         u32 sgl_count;
614         struct scatterlist *sg;
615
616         /*
617          * Find out how long sglist needs to be
618          */
619         sgl_count = 0;
620         for (i = 0; i < niov; i++)
621                 sgl_count += iov_num_pages(&iov[i]);
622
623         /* TODO overflow checking */
624
625         sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
626         if (!sg)
627                 return -ENOMEM;
628         pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
629                sg, sgl_count, !sg);
630         sg_init_table(sg, sgl_count);
631
632         tv_cmd->tvc_sgl = sg;
633         tv_cmd->tvc_sgl_count = sgl_count;
634
635         pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
636         for (i = 0; i < niov; i++) {
637                 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
638                 if (ret < 0) {
639                         for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
640                                 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
641                         kfree(tv_cmd->tvc_sgl);
642                         tv_cmd->tvc_sgl = NULL;
643                         tv_cmd->tvc_sgl_count = 0;
644                         return ret;
645                 }
646
647                 sg += ret;
648                 sgl_count -= ret;
649         }
650         return 0;
651 }
652
653 static void tcm_vhost_submission_work(struct work_struct *work)
654 {
655         struct tcm_vhost_cmd *tv_cmd =
656                 container_of(work, struct tcm_vhost_cmd, work);
657         struct tcm_vhost_nexus *tv_nexus;
658         struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
659         struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
660         int rc, sg_no_bidi = 0;
661
662         if (tv_cmd->tvc_sgl_count) {
663                 sg_ptr = tv_cmd->tvc_sgl;
664 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
665 #if 0
666                 if (se_cmd->se_cmd_flags & SCF_BIDI) {
667                         sg_bidi_ptr = NULL;
668                         sg_no_bidi = 0;
669                 }
670 #endif
671         } else {
672                 sg_ptr = NULL;
673         }
674         tv_nexus = tv_cmd->tvc_nexus;
675
676         rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
677                         tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
678                         tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
679                         tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
680                         0, sg_ptr, tv_cmd->tvc_sgl_count,
681                         sg_bidi_ptr, sg_no_bidi);
682         if (rc < 0) {
683                 transport_send_check_condition_and_sense(se_cmd,
684                                 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
685                 transport_generic_free_cmd(se_cmd, 0);
686         }
687 }
688
689 static void vhost_scsi_send_bad_target(struct vhost_scsi *vs,
690         struct vhost_virtqueue *vq, int head, unsigned out)
691 {
692         struct virtio_scsi_cmd_resp __user *resp;
693         struct virtio_scsi_cmd_resp rsp;
694         int ret;
695
696         memset(&rsp, 0, sizeof(rsp));
697         rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
698         resp = vq->iov[out].iov_base;
699         ret = __copy_to_user(resp, &rsp, sizeof(rsp));
700         if (!ret)
701                 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
702         else
703                 pr_err("Faulted on virtio_scsi_cmd_resp\n");
704 }
705
706 static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
707         struct vhost_virtqueue *vq)
708 {
709         struct tcm_vhost_tpg **vs_tpg;
710         struct virtio_scsi_cmd_req v_req;
711         struct tcm_vhost_tpg *tv_tpg;
712         struct tcm_vhost_cmd *tv_cmd;
713         u32 exp_data_len, data_first, data_num, data_direction;
714         unsigned out, in, i;
715         int head, ret;
716         u8 target;
717
718         /*
719          * We can handle the vq only after the endpoint is setup by calling the
720          * VHOST_SCSI_SET_ENDPOINT ioctl.
721          *
722          * TODO: Check that we are running from vhost_worker which acts
723          * as read-side critical section for vhost kind of RCU.
724          * See the comments in struct vhost_virtqueue in drivers/vhost/vhost.h
725          */
726         vs_tpg = rcu_dereference_check(vq->private_data, 1);
727         if (!vs_tpg)
728                 return;
729
730         mutex_lock(&vq->mutex);
731         vhost_disable_notify(&vs->dev, vq);
732
733         for (;;) {
734                 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
735                                         ARRAY_SIZE(vq->iov), &out, &in,
736                                         NULL, NULL);
737                 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
738                                         head, out, in);
739                 /* On error, stop handling until the next kick. */
740                 if (unlikely(head < 0))
741                         break;
742                 /* Nothing new?  Wait for eventfd to tell us they refilled. */
743                 if (head == vq->num) {
744                         if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
745                                 vhost_disable_notify(&vs->dev, vq);
746                                 continue;
747                         }
748                         break;
749                 }
750
751 /* FIXME: BIDI operation */
752                 if (out == 1 && in == 1) {
753                         data_direction = DMA_NONE;
754                         data_first = 0;
755                         data_num = 0;
756                 } else if (out == 1 && in > 1) {
757                         data_direction = DMA_FROM_DEVICE;
758                         data_first = out + 1;
759                         data_num = in - 1;
760                 } else if (out > 1 && in == 1) {
761                         data_direction = DMA_TO_DEVICE;
762                         data_first = 1;
763                         data_num = out - 1;
764                 } else {
765                         vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
766                                         out, in);
767                         break;
768                 }
769
770                 /*
771                  * Check for a sane resp buffer so we can report errors to
772                  * the guest.
773                  */
774                 if (unlikely(vq->iov[out].iov_len !=
775                                         sizeof(struct virtio_scsi_cmd_resp))) {
776                         vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
777                                 " bytes\n", vq->iov[out].iov_len);
778                         break;
779                 }
780
781                 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
782                         vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
783                                 " bytes\n", vq->iov[0].iov_len);
784                         break;
785                 }
786                 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
787                         " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
788                 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
789                                 sizeof(v_req));
790                 if (unlikely(ret)) {
791                         vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
792                         break;
793                 }
794
795                 /* Extract the tpgt */
796                 target = v_req.lun[1];
797                 tv_tpg = ACCESS_ONCE(vs_tpg[target]);
798
799                 /* Target does not exist, fail the request */
800                 if (unlikely(!tv_tpg)) {
801                         vhost_scsi_send_bad_target(vs, vq, head, out);
802                         continue;
803                 }
804
805                 exp_data_len = 0;
806                 for (i = 0; i < data_num; i++)
807                         exp_data_len += vq->iov[data_first + i].iov_len;
808
809                 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
810                                         exp_data_len, data_direction);
811                 if (IS_ERR(tv_cmd)) {
812                         vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
813                                         PTR_ERR(tv_cmd));
814                         goto err_cmd;
815                 }
816                 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
817                         ": %d\n", tv_cmd, exp_data_len, data_direction);
818
819                 tv_cmd->tvc_vhost = vs;
820                 tv_cmd->tvc_vq = vq;
821                 tv_cmd->tvc_resp = vq->iov[out].iov_base;
822
823                 /*
824                  * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
825                  * that will be used by tcm_vhost_new_cmd_map() and down into
826                  * target_setup_cmd_from_cdb()
827                  */
828                 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
829                 /*
830                  * Check that the recieved CDB size does not exceeded our
831                  * hardcoded max for tcm_vhost
832                  */
833                 /* TODO what if cdb was too small for varlen cdb header? */
834                 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
835                                         TCM_VHOST_MAX_CDB_SIZE)) {
836                         vq_err(vq, "Received SCSI CDB with command_size: %d that"
837                                 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
838                                 scsi_command_size(tv_cmd->tvc_cdb),
839                                 TCM_VHOST_MAX_CDB_SIZE);
840                         goto err_free;
841                 }
842                 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
843
844                 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
845                         tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
846
847                 if (data_direction != DMA_NONE) {
848                         ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
849                                         &vq->iov[data_first], data_num,
850                                         data_direction == DMA_TO_DEVICE);
851                         if (unlikely(ret)) {
852                                 vq_err(vq, "Failed to map iov to sgl\n");
853                                 goto err_free;
854                         }
855                 }
856
857                 /*
858                  * Save the descriptor from vhost_get_vq_desc() to be used to
859                  * complete the virtio-scsi request in TCM callback context via
860                  * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
861                  */
862                 tv_cmd->tvc_vq_desc = head;
863                 /*
864                  * Dispatch tv_cmd descriptor for cmwq execution in process
865                  * context provided by tcm_vhost_workqueue.  This also ensures
866                  * tv_cmd is executed on the same kworker CPU as this vhost
867                  * thread to gain positive L2 cache locality effects..
868                  */
869                 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
870                 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
871         }
872
873         mutex_unlock(&vq->mutex);
874         return;
875
876 err_free:
877         vhost_scsi_free_cmd(tv_cmd);
878 err_cmd:
879         vhost_scsi_send_bad_target(vs, vq, head, out);
880         mutex_unlock(&vq->mutex);
881 }
882
883 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
884 {
885         pr_debug("%s: The handling func for control queue.\n", __func__);
886 }
887
888 static void tcm_vhost_send_evt(struct vhost_scsi *vs, struct tcm_vhost_tpg *tpg,
889         struct se_lun *lun, u32 event, u32 reason)
890 {
891         struct tcm_vhost_evt *evt;
892
893         evt = tcm_vhost_allocate_evt(vs, event, reason);
894         if (!evt)
895                 return;
896
897         if (tpg && lun) {
898                 /* TODO: share lun setup code with virtio-scsi.ko */
899                 /*
900                  * Note: evt->event is zeroed when we allocate it and
901                  * lun[4-7] need to be zero according to virtio-scsi spec.
902                  */
903                 evt->event.lun[0] = 0x01;
904                 evt->event.lun[1] = tpg->tport_tpgt & 0xFF;
905                 if (lun->unpacked_lun >= 256)
906                         evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
907                 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
908         }
909
910         llist_add(&evt->list, &vs->vs_event_list);
911         vhost_work_queue(&vs->dev, &vs->vs_event_work);
912 }
913
914 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
915 {
916         struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
917                                                 poll.work);
918         struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
919
920         mutex_lock(&vq->mutex);
921         if (!vq->private_data)
922                 goto out;
923
924         if (vs->vs_events_missed)
925                 tcm_vhost_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
926 out:
927         mutex_unlock(&vq->mutex);
928 }
929
930 static void vhost_scsi_handle_kick(struct vhost_work *work)
931 {
932         struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
933                                                 poll.work);
934         struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
935
936         vhost_scsi_handle_vq(vs, vq);
937 }
938
939 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
940 {
941         vhost_poll_flush(&vs->dev.vqs[index].poll);
942 }
943
944 static void vhost_scsi_flush(struct vhost_scsi *vs)
945 {
946         int i;
947
948         for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
949                 vhost_scsi_flush_vq(vs, i);
950         vhost_work_flush(&vs->dev, &vs->vs_completion_work);
951         vhost_work_flush(&vs->dev, &vs->vs_event_work);
952 }
953
954 /*
955  * Called from vhost_scsi_ioctl() context to walk the list of available
956  * tcm_vhost_tpg with an active struct tcm_vhost_nexus
957  *
958  *  The lock nesting rule is:
959  *    tcm_vhost_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
960  */
961 static int vhost_scsi_set_endpoint(
962         struct vhost_scsi *vs,
963         struct vhost_scsi_target *t)
964 {
965         struct tcm_vhost_tport *tv_tport;
966         struct tcm_vhost_tpg *tv_tpg;
967         struct tcm_vhost_tpg **vs_tpg;
968         struct vhost_virtqueue *vq;
969         int index, ret, i, len;
970         bool match = false;
971
972         mutex_lock(&tcm_vhost_mutex);
973         mutex_lock(&vs->dev.mutex);
974
975         /* Verify that ring has been setup correctly. */
976         for (index = 0; index < vs->dev.nvqs; ++index) {
977                 /* Verify that ring has been setup correctly. */
978                 if (!vhost_vq_access_ok(&vs->vqs[index])) {
979                         ret = -EFAULT;
980                         goto out;
981                 }
982         }
983
984         len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
985         vs_tpg = kzalloc(len, GFP_KERNEL);
986         if (!vs_tpg) {
987                 ret = -ENOMEM;
988                 goto out;
989         }
990         if (vs->vs_tpg)
991                 memcpy(vs_tpg, vs->vs_tpg, len);
992
993         list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
994                 mutex_lock(&tv_tpg->tv_tpg_mutex);
995                 if (!tv_tpg->tpg_nexus) {
996                         mutex_unlock(&tv_tpg->tv_tpg_mutex);
997                         continue;
998                 }
999                 if (tv_tpg->tv_tpg_vhost_count != 0) {
1000                         mutex_unlock(&tv_tpg->tv_tpg_mutex);
1001                         continue;
1002                 }
1003                 tv_tport = tv_tpg->tport;
1004
1005                 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1006                         if (vs->vs_tpg && vs->vs_tpg[tv_tpg->tport_tpgt]) {
1007                                 kfree(vs_tpg);
1008                                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1009                                 ret = -EEXIST;
1010                                 goto out;
1011                         }
1012                         tv_tpg->tv_tpg_vhost_count++;
1013                         tv_tpg->vhost_scsi = vs;
1014                         vs_tpg[tv_tpg->tport_tpgt] = tv_tpg;
1015                         smp_mb__after_atomic_inc();
1016                         match = true;
1017                 }
1018                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1019         }
1020
1021         if (match) {
1022                 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1023                        sizeof(vs->vs_vhost_wwpn));
1024                 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1025                         vq = &vs->vqs[i];
1026                         /* Flushing the vhost_work acts as synchronize_rcu */
1027                         mutex_lock(&vq->mutex);
1028                         rcu_assign_pointer(vq->private_data, vs_tpg);
1029                         vhost_init_used(vq);
1030                         mutex_unlock(&vq->mutex);
1031                 }
1032                 ret = 0;
1033         } else {
1034                 ret = -EEXIST;
1035         }
1036
1037         /*
1038          * Act as synchronize_rcu to make sure access to
1039          * old vs->vs_tpg is finished.
1040          */
1041         vhost_scsi_flush(vs);
1042         kfree(vs->vs_tpg);
1043         vs->vs_tpg = vs_tpg;
1044
1045 out:
1046         mutex_unlock(&vs->dev.mutex);
1047         mutex_unlock(&tcm_vhost_mutex);
1048         return ret;
1049 }
1050
1051 static int vhost_scsi_clear_endpoint(
1052         struct vhost_scsi *vs,
1053         struct vhost_scsi_target *t)
1054 {
1055         struct tcm_vhost_tport *tv_tport;
1056         struct tcm_vhost_tpg *tv_tpg;
1057         struct vhost_virtqueue *vq;
1058         bool match = false;
1059         int index, ret, i;
1060         u8 target;
1061
1062         mutex_lock(&tcm_vhost_mutex);
1063         mutex_lock(&vs->dev.mutex);
1064         /* Verify that ring has been setup correctly. */
1065         for (index = 0; index < vs->dev.nvqs; ++index) {
1066                 if (!vhost_vq_access_ok(&vs->vqs[index])) {
1067                         ret = -EFAULT;
1068                         goto err_dev;
1069                 }
1070         }
1071
1072         if (!vs->vs_tpg) {
1073                 ret = 0;
1074                 goto err_dev;
1075         }
1076
1077         for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1078                 target = i;
1079                 tv_tpg = vs->vs_tpg[target];
1080                 if (!tv_tpg)
1081                         continue;
1082
1083                 mutex_lock(&tv_tpg->tv_tpg_mutex);
1084                 tv_tport = tv_tpg->tport;
1085                 if (!tv_tport) {
1086                         ret = -ENODEV;
1087                         goto err_tpg;
1088                 }
1089
1090                 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1091                         pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
1092                                 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1093                                 tv_tport->tport_name, tv_tpg->tport_tpgt,
1094                                 t->vhost_wwpn, t->vhost_tpgt);
1095                         ret = -EINVAL;
1096                         goto err_tpg;
1097                 }
1098                 tv_tpg->tv_tpg_vhost_count--;
1099                 tv_tpg->vhost_scsi = NULL;
1100                 vs->vs_tpg[target] = NULL;
1101                 match = true;
1102                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1103         }
1104         if (match) {
1105                 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1106                         vq = &vs->vqs[i];
1107                         /* Flushing the vhost_work acts as synchronize_rcu */
1108                         mutex_lock(&vq->mutex);
1109                         rcu_assign_pointer(vq->private_data, NULL);
1110                         mutex_unlock(&vq->mutex);
1111                 }
1112         }
1113         /*
1114          * Act as synchronize_rcu to make sure access to
1115          * old vs->vs_tpg is finished.
1116          */
1117         vhost_scsi_flush(vs);
1118         kfree(vs->vs_tpg);
1119         vs->vs_tpg = NULL;
1120         WARN_ON(vs->vs_events_nr);
1121         mutex_unlock(&vs->dev.mutex);
1122         mutex_unlock(&tcm_vhost_mutex);
1123         return 0;
1124
1125 err_tpg:
1126         mutex_unlock(&tv_tpg->tv_tpg_mutex);
1127 err_dev:
1128         mutex_unlock(&vs->dev.mutex);
1129         mutex_unlock(&tcm_vhost_mutex);
1130         return ret;
1131 }
1132
1133 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1134 {
1135         if (features & ~VHOST_SCSI_FEATURES)
1136                 return -EOPNOTSUPP;
1137
1138         mutex_lock(&vs->dev.mutex);
1139         if ((features & (1 << VHOST_F_LOG_ALL)) &&
1140             !vhost_log_access_ok(&vs->dev)) {
1141                 mutex_unlock(&vs->dev.mutex);
1142                 return -EFAULT;
1143         }
1144         vs->dev.acked_features = features;
1145         smp_wmb();
1146         vhost_scsi_flush(vs);
1147         mutex_unlock(&vs->dev.mutex);
1148         return 0;
1149 }
1150
1151 static int vhost_scsi_open(struct inode *inode, struct file *f)
1152 {
1153         struct vhost_scsi *s;
1154         int r, i;
1155
1156         s = kzalloc(sizeof(*s), GFP_KERNEL);
1157         if (!s)
1158                 return -ENOMEM;
1159
1160         vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
1161         vhost_work_init(&s->vs_event_work, tcm_vhost_evt_work);
1162
1163         s->vs_events_nr = 0;
1164         s->vs_events_missed = false;
1165
1166         s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
1167         s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
1168         for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++)
1169                 s->vqs[i].handle_kick = vhost_scsi_handle_kick;
1170         r = vhost_dev_init(&s->dev, s->vqs, VHOST_SCSI_MAX_VQ);
1171         if (r < 0) {
1172                 kfree(s);
1173                 return r;
1174         }
1175
1176         f->private_data = s;
1177         return 0;
1178 }
1179
1180 static int vhost_scsi_release(struct inode *inode, struct file *f)
1181 {
1182         struct vhost_scsi *s = f->private_data;
1183         struct vhost_scsi_target t;
1184
1185         mutex_lock(&s->dev.mutex);
1186         memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1187         mutex_unlock(&s->dev.mutex);
1188         vhost_scsi_clear_endpoint(s, &t);
1189         vhost_dev_stop(&s->dev);
1190         vhost_dev_cleanup(&s->dev, false);
1191         /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1192         vhost_scsi_flush(s);
1193         kfree(s);
1194         return 0;
1195 }
1196
1197 static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
1198                                 unsigned long arg)
1199 {
1200         struct vhost_scsi *vs = f->private_data;
1201         struct vhost_scsi_target backend;
1202         void __user *argp = (void __user *)arg;
1203         u64 __user *featurep = argp;
1204         u32 __user *eventsp = argp;
1205         u32 events_missed;
1206         u64 features;
1207         int r, abi_version = VHOST_SCSI_ABI_VERSION;
1208         struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
1209
1210         switch (ioctl) {
1211         case VHOST_SCSI_SET_ENDPOINT:
1212                 if (copy_from_user(&backend, argp, sizeof backend))
1213                         return -EFAULT;
1214                 if (backend.reserved != 0)
1215                         return -EOPNOTSUPP;
1216
1217                 return vhost_scsi_set_endpoint(vs, &backend);
1218         case VHOST_SCSI_CLEAR_ENDPOINT:
1219                 if (copy_from_user(&backend, argp, sizeof backend))
1220                         return -EFAULT;
1221                 if (backend.reserved != 0)
1222                         return -EOPNOTSUPP;
1223
1224                 return vhost_scsi_clear_endpoint(vs, &backend);
1225         case VHOST_SCSI_GET_ABI_VERSION:
1226                 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1227                         return -EFAULT;
1228                 return 0;
1229         case VHOST_SCSI_SET_EVENTS_MISSED:
1230                 if (get_user(events_missed, eventsp))
1231                         return -EFAULT;
1232                 mutex_lock(&vq->mutex);
1233                 vs->vs_events_missed = events_missed;
1234                 mutex_unlock(&vq->mutex);
1235                 return 0;
1236         case VHOST_SCSI_GET_EVENTS_MISSED:
1237                 mutex_lock(&vq->mutex);
1238                 events_missed = vs->vs_events_missed;
1239                 mutex_unlock(&vq->mutex);
1240                 if (put_user(events_missed, eventsp))
1241                         return -EFAULT;
1242                 return 0;
1243         case VHOST_GET_FEATURES:
1244                 features = VHOST_SCSI_FEATURES;
1245                 if (copy_to_user(featurep, &features, sizeof features))
1246                         return -EFAULT;
1247                 return 0;
1248         case VHOST_SET_FEATURES:
1249                 if (copy_from_user(&features, featurep, sizeof features))
1250                         return -EFAULT;
1251                 return vhost_scsi_set_features(vs, features);
1252         default:
1253                 mutex_lock(&vs->dev.mutex);
1254                 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1255                 /* TODO: flush backend after dev ioctl. */
1256                 if (r == -ENOIOCTLCMD)
1257                         r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1258                 mutex_unlock(&vs->dev.mutex);
1259                 return r;
1260         }
1261 }
1262
1263 #ifdef CONFIG_COMPAT
1264 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1265                                 unsigned long arg)
1266 {
1267         return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1268 }
1269 #endif
1270
1271 static const struct file_operations vhost_scsi_fops = {
1272         .owner          = THIS_MODULE,
1273         .release        = vhost_scsi_release,
1274         .unlocked_ioctl = vhost_scsi_ioctl,
1275 #ifdef CONFIG_COMPAT
1276         .compat_ioctl   = vhost_scsi_compat_ioctl,
1277 #endif
1278         .open           = vhost_scsi_open,
1279         .llseek         = noop_llseek,
1280 };
1281
1282 static struct miscdevice vhost_scsi_misc = {
1283         MISC_DYNAMIC_MINOR,
1284         "vhost-scsi",
1285         &vhost_scsi_fops,
1286 };
1287
1288 static int __init vhost_scsi_register(void)
1289 {
1290         return misc_register(&vhost_scsi_misc);
1291 }
1292
1293 static int vhost_scsi_deregister(void)
1294 {
1295         return misc_deregister(&vhost_scsi_misc);
1296 }
1297
1298 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1299 {
1300         switch (tport->tport_proto_id) {
1301         case SCSI_PROTOCOL_SAS:
1302                 return "SAS";
1303         case SCSI_PROTOCOL_FCP:
1304                 return "FCP";
1305         case SCSI_PROTOCOL_ISCSI:
1306                 return "iSCSI";
1307         default:
1308                 break;
1309         }
1310
1311         return "Unknown";
1312 }
1313
1314 static void tcm_vhost_do_plug(struct tcm_vhost_tpg *tpg,
1315         struct se_lun *lun, bool plug)
1316 {
1317
1318         struct vhost_scsi *vs = tpg->vhost_scsi;
1319         struct vhost_virtqueue *vq;
1320         u32 reason;
1321
1322         if (!vs)
1323                 return;
1324
1325         mutex_lock(&vs->dev.mutex);
1326         if (!vhost_has_feature(&vs->dev, VIRTIO_SCSI_F_HOTPLUG)) {
1327                 mutex_unlock(&vs->dev.mutex);
1328                 return;
1329         }
1330
1331         if (plug)
1332                 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1333         else
1334                 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1335
1336         vq = &vs->vqs[VHOST_SCSI_VQ_EVT];
1337         mutex_lock(&vq->mutex);
1338         tcm_vhost_send_evt(vs, tpg, lun,
1339                         VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1340         mutex_unlock(&vq->mutex);
1341         mutex_unlock(&vs->dev.mutex);
1342 }
1343
1344 static void tcm_vhost_hotplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1345 {
1346         tcm_vhost_do_plug(tpg, lun, true);
1347 }
1348
1349 static void tcm_vhost_hotunplug(struct tcm_vhost_tpg *tpg, struct se_lun *lun)
1350 {
1351         tcm_vhost_do_plug(tpg, lun, false);
1352 }
1353
1354 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1355         struct se_lun *lun)
1356 {
1357         struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1358                                 struct tcm_vhost_tpg, se_tpg);
1359
1360         mutex_lock(&tcm_vhost_mutex);
1361
1362         mutex_lock(&tv_tpg->tv_tpg_mutex);
1363         tv_tpg->tv_tpg_port_count++;
1364         mutex_unlock(&tv_tpg->tv_tpg_mutex);
1365
1366         tcm_vhost_hotplug(tv_tpg, lun);
1367
1368         mutex_unlock(&tcm_vhost_mutex);
1369
1370         return 0;
1371 }
1372
1373 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1374         struct se_lun *lun)
1375 {
1376         struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1377                                 struct tcm_vhost_tpg, se_tpg);
1378
1379         mutex_lock(&tcm_vhost_mutex);
1380
1381         mutex_lock(&tv_tpg->tv_tpg_mutex);
1382         tv_tpg->tv_tpg_port_count--;
1383         mutex_unlock(&tv_tpg->tv_tpg_mutex);
1384
1385         tcm_vhost_hotunplug(tv_tpg, lun);
1386
1387         mutex_unlock(&tcm_vhost_mutex);
1388 }
1389
1390 static struct se_node_acl *tcm_vhost_make_nodeacl(
1391         struct se_portal_group *se_tpg,
1392         struct config_group *group,
1393         const char *name)
1394 {
1395         struct se_node_acl *se_nacl, *se_nacl_new;
1396         struct tcm_vhost_nacl *nacl;
1397         u64 wwpn = 0;
1398         u32 nexus_depth;
1399
1400         /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1401                 return ERR_PTR(-EINVAL); */
1402         se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1403         if (!se_nacl_new)
1404                 return ERR_PTR(-ENOMEM);
1405
1406         nexus_depth = 1;
1407         /*
1408          * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1409          * when converting a NodeACL from demo mode -> explict
1410          */
1411         se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1412                                 name, nexus_depth);
1413         if (IS_ERR(se_nacl)) {
1414                 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1415                 return se_nacl;
1416         }
1417         /*
1418          * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1419          */
1420         nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1421         nacl->iport_wwpn = wwpn;
1422
1423         return se_nacl;
1424 }
1425
1426 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1427 {
1428         struct tcm_vhost_nacl *nacl = container_of(se_acl,
1429                                 struct tcm_vhost_nacl, se_node_acl);
1430         core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1431         kfree(nacl);
1432 }
1433
1434 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1435         const char *name)
1436 {
1437         struct se_portal_group *se_tpg;
1438         struct tcm_vhost_nexus *tv_nexus;
1439
1440         mutex_lock(&tv_tpg->tv_tpg_mutex);
1441         if (tv_tpg->tpg_nexus) {
1442                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1443                 pr_debug("tv_tpg->tpg_nexus already exists\n");
1444                 return -EEXIST;
1445         }
1446         se_tpg = &tv_tpg->se_tpg;
1447
1448         tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1449         if (!tv_nexus) {
1450                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1451                 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1452                 return -ENOMEM;
1453         }
1454         /*
1455          *  Initialize the struct se_session pointer
1456          */
1457         tv_nexus->tvn_se_sess = transport_init_session();
1458         if (IS_ERR(tv_nexus->tvn_se_sess)) {
1459                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1460                 kfree(tv_nexus);
1461                 return -ENOMEM;
1462         }
1463         /*
1464          * Since we are running in 'demo mode' this call with generate a
1465          * struct se_node_acl for the tcm_vhost struct se_portal_group with
1466          * the SCSI Initiator port name of the passed configfs group 'name'.
1467          */
1468         tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1469                                 se_tpg, (unsigned char *)name);
1470         if (!tv_nexus->tvn_se_sess->se_node_acl) {
1471                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1472                 pr_debug("core_tpg_check_initiator_node_acl() failed"
1473                                 " for %s\n", name);
1474                 transport_free_session(tv_nexus->tvn_se_sess);
1475                 kfree(tv_nexus);
1476                 return -ENOMEM;
1477         }
1478         /*
1479          * Now register the TCM vhost virtual I_T Nexus as active with the
1480          * call to __transport_register_session()
1481          */
1482         __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1483                         tv_nexus->tvn_se_sess, tv_nexus);
1484         tv_tpg->tpg_nexus = tv_nexus;
1485
1486         mutex_unlock(&tv_tpg->tv_tpg_mutex);
1487         return 0;
1488 }
1489
1490 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1491 {
1492         struct se_session *se_sess;
1493         struct tcm_vhost_nexus *tv_nexus;
1494
1495         mutex_lock(&tpg->tv_tpg_mutex);
1496         tv_nexus = tpg->tpg_nexus;
1497         if (!tv_nexus) {
1498                 mutex_unlock(&tpg->tv_tpg_mutex);
1499                 return -ENODEV;
1500         }
1501
1502         se_sess = tv_nexus->tvn_se_sess;
1503         if (!se_sess) {
1504                 mutex_unlock(&tpg->tv_tpg_mutex);
1505                 return -ENODEV;
1506         }
1507
1508         if (tpg->tv_tpg_port_count != 0) {
1509                 mutex_unlock(&tpg->tv_tpg_mutex);
1510                 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1511                         " active TPG port count: %d\n",
1512                         tpg->tv_tpg_port_count);
1513                 return -EBUSY;
1514         }
1515
1516         if (tpg->tv_tpg_vhost_count != 0) {
1517                 mutex_unlock(&tpg->tv_tpg_mutex);
1518                 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1519                         " active TPG vhost count: %d\n",
1520                         tpg->tv_tpg_vhost_count);
1521                 return -EBUSY;
1522         }
1523
1524         pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1525                 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1526                 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1527         /*
1528          * Release the SCSI I_T Nexus to the emulated vhost Target Port
1529          */
1530         transport_deregister_session(tv_nexus->tvn_se_sess);
1531         tpg->tpg_nexus = NULL;
1532         mutex_unlock(&tpg->tv_tpg_mutex);
1533
1534         kfree(tv_nexus);
1535         return 0;
1536 }
1537
1538 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1539         char *page)
1540 {
1541         struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1542                                 struct tcm_vhost_tpg, se_tpg);
1543         struct tcm_vhost_nexus *tv_nexus;
1544         ssize_t ret;
1545
1546         mutex_lock(&tv_tpg->tv_tpg_mutex);
1547         tv_nexus = tv_tpg->tpg_nexus;
1548         if (!tv_nexus) {
1549                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1550                 return -ENODEV;
1551         }
1552         ret = snprintf(page, PAGE_SIZE, "%s\n",
1553                         tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1554         mutex_unlock(&tv_tpg->tv_tpg_mutex);
1555
1556         return ret;
1557 }
1558
1559 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1560         const char *page,
1561         size_t count)
1562 {
1563         struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1564                                 struct tcm_vhost_tpg, se_tpg);
1565         struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1566         unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1567         int ret;
1568         /*
1569          * Shutdown the active I_T nexus if 'NULL' is passed..
1570          */
1571         if (!strncmp(page, "NULL", 4)) {
1572                 ret = tcm_vhost_drop_nexus(tv_tpg);
1573                 return (!ret) ? count : ret;
1574         }
1575         /*
1576          * Otherwise make sure the passed virtual Initiator port WWN matches
1577          * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1578          * tcm_vhost_make_nexus().
1579          */
1580         if (strlen(page) >= TCM_VHOST_NAMELEN) {
1581                 pr_err("Emulated NAA Sas Address: %s, exceeds"
1582                                 " max: %d\n", page, TCM_VHOST_NAMELEN);
1583                 return -EINVAL;
1584         }
1585         snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1586
1587         ptr = strstr(i_port, "naa.");
1588         if (ptr) {
1589                 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1590                         pr_err("Passed SAS Initiator Port %s does not"
1591                                 " match target port protoid: %s\n", i_port,
1592                                 tcm_vhost_dump_proto_id(tport_wwn));
1593                         return -EINVAL;
1594                 }
1595                 port_ptr = &i_port[0];
1596                 goto check_newline;
1597         }
1598         ptr = strstr(i_port, "fc.");
1599         if (ptr) {
1600                 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1601                         pr_err("Passed FCP Initiator Port %s does not"
1602                                 " match target port protoid: %s\n", i_port,
1603                                 tcm_vhost_dump_proto_id(tport_wwn));
1604                         return -EINVAL;
1605                 }
1606                 port_ptr = &i_port[3]; /* Skip over "fc." */
1607                 goto check_newline;
1608         }
1609         ptr = strstr(i_port, "iqn.");
1610         if (ptr) {
1611                 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1612                         pr_err("Passed iSCSI Initiator Port %s does not"
1613                                 " match target port protoid: %s\n", i_port,
1614                                 tcm_vhost_dump_proto_id(tport_wwn));
1615                         return -EINVAL;
1616                 }
1617                 port_ptr = &i_port[0];
1618                 goto check_newline;
1619         }
1620         pr_err("Unable to locate prefix for emulated Initiator Port:"
1621                         " %s\n", i_port);
1622         return -EINVAL;
1623         /*
1624          * Clear any trailing newline for the NAA WWN
1625          */
1626 check_newline:
1627         if (i_port[strlen(i_port)-1] == '\n')
1628                 i_port[strlen(i_port)-1] = '\0';
1629
1630         ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1631         if (ret < 0)
1632                 return ret;
1633
1634         return count;
1635 }
1636
1637 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1638
1639 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1640         &tcm_vhost_tpg_nexus.attr,
1641         NULL,
1642 };
1643
1644 static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
1645         struct config_group *group,
1646         const char *name)
1647 {
1648         struct tcm_vhost_tport *tport = container_of(wwn,
1649                         struct tcm_vhost_tport, tport_wwn);
1650
1651         struct tcm_vhost_tpg *tpg;
1652         unsigned long tpgt;
1653         int ret;
1654
1655         if (strstr(name, "tpgt_") != name)
1656                 return ERR_PTR(-EINVAL);
1657         if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1658                 return ERR_PTR(-EINVAL);
1659
1660         tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1661         if (!tpg) {
1662                 pr_err("Unable to allocate struct tcm_vhost_tpg");
1663                 return ERR_PTR(-ENOMEM);
1664         }
1665         mutex_init(&tpg->tv_tpg_mutex);
1666         INIT_LIST_HEAD(&tpg->tv_tpg_list);
1667         tpg->tport = tport;
1668         tpg->tport_tpgt = tpgt;
1669
1670         ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1671                                 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1672         if (ret < 0) {
1673                 kfree(tpg);
1674                 return NULL;
1675         }
1676         mutex_lock(&tcm_vhost_mutex);
1677         list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1678         mutex_unlock(&tcm_vhost_mutex);
1679
1680         return &tpg->se_tpg;
1681 }
1682
1683 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1684 {
1685         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1686                                 struct tcm_vhost_tpg, se_tpg);
1687
1688         mutex_lock(&tcm_vhost_mutex);
1689         list_del(&tpg->tv_tpg_list);
1690         mutex_unlock(&tcm_vhost_mutex);
1691         /*
1692          * Release the virtual I_T Nexus for this vhost TPG
1693          */
1694         tcm_vhost_drop_nexus(tpg);
1695         /*
1696          * Deregister the se_tpg from TCM..
1697          */
1698         core_tpg_deregister(se_tpg);
1699         kfree(tpg);
1700 }
1701
1702 static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1703         struct config_group *group,
1704         const char *name)
1705 {
1706         struct tcm_vhost_tport *tport;
1707         char *ptr;
1708         u64 wwpn = 0;
1709         int off = 0;
1710
1711         /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1712                 return ERR_PTR(-EINVAL); */
1713
1714         tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1715         if (!tport) {
1716                 pr_err("Unable to allocate struct tcm_vhost_tport");
1717                 return ERR_PTR(-ENOMEM);
1718         }
1719         tport->tport_wwpn = wwpn;
1720         /*
1721          * Determine the emulated Protocol Identifier and Target Port Name
1722          * based on the incoming configfs directory name.
1723          */
1724         ptr = strstr(name, "naa.");
1725         if (ptr) {
1726                 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1727                 goto check_len;
1728         }
1729         ptr = strstr(name, "fc.");
1730         if (ptr) {
1731                 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1732                 off = 3; /* Skip over "fc." */
1733                 goto check_len;
1734         }
1735         ptr = strstr(name, "iqn.");
1736         if (ptr) {
1737                 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1738                 goto check_len;
1739         }
1740
1741         pr_err("Unable to locate prefix for emulated Target Port:"
1742                         " %s\n", name);
1743         kfree(tport);
1744         return ERR_PTR(-EINVAL);
1745
1746 check_len:
1747         if (strlen(name) >= TCM_VHOST_NAMELEN) {
1748                 pr_err("Emulated %s Address: %s, exceeds"
1749                         " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1750                         TCM_VHOST_NAMELEN);
1751                 kfree(tport);
1752                 return ERR_PTR(-EINVAL);
1753         }
1754         snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1755
1756         pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1757                 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1758
1759         return &tport->tport_wwn;
1760 }
1761
1762 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1763 {
1764         struct tcm_vhost_tport *tport = container_of(wwn,
1765                                 struct tcm_vhost_tport, tport_wwn);
1766
1767         pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1768                 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1769                 tport->tport_name);
1770
1771         kfree(tport);
1772 }
1773
1774 static ssize_t tcm_vhost_wwn_show_attr_version(
1775         struct target_fabric_configfs *tf,
1776         char *page)
1777 {
1778         return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1779                 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1780                 utsname()->machine);
1781 }
1782
1783 TF_WWN_ATTR_RO(tcm_vhost, version);
1784
1785 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1786         &tcm_vhost_wwn_version.attr,
1787         NULL,
1788 };
1789
1790 static struct target_core_fabric_ops tcm_vhost_ops = {
1791         .get_fabric_name                = tcm_vhost_get_fabric_name,
1792         .get_fabric_proto_ident         = tcm_vhost_get_fabric_proto_ident,
1793         .tpg_get_wwn                    = tcm_vhost_get_fabric_wwn,
1794         .tpg_get_tag                    = tcm_vhost_get_tag,
1795         .tpg_get_default_depth          = tcm_vhost_get_default_depth,
1796         .tpg_get_pr_transport_id        = tcm_vhost_get_pr_transport_id,
1797         .tpg_get_pr_transport_id_len    = tcm_vhost_get_pr_transport_id_len,
1798         .tpg_parse_pr_out_transport_id  = tcm_vhost_parse_pr_out_transport_id,
1799         .tpg_check_demo_mode            = tcm_vhost_check_true,
1800         .tpg_check_demo_mode_cache      = tcm_vhost_check_true,
1801         .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1802         .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1803         .tpg_alloc_fabric_acl           = tcm_vhost_alloc_fabric_acl,
1804         .tpg_release_fabric_acl         = tcm_vhost_release_fabric_acl,
1805         .tpg_get_inst_index             = tcm_vhost_tpg_get_inst_index,
1806         .release_cmd                    = tcm_vhost_release_cmd,
1807         .shutdown_session               = tcm_vhost_shutdown_session,
1808         .close_session                  = tcm_vhost_close_session,
1809         .sess_get_index                 = tcm_vhost_sess_get_index,
1810         .sess_get_initiator_sid         = NULL,
1811         .write_pending                  = tcm_vhost_write_pending,
1812         .write_pending_status           = tcm_vhost_write_pending_status,
1813         .set_default_node_attributes    = tcm_vhost_set_default_node_attrs,
1814         .get_task_tag                   = tcm_vhost_get_task_tag,
1815         .get_cmd_state                  = tcm_vhost_get_cmd_state,
1816         .queue_data_in                  = tcm_vhost_queue_data_in,
1817         .queue_status                   = tcm_vhost_queue_status,
1818         .queue_tm_rsp                   = tcm_vhost_queue_tm_rsp,
1819         /*
1820          * Setup callers for generic logic in target_core_fabric_configfs.c
1821          */
1822         .fabric_make_wwn                = tcm_vhost_make_tport,
1823         .fabric_drop_wwn                = tcm_vhost_drop_tport,
1824         .fabric_make_tpg                = tcm_vhost_make_tpg,
1825         .fabric_drop_tpg                = tcm_vhost_drop_tpg,
1826         .fabric_post_link               = tcm_vhost_port_link,
1827         .fabric_pre_unlink              = tcm_vhost_port_unlink,
1828         .fabric_make_np                 = NULL,
1829         .fabric_drop_np                 = NULL,
1830         .fabric_make_nodeacl            = tcm_vhost_make_nodeacl,
1831         .fabric_drop_nodeacl            = tcm_vhost_drop_nodeacl,
1832 };
1833
1834 static int tcm_vhost_register_configfs(void)
1835 {
1836         struct target_fabric_configfs *fabric;
1837         int ret;
1838
1839         pr_debug("TCM_VHOST fabric module %s on %s/%s"
1840                 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1841                 utsname()->machine);
1842         /*
1843          * Register the top level struct config_item_type with TCM core
1844          */
1845         fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1846         if (IS_ERR(fabric)) {
1847                 pr_err("target_fabric_configfs_init() failed\n");
1848                 return PTR_ERR(fabric);
1849         }
1850         /*
1851          * Setup fabric->tf_ops from our local tcm_vhost_ops
1852          */
1853         fabric->tf_ops = tcm_vhost_ops;
1854         /*
1855          * Setup default attribute lists for various fabric->tf_cit_tmpl
1856          */
1857         TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1858         TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1859         TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1860         TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1861         TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1862         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1863         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1864         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1865         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1866         /*
1867          * Register the fabric for use within TCM
1868          */
1869         ret = target_fabric_configfs_register(fabric);
1870         if (ret < 0) {
1871                 pr_err("target_fabric_configfs_register() failed"
1872                                 " for TCM_VHOST\n");
1873                 return ret;
1874         }
1875         /*
1876          * Setup our local pointer to *fabric
1877          */
1878         tcm_vhost_fabric_configfs = fabric;
1879         pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1880         return 0;
1881 };
1882
1883 static void tcm_vhost_deregister_configfs(void)
1884 {
1885         if (!tcm_vhost_fabric_configfs)
1886                 return;
1887
1888         target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1889         tcm_vhost_fabric_configfs = NULL;
1890         pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1891 };
1892
1893 static int __init tcm_vhost_init(void)
1894 {
1895         int ret = -ENOMEM;
1896         /*
1897          * Use our own dedicated workqueue for submitting I/O into
1898          * target core to avoid contention within system_wq.
1899          */
1900         tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1901         if (!tcm_vhost_workqueue)
1902                 goto out;
1903
1904         ret = vhost_scsi_register();
1905         if (ret < 0)
1906                 goto out_destroy_workqueue;
1907
1908         ret = tcm_vhost_register_configfs();
1909         if (ret < 0)
1910                 goto out_vhost_scsi_deregister;
1911
1912         return 0;
1913
1914 out_vhost_scsi_deregister:
1915         vhost_scsi_deregister();
1916 out_destroy_workqueue:
1917         destroy_workqueue(tcm_vhost_workqueue);
1918 out:
1919         return ret;
1920 };
1921
1922 static void tcm_vhost_exit(void)
1923 {
1924         tcm_vhost_deregister_configfs();
1925         vhost_scsi_deregister();
1926         destroy_workqueue(tcm_vhost_workqueue);
1927 };
1928
1929 MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1930 MODULE_LICENSE("GPL");
1931 module_init(tcm_vhost_init);
1932 module_exit(tcm_vhost_exit);