blob: 79d3aeaff8fa6d07a7ae2e1fdbbe1972f895df73 [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>
Asias He1b7f3902013-02-06 13:20:59 +080051#include <linux/bitmap.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070052
53#include "vhost.c"
54#include "vhost.h"
55#include "tcm_vhost.h"
56
Nicholas Bellinger101998f2012-07-30 13:30:00 -070057enum {
58 VHOST_SCSI_VQ_CTL = 0,
59 VHOST_SCSI_VQ_EVT = 1,
60 VHOST_SCSI_VQ_IO = 2,
61};
62
Asias He1b7f3902013-02-06 13:20:59 +080063#define VHOST_SCSI_MAX_TARGET 256
64#define VHOST_SCSI_MAX_VQ 128
Asias He67e18cf2013-02-05 12:31:57 +080065
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070066struct vhost_scsi {
Asias He67e18cf2013-02-05 12:31:57 +080067 /* 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;
71
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070072 struct vhost_dev dev;
Asias He1b7f3902013-02-06 13:20:59 +080073 struct vhost_virtqueue vqs[VHOST_SCSI_MAX_VQ];
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070074
75 struct vhost_work vs_completion_work; /* cmd completion work item */
Asias He9d6064a2013-01-06 14:36:13 +080076 struct llist_head vs_completion_list; /* cmd completion queue */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070077};
78
79/* Local pointer to allocated TCM configfs fabric module */
80static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
81
82static struct workqueue_struct *tcm_vhost_workqueue;
83
84/* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
85static DEFINE_MUTEX(tcm_vhost_mutex);
86static LIST_HEAD(tcm_vhost_list);
87
Asias He765b34f2013-01-22 11:20:25 +080088static int iov_num_pages(struct iovec *iov)
89{
90 return (PAGE_ALIGN((unsigned long)iov->iov_base + iov->iov_len) -
91 ((unsigned long)iov->iov_base & PAGE_MASK)) >> PAGE_SHIFT;
92}
93
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070094static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
95{
96 return 1;
97}
98
99static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
100{
101 return 0;
102}
103
104static char *tcm_vhost_get_fabric_name(void)
105{
106 return "vhost";
107}
108
109static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
110{
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;
114
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;
126 }
127
128 return sas_get_fabric_proto_ident(se_tpg);
129}
130
131static char *tcm_vhost_get_fabric_wwn(struct se_portal_group *se_tpg)
132{
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;
136
137 return &tport->tport_name[0];
138}
139
140static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
141{
142 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
143 struct tcm_vhost_tpg, se_tpg);
144 return tpg->tport_tpgt;
145}
146
147static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
148{
149 return 1;
150}
151
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700152static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700153 struct se_node_acl *se_nacl,
154 struct t10_pr_registration *pr_reg,
155 int *format_code,
156 unsigned char *buf)
157{
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;
161
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;
176 }
177
178 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
179 format_code, buf);
180}
181
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700182static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700183 struct se_node_acl *se_nacl,
184 struct t10_pr_registration *pr_reg,
185 int *format_code)
186{
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;
190
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;
205 }
206
207 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
208 format_code);
209}
210
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700211static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700212 const char *buf,
213 u32 *out_tid_len,
214 char **port_nexus_ptr)
215{
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;
219
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;
234 }
235
236 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
237 port_nexus_ptr);
238}
239
240static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
241 struct se_portal_group *se_tpg)
242{
243 struct tcm_vhost_nacl *nacl;
244
245 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
246 if (!nacl) {
Masanari Iida744627e92012-11-05 23:30:40 +0900247 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700248 return NULL;
249 }
250
251 return &nacl->se_node_acl;
252}
253
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700254static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700255 struct se_node_acl *se_nacl)
256{
257 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
258 struct tcm_vhost_nacl, se_node_acl);
259 kfree(nacl);
260}
261
262static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
263{
264 return 1;
265}
266
267static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
268{
269 return;
270}
271
272static int tcm_vhost_shutdown_session(struct se_session *se_sess)
273{
274 return 0;
275}
276
277static void tcm_vhost_close_session(struct se_session *se_sess)
278{
279 return;
280}
281
282static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
283{
284 return 0;
285}
286
287static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
288{
289 /* Go ahead and process the write immediately */
290 target_execute_cmd(se_cmd);
291 return 0;
292}
293
294static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
295{
296 return 0;
297}
298
299static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
300{
301 return;
302}
303
304static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
305{
306 return 0;
307}
308
309static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
310{
311 return 0;
312}
313
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700314static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
315{
316 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
317
Asias He9d6064a2013-01-06 14:36:13 +0800318 llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700319
320 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
321}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700322
323static int tcm_vhost_queue_data_in(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_status(struct se_cmd *se_cmd)
332{
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;
337}
338
339static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
340{
341 return 0;
342}
343
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700344static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
345{
346 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
347
348 /* TODO locking against target/backend threads? */
349 transport_generic_free_cmd(se_cmd, 1);
350
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]));
355
356 kfree(tv_cmd->tvc_sgl);
357 }
358
359 kfree(tv_cmd);
360}
361
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700362/* Fill in status and signal that we are done processing this command
363 *
364 * This is scheduled in the vhost work queue so we are called with the owner
365 * process mm and can access the vring.
366 */
367static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
368{
369 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
370 vs_completion_work);
Asias He1b7f3902013-02-06 13:20:59 +0800371 DECLARE_BITMAP(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800372 struct virtio_scsi_cmd_resp v_rsp;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700373 struct tcm_vhost_cmd *tv_cmd;
Asias He9d6064a2013-01-06 14:36:13 +0800374 struct llist_node *llnode;
375 struct se_cmd *se_cmd;
Asias He1b7f3902013-02-06 13:20:59 +0800376 int ret, vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700377
Asias He1b7f3902013-02-06 13:20:59 +0800378 bitmap_zero(signal, VHOST_SCSI_MAX_VQ);
Asias He9d6064a2013-01-06 14:36:13 +0800379 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;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700385
386 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
387 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
388
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));
Asias He1b7f3902013-02-06 13:20:59 +0800397 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
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700402 pr_err("Faulted on virtio_scsi_cmd_resp\n");
403
404 vhost_scsi_free_cmd(tv_cmd);
405 }
406
Asias He1b7f3902013-02-06 13:20:59 +0800407 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]);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700411}
412
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700413static 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)
418{
419 struct tcm_vhost_cmd *tv_cmd;
420 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700421
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);
426 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700427
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);
432 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700433 tv_cmd->tvc_tag = v_req->tag;
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700434 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;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700438
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700439 return tv_cmd;
440}
441
442/*
443 * Map a user memory range into a scatterlist
444 *
445 * Returns the number of scatterlist entries used or -errno on error.
446 */
447static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
Asias He18100532013-01-22 11:20:27 +0800448 unsigned int sgl_count, struct iovec *iov, int write)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700449{
Asias He18100532013-01-22 11:20:27 +0800450 unsigned int npages = 0, pages_nr, offset, nbytes;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700451 struct scatterlist *sg = sgl;
Asias He18100532013-01-22 11:20:27 +0800452 void __user *ptr = iov->iov_base;
453 size_t len = iov->iov_len;
454 struct page **pages;
455 int ret, i;
456
457 pages_nr = iov_num_pages(iov);
458 if (pages_nr > sgl_count)
459 return -ENOBUFS;
460
461 pages = kmalloc(pages_nr * sizeof(struct page *), GFP_KERNEL);
462 if (!pages)
463 return -ENOMEM;
464
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;
475 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700476
477 while (len > 0) {
Asias He18100532013-01-22 11:20:27 +0800478 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);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700481 ptr += nbytes;
482 len -= nbytes;
483 sg++;
484 npages++;
485 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700486
Asias He18100532013-01-22 11:20:27 +0800487out:
488 kfree(pages);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700489 return ret;
490}
491
492static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
493 struct iovec *iov, unsigned int niov, int write)
494{
495 int ret;
496 unsigned int i;
497 u32 sgl_count;
498 struct scatterlist *sg;
499
500 /*
501 * Find out how long sglist needs to be
502 */
503 sgl_count = 0;
Asias Hef3158f32013-01-22 11:20:26 +0800504 for (i = 0; i < niov; i++)
505 sgl_count += iov_num_pages(&iov[i]);
506
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700507 /* TODO overflow checking */
508
509 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
510 if (!sg)
511 return -ENOMEM;
Fengguang Wuf0e0e9b2012-07-30 13:19:07 -0700512 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
513 sg, sgl_count, !sg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700514 sg_init_table(sg, sgl_count);
515
516 tv_cmd->tvc_sgl = sg;
517 tv_cmd->tvc_sgl_count = sgl_count;
518
519 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
520 for (i = 0; i < niov; i++) {
Asias He18100532013-01-22 11:20:27 +0800521 ret = vhost_scsi_map_to_sgl(sg, sgl_count, &iov[i], write);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700522 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;
529 }
530
531 sg += ret;
532 sgl_count -= ret;
533 }
534 return 0;
535}
536
537static void tcm_vhost_submission_work(struct work_struct *work)
538{
539 struct tcm_vhost_cmd *tv_cmd =
540 container_of(work, struct tcm_vhost_cmd, work);
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700541 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700542 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;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700545
546 if (tv_cmd->tvc_sgl_count) {
547 sg_ptr = tv_cmd->tvc_sgl;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700548/* 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;
553 }
554#endif
555 } else {
556 sg_ptr = NULL;
557 }
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700558 tv_nexus = tv_cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700559
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700560 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);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700566 if (rc < 0) {
567 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700568 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700569 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700570 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700571}
572
Asias He1b7f3902013-02-06 13:20:59 +0800573static void vhost_scsi_handle_vq(struct vhost_scsi *vs,
574 struct vhost_virtqueue *vq)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700575{
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700576 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;
Asias He67e18cf2013-02-05 12:31:57 +0800582 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700583
584 /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
Asias He67e18cf2013-02-05 12:31:57 +0800585 if (unlikely(!vs->vs_endpoint))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700586 return;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700587
588 mutex_lock(&vq->mutex);
589 vhost_disable_notify(&vs->dev, vq);
590
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;
605 }
606 break;
607 }
608
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;
626 }
627
628 /*
629 * Check for a sane resp buffer so we can report errors to
630 * the guest.
631 */
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;
637 }
638
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;
643 }
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;
651 }
652
Asias He67e18cf2013-02-05 12:31:57 +0800653 /* Extract the tpgt */
654 target = v_req.lun[1];
655 tv_tpg = vs->vs_tpg[target];
656
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;
661
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,
Asias He1b7f3902013-02-06 13:20:59 +0800668 vq, head, 0);
Asias He67e18cf2013-02-05 12:31:57 +0800669 else
670 pr_err("Faulted on virtio_scsi_cmd_resp\n");
671
672 continue;
673 }
674
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700675 exp_data_len = 0;
676 for (i = 0; i < data_num; i++)
677 exp_data_len += vq->iov[data_first + i].iov_len;
678
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;
685 }
686 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
687 ": %d\n", tv_cmd, exp_data_len, data_direction);
688
689 tv_cmd->tvc_vhost = vs;
Asias He1b7f3902013-02-06 13:20:59 +0800690 tv_cmd->tvc_vq = vq;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700691
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;
698 }
699
700 tv_cmd->tvc_resp = vq->iov[out].iov_base;
701
702 /*
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()
706 */
707 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
708 /*
709 * Check that the recieved CDB size does not exceeded our
710 * hardcoded max for tcm_vhost
711 */
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 */
720 }
721 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
722
723 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
724 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
725
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 */
733 }
734 }
735
736 /*
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()
740 */
741 tv_cmd->tvc_vq_desc = head;
742 /*
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..
747 */
748 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
749 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
750 }
751
752 mutex_unlock(&vq->mutex);
753}
754
755static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
756{
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700757 pr_debug("%s: The handling func for control queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700758}
759
760static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
761{
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700762 pr_debug("%s: The handling func for event queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700763}
764
765static void vhost_scsi_handle_kick(struct vhost_work *work)
766{
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);
770
Asias He1b7f3902013-02-06 13:20:59 +0800771 vhost_scsi_handle_vq(vs, vq);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700772}
773
774/*
775 * Called from vhost_scsi_ioctl() context to walk the list of available
776 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
777 */
778static int vhost_scsi_set_endpoint(
779 struct vhost_scsi *vs,
780 struct vhost_scsi_target *t)
781{
782 struct tcm_vhost_tport *tv_tport;
783 struct tcm_vhost_tpg *tv_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800784 bool match = false;
785 int index, ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700786
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;
794 }
795 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700796
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;
803 }
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700804 if (tv_tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700805 mutex_unlock(&tv_tpg->tv_tpg_mutex);
806 continue;
807 }
808 tv_tport = tv_tpg->tport;
809
Asias He67e18cf2013-02-05 12:31:57 +0800810 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
811 if (vs->vs_tpg[tv_tpg->tport_tpgt]) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700812 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Asias He67e18cf2013-02-05 12:31:57 +0800813 mutex_unlock(&tcm_vhost_mutex);
814 mutex_unlock(&vs->dev.mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700815 return -EEXIST;
816 }
Asias He67e18cf2013-02-05 12:31:57 +0800817 tv_tpg->tv_tpg_vhost_count++;
818 vs->vs_tpg[tv_tpg->tport_tpgt] = tv_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700819 smp_mb__after_atomic_inc();
Asias He67e18cf2013-02-05 12:31:57 +0800820 match = true;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700821 }
822 mutex_unlock(&tv_tpg->tv_tpg_mutex);
823 }
824 mutex_unlock(&tcm_vhost_mutex);
Asias He67e18cf2013-02-05 12:31:57 +0800825
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;
833 }
834
835 mutex_unlock(&vs->dev.mutex);
836 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700837}
838
839static int vhost_scsi_clear_endpoint(
840 struct vhost_scsi *vs,
841 struct vhost_scsi_target *t)
842{
843 struct tcm_vhost_tport *tv_tport;
844 struct tcm_vhost_tpg *tv_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800845 int index, ret, i;
846 u8 target;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700847
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])) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700852 ret = -EFAULT;
Asias He038e0af2013-03-15 09:14:05 +0800853 goto err_dev;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700854 }
855 }
Asias He67e18cf2013-02-05 12:31:57 +0800856 for (i = 0; i < VHOST_SCSI_MAX_TARGET; i++) {
857 target = i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700858
Asias He67e18cf2013-02-05 12:31:57 +0800859 tv_tpg = vs->vs_tpg[target];
860 if (!tv_tpg)
861 continue;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700862
Asias He038e0af2013-03-15 09:14:05 +0800863 mutex_lock(&tv_tpg->tv_tpg_mutex);
Asias He67e18cf2013-02-05 12:31:57 +0800864 tv_tport = tv_tpg->tport;
865 if (!tv_tport) {
866 ret = -ENODEV;
Asias He038e0af2013-03-15 09:14:05 +0800867 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800868 }
869
870 if (strcmp(tv_tport->tport_name, t->vhost_wwpn)) {
871 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
872 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
873 tv_tport->tport_name, tv_tpg->tport_tpgt,
874 t->vhost_wwpn, t->vhost_tpgt);
875 ret = -EINVAL;
Asias He038e0af2013-03-15 09:14:05 +0800876 goto err_tpg;
Asias He67e18cf2013-02-05 12:31:57 +0800877 }
878 tv_tpg->tv_tpg_vhost_count--;
879 vs->vs_tpg[target] = NULL;
880 vs->vs_endpoint = false;
Asias He038e0af2013-03-15 09:14:05 +0800881 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700882 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700883 mutex_unlock(&vs->dev.mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700884 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700885
Asias He038e0af2013-03-15 09:14:05 +0800886err_tpg:
887 mutex_unlock(&tv_tpg->tv_tpg_mutex);
888err_dev:
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700889 mutex_unlock(&vs->dev.mutex);
890 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700891}
892
893static int vhost_scsi_open(struct inode *inode, struct file *f)
894{
895 struct vhost_scsi *s;
Asias He1b7f3902013-02-06 13:20:59 +0800896 int r, i;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700897
898 s = kzalloc(sizeof(*s), GFP_KERNEL);
899 if (!s)
900 return -ENOMEM;
901
902 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700903
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700904 s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
905 s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
Asias He1b7f3902013-02-06 13:20:59 +0800906 for (i = VHOST_SCSI_VQ_IO; i < VHOST_SCSI_MAX_VQ; i++)
907 s->vqs[i].handle_kick = vhost_scsi_handle_kick;
908 r = vhost_dev_init(&s->dev, s->vqs, VHOST_SCSI_MAX_VQ);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700909 if (r < 0) {
910 kfree(s);
911 return r;
912 }
913
914 f->private_data = s;
915 return 0;
916}
917
918static int vhost_scsi_release(struct inode *inode, struct file *f)
919{
920 struct vhost_scsi *s = f->private_data;
Asias He67e18cf2013-02-05 12:31:57 +0800921 struct vhost_scsi_target t;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700922
Asias He67e18cf2013-02-05 12:31:57 +0800923 mutex_lock(&s->dev.mutex);
924 memcpy(t.vhost_wwpn, s->vs_vhost_wwpn, sizeof(t.vhost_wwpn));
925 mutex_unlock(&s->dev.mutex);
926 vhost_scsi_clear_endpoint(s, &t);
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000927 vhost_dev_stop(&s->dev);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700928 vhost_dev_cleanup(&s->dev, false);
929 kfree(s);
930 return 0;
931}
932
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700933static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
934{
935 vhost_poll_flush(&vs->dev.vqs[index].poll);
936}
937
938static void vhost_scsi_flush(struct vhost_scsi *vs)
939{
Asias He1b7f3902013-02-06 13:20:59 +0800940 int i;
941
942 for (i = 0; i < VHOST_SCSI_MAX_VQ; i++)
943 vhost_scsi_flush_vq(vs, i);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700944}
945
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700946static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
947{
948 if (features & ~VHOST_FEATURES)
949 return -EOPNOTSUPP;
950
951 mutex_lock(&vs->dev.mutex);
952 if ((features & (1 << VHOST_F_LOG_ALL)) &&
953 !vhost_log_access_ok(&vs->dev)) {
954 mutex_unlock(&vs->dev.mutex);
955 return -EFAULT;
956 }
957 vs->dev.acked_features = features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700958 smp_wmb();
959 vhost_scsi_flush(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700960 mutex_unlock(&vs->dev.mutex);
961 return 0;
962}
963
964static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
965 unsigned long arg)
966{
967 struct vhost_scsi *vs = f->private_data;
968 struct vhost_scsi_target backend;
969 void __user *argp = (void __user *)arg;
970 u64 __user *featurep = argp;
971 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700972 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700973
974 switch (ioctl) {
975 case VHOST_SCSI_SET_ENDPOINT:
976 if (copy_from_user(&backend, argp, sizeof backend))
977 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -0700978 if (backend.reserved != 0)
979 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700980
981 return vhost_scsi_set_endpoint(vs, &backend);
982 case VHOST_SCSI_CLEAR_ENDPOINT:
983 if (copy_from_user(&backend, argp, sizeof backend))
984 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -0700985 if (backend.reserved != 0)
986 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700987
988 return vhost_scsi_clear_endpoint(vs, &backend);
989 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700990 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700991 return -EFAULT;
992 return 0;
993 case VHOST_GET_FEATURES:
994 features = VHOST_FEATURES;
995 if (copy_to_user(featurep, &features, sizeof features))
996 return -EFAULT;
997 return 0;
998 case VHOST_SET_FEATURES:
999 if (copy_from_user(&features, featurep, sizeof features))
1000 return -EFAULT;
1001 return vhost_scsi_set_features(vs, features);
1002 default:
1003 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +02001004 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
1005 /* TODO: flush backend after dev ioctl. */
1006 if (r == -ENOIOCTLCMD)
1007 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001008 mutex_unlock(&vs->dev.mutex);
1009 return r;
1010 }
1011}
1012
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001013#ifdef CONFIG_COMPAT
1014static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
1015 unsigned long arg)
1016{
1017 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
1018}
1019#endif
1020
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001021static const struct file_operations vhost_scsi_fops = {
1022 .owner = THIS_MODULE,
1023 .release = vhost_scsi_release,
1024 .unlocked_ioctl = vhost_scsi_ioctl,
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001025#ifdef CONFIG_COMPAT
1026 .compat_ioctl = vhost_scsi_compat_ioctl,
1027#endif
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001028 .open = vhost_scsi_open,
1029 .llseek = noop_llseek,
1030};
1031
1032static struct miscdevice vhost_scsi_misc = {
1033 MISC_DYNAMIC_MINOR,
1034 "vhost-scsi",
1035 &vhost_scsi_fops,
1036};
1037
1038static int __init vhost_scsi_register(void)
1039{
1040 return misc_register(&vhost_scsi_misc);
1041}
1042
1043static int vhost_scsi_deregister(void)
1044{
1045 return misc_deregister(&vhost_scsi_misc);
1046}
1047
1048static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1049{
1050 switch (tport->tport_proto_id) {
1051 case SCSI_PROTOCOL_SAS:
1052 return "SAS";
1053 case SCSI_PROTOCOL_FCP:
1054 return "FCP";
1055 case SCSI_PROTOCOL_ISCSI:
1056 return "iSCSI";
1057 default:
1058 break;
1059 }
1060
1061 return "Unknown";
1062}
1063
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001064static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001065 struct se_lun *lun)
1066{
1067 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1068 struct tcm_vhost_tpg, se_tpg);
1069
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001070 mutex_lock(&tv_tpg->tv_tpg_mutex);
1071 tv_tpg->tv_tpg_port_count++;
1072 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001073
1074 return 0;
1075}
1076
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001077static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001078 struct se_lun *se_lun)
1079{
1080 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1081 struct tcm_vhost_tpg, se_tpg);
1082
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001083 mutex_lock(&tv_tpg->tv_tpg_mutex);
1084 tv_tpg->tv_tpg_port_count--;
1085 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001086}
1087
1088static struct se_node_acl *tcm_vhost_make_nodeacl(
1089 struct se_portal_group *se_tpg,
1090 struct config_group *group,
1091 const char *name)
1092{
1093 struct se_node_acl *se_nacl, *se_nacl_new;
1094 struct tcm_vhost_nacl *nacl;
1095 u64 wwpn = 0;
1096 u32 nexus_depth;
1097
1098 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1099 return ERR_PTR(-EINVAL); */
1100 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1101 if (!se_nacl_new)
1102 return ERR_PTR(-ENOMEM);
1103
1104 nexus_depth = 1;
1105 /*
1106 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1107 * when converting a NodeACL from demo mode -> explict
1108 */
1109 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1110 name, nexus_depth);
1111 if (IS_ERR(se_nacl)) {
1112 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1113 return se_nacl;
1114 }
1115 /*
1116 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1117 */
1118 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1119 nacl->iport_wwpn = wwpn;
1120
1121 return se_nacl;
1122}
1123
1124static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1125{
1126 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1127 struct tcm_vhost_nacl, se_node_acl);
1128 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1129 kfree(nacl);
1130}
1131
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001132static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001133 const char *name)
1134{
1135 struct se_portal_group *se_tpg;
1136 struct tcm_vhost_nexus *tv_nexus;
1137
1138 mutex_lock(&tv_tpg->tv_tpg_mutex);
1139 if (tv_tpg->tpg_nexus) {
1140 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1141 pr_debug("tv_tpg->tpg_nexus already exists\n");
1142 return -EEXIST;
1143 }
1144 se_tpg = &tv_tpg->se_tpg;
1145
1146 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1147 if (!tv_nexus) {
1148 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1149 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1150 return -ENOMEM;
1151 }
1152 /*
1153 * Initialize the struct se_session pointer
1154 */
1155 tv_nexus->tvn_se_sess = transport_init_session();
1156 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1157 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1158 kfree(tv_nexus);
1159 return -ENOMEM;
1160 }
1161 /*
1162 * Since we are running in 'demo mode' this call with generate a
1163 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1164 * the SCSI Initiator port name of the passed configfs group 'name'.
1165 */
1166 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1167 se_tpg, (unsigned char *)name);
1168 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1169 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1170 pr_debug("core_tpg_check_initiator_node_acl() failed"
1171 " for %s\n", name);
1172 transport_free_session(tv_nexus->tvn_se_sess);
1173 kfree(tv_nexus);
1174 return -ENOMEM;
1175 }
1176 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001177 * Now register the TCM vhost virtual I_T Nexus as active with the
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001178 * call to __transport_register_session()
1179 */
1180 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1181 tv_nexus->tvn_se_sess, tv_nexus);
1182 tv_tpg->tpg_nexus = tv_nexus;
1183
1184 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1185 return 0;
1186}
1187
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001188static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001189{
1190 struct se_session *se_sess;
1191 struct tcm_vhost_nexus *tv_nexus;
1192
1193 mutex_lock(&tpg->tv_tpg_mutex);
1194 tv_nexus = tpg->tpg_nexus;
1195 if (!tv_nexus) {
1196 mutex_unlock(&tpg->tv_tpg_mutex);
1197 return -ENODEV;
1198 }
1199
1200 se_sess = tv_nexus->tvn_se_sess;
1201 if (!se_sess) {
1202 mutex_unlock(&tpg->tv_tpg_mutex);
1203 return -ENODEV;
1204 }
1205
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001206 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001207 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001208 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001209 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001210 tpg->tv_tpg_port_count);
1211 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001212 }
1213
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001214 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001215 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001216 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001217 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001218 tpg->tv_tpg_vhost_count);
1219 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001220 }
1221
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001222 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001223 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1224 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1225 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001226 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001227 */
1228 transport_deregister_session(tv_nexus->tvn_se_sess);
1229 tpg->tpg_nexus = NULL;
1230 mutex_unlock(&tpg->tv_tpg_mutex);
1231
1232 kfree(tv_nexus);
1233 return 0;
1234}
1235
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001236static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001237 char *page)
1238{
1239 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1240 struct tcm_vhost_tpg, se_tpg);
1241 struct tcm_vhost_nexus *tv_nexus;
1242 ssize_t ret;
1243
1244 mutex_lock(&tv_tpg->tv_tpg_mutex);
1245 tv_nexus = tv_tpg->tpg_nexus;
1246 if (!tv_nexus) {
1247 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1248 return -ENODEV;
1249 }
1250 ret = snprintf(page, PAGE_SIZE, "%s\n",
1251 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1252 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1253
1254 return ret;
1255}
1256
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001257static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001258 const char *page,
1259 size_t count)
1260{
1261 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1262 struct tcm_vhost_tpg, se_tpg);
1263 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1264 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1265 int ret;
1266 /*
1267 * Shutdown the active I_T nexus if 'NULL' is passed..
1268 */
1269 if (!strncmp(page, "NULL", 4)) {
1270 ret = tcm_vhost_drop_nexus(tv_tpg);
1271 return (!ret) ? count : ret;
1272 }
1273 /*
1274 * Otherwise make sure the passed virtual Initiator port WWN matches
1275 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1276 * tcm_vhost_make_nexus().
1277 */
1278 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1279 pr_err("Emulated NAA Sas Address: %s, exceeds"
1280 " max: %d\n", page, TCM_VHOST_NAMELEN);
1281 return -EINVAL;
1282 }
1283 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1284
1285 ptr = strstr(i_port, "naa.");
1286 if (ptr) {
1287 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1288 pr_err("Passed SAS Initiator Port %s does not"
1289 " match target port protoid: %s\n", i_port,
1290 tcm_vhost_dump_proto_id(tport_wwn));
1291 return -EINVAL;
1292 }
1293 port_ptr = &i_port[0];
1294 goto check_newline;
1295 }
1296 ptr = strstr(i_port, "fc.");
1297 if (ptr) {
1298 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1299 pr_err("Passed FCP Initiator Port %s does not"
1300 " match target port protoid: %s\n", i_port,
1301 tcm_vhost_dump_proto_id(tport_wwn));
1302 return -EINVAL;
1303 }
1304 port_ptr = &i_port[3]; /* Skip over "fc." */
1305 goto check_newline;
1306 }
1307 ptr = strstr(i_port, "iqn.");
1308 if (ptr) {
1309 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1310 pr_err("Passed iSCSI Initiator Port %s does not"
1311 " match target port protoid: %s\n", i_port,
1312 tcm_vhost_dump_proto_id(tport_wwn));
1313 return -EINVAL;
1314 }
1315 port_ptr = &i_port[0];
1316 goto check_newline;
1317 }
1318 pr_err("Unable to locate prefix for emulated Initiator Port:"
1319 " %s\n", i_port);
1320 return -EINVAL;
1321 /*
1322 * Clear any trailing newline for the NAA WWN
1323 */
1324check_newline:
1325 if (i_port[strlen(i_port)-1] == '\n')
1326 i_port[strlen(i_port)-1] = '\0';
1327
1328 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1329 if (ret < 0)
1330 return ret;
1331
1332 return count;
1333}
1334
1335TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1336
1337static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1338 &tcm_vhost_tpg_nexus.attr,
1339 NULL,
1340};
1341
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001342static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001343 struct config_group *group,
1344 const char *name)
1345{
1346 struct tcm_vhost_tport *tport = container_of(wwn,
1347 struct tcm_vhost_tport, tport_wwn);
1348
1349 struct tcm_vhost_tpg *tpg;
1350 unsigned long tpgt;
1351 int ret;
1352
1353 if (strstr(name, "tpgt_") != name)
1354 return ERR_PTR(-EINVAL);
1355 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1356 return ERR_PTR(-EINVAL);
1357
1358 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1359 if (!tpg) {
1360 pr_err("Unable to allocate struct tcm_vhost_tpg");
1361 return ERR_PTR(-ENOMEM);
1362 }
1363 mutex_init(&tpg->tv_tpg_mutex);
1364 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1365 tpg->tport = tport;
1366 tpg->tport_tpgt = tpgt;
1367
1368 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1369 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1370 if (ret < 0) {
1371 kfree(tpg);
1372 return NULL;
1373 }
1374 mutex_lock(&tcm_vhost_mutex);
1375 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1376 mutex_unlock(&tcm_vhost_mutex);
1377
1378 return &tpg->se_tpg;
1379}
1380
1381static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1382{
1383 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1384 struct tcm_vhost_tpg, se_tpg);
1385
1386 mutex_lock(&tcm_vhost_mutex);
1387 list_del(&tpg->tv_tpg_list);
1388 mutex_unlock(&tcm_vhost_mutex);
1389 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001390 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001391 */
1392 tcm_vhost_drop_nexus(tpg);
1393 /*
1394 * Deregister the se_tpg from TCM..
1395 */
1396 core_tpg_deregister(se_tpg);
1397 kfree(tpg);
1398}
1399
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001400static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001401 struct config_group *group,
1402 const char *name)
1403{
1404 struct tcm_vhost_tport *tport;
1405 char *ptr;
1406 u64 wwpn = 0;
1407 int off = 0;
1408
1409 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1410 return ERR_PTR(-EINVAL); */
1411
1412 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1413 if (!tport) {
1414 pr_err("Unable to allocate struct tcm_vhost_tport");
1415 return ERR_PTR(-ENOMEM);
1416 }
1417 tport->tport_wwpn = wwpn;
1418 /*
1419 * Determine the emulated Protocol Identifier and Target Port Name
1420 * based on the incoming configfs directory name.
1421 */
1422 ptr = strstr(name, "naa.");
1423 if (ptr) {
1424 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1425 goto check_len;
1426 }
1427 ptr = strstr(name, "fc.");
1428 if (ptr) {
1429 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1430 off = 3; /* Skip over "fc." */
1431 goto check_len;
1432 }
1433 ptr = strstr(name, "iqn.");
1434 if (ptr) {
1435 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1436 goto check_len;
1437 }
1438
1439 pr_err("Unable to locate prefix for emulated Target Port:"
1440 " %s\n", name);
1441 kfree(tport);
1442 return ERR_PTR(-EINVAL);
1443
1444check_len:
1445 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1446 pr_err("Emulated %s Address: %s, exceeds"
1447 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1448 TCM_VHOST_NAMELEN);
1449 kfree(tport);
1450 return ERR_PTR(-EINVAL);
1451 }
1452 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1453
1454 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1455 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1456
1457 return &tport->tport_wwn;
1458}
1459
1460static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1461{
1462 struct tcm_vhost_tport *tport = container_of(wwn,
1463 struct tcm_vhost_tport, tport_wwn);
1464
1465 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1466 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1467 tport->tport_name);
1468
1469 kfree(tport);
1470}
1471
1472static ssize_t tcm_vhost_wwn_show_attr_version(
1473 struct target_fabric_configfs *tf,
1474 char *page)
1475{
1476 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1477 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1478 utsname()->machine);
1479}
1480
1481TF_WWN_ATTR_RO(tcm_vhost, version);
1482
1483static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1484 &tcm_vhost_wwn_version.attr,
1485 NULL,
1486};
1487
1488static struct target_core_fabric_ops tcm_vhost_ops = {
1489 .get_fabric_name = tcm_vhost_get_fabric_name,
1490 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1491 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1492 .tpg_get_tag = tcm_vhost_get_tag,
1493 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1494 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1495 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1496 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1497 .tpg_check_demo_mode = tcm_vhost_check_true,
1498 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1499 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1500 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1501 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1502 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1503 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1504 .release_cmd = tcm_vhost_release_cmd,
1505 .shutdown_session = tcm_vhost_shutdown_session,
1506 .close_session = tcm_vhost_close_session,
1507 .sess_get_index = tcm_vhost_sess_get_index,
1508 .sess_get_initiator_sid = NULL,
1509 .write_pending = tcm_vhost_write_pending,
1510 .write_pending_status = tcm_vhost_write_pending_status,
1511 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1512 .get_task_tag = tcm_vhost_get_task_tag,
1513 .get_cmd_state = tcm_vhost_get_cmd_state,
1514 .queue_data_in = tcm_vhost_queue_data_in,
1515 .queue_status = tcm_vhost_queue_status,
1516 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001517 /*
1518 * Setup callers for generic logic in target_core_fabric_configfs.c
1519 */
1520 .fabric_make_wwn = tcm_vhost_make_tport,
1521 .fabric_drop_wwn = tcm_vhost_drop_tport,
1522 .fabric_make_tpg = tcm_vhost_make_tpg,
1523 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1524 .fabric_post_link = tcm_vhost_port_link,
1525 .fabric_pre_unlink = tcm_vhost_port_unlink,
1526 .fabric_make_np = NULL,
1527 .fabric_drop_np = NULL,
1528 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1529 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1530};
1531
1532static int tcm_vhost_register_configfs(void)
1533{
1534 struct target_fabric_configfs *fabric;
1535 int ret;
1536
1537 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1538 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1539 utsname()->machine);
1540 /*
1541 * Register the top level struct config_item_type with TCM core
1542 */
1543 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1544 if (IS_ERR(fabric)) {
1545 pr_err("target_fabric_configfs_init() failed\n");
1546 return PTR_ERR(fabric);
1547 }
1548 /*
1549 * Setup fabric->tf_ops from our local tcm_vhost_ops
1550 */
1551 fabric->tf_ops = tcm_vhost_ops;
1552 /*
1553 * Setup default attribute lists for various fabric->tf_cit_tmpl
1554 */
1555 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1556 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1557 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1558 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1559 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1560 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1561 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1562 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1563 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1564 /*
1565 * Register the fabric for use within TCM
1566 */
1567 ret = target_fabric_configfs_register(fabric);
1568 if (ret < 0) {
1569 pr_err("target_fabric_configfs_register() failed"
1570 " for TCM_VHOST\n");
1571 return ret;
1572 }
1573 /*
1574 * Setup our local pointer to *fabric
1575 */
1576 tcm_vhost_fabric_configfs = fabric;
1577 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1578 return 0;
1579};
1580
1581static void tcm_vhost_deregister_configfs(void)
1582{
1583 if (!tcm_vhost_fabric_configfs)
1584 return;
1585
1586 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1587 tcm_vhost_fabric_configfs = NULL;
1588 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1589};
1590
1591static int __init tcm_vhost_init(void)
1592{
1593 int ret = -ENOMEM;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001594 /*
1595 * Use our own dedicated workqueue for submitting I/O into
1596 * target core to avoid contention within system_wq.
1597 */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001598 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1599 if (!tcm_vhost_workqueue)
1600 goto out;
1601
1602 ret = vhost_scsi_register();
1603 if (ret < 0)
1604 goto out_destroy_workqueue;
1605
1606 ret = tcm_vhost_register_configfs();
1607 if (ret < 0)
1608 goto out_vhost_scsi_deregister;
1609
1610 return 0;
1611
1612out_vhost_scsi_deregister:
1613 vhost_scsi_deregister();
1614out_destroy_workqueue:
1615 destroy_workqueue(tcm_vhost_workqueue);
1616out:
1617 return ret;
1618};
1619
1620static void tcm_vhost_exit(void)
1621{
1622 tcm_vhost_deregister_configfs();
1623 vhost_scsi_deregister();
1624 destroy_workqueue(tcm_vhost_workqueue);
1625};
1626
1627MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1628MODULE_LICENSE("GPL");
1629module_init(tcm_vhost_init);
1630module_exit(tcm_vhost_exit);