]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/vhost/scsi.c
fb8a93d5bfba6d10a30872c6fd66b4514a2d9686
[karo-tx-linux.git] / drivers / vhost / scsi.c
1 /*******************************************************************************
2  * Vhost kernel TCM fabric driver for virtio SCSI initiators
3  *
4  * (C) Copyright 2010-2013 Datera, Inc.
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@daterainc.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 <target/target_core_base.h>
42 #include <target/target_core_fabric.h>
43 #include <target/target_core_fabric_configfs.h>
44 #include <target/target_core_configfs.h>
45 #include <target/configfs_macros.h>
46 #include <linux/vhost.h>
47 #include <linux/virtio_scsi.h>
48 #include <linux/llist.h>
49 #include <linux/bitmap.h>
50 #include <linux/percpu_ida.h>
51
52 #include "vhost.h"
53
54 #define VHOST_SCSI_VERSION  "v0.1"
55 #define VHOST_SCSI_NAMELEN 256
56 #define VHOST_SCSI_MAX_CDB_SIZE 32
57 #define VHOST_SCSI_DEFAULT_TAGS 256
58 #define VHOST_SCSI_PREALLOC_SGLS 2048
59 #define VHOST_SCSI_PREALLOC_UPAGES 2048
60 #define VHOST_SCSI_PREALLOC_PROT_SGLS 512
61
62 struct vhost_scsi_inflight {
63         /* Wait for the flush operation to finish */
64         struct completion comp;
65         /* Refcount for the inflight reqs */
66         struct kref kref;
67 };
68
69 struct vhost_scsi_cmd {
70         /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
71         int tvc_vq_desc;
72         /* virtio-scsi initiator task attribute */
73         int tvc_task_attr;
74         /* virtio-scsi response incoming iovecs */
75         int tvc_in_iovs;
76         /* virtio-scsi initiator data direction */
77         enum dma_data_direction tvc_data_direction;
78         /* Expected data transfer length from virtio-scsi header */
79         u32 tvc_exp_data_len;
80         /* The Tag from include/linux/virtio_scsi.h:struct virtio_scsi_cmd_req */
81         u64 tvc_tag;
82         /* The number of scatterlists associated with this cmd */
83         u32 tvc_sgl_count;
84         u32 tvc_prot_sgl_count;
85         /* Saved unpacked SCSI LUN for vhost_scsi_submission_work() */
86         u32 tvc_lun;
87         /* Pointer to the SGL formatted memory from virtio-scsi */
88         struct scatterlist *tvc_sgl;
89         struct scatterlist *tvc_prot_sgl;
90         struct page **tvc_upages;
91         /* Pointer to response header iovec */
92         struct iovec *tvc_resp_iov;
93         /* Pointer to vhost_scsi for our device */
94         struct vhost_scsi *tvc_vhost;
95         /* Pointer to vhost_virtqueue for the cmd */
96         struct vhost_virtqueue *tvc_vq;
97         /* Pointer to vhost nexus memory */
98         struct vhost_scsi_nexus *tvc_nexus;
99         /* The TCM I/O descriptor that is accessed via container_of() */
100         struct se_cmd tvc_se_cmd;
101         /* work item used for cmwq dispatch to vhost_scsi_submission_work() */
102         struct work_struct work;
103         /* Copy of the incoming SCSI command descriptor block (CDB) */
104         unsigned char tvc_cdb[VHOST_SCSI_MAX_CDB_SIZE];
105         /* Sense buffer that will be mapped into outgoing status */
106         unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
107         /* Completed commands list, serviced from vhost worker thread */
108         struct llist_node tvc_completion_list;
109         /* Used to track inflight cmd */
110         struct vhost_scsi_inflight *inflight;
111 };
112
113 struct vhost_scsi_nexus {
114         /* Pointer to TCM session for I_T Nexus */
115         struct se_session *tvn_se_sess;
116 };
117
118 struct vhost_scsi_tpg {
119         /* Vhost port target portal group tag for TCM */
120         u16 tport_tpgt;
121         /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
122         int tv_tpg_port_count;
123         /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
124         int tv_tpg_vhost_count;
125         /* Used for enabling T10-PI with legacy devices */
126         int tv_fabric_prot_type;
127         /* list for vhost_scsi_list */
128         struct list_head tv_tpg_list;
129         /* Used to protect access for tpg_nexus */
130         struct mutex tv_tpg_mutex;
131         /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
132         struct vhost_scsi_nexus *tpg_nexus;
133         /* Pointer back to vhost_scsi_tport */
134         struct vhost_scsi_tport *tport;
135         /* Returned by vhost_scsi_make_tpg() */
136         struct se_portal_group se_tpg;
137         /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
138         struct vhost_scsi *vhost_scsi;
139 };
140
141 struct vhost_scsi_tport {
142         /* SCSI protocol the tport is providing */
143         u8 tport_proto_id;
144         /* Binary World Wide unique Port Name for Vhost Target port */
145         u64 tport_wwpn;
146         /* ASCII formatted WWPN for Vhost Target port */
147         char tport_name[VHOST_SCSI_NAMELEN];
148         /* Returned by vhost_scsi_make_tport() */
149         struct se_wwn tport_wwn;
150 };
151
152 struct vhost_scsi_evt {
153         /* event to be sent to guest */
154         struct virtio_scsi_event event;
155         /* event list, serviced from vhost worker thread */
156         struct llist_node list;
157 };
158
159 enum {
160         VHOST_SCSI_VQ_CTL = 0,
161         VHOST_SCSI_VQ_EVT = 1,
162         VHOST_SCSI_VQ_IO = 2,
163 };
164
165 /* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
166 enum {
167         VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
168                                                (1ULL << VIRTIO_SCSI_F_T10_PI) |
169                                                (1ULL << VIRTIO_F_ANY_LAYOUT) |
170                                                (1ULL << VIRTIO_F_VERSION_1)
171 };
172
173 #define VHOST_SCSI_MAX_TARGET   256
174 #define VHOST_SCSI_MAX_VQ       128
175 #define VHOST_SCSI_MAX_EVENT    128
176
177 struct vhost_scsi_virtqueue {
178         struct vhost_virtqueue vq;
179         /*
180          * Reference counting for inflight reqs, used for flush operation. At
181          * each time, one reference tracks new commands submitted, while we
182          * wait for another one to reach 0.
183          */
184         struct vhost_scsi_inflight inflights[2];
185         /*
186          * Indicate current inflight in use, protected by vq->mutex.
187          * Writers must also take dev mutex and flush under it.
188          */
189         int inflight_idx;
190 };
191
192 struct vhost_scsi {
193         /* Protected by vhost_scsi->dev.mutex */
194         struct vhost_scsi_tpg **vs_tpg;
195         char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
196
197         struct vhost_dev dev;
198         struct vhost_scsi_virtqueue vqs[VHOST_SCSI_MAX_VQ];
199
200         struct vhost_work vs_completion_work; /* cmd completion work item */
201         struct llist_head vs_completion_list; /* cmd completion queue */
202
203         struct vhost_work vs_event_work; /* evt injection work item */
204         struct llist_head vs_event_list; /* evt injection queue */
205
206         bool vs_events_missed; /* any missed events, protected by vq->mutex */
207         int vs_events_nr; /* num of pending events, protected by vq->mutex */
208 };
209
210 static struct target_core_fabric_ops vhost_scsi_ops;
211 static struct workqueue_struct *vhost_scsi_workqueue;
212
213 /* Global spinlock to protect vhost_scsi TPG list for vhost IOCTL access */
214 static DEFINE_MUTEX(vhost_scsi_mutex);
215 static LIST_HEAD(vhost_scsi_list);
216
217 static int iov_num_pages(void __user *iov_base, size_t iov_len)
218 {
219         return (PAGE_ALIGN((unsigned long)iov_base + iov_len) -
220                ((unsigned long)iov_base & PAGE_MASK)) >> PAGE_SHIFT;
221 }
222
223 static void vhost_scsi_done_inflight(struct kref *kref)
224 {
225         struct vhost_scsi_inflight *inflight;
226
227         inflight = container_of(kref, struct vhost_scsi_inflight, kref);
228         complete(&inflight->comp);
229 }
230
231 static void vhost_scsi_init_inflight(struct vhost_scsi *vs,
232                                     struct vhost_scsi_inflight *old_inflight[])
233 {
234         struct vhost_scsi_inflight *new_inflight;
235         struct vhost_virtqueue *vq;
236         int idx, i;
237
238         for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
239                 vq = &vs->vqs[i].vq;
240
241                 mutex_lock(&vq->mutex);
242
243                 /* store old infight */
244                 idx = vs->vqs[i].inflight_idx;
245                 if (old_inflight)
246                         old_inflight[i] = &vs->vqs[i].inflights[idx];
247
248                 /* setup new infight */
249                 vs->vqs[i].inflight_idx = idx ^ 1;
250                 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
251                 kref_init(&new_inflight->kref);
252                 init_completion(&new_inflight->comp);
253
254                 mutex_unlock(&vq->mutex);
255         }
256 }
257
258 static struct vhost_scsi_inflight *
259 vhost_scsi_get_inflight(struct vhost_virtqueue *vq)
260 {
261         struct vhost_scsi_inflight *inflight;
262         struct vhost_scsi_virtqueue *svq;
263
264         svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
265         inflight = &svq->inflights[svq->inflight_idx];
266         kref_get(&inflight->kref);
267
268         return inflight;
269 }
270
271 static void vhost_scsi_put_inflight(struct vhost_scsi_inflight *inflight)
272 {
273         kref_put(&inflight->kref, vhost_scsi_done_inflight);
274 }
275
276 static int vhost_scsi_check_true(struct se_portal_group *se_tpg)
277 {
278         return 1;
279 }
280
281 static int vhost_scsi_check_false(struct se_portal_group *se_tpg)
282 {
283         return 0;
284 }
285
286 static char *vhost_scsi_get_fabric_name(void)
287 {
288         return "vhost";
289 }
290
291 static u8 vhost_scsi_get_fabric_proto_ident(struct se_portal_group *se_tpg)
292 {
293         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
294                                 struct vhost_scsi_tpg, se_tpg);
295         struct vhost_scsi_tport *tport = tpg->tport;
296
297         switch (tport->tport_proto_id) {
298         case SCSI_PROTOCOL_SAS:
299                 return sas_get_fabric_proto_ident(se_tpg);
300         case SCSI_PROTOCOL_FCP:
301                 return fc_get_fabric_proto_ident(se_tpg);
302         case SCSI_PROTOCOL_ISCSI:
303                 return iscsi_get_fabric_proto_ident(se_tpg);
304         default:
305                 pr_err("Unknown tport_proto_id: 0x%02x, using"
306                         " SAS emulation\n", tport->tport_proto_id);
307                 break;
308         }
309
310         return sas_get_fabric_proto_ident(se_tpg);
311 }
312
313 static char *vhost_scsi_get_fabric_wwn(struct se_portal_group *se_tpg)
314 {
315         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
316                                 struct vhost_scsi_tpg, se_tpg);
317         struct vhost_scsi_tport *tport = tpg->tport;
318
319         return &tport->tport_name[0];
320 }
321
322 static u16 vhost_scsi_get_tpgt(struct se_portal_group *se_tpg)
323 {
324         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
325                                 struct vhost_scsi_tpg, se_tpg);
326         return tpg->tport_tpgt;
327 }
328
329 static u32
330 vhost_scsi_get_pr_transport_id(struct se_portal_group *se_tpg,
331                               struct se_node_acl *se_nacl,
332                               struct t10_pr_registration *pr_reg,
333                               int *format_code,
334                               unsigned char *buf)
335 {
336         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
337                                 struct vhost_scsi_tpg, se_tpg);
338         struct vhost_scsi_tport *tport = tpg->tport;
339
340         switch (tport->tport_proto_id) {
341         case SCSI_PROTOCOL_SAS:
342                 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
343                                         format_code, buf);
344         case SCSI_PROTOCOL_FCP:
345                 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
346                                         format_code, buf);
347         case SCSI_PROTOCOL_ISCSI:
348                 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
349                                         format_code, buf);
350         default:
351                 pr_err("Unknown tport_proto_id: 0x%02x, using"
352                         " SAS emulation\n", tport->tport_proto_id);
353                 break;
354         }
355
356         return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
357                         format_code, buf);
358 }
359
360 static u32
361 vhost_scsi_get_pr_transport_id_len(struct se_portal_group *se_tpg,
362                                   struct se_node_acl *se_nacl,
363                                   struct t10_pr_registration *pr_reg,
364                                   int *format_code)
365 {
366         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
367                                 struct vhost_scsi_tpg, se_tpg);
368         struct vhost_scsi_tport *tport = tpg->tport;
369
370         switch (tport->tport_proto_id) {
371         case SCSI_PROTOCOL_SAS:
372                 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
373                                         format_code);
374         case SCSI_PROTOCOL_FCP:
375                 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
376                                         format_code);
377         case SCSI_PROTOCOL_ISCSI:
378                 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
379                                         format_code);
380         default:
381                 pr_err("Unknown tport_proto_id: 0x%02x, using"
382                         " SAS emulation\n", tport->tport_proto_id);
383                 break;
384         }
385
386         return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
387                         format_code);
388 }
389
390 static char *
391 vhost_scsi_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
392                                     const char *buf,
393                                     u32 *out_tid_len,
394                                     char **port_nexus_ptr)
395 {
396         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
397                                 struct vhost_scsi_tpg, se_tpg);
398         struct vhost_scsi_tport *tport = tpg->tport;
399
400         switch (tport->tport_proto_id) {
401         case SCSI_PROTOCOL_SAS:
402                 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
403                                         port_nexus_ptr);
404         case SCSI_PROTOCOL_FCP:
405                 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
406                                         port_nexus_ptr);
407         case SCSI_PROTOCOL_ISCSI:
408                 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
409                                         port_nexus_ptr);
410         default:
411                 pr_err("Unknown tport_proto_id: 0x%02x, using"
412                         " SAS emulation\n", tport->tport_proto_id);
413                 break;
414         }
415
416         return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
417                         port_nexus_ptr);
418 }
419
420 static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg)
421 {
422         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
423                                 struct vhost_scsi_tpg, se_tpg);
424
425         return tpg->tv_fabric_prot_type;
426 }
427
428 static struct se_node_acl *
429 vhost_scsi_alloc_fabric_acl(struct se_portal_group *se_tpg)
430 {
431         return kzalloc(sizeof(struct se_node_acl), GFP_KERNEL);
432 }
433
434 static void
435 vhost_scsi_release_fabric_acl(struct se_portal_group *se_tpg,
436                              struct se_node_acl *se_nacl)
437 {
438         kfree(se_nacl);
439 }
440
441 static u32 vhost_scsi_tpg_get_inst_index(struct se_portal_group *se_tpg)
442 {
443         return 1;
444 }
445
446 static void vhost_scsi_release_cmd(struct se_cmd *se_cmd)
447 {
448         struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd,
449                                 struct vhost_scsi_cmd, tvc_se_cmd);
450         struct se_session *se_sess = tv_cmd->tvc_nexus->tvn_se_sess;
451         int i;
452
453         if (tv_cmd->tvc_sgl_count) {
454                 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
455                         put_page(sg_page(&tv_cmd->tvc_sgl[i]));
456         }
457         if (tv_cmd->tvc_prot_sgl_count) {
458                 for (i = 0; i < tv_cmd->tvc_prot_sgl_count; i++)
459                         put_page(sg_page(&tv_cmd->tvc_prot_sgl[i]));
460         }
461
462         vhost_scsi_put_inflight(tv_cmd->inflight);
463         percpu_ida_free(&se_sess->sess_tag_pool, se_cmd->map_tag);
464 }
465
466 static int vhost_scsi_shutdown_session(struct se_session *se_sess)
467 {
468         return 0;
469 }
470
471 static void vhost_scsi_close_session(struct se_session *se_sess)
472 {
473         return;
474 }
475
476 static u32 vhost_scsi_sess_get_index(struct se_session *se_sess)
477 {
478         return 0;
479 }
480
481 static int vhost_scsi_write_pending(struct se_cmd *se_cmd)
482 {
483         /* Go ahead and process the write immediately */
484         target_execute_cmd(se_cmd);
485         return 0;
486 }
487
488 static int vhost_scsi_write_pending_status(struct se_cmd *se_cmd)
489 {
490         return 0;
491 }
492
493 static void vhost_scsi_set_default_node_attrs(struct se_node_acl *nacl)
494 {
495         return;
496 }
497
498 static u32 vhost_scsi_get_task_tag(struct se_cmd *se_cmd)
499 {
500         return 0;
501 }
502
503 static int vhost_scsi_get_cmd_state(struct se_cmd *se_cmd)
504 {
505         return 0;
506 }
507
508 static void vhost_scsi_complete_cmd(struct vhost_scsi_cmd *cmd)
509 {
510         struct vhost_scsi *vs = cmd->tvc_vhost;
511
512         llist_add(&cmd->tvc_completion_list, &vs->vs_completion_list);
513
514         vhost_work_queue(&vs->dev, &vs->vs_completion_work);
515 }
516
517 static int vhost_scsi_queue_data_in(struct se_cmd *se_cmd)
518 {
519         struct vhost_scsi_cmd *cmd = container_of(se_cmd,
520                                 struct vhost_scsi_cmd, tvc_se_cmd);
521         vhost_scsi_complete_cmd(cmd);
522         return 0;
523 }
524
525 static int vhost_scsi_queue_status(struct se_cmd *se_cmd)
526 {
527         struct vhost_scsi_cmd *cmd = container_of(se_cmd,
528                                 struct vhost_scsi_cmd, tvc_se_cmd);
529         vhost_scsi_complete_cmd(cmd);
530         return 0;
531 }
532
533 static void vhost_scsi_queue_tm_rsp(struct se_cmd *se_cmd)
534 {
535         return;
536 }
537
538 static void vhost_scsi_aborted_task(struct se_cmd *se_cmd)
539 {
540         return;
541 }
542
543 static void vhost_scsi_free_evt(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
544 {
545         vs->vs_events_nr--;
546         kfree(evt);
547 }
548
549 static struct vhost_scsi_evt *
550 vhost_scsi_allocate_evt(struct vhost_scsi *vs,
551                        u32 event, u32 reason)
552 {
553         struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
554         struct vhost_scsi_evt *evt;
555
556         if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
557                 vs->vs_events_missed = true;
558                 return NULL;
559         }
560
561         evt = kzalloc(sizeof(*evt), GFP_KERNEL);
562         if (!evt) {
563                 vq_err(vq, "Failed to allocate vhost_scsi_evt\n");
564                 vs->vs_events_missed = true;
565                 return NULL;
566         }
567
568         evt->event.event = cpu_to_vhost32(vq, event);
569         evt->event.reason = cpu_to_vhost32(vq, reason);
570         vs->vs_events_nr++;
571
572         return evt;
573 }
574
575 static void vhost_scsi_free_cmd(struct vhost_scsi_cmd *cmd)
576 {
577         struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
578
579         /* TODO locking against target/backend threads? */
580         transport_generic_free_cmd(se_cmd, 0);
581
582 }
583
584 static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
585 {
586         return target_put_sess_cmd(se_cmd);
587 }
588
589 static void
590 vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
591 {
592         struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
593         struct virtio_scsi_event *event = &evt->event;
594         struct virtio_scsi_event __user *eventp;
595         unsigned out, in;
596         int head, ret;
597
598         if (!vq->private_data) {
599                 vs->vs_events_missed = true;
600                 return;
601         }
602
603 again:
604         vhost_disable_notify(&vs->dev, vq);
605         head = vhost_get_vq_desc(vq, vq->iov,
606                         ARRAY_SIZE(vq->iov), &out, &in,
607                         NULL, NULL);
608         if (head < 0) {
609                 vs->vs_events_missed = true;
610                 return;
611         }
612         if (head == vq->num) {
613                 if (vhost_enable_notify(&vs->dev, vq))
614                         goto again;
615                 vs->vs_events_missed = true;
616                 return;
617         }
618
619         if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
620                 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
621                                 vq->iov[out].iov_len);
622                 vs->vs_events_missed = true;
623                 return;
624         }
625
626         if (vs->vs_events_missed) {
627                 event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
628                 vs->vs_events_missed = false;
629         }
630
631         eventp = vq->iov[out].iov_base;
632         ret = __copy_to_user(eventp, event, sizeof(*event));
633         if (!ret)
634                 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
635         else
636                 vq_err(vq, "Faulted on vhost_scsi_send_event\n");
637 }
638
639 static void vhost_scsi_evt_work(struct vhost_work *work)
640 {
641         struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
642                                         vs_event_work);
643         struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
644         struct vhost_scsi_evt *evt;
645         struct llist_node *llnode;
646
647         mutex_lock(&vq->mutex);
648         llnode = llist_del_all(&vs->vs_event_list);
649         while (llnode) {
650                 evt = llist_entry(llnode, struct vhost_scsi_evt, list);
651                 llnode = llist_next(llnode);
652                 vhost_scsi_do_evt_work(vs, evt);
653                 vhost_scsi_free_evt(vs, evt);
654         }
655         mutex_unlock(&vq->mutex);
656 }
657
658 /* Fill in status and signal that we are done processing this command
659  *
660  * This is scheduled in the vhost work queue so we are called with the owner
661  * process mm and can access the vring.
662  */
663 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
664 {
665         struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
666                                         vs_completion_work);
667         DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
668         struct virtio_scsi_cmd_resp v_rsp;
669         struct vhost_scsi_cmd *cmd;
670         struct llist_node *llnode;
671         struct se_cmd *se_cmd;
672         struct iov_iter iov_iter;
673         int ret, vq;
674
675         bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
676         llnode = llist_del_all(&vs->vs_completion_list);
677         while (llnode) {
678                 cmd = llist_entry(llnode, struct vhost_scsi_cmd,
679                                      tvc_completion_list);
680                 llnode = llist_next(llnode);
681                 se_cmd = &cmd->tvc_se_cmd;
682
683                 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
684                         cmd, se_cmd->residual_count, se_cmd->scsi_status);
685
686                 memset(&v_rsp, 0, sizeof(v_rsp));
687                 v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq, se_cmd->residual_count);
688                 /* TODO is status_qualifier field needed? */
689                 v_rsp.status = se_cmd->scsi_status;
690                 v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
691                                                  se_cmd->scsi_sense_length);
692                 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
693                        se_cmd->scsi_sense_length);
694
695                 iov_iter_init(&iov_iter, READ, cmd->tvc_resp_iov,
696                               cmd->tvc_in_iovs, sizeof(v_rsp));
697                 ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter);
698                 if (likely(ret == sizeof(v_rsp))) {
699                         struct vhost_scsi_virtqueue *q;
700                         vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
701                         q = container_of(cmd->tvc_vq, struct vhost_scsi_virtqueue, vq);
702                         vq = q - vs->vqs;
703                         __set_bit(vq, signal);
704                 } else
705                         pr_err("Faulted on virtio_scsi_cmd_resp\n");
706
707                 vhost_scsi_free_cmd(cmd);
708         }
709
710         vq = -1;
711         while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
712                 < VHOST_SCSI_MAX_VQ)
713                 vhost_signal(&vs->dev, &vs->vqs[vq].vq);
714 }
715
716 static struct vhost_scsi_cmd *
717 vhost_scsi_get_tag(struct vhost_virtqueue *vq, struct vhost_scsi_tpg *tpg,
718                    unsigned char *cdb, u64 scsi_tag, u16 lun, u8 task_attr,
719                    u32 exp_data_len, int data_direction)
720 {
721         struct vhost_scsi_cmd *cmd;
722         struct vhost_scsi_nexus *tv_nexus;
723         struct se_session *se_sess;
724         struct scatterlist *sg, *prot_sg;
725         struct page **pages;
726         int tag;
727
728         tv_nexus = tpg->tpg_nexus;
729         if (!tv_nexus) {
730                 pr_err("Unable to locate active struct vhost_scsi_nexus\n");
731                 return ERR_PTR(-EIO);
732         }
733         se_sess = tv_nexus->tvn_se_sess;
734
735         tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
736         if (tag < 0) {
737                 pr_err("Unable to obtain tag for vhost_scsi_cmd\n");
738                 return ERR_PTR(-ENOMEM);
739         }
740
741         cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[tag];
742         sg = cmd->tvc_sgl;
743         prot_sg = cmd->tvc_prot_sgl;
744         pages = cmd->tvc_upages;
745         memset(cmd, 0, sizeof(struct vhost_scsi_cmd));
746
747         cmd->tvc_sgl = sg;
748         cmd->tvc_prot_sgl = prot_sg;
749         cmd->tvc_upages = pages;
750         cmd->tvc_se_cmd.map_tag = tag;
751         cmd->tvc_tag = scsi_tag;
752         cmd->tvc_lun = lun;
753         cmd->tvc_task_attr = task_attr;
754         cmd->tvc_exp_data_len = exp_data_len;
755         cmd->tvc_data_direction = data_direction;
756         cmd->tvc_nexus = tv_nexus;
757         cmd->inflight = vhost_scsi_get_inflight(vq);
758
759         memcpy(cmd->tvc_cdb, cdb, VHOST_SCSI_MAX_CDB_SIZE);
760
761         return cmd;
762 }
763
764 /*
765  * Map a user memory range into a scatterlist
766  *
767  * Returns the number of scatterlist entries used or -errno on error.
768  */
769 static int
770 vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
771                       void __user *ptr,
772                       size_t len,
773                       struct scatterlist *sgl,
774                       bool write)
775 {
776         unsigned int npages = 0, offset, nbytes;
777         unsigned int pages_nr = iov_num_pages(ptr, len);
778         struct scatterlist *sg = sgl;
779         struct page **pages = cmd->tvc_upages;
780         int ret, i;
781
782         if (pages_nr > VHOST_SCSI_PREALLOC_UPAGES) {
783                 pr_err("vhost_scsi_map_to_sgl() pages_nr: %u greater than"
784                        " preallocated VHOST_SCSI_PREALLOC_UPAGES: %u\n",
785                         pages_nr, VHOST_SCSI_PREALLOC_UPAGES);
786                 return -ENOBUFS;
787         }
788
789         ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
790         /* No pages were pinned */
791         if (ret < 0)
792                 goto out;
793         /* Less pages pinned than wanted */
794         if (ret != pages_nr) {
795                 for (i = 0; i < ret; i++)
796                         put_page(pages[i]);
797                 ret = -EFAULT;
798                 goto out;
799         }
800
801         while (len > 0) {
802                 offset = (uintptr_t)ptr & ~PAGE_MASK;
803                 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
804                 sg_set_page(sg, pages[npages], nbytes, offset);
805                 ptr += nbytes;
806                 len -= nbytes;
807                 sg++;
808                 npages++;
809         }
810
811 out:
812         return ret;
813 }
814
815 static int
816 vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
817 {
818         int sgl_count = 0;
819
820         if (!iter || !iter->iov) {
821                 pr_err("%s: iter->iov is NULL, but expected bytes: %zu"
822                        " present\n", __func__, bytes);
823                 return -EINVAL;
824         }
825
826         sgl_count = iov_iter_npages(iter, 0xffff);
827         if (sgl_count > max_sgls) {
828                 pr_err("%s: requested sgl_count: %d exceeds pre-allocated"
829                        " max_sgls: %d\n", __func__, sgl_count, max_sgls);
830                 return -EINVAL;
831         }
832         return sgl_count;
833 }
834
835 static int
836 vhost_scsi_iov_to_sgl(struct vhost_scsi_cmd *cmd, bool write,
837                       struct iov_iter *iter,
838                       struct scatterlist *sg, int sg_count)
839 {
840         size_t off = iter->iov_offset;
841         int i, ret;
842
843         for (i = 0; i < iter->nr_segs; i++) {
844                 void __user *base = iter->iov[i].iov_base + off;
845                 size_t len = iter->iov[i].iov_len - off;
846
847                 ret = vhost_scsi_map_to_sgl(cmd, base, len, sg, write);
848                 if (ret < 0) {
849                         for (i = 0; i < sg_count; i++) {
850                                 struct page *page = sg_page(&sg[i]);
851                                 if (page)
852                                         put_page(page);
853                         }
854                         return ret;
855                 }
856                 sg += ret;
857                 off = 0;
858         }
859         return 0;
860 }
861
862 static int
863 vhost_scsi_mapal(struct vhost_scsi_cmd *cmd,
864                  size_t prot_bytes, struct iov_iter *prot_iter,
865                  size_t data_bytes, struct iov_iter *data_iter)
866 {
867         int sgl_count, ret;
868         bool write = (cmd->tvc_data_direction == DMA_FROM_DEVICE);
869
870         if (prot_bytes) {
871                 sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes,
872                                                  VHOST_SCSI_PREALLOC_PROT_SGLS);
873                 if (sgl_count < 0)
874                         return sgl_count;
875
876                 sg_init_table(cmd->tvc_prot_sgl, sgl_count);
877                 cmd->tvc_prot_sgl_count = sgl_count;
878                 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
879                          cmd->tvc_prot_sgl, cmd->tvc_prot_sgl_count);
880
881                 ret = vhost_scsi_iov_to_sgl(cmd, write, prot_iter,
882                                             cmd->tvc_prot_sgl,
883                                             cmd->tvc_prot_sgl_count);
884                 if (ret < 0) {
885                         cmd->tvc_prot_sgl_count = 0;
886                         return ret;
887                 }
888         }
889         sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes,
890                                          VHOST_SCSI_PREALLOC_SGLS);
891         if (sgl_count < 0)
892                 return sgl_count;
893
894         sg_init_table(cmd->tvc_sgl, sgl_count);
895         cmd->tvc_sgl_count = sgl_count;
896         pr_debug("%s data_sg %p data_sgl_count %u\n", __func__,
897                   cmd->tvc_sgl, cmd->tvc_sgl_count);
898
899         ret = vhost_scsi_iov_to_sgl(cmd, write, data_iter,
900                                     cmd->tvc_sgl, cmd->tvc_sgl_count);
901         if (ret < 0) {
902                 cmd->tvc_sgl_count = 0;
903                 return ret;
904         }
905         return 0;
906 }
907
908 static int vhost_scsi_to_tcm_attr(int attr)
909 {
910         switch (attr) {
911         case VIRTIO_SCSI_S_SIMPLE:
912                 return TCM_SIMPLE_TAG;
913         case VIRTIO_SCSI_S_ORDERED:
914                 return TCM_ORDERED_TAG;
915         case VIRTIO_SCSI_S_HEAD:
916                 return TCM_HEAD_TAG;
917         case VIRTIO_SCSI_S_ACA:
918                 return TCM_ACA_TAG;
919         default:
920                 break;
921         }
922         return TCM_SIMPLE_TAG;
923 }
924
925 static void vhost_scsi_submission_work(struct work_struct *work)
926 {
927         struct vhost_scsi_cmd *cmd =
928                 container_of(work, struct vhost_scsi_cmd, work);
929         struct vhost_scsi_nexus *tv_nexus;
930         struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
931         struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
932         int rc;
933
934         /* FIXME: BIDI operation */
935         if (cmd->tvc_sgl_count) {
936                 sg_ptr = cmd->tvc_sgl;
937
938                 if (cmd->tvc_prot_sgl_count)
939                         sg_prot_ptr = cmd->tvc_prot_sgl;
940                 else
941                         se_cmd->prot_pto = true;
942         } else {
943                 sg_ptr = NULL;
944         }
945         tv_nexus = cmd->tvc_nexus;
946
947         rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
948                         cmd->tvc_cdb, &cmd->tvc_sense_buf[0],
949                         cmd->tvc_lun, cmd->tvc_exp_data_len,
950                         vhost_scsi_to_tcm_attr(cmd->tvc_task_attr),
951                         cmd->tvc_data_direction, TARGET_SCF_ACK_KREF,
952                         sg_ptr, cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr,
953                         cmd->tvc_prot_sgl_count);
954         if (rc < 0) {
955                 transport_send_check_condition_and_sense(se_cmd,
956                                 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
957                 transport_generic_free_cmd(se_cmd, 0);
958         }
959 }
960
961 static void
962 vhost_scsi_send_bad_target(struct vhost_scsi *vs,
963                            struct vhost_virtqueue *vq,
964                            int head, unsigned out)
965 {
966         struct virtio_scsi_cmd_resp __user *resp;
967         struct virtio_scsi_cmd_resp rsp;
968         int ret;
969
970         memset(&rsp, 0, sizeof(rsp));
971         rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
972         resp = vq->iov[out].iov_base;
973         ret = __copy_to_user(resp, &rsp, sizeof(rsp));
974         if (!ret)
975                 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
976         else
977                 pr_err("Faulted on virtio_scsi_cmd_resp\n");
978 }
979
980 static void
981 vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
982 {
983         struct vhost_scsi_tpg **vs_tpg, *tpg;
984         struct virtio_scsi_cmd_req v_req;
985         struct virtio_scsi_cmd_req_pi v_req_pi;
986         struct vhost_scsi_cmd *cmd;
987         struct iov_iter out_iter, in_iter, prot_iter, data_iter;
988         u64 tag;
989         u32 exp_data_len, data_direction;
990         unsigned out, in;
991         int head, ret, prot_bytes;
992         size_t req_size, rsp_size = sizeof(struct virtio_scsi_cmd_resp);
993         size_t out_size, in_size;
994         u16 lun;
995         u8 *target, *lunp, task_attr;
996         bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
997         void *req, *cdb;
998
999         mutex_lock(&vq->mutex);
1000         /*
1001          * We can handle the vq only after the endpoint is setup by calling the
1002          * VHOST_SCSI_SET_ENDPOINT ioctl.
1003          */
1004         vs_tpg = vq->private_data;
1005         if (!vs_tpg)
1006                 goto out;
1007
1008         vhost_disable_notify(&vs->dev, vq);
1009
1010         for (;;) {
1011                 head = vhost_get_vq_desc(vq, vq->iov,
1012                                          ARRAY_SIZE(vq->iov), &out, &in,
1013                                          NULL, NULL);
1014                 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
1015                          head, out, in);
1016                 /* On error, stop handling until the next kick. */
1017                 if (unlikely(head < 0))
1018                         break;
1019                 /* Nothing new?  Wait for eventfd to tell us they refilled. */
1020                 if (head == vq->num) {
1021                         if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
1022                                 vhost_disable_notify(&vs->dev, vq);
1023                                 continue;
1024                         }
1025                         break;
1026                 }
1027                 /*
1028                  * Check for a sane response buffer so we can report early
1029                  * errors back to the guest.
1030                  */
1031                 if (unlikely(vq->iov[out].iov_len < rsp_size)) {
1032                         vq_err(vq, "Expecting at least virtio_scsi_cmd_resp"
1033                                 " size, got %zu bytes\n", vq->iov[out].iov_len);
1034                         break;
1035                 }
1036                 /*
1037                  * Setup pointers and values based upon different virtio-scsi
1038                  * request header if T10_PI is enabled in KVM guest.
1039                  */
1040                 if (t10_pi) {
1041                         req = &v_req_pi;
1042                         req_size = sizeof(v_req_pi);
1043                         lunp = &v_req_pi.lun[0];
1044                         target = &v_req_pi.lun[1];
1045                 } else {
1046                         req = &v_req;
1047                         req_size = sizeof(v_req);
1048                         lunp = &v_req.lun[0];
1049                         target = &v_req.lun[1];
1050                 }
1051                 /*
1052                  * FIXME: Not correct for BIDI operation
1053                  */
1054                 out_size = iov_length(vq->iov, out);
1055                 in_size = iov_length(&vq->iov[out], in);
1056
1057                 /*
1058                  * Copy over the virtio-scsi request header, which for a
1059                  * ANY_LAYOUT enabled guest may span multiple iovecs, or a
1060                  * single iovec may contain both the header + outgoing
1061                  * WRITE payloads.
1062                  *
1063                  * copy_from_iter() will advance out_iter, so that it will
1064                  * point at the start of the outgoing WRITE payload, if
1065                  * DMA_TO_DEVICE is set.
1066                  */
1067                 iov_iter_init(&out_iter, WRITE, vq->iov, out, out_size);
1068
1069                 ret = copy_from_iter(req, req_size, &out_iter);
1070                 if (unlikely(ret != req_size)) {
1071                         vq_err(vq, "Faulted on copy_from_iter\n");
1072                         vhost_scsi_send_bad_target(vs, vq, head, out);
1073                         continue;
1074                 }
1075                 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
1076                 if (unlikely(*lunp != 1)) {
1077                         vq_err(vq, "Illegal virtio-scsi lun: %u\n", *lunp);
1078                         vhost_scsi_send_bad_target(vs, vq, head, out);
1079                         continue;
1080                 }
1081
1082                 tpg = ACCESS_ONCE(vs_tpg[*target]);
1083                 if (unlikely(!tpg)) {
1084                         /* Target does not exist, fail the request */
1085                         vhost_scsi_send_bad_target(vs, vq, head, out);
1086                         continue;
1087                 }
1088                 /*
1089                  * Determine data_direction by calculating the total outgoing
1090                  * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
1091                  * response headers respectively.
1092                  *
1093                  * For DMA_TO_DEVICE this is out_iter, which is already pointing
1094                  * to the right place.
1095                  *
1096                  * For DMA_FROM_DEVICE, the iovec will be just past the end
1097                  * of the virtio-scsi response header in either the same
1098                  * or immediately following iovec.
1099                  *
1100                  * Any associated T10_PI bytes for the outgoing / incoming
1101                  * payloads are included in calculation of exp_data_len here.
1102                  */
1103                 prot_bytes = 0;
1104
1105                 if (out_size > req_size) {
1106                         data_direction = DMA_TO_DEVICE;
1107                         exp_data_len = out_size - req_size;
1108                         data_iter = out_iter;
1109                 } else if (in_size > rsp_size) {
1110                         data_direction = DMA_FROM_DEVICE;
1111                         exp_data_len = in_size - rsp_size;
1112
1113                         iov_iter_init(&in_iter, READ, &vq->iov[out], in,
1114                                       rsp_size + exp_data_len);
1115                         iov_iter_advance(&in_iter, rsp_size);
1116                         data_iter = in_iter;
1117                 } else {
1118                         data_direction = DMA_NONE;
1119                         exp_data_len = 0;
1120                 }
1121                 /*
1122                  * If T10_PI header + payload is present, setup prot_iter values
1123                  * and recalculate data_iter for vhost_scsi_mapal() mapping to
1124                  * host scatterlists via get_user_pages_fast().
1125                  */
1126                 if (t10_pi) {
1127                         if (v_req_pi.pi_bytesout) {
1128                                 if (data_direction != DMA_TO_DEVICE) {
1129                                         vq_err(vq, "Received non zero pi_bytesout,"
1130                                                 " but wrong data_direction\n");
1131                                         vhost_scsi_send_bad_target(vs, vq, head, out);
1132                                         continue;
1133                                 }
1134                                 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
1135                         } else if (v_req_pi.pi_bytesin) {
1136                                 if (data_direction != DMA_FROM_DEVICE) {
1137                                         vq_err(vq, "Received non zero pi_bytesin,"
1138                                                 " but wrong data_direction\n");
1139                                         vhost_scsi_send_bad_target(vs, vq, head, out);
1140                                         continue;
1141                                 }
1142                                 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
1143                         }
1144                         /*
1145                          * Set prot_iter to data_iter, and advance past any
1146                          * preceeding prot_bytes that may be present.
1147                          *
1148                          * Also fix up the exp_data_len to reflect only the
1149                          * actual data payload length.
1150                          */
1151                         if (prot_bytes) {
1152                                 exp_data_len -= prot_bytes;
1153                                 prot_iter = data_iter;
1154                                 iov_iter_advance(&data_iter, prot_bytes);
1155                         }
1156                         tag = vhost64_to_cpu(vq, v_req_pi.tag);
1157                         task_attr = v_req_pi.task_attr;
1158                         cdb = &v_req_pi.cdb[0];
1159                         lun = ((v_req_pi.lun[2] << 8) | v_req_pi.lun[3]) & 0x3FFF;
1160                 } else {
1161                         tag = vhost64_to_cpu(vq, v_req.tag);
1162                         task_attr = v_req.task_attr;
1163                         cdb = &v_req.cdb[0];
1164                         lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
1165                 }
1166                 /*
1167                  * Check that the received CDB size does not exceeded our
1168                  * hardcoded max for vhost-scsi, then get a pre-allocated
1169                  * cmd descriptor for the new virtio-scsi tag.
1170                  *
1171                  * TODO what if cdb was too small for varlen cdb header?
1172                  */
1173                 if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) {
1174                         vq_err(vq, "Received SCSI CDB with command_size: %d that"
1175                                 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1176                                 scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE);
1177                         vhost_scsi_send_bad_target(vs, vq, head, out);
1178                         continue;
1179                 }
1180                 cmd = vhost_scsi_get_tag(vq, tpg, cdb, tag, lun, task_attr,
1181                                          exp_data_len + prot_bytes,
1182                                          data_direction);
1183                 if (IS_ERR(cmd)) {
1184                         vq_err(vq, "vhost_scsi_get_tag failed %ld\n",
1185                                PTR_ERR(cmd));
1186                         vhost_scsi_send_bad_target(vs, vq, head, out);
1187                         continue;
1188                 }
1189                 cmd->tvc_vhost = vs;
1190                 cmd->tvc_vq = vq;
1191                 cmd->tvc_resp_iov = &vq->iov[out];
1192                 cmd->tvc_in_iovs = in;
1193
1194                 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
1195                          cmd->tvc_cdb[0], cmd->tvc_lun);
1196                 pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
1197                          " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
1198
1199                 if (data_direction != DMA_NONE) {
1200                         ret = vhost_scsi_mapal(cmd,
1201                                                prot_bytes, &prot_iter,
1202                                                exp_data_len, &data_iter);
1203                         if (unlikely(ret)) {
1204                                 vq_err(vq, "Failed to map iov to sgl\n");
1205                                 vhost_scsi_release_cmd(&cmd->tvc_se_cmd);
1206                                 vhost_scsi_send_bad_target(vs, vq, head, out);
1207                                 continue;
1208                         }
1209                 }
1210                 /*
1211                  * Save the descriptor from vhost_get_vq_desc() to be used to
1212                  * complete the virtio-scsi request in TCM callback context via
1213                  * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
1214                  */
1215                 cmd->tvc_vq_desc = head;
1216                 /*
1217                  * Dispatch cmd descriptor for cmwq execution in process
1218                  * context provided by vhost_scsi_workqueue.  This also ensures
1219                  * cmd is executed on the same kworker CPU as this vhost
1220                  * thread to gain positive L2 cache locality effects.
1221                  */
1222                 INIT_WORK(&cmd->work, vhost_scsi_submission_work);
1223                 queue_work(vhost_scsi_workqueue, &cmd->work);
1224         }
1225 out:
1226         mutex_unlock(&vq->mutex);
1227 }
1228
1229 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1230 {
1231         pr_debug("%s: The handling func for control queue.\n", __func__);
1232 }
1233
1234 static void
1235 vhost_scsi_send_evt(struct vhost_scsi *vs,
1236                    struct vhost_scsi_tpg *tpg,
1237                    struct se_lun *lun,
1238                    u32 event,
1239                    u32 reason)
1240 {
1241         struct vhost_scsi_evt *evt;
1242
1243         evt = vhost_scsi_allocate_evt(vs, event, reason);
1244         if (!evt)
1245                 return;
1246
1247         if (tpg && lun) {
1248                 /* TODO: share lun setup code with virtio-scsi.ko */
1249                 /*
1250                  * Note: evt->event is zeroed when we allocate it and
1251                  * lun[4-7] need to be zero according to virtio-scsi spec.
1252                  */
1253                 evt->event.lun[0] = 0x01;
1254                 evt->event.lun[1] = tpg->tport_tpgt;
1255                 if (lun->unpacked_lun >= 256)
1256                         evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1257                 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1258         }
1259
1260         llist_add(&evt->list, &vs->vs_event_list);
1261         vhost_work_queue(&vs->dev, &vs->vs_event_work);
1262 }
1263
1264 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1265 {
1266         struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1267                                                 poll.work);
1268         struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1269
1270         mutex_lock(&vq->mutex);
1271         if (!vq->private_data)
1272                 goto out;
1273
1274         if (vs->vs_events_missed)
1275                 vhost_scsi_send_evt(vs, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
1276 out:
1277         mutex_unlock(&vq->mutex);
1278 }
1279
1280 static void vhost_scsi_handle_kick(struct vhost_work *work)
1281 {
1282         struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1283                                                 poll.work);
1284         struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1285
1286         vhost_scsi_handle_vq(vs, vq);
1287 }
1288
1289 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
1290 {
1291         vhost_poll_flush(&vs->vqs[index].vq.poll);
1292 }
1293
1294 /* Callers must hold dev mutex */
1295 static void vhost_scsi_flush(struct vhost_scsi *vs)
1296 {
1297         struct vhost_scsi_inflight *old_inflight[VHOST_SCSI_MAX_VQ];
1298         int i;
1299
1300         /* Init new inflight and remember the old inflight */
1301         vhost_scsi_init_inflight(vs, old_inflight);
1302
1303         /*
1304          * The inflight->kref was initialized to 1. We decrement it here to
1305          * indicate the start of the flush operation so that it will reach 0
1306          * when all the reqs are finished.
1307          */
1308         for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1309                 kref_put(&old_inflight[i]->kref, vhost_scsi_done_inflight);
1310
1311         /* Flush both the vhost poll and vhost work */
1312         for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1313                 vhost_scsi_flush_vq(vs, i);
1314         vhost_work_flush(&vs->dev, &vs->vs_completion_work);
1315         vhost_work_flush(&vs->dev, &vs->vs_event_work);
1316
1317         /* Wait for all reqs issued before the flush to be finished */
1318         for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
1319                 wait_for_completion(&old_inflight[i]->comp);
1320 }
1321
1322 /*
1323  * Called from vhost_scsi_ioctl() context to walk the list of available
1324  * vhost_scsi_tpg with an active struct vhost_scsi_nexus
1325  *
1326  *  The lock nesting rule is:
1327  *    vhost_scsi_mutex -> vs->dev.mutex -> tpg->tv_tpg_mutex -> vq->mutex
1328  */
1329 static int
1330 vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1331                         struct vhost_scsi_target *t)
1332 {
1333         struct se_portal_group *se_tpg;
1334         struct vhost_scsi_tport *tv_tport;
1335         struct vhost_scsi_tpg *tpg;
1336         struct vhost_scsi_tpg **vs_tpg;
1337         struct vhost_virtqueue *vq;
1338         int index, ret, i, len;
1339         bool match = false;
1340
1341         mutex_lock(&vhost_scsi_mutex);
1342         mutex_lock(&vs->dev.mutex);
1343
1344         /* Verify that ring has been setup correctly. */
1345         for (index = 0; index < vs->dev.nvqs; ++index) {
1346                 /* Verify that ring has been setup correctly. */
1347                 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1348                         ret = -EFAULT;
1349                         goto out;
1350                 }
1351         }
1352
1353         len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
1354         vs_tpg = kzalloc(len, GFP_KERNEL);
1355         if (!vs_tpg) {
1356                 ret = -ENOMEM;
1357                 goto out;
1358         }
1359         if (vs->vs_tpg)
1360                 memcpy(vs_tpg, vs->vs_tpg, len);
1361
1362         list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) {
1363                 mutex_lock(&tpg->tv_tpg_mutex);
1364                 if (!tpg->tpg_nexus) {
1365                         mutex_unlock(&tpg->tv_tpg_mutex);
1366                         continue;
1367                 }
1368                 if (tpg->tv_tpg_vhost_count != 0) {
1369                         mutex_unlock(&tpg->tv_tpg_mutex);
1370                         continue;
1371                 }
1372                 tv_tport = tpg->tport;
1373
1374                 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1375                         if (vs->vs_tpg && vs->vs_tpg[tpg->tport_tpgt]) {
1376                                 kfree(vs_tpg);
1377                                 mutex_unlock(&tpg->tv_tpg_mutex);
1378                                 ret = -EEXIST;
1379                                 goto out;
1380                         }
1381                         /*
1382                          * In order to ensure individual vhost-scsi configfs
1383                          * groups cannot be removed while in use by vhost ioctl,
1384                          * go ahead and take an explicit se_tpg->tpg_group.cg_item
1385                          * dependency now.
1386                          */
1387                         se_tpg = &tpg->se_tpg;
1388                         ret = target_depend_item(&se_tpg->tpg_group.cg_item);
1389                         if (ret) {
1390                                 pr_warn("configfs_depend_item() failed: %d\n", ret);
1391                                 kfree(vs_tpg);
1392                                 mutex_unlock(&tpg->tv_tpg_mutex);
1393                                 goto out;
1394                         }
1395                         tpg->tv_tpg_vhost_count++;
1396                         tpg->vhost_scsi = vs;
1397                         vs_tpg[tpg->tport_tpgt] = tpg;
1398                         smp_mb__after_atomic();
1399                         match = true;
1400                 }
1401                 mutex_unlock(&tpg->tv_tpg_mutex);
1402         }
1403
1404         if (match) {
1405                 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
1406                        sizeof(vs->vs_vhost_wwpn));
1407                 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1408                         vq = &vs->vqs[i].vq;
1409                         mutex_lock(&vq->mutex);
1410                         vq->private_data = vs_tpg;
1411                         vhost_init_used(vq);
1412                         mutex_unlock(&vq->mutex);
1413                 }
1414                 ret = 0;
1415         } else {
1416                 ret = -EEXIST;
1417         }
1418
1419         /*
1420          * Act as synchronize_rcu to make sure access to
1421          * old vs->vs_tpg is finished.
1422          */
1423         vhost_scsi_flush(vs);
1424         kfree(vs->vs_tpg);
1425         vs->vs_tpg = vs_tpg;
1426
1427 out:
1428         mutex_unlock(&vs->dev.mutex);
1429         mutex_unlock(&vhost_scsi_mutex);
1430         return ret;
1431 }
1432
1433 static int
1434 vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
1435                           struct vhost_scsi_target *t)
1436 {
1437         struct se_portal_group *se_tpg;
1438         struct vhost_scsi_tport *tv_tport;
1439         struct vhost_scsi_tpg *tpg;
1440         struct vhost_virtqueue *vq;
1441         bool match = false;
1442         int index, ret, i;
1443         u8 target;
1444
1445         mutex_lock(&vhost_scsi_mutex);
1446         mutex_lock(&vs->dev.mutex);
1447         /* Verify that ring has been setup correctly. */
1448         for (index = 0; index < vs->dev.nvqs; ++index) {
1449                 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
1450                         ret = -EFAULT;
1451                         goto err_dev;
1452                 }
1453         }
1454
1455         if (!vs->vs_tpg) {
1456                 ret = 0;
1457                 goto err_dev;
1458         }
1459
1460         for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
1461                 target = i;
1462                 tpg = vs->vs_tpg[target];
1463                 if (!tpg)
1464                         continue;
1465
1466                 mutex_lock(&tpg->tv_tpg_mutex);
1467                 tv_tport = tpg->tport;
1468                 if (!tv_tport) {
1469                         ret = -ENODEV;
1470                         goto err_tpg;
1471                 }
1472
1473                 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
1474                         pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
1475                                 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
1476                                 tv_tport->tport_name, tpg->tport_tpgt,
1477                                 t->vhost_wwpn, t->vhost_tpgt);
1478                         ret = -EINVAL;
1479                         goto err_tpg;
1480                 }
1481                 tpg->tv_tpg_vhost_count--;
1482                 tpg->vhost_scsi = NULL;
1483                 vs->vs_tpg[target] = NULL;
1484                 match = true;
1485                 mutex_unlock(&tpg->tv_tpg_mutex);
1486                 /*
1487                  * Release se_tpg->tpg_group.cg_item configfs dependency now
1488                  * to allow vhost-scsi WWPN se_tpg->tpg_group shutdown to occur.
1489                  */
1490                 se_tpg = &tpg->se_tpg;
1491                 target_undepend_item(&se_tpg->tpg_group.cg_item);
1492         }
1493         if (match) {
1494                 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1495                         vq = &vs->vqs[i].vq;
1496                         mutex_lock(&vq->mutex);
1497                         vq->private_data = NULL;
1498                         mutex_unlock(&vq->mutex);
1499                 }
1500         }
1501         /*
1502          * Act as synchronize_rcu to make sure access to
1503          * old vs->vs_tpg is finished.
1504          */
1505         vhost_scsi_flush(vs);
1506         kfree(vs->vs_tpg);
1507         vs->vs_tpg = NULL;
1508         WARN_ON(vs->vs_events_nr);
1509         mutex_unlock(&vs->dev.mutex);
1510         mutex_unlock(&vhost_scsi_mutex);
1511         return 0;
1512
1513 err_tpg:
1514         mutex_unlock(&tpg->tv_tpg_mutex);
1515 err_dev:
1516         mutex_unlock(&vs->dev.mutex);
1517         mutex_unlock(&vhost_scsi_mutex);
1518         return ret;
1519 }
1520
1521 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
1522 {
1523         struct vhost_virtqueue *vq;
1524         int i;
1525
1526         if (features & ~VHOST_SCSI_FEATURES)
1527                 return -EOPNOTSUPP;
1528
1529         mutex_lock(&vs->dev.mutex);
1530         if ((features & (1 << VHOST_F_LOG_ALL)) &&
1531             !vhost_log_access_ok(&vs->dev)) {
1532                 mutex_unlock(&vs->dev.mutex);
1533                 return -EFAULT;
1534         }
1535
1536         for (i = 0; i < VHOST_SCSI_MAX_VQ; i++) {
1537                 vq = &vs->vqs[i].vq;
1538                 mutex_lock(&vq->mutex);
1539                 vq->acked_features = features;
1540                 mutex_unlock(&vq->mutex);
1541         }
1542         mutex_unlock(&vs->dev.mutex);
1543         return 0;
1544 }
1545
1546 static int vhost_scsi_open(struct inode *inode, struct file *f)
1547 {
1548         struct vhost_scsi *vs;
1549         struct vhost_virtqueue **vqs;
1550         int r = -ENOMEM, i;
1551
1552         vs = kzalloc(sizeof(*vs), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
1553         if (!vs) {
1554                 vs = vzalloc(sizeof(*vs));
1555                 if (!vs)
1556                         goto err_vs;
1557         }
1558
1559         vqs = kmalloc(VHOST_SCSI_MAX_VQ * sizeof(*vqs), GFP_KERNEL);
1560         if (!vqs)
1561                 goto err_vqs;
1562
1563         vhost_work_init(&vs->vs_completion_work, vhost_scsi_complete_cmd_work);
1564         vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work);
1565
1566         vs->vs_events_nr = 0;
1567         vs->vs_events_missed = false;
1568
1569         vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
1570         vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1571         vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
1572         vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
1573         for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++) {
1574                 vqs[i] = &vs->vqs[i].vq;
1575                 vs->vqs[i].vq.handle_kick = vhost_scsi_handle_kick;
1576         }
1577         vhost_dev_init(&vs->dev, vqs, VHOST_SCSI_MAX_VQ);
1578
1579         vhost_scsi_init_inflight(vs, NULL);
1580
1581         f->private_data = vs;
1582         return 0;
1583
1584 err_vqs:
1585         kvfree(vs);
1586 err_vs:
1587         return r;
1588 }
1589
1590 static int vhost_scsi_release(struct inode *inode, struct file *f)
1591 {
1592         struct vhost_scsi *vs = f->private_data;
1593         struct vhost_scsi_target t;
1594
1595         mutex_lock(&vs->dev.mutex);
1596         memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
1597         mutex_unlock(&vs->dev.mutex);
1598         vhost_scsi_clear_endpoint(vs, &t);
1599         vhost_dev_stop(&vs->dev);
1600         vhost_dev_cleanup(&vs->dev, false);
1601         /* Jobs can re-queue themselves in evt kick handler. Do extra flush. */
1602         vhost_scsi_flush(vs);
1603         kfree(vs->dev.vqs);
1604         kvfree(vs);
1605         return 0;
1606 }
1607
1608 static long
1609 vhost_scsi_ioctl(struct file *f,
1610                  unsigned int ioctl,
1611                  unsigned long arg)
1612 {
1613         struct vhost_scsi *vs = f->private_data;
1614         struct vhost_scsi_target backend;
1615         void __user *argp = (void __user *)arg;
1616         u64 __user *featurep = argp;
1617         u32 __user *eventsp = argp;
1618         u32 events_missed;
1619         u64 features;
1620         int r, abi_version = VHOST_SCSI_ABI_VERSION;
1621         struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1622
1623         switch (ioctl) {
1624         case VHOST_SCSI_SET_ENDPOINT:
1625                 if (copy_from_user(&backend, argp, sizeof backend))
1626                         return -EFAULT;
1627                 if (backend.reserved != 0)
1628                         return -EOPNOTSUPP;
1629
1630                 return vhost_scsi_set_endpoint(vs, &backend);
1631         case VHOST_SCSI_CLEAR_ENDPOINT:
1632                 if (copy_from_user(&backend, argp, sizeof backend))
1633                         return -EFAULT;
1634                 if (backend.reserved != 0)
1635                         return -EOPNOTSUPP;
1636
1637                 return vhost_scsi_clear_endpoint(vs, &backend);
1638         case VHOST_SCSI_GET_ABI_VERSION:
1639                 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1640                         return -EFAULT;
1641                 return 0;
1642         case VHOST_SCSI_SET_EVENTS_MISSED:
1643                 if (get_user(events_missed, eventsp))
1644                         return -EFAULT;
1645                 mutex_lock(&vq->mutex);
1646                 vs->vs_events_missed = events_missed;
1647                 mutex_unlock(&vq->mutex);
1648                 return 0;
1649         case VHOST_SCSI_GET_EVENTS_MISSED:
1650                 mutex_lock(&vq->mutex);
1651                 events_missed = vs->vs_events_missed;
1652                 mutex_unlock(&vq->mutex);
1653                 if (put_user(events_missed, eventsp))
1654                         return -EFAULT;
1655                 return 0;
1656         case VHOST_GET_FEATURES:
1657                 features = VHOST_SCSI_FEATURES;
1658                 if (copy_to_user(featurep, &features, sizeof features))
1659                         return -EFAULT;
1660                 return 0;
1661         case VHOST_SET_FEATURES:
1662                 if (copy_from_user(&features, featurep, sizeof features))
1663                         return -EFAULT;
1664                 return vhost_scsi_set_features(vs, features);
1665         default:
1666                 mutex_lock(&vs->dev.mutex);
1667                 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1668                 /* TODO: flush backend after dev ioctl. */
1669                 if (r == -ENOIOCTLCMD)
1670                         r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1671                 mutex_unlock(&vs->dev.mutex);
1672                 return r;
1673         }
1674 }
1675
1676 #ifdef CONFIG_COMPAT
1677 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1678                                 unsigned long arg)
1679 {
1680         return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1681 }
1682 #endif
1683
1684 static const struct file_operations vhost_scsi_fops = {
1685         .owner          = THIS_MODULE,
1686         .release        = vhost_scsi_release,
1687         .unlocked_ioctl = vhost_scsi_ioctl,
1688 #ifdef CONFIG_COMPAT
1689         .compat_ioctl   = vhost_scsi_compat_ioctl,
1690 #endif
1691         .open           = vhost_scsi_open,
1692         .llseek         = noop_llseek,
1693 };
1694
1695 static struct miscdevice vhost_scsi_misc = {
1696         MISC_DYNAMIC_MINOR,
1697         "vhost-scsi",
1698         &vhost_scsi_fops,
1699 };
1700
1701 static int __init vhost_scsi_register(void)
1702 {
1703         return misc_register(&vhost_scsi_misc);
1704 }
1705
1706 static int vhost_scsi_deregister(void)
1707 {
1708         return misc_deregister(&vhost_scsi_misc);
1709 }
1710
1711 static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport)
1712 {
1713         switch (tport->tport_proto_id) {
1714         case SCSI_PROTOCOL_SAS:
1715                 return "SAS";
1716         case SCSI_PROTOCOL_FCP:
1717                 return "FCP";
1718         case SCSI_PROTOCOL_ISCSI:
1719                 return "iSCSI";
1720         default:
1721                 break;
1722         }
1723
1724         return "Unknown";
1725 }
1726
1727 static void
1728 vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg,
1729                   struct se_lun *lun, bool plug)
1730 {
1731
1732         struct vhost_scsi *vs = tpg->vhost_scsi;
1733         struct vhost_virtqueue *vq;
1734         u32 reason;
1735
1736         if (!vs)
1737                 return;
1738
1739         mutex_lock(&vs->dev.mutex);
1740
1741         if (plug)
1742                 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
1743         else
1744                 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
1745
1746         vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1747         mutex_lock(&vq->mutex);
1748         if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
1749                 vhost_scsi_send_evt(vs, tpg, lun,
1750                                    VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
1751         mutex_unlock(&vq->mutex);
1752         mutex_unlock(&vs->dev.mutex);
1753 }
1754
1755 static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
1756 {
1757         vhost_scsi_do_plug(tpg, lun, true);
1758 }
1759
1760 static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
1761 {
1762         vhost_scsi_do_plug(tpg, lun, false);
1763 }
1764
1765 static int vhost_scsi_port_link(struct se_portal_group *se_tpg,
1766                                struct se_lun *lun)
1767 {
1768         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1769                                 struct vhost_scsi_tpg, se_tpg);
1770
1771         mutex_lock(&vhost_scsi_mutex);
1772
1773         mutex_lock(&tpg->tv_tpg_mutex);
1774         tpg->tv_tpg_port_count++;
1775         mutex_unlock(&tpg->tv_tpg_mutex);
1776
1777         vhost_scsi_hotplug(tpg, lun);
1778
1779         mutex_unlock(&vhost_scsi_mutex);
1780
1781         return 0;
1782 }
1783
1784 static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg,
1785                                   struct se_lun *lun)
1786 {
1787         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1788                                 struct vhost_scsi_tpg, se_tpg);
1789
1790         mutex_lock(&vhost_scsi_mutex);
1791
1792         mutex_lock(&tpg->tv_tpg_mutex);
1793         tpg->tv_tpg_port_count--;
1794         mutex_unlock(&tpg->tv_tpg_mutex);
1795
1796         vhost_scsi_hotunplug(tpg, lun);
1797
1798         mutex_unlock(&vhost_scsi_mutex);
1799 }
1800
1801 static struct se_node_acl *
1802 vhost_scsi_make_nodeacl(struct se_portal_group *se_tpg,
1803                        struct config_group *group,
1804                        const char *name)
1805 {
1806         struct se_node_acl *se_nacl, *se_nacl_new;
1807         u32 nexus_depth;
1808
1809         /* vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
1810                 return ERR_PTR(-EINVAL); */
1811         se_nacl_new = vhost_scsi_alloc_fabric_acl(se_tpg);
1812         if (!se_nacl_new)
1813                 return ERR_PTR(-ENOMEM);
1814
1815         nexus_depth = 1;
1816         /*
1817          * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1818          * when converting a NodeACL from demo mode -> explict
1819          */
1820         se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1821                                 name, nexus_depth);
1822         if (IS_ERR(se_nacl)) {
1823                 vhost_scsi_release_fabric_acl(se_tpg, se_nacl_new);
1824                 return se_nacl;
1825         }
1826
1827         return se_nacl;
1828 }
1829
1830 static void vhost_scsi_drop_nodeacl(struct se_node_acl *se_acl)
1831 {
1832         core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1833         kfree(se_acl);
1834 }
1835
1836 static void vhost_scsi_free_cmd_map_res(struct vhost_scsi_nexus *nexus,
1837                                        struct se_session *se_sess)
1838 {
1839         struct vhost_scsi_cmd *tv_cmd;
1840         unsigned int i;
1841
1842         if (!se_sess->sess_cmd_map)
1843                 return;
1844
1845         for (i = 0; i < VHOST_SCSI_DEFAULT_TAGS; i++) {
1846                 tv_cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[i];
1847
1848                 kfree(tv_cmd->tvc_sgl);
1849                 kfree(tv_cmd->tvc_prot_sgl);
1850                 kfree(tv_cmd->tvc_upages);
1851         }
1852 }
1853
1854 static ssize_t vhost_scsi_tpg_attrib_store_fabric_prot_type(
1855         struct se_portal_group *se_tpg,
1856         const char *page,
1857         size_t count)
1858 {
1859         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1860                                 struct vhost_scsi_tpg, se_tpg);
1861         unsigned long val;
1862         int ret = kstrtoul(page, 0, &val);
1863
1864         if (ret) {
1865                 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
1866                 return ret;
1867         }
1868         if (val != 0 && val != 1 && val != 3) {
1869                 pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val);
1870                 return -EINVAL;
1871         }
1872         tpg->tv_fabric_prot_type = val;
1873
1874         return count;
1875 }
1876
1877 static ssize_t vhost_scsi_tpg_attrib_show_fabric_prot_type(
1878         struct se_portal_group *se_tpg,
1879         char *page)
1880 {
1881         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
1882                                 struct vhost_scsi_tpg, se_tpg);
1883
1884         return sprintf(page, "%d\n", tpg->tv_fabric_prot_type);
1885 }
1886 TF_TPG_ATTRIB_ATTR(vhost_scsi, fabric_prot_type, S_IRUGO | S_IWUSR);
1887
1888 static struct configfs_attribute *vhost_scsi_tpg_attrib_attrs[] = {
1889         &vhost_scsi_tpg_attrib_fabric_prot_type.attr,
1890         NULL,
1891 };
1892
1893 static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg,
1894                                 const char *name)
1895 {
1896         struct se_portal_group *se_tpg;
1897         struct se_session *se_sess;
1898         struct vhost_scsi_nexus *tv_nexus;
1899         struct vhost_scsi_cmd *tv_cmd;
1900         unsigned int i;
1901
1902         mutex_lock(&tpg->tv_tpg_mutex);
1903         if (tpg->tpg_nexus) {
1904                 mutex_unlock(&tpg->tv_tpg_mutex);
1905                 pr_debug("tpg->tpg_nexus already exists\n");
1906                 return -EEXIST;
1907         }
1908         se_tpg = &tpg->se_tpg;
1909
1910         tv_nexus = kzalloc(sizeof(struct vhost_scsi_nexus), GFP_KERNEL);
1911         if (!tv_nexus) {
1912                 mutex_unlock(&tpg->tv_tpg_mutex);
1913                 pr_err("Unable to allocate struct vhost_scsi_nexus\n");
1914                 return -ENOMEM;
1915         }
1916         /*
1917          *  Initialize the struct se_session pointer and setup tagpool
1918          *  for struct vhost_scsi_cmd descriptors
1919          */
1920         tv_nexus->tvn_se_sess = transport_init_session_tags(
1921                                         VHOST_SCSI_DEFAULT_TAGS,
1922                                         sizeof(struct vhost_scsi_cmd),
1923                                         TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
1924         if (IS_ERR(tv_nexus->tvn_se_sess)) {
1925                 mutex_unlock(&tpg->tv_tpg_mutex);
1926                 kfree(tv_nexus);
1927                 return -ENOMEM;
1928         }
1929         se_sess = tv_nexus->tvn_se_sess;
1930         for (i = 0; i < VHOST_SCSI_DEFAULT_TAGS; i++) {
1931                 tv_cmd = &((struct vhost_scsi_cmd *)se_sess->sess_cmd_map)[i];
1932
1933                 tv_cmd->tvc_sgl = kzalloc(sizeof(struct scatterlist) *
1934                                         VHOST_SCSI_PREALLOC_SGLS, GFP_KERNEL);
1935                 if (!tv_cmd->tvc_sgl) {
1936                         mutex_unlock(&tpg->tv_tpg_mutex);
1937                         pr_err("Unable to allocate tv_cmd->tvc_sgl\n");
1938                         goto out;
1939                 }
1940
1941                 tv_cmd->tvc_upages = kzalloc(sizeof(struct page *) *
1942                                         VHOST_SCSI_PREALLOC_UPAGES, GFP_KERNEL);
1943                 if (!tv_cmd->tvc_upages) {
1944                         mutex_unlock(&tpg->tv_tpg_mutex);
1945                         pr_err("Unable to allocate tv_cmd->tvc_upages\n");
1946                         goto out;
1947                 }
1948
1949                 tv_cmd->tvc_prot_sgl = kzalloc(sizeof(struct scatterlist) *
1950                                         VHOST_SCSI_PREALLOC_PROT_SGLS, GFP_KERNEL);
1951                 if (!tv_cmd->tvc_prot_sgl) {
1952                         mutex_unlock(&tpg->tv_tpg_mutex);
1953                         pr_err("Unable to allocate tv_cmd->tvc_prot_sgl\n");
1954                         goto out;
1955                 }
1956         }
1957         /*
1958          * Since we are running in 'demo mode' this call with generate a
1959          * struct se_node_acl for the vhost_scsi struct se_portal_group with
1960          * the SCSI Initiator port name of the passed configfs group 'name'.
1961          */
1962         tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1963                                 se_tpg, (unsigned char *)name);
1964         if (!tv_nexus->tvn_se_sess->se_node_acl) {
1965                 mutex_unlock(&tpg->tv_tpg_mutex);
1966                 pr_debug("core_tpg_check_initiator_node_acl() failed"
1967                                 " for %s\n", name);
1968                 goto out;
1969         }
1970         /*
1971          * Now register the TCM vhost virtual I_T Nexus as active.
1972          */
1973         transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1974                         tv_nexus->tvn_se_sess, tv_nexus);
1975         tpg->tpg_nexus = tv_nexus;
1976
1977         mutex_unlock(&tpg->tv_tpg_mutex);
1978         return 0;
1979
1980 out:
1981         vhost_scsi_free_cmd_map_res(tv_nexus, se_sess);
1982         transport_free_session(se_sess);
1983         kfree(tv_nexus);
1984         return -ENOMEM;
1985 }
1986
1987 static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg)
1988 {
1989         struct se_session *se_sess;
1990         struct vhost_scsi_nexus *tv_nexus;
1991
1992         mutex_lock(&tpg->tv_tpg_mutex);
1993         tv_nexus = tpg->tpg_nexus;
1994         if (!tv_nexus) {
1995                 mutex_unlock(&tpg->tv_tpg_mutex);
1996                 return -ENODEV;
1997         }
1998
1999         se_sess = tv_nexus->tvn_se_sess;
2000         if (!se_sess) {
2001                 mutex_unlock(&tpg->tv_tpg_mutex);
2002                 return -ENODEV;
2003         }
2004
2005         if (tpg->tv_tpg_port_count != 0) {
2006                 mutex_unlock(&tpg->tv_tpg_mutex);
2007                 pr_err("Unable to remove TCM_vhost I_T Nexus with"
2008                         " active TPG port count: %d\n",
2009                         tpg->tv_tpg_port_count);
2010                 return -EBUSY;
2011         }
2012
2013         if (tpg->tv_tpg_vhost_count != 0) {
2014                 mutex_unlock(&tpg->tv_tpg_mutex);
2015                 pr_err("Unable to remove TCM_vhost I_T Nexus with"
2016                         " active TPG vhost count: %d\n",
2017                         tpg->tv_tpg_vhost_count);
2018                 return -EBUSY;
2019         }
2020
2021         pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
2022                 " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport),
2023                 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
2024
2025         vhost_scsi_free_cmd_map_res(tv_nexus, se_sess);
2026         /*
2027          * Release the SCSI I_T Nexus to the emulated vhost Target Port
2028          */
2029         transport_deregister_session(tv_nexus->tvn_se_sess);
2030         tpg->tpg_nexus = NULL;
2031         mutex_unlock(&tpg->tv_tpg_mutex);
2032
2033         kfree(tv_nexus);
2034         return 0;
2035 }
2036
2037 static ssize_t vhost_scsi_tpg_show_nexus(struct se_portal_group *se_tpg,
2038                                         char *page)
2039 {
2040         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2041                                 struct vhost_scsi_tpg, se_tpg);
2042         struct vhost_scsi_nexus *tv_nexus;
2043         ssize_t ret;
2044
2045         mutex_lock(&tpg->tv_tpg_mutex);
2046         tv_nexus = tpg->tpg_nexus;
2047         if (!tv_nexus) {
2048                 mutex_unlock(&tpg->tv_tpg_mutex);
2049                 return -ENODEV;
2050         }
2051         ret = snprintf(page, PAGE_SIZE, "%s\n",
2052                         tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
2053         mutex_unlock(&tpg->tv_tpg_mutex);
2054
2055         return ret;
2056 }
2057
2058 static ssize_t vhost_scsi_tpg_store_nexus(struct se_portal_group *se_tpg,
2059                                          const char *page,
2060                                          size_t count)
2061 {
2062         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2063                                 struct vhost_scsi_tpg, se_tpg);
2064         struct vhost_scsi_tport *tport_wwn = tpg->tport;
2065         unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr;
2066         int ret;
2067         /*
2068          * Shutdown the active I_T nexus if 'NULL' is passed..
2069          */
2070         if (!strncmp(page, "NULL", 4)) {
2071                 ret = vhost_scsi_drop_nexus(tpg);
2072                 return (!ret) ? count : ret;
2073         }
2074         /*
2075          * Otherwise make sure the passed virtual Initiator port WWN matches
2076          * the fabric protocol_id set in vhost_scsi_make_tport(), and call
2077          * vhost_scsi_make_nexus().
2078          */
2079         if (strlen(page) >= VHOST_SCSI_NAMELEN) {
2080                 pr_err("Emulated NAA Sas Address: %s, exceeds"
2081                                 " max: %d\n", page, VHOST_SCSI_NAMELEN);
2082                 return -EINVAL;
2083         }
2084         snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page);
2085
2086         ptr = strstr(i_port, "naa.");
2087         if (ptr) {
2088                 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
2089                         pr_err("Passed SAS Initiator Port %s does not"
2090                                 " match target port protoid: %s\n", i_port,
2091                                 vhost_scsi_dump_proto_id(tport_wwn));
2092                         return -EINVAL;
2093                 }
2094                 port_ptr = &i_port[0];
2095                 goto check_newline;
2096         }
2097         ptr = strstr(i_port, "fc.");
2098         if (ptr) {
2099                 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
2100                         pr_err("Passed FCP Initiator Port %s does not"
2101                                 " match target port protoid: %s\n", i_port,
2102                                 vhost_scsi_dump_proto_id(tport_wwn));
2103                         return -EINVAL;
2104                 }
2105                 port_ptr = &i_port[3]; /* Skip over "fc." */
2106                 goto check_newline;
2107         }
2108         ptr = strstr(i_port, "iqn.");
2109         if (ptr) {
2110                 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
2111                         pr_err("Passed iSCSI Initiator Port %s does not"
2112                                 " match target port protoid: %s\n", i_port,
2113                                 vhost_scsi_dump_proto_id(tport_wwn));
2114                         return -EINVAL;
2115                 }
2116                 port_ptr = &i_port[0];
2117                 goto check_newline;
2118         }
2119         pr_err("Unable to locate prefix for emulated Initiator Port:"
2120                         " %s\n", i_port);
2121         return -EINVAL;
2122         /*
2123          * Clear any trailing newline for the NAA WWN
2124          */
2125 check_newline:
2126         if (i_port[strlen(i_port)-1] == '\n')
2127                 i_port[strlen(i_port)-1] = '\0';
2128
2129         ret = vhost_scsi_make_nexus(tpg, port_ptr);
2130         if (ret < 0)
2131                 return ret;
2132
2133         return count;
2134 }
2135
2136 TF_TPG_BASE_ATTR(vhost_scsi, nexus, S_IRUGO | S_IWUSR);
2137
2138 static struct configfs_attribute *vhost_scsi_tpg_attrs[] = {
2139         &vhost_scsi_tpg_nexus.attr,
2140         NULL,
2141 };
2142
2143 static struct se_portal_group *
2144 vhost_scsi_make_tpg(struct se_wwn *wwn,
2145                    struct config_group *group,
2146                    const char *name)
2147 {
2148         struct vhost_scsi_tport *tport = container_of(wwn,
2149                         struct vhost_scsi_tport, tport_wwn);
2150
2151         struct vhost_scsi_tpg *tpg;
2152         u16 tpgt;
2153         int ret;
2154
2155         if (strstr(name, "tpgt_") != name)
2156                 return ERR_PTR(-EINVAL);
2157         if (kstrtou16(name + 5, 10, &tpgt) || tpgt >= VHOST_SCSI_MAX_TARGET)
2158                 return ERR_PTR(-EINVAL);
2159
2160         tpg = kzalloc(sizeof(struct vhost_scsi_tpg), GFP_KERNEL);
2161         if (!tpg) {
2162                 pr_err("Unable to allocate struct vhost_scsi_tpg");
2163                 return ERR_PTR(-ENOMEM);
2164         }
2165         mutex_init(&tpg->tv_tpg_mutex);
2166         INIT_LIST_HEAD(&tpg->tv_tpg_list);
2167         tpg->tport = tport;
2168         tpg->tport_tpgt = tpgt;
2169
2170         ret = core_tpg_register(&vhost_scsi_ops, wwn,
2171                                 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
2172         if (ret < 0) {
2173                 kfree(tpg);
2174                 return NULL;
2175         }
2176         mutex_lock(&vhost_scsi_mutex);
2177         list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list);
2178         mutex_unlock(&vhost_scsi_mutex);
2179
2180         return &tpg->se_tpg;
2181 }
2182
2183 static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg)
2184 {
2185         struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2186                                 struct vhost_scsi_tpg, se_tpg);
2187
2188         mutex_lock(&vhost_scsi_mutex);
2189         list_del(&tpg->tv_tpg_list);
2190         mutex_unlock(&vhost_scsi_mutex);
2191         /*
2192          * Release the virtual I_T Nexus for this vhost TPG
2193          */
2194         vhost_scsi_drop_nexus(tpg);
2195         /*
2196          * Deregister the se_tpg from TCM..
2197          */
2198         core_tpg_deregister(se_tpg);
2199         kfree(tpg);
2200 }
2201
2202 static struct se_wwn *
2203 vhost_scsi_make_tport(struct target_fabric_configfs *tf,
2204                      struct config_group *group,
2205                      const char *name)
2206 {
2207         struct vhost_scsi_tport *tport;
2208         char *ptr;
2209         u64 wwpn = 0;
2210         int off = 0;
2211
2212         /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
2213                 return ERR_PTR(-EINVAL); */
2214
2215         tport = kzalloc(sizeof(struct vhost_scsi_tport), GFP_KERNEL);
2216         if (!tport) {
2217                 pr_err("Unable to allocate struct vhost_scsi_tport");
2218                 return ERR_PTR(-ENOMEM);
2219         }
2220         tport->tport_wwpn = wwpn;
2221         /*
2222          * Determine the emulated Protocol Identifier and Target Port Name
2223          * based on the incoming configfs directory name.
2224          */
2225         ptr = strstr(name, "naa.");
2226         if (ptr) {
2227                 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
2228                 goto check_len;
2229         }
2230         ptr = strstr(name, "fc.");
2231         if (ptr) {
2232                 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
2233                 off = 3; /* Skip over "fc." */
2234                 goto check_len;
2235         }
2236         ptr = strstr(name, "iqn.");
2237         if (ptr) {
2238                 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
2239                 goto check_len;
2240         }
2241
2242         pr_err("Unable to locate prefix for emulated Target Port:"
2243                         " %s\n", name);
2244         kfree(tport);
2245         return ERR_PTR(-EINVAL);
2246
2247 check_len:
2248         if (strlen(name) >= VHOST_SCSI_NAMELEN) {
2249                 pr_err("Emulated %s Address: %s, exceeds"
2250                         " max: %d\n", name, vhost_scsi_dump_proto_id(tport),
2251                         VHOST_SCSI_NAMELEN);
2252                 kfree(tport);
2253                 return ERR_PTR(-EINVAL);
2254         }
2255         snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]);
2256
2257         pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
2258                 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name);
2259
2260         return &tport->tport_wwn;
2261 }
2262
2263 static void vhost_scsi_drop_tport(struct se_wwn *wwn)
2264 {
2265         struct vhost_scsi_tport *tport = container_of(wwn,
2266                                 struct vhost_scsi_tport, tport_wwn);
2267
2268         pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
2269                 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport),
2270                 tport->tport_name);
2271
2272         kfree(tport);
2273 }
2274
2275 static ssize_t
2276 vhost_scsi_wwn_show_attr_version(struct target_fabric_configfs *tf,
2277                                 char *page)
2278 {
2279         return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
2280                 "on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
2281                 utsname()->machine);
2282 }
2283
2284 TF_WWN_ATTR_RO(vhost_scsi, version);
2285
2286 static struct configfs_attribute *vhost_scsi_wwn_attrs[] = {
2287         &vhost_scsi_wwn_version.attr,
2288         NULL,
2289 };
2290
2291 static struct target_core_fabric_ops vhost_scsi_ops = {
2292         .module                         = THIS_MODULE,
2293         .name                           = "vhost",
2294         .get_fabric_name                = vhost_scsi_get_fabric_name,
2295         .get_fabric_proto_ident         = vhost_scsi_get_fabric_proto_ident,
2296         .tpg_get_wwn                    = vhost_scsi_get_fabric_wwn,
2297         .tpg_get_tag                    = vhost_scsi_get_tpgt,
2298         .tpg_get_pr_transport_id        = vhost_scsi_get_pr_transport_id,
2299         .tpg_get_pr_transport_id_len    = vhost_scsi_get_pr_transport_id_len,
2300         .tpg_parse_pr_out_transport_id  = vhost_scsi_parse_pr_out_transport_id,
2301         .tpg_check_demo_mode            = vhost_scsi_check_true,
2302         .tpg_check_demo_mode_cache      = vhost_scsi_check_true,
2303         .tpg_check_demo_mode_write_protect = vhost_scsi_check_false,
2304         .tpg_check_prod_mode_write_protect = vhost_scsi_check_false,
2305         .tpg_check_prot_fabric_only     = vhost_scsi_check_prot_fabric_only,
2306         .tpg_alloc_fabric_acl           = vhost_scsi_alloc_fabric_acl,
2307         .tpg_release_fabric_acl         = vhost_scsi_release_fabric_acl,
2308         .tpg_get_inst_index             = vhost_scsi_tpg_get_inst_index,
2309         .release_cmd                    = vhost_scsi_release_cmd,
2310         .check_stop_free                = vhost_scsi_check_stop_free,
2311         .shutdown_session               = vhost_scsi_shutdown_session,
2312         .close_session                  = vhost_scsi_close_session,
2313         .sess_get_index                 = vhost_scsi_sess_get_index,
2314         .sess_get_initiator_sid         = NULL,
2315         .write_pending                  = vhost_scsi_write_pending,
2316         .write_pending_status           = vhost_scsi_write_pending_status,
2317         .set_default_node_attributes    = vhost_scsi_set_default_node_attrs,
2318         .get_task_tag                   = vhost_scsi_get_task_tag,
2319         .get_cmd_state                  = vhost_scsi_get_cmd_state,
2320         .queue_data_in                  = vhost_scsi_queue_data_in,
2321         .queue_status                   = vhost_scsi_queue_status,
2322         .queue_tm_rsp                   = vhost_scsi_queue_tm_rsp,
2323         .aborted_task                   = vhost_scsi_aborted_task,
2324         /*
2325          * Setup callers for generic logic in target_core_fabric_configfs.c
2326          */
2327         .fabric_make_wwn                = vhost_scsi_make_tport,
2328         .fabric_drop_wwn                = vhost_scsi_drop_tport,
2329         .fabric_make_tpg                = vhost_scsi_make_tpg,
2330         .fabric_drop_tpg                = vhost_scsi_drop_tpg,
2331         .fabric_post_link               = vhost_scsi_port_link,
2332         .fabric_pre_unlink              = vhost_scsi_port_unlink,
2333         .fabric_make_np                 = NULL,
2334         .fabric_drop_np                 = NULL,
2335         .fabric_make_nodeacl            = vhost_scsi_make_nodeacl,
2336         .fabric_drop_nodeacl            = vhost_scsi_drop_nodeacl,
2337
2338         .tfc_wwn_attrs                  = vhost_scsi_wwn_attrs,
2339         .tfc_tpg_base_attrs             = vhost_scsi_tpg_attrs,
2340         .tfc_tpg_attrib_attrs           = vhost_scsi_tpg_attrib_attrs,
2341 };
2342
2343 static int __init vhost_scsi_init(void)
2344 {
2345         int ret = -ENOMEM;
2346
2347         pr_debug("TCM_VHOST fabric module %s on %s/%s"
2348                 " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
2349                 utsname()->machine);
2350
2351         /*
2352          * Use our own dedicated workqueue for submitting I/O into
2353          * target core to avoid contention within system_wq.
2354          */
2355         vhost_scsi_workqueue = alloc_workqueue("vhost_scsi", 0, 0);
2356         if (!vhost_scsi_workqueue)
2357                 goto out;
2358
2359         ret = vhost_scsi_register();
2360         if (ret < 0)
2361                 goto out_destroy_workqueue;
2362
2363         ret = target_register_template(&vhost_scsi_ops);
2364         if (ret < 0)
2365                 goto out_vhost_scsi_deregister;
2366
2367         return 0;
2368
2369 out_vhost_scsi_deregister:
2370         vhost_scsi_deregister();
2371 out_destroy_workqueue:
2372         destroy_workqueue(vhost_scsi_workqueue);
2373 out:
2374         return ret;
2375 };
2376
2377 static void vhost_scsi_exit(void)
2378 {
2379         target_unregister_template(&vhost_scsi_ops);
2380         vhost_scsi_deregister();
2381         destroy_workqueue(vhost_scsi_workqueue);
2382 };
2383
2384 MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2385 MODULE_ALIAS("tcm_vhost");
2386 MODULE_LICENSE("GPL");
2387 module_init(vhost_scsi_init);
2388 module_exit(vhost_scsi_exit);