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