Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-block.git] / drivers / vhost / scsi.c
CommitLineData
23b228cb 1// SPDX-License-Identifier: GPL-2.0+
057cbf49
NB
2/*******************************************************************************
3 * Vhost kernel TCM fabric driver for virtio SCSI initiators
4 *
4c76251e 5 * (C) Copyright 2010-2013 Datera, Inc.
057cbf49
NB
6 * (C) Copyright 2010-2012 IBM Corp.
7 *
4c76251e 8 * Authors: Nicholas A. Bellinger <nab@daterainc.com>
057cbf49 9 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
057cbf49
NB
10 ****************************************************************************/
11
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <generated/utsrelease.h>
15#include <linux/utsname.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/kthread.h>
19#include <linux/types.h>
20#include <linux/string.h>
21#include <linux/configfs.h>
22#include <linux/ctype.h>
23#include <linux/compat.h>
24#include <linux/eventfd.h>
057cbf49 25#include <linux/fs.h>
5538d294 26#include <linux/vmalloc.h>
057cbf49 27#include <linux/miscdevice.h>
5ced58bf
MC
28#include <linux/blk_types.h>
29#include <linux/bio.h>
5f60d5f6 30#include <linux/unaligned.h>
ba929992
BVA
31#include <scsi/scsi_common.h>
32#include <scsi/scsi_proto.h>
057cbf49
NB
33#include <target/target_core_base.h>
34#include <target/target_core_fabric.h>
057cbf49 35#include <linux/vhost.h>
057cbf49 36#include <linux/virtio_scsi.h>
9d6064a3 37#include <linux/llist.h>
1b7f390e 38#include <linux/bitmap.h>
057cbf49 39
057cbf49 40#include "vhost.h"
5012a3a3 41
1a1ff825
NB
42#define VHOST_SCSI_VERSION "v0.1"
43#define VHOST_SCSI_NAMELEN 256
44#define VHOST_SCSI_MAX_CDB_SIZE 32
1a1ff825
NB
45#define VHOST_SCSI_PREALLOC_SGLS 2048
46#define VHOST_SCSI_PREALLOC_UPAGES 2048
864d39df 47#define VHOST_SCSI_PREALLOC_PROT_SGLS 2048
9d896067
MC
48/*
49 * For the legacy descriptor case we allocate an iov per byte in the
50 * virtio_scsi_cmd_resp struct.
51 */
52#define VHOST_SCSI_MAX_RESP_IOVS sizeof(struct virtio_scsi_cmd_resp)
5012a3a3 53
bca939d5
MC
54static unsigned int vhost_scsi_inline_sg_cnt = VHOST_SCSI_PREALLOC_SGLS;
55
56#ifdef CONFIG_ARCH_NO_SG_CHAIN
57static int vhost_scsi_set_inline_sg_cnt(const char *buf,
58 const struct kernel_param *kp)
59{
60 pr_err("Setting inline_sg_cnt is not supported.\n");
61 return -EOPNOTSUPP;
62}
63#else
64static int vhost_scsi_set_inline_sg_cnt(const char *buf,
65 const struct kernel_param *kp)
66{
67 unsigned int cnt;
68 int ret;
69
70 ret = kstrtouint(buf, 10, &cnt);
71 if (ret)
72 return ret;
73
74 if (ret > VHOST_SCSI_PREALLOC_SGLS) {
75 pr_err("Max inline_sg_cnt is %u\n", VHOST_SCSI_PREALLOC_SGLS);
76 return -EINVAL;
77 }
78
79 vhost_scsi_inline_sg_cnt = cnt;
80 return 0;
81}
82#endif
83
84static int vhost_scsi_get_inline_sg_cnt(char *buf,
85 const struct kernel_param *kp)
86{
87 return sprintf(buf, "%u\n", vhost_scsi_inline_sg_cnt);
88}
89
90static const struct kernel_param_ops vhost_scsi_inline_sg_cnt_op = {
91 .get = vhost_scsi_get_inline_sg_cnt,
92 .set = vhost_scsi_set_inline_sg_cnt,
93};
94
95module_param_cb(inline_sg_cnt, &vhost_scsi_inline_sg_cnt_op, NULL, 0644);
96MODULE_PARM_DESC(inline_sg_cnt, "Set the number of scatterlist entries to pre-allocate. The default is 2048.");
97
e82b9b07
JW
98/* Max number of requests before requeueing the job.
99 * Using this limit prevents one virtqueue from starving others with
100 * request.
101 */
102#define VHOST_SCSI_WEIGHT 256
103
5012a3a3
MT
104struct vhost_scsi_inflight {
105 /* Wait for the flush operation to finish */
106 struct completion comp;
107 /* Refcount for the inflight reqs */
108 struct kref kref;
109};
110
1a1ff825 111struct vhost_scsi_cmd {
5012a3a3
MT
112 /* Descriptor from vhost_get_vq_desc() for virt_queue segment */
113 int tvc_vq_desc;
5012a3a3
MT
114 /* The number of scatterlists associated with this cmd */
115 u32 tvc_sgl_count;
e31885dd 116 u32 tvc_prot_sgl_count;
5ced58bf 117 u32 copied_iov:1;
fd479765
MC
118 const void *read_iov;
119 struct iov_iter *read_iter;
bca939d5
MC
120 struct scatterlist *sgl;
121 struct sg_table table;
122 struct scatterlist *prot_sgl;
123 struct sg_table prot_table;
9d896067
MC
124 /* Fast path response header iovec used when only one vec is needed */
125 struct iovec tvc_resp_iov;
126 /* Number of iovs for response */
127 unsigned int tvc_resp_iovs_cnt;
128 /* Pointer to response header iovecs if more than one is needed */
129 struct iovec *tvc_resp_iovs;
5012a3a3
MT
130 /* Pointer to vhost_virtqueue for the cmd */
131 struct vhost_virtqueue *tvc_vq;
5012a3a3
MT
132 /* The TCM I/O descriptor that is accessed via container_of() */
133 struct se_cmd tvc_se_cmd;
5012a3a3
MT
134 /* Sense buffer that will be mapped into outgoing status */
135 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER];
c2c5c259
DZ
136 /*
137 * Dirty write descriptors of this command.
138 */
139 struct vhost_log *tvc_log;
140 unsigned int tvc_log_num;
5012a3a3
MT
141 /* Completed commands list, serviced from vhost worker thread */
142 struct llist_node tvc_completion_list;
143 /* Used to track inflight cmd */
144 struct vhost_scsi_inflight *inflight;
145};
146
1a1ff825 147struct vhost_scsi_nexus {
5012a3a3
MT
148 /* Pointer to TCM session for I_T Nexus */
149 struct se_session *tvn_se_sess;
150};
151
1a1ff825 152struct vhost_scsi_tpg {
5012a3a3
MT
153 /* Vhost port target portal group tag for TCM */
154 u16 tport_tpgt;
155 /* Used to track number of TPG Port/Lun Links wrt to explict I_T Nexus shutdown */
156 int tv_tpg_port_count;
157 /* Used for vhost_scsi device reference to tpg_nexus, protected by tv_tpg_mutex */
158 int tv_tpg_vhost_count;
b1d75fe5
NB
159 /* Used for enabling T10-PI with legacy devices */
160 int tv_fabric_prot_type;
1a1ff825 161 /* list for vhost_scsi_list */
5012a3a3
MT
162 struct list_head tv_tpg_list;
163 /* Used to protect access for tpg_nexus */
164 struct mutex tv_tpg_mutex;
165 /* Pointer to the TCM VHost I_T Nexus for this TPG endpoint */
1a1ff825
NB
166 struct vhost_scsi_nexus *tpg_nexus;
167 /* Pointer back to vhost_scsi_tport */
168 struct vhost_scsi_tport *tport;
169 /* Returned by vhost_scsi_make_tpg() */
5012a3a3
MT
170 struct se_portal_group se_tpg;
171 /* Pointer back to vhost_scsi, protected by tv_tpg_mutex */
172 struct vhost_scsi *vhost_scsi;
173};
174
1a1ff825 175struct vhost_scsi_tport {
5012a3a3
MT
176 /* SCSI protocol the tport is providing */
177 u8 tport_proto_id;
178 /* Binary World Wide unique Port Name for Vhost Target port */
179 u64 tport_wwpn;
180 /* ASCII formatted WWPN for Vhost Target port */
1a1ff825
NB
181 char tport_name[VHOST_SCSI_NAMELEN];
182 /* Returned by vhost_scsi_make_tport() */
5012a3a3
MT
183 struct se_wwn tport_wwn;
184};
185
1a1ff825 186struct vhost_scsi_evt {
5012a3a3
MT
187 /* event to be sent to guest */
188 struct virtio_scsi_event event;
189 /* event list, serviced from vhost worker thread */
190 struct llist_node list;
191};
057cbf49 192
101998f6
NB
193enum {
194 VHOST_SCSI_VQ_CTL = 0,
195 VHOST_SCSI_VQ_EVT = 1,
196 VHOST_SCSI_VQ_IO = 2,
197};
198
e72fd72e 199/* Note: can't set VIRTIO_F_VERSION_1 yet, since that implies ANY_LAYOUT. */
5dade710 200enum {
95e7c434 201 VHOST_SCSI_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_SCSI_F_HOTPLUG) |
4e9fa50c 202 (1ULL << VIRTIO_SCSI_F_T10_PI)
5dade710
NB
203};
204
1b7f390e 205#define VHOST_SCSI_MAX_TARGET 256
f49c2226 206#define VHOST_SCSI_MAX_IO_VQ 1024
a6c9af87 207#define VHOST_SCSI_MAX_EVENT 128
67e18cf9 208
f49c2226
MC
209static unsigned vhost_scsi_max_io_vqs = 128;
210module_param_named(max_io_vqs, vhost_scsi_max_io_vqs, uint, 0644);
211MODULE_PARM_DESC(max_io_vqs, "Set the max number of IO virtqueues a vhost scsi device can support. The default is 128. The max is 1024.");
212
3ab2e420
AH
213struct vhost_scsi_virtqueue {
214 struct vhost_virtqueue vq;
48ae70dd 215 struct vhost_scsi *vs;
3dfbff32
MT
216 /*
217 * Reference counting for inflight reqs, used for flush operation. At
218 * each time, one reference tracks new commands submitted, while we
219 * wait for another one to reach 0.
220 */
f2f0173d 221 struct vhost_scsi_inflight inflights[2];
3dfbff32
MT
222 /*
223 * Indicate current inflight in use, protected by vq->mutex.
224 * Writers must also take dev mutex and flush under it.
225 */
f2f0173d 226 int inflight_idx;
25b98b64
MC
227 struct vhost_scsi_cmd *scsi_cmds;
228 struct sbitmap scsi_tags;
229 int max_cmds;
4c1f3a7d 230 struct page **upages;
48ae70dd
MC
231
232 struct vhost_work completion_work;
233 struct llist_head completion_list;
3ab2e420
AH
234};
235
057cbf49 236struct vhost_scsi {
67e18cf9 237 /* Protected by vhost_scsi->dev.mutex */
1a1ff825 238 struct vhost_scsi_tpg **vs_tpg;
67e18cf9 239 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
67e18cf9 240
057cbf49 241 struct vhost_dev dev;
f49c2226 242 struct vhost_scsi_virtqueue *vqs;
f49c2226 243 struct vhost_scsi_inflight **old_inflight;
057cbf49 244
a6c9af87
AH
245 struct vhost_work vs_event_work; /* evt injection work item */
246 struct llist_head vs_event_list; /* evt injection queue */
247
248 bool vs_events_missed; /* any missed events, protected by vq->mutex */
249 int vs_events_nr; /* num of pending events, protected by vq->mutex */
bca939d5
MC
250
251 unsigned int inline_sg_cnt;
057cbf49
NB
252};
253
efd838fe
MC
254struct vhost_scsi_tmf {
255 struct vhost_work vwork;
59b701b9 256 struct work_struct flush_work;
efd838fe
MC
257 struct vhost_scsi *vhost;
258 struct vhost_scsi_virtqueue *svq;
efd838fe
MC
259
260 struct se_cmd se_cmd;
b4fffc17 261 u8 scsi_resp;
efd838fe
MC
262 struct vhost_scsi_inflight *inflight;
263 struct iovec resp_iov;
264 int in_iovs;
265 int vq_desc;
a94c96a3
DZ
266
267 /*
268 * Dirty write descriptors of this command.
269 */
270 struct vhost_log *tmf_log;
271 unsigned int tmf_log_num;
efd838fe
MC
272};
273
3f8ca2e1
BM
274/*
275 * Context for processing request and control queue operations.
276 */
277struct vhost_scsi_ctx {
278 int head;
279 unsigned int out, in;
280 size_t req_size, rsp_size;
281 size_t out_size, in_size;
282 u8 *target, *lunp;
283 void *req;
284 struct iov_iter out_iter;
285};
286
bea273c7
MC
287/*
288 * Global mutex to protect vhost_scsi TPG list for vhost IOCTLs and LIO
289 * configfs management operations.
290 */
1a1ff825
NB
291static DEFINE_MUTEX(vhost_scsi_mutex);
292static LIST_HEAD(vhost_scsi_list);
057cbf49 293
1a1ff825 294static void vhost_scsi_done_inflight(struct kref *kref)
f2f0173d
AH
295{
296 struct vhost_scsi_inflight *inflight;
297
298 inflight = container_of(kref, struct vhost_scsi_inflight, kref);
299 complete(&inflight->comp);
300}
301
1a1ff825 302static void vhost_scsi_init_inflight(struct vhost_scsi *vs,
f2f0173d
AH
303 struct vhost_scsi_inflight *old_inflight[])
304{
305 struct vhost_scsi_inflight *new_inflight;
306 struct vhost_virtqueue *vq;
307 int idx, i;
308
f49c2226 309 for (i = 0; i < vs->dev.nvqs; i++) {
f2f0173d
AH
310 vq = &vs->vqs[i].vq;
311
312 mutex_lock(&vq->mutex);
313
314 /* store old infight */
315 idx = vs->vqs[i].inflight_idx;
316 if (old_inflight)
317 old_inflight[i] = &vs->vqs[i].inflights[idx];
318
319 /* setup new infight */
320 vs->vqs[i].inflight_idx = idx ^ 1;
321 new_inflight = &vs->vqs[i].inflights[idx ^ 1];
322 kref_init(&new_inflight->kref);
323 init_completion(&new_inflight->comp);
324
325 mutex_unlock(&vq->mutex);
326 }
327}
328
329static struct vhost_scsi_inflight *
1a1ff825 330vhost_scsi_get_inflight(struct vhost_virtqueue *vq)
f2f0173d
AH
331{
332 struct vhost_scsi_inflight *inflight;
333 struct vhost_scsi_virtqueue *svq;
334
335 svq = container_of(vq, struct vhost_scsi_virtqueue, vq);
336 inflight = &svq->inflights[svq->inflight_idx];
337 kref_get(&inflight->kref);
338
339 return inflight;
340}
341
1a1ff825 342static void vhost_scsi_put_inflight(struct vhost_scsi_inflight *inflight)
f2f0173d 343{
1a1ff825 344 kref_put(&inflight->kref, vhost_scsi_done_inflight);
f2f0173d
AH
345}
346
1a1ff825 347static int vhost_scsi_check_true(struct se_portal_group *se_tpg)
057cbf49
NB
348{
349 return 1;
350}
351
1a1ff825 352static char *vhost_scsi_get_fabric_wwn(struct se_portal_group *se_tpg)
057cbf49 353{
1a1ff825
NB
354 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
355 struct vhost_scsi_tpg, se_tpg);
356 struct vhost_scsi_tport *tport = tpg->tport;
057cbf49
NB
357
358 return &tport->tport_name[0];
359}
360
1a1ff825 361static u16 vhost_scsi_get_tpgt(struct se_portal_group *se_tpg)
057cbf49 362{
1a1ff825
NB
363 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
364 struct vhost_scsi_tpg, se_tpg);
057cbf49
NB
365 return tpg->tport_tpgt;
366}
367
b1d75fe5
NB
368static int vhost_scsi_check_prot_fabric_only(struct se_portal_group *se_tpg)
369{
370 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
371 struct vhost_scsi_tpg, se_tpg);
372
373 return tpg->tv_fabric_prot_type;
374}
375
c2c5c259
DZ
376static int vhost_scsi_copy_cmd_log(struct vhost_virtqueue *vq,
377 struct vhost_scsi_cmd *cmd,
378 struct vhost_log *log,
379 unsigned int log_num)
380{
381 if (!cmd->tvc_log)
382 cmd->tvc_log = kmalloc_array(vq->dev->iov_limit,
383 sizeof(*cmd->tvc_log),
384 GFP_KERNEL);
385
386 if (unlikely(!cmd->tvc_log)) {
387 vq_err(vq, "Failed to alloc tvc_log\n");
388 return -ENOMEM;
389 }
390
391 memcpy(cmd->tvc_log, log, sizeof(*cmd->tvc_log) * log_num);
392 cmd->tvc_log_num = log_num;
393
394 return 0;
395}
396
397static void vhost_scsi_log_write(struct vhost_virtqueue *vq,
398 struct vhost_log *log,
399 unsigned int log_num)
400{
401 if (likely(!vhost_has_feature(vq, VHOST_F_LOG_ALL)))
402 return;
403
404 if (likely(!log_num || !log))
405 return;
406
407 /*
408 * vhost-scsi doesn't support VIRTIO_F_ACCESS_PLATFORM.
409 * No requirement for vq->iotlb case.
410 */
411 WARN_ON_ONCE(unlikely(vq->iotlb));
412 vhost_log_write(vq, log, log_num, U64_MAX, NULL, 0);
413}
414
47a3565e 415static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd)
057cbf49 416{
1a1ff825
NB
417 struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd,
418 struct vhost_scsi_cmd, tvc_se_cmd);
25b98b64
MC
419 struct vhost_scsi_virtqueue *svq = container_of(tv_cmd->tvc_vq,
420 struct vhost_scsi_virtqueue, vq);
ddc5b5f6 421 struct vhost_scsi *vs = svq->vs;
25b98b64 422 struct vhost_scsi_inflight *inflight = tv_cmd->inflight;
bca939d5
MC
423 struct scatterlist *sg;
424 struct page *page;
e31885dd 425 int i;
084ed45b
NB
426
427 if (tv_cmd->tvc_sgl_count) {
bca939d5
MC
428 for_each_sgtable_sg(&tv_cmd->table, sg, i) {
429 page = sg_page(sg);
430 if (!page)
431 continue;
432
5ced58bf 433 if (tv_cmd->copied_iov)
bca939d5 434 __free_page(page);
5ced58bf 435 else
bca939d5 436 put_page(page);
5ced58bf 437 }
fd479765
MC
438 kfree(tv_cmd->read_iter);
439 kfree(tv_cmd->read_iov);
bca939d5 440 sg_free_table_chained(&tv_cmd->table, vs->inline_sg_cnt);
d3d665a6 441 }
e31885dd 442 if (tv_cmd->tvc_prot_sgl_count) {
bca939d5
MC
443 for_each_sgtable_sg(&tv_cmd->prot_table, sg, i) {
444 page = sg_page(sg);
445 if (page)
446 put_page(page);
447 }
448 sg_free_table_chained(&tv_cmd->prot_table, vs->inline_sg_cnt);
e31885dd 449 }
084ed45b 450
9d896067
MC
451 if (tv_cmd->tvc_resp_iovs != &tv_cmd->tvc_resp_iov)
452 kfree(tv_cmd->tvc_resp_iovs);
25b98b64
MC
453 sbitmap_clear_bit(&svq->scsi_tags, se_cmd->map_tag);
454 vhost_scsi_put_inflight(inflight);
057cbf49
NB
455}
456
efd838fe
MC
457static void vhost_scsi_release_tmf_res(struct vhost_scsi_tmf *tmf)
458{
efd838fe
MC
459 struct vhost_scsi_inflight *inflight = tmf->inflight;
460
a94c96a3
DZ
461 /*
462 * tmf->tmf_log is default NULL unless VHOST_F_LOG_ALL is set.
463 */
464 kfree(tmf->tmf_log);
4c363c81 465 kfree(tmf);
efd838fe
MC
466 vhost_scsi_put_inflight(inflight);
467}
468
1eceddee
MC
469static void vhost_scsi_drop_cmds(struct vhost_scsi_virtqueue *svq)
470{
471 struct vhost_scsi_cmd *cmd, *t;
472 struct llist_node *llnode;
473
474 llnode = llist_del_all(&svq->completion_list);
475 llist_for_each_entry_safe(cmd, t, llnode, tvc_completion_list)
476 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd);
477}
478
47a3565e
MC
479static void vhost_scsi_release_cmd(struct se_cmd *se_cmd)
480{
efd838fe
MC
481 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
482 struct vhost_scsi_tmf *tmf = container_of(se_cmd,
483 struct vhost_scsi_tmf, se_cmd);
484
59b701b9 485 schedule_work(&tmf->flush_work);
efd838fe
MC
486 } else {
487 struct vhost_scsi_cmd *cmd = container_of(se_cmd,
47a3565e 488 struct vhost_scsi_cmd, tvc_se_cmd);
48ae70dd
MC
489 struct vhost_scsi_virtqueue *svq = container_of(cmd->tvc_vq,
490 struct vhost_scsi_virtqueue, vq);
47a3565e 491
48ae70dd 492 llist_add(&cmd->tvc_completion_list, &svq->completion_list);
1eceddee
MC
493 if (!vhost_vq_work_queue(&svq->vq, &svq->completion_work))
494 vhost_scsi_drop_cmds(svq);
efd838fe 495 }
47a3565e
MC
496}
497
1a1ff825 498static int vhost_scsi_write_pending(struct se_cmd *se_cmd)
057cbf49
NB
499{
500 /* Go ahead and process the write immediately */
501 target_execute_cmd(se_cmd);
502 return 0;
503}
504
1a1ff825 505static int vhost_scsi_queue_data_in(struct se_cmd *se_cmd)
057cbf49 506{
47a3565e 507 transport_generic_free_cmd(se_cmd, 0);
057cbf49
NB
508 return 0;
509}
510
1a1ff825 511static int vhost_scsi_queue_status(struct se_cmd *se_cmd)
057cbf49 512{
47a3565e 513 transport_generic_free_cmd(se_cmd, 0);
057cbf49
NB
514 return 0;
515}
516
1a1ff825 517static void vhost_scsi_queue_tm_rsp(struct se_cmd *se_cmd)
057cbf49 518{
efd838fe
MC
519 struct vhost_scsi_tmf *tmf = container_of(se_cmd, struct vhost_scsi_tmf,
520 se_cmd);
521
b4fffc17 522 tmf->scsi_resp = se_cmd->se_tmr_req->response;
efd838fe 523 transport_generic_free_cmd(&tmf->se_cmd, 0);
057cbf49
NB
524}
525
1a1ff825 526static void vhost_scsi_aborted_task(struct se_cmd *se_cmd)
131e6abc
NB
527{
528 return;
529}
530
1a1ff825 531static void vhost_scsi_free_evt(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
a6c9af87
AH
532{
533 vs->vs_events_nr--;
534 kfree(evt);
535}
536
1a1ff825
NB
537static struct vhost_scsi_evt *
538vhost_scsi_allocate_evt(struct vhost_scsi *vs,
683bd967 539 u32 event, u32 reason)
a6c9af87 540{
3ab2e420 541 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
1a1ff825 542 struct vhost_scsi_evt *evt;
a6c9af87
AH
543
544 if (vs->vs_events_nr > VHOST_SCSI_MAX_EVENT) {
545 vs->vs_events_missed = true;
546 return NULL;
547 }
548
549 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
550 if (!evt) {
1a1ff825 551 vq_err(vq, "Failed to allocate vhost_scsi_evt\n");
a6c9af87
AH
552 vs->vs_events_missed = true;
553 return NULL;
554 }
555
e72fd72e
MT
556 evt->event.event = cpu_to_vhost32(vq, event);
557 evt->event.reason = cpu_to_vhost32(vq, reason);
a6c9af87
AH
558 vs->vs_events_nr++;
559
560 return evt;
561}
562
084ed45b
NB
563static int vhost_scsi_check_stop_free(struct se_cmd *se_cmd)
564{
afc16604 565 return target_put_sess_cmd(se_cmd);
057cbf49
NB
566}
567
683bd967 568static void
1a1ff825 569vhost_scsi_do_evt_work(struct vhost_scsi *vs, struct vhost_scsi_evt *evt)
a6c9af87 570{
3ab2e420 571 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
a6c9af87
AH
572 struct virtio_scsi_event *event = &evt->event;
573 struct virtio_scsi_event __user *eventp;
ac9dcca2
DZ
574 struct vhost_log *vq_log;
575 unsigned int log_num;
a6c9af87
AH
576 unsigned out, in;
577 int head, ret;
578
247643f8 579 if (!vhost_vq_get_backend(vq)) {
a6c9af87
AH
580 vs->vs_events_missed = true;
581 return;
582 }
583
584again:
585 vhost_disable_notify(&vs->dev, vq);
ac9dcca2
DZ
586
587 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
588 vq->log : NULL;
589
590 /*
591 * Reset 'log_num' since vhost_get_vq_desc() may reset it only
592 * after certain condition checks.
593 */
594 log_num = 0;
595
47283bef 596 head = vhost_get_vq_desc(vq, vq->iov,
a6c9af87 597 ARRAY_SIZE(vq->iov), &out, &in,
ac9dcca2 598 vq_log, &log_num);
a6c9af87
AH
599 if (head < 0) {
600 vs->vs_events_missed = true;
601 return;
602 }
603 if (head == vq->num) {
604 if (vhost_enable_notify(&vs->dev, vq))
605 goto again;
606 vs->vs_events_missed = true;
607 return;
608 }
609
610 if ((vq->iov[out].iov_len != sizeof(struct virtio_scsi_event))) {
611 vq_err(vq, "Expecting virtio_scsi_event, got %zu bytes\n",
612 vq->iov[out].iov_len);
613 vs->vs_events_missed = true;
614 return;
615 }
616
617 if (vs->vs_events_missed) {
e72fd72e 618 event->event |= cpu_to_vhost32(vq, VIRTIO_SCSI_T_EVENTS_MISSED);
a6c9af87
AH
619 vs->vs_events_missed = false;
620 }
621
622 eventp = vq->iov[out].iov_base;
623 ret = __copy_to_user(eventp, event, sizeof(*event));
624 if (!ret)
625 vhost_add_used_and_signal(&vs->dev, vq, head, 0);
626 else
1a1ff825 627 vq_err(vq, "Faulted on vhost_scsi_send_event\n");
ac9dcca2
DZ
628
629 vhost_scsi_log_write(vq, vq_log, log_num);
a6c9af87
AH
630}
631
b1b2ce58 632static void vhost_scsi_complete_events(struct vhost_scsi *vs, bool drop)
a6c9af87 633{
3ab2e420 634 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
12bdcbd5 635 struct vhost_scsi_evt *evt, *t;
a6c9af87
AH
636 struct llist_node *llnode;
637
638 mutex_lock(&vq->mutex);
639 llnode = llist_del_all(&vs->vs_event_list);
12bdcbd5 640 llist_for_each_entry_safe(evt, t, llnode, list) {
b1b2ce58
MC
641 if (!drop)
642 vhost_scsi_do_evt_work(vs, evt);
1a1ff825 643 vhost_scsi_free_evt(vs, evt);
a6c9af87
AH
644 }
645 mutex_unlock(&vq->mutex);
646}
647
b1b2ce58
MC
648static void vhost_scsi_evt_work(struct vhost_work *work)
649{
650 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
651 vs_event_work);
652 vhost_scsi_complete_events(vs, false);
653}
654
5ced58bf
MC
655static int vhost_scsi_copy_sgl_to_iov(struct vhost_scsi_cmd *cmd)
656{
fd479765 657 struct iov_iter *iter = cmd->read_iter;
bca939d5 658 struct scatterlist *sg;
5ced58bf
MC
659 struct page *page;
660 size_t len;
661 int i;
662
bca939d5
MC
663 for_each_sgtable_sg(&cmd->table, sg, i) {
664 page = sg_page(sg);
665 if (!page)
666 continue;
667
668 len = sg->length;
5ced58bf
MC
669
670 if (copy_page_to_iter(page, 0, len, iter) != len) {
671 pr_err("Could not copy data while handling misaligned cmd. Error %zu\n",
672 len);
673 return -1;
674 }
675 }
676
677 return 0;
678}
679
057cbf49
NB
680/* Fill in status and signal that we are done processing this command
681 *
682 * This is scheduled in the vhost work queue so we are called with the owner
683 * process mm and can access the vring.
684 */
685static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
686{
48ae70dd
MC
687 struct vhost_scsi_virtqueue *svq = container_of(work,
688 struct vhost_scsi_virtqueue, completion_work);
9d6064a3 689 struct virtio_scsi_cmd_resp v_rsp;
816e85ed 690 struct vhost_scsi_cmd *cmd, *t;
9d6064a3
AH
691 struct llist_node *llnode;
692 struct se_cmd *se_cmd;
79c14141 693 struct iov_iter iov_iter;
48ae70dd
MC
694 bool signal = false;
695 int ret;
057cbf49 696
48ae70dd 697 llnode = llist_del_all(&svq->completion_list);
f591cf9f
DZ
698
699 mutex_lock(&svq->vq.mutex);
700
816e85ed 701 llist_for_each_entry_safe(cmd, t, llnode, tvc_completion_list) {
3c63f66a 702 se_cmd = &cmd->tvc_se_cmd;
057cbf49
NB
703
704 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
3c63f66a 705 cmd, se_cmd->residual_count, se_cmd->scsi_status);
057cbf49 706 memset(&v_rsp, 0, sizeof(v_rsp));
5ced58bf 707
fd479765 708 if (cmd->read_iter && vhost_scsi_copy_sgl_to_iov(cmd)) {
5ced58bf
MC
709 v_rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
710 } else {
711 v_rsp.resid = cpu_to_vhost32(cmd->tvc_vq,
712 se_cmd->residual_count);
713 /* TODO is status_qualifier field needed? */
714 v_rsp.status = se_cmd->scsi_status;
715 v_rsp.sense_len = cpu_to_vhost32(cmd->tvc_vq,
716 se_cmd->scsi_sense_length);
717 memcpy(v_rsp.sense, cmd->tvc_sense_buf,
718 se_cmd->scsi_sense_length);
719 }
79c14141 720
9d896067
MC
721 iov_iter_init(&iov_iter, ITER_DEST, cmd->tvc_resp_iovs,
722 cmd->tvc_resp_iovs_cnt, sizeof(v_rsp));
79c14141
NB
723 ret = copy_to_iter(&v_rsp, sizeof(v_rsp), &iov_iter);
724 if (likely(ret == sizeof(v_rsp))) {
48ae70dd
MC
725 signal = true;
726
3c63f66a 727 vhost_add_used(cmd->tvc_vq, cmd->tvc_vq_desc, 0);
1b7f390e 728 } else
057cbf49
NB
729 pr_err("Faulted on virtio_scsi_cmd_resp\n");
730
c2c5c259
DZ
731 vhost_scsi_log_write(cmd->tvc_vq, cmd->tvc_log,
732 cmd->tvc_log_num);
733
47a3565e 734 vhost_scsi_release_cmd_res(se_cmd);
057cbf49
NB
735 }
736
f591cf9f
DZ
737 mutex_unlock(&svq->vq.mutex);
738
48ae70dd
MC
739 if (signal)
740 vhost_signal(&svq->vs->dev, &svq->vq);
057cbf49
NB
741}
742
1a1ff825 743static struct vhost_scsi_cmd *
ddc5b5f6 744vhost_scsi_get_cmd(struct vhost_virtqueue *vq, u64 scsi_tag)
057cbf49 745{
25b98b64
MC
746 struct vhost_scsi_virtqueue *svq = container_of(vq,
747 struct vhost_scsi_virtqueue, vq);
1a1ff825 748 struct vhost_scsi_cmd *cmd;
bca939d5 749 struct scatterlist *sgl, *prot_sgl;
c2c5c259 750 struct vhost_log *log;
25b98b64 751 int tag;
057cbf49 752
c548e62b 753 tag = sbitmap_get(&svq->scsi_tags);
4a47d3a1 754 if (tag < 0) {
3ca51662 755 pr_warn_once("Guest sent too many cmds. Returning TASK_SET_FULL.\n");
4a47d3a1
NB
756 return ERR_PTR(-ENOMEM);
757 }
758
25b98b64 759 cmd = &svq->scsi_cmds[tag];
bca939d5
MC
760 sgl = cmd->sgl;
761 prot_sgl = cmd->prot_sgl;
c2c5c259 762 log = cmd->tvc_log;
473f0b15 763 memset(cmd, 0, sizeof(*cmd));
bca939d5
MC
764 cmd->sgl = sgl;
765 cmd->prot_sgl = prot_sgl;
c2c5c259 766 cmd->tvc_log = log;
4824d3bf 767 cmd->tvc_se_cmd.map_tag = tag;
1a1ff825 768 cmd->inflight = vhost_scsi_get_inflight(vq);
3c63f66a
AH
769
770 return cmd;
057cbf49
NB
771}
772
bca939d5
MC
773static void vhost_scsi_revert_map_iov_to_sgl(struct iov_iter *iter,
774 struct scatterlist *curr,
775 struct scatterlist *end)
776{
777 size_t revert_bytes = 0;
778 struct page *page;
779
780 while (curr != end) {
781 page = sg_page(curr);
782
783 if (page) {
784 put_page(page);
785 revert_bytes += curr->length;
786 }
787 /* Clear so we can re-use it for the copy path */
788 sg_set_page(curr, NULL, 0, 0);
789 curr = sg_next(curr);
790 }
791 iov_iter_revert(iter, revert_bytes);
792}
793
057cbf49
NB
794/*
795 * Map a user memory range into a scatterlist
796 *
797 * Returns the number of scatterlist entries used or -errno on error.
798 */
683bd967 799static int
1a1ff825 800vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
2f240c4a 801 struct iov_iter *iter,
bca939d5
MC
802 struct sg_table *sg_table,
803 struct scatterlist **sgl,
5ced58bf 804 bool is_prot)
057cbf49 805{
4c1f3a7d
MC
806 struct vhost_scsi_virtqueue *svq = container_of(cmd->tvc_vq,
807 struct vhost_scsi_virtqueue, vq);
808 struct page **pages = svq->upages;
bca939d5
MC
809 struct scatterlist *sg = *sgl;
810 ssize_t bytes;
811 size_t offset;
812 unsigned int n, npages = 0;
3aee26b4 813
1ef255e2 814 bytes = iov_iter_get_pages2(iter, pages, LONG_MAX,
2f240c4a 815 VHOST_SCSI_PREALLOC_UPAGES, &offset);
1810053e 816 /* No pages were pinned */
2f240c4a
AV
817 if (bytes <= 0)
818 return bytes < 0 ? bytes : -EFAULT;
1810053e 819
2f240c4a 820 while (bytes) {
bca939d5 821 n = min_t(unsigned int, PAGE_SIZE - offset, bytes);
5ced58bf
MC
822 /*
823 * The block layer requires bios/requests to be a multiple of
824 * 512 bytes, but Windows can send us vecs that are misaligned.
825 * This can result in bios and later requests with misaligned
826 * sizes if we have to break up a cmd/scatterlist into multiple
827 * bios.
828 *
829 * We currently only break up a command into multiple bios if
830 * we hit the vec/seg limit, so check if our sgl_count is
831 * greater than the max and if a vec in the cmd has a
832 * misaligned offset/size.
833 */
834 if (!is_prot &&
835 (offset & (SECTOR_SIZE - 1) || n & (SECTOR_SIZE - 1)) &&
836 cmd->tvc_sgl_count > BIO_MAX_VECS) {
837 WARN_ONCE(true,
838 "vhost-scsi detected misaligned IO. Performance may be degraded.");
839 goto revert_iter_get_pages;
840 }
841
bca939d5
MC
842 sg_set_page(sg, pages[npages++], n, offset);
843 sg = sg_next(sg);
2f240c4a
AV
844 bytes -= n;
845 offset = 0;
846 }
5ced58bf 847
bca939d5 848 *sgl = sg;
2f240c4a 849 return npages;
5ced58bf
MC
850
851revert_iter_get_pages:
bca939d5 852 vhost_scsi_revert_map_iov_to_sgl(iter, *sgl, sg);
5ced58bf 853
bca939d5
MC
854 iov_iter_revert(iter, bytes);
855 while (bytes) {
856 n = min_t(unsigned int, PAGE_SIZE, bytes);
5ced58bf
MC
857
858 put_page(pages[npages++]);
bca939d5 859 bytes -= n;
5ced58bf
MC
860 }
861
862 return -EINVAL;
057cbf49
NB
863}
864
683bd967 865static int
e8de56b5 866vhost_scsi_calc_sgls(struct iov_iter *iter, size_t bytes, int max_sgls)
057cbf49 867{
e8de56b5 868 int sgl_count = 0;
057cbf49 869
de4f5fed 870 if (!iter || !iter_iov(iter)) {
e8de56b5
NB
871 pr_err("%s: iter->iov is NULL, but expected bytes: %zu"
872 " present\n", __func__, bytes);
873 return -EINVAL;
874 }
f3158f36 875
e8de56b5
NB
876 sgl_count = iov_iter_npages(iter, 0xffff);
877 if (sgl_count > max_sgls) {
878 pr_err("%s: requested sgl_count: %d exceeds pre-allocated"
879 " max_sgls: %d\n", __func__, sgl_count, max_sgls);
880 return -EINVAL;
5a01d082 881 }
e8de56b5
NB
882 return sgl_count;
883}
057cbf49 884
e8de56b5 885static int
5ced58bf 886vhost_scsi_copy_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
ddc5b5f6
MC
887 struct sg_table *sg_table, int sg_count,
888 int data_dir)
5ced58bf
MC
889{
890 size_t len = iov_iter_count(iter);
891 unsigned int nbytes = 0;
bca939d5 892 struct scatterlist *sg;
5ced58bf 893 struct page *page;
891b99ea 894 int i, ret;
5ced58bf 895
ddc5b5f6 896 if (data_dir == DMA_FROM_DEVICE) {
fd479765
MC
897 cmd->read_iter = kzalloc(sizeof(*cmd->read_iter), GFP_KERNEL);
898 if (!cmd->read_iter)
5ced58bf 899 return -ENOMEM;
fd479765
MC
900
901 cmd->read_iov = dup_iter(cmd->read_iter, iter, GFP_KERNEL);
902 if (!cmd->read_iov) {
903 ret = -ENOMEM;
904 goto free_iter;
905 }
5ced58bf
MC
906 }
907
bca939d5 908 for_each_sgtable_sg(sg_table, sg, i) {
5ced58bf
MC
909 page = alloc_page(GFP_KERNEL);
910 if (!page) {
891b99ea 911 ret = -ENOMEM;
5ced58bf
MC
912 goto err;
913 }
914
915 nbytes = min_t(unsigned int, PAGE_SIZE, len);
bca939d5 916 sg_set_page(sg, page, nbytes, 0);
5ced58bf 917
ddc5b5f6 918 if (data_dir == DMA_TO_DEVICE &&
891b99ea
MC
919 copy_page_from_iter(page, 0, nbytes, iter) != nbytes) {
920 ret = -EFAULT;
5ced58bf 921 goto err;
891b99ea 922 }
5ced58bf
MC
923
924 len -= nbytes;
925 }
926
927 cmd->copied_iov = 1;
928 return 0;
929
930err:
931 pr_err("Could not read %u bytes while handling misaligned cmd\n",
932 nbytes);
933
bca939d5
MC
934 for_each_sgtable_sg(sg_table, sg, i) {
935 page = sg_page(sg);
936 if (page)
937 __free_page(page);
938 }
fd479765
MC
939 kfree(cmd->read_iov);
940free_iter:
941 kfree(cmd->read_iter);
891b99ea 942 return ret;
5ced58bf
MC
943}
944
945static int
c5ace19e 946vhost_scsi_map_iov_to_sgl(struct vhost_scsi_cmd *cmd, struct iov_iter *iter,
bca939d5 947 struct sg_table *sg_table, int sg_count, bool is_prot)
e8de56b5 948{
bca939d5 949 struct scatterlist *sg = sg_table->sgl;
2f240c4a 950 int ret;
5a01d082 951
2f240c4a 952 while (iov_iter_count(iter)) {
bca939d5 953 ret = vhost_scsi_map_to_sgl(cmd, iter, sg_table, &sg, is_prot);
057cbf49 954 if (ret < 0) {
bca939d5
MC
955 vhost_scsi_revert_map_iov_to_sgl(iter, sg_table->sgl,
956 sg);
057cbf49
NB
957 return ret;
958 }
057cbf49 959 }
5ced58bf 960
057cbf49
NB
961 return 0;
962}
963
e31885dd 964static int
ddc5b5f6 965vhost_scsi_mapal(struct vhost_scsi *vs, struct vhost_scsi_cmd *cmd,
e8de56b5 966 size_t prot_bytes, struct iov_iter *prot_iter,
ddc5b5f6 967 size_t data_bytes, struct iov_iter *data_iter, int data_dir)
e8de56b5
NB
968{
969 int sgl_count, ret;
e8de56b5
NB
970
971 if (prot_bytes) {
972 sgl_count = vhost_scsi_calc_sgls(prot_iter, prot_bytes,
1a1ff825 973 VHOST_SCSI_PREALLOC_PROT_SGLS);
bca939d5
MC
974 cmd->prot_table.sgl = cmd->prot_sgl;
975 ret = sg_alloc_table_chained(&cmd->prot_table, sgl_count,
976 cmd->prot_table.sgl,
977 vs->inline_sg_cnt);
978 if (ret)
979 return ret;
e8de56b5 980
e8de56b5
NB
981 cmd->tvc_prot_sgl_count = sgl_count;
982 pr_debug("%s prot_sg %p prot_sgl_count %u\n", __func__,
bca939d5 983 cmd->prot_table.sgl, cmd->tvc_prot_sgl_count);
e8de56b5 984
c5ace19e 985 ret = vhost_scsi_map_iov_to_sgl(cmd, prot_iter,
bca939d5 986 &cmd->prot_table,
c5ace19e 987 cmd->tvc_prot_sgl_count, true);
e31885dd 988 if (ret < 0) {
bca939d5
MC
989 sg_free_table_chained(&cmd->prot_table,
990 vs->inline_sg_cnt);
e31885dd
NB
991 cmd->tvc_prot_sgl_count = 0;
992 return ret;
993 }
e8de56b5
NB
994 }
995 sgl_count = vhost_scsi_calc_sgls(data_iter, data_bytes,
1a1ff825 996 VHOST_SCSI_PREALLOC_SGLS);
e8de56b5
NB
997 if (sgl_count < 0)
998 return sgl_count;
999
bca939d5
MC
1000 cmd->table.sgl = cmd->sgl;
1001 ret = sg_alloc_table_chained(&cmd->table, sgl_count, cmd->table.sgl,
1002 vs->inline_sg_cnt);
1003 if (ret)
1004 return ret;
1005
e8de56b5
NB
1006 cmd->tvc_sgl_count = sgl_count;
1007 pr_debug("%s data_sg %p data_sgl_count %u\n", __func__,
bca939d5 1008 cmd->table.sgl, cmd->tvc_sgl_count);
e8de56b5 1009
bca939d5 1010 ret = vhost_scsi_map_iov_to_sgl(cmd, data_iter, &cmd->table,
c5ace19e 1011 cmd->tvc_sgl_count, false);
bca939d5
MC
1012 if (ret == -EINVAL)
1013 ret = vhost_scsi_copy_iov_to_sgl(cmd, data_iter, &cmd->table,
ddc5b5f6 1014 cmd->tvc_sgl_count, data_dir);
e8de56b5 1015 if (ret < 0) {
bca939d5 1016 sg_free_table_chained(&cmd->table, vs->inline_sg_cnt);
e8de56b5
NB
1017 cmd->tvc_sgl_count = 0;
1018 return ret;
e31885dd
NB
1019 }
1020 return 0;
1021}
1022
46243860
NB
1023static int vhost_scsi_to_tcm_attr(int attr)
1024{
1025 switch (attr) {
1026 case VIRTIO_SCSI_S_SIMPLE:
1027 return TCM_SIMPLE_TAG;
1028 case VIRTIO_SCSI_S_ORDERED:
1029 return TCM_ORDERED_TAG;
1030 case VIRTIO_SCSI_S_HEAD:
1031 return TCM_HEAD_TAG;
1032 case VIRTIO_SCSI_S_ACA:
1033 return TCM_ACA_TAG;
1034 default:
1035 break;
1036 }
1037 return TCM_SIMPLE_TAG;
1038}
1039
ddc5b5f6
MC
1040static void vhost_scsi_target_queue_cmd(struct vhost_scsi_nexus *nexus,
1041 struct vhost_scsi_cmd *cmd,
1042 unsigned char *cdb, u16 lun,
1043 int task_attr, int data_dir,
1044 u32 exp_data_len)
057cbf49 1045{
3c63f66a 1046 struct se_cmd *se_cmd = &cmd->tvc_se_cmd;
95e7c434 1047 struct scatterlist *sg_ptr, *sg_prot_ptr = NULL;
057cbf49 1048
95e7c434 1049 /* FIXME: BIDI operation */
3c63f66a 1050 if (cmd->tvc_sgl_count) {
bca939d5 1051 sg_ptr = cmd->table.sgl;
95e7c434
NB
1052
1053 if (cmd->tvc_prot_sgl_count)
bca939d5 1054 sg_prot_ptr = cmd->prot_table.sgl;
95e7c434
NB
1055 else
1056 se_cmd->prot_pto = true;
057cbf49
NB
1057 } else {
1058 sg_ptr = NULL;
1059 }
9f0abc15 1060
649ee054 1061 se_cmd->tag = 0;
ddc5b5f6
MC
1062 target_init_cmd(se_cmd, nexus->tvn_se_sess, &cmd->tvc_sense_buf[0],
1063 lun, exp_data_len, vhost_scsi_to_tcm_attr(task_attr),
1064 data_dir, TARGET_SCF_ACK_KREF);
eb929804 1065
ddc5b5f6 1066 if (target_submit_prep(se_cmd, cdb, sg_ptr,
eb929804 1067 cmd->tvc_sgl_count, NULL, 0, sg_prot_ptr,
08694199 1068 cmd->tvc_prot_sgl_count, GFP_KERNEL))
eb929804
MC
1069 return;
1070
e2f4ea40 1071 target_submit(se_cmd);
057cbf49
NB
1072}
1073
3ca51662
MC
1074static void
1075vhost_scsi_send_status(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
58465d86 1076 struct vhost_scsi_ctx *vc, u8 status)
3ca51662 1077{
3ca51662 1078 struct virtio_scsi_cmd_resp rsp;
58465d86 1079 struct iov_iter iov_iter;
3ca51662
MC
1080 int ret;
1081
1082 memset(&rsp, 0, sizeof(rsp));
1083 rsp.status = status;
58465d86
DZ
1084
1085 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in,
1086 sizeof(rsp));
1087
1088 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
1089
1090 if (likely(ret == sizeof(rsp)))
1091 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
3ca51662
MC
1092 else
1093 pr_err("Faulted on virtio_scsi_cmd_resp\n");
1094}
1095
b1826871
DZ
1096#define TYPE_IO_CMD 0
1097#define TYPE_CTRL_TMF 1
1098#define TYPE_CTRL_AN 2
1099
683bd967
AH
1100static void
1101vhost_scsi_send_bad_target(struct vhost_scsi *vs,
1102 struct vhost_virtqueue *vq,
b1826871 1103 struct vhost_scsi_ctx *vc, int type)
637ab21e 1104{
b1826871
DZ
1105 union {
1106 struct virtio_scsi_cmd_resp cmd;
1107 struct virtio_scsi_ctrl_tmf_resp tmf;
1108 struct virtio_scsi_ctrl_an_resp an;
1109 } rsp;
1110 struct iov_iter iov_iter;
1111 size_t rsp_size;
637ab21e
AH
1112 int ret;
1113
1114 memset(&rsp, 0, sizeof(rsp));
b1826871
DZ
1115
1116 if (type == TYPE_IO_CMD) {
1117 rsp_size = sizeof(struct virtio_scsi_cmd_resp);
1118 rsp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
1119 } else if (type == TYPE_CTRL_TMF) {
1120 rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp);
1121 rsp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
1122 } else {
1123 rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp);
1124 rsp.an.response = VIRTIO_SCSI_S_BAD_TARGET;
1125 }
1126
1127 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in,
1128 rsp_size);
1129
1130 ret = copy_to_iter(&rsp, rsp_size, &iov_iter);
1131
1132 if (likely(ret == rsp_size))
1133 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
637ab21e 1134 else
b1826871 1135 pr_err("Faulted on virtio scsi type=%d\n", type);
637ab21e
AH
1136}
1137
09d75832
BM
1138static int
1139vhost_scsi_get_desc(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
e5e6b15b
DZ
1140 struct vhost_scsi_ctx *vc,
1141 struct vhost_log *log, unsigned int *log_num)
09d75832
BM
1142{
1143 int ret = -ENXIO;
1144
e5e6b15b
DZ
1145 if (likely(log_num))
1146 *log_num = 0;
1147
09d75832
BM
1148 vc->head = vhost_get_vq_desc(vq, vq->iov,
1149 ARRAY_SIZE(vq->iov), &vc->out, &vc->in,
e5e6b15b 1150 log, log_num);
09d75832
BM
1151
1152 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
1153 vc->head, vc->out, vc->in);
1154
1155 /* On error, stop handling until the next kick. */
1156 if (unlikely(vc->head < 0))
1157 goto done;
1158
1159 /* Nothing new? Wait for eventfd to tell us they refilled. */
1160 if (vc->head == vq->num) {
1161 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
1162 vhost_disable_notify(&vs->dev, vq);
1163 ret = -EAGAIN;
1164 }
1165 goto done;
1166 }
1167
1168 /*
1169 * Get the size of request and response buffers.
1170 * FIXME: Not correct for BIDI operation
1171 */
1172 vc->out_size = iov_length(vq->iov, vc->out);
1173 vc->in_size = iov_length(&vq->iov[vc->out], vc->in);
1174
1175 /*
1176 * Copy over the virtio-scsi request header, which for a
1177 * ANY_LAYOUT enabled guest may span multiple iovecs, or a
1178 * single iovec may contain both the header + outgoing
1179 * WRITE payloads.
1180 *
1181 * copy_from_iter() will advance out_iter, so that it will
1182 * point at the start of the outgoing WRITE payload, if
1183 * DMA_TO_DEVICE is set.
1184 */
de4eda9d 1185 iov_iter_init(&vc->out_iter, ITER_SOURCE, vq->iov, vc->out, vc->out_size);
09d75832
BM
1186 ret = 0;
1187
1188done:
1189 return ret;
1190}
1191
1192static int
1193vhost_scsi_chk_size(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc)
1194{
1195 if (unlikely(vc->in_size < vc->rsp_size)) {
1196 vq_err(vq,
1197 "Response buf too small, need min %zu bytes got %zu",
1198 vc->rsp_size, vc->in_size);
1199 return -EINVAL;
1200 } else if (unlikely(vc->out_size < vc->req_size)) {
1201 vq_err(vq,
1202 "Request buf too small, need min %zu bytes got %zu",
1203 vc->req_size, vc->out_size);
1204 return -EIO;
1205 }
1206
1207 return 0;
1208}
1209
1210static int
1211vhost_scsi_get_req(struct vhost_virtqueue *vq, struct vhost_scsi_ctx *vc,
1212 struct vhost_scsi_tpg **tpgp)
1213{
1214 int ret = -EIO;
1215
1216 if (unlikely(!copy_from_iter_full(vc->req, vc->req_size,
1217 &vc->out_iter))) {
a691ffb4 1218 vq_err(vq, "Faulted on copy_from_iter_full\n");
09d75832
BM
1219 } else if (unlikely(*vc->lunp != 1)) {
1220 /* virtio-scsi spec requires byte 0 of the lun to be 1 */
1221 vq_err(vq, "Illegal virtio-scsi lun: %u\n", *vc->lunp);
1222 } else {
221af82f
HZ
1223 struct vhost_scsi_tpg **vs_tpg, *tpg = NULL;
1224
1225 if (vc->target) {
1226 /* validated at handler entry */
1227 vs_tpg = vhost_vq_get_backend(vq);
1228 tpg = READ_ONCE(vs_tpg[*vc->target]);
1229 if (unlikely(!tpg)) {
1230 vq_err(vq, "Target 0x%x does not exist\n", *vc->target);
1231 goto out;
1232 }
09d75832 1233 }
09d75832 1234
221af82f
HZ
1235 if (tpgp)
1236 *tpgp = tpg;
1237 ret = 0;
1238 }
1239out:
09d75832
BM
1240 return ret;
1241}
1242
9d896067
MC
1243static int
1244vhost_scsi_setup_resp_iovs(struct vhost_scsi_cmd *cmd, struct iovec *in_iovs,
1245 unsigned int in_iovs_cnt)
1246{
1247 int i, cnt;
1248
1249 if (!in_iovs_cnt)
1250 return 0;
1251 /*
1252 * Initiator's normally just put the virtio_scsi_cmd_resp in the first
1253 * iov, but just in case they wedged in some data with it we check for
1254 * greater than or equal to the response struct.
1255 */
1256 if (in_iovs[0].iov_len >= sizeof(struct virtio_scsi_cmd_resp)) {
1257 cmd->tvc_resp_iovs = &cmd->tvc_resp_iov;
1258 cmd->tvc_resp_iovs_cnt = 1;
1259 } else {
1260 /*
1261 * Legacy descriptor layouts didn't specify that we must put
1262 * the entire response in one iov. Worst case we have a
1263 * iov per byte.
1264 */
1265 cnt = min(VHOST_SCSI_MAX_RESP_IOVS, in_iovs_cnt);
1266 cmd->tvc_resp_iovs = kcalloc(cnt, sizeof(struct iovec),
1267 GFP_KERNEL);
1268 if (!cmd->tvc_resp_iovs)
1269 return -ENOMEM;
1270
1271 cmd->tvc_resp_iovs_cnt = cnt;
1272 }
1273
1274 for (i = 0; i < cmd->tvc_resp_iovs_cnt; i++)
1275 cmd->tvc_resp_iovs[i] = in_iovs[i];
1276
1277 return 0;
1278}
1279
18f1becb
MC
1280static u16 vhost_buf_to_lun(u8 *lun_buf)
1281{
1282 return ((lun_buf[2] << 8) | lun_buf[3]) & 0x3FFF;
1283}
1284
683bd967
AH
1285static void
1286vhost_scsi_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
057cbf49 1287{
1a1ff825 1288 struct vhost_scsi_tpg **vs_tpg, *tpg;
057cbf49 1289 struct virtio_scsi_cmd_req v_req;
95e7c434 1290 struct virtio_scsi_cmd_req_pi v_req_pi;
ddc5b5f6 1291 struct vhost_scsi_nexus *nexus;
09d75832 1292 struct vhost_scsi_ctx vc;
1a1ff825 1293 struct vhost_scsi_cmd *cmd;
09d75832 1294 struct iov_iter in_iter, prot_iter, data_iter;
95e7c434 1295 u64 tag;
09b13fa8 1296 u32 exp_data_len, data_direction;
9d896067 1297 int ret, prot_bytes, c = 0;
95e7c434 1298 u16 lun;
09d75832 1299 u8 task_attr;
09b13fa8 1300 bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI);
ddc5b5f6 1301 u8 *cdb;
c2c5c259
DZ
1302 struct vhost_log *vq_log;
1303 unsigned int log_num;
057cbf49 1304
e7802212 1305 mutex_lock(&vq->mutex);
4f7f46d3
AH
1306 /*
1307 * We can handle the vq only after the endpoint is setup by calling the
1308 * VHOST_SCSI_SET_ENDPOINT ioctl.
4f7f46d3 1309 */
247643f8 1310 vs_tpg = vhost_vq_get_backend(vq);
4f7f46d3 1311 if (!vs_tpg)
e7802212 1312 goto out;
057cbf49 1313
09d75832
BM
1314 memset(&vc, 0, sizeof(vc));
1315 vc.rsp_size = sizeof(struct virtio_scsi_cmd_resp);
1316
057cbf49
NB
1317 vhost_disable_notify(&vs->dev, vq);
1318
c2c5c259
DZ
1319 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
1320 vq->log : NULL;
1321
c1ea02f1 1322 do {
c2c5c259 1323 ret = vhost_scsi_get_desc(vs, vq, &vc, vq_log, &log_num);
09d75832
BM
1324 if (ret)
1325 goto err;
1326
09b13fa8
NB
1327 /*
1328 * Setup pointers and values based upon different virtio-scsi
1329 * request header if T10_PI is enabled in KVM guest.
1330 */
1331 if (t10_pi) {
09d75832
BM
1332 vc.req = &v_req_pi;
1333 vc.req_size = sizeof(v_req_pi);
1334 vc.lunp = &v_req_pi.lun[0];
1335 vc.target = &v_req_pi.lun[1];
95e7c434 1336 } else {
09d75832
BM
1337 vc.req = &v_req;
1338 vc.req_size = sizeof(v_req);
1339 vc.lunp = &v_req.lun[0];
1340 vc.target = &v_req.lun[1];
95e7c434
NB
1341 }
1342
09b13fa8 1343 /*
09d75832
BM
1344 * Validate the size of request and response buffers.
1345 * Check for a sane response buffer so we can report
1346 * early errors back to the guest.
09b13fa8 1347 */
09d75832
BM
1348 ret = vhost_scsi_chk_size(vq, &vc);
1349 if (ret)
1350 goto err;
057cbf49 1351
09d75832
BM
1352 ret = vhost_scsi_get_req(vq, &vc, &tpg);
1353 if (ret)
1354 goto err;
1355
1356 ret = -EIO; /* bad target on any error from here on */
7fe412d0 1357
95e7c434 1358 /*
09b13fa8
NB
1359 * Determine data_direction by calculating the total outgoing
1360 * iovec sizes + incoming iovec sizes vs. virtio-scsi request +
1361 * response headers respectively.
95e7c434 1362 *
09b13fa8
NB
1363 * For DMA_TO_DEVICE this is out_iter, which is already pointing
1364 * to the right place.
1365 *
1366 * For DMA_FROM_DEVICE, the iovec will be just past the end
1367 * of the virtio-scsi response header in either the same
1368 * or immediately following iovec.
95e7c434 1369 *
09b13fa8
NB
1370 * Any associated T10_PI bytes for the outgoing / incoming
1371 * payloads are included in calculation of exp_data_len here.
95e7c434 1372 */
09b13fa8
NB
1373 prot_bytes = 0;
1374
09d75832 1375 if (vc.out_size > vc.req_size) {
09b13fa8 1376 data_direction = DMA_TO_DEVICE;
09d75832
BM
1377 exp_data_len = vc.out_size - vc.req_size;
1378 data_iter = vc.out_iter;
1379 } else if (vc.in_size > vc.rsp_size) {
09b13fa8 1380 data_direction = DMA_FROM_DEVICE;
09d75832 1381 exp_data_len = vc.in_size - vc.rsp_size;
09b13fa8 1382
de4eda9d 1383 iov_iter_init(&in_iter, ITER_DEST, &vq->iov[vc.out], vc.in,
09d75832
BM
1384 vc.rsp_size + exp_data_len);
1385 iov_iter_advance(&in_iter, vc.rsp_size);
09b13fa8
NB
1386 data_iter = in_iter;
1387 } else {
1388 data_direction = DMA_NONE;
1389 exp_data_len = 0;
1390 }
1391 /*
1392 * If T10_PI header + payload is present, setup prot_iter values
1393 * and recalculate data_iter for vhost_scsi_mapal() mapping to
1394 * host scatterlists via get_user_pages_fast().
95e7c434 1395 */
09b13fa8 1396 if (t10_pi) {
95e7c434
NB
1397 if (v_req_pi.pi_bytesout) {
1398 if (data_direction != DMA_TO_DEVICE) {
09b13fa8
NB
1399 vq_err(vq, "Received non zero pi_bytesout,"
1400 " but wrong data_direction\n");
09d75832 1401 goto err;
95e7c434 1402 }
e72fd72e 1403 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesout);
95e7c434
NB
1404 } else if (v_req_pi.pi_bytesin) {
1405 if (data_direction != DMA_FROM_DEVICE) {
09b13fa8
NB
1406 vq_err(vq, "Received non zero pi_bytesin,"
1407 " but wrong data_direction\n");
09d75832 1408 goto err;
95e7c434 1409 }
e72fd72e 1410 prot_bytes = vhost32_to_cpu(vq, v_req_pi.pi_bytesin);
95e7c434 1411 }
09b13fa8 1412 /*
4542d623
GE
1413 * Set prot_iter to data_iter and truncate it to
1414 * prot_bytes, and advance data_iter past any
5ff1f51e 1415 * preceding prot_bytes that may be present.
09b13fa8
NB
1416 *
1417 * Also fix up the exp_data_len to reflect only the
1418 * actual data payload length.
1419 */
95e7c434 1420 if (prot_bytes) {
09b13fa8
NB
1421 exp_data_len -= prot_bytes;
1422 prot_iter = data_iter;
4542d623 1423 iov_iter_truncate(&prot_iter, prot_bytes);
09b13fa8 1424 iov_iter_advance(&data_iter, prot_bytes);
95e7c434 1425 }
e72fd72e 1426 tag = vhost64_to_cpu(vq, v_req_pi.tag);
95e7c434
NB
1427 task_attr = v_req_pi.task_attr;
1428 cdb = &v_req_pi.cdb[0];
18f1becb 1429 lun = vhost_buf_to_lun(v_req_pi.lun);
95e7c434 1430 } else {
e72fd72e 1431 tag = vhost64_to_cpu(vq, v_req.tag);
95e7c434
NB
1432 task_attr = v_req.task_attr;
1433 cdb = &v_req.cdb[0];
18f1becb 1434 lun = vhost_buf_to_lun(v_req.lun);
95e7c434 1435 }
95e7c434 1436 /*
09b13fa8
NB
1437 * Check that the received CDB size does not exceeded our
1438 * hardcoded max for vhost-scsi, then get a pre-allocated
1439 * cmd descriptor for the new virtio-scsi tag.
95e7c434
NB
1440 *
1441 * TODO what if cdb was too small for varlen cdb header?
1442 */
1a1ff825 1443 if (unlikely(scsi_command_size(cdb) > VHOST_SCSI_MAX_CDB_SIZE)) {
95e7c434
NB
1444 vq_err(vq, "Received SCSI CDB with command_size: %d that"
1445 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1a1ff825 1446 scsi_command_size(cdb), VHOST_SCSI_MAX_CDB_SIZE);
09d75832 1447 goto err;
95e7c434 1448 }
ddc5b5f6
MC
1449
1450 nexus = tpg->tpg_nexus;
1451 if (!nexus) {
1452 vq_err(vq, "Unable to locate active struct vhost_scsi_nexus\n");
1453 ret = -EIO;
1454 goto err;
1455 }
1456
1457 cmd = vhost_scsi_get_cmd(vq, tag);
3c63f66a 1458 if (IS_ERR(cmd)) {
3ca51662
MC
1459 ret = PTR_ERR(cmd);
1460 vq_err(vq, "vhost_scsi_get_tag failed %dd\n", ret);
09d75832 1461 goto err;
057cbf49 1462 }
3c63f66a 1463 cmd->tvc_vq = vq;
9d896067
MC
1464
1465 ret = vhost_scsi_setup_resp_iovs(cmd, &vq->iov[vc.out], vc.in);
1466 if (ret) {
1467 vq_err(vq, "Failed to alloc recv iovs\n");
1468 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd);
1469 goto err;
1470 }
057cbf49 1471
c2c5c259
DZ
1472 if (unlikely(vq_log && log_num)) {
1473 ret = vhost_scsi_copy_cmd_log(vq, cmd, vq_log, log_num);
1474 if (unlikely(ret)) {
1475 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd);
1476 goto err;
1477 }
1478 }
1479
057cbf49 1480 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
ddc5b5f6 1481 cdb[0], lun);
09b13fa8
NB
1482 pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:"
1483 " %d\n", cmd, exp_data_len, prot_bytes, data_direction);
057cbf49
NB
1484
1485 if (data_direction != DMA_NONE) {
ddc5b5f6
MC
1486 ret = vhost_scsi_mapal(vs, cmd, prot_bytes, &prot_iter,
1487 exp_data_len, &data_iter,
1488 data_direction);
891b99ea 1489 if (unlikely(ret)) {
057cbf49 1490 vq_err(vq, "Failed to map iov to sgl\n");
47a3565e 1491 vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd);
09d75832 1492 goto err;
057cbf49
NB
1493 }
1494 }
057cbf49
NB
1495 /*
1496 * Save the descriptor from vhost_get_vq_desc() to be used to
1497 * complete the virtio-scsi request in TCM callback context via
09b13fa8 1498 * vhost_scsi_queue_data_in() and vhost_scsi_queue_status()
057cbf49 1499 */
09d75832 1500 cmd->tvc_vq_desc = vc.head;
ddc5b5f6
MC
1501 vhost_scsi_target_queue_cmd(nexus, cmd, cdb, lun, task_attr,
1502 data_direction,
1503 exp_data_len + prot_bytes);
09d75832
BM
1504 ret = 0;
1505err:
1506 /*
1507 * ENXIO: No more requests, or read error, wait for next kick
1508 * EINVAL: Invalid response buffer, drop the request
1509 * EIO: Respond with bad target
1510 * EAGAIN: Pending request
3ca51662 1511 * ENOMEM: Could not allocate resources for request
09d75832
BM
1512 */
1513 if (ret == -ENXIO)
1514 break;
c2c5c259 1515 else if (ret == -EIO) {
b1826871 1516 vhost_scsi_send_bad_target(vs, vq, &vc, TYPE_IO_CMD);
c2c5c259
DZ
1517 vhost_scsi_log_write(vq, vq_log, log_num);
1518 } else if (ret == -ENOMEM) {
58465d86 1519 vhost_scsi_send_status(vs, vq, &vc,
3ca51662 1520 SAM_STAT_TASK_SET_FULL);
c2c5c259
DZ
1521 vhost_scsi_log_write(vq, vq_log, log_num);
1522 }
c1ea02f1 1523 } while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
e7802212 1524out:
7ea206cf 1525 mutex_unlock(&vq->mutex);
057cbf49
NB
1526}
1527
0d02dbd6 1528static void
efd838fe
MC
1529vhost_scsi_send_tmf_resp(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
1530 int in_iovs, int vq_desc, struct iovec *resp_iov,
1531 int tmf_resp_code)
0d02dbd6 1532{
0d02dbd6 1533 struct virtio_scsi_ctrl_tmf_resp rsp;
8e5dadfe 1534 struct iov_iter iov_iter;
0d02dbd6
BM
1535 int ret;
1536
1537 pr_debug("%s\n", __func__);
1538 memset(&rsp, 0, sizeof(rsp));
efd838fe 1539 rsp.response = tmf_resp_code;
8e5dadfe 1540
de4eda9d 1541 iov_iter_init(&iov_iter, ITER_DEST, resp_iov, in_iovs, sizeof(rsp));
8e5dadfe
BM
1542
1543 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
1544 if (likely(ret == sizeof(rsp)))
efd838fe 1545 vhost_add_used_and_signal(&vs->dev, vq, vq_desc, 0);
0d02dbd6
BM
1546 else
1547 pr_err("Faulted on virtio_scsi_ctrl_tmf_resp\n");
1548}
1549
efd838fe
MC
1550static void vhost_scsi_tmf_resp_work(struct vhost_work *work)
1551{
1552 struct vhost_scsi_tmf *tmf = container_of(work, struct vhost_scsi_tmf,
1553 vwork);
59b701b9 1554 int resp_code;
efd838fe 1555
59b701b9 1556 if (tmf->scsi_resp == TMR_FUNCTION_COMPLETE)
efd838fe 1557 resp_code = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
59b701b9 1558 else
efd838fe
MC
1559 resp_code = VIRTIO_SCSI_S_FUNCTION_REJECTED;
1560
f591cf9f 1561 mutex_lock(&tmf->svq->vq.mutex);
efd838fe
MC
1562 vhost_scsi_send_tmf_resp(tmf->vhost, &tmf->svq->vq, tmf->in_iovs,
1563 tmf->vq_desc, &tmf->resp_iov, resp_code);
a94c96a3
DZ
1564 vhost_scsi_log_write(&tmf->svq->vq, tmf->tmf_log,
1565 tmf->tmf_log_num);
f591cf9f
DZ
1566 mutex_unlock(&tmf->svq->vq.mutex);
1567
efd838fe
MC
1568 vhost_scsi_release_tmf_res(tmf);
1569}
1570
59b701b9
MC
1571static void vhost_scsi_tmf_flush_work(struct work_struct *work)
1572{
1573 struct vhost_scsi_tmf *tmf = container_of(work, struct vhost_scsi_tmf,
1574 flush_work);
1575 struct vhost_virtqueue *vq = &tmf->svq->vq;
1576 /*
1577 * Make sure we have sent responses for other commands before we
1578 * send our response.
1579 */
1580 vhost_dev_flush(vq->dev);
0352c961
MC
1581 if (!vhost_vq_work_queue(vq, &tmf->vwork))
1582 vhost_scsi_release_tmf_res(tmf);
59b701b9
MC
1583}
1584
efd838fe
MC
1585static void
1586vhost_scsi_handle_tmf(struct vhost_scsi *vs, struct vhost_scsi_tpg *tpg,
1587 struct vhost_virtqueue *vq,
1588 struct virtio_scsi_ctrl_tmf_req *vtmf,
a94c96a3
DZ
1589 struct vhost_scsi_ctx *vc,
1590 struct vhost_log *log, unsigned int log_num)
efd838fe
MC
1591{
1592 struct vhost_scsi_virtqueue *svq = container_of(vq,
1593 struct vhost_scsi_virtqueue, vq);
1594 struct vhost_scsi_tmf *tmf;
1595
1596 if (vhost32_to_cpu(vq, vtmf->subtype) !=
1597 VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET)
1598 goto send_reject;
1599
1600 if (!tpg->tpg_nexus || !tpg->tpg_nexus->tvn_se_sess) {
1601 pr_err("Unable to locate active struct vhost_scsi_nexus for LUN RESET.\n");
1602 goto send_reject;
1603 }
1604
4c363c81
MC
1605 tmf = kzalloc(sizeof(*tmf), GFP_KERNEL);
1606 if (!tmf)
efd838fe 1607 goto send_reject;
efd838fe 1608
59b701b9 1609 INIT_WORK(&tmf->flush_work, vhost_scsi_tmf_flush_work);
4c363c81 1610 vhost_work_init(&tmf->vwork, vhost_scsi_tmf_resp_work);
efd838fe
MC
1611 tmf->vhost = vs;
1612 tmf->svq = svq;
1613 tmf->resp_iov = vq->iov[vc->out];
1614 tmf->vq_desc = vc->head;
1615 tmf->in_iovs = vc->in;
1616 tmf->inflight = vhost_scsi_get_inflight(vq);
1617
a94c96a3
DZ
1618 if (unlikely(log && log_num)) {
1619 tmf->tmf_log = kmalloc_array(log_num, sizeof(*tmf->tmf_log),
1620 GFP_KERNEL);
1621 if (tmf->tmf_log) {
1622 memcpy(tmf->tmf_log, log, sizeof(*tmf->tmf_log) * log_num);
1623 tmf->tmf_log_num = log_num;
1624 } else {
1625 pr_err("vhost_scsi tmf log allocation error\n");
1626 vhost_scsi_release_tmf_res(tmf);
1627 goto send_reject;
1628 }
1629 }
1630
efd838fe
MC
1631 if (target_submit_tmr(&tmf->se_cmd, tpg->tpg_nexus->tvn_se_sess, NULL,
1632 vhost_buf_to_lun(vtmf->lun), NULL,
1633 TMR_LUN_RESET, GFP_KERNEL, 0,
1634 TARGET_SCF_ACK_KREF) < 0) {
1635 vhost_scsi_release_tmf_res(tmf);
1636 goto send_reject;
1637 }
1638
1639 return;
1640
1641send_reject:
1642 vhost_scsi_send_tmf_resp(vs, vq, vc->in, vc->head, &vq->iov[vc->out],
1643 VIRTIO_SCSI_S_FUNCTION_REJECTED);
a94c96a3 1644 vhost_scsi_log_write(vq, log, log_num);
efd838fe
MC
1645}
1646
0d02dbd6
BM
1647static void
1648vhost_scsi_send_an_resp(struct vhost_scsi *vs,
3f8ca2e1
BM
1649 struct vhost_virtqueue *vq,
1650 struct vhost_scsi_ctx *vc)
0d02dbd6 1651{
0d02dbd6 1652 struct virtio_scsi_ctrl_an_resp rsp;
8e5dadfe 1653 struct iov_iter iov_iter;
0d02dbd6
BM
1654 int ret;
1655
1656 pr_debug("%s\n", __func__);
1657 memset(&rsp, 0, sizeof(rsp)); /* event_actual = 0 */
1658 rsp.response = VIRTIO_SCSI_S_OK;
8e5dadfe 1659
de4eda9d 1660 iov_iter_init(&iov_iter, ITER_DEST, &vq->iov[vc->out], vc->in, sizeof(rsp));
8e5dadfe
BM
1661
1662 ret = copy_to_iter(&rsp, sizeof(rsp), &iov_iter);
1663 if (likely(ret == sizeof(rsp)))
3f8ca2e1 1664 vhost_add_used_and_signal(&vs->dev, vq, vc->head, 0);
0d02dbd6
BM
1665 else
1666 pr_err("Faulted on virtio_scsi_ctrl_an_resp\n");
1667}
1668
1669static void
1670vhost_scsi_ctl_handle_vq(struct vhost_scsi *vs, struct vhost_virtqueue *vq)
1671{
efd838fe 1672 struct vhost_scsi_tpg *tpg;
0d02dbd6
BM
1673 union {
1674 __virtio32 type;
1675 struct virtio_scsi_ctrl_an_req an;
1676 struct virtio_scsi_ctrl_tmf_req tmf;
1677 } v_req;
3f8ca2e1
BM
1678 struct vhost_scsi_ctx vc;
1679 size_t typ_size;
c1ea02f1 1680 int ret, c = 0;
a94c96a3
DZ
1681 struct vhost_log *vq_log;
1682 unsigned int log_num;
0d02dbd6
BM
1683
1684 mutex_lock(&vq->mutex);
1685 /*
1686 * We can handle the vq only after the endpoint is setup by calling the
1687 * VHOST_SCSI_SET_ENDPOINT ioctl.
1688 */
247643f8 1689 if (!vhost_vq_get_backend(vq))
0d02dbd6
BM
1690 goto out;
1691
3f8ca2e1
BM
1692 memset(&vc, 0, sizeof(vc));
1693
0d02dbd6
BM
1694 vhost_disable_notify(&vs->dev, vq);
1695
a94c96a3
DZ
1696 vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
1697 vq->log : NULL;
1698
c1ea02f1 1699 do {
a94c96a3 1700 ret = vhost_scsi_get_desc(vs, vq, &vc, vq_log, &log_num);
3f8ca2e1
BM
1701 if (ret)
1702 goto err;
0d02dbd6
BM
1703
1704 /*
3f8ca2e1
BM
1705 * Get the request type first in order to setup
1706 * other parameters dependent on the type.
0d02dbd6 1707 */
3f8ca2e1 1708 vc.req = &v_req.type;
0d02dbd6
BM
1709 typ_size = sizeof(v_req.type);
1710
3f8ca2e1
BM
1711 if (unlikely(!copy_from_iter_full(vc.req, typ_size,
1712 &vc.out_iter))) {
0d02dbd6
BM
1713 vq_err(vq, "Faulted on copy_from_iter tmf type\n");
1714 /*
3f8ca2e1
BM
1715 * The size of the response buffer depends on the
1716 * request type and must be validated against it.
0d02dbd6
BM
1717 * Since the request type is not known, don't send
1718 * a response.
1719 */
1720 continue;
1721 }
1722
295c1b98 1723 switch (vhost32_to_cpu(vq, v_req.type)) {
0d02dbd6 1724 case VIRTIO_SCSI_T_TMF:
3f8ca2e1
BM
1725 vc.req = &v_req.tmf;
1726 vc.req_size = sizeof(struct virtio_scsi_ctrl_tmf_req);
1727 vc.rsp_size = sizeof(struct virtio_scsi_ctrl_tmf_resp);
1728 vc.lunp = &v_req.tmf.lun[0];
1729 vc.target = &v_req.tmf.lun[1];
0d02dbd6
BM
1730 break;
1731 case VIRTIO_SCSI_T_AN_QUERY:
1732 case VIRTIO_SCSI_T_AN_SUBSCRIBE:
3f8ca2e1
BM
1733 vc.req = &v_req.an;
1734 vc.req_size = sizeof(struct virtio_scsi_ctrl_an_req);
1735 vc.rsp_size = sizeof(struct virtio_scsi_ctrl_an_resp);
1736 vc.lunp = &v_req.an.lun[0];
1737 vc.target = NULL;
0d02dbd6
BM
1738 break;
1739 default:
1740 vq_err(vq, "Unknown control request %d", v_req.type);
1741 continue;
1742 }
1743
1744 /*
3f8ca2e1
BM
1745 * Validate the size of request and response buffers.
1746 * Check for a sane response buffer so we can report
1747 * early errors back to the guest.
0d02dbd6 1748 */
3f8ca2e1
BM
1749 ret = vhost_scsi_chk_size(vq, &vc);
1750 if (ret)
1751 goto err;
0d02dbd6 1752
3f8ca2e1
BM
1753 /*
1754 * Get the rest of the request now that its size is known.
1755 */
1756 vc.req += typ_size;
1757 vc.req_size -= typ_size;
0d02dbd6 1758
efd838fe 1759 ret = vhost_scsi_get_req(vq, &vc, &tpg);
3f8ca2e1
BM
1760 if (ret)
1761 goto err;
0d02dbd6 1762
3f8ca2e1 1763 if (v_req.type == VIRTIO_SCSI_T_TMF)
a94c96a3
DZ
1764 vhost_scsi_handle_tmf(vs, tpg, vq, &v_req.tmf, &vc,
1765 vq_log, log_num);
1766 else {
3f8ca2e1 1767 vhost_scsi_send_an_resp(vs, vq, &vc);
a94c96a3
DZ
1768 vhost_scsi_log_write(vq, vq_log, log_num);
1769 }
3f8ca2e1
BM
1770err:
1771 /*
1772 * ENXIO: No more requests, or read error, wait for next kick
1773 * EINVAL: Invalid response buffer, drop the request
1774 * EIO: Respond with bad target
1775 * EAGAIN: Pending request
1776 */
1777 if (ret == -ENXIO)
1778 break;
a94c96a3 1779 else if (ret == -EIO) {
b1826871
DZ
1780 vhost_scsi_send_bad_target(vs, vq, &vc,
1781 v_req.type == VIRTIO_SCSI_T_TMF ?
1782 TYPE_CTRL_TMF :
1783 TYPE_CTRL_AN);
a94c96a3
DZ
1784 vhost_scsi_log_write(vq, vq_log, log_num);
1785 }
c1ea02f1 1786 } while (likely(!vhost_exceeds_weight(vq, ++c, 0)));
0d02dbd6
BM
1787out:
1788 mutex_unlock(&vq->mutex);
1789}
1790
057cbf49
NB
1791static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
1792{
0d02dbd6
BM
1793 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1794 poll.work);
1795 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1796
101998f6 1797 pr_debug("%s: The handling func for control queue.\n", __func__);
0d02dbd6 1798 vhost_scsi_ctl_handle_vq(vs, vq);
057cbf49
NB
1799}
1800
683bd967 1801static void
78af31cc
MC
1802vhost_scsi_send_evt(struct vhost_scsi *vs, struct vhost_virtqueue *vq,
1803 struct vhost_scsi_tpg *tpg, struct se_lun *lun,
1804 u32 event, u32 reason)
a6c9af87 1805{
1a1ff825 1806 struct vhost_scsi_evt *evt;
a6c9af87 1807
1a1ff825 1808 evt = vhost_scsi_allocate_evt(vs, event, reason);
a6c9af87
AH
1809 if (!evt)
1810 return;
1811
1812 if (tpg && lun) {
1813 /* TODO: share lun setup code with virtio-scsi.ko */
1814 /*
1815 * Note: evt->event is zeroed when we allocate it and
1816 * lun[4-7] need to be zero according to virtio-scsi spec.
1817 */
1818 evt->event.lun[0] = 0x01;
59c816c1 1819 evt->event.lun[1] = tpg->tport_tpgt;
a6c9af87
AH
1820 if (lun->unpacked_lun >= 256)
1821 evt->event.lun[2] = lun->unpacked_lun >> 8 | 0x40 ;
1822 evt->event.lun[3] = lun->unpacked_lun & 0xFF;
1823 }
1824
1825 llist_add(&evt->list, &vs->vs_event_list);
b1b2ce58
MC
1826 if (!vhost_vq_work_queue(vq, &vs->vs_event_work))
1827 vhost_scsi_complete_events(vs, true);
a6c9af87
AH
1828}
1829
057cbf49
NB
1830static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
1831{
a6c9af87
AH
1832 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1833 poll.work);
1834 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1835
1836 mutex_lock(&vq->mutex);
247643f8 1837 if (!vhost_vq_get_backend(vq))
a6c9af87
AH
1838 goto out;
1839
1840 if (vs->vs_events_missed)
78af31cc
MC
1841 vhost_scsi_send_evt(vs, vq, NULL, NULL, VIRTIO_SCSI_T_NO_EVENT,
1842 0);
a6c9af87
AH
1843out:
1844 mutex_unlock(&vq->mutex);
057cbf49
NB
1845}
1846
1847static void vhost_scsi_handle_kick(struct vhost_work *work)
1848{
1849 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
1850 poll.work);
1851 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
1852
1b7f390e 1853 vhost_scsi_handle_vq(vs, vq);
057cbf49
NB
1854}
1855
3dfbff32 1856/* Callers must hold dev mutex */
4f7f46d3
AH
1857static void vhost_scsi_flush(struct vhost_scsi *vs)
1858{
1859 int i;
1860
f2f0173d 1861 /* Init new inflight and remember the old inflight */
f49c2226 1862 vhost_scsi_init_inflight(vs, vs->old_inflight);
f2f0173d
AH
1863
1864 /*
1865 * The inflight->kref was initialized to 1. We decrement it here to
1866 * indicate the start of the flush operation so that it will reach 0
1867 * when all the reqs are finished.
1868 */
f49c2226
MC
1869 for (i = 0; i < vs->dev.nvqs; i++)
1870 kref_put(&vs->old_inflight[i]->kref, vhost_scsi_done_inflight);
f2f0173d
AH
1871
1872 /* Flush both the vhost poll and vhost work */
b2ffa407 1873 vhost_dev_flush(&vs->dev);
f2f0173d
AH
1874
1875 /* Wait for all reqs issued before the flush to be finished */
f49c2226
MC
1876 for (i = 0; i < vs->dev.nvqs; i++)
1877 wait_for_completion(&vs->old_inflight[i]->comp);
4f7f46d3
AH
1878}
1879
c2c5c259
DZ
1880static void vhost_scsi_destroy_vq_log(struct vhost_virtqueue *vq)
1881{
1882 struct vhost_scsi_virtqueue *svq = container_of(vq,
1883 struct vhost_scsi_virtqueue, vq);
1884 struct vhost_scsi_cmd *tv_cmd;
1885 unsigned int i;
1886
1887 if (!svq->scsi_cmds)
1888 return;
1889
1890 for (i = 0; i < svq->max_cmds; i++) {
1891 tv_cmd = &svq->scsi_cmds[i];
1892 kfree(tv_cmd->tvc_log);
1893 tv_cmd->tvc_log = NULL;
1894 tv_cmd->tvc_log_num = 0;
1895 }
1896}
1897
25b98b64
MC
1898static void vhost_scsi_destroy_vq_cmds(struct vhost_virtqueue *vq)
1899{
1900 struct vhost_scsi_virtqueue *svq = container_of(vq,
1901 struct vhost_scsi_virtqueue, vq);
1902 struct vhost_scsi_cmd *tv_cmd;
1903 unsigned int i;
1904
1905 if (!svq->scsi_cmds)
1906 return;
1907
1908 for (i = 0; i < svq->max_cmds; i++) {
1909 tv_cmd = &svq->scsi_cmds[i];
1910
bca939d5
MC
1911 kfree(tv_cmd->sgl);
1912 kfree(tv_cmd->prot_sgl);
25b98b64
MC
1913 }
1914
1915 sbitmap_free(&svq->scsi_tags);
4c1f3a7d 1916 kfree(svq->upages);
c2c5c259 1917 vhost_scsi_destroy_vq_log(vq);
25b98b64
MC
1918 kfree(svq->scsi_cmds);
1919 svq->scsi_cmds = NULL;
1920}
1921
1922static int vhost_scsi_setup_vq_cmds(struct vhost_virtqueue *vq, int max_cmds)
1923{
1924 struct vhost_scsi_virtqueue *svq = container_of(vq,
1925 struct vhost_scsi_virtqueue, vq);
bca939d5 1926 struct vhost_scsi *vs = svq->vs;
25b98b64
MC
1927 struct vhost_scsi_cmd *tv_cmd;
1928 unsigned int i;
1929
1930 if (svq->scsi_cmds)
1931 return 0;
1932
1933 if (sbitmap_init_node(&svq->scsi_tags, max_cmds, -1, GFP_KERNEL,
c548e62b 1934 NUMA_NO_NODE, false, true))
25b98b64
MC
1935 return -ENOMEM;
1936 svq->max_cmds = max_cmds;
1937
1938 svq->scsi_cmds = kcalloc(max_cmds, sizeof(*tv_cmd), GFP_KERNEL);
1939 if (!svq->scsi_cmds) {
1940 sbitmap_free(&svq->scsi_tags);
1941 return -ENOMEM;
1942 }
1943
4c1f3a7d
MC
1944 svq->upages = kcalloc(VHOST_SCSI_PREALLOC_UPAGES, sizeof(struct page *),
1945 GFP_KERNEL);
1946 if (!svq->upages)
1947 goto out;
1948
25b98b64
MC
1949 for (i = 0; i < max_cmds; i++) {
1950 tv_cmd = &svq->scsi_cmds[i];
1951
bca939d5
MC
1952 if (vs->inline_sg_cnt) {
1953 tv_cmd->sgl = kcalloc(vs->inline_sg_cnt,
1954 sizeof(struct scatterlist),
1955 GFP_KERNEL);
1956 if (!tv_cmd->sgl) {
1957 pr_err("Unable to allocate tv_cmd->sgl\n");
1958 goto out;
1959 }
25b98b64
MC
1960 }
1961
bca939d5
MC
1962 if (vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI) &&
1963 vs->inline_sg_cnt) {
1964 tv_cmd->prot_sgl = kcalloc(vs->inline_sg_cnt,
1965 sizeof(struct scatterlist),
1966 GFP_KERNEL);
1967 if (!tv_cmd->prot_sgl) {
1968 pr_err("Unable to allocate tv_cmd->prot_sgl\n");
bf2d6503
MC
1969 goto out;
1970 }
25b98b64
MC
1971 }
1972 }
1973 return 0;
1974out:
1975 vhost_scsi_destroy_vq_cmds(vq);
1976 return -ENOMEM;
1977}
1978
057cbf49
NB
1979/*
1980 * Called from vhost_scsi_ioctl() context to walk the list of available
1a1ff825 1981 * vhost_scsi_tpg with an active struct vhost_scsi_nexus
f2b7daf5
AH
1982 *
1983 * The lock nesting rule is:
bea273c7 1984 * vs->dev.mutex -> vhost_scsi_mutex -> tpg->tv_tpg_mutex -> vq->mutex
057cbf49 1985 */
683bd967
AH
1986static int
1987vhost_scsi_set_endpoint(struct vhost_scsi *vs,
1988 struct vhost_scsi_target *t)
057cbf49 1989{
ab8edab1 1990 struct se_portal_group *se_tpg;
1a1ff825
NB
1991 struct vhost_scsi_tport *tv_tport;
1992 struct vhost_scsi_tpg *tpg;
1993 struct vhost_scsi_tpg **vs_tpg;
4f7f46d3
AH
1994 struct vhost_virtqueue *vq;
1995 int index, ret, i, len;
67e18cf9 1996 bool match = false;
057cbf49
NB
1997
1998 mutex_lock(&vs->dev.mutex);
f2b7daf5 1999
057cbf49
NB
2000 /* Verify that ring has been setup correctly. */
2001 for (index = 0; index < vs->dev.nvqs; ++index) {
2002 /* Verify that ring has been setup correctly. */
3ab2e420 2003 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
f2b7daf5
AH
2004 ret = -EFAULT;
2005 goto out;
057cbf49
NB
2006 }
2007 }
057cbf49 2008
5dd639a1
MC
2009 if (vs->vs_tpg) {
2010 pr_err("vhost-scsi endpoint already set for %s.\n",
2011 vs->vs_vhost_wwpn);
2012 ret = -EEXIST;
2013 goto out;
2014 }
2015
4f7f46d3
AH
2016 len = sizeof(vs_tpg[0]) * VHOST_SCSI_MAX_TARGET;
2017 vs_tpg = kzalloc(len, GFP_KERNEL);
2018 if (!vs_tpg) {
f2b7daf5
AH
2019 ret = -ENOMEM;
2020 goto out;
4f7f46d3 2021 }
4f7f46d3 2022
bea273c7 2023 mutex_lock(&vhost_scsi_mutex);
1a1ff825 2024 list_for_each_entry(tpg, &vhost_scsi_list, tv_tpg_list) {
98718312
AH
2025 mutex_lock(&tpg->tv_tpg_mutex);
2026 if (!tpg->tpg_nexus) {
2027 mutex_unlock(&tpg->tv_tpg_mutex);
057cbf49
NB
2028 continue;
2029 }
98718312
AH
2030 if (tpg->tv_tpg_vhost_count != 0) {
2031 mutex_unlock(&tpg->tv_tpg_mutex);
057cbf49
NB
2032 continue;
2033 }
98718312 2034 tv_tport = tpg->tport;
057cbf49 2035
67e18cf9 2036 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
ab8edab1
NB
2037 /*
2038 * In order to ensure individual vhost-scsi configfs
2039 * groups cannot be removed while in use by vhost ioctl,
2040 * go ahead and take an explicit se_tpg->tpg_group.cg_item
2041 * dependency now.
2042 */
2043 se_tpg = &tpg->se_tpg;
d588cf8f 2044 ret = target_depend_item(&se_tpg->tpg_group.cg_item);
ab8edab1 2045 if (ret) {
a691ffb4 2046 pr_warn("target_depend_item() failed: %d\n", ret);
ab8edab1 2047 mutex_unlock(&tpg->tv_tpg_mutex);
bea273c7 2048 mutex_unlock(&vhost_scsi_mutex);
25b98b64 2049 goto undepend;
ab8edab1 2050 }
98718312
AH
2051 tpg->tv_tpg_vhost_count++;
2052 tpg->vhost_scsi = vs;
2053 vs_tpg[tpg->tport_tpgt] = tpg;
67e18cf9 2054 match = true;
057cbf49 2055 }
98718312 2056 mutex_unlock(&tpg->tv_tpg_mutex);
057cbf49 2057 }
bea273c7 2058 mutex_unlock(&vhost_scsi_mutex);
67e18cf9
AH
2059
2060 if (match) {
2061 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
2062 sizeof(vs->vs_vhost_wwpn));
25b98b64 2063
f49c2226 2064 for (i = VHOST_SCSI_VQ_IO; i < vs->dev.nvqs; i++) {
25b98b64
MC
2065 vq = &vs->vqs[i].vq;
2066 if (!vhost_vq_is_setup(vq))
2067 continue;
2068
2e1139d6
ZC
2069 ret = vhost_scsi_setup_vq_cmds(vq, vq->num);
2070 if (ret)
25b98b64
MC
2071 goto destroy_vq_cmds;
2072 }
2073
f49c2226 2074 for (i = 0; i < vs->dev.nvqs; i++) {
3ab2e420 2075 vq = &vs->vqs[i].vq;
4f7f46d3 2076 mutex_lock(&vq->mutex);
247643f8 2077 vhost_vq_set_backend(vq, vs_tpg);
80f7d030 2078 vhost_vq_init_access(vq);
4f7f46d3
AH
2079 mutex_unlock(&vq->mutex);
2080 }
67e18cf9
AH
2081 ret = 0;
2082 } else {
5dd639a1
MC
2083 ret = -ENODEV;
2084 goto free_tpg;
67e18cf9
AH
2085 }
2086
4f7f46d3 2087 /*
5dd639a1
MC
2088 * Act as synchronize_rcu to make sure requests after this point
2089 * see a fully setup device.
4f7f46d3
AH
2090 */
2091 vhost_scsi_flush(vs);
4f7f46d3 2092 vs->vs_tpg = vs_tpg;
25b98b64 2093 goto out;
4f7f46d3 2094
25b98b64
MC
2095destroy_vq_cmds:
2096 for (i--; i >= VHOST_SCSI_VQ_IO; i--) {
2097 if (!vhost_vq_get_backend(&vs->vqs[i].vq))
2098 vhost_scsi_destroy_vq_cmds(&vs->vqs[i].vq);
2099 }
2100undepend:
2101 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
2102 tpg = vs_tpg[i];
2103 if (tpg) {
e508efc3
MC
2104 mutex_lock(&tpg->tv_tpg_mutex);
2105 tpg->vhost_scsi = NULL;
25b98b64 2106 tpg->tv_tpg_vhost_count--;
e508efc3 2107 mutex_unlock(&tpg->tv_tpg_mutex);
25b98b64
MC
2108 target_undepend_item(&tpg->se_tpg.tpg_group.cg_item);
2109 }
2110 }
5dd639a1 2111free_tpg:
25b98b64 2112 kfree(vs_tpg);
f2b7daf5 2113out:
67e18cf9
AH
2114 mutex_unlock(&vs->dev.mutex);
2115 return ret;
057cbf49
NB
2116}
2117
683bd967
AH
2118static int
2119vhost_scsi_clear_endpoint(struct vhost_scsi *vs,
2120 struct vhost_scsi_target *t)
057cbf49 2121{
ab8edab1 2122 struct se_portal_group *se_tpg;
1a1ff825
NB
2123 struct vhost_scsi_tport *tv_tport;
2124 struct vhost_scsi_tpg *tpg;
4f7f46d3
AH
2125 struct vhost_virtqueue *vq;
2126 bool match = false;
67e18cf9
AH
2127 int index, ret, i;
2128 u8 target;
057cbf49
NB
2129
2130 mutex_lock(&vs->dev.mutex);
2131 /* Verify that ring has been setup correctly. */
2132 for (index = 0; index < vs->dev.nvqs; ++index) {
3ab2e420 2133 if (!vhost_vq_access_ok(&vs->vqs[index].vq)) {
101998f6 2134 ret = -EFAULT;
038e0af4 2135 goto err_dev;
057cbf49
NB
2136 }
2137 }
4f7f46d3
AH
2138
2139 if (!vs->vs_tpg) {
f2b7daf5
AH
2140 ret = 0;
2141 goto err_dev;
4f7f46d3
AH
2142 }
2143
67e18cf9
AH
2144 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
2145 target = i;
98718312
AH
2146 tpg = vs->vs_tpg[target];
2147 if (!tpg)
67e18cf9
AH
2148 continue;
2149
98718312 2150 tv_tport = tpg->tport;
67e18cf9
AH
2151 if (!tv_tport) {
2152 ret = -ENODEV;
9a10cb4d 2153 goto err_dev;
67e18cf9
AH
2154 }
2155
2156 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
98718312 2157 pr_warn("tv_tport->tport_name: %s, tpg->tport_tpgt: %hu"
67e18cf9 2158 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
98718312 2159 tv_tport->tport_name, tpg->tport_tpgt,
67e18cf9
AH
2160 t->vhost_wwpn, t->vhost_tpgt);
2161 ret = -EINVAL;
9a10cb4d 2162 goto err_dev;
67e18cf9 2163 }
9a10cb4d
MC
2164 match = true;
2165 }
2166 if (!match)
2167 goto free_vs_tpg;
2168
2169 /* Prevent new cmds from starting and accessing the tpgs/sessions */
2170 for (i = 0; i < vs->dev.nvqs; i++) {
2171 vq = &vs->vqs[i].vq;
2172 mutex_lock(&vq->mutex);
2173 vhost_vq_set_backend(vq, NULL);
2174 mutex_unlock(&vq->mutex);
2175 }
2176 /* Make sure cmds are not running before tearing them down. */
2177 vhost_scsi_flush(vs);
2178
2179 for (i = 0; i < vs->dev.nvqs; i++) {
2180 vq = &vs->vqs[i].vq;
2181 vhost_scsi_destroy_vq_cmds(vq);
2182 }
2183
2184 /*
2185 * We can now release our hold on the tpg and sessions and userspace
2186 * can free them after this point.
2187 */
2188 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
2189 target = i;
2190 tpg = vs->vs_tpg[target];
2191 if (!tpg)
2192 continue;
2193
2194 mutex_lock(&tpg->tv_tpg_mutex);
2195
98718312
AH
2196 tpg->tv_tpg_vhost_count--;
2197 tpg->vhost_scsi = NULL;
67e18cf9 2198 vs->vs_tpg[target] = NULL;
9a10cb4d 2199
98718312 2200 mutex_unlock(&tpg->tv_tpg_mutex);
9a10cb4d 2201
ab8edab1 2202 se_tpg = &tpg->se_tpg;
d588cf8f 2203 target_undepend_item(&se_tpg->tpg_group.cg_item);
057cbf49 2204 }
d60146c1 2205
9a10cb4d 2206free_vs_tpg:
4f7f46d3
AH
2207 /*
2208 * Act as synchronize_rcu to make sure access to
2209 * old vs->vs_tpg is finished.
2210 */
2211 vhost_scsi_flush(vs);
2212 kfree(vs->vs_tpg);
2213 vs->vs_tpg = NULL;
5dd639a1 2214 memset(vs->vs_vhost_wwpn, 0, sizeof(vs->vs_vhost_wwpn));
a6c9af87 2215 WARN_ON(vs->vs_events_nr);
057cbf49 2216 mutex_unlock(&vs->dev.mutex);
057cbf49 2217 return 0;
101998f6 2218
038e0af4 2219err_dev:
101998f6
NB
2220 mutex_unlock(&vs->dev.mutex);
2221 return ret;
057cbf49
NB
2222}
2223
4f7f46d3
AH
2224static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
2225{
ea16c514 2226 struct vhost_virtqueue *vq;
c2c5c259 2227 bool is_log, was_log;
ea16c514
MT
2228 int i;
2229
4f7f46d3
AH
2230 if (features & ~VHOST_SCSI_FEATURES)
2231 return -EOPNOTSUPP;
2232
2233 mutex_lock(&vs->dev.mutex);
2234 if ((features & (1 << VHOST_F_LOG_ALL)) &&
2235 !vhost_log_access_ok(&vs->dev)) {
2236 mutex_unlock(&vs->dev.mutex);
2237 return -EFAULT;
2238 }
ea16c514 2239
c2c5c259
DZ
2240 if (!vs->dev.nvqs)
2241 goto out;
2242
2243 is_log = features & (1 << VHOST_F_LOG_ALL);
2244 /*
2245 * All VQs should have same feature.
2246 */
2247 was_log = vhost_has_feature(&vs->vqs[0].vq, VHOST_F_LOG_ALL);
2248
f49c2226 2249 for (i = 0; i < vs->dev.nvqs; i++) {
ea16c514
MT
2250 vq = &vs->vqs[i].vq;
2251 mutex_lock(&vq->mutex);
2252 vq->acked_features = features;
2253 mutex_unlock(&vq->mutex);
2254 }
c2c5c259
DZ
2255
2256 /*
2257 * If VHOST_F_LOG_ALL is removed, free tvc_log after
2258 * vq->acked_features is committed.
2259 */
2260 if (!is_log && was_log) {
2261 for (i = VHOST_SCSI_VQ_IO; i < vs->dev.nvqs; i++) {
2262 if (!vs->vqs[i].scsi_cmds)
2263 continue;
2264
2265 vq = &vs->vqs[i].vq;
2266 mutex_lock(&vq->mutex);
2267 vhost_scsi_destroy_vq_log(vq);
2268 mutex_unlock(&vq->mutex);
2269 }
2270 }
2271
2272out:
4f7f46d3
AH
2273 mutex_unlock(&vs->dev.mutex);
2274 return 0;
2275}
2276
057cbf49
NB
2277static int vhost_scsi_open(struct inode *inode, struct file *f)
2278{
48ae70dd 2279 struct vhost_scsi_virtqueue *svq;
c7289312 2280 struct vhost_scsi *vs;
3ab2e420 2281 struct vhost_virtqueue **vqs;
f49c2226 2282 int r = -ENOMEM, i, nvqs = vhost_scsi_max_io_vqs;
057cbf49 2283
489084dd
DZ
2284 vs = kvzalloc(sizeof(*vs), GFP_KERNEL);
2285 if (!vs)
2286 goto err_vs;
bca939d5 2287 vs->inline_sg_cnt = vhost_scsi_inline_sg_cnt;
057cbf49 2288
f49c2226
MC
2289 if (nvqs > VHOST_SCSI_MAX_IO_VQ) {
2290 pr_err("Invalid max_io_vqs of %d. Using %d.\n", nvqs,
2291 VHOST_SCSI_MAX_IO_VQ);
2292 nvqs = VHOST_SCSI_MAX_IO_VQ;
2293 } else if (nvqs == 0) {
2294 pr_err("Invalid max_io_vqs of %d. Using 1.\n", nvqs);
2295 nvqs = 1;
2296 }
2297 nvqs += VHOST_SCSI_VQ_IO;
2298
f49c2226
MC
2299 vs->old_inflight = kmalloc_array(nvqs, sizeof(*vs->old_inflight),
2300 GFP_KERNEL | __GFP_ZERO);
2301 if (!vs->old_inflight)
2302 goto err_inflight;
2303
2304 vs->vqs = kmalloc_array(nvqs, sizeof(*vs->vqs),
2305 GFP_KERNEL | __GFP_ZERO);
2306 if (!vs->vqs)
595cb754 2307 goto err_vqs;
3ab2e420 2308
f49c2226
MC
2309 vqs = kmalloc_array(nvqs, sizeof(*vqs), GFP_KERNEL);
2310 if (!vqs)
2311 goto err_local_vqs;
2312
1a1ff825 2313 vhost_work_init(&vs->vs_event_work, vhost_scsi_evt_work);
a6c9af87 2314
c7289312
AH
2315 vs->vs_events_nr = 0;
2316 vs->vs_events_missed = false;
057cbf49 2317
c7289312
AH
2318 vqs[VHOST_SCSI_VQ_CTL] = &vs->vqs[VHOST_SCSI_VQ_CTL].vq;
2319 vqs[VHOST_SCSI_VQ_EVT] = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
2320 vs->vqs[VHOST_SCSI_VQ_CTL].vq.handle_kick = vhost_scsi_ctl_handle_kick;
2321 vs->vqs[VHOST_SCSI_VQ_EVT].vq.handle_kick = vhost_scsi_evt_handle_kick;
f49c2226 2322 for (i = VHOST_SCSI_VQ_IO; i < nvqs; i++) {
48ae70dd
MC
2323 svq = &vs->vqs[i];
2324
2325 vqs[i] = &svq->vq;
2326 svq->vs = vs;
2327 init_llist_head(&svq->completion_list);
2328 vhost_work_init(&svq->completion_work,
2329 vhost_scsi_complete_cmd_work);
2330 svq->vq.handle_kick = vhost_scsi_handle_kick;
3ab2e420 2331 }
f49c2226 2332 vhost_dev_init(&vs->dev, vqs, nvqs, UIO_MAXIOV,
01fcb1cb 2333 VHOST_SCSI_WEIGHT, 0, true, NULL);
f2f0173d 2334
1a1ff825 2335 vhost_scsi_init_inflight(vs, NULL);
f2f0173d 2336
c7289312 2337 f->private_data = vs;
057cbf49 2338 return 0;
595cb754 2339
f49c2226
MC
2340err_local_vqs:
2341 kfree(vs->vqs);
595cb754 2342err_vqs:
f49c2226
MC
2343 kfree(vs->old_inflight);
2344err_inflight:
68404441 2345 kvfree(vs);
595cb754
MT
2346err_vs:
2347 return r;
057cbf49
NB
2348}
2349
2350static int vhost_scsi_release(struct inode *inode, struct file *f)
2351{
c7289312 2352 struct vhost_scsi *vs = f->private_data;
67e18cf9 2353 struct vhost_scsi_target t;
057cbf49 2354
c7289312
AH
2355 mutex_lock(&vs->dev.mutex);
2356 memcpy(t.vhost_wwpn, vs->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
2357 mutex_unlock(&vs->dev.mutex);
2358 vhost_scsi_clear_endpoint(vs, &t);
2359 vhost_dev_stop(&vs->dev);
f6f93f75 2360 vhost_dev_cleanup(&vs->dev);
c7289312 2361 kfree(vs->dev.vqs);
f49c2226
MC
2362 kfree(vs->vqs);
2363 kfree(vs->old_inflight);
68404441 2364 kvfree(vs);
057cbf49
NB
2365 return 0;
2366}
2367
683bd967
AH
2368static long
2369vhost_scsi_ioctl(struct file *f,
2370 unsigned int ioctl,
2371 unsigned long arg)
057cbf49
NB
2372{
2373 struct vhost_scsi *vs = f->private_data;
2374 struct vhost_scsi_target backend;
2375 void __user *argp = (void __user *)arg;
2376 u64 __user *featurep = argp;
11c63418
AH
2377 u32 __user *eventsp = argp;
2378 u32 events_missed;
057cbf49 2379 u64 features;
101998f6 2380 int r, abi_version = VHOST_SCSI_ABI_VERSION;
3ab2e420 2381 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
057cbf49
NB
2382
2383 switch (ioctl) {
2384 case VHOST_SCSI_SET_ENDPOINT:
2385 if (copy_from_user(&backend, argp, sizeof backend))
2386 return -EFAULT;
6de7145c
MT
2387 if (backend.reserved != 0)
2388 return -EOPNOTSUPP;
057cbf49
NB
2389
2390 return vhost_scsi_set_endpoint(vs, &backend);
2391 case VHOST_SCSI_CLEAR_ENDPOINT:
2392 if (copy_from_user(&backend, argp, sizeof backend))
2393 return -EFAULT;
6de7145c
MT
2394 if (backend.reserved != 0)
2395 return -EOPNOTSUPP;
057cbf49
NB
2396
2397 return vhost_scsi_clear_endpoint(vs, &backend);
2398 case VHOST_SCSI_GET_ABI_VERSION:
101998f6 2399 if (copy_to_user(argp, &abi_version, sizeof abi_version))
057cbf49
NB
2400 return -EFAULT;
2401 return 0;
11c63418
AH
2402 case VHOST_SCSI_SET_EVENTS_MISSED:
2403 if (get_user(events_missed, eventsp))
2404 return -EFAULT;
2405 mutex_lock(&vq->mutex);
2406 vs->vs_events_missed = events_missed;
2407 mutex_unlock(&vq->mutex);
2408 return 0;
2409 case VHOST_SCSI_GET_EVENTS_MISSED:
2410 mutex_lock(&vq->mutex);
2411 events_missed = vs->vs_events_missed;
2412 mutex_unlock(&vq->mutex);
2413 if (put_user(events_missed, eventsp))
2414 return -EFAULT;
2415 return 0;
057cbf49 2416 case VHOST_GET_FEATURES:
5dade710 2417 features = VHOST_SCSI_FEATURES;
057cbf49
NB
2418 if (copy_to_user(featurep, &features, sizeof features))
2419 return -EFAULT;
2420 return 0;
2421 case VHOST_SET_FEATURES:
2422 if (copy_from_user(&features, featurep, sizeof features))
2423 return -EFAULT;
2424 return vhost_scsi_set_features(vs, features);
d74b55e6
MC
2425 case VHOST_NEW_WORKER:
2426 case VHOST_FREE_WORKER:
2427 case VHOST_ATTACH_VRING_WORKER:
2428 case VHOST_GET_VRING_WORKER:
2429 mutex_lock(&vs->dev.mutex);
2430 r = vhost_worker_ioctl(&vs->dev, ioctl, argp);
2431 mutex_unlock(&vs->dev.mutex);
2432 return r;
057cbf49
NB
2433 default:
2434 mutex_lock(&vs->dev.mutex);
935cdee7
MT
2435 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
2436 /* TODO: flush backend after dev ioctl. */
2437 if (r == -ENOIOCTLCMD)
2438 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
057cbf49
NB
2439 mutex_unlock(&vs->dev.mutex);
2440 return r;
2441 }
2442}
2443
2444static const struct file_operations vhost_scsi_fops = {
2445 .owner = THIS_MODULE,
2446 .release = vhost_scsi_release,
2447 .unlocked_ioctl = vhost_scsi_ioctl,
407e9ef7 2448 .compat_ioctl = compat_ptr_ioctl,
057cbf49
NB
2449 .open = vhost_scsi_open,
2450 .llseek = noop_llseek,
2451};
2452
2453static struct miscdevice vhost_scsi_misc = {
2454 MISC_DYNAMIC_MINOR,
2455 "vhost-scsi",
2456 &vhost_scsi_fops,
2457};
2458
2459static int __init vhost_scsi_register(void)
2460{
2461 return misc_register(&vhost_scsi_misc);
2462}
2463
f368ed60 2464static void vhost_scsi_deregister(void)
057cbf49 2465{
f368ed60 2466 misc_deregister(&vhost_scsi_misc);
057cbf49
NB
2467}
2468
1a1ff825 2469static char *vhost_scsi_dump_proto_id(struct vhost_scsi_tport *tport)
057cbf49
NB
2470{
2471 switch (tport->tport_proto_id) {
2472 case SCSI_PROTOCOL_SAS:
2473 return "SAS";
2474 case SCSI_PROTOCOL_FCP:
2475 return "FCP";
2476 case SCSI_PROTOCOL_ISCSI:
2477 return "iSCSI";
2478 default:
2479 break;
2480 }
2481
2482 return "Unknown";
2483}
2484
683bd967 2485static void
1a1ff825 2486vhost_scsi_do_plug(struct vhost_scsi_tpg *tpg,
683bd967 2487 struct se_lun *lun, bool plug)
a6c9af87
AH
2488{
2489
2490 struct vhost_scsi *vs = tpg->vhost_scsi;
2491 struct vhost_virtqueue *vq;
2492 u32 reason;
2493
2494 if (!vs)
2495 return;
2496
a6c9af87
AH
2497 if (plug)
2498 reason = VIRTIO_SCSI_EVT_RESET_RESCAN;
2499 else
2500 reason = VIRTIO_SCSI_EVT_RESET_REMOVED;
2501
3ab2e420 2502 vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq;
a6c9af87 2503 mutex_lock(&vq->mutex);
ced9eb37
MC
2504 /*
2505 * We can't queue events if the backend has been cleared, because
2506 * we could end up queueing an event after the flush.
2507 */
2508 if (!vhost_vq_get_backend(vq))
2509 goto unlock;
2510
ea16c514 2511 if (vhost_has_feature(vq, VIRTIO_SCSI_F_HOTPLUG))
78af31cc 2512 vhost_scsi_send_evt(vs, vq, tpg, lun,
ea16c514 2513 VIRTIO_SCSI_T_TRANSPORT_RESET, reason);
ced9eb37 2514unlock:
a6c9af87 2515 mutex_unlock(&vq->mutex);
a6c9af87
AH
2516}
2517
1a1ff825 2518static void vhost_scsi_hotplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
a6c9af87 2519{
1a1ff825 2520 vhost_scsi_do_plug(tpg, lun, true);
a6c9af87
AH
2521}
2522
1a1ff825 2523static void vhost_scsi_hotunplug(struct vhost_scsi_tpg *tpg, struct se_lun *lun)
a6c9af87 2524{
1a1ff825 2525 vhost_scsi_do_plug(tpg, lun, false);
a6c9af87
AH
2526}
2527
1a1ff825 2528static int vhost_scsi_port_link(struct se_portal_group *se_tpg,
683bd967 2529 struct se_lun *lun)
057cbf49 2530{
1a1ff825
NB
2531 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2532 struct vhost_scsi_tpg, se_tpg);
057cbf49 2533
98718312
AH
2534 mutex_lock(&tpg->tv_tpg_mutex);
2535 tpg->tv_tpg_port_count++;
1a1ff825 2536 vhost_scsi_hotplug(tpg, lun);
f5ed6f9e 2537 mutex_unlock(&tpg->tv_tpg_mutex);
a6c9af87 2538
057cbf49
NB
2539 return 0;
2540}
2541
1a1ff825 2542static void vhost_scsi_port_unlink(struct se_portal_group *se_tpg,
683bd967 2543 struct se_lun *lun)
057cbf49 2544{
1a1ff825
NB
2545 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2546 struct vhost_scsi_tpg, se_tpg);
057cbf49 2547
98718312
AH
2548 mutex_lock(&tpg->tv_tpg_mutex);
2549 tpg->tv_tpg_port_count--;
1a1ff825 2550 vhost_scsi_hotunplug(tpg, lun);
f5ed6f9e 2551 mutex_unlock(&tpg->tv_tpg_mutex);
057cbf49
NB
2552}
2553
2eafd729
CH
2554static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_store(
2555 struct config_item *item, const char *page, size_t count)
b1d75fe5 2556{
2eafd729 2557 struct se_portal_group *se_tpg = attrib_to_tpg(item);
b1d75fe5
NB
2558 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2559 struct vhost_scsi_tpg, se_tpg);
2560 unsigned long val;
2561 int ret = kstrtoul(page, 0, &val);
2562
2563 if (ret) {
2564 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
2565 return ret;
2566 }
2567 if (val != 0 && val != 1 && val != 3) {
2568 pr_err("Invalid vhost_scsi fabric_prot_type: %lu\n", val);
2569 return -EINVAL;
2570 }
2571 tpg->tv_fabric_prot_type = val;
2572
2573 return count;
2574}
2575
2eafd729
CH
2576static ssize_t vhost_scsi_tpg_attrib_fabric_prot_type_show(
2577 struct config_item *item, char *page)
b1d75fe5 2578{
2eafd729 2579 struct se_portal_group *se_tpg = attrib_to_tpg(item);
b1d75fe5
NB
2580 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2581 struct vhost_scsi_tpg, se_tpg);
2582
b3d4f02e 2583 return sysfs_emit(page, "%d\n", tpg->tv_fabric_prot_type);
b1d75fe5 2584}
2eafd729
CH
2585
2586CONFIGFS_ATTR(vhost_scsi_tpg_attrib_, fabric_prot_type);
b1d75fe5
NB
2587
2588static struct configfs_attribute *vhost_scsi_tpg_attrib_attrs[] = {
2eafd729 2589 &vhost_scsi_tpg_attrib_attr_fabric_prot_type,
b1d75fe5
NB
2590 NULL,
2591};
2592
65ea7898
NB
2593static int vhost_scsi_make_nexus(struct vhost_scsi_tpg *tpg,
2594 const char *name)
2595{
65ea7898
NB
2596 struct vhost_scsi_nexus *tv_nexus;
2597
2598 mutex_lock(&tpg->tv_tpg_mutex);
2599 if (tpg->tpg_nexus) {
2600 mutex_unlock(&tpg->tv_tpg_mutex);
2601 pr_debug("tpg->tpg_nexus already exists\n");
2602 return -EEXIST;
2603 }
65ea7898 2604
473f0b15 2605 tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
65ea7898
NB
2606 if (!tv_nexus) {
2607 mutex_unlock(&tpg->tv_tpg_mutex);
2608 pr_err("Unable to allocate struct vhost_scsi_nexus\n");
2609 return -ENOMEM;
2610 }
057cbf49
NB
2611 /*
2612 * Since we are running in 'demo mode' this call with generate a
1a1ff825 2613 * struct se_node_acl for the vhost_scsi struct se_portal_group with
057cbf49
NB
2614 * the SCSI Initiator port name of the passed configfs group 'name'.
2615 */
25b98b64 2616 tv_nexus->tvn_se_sess = target_setup_session(&tpg->se_tpg, 0, 0,
65ea7898 2617 TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS,
25b98b64 2618 (unsigned char *)name, tv_nexus, NULL);
65ea7898 2619 if (IS_ERR(tv_nexus->tvn_se_sess)) {
98718312 2620 mutex_unlock(&tpg->tv_tpg_mutex);
65ea7898
NB
2621 kfree(tv_nexus);
2622 return -ENOMEM;
057cbf49 2623 }
98718312 2624 tpg->tpg_nexus = tv_nexus;
057cbf49 2625
98718312 2626 mutex_unlock(&tpg->tv_tpg_mutex);
057cbf49
NB
2627 return 0;
2628}
2629
1a1ff825 2630static int vhost_scsi_drop_nexus(struct vhost_scsi_tpg *tpg)
057cbf49
NB
2631{
2632 struct se_session *se_sess;
1a1ff825 2633 struct vhost_scsi_nexus *tv_nexus;
057cbf49
NB
2634
2635 mutex_lock(&tpg->tv_tpg_mutex);
2636 tv_nexus = tpg->tpg_nexus;
2637 if (!tv_nexus) {
2638 mutex_unlock(&tpg->tv_tpg_mutex);
2639 return -ENODEV;
2640 }
2641
2642 se_sess = tv_nexus->tvn_se_sess;
2643 if (!se_sess) {
2644 mutex_unlock(&tpg->tv_tpg_mutex);
2645 return -ENODEV;
2646 }
2647
101998f6 2648 if (tpg->tv_tpg_port_count != 0) {
057cbf49 2649 mutex_unlock(&tpg->tv_tpg_mutex);
101998f6 2650 pr_err("Unable to remove TCM_vhost I_T Nexus with"
057cbf49 2651 " active TPG port count: %d\n",
101998f6
NB
2652 tpg->tv_tpg_port_count);
2653 return -EBUSY;
057cbf49
NB
2654 }
2655
101998f6 2656 if (tpg->tv_tpg_vhost_count != 0) {
057cbf49 2657 mutex_unlock(&tpg->tv_tpg_mutex);
101998f6 2658 pr_err("Unable to remove TCM_vhost I_T Nexus with"
057cbf49 2659 " active TPG vhost count: %d\n",
101998f6
NB
2660 tpg->tv_tpg_vhost_count);
2661 return -EBUSY;
057cbf49
NB
2662 }
2663
101998f6 2664 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1a1ff825 2665 " %s Initiator Port: %s\n", vhost_scsi_dump_proto_id(tpg->tport),
057cbf49 2666 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
3aee26b4 2667
057cbf49 2668 /*
101998f6 2669 * Release the SCSI I_T Nexus to the emulated vhost Target Port
057cbf49 2670 */
25b88550 2671 target_remove_session(se_sess);
057cbf49
NB
2672 tpg->tpg_nexus = NULL;
2673 mutex_unlock(&tpg->tv_tpg_mutex);
2674
2675 kfree(tv_nexus);
2676 return 0;
2677}
2678
2eafd729 2679static ssize_t vhost_scsi_tpg_nexus_show(struct config_item *item, char *page)
057cbf49 2680{
2eafd729 2681 struct se_portal_group *se_tpg = to_tpg(item);
1a1ff825
NB
2682 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2683 struct vhost_scsi_tpg, se_tpg);
2684 struct vhost_scsi_nexus *tv_nexus;
057cbf49
NB
2685 ssize_t ret;
2686
98718312
AH
2687 mutex_lock(&tpg->tv_tpg_mutex);
2688 tv_nexus = tpg->tpg_nexus;
057cbf49 2689 if (!tv_nexus) {
98718312 2690 mutex_unlock(&tpg->tv_tpg_mutex);
057cbf49
NB
2691 return -ENODEV;
2692 }
b3d4f02e 2693 ret = sysfs_emit(page, "%s\n",
057cbf49 2694 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
98718312 2695 mutex_unlock(&tpg->tv_tpg_mutex);
057cbf49
NB
2696
2697 return ret;
2698}
2699
2eafd729
CH
2700static ssize_t vhost_scsi_tpg_nexus_store(struct config_item *item,
2701 const char *page, size_t count)
057cbf49 2702{
2eafd729 2703 struct se_portal_group *se_tpg = to_tpg(item);
1a1ff825
NB
2704 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2705 struct vhost_scsi_tpg, se_tpg);
2706 struct vhost_scsi_tport *tport_wwn = tpg->tport;
2707 unsigned char i_port[VHOST_SCSI_NAMELEN], *ptr, *port_ptr;
057cbf49
NB
2708 int ret;
2709 /*
2710 * Shutdown the active I_T nexus if 'NULL' is passed..
2711 */
2712 if (!strncmp(page, "NULL", 4)) {
1a1ff825 2713 ret = vhost_scsi_drop_nexus(tpg);
057cbf49
NB
2714 return (!ret) ? count : ret;
2715 }
2716 /*
2717 * Otherwise make sure the passed virtual Initiator port WWN matches
1a1ff825
NB
2718 * the fabric protocol_id set in vhost_scsi_make_tport(), and call
2719 * vhost_scsi_make_nexus().
057cbf49 2720 */
1a1ff825 2721 if (strlen(page) >= VHOST_SCSI_NAMELEN) {
057cbf49 2722 pr_err("Emulated NAA Sas Address: %s, exceeds"
1a1ff825 2723 " max: %d\n", page, VHOST_SCSI_NAMELEN);
057cbf49
NB
2724 return -EINVAL;
2725 }
1a1ff825 2726 snprintf(&i_port[0], VHOST_SCSI_NAMELEN, "%s", page);
057cbf49
NB
2727
2728 ptr = strstr(i_port, "naa.");
2729 if (ptr) {
2730 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
2731 pr_err("Passed SAS Initiator Port %s does not"
2732 " match target port protoid: %s\n", i_port,
1a1ff825 2733 vhost_scsi_dump_proto_id(tport_wwn));
057cbf49
NB
2734 return -EINVAL;
2735 }
2736 port_ptr = &i_port[0];
2737 goto check_newline;
2738 }
2739 ptr = strstr(i_port, "fc.");
2740 if (ptr) {
2741 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
2742 pr_err("Passed FCP Initiator Port %s does not"
2743 " match target port protoid: %s\n", i_port,
1a1ff825 2744 vhost_scsi_dump_proto_id(tport_wwn));
057cbf49
NB
2745 return -EINVAL;
2746 }
2747 port_ptr = &i_port[3]; /* Skip over "fc." */
2748 goto check_newline;
2749 }
2750 ptr = strstr(i_port, "iqn.");
2751 if (ptr) {
2752 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
2753 pr_err("Passed iSCSI Initiator Port %s does not"
2754 " match target port protoid: %s\n", i_port,
1a1ff825 2755 vhost_scsi_dump_proto_id(tport_wwn));
057cbf49
NB
2756 return -EINVAL;
2757 }
2758 port_ptr = &i_port[0];
2759 goto check_newline;
2760 }
2761 pr_err("Unable to locate prefix for emulated Initiator Port:"
2762 " %s\n", i_port);
2763 return -EINVAL;
2764 /*
2765 * Clear any trailing newline for the NAA WWN
2766 */
2767check_newline:
2768 if (i_port[strlen(i_port)-1] == '\n')
2769 i_port[strlen(i_port)-1] = '\0';
2770
1a1ff825 2771 ret = vhost_scsi_make_nexus(tpg, port_ptr);
057cbf49
NB
2772 if (ret < 0)
2773 return ret;
2774
2775 return count;
2776}
2777
2eafd729 2778CONFIGFS_ATTR(vhost_scsi_tpg_, nexus);
057cbf49 2779
1a1ff825 2780static struct configfs_attribute *vhost_scsi_tpg_attrs[] = {
2eafd729 2781 &vhost_scsi_tpg_attr_nexus,
057cbf49
NB
2782 NULL,
2783};
2784
683bd967 2785static struct se_portal_group *
aa090eab 2786vhost_scsi_make_tpg(struct se_wwn *wwn, const char *name)
057cbf49 2787{
1a1ff825
NB
2788 struct vhost_scsi_tport *tport = container_of(wwn,
2789 struct vhost_scsi_tport, tport_wwn);
057cbf49 2790
1a1ff825 2791 struct vhost_scsi_tpg *tpg;
59c816c1 2792 u16 tpgt;
057cbf49
NB
2793 int ret;
2794
2795 if (strstr(name, "tpgt_") != name)
2796 return ERR_PTR(-EINVAL);
59c816c1 2797 if (kstrtou16(name + 5, 10, &tpgt) || tpgt >= VHOST_SCSI_MAX_TARGET)
057cbf49
NB
2798 return ERR_PTR(-EINVAL);
2799
473f0b15 2800 tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
057cbf49 2801 if (!tpg) {
1a1ff825 2802 pr_err("Unable to allocate struct vhost_scsi_tpg");
057cbf49
NB
2803 return ERR_PTR(-ENOMEM);
2804 }
2805 mutex_init(&tpg->tv_tpg_mutex);
2806 INIT_LIST_HEAD(&tpg->tv_tpg_list);
2807 tpg->tport = tport;
2808 tpg->tport_tpgt = tpgt;
2809
bc0c94b1 2810 ret = core_tpg_register(wwn, &tpg->se_tpg, tport->tport_proto_id);
057cbf49
NB
2811 if (ret < 0) {
2812 kfree(tpg);
2813 return NULL;
2814 }
1a1ff825
NB
2815 mutex_lock(&vhost_scsi_mutex);
2816 list_add_tail(&tpg->tv_tpg_list, &vhost_scsi_list);
2817 mutex_unlock(&vhost_scsi_mutex);
057cbf49
NB
2818
2819 return &tpg->se_tpg;
2820}
2821
1a1ff825 2822static void vhost_scsi_drop_tpg(struct se_portal_group *se_tpg)
057cbf49 2823{
1a1ff825
NB
2824 struct vhost_scsi_tpg *tpg = container_of(se_tpg,
2825 struct vhost_scsi_tpg, se_tpg);
057cbf49 2826
1a1ff825 2827 mutex_lock(&vhost_scsi_mutex);
057cbf49 2828 list_del(&tpg->tv_tpg_list);
1a1ff825 2829 mutex_unlock(&vhost_scsi_mutex);
057cbf49 2830 /*
101998f6 2831 * Release the virtual I_T Nexus for this vhost TPG
057cbf49 2832 */
1a1ff825 2833 vhost_scsi_drop_nexus(tpg);
057cbf49
NB
2834 /*
2835 * Deregister the se_tpg from TCM..
2836 */
2837 core_tpg_deregister(se_tpg);
2838 kfree(tpg);
2839}
2840
683bd967 2841static struct se_wwn *
1a1ff825 2842vhost_scsi_make_tport(struct target_fabric_configfs *tf,
683bd967
AH
2843 struct config_group *group,
2844 const char *name)
057cbf49 2845{
1a1ff825 2846 struct vhost_scsi_tport *tport;
057cbf49
NB
2847 char *ptr;
2848 u64 wwpn = 0;
2849 int off = 0;
2850
1a1ff825 2851 /* if (vhost_scsi_parse_wwn(name, &wwpn, 1) < 0)
057cbf49
NB
2852 return ERR_PTR(-EINVAL); */
2853
473f0b15 2854 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
057cbf49 2855 if (!tport) {
1a1ff825 2856 pr_err("Unable to allocate struct vhost_scsi_tport");
057cbf49
NB
2857 return ERR_PTR(-ENOMEM);
2858 }
2859 tport->tport_wwpn = wwpn;
2860 /*
2861 * Determine the emulated Protocol Identifier and Target Port Name
2862 * based on the incoming configfs directory name.
2863 */
2864 ptr = strstr(name, "naa.");
2865 if (ptr) {
2866 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
2867 goto check_len;
2868 }
2869 ptr = strstr(name, "fc.");
2870 if (ptr) {
2871 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
2872 off = 3; /* Skip over "fc." */
2873 goto check_len;
2874 }
2875 ptr = strstr(name, "iqn.");
2876 if (ptr) {
2877 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
2878 goto check_len;
2879 }
2880
2881 pr_err("Unable to locate prefix for emulated Target Port:"
2882 " %s\n", name);
2883 kfree(tport);
2884 return ERR_PTR(-EINVAL);
2885
2886check_len:
1a1ff825 2887 if (strlen(name) >= VHOST_SCSI_NAMELEN) {
057cbf49 2888 pr_err("Emulated %s Address: %s, exceeds"
1a1ff825
NB
2889 " max: %d\n", name, vhost_scsi_dump_proto_id(tport),
2890 VHOST_SCSI_NAMELEN);
057cbf49
NB
2891 kfree(tport);
2892 return ERR_PTR(-EINVAL);
2893 }
1a1ff825 2894 snprintf(&tport->tport_name[0], VHOST_SCSI_NAMELEN, "%s", &name[off]);
057cbf49
NB
2895
2896 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1a1ff825 2897 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport), name);
057cbf49
NB
2898
2899 return &tport->tport_wwn;
2900}
2901
1a1ff825 2902static void vhost_scsi_drop_tport(struct se_wwn *wwn)
057cbf49 2903{
1a1ff825
NB
2904 struct vhost_scsi_tport *tport = container_of(wwn,
2905 struct vhost_scsi_tport, tport_wwn);
057cbf49
NB
2906
2907 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1a1ff825 2908 " %s Address: %s\n", vhost_scsi_dump_proto_id(tport),
057cbf49
NB
2909 tport->tport_name);
2910
2911 kfree(tport);
2912}
2913
683bd967 2914static ssize_t
2eafd729 2915vhost_scsi_wwn_version_show(struct config_item *item, char *page)
057cbf49 2916{
b3d4f02e 2917 return sysfs_emit(page, "TCM_VHOST fabric module %s on %s/%s"
1a1ff825 2918 "on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
057cbf49
NB
2919 utsname()->machine);
2920}
2921
2eafd729 2922CONFIGFS_ATTR_RO(vhost_scsi_wwn_, version);
057cbf49 2923
1a1ff825 2924static struct configfs_attribute *vhost_scsi_wwn_attrs[] = {
2eafd729 2925 &vhost_scsi_wwn_attr_version,
057cbf49
NB
2926 NULL,
2927};
2928
1d822a40 2929static const struct target_core_fabric_ops vhost_scsi_ops = {
9ac8928e 2930 .module = THIS_MODULE,
30c7ca93 2931 .fabric_name = "vhost",
5ae6a6a9 2932 .max_data_sg_nents = VHOST_SCSI_PREALLOC_SGLS,
1a1ff825
NB
2933 .tpg_get_wwn = vhost_scsi_get_fabric_wwn,
2934 .tpg_get_tag = vhost_scsi_get_tpgt,
1a1ff825
NB
2935 .tpg_check_demo_mode = vhost_scsi_check_true,
2936 .tpg_check_demo_mode_cache = vhost_scsi_check_true,
b1d75fe5 2937 .tpg_check_prot_fabric_only = vhost_scsi_check_prot_fabric_only,
1a1ff825 2938 .release_cmd = vhost_scsi_release_cmd,
084ed45b 2939 .check_stop_free = vhost_scsi_check_stop_free,
057cbf49 2940 .sess_get_initiator_sid = NULL,
1a1ff825 2941 .write_pending = vhost_scsi_write_pending,
1a1ff825
NB
2942 .queue_data_in = vhost_scsi_queue_data_in,
2943 .queue_status = vhost_scsi_queue_status,
2944 .queue_tm_rsp = vhost_scsi_queue_tm_rsp,
2945 .aborted_task = vhost_scsi_aborted_task,
057cbf49
NB
2946 /*
2947 * Setup callers for generic logic in target_core_fabric_configfs.c
2948 */
1a1ff825
NB
2949 .fabric_make_wwn = vhost_scsi_make_tport,
2950 .fabric_drop_wwn = vhost_scsi_drop_tport,
2951 .fabric_make_tpg = vhost_scsi_make_tpg,
2952 .fabric_drop_tpg = vhost_scsi_drop_tpg,
2953 .fabric_post_link = vhost_scsi_port_link,
2954 .fabric_pre_unlink = vhost_scsi_port_unlink,
9ac8928e
CH
2955
2956 .tfc_wwn_attrs = vhost_scsi_wwn_attrs,
2957 .tfc_tpg_base_attrs = vhost_scsi_tpg_attrs,
2958 .tfc_tpg_attrib_attrs = vhost_scsi_tpg_attrib_attrs,
194605d4
MC
2959
2960 .default_submit_type = TARGET_QUEUE_SUBMIT,
2961 .direct_submit_supp = 1,
057cbf49
NB
2962};
2963
9ac8928e 2964static int __init vhost_scsi_init(void)
057cbf49 2965{
9ac8928e 2966 int ret = -ENOMEM;
057cbf49 2967
9ac8928e 2968 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1a1ff825 2969 " on "UTS_RELEASE"\n", VHOST_SCSI_VERSION, utsname()->sysname,
057cbf49 2970 utsname()->machine);
057cbf49 2971
057cbf49
NB
2972 ret = vhost_scsi_register();
2973 if (ret < 0)
6ec29cb8 2974 goto out;
057cbf49 2975
9ac8928e 2976 ret = target_register_template(&vhost_scsi_ops);
057cbf49
NB
2977 if (ret < 0)
2978 goto out_vhost_scsi_deregister;
2979
2980 return 0;
2981
2982out_vhost_scsi_deregister:
2983 vhost_scsi_deregister();
057cbf49
NB
2984out:
2985 return ret;
2986};
2987
1a1ff825 2988static void vhost_scsi_exit(void)
057cbf49 2989{
9ac8928e 2990 target_unregister_template(&vhost_scsi_ops);
057cbf49 2991 vhost_scsi_deregister();
057cbf49
NB
2992};
2993
181c04a3
MT
2994MODULE_DESCRIPTION("VHOST_SCSI series fabric driver");
2995MODULE_ALIAS("tcm_vhost");
057cbf49 2996MODULE_LICENSE("GPL");
1a1ff825
NB
2997module_init(vhost_scsi_init);
2998module_exit(vhost_scsi_exit);