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