target: Remove unused target_core_fabric_ops.get_fabric_sense_len method
[platform/adaptation/renesas_rcar/renesas_kernel.git] / 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/vhost.h>
38 #include <linux/fs.h>
39 #include <linux/miscdevice.h>
40 #include <asm/unaligned.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_tcq.h>
43 #include <target/target_core_base.h>
44 #include <target/target_core_fabric.h>
45 #include <target/target_core_fabric_configfs.h>
46 #include <target/target_core_configfs.h>
47 #include <target/configfs_macros.h>
48 #include <linux/vhost.h>
49 #include <linux/virtio_net.h> /* TODO vhost.h currently depends on this */
50 #include <linux/virtio_scsi.h>
51
52 #include "vhost.c"
53 #include "vhost.h"
54 #include "tcm_vhost.h"
55
56 enum {
57         VHOST_SCSI_VQ_CTL = 0,
58         VHOST_SCSI_VQ_EVT = 1,
59         VHOST_SCSI_VQ_IO = 2,
60 };
61
62 struct vhost_scsi {
63         struct tcm_vhost_tpg *vs_tpg;   /* Protected by vhost_scsi->dev.mutex */
64         struct vhost_dev dev;
65         struct vhost_virtqueue vqs[3];
66
67         struct vhost_work vs_completion_work; /* cmd completion work item */
68         struct list_head vs_completion_list;  /* cmd completion queue */
69         spinlock_t vs_completion_lock;        /* protects s_completion_list */
70 };
71
72 /* Local pointer to allocated TCM configfs fabric module */
73 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
74
75 static struct workqueue_struct *tcm_vhost_workqueue;
76
77 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
78 static DEFINE_MUTEX(tcm_vhost_mutex);
79 static LIST_HEAD(tcm_vhost_list);
80
81 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
82 {
83         return 1;
84 }
85
86 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
87 {
88         return 0;
89 }
90
91 static char *tcm_vhost_get_fabric_name(void)
92 {
93         return "vhost";
94 }
95
96 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
97 {
98         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
99                                 struct tcm_vhost_tpg, se_tpg);
100         struct tcm_vhost_tport *tport = tpg->tport;
101
102         switch (tport->tport_proto_id) {
103         case SCSI_PROTOCOL_SAS:
104                 return sas_get_fabric_proto_ident(se_tpg);
105         case SCSI_PROTOCOL_FCP:
106                 return fc_get_fabric_proto_ident(se_tpg);
107         case SCSI_PROTOCOL_ISCSI:
108                 return iscsi_get_fabric_proto_ident(se_tpg);
109         default:
110                 pr_err("Unknown tport_proto_id: 0x%02x, using"
111                         " SAS emulation\n", tport->tport_proto_id);
112                 break;
113         }
114
115         return sas_get_fabric_proto_ident(se_tpg);
116 }
117
118 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
119 {
120         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
121                                 struct tcm_vhost_tpg, se_tpg);
122         struct tcm_vhost_tport *tport = tpg->tport;
123
124         return &tport->tport_name[0];
125 }
126
127 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
128 {
129         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
130                                 struct tcm_vhost_tpg, se_tpg);
131         return tpg->tport_tpgt;
132 }
133
134 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
135 {
136         return 1;
137 }
138
139 static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
140         struct se_node_acl *se_nacl,
141         struct t10_pr_registration *pr_reg,
142         int *format_code,
143         unsigned char *buf)
144 {
145         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
146                                 struct tcm_vhost_tpg, se_tpg);
147         struct tcm_vhost_tport *tport = tpg->tport;
148
149         switch (tport->tport_proto_id) {
150         case SCSI_PROTOCOL_SAS:
151                 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
152                                         format_code, buf);
153         case SCSI_PROTOCOL_FCP:
154                 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
155                                         format_code, buf);
156         case SCSI_PROTOCOL_ISCSI:
157                 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
158                                         format_code, buf);
159         default:
160                 pr_err("Unknown tport_proto_id: 0x%02x, using"
161                         " SAS emulation\n", tport->tport_proto_id);
162                 break;
163         }
164
165         return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
166                         format_code, buf);
167 }
168
169 static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
170         struct se_node_acl *se_nacl,
171         struct t10_pr_registration *pr_reg,
172         int *format_code)
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_len(se_tpg, se_nacl, pr_reg,
181                                         format_code);
182         case SCSI_PROTOCOL_FCP:
183                 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
184                                         format_code);
185         case SCSI_PROTOCOL_ISCSI:
186                 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
187                                         format_code);
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_len(se_tpg, se_nacl, pr_reg,
195                         format_code);
196 }
197
198 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
199         const char *buf,
200         u32 *out_tid_len,
201         char **port_nexus_ptr)
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_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
210                                         port_nexus_ptr);
211         case SCSI_PROTOCOL_FCP:
212                 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
213                                         port_nexus_ptr);
214         case SCSI_PROTOCOL_ISCSI:
215                 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
216                                         port_nexus_ptr);
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_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
224                         port_nexus_ptr);
225 }
226
227 static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
228         struct se_portal_group *se_tpg)
229 {
230         struct tcm_vhost_nacl *nacl;
231
232         nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
233         if (!nacl) {
234                 pr_err("Unable to alocate struct tcm_vhost_nacl\n");
235                 return NULL;
236         }
237
238         return &nacl->se_node_acl;
239 }
240
241 static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
242         struct se_node_acl *se_nacl)
243 {
244         struct tcm_vhost_nacl *nacl = container_of(se_nacl,
245                         struct tcm_vhost_nacl, se_node_acl);
246         kfree(nacl);
247 }
248
249 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
250 {
251         return 1;
252 }
253
254 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
255 {
256         return;
257 }
258
259 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
260 {
261         return 0;
262 }
263
264 static void tcm_vhost_close_session(struct se_session *se_sess)
265 {
266         return;
267 }
268
269 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
270 {
271         return 0;
272 }
273
274 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
275 {
276         /* Go ahead and process the write immediately */
277         target_execute_cmd(se_cmd);
278         return 0;
279 }
280
281 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
282 {
283         return 0;
284 }
285
286 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
287 {
288         return;
289 }
290
291 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
292 {
293         return 0;
294 }
295
296 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
297 {
298         return 0;
299 }
300
301 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
302 {
303         struct vhost_scsi *vs = tv_cmd->tvc_vhost;
304
305         spin_lock_bh(&vs->vs_completion_lock);
306         list_add_tail(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
307         spin_unlock_bh(&vs->vs_completion_lock);
308
309         vhost_work_queue(&vs->dev, &vs->vs_completion_work);
310 }
311
312 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
313 {
314         struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
315                                 struct tcm_vhost_cmd, tvc_se_cmd);
316         vhost_scsi_complete_cmd(tv_cmd);
317         return 0;
318 }
319
320 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
321 {
322         struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
323                                 struct tcm_vhost_cmd, tvc_se_cmd);
324         vhost_scsi_complete_cmd(tv_cmd);
325         return 0;
326 }
327
328 static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
329 {
330         return 0;
331 }
332
333 static u16 tcm_vhost_set_fabric_sense_len(struct se_cmd *se_cmd,
334         u32 sense_length)
335 {
336         return 0;
337 }
338
339 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
340 {
341         struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
342
343         /* TODO locking against target/backend threads? */
344         transport_generic_free_cmd(se_cmd, 1);
345
346         if (tv_cmd->tvc_sgl_count) {
347                 u32 i;
348                 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
349                         put_page(sg_page(&tv_cmd->tvc_sgl[i]));
350
351                 kfree(tv_cmd->tvc_sgl);
352         }
353
354         kfree(tv_cmd);
355 }
356
357 /* Dequeue a command from the completion list */
358 static struct tcm_vhost_cmd *vhost_scsi_get_cmd_from_completion(
359         struct vhost_scsi *vs)
360 {
361         struct tcm_vhost_cmd *tv_cmd = NULL;
362
363         spin_lock_bh(&vs->vs_completion_lock);
364         if (list_empty(&vs->vs_completion_list)) {
365                 spin_unlock_bh(&vs->vs_completion_lock);
366                 return NULL;
367         }
368
369         list_for_each_entry(tv_cmd, &vs->vs_completion_list,
370                             tvc_completion_list) {
371                 list_del(&tv_cmd->tvc_completion_list);
372                 break;
373         }
374         spin_unlock_bh(&vs->vs_completion_lock);
375         return tv_cmd;
376 }
377
378 /* Fill in status and signal that we are done processing this command
379  *
380  * This is scheduled in the vhost work queue so we are called with the owner
381  * process mm and can access the vring.
382  */
383 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
384 {
385         struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
386                                         vs_completion_work);
387         struct tcm_vhost_cmd *tv_cmd;
388
389         while ((tv_cmd = vhost_scsi_get_cmd_from_completion(vs))) {
390                 struct virtio_scsi_cmd_resp v_rsp;
391                 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
392                 int ret;
393
394                 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
395                         tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
396
397                 memset(&v_rsp, 0, sizeof(v_rsp));
398                 v_rsp.resid = se_cmd->residual_count;
399                 /* TODO is status_qualifier field needed? */
400                 v_rsp.status = se_cmd->scsi_status;
401                 v_rsp.sense_len = se_cmd->scsi_sense_length;
402                 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
403                        v_rsp.sense_len);
404                 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
405                 if (likely(ret == 0))
406                         vhost_add_used(&vs->vqs[2], tv_cmd->tvc_vq_desc, 0);
407                 else
408                         pr_err("Faulted on virtio_scsi_cmd_resp\n");
409
410                 vhost_scsi_free_cmd(tv_cmd);
411         }
412
413         vhost_signal(&vs->dev, &vs->vqs[2]);
414 }
415
416 static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
417         struct tcm_vhost_tpg *tv_tpg,
418         struct virtio_scsi_cmd_req *v_req,
419         u32 exp_data_len,
420         int data_direction)
421 {
422         struct tcm_vhost_cmd *tv_cmd;
423         struct tcm_vhost_nexus *tv_nexus;
424         struct se_portal_group *se_tpg = &tv_tpg->se_tpg;
425         struct se_session *se_sess;
426         struct se_cmd *se_cmd;
427         int sam_task_attr;
428
429         tv_nexus = tv_tpg->tpg_nexus;
430         if (!tv_nexus) {
431                 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
432                 return ERR_PTR(-EIO);
433         }
434         se_sess = tv_nexus->tvn_se_sess;
435
436         tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
437         if (!tv_cmd) {
438                 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
439                 return ERR_PTR(-ENOMEM);
440         }
441         INIT_LIST_HEAD(&tv_cmd->tvc_completion_list);
442         tv_cmd->tvc_tag = v_req->tag;
443
444         se_cmd = &tv_cmd->tvc_se_cmd;
445         /*
446          * Locate the SAM Task Attr from virtio_scsi_cmd_req
447          */
448         sam_task_attr = v_req->task_attr;
449         /*
450          * Initialize struct se_cmd descriptor from TCM infrastructure
451          */
452         transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, exp_data_len,
453                                 data_direction, sam_task_attr,
454                                 &tv_cmd->tvc_sense_buf[0]);
455
456 #if 0   /* FIXME: vhost_scsi_allocate_cmd() BIDI operation */
457         if (bidi)
458                 se_cmd->se_cmd_flags |= SCF_BIDI;
459 #endif
460         return tv_cmd;
461 }
462
463 /*
464  * Map a user memory range into a scatterlist
465  *
466  * Returns the number of scatterlist entries used or -errno on error.
467  */
468 static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
469         unsigned int sgl_count, void __user *ptr, size_t len, int write)
470 {
471         struct scatterlist *sg = sgl;
472         unsigned int npages = 0;
473         int ret;
474
475         while (len > 0) {
476                 struct page *page;
477                 unsigned int offset = (uintptr_t)ptr & ~PAGE_MASK;
478                 unsigned int nbytes = min_t(unsigned int,
479                                 PAGE_SIZE - offset, len);
480
481                 if (npages == sgl_count) {
482                         ret = -ENOBUFS;
483                         goto err;
484                 }
485
486                 ret = get_user_pages_fast((unsigned long)ptr, 1, write, &page);
487                 BUG_ON(ret == 0); /* we should either get our page or fail */
488                 if (ret < 0)
489                         goto err;
490
491                 sg_set_page(sg, page, nbytes, offset);
492                 ptr += nbytes;
493                 len -= nbytes;
494                 sg++;
495                 npages++;
496         }
497         return npages;
498
499 err:
500         /* Put pages that we hold */
501         for (sg = sgl; sg != &sgl[npages]; sg++)
502                 put_page(sg_page(sg));
503         return ret;
504 }
505
506 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
507         struct iovec *iov, unsigned int niov, int write)
508 {
509         int ret;
510         unsigned int i;
511         u32 sgl_count;
512         struct scatterlist *sg;
513
514         /*
515          * Find out how long sglist needs to be
516          */
517         sgl_count = 0;
518         for (i = 0; i < niov; i++) {
519                 sgl_count += (((uintptr_t)iov[i].iov_base + iov[i].iov_len +
520                                 PAGE_SIZE - 1) >> PAGE_SHIFT) -
521                                 ((uintptr_t)iov[i].iov_base >> PAGE_SHIFT);
522         }
523         /* TODO overflow checking */
524
525         sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
526         if (!sg)
527                 return -ENOMEM;
528         pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
529                sg, sgl_count, !sg);
530         sg_init_table(sg, sgl_count);
531
532         tv_cmd->tvc_sgl = sg;
533         tv_cmd->tvc_sgl_count = sgl_count;
534
535         pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
536         for (i = 0; i < niov; i++) {
537                 ret = vhost_scsi_map_to_sgl(sg, sgl_count, iov[i].iov_base,
538                                         iov[i].iov_len, write);
539                 if (ret < 0) {
540                         for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
541                                 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
542                         kfree(tv_cmd->tvc_sgl);
543                         tv_cmd->tvc_sgl = NULL;
544                         tv_cmd->tvc_sgl_count = 0;
545                         return ret;
546                 }
547
548                 sg += ret;
549                 sgl_count -= ret;
550         }
551         return 0;
552 }
553
554 static void tcm_vhost_submission_work(struct work_struct *work)
555 {
556         struct tcm_vhost_cmd *tv_cmd =
557                 container_of(work, struct tcm_vhost_cmd, work);
558         struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
559         struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
560         int rc, sg_no_bidi = 0;
561         /*
562          * Locate the struct se_lun pointer based on v_req->lun, and
563          * attach it to struct se_cmd
564          */
565         rc = transport_lookup_cmd_lun(&tv_cmd->tvc_se_cmd, tv_cmd->tvc_lun);
566         if (rc < 0) {
567                 pr_err("Failed to look up lun: %d\n", tv_cmd->tvc_lun);
568                 transport_send_check_condition_and_sense(&tv_cmd->tvc_se_cmd,
569                         tv_cmd->tvc_se_cmd.scsi_sense_reason, 0);
570                 transport_generic_free_cmd(se_cmd, 0);
571                 return;
572         }
573
574         rc = target_setup_cmd_from_cdb(se_cmd, tv_cmd->tvc_cdb);
575         if (rc == -ENOMEM) {
576                 transport_send_check_condition_and_sense(se_cmd,
577                                 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
578                 transport_generic_free_cmd(se_cmd, 0);
579                 return;
580         } else if (rc < 0) {
581                 if (se_cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
582                         tcm_vhost_queue_status(se_cmd);
583                 else
584                         transport_send_check_condition_and_sense(se_cmd,
585                                         se_cmd->scsi_sense_reason, 0);
586                 transport_generic_free_cmd(se_cmd, 0);
587                 return;
588         }
589
590         if (tv_cmd->tvc_sgl_count) {
591                 sg_ptr = tv_cmd->tvc_sgl;
592                 /*
593                  * For BIDI commands, pass in the extra READ buffer
594                  * to transport_generic_map_mem_to_cmd() below..
595                  */
596 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
597 #if 0
598                 if (se_cmd->se_cmd_flags & SCF_BIDI) {
599                         sg_bidi_ptr = NULL;
600                         sg_no_bidi = 0;
601                 }
602 #endif
603         } else {
604                 sg_ptr = NULL;
605         }
606
607         rc = transport_generic_map_mem_to_cmd(se_cmd, sg_ptr,
608                                 tv_cmd->tvc_sgl_count, sg_bidi_ptr,
609                                 sg_no_bidi);
610         if (rc < 0) {
611                 transport_send_check_condition_and_sense(se_cmd,
612                                 se_cmd->scsi_sense_reason, 0);
613                 transport_generic_free_cmd(se_cmd, 0);
614                 return;
615         }
616         transport_handle_cdb_direct(se_cmd);
617 }
618
619 static void vhost_scsi_handle_vq(struct vhost_scsi *vs)
620 {
621         struct vhost_virtqueue *vq = &vs->vqs[2];
622         struct virtio_scsi_cmd_req v_req;
623         struct tcm_vhost_tpg *tv_tpg;
624         struct tcm_vhost_cmd *tv_cmd;
625         u32 exp_data_len, data_first, data_num, data_direction;
626         unsigned out, in, i;
627         int head, ret;
628
629         /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
630         tv_tpg = vs->vs_tpg;
631         if (unlikely(!tv_tpg)) {
632                 pr_err("%s endpoint not set\n", __func__);
633                 return;
634         }
635
636         mutex_lock(&vq->mutex);
637         vhost_disable_notify(&vs->dev, vq);
638
639         for (;;) {
640                 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
641                                         ARRAY_SIZE(vq->iov), &out, &in,
642                                         NULL, NULL);
643                 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
644                                         head, out, in);
645                 /* On error, stop handling until the next kick. */
646                 if (unlikely(head < 0))
647                         break;
648                 /* Nothing new?  Wait for eventfd to tell us they refilled. */
649                 if (head == vq->num) {
650                         if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
651                                 vhost_disable_notify(&vs->dev, vq);
652                                 continue;
653                         }
654                         break;
655                 }
656
657 /* FIXME: BIDI operation */
658                 if (out == 1 && in == 1) {
659                         data_direction = DMA_NONE;
660                         data_first = 0;
661                         data_num = 0;
662                 } else if (out == 1 && in > 1) {
663                         data_direction = DMA_FROM_DEVICE;
664                         data_first = out + 1;
665                         data_num = in - 1;
666                 } else if (out > 1 && in == 1) {
667                         data_direction = DMA_TO_DEVICE;
668                         data_first = 1;
669                         data_num = out - 1;
670                 } else {
671                         vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
672                                         out, in);
673                         break;
674                 }
675
676                 /*
677                  * Check for a sane resp buffer so we can report errors to
678                  * the guest.
679                  */
680                 if (unlikely(vq->iov[out].iov_len !=
681                                         sizeof(struct virtio_scsi_cmd_resp))) {
682                         vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
683                                 " bytes\n", vq->iov[out].iov_len);
684                         break;
685                 }
686
687                 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
688                         vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
689                                 " bytes\n", vq->iov[0].iov_len);
690                         break;
691                 }
692                 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
693                         " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
694                 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
695                                 sizeof(v_req));
696                 if (unlikely(ret)) {
697                         vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
698                         break;
699                 }
700
701                 exp_data_len = 0;
702                 for (i = 0; i < data_num; i++)
703                         exp_data_len += vq->iov[data_first + i].iov_len;
704
705                 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
706                                         exp_data_len, data_direction);
707                 if (IS_ERR(tv_cmd)) {
708                         vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
709                                         PTR_ERR(tv_cmd));
710                         break;
711                 }
712                 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
713                         ": %d\n", tv_cmd, exp_data_len, data_direction);
714
715                 tv_cmd->tvc_vhost = vs;
716
717                 if (unlikely(vq->iov[out].iov_len !=
718                                 sizeof(struct virtio_scsi_cmd_resp))) {
719                         vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
720                                 " bytes, out: %d, in: %d\n",
721                                 vq->iov[out].iov_len, out, in);
722                         break;
723                 }
724
725                 tv_cmd->tvc_resp = vq->iov[out].iov_base;
726
727                 /*
728                  * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
729                  * that will be used by tcm_vhost_new_cmd_map() and down into
730                  * target_setup_cmd_from_cdb()
731                  */
732                 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
733                 /*
734                  * Check that the recieved CDB size does not exceeded our
735                  * hardcoded max for tcm_vhost
736                  */
737                 /* TODO what if cdb was too small for varlen cdb header? */
738                 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
739                                         TCM_VHOST_MAX_CDB_SIZE)) {
740                         vq_err(vq, "Received SCSI CDB with command_size: %d that"
741                                 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
742                                 scsi_command_size(tv_cmd->tvc_cdb),
743                                 TCM_VHOST_MAX_CDB_SIZE);
744                         break; /* TODO */
745                 }
746                 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
747
748                 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
749                         tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
750
751                 if (data_direction != DMA_NONE) {
752                         ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
753                                         &vq->iov[data_first], data_num,
754                                         data_direction == DMA_TO_DEVICE);
755                         if (unlikely(ret)) {
756                                 vq_err(vq, "Failed to map iov to sgl\n");
757                                 break; /* TODO */
758                         }
759                 }
760
761                 /*
762                  * Save the descriptor from vhost_get_vq_desc() to be used to
763                  * complete the virtio-scsi request in TCM callback context via
764                  * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
765                  */
766                 tv_cmd->tvc_vq_desc = head;
767                 /*
768                  * Dispatch tv_cmd descriptor for cmwq execution in process
769                  * context provided by tcm_vhost_workqueue.  This also ensures
770                  * tv_cmd is executed on the same kworker CPU as this vhost
771                  * thread to gain positive L2 cache locality effects..
772                  */
773                 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
774                 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
775         }
776
777         mutex_unlock(&vq->mutex);
778 }
779
780 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
781 {
782         pr_debug("%s: The handling func for control queue.\n", __func__);
783 }
784
785 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
786 {
787         pr_debug("%s: The handling func for event queue.\n", __func__);
788 }
789
790 static void vhost_scsi_handle_kick(struct vhost_work *work)
791 {
792         struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
793                                                 poll.work);
794         struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
795
796         vhost_scsi_handle_vq(vs);
797 }
798
799 /*
800  * Called from vhost_scsi_ioctl() context to walk the list of available
801  * tcm_vhost_tpg with an active struct tcm_vhost_nexus
802  */
803 static int vhost_scsi_set_endpoint(
804         struct vhost_scsi *vs,
805         struct vhost_scsi_target *t)
806 {
807         struct tcm_vhost_tport *tv_tport;
808         struct tcm_vhost_tpg *tv_tpg;
809         int index;
810
811         mutex_lock(&vs->dev.mutex);
812         /* Verify that ring has been setup correctly. */
813         for (index = 0; index < vs->dev.nvqs; ++index) {
814                 /* Verify that ring has been setup correctly. */
815                 if (!vhost_vq_access_ok(&vs->vqs[index])) {
816                         mutex_unlock(&vs->dev.mutex);
817                         return -EFAULT;
818                 }
819         }
820         mutex_unlock(&vs->dev.mutex);
821
822         mutex_lock(&tcm_vhost_mutex);
823         list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
824                 mutex_lock(&tv_tpg->tv_tpg_mutex);
825                 if (!tv_tpg->tpg_nexus) {
826                         mutex_unlock(&tv_tpg->tv_tpg_mutex);
827                         continue;
828                 }
829                 if (tv_tpg->tv_tpg_vhost_count != 0) {
830                         mutex_unlock(&tv_tpg->tv_tpg_mutex);
831                         continue;
832                 }
833                 tv_tport = tv_tpg->tport;
834
835                 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn) &&
836                     (tv_tpg->tport_tpgt == t->vhost_tpgt)) {
837                         tv_tpg->tv_tpg_vhost_count++;
838                         mutex_unlock(&tv_tpg->tv_tpg_mutex);
839                         mutex_unlock(&tcm_vhost_mutex);
840
841                         mutex_lock(&vs->dev.mutex);
842                         if (vs->vs_tpg) {
843                                 mutex_unlock(&vs->dev.mutex);
844                                 mutex_lock(&tv_tpg->tv_tpg_mutex);
845                                 tv_tpg->tv_tpg_vhost_count--;
846                                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
847                                 return -EEXIST;
848                         }
849
850                         vs->vs_tpg = tv_tpg;
851                         smp_mb__after_atomic_inc();
852                         mutex_unlock(&vs->dev.mutex);
853                         return 0;
854                 }
855                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
856         }
857         mutex_unlock(&tcm_vhost_mutex);
858         return -EINVAL;
859 }
860
861 static int vhost_scsi_clear_endpoint(
862         struct vhost_scsi *vs,
863         struct vhost_scsi_target *t)
864 {
865         struct tcm_vhost_tport *tv_tport;
866         struct tcm_vhost_tpg *tv_tpg;
867         int index, ret;
868
869         mutex_lock(&vs->dev.mutex);
870         /* Verify that ring has been setup correctly. */
871         for (index = 0; index < vs->dev.nvqs; ++index) {
872                 if (!vhost_vq_access_ok(&vs->vqs[index])) {
873                         ret = -EFAULT;
874                         goto err;
875                 }
876         }
877
878         if (!vs->vs_tpg) {
879                 ret = -ENODEV;
880                 goto err;
881         }
882         tv_tpg = vs->vs_tpg;
883         tv_tport = tv_tpg->tport;
884
885         if (strcmp(tv_tport->tport_name, t->vhost_wwpn) ||
886             (tv_tpg->tport_tpgt != t->vhost_tpgt)) {
887                 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
888                         " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
889                         tv_tport->tport_name, tv_tpg->tport_tpgt,
890                         t->vhost_wwpn, t->vhost_tpgt);
891                 ret = -EINVAL;
892                 goto err;
893         }
894         tv_tpg->tv_tpg_vhost_count--;
895         vs->vs_tpg = NULL;
896         mutex_unlock(&vs->dev.mutex);
897
898         return 0;
899
900 err:
901         mutex_unlock(&vs->dev.mutex);
902         return ret;
903 }
904
905 static int vhost_scsi_open(struct inode *inode, struct file *f)
906 {
907         struct vhost_scsi *s;
908         int r;
909
910         s = kzalloc(sizeof(*s), GFP_KERNEL);
911         if (!s)
912                 return -ENOMEM;
913
914         vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
915         INIT_LIST_HEAD(&s->vs_completion_list);
916         spin_lock_init(&s->vs_completion_lock);
917
918         s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
919         s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
920         s->vqs[VHOST_SCSI_VQ_IO].handle_kick = vhost_scsi_handle_kick;
921         r = vhost_dev_init(&s->dev, s->vqs, 3);
922         if (r < 0) {
923                 kfree(s);
924                 return r;
925         }
926
927         f->private_data = s;
928         return 0;
929 }
930
931 static int vhost_scsi_release(struct inode *inode, struct file *f)
932 {
933         struct vhost_scsi *s = f->private_data;
934
935         if (s->vs_tpg && s->vs_tpg->tport) {
936                 struct vhost_scsi_target backend;
937
938                 memcpy(backend.vhost_wwpn, s->vs_tpg->tport->tport_name,
939                                 sizeof(backend.vhost_wwpn));
940                 backend.vhost_tpgt = s->vs_tpg->tport_tpgt;
941                 vhost_scsi_clear_endpoint(s, &backend);
942         }
943
944         vhost_dev_cleanup(&s->dev, false);
945         kfree(s);
946         return 0;
947 }
948
949 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
950 {
951         vhost_poll_flush(&vs->dev.vqs[index].poll);
952 }
953
954 static void vhost_scsi_flush(struct vhost_scsi *vs)
955 {
956         vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_CTL);
957         vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_EVT);
958         vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_IO);
959 }
960
961 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
962 {
963         if (features & ~VHOST_FEATURES)
964                 return -EOPNOTSUPP;
965
966         mutex_lock(&vs->dev.mutex);
967         if ((features & (1 << VHOST_F_LOG_ALL)) &&
968             !vhost_log_access_ok(&vs->dev)) {
969                 mutex_unlock(&vs->dev.mutex);
970                 return -EFAULT;
971         }
972         vs->dev.acked_features = features;
973         smp_wmb();
974         vhost_scsi_flush(vs);
975         mutex_unlock(&vs->dev.mutex);
976         return 0;
977 }
978
979 static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
980                                 unsigned long arg)
981 {
982         struct vhost_scsi *vs = f->private_data;
983         struct vhost_scsi_target backend;
984         void __user *argp = (void __user *)arg;
985         u64 __user *featurep = argp;
986         u64 features;
987         int r, abi_version = VHOST_SCSI_ABI_VERSION;
988
989         switch (ioctl) {
990         case VHOST_SCSI_SET_ENDPOINT:
991                 if (copy_from_user(&backend, argp, sizeof backend))
992                         return -EFAULT;
993                 if (backend.reserved != 0)
994                         return -EOPNOTSUPP;
995
996                 return vhost_scsi_set_endpoint(vs, &backend);
997         case VHOST_SCSI_CLEAR_ENDPOINT:
998                 if (copy_from_user(&backend, argp, sizeof backend))
999                         return -EFAULT;
1000                 if (backend.reserved != 0)
1001                         return -EOPNOTSUPP;
1002
1003                 return vhost_scsi_clear_endpoint(vs, &backend);
1004         case VHOST_SCSI_GET_ABI_VERSION:
1005                 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1006                         return -EFAULT;
1007                 return 0;
1008         case VHOST_GET_FEATURES:
1009                 features = VHOST_FEATURES;
1010                 if (copy_to_user(featurep, &features, sizeof features))
1011                         return -EFAULT;
1012                 return 0;
1013         case VHOST_SET_FEATURES:
1014                 if (copy_from_user(&features, featurep, sizeof features))
1015                         return -EFAULT;
1016                 return vhost_scsi_set_features(vs, features);
1017         default:
1018                 mutex_lock(&vs->dev.mutex);
1019                 r = vhost_dev_ioctl(&vs->dev, ioctl, arg);
1020                 mutex_unlock(&vs->dev.mutex);
1021                 return r;
1022         }
1023 }
1024
1025 #ifdef CONFIG_COMPAT
1026 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1027                                 unsigned long arg)
1028 {
1029         return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1030 }
1031 #endif
1032
1033 static const struct file_operations vhost_scsi_fops = {
1034         .owner          = THIS_MODULE,
1035         .release        = vhost_scsi_release,
1036         .unlocked_ioctl = vhost_scsi_ioctl,
1037 #ifdef CONFIG_COMPAT
1038         .compat_ioctl   = vhost_scsi_compat_ioctl,
1039 #endif
1040         .open           = vhost_scsi_open,
1041         .llseek         = noop_llseek,
1042 };
1043
1044 static struct miscdevice vhost_scsi_misc = {
1045         MISC_DYNAMIC_MINOR,
1046         "vhost-scsi",
1047         &vhost_scsi_fops,
1048 };
1049
1050 static int __init vhost_scsi_register(void)
1051 {
1052         return misc_register(&vhost_scsi_misc);
1053 }
1054
1055 static int vhost_scsi_deregister(void)
1056 {
1057         return misc_deregister(&vhost_scsi_misc);
1058 }
1059
1060 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1061 {
1062         switch (tport->tport_proto_id) {
1063         case SCSI_PROTOCOL_SAS:
1064                 return "SAS";
1065         case SCSI_PROTOCOL_FCP:
1066                 return "FCP";
1067         case SCSI_PROTOCOL_ISCSI:
1068                 return "iSCSI";
1069         default:
1070                 break;
1071         }
1072
1073         return "Unknown";
1074 }
1075
1076 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1077         struct se_lun *lun)
1078 {
1079         struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1080                                 struct tcm_vhost_tpg, se_tpg);
1081
1082         mutex_lock(&tv_tpg->tv_tpg_mutex);
1083         tv_tpg->tv_tpg_port_count++;
1084         mutex_unlock(&tv_tpg->tv_tpg_mutex);
1085
1086         return 0;
1087 }
1088
1089 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1090         struct se_lun *se_lun)
1091 {
1092         struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1093                                 struct tcm_vhost_tpg, se_tpg);
1094
1095         mutex_lock(&tv_tpg->tv_tpg_mutex);
1096         tv_tpg->tv_tpg_port_count--;
1097         mutex_unlock(&tv_tpg->tv_tpg_mutex);
1098 }
1099
1100 static struct se_node_acl *tcm_vhost_make_nodeacl(
1101         struct se_portal_group *se_tpg,
1102         struct config_group *group,
1103         const char *name)
1104 {
1105         struct se_node_acl *se_nacl, *se_nacl_new;
1106         struct tcm_vhost_nacl *nacl;
1107         u64 wwpn = 0;
1108         u32 nexus_depth;
1109
1110         /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1111                 return ERR_PTR(-EINVAL); */
1112         se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1113         if (!se_nacl_new)
1114                 return ERR_PTR(-ENOMEM);
1115
1116         nexus_depth = 1;
1117         /*
1118          * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1119          * when converting a NodeACL from demo mode -> explict
1120          */
1121         se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1122                                 name, nexus_depth);
1123         if (IS_ERR(se_nacl)) {
1124                 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1125                 return se_nacl;
1126         }
1127         /*
1128          * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1129          */
1130         nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1131         nacl->iport_wwpn = wwpn;
1132
1133         return se_nacl;
1134 }
1135
1136 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1137 {
1138         struct tcm_vhost_nacl *nacl = container_of(se_acl,
1139                                 struct tcm_vhost_nacl, se_node_acl);
1140         core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1141         kfree(nacl);
1142 }
1143
1144 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1145         const char *name)
1146 {
1147         struct se_portal_group *se_tpg;
1148         struct tcm_vhost_nexus *tv_nexus;
1149
1150         mutex_lock(&tv_tpg->tv_tpg_mutex);
1151         if (tv_tpg->tpg_nexus) {
1152                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1153                 pr_debug("tv_tpg->tpg_nexus already exists\n");
1154                 return -EEXIST;
1155         }
1156         se_tpg = &tv_tpg->se_tpg;
1157
1158         tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1159         if (!tv_nexus) {
1160                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1161                 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1162                 return -ENOMEM;
1163         }
1164         /*
1165          *  Initialize the struct se_session pointer
1166          */
1167         tv_nexus->tvn_se_sess = transport_init_session();
1168         if (IS_ERR(tv_nexus->tvn_se_sess)) {
1169                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1170                 kfree(tv_nexus);
1171                 return -ENOMEM;
1172         }
1173         /*
1174          * Since we are running in 'demo mode' this call with generate a
1175          * struct se_node_acl for the tcm_vhost struct se_portal_group with
1176          * the SCSI Initiator port name of the passed configfs group 'name'.
1177          */
1178         tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1179                                 se_tpg, (unsigned char *)name);
1180         if (!tv_nexus->tvn_se_sess->se_node_acl) {
1181                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1182                 pr_debug("core_tpg_check_initiator_node_acl() failed"
1183                                 " for %s\n", name);
1184                 transport_free_session(tv_nexus->tvn_se_sess);
1185                 kfree(tv_nexus);
1186                 return -ENOMEM;
1187         }
1188         /*
1189          * Now register the TCM vhost virtual I_T Nexus as active with the
1190          * call to __transport_register_session()
1191          */
1192         __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1193                         tv_nexus->tvn_se_sess, tv_nexus);
1194         tv_tpg->tpg_nexus = tv_nexus;
1195
1196         mutex_unlock(&tv_tpg->tv_tpg_mutex);
1197         return 0;
1198 }
1199
1200 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1201 {
1202         struct se_session *se_sess;
1203         struct tcm_vhost_nexus *tv_nexus;
1204
1205         mutex_lock(&tpg->tv_tpg_mutex);
1206         tv_nexus = tpg->tpg_nexus;
1207         if (!tv_nexus) {
1208                 mutex_unlock(&tpg->tv_tpg_mutex);
1209                 return -ENODEV;
1210         }
1211
1212         se_sess = tv_nexus->tvn_se_sess;
1213         if (!se_sess) {
1214                 mutex_unlock(&tpg->tv_tpg_mutex);
1215                 return -ENODEV;
1216         }
1217
1218         if (tpg->tv_tpg_port_count != 0) {
1219                 mutex_unlock(&tpg->tv_tpg_mutex);
1220                 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1221                         " active TPG port count: %d\n",
1222                         tpg->tv_tpg_port_count);
1223                 return -EBUSY;
1224         }
1225
1226         if (tpg->tv_tpg_vhost_count != 0) {
1227                 mutex_unlock(&tpg->tv_tpg_mutex);
1228                 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1229                         " active TPG vhost count: %d\n",
1230                         tpg->tv_tpg_vhost_count);
1231                 return -EBUSY;
1232         }
1233
1234         pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1235                 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1236                 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1237         /*
1238          * Release the SCSI I_T Nexus to the emulated vhost Target Port
1239          */
1240         transport_deregister_session(tv_nexus->tvn_se_sess);
1241         tpg->tpg_nexus = NULL;
1242         mutex_unlock(&tpg->tv_tpg_mutex);
1243
1244         kfree(tv_nexus);
1245         return 0;
1246 }
1247
1248 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1249         char *page)
1250 {
1251         struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1252                                 struct tcm_vhost_tpg, se_tpg);
1253         struct tcm_vhost_nexus *tv_nexus;
1254         ssize_t ret;
1255
1256         mutex_lock(&tv_tpg->tv_tpg_mutex);
1257         tv_nexus = tv_tpg->tpg_nexus;
1258         if (!tv_nexus) {
1259                 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1260                 return -ENODEV;
1261         }
1262         ret = snprintf(page, PAGE_SIZE, "%s\n",
1263                         tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1264         mutex_unlock(&tv_tpg->tv_tpg_mutex);
1265
1266         return ret;
1267 }
1268
1269 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1270         const char *page,
1271         size_t count)
1272 {
1273         struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1274                                 struct tcm_vhost_tpg, se_tpg);
1275         struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1276         unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1277         int ret;
1278         /*
1279          * Shutdown the active I_T nexus if 'NULL' is passed..
1280          */
1281         if (!strncmp(page, "NULL", 4)) {
1282                 ret = tcm_vhost_drop_nexus(tv_tpg);
1283                 return (!ret) ? count : ret;
1284         }
1285         /*
1286          * Otherwise make sure the passed virtual Initiator port WWN matches
1287          * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1288          * tcm_vhost_make_nexus().
1289          */
1290         if (strlen(page) >= TCM_VHOST_NAMELEN) {
1291                 pr_err("Emulated NAA Sas Address: %s, exceeds"
1292                                 " max: %d\n", page, TCM_VHOST_NAMELEN);
1293                 return -EINVAL;
1294         }
1295         snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1296
1297         ptr = strstr(i_port, "naa.");
1298         if (ptr) {
1299                 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1300                         pr_err("Passed SAS Initiator Port %s does not"
1301                                 " match target port protoid: %s\n", i_port,
1302                                 tcm_vhost_dump_proto_id(tport_wwn));
1303                         return -EINVAL;
1304                 }
1305                 port_ptr = &i_port[0];
1306                 goto check_newline;
1307         }
1308         ptr = strstr(i_port, "fc.");
1309         if (ptr) {
1310                 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1311                         pr_err("Passed FCP Initiator Port %s does not"
1312                                 " match target port protoid: %s\n", i_port,
1313                                 tcm_vhost_dump_proto_id(tport_wwn));
1314                         return -EINVAL;
1315                 }
1316                 port_ptr = &i_port[3]; /* Skip over "fc." */
1317                 goto check_newline;
1318         }
1319         ptr = strstr(i_port, "iqn.");
1320         if (ptr) {
1321                 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1322                         pr_err("Passed iSCSI Initiator Port %s does not"
1323                                 " match target port protoid: %s\n", i_port,
1324                                 tcm_vhost_dump_proto_id(tport_wwn));
1325                         return -EINVAL;
1326                 }
1327                 port_ptr = &i_port[0];
1328                 goto check_newline;
1329         }
1330         pr_err("Unable to locate prefix for emulated Initiator Port:"
1331                         " %s\n", i_port);
1332         return -EINVAL;
1333         /*
1334          * Clear any trailing newline for the NAA WWN
1335          */
1336 check_newline:
1337         if (i_port[strlen(i_port)-1] == '\n')
1338                 i_port[strlen(i_port)-1] = '\0';
1339
1340         ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1341         if (ret < 0)
1342                 return ret;
1343
1344         return count;
1345 }
1346
1347 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1348
1349 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1350         &tcm_vhost_tpg_nexus.attr,
1351         NULL,
1352 };
1353
1354 static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
1355         struct config_group *group,
1356         const char *name)
1357 {
1358         struct tcm_vhost_tport *tport = container_of(wwn,
1359                         struct tcm_vhost_tport, tport_wwn);
1360
1361         struct tcm_vhost_tpg *tpg;
1362         unsigned long tpgt;
1363         int ret;
1364
1365         if (strstr(name, "tpgt_") != name)
1366                 return ERR_PTR(-EINVAL);
1367         if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1368                 return ERR_PTR(-EINVAL);
1369
1370         tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1371         if (!tpg) {
1372                 pr_err("Unable to allocate struct tcm_vhost_tpg");
1373                 return ERR_PTR(-ENOMEM);
1374         }
1375         mutex_init(&tpg->tv_tpg_mutex);
1376         INIT_LIST_HEAD(&tpg->tv_tpg_list);
1377         tpg->tport = tport;
1378         tpg->tport_tpgt = tpgt;
1379
1380         ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1381                                 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1382         if (ret < 0) {
1383                 kfree(tpg);
1384                 return NULL;
1385         }
1386         mutex_lock(&tcm_vhost_mutex);
1387         list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1388         mutex_unlock(&tcm_vhost_mutex);
1389
1390         return &tpg->se_tpg;
1391 }
1392
1393 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1394 {
1395         struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1396                                 struct tcm_vhost_tpg, se_tpg);
1397
1398         mutex_lock(&tcm_vhost_mutex);
1399         list_del(&tpg->tv_tpg_list);
1400         mutex_unlock(&tcm_vhost_mutex);
1401         /*
1402          * Release the virtual I_T Nexus for this vhost TPG
1403          */
1404         tcm_vhost_drop_nexus(tpg);
1405         /*
1406          * Deregister the se_tpg from TCM..
1407          */
1408         core_tpg_deregister(se_tpg);
1409         kfree(tpg);
1410 }
1411
1412 static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1413         struct config_group *group,
1414         const char *name)
1415 {
1416         struct tcm_vhost_tport *tport;
1417         char *ptr;
1418         u64 wwpn = 0;
1419         int off = 0;
1420
1421         /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1422                 return ERR_PTR(-EINVAL); */
1423
1424         tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1425         if (!tport) {
1426                 pr_err("Unable to allocate struct tcm_vhost_tport");
1427                 return ERR_PTR(-ENOMEM);
1428         }
1429         tport->tport_wwpn = wwpn;
1430         /*
1431          * Determine the emulated Protocol Identifier and Target Port Name
1432          * based on the incoming configfs directory name.
1433          */
1434         ptr = strstr(name, "naa.");
1435         if (ptr) {
1436                 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1437                 goto check_len;
1438         }
1439         ptr = strstr(name, "fc.");
1440         if (ptr) {
1441                 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1442                 off = 3; /* Skip over "fc." */
1443                 goto check_len;
1444         }
1445         ptr = strstr(name, "iqn.");
1446         if (ptr) {
1447                 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1448                 goto check_len;
1449         }
1450
1451         pr_err("Unable to locate prefix for emulated Target Port:"
1452                         " %s\n", name);
1453         kfree(tport);
1454         return ERR_PTR(-EINVAL);
1455
1456 check_len:
1457         if (strlen(name) >= TCM_VHOST_NAMELEN) {
1458                 pr_err("Emulated %s Address: %s, exceeds"
1459                         " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1460                         TCM_VHOST_NAMELEN);
1461                 kfree(tport);
1462                 return ERR_PTR(-EINVAL);
1463         }
1464         snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1465
1466         pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1467                 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1468
1469         return &tport->tport_wwn;
1470 }
1471
1472 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1473 {
1474         struct tcm_vhost_tport *tport = container_of(wwn,
1475                                 struct tcm_vhost_tport, tport_wwn);
1476
1477         pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1478                 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1479                 tport->tport_name);
1480
1481         kfree(tport);
1482 }
1483
1484 static ssize_t tcm_vhost_wwn_show_attr_version(
1485         struct target_fabric_configfs *tf,
1486         char *page)
1487 {
1488         return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1489                 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1490                 utsname()->machine);
1491 }
1492
1493 TF_WWN_ATTR_RO(tcm_vhost, version);
1494
1495 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1496         &tcm_vhost_wwn_version.attr,
1497         NULL,
1498 };
1499
1500 static struct target_core_fabric_ops tcm_vhost_ops = {
1501         .get_fabric_name                = tcm_vhost_get_fabric_name,
1502         .get_fabric_proto_ident         = tcm_vhost_get_fabric_proto_ident,
1503         .tpg_get_wwn                    = tcm_vhost_get_fabric_wwn,
1504         .tpg_get_tag                    = tcm_vhost_get_tag,
1505         .tpg_get_default_depth          = tcm_vhost_get_default_depth,
1506         .tpg_get_pr_transport_id        = tcm_vhost_get_pr_transport_id,
1507         .tpg_get_pr_transport_id_len    = tcm_vhost_get_pr_transport_id_len,
1508         .tpg_parse_pr_out_transport_id  = tcm_vhost_parse_pr_out_transport_id,
1509         .tpg_check_demo_mode            = tcm_vhost_check_true,
1510         .tpg_check_demo_mode_cache      = tcm_vhost_check_true,
1511         .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1512         .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1513         .tpg_alloc_fabric_acl           = tcm_vhost_alloc_fabric_acl,
1514         .tpg_release_fabric_acl         = tcm_vhost_release_fabric_acl,
1515         .tpg_get_inst_index             = tcm_vhost_tpg_get_inst_index,
1516         .release_cmd                    = tcm_vhost_release_cmd,
1517         .shutdown_session               = tcm_vhost_shutdown_session,
1518         .close_session                  = tcm_vhost_close_session,
1519         .sess_get_index                 = tcm_vhost_sess_get_index,
1520         .sess_get_initiator_sid         = NULL,
1521         .write_pending                  = tcm_vhost_write_pending,
1522         .write_pending_status           = tcm_vhost_write_pending_status,
1523         .set_default_node_attributes    = tcm_vhost_set_default_node_attrs,
1524         .get_task_tag                   = tcm_vhost_get_task_tag,
1525         .get_cmd_state                  = tcm_vhost_get_cmd_state,
1526         .queue_data_in                  = tcm_vhost_queue_data_in,
1527         .queue_status                   = tcm_vhost_queue_status,
1528         .queue_tm_rsp                   = tcm_vhost_queue_tm_rsp,
1529         .set_fabric_sense_len           = tcm_vhost_set_fabric_sense_len,
1530         /*
1531          * Setup callers for generic logic in target_core_fabric_configfs.c
1532          */
1533         .fabric_make_wwn                = tcm_vhost_make_tport,
1534         .fabric_drop_wwn                = tcm_vhost_drop_tport,
1535         .fabric_make_tpg                = tcm_vhost_make_tpg,
1536         .fabric_drop_tpg                = tcm_vhost_drop_tpg,
1537         .fabric_post_link               = tcm_vhost_port_link,
1538         .fabric_pre_unlink              = tcm_vhost_port_unlink,
1539         .fabric_make_np                 = NULL,
1540         .fabric_drop_np                 = NULL,
1541         .fabric_make_nodeacl            = tcm_vhost_make_nodeacl,
1542         .fabric_drop_nodeacl            = tcm_vhost_drop_nodeacl,
1543 };
1544
1545 static int tcm_vhost_register_configfs(void)
1546 {
1547         struct target_fabric_configfs *fabric;
1548         int ret;
1549
1550         pr_debug("TCM_VHOST fabric module %s on %s/%s"
1551                 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1552                 utsname()->machine);
1553         /*
1554          * Register the top level struct config_item_type with TCM core
1555          */
1556         fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1557         if (IS_ERR(fabric)) {
1558                 pr_err("target_fabric_configfs_init() failed\n");
1559                 return PTR_ERR(fabric);
1560         }
1561         /*
1562          * Setup fabric->tf_ops from our local tcm_vhost_ops
1563          */
1564         fabric->tf_ops = tcm_vhost_ops;
1565         /*
1566          * Setup default attribute lists for various fabric->tf_cit_tmpl
1567          */
1568         TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1569         TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1570         TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1571         TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1572         TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1573         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1574         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1575         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1576         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1577         /*
1578          * Register the fabric for use within TCM
1579          */
1580         ret = target_fabric_configfs_register(fabric);
1581         if (ret < 0) {
1582                 pr_err("target_fabric_configfs_register() failed"
1583                                 " for TCM_VHOST\n");
1584                 return ret;
1585         }
1586         /*
1587          * Setup our local pointer to *fabric
1588          */
1589         tcm_vhost_fabric_configfs = fabric;
1590         pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1591         return 0;
1592 };
1593
1594 static void tcm_vhost_deregister_configfs(void)
1595 {
1596         if (!tcm_vhost_fabric_configfs)
1597                 return;
1598
1599         target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1600         tcm_vhost_fabric_configfs = NULL;
1601         pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1602 };
1603
1604 static int __init tcm_vhost_init(void)
1605 {
1606         int ret = -ENOMEM;
1607         /*
1608          * Use our own dedicated workqueue for submitting I/O into
1609          * target core to avoid contention within system_wq.
1610          */
1611         tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1612         if (!tcm_vhost_workqueue)
1613                 goto out;
1614
1615         ret = vhost_scsi_register();
1616         if (ret < 0)
1617                 goto out_destroy_workqueue;
1618
1619         ret = tcm_vhost_register_configfs();
1620         if (ret < 0)
1621                 goto out_vhost_scsi_deregister;
1622
1623         return 0;
1624
1625 out_vhost_scsi_deregister:
1626         vhost_scsi_deregister();
1627 out_destroy_workqueue:
1628         destroy_workqueue(tcm_vhost_workqueue);
1629 out:
1630         return ret;
1631 };
1632
1633 static void tcm_vhost_exit(void)
1634 {
1635         tcm_vhost_deregister_configfs();
1636         vhost_scsi_deregister();
1637         destroy_workqueue(tcm_vhost_workqueue);
1638 };
1639
1640 MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1641 MODULE_LICENSE("GPL");
1642 module_init(tcm_vhost_init);
1643 module_exit(tcm_vhost_exit);