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