blob: 77d760bb2e44b4fb7626ab460e00b0f95bd10f1b [file] [log] [blame]
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001/*******************************************************************************
2 * Vhost kernel TCM fabric driver for virtio SCSI initiators
3 *
4 * (C) Copyright 2010-2012 RisingTide Systems LLC.
5 * (C) Copyright 2010-2012 IBM Corp.
6 *
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 *
9 * Authors: Nicholas A. Bellinger <nab@risingtidesystems.com>
10 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 ****************************************************************************/
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <generated/utsrelease.h>
27#include <linux/utsname.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/kthread.h>
31#include <linux/types.h>
32#include <linux/string.h>
33#include <linux/configfs.h>
34#include <linux/ctype.h>
35#include <linux/compat.h>
36#include <linux/eventfd.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070037#include <linux/fs.h>
38#include <linux/miscdevice.h>
39#include <asm/unaligned.h>
40#include <scsi/scsi.h>
41#include <scsi/scsi_tcq.h>
42#include <target/target_core_base.h>
43#include <target/target_core_fabric.h>
44#include <target/target_core_fabric_configfs.h>
45#include <target/target_core_configfs.h>
46#include <target/configfs_macros.h>
47#include <linux/vhost.h>
48#include <linux/virtio_net.h> /* TODO vhost.h currently depends on this */
49#include <linux/virtio_scsi.h>
Asias He9d6064a2013-01-06 14:36:13 +080050#include <linux/llist.h>
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070051
52#include "vhost.c"
53#include "vhost.h"
54#include "tcm_vhost.h"
55
Nicholas Bellinger101998f2012-07-30 13:30:00 -070056enum {
57 VHOST_SCSI_VQ_CTL = 0,
58 VHOST_SCSI_VQ_EVT = 1,
59 VHOST_SCSI_VQ_IO = 2,
60};
61
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070062struct vhost_scsi {
Nicholas Bellinger101998f2012-07-30 13:30:00 -070063 struct tcm_vhost_tpg *vs_tpg; /* Protected by vhost_scsi->dev.mutex */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070064 struct vhost_dev dev;
65 struct vhost_virtqueue vqs[3];
66
67 struct vhost_work vs_completion_work; /* cmd completion work item */
Asias He9d6064a2013-01-06 14:36:13 +080068 struct llist_head vs_completion_list; /* cmd completion queue */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -070069};
70
71/* Local pointer to allocated TCM configfs fabric module */
72static struct target_fabric_configfs *tcm_vhost_fabric_configfs;
73
74static struct workqueue_struct *tcm_vhost_workqueue;
75
76/* Global spinlock to protect tcm_vhost TPG list for vhost IOCTL access */
77static DEFINE_MUTEX(tcm_vhost_mutex);
78static LIST_HEAD(tcm_vhost_list);
79
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) {
Masanari Iida744627e92012-11-05 23:30:40 +0900233 pr_err("Unable to allocate struct tcm_vhost_nacl\n");
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700234 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
Asias He9d6064a2013-01-06 14:36:13 +0800304 llist_add(&tv_cmd->tvc_completion_list, &vs->vs_completion_list);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700305
306 vhost_work_queue(&vs->dev, &vs->vs_completion_work);
307}
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700308
309static int tcm_vhost_queue_data_in(struct se_cmd *se_cmd)
310{
311 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
312 struct tcm_vhost_cmd, tvc_se_cmd);
313 vhost_scsi_complete_cmd(tv_cmd);
314 return 0;
315}
316
317static int tcm_vhost_queue_status(struct se_cmd *se_cmd)
318{
319 struct tcm_vhost_cmd *tv_cmd = container_of(se_cmd,
320 struct tcm_vhost_cmd, tvc_se_cmd);
321 vhost_scsi_complete_cmd(tv_cmd);
322 return 0;
323}
324
325static int tcm_vhost_queue_tm_rsp(struct se_cmd *se_cmd)
326{
327 return 0;
328}
329
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700330static void vhost_scsi_free_cmd(struct tcm_vhost_cmd *tv_cmd)
331{
332 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
333
334 /* TODO locking against target/backend threads? */
335 transport_generic_free_cmd(se_cmd, 1);
336
337 if (tv_cmd->tvc_sgl_count) {
338 u32 i;
339 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
340 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
341
342 kfree(tv_cmd->tvc_sgl);
343 }
344
345 kfree(tv_cmd);
346}
347
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700348/* Fill in status and signal that we are done processing this command
349 *
350 * This is scheduled in the vhost work queue so we are called with the owner
351 * process mm and can access the vring.
352 */
353static void vhost_scsi_complete_cmd_work(struct vhost_work *work)
354{
355 struct vhost_scsi *vs = container_of(work, struct vhost_scsi,
356 vs_completion_work);
Asias He9d6064a2013-01-06 14:36:13 +0800357 struct virtio_scsi_cmd_resp v_rsp;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700358 struct tcm_vhost_cmd *tv_cmd;
Asias He9d6064a2013-01-06 14:36:13 +0800359 struct llist_node *llnode;
360 struct se_cmd *se_cmd;
361 int ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700362
Asias He9d6064a2013-01-06 14:36:13 +0800363 llnode = llist_del_all(&vs->vs_completion_list);
364 while (llnode) {
365 tv_cmd = llist_entry(llnode, struct tcm_vhost_cmd,
366 tvc_completion_list);
367 llnode = llist_next(llnode);
368 se_cmd = &tv_cmd->tvc_se_cmd;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700369
370 pr_debug("%s tv_cmd %p resid %u status %#02x\n", __func__,
371 tv_cmd, se_cmd->residual_count, se_cmd->scsi_status);
372
373 memset(&v_rsp, 0, sizeof(v_rsp));
374 v_rsp.resid = se_cmd->residual_count;
375 /* TODO is status_qualifier field needed? */
376 v_rsp.status = se_cmd->scsi_status;
377 v_rsp.sense_len = se_cmd->scsi_sense_length;
378 memcpy(v_rsp.sense, tv_cmd->tvc_sense_buf,
379 v_rsp.sense_len);
380 ret = copy_to_user(tv_cmd->tvc_resp, &v_rsp, sizeof(v_rsp));
381 if (likely(ret == 0))
382 vhost_add_used(&vs->vqs[2], tv_cmd->tvc_vq_desc, 0);
383 else
384 pr_err("Faulted on virtio_scsi_cmd_resp\n");
385
386 vhost_scsi_free_cmd(tv_cmd);
387 }
388
389 vhost_signal(&vs->dev, &vs->vqs[2]);
390}
391
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700392static struct tcm_vhost_cmd *vhost_scsi_allocate_cmd(
393 struct tcm_vhost_tpg *tv_tpg,
394 struct virtio_scsi_cmd_req *v_req,
395 u32 exp_data_len,
396 int data_direction)
397{
398 struct tcm_vhost_cmd *tv_cmd;
399 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700400
401 tv_nexus = tv_tpg->tpg_nexus;
402 if (!tv_nexus) {
403 pr_err("Unable to locate active struct tcm_vhost_nexus\n");
404 return ERR_PTR(-EIO);
405 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700406
407 tv_cmd = kzalloc(sizeof(struct tcm_vhost_cmd), GFP_ATOMIC);
408 if (!tv_cmd) {
409 pr_err("Unable to allocate struct tcm_vhost_cmd\n");
410 return ERR_PTR(-ENOMEM);
411 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700412 tv_cmd->tvc_tag = v_req->tag;
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700413 tv_cmd->tvc_task_attr = v_req->task_attr;
414 tv_cmd->tvc_exp_data_len = exp_data_len;
415 tv_cmd->tvc_data_direction = data_direction;
416 tv_cmd->tvc_nexus = tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700417
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700418 return tv_cmd;
419}
420
421/*
422 * Map a user memory range into a scatterlist
423 *
424 * Returns the number of scatterlist entries used or -errno on error.
425 */
426static int vhost_scsi_map_to_sgl(struct scatterlist *sgl,
427 unsigned int sgl_count, void __user *ptr, size_t len, int write)
428{
429 struct scatterlist *sg = sgl;
430 unsigned int npages = 0;
431 int ret;
432
433 while (len > 0) {
434 struct page *page;
435 unsigned int offset = (uintptr_t)ptr & ~PAGE_MASK;
436 unsigned int nbytes = min_t(unsigned int,
437 PAGE_SIZE - offset, len);
438
439 if (npages == sgl_count) {
440 ret = -ENOBUFS;
441 goto err;
442 }
443
444 ret = get_user_pages_fast((unsigned long)ptr, 1, write, &page);
445 BUG_ON(ret == 0); /* we should either get our page or fail */
446 if (ret < 0)
447 goto err;
448
449 sg_set_page(sg, page, nbytes, offset);
450 ptr += nbytes;
451 len -= nbytes;
452 sg++;
453 npages++;
454 }
455 return npages;
456
457err:
458 /* Put pages that we hold */
459 for (sg = sgl; sg != &sgl[npages]; sg++)
460 put_page(sg_page(sg));
461 return ret;
462}
463
464static int vhost_scsi_map_iov_to_sgl(struct tcm_vhost_cmd *tv_cmd,
465 struct iovec *iov, unsigned int niov, int write)
466{
467 int ret;
468 unsigned int i;
469 u32 sgl_count;
470 struct scatterlist *sg;
471
472 /*
473 * Find out how long sglist needs to be
474 */
475 sgl_count = 0;
476 for (i = 0; i < niov; i++) {
477 sgl_count += (((uintptr_t)iov[i].iov_base + iov[i].iov_len +
478 PAGE_SIZE - 1) >> PAGE_SHIFT) -
479 ((uintptr_t)iov[i].iov_base >> PAGE_SHIFT);
480 }
481 /* TODO overflow checking */
482
483 sg = kmalloc(sizeof(tv_cmd->tvc_sgl[0]) * sgl_count, GFP_ATOMIC);
484 if (!sg)
485 return -ENOMEM;
Fengguang Wuf0e0e9b2012-07-30 13:19:07 -0700486 pr_debug("%s sg %p sgl_count %u is_err %d\n", __func__,
487 sg, sgl_count, !sg);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700488 sg_init_table(sg, sgl_count);
489
490 tv_cmd->tvc_sgl = sg;
491 tv_cmd->tvc_sgl_count = sgl_count;
492
493 pr_debug("Mapping %u iovecs for %u pages\n", niov, sgl_count);
494 for (i = 0; i < niov; i++) {
495 ret = vhost_scsi_map_to_sgl(sg, sgl_count, iov[i].iov_base,
496 iov[i].iov_len, write);
497 if (ret < 0) {
498 for (i = 0; i < tv_cmd->tvc_sgl_count; i++)
499 put_page(sg_page(&tv_cmd->tvc_sgl[i]));
500 kfree(tv_cmd->tvc_sgl);
501 tv_cmd->tvc_sgl = NULL;
502 tv_cmd->tvc_sgl_count = 0;
503 return ret;
504 }
505
506 sg += ret;
507 sgl_count -= ret;
508 }
509 return 0;
510}
511
512static void tcm_vhost_submission_work(struct work_struct *work)
513{
514 struct tcm_vhost_cmd *tv_cmd =
515 container_of(work, struct tcm_vhost_cmd, work);
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700516 struct tcm_vhost_nexus *tv_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700517 struct se_cmd *se_cmd = &tv_cmd->tvc_se_cmd;
518 struct scatterlist *sg_ptr, *sg_bidi_ptr = NULL;
519 int rc, sg_no_bidi = 0;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700520
521 if (tv_cmd->tvc_sgl_count) {
522 sg_ptr = tv_cmd->tvc_sgl;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700523/* FIXME: Fix BIDI operation in tcm_vhost_submission_work() */
524#if 0
525 if (se_cmd->se_cmd_flags & SCF_BIDI) {
526 sg_bidi_ptr = NULL;
527 sg_no_bidi = 0;
528 }
529#endif
530 } else {
531 sg_ptr = NULL;
532 }
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700533 tv_nexus = tv_cmd->tvc_nexus;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700534
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700535 rc = target_submit_cmd_map_sgls(se_cmd, tv_nexus->tvn_se_sess,
536 tv_cmd->tvc_cdb, &tv_cmd->tvc_sense_buf[0],
537 tv_cmd->tvc_lun, tv_cmd->tvc_exp_data_len,
538 tv_cmd->tvc_task_attr, tv_cmd->tvc_data_direction,
539 0, sg_ptr, tv_cmd->tvc_sgl_count,
540 sg_bidi_ptr, sg_no_bidi);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700541 if (rc < 0) {
542 transport_send_check_condition_and_sense(se_cmd,
Nicholas Bellinger9f0abc12012-10-01 18:40:55 -0700543 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700544 transport_generic_free_cmd(se_cmd, 0);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700545 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700546}
547
548static void vhost_scsi_handle_vq(struct vhost_scsi *vs)
549{
550 struct vhost_virtqueue *vq = &vs->vqs[2];
551 struct virtio_scsi_cmd_req v_req;
552 struct tcm_vhost_tpg *tv_tpg;
553 struct tcm_vhost_cmd *tv_cmd;
554 u32 exp_data_len, data_first, data_num, data_direction;
555 unsigned out, in, i;
556 int head, ret;
557
558 /* Must use ioctl VHOST_SCSI_SET_ENDPOINT */
559 tv_tpg = vs->vs_tpg;
Michael S. Tsirkin71f1e452013-01-31 13:12:29 +0200560 if (unlikely(!tv_tpg))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700561 return;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700562
563 mutex_lock(&vq->mutex);
564 vhost_disable_notify(&vs->dev, vq);
565
566 for (;;) {
567 head = vhost_get_vq_desc(&vs->dev, vq, vq->iov,
568 ARRAY_SIZE(vq->iov), &out, &in,
569 NULL, NULL);
570 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n",
571 head, out, in);
572 /* On error, stop handling until the next kick. */
573 if (unlikely(head < 0))
574 break;
575 /* Nothing new? Wait for eventfd to tell us they refilled. */
576 if (head == vq->num) {
577 if (unlikely(vhost_enable_notify(&vs->dev, vq))) {
578 vhost_disable_notify(&vs->dev, vq);
579 continue;
580 }
581 break;
582 }
583
584/* FIXME: BIDI operation */
585 if (out == 1 && in == 1) {
586 data_direction = DMA_NONE;
587 data_first = 0;
588 data_num = 0;
589 } else if (out == 1 && in > 1) {
590 data_direction = DMA_FROM_DEVICE;
591 data_first = out + 1;
592 data_num = in - 1;
593 } else if (out > 1 && in == 1) {
594 data_direction = DMA_TO_DEVICE;
595 data_first = 1;
596 data_num = out - 1;
597 } else {
598 vq_err(vq, "Invalid buffer layout out: %u in: %u\n",
599 out, in);
600 break;
601 }
602
603 /*
604 * Check for a sane resp buffer so we can report errors to
605 * the guest.
606 */
607 if (unlikely(vq->iov[out].iov_len !=
608 sizeof(struct virtio_scsi_cmd_resp))) {
609 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
610 " bytes\n", vq->iov[out].iov_len);
611 break;
612 }
613
614 if (unlikely(vq->iov[0].iov_len != sizeof(v_req))) {
615 vq_err(vq, "Expecting virtio_scsi_cmd_req, got %zu"
616 " bytes\n", vq->iov[0].iov_len);
617 break;
618 }
619 pr_debug("Calling __copy_from_user: vq->iov[0].iov_base: %p,"
620 " len: %zu\n", vq->iov[0].iov_base, sizeof(v_req));
621 ret = __copy_from_user(&v_req, vq->iov[0].iov_base,
622 sizeof(v_req));
623 if (unlikely(ret)) {
624 vq_err(vq, "Faulted on virtio_scsi_cmd_req\n");
625 break;
626 }
627
628 exp_data_len = 0;
629 for (i = 0; i < data_num; i++)
630 exp_data_len += vq->iov[data_first + i].iov_len;
631
632 tv_cmd = vhost_scsi_allocate_cmd(tv_tpg, &v_req,
633 exp_data_len, data_direction);
634 if (IS_ERR(tv_cmd)) {
635 vq_err(vq, "vhost_scsi_allocate_cmd failed %ld\n",
636 PTR_ERR(tv_cmd));
637 break;
638 }
639 pr_debug("Allocated tv_cmd: %p exp_data_len: %d, data_direction"
640 ": %d\n", tv_cmd, exp_data_len, data_direction);
641
642 tv_cmd->tvc_vhost = vs;
643
644 if (unlikely(vq->iov[out].iov_len !=
645 sizeof(struct virtio_scsi_cmd_resp))) {
646 vq_err(vq, "Expecting virtio_scsi_cmd_resp, got %zu"
647 " bytes, out: %d, in: %d\n",
648 vq->iov[out].iov_len, out, in);
649 break;
650 }
651
652 tv_cmd->tvc_resp = vq->iov[out].iov_base;
653
654 /*
655 * Copy in the recieved CDB descriptor into tv_cmd->tvc_cdb
656 * that will be used by tcm_vhost_new_cmd_map() and down into
657 * target_setup_cmd_from_cdb()
658 */
659 memcpy(tv_cmd->tvc_cdb, v_req.cdb, TCM_VHOST_MAX_CDB_SIZE);
660 /*
661 * Check that the recieved CDB size does not exceeded our
662 * hardcoded max for tcm_vhost
663 */
664 /* TODO what if cdb was too small for varlen cdb header? */
665 if (unlikely(scsi_command_size(tv_cmd->tvc_cdb) >
666 TCM_VHOST_MAX_CDB_SIZE)) {
667 vq_err(vq, "Received SCSI CDB with command_size: %d that"
668 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
669 scsi_command_size(tv_cmd->tvc_cdb),
670 TCM_VHOST_MAX_CDB_SIZE);
671 break; /* TODO */
672 }
673 tv_cmd->tvc_lun = ((v_req.lun[2] << 8) | v_req.lun[3]) & 0x3FFF;
674
675 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n",
676 tv_cmd->tvc_cdb[0], tv_cmd->tvc_lun);
677
678 if (data_direction != DMA_NONE) {
679 ret = vhost_scsi_map_iov_to_sgl(tv_cmd,
680 &vq->iov[data_first], data_num,
681 data_direction == DMA_TO_DEVICE);
682 if (unlikely(ret)) {
683 vq_err(vq, "Failed to map iov to sgl\n");
684 break; /* TODO */
685 }
686 }
687
688 /*
689 * Save the descriptor from vhost_get_vq_desc() to be used to
690 * complete the virtio-scsi request in TCM callback context via
691 * tcm_vhost_queue_data_in() and tcm_vhost_queue_status()
692 */
693 tv_cmd->tvc_vq_desc = head;
694 /*
695 * Dispatch tv_cmd descriptor for cmwq execution in process
696 * context provided by tcm_vhost_workqueue. This also ensures
697 * tv_cmd is executed on the same kworker CPU as this vhost
698 * thread to gain positive L2 cache locality effects..
699 */
700 INIT_WORK(&tv_cmd->work, tcm_vhost_submission_work);
701 queue_work(tcm_vhost_workqueue, &tv_cmd->work);
702 }
703
704 mutex_unlock(&vq->mutex);
705}
706
707static void vhost_scsi_ctl_handle_kick(struct vhost_work *work)
708{
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700709 pr_debug("%s: The handling func for control queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700710}
711
712static void vhost_scsi_evt_handle_kick(struct vhost_work *work)
713{
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700714 pr_debug("%s: The handling func for event queue.\n", __func__);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700715}
716
717static void vhost_scsi_handle_kick(struct vhost_work *work)
718{
719 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
720 poll.work);
721 struct vhost_scsi *vs = container_of(vq->dev, struct vhost_scsi, dev);
722
723 vhost_scsi_handle_vq(vs);
724}
725
726/*
727 * Called from vhost_scsi_ioctl() context to walk the list of available
728 * tcm_vhost_tpg with an active struct tcm_vhost_nexus
729 */
730static int vhost_scsi_set_endpoint(
731 struct vhost_scsi *vs,
732 struct vhost_scsi_target *t)
733{
734 struct tcm_vhost_tport *tv_tport;
735 struct tcm_vhost_tpg *tv_tpg;
736 int index;
737
738 mutex_lock(&vs->dev.mutex);
739 /* Verify that ring has been setup correctly. */
740 for (index = 0; index < vs->dev.nvqs; ++index) {
741 /* Verify that ring has been setup correctly. */
742 if (!vhost_vq_access_ok(&vs->vqs[index])) {
743 mutex_unlock(&vs->dev.mutex);
744 return -EFAULT;
745 }
746 }
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700747 mutex_unlock(&vs->dev.mutex);
748
749 mutex_lock(&tcm_vhost_mutex);
750 list_for_each_entry(tv_tpg, &tcm_vhost_list, tv_tpg_list) {
751 mutex_lock(&tv_tpg->tv_tpg_mutex);
752 if (!tv_tpg->tpg_nexus) {
753 mutex_unlock(&tv_tpg->tv_tpg_mutex);
754 continue;
755 }
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700756 if (tv_tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700757 mutex_unlock(&tv_tpg->tv_tpg_mutex);
758 continue;
759 }
760 tv_tport = tv_tpg->tport;
761
762 if (!strcmp(tv_tport->tport_name, t->vhost_wwpn) &&
763 (tv_tpg->tport_tpgt == t->vhost_tpgt)) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700764 tv_tpg->tv_tpg_vhost_count++;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700765 mutex_unlock(&tv_tpg->tv_tpg_mutex);
766 mutex_unlock(&tcm_vhost_mutex);
767
768 mutex_lock(&vs->dev.mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700769 if (vs->vs_tpg) {
770 mutex_unlock(&vs->dev.mutex);
771 mutex_lock(&tv_tpg->tv_tpg_mutex);
772 tv_tpg->tv_tpg_vhost_count--;
773 mutex_unlock(&tv_tpg->tv_tpg_mutex);
774 return -EEXIST;
775 }
776
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700777 vs->vs_tpg = tv_tpg;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700778 smp_mb__after_atomic_inc();
779 mutex_unlock(&vs->dev.mutex);
780 return 0;
781 }
782 mutex_unlock(&tv_tpg->tv_tpg_mutex);
783 }
784 mutex_unlock(&tcm_vhost_mutex);
785 return -EINVAL;
786}
787
788static int vhost_scsi_clear_endpoint(
789 struct vhost_scsi *vs,
790 struct vhost_scsi_target *t)
791{
792 struct tcm_vhost_tport *tv_tport;
793 struct tcm_vhost_tpg *tv_tpg;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700794 int index, ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700795
796 mutex_lock(&vs->dev.mutex);
797 /* Verify that ring has been setup correctly. */
798 for (index = 0; index < vs->dev.nvqs; ++index) {
799 if (!vhost_vq_access_ok(&vs->vqs[index])) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700800 ret = -EFAULT;
801 goto err;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700802 }
803 }
804
805 if (!vs->vs_tpg) {
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700806 ret = -ENODEV;
807 goto err;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700808 }
809 tv_tpg = vs->vs_tpg;
810 tv_tport = tv_tpg->tport;
811
812 if (strcmp(tv_tport->tport_name, t->vhost_wwpn) ||
813 (tv_tpg->tport_tpgt != t->vhost_tpgt)) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700814 pr_warn("tv_tport->tport_name: %s, tv_tpg->tport_tpgt: %hu"
815 " does not match t->vhost_wwpn: %s, t->vhost_tpgt: %hu\n",
816 tv_tport->tport_name, tv_tpg->tport_tpgt,
817 t->vhost_wwpn, t->vhost_tpgt);
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700818 ret = -EINVAL;
819 goto err;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700820 }
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700821 tv_tpg->tv_tpg_vhost_count--;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700822 vs->vs_tpg = NULL;
823 mutex_unlock(&vs->dev.mutex);
824
825 return 0;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700826
827err:
828 mutex_unlock(&vs->dev.mutex);
829 return ret;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700830}
831
832static int vhost_scsi_open(struct inode *inode, struct file *f)
833{
834 struct vhost_scsi *s;
835 int r;
836
837 s = kzalloc(sizeof(*s), GFP_KERNEL);
838 if (!s)
839 return -ENOMEM;
840
841 vhost_work_init(&s->vs_completion_work, vhost_scsi_complete_cmd_work);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700842
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700843 s->vqs[VHOST_SCSI_VQ_CTL].handle_kick = vhost_scsi_ctl_handle_kick;
844 s->vqs[VHOST_SCSI_VQ_EVT].handle_kick = vhost_scsi_evt_handle_kick;
845 s->vqs[VHOST_SCSI_VQ_IO].handle_kick = vhost_scsi_handle_kick;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700846 r = vhost_dev_init(&s->dev, s->vqs, 3);
847 if (r < 0) {
848 kfree(s);
849 return r;
850 }
851
852 f->private_data = s;
853 return 0;
854}
855
856static int vhost_scsi_release(struct inode *inode, struct file *f)
857{
858 struct vhost_scsi *s = f->private_data;
859
860 if (s->vs_tpg && s->vs_tpg->tport) {
861 struct vhost_scsi_target backend;
862
863 memcpy(backend.vhost_wwpn, s->vs_tpg->tport->tport_name,
864 sizeof(backend.vhost_wwpn));
865 backend.vhost_tpgt = s->vs_tpg->tport_tpgt;
866 vhost_scsi_clear_endpoint(s, &backend);
867 }
868
Michael S. Tsirkinb2116162012-11-01 09:16:46 +0000869 vhost_dev_stop(&s->dev);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700870 vhost_dev_cleanup(&s->dev, false);
871 kfree(s);
872 return 0;
873}
874
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700875static void vhost_scsi_flush_vq(struct vhost_scsi *vs, int index)
876{
877 vhost_poll_flush(&vs->dev.vqs[index].poll);
878}
879
880static void vhost_scsi_flush(struct vhost_scsi *vs)
881{
882 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_CTL);
883 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_EVT);
884 vhost_scsi_flush_vq(vs, VHOST_SCSI_VQ_IO);
885}
886
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700887static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features)
888{
889 if (features & ~VHOST_FEATURES)
890 return -EOPNOTSUPP;
891
892 mutex_lock(&vs->dev.mutex);
893 if ((features & (1 << VHOST_F_LOG_ALL)) &&
894 !vhost_log_access_ok(&vs->dev)) {
895 mutex_unlock(&vs->dev.mutex);
896 return -EFAULT;
897 }
898 vs->dev.acked_features = features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700899 smp_wmb();
900 vhost_scsi_flush(vs);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700901 mutex_unlock(&vs->dev.mutex);
902 return 0;
903}
904
905static long vhost_scsi_ioctl(struct file *f, unsigned int ioctl,
906 unsigned long arg)
907{
908 struct vhost_scsi *vs = f->private_data;
909 struct vhost_scsi_target backend;
910 void __user *argp = (void __user *)arg;
911 u64 __user *featurep = argp;
912 u64 features;
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700913 int r, abi_version = VHOST_SCSI_ABI_VERSION;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700914
915 switch (ioctl) {
916 case VHOST_SCSI_SET_ENDPOINT:
917 if (copy_from_user(&backend, argp, sizeof backend))
918 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -0700919 if (backend.reserved != 0)
920 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700921
922 return vhost_scsi_set_endpoint(vs, &backend);
923 case VHOST_SCSI_CLEAR_ENDPOINT:
924 if (copy_from_user(&backend, argp, sizeof backend))
925 return -EFAULT;
Michael S. Tsirkin6de71452012-08-18 15:44:09 -0700926 if (backend.reserved != 0)
927 return -EOPNOTSUPP;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700928
929 return vhost_scsi_clear_endpoint(vs, &backend);
930 case VHOST_SCSI_GET_ABI_VERSION:
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700931 if (copy_to_user(argp, &abi_version, sizeof abi_version))
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700932 return -EFAULT;
933 return 0;
934 case VHOST_GET_FEATURES:
935 features = VHOST_FEATURES;
936 if (copy_to_user(featurep, &features, sizeof features))
937 return -EFAULT;
938 return 0;
939 case VHOST_SET_FEATURES:
940 if (copy_from_user(&features, featurep, sizeof features))
941 return -EFAULT;
942 return vhost_scsi_set_features(vs, features);
943 default:
944 mutex_lock(&vs->dev.mutex);
Michael S. Tsirkin935cdee2012-12-06 14:03:34 +0200945 r = vhost_dev_ioctl(&vs->dev, ioctl, argp);
946 /* TODO: flush backend after dev ioctl. */
947 if (r == -ENOIOCTLCMD)
948 r = vhost_vring_ioctl(&vs->dev, ioctl, argp);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700949 mutex_unlock(&vs->dev.mutex);
950 return r;
951 }
952}
953
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700954#ifdef CONFIG_COMPAT
955static long vhost_scsi_compat_ioctl(struct file *f, unsigned int ioctl,
956 unsigned long arg)
957{
958 return vhost_scsi_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
959}
960#endif
961
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700962static const struct file_operations vhost_scsi_fops = {
963 .owner = THIS_MODULE,
964 .release = vhost_scsi_release,
965 .unlocked_ioctl = vhost_scsi_ioctl,
Nicholas Bellinger101998f2012-07-30 13:30:00 -0700966#ifdef CONFIG_COMPAT
967 .compat_ioctl = vhost_scsi_compat_ioctl,
968#endif
Nicholas Bellinger057cbf42012-07-18 14:31:32 -0700969 .open = vhost_scsi_open,
970 .llseek = noop_llseek,
971};
972
973static struct miscdevice vhost_scsi_misc = {
974 MISC_DYNAMIC_MINOR,
975 "vhost-scsi",
976 &vhost_scsi_fops,
977};
978
979static int __init vhost_scsi_register(void)
980{
981 return misc_register(&vhost_scsi_misc);
982}
983
984static int vhost_scsi_deregister(void)
985{
986 return misc_deregister(&vhost_scsi_misc);
987}
988
989static char *tcm_vhost_dump_proto_id(struct tcm_vhost_tport *tport)
990{
991 switch (tport->tport_proto_id) {
992 case SCSI_PROTOCOL_SAS:
993 return "SAS";
994 case SCSI_PROTOCOL_FCP:
995 return "FCP";
996 case SCSI_PROTOCOL_ISCSI:
997 return "iSCSI";
998 default:
999 break;
1000 }
1001
1002 return "Unknown";
1003}
1004
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001005static int tcm_vhost_port_link(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001006 struct se_lun *lun)
1007{
1008 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1009 struct tcm_vhost_tpg, se_tpg);
1010
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001011 mutex_lock(&tv_tpg->tv_tpg_mutex);
1012 tv_tpg->tv_tpg_port_count++;
1013 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001014
1015 return 0;
1016}
1017
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001018static void tcm_vhost_port_unlink(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001019 struct se_lun *se_lun)
1020{
1021 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1022 struct tcm_vhost_tpg, se_tpg);
1023
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001024 mutex_lock(&tv_tpg->tv_tpg_mutex);
1025 tv_tpg->tv_tpg_port_count--;
1026 mutex_unlock(&tv_tpg->tv_tpg_mutex);
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001027}
1028
1029static struct se_node_acl *tcm_vhost_make_nodeacl(
1030 struct se_portal_group *se_tpg,
1031 struct config_group *group,
1032 const char *name)
1033{
1034 struct se_node_acl *se_nacl, *se_nacl_new;
1035 struct tcm_vhost_nacl *nacl;
1036 u64 wwpn = 0;
1037 u32 nexus_depth;
1038
1039 /* tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1040 return ERR_PTR(-EINVAL); */
1041 se_nacl_new = tcm_vhost_alloc_fabric_acl(se_tpg);
1042 if (!se_nacl_new)
1043 return ERR_PTR(-ENOMEM);
1044
1045 nexus_depth = 1;
1046 /*
1047 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
1048 * when converting a NodeACL from demo mode -> explict
1049 */
1050 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
1051 name, nexus_depth);
1052 if (IS_ERR(se_nacl)) {
1053 tcm_vhost_release_fabric_acl(se_tpg, se_nacl_new);
1054 return se_nacl;
1055 }
1056 /*
1057 * Locate our struct tcm_vhost_nacl and set the FC Nport WWPN
1058 */
1059 nacl = container_of(se_nacl, struct tcm_vhost_nacl, se_node_acl);
1060 nacl->iport_wwpn = wwpn;
1061
1062 return se_nacl;
1063}
1064
1065static void tcm_vhost_drop_nodeacl(struct se_node_acl *se_acl)
1066{
1067 struct tcm_vhost_nacl *nacl = container_of(se_acl,
1068 struct tcm_vhost_nacl, se_node_acl);
1069 core_tpg_del_initiator_node_acl(se_acl->se_tpg, se_acl, 1);
1070 kfree(nacl);
1071}
1072
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001073static int tcm_vhost_make_nexus(struct tcm_vhost_tpg *tv_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001074 const char *name)
1075{
1076 struct se_portal_group *se_tpg;
1077 struct tcm_vhost_nexus *tv_nexus;
1078
1079 mutex_lock(&tv_tpg->tv_tpg_mutex);
1080 if (tv_tpg->tpg_nexus) {
1081 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1082 pr_debug("tv_tpg->tpg_nexus already exists\n");
1083 return -EEXIST;
1084 }
1085 se_tpg = &tv_tpg->se_tpg;
1086
1087 tv_nexus = kzalloc(sizeof(struct tcm_vhost_nexus), GFP_KERNEL);
1088 if (!tv_nexus) {
1089 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1090 pr_err("Unable to allocate struct tcm_vhost_nexus\n");
1091 return -ENOMEM;
1092 }
1093 /*
1094 * Initialize the struct se_session pointer
1095 */
1096 tv_nexus->tvn_se_sess = transport_init_session();
1097 if (IS_ERR(tv_nexus->tvn_se_sess)) {
1098 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1099 kfree(tv_nexus);
1100 return -ENOMEM;
1101 }
1102 /*
1103 * Since we are running in 'demo mode' this call with generate a
1104 * struct se_node_acl for the tcm_vhost struct se_portal_group with
1105 * the SCSI Initiator port name of the passed configfs group 'name'.
1106 */
1107 tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1108 se_tpg, (unsigned char *)name);
1109 if (!tv_nexus->tvn_se_sess->se_node_acl) {
1110 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1111 pr_debug("core_tpg_check_initiator_node_acl() failed"
1112 " for %s\n", name);
1113 transport_free_session(tv_nexus->tvn_se_sess);
1114 kfree(tv_nexus);
1115 return -ENOMEM;
1116 }
1117 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001118 * Now register the TCM vhost virtual I_T Nexus as active with the
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001119 * call to __transport_register_session()
1120 */
1121 __transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
1122 tv_nexus->tvn_se_sess, tv_nexus);
1123 tv_tpg->tpg_nexus = tv_nexus;
1124
1125 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1126 return 0;
1127}
1128
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001129static int tcm_vhost_drop_nexus(struct tcm_vhost_tpg *tpg)
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001130{
1131 struct se_session *se_sess;
1132 struct tcm_vhost_nexus *tv_nexus;
1133
1134 mutex_lock(&tpg->tv_tpg_mutex);
1135 tv_nexus = tpg->tpg_nexus;
1136 if (!tv_nexus) {
1137 mutex_unlock(&tpg->tv_tpg_mutex);
1138 return -ENODEV;
1139 }
1140
1141 se_sess = tv_nexus->tvn_se_sess;
1142 if (!se_sess) {
1143 mutex_unlock(&tpg->tv_tpg_mutex);
1144 return -ENODEV;
1145 }
1146
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001147 if (tpg->tv_tpg_port_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001148 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001149 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001150 " active TPG port count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001151 tpg->tv_tpg_port_count);
1152 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001153 }
1154
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001155 if (tpg->tv_tpg_vhost_count != 0) {
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001156 mutex_unlock(&tpg->tv_tpg_mutex);
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001157 pr_err("Unable to remove TCM_vhost I_T Nexus with"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001158 " active TPG vhost count: %d\n",
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001159 tpg->tv_tpg_vhost_count);
1160 return -EBUSY;
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001161 }
1162
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001163 pr_debug("TCM_vhost_ConfigFS: Removing I_T Nexus to emulated"
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001164 " %s Initiator Port: %s\n", tcm_vhost_dump_proto_id(tpg->tport),
1165 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1166 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001167 * Release the SCSI I_T Nexus to the emulated vhost Target Port
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001168 */
1169 transport_deregister_session(tv_nexus->tvn_se_sess);
1170 tpg->tpg_nexus = NULL;
1171 mutex_unlock(&tpg->tv_tpg_mutex);
1172
1173 kfree(tv_nexus);
1174 return 0;
1175}
1176
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001177static ssize_t tcm_vhost_tpg_show_nexus(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001178 char *page)
1179{
1180 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1181 struct tcm_vhost_tpg, se_tpg);
1182 struct tcm_vhost_nexus *tv_nexus;
1183 ssize_t ret;
1184
1185 mutex_lock(&tv_tpg->tv_tpg_mutex);
1186 tv_nexus = tv_tpg->tpg_nexus;
1187 if (!tv_nexus) {
1188 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1189 return -ENODEV;
1190 }
1191 ret = snprintf(page, PAGE_SIZE, "%s\n",
1192 tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
1193 mutex_unlock(&tv_tpg->tv_tpg_mutex);
1194
1195 return ret;
1196}
1197
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001198static ssize_t tcm_vhost_tpg_store_nexus(struct se_portal_group *se_tpg,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001199 const char *page,
1200 size_t count)
1201{
1202 struct tcm_vhost_tpg *tv_tpg = container_of(se_tpg,
1203 struct tcm_vhost_tpg, se_tpg);
1204 struct tcm_vhost_tport *tport_wwn = tv_tpg->tport;
1205 unsigned char i_port[TCM_VHOST_NAMELEN], *ptr, *port_ptr;
1206 int ret;
1207 /*
1208 * Shutdown the active I_T nexus if 'NULL' is passed..
1209 */
1210 if (!strncmp(page, "NULL", 4)) {
1211 ret = tcm_vhost_drop_nexus(tv_tpg);
1212 return (!ret) ? count : ret;
1213 }
1214 /*
1215 * Otherwise make sure the passed virtual Initiator port WWN matches
1216 * the fabric protocol_id set in tcm_vhost_make_tport(), and call
1217 * tcm_vhost_make_nexus().
1218 */
1219 if (strlen(page) >= TCM_VHOST_NAMELEN) {
1220 pr_err("Emulated NAA Sas Address: %s, exceeds"
1221 " max: %d\n", page, TCM_VHOST_NAMELEN);
1222 return -EINVAL;
1223 }
1224 snprintf(&i_port[0], TCM_VHOST_NAMELEN, "%s", page);
1225
1226 ptr = strstr(i_port, "naa.");
1227 if (ptr) {
1228 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_SAS) {
1229 pr_err("Passed SAS Initiator Port %s does not"
1230 " match target port protoid: %s\n", i_port,
1231 tcm_vhost_dump_proto_id(tport_wwn));
1232 return -EINVAL;
1233 }
1234 port_ptr = &i_port[0];
1235 goto check_newline;
1236 }
1237 ptr = strstr(i_port, "fc.");
1238 if (ptr) {
1239 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_FCP) {
1240 pr_err("Passed FCP Initiator Port %s does not"
1241 " match target port protoid: %s\n", i_port,
1242 tcm_vhost_dump_proto_id(tport_wwn));
1243 return -EINVAL;
1244 }
1245 port_ptr = &i_port[3]; /* Skip over "fc." */
1246 goto check_newline;
1247 }
1248 ptr = strstr(i_port, "iqn.");
1249 if (ptr) {
1250 if (tport_wwn->tport_proto_id != SCSI_PROTOCOL_ISCSI) {
1251 pr_err("Passed iSCSI Initiator Port %s does not"
1252 " match target port protoid: %s\n", i_port,
1253 tcm_vhost_dump_proto_id(tport_wwn));
1254 return -EINVAL;
1255 }
1256 port_ptr = &i_port[0];
1257 goto check_newline;
1258 }
1259 pr_err("Unable to locate prefix for emulated Initiator Port:"
1260 " %s\n", i_port);
1261 return -EINVAL;
1262 /*
1263 * Clear any trailing newline for the NAA WWN
1264 */
1265check_newline:
1266 if (i_port[strlen(i_port)-1] == '\n')
1267 i_port[strlen(i_port)-1] = '\0';
1268
1269 ret = tcm_vhost_make_nexus(tv_tpg, port_ptr);
1270 if (ret < 0)
1271 return ret;
1272
1273 return count;
1274}
1275
1276TF_TPG_BASE_ATTR(tcm_vhost, nexus, S_IRUGO | S_IWUSR);
1277
1278static struct configfs_attribute *tcm_vhost_tpg_attrs[] = {
1279 &tcm_vhost_tpg_nexus.attr,
1280 NULL,
1281};
1282
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001283static struct se_portal_group *tcm_vhost_make_tpg(struct se_wwn *wwn,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001284 struct config_group *group,
1285 const char *name)
1286{
1287 struct tcm_vhost_tport *tport = container_of(wwn,
1288 struct tcm_vhost_tport, tport_wwn);
1289
1290 struct tcm_vhost_tpg *tpg;
1291 unsigned long tpgt;
1292 int ret;
1293
1294 if (strstr(name, "tpgt_") != name)
1295 return ERR_PTR(-EINVAL);
1296 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
1297 return ERR_PTR(-EINVAL);
1298
1299 tpg = kzalloc(sizeof(struct tcm_vhost_tpg), GFP_KERNEL);
1300 if (!tpg) {
1301 pr_err("Unable to allocate struct tcm_vhost_tpg");
1302 return ERR_PTR(-ENOMEM);
1303 }
1304 mutex_init(&tpg->tv_tpg_mutex);
1305 INIT_LIST_HEAD(&tpg->tv_tpg_list);
1306 tpg->tport = tport;
1307 tpg->tport_tpgt = tpgt;
1308
1309 ret = core_tpg_register(&tcm_vhost_fabric_configfs->tf_ops, wwn,
1310 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1311 if (ret < 0) {
1312 kfree(tpg);
1313 return NULL;
1314 }
1315 mutex_lock(&tcm_vhost_mutex);
1316 list_add_tail(&tpg->tv_tpg_list, &tcm_vhost_list);
1317 mutex_unlock(&tcm_vhost_mutex);
1318
1319 return &tpg->se_tpg;
1320}
1321
1322static void tcm_vhost_drop_tpg(struct se_portal_group *se_tpg)
1323{
1324 struct tcm_vhost_tpg *tpg = container_of(se_tpg,
1325 struct tcm_vhost_tpg, se_tpg);
1326
1327 mutex_lock(&tcm_vhost_mutex);
1328 list_del(&tpg->tv_tpg_list);
1329 mutex_unlock(&tcm_vhost_mutex);
1330 /*
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001331 * Release the virtual I_T Nexus for this vhost TPG
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001332 */
1333 tcm_vhost_drop_nexus(tpg);
1334 /*
1335 * Deregister the se_tpg from TCM..
1336 */
1337 core_tpg_deregister(se_tpg);
1338 kfree(tpg);
1339}
1340
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001341static struct se_wwn *tcm_vhost_make_tport(struct target_fabric_configfs *tf,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001342 struct config_group *group,
1343 const char *name)
1344{
1345 struct tcm_vhost_tport *tport;
1346 char *ptr;
1347 u64 wwpn = 0;
1348 int off = 0;
1349
1350 /* if (tcm_vhost_parse_wwn(name, &wwpn, 1) < 0)
1351 return ERR_PTR(-EINVAL); */
1352
1353 tport = kzalloc(sizeof(struct tcm_vhost_tport), GFP_KERNEL);
1354 if (!tport) {
1355 pr_err("Unable to allocate struct tcm_vhost_tport");
1356 return ERR_PTR(-ENOMEM);
1357 }
1358 tport->tport_wwpn = wwpn;
1359 /*
1360 * Determine the emulated Protocol Identifier and Target Port Name
1361 * based on the incoming configfs directory name.
1362 */
1363 ptr = strstr(name, "naa.");
1364 if (ptr) {
1365 tport->tport_proto_id = SCSI_PROTOCOL_SAS;
1366 goto check_len;
1367 }
1368 ptr = strstr(name, "fc.");
1369 if (ptr) {
1370 tport->tport_proto_id = SCSI_PROTOCOL_FCP;
1371 off = 3; /* Skip over "fc." */
1372 goto check_len;
1373 }
1374 ptr = strstr(name, "iqn.");
1375 if (ptr) {
1376 tport->tport_proto_id = SCSI_PROTOCOL_ISCSI;
1377 goto check_len;
1378 }
1379
1380 pr_err("Unable to locate prefix for emulated Target Port:"
1381 " %s\n", name);
1382 kfree(tport);
1383 return ERR_PTR(-EINVAL);
1384
1385check_len:
1386 if (strlen(name) >= TCM_VHOST_NAMELEN) {
1387 pr_err("Emulated %s Address: %s, exceeds"
1388 " max: %d\n", name, tcm_vhost_dump_proto_id(tport),
1389 TCM_VHOST_NAMELEN);
1390 kfree(tport);
1391 return ERR_PTR(-EINVAL);
1392 }
1393 snprintf(&tport->tport_name[0], TCM_VHOST_NAMELEN, "%s", &name[off]);
1394
1395 pr_debug("TCM_VHost_ConfigFS: Allocated emulated Target"
1396 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport), name);
1397
1398 return &tport->tport_wwn;
1399}
1400
1401static void tcm_vhost_drop_tport(struct se_wwn *wwn)
1402{
1403 struct tcm_vhost_tport *tport = container_of(wwn,
1404 struct tcm_vhost_tport, tport_wwn);
1405
1406 pr_debug("TCM_VHost_ConfigFS: Deallocating emulated Target"
1407 " %s Address: %s\n", tcm_vhost_dump_proto_id(tport),
1408 tport->tport_name);
1409
1410 kfree(tport);
1411}
1412
1413static ssize_t tcm_vhost_wwn_show_attr_version(
1414 struct target_fabric_configfs *tf,
1415 char *page)
1416{
1417 return sprintf(page, "TCM_VHOST fabric module %s on %s/%s"
1418 "on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1419 utsname()->machine);
1420}
1421
1422TF_WWN_ATTR_RO(tcm_vhost, version);
1423
1424static struct configfs_attribute *tcm_vhost_wwn_attrs[] = {
1425 &tcm_vhost_wwn_version.attr,
1426 NULL,
1427};
1428
1429static struct target_core_fabric_ops tcm_vhost_ops = {
1430 .get_fabric_name = tcm_vhost_get_fabric_name,
1431 .get_fabric_proto_ident = tcm_vhost_get_fabric_proto_ident,
1432 .tpg_get_wwn = tcm_vhost_get_fabric_wwn,
1433 .tpg_get_tag = tcm_vhost_get_tag,
1434 .tpg_get_default_depth = tcm_vhost_get_default_depth,
1435 .tpg_get_pr_transport_id = tcm_vhost_get_pr_transport_id,
1436 .tpg_get_pr_transport_id_len = tcm_vhost_get_pr_transport_id_len,
1437 .tpg_parse_pr_out_transport_id = tcm_vhost_parse_pr_out_transport_id,
1438 .tpg_check_demo_mode = tcm_vhost_check_true,
1439 .tpg_check_demo_mode_cache = tcm_vhost_check_true,
1440 .tpg_check_demo_mode_write_protect = tcm_vhost_check_false,
1441 .tpg_check_prod_mode_write_protect = tcm_vhost_check_false,
1442 .tpg_alloc_fabric_acl = tcm_vhost_alloc_fabric_acl,
1443 .tpg_release_fabric_acl = tcm_vhost_release_fabric_acl,
1444 .tpg_get_inst_index = tcm_vhost_tpg_get_inst_index,
1445 .release_cmd = tcm_vhost_release_cmd,
1446 .shutdown_session = tcm_vhost_shutdown_session,
1447 .close_session = tcm_vhost_close_session,
1448 .sess_get_index = tcm_vhost_sess_get_index,
1449 .sess_get_initiator_sid = NULL,
1450 .write_pending = tcm_vhost_write_pending,
1451 .write_pending_status = tcm_vhost_write_pending_status,
1452 .set_default_node_attributes = tcm_vhost_set_default_node_attrs,
1453 .get_task_tag = tcm_vhost_get_task_tag,
1454 .get_cmd_state = tcm_vhost_get_cmd_state,
1455 .queue_data_in = tcm_vhost_queue_data_in,
1456 .queue_status = tcm_vhost_queue_status,
1457 .queue_tm_rsp = tcm_vhost_queue_tm_rsp,
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001458 /*
1459 * Setup callers for generic logic in target_core_fabric_configfs.c
1460 */
1461 .fabric_make_wwn = tcm_vhost_make_tport,
1462 .fabric_drop_wwn = tcm_vhost_drop_tport,
1463 .fabric_make_tpg = tcm_vhost_make_tpg,
1464 .fabric_drop_tpg = tcm_vhost_drop_tpg,
1465 .fabric_post_link = tcm_vhost_port_link,
1466 .fabric_pre_unlink = tcm_vhost_port_unlink,
1467 .fabric_make_np = NULL,
1468 .fabric_drop_np = NULL,
1469 .fabric_make_nodeacl = tcm_vhost_make_nodeacl,
1470 .fabric_drop_nodeacl = tcm_vhost_drop_nodeacl,
1471};
1472
1473static int tcm_vhost_register_configfs(void)
1474{
1475 struct target_fabric_configfs *fabric;
1476 int ret;
1477
1478 pr_debug("TCM_VHOST fabric module %s on %s/%s"
1479 " on "UTS_RELEASE"\n", TCM_VHOST_VERSION, utsname()->sysname,
1480 utsname()->machine);
1481 /*
1482 * Register the top level struct config_item_type with TCM core
1483 */
1484 fabric = target_fabric_configfs_init(THIS_MODULE, "vhost");
1485 if (IS_ERR(fabric)) {
1486 pr_err("target_fabric_configfs_init() failed\n");
1487 return PTR_ERR(fabric);
1488 }
1489 /*
1490 * Setup fabric->tf_ops from our local tcm_vhost_ops
1491 */
1492 fabric->tf_ops = tcm_vhost_ops;
1493 /*
1494 * Setup default attribute lists for various fabric->tf_cit_tmpl
1495 */
1496 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_vhost_wwn_attrs;
1497 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_vhost_tpg_attrs;
1498 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1499 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1500 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1501 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1502 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1503 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1504 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1505 /*
1506 * Register the fabric for use within TCM
1507 */
1508 ret = target_fabric_configfs_register(fabric);
1509 if (ret < 0) {
1510 pr_err("target_fabric_configfs_register() failed"
1511 " for TCM_VHOST\n");
1512 return ret;
1513 }
1514 /*
1515 * Setup our local pointer to *fabric
1516 */
1517 tcm_vhost_fabric_configfs = fabric;
1518 pr_debug("TCM_VHOST[0] - Set fabric -> tcm_vhost_fabric_configfs\n");
1519 return 0;
1520};
1521
1522static void tcm_vhost_deregister_configfs(void)
1523{
1524 if (!tcm_vhost_fabric_configfs)
1525 return;
1526
1527 target_fabric_configfs_deregister(tcm_vhost_fabric_configfs);
1528 tcm_vhost_fabric_configfs = NULL;
1529 pr_debug("TCM_VHOST[0] - Cleared tcm_vhost_fabric_configfs\n");
1530};
1531
1532static int __init tcm_vhost_init(void)
1533{
1534 int ret = -ENOMEM;
Nicholas Bellinger101998f2012-07-30 13:30:00 -07001535 /*
1536 * Use our own dedicated workqueue for submitting I/O into
1537 * target core to avoid contention within system_wq.
1538 */
Nicholas Bellinger057cbf42012-07-18 14:31:32 -07001539 tcm_vhost_workqueue = alloc_workqueue("tcm_vhost", 0, 0);
1540 if (!tcm_vhost_workqueue)
1541 goto out;
1542
1543 ret = vhost_scsi_register();
1544 if (ret < 0)
1545 goto out_destroy_workqueue;
1546
1547 ret = tcm_vhost_register_configfs();
1548 if (ret < 0)
1549 goto out_vhost_scsi_deregister;
1550
1551 return 0;
1552
1553out_vhost_scsi_deregister:
1554 vhost_scsi_deregister();
1555out_destroy_workqueue:
1556 destroy_workqueue(tcm_vhost_workqueue);
1557out:
1558 return ret;
1559};
1560
1561static void tcm_vhost_exit(void)
1562{
1563 tcm_vhost_deregister_configfs();
1564 vhost_scsi_deregister();
1565 destroy_workqueue(tcm_vhost_workqueue);
1566};
1567
1568MODULE_DESCRIPTION("TCM_VHOST series fabric driver");
1569MODULE_LICENSE("GPL");
1570module_init(tcm_vhost_init);
1571module_exit(tcm_vhost_exit);