Merge git://git.infradead.org/users/willy/linux-nvme
[linux-2.6/cjktty.git] / drivers / vhost / tcm_vhost.c
blob9951297b24279105aa118c1e9bba1c0320972a15
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,
63 #define VHOST_SCSI_MAX_TARGET 256
64 #define VHOST_SCSI_MAX_VQ 128
66 struct vhost_scsi {
67 /* Protected by vhost_scsi->dev.mutex */
68 struct tcm_vhost_tpg *vs_tpg[VHOST_SCSI_MAX_TARGET];
69 char vs_vhost_wwpn[TRANSPORT_IQN_LEN];
70 bool vs_endpoint;
72 struct vhost_dev dev;
73 struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
75 struct vhost_work vs_completion_work; /* cmd completion work item */
76 struct llist_head vs_completion_list; /* cmd completion queue */
79 /* Local pointer to allocated TCM configfs fabric module */
80 static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
82 static struct workqueue_struct *tcm_vhost_workqueue;
84 /* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
85 static DEFINE_MUTEX(tcm_vhost_mutex);
86 static LIST_HEAD(tcm_vhost_list);
88 static int iov_num_pages(struct iovec *iov)
90 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
91 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
94 static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
96 return 1;
99 static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
101 return 0;
104 static char *tcm_vhost_get_fabric_name(void)
106 return "vhost";
109 static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
111 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
112 struct tcm_vhost_tpg, se_tpg);
113 struct tcm_vhost_tport *tport = tpg->tport;
115 switch (tport->tport_proto_id) {
116 case SCSI_PROTOCOL_SAS:
117 return sas_get_fabric_proto_ident(se_tpg);
118 case SCSI_PROTOCOL_FCP:
119 return fc_get_fabric_proto_ident(se_tpg);
120 case SCSI_PROTOCOL_ISCSI:
121 return iscsi_get_fabric_proto_ident(se_tpg);
122 default:
123 pr_err("Unknown tport_proto_id: 0x%02x, using"
124 " SAS emulation\n", tport->tport_proto_id);
125 break;
128 return sas_get_fabric_proto_ident(se_tpg);
131 static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
133 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
134 struct tcm_vhost_tpg, se_tpg);
135 struct tcm_vhost_tport *tport = tpg->tport;
137 return &tport->tport_name[0];
140 static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
142 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
143 struct tcm_vhost_tpg, se_tpg);
144 return tpg->tport_tpgt;
147 static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
149 return 1;
152 static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
153 struct se_node_acl *se_nacl,
154 struct t10_pr_registration *pr_reg,
155 int *format_code,
156 unsigned char *buf)
158 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
159 struct tcm_vhost_tpg, se_tpg);
160 struct tcm_vhost_tport *tport = tpg->tport;
162 switch (tport->tport_proto_id) {
163 case SCSI_PROTOCOL_SAS:
164 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
165 format_code, buf);
166 case SCSI_PROTOCOL_FCP:
167 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
168 format_code, buf);
169 case SCSI_PROTOCOL_ISCSI:
170 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
171 format_code, buf);
172 default:
173 pr_err("Unknown tport_proto_id: 0x%02x, using"
174 " SAS emulation\n", tport->tport_proto_id);
175 break;
178 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
179 format_code, buf);
182 static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
183 struct se_node_acl *se_nacl,
184 struct t10_pr_registration *pr_reg,
185 int *format_code)
187 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
188 struct tcm_vhost_tpg, se_tpg);
189 struct tcm_vhost_tport *tport = tpg->tport;
191 switch (tport->tport_proto_id) {
192 case SCSI_PROTOCOL_SAS:
193 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
194 format_code);
195 case SCSI_PROTOCOL_FCP:
196 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
197 format_code);
198 case SCSI_PROTOCOL_ISCSI:
199 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
200 format_code);
201 default:
202 pr_err("Unknown tport_proto_id: 0x%02x, using"
203 " SAS emulation\n", tport->tport_proto_id);
204 break;
207 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
208 format_code);
211 static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
212 const char *buf,
213 u32 *out_tid_len,
214 char **port_nexus_ptr)
216 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
217 struct tcm_vhost_tpg, se_tpg);
218 struct tcm_vhost_tport *tport = tpg->tport;
220 switch (tport->tport_proto_id) {
221 case SCSI_PROTOCOL_SAS:
222 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
223 port_nexus_ptr);
224 case SCSI_PROTOCOL_FCP:
225 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
226 port_nexus_ptr);
227 case SCSI_PROTOCOL_ISCSI:
228 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
229 port_nexus_ptr);
230 default:
231 pr_err("Unknown tport_proto_id: 0x%02x, using"
232 " SAS emulation\n", tport->tport_proto_id);
233 break;
236 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
237 port_nexus_ptr);
240 static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
241 struct se_portal_group *se_tpg)
243 struct tcm_vhost_nacl *nacl;
245 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
246 if (!nacl) {
247 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
248 return NULL;
251 return &nacl->se_node_acl;
254 static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
255 struct se_node_acl *se_nacl)
257 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
258 struct tcm_vhost_nacl, se_node_acl);
259 kfree(nacl);
262 static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
264 return 1;
267 static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
269 return;
272 static int tcm_vhost_shutdown_session(struct se_session *se_sess)
274 return 0;
277 static void tcm_vhost_close_session(struct se_session *se_sess)
279 return;
282 static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
284 return 0;
287 static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
289 /* Go ahead and process the write immediately */
290 target_execute_cmd(se_cmd);
291 return 0;
294 static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
296 return 0;
299 static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
301 return;
304 static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
306 return 0;
309 static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
311 return 0;
314 static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
316 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
318 llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
320 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
323 static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
325 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
326 struct tcm_vhost_cmd, tvc_se_cmd);
327 vhost_scsi_complete_cmd(tv_cmd);
328 return 0;
331 static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
333 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
334 struct tcm_vhost_cmd, tvc_se_cmd);
335 vhost_scsi_complete_cmd(tv_cmd);
336 return 0;
339 static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
341 return 0;
344 static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
346 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
348 /* TODO locking against target/backend threads? */
349 transport_generic_free_cmd(se_cmd, 1);
351 if (tv_cmd->tvc_sgl_count) {
352 u32 i;
353 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
354 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
356 kfree(tv_cmd->tvc_sgl);
359 kfree(tv_cmd);
362 /* Fill in status and signal that we are done processing this command
364 * This is scheduled in the vhost work queue so we are called with the owner
365 * process mm and can access the vring.
367 static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
369 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
370 vs_completion_work);
371 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
372 struct virtio_scsi_cmd_resp v_rsp;
373 struct tcm_vhost_cmd *tv_cmd;
374 struct llist_node *llnode;
375 struct se_cmd *se_cmd;
376 int ret, vq;
378 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
379 llnode = llist_del_all(&vs->vs_completion_list);
380 while (llnode) {
381 tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
382 tvc_completion_list);
383 llnode = llist_next(llnode);
384 se_cmd = &tv_cmd->tvc_se_cmd;
386 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
387 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
389 memset(&v_rsp, 0, sizeof(v_rsp));
390 v_rsp.resid = se_cmd->residual_count;
391 /* TODO is status_qualifier field needed? */
392 v_rsp.status = se_cmd->scsi_status;
393 v_rsp.sense_len = se_cmd->scsi_sense_length;
394 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
395 v_rsp.sense_len);
396 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
397 if (likely(ret == 0)) {
398 vhost_add_used(tv_cmd->tvc_vq, tv_cmd->tvc_vq_desc, 0);
399 vq = tv_cmd->tvc_vq - vs->vqs;
400 __set_bit(vq, signal);
401 } else
402 pr_err("Faulted on virtio_scsi_cmd_resp\n");
404 vhost_scsi_free_cmd(tv_cmd);
407 vq = -1;
408 while ((vq = find_next_bit(signal, VHOST_SCSI_MAX_VQ, vq + 1))
409 < VHOST_SCSI_MAX_VQ)
410 vhost_signal(&vs->dev, &vs->vqs[vq]);
413 static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
414 struct tcm_vhost_tpg *tv_tpg,
415 struct virtio_scsi_cmd_req *v_req,
416 u32 exp_data_len,
417 int data_direction)
419 struct tcm_vhost_cmd *tv_cmd;
420 struct tcm_vhost_nexus *tv_nexus;
422 tv_nexus = tv_tpg->tpg_nexus;
423 if (!tv_nexus) {
424 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
425 return ERR_PTR(-EIO);
428 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
429 if (!tv_cmd) {
430 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
431 return ERR_PTR(-ENOMEM);
433 tv_cmd->tvc_tag = v_req->tag;
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;
439 return tv_cmd;
443 * Map a user memory range into a scatterlist
445 * Returns the number of scatterlist entries used or -errno on error.
447 static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
448 unsigned int sgl_count, struct iovec *iov, int write)
450 unsigned int npages = 0, pages_nr, offset, nbytes;
451 struct scatterlist *sg = sgl;
452 void __user *ptr = iov->iov_base;
453 size_t len = iov->iov_len;
454 struct page **pages;
455 int ret, i;
457 pages_nr = iov_num_pages(iov);
458 if (pages_nr > sgl_count)
459 return -ENOBUFS;
461 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
462 if (!pages)
463 return -ENOMEM;
465 ret = get_user_pages_fast((unsigned long)ptr, pages_nr, write, pages);
466 /* No pages were pinned */
467 if (ret < 0)
468 goto out;
469 /* Less pages pinned than wanted */
470 if (ret != pages_nr) {
471 for (i = 0; i < ret; i++)
472 put_page(pages[i]);
473 ret = -EFAULT;
474 goto out;
477 while (len > 0) {
478 offset = (uintptr_t)ptr & ~PAGE_MASK;
479 nbytes = min_t(unsigned int, PAGE_SIZE - offset, len);
480 sg_set_page(sg, pages[npages], nbytes, offset);
481 ptr += nbytes;
482 len -= nbytes;
483 sg++;
484 npages++;
487 out:
488 kfree(pages);
489 return ret;
492 static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
493 struct iovec *iov, unsigned int niov, int write)
495 int ret;
496 unsigned int i;
497 u32 sgl_count;
498 struct scatterlist *sg;
501 * Find out how long sglist needs to be
503 sgl_count = 0;
504 for (i = 0; i < niov; i++)
505 sgl_count += iov_num_pages(&iov[i]);
507 /* TODO overflow checking */
509 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
510 if (!sg)
511 return -ENOMEM;
512 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
513 sg, sgl_count, !sg);
514 sg_init_table(sg, sgl_count);
516 tv_cmd->tvc_sgl = sg;
517 tv_cmd->tvc_sgl_count = sgl_count;
519 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
520 for (i = 0; i < niov; i++) {
521 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
522 if (ret < 0) {
523 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
524 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
525 kfree(tv_cmd->tvc_sgl);
526 tv_cmd->tvc_sgl = NULL;
527 tv_cmd->tvc_sgl_count = 0;
528 return ret;
531 sg += ret;
532 sgl_count -= ret;
534 return 0;
537 static void tcm_vhost_submission_work(struct work_struct *work)
539 struct tcm_vhost_cmd *tv_cmd =
540 container_of(work, struct tcm_vhost_cmd, work);
541 struct tcm_vhost_nexus *tv_nexus;
542 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
543 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
544 int rc, sg_no_bidi = 0;
546 if (tv_cmd->tvc_sgl_count) {
547 sg_ptr = tv_cmd->tvc_sgl;
548 /* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
549 #if 0
550 if (se_cmd->se_cmd_flags & SCF_BIDI) {
551 sg_bidi_ptr = NULL;
552 sg_no_bidi = 0;
554 #endif
555 } else {
556 sg_ptr = NULL;
558 tv_nexus = tv_cmd->tvc_nexus;
560 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
561 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
562 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
563 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
564 0, sg_ptr, tv_cmd->tvc_sgl_count,
565 sg_bidi_ptr, sg_no_bidi);
566 if (rc < 0) {
567 transport_send_check_condition_and_sense(se_cmd,
568 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
569 transport_generic_free_cmd(se_cmd, 0);
573 static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
574 struct vhost_virtqueue *vq)
576 struct virtio_scsi_cmd_req v_req;
577 struct tcm_vhost_tpg *tv_tpg;
578 struct tcm_vhost_cmd *tv_cmd;
579 u32 exp_data_len, data_first, data_num, data_direction;
580 unsigned out, in, i;
581 int head, ret;
582 u8 target;
584 /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
585 if (unlikely(!vs->vs_endpoint))
586 return;
588 mutex_lock(&vq->mutex);
589 vhost_disable_notify(&vs->dev, vq);
591 for (;;) {
592 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
593 ARRAY_SIZE(vq->iov), &out, &in,
594 NULL, NULL);
595 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
596 head, out, in);
597 /* On error, stop handling until the next kick. */
598 if (unlikely(head < 0))
599 break;
600 /* Nothing new? Wait for eventfd to tell us they refilled. */
601 if (head == vq->num) {
602 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
603 vhost_disable_notify(&vs->dev, vq);
604 continue;
606 break;
609 /* FIXME: BIDI operation */
610 if (out == 1 && in == 1) {
611 data_direction = DMA_NONE;
612 data_first = 0;
613 data_num = 0;
614 } else if (out == 1 && in > 1) {
615 data_direction = DMA_FROM_DEVICE;
616 data_first = out + 1;
617 data_num = in - 1;
618 } else if (out > 1 && in == 1) {
619 data_direction = DMA_TO_DEVICE;
620 data_first = 1;
621 data_num = out - 1;
622 } else {
623 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
624 out, in);
625 break;
629 * Check for a sane resp buffer so we can report errors to
630 * the guest.
632 if (unlikely(vq->iov[out].iov_len !=
633 sizeof(struct virtio_scsi_cmd_resp))) {
634 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
635 " bytes\n", vq->iov[out].iov_len);
636 break;
639 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
640 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
641 " bytes\n", vq->iov[0].iov_len);
642 break;
644 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
645 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
646 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
647 sizeof(v_req));
648 if (unlikely(ret)) {
649 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
650 break;
653 /* Extract the tpgt */
654 target = v_req.lun[1];
655 tv_tpg = vs->vs_tpg[target];
657 /* Target does not exist, fail the request */
658 if (unlikely(!tv_tpg)) {
659 struct virtio_scsi_cmd_resp __user *resp;
660 struct virtio_scsi_cmd_resp rsp;
662 memset(&rsp, 0, sizeof(rsp));
663 rsp.response = VIRTIO_SCSI_S_BAD_TARGET;
664 resp = vq->iov[out].iov_base;
665 ret = __copy_to_user(resp, &rsp, sizeof(rsp));
666 if (!ret)
667 vhost_add_used_and_signal(&vs->dev,
668 vq, head, 0);
669 else
670 pr_err("Faulted on virtio_scsi_cmd_resp\n");
672 continue;
675 exp_data_len = 0;
676 for (i = 0; i < data_num; i++)
677 exp_data_len += vq->iov[data_first + i].iov_len;
679 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
680 exp_data_len, data_direction);
681 if (IS_ERR(tv_cmd)) {
682 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
683 PTR_ERR(tv_cmd));
684 break;
686 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
687 ": %d\n", tv_cmd, exp_data_len, data_direction);
689 tv_cmd->tvc_vhost = vs;
690 tv_cmd->tvc_vq = vq;
692 if (unlikely(vq->iov[out].iov_len !=
693 sizeof(struct virtio_scsi_cmd_resp))) {
694 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
695 " bytes, out: %d, in: %d\n",
696 vq->iov[out].iov_len, out, in);
697 break;
700 tv_cmd->tvc_resp = vq->iov[out].iov_base;
703 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
704 * that will be used by tcm_vhost_new_cmd_map() and down into
705 * target_setup_cmd_from_cdb()
707 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
709 * Check that the recieved CDB size does not exceeded our
710 * hardcoded max for tcm_vhost
712 /* TODO what if cdb was too small for varlen cdb header? */
713 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
714 TCM_VHOST_MAX_CDB_SIZE)) {
715 vq_err(vq, "Received SCSI CDB with command_size: %d that"
716 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
717 scsi_command_size(tv_cmd->tvc_cdb),
718 TCM_VHOST_MAX_CDB_SIZE);
719 break; /* TODO */
721 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
723 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
724 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
726 if (data_direction != DMA_NONE) {
727 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
728 &vq->iov[data_first], data_num,
729 data_direction == DMA_TO_DEVICE);
730 if (unlikely(ret)) {
731 vq_err(vq, "Failed to map iov to sgl\n");
732 break; /* TODO */
737 * Save the descriptor from vhost_get_vq_desc() to be used to
738 * complete the virtio-scsi request in TCM callback context via
739 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
741 tv_cmd->tvc_vq_desc = head;
743 * Dispatch tv_cmd descriptor for cmwq execution in process
744 * context provided by tcm_vhost_workqueue. This also ensures
745 * tv_cmd is executed on the same kworker CPU as this vhost
746 * thread to gain positive L2 cache locality effects..
748 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
749 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
752 mutex_unlock(&vq->mutex);
755 static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
757 pr_debug("%s: The handling func for control queue.\n", __func__);
760 static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
762 pr_debug("%s: The handling func for event queue.\n", __func__);
765 static void vhost_scsi_handle_kick(struct vhost_work *work)
767 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
768 poll.work);
769 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
771 vhost_scsi_handle_vq(vs, vq);
775 * Called from vhost_scsi_ioctl() context to walk the list of available
776 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
778 static int vhost_scsi_set_endpoint(
779 struct vhost_scsi *vs,
780 struct vhost_scsi_target *t)
782 struct tcm_vhost_tport *tv_tport;
783 struct tcm_vhost_tpg *tv_tpg;
784 bool match = false;
785 int index, ret;
787 mutex_lock(&vs->dev.mutex);
788 /* Verify that ring has been setup correctly. */
789 for (index = 0; index < vs->dev.nvqs; ++index) {
790 /* Verify that ring has been setup correctly. */
791 if (!vhost_vq_access_ok(&vs->vqs[index])) {
792 mutex_unlock(&vs->dev.mutex);
793 return -EFAULT;
797 mutex_lock(&tcm_vhost_mutex);
798 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
799 mutex_lock(&tv_tpg->tv_tpg_mutex);
800 if (!tv_tpg->tpg_nexus) {
801 mutex_unlock(&tv_tpg->tv_tpg_mutex);
802 continue;
804 if (tv_tpg->tv_tpg_vhost_count != 0) {
805 mutex_unlock(&tv_tpg->tv_tpg_mutex);
806 continue;
808 tv_tport = tv_tpg->tport;
810 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
811 if (vs->vs_tpg[tv_tpg->tport_tpgt]) {
812 mutex_unlock(&tv_tpg->tv_tpg_mutex);
813 mutex_unlock(&tcm_vhost_mutex);
814 mutex_unlock(&vs->dev.mutex);
815 return -EEXIST;
817 tv_tpg->tv_tpg_vhost_count++;
818 vs->vs_tpg[tv_tpg->tport_tpgt] = tv_tpg;
819 smp_mb__after_atomic_inc();
820 match = true;
822 mutex_unlock(&tv_tpg->tv_tpg_mutex);
824 mutex_unlock(&tcm_vhost_mutex);
826 if (match) {
827 memcpy(vs->vs_vhost_wwpn, t->vhost_wwpn,
828 sizeof(vs->vs_vhost_wwpn));
829 vs->vs_endpoint = true;
830 ret = 0;
831 } else {
832 ret = -EEXIST;
835 mutex_unlock(&vs->dev.mutex);
836 return ret;
839 static int vhost_scsi_clear_endpoint(
840 struct vhost_scsi *vs,
841 struct vhost_scsi_target *t)
843 struct tcm_vhost_tport *tv_tport;
844 struct tcm_vhost_tpg *tv_tpg;
845 int index, ret, i;
846 u8 target;
848 mutex_lock(&vs->dev.mutex);
849 /* Verify that ring has been setup correctly. */
850 for (index = 0; index < vs->dev.nvqs; ++index) {
851 if (!vhost_vq_access_ok(&vs->vqs[index])) {
852 ret = -EFAULT;
853 goto err;
856 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
857 target = i;
859 tv_tpg = vs->vs_tpg[target];
860 if (!tv_tpg)
861 continue;
863 tv_tport = tv_tpg->tport;
864 if (!tv_tport) {
865 ret = -ENODEV;
866 goto err;
869 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
870 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
871 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
872 tv_tport->tport_name, tv_tpg->tport_tpgt,
873 t->vhost_wwpn, t->vhost_tpgt);
874 ret = -EINVAL;
875 goto err;
877 tv_tpg->tv_tpg_vhost_count--;
878 vs->vs_tpg[target] = NULL;
879 vs->vs_endpoint = false;
881 mutex_unlock(&vs->dev.mutex);
882 return 0;
884 err:
885 mutex_unlock(&vs->dev.mutex);
886 return ret;
889 static int vhost_scsi_open(struct inode *inode, struct file *f)
891 struct vhost_scsi *s;
892 int r, i;
894 s = kzalloc(sizeof(*s), GFP_KERNEL);
895 if (!s)
896 return -ENOMEM;
898 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
900 s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
901 s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
902 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++)
903 s->vqs[i].handle_kick = vhost_scsi_handle_kick;
904 r = vhost_dev_init(&s->dev, s->vqs, VHOST_SCSI_MAX_VQ);
905 if (r < 0) {
906 kfree(s);
907 return r;
910 f->private_data = s;
911 return 0;
914 static int vhost_scsi_release(struct inode *inode, struct file *f)
916 struct vhost_scsi *s = f->private_data;
917 struct vhost_scsi_target t;
919 mutex_lock(&s->dev.mutex);
920 memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
921 mutex_unlock(&s->dev.mutex);
922 vhost_scsi_clear_endpoint(s, &t);
923 vhost_dev_stop(&s->dev);
924 vhost_dev_cleanup(&s->dev, false);
925 kfree(s);
926 return 0;
929 static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
931 vhost_poll_flush(&vs->dev.vqs[index].poll);
934 static void vhost_scsi_flush(struct vhost_scsi *vs)
936 int i;
938 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
939 vhost_scsi_flush_vq(vs, i);
942 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
944 if (features & ~VHOST_FEATURES)
945 return -EOPNOTSUPP;
947 mutex_lock(&vs->dev.mutex);
948 if ((features & (1 << VHOST_F_LOG_ALL)) &&
949 !vhost_log_access_ok(&vs->dev)) {
950 mutex_unlock(&vs->dev.mutex);
951 return -EFAULT;
953 vs->dev.acked_features = features;
954 smp_wmb();
955 vhost_scsi_flush(vs);
956 mutex_unlock(&vs->dev.mutex);
957 return 0;
960 static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
961 unsigned long arg)
963 struct vhost_scsi *vs = f->private_data;
964 struct vhost_scsi_target backend;
965 void __user *argp = (void __user *)arg;
966 u64 __user *featurep = argp;
967 u64 features;
968 int r, abi_version = VHOST_SCSI_ABI_VERSION;
970 switch (ioctl) {
971 case VHOST_SCSI_SET_ENDPOINT:
972 if (copy_from_user(&backend, argp, sizeof backend))
973 return -EFAULT;
974 if (backend.reserved != 0)
975 return -EOPNOTSUPP;
977 return vhost_scsi_set_endpoint(vs, &backend);
978 case VHOST_SCSI_CLEAR_ENDPOINT:
979 if (copy_from_user(&backend, argp, sizeof backend))
980 return -EFAULT;
981 if (backend.reserved != 0)
982 return -EOPNOTSUPP;
984 return vhost_scsi_clear_endpoint(vs, &backend);
985 case VHOST_SCSI_GET_ABI_VERSION:
986 if (copy_to_user(argp, &abi_version, sizeof abi_version))
987 return -EFAULT;
988 return 0;
989 case VHOST_GET_FEATURES:
990 features = VHOST_FEATURES;
991 if (copy_to_user(featurep, &features, sizeof features))
992 return -EFAULT;
993 return 0;
994 case VHOST_SET_FEATURES:
995 if (copy_from_user(&features, featurep, sizeof features))
996 return -EFAULT;
997 return vhost_scsi_set_features(vs, features);
998 default:
999 mutex_lock(&vs->dev.mutex);
1000 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1001 /* TODO: flush backend after dev ioctl. */
1002 if (r == -ENOIOCTLCMD)
1003 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
1004 mutex_unlock(&vs->dev.mutex);
1005 return r;
1009 #ifdef CONFIG_COMPAT
1010 static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1011 unsigned long arg)
1013 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1015 #endif
1017 static const struct file_operations vhost_scsi_fops = {
1018 .owner = THIS_MODULE,
1019 .release = vhost_scsi_release,
1020 .unlocked_ioctl = vhost_scsi_ioctl,
1021 #ifdef CONFIG_COMPAT
1022 .compat_ioctl = vhost_scsi_compat_ioctl,
1023 #endif
1024 .open = vhost_scsi_open,
1025 .llseek = noop_llseek,
1028 static struct miscdevice vhost_scsi_misc = {
1029 MISC_DYNAMIC_MINOR,
1030 "vhost-scsi",
1031 &vhost_scsi_fops,
1034 static int __init vhost_scsi_register(void)
1036 return misc_register(&vhost_scsi_misc);
1039 static int vhost_scsi_deregister(void)
1041 return misc_deregister(&vhost_scsi_misc);
1044 static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1046 switch (tport->tport_proto_id) {
1047 case SCSI_PROTOCOL_SAS:
1048 return "SAS";
1049 case SCSI_PROTOCOL_FCP:
1050 return "FCP";
1051 case SCSI_PROTOCOL_ISCSI:
1052 return "iSCSI";
1053 default:
1054 break;
1057 return "Unknown";
1060 static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
1061 struct se_lun *lun)
1063 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1064 struct tcm_vhost_tpg, se_tpg);
1066 mutex_lock(&tv_tpg->tv_tpg_mutex);
1067 tv_tpg->tv_tpg_port_count++;
1068 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1070 return 0;
1073 static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
1074 struct se_lun *se_lun)
1076 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1077 struct tcm_vhost_tpg, se_tpg);
1079 mutex_lock(&tv_tpg->tv_tpg_mutex);
1080 tv_tpg->tv_tpg_port_count--;
1081 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1084 static struct se_node_acl *tcm_vhost_make_nodeacl(
1085 struct se_portal_group *se_tpg,
1086 struct config_group *group,
1087 const char *name)
1089 struct se_node_acl *se_nacl, *se_nacl_new;
1090 struct tcm_vhost_nacl *nacl;
1091 u64 wwpn = 0;
1092 u32 nexus_depth;
1094 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1095 return ERR_PTR(-EINVAL); */
1096 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1097 if (!se_nacl_new)
1098 return ERR_PTR(-ENOMEM);
1100 nexus_depth = 1;
1102 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1103 * when converting a NodeACL from demo mode -> explict
1105 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1106 name, nexus_depth);
1107 if (IS_ERR(se_nacl)) {
1108 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1109 return se_nacl;
1112 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1114 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1115 nacl->iport_wwpn = wwpn;
1117 return se_nacl;
1120 static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1122 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1123 struct tcm_vhost_nacl, se_node_acl);
1124 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1125 kfree(nacl);
1128 static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
1129 const char *name)
1131 struct se_portal_group *se_tpg;
1132 struct tcm_vhost_nexus *tv_nexus;
1134 mutex_lock(&tv_tpg->tv_tpg_mutex);
1135 if (tv_tpg->tpg_nexus) {
1136 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1137 pr_debug("tv_tpg->tpg_nexus already exists\n");
1138 return -EEXIST;
1140 se_tpg = &tv_tpg->se_tpg;
1142 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1143 if (!tv_nexus) {
1144 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1145 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1146 return -ENOMEM;
1149 * Initialize the struct se_session pointer
1151 tv_nexus->tvn_se_sess = transport_init_session();
1152 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1153 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1154 kfree(tv_nexus);
1155 return -ENOMEM;
1158 * Since we are running in 'demo mode' this call with generate a
1159 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1160 * the SCSI Initiator port name of the passed configfs group 'name'.
1162 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1163 se_tpg, (unsigned char *)name);
1164 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1165 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1166 pr_debug("core_tpg_check_initiator_node_acl() failed"
1167 " for %s\n", name);
1168 transport_free_session(tv_nexus->tvn_se_sess);
1169 kfree(tv_nexus);
1170 return -ENOMEM;
1173 * Now register the TCM vhost virtual I_T Nexus as active with the
1174 * call to __transport_register_session()
1176 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1177 tv_nexus->tvn_se_sess, tv_nexus);
1178 tv_tpg->tpg_nexus = tv_nexus;
1180 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1181 return 0;
1184 static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
1186 struct se_session *se_sess;
1187 struct tcm_vhost_nexus *tv_nexus;
1189 mutex_lock(&tpg->tv_tpg_mutex);
1190 tv_nexus = tpg->tpg_nexus;
1191 if (!tv_nexus) {
1192 mutex_unlock(&tpg->tv_tpg_mutex);
1193 return -ENODEV;
1196 se_sess = tv_nexus->tvn_se_sess;
1197 if (!se_sess) {
1198 mutex_unlock(&tpg->tv_tpg_mutex);
1199 return -ENODEV;
1202 if (tpg->tv_tpg_port_count != 0) {
1203 mutex_unlock(&tpg->tv_tpg_mutex);
1204 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1205 " active TPG port count: %d\n",
1206 tpg->tv_tpg_port_count);
1207 return -EBUSY;
1210 if (tpg->tv_tpg_vhost_count != 0) {
1211 mutex_unlock(&tpg->tv_tpg_mutex);
1212 pr_err("Unable to remove TCM_vhost I_T Nexus with"
1213 " active TPG vhost count: %d\n",
1214 tpg->tv_tpg_vhost_count);
1215 return -EBUSY;
1218 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
1219 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1220 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1222 * Release the SCSI I_T Nexus to the emulated vhost Target Port
1224 transport_deregister_session(tv_nexus->tvn_se_sess);
1225 tpg->tpg_nexus = NULL;
1226 mutex_unlock(&tpg->tv_tpg_mutex);
1228 kfree(tv_nexus);
1229 return 0;
1232 static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
1233 char *page)
1235 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1236 struct tcm_vhost_tpg, se_tpg);
1237 struct tcm_vhost_nexus *tv_nexus;
1238 ssize_t ret;
1240 mutex_lock(&tv_tpg->tv_tpg_mutex);
1241 tv_nexus = tv_tpg->tpg_nexus;
1242 if (!tv_nexus) {
1243 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1244 return -ENODEV;
1246 ret = snprintf(page, PAGE_SIZE, "%s\n",
1247 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1248 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1250 return ret;
1253 static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
1254 const char *page,
1255 size_t count)
1257 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1258 struct tcm_vhost_tpg, se_tpg);
1259 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1260 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1261 int ret;
1263 * Shutdown the active I_T nexus if 'NULL' is passed..
1265 if (!strncmp(page, "NULL", 4)) {
1266 ret = tcm_vhost_drop_nexus(tv_tpg);
1267 return (!ret) ? count : ret;
1270 * Otherwise make sure the passed virtual Initiator port WWN matches
1271 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1272 * tcm_vhost_make_nexus().
1274 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1275 pr_err("Emulated NAA Sas Address: %s, exceeds"
1276 " max: %d\n", page, TCM_VHOST_NAMELEN);
1277 return -EINVAL;
1279 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1281 ptr = strstr(i_port, "naa.");
1282 if (ptr) {
1283 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1284 pr_err("Passed SAS Initiator Port %s does not"
1285 " match target port protoid: %s\n", i_port,
1286 tcm_vhost_dump_proto_id(tport_wwn));
1287 return -EINVAL;
1289 port_ptr = &i_port[0];
1290 goto check_newline;
1292 ptr = strstr(i_port, "fc.");
1293 if (ptr) {
1294 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1295 pr_err("Passed FCP Initiator Port %s does not"
1296 " match target port protoid: %s\n", i_port,
1297 tcm_vhost_dump_proto_id(tport_wwn));
1298 return -EINVAL;
1300 port_ptr = &i_port[3]; /* Skip over "fc." */
1301 goto check_newline;
1303 ptr = strstr(i_port, "iqn.");
1304 if (ptr) {
1305 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1306 pr_err("Passed iSCSI Initiator Port %s does not"
1307 " match target port protoid: %s\n", i_port,
1308 tcm_vhost_dump_proto_id(tport_wwn));
1309 return -EINVAL;
1311 port_ptr = &i_port[0];
1312 goto check_newline;
1314 pr_err("Unable to locate prefix for emulated Initiator Port:"
1315 " %s\n", i_port);
1316 return -EINVAL;
1318 * Clear any trailing newline for the NAA WWN
1320 check_newline:
1321 if (i_port[strlen(i_port)-1] == '\n')
1322 i_port[strlen(i_port)-1] = '\0';
1324 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1325 if (ret < 0)
1326 return ret;
1328 return count;
1331 TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1333 static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1334 &tcm_vhost_tpg_nexus.attr,
1335 NULL,
1338 static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
1339 struct config_group *group,
1340 const char *name)
1342 struct tcm_vhost_tport *tport = container_of(wwn,
1343 struct tcm_vhost_tport, tport_wwn);
1345 struct tcm_vhost_tpg *tpg;
1346 unsigned long tpgt;
1347 int ret;
1349 if (strstr(name, "tpgt_") != name)
1350 return ERR_PTR(-EINVAL);
1351 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1352 return ERR_PTR(-EINVAL);
1354 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1355 if (!tpg) {
1356 pr_err("Unable to allocate struct tcm_vhost_tpg");
1357 return ERR_PTR(-ENOMEM);
1359 mutex_init(&tpg->tv_tpg_mutex);
1360 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1361 tpg->tport = tport;
1362 tpg->tport_tpgt = tpgt;
1364 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1365 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1366 if (ret < 0) {
1367 kfree(tpg);
1368 return NULL;
1370 mutex_lock(&tcm_vhost_mutex);
1371 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1372 mutex_unlock(&tcm_vhost_mutex);
1374 return &tpg->se_tpg;
1377 static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1379 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1380 struct tcm_vhost_tpg, se_tpg);
1382 mutex_lock(&tcm_vhost_mutex);
1383 list_del(&tpg->tv_tpg_list);
1384 mutex_unlock(&tcm_vhost_mutex);
1386 * Release the virtual I_T Nexus for this vhost TPG
1388 tcm_vhost_drop_nexus(tpg);
1390 * Deregister the se_tpg from TCM..
1392 core_tpg_deregister(se_tpg);
1393 kfree(tpg);
1396 static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
1397 struct config_group *group,
1398 const char *name)
1400 struct tcm_vhost_tport *tport;
1401 char *ptr;
1402 u64 wwpn = 0;
1403 int off = 0;
1405 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1406 return ERR_PTR(-EINVAL); */
1408 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1409 if (!tport) {
1410 pr_err("Unable to allocate struct tcm_vhost_tport");
1411 return ERR_PTR(-ENOMEM);
1413 tport->tport_wwpn = wwpn;
1415 * Determine the emulated Protocol Identifier and Target Port Name
1416 * based on the incoming configfs directory name.
1418 ptr = strstr(name, "naa.");
1419 if (ptr) {
1420 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1421 goto check_len;
1423 ptr = strstr(name, "fc.");
1424 if (ptr) {
1425 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1426 off = 3; /* Skip over "fc." */
1427 goto check_len;
1429 ptr = strstr(name, "iqn.");
1430 if (ptr) {
1431 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1432 goto check_len;
1435 pr_err("Unable to locate prefix for emulated Target Port:"
1436 " %s\n", name);
1437 kfree(tport);
1438 return ERR_PTR(-EINVAL);
1440 check_len:
1441 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1442 pr_err("Emulated %s Address: %s, exceeds"
1443 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1444 TCM_VHOST_NAMELEN);
1445 kfree(tport);
1446 return ERR_PTR(-EINVAL);
1448 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1450 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1451 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1453 return &tport->tport_wwn;
1456 static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1458 struct tcm_vhost_tport *tport = container_of(wwn,
1459 struct tcm_vhost_tport, tport_wwn);
1461 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1462 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1463 tport->tport_name);
1465 kfree(tport);
1468 static ssize_t tcm_vhost_wwn_show_attr_version(
1469 struct target_fabric_configfs *tf,
1470 char *page)
1472 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1473 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1474 utsname()->machine);
1477 TF_WWN_ATTR_RO(tcm_vhost, version);
1479 static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1480 &tcm_vhost_wwn_version.attr,
1481 NULL,
1484 static struct target_core_fabric_ops tcm_vhost_ops = {
1485 .get_fabric_name = tcm_vhost_get_fabric_name,
1486 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1487 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1488 .tpg_get_tag = tcm_vhost_get_tag,
1489 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1490 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1491 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1492 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1493 .tpg_check_demo_mode = tcm_vhost_check_true,
1494 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1495 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1496 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1497 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1498 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1499 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1500 .release_cmd = tcm_vhost_release_cmd,
1501 .shutdown_session = tcm_vhost_shutdown_session,
1502 .close_session = tcm_vhost_close_session,
1503 .sess_get_index = tcm_vhost_sess_get_index,
1504 .sess_get_initiator_sid = NULL,
1505 .write_pending = tcm_vhost_write_pending,
1506 .write_pending_status = tcm_vhost_write_pending_status,
1507 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1508 .get_task_tag = tcm_vhost_get_task_tag,
1509 .get_cmd_state = tcm_vhost_get_cmd_state,
1510 .queue_data_in = tcm_vhost_queue_data_in,
1511 .queue_status = tcm_vhost_queue_status,
1512 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
1514 * Setup callers for generic logic in target_core_fabric_configfs.c
1516 .fabric_make_wwn = tcm_vhost_make_tport,
1517 .fabric_drop_wwn = tcm_vhost_drop_tport,
1518 .fabric_make_tpg = tcm_vhost_make_tpg,
1519 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1520 .fabric_post_link = tcm_vhost_port_link,
1521 .fabric_pre_unlink = tcm_vhost_port_unlink,
1522 .fabric_make_np = NULL,
1523 .fabric_drop_np = NULL,
1524 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1525 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1528 static int tcm_vhost_register_configfs(void)
1530 struct target_fabric_configfs *fabric;
1531 int ret;
1533 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1534 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1535 utsname()->machine);
1537 * Register the top level struct config_item_type with TCM core
1539 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1540 if (IS_ERR(fabric)) {
1541 pr_err("target_fabric_configfs_init() failed\n");
1542 return PTR_ERR(fabric);
1545 * Setup fabric->tf_ops from our local tcm_vhost_ops
1547 fabric->tf_ops = tcm_vhost_ops;
1549 * Setup default attribute lists for various fabric->tf_cit_tmpl
1551 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1552 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1553 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1554 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1555 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1556 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1557 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1558 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1559 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1561 * Register the fabric for use within TCM
1563 ret = target_fabric_configfs_register(fabric);
1564 if (ret < 0) {
1565 pr_err("target_fabric_configfs_register() failed"
1566 " for TCM_VHOST\n");
1567 return ret;
1570 * Setup our local pointer to *fabric
1572 tcm_vhost_fabric_configfs = fabric;
1573 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1574 return 0;
1577 static void tcm_vhost_deregister_configfs(void)
1579 if (!tcm_vhost_fabric_configfs)
1580 return;
1582 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1583 tcm_vhost_fabric_configfs = NULL;
1584 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1587 static int __init tcm_vhost_init(void)
1589 int ret = -ENOMEM;
1591 * Use our own dedicated workqueue for submitting I/O into
1592 * target core to avoid contention within system_wq.
1594 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1595 if (!tcm_vhost_workqueue)
1596 goto out;
1598 ret = vhost_scsi_register();
1599 if (ret < 0)
1600 goto out_destroy_workqueue;
1602 ret = tcm_vhost_register_configfs();
1603 if (ret < 0)
1604 goto out_vhost_scsi_deregister;
1606 return 0;
1608 out_vhost_scsi_deregister:
1609 vhost_scsi_deregister();
1610 out_destroy_workqueue:
1611 destroy_workqueue(tcm_vhost_workqueue);
1612 out:
1613 return ret;
1616 static void tcm_vhost_exit(void)
1618 tcm_vhost_deregister_configfs();
1619 vhost_scsi_deregister();
1620 destroy_workqueue(tcm_vhost_workqueue);
1623 MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1624 MODULE_LICENSE("GPL");
1625 module_init(tcm_vhost_init);
1626 module_exit(tcm_vhost_exit);