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