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