blob: 33e5f90dfcf5c2447fe001b12ecc53d90868d461 [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>
50
51#include "vhost.c"
52#include "vhost.h"
53#include "tcm_vhost.h"
54
Nicholas Bellinger101998f2012-07-30 13:30:00 -070055enum {
56 VHOST_SCSI_VQ_CTL = 0,
57 VHOST_SCSI_VQ_EVT = 1,
58 VHOST_SCSI_VQ_IO = 2,
59};
60
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070061struct vhost_scsi {
Nicholas Bellinger101998f2012-07-30 13:30:00 -070062 struct tcm_vhost_tpg *vs_tpg; /* Protected by vhost_scsi->dev.mutex */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070063 struct vhost_dev dev;
64 struct vhost_virtqueue vqs[3];
65
66 struct vhost_work vs_completion_work; /* cmd completion work item */
67 struct list_head vs_completion_list; /* cmd completion queue */
68 spinlock_t vs_completion_lock; /* protects s_completion_list */
69};
70
71/* Local pointer to allocated TCM configfs fabric module */
72static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
73
74static struct workqueue_struct *tcm_vhost_workqueue;
75
76/* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
77static DEFINE_MUTEX(tcm_vhost_mutex);
78static LIST_HEAD(tcm_vhost_list);
79
80static int tcm_vhost_check_true(struct se_portal_group *se_tpg)
81{
82 return 1;
83}
84
85static int tcm_vhost_check_false(struct se_portal_group *se_tpg)
86{
87 return 0;
88}
89
90static char *tcm_vhost_get_fabric_name(void)
91{
92 return "vhost";
93}
94
95static u8 tcm_vhost_get_fabric_proto_ident(struct se_portal_group *se_tpg)
96{
97 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
98 struct tcm_vhost_tpg, se_tpg);
99 struct tcm_vhost_tport *tport = tpg->tport;
100
101 switch (tport->tport_proto_id) {
102 case SCSI_PROTOCOL_SAS:
103 return sas_get_fabric_proto_ident(se_tpg);
104 case SCSI_PROTOCOL_FCP:
105 return fc_get_fabric_proto_ident(se_tpg);
106 case SCSI_PROTOCOL_ISCSI:
107 return iscsi_get_fabric_proto_ident(se_tpg);
108 default:
109 pr_err("Unknown tport_proto_id: 0x%02x, using"
110 " SAS emulation\n", tport->tport_proto_id);
111 break;
112 }
113
114 return sas_get_fabric_proto_ident(se_tpg);
115}
116
117static char *tcm_vhost_get_fabric_wwn(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 return &tport->tport_name[0];
124}
125
126static u16 tcm_vhost_get_tag(struct se_portal_group *se_tpg)
127{
128 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
129 struct tcm_vhost_tpg, se_tpg);
130 return tpg->tport_tpgt;
131}
132
133static u32 tcm_vhost_get_default_depth(struct se_portal_group *se_tpg)
134{
135 return 1;
136}
137
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700138static u32 tcm_vhost_get_pr_transport_id(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700139 struct se_node_acl *se_nacl,
140 struct t10_pr_registration *pr_reg,
141 int *format_code,
142 unsigned char *buf)
143{
144 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
145 struct tcm_vhost_tpg, se_tpg);
146 struct tcm_vhost_tport *tport = tpg->tport;
147
148 switch (tport->tport_proto_id) {
149 case SCSI_PROTOCOL_SAS:
150 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
151 format_code, buf);
152 case SCSI_PROTOCOL_FCP:
153 return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
154 format_code, buf);
155 case SCSI_PROTOCOL_ISCSI:
156 return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
157 format_code, buf);
158 default:
159 pr_err("Unknown tport_proto_id: 0x%02x, using"
160 " SAS emulation\n", tport->tport_proto_id);
161 break;
162 }
163
164 return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
165 format_code, buf);
166}
167
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700168static u32 tcm_vhost_get_pr_transport_id_len(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700169 struct se_node_acl *se_nacl,
170 struct t10_pr_registration *pr_reg,
171 int *format_code)
172{
173 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
174 struct tcm_vhost_tpg, se_tpg);
175 struct tcm_vhost_tport *tport = tpg->tport;
176
177 switch (tport->tport_proto_id) {
178 case SCSI_PROTOCOL_SAS:
179 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
180 format_code);
181 case SCSI_PROTOCOL_FCP:
182 return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
183 format_code);
184 case SCSI_PROTOCOL_ISCSI:
185 return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
186 format_code);
187 default:
188 pr_err("Unknown tport_proto_id: 0x%02x, using"
189 " SAS emulation\n", tport->tport_proto_id);
190 break;
191 }
192
193 return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
194 format_code);
195}
196
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700197static char *tcm_vhost_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700198 const char *buf,
199 u32 *out_tid_len,
200 char **port_nexus_ptr)
201{
202 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
203 struct tcm_vhost_tpg, se_tpg);
204 struct tcm_vhost_tport *tport = tpg->tport;
205
206 switch (tport->tport_proto_id) {
207 case SCSI_PROTOCOL_SAS:
208 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
209 port_nexus_ptr);
210 case SCSI_PROTOCOL_FCP:
211 return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
212 port_nexus_ptr);
213 case SCSI_PROTOCOL_ISCSI:
214 return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
215 port_nexus_ptr);
216 default:
217 pr_err("Unknown tport_proto_id: 0x%02x, using"
218 " SAS emulation\n", tport->tport_proto_id);
219 break;
220 }
221
222 return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
223 port_nexus_ptr);
224}
225
226static struct se_node_acl *tcm_vhost_alloc_fabric_acl(
227 struct se_portal_group *se_tpg)
228{
229 struct tcm_vhost_nacl *nacl;
230
231 nacl = kzalloc(sizeof(struct tcm_vhost_nacl), GFP_KERNEL);
232 if (!nacl) {
233 pr_err("Unable to alocate struct tcm_vhost_nacl\n");
234 return NULL;
235 }
236
237 return &nacl->se_node_acl;
238}
239
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700240static void tcm_vhost_release_fabric_acl(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700241 struct se_node_acl *se_nacl)
242{
243 struct tcm_vhost_nacl *nacl = container_of(se_nacl,
244 struct tcm_vhost_nacl, se_node_acl);
245 kfree(nacl);
246}
247
248static u32 tcm_vhost_tpg_get_inst_index(struct se_portal_group *se_tpg)
249{
250 return 1;
251}
252
253static void tcm_vhost_release_cmd(struct se_cmd *se_cmd)
254{
255 return;
256}
257
258static int tcm_vhost_shutdown_session(struct se_session *se_sess)
259{
260 return 0;
261}
262
263static void tcm_vhost_close_session(struct se_session *se_sess)
264{
265 return;
266}
267
268static u32 tcm_vhost_sess_get_index(struct se_session *se_sess)
269{
270 return 0;
271}
272
273static int tcm_vhost_write_pending(struct se_cmd *se_cmd)
274{
275 /* Go ahead and process the write immediately */
276 target_execute_cmd(se_cmd);
277 return 0;
278}
279
280static int tcm_vhost_write_pending_status(struct se_cmd *se_cmd)
281{
282 return 0;
283}
284
285static void tcm_vhost_set_default_node_attrs(struct se_node_acl *nacl)
286{
287 return;
288}
289
290static u32 tcm_vhost_get_task_tag(struct se_cmd *se_cmd)
291{
292 return 0;
293}
294
295static int tcm_vhost_get_cmd_state(struct se_cmd *se_cmd)
296{
297 return 0;
298}
299
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700300static void vhost_scsi_complete_cmd(struct tcm_vhost_cmd *tv_cmd)
301{
302 struct vhost_scsi *vs = tv_cmd->tvc_vhost;
303
304 spin_lock_bh(&vs->vs_completion_lock);
305 list_add_tail(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
306 spin_unlock_bh(&vs->vs_completion_lock);
307
308 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
309}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700310
311static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
312{
313 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
314 struct tcm_vhost_cmd, tvc_se_cmd);
315 vhost_scsi_complete_cmd(tv_cmd);
316 return 0;
317}
318
319static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
320{
321 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
322 struct tcm_vhost_cmd, tvc_se_cmd);
323 vhost_scsi_complete_cmd(tv_cmd);
324 return 0;
325}
326
327static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
328{
329 return 0;
330}
331
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700332static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
333{
334 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
335
336 /* TODO locking against target/backend threads? */
337 transport_generic_free_cmd(se_cmd, 1);
338
339 if (tv_cmd->tvc_sgl_count) {
340 u32 i;
341 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
342 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
343
344 kfree(tv_cmd->tvc_sgl);
345 }
346
347 kfree(tv_cmd);
348}
349
350/* Dequeue a command from the completion list */
351static struct tcm_vhost_cmd *vhost_scsi_get_cmd_from_completion(
352 struct vhost_scsi *vs)
353{
354 struct tcm_vhost_cmd *tv_cmd = NULL;
355
356 spin_lock_bh(&vs->vs_completion_lock);
357 if (list_empty(&vs->vs_completion_list)) {
358 spin_unlock_bh(&vs->vs_completion_lock);
359 return NULL;
360 }
361
362 list_for_each_entry(tv_cmd, &vs->vs_completion_list,
363 tvc_completion_list) {
364 list_del(&tv_cmd->tvc_completion_list);
365 break;
366 }
367 spin_unlock_bh(&vs->vs_completion_lock);
368 return tv_cmd;
369}
370
371/* Fill in status and signal that we are done processing this command
372 *
373 * This is scheduled in the vhost work queue so we are called with the owner
374 * process mm and can access the vring.
375 */
376static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
377{
378 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
379 vs_completion_work);
380 struct tcm_vhost_cmd *tv_cmd;
381
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700382 while ((tv_cmd = vhost_scsi_get_cmd_from_completion(vs))) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700383 struct virtio_scsi_cmd_resp v_rsp;
384 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
385 int ret;
386
387 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
388 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
389
390 memset(&v_rsp, 0, sizeof(v_rsp));
391 v_rsp.resid = se_cmd->residual_count;
392 /* TODO is status_qualifier field needed? */
393 v_rsp.status = se_cmd->scsi_status;
394 v_rsp.sense_len = se_cmd->scsi_sense_length;
395 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
396 v_rsp.sense_len);
397 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
398 if (likely(ret == 0))
399 vhost_add_used(&vs->vqs[2], tv_cmd->tvc_vq_desc, 0);
400 else
401 pr_err("Faulted on virtio_scsi_cmd_resp\n");
402
403 vhost_scsi_free_cmd(tv_cmd);
404 }
405
406 vhost_signal(&vs->dev, &vs->vqs[2]);
407}
408
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700409static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
410 struct tcm_vhost_tpg *tv_tpg,
411 struct virtio_scsi_cmd_req *v_req,
412 u32 exp_data_len,
413 int data_direction)
414{
415 struct tcm_vhost_cmd *tv_cmd;
416 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700417 struct se_session *se_sess;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700418
419 tv_nexus = tv_tpg->tpg_nexus;
420 if (!tv_nexus) {
421 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
422 return ERR_PTR(-EIO);
423 }
424 se_sess = tv_nexus->tvn_se_sess;
425
426 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
427 if (!tv_cmd) {
428 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
429 return ERR_PTR(-ENOMEM);
430 }
431 INIT_LIST_HEAD(&tv_cmd->tvc_completion_list);
432 tv_cmd->tvc_tag = v_req->tag;
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700433 tv_cmd->tvc_task_attr = v_req->task_attr;
434 tv_cmd->tvc_exp_data_len = exp_data_len;
435 tv_cmd->tvc_data_direction = data_direction;
436 tv_cmd->tvc_nexus = tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700437
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700438 return tv_cmd;
439}
440
441/*
442 * Map a user memory range into a scatterlist
443 *
444 * Returns the number of scatterlist entries used or -errno on error.
445 */
446static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
447 unsigned int sgl_count, void __user *ptr, size_t len, int write)
448{
449 struct scatterlist *sg = sgl;
450 unsigned int npages = 0;
451 int ret;
452
453 while (len > 0) {
454 struct page *page;
455 unsigned int offset = (uintptr_t)ptr & ~PAGE_MASK;
456 unsigned int nbytes = min_t(unsigned int,
457 PAGE_SIZE - offset, len);
458
459 if (npages == sgl_count) {
460 ret = -ENOBUFS;
461 goto err;
462 }
463
464 ret = get_user_pages_fast((unsigned long)ptr, 1, write, &page);
465 BUG_ON(ret == 0); /* we should either get our page or fail */
466 if (ret < 0)
467 goto err;
468
469 sg_set_page(sg, page, nbytes, offset);
470 ptr += nbytes;
471 len -= nbytes;
472 sg++;
473 npages++;
474 }
475 return npages;
476
477err:
478 /* Put pages that we hold */
479 for (sg = sgl; sg != &sgl[npages]; sg++)
480 put_page(sg_page(sg));
481 return ret;
482}
483
484static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
485 struct iovec *iov, unsigned int niov, int write)
486{
487 int ret;
488 unsigned int i;
489 u32 sgl_count;
490 struct scatterlist *sg;
491
492 /*
493 * Find out how long sglist needs to be
494 */
495 sgl_count = 0;
496 for (i = 0; i < niov; i++) {
497 sgl_count += (((uintptr_t)iov[i].iov_base + iov[i].iov_len +
498 PAGE_SIZE - 1) >> PAGE_SHIFT) -
499 ((uintptr_t)iov[i].iov_base >> PAGE_SHIFT);
500 }
501 /* TODO overflow checking */
502
503 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
504 if (!sg)
505 return -ENOMEM;
Fengguang Wuf0e0e9b2012-07-30 13:19:07 -0700506 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
507 sg, sgl_count, !sg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700508 sg_init_table(sg, sgl_count);
509
510 tv_cmd->tvc_sgl = sg;
511 tv_cmd->tvc_sgl_count = sgl_count;
512
513 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
514 for (i = 0; i < niov; i++) {
515 ret = vhost_scsi_map_to_sgl(sg, sgl_count, iov[i].iov_base,
516 iov[i].iov_len, write);
517 if (ret < 0) {
518 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
519 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
520 kfree(tv_cmd->tvc_sgl);
521 tv_cmd->tvc_sgl = NULL;
522 tv_cmd->tvc_sgl_count = 0;
523 return ret;
524 }
525
526 sg += ret;
527 sgl_count -= ret;
528 }
529 return 0;
530}
531
532static void tcm_vhost_submission_work(struct work_struct *work)
533{
534 struct tcm_vhost_cmd *tv_cmd =
535 container_of(work, struct tcm_vhost_cmd, work);
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700536 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700537 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
538 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
539 int rc, sg_no_bidi = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700540
541 if (tv_cmd->tvc_sgl_count) {
542 sg_ptr = tv_cmd->tvc_sgl;
543 /*
544 * For BIDI commands, pass in the extra READ buffer
545 * to transport_generic_map_mem_to_cmd() below..
546 */
547/* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
548#if 0
549 if (se_cmd->se_cmd_flags & SCF_BIDI) {
550 sg_bidi_ptr = NULL;
551 sg_no_bidi = 0;
552 }
553#endif
554 } else {
555 sg_ptr = NULL;
556 }
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700557 tv_nexus = tv_cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700558
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700559 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
560 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
561 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
562 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
563 0, sg_ptr, tv_cmd->tvc_sgl_count,
564 sg_bidi_ptr, sg_no_bidi);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700565 if (rc < 0) {
566 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700567 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700568 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700569 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700570}
571
572static void vhost_scsi_handle_vq(struct vhost_scsi *vs)
573{
574 struct vhost_virtqueue *vq = &vs->vqs[2];
575 struct virtio_scsi_cmd_req v_req;
576 struct tcm_vhost_tpg *tv_tpg;
577 struct tcm_vhost_cmd *tv_cmd;
578 u32 exp_data_len, data_first, data_num, data_direction;
579 unsigned out, in, i;
580 int head, ret;
581
582 /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
583 tv_tpg = vs->vs_tpg;
584 if (unlikely(!tv_tpg)) {
585 pr_err("%s endpoint not set\n", __func__);
586 return;
587 }
588
589 mutex_lock(&vq->mutex);
590 vhost_disable_notify(&vs->dev, vq);
591
592 for (;;) {
593 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
594 ARRAY_SIZE(vq->iov), &out, &in,
595 NULL, NULL);
596 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
597 head, out, in);
598 /* On error, stop handling until the next kick. */
599 if (unlikely(head < 0))
600 break;
601 /* Nothing new? Wait for eventfd to tell us they refilled. */
602 if (head == vq->num) {
603 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
604 vhost_disable_notify(&vs->dev, vq);
605 continue;
606 }
607 break;
608 }
609
610/* FIXME: BIDI operation */
611 if (out == 1 && in == 1) {
612 data_direction = DMA_NONE;
613 data_first = 0;
614 data_num = 0;
615 } else if (out == 1 && in > 1) {
616 data_direction = DMA_FROM_DEVICE;
617 data_first = out + 1;
618 data_num = in - 1;
619 } else if (out > 1 && in == 1) {
620 data_direction = DMA_TO_DEVICE;
621 data_first = 1;
622 data_num = out - 1;
623 } else {
624 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
625 out, in);
626 break;
627 }
628
629 /*
630 * Check for a sane resp buffer so we can report errors to
631 * the guest.
632 */
633 if (unlikely(vq->iov[out].iov_len !=
634 sizeof(struct virtio_scsi_cmd_resp))) {
635 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
636 " bytes\n", vq->iov[out].iov_len);
637 break;
638 }
639
640 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
641 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
642 " bytes\n", vq->iov[0].iov_len);
643 break;
644 }
645 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
646 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
647 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
648 sizeof(v_req));
649 if (unlikely(ret)) {
650 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
651 break;
652 }
653
654 exp_data_len = 0;
655 for (i = 0; i < data_num; i++)
656 exp_data_len += vq->iov[data_first + i].iov_len;
657
658 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
659 exp_data_len, data_direction);
660 if (IS_ERR(tv_cmd)) {
661 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
662 PTR_ERR(tv_cmd));
663 break;
664 }
665 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
666 ": %d\n", tv_cmd, exp_data_len, data_direction);
667
668 tv_cmd->tvc_vhost = vs;
669
670 if (unlikely(vq->iov[out].iov_len !=
671 sizeof(struct virtio_scsi_cmd_resp))) {
672 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
673 " bytes, out: %d, in: %d\n",
674 vq->iov[out].iov_len, out, in);
675 break;
676 }
677
678 tv_cmd->tvc_resp = vq->iov[out].iov_base;
679
680 /*
681 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
682 * that will be used by tcm_vhost_new_cmd_map() and down into
683 * target_setup_cmd_from_cdb()
684 */
685 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
686 /*
687 * Check that the recieved CDB size does not exceeded our
688 * hardcoded max for tcm_vhost
689 */
690 /* TODO what if cdb was too small for varlen cdb header? */
691 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
692 TCM_VHOST_MAX_CDB_SIZE)) {
693 vq_err(vq, "Received SCSI CDB with command_size: %d that"
694 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
695 scsi_command_size(tv_cmd->tvc_cdb),
696 TCM_VHOST_MAX_CDB_SIZE);
697 break; /* TODO */
698 }
699 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
700
701 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
702 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
703
704 if (data_direction != DMA_NONE) {
705 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
706 &vq->iov[data_first], data_num,
707 data_direction == DMA_TO_DEVICE);
708 if (unlikely(ret)) {
709 vq_err(vq, "Failed to map iov to sgl\n");
710 break; /* TODO */
711 }
712 }
713
714 /*
715 * Save the descriptor from vhost_get_vq_desc() to be used to
716 * complete the virtio-scsi request in TCM callback context via
717 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
718 */
719 tv_cmd->tvc_vq_desc = head;
720 /*
721 * Dispatch tv_cmd descriptor for cmwq execution in process
722 * context provided by tcm_vhost_workqueue. This also ensures
723 * tv_cmd is executed on the same kworker CPU as this vhost
724 * thread to gain positive L2 cache locality effects..
725 */
726 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
727 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
728 }
729
730 mutex_unlock(&vq->mutex);
731}
732
733static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
734{
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700735 pr_debug("%s: The handling func for control queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700736}
737
738static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
739{
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700740 pr_debug("%s: The handling func for event queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700741}
742
743static void vhost_scsi_handle_kick(struct vhost_work *work)
744{
745 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
746 poll.work);
747 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
748
749 vhost_scsi_handle_vq(vs);
750}
751
752/*
753 * Called from vhost_scsi_ioctl() context to walk the list of available
754 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
755 */
756static int vhost_scsi_set_endpoint(
757 struct vhost_scsi *vs,
758 struct vhost_scsi_target *t)
759{
760 struct tcm_vhost_tport *tv_tport;
761 struct tcm_vhost_tpg *tv_tpg;
762 int index;
763
764 mutex_lock(&vs->dev.mutex);
765 /* Verify that ring has been setup correctly. */
766 for (index = 0; index < vs->dev.nvqs; ++index) {
767 /* Verify that ring has been setup correctly. */
768 if (!vhost_vq_access_ok(&vs->vqs[index])) {
769 mutex_unlock(&vs->dev.mutex);
770 return -EFAULT;
771 }
772 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700773 mutex_unlock(&vs->dev.mutex);
774
775 mutex_lock(&tcm_vhost_mutex);
776 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
777 mutex_lock(&tv_tpg->tv_tpg_mutex);
778 if (!tv_tpg->tpg_nexus) {
779 mutex_unlock(&tv_tpg->tv_tpg_mutex);
780 continue;
781 }
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700782 if (tv_tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700783 mutex_unlock(&tv_tpg->tv_tpg_mutex);
784 continue;
785 }
786 tv_tport = tv_tpg->tport;
787
788 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn) &&
789 (tv_tpg->tport_tpgt == t->vhost_tpgt)) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700790 tv_tpg->tv_tpg_vhost_count++;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700791 mutex_unlock(&tv_tpg->tv_tpg_mutex);
792 mutex_unlock(&tcm_vhost_mutex);
793
794 mutex_lock(&vs->dev.mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700795 if (vs->vs_tpg) {
796 mutex_unlock(&vs->dev.mutex);
797 mutex_lock(&tv_tpg->tv_tpg_mutex);
798 tv_tpg->tv_tpg_vhost_count--;
799 mutex_unlock(&tv_tpg->tv_tpg_mutex);
800 return -EEXIST;
801 }
802
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700803 vs->vs_tpg = tv_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700804 smp_mb__after_atomic_inc();
805 mutex_unlock(&vs->dev.mutex);
806 return 0;
807 }
808 mutex_unlock(&tv_tpg->tv_tpg_mutex);
809 }
810 mutex_unlock(&tcm_vhost_mutex);
811 return -EINVAL;
812}
813
814static int vhost_scsi_clear_endpoint(
815 struct vhost_scsi *vs,
816 struct vhost_scsi_target *t)
817{
818 struct tcm_vhost_tport *tv_tport;
819 struct tcm_vhost_tpg *tv_tpg;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700820 int index, ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700821
822 mutex_lock(&vs->dev.mutex);
823 /* Verify that ring has been setup correctly. */
824 for (index = 0; index < vs->dev.nvqs; ++index) {
825 if (!vhost_vq_access_ok(&vs->vqs[index])) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700826 ret = -EFAULT;
827 goto err;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700828 }
829 }
830
831 if (!vs->vs_tpg) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700832 ret = -ENODEV;
833 goto err;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700834 }
835 tv_tpg = vs->vs_tpg;
836 tv_tport = tv_tpg->tport;
837
838 if (strcmp(tv_tport->tport_name, t->vhost_wwpn) ||
839 (tv_tpg->tport_tpgt != t->vhost_tpgt)) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700840 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
841 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
842 tv_tport->tport_name, tv_tpg->tport_tpgt,
843 t->vhost_wwpn, t->vhost_tpgt);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700844 ret = -EINVAL;
845 goto err;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700846 }
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700847 tv_tpg->tv_tpg_vhost_count--;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700848 vs->vs_tpg = NULL;
849 mutex_unlock(&vs->dev.mutex);
850
851 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700852
853err:
854 mutex_unlock(&vs->dev.mutex);
855 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700856}
857
858static int vhost_scsi_open(struct inode *inode, struct file *f)
859{
860 struct vhost_scsi *s;
861 int r;
862
863 s = kzalloc(sizeof(*s), GFP_KERNEL);
864 if (!s)
865 return -ENOMEM;
866
867 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
868 INIT_LIST_HEAD(&s->vs_completion_list);
869 spin_lock_init(&s->vs_completion_lock);
870
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700871 s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
872 s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
873 s->vqs[VHOST_SCSI_VQ_IO].handle_kick = vhost_scsi_handle_kick;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700874 r = vhost_dev_init(&s->dev, s->vqs, 3);
875 if (r < 0) {
876 kfree(s);
877 return r;
878 }
879
880 f->private_data = s;
881 return 0;
882}
883
884static int vhost_scsi_release(struct inode *inode, struct file *f)
885{
886 struct vhost_scsi *s = f->private_data;
887
888 if (s->vs_tpg && s->vs_tpg->tport) {
889 struct vhost_scsi_target backend;
890
891 memcpy(backend.vhost_wwpn, s->vs_tpg->tport->tport_name,
892 sizeof(backend.vhost_wwpn));
893 backend.vhost_tpgt = s->vs_tpg->tport_tpgt;
894 vhost_scsi_clear_endpoint(s, &backend);
895 }
896
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000897 vhost_dev_stop(&s->dev);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700898 vhost_dev_cleanup(&s->dev, false);
899 kfree(s);
900 return 0;
901}
902
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700903static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
904{
905 vhost_poll_flush(&vs->dev.vqs[index].poll);
906}
907
908static void vhost_scsi_flush(struct vhost_scsi *vs)
909{
910 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_CTL);
911 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_EVT);
912 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_IO);
913}
914
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700915static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
916{
917 if (features & ~VHOST_FEATURES)
918 return -EOPNOTSUPP;
919
920 mutex_lock(&vs->dev.mutex);
921 if ((features & (1 << VHOST_F_LOG_ALL)) &&
922 !vhost_log_access_ok(&vs->dev)) {
923 mutex_unlock(&vs->dev.mutex);
924 return -EFAULT;
925 }
926 vs->dev.acked_features = features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700927 smp_wmb();
928 vhost_scsi_flush(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700929 mutex_unlock(&vs->dev.mutex);
930 return 0;
931}
932
933static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
934 unsigned long arg)
935{
936 struct vhost_scsi *vs = f->private_data;
937 struct vhost_scsi_target backend;
938 void __user *argp = (void __user *)arg;
939 u64 __user *featurep = argp;
940 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700941 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700942
943 switch (ioctl) {
944 case VHOST_SCSI_SET_ENDPOINT:
945 if (copy_from_user(&backend, argp, sizeof backend))
946 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -0700947 if (backend.reserved != 0)
948 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700949
950 return vhost_scsi_set_endpoint(vs, &backend);
951 case VHOST_SCSI_CLEAR_ENDPOINT:
952 if (copy_from_user(&backend, argp, sizeof backend))
953 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -0700954 if (backend.reserved != 0)
955 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700956
957 return vhost_scsi_clear_endpoint(vs, &backend);
958 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700959 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700960 return -EFAULT;
961 return 0;
962 case VHOST_GET_FEATURES:
963 features = VHOST_FEATURES;
964 if (copy_to_user(featurep, &features, sizeof features))
965 return -EFAULT;
966 return 0;
967 case VHOST_SET_FEATURES:
968 if (copy_from_user(&features, featurep, sizeof features))
969 return -EFAULT;
970 return vhost_scsi_set_features(vs, features);
971 default:
972 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200973 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
974 /* TODO: flush backend after dev ioctl. */
975 if (r == -ENOIOCTLCMD)
976 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700977 mutex_unlock(&vs->dev.mutex);
978 return r;
979 }
980}
981
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700982#ifdef CONFIG_COMPAT
983static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
984 unsigned long arg)
985{
986 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
987}
988#endif
989
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700990static const struct file_operations vhost_scsi_fops = {
991 .owner = THIS_MODULE,
992 .release = vhost_scsi_release,
993 .unlocked_ioctl = vhost_scsi_ioctl,
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700994#ifdef CONFIG_COMPAT
995 .compat_ioctl = vhost_scsi_compat_ioctl,
996#endif
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700997 .open = vhost_scsi_open,
998 .llseek = noop_llseek,
999};
1000
1001static struct miscdevice vhost_scsi_misc = {
1002 MISC_DYNAMIC_MINOR,
1003 "vhost-scsi",
1004 &vhost_scsi_fops,
1005};
1006
1007static int __init vhost_scsi_register(void)
1008{
1009 return misc_register(&vhost_scsi_misc);
1010}
1011
1012static int vhost_scsi_deregister(void)
1013{
1014 return misc_deregister(&vhost_scsi_misc);
1015}
1016
1017static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
1018{
1019 switch (tport->tport_proto_id) {
1020 case SCSI_PROTOCOL_SAS:
1021 return "SAS";
1022 case SCSI_PROTOCOL_FCP:
1023 return "FCP";
1024 case SCSI_PROTOCOL_ISCSI:
1025 return "iSCSI";
1026 default:
1027 break;
1028 }
1029
1030 return "Unknown";
1031}
1032
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001033static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001034 struct se_lun *lun)
1035{
1036 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1037 struct tcm_vhost_tpg, se_tpg);
1038
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001039 mutex_lock(&tv_tpg->tv_tpg_mutex);
1040 tv_tpg->tv_tpg_port_count++;
1041 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001042
1043 return 0;
1044}
1045
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001046static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001047 struct se_lun *se_lun)
1048{
1049 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1050 struct tcm_vhost_tpg, se_tpg);
1051
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001052 mutex_lock(&tv_tpg->tv_tpg_mutex);
1053 tv_tpg->tv_tpg_port_count--;
1054 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001055}
1056
1057static struct se_node_acl *tcm_vhost_make_nodeacl(
1058 struct se_portal_group *se_tpg,
1059 struct config_group *group,
1060 const char *name)
1061{
1062 struct se_node_acl *se_nacl, *se_nacl_new;
1063 struct tcm_vhost_nacl *nacl;
1064 u64 wwpn = 0;
1065 u32 nexus_depth;
1066
1067 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1068 return ERR_PTR(-EINVAL); */
1069 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1070 if (!se_nacl_new)
1071 return ERR_PTR(-ENOMEM);
1072
1073 nexus_depth = 1;
1074 /*
1075 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1076 * when converting a NodeACL from demo mode -> explict
1077 */
1078 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1079 name, nexus_depth);
1080 if (IS_ERR(se_nacl)) {
1081 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1082 return se_nacl;
1083 }
1084 /*
1085 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1086 */
1087 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1088 nacl->iport_wwpn = wwpn;
1089
1090 return se_nacl;
1091}
1092
1093static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1094{
1095 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1096 struct tcm_vhost_nacl, se_node_acl);
1097 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1098 kfree(nacl);
1099}
1100
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001101static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001102 const char *name)
1103{
1104 struct se_portal_group *se_tpg;
1105 struct tcm_vhost_nexus *tv_nexus;
1106
1107 mutex_lock(&tv_tpg->tv_tpg_mutex);
1108 if (tv_tpg->tpg_nexus) {
1109 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1110 pr_debug("tv_tpg->tpg_nexus already exists\n");
1111 return -EEXIST;
1112 }
1113 se_tpg = &tv_tpg->se_tpg;
1114
1115 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1116 if (!tv_nexus) {
1117 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1118 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1119 return -ENOMEM;
1120 }
1121 /*
1122 * Initialize the struct se_session pointer
1123 */
1124 tv_nexus->tvn_se_sess = transport_init_session();
1125 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1126 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1127 kfree(tv_nexus);
1128 return -ENOMEM;
1129 }
1130 /*
1131 * Since we are running in 'demo mode' this call with generate a
1132 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1133 * the SCSI Initiator port name of the passed configfs group 'name'.
1134 */
1135 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1136 se_tpg, (unsigned char *)name);
1137 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1138 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1139 pr_debug("core_tpg_check_initiator_node_acl() failed"
1140 " for %s\n", name);
1141 transport_free_session(tv_nexus->tvn_se_sess);
1142 kfree(tv_nexus);
1143 return -ENOMEM;
1144 }
1145 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001146 * Now register the TCM vhost virtual I_T Nexus as active with the
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001147 * call to __transport_register_session()
1148 */
1149 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1150 tv_nexus->tvn_se_sess, tv_nexus);
1151 tv_tpg->tpg_nexus = tv_nexus;
1152
1153 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1154 return 0;
1155}
1156
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001157static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001158{
1159 struct se_session *se_sess;
1160 struct tcm_vhost_nexus *tv_nexus;
1161
1162 mutex_lock(&tpg->tv_tpg_mutex);
1163 tv_nexus = tpg->tpg_nexus;
1164 if (!tv_nexus) {
1165 mutex_unlock(&tpg->tv_tpg_mutex);
1166 return -ENODEV;
1167 }
1168
1169 se_sess = tv_nexus->tvn_se_sess;
1170 if (!se_sess) {
1171 mutex_unlock(&tpg->tv_tpg_mutex);
1172 return -ENODEV;
1173 }
1174
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001175 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001176 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001177 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001178 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001179 tpg->tv_tpg_port_count);
1180 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001181 }
1182
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001183 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001184 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001185 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001186 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001187 tpg->tv_tpg_vhost_count);
1188 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001189 }
1190
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001191 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001192 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1193 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1194 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001195 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001196 */
1197 transport_deregister_session(tv_nexus->tvn_se_sess);
1198 tpg->tpg_nexus = NULL;
1199 mutex_unlock(&tpg->tv_tpg_mutex);
1200
1201 kfree(tv_nexus);
1202 return 0;
1203}
1204
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001205static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001206 char *page)
1207{
1208 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1209 struct tcm_vhost_tpg, se_tpg);
1210 struct tcm_vhost_nexus *tv_nexus;
1211 ssize_t ret;
1212
1213 mutex_lock(&tv_tpg->tv_tpg_mutex);
1214 tv_nexus = tv_tpg->tpg_nexus;
1215 if (!tv_nexus) {
1216 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1217 return -ENODEV;
1218 }
1219 ret = snprintf(page, PAGE_SIZE, "%s\n",
1220 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1221 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1222
1223 return ret;
1224}
1225
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001226static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001227 const char *page,
1228 size_t count)
1229{
1230 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1231 struct tcm_vhost_tpg, se_tpg);
1232 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1233 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1234 int ret;
1235 /*
1236 * Shutdown the active I_T nexus if 'NULL' is passed..
1237 */
1238 if (!strncmp(page, "NULL", 4)) {
1239 ret = tcm_vhost_drop_nexus(tv_tpg);
1240 return (!ret) ? count : ret;
1241 }
1242 /*
1243 * Otherwise make sure the passed virtual Initiator port WWN matches
1244 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1245 * tcm_vhost_make_nexus().
1246 */
1247 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1248 pr_err("Emulated NAA Sas Address: %s, exceeds"
1249 " max: %d\n", page, TCM_VHOST_NAMELEN);
1250 return -EINVAL;
1251 }
1252 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1253
1254 ptr = strstr(i_port, "naa.");
1255 if (ptr) {
1256 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1257 pr_err("Passed SAS Initiator Port %s does not"
1258 " match target port protoid: %s\n", i_port,
1259 tcm_vhost_dump_proto_id(tport_wwn));
1260 return -EINVAL;
1261 }
1262 port_ptr = &i_port[0];
1263 goto check_newline;
1264 }
1265 ptr = strstr(i_port, "fc.");
1266 if (ptr) {
1267 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1268 pr_err("Passed FCP Initiator Port %s does not"
1269 " match target port protoid: %s\n", i_port,
1270 tcm_vhost_dump_proto_id(tport_wwn));
1271 return -EINVAL;
1272 }
1273 port_ptr = &i_port[3]; /* Skip over "fc." */
1274 goto check_newline;
1275 }
1276 ptr = strstr(i_port, "iqn.");
1277 if (ptr) {
1278 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1279 pr_err("Passed iSCSI Initiator Port %s does not"
1280 " match target port protoid: %s\n", i_port,
1281 tcm_vhost_dump_proto_id(tport_wwn));
1282 return -EINVAL;
1283 }
1284 port_ptr = &i_port[0];
1285 goto check_newline;
1286 }
1287 pr_err("Unable to locate prefix for emulated Initiator Port:"
1288 " %s\n", i_port);
1289 return -EINVAL;
1290 /*
1291 * Clear any trailing newline for the NAA WWN
1292 */
1293check_newline:
1294 if (i_port[strlen(i_port)-1] == '\n')
1295 i_port[strlen(i_port)-1] = '\0';
1296
1297 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1298 if (ret < 0)
1299 return ret;
1300
1301 return count;
1302}
1303
1304TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1305
1306static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1307 &tcm_vhost_tpg_nexus.attr,
1308 NULL,
1309};
1310
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001311static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001312 struct config_group *group,
1313 const char *name)
1314{
1315 struct tcm_vhost_tport *tport = container_of(wwn,
1316 struct tcm_vhost_tport, tport_wwn);
1317
1318 struct tcm_vhost_tpg *tpg;
1319 unsigned long tpgt;
1320 int ret;
1321
1322 if (strstr(name, "tpgt_") != name)
1323 return ERR_PTR(-EINVAL);
1324 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1325 return ERR_PTR(-EINVAL);
1326
1327 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1328 if (!tpg) {
1329 pr_err("Unable to allocate struct tcm_vhost_tpg");
1330 return ERR_PTR(-ENOMEM);
1331 }
1332 mutex_init(&tpg->tv_tpg_mutex);
1333 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1334 tpg->tport = tport;
1335 tpg->tport_tpgt = tpgt;
1336
1337 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1338 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1339 if (ret < 0) {
1340 kfree(tpg);
1341 return NULL;
1342 }
1343 mutex_lock(&tcm_vhost_mutex);
1344 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1345 mutex_unlock(&tcm_vhost_mutex);
1346
1347 return &tpg->se_tpg;
1348}
1349
1350static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1351{
1352 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1353 struct tcm_vhost_tpg, se_tpg);
1354
1355 mutex_lock(&tcm_vhost_mutex);
1356 list_del(&tpg->tv_tpg_list);
1357 mutex_unlock(&tcm_vhost_mutex);
1358 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001359 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001360 */
1361 tcm_vhost_drop_nexus(tpg);
1362 /*
1363 * Deregister the se_tpg from TCM..
1364 */
1365 core_tpg_deregister(se_tpg);
1366 kfree(tpg);
1367}
1368
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001369static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001370 struct config_group *group,
1371 const char *name)
1372{
1373 struct tcm_vhost_tport *tport;
1374 char *ptr;
1375 u64 wwpn = 0;
1376 int off = 0;
1377
1378 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1379 return ERR_PTR(-EINVAL); */
1380
1381 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1382 if (!tport) {
1383 pr_err("Unable to allocate struct tcm_vhost_tport");
1384 return ERR_PTR(-ENOMEM);
1385 }
1386 tport->tport_wwpn = wwpn;
1387 /*
1388 * Determine the emulated Protocol Identifier and Target Port Name
1389 * based on the incoming configfs directory name.
1390 */
1391 ptr = strstr(name, "naa.");
1392 if (ptr) {
1393 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1394 goto check_len;
1395 }
1396 ptr = strstr(name, "fc.");
1397 if (ptr) {
1398 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1399 off = 3; /* Skip over "fc." */
1400 goto check_len;
1401 }
1402 ptr = strstr(name, "iqn.");
1403 if (ptr) {
1404 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1405 goto check_len;
1406 }
1407
1408 pr_err("Unable to locate prefix for emulated Target Port:"
1409 " %s\n", name);
1410 kfree(tport);
1411 return ERR_PTR(-EINVAL);
1412
1413check_len:
1414 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1415 pr_err("Emulated %s Address: %s, exceeds"
1416 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1417 TCM_VHOST_NAMELEN);
1418 kfree(tport);
1419 return ERR_PTR(-EINVAL);
1420 }
1421 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1422
1423 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1424 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1425
1426 return &tport->tport_wwn;
1427}
1428
1429static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1430{
1431 struct tcm_vhost_tport *tport = container_of(wwn,
1432 struct tcm_vhost_tport, tport_wwn);
1433
1434 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1435 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1436 tport->tport_name);
1437
1438 kfree(tport);
1439}
1440
1441static ssize_t tcm_vhost_wwn_show_attr_version(
1442 struct target_fabric_configfs *tf,
1443 char *page)
1444{
1445 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1446 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1447 utsname()->machine);
1448}
1449
1450TF_WWN_ATTR_RO(tcm_vhost, version);
1451
1452static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1453 &tcm_vhost_wwn_version.attr,
1454 NULL,
1455};
1456
1457static struct target_core_fabric_ops tcm_vhost_ops = {
1458 .get_fabric_name = tcm_vhost_get_fabric_name,
1459 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1460 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1461 .tpg_get_tag = tcm_vhost_get_tag,
1462 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1463 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1464 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1465 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1466 .tpg_check_demo_mode = tcm_vhost_check_true,
1467 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1468 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1469 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1470 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1471 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1472 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1473 .release_cmd = tcm_vhost_release_cmd,
1474 .shutdown_session = tcm_vhost_shutdown_session,
1475 .close_session = tcm_vhost_close_session,
1476 .sess_get_index = tcm_vhost_sess_get_index,
1477 .sess_get_initiator_sid = NULL,
1478 .write_pending = tcm_vhost_write_pending,
1479 .write_pending_status = tcm_vhost_write_pending_status,
1480 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1481 .get_task_tag = tcm_vhost_get_task_tag,
1482 .get_cmd_state = tcm_vhost_get_cmd_state,
1483 .queue_data_in = tcm_vhost_queue_data_in,
1484 .queue_status = tcm_vhost_queue_status,
1485 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001486 /*
1487 * Setup callers for generic logic in target_core_fabric_configfs.c
1488 */
1489 .fabric_make_wwn = tcm_vhost_make_tport,
1490 .fabric_drop_wwn = tcm_vhost_drop_tport,
1491 .fabric_make_tpg = tcm_vhost_make_tpg,
1492 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1493 .fabric_post_link = tcm_vhost_port_link,
1494 .fabric_pre_unlink = tcm_vhost_port_unlink,
1495 .fabric_make_np = NULL,
1496 .fabric_drop_np = NULL,
1497 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1498 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1499};
1500
1501static int tcm_vhost_register_configfs(void)
1502{
1503 struct target_fabric_configfs *fabric;
1504 int ret;
1505
1506 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1507 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1508 utsname()->machine);
1509 /*
1510 * Register the top level struct config_item_type with TCM core
1511 */
1512 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1513 if (IS_ERR(fabric)) {
1514 pr_err("target_fabric_configfs_init() failed\n");
1515 return PTR_ERR(fabric);
1516 }
1517 /*
1518 * Setup fabric->tf_ops from our local tcm_vhost_ops
1519 */
1520 fabric->tf_ops = tcm_vhost_ops;
1521 /*
1522 * Setup default attribute lists for various fabric->tf_cit_tmpl
1523 */
1524 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1525 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1526 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1527 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1528 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1529 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1530 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1531 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1532 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1533 /*
1534 * Register the fabric for use within TCM
1535 */
1536 ret = target_fabric_configfs_register(fabric);
1537 if (ret < 0) {
1538 pr_err("target_fabric_configfs_register() failed"
1539 " for TCM_VHOST\n");
1540 return ret;
1541 }
1542 /*
1543 * Setup our local pointer to *fabric
1544 */
1545 tcm_vhost_fabric_configfs = fabric;
1546 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1547 return 0;
1548};
1549
1550static void tcm_vhost_deregister_configfs(void)
1551{
1552 if (!tcm_vhost_fabric_configfs)
1553 return;
1554
1555 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1556 tcm_vhost_fabric_configfs = NULL;
1557 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1558};
1559
1560static int __init tcm_vhost_init(void)
1561{
1562 int ret = -ENOMEM;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001563 /*
1564 * Use our own dedicated workqueue for submitting I/O into
1565 * target core to avoid contention within system_wq.
1566 */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001567 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1568 if (!tcm_vhost_workqueue)
1569 goto out;
1570
1571 ret = vhost_scsi_register();
1572 if (ret < 0)
1573 goto out_destroy_workqueue;
1574
1575 ret = tcm_vhost_register_configfs();
1576 if (ret < 0)
1577 goto out_vhost_scsi_deregister;
1578
1579 return 0;
1580
1581out_vhost_scsi_deregister:
1582 vhost_scsi_deregister();
1583out_destroy_workqueue:
1584 destroy_workqueue(tcm_vhost_workqueue);
1585out:
1586 return ret;
1587};
1588
1589static void tcm_vhost_exit(void)
1590{
1591 tcm_vhost_deregister_configfs();
1592 vhost_scsi_deregister();
1593 destroy_workqueue(tcm_vhost_workqueue);
1594};
1595
1596MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1597MODULE_LICENSE("GPL");
1598module_init(tcm_vhost_init);
1599module_exit(tcm_vhost_exit);