]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/vhost/tcm_vhost.c
target: pass sense_reason as a return value
[mirror_ubuntu-zesty-kernel.git] / drivers / vhost / tcm_vhost.c
CommitLineData
057cbf49
NB
1/*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
3 *
4 * (C) Copyright 2010-2012 RisingTide Systems LLC.
5 * (C) Copyright 2010-2012 IBM Corp.
6 *
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 *
9 * Authors: Nicholas A. Bellinger <nab@risingtidesystems.com>
10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 ****************************************************************************/
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <generated/utsrelease.h>
27#include <linux/utsname.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/kthread.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/configfs.h>
34#include <linux/ctype.h>
35#include <linux/compat.h>
36#include <linux/eventfd.h>
37#include <linux/vhost.h>
38#include <linux/fs.h>
39#include <linux/miscdevice.h>
40#include <asm/unaligned.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_tcq.h>
43#include <target/target_core_base.h>
44#include <target/target_core_fabric.h>
45#include <target/target_core_fabric_configfs.h>
46#include <target/target_core_configfs.h>
47#include <target/configfs_macros.h>
48#include <linux/vhost.h>
49#include <linux/virtio_net.h> /* TODO vhost.h currently depends on this */
50#include <linux/virtio_scsi.h>
51
52#include "vhost.c"
53#include "vhost.h"
54#include "tcm_vhost.h"
55
101998f6
NB
56enum {
57 VHOST_SCSI_VQ_CTL = 0,
58 VHOST_SCSI_VQ_EVT = 1,
59 VHOST_SCSI_VQ_IO = 2,
60};
61
057cbf49 62struct vhost_scsi {
101998f6 63 struct tcm_vhost_tpg *vs_tpg; /* Protected by vhost_scsi->dev.mutex */
057cbf49
NB
64 struct vhost_dev dev;
65 struct vhost_virtqueue vqs[3];
66
67 struct vhost_work vs_completion_work; /* cmd completion work item */
68 struct list_head vs_completion_list; /* cmd completion queue */
69 spinlock_t vs_completion_lock; /* protects s_completion_list */
70};
71
72/* Local pointer to allocated TCM configfs fabric module */
73static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
74
75static struct workqueue_struct *tcm_vhost_workqueue;
76
77/* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
78static DEFINE_MUTEX(tcm_vhost_mutex);
79static LIST_HEAD(tcm_vhost_list);
80
81static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
82{
83 return 1;
84}
85
86static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
87{
88 return 0;
89}
90
91static char *tcm_vhost_get_fabric_name(void)
92{
93 return "vhost";
94}
95
96static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
97{
98 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
99 struct tcm_vhost_tpg, se_tpg);
100 struct tcm_vhost_tport *tport = tpg->tport;
101
102 switch (tport->tport_proto_id) {
103 case SCSI_PROTOCOL_SAS:
104 return sas_get_fabric_proto_ident(se_tpg);
105 case SCSI_PROTOCOL_FCP:
106 return fc_get_fabric_proto_ident(se_tpg);
107 case SCSI_PROTOCOL_ISCSI:
108 return iscsi_get_fabric_proto_ident(se_tpg);
109 default:
110 pr_err("Unknown tport_proto_id: 0x%02x, using"
111 " SAS emulation\n", tport->tport_proto_id);
112 break;
113 }
114
115 return sas_get_fabric_proto_ident(se_tpg);
116}
117
118static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
119{
120 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
121 struct tcm_vhost_tpg, se_tpg);
122 struct tcm_vhost_tport *tport = tpg->tport;
123
124 return &tport->tport_name[0];
125}
126
127static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
128{
129 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
130 struct tcm_vhost_tpg, se_tpg);
131 return tpg->tport_tpgt;
132}
133
134static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
135{
136 return 1;
137}
138
101998f6 139static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
057cbf49
NB
140 struct se_node_acl *se_nacl,
141 struct t10_pr_registration *pr_reg,
142 int *format_code,
143 unsigned char *buf)
144{
145 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
146 struct tcm_vhost_tpg, se_tpg);
147 struct tcm_vhost_tport *tport = tpg->tport;
148
149 switch (tport->tport_proto_id) {
150 case SCSI_PROTOCOL_SAS:
151 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
152 format_code, buf);
153 case SCSI_PROTOCOL_FCP:
154 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
155 format_code, buf);
156 case SCSI_PROTOCOL_ISCSI:
157 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
158 format_code, buf);
159 default:
160 pr_err("Unknown tport_proto_id: 0x%02x, using"
161 " SAS emulation\n", tport->tport_proto_id);
162 break;
163 }
164
165 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
166 format_code, buf);
167}
168
101998f6 169static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
057cbf49
NB
170 struct se_node_acl *se_nacl,
171 struct t10_pr_registration *pr_reg,
172 int *format_code)
173{
174 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
175 struct tcm_vhost_tpg, se_tpg);
176 struct tcm_vhost_tport *tport = tpg->tport;
177
178 switch (tport->tport_proto_id) {
179 case SCSI_PROTOCOL_SAS:
180 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
181 format_code);
182 case SCSI_PROTOCOL_FCP:
183 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
184 format_code);
185 case SCSI_PROTOCOL_ISCSI:
186 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
187 format_code);
188 default:
189 pr_err("Unknown tport_proto_id: 0x%02x, using"
190 " SAS emulation\n", tport->tport_proto_id);
191 break;
192 }
193
194 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
195 format_code);
196}
197
101998f6 198static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
057cbf49
NB
199 const char *buf,
200 u32 *out_tid_len,
201 char **port_nexus_ptr)
202{
203 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
204 struct tcm_vhost_tpg, se_tpg);
205 struct tcm_vhost_tport *tport = tpg->tport;
206
207 switch (tport->tport_proto_id) {
208 case SCSI_PROTOCOL_SAS:
209 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
210 port_nexus_ptr);
211 case SCSI_PROTOCOL_FCP:
212 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
213 port_nexus_ptr);
214 case SCSI_PROTOCOL_ISCSI:
215 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
216 port_nexus_ptr);
217 default:
218 pr_err("Unknown tport_proto_id: 0x%02x, using"
219 " SAS emulation\n", tport->tport_proto_id);
220 break;
221 }
222
223 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
224 port_nexus_ptr);
225}
226
227static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
228 struct se_portal_group *se_tpg)
229{
230 struct tcm_vhost_nacl *nacl;
231
232 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
233 if (!nacl) {
234 pr_err("Unable to alocate struct tcm_vhost_nacl\n");
235 return NULL;
236 }
237
238 return &nacl->se_node_acl;
239}
240
101998f6 241static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
057cbf49
NB
242 struct se_node_acl *se_nacl)
243{
244 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
245 struct tcm_vhost_nacl, se_node_acl);
246 kfree(nacl);
247}
248
249static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
250{
251 return 1;
252}
253
254static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
255{
256 return;
257}
258
259static int tcm_vhost_shutdown_session(struct se_session *se_sess)
260{
261 return 0;
262}
263
264static void tcm_vhost_close_session(struct se_session *se_sess)
265{
266 return;
267}
268
269static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
270{
271 return 0;
272}
273
274static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
275{
276 /* Go ahead and process the write immediately */
277 target_execute_cmd(se_cmd);
278 return 0;
279}
280
281static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
282{
283 return 0;
284}
285
286static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
287{
288 return;
289}
290
291static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
292{
293 return 0;
294}
295
296static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
297{
298 return 0;
299}
300
101998f6
NB
301static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
302{
303 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
304
305 spin_lock_bh(&vs->vs_completion_lock);
306 list_add_tail(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
307 spin_unlock_bh(&vs->vs_completion_lock);
308
309 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
310}
057cbf49
NB
311
312static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
313{
314 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
315 struct tcm_vhost_cmd, tvc_se_cmd);
316 vhost_scsi_complete_cmd(tv_cmd);
317 return 0;
318}
319
320static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
321{
322 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
323 struct tcm_vhost_cmd, tvc_se_cmd);
324 vhost_scsi_complete_cmd(tv_cmd);
325 return 0;
326}
327
328static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
329{
330 return 0;
331}
332
057cbf49
NB
333static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
334{
335 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
336
337 /* TODO locking against target/backend threads? */
338 transport_generic_free_cmd(se_cmd, 1);
339
340 if (tv_cmd->tvc_sgl_count) {
341 u32 i;
342 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
343 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
344
345 kfree(tv_cmd->tvc_sgl);
346 }
347
348 kfree(tv_cmd);
349}
350
351/* Dequeue a command from the completion list */
352static struct tcm_vhost_cmd *vhost_scsi_get_cmd_from_completion(
353 struct vhost_scsi *vs)
354{
355 struct tcm_vhost_cmd *tv_cmd = NULL;
356
357 spin_lock_bh(&vs->vs_completion_lock);
358 if (list_empty(&vs->vs_completion_list)) {
359 spin_unlock_bh(&vs->vs_completion_lock);
360 return NULL;
361 }
362
363 list_for_each_entry(tv_cmd, &vs->vs_completion_list,
364 tvc_completion_list) {
365 list_del(&tv_cmd->tvc_completion_list);
366 break;
367 }
368 spin_unlock_bh(&vs->vs_completion_lock);
369 return tv_cmd;
370}
371
372/* Fill in status and signal that we are done processing this command
373 *
374 * This is scheduled in the vhost work queue so we are called with the owner
375 * process mm and can access the vring.
376 */
377static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
378{
379 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
380 vs_completion_work);
381 struct tcm_vhost_cmd *tv_cmd;
382
101998f6 383 while ((tv_cmd = vhost_scsi_get_cmd_from_completion(vs))) {
057cbf49
NB
384 struct virtio_scsi_cmd_resp v_rsp;
385 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
386 int ret;
387
388 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
389 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
390
391 memset(&v_rsp, 0, sizeof(v_rsp));
392 v_rsp.resid = se_cmd->residual_count;
393 /* TODO is status_qualifier field needed? */
394 v_rsp.status = se_cmd->scsi_status;
395 v_rsp.sense_len = se_cmd->scsi_sense_length;
396 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
397 v_rsp.sense_len);
398 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
399 if (likely(ret == 0))
400 vhost_add_used(&vs->vqs[2], tv_cmd->tvc_vq_desc, 0);
401 else
402 pr_err("Faulted on virtio_scsi_cmd_resp\n");
403
404 vhost_scsi_free_cmd(tv_cmd);
405 }
406
407 vhost_signal(&vs->dev, &vs->vqs[2]);
408}
409
057cbf49
NB
410static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
411 struct tcm_vhost_tpg *tv_tpg,
412 struct virtio_scsi_cmd_req *v_req,
413 u32 exp_data_len,
414 int data_direction)
415{
416 struct tcm_vhost_cmd *tv_cmd;
417 struct tcm_vhost_nexus *tv_nexus;
057cbf49 418 struct se_session *se_sess;
057cbf49
NB
419
420 tv_nexus = tv_tpg->tpg_nexus;
421 if (!tv_nexus) {
422 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
423 return ERR_PTR(-EIO);
424 }
425 se_sess = tv_nexus->tvn_se_sess;
426
427 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
428 if (!tv_cmd) {
429 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
430 return ERR_PTR(-ENOMEM);
431 }
432 INIT_LIST_HEAD(&tv_cmd->tvc_completion_list);
433 tv_cmd->tvc_tag = v_req->tag;
9f0abc15
NB
434 tv_cmd->tvc_task_attr = v_req->task_attr;
435 tv_cmd->tvc_exp_data_len = exp_data_len;
436 tv_cmd->tvc_data_direction = data_direction;
437 tv_cmd->tvc_nexus = tv_nexus;
057cbf49 438
057cbf49
NB
439 return tv_cmd;
440}
441
442/*
443 * Map a user memory range into a scatterlist
444 *
445 * Returns the number of scatterlist entries used or -errno on error.
446 */
447static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
448 unsigned int sgl_count, void __user *ptr, size_t len, int write)
449{
450 struct scatterlist *sg = sgl;
451 unsigned int npages = 0;
452 int ret;
453
454 while (len > 0) {
455 struct page *page;
456 unsigned int offset = (uintptr_t)ptr & ~PAGE_MASK;
457 unsigned int nbytes = min_t(unsigned int,
458 PAGE_SIZE - offset, len);
459
460 if (npages == sgl_count) {
461 ret = -ENOBUFS;
462 goto err;
463 }
464
465 ret = get_user_pages_fast((unsigned long)ptr, 1, write, &page);
466 BUG_ON(ret == 0); /* we should either get our page or fail */
467 if (ret < 0)
468 goto err;
469
470 sg_set_page(sg, page, nbytes, offset);
471 ptr += nbytes;
472 len -= nbytes;
473 sg++;
474 npages++;
475 }
476 return npages;
477
478err:
479 /* Put pages that we hold */
480 for (sg = sgl; sg != &sgl[npages]; sg++)
481 put_page(sg_page(sg));
482 return ret;
483}
484
485static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
486 struct iovec *iov, unsigned int niov, int write)
487{
488 int ret;
489 unsigned int i;
490 u32 sgl_count;
491 struct scatterlist *sg;
492
493 /*
494 * Find out how long sglist needs to be
495 */
496 sgl_count = 0;
497 for (i = 0; i < niov; i++) {
498 sgl_count += (((uintptr_t)iov[i].iov_base + iov[i].iov_len +
499 PAGE_SIZE - 1) >> PAGE_SHIFT) -
500 ((uintptr_t)iov[i].iov_base >> PAGE_SHIFT);
501 }
502 /* TODO overflow checking */
503
504 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
505 if (!sg)
506 return -ENOMEM;
f0e0e9bb
FW
507 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
508 sg, sgl_count, !sg);
057cbf49
NB
509 sg_init_table(sg, sgl_count);
510
511 tv_cmd->tvc_sgl = sg;
512 tv_cmd->tvc_sgl_count = sgl_count;
513
514 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
515 for (i = 0; i < niov; i++) {
516 ret = vhost_scsi_map_to_sgl(sg, sgl_count, iov[i].iov_base,
517 iov[i].iov_len, write);
518 if (ret < 0) {
519 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
520 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
521 kfree(tv_cmd->tvc_sgl);
522 tv_cmd->tvc_sgl = NULL;
523 tv_cmd->tvc_sgl_count = 0;
524 return ret;
525 }
526
527 sg += ret;
528 sgl_count -= ret;
529 }
530 return 0;
531}
532
533static void tcm_vhost_submission_work(struct work_struct *work)
534{
535 struct tcm_vhost_cmd *tv_cmd =
536 container_of(work, struct tcm_vhost_cmd, work);
9f0abc15 537 struct tcm_vhost_nexus *tv_nexus;
057cbf49
NB
538 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
539 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
540 int rc, sg_no_bidi = 0;
057cbf49
NB
541
542 if (tv_cmd->tvc_sgl_count) {
543 sg_ptr = tv_cmd->tvc_sgl;
057cbf49
NB
544/* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
545#if 0
546 if (se_cmd->se_cmd_flags & SCF_BIDI) {
547 sg_bidi_ptr = NULL;
548 sg_no_bidi = 0;
549 }
550#endif
551 } else {
552 sg_ptr = NULL;
553 }
9f0abc15
NB
554 tv_nexus = tv_cmd->tvc_nexus;
555
556 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
557 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
558 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
559 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
560 0, sg_ptr, tv_cmd->tvc_sgl_count,
561 sg_bidi_ptr, sg_no_bidi);
057cbf49
NB
562 if (rc < 0) {
563 transport_send_check_condition_and_sense(se_cmd,
9f0abc15 564 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
057cbf49 565 transport_generic_free_cmd(se_cmd, 0);
057cbf49 566 }
057cbf49
NB
567}
568
569static void vhost_scsi_handle_vq(struct vhost_scsi *vs)
570{
571 struct vhost_virtqueue *vq = &vs->vqs[2];
572 struct virtio_scsi_cmd_req v_req;
573 struct tcm_vhost_tpg *tv_tpg;
574 struct tcm_vhost_cmd *tv_cmd;
575 u32 exp_data_len, data_first, data_num, data_direction;
576 unsigned out, in, i;
577 int head, ret;
578
579 /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
580 tv_tpg = vs->vs_tpg;
581 if (unlikely(!tv_tpg)) {
582 pr_err("%s endpoint not set\n", __func__);
583 return;
584 }
585
586 mutex_lock(&vq->mutex);
587 vhost_disable_notify(&vs->dev, vq);
588
589 for (;;) {
590 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
591 ARRAY_SIZE(vq->iov), &out, &in,
592 NULL, NULL);
593 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
594 head, out, in);
595 /* On error, stop handling until the next kick. */
596 if (unlikely(head < 0))
597 break;
598 /* Nothing new? Wait for eventfd to tell us they refilled. */
599 if (head == vq->num) {
600 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
601 vhost_disable_notify(&vs->dev, vq);
602 continue;
603 }
604 break;
605 }
606
607/* FIXME: BIDI operation */
608 if (out == 1 && in == 1) {
609 data_direction = DMA_NONE;
610 data_first = 0;
611 data_num = 0;
612 } else if (out == 1 && in > 1) {
613 data_direction = DMA_FROM_DEVICE;
614 data_first = out + 1;
615 data_num = in - 1;
616 } else if (out > 1 && in == 1) {
617 data_direction = DMA_TO_DEVICE;
618 data_first = 1;
619 data_num = out - 1;
620 } else {
621 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
622 out, in);
623 break;
624 }
625
626 /*
627 * Check for a sane resp buffer so we can report errors to
628 * the guest.
629 */
630 if (unlikely(vq->iov[out].iov_len !=
631 sizeof(struct virtio_scsi_cmd_resp))) {
632 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
633 " bytes\n", vq->iov[out].iov_len);
634 break;
635 }
636
637 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
638 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
639 " bytes\n", vq->iov[0].iov_len);
640 break;
641 }
642 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
643 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
644 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
645 sizeof(v_req));
646 if (unlikely(ret)) {
647 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
648 break;
649 }
650
651 exp_data_len = 0;
652 for (i = 0; i < data_num; i++)
653 exp_data_len += vq->iov[data_first + i].iov_len;
654
655 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
656 exp_data_len, data_direction);
657 if (IS_ERR(tv_cmd)) {
658 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
659 PTR_ERR(tv_cmd));
660 break;
661 }
662 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
663 ": %d\n", tv_cmd, exp_data_len, data_direction);
664
665 tv_cmd->tvc_vhost = vs;
666
667 if (unlikely(vq->iov[out].iov_len !=
668 sizeof(struct virtio_scsi_cmd_resp))) {
669 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
670 " bytes, out: %d, in: %d\n",
671 vq->iov[out].iov_len, out, in);
672 break;
673 }
674
675 tv_cmd->tvc_resp = vq->iov[out].iov_base;
676
677 /*
678 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
679 * that will be used by tcm_vhost_new_cmd_map() and down into
680 * target_setup_cmd_from_cdb()
681 */
682 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
683 /*
684 * Check that the recieved CDB size does not exceeded our
685 * hardcoded max for tcm_vhost
686 */
687 /* TODO what if cdb was too small for varlen cdb header? */
688 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
689 TCM_VHOST_MAX_CDB_SIZE)) {
690 vq_err(vq, "Received SCSI CDB with command_size: %d that"
691 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
692 scsi_command_size(tv_cmd->tvc_cdb),
693 TCM_VHOST_MAX_CDB_SIZE);
694 break; /* TODO */
695 }
696 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
697
698 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
699 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
700
701 if (data_direction != DMA_NONE) {
702 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
703 &vq->iov[data_first], data_num,
704 data_direction == DMA_TO_DEVICE);
705 if (unlikely(ret)) {
706 vq_err(vq, "Failed to map iov to sgl\n");
707 break; /* TODO */
708 }
709 }
710
711 /*
712 * Save the descriptor from vhost_get_vq_desc() to be used to
713 * complete the virtio-scsi request in TCM callback context via
714 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
715 */
716 tv_cmd->tvc_vq_desc = head;
717 /*
718 * Dispatch tv_cmd descriptor for cmwq execution in process
719 * context provided by tcm_vhost_workqueue. This also ensures
720 * tv_cmd is executed on the same kworker CPU as this vhost
721 * thread to gain positive L2 cache locality effects..
722 */
723 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
724 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
725 }
726
727 mutex_unlock(&vq->mutex);
728}
729
730static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
731{
101998f6 732 pr_debug("%s: The handling func for control queue.\n", __func__);
057cbf49
NB
733}
734
735static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
736{
101998f6 737 pr_debug("%s: The handling func for event queue.\n", __func__);
057cbf49
NB
738}
739
740static void vhost_scsi_handle_kick(struct vhost_work *work)
741{
742 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
743 poll.work);
744 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
745
746 vhost_scsi_handle_vq(vs);
747}
748
749/*
750 * Called from vhost_scsi_ioctl() context to walk the list of available
751 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
752 */
753static int vhost_scsi_set_endpoint(
754 struct vhost_scsi *vs,
755 struct vhost_scsi_target *t)
756{
757 struct tcm_vhost_tport *tv_tport;
758 struct tcm_vhost_tpg *tv_tpg;
759 int index;
760
761 mutex_lock(&vs->dev.mutex);
762 /* Verify that ring has been setup correctly. */
763 for (index = 0; index < vs->dev.nvqs; ++index) {
764 /* Verify that ring has been setup correctly. */
765 if (!vhost_vq_access_ok(&vs->vqs[index])) {
766 mutex_unlock(&vs->dev.mutex);
767 return -EFAULT;
768 }
769 }
057cbf49
NB
770 mutex_unlock(&vs->dev.mutex);
771
772 mutex_lock(&tcm_vhost_mutex);
773 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
774 mutex_lock(&tv_tpg->tv_tpg_mutex);
775 if (!tv_tpg->tpg_nexus) {
776 mutex_unlock(&tv_tpg->tv_tpg_mutex);
777 continue;
778 }
101998f6 779 if (tv_tpg->tv_tpg_vhost_count != 0) {
057cbf49
NB
780 mutex_unlock(&tv_tpg->tv_tpg_mutex);
781 continue;
782 }
783 tv_tport = tv_tpg->tport;
784
785 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn) &&
786 (tv_tpg->tport_tpgt == t->vhost_tpgt)) {
101998f6 787 tv_tpg->tv_tpg_vhost_count++;
057cbf49
NB
788 mutex_unlock(&tv_tpg->tv_tpg_mutex);
789 mutex_unlock(&tcm_vhost_mutex);
790
791 mutex_lock(&vs->dev.mutex);
101998f6
NB
792 if (vs->vs_tpg) {
793 mutex_unlock(&vs->dev.mutex);
794 mutex_lock(&tv_tpg->tv_tpg_mutex);
795 tv_tpg->tv_tpg_vhost_count--;
796 mutex_unlock(&tv_tpg->tv_tpg_mutex);
797 return -EEXIST;
798 }
799
057cbf49 800 vs->vs_tpg = tv_tpg;
057cbf49
NB
801 smp_mb__after_atomic_inc();
802 mutex_unlock(&vs->dev.mutex);
803 return 0;
804 }
805 mutex_unlock(&tv_tpg->tv_tpg_mutex);
806 }
807 mutex_unlock(&tcm_vhost_mutex);
808 return -EINVAL;
809}
810
811static int vhost_scsi_clear_endpoint(
812 struct vhost_scsi *vs,
813 struct vhost_scsi_target *t)
814{
815 struct tcm_vhost_tport *tv_tport;
816 struct tcm_vhost_tpg *tv_tpg;
101998f6 817 int index, ret;
057cbf49
NB
818
819 mutex_lock(&vs->dev.mutex);
820 /* Verify that ring has been setup correctly. */
821 for (index = 0; index < vs->dev.nvqs; ++index) {
822 if (!vhost_vq_access_ok(&vs->vqs[index])) {
101998f6
NB
823 ret = -EFAULT;
824 goto err;
057cbf49
NB
825 }
826 }
827
828 if (!vs->vs_tpg) {
101998f6
NB
829 ret = -ENODEV;
830 goto err;
057cbf49
NB
831 }
832 tv_tpg = vs->vs_tpg;
833 tv_tport = tv_tpg->tport;
834
835 if (strcmp(tv_tport->tport_name, t->vhost_wwpn) ||
836 (tv_tpg->tport_tpgt != t->vhost_tpgt)) {
057cbf49
NB
837 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
838 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
839 tv_tport->tport_name, tv_tpg->tport_tpgt,
840 t->vhost_wwpn, t->vhost_tpgt);
101998f6
NB
841 ret = -EINVAL;
842 goto err;
057cbf49 843 }
101998f6 844 tv_tpg->tv_tpg_vhost_count--;
057cbf49
NB
845 vs->vs_tpg = NULL;
846 mutex_unlock(&vs->dev.mutex);
847
848 return 0;
101998f6
NB
849
850err:
851 mutex_unlock(&vs->dev.mutex);
852 return ret;
057cbf49
NB
853}
854
855static int vhost_scsi_open(struct inode *inode, struct file *f)
856{
857 struct vhost_scsi *s;
858 int r;
859
860 s = kzalloc(sizeof(*s), GFP_KERNEL);
861 if (!s)
862 return -ENOMEM;
863
864 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
865 INIT_LIST_HEAD(&s->vs_completion_list);
866 spin_lock_init(&s->vs_completion_lock);
867
101998f6
NB
868 s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
869 s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
870 s->vqs[VHOST_SCSI_VQ_IO].handle_kick = vhost_scsi_handle_kick;
057cbf49
NB
871 r = vhost_dev_init(&s->dev, s->vqs, 3);
872 if (r < 0) {
873 kfree(s);
874 return r;
875 }
876
877 f->private_data = s;
878 return 0;
879}
880
881static int vhost_scsi_release(struct inode *inode, struct file *f)
882{
883 struct vhost_scsi *s = f->private_data;
884
885 if (s->vs_tpg && s->vs_tpg->tport) {
886 struct vhost_scsi_target backend;
887
888 memcpy(backend.vhost_wwpn, s->vs_tpg->tport->tport_name,
889 sizeof(backend.vhost_wwpn));
890 backend.vhost_tpgt = s->vs_tpg->tport_tpgt;
891 vhost_scsi_clear_endpoint(s, &backend);
892 }
893
894 vhost_dev_cleanup(&s->dev, false);
895 kfree(s);
896 return 0;
897}
898
101998f6
NB
899static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
900{
901 vhost_poll_flush(&vs->dev.vqs[index].poll);
902}
903
904static void vhost_scsi_flush(struct vhost_scsi *vs)
905{
906 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_CTL);
907 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_EVT);
908 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_IO);
909}
910
057cbf49
NB
911static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
912{
913 if (features & ~VHOST_FEATURES)
914 return -EOPNOTSUPP;
915
916 mutex_lock(&vs->dev.mutex);
917 if ((features & (1 << VHOST_F_LOG_ALL)) &&
918 !vhost_log_access_ok(&vs->dev)) {
919 mutex_unlock(&vs->dev.mutex);
920 return -EFAULT;
921 }
922 vs->dev.acked_features = features;
101998f6
NB
923 smp_wmb();
924 vhost_scsi_flush(vs);
057cbf49
NB
925 mutex_unlock(&vs->dev.mutex);
926 return 0;
927}
928
929static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
930 unsigned long arg)
931{
932 struct vhost_scsi *vs = f->private_data;
933 struct vhost_scsi_target backend;
934 void __user *argp = (void __user *)arg;
935 u64 __user *featurep = argp;
936 u64 features;
101998f6 937 int r, abi_version = VHOST_SCSI_ABI_VERSION;
057cbf49
NB
938
939 switch (ioctl) {
940 case VHOST_SCSI_SET_ENDPOINT:
941 if (copy_from_user(&backend, argp, sizeof backend))
942 return -EFAULT;
6de7145c
MT
943 if (backend.reserved != 0)
944 return -EOPNOTSUPP;
057cbf49
NB
945
946 return vhost_scsi_set_endpoint(vs, &backend);
947 case VHOST_SCSI_CLEAR_ENDPOINT:
948 if (copy_from_user(&backend, argp, sizeof backend))
949 return -EFAULT;
6de7145c
MT
950 if (backend.reserved != 0)
951 return -EOPNOTSUPP;
057cbf49
NB
952
953 return vhost_scsi_clear_endpoint(vs, &backend);
954 case VHOST_SCSI_GET_ABI_VERSION:
101998f6 955 if (copy_to_user(argp, &abi_version, sizeof abi_version))
057cbf49
NB
956 return -EFAULT;
957 return 0;
958 case VHOST_GET_FEATURES:
959 features = VHOST_FEATURES;
960 if (copy_to_user(featurep, &features, sizeof features))
961 return -EFAULT;
962 return 0;
963 case VHOST_SET_FEATURES:
964 if (copy_from_user(&features, featurep, sizeof features))
965 return -EFAULT;
966 return vhost_scsi_set_features(vs, features);
967 default:
968 mutex_lock(&vs->dev.mutex);
969 r = vhost_dev_ioctl(&vs->dev, ioctl, arg);
970 mutex_unlock(&vs->dev.mutex);
971 return r;
972 }
973}
974
101998f6
NB
975#ifdef CONFIG_COMPAT
976static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
977 unsigned long arg)
978{
979 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
980}
981#endif
982
057cbf49
NB
983static const struct file_operations vhost_scsi_fops = {
984 .owner = THIS_MODULE,
985 .release = vhost_scsi_release,
986 .unlocked_ioctl = vhost_scsi_ioctl,
101998f6
NB
987#ifdef CONFIG_COMPAT
988 .compat_ioctl = vhost_scsi_compat_ioctl,
989#endif
057cbf49
NB
990 .open = vhost_scsi_open,
991 .llseek = noop_llseek,
992};
993
994static struct miscdevice vhost_scsi_misc = {
995 MISC_DYNAMIC_MINOR,
996 "vhost-scsi",
997 &vhost_scsi_fops,
998};
999
1000static int __init vhost_scsi_register(void)
1001{
1002 return misc_register(&vhost_scsi_misc);
1003}
1004
1005static int vhost_scsi_deregister(void)
1006{
1007 return misc_deregister(&vhost_scsi_misc);
1008}
1009
1010static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1011{
1012 switch (tport->tport_proto_id) {
1013 case SCSI_PROTOCOL_SAS:
1014 return "SAS";
1015 case SCSI_PROTOCOL_FCP:
1016 return "FCP";
1017 case SCSI_PROTOCOL_ISCSI:
1018 return "iSCSI";
1019 default:
1020 break;
1021 }
1022
1023 return "Unknown";
1024}
1025
101998f6 1026static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
057cbf49
NB
1027 struct se_lun *lun)
1028{
1029 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1030 struct tcm_vhost_tpg, se_tpg);
1031
101998f6
NB
1032 mutex_lock(&tv_tpg->tv_tpg_mutex);
1033 tv_tpg->tv_tpg_port_count++;
1034 mutex_unlock(&tv_tpg->tv_tpg_mutex);
057cbf49
NB
1035
1036 return 0;
1037}
1038
101998f6 1039static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
057cbf49
NB
1040 struct se_lun *se_lun)
1041{
1042 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1043 struct tcm_vhost_tpg, se_tpg);
1044
101998f6
NB
1045 mutex_lock(&tv_tpg->tv_tpg_mutex);
1046 tv_tpg->tv_tpg_port_count--;
1047 mutex_unlock(&tv_tpg->tv_tpg_mutex);
057cbf49
NB
1048}
1049
1050static struct se_node_acl *tcm_vhost_make_nodeacl(
1051 struct se_portal_group *se_tpg,
1052 struct config_group *group,
1053 const char *name)
1054{
1055 struct se_node_acl *se_nacl, *se_nacl_new;
1056 struct tcm_vhost_nacl *nacl;
1057 u64 wwpn = 0;
1058 u32 nexus_depth;
1059
1060 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1061 return ERR_PTR(-EINVAL); */
1062 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1063 if (!se_nacl_new)
1064 return ERR_PTR(-ENOMEM);
1065
1066 nexus_depth = 1;
1067 /*
1068 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1069 * when converting a NodeACL from demo mode -> explict
1070 */
1071 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1072 name, nexus_depth);
1073 if (IS_ERR(se_nacl)) {
1074 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1075 return se_nacl;
1076 }
1077 /*
1078 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1079 */
1080 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1081 nacl->iport_wwpn = wwpn;
1082
1083 return se_nacl;
1084}
1085
1086static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1087{
1088 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1089 struct tcm_vhost_nacl, se_node_acl);
1090 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1091 kfree(nacl);
1092}
1093
101998f6 1094static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
057cbf49
NB
1095 const char *name)
1096{
1097 struct se_portal_group *se_tpg;
1098 struct tcm_vhost_nexus *tv_nexus;
1099
1100 mutex_lock(&tv_tpg->tv_tpg_mutex);
1101 if (tv_tpg->tpg_nexus) {
1102 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1103 pr_debug("tv_tpg->tpg_nexus already exists\n");
1104 return -EEXIST;
1105 }
1106 se_tpg = &tv_tpg->se_tpg;
1107
1108 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1109 if (!tv_nexus) {
1110 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1111 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1112 return -ENOMEM;
1113 }
1114 /*
1115 * Initialize the struct se_session pointer
1116 */
1117 tv_nexus->tvn_se_sess = transport_init_session();
1118 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1119 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1120 kfree(tv_nexus);
1121 return -ENOMEM;
1122 }
1123 /*
1124 * Since we are running in 'demo mode' this call with generate a
1125 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1126 * the SCSI Initiator port name of the passed configfs group 'name'.
1127 */
1128 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1129 se_tpg, (unsigned char *)name);
1130 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1131 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1132 pr_debug("core_tpg_check_initiator_node_acl() failed"
1133 " for %s\n", name);
1134 transport_free_session(tv_nexus->tvn_se_sess);
1135 kfree(tv_nexus);
1136 return -ENOMEM;
1137 }
1138 /*
101998f6 1139 * Now register the TCM vhost virtual I_T Nexus as active with the
057cbf49
NB
1140 * call to __transport_register_session()
1141 */
1142 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1143 tv_nexus->tvn_se_sess, tv_nexus);
1144 tv_tpg->tpg_nexus = tv_nexus;
1145
1146 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1147 return 0;
1148}
1149
101998f6 1150static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
057cbf49
NB
1151{
1152 struct se_session *se_sess;
1153 struct tcm_vhost_nexus *tv_nexus;
1154
1155 mutex_lock(&tpg->tv_tpg_mutex);
1156 tv_nexus = tpg->tpg_nexus;
1157 if (!tv_nexus) {
1158 mutex_unlock(&tpg->tv_tpg_mutex);
1159 return -ENODEV;
1160 }
1161
1162 se_sess = tv_nexus->tvn_se_sess;
1163 if (!se_sess) {
1164 mutex_unlock(&tpg->tv_tpg_mutex);
1165 return -ENODEV;
1166 }
1167
101998f6 1168 if (tpg->tv_tpg_port_count != 0) {
057cbf49 1169 mutex_unlock(&tpg->tv_tpg_mutex);
101998f6 1170 pr_err("Unable to remove TCM_vhost I_T Nexus with"
057cbf49 1171 " active TPG port count: %d\n",
101998f6
NB
1172 tpg->tv_tpg_port_count);
1173 return -EBUSY;
057cbf49
NB
1174 }
1175
101998f6 1176 if (tpg->tv_tpg_vhost_count != 0) {
057cbf49 1177 mutex_unlock(&tpg->tv_tpg_mutex);
101998f6 1178 pr_err("Unable to remove TCM_vhost I_T Nexus with"
057cbf49 1179 " active TPG vhost count: %d\n",
101998f6
NB
1180 tpg->tv_tpg_vhost_count);
1181 return -EBUSY;
057cbf49
NB
1182 }
1183
101998f6 1184 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
057cbf49
NB
1185 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1186 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1187 /*
101998f6 1188 * Release the SCSI I_T Nexus to the emulated vhost Target Port
057cbf49
NB
1189 */
1190 transport_deregister_session(tv_nexus->tvn_se_sess);
1191 tpg->tpg_nexus = NULL;
1192 mutex_unlock(&tpg->tv_tpg_mutex);
1193
1194 kfree(tv_nexus);
1195 return 0;
1196}
1197
101998f6 1198static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
057cbf49
NB
1199 char *page)
1200{
1201 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1202 struct tcm_vhost_tpg, se_tpg);
1203 struct tcm_vhost_nexus *tv_nexus;
1204 ssize_t ret;
1205
1206 mutex_lock(&tv_tpg->tv_tpg_mutex);
1207 tv_nexus = tv_tpg->tpg_nexus;
1208 if (!tv_nexus) {
1209 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1210 return -ENODEV;
1211 }
1212 ret = snprintf(page, PAGE_SIZE, "%s\n",
1213 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1214 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1215
1216 return ret;
1217}
1218
101998f6 1219static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
057cbf49
NB
1220 const char *page,
1221 size_t count)
1222{
1223 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1224 struct tcm_vhost_tpg, se_tpg);
1225 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1226 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1227 int ret;
1228 /*
1229 * Shutdown the active I_T nexus if 'NULL' is passed..
1230 */
1231 if (!strncmp(page, "NULL", 4)) {
1232 ret = tcm_vhost_drop_nexus(tv_tpg);
1233 return (!ret) ? count : ret;
1234 }
1235 /*
1236 * Otherwise make sure the passed virtual Initiator port WWN matches
1237 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1238 * tcm_vhost_make_nexus().
1239 */
1240 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1241 pr_err("Emulated NAA Sas Address: %s, exceeds"
1242 " max: %d\n", page, TCM_VHOST_NAMELEN);
1243 return -EINVAL;
1244 }
1245 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1246
1247 ptr = strstr(i_port, "naa.");
1248 if (ptr) {
1249 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1250 pr_err("Passed SAS Initiator Port %s does not"
1251 " match target port protoid: %s\n", i_port,
1252 tcm_vhost_dump_proto_id(tport_wwn));
1253 return -EINVAL;
1254 }
1255 port_ptr = &i_port[0];
1256 goto check_newline;
1257 }
1258 ptr = strstr(i_port, "fc.");
1259 if (ptr) {
1260 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1261 pr_err("Passed FCP Initiator Port %s does not"
1262 " match target port protoid: %s\n", i_port,
1263 tcm_vhost_dump_proto_id(tport_wwn));
1264 return -EINVAL;
1265 }
1266 port_ptr = &i_port[3]; /* Skip over "fc." */
1267 goto check_newline;
1268 }
1269 ptr = strstr(i_port, "iqn.");
1270 if (ptr) {
1271 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1272 pr_err("Passed iSCSI Initiator Port %s does not"
1273 " match target port protoid: %s\n", i_port,
1274 tcm_vhost_dump_proto_id(tport_wwn));
1275 return -EINVAL;
1276 }
1277 port_ptr = &i_port[0];
1278 goto check_newline;
1279 }
1280 pr_err("Unable to locate prefix for emulated Initiator Port:"
1281 " %s\n", i_port);
1282 return -EINVAL;
1283 /*
1284 * Clear any trailing newline for the NAA WWN
1285 */
1286check_newline:
1287 if (i_port[strlen(i_port)-1] == '\n')
1288 i_port[strlen(i_port)-1] = '\0';
1289
1290 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1291 if (ret < 0)
1292 return ret;
1293
1294 return count;
1295}
1296
1297TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1298
1299static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1300 &tcm_vhost_tpg_nexus.attr,
1301 NULL,
1302};
1303
101998f6 1304static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
057cbf49
NB
1305 struct config_group *group,
1306 const char *name)
1307{
1308 struct tcm_vhost_tport *tport = container_of(wwn,
1309 struct tcm_vhost_tport, tport_wwn);
1310
1311 struct tcm_vhost_tpg *tpg;
1312 unsigned long tpgt;
1313 int ret;
1314
1315 if (strstr(name, "tpgt_") != name)
1316 return ERR_PTR(-EINVAL);
1317 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1318 return ERR_PTR(-EINVAL);
1319
1320 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1321 if (!tpg) {
1322 pr_err("Unable to allocate struct tcm_vhost_tpg");
1323 return ERR_PTR(-ENOMEM);
1324 }
1325 mutex_init(&tpg->tv_tpg_mutex);
1326 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1327 tpg->tport = tport;
1328 tpg->tport_tpgt = tpgt;
1329
1330 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1331 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1332 if (ret < 0) {
1333 kfree(tpg);
1334 return NULL;
1335 }
1336 mutex_lock(&tcm_vhost_mutex);
1337 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1338 mutex_unlock(&tcm_vhost_mutex);
1339
1340 return &tpg->se_tpg;
1341}
1342
1343static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1344{
1345 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1346 struct tcm_vhost_tpg, se_tpg);
1347
1348 mutex_lock(&tcm_vhost_mutex);
1349 list_del(&tpg->tv_tpg_list);
1350 mutex_unlock(&tcm_vhost_mutex);
1351 /*
101998f6 1352 * Release the virtual I_T Nexus for this vhost TPG
057cbf49
NB
1353 */
1354 tcm_vhost_drop_nexus(tpg);
1355 /*
1356 * Deregister the se_tpg from TCM..
1357 */
1358 core_tpg_deregister(se_tpg);
1359 kfree(tpg);
1360}
1361
101998f6 1362static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
057cbf49
NB
1363 struct config_group *group,
1364 const char *name)
1365{
1366 struct tcm_vhost_tport *tport;
1367 char *ptr;
1368 u64 wwpn = 0;
1369 int off = 0;
1370
1371 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1372 return ERR_PTR(-EINVAL); */
1373
1374 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1375 if (!tport) {
1376 pr_err("Unable to allocate struct tcm_vhost_tport");
1377 return ERR_PTR(-ENOMEM);
1378 }
1379 tport->tport_wwpn = wwpn;
1380 /*
1381 * Determine the emulated Protocol Identifier and Target Port Name
1382 * based on the incoming configfs directory name.
1383 */
1384 ptr = strstr(name, "naa.");
1385 if (ptr) {
1386 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1387 goto check_len;
1388 }
1389 ptr = strstr(name, "fc.");
1390 if (ptr) {
1391 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1392 off = 3; /* Skip over "fc." */
1393 goto check_len;
1394 }
1395 ptr = strstr(name, "iqn.");
1396 if (ptr) {
1397 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1398 goto check_len;
1399 }
1400
1401 pr_err("Unable to locate prefix for emulated Target Port:"
1402 " %s\n", name);
1403 kfree(tport);
1404 return ERR_PTR(-EINVAL);
1405
1406check_len:
1407 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1408 pr_err("Emulated %s Address: %s, exceeds"
1409 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1410 TCM_VHOST_NAMELEN);
1411 kfree(tport);
1412 return ERR_PTR(-EINVAL);
1413 }
1414 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1415
1416 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1417 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1418
1419 return &tport->tport_wwn;
1420}
1421
1422static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1423{
1424 struct tcm_vhost_tport *tport = container_of(wwn,
1425 struct tcm_vhost_tport, tport_wwn);
1426
1427 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1428 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1429 tport->tport_name);
1430
1431 kfree(tport);
1432}
1433
1434static ssize_t tcm_vhost_wwn_show_attr_version(
1435 struct target_fabric_configfs *tf,
1436 char *page)
1437{
1438 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1439 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1440 utsname()->machine);
1441}
1442
1443TF_WWN_ATTR_RO(tcm_vhost, version);
1444
1445static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1446 &tcm_vhost_wwn_version.attr,
1447 NULL,
1448};
1449
1450static struct target_core_fabric_ops tcm_vhost_ops = {
1451 .get_fabric_name = tcm_vhost_get_fabric_name,
1452 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1453 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1454 .tpg_get_tag = tcm_vhost_get_tag,
1455 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1456 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1457 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1458 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1459 .tpg_check_demo_mode = tcm_vhost_check_true,
1460 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1461 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1462 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1463 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1464 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1465 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1466 .release_cmd = tcm_vhost_release_cmd,
1467 .shutdown_session = tcm_vhost_shutdown_session,
1468 .close_session = tcm_vhost_close_session,
1469 .sess_get_index = tcm_vhost_sess_get_index,
1470 .sess_get_initiator_sid = NULL,
1471 .write_pending = tcm_vhost_write_pending,
1472 .write_pending_status = tcm_vhost_write_pending_status,
1473 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1474 .get_task_tag = tcm_vhost_get_task_tag,
1475 .get_cmd_state = tcm_vhost_get_cmd_state,
1476 .queue_data_in = tcm_vhost_queue_data_in,
1477 .queue_status = tcm_vhost_queue_status,
1478 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
057cbf49
NB
1479 /*
1480 * Setup callers for generic logic in target_core_fabric_configfs.c
1481 */
1482 .fabric_make_wwn = tcm_vhost_make_tport,
1483 .fabric_drop_wwn = tcm_vhost_drop_tport,
1484 .fabric_make_tpg = tcm_vhost_make_tpg,
1485 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1486 .fabric_post_link = tcm_vhost_port_link,
1487 .fabric_pre_unlink = tcm_vhost_port_unlink,
1488 .fabric_make_np = NULL,
1489 .fabric_drop_np = NULL,
1490 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1491 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1492};
1493
1494static int tcm_vhost_register_configfs(void)
1495{
1496 struct target_fabric_configfs *fabric;
1497 int ret;
1498
1499 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1500 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1501 utsname()->machine);
1502 /*
1503 * Register the top level struct config_item_type with TCM core
1504 */
1505 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1506 if (IS_ERR(fabric)) {
1507 pr_err("target_fabric_configfs_init() failed\n");
1508 return PTR_ERR(fabric);
1509 }
1510 /*
1511 * Setup fabric->tf_ops from our local tcm_vhost_ops
1512 */
1513 fabric->tf_ops = tcm_vhost_ops;
1514 /*
1515 * Setup default attribute lists for various fabric->tf_cit_tmpl
1516 */
1517 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1518 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1519 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1520 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1521 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1522 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1523 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1524 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1525 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1526 /*
1527 * Register the fabric for use within TCM
1528 */
1529 ret = target_fabric_configfs_register(fabric);
1530 if (ret < 0) {
1531 pr_err("target_fabric_configfs_register() failed"
1532 " for TCM_VHOST\n");
1533 return ret;
1534 }
1535 /*
1536 * Setup our local pointer to *fabric
1537 */
1538 tcm_vhost_fabric_configfs = fabric;
1539 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1540 return 0;
1541};
1542
1543static void tcm_vhost_deregister_configfs(void)
1544{
1545 if (!tcm_vhost_fabric_configfs)
1546 return;
1547
1548 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1549 tcm_vhost_fabric_configfs = NULL;
1550 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1551};
1552
1553static int __init tcm_vhost_init(void)
1554{
1555 int ret = -ENOMEM;
101998f6
NB
1556 /*
1557 * Use our own dedicated workqueue for submitting I/O into
1558 * target core to avoid contention within system_wq.
1559 */
057cbf49
NB
1560 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1561 if (!tcm_vhost_workqueue)
1562 goto out;
1563
1564 ret = vhost_scsi_register();
1565 if (ret < 0)
1566 goto out_destroy_workqueue;
1567
1568 ret = tcm_vhost_register_configfs();
1569 if (ret < 0)
1570 goto out_vhost_scsi_deregister;
1571
1572 return 0;
1573
1574out_vhost_scsi_deregister:
1575 vhost_scsi_deregister();
1576out_destroy_workqueue:
1577 destroy_workqueue(tcm_vhost_workqueue);
1578out:
1579 return ret;
1580};
1581
1582static void tcm_vhost_exit(void)
1583{
1584 tcm_vhost_deregister_configfs();
1585 vhost_scsi_deregister();
1586 destroy_workqueue(tcm_vhost_workqueue);
1587};
1588
1589MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1590MODULE_LICENSE("GPL");
1591module_init(tcm_vhost_init);
1592module_exit(tcm_vhost_exit);