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