Merge remote-tracking branch 'efi/urgent' into x86/urgent
[linux-2.6/btrfs-unstable.git] / drivers / vhost / tcm_vhost.c
blob2968b4934659aab01dd626334a9ab7fc0e607383
1 /*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
4 * (C) Copyright 2010-2012 RisingTide Systems LLC.
5 * (C) Copyright 2010-2012 IBM Corp.
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
9 * Authors: Nicholas A. Bellinger <nab@risingtidesystems.com>
10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
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.
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.
22 ****************************************************************************/
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/fs.h>
38 #include <linux/miscdevice.h>
39 #include <asm/unaligned.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_tcq.h>
42 #include <target/target_core_base.h>
43 #include <target/target_core_fabric.h>
44 #include <target/target_core_fabric_configfs.h>
45 #include <target/target_core_configfs.h>
46 #include <target/configfs_macros.h>
47 #include <linux/vhost.h>
48 #include <linux/virtio_net.h> /* TODO vhost.h currently depends on this */
49 #include <linux/virtio_scsi.h>
50 #include <linux/llist.h>
51 #include <linux/bitmap.h>
53 #include "vhost.c"
54 #include "vhost.h"
55 #include "tcm_vhost.h"
57 enum {
58 VHOST_SCSI_VQ_CTL = 0,
59 VHOST_SCSI_VQ_EVT = 1,
60 VHOST_SCSI_VQ_IO = 2,
64 * VIRTIO_RING_F_EVENT_IDX seems broken. Not sure the bug is in
65 * kernel but disabling it helps.
66 * TODO: debug and remove the workaround.
68 enum {
69 VHOST_SCSI_FEATURES = VHOST_FEATURES & (~VIRTIO_RING_F_EVENT_IDX)
72 #define VHOST_SCSI_MAX_TARGET 256
73 #define VHOST_SCSI_MAX_VQ 128
75 struct vhost_scsi {
76 /* Protected by vhost_scsi->dev.mutex */
77 struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
78 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
79 bool vs_endpoint;
81 struct vhost_dev dev;
82 struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
84 struct vhost_work vs_completion_work; /* cmd completion work item */
85 struct llist_head vs_completion_list; /* cmd completion queue */
88 /* Local pointer to allocated TCM configfs fabric module */
89 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
91 static struct workqueue_struct *tcm_vhost_workqueue;
93 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
94 static DEFINE_MUTEX(tcm_vhost_mutex);
95 static LIST_HEAD(tcm_vhost_list);
97 static int iov_num_pages(struct iovec *iov)
99 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
100 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
103 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
105 return 1;
108 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
110 return 0;
113 static char *tcm_vhost_get_fabric_name(void)
115 return "vhost";
118 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
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;
124 switch (tport->tport_proto_id) {
125 case SCSI_PROTOCOL_SAS:
126 return sas_get_fabric_proto_ident(se_tpg);
127 case SCSI_PROTOCOL_FCP:
128 return fc_get_fabric_proto_ident(se_tpg);
129 case SCSI_PROTOCOL_ISCSI:
130 return iscsi_get_fabric_proto_ident(se_tpg);
131 default:
132 pr_err("Unknown tport_proto_id: 0x%02x, using"
133 " SAS emulation\n", tport->tport_proto_id);
134 break;
137 return sas_get_fabric_proto_ident(se_tpg);
140 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
142 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
143 struct tcm_vhost_tpg, se_tpg);
144 struct tcm_vhost_tport *tport = tpg->tport;
146 return &tport->tport_name[0];
149 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
151 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
152 struct tcm_vhost_tpg, se_tpg);
153 return tpg->tport_tpgt;
156 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
158 return 1;
161 static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
162 struct se_node_acl *se_nacl,
163 struct t10_pr_registration *pr_reg,
164 int *format_code,
165 unsigned char *buf)
167 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
168 struct tcm_vhost_tpg, se_tpg);
169 struct tcm_vhost_tport *tport = tpg->tport;
171 switch (tport->tport_proto_id) {
172 case SCSI_PROTOCOL_SAS:
173 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
174 format_code, buf);
175 case SCSI_PROTOCOL_FCP:
176 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
177 format_code, buf);
178 case SCSI_PROTOCOL_ISCSI:
179 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
180 format_code, buf);
181 default:
182 pr_err("Unknown tport_proto_id: 0x%02x, using"
183 " SAS emulation\n", tport->tport_proto_id);
184 break;
187 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
188 format_code, buf);
191 static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
192 struct se_node_acl *se_nacl,
193 struct t10_pr_registration *pr_reg,
194 int *format_code)
196 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
197 struct tcm_vhost_tpg, se_tpg);
198 struct tcm_vhost_tport *tport = tpg->tport;
200 switch (tport->tport_proto_id) {
201 case SCSI_PROTOCOL_SAS:
202 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
203 format_code);
204 case SCSI_PROTOCOL_FCP:
205 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
206 format_code);
207 case SCSI_PROTOCOL_ISCSI:
208 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
209 format_code);
210 default:
211 pr_err("Unknown tport_proto_id: 0x%02x, using"
212 " SAS emulation\n", tport->tport_proto_id);
213 break;
216 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
217 format_code);
220 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
221 const char *buf,
222 u32 *out_tid_len,
223 char **port_nexus_ptr)
225 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
226 struct tcm_vhost_tpg, se_tpg);
227 struct tcm_vhost_tport *tport = tpg->tport;
229 switch (tport->tport_proto_id) {
230 case SCSI_PROTOCOL_SAS:
231 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
232 port_nexus_ptr);
233 case SCSI_PROTOCOL_FCP:
234 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
235 port_nexus_ptr);
236 case SCSI_PROTOCOL_ISCSI:
237 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
238 port_nexus_ptr);
239 default:
240 pr_err("Unknown tport_proto_id: 0x%02x, using"
241 " SAS emulation\n", tport->tport_proto_id);
242 break;
245 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
246 port_nexus_ptr);
249 static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
250 struct se_portal_group *se_tpg)
252 struct tcm_vhost_nacl *nacl;
254 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
255 if (!nacl) {
256 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
257 return NULL;
260 return &nacl->se_node_acl;
263 static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
264 struct se_node_acl *se_nacl)
266 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
267 struct tcm_vhost_nacl, se_node_acl);
268 kfree(nacl);
271 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
273 return 1;
276 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
278 return;
281 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
283 return 0;
286 static void tcm_vhost_close_session(struct se_session *se_sess)
288 return;
291 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
293 return 0;
296 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
298 /* Go ahead and process the write immediately */
299 target_execute_cmd(se_cmd);
300 return 0;
303 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
305 return 0;
308 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
310 return;
313 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
315 return 0;
318 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
320 return 0;
323 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
325 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
327 llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
329 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
332 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
334 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
335 struct tcm_vhost_cmd, tvc_se_cmd);
336 vhost_scsi_complete_cmd(tv_cmd);
337 return 0;
340 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
342 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
343 struct tcm_vhost_cmd, tvc_se_cmd);
344 vhost_scsi_complete_cmd(tv_cmd);
345 return 0;
348 static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
350 return 0;
353 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
355 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
357 /* TODO locking against target/backend threads? */
358 transport_generic_free_cmd(se_cmd, 1);
360 if (tv_cmd->tvc_sgl_count) {
361 u32 i;
362 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
363 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
365 kfree(tv_cmd->tvc_sgl);
368 kfree(tv_cmd);
371 /* Fill in status and signal that we are done processing this command
373 * This is scheduled in the vhost work queue so we are called with the owner
374 * process mm and can access the vring.
376 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
378 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
379 vs_completion_work);
380 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
381 struct virtio_scsi_cmd_resp v_rsp;
382 struct tcm_vhost_cmd *tv_cmd;
383 struct llist_node *llnode;
384 struct se_cmd *se_cmd;
385 int ret, vq;
387 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
388 llnode = llist_del_all(&vs->vs_completion_list);
389 while (llnode) {
390 tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
391 tvc_completion_list);
392 llnode = llist_next(llnode);
393 se_cmd = &tv_cmd->tvc_se_cmd;
395 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
396 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
398 memset(&v_rsp, 0, sizeof(v_rsp));
399 v_rsp.resid = se_cmd->residual_count;
400 /* TODO is status_qualifier field needed? */
401 v_rsp.status = se_cmd->scsi_status;
402 v_rsp.sense_len = se_cmd->scsi_sense_length;
403 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
404 v_rsp.sense_len);
405 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
406 if (likely(ret == 0)) {
407 vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0);
408 vq = tv_cmd->tvc_vq - vs->vqs;
409 __set_bit(vq, signal);
410 } else
411 pr_err("Faulted on virtio_scsi_cmd_resp\n");
413 vhost_scsi_free_cmd(tv_cmd);
416 vq = -1;
417 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
418 < VHOST_SCSI_MAX_VQ)
419 vhost_signal(&vs->dev, &vs->vqs[vq]);
422 static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
423 struct tcm_vhost_tpg *tv_tpg,
424 struct virtio_scsi_cmd_req *v_req,
425 u32 exp_data_len,
426 int data_direction)
428 struct tcm_vhost_cmd *tv_cmd;
429 struct tcm_vhost_nexus *tv_nexus;
431 tv_nexus = tv_tpg->tpg_nexus;
432 if (!tv_nexus) {
433 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
434 return ERR_PTR(-EIO);
437 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
438 if (!tv_cmd) {
439 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
440 return ERR_PTR(-ENOMEM);
442 tv_cmd->tvc_tag = v_req->tag;
443 tv_cmd->tvc_task_attr = v_req->task_attr;
444 tv_cmd->tvc_exp_data_len = exp_data_len;
445 tv_cmd->tvc_data_direction = data_direction;
446 tv_cmd->tvc_nexus = tv_nexus;
448 return tv_cmd;
452 * Map a user memory range into a scatterlist
454 * Returns the number of scatterlist entries used or -errno on error.
456 static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
457 unsigned int sgl_count, struct iovec *iov, int write)
459 unsigned int npages = 0, pages_nr, offset, nbytes;
460 struct scatterlist *sg = sgl;
461 void __user *ptr = iov->iov_base;
462 size_t len = iov->iov_len;
463 struct page **pages;
464 int ret, i;
466 pages_nr = iov_num_pages(iov);
467 if (pages_nr > sgl_count)
468 return -ENOBUFS;
470 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
471 if (!pages)
472 return -ENOMEM;
474 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
475 /* No pages were pinned */
476 if (ret < 0)
477 goto out;
478 /* Less pages pinned than wanted */
479 if (ret != pages_nr) {
480 for (i = 0; i < ret; i++)
481 put_page(pages[i]);
482 ret = -EFAULT;
483 goto out;
486 while (len > 0) {
487 offset = (uintptr_t)ptr & ~PAGE_MASK;
488 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
489 sg_set_page(sg, pages[npages], nbytes, offset);
490 ptr += nbytes;
491 len -= nbytes;
492 sg++;
493 npages++;
496 out:
497 kfree(pages);
498 return ret;
501 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
502 struct iovec *iov, unsigned int niov, int write)
504 int ret;
505 unsigned int i;
506 u32 sgl_count;
507 struct scatterlist *sg;
510 * Find out how long sglist needs to be
512 sgl_count = 0;
513 for (i = 0; i < niov; i++)
514 sgl_count += iov_num_pages(&iov[i]);
516 /* TODO overflow checking */
518 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
519 if (!sg)
520 return -ENOMEM;
521 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
522 sg, sgl_count, !sg);
523 sg_init_table(sg, sgl_count);
525 tv_cmd->tvc_sgl = sg;
526 tv_cmd->tvc_sgl_count = sgl_count;
528 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
529 for (i = 0; i < niov; i++) {
530 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
531 if (ret < 0) {
532 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
533 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
534 kfree(tv_cmd->tvc_sgl);
535 tv_cmd->tvc_sgl = NULL;
536 tv_cmd->tvc_sgl_count = 0;
537 return ret;
540 sg += ret;
541 sgl_count -= ret;
543 return 0;
546 static void tcm_vhost_submission_work(struct work_struct *work)
548 struct tcm_vhost_cmd *tv_cmd =
549 container_of(work, struct tcm_vhost_cmd, work);
550 struct tcm_vhost_nexus *tv_nexus;
551 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
552 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
553 int rc, sg_no_bidi = 0;
555 if (tv_cmd->tvc_sgl_count) {
556 sg_ptr = tv_cmd->tvc_sgl;
557 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
558 #if 0
559 if (se_cmd->se_cmd_flags & SCF_BIDI) {
560 sg_bidi_ptr = NULL;
561 sg_no_bidi = 0;
563 #endif
564 } else {
565 sg_ptr = NULL;
567 tv_nexus = tv_cmd->tvc_nexus;
569 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
570 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
571 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
572 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
573 0, sg_ptr, tv_cmd->tvc_sgl_count,
574 sg_bidi_ptr, sg_no_bidi);
575 if (rc < 0) {
576 transport_send_check_condition_and_sense(se_cmd,
577 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
578 transport_generic_free_cmd(se_cmd, 0);
582 static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
583 struct vhost_virtqueue *vq)
585 struct virtio_scsi_cmd_req v_req;
586 struct tcm_vhost_tpg *tv_tpg;
587 struct tcm_vhost_cmd *tv_cmd;
588 u32 exp_data_len, data_first, data_num, data_direction;
589 unsigned out, in, i;
590 int head, ret;
591 u8 target;
593 /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
594 if (unlikely(!vs->vs_endpoint))
595 return;
597 mutex_lock(&vq->mutex);
598 vhost_disable_notify(&vs->dev, vq);
600 for (;;) {
601 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
602 ARRAY_SIZE(vq->iov), &out, &in,
603 NULL, NULL);
604 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
605 head, out, in);
606 /* On error, stop handling until the next kick. */
607 if (unlikely(head < 0))
608 break;
609 /* Nothing new? Wait for eventfd to tell us they refilled. */
610 if (head == vq->num) {
611 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
612 vhost_disable_notify(&vs->dev, vq);
613 continue;
615 break;
618 /* FIXME: BIDI operation */
619 if (out == 1 && in == 1) {
620 data_direction = DMA_NONE;
621 data_first = 0;
622 data_num = 0;
623 } else if (out == 1 && in > 1) {
624 data_direction = DMA_FROM_DEVICE;
625 data_first = out + 1;
626 data_num = in - 1;
627 } else if (out > 1 && in == 1) {
628 data_direction = DMA_TO_DEVICE;
629 data_first = 1;
630 data_num = out - 1;
631 } else {
632 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
633 out, in);
634 break;
638 * Check for a sane resp buffer so we can report errors to
639 * the guest.
641 if (unlikely(vq->iov[out].iov_len !=
642 sizeof(struct virtio_scsi_cmd_resp))) {
643 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
644 " bytes\n", vq->iov[out].iov_len);
645 break;
648 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
649 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
650 " bytes\n", vq->iov[0].iov_len);
651 break;
653 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
654 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
655 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
656 sizeof(v_req));
657 if (unlikely(ret)) {
658 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
659 break;
662 /* Extract the tpgt */
663 target = v_req.lun[1];
664 tv_tpg = vs->vs_tpg[target];
666 /* Target does not exist, fail the request */
667 if (unlikely(!tv_tpg)) {
668 struct virtio_scsi_cmd_resp __user *resp;
669 struct virtio_scsi_cmd_resp rsp;
671 memset(&rsp, 0, sizeof(rsp));
672 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
673 resp = vq->iov[out].iov_base;
674 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
675 if (!ret)
676 vhost_add_used_and_signal(&vs->dev,
677 vq, head, 0);
678 else
679 pr_err("Faulted on virtio_scsi_cmd_resp\n");
681 continue;
684 exp_data_len = 0;
685 for (i = 0; i < data_num; i++)
686 exp_data_len += vq->iov[data_first + i].iov_len;
688 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
689 exp_data_len, data_direction);
690 if (IS_ERR(tv_cmd)) {
691 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
692 PTR_ERR(tv_cmd));
693 break;
695 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
696 ": %d\n", tv_cmd, exp_data_len, data_direction);
698 tv_cmd->tvc_vhost = vs;
699 tv_cmd->tvc_vq = vq;
701 if (unlikely(vq->iov[out].iov_len !=
702 sizeof(struct virtio_scsi_cmd_resp))) {
703 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
704 " bytes, out: %d, in: %d\n",
705 vq->iov[out].iov_len, out, in);
706 break;
709 tv_cmd->tvc_resp = vq->iov[out].iov_base;
712 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
713 * that will be used by tcm_vhost_new_cmd_map() and down into
714 * target_setup_cmd_from_cdb()
716 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
718 * Check that the recieved CDB size does not exceeded our
719 * hardcoded max for tcm_vhost
721 /* TODO what if cdb was too small for varlen cdb header? */
722 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
723 TCM_VHOST_MAX_CDB_SIZE)) {
724 vq_err(vq, "Received SCSI CDB with command_size: %d that"
725 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
726 scsi_command_size(tv_cmd->tvc_cdb),
727 TCM_VHOST_MAX_CDB_SIZE);
728 break; /* TODO */
730 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
732 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
733 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
735 if (data_direction != DMA_NONE) {
736 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
737 &vq->iov[data_first], data_num,
738 data_direction == DMA_TO_DEVICE);
739 if (unlikely(ret)) {
740 vq_err(vq, "Failed to map iov to sgl\n");
741 break; /* TODO */
746 * Save the descriptor from vhost_get_vq_desc() to be used to
747 * complete the virtio-scsi request in TCM callback context via
748 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
750 tv_cmd->tvc_vq_desc = head;
752 * Dispatch tv_cmd descriptor for cmwq execution in process
753 * context provided by tcm_vhost_workqueue. This also ensures
754 * tv_cmd is executed on the same kworker CPU as this vhost
755 * thread to gain positive L2 cache locality effects..
757 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
758 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
761 mutex_unlock(&vq->mutex);
764 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
766 pr_debug("%s: The handling func for control queue.\n", __func__);
769 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
771 pr_debug("%s: The handling func for event queue.\n", __func__);
774 static void vhost_scsi_handle_kick(struct vhost_work *work)
776 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
777 poll.work);
778 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
780 vhost_scsi_handle_vq(vs, vq);
784 * Called from vhost_scsi_ioctl() context to walk the list of available
785 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
787 static int vhost_scsi_set_endpoint(
788 struct vhost_scsi *vs,
789 struct vhost_scsi_target *t)
791 struct tcm_vhost_tport *tv_tport;
792 struct tcm_vhost_tpg *tv_tpg;
793 bool match = false;
794 int index, ret;
796 mutex_lock(&vs->dev.mutex);
797 /* Verify that ring has been setup correctly. */
798 for (index = 0; index < vs->dev.nvqs; ++index) {
799 /* Verify that ring has been setup correctly. */
800 if (!vhost_vq_access_ok(&vs->vqs[index])) {
801 mutex_unlock(&vs->dev.mutex);
802 return -EFAULT;
806 mutex_lock(&tcm_vhost_mutex);
807 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
808 mutex_lock(&tv_tpg->tv_tpg_mutex);
809 if (!tv_tpg->tpg_nexus) {
810 mutex_unlock(&tv_tpg->tv_tpg_mutex);
811 continue;
813 if (tv_tpg->tv_tpg_vhost_count != 0) {
814 mutex_unlock(&tv_tpg->tv_tpg_mutex);
815 continue;
817 tv_tport = tv_tpg->tport;
819 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
820 if (vs->vs_tpg[tv_tpg->tport_tpgt]) {
821 mutex_unlock(&tv_tpg->tv_tpg_mutex);
822 mutex_unlock(&tcm_vhost_mutex);
823 mutex_unlock(&vs->dev.mutex);
824 return -EEXIST;
826 tv_tpg->tv_tpg_vhost_count++;
827 vs->vs_tpg[tv_tpg->tport_tpgt] = tv_tpg;
828 smp_mb__after_atomic_inc();
829 match = true;
831 mutex_unlock(&tv_tpg->tv_tpg_mutex);
833 mutex_unlock(&tcm_vhost_mutex);
835 if (match) {
836 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
837 sizeof(vs->vs_vhost_wwpn));
838 vs->vs_endpoint = true;
839 ret = 0;
840 } else {
841 ret = -EEXIST;
844 mutex_unlock(&vs->dev.mutex);
845 return ret;
848 static int vhost_scsi_clear_endpoint(
849 struct vhost_scsi *vs,
850 struct vhost_scsi_target *t)
852 struct tcm_vhost_tport *tv_tport;
853 struct tcm_vhost_tpg *tv_tpg;
854 int index, ret, i;
855 u8 target;
857 mutex_lock(&vs->dev.mutex);
858 /* Verify that ring has been setup correctly. */
859 for (index = 0; index < vs->dev.nvqs; ++index) {
860 if (!vhost_vq_access_ok(&vs->vqs[index])) {
861 ret = -EFAULT;
862 goto err_dev;
865 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
866 target = i;
868 tv_tpg = vs->vs_tpg[target];
869 if (!tv_tpg)
870 continue;
872 mutex_lock(&tv_tpg->tv_tpg_mutex);
873 tv_tport = tv_tpg->tport;
874 if (!tv_tport) {
875 ret = -ENODEV;
876 goto err_tpg;
879 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
880 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
881 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
882 tv_tport->tport_name, tv_tpg->tport_tpgt,
883 t->vhost_wwpn, t->vhost_tpgt);
884 ret = -EINVAL;
885 goto err_tpg;
887 tv_tpg->tv_tpg_vhost_count--;
888 vs->vs_tpg[target] = NULL;
889 vs->vs_endpoint = false;
890 mutex_unlock(&tv_tpg->tv_tpg_mutex);
892 mutex_unlock(&vs->dev.mutex);
893 return 0;
895 err_tpg:
896 mutex_unlock(&tv_tpg->tv_tpg_mutex);
897 err_dev:
898 mutex_unlock(&vs->dev.mutex);
899 return ret;
902 static int vhost_scsi_open(struct inode *inode, struct file *f)
904 struct vhost_scsi *s;
905 int r, i;
907 s = kzalloc(sizeof(*s), GFP_KERNEL);
908 if (!s)
909 return -ENOMEM;
911 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
913 s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
914 s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
915 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++)
916 s->vqs[i].handle_kick = vhost_scsi_handle_kick;
917 r = vhost_dev_init(&s->dev, s->vqs, VHOST_SCSI_MAX_VQ);
918 if (r < 0) {
919 kfree(s);
920 return r;
923 f->private_data = s;
924 return 0;
927 static int vhost_scsi_release(struct inode *inode, struct file *f)
929 struct vhost_scsi *s = f->private_data;
930 struct vhost_scsi_target t;
932 mutex_lock(&s->dev.mutex);
933 memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
934 mutex_unlock(&s->dev.mutex);
935 vhost_scsi_clear_endpoint(s, &t);
936 vhost_dev_stop(&s->dev);
937 vhost_dev_cleanup(&s->dev, false);
938 kfree(s);
939 return 0;
942 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
944 vhost_poll_flush(&vs->dev.vqs[index].poll);
947 static void vhost_scsi_flush(struct vhost_scsi *vs)
949 int i;
951 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
952 vhost_scsi_flush_vq(vs, i);
953 vhost_work_flush(&vs->dev, &vs->vs_completion_work);
956 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
958 if (features & ~VHOST_SCSI_FEATURES)
959 return -EOPNOTSUPP;
961 mutex_lock(&vs->dev.mutex);
962 if ((features & (1 << VHOST_F_LOG_ALL)) &&
963 !vhost_log_access_ok(&vs->dev)) {
964 mutex_unlock(&vs->dev.mutex);
965 return -EFAULT;
967 vs->dev.acked_features = features;
968 smp_wmb();
969 vhost_scsi_flush(vs);
970 mutex_unlock(&vs->dev.mutex);
971 return 0;
974 static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
975 unsigned long arg)
977 struct vhost_scsi *vs = f->private_data;
978 struct vhost_scsi_target backend;
979 void __user *argp = (void __user *)arg;
980 u64 __user *featurep = argp;
981 u64 features;
982 int r, abi_version = VHOST_SCSI_ABI_VERSION;
984 switch (ioctl) {
985 case VHOST_SCSI_SET_ENDPOINT:
986 if (copy_from_user(&backend, argp, sizeof backend))
987 return -EFAULT;
988 if (backend.reserved != 0)
989 return -EOPNOTSUPP;
991 return vhost_scsi_set_endpoint(vs, &backend);
992 case VHOST_SCSI_CLEAR_ENDPOINT:
993 if (copy_from_user(&backend, argp, sizeof backend))
994 return -EFAULT;
995 if (backend.reserved != 0)
996 return -EOPNOTSUPP;
998 return vhost_scsi_clear_endpoint(vs, &backend);
999 case VHOST_SCSI_GET_ABI_VERSION:
1000 if (copy_to_user(argp, &abi_version, sizeof abi_version))
1001 return -EFAULT;
1002 return 0;
1003 case VHOST_GET_FEATURES:
1004 features = VHOST_SCSI_FEATURES;
1005 if (copy_to_user(featurep, &features, sizeof features))
1006 return -EFAULT;
1007 return 0;
1008 case VHOST_SET_FEATURES:
1009 if (copy_from_user(&features, featurep, sizeof features))
1010 return -EFAULT;
1011 return vhost_scsi_set_features(vs, features);
1012 default:
1013 mutex_lock(&vs->dev.mutex);
1014 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1015 /* TODO: flush backend after dev ioctl. */
1016 if (r == -ENOIOCTLCMD)
1017 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1018 mutex_unlock(&vs->dev.mutex);
1019 return r;
1023 #ifdef CONFIG_COMPAT
1024 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1025 unsigned long arg)
1027 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1029 #endif
1031 static const struct file_operations vhost_scsi_fops = {
1032 .owner = THIS_MODULE,
1033 .release = vhost_scsi_release,
1034 .unlocked_ioctl = vhost_scsi_ioctl,
1035 #ifdef CONFIG_COMPAT
1036 .compat_ioctl = vhost_scsi_compat_ioctl,
1037 #endif
1038 .open = vhost_scsi_open,
1039 .llseek = noop_llseek,
1042 static struct miscdevice vhost_scsi_misc = {
1043 MISC_DYNAMIC_MINOR,
1044 "vhost-scsi",
1045 &vhost_scsi_fops,
1048 static int __init vhost_scsi_register(void)
1050 return misc_register(&vhost_scsi_misc);
1053 static int vhost_scsi_deregister(void)
1055 return misc_deregister(&vhost_scsi_misc);
1058 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1060 switch (tport->tport_proto_id) {
1061 case SCSI_PROTOCOL_SAS:
1062 return "SAS";
1063 case SCSI_PROTOCOL_FCP:
1064 return "FCP";
1065 case SCSI_PROTOCOL_ISCSI:
1066 return "iSCSI";
1067 default:
1068 break;
1071 return "Unknown";
1074 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1075 struct se_lun *lun)
1077 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1078 struct tcm_vhost_tpg, se_tpg);
1080 mutex_lock(&tv_tpg->tv_tpg_mutex);
1081 tv_tpg->tv_tpg_port_count++;
1082 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1084 return 0;
1087 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1088 struct se_lun *se_lun)
1090 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1091 struct tcm_vhost_tpg, se_tpg);
1093 mutex_lock(&tv_tpg->tv_tpg_mutex);
1094 tv_tpg->tv_tpg_port_count--;
1095 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1098 static struct se_node_acl *tcm_vhost_make_nodeacl(
1099 struct se_portal_group *se_tpg,
1100 struct config_group *group,
1101 const char *name)
1103 struct se_node_acl *se_nacl, *se_nacl_new;
1104 struct tcm_vhost_nacl *nacl;
1105 u64 wwpn = 0;
1106 u32 nexus_depth;
1108 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1109 return ERR_PTR(-EINVAL); */
1110 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1111 if (!se_nacl_new)
1112 return ERR_PTR(-ENOMEM);
1114 nexus_depth = 1;
1116 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1117 * when converting a NodeACL from demo mode -> explict
1119 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1120 name, nexus_depth);
1121 if (IS_ERR(se_nacl)) {
1122 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1123 return se_nacl;
1126 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1128 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1129 nacl->iport_wwpn = wwpn;
1131 return se_nacl;
1134 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1136 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1137 struct tcm_vhost_nacl, se_node_acl);
1138 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1139 kfree(nacl);
1142 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1143 const char *name)
1145 struct se_portal_group *se_tpg;
1146 struct tcm_vhost_nexus *tv_nexus;
1148 mutex_lock(&tv_tpg->tv_tpg_mutex);
1149 if (tv_tpg->tpg_nexus) {
1150 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1151 pr_debug("tv_tpg->tpg_nexus already exists\n");
1152 return -EEXIST;
1154 se_tpg = &tv_tpg->se_tpg;
1156 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1157 if (!tv_nexus) {
1158 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1159 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1160 return -ENOMEM;
1163 * Initialize the struct se_session pointer
1165 tv_nexus->tvn_se_sess = transport_init_session();
1166 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1167 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1168 kfree(tv_nexus);
1169 return -ENOMEM;
1172 * Since we are running in 'demo mode' this call with generate a
1173 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1174 * the SCSI Initiator port name of the passed configfs group 'name'.
1176 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1177 se_tpg, (unsigned char *)name);
1178 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1179 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1180 pr_debug("core_tpg_check_initiator_node_acl() failed"
1181 " for %s\n", name);
1182 transport_free_session(tv_nexus->tvn_se_sess);
1183 kfree(tv_nexus);
1184 return -ENOMEM;
1187 * Now register the TCM vhost virtual I_T Nexus as active with the
1188 * call to __transport_register_session()
1190 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1191 tv_nexus->tvn_se_sess, tv_nexus);
1192 tv_tpg->tpg_nexus = tv_nexus;
1194 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1195 return 0;
1198 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1200 struct se_session *se_sess;
1201 struct tcm_vhost_nexus *tv_nexus;
1203 mutex_lock(&tpg->tv_tpg_mutex);
1204 tv_nexus = tpg->tpg_nexus;
1205 if (!tv_nexus) {
1206 mutex_unlock(&tpg->tv_tpg_mutex);
1207 return -ENODEV;
1210 se_sess = tv_nexus->tvn_se_sess;
1211 if (!se_sess) {
1212 mutex_unlock(&tpg->tv_tpg_mutex);
1213 return -ENODEV;
1216 if (tpg->tv_tpg_port_count != 0) {
1217 mutex_unlock(&tpg->tv_tpg_mutex);
1218 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1219 " active TPG port count: %d\n",
1220 tpg->tv_tpg_port_count);
1221 return -EBUSY;
1224 if (tpg->tv_tpg_vhost_count != 0) {
1225 mutex_unlock(&tpg->tv_tpg_mutex);
1226 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1227 " active TPG vhost count: %d\n",
1228 tpg->tv_tpg_vhost_count);
1229 return -EBUSY;
1232 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1233 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1234 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1236 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1238 transport_deregister_session(tv_nexus->tvn_se_sess);
1239 tpg->tpg_nexus = NULL;
1240 mutex_unlock(&tpg->tv_tpg_mutex);
1242 kfree(tv_nexus);
1243 return 0;
1246 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1247 char *page)
1249 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1250 struct tcm_vhost_tpg, se_tpg);
1251 struct tcm_vhost_nexus *tv_nexus;
1252 ssize_t ret;
1254 mutex_lock(&tv_tpg->tv_tpg_mutex);
1255 tv_nexus = tv_tpg->tpg_nexus;
1256 if (!tv_nexus) {
1257 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1258 return -ENODEV;
1260 ret = snprintf(page, PAGE_SIZE, "%s\n",
1261 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1262 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1264 return ret;
1267 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1268 const char *page,
1269 size_t count)
1271 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1272 struct tcm_vhost_tpg, se_tpg);
1273 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1274 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1275 int ret;
1277 * Shutdown the active I_T nexus if 'NULL' is passed..
1279 if (!strncmp(page, "NULL", 4)) {
1280 ret = tcm_vhost_drop_nexus(tv_tpg);
1281 return (!ret) ? count : ret;
1284 * Otherwise make sure the passed virtual Initiator port WWN matches
1285 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1286 * tcm_vhost_make_nexus().
1288 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1289 pr_err("Emulated NAA Sas Address: %s, exceeds"
1290 " max: %d\n", page, TCM_VHOST_NAMELEN);
1291 return -EINVAL;
1293 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1295 ptr = strstr(i_port, "naa.");
1296 if (ptr) {
1297 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1298 pr_err("Passed SAS Initiator Port %s does not"
1299 " match target port protoid: %s\n", i_port,
1300 tcm_vhost_dump_proto_id(tport_wwn));
1301 return -EINVAL;
1303 port_ptr = &i_port[0];
1304 goto check_newline;
1306 ptr = strstr(i_port, "fc.");
1307 if (ptr) {
1308 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1309 pr_err("Passed FCP Initiator Port %s does not"
1310 " match target port protoid: %s\n", i_port,
1311 tcm_vhost_dump_proto_id(tport_wwn));
1312 return -EINVAL;
1314 port_ptr = &i_port[3]; /* Skip over "fc." */
1315 goto check_newline;
1317 ptr = strstr(i_port, "iqn.");
1318 if (ptr) {
1319 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1320 pr_err("Passed iSCSI Initiator Port %s does not"
1321 " match target port protoid: %s\n", i_port,
1322 tcm_vhost_dump_proto_id(tport_wwn));
1323 return -EINVAL;
1325 port_ptr = &i_port[0];
1326 goto check_newline;
1328 pr_err("Unable to locate prefix for emulated Initiator Port:"
1329 " %s\n", i_port);
1330 return -EINVAL;
1332 * Clear any trailing newline for the NAA WWN
1334 check_newline:
1335 if (i_port[strlen(i_port)-1] == '\n')
1336 i_port[strlen(i_port)-1] = '\0';
1338 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1339 if (ret < 0)
1340 return ret;
1342 return count;
1345 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1347 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1348 &tcm_vhost_tpg_nexus.attr,
1349 NULL,
1352 static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
1353 struct config_group *group,
1354 const char *name)
1356 struct tcm_vhost_tport *tport = container_of(wwn,
1357 struct tcm_vhost_tport, tport_wwn);
1359 struct tcm_vhost_tpg *tpg;
1360 unsigned long tpgt;
1361 int ret;
1363 if (strstr(name, "tpgt_") != name)
1364 return ERR_PTR(-EINVAL);
1365 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1366 return ERR_PTR(-EINVAL);
1368 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1369 if (!tpg) {
1370 pr_err("Unable to allocate struct tcm_vhost_tpg");
1371 return ERR_PTR(-ENOMEM);
1373 mutex_init(&tpg->tv_tpg_mutex);
1374 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1375 tpg->tport = tport;
1376 tpg->tport_tpgt = tpgt;
1378 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1379 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1380 if (ret < 0) {
1381 kfree(tpg);
1382 return NULL;
1384 mutex_lock(&tcm_vhost_mutex);
1385 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1386 mutex_unlock(&tcm_vhost_mutex);
1388 return &tpg->se_tpg;
1391 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1393 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1394 struct tcm_vhost_tpg, se_tpg);
1396 mutex_lock(&tcm_vhost_mutex);
1397 list_del(&tpg->tv_tpg_list);
1398 mutex_unlock(&tcm_vhost_mutex);
1400 * Release the virtual I_T Nexus for this vhost TPG
1402 tcm_vhost_drop_nexus(tpg);
1404 * Deregister the se_tpg from TCM..
1406 core_tpg_deregister(se_tpg);
1407 kfree(tpg);
1410 static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1411 struct config_group *group,
1412 const char *name)
1414 struct tcm_vhost_tport *tport;
1415 char *ptr;
1416 u64 wwpn = 0;
1417 int off = 0;
1419 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1420 return ERR_PTR(-EINVAL); */
1422 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1423 if (!tport) {
1424 pr_err("Unable to allocate struct tcm_vhost_tport");
1425 return ERR_PTR(-ENOMEM);
1427 tport->tport_wwpn = wwpn;
1429 * Determine the emulated Protocol Identifier and Target Port Name
1430 * based on the incoming configfs directory name.
1432 ptr = strstr(name, "naa.");
1433 if (ptr) {
1434 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1435 goto check_len;
1437 ptr = strstr(name, "fc.");
1438 if (ptr) {
1439 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1440 off = 3; /* Skip over "fc." */
1441 goto check_len;
1443 ptr = strstr(name, "iqn.");
1444 if (ptr) {
1445 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1446 goto check_len;
1449 pr_err("Unable to locate prefix for emulated Target Port:"
1450 " %s\n", name);
1451 kfree(tport);
1452 return ERR_PTR(-EINVAL);
1454 check_len:
1455 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1456 pr_err("Emulated %s Address: %s, exceeds"
1457 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1458 TCM_VHOST_NAMELEN);
1459 kfree(tport);
1460 return ERR_PTR(-EINVAL);
1462 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1464 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1465 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1467 return &tport->tport_wwn;
1470 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1472 struct tcm_vhost_tport *tport = container_of(wwn,
1473 struct tcm_vhost_tport, tport_wwn);
1475 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1476 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1477 tport->tport_name);
1479 kfree(tport);
1482 static ssize_t tcm_vhost_wwn_show_attr_version(
1483 struct target_fabric_configfs *tf,
1484 char *page)
1486 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1487 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1488 utsname()->machine);
1491 TF_WWN_ATTR_RO(tcm_vhost, version);
1493 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1494 &tcm_vhost_wwn_version.attr,
1495 NULL,
1498 static struct target_core_fabric_ops tcm_vhost_ops = {
1499 .get_fabric_name = tcm_vhost_get_fabric_name,
1500 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1501 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1502 .tpg_get_tag = tcm_vhost_get_tag,
1503 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1504 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1505 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1506 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1507 .tpg_check_demo_mode = tcm_vhost_check_true,
1508 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1509 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1510 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1511 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1512 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1513 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1514 .release_cmd = tcm_vhost_release_cmd,
1515 .shutdown_session = tcm_vhost_shutdown_session,
1516 .close_session = tcm_vhost_close_session,
1517 .sess_get_index = tcm_vhost_sess_get_index,
1518 .sess_get_initiator_sid = NULL,
1519 .write_pending = tcm_vhost_write_pending,
1520 .write_pending_status = tcm_vhost_write_pending_status,
1521 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1522 .get_task_tag = tcm_vhost_get_task_tag,
1523 .get_cmd_state = tcm_vhost_get_cmd_state,
1524 .queue_data_in = tcm_vhost_queue_data_in,
1525 .queue_status = tcm_vhost_queue_status,
1526 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
1528 * Setup callers for generic logic in target_core_fabric_configfs.c
1530 .fabric_make_wwn = tcm_vhost_make_tport,
1531 .fabric_drop_wwn = tcm_vhost_drop_tport,
1532 .fabric_make_tpg = tcm_vhost_make_tpg,
1533 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1534 .fabric_post_link = tcm_vhost_port_link,
1535 .fabric_pre_unlink = tcm_vhost_port_unlink,
1536 .fabric_make_np = NULL,
1537 .fabric_drop_np = NULL,
1538 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1539 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1542 static int tcm_vhost_register_configfs(void)
1544 struct target_fabric_configfs *fabric;
1545 int ret;
1547 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1548 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1549 utsname()->machine);
1551 * Register the top level struct config_item_type with TCM core
1553 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1554 if (IS_ERR(fabric)) {
1555 pr_err("target_fabric_configfs_init() failed\n");
1556 return PTR_ERR(fabric);
1559 * Setup fabric->tf_ops from our local tcm_vhost_ops
1561 fabric->tf_ops = tcm_vhost_ops;
1563 * Setup default attribute lists for various fabric->tf_cit_tmpl
1565 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1566 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1567 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1568 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1569 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1570 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1571 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1572 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1573 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1575 * Register the fabric for use within TCM
1577 ret = target_fabric_configfs_register(fabric);
1578 if (ret < 0) {
1579 pr_err("target_fabric_configfs_register() failed"
1580 " for TCM_VHOST\n");
1581 return ret;
1584 * Setup our local pointer to *fabric
1586 tcm_vhost_fabric_configfs = fabric;
1587 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1588 return 0;
1591 static void tcm_vhost_deregister_configfs(void)
1593 if (!tcm_vhost_fabric_configfs)
1594 return;
1596 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1597 tcm_vhost_fabric_configfs = NULL;
1598 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1601 static int __init tcm_vhost_init(void)
1603 int ret = -ENOMEM;
1605 * Use our own dedicated workqueue for submitting I/O into
1606 * target core to avoid contention within system_wq.
1608 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1609 if (!tcm_vhost_workqueue)
1610 goto out;
1612 ret = vhost_scsi_register();
1613 if (ret < 0)
1614 goto out_destroy_workqueue;
1616 ret = tcm_vhost_register_configfs();
1617 if (ret < 0)
1618 goto out_vhost_scsi_deregister;
1620 return 0;
1622 out_vhost_scsi_deregister:
1623 vhost_scsi_deregister();
1624 out_destroy_workqueue:
1625 destroy_workqueue(tcm_vhost_workqueue);
1626 out:
1627 return ret;
1630 static void tcm_vhost_exit(void)
1632 tcm_vhost_deregister_configfs();
1633 vhost_scsi_deregister();
1634 destroy_workqueue(tcm_vhost_workqueue);
1637 MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1638 MODULE_LICENSE("GPL");
1639 module_init(tcm_vhost_init);
1640 module_exit(tcm_vhost_exit);